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> |
AKASHI Takahiro | 4e65ca0 | 2022-04-28 17:09:39 +0900 | [diff] [blame^] | 14 | #include <efi_default_filename.h> |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 15 | #include <efi_loader.h> |
Heinrich Schuchardt | dda8c71 | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 16 | #include <efi_variable.h> |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 17 | #include <asm/unaligned.h> |
Rob Clark | 9975fe9 | 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 | |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 22 | /* |
| 23 | * bootmgr implements the logic of trying to find a payload to boot |
| 24 | * based on the BootOrder + BootXXXX variables, and then loading it. |
| 25 | * |
| 26 | * TODO detecting a special key held (f9?) and displaying a boot menu |
| 27 | * like you would get on a PC would be clever. |
| 28 | * |
| 29 | * TODO if we had a way to write and persist variables after the OS |
| 30 | * has started, we'd also want to check OsIndications to see if we |
| 31 | * should do normal or recovery boot. |
| 32 | */ |
| 33 | |
Heinrich Schuchardt | 1064d04 | 2020-08-07 17:47:13 +0200 | [diff] [blame] | 34 | /** |
AKASHI Takahiro | 4e65ca0 | 2022-04-28 17:09:39 +0900 | [diff] [blame^] | 35 | * expand_media_path() - expand a device path for default file name |
| 36 | * @device_path: device path to check against |
| 37 | * |
| 38 | * If @device_path is a media or disk partition which houses a file |
| 39 | * system, this function returns a full device path which contains |
| 40 | * an architecture-specific default file name for removable media. |
| 41 | * |
| 42 | * Return: a newly allocated device path |
| 43 | */ |
| 44 | static |
| 45 | struct efi_device_path *expand_media_path(struct efi_device_path *device_path) |
| 46 | { |
| 47 | struct efi_device_path *dp, *full_path; |
| 48 | efi_handle_t handle; |
| 49 | efi_status_t ret; |
| 50 | |
| 51 | if (!device_path) |
| 52 | return NULL; |
| 53 | |
| 54 | /* |
| 55 | * If device_path is a (removable) media or partition which provides |
| 56 | * simple file system protocol, append a default file name to support |
| 57 | * booting from removable media. |
| 58 | */ |
| 59 | dp = device_path; |
| 60 | ret = EFI_CALL(efi_locate_device_path( |
| 61 | &efi_simple_file_system_protocol_guid, |
| 62 | &dp, &handle)); |
| 63 | if (ret == EFI_SUCCESS) { |
| 64 | if (dp->type == DEVICE_PATH_TYPE_END) { |
| 65 | dp = efi_dp_from_file(NULL, 0, |
| 66 | "/EFI/BOOT/" BOOTEFI_NAME); |
| 67 | full_path = efi_dp_append(device_path, dp); |
| 68 | efi_free_pool(dp); |
| 69 | } else { |
| 70 | full_path = efi_dp_dup(device_path); |
| 71 | } |
| 72 | } else { |
| 73 | full_path = efi_dp_dup(device_path); |
| 74 | } |
| 75 | |
| 76 | return full_path; |
| 77 | } |
| 78 | |
| 79 | /** |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 80 | * try_load_entry() - try to load image for boot option |
| 81 | * |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 82 | * 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] | 83 | * if successful. This checks that the EFI_LOAD_OPTION is active (enabled) |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 84 | * and that the specified file to boot exists. |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 85 | * |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 86 | * @n: number of the boot option, e.g. 0x0a13 for Boot0A13 |
| 87 | * @handle: on return handle for the newly installed image |
| 88 | * @load_options: load options set on the loaded image protocol |
| 89 | * Return: status code |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 90 | */ |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 91 | static efi_status_t try_load_entry(u16 n, efi_handle_t *handle, |
| 92 | void **load_options) |
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 | struct efi_load_option lo; |
Heinrich Schuchardt | 4f41996 | 2022-04-25 23:35:01 +0200 | [diff] [blame] | 95 | u16 varname[9]; |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 96 | void *load_option; |
Heinrich Schuchardt | 45c66f9 | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 97 | efi_uintn_t size; |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 98 | efi_status_t ret; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 99 | |
Heinrich Schuchardt | 4f41996 | 2022-04-25 23:35:01 +0200 | [diff] [blame] | 100 | efi_create_indexed_name(varname, sizeof(varname), "Boot", n); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 101 | |
Ilias Apalodimas | f4dc1bc | 2021-03-27 10:56:07 +0200 | [diff] [blame] | 102 | load_option = efi_get_var(varname, &efi_global_variable_guid, &size); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 103 | if (!load_option) |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 104 | return EFI_LOAD_ERROR; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 105 | |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 106 | ret = efi_deserialize_load_option(&lo, load_option, &size); |
| 107 | if (ret != EFI_SUCCESS) { |
| 108 | log_warning("Invalid load option for %ls\n", varname); |
| 109 | goto error; |
| 110 | } |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 111 | |
| 112 | if (lo.attributes & LOAD_OPTION_ACTIVE) { |
AKASHI Takahiro | 4e65ca0 | 2022-04-28 17:09:39 +0900 | [diff] [blame^] | 113 | struct efi_device_path *file_path; |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 114 | u32 attributes; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 115 | |
Heinrich Schuchardt | 7ea79e5 | 2022-04-29 07:15:04 +0200 | [diff] [blame] | 116 | log_debug("trying to load \"%ls\" from %pD\n", lo.label, |
| 117 | lo.file_path); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 118 | |
AKASHI Takahiro | 4e65ca0 | 2022-04-28 17:09:39 +0900 | [diff] [blame^] | 119 | file_path = expand_media_path(lo.file_path); |
| 120 | ret = EFI_CALL(efi_load_image(true, efi_root, file_path, |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 121 | NULL, 0, handle)); |
AKASHI Takahiro | 4e65ca0 | 2022-04-28 17:09:39 +0900 | [diff] [blame^] | 122 | efi_free_pool(file_path); |
AKASHI Takahiro | 94e6e55 | 2019-05-29 20:54:25 +0200 | [diff] [blame] | 123 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 124 | log_warning("Loading %ls '%ls' failed\n", |
| 125 | varname, lo.label); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 126 | goto error; |
AKASHI Takahiro | 94e6e55 | 2019-05-29 20:54:25 +0200 | [diff] [blame] | 127 | } |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 128 | |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 129 | attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 130 | EFI_VARIABLE_RUNTIME_ACCESS; |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 131 | ret = efi_set_variable_int(u"BootCurrent", |
Heinrich Schuchardt | dda8c71 | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 132 | &efi_global_variable_guid, |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 133 | attributes, sizeof(n), &n, false); |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 134 | if (ret != EFI_SUCCESS) |
| 135 | goto unload; |
| 136 | /* try to register load file2 for initrd's */ |
| 137 | if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) { |
| 138 | ret = efi_initrd_register(); |
| 139 | if (ret != EFI_SUCCESS) |
| 140 | goto unload; |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 141 | } |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 142 | |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 143 | log_info("Booting: %ls\n", lo.label); |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 144 | } else { |
| 145 | ret = EFI_LOAD_ERROR; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 148 | /* Set load options */ |
| 149 | if (size) { |
| 150 | *load_options = malloc(size); |
| 151 | if (!*load_options) { |
| 152 | ret = EFI_OUT_OF_RESOURCES; |
| 153 | goto error; |
| 154 | } |
| 155 | memcpy(*load_options, lo.optional_data, size); |
| 156 | ret = efi_set_load_options(*handle, size, *load_options); |
| 157 | } else { |
Heinrich Schuchardt | e434311 | 2020-12-27 15:46:00 +0100 | [diff] [blame] | 158 | *load_options = NULL; |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 161 | error: |
| 162 | free(load_option); |
| 163 | |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 164 | return ret; |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 165 | |
| 166 | unload: |
| 167 | if (EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS) |
| 168 | log_err("Unloading image failed\n"); |
| 169 | free(load_option); |
| 170 | |
| 171 | return ret; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 174 | /** |
| 175 | * efi_bootmgr_load() - try to load from BootNext or BootOrder |
| 176 | * |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 177 | * Attempt to load from BootNext or in the order specified by BootOrder |
| 178 | * EFI variable, the available load-options, finding and returning |
| 179 | * the first one that can be loaded successfully. |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 180 | * |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 181 | * @handle: on return handle for the newly installed image |
| 182 | * @load_options: load options set on the loaded image protocol |
| 183 | * Return: status code |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 184 | */ |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 185 | 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] | 186 | { |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 187 | u16 bootnext, *bootorder; |
Heinrich Schuchardt | 45c66f9 | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 188 | efi_uintn_t size; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 189 | int i, num; |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 190 | efi_status_t ret; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 191 | |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 192 | bs = systab.boottime; |
| 193 | rs = systab.runtime; |
| 194 | |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 195 | /* BootNext */ |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 196 | size = sizeof(bootnext); |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 197 | ret = efi_get_variable_int(u"BootNext", |
Heinrich Schuchardt | dda8c71 | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 198 | &efi_global_variable_guid, |
| 199 | NULL, &size, &bootnext, NULL); |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 200 | if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) { |
| 201 | /* BootNext does exist here */ |
| 202 | if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 203 | log_err("BootNext must be 16-bit integer\n"); |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 204 | |
| 205 | /* delete BootNext */ |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 206 | ret = efi_set_variable_int(u"BootNext", |
Heinrich Schuchardt | dda8c71 | 2020-06-24 19:09:18 +0200 | [diff] [blame] | 207 | &efi_global_variable_guid, |
| 208 | 0, 0, NULL, false); |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 209 | |
| 210 | /* load BootNext */ |
| 211 | if (ret == EFI_SUCCESS) { |
| 212 | if (size == sizeof(u16)) { |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 213 | ret = try_load_entry(bootnext, handle, |
| 214 | load_options); |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 215 | if (ret == EFI_SUCCESS) |
| 216 | return ret; |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 217 | log_warning( |
| 218 | "Loading from BootNext failed, falling back to BootOrder\n"); |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 219 | } |
| 220 | } else { |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 221 | log_err("Deleting BootNext failed\n"); |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | /* BootOrder */ |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 226 | bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); |
Heinrich Schuchardt | 33e4497 | 2019-02-24 04:44:48 +0100 | [diff] [blame] | 227 | if (!bootorder) { |
Heinrich Schuchardt | 7a373e5 | 2020-05-31 10:07:31 +0200 | [diff] [blame] | 228 | log_info("BootOrder not defined\n"); |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 229 | ret = EFI_NOT_FOUND; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 230 | goto error; |
Heinrich Schuchardt | 33e4497 | 2019-02-24 04:44:48 +0100 | [diff] [blame] | 231 | } |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 232 | |
| 233 | num = size / sizeof(uint16_t); |
| 234 | for (i = 0; i < num; i++) { |
Heinrich Schuchardt | 7ea79e5 | 2022-04-29 07:15:04 +0200 | [diff] [blame] | 235 | log_debug("trying to load Boot%04X\n", bootorder[i]); |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 236 | ret = try_load_entry(bootorder[i], handle, load_options); |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 237 | if (ret == EFI_SUCCESS) |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 238 | break; |
| 239 | } |
| 240 | |
| 241 | free(bootorder); |
| 242 | |
| 243 | error: |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 244 | return ret; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 245 | } |