bootmenu.c 9.3 KB

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