tpm: core: Set timeouts before requesting locality
[platform/kernel/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 <charset.h>
7 #include <common.h>
8 #include <command.h>
9 #include <ansi.h>
10 #include <efi_loader.h>
11 #include <efi_variable.h>
12 #include <env.h>
13 #include <log.h>
14 #include <menu.h>
15 #include <watchdog.h>
16 #include <malloc.h>
17 #include <linux/delay.h>
18 #include <linux/string.h>
19
20 /* maximum bootmenu entries */
21 #define MAX_COUNT       99
22
23 /* maximal size of bootmenu env
24  *  9 = strlen("bootmenu_")
25  *  2 = strlen(MAX_COUNT)
26  *  1 = NULL term
27  */
28 #define MAX_ENV_SIZE    (9 + 2 + 1)
29
30 enum bootmenu_ret {
31         BOOTMENU_RET_SUCCESS = 0,
32         BOOTMENU_RET_FAIL,
33         BOOTMENU_RET_QUIT,
34         BOOTMENU_RET_UPDATED,
35 };
36
37 enum boot_type {
38         BOOTMENU_TYPE_NONE = 0,
39         BOOTMENU_TYPE_BOOTMENU,
40         BOOTMENU_TYPE_UEFI_BOOT_OPTION,
41 };
42
43 struct bootmenu_entry {
44         unsigned short int num;         /* unique number 0 .. MAX_COUNT */
45         char key[3];                    /* key identifier of number */
46         u16 *title;                     /* title of entry */
47         char *command;                  /* hush command of entry */
48         enum boot_type type;            /* boot type of entry */
49         u16 bootorder;                  /* order for each boot type */
50         struct bootmenu_data *menu;     /* this bootmenu */
51         struct bootmenu_entry *next;    /* next menu entry (num+1) */
52 };
53
54 static char *bootmenu_getoption(unsigned short int n)
55 {
56         char name[MAX_ENV_SIZE];
57
58         if (n > MAX_COUNT)
59                 return NULL;
60
61         sprintf(name, "bootmenu_%d", n);
62         return env_get(name);
63 }
64
65 static void bootmenu_print_entry(void *data)
66 {
67         struct bootmenu_entry *entry = data;
68         int reverse = (entry->menu->active == entry->num);
69
70         /*
71          * Move cursor to line where the entry will be drown (entry->num)
72          * First 3 lines contain bootmenu header + 1 empty line
73          */
74         printf(ANSI_CURSOR_POSITION, entry->num + 4, 7);
75
76         if (reverse)
77                 puts(ANSI_COLOR_REVERSE);
78
79         printf("%ls", entry->title);
80
81         if (reverse)
82                 puts(ANSI_COLOR_RESET);
83 }
84
85 static char *bootmenu_choice_entry(void *data)
86 {
87         struct bootmenu_data *menu = data;
88         struct bootmenu_entry *iter;
89         enum bootmenu_key key = KEY_NONE;
90         int esc = 0;
91         int i;
92
93         while (1) {
94                 if (menu->delay >= 0) {
95                         /* Autoboot was not stopped */
96                         bootmenu_autoboot_loop(menu, &key, &esc);
97                 } else {
98                         /* Some key was pressed, so autoboot was stopped */
99                         bootmenu_loop(menu, &key, &esc);
100                 }
101
102                 switch (key) {
103                 case KEY_UP:
104                         if (menu->active > 0)
105                                 --menu->active;
106                         /* no menu key selected, regenerate menu */
107                         return NULL;
108                 case KEY_DOWN:
109                         if (menu->active < menu->count - 1)
110                                 ++menu->active;
111                         /* no menu key selected, regenerate menu */
112                         return NULL;
113                 case KEY_SELECT:
114                         iter = menu->first;
115                         for (i = 0; i < menu->active; ++i)
116                                 iter = iter->next;
117                         return iter->key;
118                 case KEY_QUIT:
119                         /* Quit by choosing the last entry - U-Boot console */
120                         iter = menu->first;
121                         while (iter->next)
122                                 iter = iter->next;
123                         return iter->key;
124                 default:
125                         break;
126                 }
127         }
128
129         /* never happens */
130         debug("bootmenu: this should not happen");
131         return NULL;
132 }
133
134 static void bootmenu_destroy(struct bootmenu_data *menu)
135 {
136         struct bootmenu_entry *iter = menu->first;
137         struct bootmenu_entry *next;
138
139         while (iter) {
140                 next = iter->next;
141                 free(iter->title);
142                 free(iter->command);
143                 free(iter);
144                 iter = next;
145         }
146         free(menu);
147 }
148
149 /**
150  * prepare_bootmenu_entry() - generate the bootmenu_xx entries
151  *
152  * This function read the "bootmenu_x" U-Boot environment variable
153  * and generate the bootmenu entries.
154  *
155  * @menu:       pointer to the bootmenu structure
156  * @current:    pointer to the last bootmenu entry list
157  * @index:      pointer to the index of the last bootmenu entry,
158  *              the number of bootmenu entry is added by this function
159  * Return:      1 on success, negative value on error
160  */
161 static int prepare_bootmenu_entry(struct bootmenu_data *menu,
162                                   struct bootmenu_entry **current,
163                                   unsigned short int *index)
164 {
165         int len;
166         char *sep;
167         const char *option;
168         unsigned short int i = *index;
169         struct bootmenu_entry *entry = NULL;
170         struct bootmenu_entry *iter = *current;
171
172         while ((option = bootmenu_getoption(i))) {
173                 u16 *buf;
174
175                 sep = strchr(option, '=');
176                 if (!sep) {
177                         printf("Invalid bootmenu entry: %s\n", option);
178                         break;
179                 }
180
181                 entry = malloc(sizeof(struct bootmenu_entry));
182                 if (!entry)
183                         return -ENOMEM;
184
185                 len = sep-option;
186                 buf = calloc(1, (len + 1) * sizeof(u16));
187                 entry->title = buf;
188                 if (!entry->title) {
189                         free(entry);
190                         return -ENOMEM;
191                 }
192                 utf8_utf16_strncpy(&buf, option, len);
193
194                 len = strlen(sep + 1);
195                 entry->command = malloc(len + 1);
196                 if (!entry->command) {
197                         free(entry->title);
198                         free(entry);
199                         return -ENOMEM;
200                 }
201                 memcpy(entry->command, sep + 1, len);
202                 entry->command[len] = 0;
203
204                 sprintf(entry->key, "%d", i);
205
206                 entry->num = i;
207                 entry->menu = menu;
208                 entry->type = BOOTMENU_TYPE_BOOTMENU;
209                 entry->bootorder = i;
210                 entry->next = NULL;
211
212                 if (!iter)
213                         menu->first = entry;
214                 else
215                         iter->next = entry;
216
217                 iter = entry;
218                 ++i;
219
220                 if (i == MAX_COUNT - 1)
221                         break;
222         }
223
224         *index = i;
225         *current = iter;
226
227         return 1;
228 }
229
230 /**
231  * prepare_uefi_bootorder_entry() - generate the uefi bootmenu entries
232  *
233  * This function read the "BootOrder" UEFI variable
234  * and generate the bootmenu entries in the order of "BootOrder".
235  *
236  * @menu:       pointer to the bootmenu structure
237  * @current:    pointer to the last bootmenu entry list
238  * @index:      pointer to the index of the last bootmenu entry,
239  *              the number of uefi entry is added by this function
240  * Return:      1 on success, negative value on error
241  */
242 static int prepare_uefi_bootorder_entry(struct bootmenu_data *menu,
243                                         struct bootmenu_entry **current,
244                                         unsigned short int *index)
245 {
246         u16 *bootorder;
247         efi_status_t ret;
248         unsigned short j;
249         efi_uintn_t num, size;
250         void *load_option;
251         struct efi_load_option lo;
252         u16 varname[] = u"Boot####";
253         unsigned short int i = *index;
254         struct bootmenu_entry *entry = NULL;
255         struct bootmenu_entry *iter = *current;
256
257         bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
258         if (!bootorder)
259                 return -ENOENT;
260
261         num = size / sizeof(u16);
262         for (j = 0; j < num; j++) {
263                 entry = malloc(sizeof(struct bootmenu_entry));
264                 if (!entry)
265                         return -ENOMEM;
266
267                 efi_create_indexed_name(varname, sizeof(varname),
268                                         "Boot", bootorder[j]);
269                 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
270                 if (!load_option)
271                         continue;
272
273                 ret = efi_deserialize_load_option(&lo, load_option, &size);
274                 if (ret != EFI_SUCCESS) {
275                         log_warning("Invalid load option for %ls\n", varname);
276                         free(load_option);
277                         free(entry);
278                         continue;
279                 }
280
281                 if (lo.attributes & LOAD_OPTION_ACTIVE) {
282                         entry->title = u16_strdup(lo.label);
283                         if (!entry->title) {
284                                 free(load_option);
285                                 free(entry);
286                                 free(bootorder);
287                                 return -ENOMEM;
288                         }
289                         entry->command = strdup("bootefi bootmgr");
290                         sprintf(entry->key, "%d", i);
291                         entry->num = i;
292                         entry->menu = menu;
293                         entry->type = BOOTMENU_TYPE_UEFI_BOOT_OPTION;
294                         entry->bootorder = bootorder[j];
295                         entry->next = NULL;
296
297                         if (!iter)
298                                 menu->first = entry;
299                         else
300                                 iter->next = entry;
301
302                         iter = entry;
303                         i++;
304                 }
305
306                 free(load_option);
307
308                 if (i == MAX_COUNT - 1)
309                         break;
310         }
311
312         free(bootorder);
313         *index = i;
314         *current = iter;
315
316         return 1;
317 }
318
319 static struct bootmenu_data *bootmenu_create(int delay)
320 {
321         int ret;
322         unsigned short int i = 0;
323         struct bootmenu_data *menu;
324         struct bootmenu_entry *iter = NULL;
325         struct bootmenu_entry *entry;
326         char *default_str;
327
328         menu = malloc(sizeof(struct bootmenu_data));
329         if (!menu)
330                 return NULL;
331
332         menu->delay = delay;
333         menu->active = 0;
334         menu->first = NULL;
335
336         default_str = env_get("bootmenu_default");
337         if (default_str)
338                 menu->active = (int)simple_strtol(default_str, NULL, 10);
339
340         ret = prepare_bootmenu_entry(menu, &iter, &i);
341         if (ret < 0)
342                 goto cleanup;
343
344         if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
345                 if (i < MAX_COUNT - 1) {
346                         ret = prepare_uefi_bootorder_entry(menu, &iter, &i);
347                         if (ret < 0 && ret != -ENOENT)
348                                 goto cleanup;
349                 }
350         }
351
352         /* Add U-Boot console entry at the end */
353         if (i <= MAX_COUNT - 1) {
354                 entry = malloc(sizeof(struct bootmenu_entry));
355                 if (!entry)
356                         goto cleanup;
357
358                 /* Add Quit entry if entering U-Boot console is disabled */
359                 if (IS_ENABLED(CONFIG_CMD_BOOTMENU_ENTER_UBOOT_CONSOLE))
360                         entry->title = u16_strdup(u"U-Boot console");
361                 else
362                         entry->title = u16_strdup(u"Quit");
363
364                 if (!entry->title) {
365                         free(entry);
366                         goto cleanup;
367                 }
368
369                 entry->command = strdup("");
370                 if (!entry->command) {
371                         free(entry->title);
372                         free(entry);
373                         goto cleanup;
374                 }
375
376                 sprintf(entry->key, "%d", i);
377
378                 entry->num = i;
379                 entry->menu = menu;
380                 entry->type = BOOTMENU_TYPE_NONE;
381                 entry->next = NULL;
382
383                 if (!iter)
384                         menu->first = entry;
385                 else
386                         iter->next = entry;
387
388                 iter = entry;
389                 ++i;
390         }
391
392         menu->count = i;
393
394         if ((menu->active >= menu->count)||(menu->active < 0)) { //ensure active menuitem is inside menu
395                 printf("active menuitem (%d) is outside menu (0..%d)\n",menu->active,menu->count-1);
396                 menu->active=0;
397         }
398
399         return menu;
400
401 cleanup:
402         bootmenu_destroy(menu);
403         return NULL;
404 }
405
406 static void menu_display_statusline(struct menu *m)
407 {
408         struct bootmenu_entry *entry;
409         struct bootmenu_data *menu;
410
411         if (menu_default_choice(m, (void *)&entry) < 0)
412                 return;
413
414         menu = entry->menu;
415
416         printf(ANSI_CURSOR_POSITION, 1, 1);
417         puts(ANSI_CLEAR_LINE);
418         printf(ANSI_CURSOR_POSITION, 2, 3);
419         puts("*** U-Boot Boot Menu ***");
420         puts(ANSI_CLEAR_LINE_TO_END);
421         printf(ANSI_CURSOR_POSITION, 3, 1);
422         puts(ANSI_CLEAR_LINE);
423
424         /* First 3 lines are bootmenu header + 2 empty lines between entries */
425         printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
426         puts(ANSI_CLEAR_LINE);
427         printf(ANSI_CURSOR_POSITION, menu->count + 6, 3);
428         puts("Press UP/DOWN to move, ENTER to select, ESC/CTRL+C to quit");
429         puts(ANSI_CLEAR_LINE_TO_END);
430         printf(ANSI_CURSOR_POSITION, menu->count + 7, 1);
431         puts(ANSI_CLEAR_LINE);
432 }
433
434 static void handle_uefi_bootnext(void)
435 {
436         u16 bootnext;
437         efi_status_t ret;
438         efi_uintn_t size;
439
440         /* Initialize EFI drivers */
441         ret = efi_init_obj_list();
442         if (ret != EFI_SUCCESS) {
443                 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
444                         ret & ~EFI_ERROR_MASK);
445
446                 return;
447         }
448
449         /* If UEFI BootNext variable is set, boot the BootNext load option */
450         size = sizeof(u16);
451         ret = efi_get_variable_int(u"BootNext",
452                                    &efi_global_variable_guid,
453                                    NULL, &size, &bootnext, NULL);
454         if (ret == EFI_SUCCESS)
455                 /* BootNext does exist here, try to boot */
456                 run_command("bootefi bootmgr", 0);
457 }
458
459 static enum bootmenu_ret bootmenu_show(int delay)
460 {
461         int cmd_ret;
462         int init = 0;
463         void *choice = NULL;
464         u16 *title = NULL;
465         char *command = NULL;
466         struct menu *menu;
467         struct bootmenu_entry *iter;
468         int ret = BOOTMENU_RET_SUCCESS;
469         struct bootmenu_data *bootmenu;
470         efi_status_t efi_ret = EFI_SUCCESS;
471         char *option, *sep;
472
473         if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
474                 handle_uefi_bootnext();
475
476         /* If delay is 0 do not create menu, just run first entry */
477         if (delay == 0) {
478                 option = bootmenu_getoption(0);
479                 if (!option) {
480                         puts("bootmenu option 0 was not found\n");
481                         return BOOTMENU_RET_FAIL;
482                 }
483                 sep = strchr(option, '=');
484                 if (!sep) {
485                         puts("bootmenu option 0 is invalid\n");
486                         return BOOTMENU_RET_FAIL;
487                 }
488                 cmd_ret = run_command(sep + 1, 0);
489                 return (cmd_ret == CMD_RET_SUCCESS ? BOOTMENU_RET_SUCCESS : BOOTMENU_RET_FAIL);
490         }
491
492         bootmenu = bootmenu_create(delay);
493         if (!bootmenu)
494                 return BOOTMENU_RET_FAIL;
495
496         menu = menu_create(NULL, bootmenu->delay, 1, menu_display_statusline,
497                            bootmenu_print_entry, bootmenu_choice_entry,
498                            bootmenu);
499         if (!menu) {
500                 bootmenu_destroy(bootmenu);
501                 return BOOTMENU_RET_FAIL;
502         }
503
504         for (iter = bootmenu->first; iter; iter = iter->next) {
505                 if (menu_item_add(menu, iter->key, iter) != 1)
506                         goto cleanup;
507         }
508
509         /* Default menu entry is always first */
510         menu_default_set(menu, "0");
511
512         puts(ANSI_CURSOR_HIDE);
513         puts(ANSI_CLEAR_CONSOLE);
514         printf(ANSI_CURSOR_POSITION, 1, 1);
515
516         init = 1;
517
518         if (menu_get_choice(menu, &choice) == 1) {
519                 iter = choice;
520                 title = u16_strdup(iter->title);
521                 command = strdup(iter->command);
522
523                 /* last entry is U-Boot console or Quit */
524                 if (iter->num == iter->menu->count - 1) {
525                         ret = BOOTMENU_RET_QUIT;
526                         goto cleanup;
527                 }
528         } else {
529                 goto cleanup;
530         }
531
532         /*
533          * If the selected entry is UEFI BOOT####, set the BootNext variable.
534          * Then uefi bootmgr is invoked by the preset command in iter->command.
535          */
536         if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
537                 if (iter->type == BOOTMENU_TYPE_UEFI_BOOT_OPTION) {
538                         /*
539                          * UEFI specification requires BootNext variable needs non-volatile
540                          * attribute, but this BootNext is only used inside of U-Boot and
541                          * removed by efi bootmgr once BootNext is processed.
542                          * So this BootNext can be volatile.
543                          */
544                         efi_ret = efi_set_variable_int(u"BootNext", &efi_global_variable_guid,
545                                                        EFI_VARIABLE_BOOTSERVICE_ACCESS |
546                                                        EFI_VARIABLE_RUNTIME_ACCESS,
547                                                        sizeof(u16), &iter->bootorder, false);
548                         if (efi_ret != EFI_SUCCESS)
549                                 goto cleanup;
550                 }
551         }
552
553 cleanup:
554         menu_destroy(menu);
555         bootmenu_destroy(bootmenu);
556
557         if (init) {
558                 puts(ANSI_CURSOR_SHOW);
559                 puts(ANSI_CLEAR_CONSOLE);
560                 printf(ANSI_CURSOR_POSITION, 1, 1);
561         }
562
563         if (title && command) {
564                 debug("Starting entry '%ls'\n", title);
565                 free(title);
566                 if (efi_ret == EFI_SUCCESS)
567                         cmd_ret = run_command(command, 0);
568                 free(command);
569         }
570
571 #ifdef CONFIG_POSTBOOTMENU
572         run_command(CONFIG_POSTBOOTMENU, 0);
573 #endif
574
575         if (efi_ret != EFI_SUCCESS || cmd_ret != CMD_RET_SUCCESS)
576                 ret = BOOTMENU_RET_FAIL;
577
578         return ret;
579 }
580
581 #ifdef CONFIG_AUTOBOOT_MENU_SHOW
582 int menu_show(int bootdelay)
583 {
584         int ret;
585
586         while (1) {
587                 ret = bootmenu_show(bootdelay);
588                 bootdelay = -1;
589                 if (ret == BOOTMENU_RET_UPDATED)
590                         continue;
591
592                 if (!IS_ENABLED(CONFIG_CMD_BOOTMENU_ENTER_UBOOT_CONSOLE)) {
593                         if (ret == BOOTMENU_RET_QUIT) {
594                                 /* default boot process */
595                                 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR))
596                                         run_command("bootefi bootmgr", 0);
597
598                                 run_command("run bootcmd", 0);
599                         }
600                 } else {
601                         break;
602                 }
603         }
604
605         return -1; /* -1 - abort boot and run monitor code */
606 }
607 #endif
608
609 int do_bootmenu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
610 {
611         char *delay_str = NULL;
612         int delay = 10;
613
614 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
615         delay = CONFIG_BOOTDELAY;
616 #endif
617
618         if (argc >= 2)
619                 delay_str = argv[1];
620
621         if (!delay_str)
622                 delay_str = env_get("bootmenu_delay");
623
624         if (delay_str)
625                 delay = (int)simple_strtol(delay_str, NULL, 10);
626
627         bootmenu_show(delay);
628         return 0;
629 }
630
631 U_BOOT_CMD(
632         bootmenu, 2, 1, do_bootmenu,
633         "ANSI terminal bootmenu",
634         "[delay]\n"
635         "    - show ANSI terminal bootmenu with autoboot delay"
636 );