Rev 585 | Rev 591 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 510 | jermar | 1 | /* |
| 2 | * Copyright (C) 2005 Jakub Jermar |
||
| 3 | * All rights reserved. |
||
| 4 | * |
||
| 5 | * Redistribution and use in source and binary forms, with or without |
||
| 6 | * modification, are permitted provided that the following conditions |
||
| 7 | * are met: |
||
| 8 | * |
||
| 9 | * - Redistributions of source code must retain the above copyright |
||
| 10 | * notice, this list of conditions and the following disclaimer. |
||
| 11 | * - Redistributions in binary form must reproduce the above copyright |
||
| 12 | * notice, this list of conditions and the following disclaimer in the |
||
| 13 | * documentation and/or other materials provided with the distribution. |
||
| 14 | * - The name of the author may not be used to endorse or promote products |
||
| 15 | * derived from this software without specific prior written permission. |
||
| 16 | * |
||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 27 | */ |
||
| 28 | |||
| 518 | jermar | 29 | #include <console/kconsole.h> |
| 510 | jermar | 30 | #include <console/console.h> |
| 31 | #include <console/chardev.h> |
||
| 32 | #include <print.h> |
||
| 517 | jermar | 33 | #include <panic.h> |
| 510 | jermar | 34 | #include <typedefs.h> |
| 35 | #include <arch/types.h> |
||
| 517 | jermar | 36 | #include <list.h> |
| 37 | #include <arch.h> |
||
| 38 | #include <func.h> |
||
| 39 | #include <macros.h> |
||
| 518 | jermar | 40 | #include <debug.h> |
| 582 | palkovsky | 41 | #include <symtab.h> |
| 510 | jermar | 42 | |
| 517 | jermar | 43 | #define MAX_CMDLINE 256 |
| 44 | |||
| 45 | /** Simple kernel console. |
||
| 46 | * |
||
| 47 | * The console is realized by kernel thread kconsole. |
||
| 518 | jermar | 48 | * It doesn't understand any useful command on its own, |
| 49 | * but makes it possible for other kernel subsystems to |
||
| 517 | jermar | 50 | * register their own commands. |
| 51 | */ |
||
| 52 | |||
| 53 | /** Locking. |
||
| 54 | * |
||
| 55 | * There is a list of cmd_info_t structures. This list |
||
| 56 | * is protected by cmd_lock spinlock. Note that specially |
||
| 57 | * the link elements of cmd_info_t are protected by |
||
| 58 | * this lock. |
||
| 59 | * |
||
| 60 | * Each cmd_info_t also has its own lock, which protects |
||
| 61 | * all elements thereof except the link element. |
||
| 62 | * |
||
| 63 | * cmd_lock must be acquired before any cmd_info lock. |
||
| 64 | * When locking two cmd info structures, structure with |
||
| 65 | * lower address must be locked first. |
||
| 66 | */ |
||
| 67 | |||
| 68 | spinlock_t cmd_lock; /**< Lock protecting command list. */ |
||
| 69 | link_t cmd_head; /**< Command list. */ |
||
| 70 | |||
| 71 | static cmd_info_t *parse_cmdline(char *cmdline, size_t len); |
||
| 518 | jermar | 72 | static bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end); |
| 517 | jermar | 73 | |
| 518 | jermar | 74 | /** Data and methods for 'help' command. */ |
| 75 | static int cmd_help(cmd_arg_t *argv); |
||
| 582 | palkovsky | 76 | static cmd_info_t help_info = { |
| 77 | .name = "help", |
||
| 78 | .description = "List of supported commands.", |
||
| 79 | .func = cmd_help, |
||
| 80 | .argc = 0 |
||
| 81 | }; |
||
| 517 | jermar | 82 | |
| 518 | jermar | 83 | /** Data and methods for 'description' command. */ |
| 84 | static int cmd_desc(cmd_arg_t *argv); |
||
| 85 | static void desc_help(void); |
||
| 86 | static char desc_buf[MAX_CMDLINE+1]; |
||
| 87 | static cmd_arg_t desc_argv = { |
||
| 88 | .type = ARG_TYPE_STRING, |
||
| 89 | .buffer = desc_buf, |
||
| 90 | .len = sizeof(desc_buf) |
||
| 91 | }; |
||
| 582 | palkovsky | 92 | static cmd_info_t desc_info = { |
| 93 | .name = "describe", |
||
| 94 | .description = "Describe specified command.", |
||
| 95 | .help = desc_help, |
||
| 96 | .func = cmd_desc, |
||
| 97 | .argc = 1, |
||
| 98 | .argv = &desc_argv |
||
| 99 | }; |
||
| 518 | jermar | 100 | |
| 582 | palkovsky | 101 | /** Data and methods for 'symaddr' command. */ |
| 102 | static int cmd_symaddr(cmd_arg_t *argv); |
||
| 103 | static char symaddr_buf[MAX_CMDLINE+1]; |
||
| 104 | static cmd_arg_t symaddr_argv = { |
||
| 105 | .type = ARG_TYPE_STRING, |
||
| 106 | .buffer = symaddr_buf, |
||
| 107 | .len = sizeof(symaddr_buf) |
||
| 108 | }; |
||
| 109 | static cmd_info_t symaddr_info = { |
||
| 110 | .name = "symaddr", |
||
| 111 | .description = "Return symbol address.", |
||
| 112 | .func = cmd_symaddr, |
||
| 113 | .argc = 1, |
||
| 114 | .argv = &symaddr_argv |
||
| 115 | }; |
||
| 116 | |||
| 117 | /** Call0 - call function with no parameters */ |
||
| 118 | static char call0_buf[MAX_CMDLINE+1]; |
||
| 585 | palkovsky | 119 | static char carg1_buf[MAX_CMDLINE+1]; |
| 120 | static char carg2_buf[MAX_CMDLINE+1]; |
||
| 582 | palkovsky | 121 | |
| 122 | static int cmd_call0(cmd_arg_t *argv); |
||
| 123 | static cmd_arg_t call0_argv = { |
||
| 124 | .type = ARG_TYPE_STRING, |
||
| 125 | .buffer = call0_buf, |
||
| 126 | .len = sizeof(call0_buf) |
||
| 127 | }; |
||
| 128 | static cmd_info_t call0_info = { |
||
| 129 | .name = "call0", |
||
| 130 | .description = "call0 <function> -> call function().", |
||
| 131 | .func = cmd_call0, |
||
| 132 | .argc = 1, |
||
| 133 | .argv = &call0_argv |
||
| 134 | }; |
||
| 135 | |||
| 136 | static int cmd_call1(cmd_arg_t *argv); |
||
| 137 | static cmd_arg_t call1_argv[] = { |
||
| 138 | { |
||
| 139 | .type = ARG_TYPE_STRING, |
||
| 140 | .buffer = call0_buf, |
||
| 141 | .len = sizeof(call0_buf) |
||
| 142 | }, |
||
| 585 | palkovsky | 143 | { |
| 144 | .type = ARG_TYPE_VAR, |
||
| 145 | .buffer = carg1_buf, |
||
| 146 | .len = sizeof(carg1_buf) |
||
| 147 | } |
||
| 582 | palkovsky | 148 | }; |
| 149 | static cmd_info_t call1_info = { |
||
| 150 | .name = "call1", |
||
| 151 | .description = "call1 <function> <arg1> -> call function(arg1).", |
||
| 152 | .func = cmd_call1, |
||
| 153 | .argc = 2, |
||
| 154 | .argv = call1_argv |
||
| 155 | }; |
||
| 156 | |||
| 157 | static int cmd_call2(cmd_arg_t *argv); |
||
| 158 | static cmd_arg_t call2_argv[] = { |
||
| 159 | { |
||
| 160 | .type = ARG_TYPE_STRING, |
||
| 161 | .buffer = call0_buf, |
||
| 162 | .len = sizeof(call0_buf) |
||
| 163 | }, |
||
| 585 | palkovsky | 164 | { |
| 165 | .type = ARG_TYPE_VAR, |
||
| 166 | .buffer = carg1_buf, |
||
| 167 | .len = sizeof(carg1_buf) |
||
| 168 | }, |
||
| 169 | { |
||
| 170 | .type = ARG_TYPE_VAR, |
||
| 171 | .buffer = carg2_buf, |
||
| 172 | .len = sizeof(carg2_buf) |
||
| 173 | } |
||
| 582 | palkovsky | 174 | }; |
| 175 | static cmd_info_t call2_info = { |
||
| 176 | .name = "call2", |
||
| 177 | .description = "call2 <function> <arg1> <arg2> -> call function(arg1,arg2).", |
||
| 178 | .func = cmd_call2, |
||
| 179 | .argc = 3, |
||
| 180 | .argv = call2_argv |
||
| 181 | }; |
||
| 182 | |||
| 183 | |||
| 544 | decky | 184 | /** Data and methods for 'halt' command. */ |
| 185 | static int cmd_halt(cmd_arg_t *argv); |
||
| 582 | palkovsky | 186 | static cmd_info_t halt_info = { |
| 187 | .name = "halt", |
||
| 188 | .description = "Halt the kernel.", |
||
| 189 | .func = cmd_halt, |
||
| 190 | .argc = 0 |
||
| 191 | }; |
||
| 544 | decky | 192 | |
| 517 | jermar | 193 | /** Initialize kconsole data structures. */ |
| 194 | void kconsole_init(void) |
||
| 195 | { |
||
| 552 | palkovsky | 196 | spinlock_initialize(&cmd_lock, "kconsole_cmd"); |
| 517 | jermar | 197 | list_initialize(&cmd_head); |
| 198 | |||
| 552 | palkovsky | 199 | spinlock_initialize(&help_info.lock, "kconsole_help"); |
| 517 | jermar | 200 | link_initialize(&help_info.link); |
| 518 | jermar | 201 | if (!cmd_register(&help_info)) |
| 202 | panic("could not register command %s\n", help_info.name); |
||
| 203 | |||
| 204 | |||
| 552 | palkovsky | 205 | spinlock_initialize(&desc_info.lock, "kconsole_desc"); |
| 518 | jermar | 206 | link_initialize(&desc_info.link); |
| 207 | if (!cmd_register(&desc_info)) |
||
| 208 | panic("could not register command %s\n", desc_info.name); |
||
| 544 | decky | 209 | |
| 582 | palkovsky | 210 | spinlock_initialize(&symaddr_info.lock, "kconsole_symaddr"); |
| 211 | link_initialize(&symaddr_info.link); |
||
| 212 | if (!cmd_register(&symaddr_info)) |
||
| 213 | panic("could not register command %s\n", symaddr_info.name); |
||
| 214 | |||
| 215 | spinlock_initialize(&call0_info.lock, "kconsole_call0"); |
||
| 216 | link_initialize(&call0_info.link); |
||
| 217 | if (!cmd_register(&call0_info)) |
||
| 218 | panic("could not register command %s\n", call0_info.name); |
||
| 219 | |||
| 220 | spinlock_initialize(&call1_info.lock, "kconsole_call1"); |
||
| 221 | link_initialize(&call1_info.link); |
||
| 222 | if (!cmd_register(&call1_info)) |
||
| 223 | panic("could not register command %s\n", call1_info.name); |
||
| 224 | |||
| 225 | |||
| 226 | spinlock_initialize(&call2_info.lock, "kconsole_call2"); |
||
| 227 | link_initialize(&call2_info.link); |
||
| 228 | if (!cmd_register(&call2_info)) |
||
| 229 | panic("could not register command %s\n", call2_info.name); |
||
| 544 | decky | 230 | |
| 552 | palkovsky | 231 | spinlock_initialize(&halt_info.lock, "kconsole_halt"); |
| 544 | decky | 232 | link_initialize(&halt_info.link); |
| 233 | if (!cmd_register(&halt_info)) |
||
| 234 | panic("could not register command %s\n", halt_info.name); |
||
| 517 | jermar | 235 | } |
| 236 | |||
| 237 | |||
| 238 | /** Register kconsole command. |
||
| 239 | * |
||
| 240 | * @param cmd Structure describing the command. |
||
| 241 | * |
||
| 242 | * @return 0 on failure, 1 on success. |
||
| 243 | */ |
||
| 244 | int cmd_register(cmd_info_t *cmd) |
||
| 245 | { |
||
| 246 | ipl_t ipl; |
||
| 247 | link_t *cur; |
||
| 248 | |||
| 249 | spinlock_lock(&cmd_lock); |
||
| 250 | |||
| 251 | /* |
||
| 252 | * Make sure the command is not already listed. |
||
| 253 | */ |
||
| 254 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
||
| 255 | cmd_info_t *hlp; |
||
| 256 | |||
| 257 | hlp = list_get_instance(cur, cmd_info_t, link); |
||
| 258 | |||
| 259 | if (hlp == cmd) { |
||
| 260 | /* The command is already there. */ |
||
| 261 | spinlock_unlock(&cmd_lock); |
||
| 262 | return 0; |
||
| 263 | } |
||
| 264 | |||
| 265 | /* Avoid deadlock. */ |
||
| 266 | if (hlp < cmd) { |
||
| 267 | spinlock_lock(&hlp->lock); |
||
| 268 | spinlock_lock(&cmd->lock); |
||
| 269 | } else { |
||
| 270 | spinlock_lock(&cmd->lock); |
||
| 271 | spinlock_lock(&hlp->lock); |
||
| 272 | } |
||
| 273 | |||
| 518 | jermar | 274 | if ((strncmp(hlp->name, cmd->name, strlen(cmd->name)) == 0)) { |
| 517 | jermar | 275 | /* The command is already there. */ |
| 276 | spinlock_unlock(&hlp->lock); |
||
| 277 | spinlock_unlock(&cmd->lock); |
||
| 278 | spinlock_unlock(&cmd_lock); |
||
| 279 | return 0; |
||
| 280 | } |
||
| 281 | |||
| 282 | spinlock_unlock(&hlp->lock); |
||
| 283 | spinlock_unlock(&cmd->lock); |
||
| 284 | } |
||
| 285 | |||
| 286 | /* |
||
| 287 | * Now the command can be added. |
||
| 288 | */ |
||
| 289 | list_append(&cmd->link, &cmd_head); |
||
| 290 | |||
| 291 | spinlock_unlock(&cmd_lock); |
||
| 292 | return 1; |
||
| 293 | } |
||
| 294 | |||
| 510 | jermar | 295 | /** Kernel console managing thread. |
| 296 | * |
||
| 297 | * @param arg Not used. |
||
| 298 | */ |
||
| 299 | void kconsole(void *arg) |
||
| 300 | { |
||
| 517 | jermar | 301 | char cmdline[MAX_CMDLINE+1]; |
| 302 | cmd_info_t *cmd_info; |
||
| 303 | count_t len; |
||
| 510 | jermar | 304 | |
| 305 | if (!stdin) { |
||
| 306 | printf("%s: no stdin\n", __FUNCTION__); |
||
| 307 | return; |
||
| 308 | } |
||
| 309 | |||
| 310 | while (true) { |
||
| 311 | printf("%s> ", __FUNCTION__); |
||
| 518 | jermar | 312 | if (!(len = gets(stdin, cmdline, sizeof(cmdline)))) |
| 313 | continue; |
||
| 517 | jermar | 314 | cmdline[len] = '\0'; |
| 315 | cmd_info = parse_cmdline(cmdline, len); |
||
| 518 | jermar | 316 | if (!cmd_info) |
| 517 | jermar | 317 | continue; |
| 318 | (void) cmd_info->func(cmd_info->argv); |
||
| 510 | jermar | 319 | } |
| 320 | } |
||
| 517 | jermar | 321 | |
| 585 | palkovsky | 322 | static int parse_int_arg(char *text, size_t len, __native *result) |
| 323 | { |
||
| 324 | char symname[MAX_SYMBOL_NAME]; |
||
| 325 | __address symaddr; |
||
| 326 | bool isaddr = false; |
||
| 589 | palkovsky | 327 | bool isptr = false; |
| 585 | palkovsky | 328 | |
| 329 | /* If we get a name, try to find it in symbol table */ |
||
| 330 | if (text[0] < '0' | text[0] > '9') { |
||
| 331 | if (text[0] == '&') { |
||
| 332 | isaddr = true; |
||
| 333 | text++;len--; |
||
| 589 | palkovsky | 334 | } else if (text[0] == '*') { |
| 335 | isptr = true; |
||
| 336 | text++;len--; |
||
| 585 | palkovsky | 337 | } |
| 338 | strncpy(symname, text, min(len+1, MAX_SYMBOL_NAME)); |
||
| 339 | symaddr = get_symbol_addr(symname); |
||
| 340 | if (!symaddr) { |
||
| 341 | printf("Symbol %s not found.\n",symname); |
||
| 342 | return -1; |
||
| 343 | } |
||
| 344 | if (symaddr == (__address) -1) { |
||
| 345 | printf("Duplicate symbol %s.\n",symname); |
||
| 346 | symtab_print_search(symname); |
||
| 347 | return -1; |
||
| 348 | } |
||
| 349 | if (isaddr) |
||
| 350 | *result = (__native)symaddr; |
||
| 589 | palkovsky | 351 | else if (isptr) |
| 352 | *result = **((__native **)symaddr); |
||
| 585 | palkovsky | 353 | else |
| 354 | *result = *((__native *)symaddr); |
||
| 355 | } else /* It's a number - convert it */ |
||
| 356 | *result = atoi(text); |
||
| 357 | return 0; |
||
| 358 | } |
||
| 359 | |||
| 517 | jermar | 360 | /** Parse command line. |
| 361 | * |
||
| 362 | * @param cmdline Command line as read from input device. |
||
| 363 | * @param len Command line length. |
||
| 364 | * |
||
| 365 | * @return Structure describing the command. |
||
| 366 | */ |
||
| 367 | cmd_info_t *parse_cmdline(char *cmdline, size_t len) |
||
| 368 | { |
||
| 369 | index_t start = 0, end = 0; |
||
| 370 | cmd_info_t *cmd = NULL; |
||
| 371 | link_t *cur; |
||
| 372 | ipl_t ipl; |
||
| 373 | int i; |
||
| 374 | |||
| 518 | jermar | 375 | if (!parse_argument(cmdline, len, &start, &end)) { |
| 517 | jermar | 376 | /* Command line did not contain alphanumeric word. */ |
| 377 | return NULL; |
||
| 378 | } |
||
| 379 | |||
| 380 | spinlock_lock(&cmd_lock); |
||
| 381 | |||
| 382 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
||
| 383 | cmd_info_t *hlp; |
||
| 384 | |||
| 385 | hlp = list_get_instance(cur, cmd_info_t, link); |
||
| 386 | spinlock_lock(&hlp->lock); |
||
| 387 | |||
| 518 | jermar | 388 | if (strncmp(hlp->name, &cmdline[start], (end - start) + 1) == 0) { |
| 517 | jermar | 389 | cmd = hlp; |
| 390 | break; |
||
| 391 | } |
||
| 392 | |||
| 393 | spinlock_unlock(&hlp->lock); |
||
| 394 | } |
||
| 395 | |||
| 396 | spinlock_unlock(&cmd_lock); |
||
| 397 | |||
| 398 | if (!cmd) { |
||
| 399 | /* Unknown command. */ |
||
| 518 | jermar | 400 | printf("Unknown command.\n"); |
| 517 | jermar | 401 | return NULL; |
| 402 | } |
||
| 403 | |||
| 404 | /* cmd == hlp is locked */ |
||
| 405 | |||
| 406 | /* |
||
| 407 | * The command line must be further analyzed and |
||
| 408 | * the parameters therefrom must be matched and |
||
| 409 | * converted to those specified in the cmd info |
||
| 410 | * structure. |
||
| 411 | */ |
||
| 518 | jermar | 412 | |
| 413 | for (i = 0; i < cmd->argc; i++) { |
||
| 414 | char *buf; |
||
| 415 | start = end + 1; |
||
| 416 | if (!parse_argument(cmdline, len, &start, &end)) { |
||
| 417 | printf("Too few arguments.\n"); |
||
| 418 | spinlock_unlock(&cmd->lock); |
||
| 419 | return NULL; |
||
| 420 | } |
||
| 421 | |||
| 422 | switch (cmd->argv[i].type) { |
||
| 582 | palkovsky | 423 | case ARG_TYPE_STRING: |
| 518 | jermar | 424 | buf = cmd->argv[i].buffer; |
| 585 | palkovsky | 425 | strncpy(buf, (const char *) &cmdline[start], min((end - start) + 2, cmd->argv[i].len)); |
| 518 | jermar | 426 | buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0'; |
| 427 | break; |
||
| 585 | palkovsky | 428 | case ARG_TYPE_INT: |
| 429 | if (parse_int_arg(cmdline+start, end-start+1, |
||
| 430 | &cmd->argv[i].intval)) |
||
| 431 | return NULL; |
||
| 518 | jermar | 432 | break; |
| 585 | palkovsky | 433 | case ARG_TYPE_VAR: |
| 434 | if (start != end && cmdline[start] == '"' && cmdline[end] == '"') { |
||
| 435 | buf = cmd->argv[i].buffer; |
||
| 436 | strncpy(buf, (const char *) &cmdline[start+1], |
||
| 437 | min((end-start), cmd->argv[i].len)); |
||
| 438 | buf[min((end - start), cmd->argv[i].len - 1)] = '\0'; |
||
| 439 | cmd->argv[i].intval = (__native) buf; |
||
| 440 | cmd->argv[i].vartype = ARG_TYPE_STRING; |
||
| 441 | } else if (!parse_int_arg(cmdline+start, end-start+1, |
||
| 442 | &cmd->argv[i].intval)) |
||
| 443 | cmd->argv[i].vartype = ARG_TYPE_INT; |
||
| 444 | else { |
||
| 445 | printf("Unrecognized variable argument.\n"); |
||
| 446 | return NULL; |
||
| 582 | palkovsky | 447 | } |
| 585 | palkovsky | 448 | break; |
| 582 | palkovsky | 449 | case ARG_TYPE_INVALID: |
| 450 | default: |
||
| 451 | printf("invalid argument type\n"); |
||
| 452 | return NULL; |
||
| 453 | break; |
||
| 518 | jermar | 454 | } |
| 455 | } |
||
| 517 | jermar | 456 | |
| 518 | jermar | 457 | start = end + 1; |
| 458 | if (parse_argument(cmdline, len, &start, &end)) { |
||
| 459 | printf("Too many arguments.\n"); |
||
| 460 | spinlock_unlock(&cmd->lock); |
||
| 461 | return NULL; |
||
| 462 | } |
||
| 463 | |||
| 517 | jermar | 464 | spinlock_unlock(&cmd->lock); |
| 465 | return cmd; |
||
| 466 | } |
||
| 467 | |||
| 518 | jermar | 468 | /** Parse argument. |
| 469 | * |
||
| 470 | * Find start and end positions of command line argument. |
||
| 471 | * |
||
| 472 | * @param cmdline Command line as read from the input device. |
||
| 473 | * @param len Number of characters in cmdline. |
||
| 474 | * @param start On entry, 'start' contains pointer to the index |
||
| 475 | * of first unprocessed character of cmdline. |
||
| 476 | * On successful exit, it marks beginning of the next argument. |
||
| 477 | * @param end Undefined on entry. On exit, 'end' points to the last character |
||
| 478 | * of the next argument. |
||
| 479 | * |
||
| 480 | * @return false on failure, true on success. |
||
| 481 | */ |
||
| 482 | bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end) |
||
| 483 | { |
||
| 484 | int i; |
||
| 485 | bool found_start = false; |
||
| 486 | |||
| 487 | ASSERT(start != NULL); |
||
| 488 | ASSERT(end != NULL); |
||
| 489 | |||
| 490 | for (i = *start; i < len; i++) { |
||
| 491 | if (!found_start) { |
||
| 492 | if (is_white(cmdline[i])) |
||
| 493 | (*start)++; |
||
| 494 | else |
||
| 495 | found_start = true; |
||
| 496 | } else { |
||
| 497 | if (is_white(cmdline[i])) |
||
| 498 | break; |
||
| 499 | } |
||
| 500 | } |
||
| 501 | *end = i - 1; |
||
| 502 | |||
| 503 | return found_start; |
||
| 504 | } |
||
| 505 | |||
| 506 | |||
| 517 | jermar | 507 | /** List supported commands. |
| 508 | * |
||
| 518 | jermar | 509 | * @param argv Argument vector. |
| 517 | jermar | 510 | * |
| 511 | * @return 0 on failure, 1 on success. |
||
| 512 | */ |
||
| 518 | jermar | 513 | int cmd_help(cmd_arg_t *argv) |
| 517 | jermar | 514 | { |
| 515 | link_t *cur; |
||
| 516 | ipl_t ipl; |
||
| 517 | |||
| 518 | spinlock_lock(&cmd_lock); |
||
| 519 | |||
| 520 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
||
| 521 | cmd_info_t *hlp; |
||
| 522 | |||
| 523 | hlp = list_get_instance(cur, cmd_info_t, link); |
||
| 524 | spinlock_lock(&hlp->lock); |
||
| 525 | |||
| 518 | jermar | 526 | printf("%s - %s\n", hlp->name, hlp->description); |
| 517 | jermar | 527 | |
| 528 | spinlock_unlock(&hlp->lock); |
||
| 529 | } |
||
| 530 | |||
| 531 | spinlock_unlock(&cmd_lock); |
||
| 532 | |||
| 533 | return 1; |
||
| 534 | } |
||
| 518 | jermar | 535 | |
| 536 | /** Describe specified command. |
||
| 537 | * |
||
| 538 | * @param argv Argument vector. |
||
| 539 | * |
||
| 540 | * @return 0 on failure, 1 on success. |
||
| 541 | */ |
||
| 542 | int cmd_desc(cmd_arg_t *argv) |
||
| 543 | { |
||
| 544 | link_t *cur; |
||
| 545 | ipl_t ipl; |
||
| 546 | |||
| 547 | spinlock_lock(&cmd_lock); |
||
| 548 | |||
| 549 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
||
| 550 | cmd_info_t *hlp; |
||
| 551 | |||
| 552 | hlp = list_get_instance(cur, cmd_info_t, link); |
||
| 553 | spinlock_lock(&hlp->lock); |
||
| 554 | |||
| 555 | if (strncmp(hlp->name, (const char *) argv->buffer, strlen(hlp->name)) == 0) { |
||
| 556 | printf("%s - %s\n", hlp->name, hlp->description); |
||
| 557 | if (hlp->help) |
||
| 558 | hlp->help(); |
||
| 559 | spinlock_unlock(&hlp->lock); |
||
| 560 | break; |
||
| 561 | } |
||
| 562 | |||
| 563 | spinlock_unlock(&hlp->lock); |
||
| 564 | } |
||
| 565 | |||
| 566 | spinlock_unlock(&cmd_lock); |
||
| 567 | |||
| 568 | return 1; |
||
| 569 | } |
||
| 570 | |||
| 582 | palkovsky | 571 | /** Search symbol table */ |
| 572 | int cmd_symaddr(cmd_arg_t *argv) |
||
| 573 | { |
||
| 574 | __address symaddr; |
||
| 575 | char *symbol; |
||
| 576 | |||
| 577 | symtab_print_search(argv->buffer); |
||
| 578 | |||
| 579 | return 1; |
||
| 580 | } |
||
| 581 | |||
| 582 | /** Call function with zero parameters */ |
||
| 583 | int cmd_call0(cmd_arg_t *argv) |
||
| 584 | { |
||
| 585 | __address symaddr; |
||
| 586 | char *symbol; |
||
| 587 | __native (*f)(void); |
||
| 588 | |||
| 589 | symaddr = get_symbol_addr(argv->buffer); |
||
| 590 | if (!symaddr) |
||
| 585 | palkovsky | 591 | printf("Symbol %s not found.\n", argv->buffer); |
| 582 | palkovsky | 592 | else if (symaddr == (__address) -1) { |
| 593 | symtab_print_search(argv->buffer); |
||
| 594 | printf("Duplicate symbol, be more specific.\n"); |
||
| 595 | } else { |
||
| 596 | symbol = get_symtab_entry(symaddr); |
||
| 597 | printf("Calling f(): 0x%p: %s\n", symaddr, symbol); |
||
| 598 | f = (__native (*)(void)) symaddr; |
||
| 599 | printf("Result: 0x%X\n", f()); |
||
| 600 | } |
||
| 601 | |||
| 602 | return 1; |
||
| 603 | } |
||
| 604 | |||
| 605 | /** Call function with one parameter */ |
||
| 606 | int cmd_call1(cmd_arg_t *argv) |
||
| 607 | { |
||
| 608 | __address symaddr; |
||
| 609 | char *symbol; |
||
| 610 | __native (*f)(__native); |
||
| 611 | __native arg1 = argv[1].intval; |
||
| 612 | |||
| 613 | symaddr = get_symbol_addr(argv->buffer); |
||
| 614 | if (!symaddr) |
||
| 585 | palkovsky | 615 | printf("Symbol %s not found.\n", argv->buffer); |
| 582 | palkovsky | 616 | else if (symaddr == (__address) -1) { |
| 617 | symtab_print_search(argv->buffer); |
||
| 618 | printf("Duplicate symbol, be more specific.\n"); |
||
| 619 | } else { |
||
| 620 | symbol = get_symtab_entry(symaddr); |
||
| 621 | printf("Calling f(0x%x): 0x%p: %s\n", arg1, symaddr, symbol); |
||
| 622 | f = (__native (*)(__native)) symaddr; |
||
| 623 | printf("Result: 0x%x\n", f(arg1)); |
||
| 624 | } |
||
| 625 | |||
| 626 | return 1; |
||
| 627 | } |
||
| 628 | |||
| 629 | /** Call function with two parameters */ |
||
| 630 | int cmd_call2(cmd_arg_t *argv) |
||
| 631 | { |
||
| 632 | __address symaddr; |
||
| 633 | char *symbol; |
||
| 585 | palkovsky | 634 | __native (*f)(__native,__native); |
| 582 | palkovsky | 635 | __native arg1 = argv[1].intval; |
| 636 | __native arg2 = argv[2].intval; |
||
| 637 | |||
| 638 | symaddr = get_symbol_addr(argv->buffer); |
||
| 639 | if (!symaddr) |
||
| 585 | palkovsky | 640 | printf("Symbol %s not found.\n", argv->buffer); |
| 582 | palkovsky | 641 | else if (symaddr == (__address) -1) { |
| 642 | symtab_print_search(argv->buffer); |
||
| 643 | printf("Duplicate symbol, be more specific.\n"); |
||
| 644 | } else { |
||
| 645 | symbol = get_symtab_entry(symaddr); |
||
| 646 | printf("Calling f(0x%x,0x%x): 0x%p: %s\n", |
||
| 647 | arg1, arg2, symaddr, symbol); |
||
| 585 | palkovsky | 648 | f = (__native (*)(__native,__native)) symaddr; |
| 649 | printf("Result: 0x%x\n", f(arg1, arg2)); |
||
| 582 | palkovsky | 650 | } |
| 651 | |||
| 652 | return 1; |
||
| 653 | } |
||
| 654 | |||
| 655 | |||
| 548 | jermar | 656 | /** Print detailed description of 'describe' command. */ |
| 657 | void desc_help(void) |
||
| 658 | { |
||
| 659 | printf("Syntax: describe command_name\n"); |
||
| 660 | } |
||
| 661 | |||
| 544 | decky | 662 | /** Halt the kernel. |
| 663 | * |
||
| 548 | jermar | 664 | * @param argv Argument vector (ignored). |
| 544 | decky | 665 | * |
| 548 | jermar | 666 | * @return 0 on failure, 1 on success (never returns). |
| 544 | decky | 667 | */ |
| 668 | int cmd_halt(cmd_arg_t *argv) |
||
| 669 | { |
||
| 670 | halt(); |
||
| 671 | return 1; |
||
| 672 | } |