cmd_bootmenu.c 9.3 KB

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