terminal.c 19 KB

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