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