]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_bedbug.c
net: fec_mxc: use some more appropriate return values
[karo-tx-uboot.git] / common / cmd_bedbug.c
1 /*
2  * BedBug Functions
3  */
4
5 #include <common.h>
6 #include <cli.h>
7 #include <command.h>
8 #include <linux/ctype.h>
9 #include <net.h>
10 #include <bedbug/type.h>
11 #include <bedbug/bedbug.h>
12 #include <bedbug/regs.h>
13 #include <bedbug/ppc.h>
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 extern void show_regs __P ((struct pt_regs *));
18 extern int run_command __P ((const char *, int));
19
20 ulong dis_last_addr = 0;        /* Last address disassembled   */
21 ulong dis_last_len = 20;        /* Default disassembler length */
22 CPU_DEBUG_CTX bug_ctx;          /* Bedbug context structure    */
23
24
25 /* ======================================================================
26  * U-Boot's puts function does not append a newline, so the bedbug stuff
27  * will use this for the output of the dis/assembler.
28  * ====================================================================== */
29
30 int bedbug_puts (const char *str)
31 {
32         /* -------------------------------------------------- */
33
34         printf ("%s\r\n", str);
35         return 0;
36 }                               /* bedbug_puts */
37
38
39
40 /* ======================================================================
41  * Initialize the bug_ctx structure used by the bedbug debugger.  This is
42  * specific to the CPU since each has different debug registers and
43  * settings.
44  * ====================================================================== */
45
46 void bedbug_init (void)
47 {
48         /* -------------------------------------------------- */
49
50 #if defined(CONFIG_4xx)
51         void bedbug405_init (void);
52
53         bedbug405_init ();
54 #elif defined(CONFIG_8xx)
55         void bedbug860_init (void);
56
57         bedbug860_init ();
58 #endif
59
60 #if defined(CONFIG_MPC824X) || defined(CONFIG_MPC8260)
61         /* Processors that are 603e core based */
62         void bedbug603e_init (void);
63
64         bedbug603e_init ();
65 #endif
66
67         return;
68 }                               /* bedbug_init */
69
70
71
72 /* ======================================================================
73  * Entry point from the interpreter to the disassembler.  Repeated calls
74  * will resume from the last disassembled address.
75  * ====================================================================== */
76 int do_bedbug_dis (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
77 {
78         ulong addr;             /* Address to start disassembly from    */
79         ulong len;              /* # of instructions to disassemble     */
80
81         /* -------------------------------------------------- */
82
83         /* Setup to go from the last address if none is given */
84         addr = dis_last_addr;
85         len = dis_last_len;
86
87         if (argc < 2)
88                 return CMD_RET_USAGE;
89
90         if ((flag & CMD_FLAG_REPEAT) == 0) {
91                 /* New command */
92                 addr = simple_strtoul (argv[1], NULL, 16);
93
94                 /* If an extra param is given then it is the length */
95                 if (argc > 2)
96                         len = simple_strtoul (argv[2], NULL, 16);
97         }
98
99         /* Run the disassembler */
100         disppc ((unsigned char *) addr, 0, len, bedbug_puts, F_RADHEX);
101
102         dis_last_addr = addr + (len * 4);
103         dis_last_len = len;
104         return 0;
105 }                               /* do_bedbug_dis */
106
107 U_BOOT_CMD (ds, 3, 1, do_bedbug_dis,
108             "disassemble memory",
109             "ds <address> [# instructions]");
110
111 /* ======================================================================
112  * Entry point from the interpreter to the assembler.  Assembles
113  * instructions in consecutive memory locations until a '.' (period) is
114  * entered on a line by itself.
115  * ====================================================================== */
116 int do_bedbug_asm (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
117 {
118         long mem_addr;          /* Address to assemble into     */
119         unsigned long instr;    /* Machine code for text        */
120         char prompt[15];        /* Prompt string for user input */
121         int asm_err;            /* Error code from the assembler */
122
123         /* -------------------------------------------------- */
124         int rcode = 0;
125
126         if (argc < 2)
127                 return CMD_RET_USAGE;
128
129         printf ("\nEnter '.' when done\n");
130         mem_addr = simple_strtoul (argv[1], NULL, 16);
131
132         while (1) {
133                 putc ('\n');
134                 disppc ((unsigned char *) mem_addr, 0, 1, bedbug_puts,
135                         F_RADHEX);
136
137                 sprintf (prompt, "%08lx:    ", mem_addr);
138                 cli_readline(prompt);
139
140                 if (console_buffer[0] && strcmp (console_buffer, ".")) {
141                         if ((instr =
142                              asmppc (mem_addr, console_buffer,
143                                      &asm_err)) != 0) {
144                                 *(unsigned long *) mem_addr = instr;
145                                 mem_addr += 4;
146                         } else {
147                                 printf ("*** Error: %s ***\n",
148                                         asm_error_str (asm_err));
149                                 rcode = 1;
150                         }
151                 } else {
152                         break;
153                 }
154         }
155         return rcode;
156 }                               /* do_bedbug_asm */
157
158 U_BOOT_CMD (as, 2, 0, do_bedbug_asm,
159             "assemble memory", "as <address>");
160
161 /* ======================================================================
162  * Used to set a break point from the interpreter.  Simply calls into the
163  * CPU-specific break point set routine.
164  * ====================================================================== */
165
166 int do_bedbug_break (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
167 {
168         /* -------------------------------------------------- */
169         if (bug_ctx.do_break)
170                 (*bug_ctx.do_break) (cmdtp, flag, argc, argv);
171         return 0;
172
173 }                               /* do_bedbug_break */
174
175 U_BOOT_CMD (break, 3, 0, do_bedbug_break,
176             "set or clear a breakpoint",
177             " - Set or clear a breakpoint\n"
178             "break <address> - Break at an address\n"
179             "break off <bp#> - Disable breakpoint.\n"
180             "break show      - List breakpoints.");
181
182 /* ======================================================================
183  * Called from the debug interrupt routine.  Simply calls the CPU-specific
184  * breakpoint handling routine.
185  * ====================================================================== */
186
187 void do_bedbug_breakpoint (struct pt_regs *regs)
188 {
189         /* -------------------------------------------------- */
190
191         if (bug_ctx.break_isr)
192                 (*bug_ctx.break_isr) (regs);
193
194         return;
195 }                               /* do_bedbug_breakpoint */
196
197
198
199 /* ======================================================================
200  * Called from the CPU-specific breakpoint handling routine.  Enter a
201  * mini main loop until the stopped flag is cleared from the breakpoint
202  * context.
203  *
204  * This handles the parts of the debugger that are common to all CPU's.
205  * ====================================================================== */
206
207 void bedbug_main_loop (unsigned long addr, struct pt_regs *regs)
208 {
209         int len;                /* Length of command line */
210         int flag;               /* Command flags          */
211         int rc = 0;             /* Result from run_command */
212         char prompt_str[20];    /* Prompt string          */
213         static char lastcommand[CONFIG_SYS_CBSIZE] = { 0 };     /* previous command */
214         /* -------------------------------------------------- */
215
216         if (bug_ctx.clear)
217                 (*bug_ctx.clear) (bug_ctx.current_bp);
218
219         printf ("Breakpoint %d: ", bug_ctx.current_bp);
220         disppc ((unsigned char *) addr, 0, 1, bedbug_puts, F_RADHEX);
221
222         bug_ctx.stopped = 1;
223         bug_ctx.regs = regs;
224
225         sprintf (prompt_str, "BEDBUG.%d =>", bug_ctx.current_bp);
226
227         /* A miniature main loop */
228         while (bug_ctx.stopped) {
229                 len = cli_readline(prompt_str);
230
231                 flag = 0;       /* assume no special flags for now */
232
233                 if (len > 0)
234                         strcpy (lastcommand, console_buffer);
235                 else if (len == 0)
236                         flag |= CMD_FLAG_REPEAT;
237
238                 if (len == -1)
239                         printf ("<INTERRUPT>\n");
240                 else
241                         rc = run_command_repeatable(lastcommand, flag);
242
243                 if (rc <= 0) {
244                         /* invalid command or not repeatable, forget it */
245                         lastcommand[0] = 0;
246                 }
247         }
248
249         bug_ctx.regs = NULL;
250         bug_ctx.current_bp = 0;
251
252         return;
253 }                               /* bedbug_main_loop */
254
255
256
257 /* ======================================================================
258  * Interpreter command to continue from a breakpoint.  Just clears the
259  * stopped flag in the context so that the breakpoint routine will
260  * return.
261  * ====================================================================== */
262 int do_bedbug_continue (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
263 {
264         /* -------------------------------------------------- */
265
266         if (!bug_ctx.stopped) {
267                 printf ("Not at a breakpoint\n");
268                 return 1;
269         }
270
271         bug_ctx.stopped = 0;
272         return 0;
273 }                               /* do_bedbug_continue */
274
275 U_BOOT_CMD (continue, 1, 0, do_bedbug_continue,
276             "continue from a breakpoint",
277             "");
278
279 /* ======================================================================
280  * Interpreter command to continue to the next instruction, stepping into
281  * subroutines.  Works by calling the find_next_addr() routine to compute
282  * the address passes control to the CPU-specific set breakpoint routine
283  * for the current breakpoint number.
284  * ====================================================================== */
285 int do_bedbug_step (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
286 {
287         unsigned long addr;     /* Address to stop at */
288
289         /* -------------------------------------------------- */
290
291         if (!bug_ctx.stopped) {
292                 printf ("Not at a breakpoint\n");
293                 return 1;
294         }
295
296         if (!find_next_address((unsigned char *) &addr, false, bug_ctx.regs))
297                 return 1;
298
299         if (bug_ctx.set)
300                 (*bug_ctx.set) (bug_ctx.current_bp, addr);
301
302         bug_ctx.stopped = 0;
303         return 0;
304 }                               /* do_bedbug_step */
305
306 U_BOOT_CMD (step, 1, 1, do_bedbug_step,
307             "single step execution.",
308             "");
309
310 /* ======================================================================
311  * Interpreter command to continue to the next instruction, stepping over
312  * subroutines.  Works by calling the find_next_addr() routine to compute
313  * the address passes control to the CPU-specific set breakpoint routine
314  * for the current breakpoint number.
315  * ====================================================================== */
316 int do_bedbug_next (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
317 {
318         unsigned long addr;     /* Address to stop at */
319
320         /* -------------------------------------------------- */
321
322         if (!bug_ctx.stopped) {
323                 printf ("Not at a breakpoint\n");
324                 return 1;
325         }
326
327         if (!find_next_address((unsigned char *) &addr, true, bug_ctx.regs))
328                 return 1;
329
330         if (bug_ctx.set)
331                 (*bug_ctx.set) (bug_ctx.current_bp, addr);
332
333         bug_ctx.stopped = 0;
334         return 0;
335 }                               /* do_bedbug_next */
336
337 U_BOOT_CMD (next, 1, 1, do_bedbug_next,
338             "single step execution, stepping over subroutines.",
339             "");
340
341 /* ======================================================================
342  * Interpreter command to print the current stack.  This assumes an EABI
343  * architecture, so it starts with GPR R1 and works back up the stack.
344  * ====================================================================== */
345 int do_bedbug_stack (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
346 {
347         unsigned long sp;       /* Stack pointer                */
348         unsigned long func;     /* LR from stack                */
349         int depth;              /* Stack iteration level        */
350         int skip = 1;           /* Flag to skip the first entry */
351         unsigned long top;      /* Top of memory address        */
352
353         /* -------------------------------------------------- */
354
355         if (!bug_ctx.stopped) {
356                 printf ("Not at a breakpoint\n");
357                 return 1;
358         }
359
360         top = gd->bd->bi_memstart + gd->bd->bi_memsize;
361         depth = 0;
362
363         printf ("Depth     PC\n");
364         printf ("-----  --------\n");
365         printf ("%5d  %08lx\n", depth++, bug_ctx.regs->nip);
366
367         sp = bug_ctx.regs->gpr[1];
368         func = *(unsigned long *) (sp + 4);
369
370         while ((func < top) && (sp < top)) {
371                 if (!skip)
372                         printf ("%5d  %08lx\n", depth++, func);
373                 else
374                         --skip;
375
376                 sp = *(unsigned long *) sp;
377                 func = *(unsigned long *) (sp + 4);
378         }
379         return 0;
380 }                               /* do_bedbug_stack */
381
382 U_BOOT_CMD (where, 1, 1, do_bedbug_stack,
383             "Print the running stack.",
384             "");
385
386 /* ======================================================================
387  * Interpreter command to dump the registers.  Calls the CPU-specific
388  * show registers routine.
389  * ====================================================================== */
390 int do_bedbug_rdump (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
391 {
392         /* -------------------------------------------------- */
393
394         if (!bug_ctx.stopped) {
395                 printf ("Not at a breakpoint\n");
396                 return 1;
397         }
398
399         show_regs (bug_ctx.regs);
400         return 0;
401 }                               /* do_bedbug_rdump */
402
403 U_BOOT_CMD (rdump, 1, 1, do_bedbug_rdump,
404             "Show registers.", "");
405 /* ====================================================================== */
406
407
408 /*
409  * Copyright (c) 2001 William L. Pitts
410  * All rights reserved.
411  *
412  * Redistribution and use in source and binary forms are freely
413  * permitted provided that the above copyright notice and this
414  * paragraph and the following disclaimer are duplicated in all
415  * such forms.
416  *
417  * This software is provided "AS IS" and without any express or
418  * implied warranties, including, without limitation, the implied
419  * warranties of merchantability and fitness for a particular
420  * purpose.
421  */