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