shell.c
20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#ifndef WIN32
#include <unistd.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "ultrahost.h"
#include "PR/bbfs.h"
#include "PR/R4300.h"
#include "PR/bcp.h"
#else
#include <winsock2.h>
#include <io.h>
#include <sys/stat.h>
#include "bbfs.h"
#include "ultrahost.h"
#define R_OK 4
#define PHYS_TO_K1(x) ((x) | 0xA0000000)
#define BOOT_RAM_LO_START 0x1fc00000
#endif
#ifdef USE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
#include "mon.h"
/*
* send commands to remote monitor and receive responses
* basic flow is:
* send 2-word request <command, length of data>
* send data
* receive 2-word response <response is 255-cmd, length of data>
* receive data if any
*/
static int fd = 255;
static unsigned char block[BB_FL_BLOCK_SIZE];
static unsigned char blist[BB_FL_BLOCK_SIZE];
static BbFat16 fat[2]; /* maximum 128MB flash */
static int blocks;
#define SK_BLOCKS 4
#define SL_BLOCKS 1
#define SA_BLOCKS (BB_FL_BYTE_TO_BLOCK(BB_SYSTEM_AREA_SIZE)-2*SL_BLOCKS-SK_BLOCKS)
#define SYS_BLOCKS (SK_BLOCKS + 2*SL_BLOCKS + SA_BLOCKS)
#define SK_OFF 0 /* first sk block */
#define SL0_OFF SK_BLOCKS /* sys license0 block */
#define SL1_OFF (SL0_OFF+SL_BLOCKS) /* sys license1 block */
#define SA0_OFF (SL1_OFF+SL_BLOCKS) /* first sys app0 block */
/*XXXblythe round up transfers to a multiple of 4 for irix */
#define RND(x) (((x)+3)&~3)
static int sblocks[SYS_BLOCKS];
static const char*
basename(const char* s) {
char* p;
if ((p = strrchr(s, '/')) && p[1]) return p+1;
return s;;
}
void
sum(const unsigned char* p, int n) {
int i;
unsigned short s = 0;
for(i = 0; i < n; i++)
s += p[i];
printf("sum %04x\n", s);
}
static int
send_cmd(const void* buf, int len) {
int rv, i;
for(i = 0; i < len/4; i++)
*((unsigned int*)buf+i) = htonl(*((unsigned int*)buf+i));
if ((rv = uhWriteGame(fd, (void*)buf, len)) == -1) {
perror("uhWriteGame");
exit(1);
}
return rv;
}
static int
read_rsp(void* buf, int len) {
int rv, i;
if ((rv = uhReadGame(fd, buf, len)) == -1) {
perror("uhReadGame");
exit(1);
}
for(i = 0; i < len/4; i++)
*((unsigned int*)buf+i) = ntohl(*((unsigned int*)buf+i));
return rv;
}
static int
send_data(const void* buf, int len) {
int rv;
if ((rv = uhWriteGame(fd, (void*)buf, len)) == -1) {
perror("uhWriteGame");
exit(1);
}
return rv;
}
static int
read_data(void* buf, int len) {
int rv;
if ((rv = uhReadGame(fd, buf, len)) == -1) {
perror("uhReadGame");
exit(1);
}
return rv;
}
static int
jump(unsigned int addr) {
unsigned int hbuf[2];
hbuf[0] = REQ_JUMP;
hbuf[1] = addr;
send_cmd(hbuf, 8);
read_rsp(hbuf, 8);
if (hbuf[0] != 255-REQ_JUMP || hbuf[1] != 0) {
fprintf(stderr, "jump failed\n");
return 1;
}
return 0;
}
static int blist_good;
static int
read_blist(int first_only) {
unsigned int hbuf[2];
int rv;
if (blist_good && first_only) return 0;
blist_good = 1;
printf("scanning blocks\n");
hbuf[0] = REQ_SCAN_BLOCKS;
hbuf[1] = 0;
send_cmd(hbuf, 8);
read_rsp(hbuf, 8);
if (hbuf[0] != 255-REQ_SCAN_BLOCKS || (hbuf[1]&0x80000000)) {
fprintf(stderr, "scan blocks failed %d\n", hbuf[1]);
return -1;
}
rv = read_data(blist, blocks = hbuf[1]);
if (rv > 0) {
/* compute addresses for sk, sal, and sa */
int i, j;
for(j = i = 0; i < blocks; i++) {
if (blist[i] == 0) {
sblocks[j++] = i;
if (j >= SYS_BLOCKS) break;
}
}
printf("found %d sys blocks\n", j);
}
return rv;
}
static int
write_block(const void* block, const void*spare, int off) {
unsigned int hbuf[2], r;
/* write a block on the BBCard */
hbuf[0] = spare ? REQ_WRITE_BLOCK_SP : REQ_WRITE_BLOCK;
r = 255-hbuf[0];
hbuf[1] = off;
send_cmd(hbuf, 8);
send_data(block, BB_FL_BLOCK_SIZE);
if (spare) send_data(spare, BB_FL_SPARE_SIZE);
read_rsp(hbuf, 8);
if (hbuf[0] != r || hbuf[1] != 0) {
fprintf(stderr, "write block %d failed\n", off);
return -1;
}
return 0;
}
static int
read_block(void* block, void* spare, int off) {
unsigned int hbuf[2], r;
/* write a block on the BBCard */
hbuf[0] = spare ? REQ_READ_BLOCK_SP : REQ_READ_BLOCK;
r = 255-hbuf[0];
hbuf[1] = off;
send_cmd(hbuf, 8);
read_rsp(hbuf, 8);
read_data(block, BB_FL_BLOCK_SIZE);
if (spare) read_data(spare, BB_FL_SPARE_SIZE);
if (hbuf[0] != r || hbuf[1] != 0) {
fprintf(stderr, "read block %d failed\n", off);
return -1;
}
return 0;
}
static int
read_blocks(const char* file, int start, int n) {
FILE* fp;
int i;
if ((fp = fopen(file, "wb")) == NULL) {
perror(file);
return -1;
}
for(i = 0; i < n; i++) {
memset(block, 0, sizeof block);
if (read_block(block, 0, sblocks[start+i]) < 0) {
fclose(fp);
return -1;
}
fwrite(block, sizeof block, 1, fp);
}
fclose(fp);
return 0;
}
static int
write_blocks(const char* file, int start, int n, int link_start) {
FILE* fp;
int link = link_start;
u8 spare[BB_FL_SPARE_SIZE];
if ((fp = fopen(file, "rb")) == NULL) {
perror(file);
return -1;
}
memset(block, 0, sizeof block);
memset(spare, 0xff, sizeof spare);
while(fread(block, 1, sizeof block, fp) > 0) {
if (--n < 0) {
printf("%s too large, truncating :)\n", file);
break;
}
if (link) {
int prev = sblocks[link_start++];
if(prev>255) return -1;
spare[BB_FL_BLOCK_LINK_OFF] = prev;
spare[BB_FL_BLOCK_LINK_OFF+1] = prev;
spare[BB_FL_BLOCK_LINK_OFF+2] = prev;
if (link_start >= SYS_BLOCKS) link_start = 0;
}
if (write_block(block, link ? spare : 0, sblocks[start++]) < 0) {
fclose(fp);
return -1;
}
memset(block, 0, sizeof block);
}
fclose(fp);
return 0;
}
static int
delete_file(const char* file, int report) {
unsigned int hbuf[2];
hbuf[0] = REQ_DELETE_FILE;
hbuf[1] = strlen(file)+1;
send_cmd(hbuf, 8);
send_data(file, RND(strlen(file)+1));
read_rsp(hbuf, 8);
if (hbuf[0] != 255-REQ_DELETE_FILE || (report && hbuf[1] != 0)) {
fprintf(stderr, "delete %s failed\n", file);
}
return 0;
}
static int
move_file(const char* old, const char* new) {
unsigned int hbuf[2];
hbuf[0] = REQ_RENAME_FILE;
hbuf[1] = strlen(old)+1;
send_cmd(hbuf, 8);
send_data(old, RND(strlen(old)+1));
hbuf[0] = htonl(strlen(new)+1);
send_data(hbuf, 4);
send_data(new, RND(strlen(new)+1));
read_rsp(hbuf, 8);
if (hbuf[0] != 255-REQ_RENAME_FILE || hbuf[1] != 0) {
fprintf(stderr, "move %s %s failed %d\n", new, old, hbuf[1]);
}
return 0;
}
static int
write_file(const char* in, const char* out) {
FILE* fp;
unsigned char* b;
unsigned int hbuf[2];
struct stat sb;
unsigned int x, n, i;
#ifndef WIN32
struct timeval tv1, tv2;
#else
SYSTEMTIME tv1, tv2;
#endif
float t;
if ((fp = fopen(in, "rb")) == NULL) {
perror(in);
return -1;
}
fstat(fileno(fp), &sb);
n = RND(sb.st_size);
b = malloc(n);
fread(b, sb.st_size, 1, fp);
fclose(fp);
#ifndef WIN32
gettimeofday(&tv1, NULL);
#else
GetSystemTime(&tv1);
#endif
hbuf[0] = REQ_WRITE_FILE;
hbuf[1] = sb.st_size;
send_cmd(hbuf, 8);
x = htonl(strlen(out)+1);
send_data(&x, 4);
send_data(out, RND(ntohl(x)));
/* read first response */
read_rsp(hbuf, 8);
if (hbuf[0] != 255-REQ_WRITE_FILE || hbuf[1] != 0) {
fprintf(stderr, "file write of %s failed\n", in);
free(b);
return -1;
}
for(i = 0; i < n; i += BB_FL_BLOCK_SIZE)
send_data(b+i, n-i < BB_FL_BLOCK_SIZE ? n-i : BB_FL_BLOCK_SIZE);
free(b);
/* read second response */
read_rsp(hbuf, 8);
if (hbuf[0] != 255-REQ_WRITE_FILE || hbuf[1] != 0) {
fprintf(stderr, "file write of %s failed\n", in);
return -1;
}
#ifndef WIN32
gettimeofday(&tv2, NULL);
t = (float)(tv2.tv_sec-tv1.tv_sec) + (tv2.tv_usec-tv1.tv_usec)/1000000.f;
#else
GetSystemTime(&tv2);
t = (float)(tv2.wSecond-tv1.wSecond) + (tv2.wMilliseconds-tv1.wMilliseconds)/1000.f;
#endif
printf("%.1f KB %.1f sec %.1f KB/s\n", sb.st_size/1024.f, t, sb.st_size/1000.f/t);
return 0;
}
static int
read_file(const char* in, const char* out) {
FILE* fp;
unsigned char* b;
unsigned int hbuf[2];
int len, n;
if ((fp = fopen(out, "wb")) == NULL) {
perror(out);
return -1;
}
hbuf[0] = REQ_READ_FILE;
hbuf[1] = strlen(in)+1;
send_cmd(hbuf, 8);
send_data(in, RND(strlen(in)+1));
read_rsp(hbuf, 8);
if (hbuf[0] != 255-REQ_READ_FILE || (hbuf[1]&0x80000000)) {
fprintf(stderr, "file read of %s failed\n", in);
fclose(fp);
return -1;
}
b = malloc(n = RND(hbuf[1]));
for(len = 0; len < n; len += BB_FL_BLOCK_SIZE) {
read_data(b+len, n-len < BB_FL_BLOCK_SIZE ? n-len : BB_FL_BLOCK_SIZE);
}
fwrite(b, hbuf[1], 1, fp);
fclose(fp);
free(b);
return 0;
}
static int
mkfat(unsigned char* blist, BbFat16* fat) {
int i, k, l;
u16 csum = 0, *p = (u16*)fat, link = 0;
for(k = 0; k < blocks/BB_FAT16_ENTRIES; k++) {
memcpy(fat[k].magic, k ? BB_FAT16_LINK_MAGIC : BB_FAT16_MAGIC, sizeof fat[k].magic);
fat[k].seq = 1;
}
for(i = 0; i < blocks; i++)
BB_FAT16_NEXT(fat,i) = blist[i] ? BB_FAT_BAD : BB_FAT_AVAIL;
for(i = 0; i < BB_FAT16_BLOCKS; i++) {
if (BB_FAT16_NEXT(fat,blocks-1-i) != BB_FAT_BAD)
BB_FAT16_NEXT(fat,blocks-1-i) = BB_FAT_RESERVED;
}
for(i = 0; i < BB_SYSTEM_AREA_SIZE/BB_FL_BLOCK_SIZE; i++) {
if (BB_FAT16_NEXT(fat,i) != BB_FAT_BAD)
BB_FAT16_NEXT(fat,i) = BB_FAT_RESERVED;
}
/* fix endianness */
for(i = 0; i < blocks; i++)
BB_FAT16_NEXT(fat,i) = htons(BB_FAT16_NEXT(fat,i));
for(i = 0; i < BB_INODE16_ENTRIES; i++) {
fat->inode[i].block = htons(fat->inode[i].block);
fat->inode[i].size = htonl(fat->inode[i].size);
}
l = blocks-1;
for(k = blocks/BB_FAT16_ENTRIES; --k >= 0;) {
fat[k].seq = htonl(fat[k].seq);
fat[k].link = htons(link);
/* update version stamp, checksum */
for(csum = i = 0; i < BB_FL_BLOCK_SIZE/sizeof(u16)-1; i++)
csum += ntohs(p[k*BB_FL_BLOCK_SIZE/sizeof(u16)+i]);
fat[k].cksum = htons(BB_FAT16_CKSUM - csum);
while(blist[l]) l--;
link = l;
}
return 0;
}
int
main(int argc, char **argv) {
char *p, file[256], file2[256], args[256];
#ifdef USE_READLINE
char *line=NULL, *expansion = NULL;
int result;
char history_file[256];
#else
char line[256];
#endif
unsigned int hbuf[2];
int n, i, rv;
FILE* fp;
#if 1
#ifndef WIN32
#ifdef __sgi
if ((fd = uhOpenGame("/dev/u64_data")) == -1) {
#else
if ((fd = uhOpenGame("/tmp/u64_data")) == -1) {
#endif
#else
if ((fd = uhOpenGame(argc>1 ? argv[1] : "8087")) == -1) {
#endif
perror("uhOpenGame");
exit(1);
}
#endif
#ifdef USE_READLINE
if (getenv("HOME")) {
strcpy(history_file, getenv("HOME"));
} else {
history_file[0] = '.';
history_file[1] = 0;
}
strcat(history_file, "/.bb_history");
read_history(history_file);
#endif
for(;;) {
next:
#ifdef USE_READLINE
if (line) free(line);
if ((line = readline("> ")) == NULL) break;
expansion = NULL;
result = history_expand(line, &expansion);
if (expansion) {
free(line);
line = expansion;
}
if (result < 0 || result == 2) {
printf("%s\n", line);
continue;
}
#else
printf("> "); fflush(stdout);
if (!fgets(line, sizeof line, stdin)) break;
#endif
if ((p = strrchr(line, '\n'))) *p = '\0';
for(p = line; *p && isspace(*p); ++p) ;
#ifdef USE_READLINE
if (*p) add_history(p);
#endif
memset(hbuf, 0, sizeof hbuf);
memset(file, 0, sizeof file);
memset(file2, 0, sizeof file2);
if (*p == 'h' || *p == '?') { /* help */
printf("\th - help\n"
"\ta file [name] - add file [name] to BBCard\n"
"\tc file [name] - copy file from BBCard to host\n"
"\td file - delete file from BBCard\n"
"\tf - reload file system\n"
"\tg file - execute unencrypted kernel from host\n"
"\tl - list BBCard files\n"
"\tm old new - rename old to new\n"
"\tx file [args] - load and execute rom file from BBCard\n"
"\tr file [args] - copy file to BBCard and execute\n"
"\ty file - load and execute rom file from host\n"
"\tk file - update secure kernel on BBCard\n"
"\tK file - upload secure kernel to host\n"
"\ts file - update system app on BBCard\n"
"\tS file - upload system app to host\n"
"\tt file - update system app license on BBCard\n"
"\tT file - upload system app license to host\n"
"\tX file [host] - checksum file and host file\n"
"\tp - ping BB\n"
"\to - power off\n"
"\tb - list bad blocks\n"
"\tz - reformat/make file system\n"
"\ti - run diag\n"
"\tq - quit\n");
} else if (*p == 'q') { /* quit */
break;
} else if (*p == 'p') { /* ping */
hbuf[0] = REQ_PING;
send_cmd(hbuf, 8);
read_rsp(hbuf, 8);
if (hbuf[0] != 255-REQ_PING || hbuf[1] != 0) {
fprintf(stderr, "sync loss\n");
return 1;
}
} else if (*p == 'o') { /* off */
hbuf[0] = REQ_POWER_OFF;
send_cmd(hbuf, 8);
read_rsp(hbuf, 8);
if (hbuf[0] != 255-REQ_POWER_OFF || hbuf[1] != 0) {
fprintf(stderr, "power off failed\n");
return 1;
}
} else if (*p == 'a') { /* add file */
if ((rv = sscanf(p+1, "%s %s", file, file2)) < 1) {
fprintf(stderr, "usage: a file [name]\n");
goto next;
}
write_file(file, rv == 1 ? basename(file) : file2);
} else if (*p == 'c') { /* copy file */
if ((rv = sscanf(p+1, "%s %s", file, file2)) < 1) {
fprintf(stderr, "usage: c file [name]\n");
goto next;
}
read_file(file, rv == 1 ? file : file2);
} else if (*p == 'd') { /* delete file */
if (sscanf(p+1, "%s", file) != 1) {
fprintf(stderr, "usage: d file\n");
goto next;
}
delete_file(file, 1);
} else if (*p == 'm') { /* move file */
if ((rv = sscanf(p+1, "%s %s", file, file2)) < 2) {
fprintf(stderr, "usage: m old new\n");
goto next;
}
move_file(file, file2);
} else if (*p == 'f') { /* reload filesystem */
hbuf[0] = REQ_FS_INIT;
send_cmd(hbuf, 8);
read_rsp(hbuf, 8);
if (hbuf[0] != 255-REQ_FS_INIT || hbuf[1] & 0x80000000)
fprintf(stderr, "fs reload failed %d\n", hbuf[1]);
blist_good = 0;
} else if (*p == 'l') { /* list directory */
hbuf[0] = REQ_READ_DIR;
send_cmd(hbuf, 8);
read_rsp(hbuf, 8);
printf("read dir %d\n", hbuf[1]);
if (hbuf[0] != 255-REQ_READ_DIR || hbuf[1] & 0x80000000) {
fprintf(stderr, "read directory failed %d\n", hbuf[1]);
} else {
int size;
for(i = 0; i < (int) hbuf[1]; i++) {
read_data(file, 16);
read_data(&size, 4); size = ntohl(size);
printf("%.12s %d\n", file, size);
}
}
} else if (*p == 'r') { /* copy file to BBCard and execute */
if ((rv = sscanf(p+1, "%s%n", file, &n)) != 1) {
fprintf(stderr, "usage: r file [args]\n");
goto next;
}
if (access(file, R_OK) < 0) {
perror(file);
goto next;
}
delete_file(basename(file), 0);
if (write_file(file, basename(file)) < 0) goto next;
strcpy(file, basename(file));
goto exec;
} else if (*p == 'x') { /* execute BBCard file */
if (sscanf(p+1, "%s%n", file, &n) != 1) {
fprintf(stderr, "usage: x file\n");
goto next;
}
exec:
memset(args, 0, sizeof args);
while(isspace(p[1+n])) n++;
for(i = 0; p[n+1] != '\0';)
args[i++] = p[1+n++];
if (!i) i = 1; /* can't xfer 0 bytes */
hbuf[0] = REQ_EXEC_FILE;
hbuf[1] = strlen(file)+1;
send_cmd(hbuf, 8);
send_data(file, RND(strlen(file)+1));
hbuf[0] = htonl(RND(i));
send_data(hbuf, 4);
send_data(args, RND(i));
read_rsp(hbuf, 8);
if (hbuf[0] != 255-REQ_EXEC_FILE || hbuf[1] != 0) {
fprintf(stderr, "exec %s failed\n", file);
}
} else if (*p == 'y') { /* execute host file */
unsigned char* b;
unsigned int addr;
struct stat sb;
if (sscanf(p+1, "%s", file) != 1) {
fprintf(stderr, "usage: y file\n");
goto next;
}
if ((fp = fopen(file, "rb")) == NULL) {
perror(file);
goto next;
}
fstat(fileno(fp), &sb);
if (sb.st_size > 1024*1024+4096)
sb.st_size = 1024*1024+4096;
b = malloc(RND(sb.st_size));
fread(b, sb.st_size, 1, fp);
fclose(fp);
hbuf[0] = REQ_WRITE_MEM;
hbuf[1] = sb.st_size-4096;
send_cmd(hbuf, 8);
send_data(b+8, 4);
addr = ntohl(*(unsigned int*)(b+8));
send_data(b+4096, RND(sb.st_size-4096));
sum(b+4096, sb.st_size-4096);
free(b);
read_rsp(hbuf, 8);
if (hbuf[0] != 255-REQ_WRITE_MEM || hbuf[1] != 0) {
fprintf(stderr, "mem write of %s failed\n", file);
goto next;
}
jump(addr);
} else if (*p == 'g') { /* execute host kernel */
unsigned char* b;
unsigned int addr;
struct stat sb;
if (sscanf(p+1, "%s", file) != 1) {
fprintf(stderr, "usage: y file\n");
goto next;
}
if ((fp = fopen(file, "rb")) == NULL) {
perror(file);
goto next;
}
fstat(fileno(fp), &sb);
if (sb.st_size > 1024*64)
sb.st_size = 1024*64;
b = malloc(RND(sb.st_size));
fread(b, sb.st_size, 1, fp);
fclose(fp);
hbuf[0] = REQ_WRITE_MEM;
hbuf[1] = sb.st_size;
send_cmd(hbuf, 8);
addr = PHYS_TO_K1(BOOT_RAM_LO_START);
hbuf[0] = htonl(addr);
send_data(hbuf, 4);
send_data(b, RND(sb.st_size));
sum(b, sb.st_size);
free(b);
read_rsp(hbuf, 8);
if (hbuf[0] != 255-REQ_WRITE_MEM || hbuf[1] != 0) {
fprintf(stderr, "mem write of %s failed\n", file);
goto next;
}
jump(addr);
} else if (*p == 'k') { /* update kernel */
if (sscanf(p+1, "%s", file) != 1) {
fprintf(stderr, "usage: k file\n");
goto next;
}
read_blist(1);
write_blocks(file, SK_OFF, SK_BLOCKS, 0);
} else if (*p == 'K') { /* copy kernel */
if (sscanf(p+1, "%s", file) != 1) {
fprintf(stderr, "usage: K file\n");
goto next;
}
read_blist(1);
read_blocks(file, SK_OFF, SK_BLOCKS);
} else if (*p == 's') { /* update sys app */
if (sscanf(p+1, "%s", file) != 1) {
fprintf(stderr, "usage: s file\n");
goto next;
}
read_blist(1);
write_blocks(file, SA0_OFF, SA_BLOCKS, SA0_OFF+1);
} else if (*p == 'S') { /* copy sys app */
if (sscanf(p+1, "%s", file) != 1) {
fprintf(stderr, "usage: S file\n");
goto next;
}
read_blist(1);
read_blocks(file, SA0_OFF, SA_BLOCKS);
} else if (*p == 't') { /* update license */
if (sscanf(p+1, "%s", file) != 1) {
fprintf(stderr, "usage: t file\n");
goto next;
}
read_blist(1);
write_blocks(file, SL0_OFF, SL_BLOCKS, SA0_OFF);
} else if (*p == 'T') { /* copy license */
if (sscanf(p+1, "%s", file) != 1) {
fprintf(stderr, "usage: T file\n");
goto next;
}
read_blist(1);
read_blocks(file, SL0_OFF, SL_BLOCKS);
} else if (*p == 'b') { /* list bad blocks */
read_blist(0);
for(i = 0; i < blocks; i++) {
printf("%c", blist[i] == 0 ? '.' : '@');
if (i % 64 == 63) printf("\n");
}
} else if (*p == 'X') { /* checksum */
if ((rv = sscanf(p+1, "%s %s", file, file2)) < 1) {
fprintf(stderr, "usage: X file [name]\n");
goto next;
}
hbuf[0] = REQ_SUM_FILE;
hbuf[1] = strlen(file)+1;
send_cmd(hbuf, 8);
send_data(file, RND(strlen(file)+1));
read_rsp(hbuf, 8);
if (hbuf[0] != 255-REQ_SUM_FILE || (hbuf[1]&0x80000000)) {
fprintf(stderr, "file sum of %s failed\n", file);
goto next;
}
if (rv > 1) {
FILE *fp = fopen(file2, "rb");
unsigned short s = 0;
if (fp == NULL) {
perror(file2);
goto next;
}
while(fread(block, 1, sizeof block, fp) > 0) {
for(i = 0; i < sizeof block; i++)
s += block[i];
memset(block, 0, sizeof block);
}
fclose(fp);
printf("sum %04x %04x\n", hbuf[1], s);
} else
printf("sum %04x\n", hbuf[1]);
} else if (*p == 'z') { /* reformat file system */
int j, k;
read_blist(1);
memset(fat, 0x0, sizeof fat);
/* create an empty superblock */
mkfat(blist, (BbFat16*)fat);
k = (blocks/BB_FAT16_ENTRIES-1);
for(j = i = 0; i < BB_FAT16_BLOCKS; j++) {
if(blist[blocks-1-j] == 0) {
write_block(fat+k, 0, blocks-1-j);
if (--k < 0)
k = (blocks/BB_FAT16_ENTRIES-1);
i++;
}
}
} else if (*p == 'i') { /* run diag */
hbuf[0] = REQ_DBG_TEST;
send_cmd(hbuf, 8);
read_rsp(hbuf, 8);
if (hbuf[0] != 255-REQ_DBG_TEST || hbuf[1] != 0) {
fprintf(stderr, "dbg_tests failed %d\n", hbuf[1]);
}
} else {
fprintf(stderr, "unknown command '%s'\n", p);
}
}
#ifdef USE_READLINE
write_history(history_file);
history_truncate_file(history_file, 100);
#endif
if (uhCloseGame(fd) != 0) {
perror("uhCloseGame");
exit(1);
}
return 0;
}