rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 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 | #include <arch/mmu.h> |
| 41 | #endif |
| 42 | |
| 43 | static int cmd_display_mem(int argc, const cmd_args *argv); |
| 44 | static int cmd_modify_mem(int argc, const cmd_args *argv); |
| 45 | static int cmd_fill_mem(int argc, const cmd_args *argv); |
| 46 | static int cmd_reset(int argc, const cmd_args *argv); |
| 47 | static int cmd_memtest(int argc, const cmd_args *argv); |
| 48 | static int cmd_copy_mem(int argc, const cmd_args *argv); |
| 49 | static int cmd_chain(int argc, const cmd_args *argv); |
| 50 | static int cmd_sleep(int argc, const cmd_args *argv); |
| 51 | static int cmd_crash(int argc, const cmd_args *argv); |
| 52 | static int cmd_stackstomp(int argc, const cmd_args *argv); |
| 53 | |
| 54 | STATIC_COMMAND_START |
| 55 | #if LK_DEBUGLEVEL > 0 |
| 56 | STATIC_COMMAND_MASKED("dw", "display memory in words", &cmd_display_mem, CMD_AVAIL_ALWAYS) |
| 57 | STATIC_COMMAND_MASKED("dh", "display memory in halfwords", &cmd_display_mem, CMD_AVAIL_ALWAYS) |
| 58 | STATIC_COMMAND_MASKED("db", "display memory in bytes", &cmd_display_mem, CMD_AVAIL_ALWAYS) |
| 59 | STATIC_COMMAND_MASKED("mw", "modify word of memory", &cmd_modify_mem, CMD_AVAIL_ALWAYS) |
| 60 | STATIC_COMMAND_MASKED("mh", "modify halfword of memory", &cmd_modify_mem, CMD_AVAIL_ALWAYS) |
| 61 | STATIC_COMMAND_MASKED("mb", "modify byte of memory", &cmd_modify_mem, CMD_AVAIL_ALWAYS) |
| 62 | STATIC_COMMAND_MASKED("fw", "fill range of memory by word", &cmd_fill_mem, CMD_AVAIL_ALWAYS) |
| 63 | STATIC_COMMAND_MASKED("fh", "fill range of memory by halfword", &cmd_fill_mem, CMD_AVAIL_ALWAYS) |
| 64 | STATIC_COMMAND_MASKED("fb", "fill range of memory by byte", &cmd_fill_mem, CMD_AVAIL_ALWAYS) |
| 65 | STATIC_COMMAND_MASKED("mc", "copy a range of memory", &cmd_copy_mem, CMD_AVAIL_ALWAYS) |
| 66 | STATIC_COMMAND("crash", "intentionally crash", &cmd_crash) |
| 67 | STATIC_COMMAND("stackstomp", "intentionally overrun the stack", &cmd_stackstomp) |
| 68 | #endif |
| 69 | #if LK_DEBUGLEVEL > 1 |
| 70 | STATIC_COMMAND("mtest", "simple memory test", &cmd_memtest) |
| 71 | #endif |
| 72 | STATIC_COMMAND("chain", "chain load another binary", &cmd_chain) |
| 73 | STATIC_COMMAND("sleep", "sleep number of seconds", &cmd_sleep) |
| 74 | STATIC_COMMAND("sleepm", "sleep number of milliseconds", &cmd_sleep) |
| 75 | STATIC_COMMAND_END(mem); |
| 76 | |
| 77 | static int cmd_display_mem(int argc, const cmd_args *argv) |
| 78 | { |
| 79 | /* save the last address and len so we can continue where we left off */ |
| 80 | static unsigned long address; |
| 81 | static size_t len; |
| 82 | |
| 83 | if (argc < 3 && len == 0) { |
| 84 | printf("not enough arguments\n"); |
| 85 | printf("%s [-l] [-b] [address] [length]\n", argv[0].str); |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | int size; |
| 90 | if (strcmp(argv[0].str, "dw") == 0) { |
| 91 | size = 4; |
| 92 | } else if (strcmp(argv[0].str, "dh") == 0) { |
| 93 | size = 2; |
| 94 | } else { |
| 95 | size = 1; |
| 96 | } |
| 97 | |
| 98 | uint byte_order = BYTE_ORDER; |
| 99 | int argindex = 1; |
| 100 | bool read_address = false; |
| 101 | while (argc > argindex) { |
| 102 | if (!strcmp(argv[argindex].str, "-l")) { |
| 103 | byte_order = LITTLE_ENDIAN; |
| 104 | } else if (!strcmp(argv[argindex].str, "-b")) { |
| 105 | byte_order = BIG_ENDIAN; |
| 106 | } else if (!read_address) { |
| 107 | address = argv[argindex].u; |
| 108 | read_address = true; |
| 109 | } else { |
| 110 | len = argv[argindex].u; |
| 111 | } |
| 112 | |
| 113 | argindex++; |
| 114 | } |
| 115 | |
| 116 | unsigned long stop = address + len; |
| 117 | int count = 0; |
| 118 | |
| 119 | if ((address & (size - 1)) != 0) { |
| 120 | printf("unaligned address, cannot display\n"); |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | #if WITH_KERNEL_VM |
| 125 | /* preflight the start address to see if it's mapped */ |
| 126 | if (arch_mmu_query((vaddr_t)address, NULL, NULL) < 0) { |
| 127 | printf("ERROR: address 0x%lx is unmapped\n", address); |
| 128 | return -1; |
| 129 | } |
| 130 | #endif |
| 131 | |
| 132 | for ( ; address < stop; address += size) { |
| 133 | if (count == 0) |
| 134 | printf("0x%08lx: ", address); |
| 135 | switch (size) { |
| 136 | case 4: { |
| 137 | uint32_t val = (byte_order != BYTE_ORDER) ? |
| 138 | SWAP_32(*(uint32_t *)address) : |
| 139 | *(uint32_t *)address; |
| 140 | printf("%08x ", val); |
| 141 | break; |
| 142 | } |
| 143 | case 2: { |
| 144 | uint16_t val = (byte_order != BYTE_ORDER) ? |
| 145 | SWAP_16(*(uint16_t *)address) : |
| 146 | *(uint16_t *)address; |
| 147 | printf("%04hx ", val); |
| 148 | break; |
| 149 | } |
| 150 | case 1: |
| 151 | printf("%02hhx ", *(uint8_t *)address); |
| 152 | break; |
| 153 | } |
| 154 | count += size; |
| 155 | if (count == 16) { |
| 156 | printf("\n"); |
| 157 | count = 0; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (count != 0) |
| 162 | printf("\n"); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static int cmd_modify_mem(int argc, const cmd_args *argv) |
| 168 | { |
| 169 | int size; |
| 170 | |
| 171 | if (argc < 3) { |
| 172 | printf("not enough arguments\n"); |
| 173 | printf("%s <address> <val>\n", argv[0].str); |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | if (strcmp(argv[0].str, "mw") == 0) { |
| 178 | size = 4; |
| 179 | } else if (strcmp(argv[0].str, "mh") == 0) { |
| 180 | size = 2; |
| 181 | } else { |
| 182 | size = 1; |
| 183 | } |
| 184 | |
| 185 | unsigned long address = argv[1].u; |
| 186 | unsigned int val = argv[2].u; |
| 187 | |
| 188 | if ((address & (size - 1)) != 0) { |
| 189 | printf("unaligned address, cannot modify\n"); |
| 190 | return -1; |
| 191 | } |
| 192 | |
| 193 | switch (size) { |
| 194 | case 4: |
| 195 | *(uint32_t *)address = (uint32_t)val; |
| 196 | break; |
| 197 | case 2: |
| 198 | *(uint16_t *)address = (uint16_t)val; |
| 199 | break; |
| 200 | case 1: |
| 201 | *(uint8_t *)address = (uint8_t)val; |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static int cmd_fill_mem(int argc, const cmd_args *argv) |
| 209 | { |
| 210 | int size; |
| 211 | |
| 212 | if (argc < 4) { |
| 213 | printf("not enough arguments\n"); |
| 214 | printf("%s <address> <len> <val>\n", argv[0].str); |
| 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | if (strcmp(argv[0].str, "fw") == 0) { |
| 219 | size = 4; |
| 220 | } else if (strcmp(argv[0].str, "fh") == 0) { |
| 221 | size = 2; |
| 222 | } else { |
| 223 | size = 1; |
| 224 | } |
| 225 | |
| 226 | unsigned long address = argv[1].u; |
| 227 | unsigned long len = argv[2].u; |
| 228 | unsigned long stop = address + len; |
| 229 | unsigned int val = argv[3].u; |
| 230 | |
| 231 | if ((address & (size - 1)) != 0) { |
| 232 | printf("unaligned address, cannot modify\n"); |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | for ( ; address < stop; address += size) { |
| 237 | switch (size) { |
| 238 | case 4: |
| 239 | *(uint32_t *)address = (uint32_t)val; |
| 240 | break; |
| 241 | case 2: |
| 242 | *(uint16_t *)address = (uint16_t)val; |
| 243 | break; |
| 244 | case 1: |
| 245 | *(uint8_t *)address = (uint8_t)val; |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int cmd_copy_mem(int argc, const cmd_args *argv) |
| 254 | { |
| 255 | if (argc < 4) { |
| 256 | printf("not enough arguments\n"); |
| 257 | printf("%s <source address> <target address> <len>\n", argv[0].str); |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | addr_t source = argv[1].u; |
| 262 | addr_t target = argv[2].u; |
| 263 | size_t len = argv[3].u; |
| 264 | |
| 265 | memcpy((void *)target, (const void *)source, len); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int cmd_memtest(int argc, const cmd_args *argv) |
| 271 | { |
| 272 | if (argc < 3) { |
| 273 | printf("not enough arguments\n"); |
| 274 | printf("%s <base> <len>\n", argv[0].str); |
| 275 | return -1; |
| 276 | } |
| 277 | |
| 278 | uint32_t *ptr; |
| 279 | size_t len; |
| 280 | |
| 281 | ptr = (uint32_t *)argv[1].u; |
| 282 | len = (size_t)argv[2].u; |
| 283 | |
| 284 | size_t i; |
| 285 | // write out |
| 286 | printf("writing first pass..."); |
| 287 | for (i = 0; i < len / 4; i++) { |
| 288 | ptr[i] = i; |
| 289 | } |
| 290 | printf("done\n"); |
| 291 | |
| 292 | // verify |
| 293 | printf("verifying..."); |
| 294 | for (i = 0; i < len / 4; i++) { |
| 295 | if (ptr[i] != i) |
| 296 | printf("error at %p\n", &ptr[i]); |
| 297 | } |
| 298 | printf("done\n"); |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int cmd_chain(int argc, const cmd_args *argv) |
| 304 | { |
| 305 | if (argc < 2) { |
| 306 | printf("not enough arguments\n"); |
| 307 | printf("%s <address>\n", argv[0].str); |
| 308 | return -1; |
| 309 | } |
| 310 | |
| 311 | arch_chain_load(argv[1].p, 0, 0, 0, 0); |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static int cmd_sleep(int argc, const cmd_args *argv) |
| 317 | { |
| 318 | lk_time_t t = 1000; /* default to 1 second */ |
| 319 | |
| 320 | if (argc >= 2) { |
| 321 | t = argv[1].u; |
| 322 | if (!strcmp(argv[0].str, "sleep")) |
| 323 | t *= 1000; |
| 324 | } |
| 325 | |
| 326 | thread_sleep(t); |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | static int cmd_crash(int argc, const cmd_args *argv) |
| 332 | { |
| 333 | /* should crash */ |
| 334 | volatile uint32_t *ptr = (void *)1; |
| 335 | *ptr = 1; |
| 336 | |
| 337 | /* if it didn't, panic the system */ |
| 338 | panic("crash"); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static int cmd_stackstomp(int argc, const cmd_args *argv) |
| 344 | { |
| 345 | for (size_t i = 0; i < DEFAULT_STACK_SIZE * 2; i++) { |
| 346 | uint8_t death[i]; |
| 347 | |
| 348 | memset(death, 0xaa, i); |
| 349 | thread_sleep(1); |
| 350 | } |
| 351 | |
| 352 | printf("survived.\n"); |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | |