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 | |
| 8 | #include <common.h> |
| 9 | #include <charset.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame^] | 10 | #include <log.h> |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | #include <efi_loader.h> |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 13 | #include <asm/unaligned.h> |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 14 | |
| 15 | static const struct efi_boot_services *bs; |
| 16 | static const struct efi_runtime_services *rs; |
| 17 | |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 18 | /* |
| 19 | * bootmgr implements the logic of trying to find a payload to boot |
| 20 | * based on the BootOrder + BootXXXX variables, and then loading it. |
| 21 | * |
| 22 | * TODO detecting a special key held (f9?) and displaying a boot menu |
| 23 | * like you would get on a PC would be clever. |
| 24 | * |
| 25 | * TODO if we had a way to write and persist variables after the OS |
| 26 | * has started, we'd also want to check OsIndications to see if we |
| 27 | * should do normal or recovery boot. |
| 28 | */ |
| 29 | |
| 30 | |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 31 | /** |
| 32 | * efi_deserialize_load_option() - parse serialized data |
| 33 | * |
| 34 | * Parse serialized data describing a load option and transform it to the |
| 35 | * efi_load_option structure. |
| 36 | * |
| 37 | * @lo: pointer to target |
| 38 | * @data: serialized data |
| 39 | */ |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 40 | void efi_deserialize_load_option(struct efi_load_option *lo, u8 *data) |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 41 | { |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 42 | lo->attributes = get_unaligned_le32(data); |
| 43 | data += sizeof(u32); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 44 | |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 45 | lo->file_path_length = get_unaligned_le16(data); |
| 46 | data += sizeof(u16); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 47 | |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 48 | /* FIXME */ |
| 49 | lo->label = (u16 *)data; |
| 50 | data += (u16_strlen(lo->label) + 1) * sizeof(u16); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 51 | |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 52 | /* FIXME */ |
| 53 | lo->file_path = (struct efi_device_path *)data; |
| 54 | data += lo->file_path_length; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 55 | |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 56 | lo->optional_data = data; |
| 57 | } |
| 58 | |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 59 | /** |
| 60 | * efi_serialize_load_option() - serialize load option |
| 61 | * |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 62 | * Serialize efi_load_option structure into byte stream for BootXXXX. |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 63 | * |
| 64 | * @data: buffer for serialized data |
| 65 | * @lo: load option |
| 66 | * Return: size of allocated buffer |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 67 | */ |
| 68 | unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data) |
| 69 | { |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 70 | unsigned long label_len; |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 71 | unsigned long size; |
| 72 | u8 *p; |
| 73 | |
| 74 | label_len = (u16_strlen(lo->label) + 1) * sizeof(u16); |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 75 | |
| 76 | /* total size */ |
| 77 | size = sizeof(lo->attributes); |
| 78 | size += sizeof(lo->file_path_length); |
| 79 | size += label_len; |
| 80 | size += lo->file_path_length; |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 81 | if (lo->optional_data) |
| 82 | size += (utf8_utf16_strlen((const char *)lo->optional_data) |
| 83 | + 1) * sizeof(u16); |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 84 | p = malloc(size); |
| 85 | if (!p) |
| 86 | return 0; |
| 87 | |
| 88 | /* copy data */ |
| 89 | *data = p; |
| 90 | memcpy(p, &lo->attributes, sizeof(lo->attributes)); |
| 91 | p += sizeof(lo->attributes); |
| 92 | |
| 93 | memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length)); |
| 94 | p += sizeof(lo->file_path_length); |
| 95 | |
| 96 | memcpy(p, lo->label, label_len); |
| 97 | p += label_len; |
| 98 | |
| 99 | memcpy(p, lo->file_path, lo->file_path_length); |
| 100 | p += lo->file_path_length; |
| 101 | |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 102 | if (lo->optional_data) { |
| 103 | utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data); |
| 104 | p += sizeof(u16); /* size of trailing \0 */ |
| 105 | } |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 106 | return size; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 109 | /** |
| 110 | * get_var() - get UEFI variable |
| 111 | * |
| 112 | * It is the caller's duty to free the returned buffer. |
| 113 | * |
| 114 | * @name: name of variable |
| 115 | * @vendor: vendor GUID of variable |
| 116 | * @size: size of allocated buffer |
| 117 | * Return: buffer with variable data or NULL |
| 118 | */ |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 119 | static void *get_var(u16 *name, const efi_guid_t *vendor, |
Heinrich Schuchardt | 45c66f9 | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 120 | efi_uintn_t *size) |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 121 | { |
| 122 | efi_guid_t *v = (efi_guid_t *)vendor; |
| 123 | efi_status_t ret; |
| 124 | void *buf = NULL; |
| 125 | |
| 126 | *size = 0; |
Heinrich Schuchardt | 45c66f9 | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 127 | EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf)); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 128 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 129 | buf = malloc(*size); |
Heinrich Schuchardt | 45c66f9 | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 130 | EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf)); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | if (ret != EFI_SUCCESS) { |
| 134 | free(buf); |
| 135 | *size = 0; |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | return buf; |
| 140 | } |
| 141 | |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 142 | /** |
| 143 | * try_load_entry() - try to load image for boot option |
| 144 | * |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 145 | * 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] | 146 | * if successful. This checks that the EFI_LOAD_OPTION is active (enabled) |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 147 | * and that the specified file to boot exists. |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 148 | * |
| 149 | * @n: number of the boot option, e.g. 0x0a13 for Boot0A13 |
| 150 | * @handle: on return handle for the newly installed image |
| 151 | * Return: status code |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 152 | */ |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 153 | static efi_status_t try_load_entry(u16 n, efi_handle_t *handle) |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 154 | { |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 155 | struct efi_load_option lo; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 156 | u16 varname[] = L"Boot0000"; |
| 157 | u16 hexmap[] = L"0123456789ABCDEF"; |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 158 | void *load_option; |
Heinrich Schuchardt | 45c66f9 | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 159 | efi_uintn_t size; |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 160 | efi_status_t ret; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 161 | |
| 162 | varname[4] = hexmap[(n & 0xf000) >> 12]; |
| 163 | varname[5] = hexmap[(n & 0x0f00) >> 8]; |
| 164 | varname[6] = hexmap[(n & 0x00f0) >> 4]; |
| 165 | varname[7] = hexmap[(n & 0x000f) >> 0]; |
| 166 | |
| 167 | load_option = get_var(varname, &efi_global_variable_guid, &size); |
| 168 | if (!load_option) |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 169 | return EFI_LOAD_ERROR; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 170 | |
AKASHI Takahiro | 1a82b34 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 171 | efi_deserialize_load_option(&lo, load_option); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 172 | |
| 173 | if (lo.attributes & LOAD_OPTION_ACTIVE) { |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 174 | u32 attributes; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 175 | |
Heinrich Schuchardt | 4a8b5e7 | 2018-01-26 07:20:54 +0100 | [diff] [blame] | 176 | debug("%s: trying to load \"%ls\" from %pD\n", |
| 177 | __func__, lo.label, lo.file_path); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 178 | |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 179 | ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path, |
| 180 | NULL, 0, handle)); |
AKASHI Takahiro | 94e6e55 | 2019-05-29 20:54:25 +0200 | [diff] [blame] | 181 | if (ret != EFI_SUCCESS) { |
| 182 | printf("Loading from Boot%04X '%ls' failed\n", n, |
| 183 | lo.label); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 184 | goto error; |
AKASHI Takahiro | 94e6e55 | 2019-05-29 20:54:25 +0200 | [diff] [blame] | 185 | } |
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 | attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 188 | EFI_VARIABLE_RUNTIME_ACCESS; |
| 189 | size = sizeof(n); |
| 190 | ret = EFI_CALL(efi_set_variable( |
| 191 | L"BootCurrent", |
| 192 | (efi_guid_t *)&efi_global_variable_guid, |
| 193 | attributes, size, &n)); |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 194 | if (ret != EFI_SUCCESS) { |
| 195 | if (EFI_CALL(efi_unload_image(*handle)) |
| 196 | != EFI_SUCCESS) |
| 197 | printf("Unloading image failed\n"); |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 198 | goto error; |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 199 | } |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 200 | |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 201 | printf("Booting: %ls\n", lo.label); |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 202 | } else { |
| 203 | ret = EFI_LOAD_ERROR; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | error: |
| 207 | free(load_option); |
| 208 | |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 209 | return ret; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 212 | /** |
| 213 | * efi_bootmgr_load() - try to load from BootNext or BootOrder |
| 214 | * |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 215 | * Attempt to load from BootNext or in the order specified by BootOrder |
| 216 | * EFI variable, the available load-options, finding and returning |
| 217 | * the first one that can be loaded successfully. |
Heinrich Schuchardt | 15621aa | 2019-07-14 13:20:28 +0200 | [diff] [blame] | 218 | * |
| 219 | * @handle: on return handle for the newly installed image |
| 220 | * Return: status code |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 221 | */ |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 222 | efi_status_t efi_bootmgr_load(efi_handle_t *handle) |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 223 | { |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 224 | u16 bootnext, *bootorder; |
Heinrich Schuchardt | 45c66f9 | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 225 | efi_uintn_t size; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 226 | int i, num; |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 227 | efi_status_t ret; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 228 | |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 229 | bs = systab.boottime; |
| 230 | rs = systab.runtime; |
| 231 | |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 232 | /* BootNext */ |
| 233 | bootnext = 0; |
| 234 | size = sizeof(bootnext); |
| 235 | ret = EFI_CALL(efi_get_variable(L"BootNext", |
| 236 | (efi_guid_t *)&efi_global_variable_guid, |
| 237 | NULL, &size, &bootnext)); |
| 238 | if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) { |
| 239 | /* BootNext does exist here */ |
| 240 | if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) |
| 241 | printf("BootNext must be 16-bit integer\n"); |
| 242 | |
| 243 | /* delete BootNext */ |
| 244 | ret = EFI_CALL(efi_set_variable( |
| 245 | L"BootNext", |
| 246 | (efi_guid_t *)&efi_global_variable_guid, |
AKASHI Takahiro | 366161c | 2019-06-04 15:52:09 +0900 | [diff] [blame] | 247 | EFI_VARIABLE_NON_VOLATILE, 0, |
| 248 | &bootnext)); |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 249 | |
| 250 | /* load BootNext */ |
| 251 | if (ret == EFI_SUCCESS) { |
| 252 | if (size == sizeof(u16)) { |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 253 | ret = try_load_entry(bootnext, handle); |
| 254 | if (ret == EFI_SUCCESS) |
| 255 | return ret; |
AKASHI Takahiro | 94e6e55 | 2019-05-29 20:54:25 +0200 | [diff] [blame] | 256 | printf("Loading from BootNext failed, falling back to BootOrder\n"); |
AKASHI Takahiro | 37279ad | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 257 | } |
| 258 | } else { |
| 259 | printf("Deleting BootNext failed\n"); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /* BootOrder */ |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 264 | bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size); |
Heinrich Schuchardt | 33e4497 | 2019-02-24 04:44:48 +0100 | [diff] [blame] | 265 | if (!bootorder) { |
| 266 | printf("BootOrder not defined\n"); |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 267 | ret = EFI_NOT_FOUND; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 268 | goto error; |
Heinrich Schuchardt | 33e4497 | 2019-02-24 04:44:48 +0100 | [diff] [blame] | 269 | } |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 270 | |
| 271 | num = size / sizeof(uint16_t); |
| 272 | for (i = 0; i < num; i++) { |
| 273 | debug("%s: trying to load Boot%04X\n", __func__, bootorder[i]); |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 274 | ret = try_load_entry(bootorder[i], handle); |
| 275 | if (ret == EFI_SUCCESS) |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 276 | break; |
| 277 | } |
| 278 | |
| 279 | free(bootorder); |
| 280 | |
| 281 | error: |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 282 | return ret; |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 283 | } |