Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 3 | * EFI boot manager |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2017 Rob Clark |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 10 | #include <common.h> |
| 11 | #include <charset.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 13 | #include <malloc.h> |
AKASHI Takahiro | fd972f5 | 2022-04-28 17:09:39 +0900 | [diff] [blame] | 14 | #include <efi_default_filename.h> |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 15 | #include <efi_loader.h> |
Heinrich Schuchardt | f625fed | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 16 | #include <efi_variable.h> |
AKASHI Takahiro | bd23774 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 17 | #include <asm/unaligned.h> |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 18 | |
| 19 | static const struct efi_boot_services *bs; |
| 20 | static const struct efi_runtime_services *rs; |
| 21 | |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 22 | const efi_guid_t efi_guid_bootmenu_auto_generated = |
| 23 | EFICONFIG_AUTO_GENERATED_ENTRY_GUID; |
| 24 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 25 | /* |
| 26 | * bootmgr implements the logic of trying to find a payload to boot |
| 27 | * based on the BootOrder + BootXXXX variables, and then loading it. |
| 28 | * |
| 29 | * TODO detecting a special key held (f9?) and displaying a boot menu |
| 30 | * like you would get on a PC would be clever. |
| 31 | * |
| 32 | * TODO if we had a way to write and persist variables after the OS |
| 33 | * has started, we'd also want to check OsIndications to see if we |
| 34 | * should do normal or recovery boot. |
| 35 | */ |
| 36 | |
Heinrich Schuchardt | 25c6be5 | 2020-08-07 17:47:13 +0200 | [diff] [blame] | 37 | /** |
AKASHI Takahiro | fd972f5 | 2022-04-28 17:09:39 +0900 | [diff] [blame] | 38 | * expand_media_path() - expand a device path for default file name |
| 39 | * @device_path: device path to check against |
| 40 | * |
| 41 | * If @device_path is a media or disk partition which houses a file |
| 42 | * system, this function returns a full device path which contains |
| 43 | * an architecture-specific default file name for removable media. |
| 44 | * |
| 45 | * Return: a newly allocated device path |
| 46 | */ |
| 47 | static |
| 48 | struct efi_device_path *expand_media_path(struct efi_device_path *device_path) |
| 49 | { |
Heinrich Schuchardt | ff08eac | 2023-05-13 10:36:21 +0200 | [diff] [blame] | 50 | struct efi_device_path *rem, *full_path; |
AKASHI Takahiro | fd972f5 | 2022-04-28 17:09:39 +0900 | [diff] [blame] | 51 | efi_handle_t handle; |
AKASHI Takahiro | fd972f5 | 2022-04-28 17:09:39 +0900 | [diff] [blame] | 52 | |
| 53 | if (!device_path) |
| 54 | return NULL; |
| 55 | |
| 56 | /* |
| 57 | * If device_path is a (removable) media or partition which provides |
| 58 | * simple file system protocol, append a default file name to support |
| 59 | * booting from removable media. |
| 60 | */ |
Heinrich Schuchardt | ff08eac | 2023-05-13 10:36:21 +0200 | [diff] [blame] | 61 | handle = efi_dp_find_obj(device_path, |
| 62 | &efi_simple_file_system_protocol_guid, &rem); |
Heinrich Schuchardt | f57eacb | 2022-06-11 05:22:08 +0000 | [diff] [blame] | 63 | if (handle) { |
Heinrich Schuchardt | 0628e1c | 2022-06-11 05:22:07 +0000 | [diff] [blame] | 64 | if (rem->type == DEVICE_PATH_TYPE_END) { |
Heinrich Schuchardt | ff08eac | 2023-05-13 10:36:21 +0200 | [diff] [blame] | 65 | full_path = efi_dp_from_file(device_path, |
| 66 | "/EFI/BOOT/" BOOTEFI_NAME); |
AKASHI Takahiro | fd972f5 | 2022-04-28 17:09:39 +0900 | [diff] [blame] | 67 | } else { |
| 68 | full_path = efi_dp_dup(device_path); |
| 69 | } |
| 70 | } else { |
| 71 | full_path = efi_dp_dup(device_path); |
| 72 | } |
| 73 | |
| 74 | return full_path; |
| 75 | } |
| 76 | |
| 77 | /** |
AKASHI Takahiro | dac4d09 | 2022-05-12 11:29:02 +0900 | [diff] [blame] | 78 | * try_load_from_file_path() - try to load a file |
| 79 | * |
| 80 | * Given a file media path iterate through a list of handles and try to |
| 81 | * to load the file from each of them until the first success. |
| 82 | * |
| 83 | * @fs_handles: array of handles with the simple file protocol |
| 84 | * @num: number of handles in fs_handles |
| 85 | * @fp: file path to open |
| 86 | * @handle: on return pointer to handle for loaded image |
| 87 | * @removable: if true only consider removable media, else only non-removable |
| 88 | */ |
| 89 | static efi_status_t try_load_from_file_path(efi_handle_t *fs_handles, |
| 90 | efi_uintn_t num, |
| 91 | struct efi_device_path *fp, |
| 92 | efi_handle_t *handle, |
| 93 | bool removable) |
| 94 | { |
| 95 | struct efi_handler *handler; |
| 96 | struct efi_device_path *dp; |
| 97 | int i; |
| 98 | efi_status_t ret; |
| 99 | |
| 100 | for (i = 0; i < num; i++) { |
| 101 | if (removable != efi_disk_is_removable(fs_handles[i])) |
| 102 | continue; |
| 103 | |
| 104 | ret = efi_search_protocol(fs_handles[i], &efi_guid_device_path, |
| 105 | &handler); |
| 106 | if (ret != EFI_SUCCESS) |
| 107 | continue; |
| 108 | |
| 109 | dp = handler->protocol_interface; |
| 110 | if (!dp) |
| 111 | continue; |
| 112 | |
| 113 | dp = efi_dp_append(dp, fp); |
| 114 | if (!dp) |
| 115 | continue; |
| 116 | |
| 117 | ret = EFI_CALL(efi_load_image(true, efi_root, dp, NULL, 0, |
| 118 | handle)); |
| 119 | efi_free_pool(dp); |
| 120 | if (ret == EFI_SUCCESS) |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | return EFI_NOT_FOUND; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * try_load_from_short_path |
| 129 | * @fp: file path |
| 130 | * @handle: pointer to handle for newly installed image |
| 131 | * |
| 132 | * Enumerate all the devices which support file system operations, |
| 133 | * prepend its media device path to the file path, @fp, and |
| 134 | * try to load the file. |
| 135 | * This function should be called when handling a short-form path |
| 136 | * which is starting with a file device path. |
| 137 | * |
| 138 | * Return: status code |
| 139 | */ |
| 140 | static efi_status_t try_load_from_short_path(struct efi_device_path *fp, |
| 141 | efi_handle_t *handle) |
| 142 | { |
| 143 | efi_handle_t *fs_handles; |
| 144 | efi_uintn_t num; |
| 145 | efi_status_t ret; |
| 146 | |
| 147 | ret = EFI_CALL(efi_locate_handle_buffer( |
| 148 | BY_PROTOCOL, |
| 149 | &efi_simple_file_system_protocol_guid, |
| 150 | NULL, |
| 151 | &num, &fs_handles)); |
| 152 | if (ret != EFI_SUCCESS) |
| 153 | return ret; |
| 154 | if (!num) |
| 155 | return EFI_NOT_FOUND; |
| 156 | |
| 157 | /* removable media first */ |
| 158 | ret = try_load_from_file_path(fs_handles, num, fp, handle, true); |
| 159 | if (ret == EFI_SUCCESS) |
| 160 | goto out; |
| 161 | |
| 162 | /* fixed media */ |
| 163 | ret = try_load_from_file_path(fs_handles, num, fp, handle, false); |
| 164 | if (ret == EFI_SUCCESS) |
| 165 | goto out; |
| 166 | |
| 167 | out: |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | /** |
Heinrich Schuchardt | e612ba6 | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 172 | * try_load_entry() - try to load image for boot option |
| 173 | * |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 174 | * Attempt to load load-option number 'n', returning device_path and file_path |
Heinrich Schuchardt | e612ba6 | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 175 | * if successful. This checks that the EFI_LOAD_OPTION is active (enabled) |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 176 | * and that the specified file to boot exists. |
Heinrich Schuchardt | e612ba6 | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 177 | * |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 178 | * @n: number of the boot option, e.g. 0x0a13 for Boot0A13 |
| 179 | * @handle: on return handle for the newly installed image |
| 180 | * @load_options: load options set on the loaded image protocol |
| 181 | * Return: status code |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 182 | */ |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 183 | static efi_status_t try_load_entry(u16 n, efi_handle_t *handle, |
| 184 | void **load_options) |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 185 | { |
AKASHI Takahiro | bd23774 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 186 | struct efi_load_option lo; |
Heinrich Schuchardt | c79cebe | 2022-04-25 23:35:01 +0200 | [diff] [blame] | 187 | u16 varname[9]; |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 188 | void *load_option; |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 189 | efi_uintn_t size; |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 190 | efi_status_t ret; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 191 | |
Heinrich Schuchardt | c79cebe | 2022-04-25 23:35:01 +0200 | [diff] [blame] | 192 | efi_create_indexed_name(varname, sizeof(varname), "Boot", n); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 193 | |
Ilias Apalodimas | fc4ca6b | 2021-03-27 10:56:07 +0200 | [diff] [blame] | 194 | load_option = efi_get_var(varname, &efi_global_variable_guid, &size); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 195 | if (!load_option) |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 196 | return EFI_LOAD_ERROR; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 197 | |
Heinrich Schuchardt | 7ca84f8 | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 198 | ret = efi_deserialize_load_option(&lo, load_option, &size); |
| 199 | if (ret != EFI_SUCCESS) { |
| 200 | log_warning("Invalid load option for %ls\n", varname); |
| 201 | goto error; |
| 202 | } |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 203 | |
| 204 | if (lo.attributes & LOAD_OPTION_ACTIVE) { |
AKASHI Takahiro | fd972f5 | 2022-04-28 17:09:39 +0900 | [diff] [blame] | 205 | struct efi_device_path *file_path; |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 206 | u32 attributes; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 207 | |
Heinrich Schuchardt | e07fe5c | 2022-04-29 07:15:04 +0200 | [diff] [blame] | 208 | log_debug("trying to load \"%ls\" from %pD\n", lo.label, |
| 209 | lo.file_path); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 210 | |
AKASHI Takahiro | dac4d09 | 2022-05-12 11:29:02 +0900 | [diff] [blame] | 211 | if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) { |
| 212 | /* file_path doesn't contain a device path */ |
| 213 | ret = try_load_from_short_path(lo.file_path, handle); |
| 214 | } else { |
| 215 | file_path = expand_media_path(lo.file_path); |
| 216 | ret = EFI_CALL(efi_load_image(true, efi_root, file_path, |
| 217 | NULL, 0, handle)); |
| 218 | efi_free_pool(file_path); |
| 219 | } |
AKASHI Takahiro | 15db9ce | 2019-05-29 20:54:25 +0200 | [diff] [blame] | 220 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 221 | log_warning("Loading %ls '%ls' failed\n", |
| 222 | varname, lo.label); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 223 | goto error; |
AKASHI Takahiro | 15db9ce | 2019-05-29 20:54:25 +0200 | [diff] [blame] | 224 | } |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 225 | |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 226 | attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 227 | EFI_VARIABLE_RUNTIME_ACCESS; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 228 | ret = efi_set_variable_int(u"BootCurrent", |
Heinrich Schuchardt | f625fed | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 229 | &efi_global_variable_guid, |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 230 | attributes, sizeof(n), &n, false); |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 231 | if (ret != EFI_SUCCESS) |
| 232 | goto unload; |
| 233 | /* try to register load file2 for initrd's */ |
| 234 | if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) { |
| 235 | ret = efi_initrd_register(); |
| 236 | if (ret != EFI_SUCCESS) |
| 237 | goto unload; |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 238 | } |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 239 | |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 240 | log_info("Booting: %ls\n", lo.label); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 241 | } else { |
| 242 | ret = EFI_LOAD_ERROR; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 243 | } |
| 244 | |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 245 | /* Set load options */ |
Masahisa Kojima | 767a9e6 | 2022-09-12 17:33:54 +0900 | [diff] [blame] | 246 | if (size >= sizeof(efi_guid_t) && |
| 247 | !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) |
| 248 | size = 0; |
| 249 | |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 250 | if (size) { |
| 251 | *load_options = malloc(size); |
| 252 | if (!*load_options) { |
| 253 | ret = EFI_OUT_OF_RESOURCES; |
| 254 | goto error; |
| 255 | } |
| 256 | memcpy(*load_options, lo.optional_data, size); |
| 257 | ret = efi_set_load_options(*handle, size, *load_options); |
| 258 | } else { |
Heinrich Schuchardt | 7c2a859 | 2020-12-27 15:46:00 +0100 | [diff] [blame] | 259 | *load_options = NULL; |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 262 | error: |
| 263 | free(load_option); |
| 264 | |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 265 | return ret; |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 266 | |
| 267 | unload: |
| 268 | if (EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS) |
| 269 | log_err("Unloading image failed\n"); |
| 270 | free(load_option); |
| 271 | |
| 272 | return ret; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 273 | } |
| 274 | |
Heinrich Schuchardt | e612ba6 | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 275 | /** |
| 276 | * efi_bootmgr_load() - try to load from BootNext or BootOrder |
| 277 | * |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 278 | * Attempt to load from BootNext or in the order specified by BootOrder |
| 279 | * EFI variable, the available load-options, finding and returning |
| 280 | * the first one that can be loaded successfully. |
Heinrich Schuchardt | e612ba6 | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 281 | * |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 282 | * @handle: on return handle for the newly installed image |
| 283 | * @load_options: load options set on the loaded image protocol |
| 284 | * Return: status code |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 285 | */ |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 286 | efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options) |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 287 | { |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 288 | u16 bootnext, *bootorder; |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 289 | efi_uintn_t size; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 290 | int i, num; |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 291 | efi_status_t ret; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 292 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 293 | bs = systab.boottime; |
| 294 | rs = systab.runtime; |
| 295 | |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 296 | /* BootNext */ |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 297 | size = sizeof(bootnext); |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 298 | ret = efi_get_variable_int(u"BootNext", |
Heinrich Schuchardt | f625fed | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 299 | &efi_global_variable_guid, |
| 300 | NULL, &size, &bootnext, NULL); |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 301 | if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) { |
| 302 | /* BootNext does exist here */ |
| 303 | if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 304 | log_err("BootNext must be 16-bit integer\n"); |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 305 | |
| 306 | /* delete BootNext */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 307 | ret = efi_set_variable_int(u"BootNext", |
Heinrich Schuchardt | f625fed | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 308 | &efi_global_variable_guid, |
| 309 | 0, 0, NULL, false); |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 310 | |
| 311 | /* load BootNext */ |
| 312 | if (ret == EFI_SUCCESS) { |
| 313 | if (size == sizeof(u16)) { |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 314 | ret = try_load_entry(bootnext, handle, |
| 315 | load_options); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 316 | if (ret == EFI_SUCCESS) |
| 317 | return ret; |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 318 | log_warning( |
| 319 | "Loading from BootNext failed, falling back to BootOrder\n"); |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 320 | } |
| 321 | } else { |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 322 | log_err("Deleting BootNext failed\n"); |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | /* BootOrder */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 327 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); |
Heinrich Schuchardt | 3d2257f | 2019-02-24 04:44:48 +0100 | [diff] [blame] | 328 | if (!bootorder) { |
Heinrich Schuchardt | 273df96 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 329 | log_info("BootOrder not defined\n"); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 330 | ret = EFI_NOT_FOUND; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 331 | goto error; |
Heinrich Schuchardt | 3d2257f | 2019-02-24 04:44:48 +0100 | [diff] [blame] | 332 | } |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 333 | |
| 334 | num = size / sizeof(uint16_t); |
| 335 | for (i = 0; i < num; i++) { |
Heinrich Schuchardt | e07fe5c | 2022-04-29 07:15:04 +0200 | [diff] [blame] | 336 | log_debug("trying to load Boot%04X\n", bootorder[i]); |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 337 | ret = try_load_entry(bootorder[i], handle, load_options); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 338 | if (ret == EFI_SUCCESS) |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | |
| 342 | free(bootorder); |
| 343 | |
| 344 | error: |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 345 | return ret; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 346 | } |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 347 | |
| 348 | /** |
| 349 | * efi_bootmgr_enumerate_boot_option() - enumerate the possible bootable media |
| 350 | * |
| 351 | * @opt: pointer to the media boot option structure |
| 352 | * @volume_handles: pointer to the efi handles |
| 353 | * @count: number of efi handle |
| 354 | * Return: status code |
| 355 | */ |
| 356 | static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boot_option *opt, |
| 357 | efi_handle_t *volume_handles, |
| 358 | efi_status_t count) |
| 359 | { |
| 360 | u32 i; |
| 361 | struct efi_handler *handler; |
| 362 | efi_status_t ret = EFI_SUCCESS; |
| 363 | |
| 364 | for (i = 0; i < count; i++) { |
| 365 | u16 *p; |
| 366 | u16 dev_name[BOOTMENU_DEVICE_NAME_MAX]; |
| 367 | char *optional_data; |
| 368 | struct efi_load_option lo; |
| 369 | char buf[BOOTMENU_DEVICE_NAME_MAX]; |
| 370 | struct efi_device_path *device_path; |
Raymond Mao | b5542a6 | 2023-06-19 14:23:01 -0700 | [diff] [blame] | 371 | struct efi_device_path *short_dp; |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 372 | |
| 373 | ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler); |
| 374 | if (ret != EFI_SUCCESS) |
| 375 | continue; |
| 376 | ret = efi_protocol_open(handler, (void **)&device_path, |
| 377 | efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 378 | if (ret != EFI_SUCCESS) |
| 379 | continue; |
| 380 | |
| 381 | ret = efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEVICE_NAME_MAX); |
| 382 | if (ret != EFI_SUCCESS) |
| 383 | continue; |
| 384 | |
| 385 | p = dev_name; |
| 386 | utf8_utf16_strncpy(&p, buf, strlen(buf)); |
| 387 | |
Raymond Mao | b5542a6 | 2023-06-19 14:23:01 -0700 | [diff] [blame] | 388 | /* prefer to short form device path */ |
| 389 | short_dp = efi_dp_shorten(device_path); |
| 390 | if (short_dp) |
| 391 | device_path = short_dp; |
| 392 | |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 393 | lo.label = dev_name; |
| 394 | lo.attributes = LOAD_OPTION_ACTIVE; |
| 395 | lo.file_path = device_path; |
| 396 | lo.file_path_length = efi_dp_size(device_path) + sizeof(END); |
| 397 | /* |
| 398 | * Set the dedicated guid to optional_data, it is used to identify |
| 399 | * the boot option that automatically generated by the bootmenu. |
| 400 | * efi_serialize_load_option() expects optional_data is null-terminated |
| 401 | * utf8 string, so set the "1234567" string to allocate enough space |
| 402 | * to store guid, instead of realloc the load_option. |
| 403 | */ |
| 404 | lo.optional_data = "1234567"; |
| 405 | opt[i].size = efi_serialize_load_option(&lo, (u8 **)&opt[i].lo); |
| 406 | if (!opt[i].size) { |
| 407 | ret = EFI_OUT_OF_RESOURCES; |
| 408 | goto out; |
| 409 | } |
| 410 | /* set the guid */ |
| 411 | optional_data = (char *)opt[i].lo + (opt[i].size - u16_strsize(u"1234567")); |
| 412 | memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t)); |
| 413 | } |
| 414 | |
| 415 | out: |
| 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * efi_bootmgr_delete_invalid_boot_option() - delete non-existing boot option |
| 421 | * |
| 422 | * @opt: pointer to the media boot option structure |
| 423 | * @count: number of media boot option structure |
| 424 | * Return: status code |
| 425 | */ |
| 426 | static efi_status_t efi_bootmgr_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt, |
| 427 | efi_status_t count) |
| 428 | { |
| 429 | efi_uintn_t size; |
| 430 | void *load_option; |
| 431 | u32 i, list_size = 0; |
| 432 | struct efi_load_option lo; |
| 433 | u16 *var_name16 = NULL; |
| 434 | u16 varname[] = u"Boot####"; |
| 435 | efi_status_t ret = EFI_SUCCESS; |
| 436 | u16 *delete_index_list = NULL, *p; |
| 437 | efi_uintn_t buf_size; |
| 438 | |
| 439 | buf_size = 128; |
| 440 | var_name16 = malloc(buf_size); |
| 441 | if (!var_name16) |
| 442 | return EFI_OUT_OF_RESOURCES; |
| 443 | |
| 444 | var_name16[0] = 0; |
| 445 | for (;;) { |
| 446 | int index; |
| 447 | efi_guid_t guid; |
| 448 | efi_uintn_t tmp; |
| 449 | |
| 450 | ret = efi_next_variable_name(&buf_size, &var_name16, &guid); |
| 451 | if (ret == EFI_NOT_FOUND) { |
| 452 | /* |
| 453 | * EFI_NOT_FOUND indicates we retrieved all EFI variables. |
| 454 | * This should be treated as success. |
| 455 | */ |
| 456 | ret = EFI_SUCCESS; |
| 457 | break; |
| 458 | } |
| 459 | |
| 460 | if (ret != EFI_SUCCESS) |
| 461 | goto out; |
| 462 | |
| 463 | if (!efi_varname_is_load_option(var_name16, &index)) |
| 464 | continue; |
| 465 | |
| 466 | efi_create_indexed_name(varname, sizeof(varname), "Boot", index); |
| 467 | load_option = efi_get_var(varname, &efi_global_variable_guid, &size); |
| 468 | if (!load_option) |
| 469 | continue; |
| 470 | |
| 471 | tmp = size; |
| 472 | ret = efi_deserialize_load_option(&lo, load_option, &size); |
| 473 | if (ret != EFI_SUCCESS) |
| 474 | goto next; |
| 475 | |
| 476 | if (size >= sizeof(efi_guid_bootmenu_auto_generated) && |
| 477 | !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) { |
| 478 | for (i = 0; i < count; i++) { |
| 479 | if (opt[i].size == tmp && |
| 480 | memcmp(opt[i].lo, load_option, tmp) == 0) { |
| 481 | opt[i].exist = true; |
| 482 | break; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * The entire list of variables must be retrieved by |
| 488 | * efi_get_next_variable_name_int() before deleting the invalid |
| 489 | * boot option, just save the index here. |
| 490 | */ |
| 491 | if (i == count) { |
| 492 | p = realloc(delete_index_list, sizeof(u32) * |
| 493 | (list_size + 1)); |
| 494 | if (!p) { |
| 495 | ret = EFI_OUT_OF_RESOURCES; |
| 496 | goto out; |
| 497 | } |
| 498 | delete_index_list = p; |
| 499 | delete_index_list[list_size++] = index; |
| 500 | } |
| 501 | } |
| 502 | next: |
| 503 | free(load_option); |
| 504 | } |
| 505 | |
| 506 | /* delete all invalid boot options */ |
| 507 | for (i = 0; i < list_size; i++) { |
| 508 | ret = efi_bootmgr_delete_boot_option(delete_index_list[i]); |
| 509 | if (ret != EFI_SUCCESS) |
| 510 | goto out; |
| 511 | } |
| 512 | |
| 513 | out: |
| 514 | free(var_name16); |
| 515 | free(delete_index_list); |
| 516 | |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * efi_bootmgr_get_unused_bootoption() - get unused "Boot####" index |
| 522 | * |
| 523 | * @buf: pointer to the buffer to store boot option variable name |
| 524 | * @buf_size: buffer size |
| 525 | * @index: pointer to store the index in the BootOrder variable |
| 526 | * Return: status code |
| 527 | */ |
| 528 | efi_status_t efi_bootmgr_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size, |
| 529 | unsigned int *index) |
| 530 | { |
| 531 | u32 i; |
| 532 | efi_status_t ret; |
| 533 | efi_uintn_t size; |
| 534 | |
| 535 | if (buf_size < u16_strsize(u"Boot####")) |
| 536 | return EFI_BUFFER_TOO_SMALL; |
| 537 | |
| 538 | for (i = 0; i <= 0xFFFF; i++) { |
| 539 | size = 0; |
| 540 | efi_create_indexed_name(buf, buf_size, "Boot", i); |
| 541 | ret = efi_get_variable_int(buf, &efi_global_variable_guid, |
| 542 | NULL, &size, NULL, NULL); |
| 543 | if (ret == EFI_BUFFER_TOO_SMALL) |
| 544 | continue; |
| 545 | else |
| 546 | break; |
| 547 | } |
| 548 | |
| 549 | if (i > 0xFFFF) |
| 550 | return EFI_OUT_OF_RESOURCES; |
| 551 | |
| 552 | *index = i; |
| 553 | |
| 554 | return EFI_SUCCESS; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * efi_bootmgr_append_bootorder() - append new boot option in BootOrder variable |
| 559 | * |
| 560 | * @index: "Boot####" index to append to BootOrder variable |
| 561 | * Return: status code |
| 562 | */ |
| 563 | efi_status_t efi_bootmgr_append_bootorder(u16 index) |
| 564 | { |
| 565 | u16 *bootorder; |
| 566 | efi_status_t ret; |
| 567 | u16 *new_bootorder = NULL; |
| 568 | efi_uintn_t last, size, new_size; |
| 569 | |
| 570 | /* append new boot option */ |
| 571 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); |
| 572 | last = size / sizeof(u16); |
| 573 | new_size = size + sizeof(u16); |
| 574 | new_bootorder = calloc(1, new_size); |
| 575 | if (!new_bootorder) { |
| 576 | ret = EFI_OUT_OF_RESOURCES; |
| 577 | goto out; |
| 578 | } |
| 579 | memcpy(new_bootorder, bootorder, size); |
| 580 | new_bootorder[last] = index; |
| 581 | |
| 582 | ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid, |
| 583 | EFI_VARIABLE_NON_VOLATILE | |
| 584 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 585 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 586 | new_size, new_bootorder, false); |
| 587 | if (ret != EFI_SUCCESS) |
| 588 | goto out; |
| 589 | |
| 590 | out: |
| 591 | free(bootorder); |
| 592 | free(new_bootorder); |
| 593 | |
| 594 | return ret; |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * efi_bootmgr_delete_boot_option() - delete selected boot option |
| 599 | * |
| 600 | * @boot_index: boot option index to delete |
| 601 | * Return: status code |
| 602 | */ |
| 603 | efi_status_t efi_bootmgr_delete_boot_option(u16 boot_index) |
| 604 | { |
| 605 | u16 *bootorder; |
| 606 | u16 varname[9]; |
| 607 | efi_status_t ret; |
| 608 | unsigned int index; |
| 609 | efi_uintn_t num, size; |
| 610 | |
| 611 | efi_create_indexed_name(varname, sizeof(varname), |
| 612 | "Boot", boot_index); |
| 613 | ret = efi_set_variable_int(varname, &efi_global_variable_guid, |
| 614 | 0, 0, NULL, false); |
| 615 | if (ret != EFI_SUCCESS) { |
| 616 | log_err("delete boot option(%ls) failed\n", varname); |
| 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | /* update BootOrder if necessary */ |
| 621 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); |
| 622 | if (!bootorder) |
| 623 | return EFI_SUCCESS; |
| 624 | |
| 625 | num = size / sizeof(u16); |
| 626 | if (!efi_search_bootorder(bootorder, num, boot_index, &index)) |
| 627 | return EFI_SUCCESS; |
| 628 | |
| 629 | memmove(&bootorder[index], &bootorder[index + 1], |
| 630 | (num - index - 1) * sizeof(u16)); |
| 631 | size -= sizeof(u16); |
| 632 | ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid, |
| 633 | EFI_VARIABLE_NON_VOLATILE | |
| 634 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 635 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 636 | size, bootorder, false); |
| 637 | |
| 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * efi_bootmgr_update_media_device_boot_option() - generate the media device boot option |
| 643 | * |
| 644 | * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL |
| 645 | * and generate the bootmenu entries. |
| 646 | * This function also provide the BOOT#### variable maintenance for |
| 647 | * the media device entries. |
| 648 | * - Automatically create the BOOT#### variable for the newly detected device, |
| 649 | * this BOOT#### variable is distinguished by the special GUID |
| 650 | * stored in the EFI_LOAD_OPTION.optional_data |
| 651 | * - If the device is not attached to the system, the associated BOOT#### variable |
| 652 | * is automatically deleted. |
| 653 | * |
| 654 | * Return: status code |
| 655 | */ |
| 656 | efi_status_t efi_bootmgr_update_media_device_boot_option(void) |
| 657 | { |
| 658 | u32 i; |
| 659 | efi_status_t ret; |
| 660 | efi_uintn_t count; |
| 661 | efi_handle_t *volume_handles = NULL; |
| 662 | struct eficonfig_media_boot_option *opt = NULL; |
| 663 | |
| 664 | ret = efi_locate_handle_buffer_int(BY_PROTOCOL, |
| 665 | &efi_simple_file_system_protocol_guid, |
| 666 | NULL, &count, |
| 667 | (efi_handle_t **)&volume_handles); |
| 668 | if (ret != EFI_SUCCESS) |
Raymond Mao | a35784d | 2023-06-19 14:22:59 -0700 | [diff] [blame] | 669 | goto out; |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 670 | |
| 671 | opt = calloc(count, sizeof(struct eficonfig_media_boot_option)); |
Raymond Mao | a35784d | 2023-06-19 14:22:59 -0700 | [diff] [blame] | 672 | if (!opt) { |
| 673 | ret = EFI_OUT_OF_RESOURCES; |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 674 | goto out; |
Raymond Mao | a35784d | 2023-06-19 14:22:59 -0700 | [diff] [blame] | 675 | } |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 676 | |
| 677 | /* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */ |
| 678 | ret = efi_bootmgr_enumerate_boot_option(opt, volume_handles, count); |
| 679 | if (ret != EFI_SUCCESS) |
| 680 | goto out; |
| 681 | |
| 682 | /* |
| 683 | * System hardware configuration may vary depending on the user setup. |
| 684 | * The boot option is automatically added by the bootmenu. |
| 685 | * If the device is not attached to the system, the boot option needs |
| 686 | * to be deleted. |
| 687 | */ |
| 688 | ret = efi_bootmgr_delete_invalid_boot_option(opt, count); |
| 689 | if (ret != EFI_SUCCESS) |
| 690 | goto out; |
| 691 | |
| 692 | /* add non-existent boot option */ |
| 693 | for (i = 0; i < count; i++) { |
| 694 | u32 boot_index; |
| 695 | u16 var_name[9]; |
| 696 | |
| 697 | if (!opt[i].exist) { |
| 698 | ret = efi_bootmgr_get_unused_bootoption(var_name, sizeof(var_name), |
| 699 | &boot_index); |
| 700 | if (ret != EFI_SUCCESS) |
| 701 | goto out; |
| 702 | |
| 703 | ret = efi_set_variable_int(var_name, &efi_global_variable_guid, |
| 704 | EFI_VARIABLE_NON_VOLATILE | |
| 705 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 706 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 707 | opt[i].size, opt[i].lo, false); |
| 708 | if (ret != EFI_SUCCESS) |
| 709 | goto out; |
| 710 | |
| 711 | ret = efi_bootmgr_append_bootorder(boot_index); |
| 712 | if (ret != EFI_SUCCESS) { |
| 713 | efi_set_variable_int(var_name, &efi_global_variable_guid, |
| 714 | 0, 0, NULL, false); |
| 715 | goto out; |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | out: |
| 721 | if (opt) { |
| 722 | for (i = 0; i < count; i++) |
| 723 | free(opt[i].lo); |
| 724 | } |
| 725 | free(opt); |
| 726 | efi_free_pool(volume_handles); |
| 727 | |
Raymond Mao | a35784d | 2023-06-19 14:22:59 -0700 | [diff] [blame] | 728 | if (ret == EFI_NOT_FOUND) |
| 729 | return EFI_SUCCESS; |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 730 | return ret; |
| 731 | } |