bootmenu.c 9.6 KB

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