Tom Rini | f739fcd | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | e1fec15 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 3 | * EFI boot manager |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2017 Rob Clark |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 10 | #include <common.h> |
| 11 | #include <charset.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <efi_loader.h> |
Heinrich Schuchardt | dda8c71 | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 15 | #include <efi_variable.h> |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 16 | #include <asm/unaligned.h> |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 17 | |
| 18 | static const struct efi_boot_services *bs; |
| 19 | static const struct efi_runtime_services *rs; |
| 20 | |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 21 | /* |
| 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 Schuchardt | 1064d04 | 2020-08-07 17:47:13 +0200 | [diff] [blame] | 33 | /** |
| 34 | * efi_set_load_options() - set the load options of a loaded image |
| 35 | * |
| 36 | * @handle: the image handle |
| 37 | * @load_options_size: size of load options |
| 38 | * @load_options: pointer to load options |
| 39 | * Return: status code |
| 40 | */ |
| 41 | efi_status_t efi_set_load_options(efi_handle_t handle, |
| 42 | efi_uintn_t load_options_size, |
| 43 | void *load_options) |
| 44 | { |
| 45 | struct efi_loaded_image *loaded_image_info; |
| 46 | efi_status_t ret; |
| 47 | |
| 48 | ret = EFI_CALL(systab.boottime->open_protocol( |
| 49 | handle, |
| 50 | &efi_guid_loaded_image, |
| 51 | (void **)&loaded_image_info, |
| 52 | efi_root, NULL, |
| 53 | EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)); |
| 54 | if (ret != EFI_SUCCESS) |
| 55 | return EFI_INVALID_PARAMETER; |
| 56 | |
| 57 | loaded_image_info->load_options = load_options; |
| 58 | loaded_image_info->load_options_size = load_options_size; |
| 59 | |
| 60 | return EFI_CALL(systab.boottime->close_protocol(handle, |
| 61 | &efi_guid_loaded_image, |
| 62 | efi_root, NULL)); |
| 63 | } |
| 64 | |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 65 | |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 66 | /** |
| 67 | * efi_deserialize_load_option() - parse serialized data |
| 68 | * |
| 69 | * Parse serialized data describing a load option and transform it to the |
| 70 | * efi_load_option structure. |
| 71 | * |
| 72 | * @lo: pointer to target |
| 73 | * @data: serialized data |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 74 | * @size: size of the load option, on return size of the optional data |
| 75 | * Return: status code |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 76 | */ |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 77 | efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data, |
| 78 | efi_uintn_t *size) |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 79 | { |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 80 | efi_uintn_t len; |
| 81 | |
| 82 | len = sizeof(u32); |
| 83 | if (*size < len + 2 * sizeof(u16)) |
| 84 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 85 | lo->attributes = get_unaligned_le32(data); |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 86 | data += len; |
| 87 | *size -= len; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 88 | |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 89 | len = sizeof(u16); |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 90 | lo->file_path_length = get_unaligned_le16(data); |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 91 | data += len; |
| 92 | *size -= len; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 93 | |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 94 | lo->label = (u16 *)data; |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 95 | len = u16_strnlen(lo->label, *size / sizeof(u16) - 1); |
| 96 | if (lo->label[len]) |
| 97 | return EFI_INVALID_PARAMETER; |
| 98 | len = (len + 1) * sizeof(u16); |
| 99 | if (*size < len) |
| 100 | return EFI_INVALID_PARAMETER; |
| 101 | data += len; |
| 102 | *size -= len; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 103 | |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 104 | len = lo->file_path_length; |
| 105 | if (*size < len) |
| 106 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 107 | lo->file_path = (struct efi_device_path *)data; |
Heinrich Schuchardt | 15d8f00 | 2020-08-23 10:59:17 +0200 | [diff] [blame] | 108 | if (efi_dp_check_length(lo->file_path, len) < 0) |
| 109 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 110 | data += len; |
| 111 | *size -= len; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 112 | |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 113 | lo->optional_data = data; |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 114 | |
| 115 | return EFI_SUCCESS; |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 116 | } |
| 117 | |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 118 | /** |
| 119 | * efi_serialize_load_option() - serialize load option |
| 120 | * |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 121 | * Serialize efi_load_option structure into byte stream for BootXXXX. |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 122 | * |
| 123 | * @data: buffer for serialized data |
| 124 | * @lo: load option |
| 125 | * Return: size of allocated buffer |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 126 | */ |
| 127 | unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data) |
| 128 | { |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 129 | unsigned long label_len; |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 130 | unsigned long size; |
| 131 | u8 *p; |
| 132 | |
| 133 | label_len = (u16_strlen(lo->label) + 1) * sizeof(u16); |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 134 | |
| 135 | /* total size */ |
| 136 | size = sizeof(lo->attributes); |
| 137 | size += sizeof(lo->file_path_length); |
| 138 | size += label_len; |
| 139 | size += lo->file_path_length; |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 140 | if (lo->optional_data) |
| 141 | size += (utf8_utf16_strlen((const char *)lo->optional_data) |
| 142 | + 1) * sizeof(u16); |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 143 | p = malloc(size); |
| 144 | if (!p) |
| 145 | return 0; |
| 146 | |
| 147 | /* copy data */ |
| 148 | *data = p; |
| 149 | memcpy(p, &lo->attributes, sizeof(lo->attributes)); |
| 150 | p += sizeof(lo->attributes); |
| 151 | |
| 152 | memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length)); |
| 153 | p += sizeof(lo->file_path_length); |
| 154 | |
| 155 | memcpy(p, lo->label, label_len); |
| 156 | p += label_len; |
| 157 | |
| 158 | memcpy(p, lo->file_path, lo->file_path_length); |
| 159 | p += lo->file_path_length; |
| 160 | |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 161 | if (lo->optional_data) { |
| 162 | utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data); |
| 163 | p += sizeof(u16); /* size of trailing \0 */ |
| 164 | } |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 165 | return size; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 166 | } |
| 167 | |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 168 | /** |
| 169 | * get_var() - get UEFI variable |
| 170 | * |
| 171 | * It is the caller's duty to free the returned buffer. |
| 172 | * |
| 173 | * @name: name of variable |
| 174 | * @vendor: vendor GUID of variable |
| 175 | * @size: size of allocated buffer |
| 176 | * Return: buffer with variable data or NULL |
| 177 | */ |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 178 | static void *get_var(u16 *name, const efi_guid_t *vendor, |
Heinrich Schuchardt | 45c66f9 | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 179 | efi_uintn_t *size) |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 180 | { |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 181 | efi_status_t ret; |
| 182 | void *buf = NULL; |
| 183 | |
| 184 | *size = 0; |
Heinrich Schuchardt | dda8c71 | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 185 | ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 186 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 187 | buf = malloc(*size); |
Heinrich Schuchardt | dda8c71 | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 188 | ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | if (ret != EFI_SUCCESS) { |
| 192 | free(buf); |
| 193 | *size = 0; |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | return buf; |
| 198 | } |
| 199 | |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 200 | /** |
| 201 | * try_load_entry() - try to load image for boot option |
| 202 | * |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 203 | * Attempt to load load-option number 'n', returning device_path and file_path |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 204 | * if successful. This checks that the EFI_LOAD_OPTION is active (enabled) |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 205 | * and that the specified file to boot exists. |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 206 | * |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 207 | * @n: number of the boot option, e.g. 0x0a13 for Boot0A13 |
| 208 | * @handle: on return handle for the newly installed image |
| 209 | * @load_options: load options set on the loaded image protocol |
| 210 | * Return: status code |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 211 | */ |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 212 | static efi_status_t try_load_entry(u16 n, efi_handle_t *handle, |
| 213 | void **load_options) |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 214 | { |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 215 | struct efi_load_option lo; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 216 | u16 varname[] = L"Boot0000"; |
| 217 | u16 hexmap[] = L"0123456789ABCDEF"; |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 218 | void *load_option; |
Heinrich Schuchardt | 45c66f9 | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 219 | efi_uintn_t size; |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 220 | efi_status_t ret; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 221 | |
| 222 | varname[4] = hexmap[(n & 0xf000) >> 12]; |
| 223 | varname[5] = hexmap[(n & 0x0f00) >> 8]; |
| 224 | varname[6] = hexmap[(n & 0x00f0) >> 4]; |
| 225 | varname[7] = hexmap[(n & 0x000f) >> 0]; |
| 226 | |
| 227 | load_option = get_var(varname, &efi_global_variable_guid, &size); |
| 228 | if (!load_option) |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 229 | return EFI_LOAD_ERROR; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 230 | |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 231 | ret = efi_deserialize_load_option(&lo, load_option, &size); |
| 232 | if (ret != EFI_SUCCESS) { |
| 233 | log_warning("Invalid load option for %ls\n", varname); |
| 234 | goto error; |
| 235 | } |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 236 | |
| 237 | if (lo.attributes & LOAD_OPTION_ACTIVE) { |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 238 | u32 attributes; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 239 | |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 240 | log_debug("%s: trying to load \"%ls\" from %pD\n", |
| 241 | __func__, lo.label, lo.file_path); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 242 | |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 243 | ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path, |
| 244 | NULL, 0, handle)); |
AKASHI Takahiro | 94e6e55 | 2019-05-29 20:54:25 +0200 | [diff] [blame] | 245 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 246 | log_warning("Loading %ls '%ls' failed\n", |
| 247 | varname, lo.label); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 248 | goto error; |
AKASHI Takahiro | 94e6e55 | 2019-05-29 20:54:25 +0200 | [diff] [blame] | 249 | } |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 250 | |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 251 | attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 252 | EFI_VARIABLE_RUNTIME_ACCESS; |
Heinrich Schuchardt | dda8c71 | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 253 | ret = efi_set_variable_int(L"BootCurrent", |
| 254 | &efi_global_variable_guid, |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 255 | attributes, sizeof(n), &n, false); |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 256 | if (ret != EFI_SUCCESS) { |
| 257 | if (EFI_CALL(efi_unload_image(*handle)) |
| 258 | != EFI_SUCCESS) |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 259 | log_err("Unloading image failed\n"); |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 260 | goto error; |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 261 | } |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 262 | |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 263 | log_info("Booting: %ls\n", lo.label); |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 264 | } else { |
| 265 | ret = EFI_LOAD_ERROR; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 266 | } |
| 267 | |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 268 | /* Set load options */ |
| 269 | if (size) { |
| 270 | *load_options = malloc(size); |
| 271 | if (!*load_options) { |
| 272 | ret = EFI_OUT_OF_RESOURCES; |
| 273 | goto error; |
| 274 | } |
| 275 | memcpy(*load_options, lo.optional_data, size); |
| 276 | ret = efi_set_load_options(*handle, size, *load_options); |
| 277 | } else { |
| 278 | load_options = NULL; |
| 279 | } |
| 280 | |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 281 | error: |
| 282 | free(load_option); |
| 283 | |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 284 | return ret; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 285 | } |
| 286 | |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 287 | /** |
| 288 | * efi_bootmgr_load() - try to load from BootNext or BootOrder |
| 289 | * |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 290 | * Attempt to load from BootNext or in the order specified by BootOrder |
| 291 | * EFI variable, the available load-options, finding and returning |
| 292 | * the first one that can be loaded successfully. |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 293 | * |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 294 | * @handle: on return handle for the newly installed image |
| 295 | * @load_options: load options set on the loaded image protocol |
| 296 | * Return: status code |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 297 | */ |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 298 | efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options) |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 299 | { |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 300 | u16 bootnext, *bootorder; |
Heinrich Schuchardt | 45c66f9 | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 301 | efi_uintn_t size; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 302 | int i, num; |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 303 | efi_status_t ret; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 304 | |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 305 | bs = systab.boottime; |
| 306 | rs = systab.runtime; |
| 307 | |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 308 | /* BootNext */ |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 309 | size = sizeof(bootnext); |
Heinrich Schuchardt | dda8c71 | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 310 | ret = efi_get_variable_int(L"BootNext", |
| 311 | &efi_global_variable_guid, |
| 312 | NULL, &size, &bootnext, NULL); |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 313 | if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) { |
| 314 | /* BootNext does exist here */ |
| 315 | if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 316 | log_err("BootNext must be 16-bit integer\n"); |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 317 | |
| 318 | /* delete BootNext */ |
Heinrich Schuchardt | dda8c71 | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 319 | ret = efi_set_variable_int(L"BootNext", |
| 320 | &efi_global_variable_guid, |
| 321 | 0, 0, NULL, false); |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 322 | |
| 323 | /* load BootNext */ |
| 324 | if (ret == EFI_SUCCESS) { |
| 325 | if (size == sizeof(u16)) { |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 326 | ret = try_load_entry(bootnext, handle, |
| 327 | load_options); |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 328 | if (ret == EFI_SUCCESS) |
| 329 | return ret; |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 330 | log_warning( |
| 331 | "Loading from BootNext failed, falling back to BootOrder\n"); |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 332 | } |
| 333 | } else { |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 334 | log_err("Deleting BootNext failed\n"); |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | /* BootOrder */ |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 339 | bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size); |
Heinrich Schuchardt | 33e4497 | 2019-02-24 04:44:48 +0100 | [diff] [blame] | 340 | if (!bootorder) { |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 341 | log_info("BootOrder not defined\n"); |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 342 | ret = EFI_NOT_FOUND; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 343 | goto error; |
Heinrich Schuchardt | 33e4497 | 2019-02-24 04:44:48 +0100 | [diff] [blame] | 344 | } |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 345 | |
| 346 | num = size / sizeof(uint16_t); |
| 347 | for (i = 0; i < num; i++) { |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 348 | log_debug("%s trying to load Boot%04X\n", __func__, |
| 349 | bootorder[i]); |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 350 | ret = try_load_entry(bootorder[i], handle, load_options); |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 351 | if (ret == EFI_SUCCESS) |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 352 | break; |
| 353 | } |
| 354 | |
| 355 | free(bootorder); |
| 356 | |
| 357 | error: |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 358 | return ret; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 359 | } |