terminal.c 19 KB

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