bootmenu.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 <env.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 env_get(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. char *default_str;
  217. struct bootmenu_entry *entry;
  218. menu = malloc(sizeof(struct bootmenu_data));
  219. if (!menu)
  220. return NULL;
  221. menu->delay = delay;
  222. menu->active = 0;
  223. menu->first = NULL;
  224. default_str = env_get("bootmenu_default");
  225. if (default_str)
  226. menu->active = (int)simple_strtol(default_str, NULL, 10);
  227. while ((option = bootmenu_getoption(i))) {
  228. sep = strchr(option, '=');
  229. if (!sep) {
  230. printf("Invalid bootmenu entry: %s\n", option);
  231. break;
  232. }
  233. entry = malloc(sizeof(struct bootmenu_entry));
  234. if (!entry)
  235. goto cleanup;
  236. len = sep-option;
  237. entry->title = malloc(len + 1);
  238. if (!entry->title) {
  239. free(entry);
  240. goto cleanup;
  241. }
  242. memcpy(entry->title, option, len);
  243. entry->title[len] = 0;
  244. len = strlen(sep + 1);
  245. entry->command = malloc(len + 1);
  246. if (!entry->command) {
  247. free(entry->title);
  248. free(entry);
  249. goto cleanup;
  250. }
  251. memcpy(entry->command, sep + 1, len);
  252. entry->command[len] = 0;
  253. sprintf(entry->key, "%d", i);
  254. entry->num = i;
  255. entry->menu = menu;
  256. entry->next = NULL;
  257. if (!iter)
  258. menu->first = entry;
  259. else
  260. iter->next = entry;
  261. iter = entry;
  262. ++i;
  263. if (i == MAX_COUNT - 1)
  264. break;
  265. }
  266. /* Add U-Boot console entry at the end */
  267. if (i <= MAX_COUNT - 1) {
  268. entry = malloc(sizeof(struct bootmenu_entry));
  269. if (!entry)
  270. goto cleanup;
  271. entry->title = strdup("U-Boot console");
  272. if (!entry->title) {
  273. free(entry);
  274. goto cleanup;
  275. }
  276. entry->command = strdup("");
  277. if (!entry->command) {
  278. free(entry->title);
  279. free(entry);
  280. goto cleanup;
  281. }
  282. sprintf(entry->key, "%d", i);
  283. entry->num = i;
  284. entry->menu = menu;
  285. entry->next = NULL;
  286. if (!iter)
  287. menu->first = entry;
  288. else
  289. iter->next = entry;
  290. iter = entry;
  291. ++i;
  292. }
  293. menu->count = i;
  294. if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
  295. printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
  296. menu->active=0;
  297. }
  298. return menu;
  299. cleanup:
  300. bootmenu_destroy(menu);
  301. return NULL;
  302. }
  303. static void bootmenu_show(int delay)
  304. {
  305. int init = 0;
  306. void *choice = NULL;
  307. char *title = NULL;
  308. char *command = NULL;
  309. struct menu *menu;
  310. struct bootmenu_data *bootmenu;
  311. struct bootmenu_entry *iter;
  312. char *option, *sep;
  313. /* If delay is 0 do not create menu, just run first entry */
  314. if (delay == 0) {
  315. option = bootmenu_getoption(0);
  316. if (!option) {
  317. puts("bootmenu option 0 was not found\n");
  318. return;
  319. }
  320. sep = strchr(option, '=');
  321. if (!sep) {
  322. puts("bootmenu option 0 is invalid\n");
  323. return;
  324. }
  325. run_command(sep+1, 0);
  326. return;
  327. }
  328. bootmenu = bootmenu_create(delay);
  329. if (!bootmenu)
  330. return;
  331. menu = menu_create(NULL, bootmenu->delay, 1, bootmenu_print_entry,
  332. bootmenu_choice_entry, bootmenu);
  333. if (!menu) {
  334. bootmenu_destroy(bootmenu);
  335. return;
  336. }
  337. for (iter = bootmenu->first; iter; iter = iter->next) {
  338. if (!menu_item_add(menu, iter->key, iter))
  339. goto cleanup;
  340. }
  341. /* Default menu entry is always first */
  342. menu_default_set(menu, "0");
  343. puts(ANSI_CURSOR_HIDE);
  344. puts(ANSI_CLEAR_CONSOLE);
  345. printf(ANSI_CURSOR_POSITION, 1, 1);
  346. init = 1;
  347. if (menu_get_choice(menu, &choice)) {
  348. iter = choice;
  349. title = strdup(iter->title);
  350. command = strdup(iter->command);
  351. }
  352. cleanup:
  353. menu_destroy(menu);
  354. bootmenu_destroy(bootmenu);
  355. if (init) {
  356. puts(ANSI_CURSOR_SHOW);
  357. puts(ANSI_CLEAR_CONSOLE);
  358. printf(ANSI_CURSOR_POSITION, 1, 1);
  359. }
  360. if (title && command) {
  361. debug("Starting entry '%s'\n", title);
  362. free(title);
  363. run_command(command, 0);
  364. free(command);
  365. }
  366. #ifdef CONFIG_POSTBOOTMENU
  367. run_command(CONFIG_POSTBOOTMENU, 0);
  368. #endif
  369. }
  370. void menu_display_statusline(struct menu *m)
  371. {
  372. struct bootmenu_entry *entry;
  373. struct bootmenu_data *menu;
  374. if (menu_default_choice(m, (void *)&entry) < 0)
  375. return;
  376. menu = entry->menu;
  377. printf(ANSI_CURSOR_POSITION, 1, 1);
  378. puts(ANSI_CLEAR_LINE);
  379. printf(ANSI_CURSOR_POSITION, 2, 1);
  380. puts(" *** U-Boot Boot Menu ***");
  381. puts(ANSI_CLEAR_LINE_TO_END);
  382. printf(ANSI_CURSOR_POSITION, 3, 1);
  383. puts(ANSI_CLEAR_LINE);
  384. /* First 3 lines are bootmenu header + 2 empty lines between entries */
  385. printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
  386. puts(ANSI_CLEAR_LINE);
  387. printf(ANSI_CURSOR_POSITION, menu->count + 6, 1);
  388. puts(" Press UP/DOWN to move, ENTER to select");
  389. puts(ANSI_CLEAR_LINE_TO_END);
  390. printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
  391. puts(ANSI_CLEAR_LINE);
  392. }
  393. #ifdef CONFIG_AUTOBOOT_MENU_SHOW
  394. int menu_show(int bootdelay)
  395. {
  396. bootmenu_show(bootdelay);
  397. return -1; /* -1 - abort boot and run monitor code */
  398. }
  399. #endif
  400. int do_bootmenu(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  401. {
  402. char *delay_str = NULL;
  403. int delay = 10;
  404. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  405. delay = CONFIG_BOOTDELAY;
  406. #endif
  407. if (argc >= 2)
  408. delay_str = argv[1];
  409. if (!delay_str)
  410. delay_str = env_get("bootmenu_delay");
  411. if (delay_str)
  412. delay = (int)simple_strtol(delay_str, NULL, 10);
  413. bootmenu_show(delay);
  414. return 0;
  415. }
  416. U_BOOT_CMD(
  417. bootmenu, 2, 1, do_bootmenu,
  418. "ANSI terminal bootmenu",
  419. "[delay]\n"
  420. " - show ANSI terminal bootmenu with autoboot delay"
  421. );