bootmenu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2011-2013 Pali Rohár <pali@kernel.org>
  4. */
  5. #include <common.h>
  6. #include <command.h>
  7. #include <ansi.h>
  8. #include <env.h>
  9. #include <log.h>
  10. #include <menu.h>
  11. #include <watchdog.h>
  12. #include <malloc.h>
  13. #include <linux/delay.h>
  14. #include <linux/string.h>
  15. /* maximum bootmenu entries */
  16. #define MAX_COUNT 99
  17. /* maximal size of bootmenu env
  18. * 9 = strlen("bootmenu_")
  19. * 2 = strlen(MAX_COUNT)
  20. * 1 = NULL term
  21. */
  22. #define MAX_ENV_SIZE (9 + 2 + 1)
  23. struct bootmenu_entry {
  24. unsigned short int num; /* unique number 0 .. MAX_COUNT */
  25. char key[3]; /* key identifier of number */
  26. char *title; /* title of entry */
  27. char *command; /* hush command of entry */
  28. struct bootmenu_data *menu; /* this bootmenu */
  29. struct bootmenu_entry *next; /* next menu entry (num+1) */
  30. };
  31. struct bootmenu_data {
  32. int delay; /* delay for autoboot */
  33. int active; /* active menu entry */
  34. int count; /* total count of menu entries */
  35. struct bootmenu_entry *first; /* first menu entry */
  36. };
  37. enum bootmenu_key {
  38. KEY_NONE = 0,
  39. KEY_UP,
  40. KEY_DOWN,
  41. KEY_SELECT,
  42. KEY_QUIT,
  43. };
  44. static char *bootmenu_getoption(unsigned short int n)
  45. {
  46. char name[MAX_ENV_SIZE];
  47. if (n > MAX_COUNT)
  48. return NULL;
  49. sprintf(name, "bootmenu_%d", n);
  50. return env_get(name);
  51. }
  52. static void bootmenu_print_entry(void *data)
  53. {
  54. struct bootmenu_entry *entry = data;
  55. int reverse = (entry->menu->active == entry->num);
  56. /*
  57. * Move cursor to line where the entry will be drown (entry->num)
  58. * First 3 lines contain bootmenu header + 1 empty line
  59. */
  60. printf(ANSI_CURSOR_POSITION, entry->num + 4, 1);
  61. puts(" ");
  62. if (reverse)
  63. puts(ANSI_COLOR_REVERSE);
  64. puts(entry->title);
  65. if (reverse)
  66. puts(ANSI_COLOR_RESET);
  67. }
  68. static void bootmenu_autoboot_loop(struct bootmenu_data *menu,
  69. enum bootmenu_key *key, int *esc)
  70. {
  71. int i, c;
  72. if (menu->delay > 0) {
  73. printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
  74. printf(" Hit any key to stop autoboot: %2d ", menu->delay);
  75. }
  76. while (menu->delay > 0) {
  77. for (i = 0; i < 100; ++i) {
  78. if (!tstc()) {
  79. WATCHDOG_RESET();
  80. mdelay(10);
  81. continue;
  82. }
  83. menu->delay = -1;
  84. c = getchar();
  85. switch (c) {
  86. case '\e':
  87. *esc = 1;
  88. *key = KEY_NONE;
  89. break;
  90. case '\r':
  91. *key = KEY_SELECT;
  92. break;
  93. case 0x3: /* ^C */
  94. *key = KEY_QUIT;
  95. break;
  96. default:
  97. *key = KEY_NONE;
  98. break;
  99. }
  100. break;
  101. }
  102. if (menu->delay < 0)
  103. break;
  104. --menu->delay;
  105. printf("\b\b\b%2d ", menu->delay);
  106. }
  107. printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
  108. puts(ANSI_CLEAR_LINE);
  109. if (menu->delay == 0)
  110. *key = KEY_SELECT;
  111. }
  112. static void bootmenu_loop(struct bootmenu_data *menu,
  113. enum bootmenu_key *key, int *esc)
  114. {
  115. int c;
  116. if (*esc == 1) {
  117. if (tstc()) {
  118. c = getchar();
  119. } else {
  120. WATCHDOG_RESET();
  121. mdelay(10);
  122. if (tstc())
  123. c = getchar();
  124. else
  125. c = '\e';
  126. }
  127. } else {
  128. while (!tstc()) {
  129. WATCHDOG_RESET();
  130. mdelay(10);
  131. }
  132. c = getchar();
  133. }
  134. switch (*esc) {
  135. case 0:
  136. /* First char of ANSI escape sequence '\e' */
  137. if (c == '\e') {
  138. *esc = 1;
  139. *key = KEY_NONE;
  140. }
  141. break;
  142. case 1:
  143. /* Second char of ANSI '[' */
  144. if (c == '[') {
  145. *esc = 2;
  146. *key = KEY_NONE;
  147. } else {
  148. /* Alone ESC key was pressed */
  149. *key = KEY_QUIT;
  150. *esc = (c == '\e') ? 1 : 0;
  151. }
  152. break;
  153. case 2:
  154. case 3:
  155. /* Third char of ANSI (number '1') - optional */
  156. if (*esc == 2 && c == '1') {
  157. *esc = 3;
  158. *key = KEY_NONE;
  159. break;
  160. }
  161. *esc = 0;
  162. /* ANSI 'A' - key up was pressed */
  163. if (c == 'A')
  164. *key = KEY_UP;
  165. /* ANSI 'B' - key down was pressed */
  166. else if (c == 'B')
  167. *key = KEY_DOWN;
  168. /* other key was pressed */
  169. else
  170. *key = KEY_NONE;
  171. break;
  172. }
  173. /* enter key was pressed */
  174. if (c == '\r')
  175. *key = KEY_SELECT;
  176. /* ^C was pressed */
  177. if (c == 0x3)
  178. *key = KEY_QUIT;
  179. }
  180. static char *bootmenu_choice_entry(void *data)
  181. {
  182. struct bootmenu_data *menu = data;
  183. struct bootmenu_entry *iter;
  184. enum bootmenu_key key = KEY_NONE;
  185. int esc = 0;
  186. int i;
  187. while (1) {
  188. if (menu->delay >= 0) {
  189. /* Autoboot was not stopped */
  190. bootmenu_autoboot_loop(menu, &key, &esc);
  191. } else {
  192. /* Some key was pressed, so autoboot was stopped */
  193. bootmenu_loop(menu, &key, &esc);
  194. }
  195. switch (key) {
  196. case KEY_UP:
  197. if (menu->active > 0)
  198. --menu->active;
  199. /* no menu key selected, regenerate menu */
  200. return NULL;
  201. case KEY_DOWN:
  202. if (menu->active < menu->count - 1)
  203. ++menu->active;
  204. /* no menu key selected, regenerate menu */
  205. return NULL;
  206. case KEY_SELECT:
  207. iter = menu->first;
  208. for (i = 0; i < menu->active; ++i)
  209. iter = iter->next;
  210. return iter->key;
  211. case KEY_QUIT:
  212. /* Quit by choosing the last entry - U-Boot console */
  213. iter = menu->first;
  214. while (iter->next)
  215. iter = iter->next;
  216. return iter->key;
  217. default:
  218. break;
  219. }
  220. }
  221. /* never happens */
  222. debug("bootmenu: this should not happen");
  223. return NULL;
  224. }
  225. static void bootmenu_destroy(struct bootmenu_data *menu)
  226. {
  227. struct bootmenu_entry *iter = menu->first;
  228. struct bootmenu_entry *next;
  229. while (iter) {
  230. next = iter->next;
  231. free(iter->title);
  232. free(iter->command);
  233. free(iter);
  234. iter = next;
  235. }
  236. free(menu);
  237. }
  238. static struct bootmenu_data *bootmenu_create(int delay)
  239. {
  240. unsigned short int i = 0;
  241. const char *option;
  242. struct bootmenu_data *menu;
  243. struct bootmenu_entry *iter = NULL;
  244. int len;
  245. char *sep;
  246. char *default_str;
  247. struct bootmenu_entry *entry;
  248. menu = malloc(sizeof(struct bootmenu_data));
  249. if (!menu)
  250. return NULL;
  251. menu->delay = delay;
  252. menu->active = 0;
  253. menu->first = NULL;
  254. default_str = env_get("bootmenu_default");
  255. if (default_str)
  256. menu->active = (int)simple_strtol(default_str, NULL, 10);
  257. while ((option = bootmenu_getoption(i))) {
  258. sep = strchr(option, '=');
  259. if (!sep) {
  260. printf("Invalid bootmenu entry: %s\n", option);
  261. break;
  262. }
  263. entry = malloc(sizeof(struct bootmenu_entry));
  264. if (!entry)
  265. goto cleanup;
  266. len = sep-option;
  267. entry->title = malloc(len + 1);
  268. if (!entry->title) {
  269. free(entry);
  270. goto cleanup;
  271. }
  272. memcpy(entry->title, option, len);
  273. entry->title[len] = 0;
  274. len = strlen(sep + 1);
  275. entry->command = malloc(len + 1);
  276. if (!entry->command) {
  277. free(entry->title);
  278. free(entry);
  279. goto cleanup;
  280. }
  281. memcpy(entry->command, sep + 1, len);
  282. entry->command[len] = 0;
  283. sprintf(entry->key, "%d", i);
  284. entry->num = i;
  285. entry->menu = menu;
  286. entry->next = NULL;
  287. if (!iter)
  288. menu->first = entry;
  289. else
  290. iter->next = entry;
  291. iter = entry;
  292. ++i;
  293. if (i == MAX_COUNT - 1)
  294. break;
  295. }
  296. /* Add U-Boot console entry at the end */
  297. if (i <= MAX_COUNT - 1) {
  298. entry = malloc(sizeof(struct bootmenu_entry));
  299. if (!entry)
  300. goto cleanup;
  301. entry->title = strdup("U-Boot console");
  302. if (!entry->title) {
  303. free(entry);
  304. goto cleanup;
  305. }
  306. entry->command = strdup("");
  307. if (!entry->command) {
  308. free(entry->title);
  309. free(entry);
  310. goto cleanup;
  311. }
  312. sprintf(entry->key, "%d", i);
  313. entry->num = i;
  314. entry->menu = menu;
  315. entry->next = NULL;
  316. if (!iter)
  317. menu->first = entry;
  318. else
  319. iter->next = entry;
  320. iter = entry;
  321. ++i;
  322. }
  323. menu->count = i;
  324. if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
  325. printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
  326. menu->active=0;
  327. }
  328. return menu;
  329. cleanup:
  330. bootmenu_destroy(menu);
  331. return NULL;
  332. }
  333. static void menu_display_statusline(struct menu *m)
  334. {
  335. struct bootmenu_entry *entry;
  336. struct bootmenu_data *menu;
  337. if (menu_default_choice(m, (void *)&entry) < 0)
  338. return;
  339. menu = entry->menu;
  340. printf(ANSI_CURSOR_POSITION, 1, 1);
  341. puts(ANSI_CLEAR_LINE);
  342. printf(ANSI_CURSOR_POSITION, 2, 1);
  343. puts(" *** U-Boot Boot Menu ***");
  344. puts(ANSI_CLEAR_LINE_TO_END);
  345. printf(ANSI_CURSOR_POSITION, 3, 1);
  346. puts(ANSI_CLEAR_LINE);
  347. /* First 3 lines are bootmenu header + 2 empty lines between entries */
  348. printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
  349. puts(ANSI_CLEAR_LINE);
  350. printf(ANSI_CURSOR_POSITION, menu->count + 6, 1);
  351. puts(" Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit");
  352. puts(ANSI_CLEAR_LINE_TO_END);
  353. printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
  354. puts(ANSI_CLEAR_LINE);
  355. }
  356. static void bootmenu_show(int delay)
  357. {
  358. int init = 0;
  359. void *choice = NULL;
  360. char *title = NULL;
  361. char *command = NULL;
  362. struct menu *menu;
  363. struct bootmenu_data *bootmenu;
  364. struct bootmenu_entry *iter;
  365. char *option, *sep;
  366. /* If delay is 0 do not create menu, just run first entry */
  367. if (delay == 0) {
  368. option = bootmenu_getoption(0);
  369. if (!option) {
  370. puts("bootmenu option 0 was not found\n");
  371. return;
  372. }
  373. sep = strchr(option, '=');
  374. if (!sep) {
  375. puts("bootmenu option 0 is invalid\n");
  376. return;
  377. }
  378. run_command(sep+1, 0);
  379. return;
  380. }
  381. bootmenu = bootmenu_create(delay);
  382. if (!bootmenu)
  383. return;
  384. menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline,
  385. bootmenu_print_entry, bootmenu_choice_entry,
  386. bootmenu);
  387. if (!menu) {
  388. bootmenu_destroy(bootmenu);
  389. return;
  390. }
  391. for (iter = bootmenu->first; iter; iter = iter->next) {
  392. if (!menu_item_add(menu, iter->key, iter))
  393. goto cleanup;
  394. }
  395. /* Default menu entry is always first */
  396. menu_default_set(menu, "0");
  397. puts(ANSI_CURSOR_HIDE);
  398. puts(ANSI_CLEAR_CONSOLE);
  399. printf(ANSI_CURSOR_POSITION, 1, 1);
  400. init = 1;
  401. if (menu_get_choice(menu, &choice)) {
  402. iter = choice;
  403. title = strdup(iter->title);
  404. command = strdup(iter->command);
  405. }
  406. cleanup:
  407. menu_destroy(menu);
  408. bootmenu_destroy(bootmenu);
  409. if (init) {
  410. puts(ANSI_CURSOR_SHOW);
  411. puts(ANSI_CLEAR_CONSOLE);
  412. printf(ANSI_CURSOR_POSITION, 1, 1);
  413. }
  414. if (title && command) {
  415. debug("Starting entry '%s'\n", title);
  416. free(title);
  417. run_command(command, 0);
  418. free(command);
  419. }
  420. #ifdef CONFIG_POSTBOOTMENU
  421. run_command(CONFIG_POSTBOOTMENU, 0);
  422. #endif
  423. }
  424. #ifdef CONFIG_AUTOBOOT_MENU_SHOW
  425. int menu_show(int bootdelay)
  426. {
  427. bootmenu_show(bootdelay);
  428. return -1; /* -1 - abort boot and run monitor code */
  429. }
  430. #endif
  431. int do_bootmenu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  432. {
  433. char *delay_str = NULL;
  434. int delay = 10;
  435. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  436. delay = CONFIG_BOOTDELAY;
  437. #endif
  438. if (argc >= 2)
  439. delay_str = argv[1];
  440. if (!delay_str)
  441. delay_str = env_get("bootmenu_delay");
  442. if (delay_str)
  443. delay = (int)simple_strtol(delay_str, NULL, 10);
  444. bootmenu_show(delay);
  445. return 0;
  446. }
  447. U_BOOT_CMD(
  448. bootmenu, 2, 1, do_bootmenu,
  449. "ANSI terminal bootmenu",
  450. "[delay]\n"
  451. " - show ANSI terminal bootmenu with autoboot delay"
  452. );