Rev 4089 | Rev 4137 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 510 | jermar | 1 | /* |
| 2071 | jermar | 2 | * Copyright (c) 2005 Jakub Jermar |
| 510 | 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 | |||
| 1888 | jermar | 29 | /** @addtogroup genericconsole |
| 1702 | cejka | 30 | * @{ |
| 31 | */ |
||
| 32 | |||
| 1264 | jermar | 33 | /** |
| 34 | * @file kconsole.c |
||
| 35 | * @brief Kernel console. |
||
| 36 | * |
||
| 37 | * This file contains kernel thread managing the kernel console. |
||
| 38 | */ |
||
| 39 | |||
| 518 | jermar | 40 | #include <console/kconsole.h> |
| 510 | jermar | 41 | #include <console/console.h> |
| 42 | #include <console/chardev.h> |
||
| 596 | jermar | 43 | #include <console/cmd.h> |
| 510 | jermar | 44 | #include <print.h> |
| 517 | jermar | 45 | #include <panic.h> |
| 510 | jermar | 46 | #include <arch/types.h> |
| 788 | jermar | 47 | #include <adt/list.h> |
| 517 | jermar | 48 | #include <arch.h> |
| 49 | #include <macros.h> |
||
| 518 | jermar | 50 | #include <debug.h> |
| 596 | jermar | 51 | #include <func.h> |
| 4011 | svoboda | 52 | #include <string.h> |
| 609 | palkovsky | 53 | #include <macros.h> |
| 3761 | decky | 54 | #include <sysinfo/sysinfo.h> |
| 55 | #include <ddi/device.h> |
||
| 510 | jermar | 56 | |
| 4132 | svoboda | 57 | #ifdef CONFIG_SYMTAB |
| 58 | #include <symtab.h> |
||
| 59 | #endif |
||
| 60 | |||
| 517 | jermar | 61 | /** Simple kernel console. |
| 62 | * |
||
| 63 | * The console is realized by kernel thread kconsole. |
||
| 518 | jermar | 64 | * It doesn't understand any useful command on its own, |
| 65 | * but makes it possible for other kernel subsystems to |
||
| 517 | jermar | 66 | * register their own commands. |
| 67 | */ |
||
| 68 | |||
| 69 | /** Locking. |
||
| 70 | * |
||
| 71 | * There is a list of cmd_info_t structures. This list |
||
| 72 | * is protected by cmd_lock spinlock. Note that specially |
||
| 73 | * the link elements of cmd_info_t are protected by |
||
| 74 | * this lock. |
||
| 75 | * |
||
| 76 | * Each cmd_info_t also has its own lock, which protects |
||
| 77 | * all elements thereof except the link element. |
||
| 78 | * |
||
| 79 | * cmd_lock must be acquired before any cmd_info lock. |
||
| 80 | * When locking two cmd info structures, structure with |
||
| 81 | * lower address must be locked first. |
||
| 82 | */ |
||
| 83 | |||
| 623 | jermar | 84 | SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */ |
| 624 | jermar | 85 | LIST_INITIALIZE(cmd_head); /**< Command list. */ |
| 517 | jermar | 86 | |
| 87 | static cmd_info_t *parse_cmdline(char *cmdline, size_t len); |
||
| 2108 | jermar | 88 | static bool parse_argument(char *cmdline, size_t len, index_t *start, |
| 89 | index_t *end); |
||
| 601 | palkovsky | 90 | static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {}; |
| 517 | jermar | 91 | |
| 3761 | decky | 92 | /* |
| 93 | * For now, we use 0 as INR. |
||
| 94 | * However, it is therefore desirable to have architecture specific |
||
| 95 | * definition of KCONSOLE_VIRT_INR in the future. |
||
| 96 | */ |
||
| 97 | #define KCONSOLE_VIRT_INR 0 |
||
| 98 | |||
| 99 | bool kconsole_notify = false; |
||
| 100 | irq_t kconsole_irq; |
||
| 101 | |||
| 102 | |||
| 103 | /** Allways refuse IRQ ownership. |
||
| 104 | * |
||
| 105 | * This is not a real IRQ, so we always decline. |
||
| 106 | * |
||
| 107 | * @return Always returns IRQ_DECLINE. |
||
| 108 | * |
||
| 109 | */ |
||
| 3941 | jermar | 110 | static irq_ownership_t kconsole_claim(irq_t *irq) |
| 3761 | decky | 111 | { |
| 112 | return IRQ_DECLINE; |
||
| 113 | } |
||
| 114 | |||
| 115 | |||
| 116 | /** Initialize kconsole data structures |
||
| 117 | * |
||
| 118 | * This is the most basic initialization, almost no |
||
| 119 | * other kernel subsystem is ready yet. |
||
| 120 | * |
||
| 121 | */ |
||
| 517 | jermar | 122 | void kconsole_init(void) |
| 123 | { |
||
| 3761 | decky | 124 | unsigned int i; |
| 601 | palkovsky | 125 | |
| 596 | jermar | 126 | cmd_init(); |
| 2108 | jermar | 127 | for (i = 0; i < KCONSOLE_HISTORY; i++) |
| 601 | palkovsky | 128 | history[i][0] = '\0'; |
| 517 | jermar | 129 | } |
| 130 | |||
| 131 | |||
| 3761 | decky | 132 | /** Initialize kconsole notification mechanism |
| 133 | * |
||
| 134 | * Initialize the virtual IRQ notification mechanism. |
||
| 135 | * |
||
| 136 | */ |
||
| 137 | void kconsole_notify_init(void) |
||
| 138 | { |
||
| 139 | devno_t devno = device_assign_devno(); |
||
| 140 | |||
| 141 | sysinfo_set_item_val("kconsole.present", NULL, true); |
||
| 142 | sysinfo_set_item_val("kconsole.devno", NULL, devno); |
||
| 143 | sysinfo_set_item_val("kconsole.inr", NULL, KCONSOLE_VIRT_INR); |
||
| 144 | |||
| 145 | irq_initialize(&kconsole_irq); |
||
| 146 | kconsole_irq.devno = devno; |
||
| 147 | kconsole_irq.inr = KCONSOLE_VIRT_INR; |
||
| 148 | kconsole_irq.claim = kconsole_claim; |
||
| 149 | irq_register(&kconsole_irq); |
||
| 150 | |||
| 151 | kconsole_notify = true; |
||
| 152 | } |
||
| 153 | |||
| 154 | |||
| 517 | jermar | 155 | /** Register kconsole command. |
| 156 | * |
||
| 157 | * @param cmd Structure describing the command. |
||
| 158 | * |
||
| 159 | * @return 0 on failure, 1 on success. |
||
| 160 | */ |
||
| 161 | int cmd_register(cmd_info_t *cmd) |
||
| 162 | { |
||
| 163 | link_t *cur; |
||
| 164 | |||
| 165 | spinlock_lock(&cmd_lock); |
||
| 166 | |||
| 167 | /* |
||
| 168 | * Make sure the command is not already listed. |
||
| 169 | */ |
||
| 170 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
||
| 171 | cmd_info_t *hlp; |
||
| 172 | |||
| 173 | hlp = list_get_instance(cur, cmd_info_t, link); |
||
| 174 | |||
| 175 | if (hlp == cmd) { |
||
| 176 | /* The command is already there. */ |
||
| 177 | spinlock_unlock(&cmd_lock); |
||
| 178 | return 0; |
||
| 179 | } |
||
| 180 | |||
| 181 | /* Avoid deadlock. */ |
||
| 182 | if (hlp < cmd) { |
||
| 183 | spinlock_lock(&hlp->lock); |
||
| 184 | spinlock_lock(&cmd->lock); |
||
| 185 | } else { |
||
| 186 | spinlock_lock(&cmd->lock); |
||
| 187 | spinlock_lock(&hlp->lock); |
||
| 188 | } |
||
| 2108 | jermar | 189 | if ((strncmp(hlp->name, cmd->name, max(strlen(cmd->name), |
| 190 | strlen(hlp->name))) == 0)) { |
||
| 517 | jermar | 191 | /* The command is already there. */ |
| 192 | spinlock_unlock(&hlp->lock); |
||
| 193 | spinlock_unlock(&cmd->lock); |
||
| 194 | spinlock_unlock(&cmd_lock); |
||
| 195 | return 0; |
||
| 196 | } |
||
| 197 | |||
| 198 | spinlock_unlock(&hlp->lock); |
||
| 199 | spinlock_unlock(&cmd->lock); |
||
| 200 | } |
||
| 201 | |||
| 202 | /* |
||
| 203 | * Now the command can be added. |
||
| 204 | */ |
||
| 205 | list_append(&cmd->link, &cmd_head); |
||
| 206 | |||
| 207 | spinlock_unlock(&cmd_lock); |
||
| 208 | return 1; |
||
| 209 | } |
||
| 210 | |||
| 635 | palkovsky | 211 | /** Print count times a character */ |
| 601 | palkovsky | 212 | static void rdln_print_c(char ch, int count) |
| 213 | { |
||
| 214 | int i; |
||
| 2108 | jermar | 215 | for (i = 0; i < count; i++) |
| 601 | palkovsky | 216 | putchar(ch); |
| 217 | } |
||
| 218 | |||
| 635 | palkovsky | 219 | /** Insert character to string */ |
| 601 | palkovsky | 220 | static void insert_char(char *str, char ch, int pos) |
| 221 | { |
||
| 222 | int i; |
||
| 223 | |||
| 2108 | jermar | 224 | for (i = strlen(str); i > pos; i--) |
| 225 | str[i] = str[i - 1]; |
||
| 601 | palkovsky | 226 | str[pos] = ch; |
| 227 | } |
||
| 228 | |||
| 640 | jermar | 229 | /** Try to find a command beginning with prefix */ |
| 3193 | jermar | 230 | static const char *cmdtab_search_one(const char *name,link_t **startpos) |
| 601 | palkovsky | 231 | { |
| 2113 | decky | 232 | size_t namelen = strlen(name); |
| 601 | palkovsky | 233 | const char *curname; |
| 234 | |||
| 235 | spinlock_lock(&cmd_lock); |
||
| 236 | |||
| 237 | if (!*startpos) |
||
| 238 | *startpos = cmd_head.next; |
||
| 239 | |||
| 2108 | jermar | 240 | for (; *startpos != &cmd_head; *startpos = (*startpos)->next) { |
| 601 | palkovsky | 241 | cmd_info_t *hlp; |
| 242 | hlp = list_get_instance(*startpos, cmd_info_t, link); |
||
| 243 | |||
| 244 | curname = hlp->name; |
||
| 245 | if (strlen(curname) < namelen) |
||
| 246 | continue; |
||
| 247 | if (strncmp(curname, name, namelen) == 0) { |
||
| 248 | spinlock_unlock(&cmd_lock); |
||
| 249 | return curname+namelen; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | spinlock_unlock(&cmd_lock); |
||
| 253 | return NULL; |
||
| 254 | } |
||
| 255 | |||
| 256 | |||
| 257 | /** Command completion of the commands |
||
| 258 | * |
||
| 259 | * @param name - string to match, changed to hint on exit |
||
| 260 | * @return number of found matches |
||
| 261 | */ |
||
| 262 | static int cmdtab_compl(char *name) |
||
| 263 | { |
||
| 4132 | svoboda | 264 | static char output[/*MAX_SYMBOL_NAME*/128 + 1]; |
| 601 | palkovsky | 265 | link_t *startpos = NULL; |
| 266 | const char *foundtxt; |
||
| 267 | int found = 0; |
||
| 268 | int i; |
||
| 269 | |||
| 270 | output[0] = '\0'; |
||
| 271 | while ((foundtxt = cmdtab_search_one(name, &startpos))) { |
||
| 272 | startpos = startpos->next; |
||
| 273 | if (!found) |
||
| 3193 | jermar | 274 | strncpy(output, foundtxt, strlen(foundtxt) + 1); |
| 601 | palkovsky | 275 | else { |
| 2108 | jermar | 276 | for (i = 0; output[i] && foundtxt[i] && |
| 277 | output[i] == foundtxt[i]; i++) |
||
| 601 | palkovsky | 278 | ; |
| 279 | output[i] = '\0'; |
||
| 280 | } |
||
| 281 | found++; |
||
| 282 | } |
||
| 283 | if (!found) |
||
| 284 | return 0; |
||
| 285 | |||
| 602 | palkovsky | 286 | if (found > 1 && !strlen(output)) { |
| 601 | palkovsky | 287 | printf("\n"); |
| 288 | startpos = NULL; |
||
| 289 | while ((foundtxt = cmdtab_search_one(name, &startpos))) { |
||
| 290 | cmd_info_t *hlp; |
||
| 291 | hlp = list_get_instance(startpos, cmd_info_t, link); |
||
| 292 | printf("%s - %s\n", hlp->name, hlp->description); |
||
| 293 | startpos = startpos->next; |
||
| 294 | } |
||
| 295 | } |
||
| 4132 | svoboda | 296 | strncpy(name, output, 128/*MAX_SYMBOL_NAME*/); |
| 601 | palkovsky | 297 | return found; |
| 298 | } |
||
| 299 | |||
| 4089 | decky | 300 | static char *clever_readline(const char *prompt, indev_t *input) |
| 601 | palkovsky | 301 | { |
| 302 | static int histposition = 0; |
||
| 303 | |||
| 3193 | jermar | 304 | static char tmp[MAX_CMDLINE + 1]; |
| 601 | palkovsky | 305 | int curlen = 0, position = 0; |
| 306 | char *current = history[histposition]; |
||
| 307 | int i; |
||
| 606 | palkovsky | 308 | char mod; /* Command Modifier */ |
| 601 | palkovsky | 309 | char c; |
| 310 | |||
| 311 | printf("%s> ", prompt); |
||
| 312 | while (1) { |
||
| 313 | c = _getc(input); |
||
| 314 | if (c == '\n') { |
||
| 315 | putchar(c); |
||
| 316 | break; |
||
| 3193 | jermar | 317 | } |
| 318 | if (c == '\b') { /* Backspace */ |
||
| 601 | palkovsky | 319 | if (position == 0) |
| 320 | continue; |
||
| 2108 | jermar | 321 | for (i = position; i < curlen; i++) |
| 322 | current[i - 1] = current[i]; |
||
| 601 | palkovsky | 323 | curlen--; |
| 324 | position--; |
||
| 325 | putchar('\b'); |
||
| 2108 | jermar | 326 | for (i = position; i < curlen; i++) |
| 601 | palkovsky | 327 | putchar(current[i]); |
| 328 | putchar(' '); |
||
| 2108 | jermar | 329 | rdln_print_c('\b', curlen - position + 1); |
| 601 | palkovsky | 330 | continue; |
| 331 | } |
||
| 607 | palkovsky | 332 | if (c == '\t') { /* Tabulator */ |
| 601 | palkovsky | 333 | int found; |
| 334 | |||
| 335 | /* Move to the end of the word */ |
||
| 2108 | jermar | 336 | for (; position < curlen && current[position] != ' '; |
| 337 | position++) |
||
| 601 | palkovsky | 338 | putchar(current[position]); |
| 339 | /* Copy to tmp last word */ |
||
| 2108 | jermar | 340 | for (i = position - 1; i >= 0 && current[i] != ' '; i--) |
| 601 | palkovsky | 341 | ; |
| 342 | /* If word begins with * or &, skip it */ |
||
| 343 | if (tmp[0] == '*' || tmp[0] == '&') |
||
| 2108 | jermar | 344 | for (i = 1; tmp[i]; i++) |
| 345 | tmp[i - 1] = tmp[i]; |
||
| 601 | palkovsky | 346 | i++; /* I is at the start of the word */ |
| 2108 | jermar | 347 | strncpy(tmp, current + i, position - i + 1); |
| 601 | palkovsky | 348 | |
| 2108 | jermar | 349 | if (i == 0) { /* Command completion */ |
| 601 | palkovsky | 350 | found = cmdtab_compl(tmp); |
| 351 | } else { /* Symtab completion */ |
||
| 4132 | svoboda | 352 | #ifdef CONFIG_SYMTAB |
| 601 | palkovsky | 353 | found = symtab_compl(tmp); |
| 4132 | svoboda | 354 | #else |
| 355 | found = 0; |
||
| 356 | #endif |
||
| 601 | palkovsky | 357 | } |
| 358 | |||
| 359 | if (found == 0) |
||
| 360 | continue; |
||
| 2108 | jermar | 361 | for (i = 0; tmp[i] && curlen < MAX_CMDLINE; |
| 362 | i++, curlen++) |
||
| 363 | insert_char(current, tmp[i], i + position); |
||
| 602 | palkovsky | 364 | |
| 2108 | jermar | 365 | if (strlen(tmp) || found == 1) { /* If we have a hint */ |
| 366 | for (i = position; i < curlen; i++) |
||
| 601 | palkovsky | 367 | putchar(current[i]); |
| 368 | position += strlen(tmp); |
||
| 369 | /* Add space to end */ |
||
| 2108 | jermar | 370 | if (found == 1 && position == curlen && |
| 602 | palkovsky | 371 | curlen < MAX_CMDLINE) { |
| 601 | palkovsky | 372 | current[position] = ' '; |
| 373 | curlen++; |
||
| 374 | position++; |
||
| 375 | putchar(' '); |
||
| 376 | } |
||
| 602 | palkovsky | 377 | } else { /* No hint, table was printed */ |
| 601 | palkovsky | 378 | printf("%s> ", prompt); |
| 2108 | jermar | 379 | for (i = 0; i < curlen; i++) |
| 601 | palkovsky | 380 | putchar(current[i]); |
| 381 | position += strlen(tmp); |
||
| 382 | } |
||
| 2108 | jermar | 383 | rdln_print_c('\b', curlen - position); |
| 601 | palkovsky | 384 | continue; |
| 385 | } |
||
| 607 | palkovsky | 386 | if (c == 0x1b) { /* Special command */ |
| 606 | palkovsky | 387 | mod = _getc(input); |
| 601 | palkovsky | 388 | c = _getc(input); |
| 606 | palkovsky | 389 | |
| 390 | if (mod != 0x5b && mod != 0x4f) |
||
| 601 | palkovsky | 391 | continue; |
| 606 | palkovsky | 392 | |
| 393 | if (c == 0x33 && _getc(input) == 0x7e) { |
||
| 607 | palkovsky | 394 | /* Delete */ |
| 606 | palkovsky | 395 | if (position == curlen) |
| 396 | continue; |
||
| 2108 | jermar | 397 | for (i = position + 1; i < curlen; i++) { |
| 606 | palkovsky | 398 | putchar(current[i]); |
| 2108 | jermar | 399 | current[i - 1] = current[i]; |
| 606 | palkovsky | 400 | } |
| 401 | putchar(' '); |
||
| 2108 | jermar | 402 | rdln_print_c('\b', curlen - position); |
| 606 | palkovsky | 403 | curlen--; |
| 2108 | jermar | 404 | } else if (c == 0x48) { /* Home */ |
| 405 | rdln_print_c('\b', position); |
||
| 606 | palkovsky | 406 | position = 0; |
| 2108 | jermar | 407 | } else if (c == 0x46) { /* End */ |
| 408 | for (i = position; i < curlen; i++) |
||
| 606 | palkovsky | 409 | putchar(current[i]); |
| 410 | position = curlen; |
||
| 2108 | jermar | 411 | } else if (c == 0x44) { /* Left */ |
| 601 | palkovsky | 412 | if (position > 0) { |
| 413 | putchar('\b'); |
||
| 414 | position--; |
||
| 415 | } |
||
| 416 | continue; |
||
| 2108 | jermar | 417 | } else if (c == 0x43) { /* Right */ |
| 601 | palkovsky | 418 | if (position < curlen) { |
| 419 | putchar(current[position]); |
||
| 420 | position++; |
||
| 421 | } |
||
| 422 | continue; |
||
| 2108 | jermar | 423 | } else if (c == 0x41 || c == 0x42) { |
| 424 | /* Up, down */ |
||
| 425 | rdln_print_c('\b', position); |
||
| 426 | rdln_print_c(' ', curlen); |
||
| 427 | rdln_print_c('\b', curlen); |
||
| 607 | palkovsky | 428 | if (c == 0x41) /* Up */ |
| 601 | palkovsky | 429 | histposition--; |
| 430 | else |
||
| 431 | histposition++; |
||
| 2108 | jermar | 432 | if (histposition < 0) { |
| 433 | histposition = KCONSOLE_HISTORY - 1; |
||
| 434 | } else { |
||
| 435 | histposition = |
||
| 436 | histposition % KCONSOLE_HISTORY; |
||
| 437 | } |
||
| 601 | palkovsky | 438 | current = history[histposition]; |
| 439 | printf("%s", current); |
||
| 440 | curlen = strlen(current); |
||
| 441 | position = curlen; |
||
| 442 | continue; |
||
| 443 | } |
||
| 444 | continue; |
||
| 445 | } |
||
| 446 | if (curlen >= MAX_CMDLINE) |
||
| 447 | continue; |
||
| 448 | |||
| 449 | insert_char(current, c, position); |
||
| 450 | |||
| 451 | curlen++; |
||
| 2108 | jermar | 452 | for (i = position; i < curlen; i++) |
| 601 | palkovsky | 453 | putchar(current[i]); |
| 454 | position++; |
||
| 2108 | jermar | 455 | rdln_print_c('\b',curlen - position); |
| 601 | palkovsky | 456 | } |
| 603 | palkovsky | 457 | if (curlen) { |
| 458 | histposition++; |
||
| 459 | histposition = histposition % KCONSOLE_HISTORY; |
||
| 460 | } |
||
| 601 | palkovsky | 461 | current[curlen] = '\0'; |
| 462 | return current; |
||
| 463 | } |
||
| 464 | |||
| 4089 | decky | 465 | bool kconsole_check_poll(void) |
| 466 | { |
||
| 467 | return check_poll(stdin); |
||
| 468 | } |
||
| 469 | |||
| 3707 | decky | 470 | /** Kernel console prompt. |
| 510 | jermar | 471 | * |
| 1708 | jermar | 472 | * @param prompt Kernel console prompt (e.g kconsole/panic). |
| 3707 | decky | 473 | * @param msg Message to display in the beginning. |
| 474 | * @param kcon Wait for keypress to show the prompt |
||
| 475 | * and never exit. |
||
| 476 | * |
||
| 510 | jermar | 477 | */ |
| 3707 | decky | 478 | void kconsole(char *prompt, char *msg, bool kcon) |
| 510 | jermar | 479 | { |
| 517 | jermar | 480 | cmd_info_t *cmd_info; |
| 481 | count_t len; |
||
| 601 | palkovsky | 482 | char *cmdline; |
| 4089 | decky | 483 | |
| 510 | jermar | 484 | if (!stdin) { |
| 3707 | decky | 485 | LOG("No stdin for kernel console"); |
| 510 | jermar | 486 | return; |
| 487 | } |
||
| 488 | |||
| 3707 | decky | 489 | if (msg) |
| 490 | printf("%s", msg); |
||
| 491 | |||
| 492 | if (kcon) |
||
| 493 | _getc(stdin); |
||
| 4089 | decky | 494 | else |
| 495 | printf("Type \"exit\" to leave the console.\n"); |
||
| 3707 | decky | 496 | |
| 510 | jermar | 497 | while (true) { |
| 2113 | decky | 498 | cmdline = clever_readline((char *) prompt, stdin); |
| 601 | palkovsky | 499 | len = strlen(cmdline); |
| 500 | if (!len) |
||
| 518 | jermar | 501 | continue; |
| 3707 | decky | 502 | |
| 4089 | decky | 503 | if ((!kcon) && (len == 4) && (strncmp(cmdline, "exit", 4) == 0)) |
| 504 | break; |
||
| 505 | |||
| 517 | jermar | 506 | cmd_info = parse_cmdline(cmdline, len); |
| 518 | jermar | 507 | if (!cmd_info) |
| 517 | jermar | 508 | continue; |
| 3707 | decky | 509 | |
| 517 | jermar | 510 | (void) cmd_info->func(cmd_info->argv); |
| 510 | jermar | 511 | } |
| 512 | } |
||
| 517 | jermar | 513 | |
| 3707 | decky | 514 | /** Kernel console managing thread. |
| 515 | * |
||
| 516 | */ |
||
| 517 | void kconsole_thread(void *data) |
||
| 518 | { |
||
| 519 | kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true); |
||
| 520 | } |
||
| 521 | |||
| 1780 | jermar | 522 | static int parse_int_arg(char *text, size_t len, unative_t *result) |
| 585 | palkovsky | 523 | { |
| 1780 | jermar | 524 | uintptr_t symaddr; |
| 585 | palkovsky | 525 | bool isaddr = false; |
| 589 | palkovsky | 526 | bool isptr = false; |
| 4132 | svoboda | 527 | |
| 528 | #ifdef CONFIG_SYMTAB |
||
| 529 | static char symname[MAX_SYMBOL_NAME]; |
||
| 530 | #endif |
||
| 585 | palkovsky | 531 | |
| 532 | /* If we get a name, try to find it in symbol table */ |
||
| 931 | palkovsky | 533 | if (text[0] == '&') { |
| 534 | isaddr = true; |
||
| 2108 | jermar | 535 | text++; |
| 536 | len--; |
||
| 931 | palkovsky | 537 | } else if (text[0] == '*') { |
| 538 | isptr = true; |
||
| 2108 | jermar | 539 | text++; |
| 540 | len--; |
||
| 931 | palkovsky | 541 | } |
| 614 | palkovsky | 542 | if (text[0] < '0' || text[0] > '9') { |
| 4132 | svoboda | 543 | #ifdef CONFIG_SYMTAB |
| 2108 | jermar | 544 | strncpy(symname, text, min(len + 1, MAX_SYMBOL_NAME)); |
| 585 | palkovsky | 545 | symaddr = get_symbol_addr(symname); |
| 546 | if (!symaddr) { |
||
| 2108 | jermar | 547 | printf("Symbol %s not found.\n", symname); |
| 585 | palkovsky | 548 | return -1; |
| 549 | } |
||
| 1780 | jermar | 550 | if (symaddr == (uintptr_t) -1) { |
| 2108 | jermar | 551 | printf("Duplicate symbol %s.\n", symname); |
| 585 | palkovsky | 552 | symtab_print_search(symname); |
| 553 | return -1; |
||
| 554 | } |
||
| 4132 | svoboda | 555 | #else |
| 556 | symaddr = 0; |
||
| 557 | #endif |
||
| 932 | palkovsky | 558 | if (isaddr) |
| 1780 | jermar | 559 | *result = (unative_t)symaddr; |
| 932 | palkovsky | 560 | else if (isptr) |
| 1780 | jermar | 561 | *result = **((unative_t **)symaddr); |
| 932 | palkovsky | 562 | else |
| 1780 | jermar | 563 | *result = *((unative_t *)symaddr); |
| 932 | palkovsky | 564 | } else { /* It's a number - convert it */ |
| 585 | palkovsky | 565 | *result = atoi(text); |
| 932 | palkovsky | 566 | if (isptr) |
| 1780 | jermar | 567 | *result = *((unative_t *)*result); |
| 932 | palkovsky | 568 | } |
| 931 | palkovsky | 569 | |
| 585 | palkovsky | 570 | return 0; |
| 571 | } |
||
| 572 | |||
| 517 | jermar | 573 | /** Parse command line. |
| 574 | * |
||
| 575 | * @param cmdline Command line as read from input device. |
||
| 576 | * @param len Command line length. |
||
| 577 | * |
||
| 578 | * @return Structure describing the command. |
||
| 579 | */ |
||
| 580 | cmd_info_t *parse_cmdline(char *cmdline, size_t len) |
||
| 581 | { |
||
| 582 | index_t start = 0, end = 0; |
||
| 583 | cmd_info_t *cmd = NULL; |
||
| 584 | link_t *cur; |
||
| 2113 | decky | 585 | count_t i; |
| 668 | bondari | 586 | int error = 0; |
| 517 | jermar | 587 | |
| 518 | jermar | 588 | if (!parse_argument(cmdline, len, &start, &end)) { |
| 517 | jermar | 589 | /* Command line did not contain alphanumeric word. */ |
| 590 | return NULL; |
||
| 591 | } |
||
| 592 | |||
| 593 | spinlock_lock(&cmd_lock); |
||
| 594 | |||
| 595 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
||
| 596 | cmd_info_t *hlp; |
||
| 597 | |||
| 598 | hlp = list_get_instance(cur, cmd_info_t, link); |
||
| 599 | spinlock_lock(&hlp->lock); |
||
| 600 | |||
| 635 | palkovsky | 601 | if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name), |
| 2108 | jermar | 602 | end - start + 1)) == 0) { |
| 517 | jermar | 603 | cmd = hlp; |
| 604 | break; |
||
| 605 | } |
||
| 606 | |||
| 607 | spinlock_unlock(&hlp->lock); |
||
| 608 | } |
||
| 609 | |||
| 610 | spinlock_unlock(&cmd_lock); |
||
| 611 | |||
| 612 | if (!cmd) { |
||
| 613 | /* Unknown command. */ |
||
| 518 | jermar | 614 | printf("Unknown command.\n"); |
| 517 | jermar | 615 | return NULL; |
| 616 | } |
||
| 617 | |||
| 618 | /* cmd == hlp is locked */ |
||
| 619 | |||
| 620 | /* |
||
| 621 | * The command line must be further analyzed and |
||
| 622 | * the parameters therefrom must be matched and |
||
| 623 | * converted to those specified in the cmd info |
||
| 624 | * structure. |
||
| 625 | */ |
||
| 518 | jermar | 626 | |
| 627 | for (i = 0; i < cmd->argc; i++) { |
||
| 628 | char *buf; |
||
| 629 | start = end + 1; |
||
| 630 | if (!parse_argument(cmdline, len, &start, &end)) { |
||
| 631 | printf("Too few arguments.\n"); |
||
| 632 | spinlock_unlock(&cmd->lock); |
||
| 633 | return NULL; |
||
| 634 | } |
||
| 635 | |||
| 668 | bondari | 636 | error = 0; |
| 518 | jermar | 637 | switch (cmd->argv[i].type) { |
| 582 | palkovsky | 638 | case ARG_TYPE_STRING: |
| 2113 | decky | 639 | buf = (char *) cmd->argv[i].buffer; |
| 640 | strncpy(buf, (const char *) &cmdline[start], |
||
| 2108 | jermar | 641 | min((end - start) + 2, cmd->argv[i].len)); |
| 3193 | jermar | 642 | buf[min((end - start) + 1, cmd->argv[i].len - 1)] = |
| 643 | '\0'; |
||
| 518 | jermar | 644 | break; |
| 585 | palkovsky | 645 | case ARG_TYPE_INT: |
| 2108 | jermar | 646 | if (parse_int_arg(cmdline + start, end - start + 1, |
| 647 | &cmd->argv[i].intval)) |
||
| 668 | bondari | 648 | error = 1; |
| 518 | jermar | 649 | break; |
| 585 | palkovsky | 650 | case ARG_TYPE_VAR: |
| 2108 | jermar | 651 | if (start != end && cmdline[start] == '"' && |
| 652 | cmdline[end] == '"') { |
||
| 2113 | decky | 653 | buf = (char *) cmd->argv[i].buffer; |
| 2108 | jermar | 654 | strncpy(buf, (const char *) &cmdline[start + 1], |
| 655 | min((end-start), cmd->argv[i].len)); |
||
| 656 | buf[min((end - start), cmd->argv[i].len - 1)] = |
||
| 657 | '\0'; |
||
| 1780 | jermar | 658 | cmd->argv[i].intval = (unative_t) buf; |
| 585 | palkovsky | 659 | cmd->argv[i].vartype = ARG_TYPE_STRING; |
| 3193 | jermar | 660 | } else if (!parse_int_arg(cmdline + start, |
| 661 | end - start + 1, &cmd->argv[i].intval)) { |
||
| 585 | palkovsky | 662 | cmd->argv[i].vartype = ARG_TYPE_INT; |
| 2108 | jermar | 663 | } else { |
| 585 | palkovsky | 664 | printf("Unrecognized variable argument.\n"); |
| 668 | bondari | 665 | error = 1; |
| 582 | palkovsky | 666 | } |
| 585 | palkovsky | 667 | break; |
| 582 | palkovsky | 668 | case ARG_TYPE_INVALID: |
| 669 | default: |
||
| 670 | printf("invalid argument type\n"); |
||
| 668 | bondari | 671 | error = 1; |
| 582 | palkovsky | 672 | break; |
| 518 | jermar | 673 | } |
| 674 | } |
||
| 517 | jermar | 675 | |
| 668 | bondari | 676 | if (error) { |
| 677 | spinlock_unlock(&cmd->lock); |
||
| 678 | return NULL; |
||
| 679 | } |
||
| 680 | |||
| 518 | jermar | 681 | start = end + 1; |
| 682 | if (parse_argument(cmdline, len, &start, &end)) { |
||
| 683 | printf("Too many arguments.\n"); |
||
| 684 | spinlock_unlock(&cmd->lock); |
||
| 685 | return NULL; |
||
| 686 | } |
||
| 687 | |||
| 517 | jermar | 688 | spinlock_unlock(&cmd->lock); |
| 689 | return cmd; |
||
| 690 | } |
||
| 691 | |||
| 518 | jermar | 692 | /** Parse argument. |
| 693 | * |
||
| 694 | * Find start and end positions of command line argument. |
||
| 695 | * |
||
| 696 | * @param cmdline Command line as read from the input device. |
||
| 697 | * @param len Number of characters in cmdline. |
||
| 698 | * @param start On entry, 'start' contains pointer to the index |
||
| 699 | * of first unprocessed character of cmdline. |
||
| 700 | * On successful exit, it marks beginning of the next argument. |
||
| 701 | * @param end Undefined on entry. On exit, 'end' points to the last character |
||
| 702 | * of the next argument. |
||
| 703 | * |
||
| 704 | * @return false on failure, true on success. |
||
| 705 | */ |
||
| 706 | bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end) |
||
| 707 | { |
||
| 2113 | decky | 708 | index_t i; |
| 518 | jermar | 709 | bool found_start = false; |
| 710 | |||
| 711 | ASSERT(start != NULL); |
||
| 712 | ASSERT(end != NULL); |
||
| 713 | |||
| 714 | for (i = *start; i < len; i++) { |
||
| 715 | if (!found_start) { |
||
| 2572 | jermar | 716 | if (isspace(cmdline[i])) |
| 518 | jermar | 717 | (*start)++; |
| 718 | else |
||
| 719 | found_start = true; |
||
| 720 | } else { |
||
| 2572 | jermar | 721 | if (isspace(cmdline[i])) |
| 518 | jermar | 722 | break; |
| 723 | } |
||
| 724 | } |
||
| 725 | *end = i - 1; |
||
| 726 | |||
| 727 | return found_start; |
||
| 728 | } |
||
| 1702 | cejka | 729 | |
| 1888 | jermar | 730 | /** @} |
| 1702 | cejka | 731 | */ |