bootmenu.c 9.7 KB

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