1 /*
2  * Copyright (c) 2008-2012 Travis Geiselbrecht
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <ctype.h>
25 #include <debug.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <list.h>
29 #include <string.h>
30 #include <arch/ops.h>
31 #include <platform.h>
32 #include <platform/debug.h>
33 #include <kernel/thread.h>
34 #include <arch.h>
35 
36 #include <lib/console.h>
37 
38 #if WITH_KERNEL_VM
39 #include <kernel/vm.h>
40 #endif
41 
42 static int cmd_display_mem(int argc, const cmd_args *argv);
43 static int cmd_modify_mem(int argc, const cmd_args *argv);
44 static int cmd_fill_mem(int argc, const cmd_args *argv);
45 static int cmd_reset(int argc, const cmd_args *argv);
46 static int cmd_memtest(int argc, const cmd_args *argv);
47 static int cmd_copy_mem(int argc, const cmd_args *argv);
48 static int cmd_chain(int argc, const cmd_args *argv);
49 static int cmd_sleep(int argc, const cmd_args *argv);
50 static int cmd_crash(int argc, const cmd_args *argv);
51 static int cmd_stackstomp(int argc, const cmd_args *argv);
52 
53 STATIC_COMMAND_START
54 #if LK_DEBUGLEVEL > 0
55 STATIC_COMMAND_MASKED("dw", "display memory in words", &cmd_display_mem, CMD_AVAIL_ALWAYS)
56 STATIC_COMMAND_MASKED("dh", "display memory in halfwords", &cmd_display_mem, CMD_AVAIL_ALWAYS)
57 STATIC_COMMAND_MASKED("db", "display memory in bytes", &cmd_display_mem, CMD_AVAIL_ALWAYS)
58 STATIC_COMMAND_MASKED("mw", "modify word of memory", &cmd_modify_mem, CMD_AVAIL_ALWAYS)
59 STATIC_COMMAND_MASKED("mh", "modify halfword of memory", &cmd_modify_mem, CMD_AVAIL_ALWAYS)
60 STATIC_COMMAND_MASKED("mb", "modify byte of memory", &cmd_modify_mem, CMD_AVAIL_ALWAYS)
61 STATIC_COMMAND_MASKED("fw", "fill range of memory by word", &cmd_fill_mem, CMD_AVAIL_ALWAYS)
62 STATIC_COMMAND_MASKED("fh", "fill range of memory by halfword", &cmd_fill_mem, CMD_AVAIL_ALWAYS)
63 STATIC_COMMAND_MASKED("fb", "fill range of memory by byte", &cmd_fill_mem, CMD_AVAIL_ALWAYS)
64 STATIC_COMMAND_MASKED("mc", "copy a range of memory", &cmd_copy_mem, CMD_AVAIL_ALWAYS)
65 STATIC_COMMAND("crash", "intentionally crash", &cmd_crash)
66 STATIC_COMMAND("stackstomp", "intentionally overrun the stack", &cmd_stackstomp)
67 #endif
68 #if LK_DEBUGLEVEL > 1
69 STATIC_COMMAND("mtest", "simple memory test", &cmd_memtest)
70 #endif
71 STATIC_COMMAND("chain", "chain load another binary", &cmd_chain)
72 STATIC_COMMAND("sleep", "sleep number of seconds", &cmd_sleep)
73 STATIC_COMMAND("sleepm", "sleep number of milliseconds", &cmd_sleep)
74 STATIC_COMMAND_END(mem);
75 
cmd_display_mem(int argc,const cmd_args * argv)76 static int cmd_display_mem(int argc, const cmd_args *argv)
77 {
78     /* save the last address and len so we can continue where we left off */
79     static unsigned long address;
80     static size_t len;
81 
82     if (argc < 3 && len == 0) {
83         printf("not enough arguments\n");
84         printf("%s [-l] [-b] [address] [length]\n", argv[0].str);
85         return -1;
86     }
87 
88     int size;
89     if (strcmp(argv[0].str, "dw") == 0) {
90         size = 4;
91     } else if (strcmp(argv[0].str, "dh") == 0) {
92         size = 2;
93     } else {
94         size = 1;
95     }
96 
97     uint byte_order = BYTE_ORDER;
98     int argindex = 1;
99     bool read_address = false;
100     while (argc > argindex) {
101         if (!strcmp(argv[argindex].str, "-l")) {
102             byte_order = LITTLE_ENDIAN;
103         } else if (!strcmp(argv[argindex].str, "-b")) {
104             byte_order = BIG_ENDIAN;
105         } else if (!read_address) {
106             address = argv[argindex].u;
107             read_address = true;
108         } else {
109             len = argv[argindex].u;
110         }
111 
112         argindex++;
113     }
114 
115     unsigned long stop = address + len;
116     int count = 0;
117 
118     if ((address & (size - 1)) != 0) {
119         printf("unaligned address, cannot display\n");
120         return -1;
121     }
122 
123 #if WITH_KERNEL_VM
124     /* preflight the start address to see if it's mapped */
125     if (vaddr_to_paddr((void *)address) == 0) {
126         printf("ERROR: address 0x%lx is unmapped\n", address);
127         return -1;
128     }
129 #endif
130 
131     for ( ; address < stop; address += size) {
132         if (count == 0)
133             printf("0x%08lx: ", address);
134         switch (size) {
135             case 4: {
136                 uint32_t val = (byte_order != BYTE_ORDER) ?
137                                SWAP_32(*(uint32_t *)address) :
138                                *(uint32_t *)address;
139                 printf("%08x ", val);
140                 break;
141             }
142             case 2: {
143                 uint16_t val = (byte_order != BYTE_ORDER) ?
144                                SWAP_16(*(uint16_t *)address) :
145                                *(uint16_t *)address;
146                 printf("%04hx ", val);
147                 break;
148             }
149             case 1:
150                 printf("%02hhx ", *(uint8_t *)address);
151                 break;
152         }
153         count += size;
154         if (count == 16) {
155             printf("\n");
156             count = 0;
157         }
158     }
159 
160     if (count != 0)
161         printf("\n");
162 
163     return 0;
164 }
165 
cmd_modify_mem(int argc,const cmd_args * argv)166 static int cmd_modify_mem(int argc, const cmd_args *argv)
167 {
168     int size;
169 
170     if (argc < 3) {
171         printf("not enough arguments\n");
172         printf("%s <address> <val>\n", argv[0].str);
173         return -1;
174     }
175 
176     if (strcmp(argv[0].str, "mw") == 0) {
177         size = 4;
178     } else if (strcmp(argv[0].str, "mh") == 0) {
179         size = 2;
180     } else {
181         size = 1;
182     }
183 
184     unsigned long address = argv[1].u;
185     unsigned int val = argv[2].u;
186 
187     if ((address & (size - 1)) != 0) {
188         printf("unaligned address, cannot modify\n");
189         return -1;
190     }
191 
192     switch (size) {
193         case 4:
194             *(uint32_t *)address = (uint32_t)val;
195             break;
196         case 2:
197             *(uint16_t *)address = (uint16_t)val;
198             break;
199         case 1:
200             *(uint8_t *)address = (uint8_t)val;
201             break;
202     }
203 
204     return 0;
205 }
206 
cmd_fill_mem(int argc,const cmd_args * argv)207 static int cmd_fill_mem(int argc, const cmd_args *argv)
208 {
209     int size;
210 
211     if (argc < 4) {
212         printf("not enough arguments\n");
213         printf("%s <address> <len> <val>\n", argv[0].str);
214         return -1;
215     }
216 
217     if (strcmp(argv[0].str, "fw") == 0) {
218         size = 4;
219     } else if (strcmp(argv[0].str, "fh") == 0) {
220         size = 2;
221     } else {
222         size = 1;
223     }
224 
225     unsigned long address = argv[1].u;
226     unsigned long len = argv[2].u;
227     unsigned long stop = address + len;
228     unsigned int val = argv[3].u;
229 
230     if ((address & (size - 1)) != 0) {
231         printf("unaligned address, cannot modify\n");
232         return -1;
233     }
234 
235     for ( ; address < stop; address += size) {
236         switch (size) {
237             case 4:
238                 *(uint32_t *)address = (uint32_t)val;
239                 break;
240             case 2:
241                 *(uint16_t *)address = (uint16_t)val;
242                 break;
243             case 1:
244                 *(uint8_t *)address = (uint8_t)val;
245                 break;
246         }
247     }
248 
249     return 0;
250 }
251 
cmd_copy_mem(int argc,const cmd_args * argv)252 static int cmd_copy_mem(int argc, const cmd_args *argv)
253 {
254     if (argc < 4) {
255         printf("not enough arguments\n");
256         printf("%s <source address> <target address> <len>\n", argv[0].str);
257         return -1;
258     }
259 
260     addr_t source = argv[1].u;
261     addr_t target = argv[2].u;
262     size_t len = argv[3].u;
263 
264     memcpy((void *)target, (const void *)source, len);
265 
266     return 0;
267 }
268 
cmd_memtest(int argc,const cmd_args * argv)269 static int cmd_memtest(int argc, const cmd_args *argv)
270 {
271     if (argc < 3) {
272         printf("not enough arguments\n");
273         printf("%s <base> <len>\n", argv[0].str);
274         return -1;
275     }
276 
277     uint32_t *ptr;
278     size_t len;
279 
280     ptr = (uint32_t *)argv[1].u;
281     len = (size_t)argv[2].u;
282 
283     size_t i;
284     // write out
285     printf("writing first pass...");
286     for (i = 0; i < len / 4; i++) {
287         ptr[i] = i;
288     }
289     printf("done\n");
290 
291     // verify
292     printf("verifying...");
293     for (i = 0; i < len / 4; i++) {
294         if (ptr[i] != i)
295             printf("error at %p\n", &ptr[i]);
296     }
297     printf("done\n");
298 
299     return 0;
300 }
301 
cmd_chain(int argc,const cmd_args * argv)302 static int cmd_chain(int argc, const cmd_args *argv)
303 {
304     if (argc < 2) {
305         printf("not enough arguments\n");
306         printf("%s <address>\n", argv[0].str);
307         return -1;
308     }
309 
310     arch_chain_load(argv[1].p, 0, 0, 0, 0);
311 
312     return 0;
313 }
314 
cmd_sleep(int argc,const cmd_args * argv)315 static int cmd_sleep(int argc, const cmd_args *argv)
316 {
317     lk_time_t t = 1000; /* default to 1 second */
318 
319     if (argc >= 2) {
320         t = argv[1].u;
321         if (!strcmp(argv[0].str, "sleep"))
322             t *= 1000;
323     }
324 
325     thread_sleep(t);
326 
327     return 0;
328 }
329 
cmd_crash(int argc,const cmd_args * argv)330 static int cmd_crash(int argc, const cmd_args *argv)
331 {
332     /* should crash */
333     volatile uint32_t *ptr = (void *)1;
334     *ptr = 1;
335 
336     /* if it didn't, panic the system */
337     panic("crash");
338 
339     return 0;
340 }
341 
cmd_stackstomp(int argc,const cmd_args * argv)342 static int cmd_stackstomp(int argc, const cmd_args *argv)
343 {
344     for (size_t i = 0; i < DEFAULT_STACK_SIZE * 2; i++) {
345         uint8_t death[i];
346 
347         memset(death, 0xaa, i);
348         thread_sleep(1);
349     }
350 
351     printf("survived.\n");
352 
353     return 0;
354 }
355 
356 
357