terminal.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /* Terminal: prompt, datetime (top right), blinking cursor, echo loop, command dispatch.
  2. Commands are spawned as separate processes for preemptive multitasking. */
  3. #include "io.h"
  4. #include "acpi.h"
  5. #include "backlight.h"
  6. #include "power.h"
  7. #include "bootcon.h"
  8. #include "dbg.h"
  9. #include "idle.h"
  10. #include "kexec.h"
  11. #include "kmalloc.h"
  12. #include "mm.h"
  13. #include "process.h"
  14. #include "sys_fs.h"
  15. #include "terminal.h"
  16. #include "timer.h"
  17. extern void program_reboot_main(void);
  18. extern void program_poweroff_main(const char *arg);
  19. extern void program_display_main(void);
  20. extern void program_gfxbench_main(void);
  21. extern void program_serial_loop_main(const char *arg);
  22. extern void program_lsusb_main(void);
  23. extern void program_touchpad_main(const char *arg);
  24. extern void program_netdev_main(const char *arg);
  25. extern void program_format_main(const char *arg);
  26. extern void program_install_main(const char *arg);
  27. extern void program_lspci_main(const char *filter);
  28. extern void program_peek_main(const char *arg);
  29. extern void program_poke_main(const char *arg);
  30. /* ---- Command registry ----
  31. *
  32. * One table drives `help` and the privileged kernel commands that the ring-3
  33. * terminal spawns through SYS_SPAWN_KCMD. The ring-3 shell owns its own
  34. * builtins, Tab completion and PATH lookup. */
  35. enum cmd_kind {
  36. CK_SPAWN = 0, /* spawn, no arguments */
  37. CK_SPAWN_ARG, /* spawn with the raw remainder of the line */
  38. CK_CUSTOM /* handled by a cmd_*() handler below */
  39. };
  40. #define CMD_REQARG 0x01 /* the argument must be non-empty */
  41. #define CMD_BUILTIN 0x04 /* handled inline, before the PATH lookup */
  42. struct kcmd {
  43. const char *name; /* dispatch token */
  44. const char *usage; /* `help` line (and completion name) */
  45. unsigned char kind;
  46. unsigned char flags;
  47. void (*fn)(void); /* spawned entry, no argument */
  48. void (*fn_arg)(const char *); /* spawned entry with an argument */
  49. const char *fixed; /* CK_SPAWN: fixed argument, if any */
  50. int (*custom)(struct term *t, const char *args);
  51. };
  52. /* The terminal's spawn/path helpers are defined later in this file. */
  53. static int cmd_bootlog(struct term *t, const char *args);
  54. static int cmd_seriallog(struct term *t, const char *args);
  55. static int cmd_acpidump(struct term *t, const char *args);
  56. static int cmd_dbg(struct term *t, const char *args);
  57. static int cmd_format(struct term *t, const char *args);
  58. static int cmd_install(struct term *t, const char *args);
  59. static int cmd_help(struct term *t, const char *args);
  60. static int cmd_idle(struct term *t, const char *args);
  61. static int cmd_cpufreq(struct term *t, const char *args);
  62. static int cmd_power(struct term *t, const char *args);
  63. static int cmd_kexec(struct term *t, const char *args);
  64. static int cmd_keytest(struct term *t, const char *args);
  65. static int cmd_kill(struct term *t, const char *args);
  66. static int cmd_backlight(struct term *t, const char *args);
  67. static int cmd_ps(struct term *t, const char *args);
  68. static int cmd_renice(struct term *t, const char *args);
  69. static int cmd_sched(struct term *t, const char *args);
  70. static int cmd_reboot(struct term *t, const char *args);
  71. static int cmd_serial(struct term *t, const char *args);
  72. /* Table order defines the `help` layout (printed as two columns). Ring-3
  73. * commands are not listed here: they are files on the PATH. */
  74. static const struct kcmd cmds[] = {
  75. { "help", "help", CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_help },
  76. { "bootlog", "bootlog", CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_bootlog },
  77. { "seriallog", "seriallog save <path>|show", CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_seriallog },
  78. { "acpidump", "acpidump", CK_CUSTOM, 0, 0, 0, 0, cmd_acpidump },
  79. { "reboot", "reboot", CK_CUSTOM, 0, 0, 0, 0, cmd_reboot },
  80. { "poweroff", "poweroff", CK_SPAWN_ARG, 0, 0, program_poweroff_main, 0, 0 },
  81. { "display", "display", CK_SPAWN, 0, program_display_main, 0, 0, 0 },
  82. { "gfxbench", "gfxbench", CK_SPAWN, 0, program_gfxbench_main, 0, 0, 0 },
  83. { "backlight", "backlight [0-100|up|down]", CK_CUSTOM, 0, 0, 0, 0, cmd_backlight },
  84. { "keytest", "keytest", CK_CUSTOM, 0, 0, 0, 0, cmd_keytest },
  85. { "lsusb", "lsusb", CK_SPAWN, 0, program_lsusb_main, 0, 0, 0 },
  86. { "touchpad", "touchpad [rightclick|tapclick|scroll]", CK_SPAWN_ARG, 0, 0, program_touchpad_main, 0, 0 },
  87. { "netdev", "netdev [name]", CK_SPAWN_ARG, 0, 0, program_netdev_main, 0, 0 },
  88. { "format", "format <device> [size]", CK_CUSTOM, 0, 0, 0, 0, cmd_format },
  89. { "install", "install <target> [source|self]", CK_CUSTOM, 0, 0, 0, 0, cmd_install },
  90. { "lspci", "lspci [id]", CK_SPAWN_ARG, 0, 0, program_lspci_main, 0, 0 },
  91. { "peek", "peek <addr> [len]", CK_SPAWN_ARG, 0, 0, program_peek_main, 0, 0 },
  92. { "poke", "poke <addr> <val>", CK_SPAWN_ARG, CMD_REQARG, 0, program_poke_main, 0, 0 },
  93. { "ps", "ps", CK_CUSTOM, 0, 0, 0, 0, cmd_ps },
  94. { "sched", "sched", CK_CUSTOM, 0, 0, 0, 0, cmd_sched },
  95. { "power", "power", CK_CUSTOM, 0, 0, 0, 0, cmd_power },
  96. { "cpufreq", "cpufreq [auto|min|max|off|n]", CK_CUSTOM, 0, 0, 0, 0, cmd_cpufreq },
  97. { "idle", "idle [auto|hlt|c1e|c3|c6]", CK_CUSTOM, 0, 0, 0, 0, cmd_idle },
  98. { "renice", "renice <pid> <weight>", CK_CUSTOM, CMD_REQARG, 0, 0, 0, cmd_renice },
  99. { "kill", "kill <pid>", CK_CUSTOM, 0, 0, 0, 0, cmd_kill },
  100. { "kexec", "kexec", CK_CUSTOM, 0, 0, 0, 0, cmd_kexec },
  101. { "dbg", "dbg", CK_CUSTOM, 0, 0, 0, 0, cmd_dbg },
  102. { "serial", "serial test", CK_CUSTOM, 0, 0, 0, 0, cmd_serial },
  103. };
  104. #define NCMDS (sizeof(cmds) / sizeof(cmds[0]))
  105. /* Find the registry entry whose name matches the first word of the line. */
  106. /* Split the line into the command word and the trimmed remainder. */
  107. /* Dispatch one registry entry. Returns 1 when the handler already printed the
  108. * next prompt (clear), 0 otherwise. */
  109. /* `help`: printed as two 32-column columns so it fits the 64-column terminal
  110. * without scrolling. */
  111. static void help_item_pad(const char *s)
  112. {
  113. unsigned int n = 0;
  114. while (s[n]) {
  115. io_putchar(s[n]);
  116. n++;
  117. }
  118. while (n < 32) {
  119. io_putchar(' ');
  120. n++;
  121. }
  122. }
  123. static void help_print(void)
  124. {
  125. unsigned int nitems = NCMDS;
  126. unsigned int half = (nitems + 1) / 2;
  127. unsigned int k;
  128. for (k = 0; k < half; k++) {
  129. if (k + half < nitems) {
  130. help_item_pad(cmds[k].usage);
  131. io_putstr(cmds[k + half].usage);
  132. } else {
  133. io_putstr(cmds[k].usage);
  134. }
  135. io_putchar('\n');
  136. }
  137. }
  138. /* ---- Command handlers that run inline in the terminal ---- */
  139. /* Save the mapped DSDT to /share for offline analysis (acpidump). */
  140. static int cmd_acpidump(struct term *t, const char *args)
  141. {
  142. (void)t;
  143. (void)args;
  144. if (acpi_dump_dsdt("/share/DSDT.BIN") == 0)
  145. io_putstr("acpi: saved /share/DSDT.BIN\n");
  146. else
  147. io_putstr("acpi: dump failed\n");
  148. return 0;
  149. }
  150. /* Dump the whole boot console text captured by bootcon_puts(). The ring-3
  151. * terminal keeps scrollback, so no paging is needed; sleep briefly every
  152. * 512 bytes so the terminal can drain its tty ring while we write. */
  153. static int cmd_bootlog(struct term *t, const char *args)
  154. {
  155. const char *p;
  156. unsigned int len, i;
  157. (void)t;
  158. p = bootcon_log(&len);
  159. while (*args == ' ')
  160. args++;
  161. if (args[0] == 's' && args[1] == 'a' && args[2] == 'v' &&
  162. args[3] == 'e' && (args[4] == ' ' || args[4] == '\0')) {
  163. const char *path = args + 4;
  164. int r;
  165. while (*path == ' ')
  166. path++;
  167. if (!*path) {
  168. io_putstr("bootlog: usage bootlog save <path>\n");
  169. return 0;
  170. }
  171. r = sys_write_file(path, p, len);
  172. if (r == 0)
  173. io_putstr("bootlog saved\n");
  174. else {
  175. io_putstr("bootlog: save failed (");
  176. io_puthex((unsigned int)r);
  177. io_putstr(")\n");
  178. }
  179. return 0;
  180. }
  181. for (i = 0; i < len; i++) {
  182. io_putchar(p[i]);
  183. if ((i & 511u) == 511u)
  184. process_sleep_ms(1);
  185. }
  186. if (bootcon_log_truncated())
  187. io_putstr("\n[boot log truncated]\n");
  188. return 0;
  189. }
  190. /* Save the always-on COM1 capture to a file, or print the whole capture on
  191. * screen. On a serial-less board, write it to the USB ESP and read it from
  192. * Linux after a reboot. */
  193. static int cmd_seriallog(struct term *t, const char *args)
  194. {
  195. const char *p;
  196. unsigned int len;
  197. (void)t;
  198. p = bootcon_serlog(&len);
  199. while (*args == ' ')
  200. args++;
  201. if (args[0] == 's' && args[1] == 'h' && args[2] == 'o' &&
  202. args[3] == 'w' && (args[4] == ' ' || args[4] == '\0')) {
  203. unsigned int i = 0;
  204. while (i < len) {
  205. unsigned int j = i, k;
  206. while (j < len && p[j] != '\n')
  207. j++;
  208. for (k = i; k < j; k++)
  209. if (p[k] != '\r')
  210. io_putchar(p[k]);
  211. io_putchar('\n');
  212. i = j < len ? j + 1 : j;
  213. }
  214. if (bootcon_serlog_truncated())
  215. io_putstr("seriallog: [truncated]\n");
  216. return 0;
  217. }
  218. if (!(args[0] == 's' && args[1] == 'a' && args[2] == 'v' &&
  219. args[3] == 'e' && (args[4] == ' ' || args[4] == '\0'))) {
  220. io_putstr("seriallog: usage seriallog save <path>|show\n");
  221. return 0;
  222. }
  223. {
  224. const char *path = args + 4;
  225. int r;
  226. while (*path == ' ')
  227. path++;
  228. if (!*path) {
  229. io_putstr("seriallog: usage seriallog save <path>\n");
  230. return 0;
  231. }
  232. r = sys_write_file(path, p, len);
  233. if (r == 0)
  234. io_putstr("seriallog saved\n");
  235. else {
  236. io_putstr("seriallog: save failed (");
  237. io_puthex((unsigned int)r);
  238. io_putstr(")\n");
  239. }
  240. }
  241. if (bootcon_serlog_truncated())
  242. io_putstr("seriallog: [truncated]\n");
  243. return 0;
  244. }
  245. static int cmd_dbg(struct term *t, const char *args)
  246. {
  247. (void)t;
  248. (void)args;
  249. program_dbg_main();
  250. return 0;
  251. }
  252. static int cmd_format(struct term *t, const char *args)
  253. {
  254. (void)t;
  255. program_format_main(args);
  256. return 0;
  257. }
  258. static int cmd_install(struct term *t, const char *args)
  259. {
  260. (void)t;
  261. program_install_main(args);
  262. return 0;
  263. }
  264. /* `path` builtin: show or set the command search path. */
  265. /* List the command files found on the search path. */
  266. static void help_print_path(struct term *t)
  267. {
  268. const char *p = t->path;
  269. while (*p) {
  270. char dir[VFS_PATH_MAX];
  271. unsigned int n = 0, col = 0, i;
  272. while (*p && *p != ':' && n + 1 < sizeof(dir))
  273. dir[n++] = *p++;
  274. dir[n] = '\0';
  275. if (*p == ':')
  276. p++;
  277. if (dir[0] == '\0')
  278. continue;
  279. io_putstr(dir);
  280. io_putstr(":\n");
  281. for (i = 0; ; i++) {
  282. struct vfs_dirent d;
  283. unsigned int len = 0;
  284. int r = vfs_list_dir(dir, i, &d);
  285. if (r != 1)
  286. break;
  287. if (d.is_dir)
  288. continue;
  289. while (d.name[len])
  290. len++;
  291. if (col > 0 && col + 2 + len > t->screen.cols) {
  292. io_putchar('\n');
  293. col = 0;
  294. }
  295. if (col > 0) {
  296. io_putstr(" ");
  297. col += 2;
  298. }
  299. io_putstr(d.name);
  300. col += len;
  301. }
  302. io_putchar('\n');
  303. }
  304. }
  305. static int cmd_help(struct term *t, const char *args)
  306. {
  307. (void)args;
  308. io_putstr("shell builtins: cd path clear\n");
  309. help_print();
  310. help_print_path(t);
  311. return 0;
  312. }
  313. static int cmd_kexec(struct term *t, const char *args)
  314. {
  315. (void)t;
  316. (void)args;
  317. if (kexec_busy) {
  318. io_putstr("kexec: already receiving\n");
  319. return 0;
  320. }
  321. kexec_busy = 1;
  322. if (process_create("kexec", program_kexec_main) < 0) {
  323. kexec_busy = 0;
  324. io_putstr("error: too many processes\n");
  325. }
  326. return 0;
  327. }
  328. static int cmd_kill(struct term *t, const char *args)
  329. {
  330. unsigned int pid = 0;
  331. int has_digit = 0;
  332. (void)t;
  333. while (*args >= '0' && *args <= '9') {
  334. pid = pid * 10 + (unsigned int)(*args - '0');
  335. args++;
  336. has_digit = 1;
  337. }
  338. while (*args == ' ')
  339. args++;
  340. if (!has_digit || *args) {
  341. io_putstr("kill: usage kill <pid>\n");
  342. return 0;
  343. }
  344. if (process_kill(pid) < 0)
  345. io_putstr("kill: no such process\n");
  346. else
  347. io_putstr("killed\n");
  348. return 0;
  349. }
  350. static int cmd_ps(struct term *t, const char *args)
  351. {
  352. (void)t;
  353. (void)args;
  354. process_ps();
  355. return 0;
  356. }
  357. static int cmd_sched(struct term *t, const char *args)
  358. {
  359. (void)t;
  360. (void)args;
  361. sched_print();
  362. return 0;
  363. }
  364. static int arg_is(const char *args, const char *word)
  365. {
  366. unsigned int i = 0;
  367. while (word[i] && args[i] == word[i])
  368. i++;
  369. return word[i] == '\0' && args[i] == '\0';
  370. }
  371. static int cmd_idle(struct term *t, const char *args)
  372. {
  373. (void)t;
  374. if (!args[0] || arg_is(args, "status")) {
  375. io_putstr("idle ");
  376. io_putstr(idle_mode_name(idle_mode()));
  377. if (!idle_mwait_supported())
  378. io_putstr(" (mwait unsupported)");
  379. io_putchar('\n');
  380. return 0;
  381. }
  382. if (arg_is(args, "auto")) {
  383. idle_set_mode(IDLE_AUTO);
  384. io_putstr("idle auto\n");
  385. return 0;
  386. }
  387. if (arg_is(args, "hlt")) {
  388. idle_set_mode(IDLE_C1);
  389. io_putstr("idle hlt\n");
  390. return 0;
  391. }
  392. {
  393. int mode = -1;
  394. if (arg_is(args, "c1e"))
  395. mode = IDLE_C1E;
  396. else if (arg_is(args, "c3"))
  397. mode = IDLE_C3;
  398. else if (arg_is(args, "c6"))
  399. mode = IDLE_C6;
  400. else if (arg_is(args, "c3ap"))
  401. mode = IDLE_C3_AP;
  402. else if (arg_is(args, "c6ap"))
  403. mode = IDLE_C6_AP;
  404. if (mode >= 0) {
  405. if (idle_set_mode(mode) < 0) {
  406. io_putstr("idle: mwait not available\n");
  407. return 0;
  408. }
  409. io_putstr("idle ");
  410. io_putstr(idle_mode_name(mode));
  411. io_putstr(" (test the keyboard; power-cycle if it hangs)\n");
  412. return 0;
  413. }
  414. }
  415. io_putstr("idle: usage idle [auto|hlt|c1e|c3|c6|c3ap|c6ap]\n");
  416. return 0;
  417. }
  418. static int cmd_power(struct term *t, const char *args)
  419. {
  420. (void)t;
  421. (void)args;
  422. power_print();
  423. return 0;
  424. }
  425. static void cmd_put_dec(unsigned int v)
  426. {
  427. char b[12];
  428. int i = 0;
  429. if (!v) {
  430. io_putchar('0');
  431. return;
  432. }
  433. while (v) {
  434. b[i++] = (char)('0' + v % 10);
  435. v /= 10;
  436. }
  437. while (i)
  438. io_putchar(b[--i]);
  439. }
  440. static void cmd_put_hex(unsigned int v, unsigned int digits)
  441. {
  442. static const char hex[] = "0123456789ABCDEF";
  443. while (digits) {
  444. digits--;
  445. io_putchar(hex[(v >> (digits * 4)) & 0xF]);
  446. }
  447. }
  448. static const char *cmd_raw_name(unsigned char raw)
  449. {
  450. switch (raw) {
  451. case 0x3B: return "F1";
  452. case 0x3C: return "F2";
  453. case 0x3D: return "F3";
  454. case 0x3E: return "F4";
  455. case 0x3F: return "F5";
  456. case 0x40: return "F6";
  457. case 0x41: return "F7";
  458. case 0x42: return "F8";
  459. case 0x43: return "F9";
  460. case 0x44: return "F10";
  461. case 0x57: return "F11";
  462. case 0x58: return "F12";
  463. }
  464. return 0;
  465. }
  466. static const char *cmd_key_name(int k)
  467. {
  468. switch (k) {
  469. case '\n': return "Enter";
  470. case '\t': return "Tab";
  471. case '\b': return "Backspace";
  472. case 0x1B: return "Esc";
  473. case IO_KEY_LEFT: return "Left";
  474. case IO_KEY_RIGHT: return "Right";
  475. case IO_KEY_UP: return "Up";
  476. case IO_KEY_DOWN: return "Down";
  477. case IO_KEY_PAGE_UP: return "PageUp";
  478. case IO_KEY_PAGE_DOWN: return "PageDown";
  479. case IO_KEY_HOME: return "Home";
  480. case IO_KEY_END: return "End";
  481. case IO_KEY_DELETE: return "Delete";
  482. case IO_KEY_CTRL_LEFT: return "Ctrl+Left";
  483. case IO_KEY_CTRL_RIGHT: return "Ctrl+Right";
  484. case IO_KEY_CTRL_DELETE: return "Ctrl+Delete";
  485. case IO_KEY_CTRL_BACKSPACE: return "Ctrl+Backspace";
  486. }
  487. return 0;
  488. }
  489. /* Print every raw scancode the decoder sees, including releases, E0
  490. * prefixes and keys that decode to 0 (the brightness/action keys). */
  491. static int cmd_keytest(struct term *t, const char *args)
  492. {
  493. struct io_keytap e;
  494. (void)t;
  495. (void)args;
  496. io_keytap_reset();
  497. io_putstr("keytest: press keys (Ctrl+C to stop; E0 = prefix)\n");
  498. for (;;) {
  499. while (io_keytap_read(&e)) {
  500. const char *name;
  501. io_putstr("raw ");
  502. cmd_put_hex(e.raw, 2);
  503. io_putstr(e.ext ? " ext" : " ");
  504. if (e.kind == 2)
  505. io_putstr(" prefix");
  506. else if (e.kind == 1)
  507. io_putstr(" break ");
  508. else
  509. io_putstr(" make ");
  510. io_putstr(" -> 0x");
  511. cmd_put_hex((unsigned int)e.key, 4);
  512. name = e.kind == 0 ? cmd_key_name(e.key) : 0;
  513. if (name) {
  514. io_putstr(" (");
  515. io_putstr(name);
  516. io_putchar(')');
  517. } else if (e.kind == 0 && e.key >= 0x20 && e.key <= 0x7E) {
  518. io_putstr(" '");
  519. io_putchar((char)e.key);
  520. io_putstr("'");
  521. } else if (e.kind == 0 && !e.ext) {
  522. name = cmd_raw_name(e.raw);
  523. if (name) {
  524. io_putstr(" (");
  525. io_putstr(name);
  526. io_putchar(')');
  527. }
  528. }
  529. io_putchar('\n');
  530. }
  531. process_sleep_ms(20);
  532. }
  533. return 0;
  534. }
  535. static void backlight_print(int probe)
  536. {
  537. struct backlight_status st;
  538. if (backlight_status(&st) != 0) {
  539. io_putstr("backlight: no Intel CHV/VLV panel PWM\n");
  540. return;
  541. }
  542. io_putstr("backlight: ");
  543. cmd_put_dec((unsigned int)st.level);
  544. io_putstr("% (pipe ");
  545. io_putstr(st.pipe ? "B" : "A");
  546. io_putstr(", period ");
  547. cmd_put_dec(st.period);
  548. io_putstr(", duty ");
  549. cmd_put_dec(st.duty);
  550. io_putstr(", ");
  551. io_putstr(st.enabled ? "enabled" : "disabled");
  552. io_putstr(")\n");
  553. if (!probe)
  554. return;
  555. io_putstr(" bar 0x");
  556. cmd_put_hex(st.bar, 8);
  557. io_putstr("\n ctl2 A=0x");
  558. cmd_put_hex(st.ctl2[0], 8);
  559. io_putstr(" B=0x");
  560. cmd_put_hex(st.ctl2[1], 8);
  561. io_putstr("\n ctl A=0x");
  562. cmd_put_hex(st.ctl[0], 8);
  563. io_putstr(" B=0x");
  564. cmd_put_hex(st.ctl[1], 8);
  565. io_putchar('\n');
  566. }
  567. static int cmd_backlight(struct term *t, const char *args)
  568. {
  569. const char *p = args;
  570. (void)t;
  571. while (*p == ' ')
  572. p++;
  573. if (!*p) {
  574. backlight_print(0);
  575. return 0;
  576. }
  577. if (arg_is(p, "probe")) {
  578. backlight_print(1);
  579. return 0;
  580. }
  581. if (arg_is(p, "up")) {
  582. backlight_step(1);
  583. backlight_print(0);
  584. return 0;
  585. }
  586. if (arg_is(p, "down")) {
  587. backlight_step(-1);
  588. backlight_print(0);
  589. return 0;
  590. }
  591. {
  592. unsigned int pct = 0;
  593. int digits = 0;
  594. while (*p >= '0' && *p <= '9') {
  595. pct = pct * 10 + (unsigned int)(*p - '0');
  596. p++;
  597. digits = 1;
  598. }
  599. while (*p == ' ')
  600. p++;
  601. if (digits && !*p) {
  602. backlight_set_percent((int)pct);
  603. backlight_print(0);
  604. return 0;
  605. }
  606. }
  607. io_putstr("backlight: usage backlight [0-100|up|down|probe]\n");
  608. return 0;
  609. }
  610. static int cmd_cpufreq(struct term *t, const char *args)
  611. {
  612. (void)t;
  613. power_cpufreq_command(args);
  614. return 0;
  615. }
  616. static int cmd_renice(struct term *t, const char *args)
  617. {
  618. unsigned int pid = 0, weight = 0;
  619. int have_pid = 0, have_weight = 0;
  620. (void)t;
  621. while (*args >= '0' && *args <= '9') {
  622. pid = pid * 10 + (unsigned int)(*args - '0');
  623. args++;
  624. have_pid = 1;
  625. }
  626. while (*args == ' ')
  627. args++;
  628. while (*args >= '0' && *args <= '9') {
  629. weight = weight * 10 + (unsigned int)(*args - '0');
  630. args++;
  631. have_weight = 1;
  632. }
  633. while (*args == ' ')
  634. args++;
  635. if (!have_pid || !have_weight || *args ||
  636. weight < 1 || weight > SCHED_WEIGHT_MAX) {
  637. io_putstr("renice: usage renice <pid> <1..16>\n");
  638. return 0;
  639. }
  640. if (process_set_weight(pid, weight) < 0)
  641. io_putstr("renice: no such process\n");
  642. else
  643. io_putstr("weight changed\n");
  644. return 0;
  645. }
  646. static int cmd_reboot(struct term *t, const char *args)
  647. {
  648. (void)t;
  649. (void)args;
  650. program_reboot_main();
  651. return 0;
  652. }
  653. static int cmd_serial(struct term *t, const char *args)
  654. {
  655. (void)t;
  656. if (args[0] == 't' && args[1] == 'e' && args[2] == 's' && args[3] == 't' &&
  657. args[4] == '\0') {
  658. if (process_create_arg("serial_loop", program_serial_loop_main, "test") < 0)
  659. io_putstr("error: too many processes\n");
  660. } else {
  661. io_putstr("serial: usage serial test\n");
  662. }
  663. return 0;
  664. }
  665. struct kcmd_ctx {
  666. char name[16];
  667. char args[VFS_PATH_MAX];
  668. };
  669. static const struct kcmd *kcmd_find(const char *name)
  670. {
  671. unsigned int i;
  672. for (i = 0; i < NCMDS; i++)
  673. if (cmds[i].name[0] == name[0]) {
  674. unsigned int j = 0;
  675. while (cmds[i].name[j] && name[j] && cmds[i].name[j] == name[j])
  676. j++;
  677. if (!cmds[i].name[j] && !name[j])
  678. return &cmds[i];
  679. }
  680. return 0;
  681. }
  682. static void kcmd_entry(void *arg)
  683. {
  684. struct kcmd_ctx *ctx = (struct kcmd_ctx *)arg;
  685. const struct kcmd *c = kcmd_find(ctx->name);
  686. if (c) {
  687. if (c->kind == CK_CUSTOM) {
  688. struct term *t = (struct term *)mm_alloc_pages(2);
  689. unsigned int i;
  690. if (!t) {
  691. io_putstr("kcmd: out of memory\n");
  692. } else {
  693. unsigned char *p = (unsigned char *)t;
  694. for (i = 0; i < 2 * PAGE_SIZE; i++)
  695. p[i] = 0;
  696. /* Kernel-command output has no cell screen; help uses
  697. * this width to lay out its columns on the tty. */
  698. t->screen.cols = 64;
  699. t->screen.rows = 32;
  700. for (i = 0; i + 1 < sizeof(t->cwd) &&
  701. current_proc()->cwd[i]; i++)
  702. t->cwd[i] = current_proc()->cwd[i];
  703. t->cwd[i] = '\0';
  704. {
  705. const char *def = "/ramfs/bin";
  706. for (i = 0; def[i] && i + 1 < sizeof(t->path); i++)
  707. t->path[i] = def[i];
  708. t->path[i] = '\0';
  709. }
  710. c->custom(t, ctx->args);
  711. mm_free_page((unsigned int)t);
  712. mm_free_page((unsigned int)t + PAGE_SIZE);
  713. }
  714. } else if (c->kind == CK_SPAWN) {
  715. if (c->fixed)
  716. c->fn_arg(c->fixed);
  717. else
  718. c->fn();
  719. } else if (c->kind == CK_SPAWN_ARG) {
  720. c->fn_arg(ctx->args);
  721. }
  722. } else {
  723. io_putstr(ctx->name);
  724. io_putstr(": no such kernel command\n");
  725. }
  726. kfree(ctx);
  727. process_exit();
  728. }
  729. int term_kcmd_spawn(const char *name, const char *args, unsigned int tty)
  730. {
  731. const struct kcmd *c = kcmd_find(name);
  732. struct kcmd_ctx *ctx;
  733. unsigned int i;
  734. int pid;
  735. if (!c || ((c->flags & CMD_BUILTIN) && c->custom != cmd_help &&
  736. c->custom != cmd_bootlog &&
  737. c->custom != cmd_seriallog))
  738. return -1;
  739. ctx = (struct kcmd_ctx *)kmalloc(sizeof(*ctx));
  740. if (!ctx)
  741. return -1;
  742. for (i = 0; i + 1 < sizeof(ctx->name) && name[i]; i++)
  743. ctx->name[i] = name[i];
  744. ctx->name[i] = '\0';
  745. for (i = 0; i + 1 < sizeof(ctx->args) && args[i]; i++)
  746. ctx->args[i] = args[i];
  747. ctx->args[i] = '\0';
  748. pid = process_create_ptr("kcmd", kcmd_entry, ctx);
  749. if (pid < 0) {
  750. kfree(ctx);
  751. return -1;
  752. }
  753. if (tty)
  754. process_set_tty((unsigned int)pid, (int)tty);
  755. return pid;
  756. }