bootmenu.c 9.3 KB

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