terminal.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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 (xHCI, usbhid, USB errors and explicit
  191. * touchpad diagnostics) to a file. On a serial-less board, write it to the
  192. * USB ESP and read it from 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. int show = 0;
  207. while (j < len && p[j] != '\n')
  208. j++;
  209. {
  210. static const char *const pat[3] = {
  211. "xhci", "usbhid", "touchpad"
  212. };
  213. for (k = 0; k < 3; k++) {
  214. const char *q = pat[k];
  215. unsigned int a, b;
  216. for (a = i; a < j && !show; a++) {
  217. for (b = 0; q[b] &&
  218. a + b < j &&
  219. p[a + b] == q[b]; b++)
  220. ;
  221. if (!q[b])
  222. show = 1;
  223. }
  224. }
  225. }
  226. if (show) {
  227. for (k = i; k < j; k++)
  228. if (p[k] != '\r')
  229. io_putchar(p[k]);
  230. io_putchar('\n');
  231. }
  232. i = j < len ? j + 1 : j;
  233. }
  234. if (bootcon_serlog_truncated())
  235. io_putstr("seriallog: [truncated]\n");
  236. return 0;
  237. }
  238. if (!(args[0] == 's' && args[1] == 'a' && args[2] == 'v' &&
  239. args[3] == 'e' && (args[4] == ' ' || args[4] == '\0'))) {
  240. io_putstr("seriallog: usage seriallog save <path>|show\n");
  241. return 0;
  242. }
  243. {
  244. const char *path = args + 4;
  245. int r;
  246. while (*path == ' ')
  247. path++;
  248. if (!*path) {
  249. io_putstr("seriallog: usage seriallog save <path>\n");
  250. return 0;
  251. }
  252. r = sys_write_file(path, p, len);
  253. if (r == 0)
  254. io_putstr("seriallog saved\n");
  255. else {
  256. io_putstr("seriallog: save failed (");
  257. io_puthex((unsigned int)r);
  258. io_putstr(")\n");
  259. }
  260. }
  261. if (bootcon_serlog_truncated())
  262. io_putstr("seriallog: [truncated]\n");
  263. return 0;
  264. }
  265. static int cmd_dbg(struct term *t, const char *args)
  266. {
  267. (void)t;
  268. (void)args;
  269. program_dbg_main();
  270. return 0;
  271. }
  272. static int cmd_format(struct term *t, const char *args)
  273. {
  274. (void)t;
  275. program_format_main(args);
  276. return 0;
  277. }
  278. static int cmd_install(struct term *t, const char *args)
  279. {
  280. (void)t;
  281. program_install_main(args);
  282. return 0;
  283. }
  284. /* `path` builtin: show or set the command search path. */
  285. /* List the command files found on the search path. */
  286. static void help_print_path(struct term *t)
  287. {
  288. const char *p = t->path;
  289. while (*p) {
  290. char dir[VFS_PATH_MAX];
  291. unsigned int n = 0, col = 0, i;
  292. while (*p && *p != ':' && n + 1 < sizeof(dir))
  293. dir[n++] = *p++;
  294. dir[n] = '\0';
  295. if (*p == ':')
  296. p++;
  297. if (dir[0] == '\0')
  298. continue;
  299. io_putstr(dir);
  300. io_putstr(":\n");
  301. for (i = 0; ; i++) {
  302. struct vfs_dirent d;
  303. unsigned int len = 0;
  304. int r = vfs_list_dir(dir, i, &d);
  305. if (r != 1)
  306. break;
  307. if (d.is_dir)
  308. continue;
  309. while (d.name[len])
  310. len++;
  311. if (col > 0 && col + 2 + len > t->screen.cols) {
  312. io_putchar('\n');
  313. col = 0;
  314. }
  315. if (col > 0) {
  316. io_putstr(" ");
  317. col += 2;
  318. }
  319. io_putstr(d.name);
  320. col += len;
  321. }
  322. io_putchar('\n');
  323. }
  324. }
  325. static int cmd_help(struct term *t, const char *args)
  326. {
  327. (void)args;
  328. io_putstr("shell builtins: cd path clear\n");
  329. help_print();
  330. help_print_path(t);
  331. return 0;
  332. }
  333. static int cmd_kexec(struct term *t, const char *args)
  334. {
  335. (void)t;
  336. (void)args;
  337. if (kexec_busy) {
  338. io_putstr("kexec: already receiving\n");
  339. return 0;
  340. }
  341. kexec_busy = 1;
  342. if (process_create("kexec", program_kexec_main) < 0) {
  343. kexec_busy = 0;
  344. io_putstr("error: too many processes\n");
  345. }
  346. return 0;
  347. }
  348. static int cmd_kill(struct term *t, const char *args)
  349. {
  350. unsigned int pid = 0;
  351. int has_digit = 0;
  352. (void)t;
  353. while (*args >= '0' && *args <= '9') {
  354. pid = pid * 10 + (unsigned int)(*args - '0');
  355. args++;
  356. has_digit = 1;
  357. }
  358. while (*args == ' ')
  359. args++;
  360. if (!has_digit || *args) {
  361. io_putstr("kill: usage kill <pid>\n");
  362. return 0;
  363. }
  364. if (process_kill(pid) < 0)
  365. io_putstr("kill: no such process\n");
  366. else
  367. io_putstr("killed\n");
  368. return 0;
  369. }
  370. static int cmd_ps(struct term *t, const char *args)
  371. {
  372. (void)t;
  373. (void)args;
  374. process_ps();
  375. return 0;
  376. }
  377. static int cmd_sched(struct term *t, const char *args)
  378. {
  379. (void)t;
  380. (void)args;
  381. sched_print();
  382. return 0;
  383. }
  384. static int arg_is(const char *args, const char *word)
  385. {
  386. unsigned int i = 0;
  387. while (word[i] && args[i] == word[i])
  388. i++;
  389. return word[i] == '\0' && args[i] == '\0';
  390. }
  391. static int cmd_idle(struct term *t, const char *args)
  392. {
  393. (void)t;
  394. if (!args[0] || arg_is(args, "status")) {
  395. io_putstr("idle ");
  396. io_putstr(idle_mode_name(idle_mode()));
  397. if (!idle_mwait_supported())
  398. io_putstr(" (mwait unsupported)");
  399. io_putchar('\n');
  400. return 0;
  401. }
  402. if (arg_is(args, "auto")) {
  403. idle_set_mode(IDLE_AUTO);
  404. io_putstr("idle auto\n");
  405. return 0;
  406. }
  407. if (arg_is(args, "hlt")) {
  408. idle_set_mode(IDLE_C1);
  409. io_putstr("idle hlt\n");
  410. return 0;
  411. }
  412. {
  413. int mode = -1;
  414. if (arg_is(args, "c1e"))
  415. mode = IDLE_C1E;
  416. else if (arg_is(args, "c3"))
  417. mode = IDLE_C3;
  418. else if (arg_is(args, "c6"))
  419. mode = IDLE_C6;
  420. else if (arg_is(args, "c3ap"))
  421. mode = IDLE_C3_AP;
  422. else if (arg_is(args, "c6ap"))
  423. mode = IDLE_C6_AP;
  424. if (mode >= 0) {
  425. if (idle_set_mode(mode) < 0) {
  426. io_putstr("idle: mwait not available\n");
  427. return 0;
  428. }
  429. io_putstr("idle ");
  430. io_putstr(idle_mode_name(mode));
  431. io_putstr(" (test the keyboard; power-cycle if it hangs)\n");
  432. return 0;
  433. }
  434. }
  435. io_putstr("idle: usage idle [auto|hlt|c1e|c3|c6|c3ap|c6ap]\n");
  436. return 0;
  437. }
  438. static int cmd_power(struct term *t, const char *args)
  439. {
  440. (void)t;
  441. (void)args;
  442. power_print();
  443. return 0;
  444. }
  445. static void cmd_put_dec(unsigned int v)
  446. {
  447. char b[12];
  448. int i = 0;
  449. if (!v) {
  450. io_putchar('0');
  451. return;
  452. }
  453. while (v) {
  454. b[i++] = (char)('0' + v % 10);
  455. v /= 10;
  456. }
  457. while (i)
  458. io_putchar(b[--i]);
  459. }
  460. static void cmd_put_hex(unsigned int v, unsigned int digits)
  461. {
  462. static const char hex[] = "0123456789ABCDEF";
  463. while (digits) {
  464. digits--;
  465. io_putchar(hex[(v >> (digits * 4)) & 0xF]);
  466. }
  467. }
  468. static const char *cmd_raw_name(unsigned char raw)
  469. {
  470. switch (raw) {
  471. case 0x3B: return "F1";
  472. case 0x3C: return "F2";
  473. case 0x3D: return "F3";
  474. case 0x3E: return "F4";
  475. case 0x3F: return "F5";
  476. case 0x40: return "F6";
  477. case 0x41: return "F7";
  478. case 0x42: return "F8";
  479. case 0x43: return "F9";
  480. case 0x44: return "F10";
  481. case 0x57: return "F11";
  482. case 0x58: return "F12";
  483. }
  484. return 0;
  485. }
  486. static const char *cmd_key_name(int k)
  487. {
  488. switch (k) {
  489. case '\n': return "Enter";
  490. case '\t': return "Tab";
  491. case '\b': return "Backspace";
  492. case 0x1B: return "Esc";
  493. case IO_KEY_LEFT: return "Left";
  494. case IO_KEY_RIGHT: return "Right";
  495. case IO_KEY_UP: return "Up";
  496. case IO_KEY_DOWN: return "Down";
  497. case IO_KEY_PAGE_UP: return "PageUp";
  498. case IO_KEY_PAGE_DOWN: return "PageDown";
  499. case IO_KEY_HOME: return "Home";
  500. case IO_KEY_END: return "End";
  501. case IO_KEY_DELETE: return "Delete";
  502. case IO_KEY_CTRL_LEFT: return "Ctrl+Left";
  503. case IO_KEY_CTRL_RIGHT: return "Ctrl+Right";
  504. case IO_KEY_CTRL_DELETE: return "Ctrl+Delete";
  505. case IO_KEY_CTRL_BACKSPACE: return "Ctrl+Backspace";
  506. }
  507. return 0;
  508. }
  509. /* Print every raw scancode the decoder sees, including releases, E0
  510. * prefixes and keys that decode to 0 (the brightness/action keys). */
  511. static int cmd_keytest(struct term *t, const char *args)
  512. {
  513. struct io_keytap e;
  514. (void)t;
  515. (void)args;
  516. io_keytap_reset();
  517. io_putstr("keytest: press keys (Ctrl+C to stop; E0 = prefix)\n");
  518. for (;;) {
  519. while (io_keytap_read(&e)) {
  520. const char *name;
  521. io_putstr("raw ");
  522. cmd_put_hex(e.raw, 2);
  523. io_putstr(e.ext ? " ext" : " ");
  524. if (e.kind == 2)
  525. io_putstr(" prefix");
  526. else if (e.kind == 1)
  527. io_putstr(" break ");
  528. else
  529. io_putstr(" make ");
  530. io_putstr(" -> 0x");
  531. cmd_put_hex((unsigned int)e.key, 4);
  532. name = e.kind == 0 ? cmd_key_name(e.key) : 0;
  533. if (name) {
  534. io_putstr(" (");
  535. io_putstr(name);
  536. io_putchar(')');
  537. } else if (e.kind == 0 && e.key >= 0x20 && e.key <= 0x7E) {
  538. io_putstr(" '");
  539. io_putchar((char)e.key);
  540. io_putstr("'");
  541. } else if (e.kind == 0 && !e.ext) {
  542. name = cmd_raw_name(e.raw);
  543. if (name) {
  544. io_putstr(" (");
  545. io_putstr(name);
  546. io_putchar(')');
  547. }
  548. }
  549. io_putchar('\n');
  550. }
  551. process_sleep_ms(20);
  552. }
  553. return 0;
  554. }
  555. static void backlight_print(int probe)
  556. {
  557. struct backlight_status st;
  558. if (backlight_status(&st) != 0) {
  559. io_putstr("backlight: no Intel CHV/VLV panel PWM\n");
  560. return;
  561. }
  562. io_putstr("backlight: ");
  563. cmd_put_dec((unsigned int)st.level);
  564. io_putstr("% (pipe ");
  565. io_putstr(st.pipe ? "B" : "A");
  566. io_putstr(", period ");
  567. cmd_put_dec(st.period);
  568. io_putstr(", duty ");
  569. cmd_put_dec(st.duty);
  570. io_putstr(", ");
  571. io_putstr(st.enabled ? "enabled" : "disabled");
  572. io_putstr(")\n");
  573. if (!probe)
  574. return;
  575. io_putstr(" bar 0x");
  576. cmd_put_hex(st.bar, 8);
  577. io_putstr("\n ctl2 A=0x");
  578. cmd_put_hex(st.ctl2[0], 8);
  579. io_putstr(" B=0x");
  580. cmd_put_hex(st.ctl2[1], 8);
  581. io_putstr("\n ctl A=0x");
  582. cmd_put_hex(st.ctl[0], 8);
  583. io_putstr(" B=0x");
  584. cmd_put_hex(st.ctl[1], 8);
  585. io_putchar('\n');
  586. }
  587. static int cmd_backlight(struct term *t, const char *args)
  588. {
  589. const char *p = args;
  590. (void)t;
  591. while (*p == ' ')
  592. p++;
  593. if (!*p) {
  594. backlight_print(0);
  595. return 0;
  596. }
  597. if (arg_is(p, "probe")) {
  598. backlight_print(1);
  599. return 0;
  600. }
  601. if (arg_is(p, "up")) {
  602. backlight_step(1);
  603. backlight_print(0);
  604. return 0;
  605. }
  606. if (arg_is(p, "down")) {
  607. backlight_step(-1);
  608. backlight_print(0);
  609. return 0;
  610. }
  611. {
  612. unsigned int pct = 0;
  613. int digits = 0;
  614. while (*p >= '0' && *p <= '9') {
  615. pct = pct * 10 + (unsigned int)(*p - '0');
  616. p++;
  617. digits = 1;
  618. }
  619. while (*p == ' ')
  620. p++;
  621. if (digits && !*p) {
  622. backlight_set_percent((int)pct);
  623. backlight_print(0);
  624. return 0;
  625. }
  626. }
  627. io_putstr("backlight: usage backlight [0-100|up|down|probe]\n");
  628. return 0;
  629. }
  630. static int cmd_cpufreq(struct term *t, const char *args)
  631. {
  632. (void)t;
  633. power_cpufreq_command(args);
  634. return 0;
  635. }
  636. static int cmd_renice(struct term *t, const char *args)
  637. {
  638. unsigned int pid = 0, weight = 0;
  639. int have_pid = 0, have_weight = 0;
  640. (void)t;
  641. while (*args >= '0' && *args <= '9') {
  642. pid = pid * 10 + (unsigned int)(*args - '0');
  643. args++;
  644. have_pid = 1;
  645. }
  646. while (*args == ' ')
  647. args++;
  648. while (*args >= '0' && *args <= '9') {
  649. weight = weight * 10 + (unsigned int)(*args - '0');
  650. args++;
  651. have_weight = 1;
  652. }
  653. while (*args == ' ')
  654. args++;
  655. if (!have_pid || !have_weight || *args ||
  656. weight < 1 || weight > SCHED_WEIGHT_MAX) {
  657. io_putstr("renice: usage renice <pid> <1..16>\n");
  658. return 0;
  659. }
  660. if (process_set_weight(pid, weight) < 0)
  661. io_putstr("renice: no such process\n");
  662. else
  663. io_putstr("weight changed\n");
  664. return 0;
  665. }
  666. static int cmd_reboot(struct term *t, const char *args)
  667. {
  668. (void)t;
  669. (void)args;
  670. program_reboot_main();
  671. return 0;
  672. }
  673. static int cmd_serial(struct term *t, const char *args)
  674. {
  675. (void)t;
  676. if (args[0] == 't' && args[1] == 'e' && args[2] == 's' && args[3] == 't' &&
  677. args[4] == '\0') {
  678. if (process_create_arg("serial_loop", program_serial_loop_main, "test") < 0)
  679. io_putstr("error: too many processes\n");
  680. } else {
  681. io_putstr("serial: usage serial test\n");
  682. }
  683. return 0;
  684. }
  685. struct kcmd_ctx {
  686. char name[16];
  687. char args[VFS_PATH_MAX];
  688. };
  689. static const struct kcmd *kcmd_find(const char *name)
  690. {
  691. unsigned int i;
  692. for (i = 0; i < NCMDS; i++)
  693. if (cmds[i].name[0] == name[0]) {
  694. unsigned int j = 0;
  695. while (cmds[i].name[j] && name[j] && cmds[i].name[j] == name[j])
  696. j++;
  697. if (!cmds[i].name[j] && !name[j])
  698. return &cmds[i];
  699. }
  700. return 0;
  701. }
  702. static void kcmd_entry(void *arg)
  703. {
  704. struct kcmd_ctx *ctx = (struct kcmd_ctx *)arg;
  705. const struct kcmd *c = kcmd_find(ctx->name);
  706. if (c) {
  707. if (c->kind == CK_CUSTOM) {
  708. struct term *t = (struct term *)mm_alloc_pages(2);
  709. unsigned int i;
  710. if (!t) {
  711. io_putstr("kcmd: out of memory\n");
  712. } else {
  713. unsigned char *p = (unsigned char *)t;
  714. for (i = 0; i < 2 * PAGE_SIZE; i++)
  715. p[i] = 0;
  716. /* Kernel-command output has no cell screen; help uses
  717. * this width to lay out its columns on the tty. */
  718. t->screen.cols = 64;
  719. t->screen.rows = 32;
  720. for (i = 0; i + 1 < sizeof(t->cwd) &&
  721. current_proc()->cwd[i]; i++)
  722. t->cwd[i] = current_proc()->cwd[i];
  723. t->cwd[i] = '\0';
  724. {
  725. const char *def = "/ramfs/bin";
  726. for (i = 0; def[i] && i + 1 < sizeof(t->path); i++)
  727. t->path[i] = def[i];
  728. t->path[i] = '\0';
  729. }
  730. c->custom(t, ctx->args);
  731. mm_free_page((unsigned int)t);
  732. mm_free_page((unsigned int)t + PAGE_SIZE);
  733. }
  734. } else if (c->kind == CK_SPAWN) {
  735. if (c->fixed)
  736. c->fn_arg(c->fixed);
  737. else
  738. c->fn();
  739. } else if (c->kind == CK_SPAWN_ARG) {
  740. c->fn_arg(ctx->args);
  741. }
  742. } else {
  743. io_putstr(ctx->name);
  744. io_putstr(": no such kernel command\n");
  745. }
  746. kfree(ctx);
  747. process_exit();
  748. }
  749. int term_kcmd_spawn(const char *name, const char *args, unsigned int tty)
  750. {
  751. const struct kcmd *c = kcmd_find(name);
  752. struct kcmd_ctx *ctx;
  753. unsigned int i;
  754. int pid;
  755. if (!c || ((c->flags & CMD_BUILTIN) && c->custom != cmd_help &&
  756. c->custom != cmd_bootlog &&
  757. c->custom != cmd_seriallog))
  758. return -1;
  759. ctx = (struct kcmd_ctx *)kmalloc(sizeof(*ctx));
  760. if (!ctx)
  761. return -1;
  762. for (i = 0; i + 1 < sizeof(ctx->name) && name[i]; i++)
  763. ctx->name[i] = name[i];
  764. ctx->name[i] = '\0';
  765. for (i = 0; i + 1 < sizeof(ctx->args) && args[i]; i++)
  766. ctx->args[i] = args[i];
  767. ctx->args[i] = '\0';
  768. pid = process_create_ptr("kcmd", kcmd_entry, ctx);
  769. if (pid < 0) {
  770. kfree(ctx);
  771. return -1;
  772. }
  773. if (tty)
  774. process_set_tty((unsigned int)pid, (int)tty);
  775. return pid;
  776. }