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 | |
AKASHI Takahiro | bd23774 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 30 | /* Parse serialized data and transform it into efi_load_option structure */ |
| 31 | void efi_deserialize_load_option(struct efi_load_option *lo, u8 *data) |
| 32 | { |
| 33 | lo->attributes = get_unaligned_le32(data); |
| 34 | data += sizeof(u32); |
| 35 | |
| 36 | lo->file_path_length = get_unaligned_le16(data); |
| 37 | data += sizeof(u16); |
| 38 | |
| 39 | /* FIXME */ |
| 40 | lo->label = (u16 *)data; |
| 41 | data += (u16_strlen(lo->label) + 1) * sizeof(u16); |
| 42 | |
| 43 | /* FIXME */ |
| 44 | lo->file_path = (struct efi_device_path *)data; |
| 45 | data += lo->file_path_length; |
| 46 | |
| 47 | lo->optional_data = data; |
| 48 | } |
| 49 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 50 | /* |
AKASHI Takahiro | bd23774 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 51 | * Serialize efi_load_option structure into byte stream for BootXXXX. |
| 52 | * Return a size of allocated data. |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 53 | */ |
AKASHI Takahiro | bd23774 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 54 | 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] | 55 | { |
AKASHI Takahiro | bd23774 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 56 | unsigned long label_len, option_len; |
| 57 | unsigned long size; |
| 58 | u8 *p; |
| 59 | |
| 60 | label_len = (u16_strlen(lo->label) + 1) * sizeof(u16); |
| 61 | option_len = strlen((char *)lo->optional_data); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 62 | |
AKASHI Takahiro | bd23774 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 63 | /* total size */ |
| 64 | size = sizeof(lo->attributes); |
| 65 | size += sizeof(lo->file_path_length); |
| 66 | size += label_len; |
| 67 | size += lo->file_path_length; |
| 68 | size += option_len + 1; |
| 69 | p = malloc(size); |
| 70 | if (!p) |
| 71 | return 0; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 72 | |
AKASHI Takahiro | bd23774 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 73 | /* copy data */ |
| 74 | *data = p; |
| 75 | memcpy(p, &lo->attributes, sizeof(lo->attributes)); |
| 76 | p += sizeof(lo->attributes); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 77 | |
AKASHI Takahiro | bd23774 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 78 | memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length)); |
| 79 | p += sizeof(lo->file_path_length); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 80 | |
AKASHI Takahiro | bd23774 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 81 | memcpy(p, lo->label, label_len); |
| 82 | p += label_len; |
| 83 | |
| 84 | memcpy(p, lo->file_path, lo->file_path_length); |
| 85 | p += lo->file_path_length; |
| 86 | |
| 87 | memcpy(p, lo->optional_data, option_len); |
| 88 | p += option_len; |
| 89 | *(char *)p = '\0'; |
| 90 | |
| 91 | return size; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* free() the result */ |
| 95 | static void *get_var(u16 *name, const efi_guid_t *vendor, |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 96 | efi_uintn_t *size) |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 97 | { |
| 98 | efi_guid_t *v = (efi_guid_t *)vendor; |
| 99 | efi_status_t ret; |
| 100 | void *buf = NULL; |
| 101 | |
| 102 | *size = 0; |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 103 | EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf)); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 104 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 105 | buf = malloc(*size); |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 106 | EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf)); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (ret != EFI_SUCCESS) { |
| 110 | free(buf); |
| 111 | *size = 0; |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | return buf; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Attempt to load load-option number 'n', returning device_path and file_path |
| 120 | * if successful. This checks that the EFI_LOAD_OPTION is active (enabled) |
| 121 | * and that the specified file to boot exists. |
| 122 | */ |
| 123 | static void *try_load_entry(uint16_t n, struct efi_device_path **device_path, |
| 124 | struct efi_device_path **file_path) |
| 125 | { |
AKASHI Takahiro | bd23774 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 126 | struct efi_load_option lo; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 127 | u16 varname[] = L"Boot0000"; |
| 128 | u16 hexmap[] = L"0123456789ABCDEF"; |
| 129 | void *load_option, *image = NULL; |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 130 | efi_uintn_t size; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 131 | |
| 132 | varname[4] = hexmap[(n & 0xf000) >> 12]; |
| 133 | varname[5] = hexmap[(n & 0x0f00) >> 8]; |
| 134 | varname[6] = hexmap[(n & 0x00f0) >> 4]; |
| 135 | varname[7] = hexmap[(n & 0x000f) >> 0]; |
| 136 | |
| 137 | load_option = get_var(varname, &efi_global_variable_guid, &size); |
| 138 | if (!load_option) |
| 139 | return NULL; |
| 140 | |
AKASHI Takahiro | bd23774 | 2018-11-05 18:06:41 +0900 | [diff] [blame] | 141 | efi_deserialize_load_option(&lo, load_option); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 142 | |
| 143 | if (lo.attributes & LOAD_OPTION_ACTIVE) { |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 144 | u32 attributes; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 145 | efi_status_t ret; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 146 | |
Heinrich Schuchardt | 253aa64 | 2018-01-26 07:20:54 +0100 | [diff] [blame] | 147 | debug("%s: trying to load \"%ls\" from %pD\n", |
| 148 | __func__, lo.label, lo.file_path); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 149 | |
Heinrich Schuchardt | e3d3a0c | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 150 | ret = efi_load_image_from_path(lo.file_path, &image, &size); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 151 | |
| 152 | if (ret != EFI_SUCCESS) |
| 153 | goto error; |
| 154 | |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 155 | attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 156 | EFI_VARIABLE_RUNTIME_ACCESS; |
| 157 | size = sizeof(n); |
| 158 | ret = EFI_CALL(efi_set_variable( |
| 159 | L"BootCurrent", |
| 160 | (efi_guid_t *)&efi_global_variable_guid, |
| 161 | attributes, size, &n)); |
| 162 | if (ret != EFI_SUCCESS) |
| 163 | goto error; |
| 164 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 165 | printf("Booting: %ls\n", lo.label); |
| 166 | efi_dp_split_file_path(lo.file_path, device_path, file_path); |
| 167 | } |
| 168 | |
| 169 | error: |
| 170 | free(load_option); |
| 171 | |
| 172 | return image; |
| 173 | } |
| 174 | |
| 175 | /* |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 176 | * Attempt to load from BootNext or in the order specified by BootOrder |
| 177 | * EFI variable, the available load-options, finding and returning |
| 178 | * the first one that can be loaded successfully. |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 179 | */ |
| 180 | void *efi_bootmgr_load(struct efi_device_path **device_path, |
| 181 | struct efi_device_path **file_path) |
| 182 | { |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 183 | u16 bootnext, *bootorder; |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 184 | efi_uintn_t size; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 185 | void *image = NULL; |
| 186 | int i, num; |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 187 | efi_status_t ret; |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 188 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 189 | bs = systab.boottime; |
| 190 | rs = systab.runtime; |
| 191 | |
AKASHI Takahiro | e6577f9 | 2019-03-20 09:07:55 +0900 | [diff] [blame] | 192 | /* BootNext */ |
| 193 | bootnext = 0; |
| 194 | size = sizeof(bootnext); |
| 195 | ret = EFI_CALL(efi_get_variable(L"BootNext", |
| 196 | (efi_guid_t *)&efi_global_variable_guid, |
| 197 | NULL, &size, &bootnext)); |
| 198 | if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) { |
| 199 | /* BootNext does exist here */ |
| 200 | if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) |
| 201 | printf("BootNext must be 16-bit integer\n"); |
| 202 | |
| 203 | /* delete BootNext */ |
| 204 | ret = EFI_CALL(efi_set_variable( |
| 205 | L"BootNext", |
| 206 | (efi_guid_t *)&efi_global_variable_guid, |
| 207 | 0, 0, &bootnext)); |
| 208 | |
| 209 | /* load BootNext */ |
| 210 | if (ret == EFI_SUCCESS) { |
| 211 | if (size == sizeof(u16)) { |
| 212 | image = try_load_entry(bootnext, device_path, |
| 213 | file_path); |
| 214 | if (image) |
| 215 | return image; |
| 216 | } |
| 217 | } else { |
| 218 | printf("Deleting BootNext failed\n"); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /* BootOrder */ |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 223 | bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size); |
Heinrich Schuchardt | 3d2257f | 2019-02-24 04:44:48 +0100 | [diff] [blame] | 224 | if (!bootorder) { |
| 225 | printf("BootOrder not defined\n"); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 226 | goto error; |
Heinrich Schuchardt | 3d2257f | 2019-02-24 04:44:48 +0100 | [diff] [blame] | 227 | } |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 228 | |
| 229 | num = size / sizeof(uint16_t); |
| 230 | for (i = 0; i < num; i++) { |
| 231 | debug("%s: trying to load Boot%04X\n", __func__, bootorder[i]); |
| 232 | image = try_load_entry(bootorder[i], device_path, file_path); |
| 233 | if (image) |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | free(bootorder); |
| 238 | |
| 239 | error: |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 240 | return image; |
| 241 | } |