blob: f1562883f5217c9f43750fc36a3add2f71ec5769 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Pali Roháre7abe912013-03-23 14:53:08 +00002/*
3 * (C) Copyright 2011-2013 Pali Rohár <pali.rohar@gmail.com>
Pali Roháre7abe912013-03-23 14:53:08 +00004 */
5
6#include <common.h>
7#include <command.h>
8#include <ansi.h>
Simon Glass7b51b572019-08-01 09:46:52 -06009#include <env.h>
Pali Roháre7abe912013-03-23 14:53:08 +000010#include <menu.h>
Pali Roháre7abe912013-03-23 14:53:08 +000011#include <watchdog.h>
12#include <malloc.h>
13#include <linux/string.h>
14
15/* maximum bootmenu entries */
16#define MAX_COUNT 99
17
18/* maximal size of bootmenu env
19 * 9 = strlen("bootmenu_")
20 * 2 = strlen(MAX_COUNT)
21 * 1 = NULL term
22 */
23#define MAX_ENV_SIZE (9 + 2 + 1)
24
25struct bootmenu_entry {
26 unsigned short int num; /* unique number 0 .. MAX_COUNT */
27 char key[3]; /* key identifier of number */
28 char *title; /* title of entry */
29 char *command; /* hush command of entry */
30 struct bootmenu_data *menu; /* this bootmenu */
31 struct bootmenu_entry *next; /* next menu entry (num+1) */
32};
33
34struct bootmenu_data {
35 int delay; /* delay for autoboot */
36 int active; /* active menu entry */
37 int count; /* total count of menu entries */
38 struct bootmenu_entry *first; /* first menu entry */
39};
40
41enum bootmenu_key {
42 KEY_NONE = 0,
43 KEY_UP,
44 KEY_DOWN,
45 KEY_SELECT,
46};
47
48static char *bootmenu_getoption(unsigned short int n)
49{
Lan Yixun (dlan)0eb33ad2013-06-27 18:58:53 +080050 char name[MAX_ENV_SIZE];
Pali Roháre7abe912013-03-23 14:53:08 +000051
52 if (n > MAX_COUNT)
53 return NULL;
54
Lan Yixun (dlan)0eb33ad2013-06-27 18:58:53 +080055 sprintf(name, "bootmenu_%d", n);
Simon Glass00caae62017-08-03 12:22:12 -060056 return env_get(name);
Pali Roháre7abe912013-03-23 14:53:08 +000057}
58
59static void bootmenu_print_entry(void *data)
60{
61 struct bootmenu_entry *entry = data;
62 int reverse = (entry->menu->active == entry->num);
63
64 /*
65 * Move cursor to line where the entry will be drown (entry->num)
66 * First 3 lines contain bootmenu header + 1 empty line
67 */
68 printf(ANSI_CURSOR_POSITION, entry->num + 4, 1);
69
70 puts(" ");
71
72 if (reverse)
73 puts(ANSI_COLOR_REVERSE);
74
75 puts(entry->title);
76
77 if (reverse)
78 puts(ANSI_COLOR_RESET);
79}
80
81static void bootmenu_autoboot_loop(struct bootmenu_data *menu,
82 enum bootmenu_key *key, int *esc)
83{
84 int i, c;
85
86 if (menu->delay > 0) {
87 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
88 printf(" Hit any key to stop autoboot: %2d ", menu->delay);
89 }
90
91 while (menu->delay > 0) {
92 for (i = 0; i < 100; ++i) {
93 if (!tstc()) {
94 WATCHDOG_RESET();
95 mdelay(10);
96 continue;
97 }
98
99 menu->delay = -1;
100 c = getc();
101
102 switch (c) {
103 case '\e':
104 *esc = 1;
105 *key = KEY_NONE;
106 break;
107 case '\r':
108 *key = KEY_SELECT;
109 break;
110 default:
111 *key = KEY_NONE;
112 break;
113 }
114
115 break;
116 }
117
118 if (menu->delay < 0)
119 break;
120
121 --menu->delay;
122 printf("\b\b\b%2d ", menu->delay);
123 }
124
125 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
126 puts(ANSI_CLEAR_LINE);
127
128 if (menu->delay == 0)
129 *key = KEY_SELECT;
130}
131
132static void bootmenu_loop(struct bootmenu_data *menu,
133 enum bootmenu_key *key, int *esc)
134{
135 int c;
136
137 while (!tstc()) {
138 WATCHDOG_RESET();
139 mdelay(10);
140 }
141
142 c = getc();
143
144 switch (*esc) {
145 case 0:
146 /* First char of ANSI escape sequence '\e' */
147 if (c == '\e') {
148 *esc = 1;
149 *key = KEY_NONE;
150 }
151 break;
152 case 1:
153 /* Second char of ANSI '[' */
154 if (c == '[') {
155 *esc = 2;
156 *key = KEY_NONE;
157 } else {
158 *esc = 0;
159 }
160 break;
161 case 2:
162 case 3:
163 /* Third char of ANSI (number '1') - optional */
164 if (*esc == 2 && c == '1') {
165 *esc = 3;
166 *key = KEY_NONE;
167 break;
168 }
169
170 *esc = 0;
171
172 /* ANSI 'A' - key up was pressed */
173 if (c == 'A')
174 *key = KEY_UP;
175 /* ANSI 'B' - key down was pressed */
176 else if (c == 'B')
177 *key = KEY_DOWN;
178 /* other key was pressed */
179 else
180 *key = KEY_NONE;
181
182 break;
183 }
184
185 /* enter key was pressed */
186 if (c == '\r')
187 *key = KEY_SELECT;
188}
189
190static char *bootmenu_choice_entry(void *data)
191{
192 struct bootmenu_data *menu = data;
193 struct bootmenu_entry *iter;
194 enum bootmenu_key key = KEY_NONE;
195 int esc = 0;
196 int i;
197
198 while (1) {
199 if (menu->delay >= 0) {
200 /* Autoboot was not stopped */
201 bootmenu_autoboot_loop(menu, &key, &esc);
202 } else {
203 /* Some key was pressed, so autoboot was stopped */
204 bootmenu_loop(menu, &key, &esc);
205 }
206
207 switch (key) {
208 case KEY_UP:
209 if (menu->active > 0)
210 --menu->active;
211 /* no menu key selected, regenerate menu */
212 return NULL;
213 case KEY_DOWN:
214 if (menu->active < menu->count - 1)
215 ++menu->active;
216 /* no menu key selected, regenerate menu */
217 return NULL;
218 case KEY_SELECT:
219 iter = menu->first;
220 for (i = 0; i < menu->active; ++i)
221 iter = iter->next;
222 return iter->key;
223 default:
224 break;
225 }
226 }
227
228 /* never happens */
229 debug("bootmenu: this should not happen");
230 return NULL;
231}
232
233static void bootmenu_destroy(struct bootmenu_data *menu)
234{
235 struct bootmenu_entry *iter = menu->first;
236 struct bootmenu_entry *next;
237
238 while (iter) {
239 next = iter->next;
240 free(iter->title);
241 free(iter->command);
242 free(iter);
243 iter = next;
244 }
245 free(menu);
246}
247
248static struct bootmenu_data *bootmenu_create(int delay)
249{
250 unsigned short int i = 0;
251 const char *option;
252 struct bootmenu_data *menu;
253 struct bootmenu_entry *iter = NULL;
254
255 int len;
256 char *sep;
Frank Wunderlichf7bb20a2018-10-05 11:41:59 +0200257 char *default_str;
Pali Roháre7abe912013-03-23 14:53:08 +0000258 struct bootmenu_entry *entry;
259
260 menu = malloc(sizeof(struct bootmenu_data));
261 if (!menu)
262 return NULL;
263
264 menu->delay = delay;
265 menu->active = 0;
266 menu->first = NULL;
267
Frank Wunderlichf7bb20a2018-10-05 11:41:59 +0200268 default_str = env_get("bootmenu_default");
269 if (default_str)
270 menu->active = (int)simple_strtol(default_str, NULL, 10);
271
Pali Roháre7abe912013-03-23 14:53:08 +0000272 while ((option = bootmenu_getoption(i))) {
273 sep = strchr(option, '=');
274 if (!sep) {
275 printf("Invalid bootmenu entry: %s\n", option);
276 break;
277 }
278
279 entry = malloc(sizeof(struct bootmenu_entry));
280 if (!entry)
281 goto cleanup;
282
283 len = sep-option;
284 entry->title = malloc(len + 1);
285 if (!entry->title) {
286 free(entry);
287 goto cleanup;
288 }
289 memcpy(entry->title, option, len);
290 entry->title[len] = 0;
291
292 len = strlen(sep + 1);
293 entry->command = malloc(len + 1);
294 if (!entry->command) {
295 free(entry->title);
296 free(entry);
297 goto cleanup;
298 }
299 memcpy(entry->command, sep + 1, len);
300 entry->command[len] = 0;
301
302 sprintf(entry->key, "%d", i);
303
304 entry->num = i;
305 entry->menu = menu;
306 entry->next = NULL;
307
308 if (!iter)
309 menu->first = entry;
310 else
311 iter->next = entry;
312
313 iter = entry;
314 ++i;
315
316 if (i == MAX_COUNT - 1)
317 break;
318 }
319
320 /* Add U-Boot console entry at the end */
321 if (i <= MAX_COUNT - 1) {
322 entry = malloc(sizeof(struct bootmenu_entry));
323 if (!entry)
324 goto cleanup;
325
326 entry->title = strdup("U-Boot console");
327 if (!entry->title) {
328 free(entry);
329 goto cleanup;
330 }
331
332 entry->command = strdup("");
333 if (!entry->command) {
334 free(entry->title);
335 free(entry);
336 goto cleanup;
337 }
338
339 sprintf(entry->key, "%d", i);
340
341 entry->num = i;
342 entry->menu = menu;
343 entry->next = NULL;
344
345 if (!iter)
346 menu->first = entry;
347 else
348 iter->next = entry;
349
350 iter = entry;
351 ++i;
352 }
353
354 menu->count = i;
Frank Wunderlichbace2212018-12-03 11:23:41 +0100355
356 if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
357 printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
358 menu->active=0;
359 }
360
Pali Roháre7abe912013-03-23 14:53:08 +0000361 return menu;
362
363cleanup:
364 bootmenu_destroy(menu);
365 return NULL;
366}
367
Thirupathaiah Annapureddy5168d7a2020-03-18 11:38:42 -0700368static void menu_display_statusline(struct menu *m)
369{
370 struct bootmenu_entry *entry;
371 struct bootmenu_data *menu;
372
373 if (menu_default_choice(m, (void *)&entry) < 0)
374 return;
375
376 menu = entry->menu;
377
378 printf(ANSI_CURSOR_POSITION, 1, 1);
379 puts(ANSI_CLEAR_LINE);
380 printf(ANSI_CURSOR_POSITION, 2, 1);
381 puts(" *** U-Boot Boot Menu ***");
382 puts(ANSI_CLEAR_LINE_TO_END);
383 printf(ANSI_CURSOR_POSITION, 3, 1);
384 puts(ANSI_CLEAR_LINE);
385
386 /* First 3 lines are bootmenu header + 2 empty lines between entries */
387 printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
388 puts(ANSI_CLEAR_LINE);
389 printf(ANSI_CURSOR_POSITION, menu->count + 6, 1);
390 puts(" Press UP/DOWN to move, ENTER to select");
391 puts(ANSI_CLEAR_LINE_TO_END);
392 printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
393 puts(ANSI_CLEAR_LINE);
394}
395
Pali Roháre7abe912013-03-23 14:53:08 +0000396static void bootmenu_show(int delay)
397{
398 int init = 0;
399 void *choice = NULL;
400 char *title = NULL;
401 char *command = NULL;
402 struct menu *menu;
403 struct bootmenu_data *bootmenu;
404 struct bootmenu_entry *iter;
405 char *option, *sep;
406
407 /* If delay is 0 do not create menu, just run first entry */
408 if (delay == 0) {
409 option = bootmenu_getoption(0);
410 if (!option) {
411 puts("bootmenu option 0 was not found\n");
412 return;
413 }
414 sep = strchr(option, '=');
415 if (!sep) {
416 puts("bootmenu option 0 is invalid\n");
417 return;
418 }
419 run_command(sep+1, 0);
420 return;
421 }
422
423 bootmenu = bootmenu_create(delay);
424 if (!bootmenu)
425 return;
426
Thirupathaiah Annapureddy5168d7a2020-03-18 11:38:42 -0700427 menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline,
428 bootmenu_print_entry, bootmenu_choice_entry,
429 bootmenu);
Pali Roháre7abe912013-03-23 14:53:08 +0000430 if (!menu) {
431 bootmenu_destroy(bootmenu);
432 return;
433 }
434
435 for (iter = bootmenu->first; iter; iter = iter->next) {
436 if (!menu_item_add(menu, iter->key, iter))
437 goto cleanup;
438 }
439
440 /* Default menu entry is always first */
441 menu_default_set(menu, "0");
442
443 puts(ANSI_CURSOR_HIDE);
444 puts(ANSI_CLEAR_CONSOLE);
445 printf(ANSI_CURSOR_POSITION, 1, 1);
446
447 init = 1;
448
449 if (menu_get_choice(menu, &choice)) {
450 iter = choice;
451 title = strdup(iter->title);
452 command = strdup(iter->command);
453 }
454
455cleanup:
456 menu_destroy(menu);
457 bootmenu_destroy(bootmenu);
458
459 if (init) {
460 puts(ANSI_CURSOR_SHOW);
461 puts(ANSI_CLEAR_CONSOLE);
462 printf(ANSI_CURSOR_POSITION, 1, 1);
463 }
464
465 if (title && command) {
466 debug("Starting entry '%s'\n", title);
467 free(title);
468 run_command(command, 0);
469 free(command);
470 }
471
472#ifdef CONFIG_POSTBOOTMENU
473 run_command(CONFIG_POSTBOOTMENU, 0);
474#endif
475}
476
Simon Glasse2313062019-07-20 20:51:24 -0600477#ifdef CONFIG_AUTOBOOT_MENU_SHOW
Pali Roháre7abe912013-03-23 14:53:08 +0000478int menu_show(int bootdelay)
479{
480 bootmenu_show(bootdelay);
481 return -1; /* -1 - abort boot and run monitor code */
482}
483#endif
484
485int do_bootmenu(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
486{
487 char *delay_str = NULL;
488 int delay = 10;
489
490#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
491 delay = CONFIG_BOOTDELAY;
492#endif
493
494 if (argc >= 2)
495 delay_str = argv[1];
496
497 if (!delay_str)
Simon Glass00caae62017-08-03 12:22:12 -0600498 delay_str = env_get("bootmenu_delay");
Pali Roháre7abe912013-03-23 14:53:08 +0000499
500 if (delay_str)
501 delay = (int)simple_strtol(delay_str, NULL, 10);
502
503 bootmenu_show(delay);
504 return 0;
505}
506
507U_BOOT_CMD(
508 bootmenu, 2, 1, do_bootmenu,
509 "ANSI terminal bootmenu",
510 "[delay]\n"
511 " - show ANSI terminal bootmenu with autoboot delay"
512);