blob: 8c04ecbdc83b6bffde06287fca19a31a93d644ac [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clark9975fe92017-09-13 18:05:38 -04002/*
Heinrich Schuchardte1fec152018-10-18 21:51:38 +02003 * EFI boot manager
Rob Clark9975fe92017-09-13 18:05:38 -04004 *
5 * Copyright (c) 2017 Rob Clark
Rob Clark9975fe92017-09-13 18:05:38 -04006 */
7
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +02008#define LOG_CATEGORY LOGC_EFI
9
Rob Clark9975fe92017-09-13 18:05:38 -040010#include <common.h>
11#include <charset.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Rob Clark9975fe92017-09-13 18:05:38 -040013#include <malloc.h>
14#include <efi_loader.h>
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +020015#include <efi_variable.h>
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090016#include <asm/unaligned.h>
Rob Clark9975fe92017-09-13 18:05:38 -040017
18static const struct efi_boot_services *bs;
19static const struct efi_runtime_services *rs;
20
Rob Clark9975fe92017-09-13 18:05:38 -040021/*
22 * bootmgr implements the logic of trying to find a payload to boot
23 * based on the BootOrder + BootXXXX variables, and then loading it.
24 *
25 * TODO detecting a special key held (f9?) and displaying a boot menu
26 * like you would get on a PC would be clever.
27 *
28 * TODO if we had a way to write and persist variables after the OS
29 * has started, we'd also want to check OsIndications to see if we
30 * should do normal or recovery boot.
31 */
32
Heinrich Schuchardt1064d042020-08-07 17:47:13 +020033/**
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020034 * try_load_entry() - try to load image for boot option
35 *
Rob Clark9975fe92017-09-13 18:05:38 -040036 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020037 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clark9975fe92017-09-13 18:05:38 -040038 * and that the specified file to boot exists.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020039 *
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +020040 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
41 * @handle: on return handle for the newly installed image
42 * @load_options: load options set on the loaded image protocol
43 * Return: status code
Rob Clark9975fe92017-09-13 18:05:38 -040044 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +020045static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
46 void **load_options)
Rob Clark9975fe92017-09-13 18:05:38 -040047{
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090048 struct efi_load_option lo;
Simon Glass156ccbc2022-01-23 12:55:12 -070049 u16 varname[] = u"Boot0000";
50 u16 hexmap[] = u"0123456789ABCDEF";
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090051 void *load_option;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +020052 efi_uintn_t size;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090053 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -040054
55 varname[4] = hexmap[(n & 0xf000) >> 12];
56 varname[5] = hexmap[(n & 0x0f00) >> 8];
57 varname[6] = hexmap[(n & 0x00f0) >> 4];
58 varname[7] = hexmap[(n & 0x000f) >> 0];
59
Ilias Apalodimasf4dc1bc2021-03-27 10:56:07 +020060 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
Rob Clark9975fe92017-09-13 18:05:38 -040061 if (!load_option)
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090062 return EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -040063
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +020064 ret = efi_deserialize_load_option(&lo, load_option, &size);
65 if (ret != EFI_SUCCESS) {
66 log_warning("Invalid load option for %ls\n", varname);
67 goto error;
68 }
Rob Clark9975fe92017-09-13 18:05:38 -040069
70 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiro37279ad2019-03-20 09:07:55 +090071 u32 attributes;
Rob Clark9975fe92017-09-13 18:05:38 -040072
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +020073 log_debug("%s: trying to load \"%ls\" from %pD\n",
74 __func__, lo.label, lo.file_path);
Rob Clark9975fe92017-09-13 18:05:38 -040075
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090076 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
77 NULL, 0, handle));
AKASHI Takahiro94e6e552019-05-29 20:54:25 +020078 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +020079 log_warning("Loading %ls '%ls' failed\n",
80 varname, lo.label);
Rob Clark9975fe92017-09-13 18:05:38 -040081 goto error;
AKASHI Takahiro94e6e552019-05-29 20:54:25 +020082 }
Rob Clark9975fe92017-09-13 18:05:38 -040083
AKASHI Takahiro37279ad2019-03-20 09:07:55 +090084 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
85 EFI_VARIABLE_RUNTIME_ACCESS;
Simon Glass156ccbc2022-01-23 12:55:12 -070086 ret = efi_set_variable_int(u"BootCurrent",
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +020087 &efi_global_variable_guid,
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +020088 attributes, sizeof(n), &n, false);
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +020089 if (ret != EFI_SUCCESS)
90 goto unload;
91 /* try to register load file2 for initrd's */
92 if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) {
93 ret = efi_initrd_register();
94 if (ret != EFI_SUCCESS)
95 goto unload;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090096 }
AKASHI Takahiro37279ad2019-03-20 09:07:55 +090097
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +020098 log_info("Booting: %ls\n", lo.label);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090099 } else {
100 ret = EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400101 }
102
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200103 /* Set load options */
104 if (size) {
105 *load_options = malloc(size);
106 if (!*load_options) {
107 ret = EFI_OUT_OF_RESOURCES;
108 goto error;
109 }
110 memcpy(*load_options, lo.optional_data, size);
111 ret = efi_set_load_options(*handle, size, *load_options);
112 } else {
Heinrich Schuchardte4343112020-12-27 15:46:00 +0100113 *load_options = NULL;
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200114 }
115
Rob Clark9975fe92017-09-13 18:05:38 -0400116error:
117 free(load_option);
118
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900119 return ret;
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200120
121unload:
122 if (EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
123 log_err("Unloading image failed\n");
124 free(load_option);
125
126 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400127}
128
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200129/**
130 * efi_bootmgr_load() - try to load from BootNext or BootOrder
131 *
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900132 * Attempt to load from BootNext or in the order specified by BootOrder
133 * EFI variable, the available load-options, finding and returning
134 * the first one that can be loaded successfully.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200135 *
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200136 * @handle: on return handle for the newly installed image
137 * @load_options: load options set on the loaded image protocol
138 * Return: status code
Rob Clark9975fe92017-09-13 18:05:38 -0400139 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200140efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clark9975fe92017-09-13 18:05:38 -0400141{
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900142 u16 bootnext, *bootorder;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200143 efi_uintn_t size;
Rob Clark9975fe92017-09-13 18:05:38 -0400144 int i, num;
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900145 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400146
Rob Clark9975fe92017-09-13 18:05:38 -0400147 bs = systab.boottime;
148 rs = systab.runtime;
149
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900150 /* BootNext */
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900151 size = sizeof(bootnext);
Simon Glass156ccbc2022-01-23 12:55:12 -0700152 ret = efi_get_variable_int(u"BootNext",
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200153 &efi_global_variable_guid,
154 NULL, &size, &bootnext, NULL);
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900155 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
156 /* BootNext does exist here */
157 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200158 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900159
160 /* delete BootNext */
Simon Glass156ccbc2022-01-23 12:55:12 -0700161 ret = efi_set_variable_int(u"BootNext",
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200162 &efi_global_variable_guid,
163 0, 0, NULL, false);
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900164
165 /* load BootNext */
166 if (ret == EFI_SUCCESS) {
167 if (size == sizeof(u16)) {
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200168 ret = try_load_entry(bootnext, handle,
169 load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900170 if (ret == EFI_SUCCESS)
171 return ret;
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200172 log_warning(
173 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900174 }
175 } else {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200176 log_err("Deleting BootNext failed\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900177 }
178 }
179
180 /* BootOrder */
Simon Glass156ccbc2022-01-23 12:55:12 -0700181 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100182 if (!bootorder) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200183 log_info("BootOrder not defined\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900184 ret = EFI_NOT_FOUND;
Rob Clark9975fe92017-09-13 18:05:38 -0400185 goto error;
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100186 }
Rob Clark9975fe92017-09-13 18:05:38 -0400187
188 num = size / sizeof(uint16_t);
189 for (i = 0; i < num; i++) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200190 log_debug("%s trying to load Boot%04X\n", __func__,
191 bootorder[i]);
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200192 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900193 if (ret == EFI_SUCCESS)
Rob Clark9975fe92017-09-13 18:05:38 -0400194 break;
195 }
196
197 free(bootorder);
198
199error:
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900200 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400201}