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