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