Repository navigation
Expand file tree
/
Copy pathsio_shell.c
More file actions
executable file
·3000 lines (2584 loc) · 57.8 KB
/
Copy pathsio_shell.c
File metadata and controls
executable file
·3000 lines (2584 loc) · 57.8 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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////////
// sio_shell.c - C side code for the SIO shell
// Written by TyRaNiD (c) 2005
// Much props to Herben und Pixel
////////////////////////////////////////////////////////////////////////////////
#include <tamtypes.h>
#include <stdio.h>
#include <string.h>
#include <kernel.h>
#include <sio.h>
#include <sio_shell.h>
#include <hwbp.h>
#include "R5900Disasm.h"
#include "ymodem.h"
#include "iopmon.h"
////////////////////////////////////////////////////////////////////////////////
// Function Prototypes
////////////////////////////////////////////////////////////////////////////////
/* Command prototypes */
static int exit_cmd(void);
static int md_cmd(void);
static int help_cmd(void);
static int pb_cmd(void);
static int ph_cmd(void);
static int pw_cmd(void);
static int pd_cmd(void);
static int dr_cmd(void);
static int er_cmd(void);
static int exec_cmd(void);
static int start_cmd(void);
static int dis_cmd(void);
static int bpi_cmd(void);
static int bpv_cmd(void);
static int bpd_cmd(void);
static int swbp_cmd(void);
static int shswbp_cmd(void);
static int swdel_cmd(void);
static int bpregs_cmd(void);
static int bpoff_cmd(void);
static int fill_cmd(void);
static int copy_cmd(void);
static int find_cmd(void);
static int step_cmd(void);
static int skip_cmd(void);
static int dt_cmd(void);
static int ch_cmd(void);
static int ih_cmd(void);
static int recv_cmd(void);
static int send_cmd(void);
static int ct_cmd(void);
static int sp_cmd(void);
static int hs_cmd(void);
static int us_cmd(void);
static int baud_cmd(void);
////////////////////////////////////////////////////////////////////////////////
// Global Variables
////////////////////////////////////////////////////////////////////////////////
/* Maximum command line */
#define CLI_MAX 128
/* Maximum history */
#define CLI_HISTSIZE 8
/* Last command line (history) */
static char g_lastcli[CLI_HISTSIZE][CLI_MAX];
/* Current command line */
static char g_cli[CLI_MAX];
/* Current position in the command line buffer */
static int g_cli_pos = 0;
/* Current size of the cli buffer */
static int g_cli_size = 0;
/* Current insert mode */
static int g_insert_mode = 0;
/* Last position in the history buffer */
static int g_lastcli_pos = 0;
/* Current scrolling position in the history buffer */
static int g_currcli_pos = 0;
/* Pointer to the kernel printf function */
printf_type g_sio_printf;
struct ee_regs* g_currSavedRegs;
/* Saved registers from level2 handler */
extern struct ee_regs _level2SavedRegs;
/* Saved registers from level1 handler */
extern struct ee_regs _level1SavedRegs;
/* Pointer to the syscall table */
struct syscall_table* _syscallTable;
/* Pointer to the stored syscall table (for hooking) */
struct syscall_table* _syscallTableOld;
/* Current exception level */
int g_currLevel;
/* Pointer to the kernel thread setup routine */
int (*g_thread_setup)(char *, u32, u32, u32);
/* Break on syscall */
int g_breakOnSyscall;
////////////////////////////////////////////////////////////////////////////////
// Utility Functions
////////////////////////////////////////////////////////////////////////////////
/* Search pattern for start of printf function */
#define PRINTF_SEARCH_PATTERN "\x70\xFF\xBD\x27\x58\x00\xA5\xFF"
/* Find the kernel printf function */
static void find_sioprintf(void)
{
u8 *ptr;
u8 *search = PRINTF_SEARCH_PATTERN;
ptr = (u8 *) 0x80004000;
while(ptr < (u8 *) 0x80008000)
{
int i;
int found = 1;
for(i = 0; i < 8; i++)
{
if(ptr[i] != search[i])
{
found = 0;
break;
}
}
if(found)
{
printf = (printf_type) ptr;
break;
}
ptr += 4;
}
}
/* Make the character upper case */
static char upcase(char ch)
{
if((ch >= 'a') && (ch <= 'z'))
{
ch ^= (1 << 5);
}
return ch;
}
/* Check if character is a hexadecimal character */
static int is_hex(char ch)
{
ch = upcase(ch);
if((ch >= '0') && (ch <= '9'))
return 1;
if((ch >= 'A') && (ch <= 'F'))
return 1;
return 0;
}
static int is_alnum(char ch)
{
ch = upcase(ch);
if((ch >= '0') && (ch <= '9'))
return 1;
if((ch >= 'A') && (ch <= 'Z'))
return 1;
return 0;
}
/* Check if character is a space/tab */
static int is_space(char ch)
{
if((ch == ' ') || (ch == '\t'))
{
return 1;
}
return 0;
}
/* Convert a single hex digit to an int */
static int hex_to_int(char ch)
{
if((ch >= '0') && (ch <= '9'))
{
return ch - '0';
}
ch = upcase(ch);
if((ch >= 'A') && (ch <= 'F'))
{
return ch - 'A' + 10;
}
return 0;
}
// Convert a hex number to an integer of a set number of nibbles
// Also assumes there is no leading white space strtok should take care of this
static u64 hextoi_generic(const char *hex, int total_nibbles)
{
int pos = 0;
int nibbles = 0;
u64 output = 0;
// Optional 0x prefix
if((hex[pos] == '0') && (toupper(hex[pos+1]) == 'X'))
{
pos += 2;
}
while(hex[pos] && (nibbles < total_nibbles))
{
if(is_hex(hex[pos]))
{
output = (output << 4) | hex_to_int(hex[pos]);
nibbles++;
pos++;
}
else
{
break;
}
}
return output;
}
/* Convert a hex string to a double word */
static u64 hextod(const char *hex)
{
return hextoi_generic(hex, 16);
}
/* Conver a hex string to a word */
static u32 hextoi(const char *hex)
{
return (u32) hextoi_generic(hex, 8);
}
static u32 dectoi(const char *dec)
{
u32 i = 0;
while((*dec) && (*dec >= '0') && (*dec <= '9'))
{
i *= 10;
i += *dec - '0';
dec++;
}
return i;
}
/* List of EE register names */
static const unsigned char regName[32][5] =
{
"zr", "at", "v0", "v1", "a0", "a1", "a2", "a3",
"t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
"t8", "t9", "s0", "s1", "s2", "s3", "s4", "s5",
"s6", "s7", "k0", "k1", "gp", "sp", "fp", "ra"
};
/* Find a register based on its name */
static u32 *find_reg32(const char *reg)
{
if(strcasecmp(reg, "ErrorPC") == 0)
{
return &g_currSavedRegs->ErrorPC;
}
else if(strcasecmp(reg, "EPC") == 0)
{
return &g_currSavedRegs->EPC;
}
else // Do search of normal regs
{
int reg_loop;
for(reg_loop = 0; reg_loop < 32; reg_loop++)
{
if(strcasecmp(regName[reg_loop], reg) == 0)
{
return g_currSavedRegs->r[reg_loop].r32;
}
}
}
return NULL;
}
#define MEM_ATTRIB_READ (1 << 0)
#define MEM_ATTRIB_WRITE (1 << 1)
#define MEM_ATTRIB_EXEC (1 << 2)
#define MEM_ATTRIB_BYTE (1 << 3)
#define MEM_ATTRIB_HALF (1 << 4)
#define MEM_ATTRIB_WORD (1 << 5)
#define MEM_ATTRIB_DBL (1 << 6)
#define MEM_ATTRIB_ALL 0xFFFFFFFF
struct mem_entry
{
u32 addr;
s32 size;
u32 attrib;
};
static struct mem_entry g_memareas[] =
{
{ 0, (32 * 1024 * 1024), MEM_ATTRIB_ALL }, /* Normal memory */
{ 0x80000000, (32 * 1024 * 1024), MEM_ATTRIB_ALL }, /* Kernel memory */
{ 0xBC000000, (2 * 1024 * 1024), MEM_ATTRIB_ALL & ~MEM_ATTRIB_EXEC }, /* IOP memory */
{ 0xBFC00000, (32 * 1024 * 1024), MEM_ATTRIB_ALL & ~MEM_ATTRIB_WRITE }, /* Rom0 memory */
{ 0xBE000000, (32 * 1024 * 1024), MEM_ATTRIB_ALL & ~MEM_ATTRIB_WRITE }, /* Rom1 memory */
{ 0, 0, 0 }
};
// return the size available that can be read/written (checking)
// If any attrib flags are passed then they must be present in the memory bit field
static s32 validate_memaddr(u32 addr, u32 attrib)
{
const struct mem_entry *entry;
int size_left = 0;
entry = g_memareas;
while(entry->size != 0)
{
if((addr >= entry->addr) && (addr < (entry->addr + (u32) entry->size)))
{
// Only pass through areas with valid attributes (e.g. write or execute)
if((entry->attrib & attrib) == attrib)
{
size_left = entry->size - (int) (addr - entry->addr);
}
break;
}
entry++;
}
return size_left;
}
// Return 1 if decode was successful
// Takes forms either $reg or addr or $reg+ofs/$ref-ofs with optional @'s for dereferencing
static int decode_memaddr(const char *straddr, u32 *memaddr)
{
int ret = 0;
int deref = 0;
int i = 0;
u32 addr = 0;
/* We are dereferencing */
while(*straddr == '@')
{
/* Incremement the deref count */
deref++;
straddr++;
}
// This is a register
if(straddr[0] == '$')
{
const u32 *reg_data;
char reg_name[8];
int reg_pos;
int ofs;
int sign;
straddr++;
reg_pos = 0;
while((is_alnum(*straddr)) && (reg_pos < 7))
{
reg_name[reg_pos++] = *straddr++;
}
reg_name[reg_pos] = 0;
reg_data = find_reg32(reg_name);
if(reg_data)
{
addr = reg_data[0];
/* Let's get an offset if it exists */
do
{
if(*straddr == '+')
{
sign = 1;
straddr++;
}
else if(*straddr == '-')
{
sign = -1;
straddr++;
}
else
{
break;
}
if(is_hex(*straddr))
{
ofs = hextoi(straddr);
addr += sign * ofs;
}
}
while(0);
ret = 1;
}
else
{
printf("Could not find register %s\n", straddr);
ret = 0;
}
}
else
{
if(is_hex(straddr[0]))
{
addr = hextoi(straddr);
ret = 1;
}
else
{
printf("Could not decode address %s\n", straddr);
ret = 0;
}
}
/* Only deref if it was valid */
i = 0;
while((i < deref) && (ret))
{
addr = addr & ~3;
if(validate_memaddr(addr, MEM_ATTRIB_READ | MEM_ATTRIB_WORD))
{
addr = _lw(addr);
}
else
{
printf("Invalid de-reference at depth %d (%08x)\n", i, addr);
ret = 0;
}
i++;
}
if(ret)
{
*memaddr = addr;
}
return ret;
}
#if 0
// Return 1 if decode was successful
// Takes forms either $reg or addr or $reg[ofs] with optional @'s for dereferencing
static int decode_memaddr(const char *straddr, u32 *memaddr)
{
int ret = 0;
int deref = 0;
int i = 0;
u32 addr = 0;
/* We are dereferencing */
while(*straddr == '@')
{
/* Incremement the deref count */
deref++;
straddr++;
}
// This is a register
if(straddr[0] == '$')
{
const u32 *reg_data;
reg_data = find_reg32(&straddr[1]);
if(reg_data)
{
addr = reg_data[0];
ret = 1;
}
else
{
printf("Could not find register %s\n", straddr);
ret = 0;
}
}
else
{
if(is_hex(straddr[0]))
{
addr = hextoi(straddr);
ret = 1;
}
else
{
printf("Could not decode address %s\n", straddr);
ret = 0;
}
}
/* Only deref if it was valid */
i = 0;
while((i < deref) && (ret))
{
addr = addr & ~3;
if(validate_memaddr(addr, MEM_ATTRIB_READ | MEM_ATTRIB_WORD))
{
addr = _lw(addr);
}
else
{
printf("Invalid de-reference at depth %d (%08x)\n", i, addr);
ret = 0;
}
i++;
}
if(ret)
{
*memaddr = addr;
}
return ret;
}
#endif
////////////////////////////////////////////////////////////////////////////////
// Debugging functions
////////////////////////////////////////////////////////////////////////////////
#define MAX_SWBREAKS 16
#define SW_BREAK_INST 0x0000000d
/* Define a software breakpoint structure */
struct sw_breakpoint
{
u32 addr;
u32 oldinst;
u32 active;
};
struct sw_breakpoint g_swbreaks[MAX_SWBREAKS];
// Software breakpoints used for stepping
struct sw_breakpoint g_stepbp[2];
/* Initialise the software breakpoint array */
static void init_swbps(void)
{
int i;
for(i = 0; i < MAX_SWBREAKS; i++)
{
g_swbreaks[i].active = 0;
}
g_stepbp[0].active = 0;
g_stepbp[1].active = 0;
}
/* Find a software breakpoint based on its address */
static int find_swbp(u32 addr)
{
int bp;
for(bp = 0; bp < MAX_SWBREAKS; bp++)
{
if((g_swbreaks[bp].active) && (g_swbreaks[bp].addr == addr))
{
break;
}
}
if(bp < MAX_SWBREAKS)
{
return bp;
}
/* let's check the step breakpoints */
if((g_stepbp[0].active) && (g_stepbp[0].addr == addr))
{
return MAX_SWBREAKS;
}
else if((g_stepbp[1].active) && (g_stepbp[1].addr == addr))
{
return MAX_SWBREAKS + 1;
}
return -1;
}
/* Clear a software breakpoint */
static void clear_swbp(int bp)
{
if(bp >= MAX_SWBREAKS)
{
if(g_stepbp[0].active)
{
_sw(g_stepbp[0].oldinst, g_stepbp[0].addr);
g_stepbp[0].active = 0;
}
if(g_stepbp[1].active)
{
_sw(g_stepbp[1].oldinst, g_stepbp[1].addr);
g_stepbp[1].active = 0;
}
}
else
{
_sw(g_swbreaks[bp].oldinst, g_swbreaks[bp].addr);
g_swbreaks[bp].active = 0;
}
flush_cache();
}
/* Find a free (non active) break point. Returns -1 if not found */
static int find_freeswbp(void)
{
int bp;
for(bp = 0; bp < MAX_SWBREAKS; bp++)
{
if(g_swbreaks[bp].active == 0)
{
break;
}
}
if(bp < MAX_SWBREAKS)
{
return bp;
}
return -1;
}
////////////////////////////////////////////////////////////////////////////////
// Exception Handlers
////////////////////////////////////////////////////////////////////////////////
extern void level1ExceptionHandler(void);
extern void level2ExceptionHandler(void);
/* Install a V_DEBUG handler in place of the kernel one */
static void install_debug_handler(void)
{
u32 data;
/* Build a 'j level2ExceptionHandler' instruction */
data = (u32) level2ExceptionHandler & 0xFFFFFFF;
data = 0x8000000 | (data >> 2);
_sw(data, 0x80000100);
_sw(0, 0x80000104);
flush_cache();
}
/* Install a Level 1 BREAK and Syscall handler */
static void install_level1_handler(void)
{
_syscallTable->SetVCommonHandler(9, level1ExceptionHandler);
}
////////////////////////////////////////////////////////////////////////////////
// Shell commands
////////////////////////////////////////////////////////////////////////////////
/* Return values for the commands */
#define CMD_EXITSHELL 1
#define CMD_OK 0
#define CMD_ERROR -1
/* Structure to hold a single command entry */
struct sh_command
{
const char *name; /* Normal name of the command */
const char *syn; /* Synonym of the command */
int (*func)(void); /* Pointer to the command function */
const char *desc; /* Textual description */
const char *help; /* Command usage */
};
/* Define the list of commands */
struct sh_command commands[] = {
{ "exit", "ex", exit_cmd, "Exit the shell", "exit [retaddr]" },
{ "memdump", "md", md_cmd, "Dump memory to shell", "memdump addr" },
{ "pokeb", "pb", pb_cmd, "Poke in bytes", "pokeb pokeaddr value [value2 ...]" },
{ "pokeh", "ph", ph_cmd, "Poke in half words", "pokeh pokeaddr value [value2 ...]" },
{ "pokew", "pw", pw_cmd, "Poke in words", "pokew pokeaddr value [value2 ...]" },
{ "poked", "pd", pd_cmd, "Poke in doubles", "poked pokeaddr value [value2 ...]" },
{ "fill", "fi" , fill_cmd, "Fill memory up with a single byte value", "fill memaddr size val"},
{ "copy", "cp" , copy_cmd, "Copy memory from one place to another", "copy srcaddr destaddr size"},
{ "find", "fd" , find_cmd, "Find an ASCII or hex string in memory", "find startaddr size \"ASCII\"/Hex Values [mask M1...]" },
{ "dumpreg", "dr", dr_cmd, "Dump EE regs", "dumpreg" },
{ "editreg", "er", er_cmd, "Edit an EE reg (32bit only)", "editreg $reg value" },
{ "exec", NULL, exec_cmd, "Exec a program (LoadExecPS2)", "exec elfname [arg1 arg2 ...]" },
{ "start", NULL, start_cmd, "Start an already loaded program (ExecPS2)", "start addr [arg1 arg2 ...]" },
{ "disasm", "dis", dis_cmd, "Disassemble instructions", "disasm disaddr [count]" },
{ "bpinst", "bpi", bpi_cmd, "Set the hardware instruction bp", "bpinst address mask [uskx]" },
{ "bpdata", "bpd", bpd_cmd, "Set the hardware data bp", "bpdata address mask [rwuskx]" },
{ "bpval", "bpv", bpv_cmd, "Set the hardware data value bp", "bpval address amask value vmask [rwuskx]]" },
{ "swbp", NULL, swbp_cmd, "Set a software breakpoint", "swbp address" },
{ "shswbp", NULL, shswbp_cmd, "Show the current software breakpoints", "shswbp" },
{ "swdel", NULL, swdel_cmd, "Delete a currently set software breakpoint", "swdel num" },
{ "bpregs", "bpr", bpregs_cmd, "Dump the breakpoint registers", "bpregs"},
{ "bpoff", "bpo", bpoff_cmd, "Disable the hardware breakpoint(s)", "dboff" },
{ "step", "s", step_cmd, "Step to the next instruction", "step" },
{ "skip", "k", skip_cmd, "Step the next command, skipping sub routine jump", "skip" },
{ "dumpt", "dt", dt_cmd, "Dump the current thread list", "dumpt" },
{ "recv", NULL, recv_cmd, "Receive a data block using ymodem", "recv startaddr [maxsize]" },
{ "send", NULL, send_cmd, "Send a data block using ymodem", "send startaddr size filename" },
{ "createt", "ct", ct_cmd, "Create a new thread on the EE", "createt startaddr stackaddr priority" },
{ "sprint", "sp", sp_cmd, "Write out a print handler to EE memeory", "sprint startaddr" },
{ "hooksys", "hs", hs_cmd, "Hook the syscall handler and optionally break", "hooksys [break]" },
{ "unhksys", "us", us_cmd, "Unhook the syscall handler", "unhksys" },
{ "baud", NULL, baud_cmd, "Set the SIO baud rate", "baud (9600|19200|38400|57600|115200)" },
{ "clihist", "ch", ch_cmd, "Displays the CLI history", "clihist" },
{ "insthdl", "ih", ih_cmd, "Install EE exception handlers for common errors", "insthld" },
{ "help", "?", help_cmd, "Help (Obviously)", "help [command]" },
{ NULL, NULL, NULL, NULL }
};
/* Find a command from the command list */
static struct sh_command* find_command(const char *cmd)
{
struct sh_command* found_cmd = NULL;
int cmd_loop;
for(cmd_loop = 0; commands[cmd_loop].name != NULL; cmd_loop++)
{
if(strcasecmp(cmd, commands[cmd_loop].name) == 0)
{
found_cmd = &commands[cmd_loop];
break;
}
if(commands[cmd_loop].syn)
{
if(strcasecmp(cmd, commands[cmd_loop].syn) == 0)
{
found_cmd = &commands[cmd_loop];
break;
}
}
}
return found_cmd;
}
/* Exit command */
static int exit_cmd(void)
{
char *retaddr;
u32 ret = 0xffffffff;
retaddr = strtok(NULL, " \t");
if(retaddr)
{
ret = hextoi(retaddr);
ret &= ~3;
if(validate_memaddr(ret, MEM_ATTRIB_EXEC))
{
if(g_currLevel == 1)
{
g_currSavedRegs->EPC = ret;
printf("Setting EPC to %08x\n", ret);
}
else
{
g_currSavedRegs->ErrorPC = ret;
printf("Setting ErrorPC to %08x\n", ret);
}
}
}
printf("Exiting Shell\n");
return CMD_EXITSHELL;
}
/* Maximum memory dump size (per screen) */
#define MAX_MEMDUMP_SIZE 256
/* Print a row of a memory dump, up to row_size */
static void print_row(const u32* row, s32 row_size, u32 addr)
{
int i = 0;
printf("%08x - ", addr);
for(i = 0; i < 16; i++)
{
if(i < row_size)
{
printf("%02x ", row[i]);
}
else
{
printf("-- ", row[i]);
}
}
printf("- ");
for(i = 0; i < 16; i++)
{
if(i < row_size)
{
if((row[i] >= 32) && (row[i] < 127))
{
sio_putc(row[i]);
}
else
{
sio_putc('.');
}
}
else
{
sio_putc('.');
}
}
printf("\n");
}
/* Print a memory dump to SIO */
static void print_memdump(u32 addr, s32 size)
{
int size_left;
u32 row[16];
int row_size;
u8 *p_addr = (u8 *) addr;
printf(" - 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f - 0123456789abcdef\n");
printf("-----------------------------------------------------------------------------\n");
size_left = size > MAX_MEMDUMP_SIZE ? MAX_MEMDUMP_SIZE : size;
row_size = 0;
while(size_left > 0)
{
row[row_size] = p_addr[row_size];
row_size++;
if(row_size == 16)
{
// draw row
print_row(row, row_size, (u32) p_addr);
p_addr += 16;
row_size = 0;
}
size_left--;
}
}
/* Memory dump command */
static int md_cmd(void)
{
char *mem_addr;
u32 addr;
s32 size_left;
// Get memory address
mem_addr = strtok(NULL, " ");
if((mem_addr != NULL) && (decode_memaddr(mem_addr, &addr)))
{
size_left = validate_memaddr(addr, MEM_ATTRIB_READ | MEM_ATTRIB_BYTE);
if(size_left > 0)
{
while(size_left > 0)
{
char ch;
print_memdump(addr, size_left);
printf("Press b to go back, space to go forward, or q to quit.\n");
while((ch = sio_getc()) == -1);
ch = upcase(ch);
if(ch == 'Q')
{
break;
}
if(ch == 'B')
{
addr -= MAX_MEMDUMP_SIZE;
}
else
{
addr += MAX_MEMDUMP_SIZE;
}
size_left = validate_memaddr(addr, MEM_ATTRIB_READ | MEM_ATTRIB_BYTE);
}
}
else
{
printf("Invalid memory address %x\n", addr);
}
}
else
{
return CMD_ERROR;
}
return CMD_OK;
}
/* generic poke command, size is the number of bytes to poke */
static int poke_generic(int size)
{
if((size == 1) || (size == 2) || (size == 4) || (size == 8))
{
char *mem_addr;
char *mem_data;
u32 addr_mask;
u32 addr;
u64 data;
u32 mem_attrib = 0;
switch(size)
{
case 1: addr_mask = 0xFFFFFFFF;
mem_attrib = MEM_ATTRIB_BYTE;
break;
case 2: addr_mask = 0xFFFFFFFE;
mem_attrib = MEM_ATTRIB_HALF;
break;
case 4: addr_mask = 0xFFFFFFFC;
mem_attrib = MEM_ATTRIB_WORD;
break;
case 8: addr_mask = 0xFFFFFFF8;
mem_attrib = MEM_ATTRIB_DBL;
break;
default: // should not get here
return 0;
};
mem_addr = strtok(NULL, " \t");
mem_data = strtok(NULL, " \t");
if((mem_addr) && (mem_data) && (decode_memaddr(mem_addr, &addr)))
{
addr &= addr_mask;
while(mem_data != NULL)
{
data = hextod(mem_data);
if(validate_memaddr(addr, mem_attrib | MEM_ATTRIB_READ) >= size)
{
switch(size)
{
case 1: _sb(data & 0xFF, addr);
break;
case 2: _sh(data & 0xFFFF, addr);
break;
case 4: _sw(data & 0xFFFFFFFF, addr);
break;
case 8: _sd(data, addr);
break;
default: // should not get here
break;
};
}
else
{
printf("Invalid poke memory address %08x\n", addr);
break;
}
addr += size;
mem_data = strtok(NULL, " \t");
}
flush_cache();
}
else
{
return CMD_ERROR;
}
}
return CMD_OK;
}