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