blob: 46c8011344b9aae30a8627ffe5032ffeeb4c8906 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clark9975fe92017-09-13 18:05:38 -04002/*
Heinrich Schuchardte1fec152018-10-18 21:51:38 +02003 * EFI boot manager
Rob Clark9975fe92017-09-13 18:05:38 -04004 *
5 * Copyright (c) 2017 Rob Clark
Rob Clark9975fe92017-09-13 18:05:38 -04006 */
7
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +02008#define LOG_CATEGORY LOGC_EFI
9
Rob Clark9975fe92017-09-13 18:05:38 -040010#include <common.h>
11#include <charset.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Rob Clark9975fe92017-09-13 18:05:38 -040013#include <malloc.h>
14#include <efi_loader.h>
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +020015#include <efi_variable.h>
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090016#include <asm/unaligned.h>
Rob Clark9975fe92017-09-13 18:05:38 -040017
18static const struct efi_boot_services *bs;
19static const struct efi_runtime_services *rs;
20
Rob Clark9975fe92017-09-13 18:05:38 -040021/*
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 Schuchardt1064d042020-08-07 17:47:13 +020033/**
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020034 * get_var() - get UEFI variable
35 *
36 * It is the caller's duty to free the returned buffer.
37 *
38 * @name: name of variable
39 * @vendor: vendor GUID of variable
40 * @size: size of allocated buffer
41 * Return: buffer with variable data or NULL
42 */
Rob Clark9975fe92017-09-13 18:05:38 -040043static void *get_var(u16 *name, const efi_guid_t *vendor,
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +020044 efi_uintn_t *size)
Rob Clark9975fe92017-09-13 18:05:38 -040045{
Rob Clark9975fe92017-09-13 18:05:38 -040046 efi_status_t ret;
47 void *buf = NULL;
48
49 *size = 0;
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +020050 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
Rob Clark9975fe92017-09-13 18:05:38 -040051 if (ret == EFI_BUFFER_TOO_SMALL) {
52 buf = malloc(*size);
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +020053 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
Rob Clark9975fe92017-09-13 18:05:38 -040054 }
55
56 if (ret != EFI_SUCCESS) {
57 free(buf);
58 *size = 0;
59 return NULL;
60 }
61
62 return buf;
63}
64
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020065/**
66 * try_load_entry() - try to load image for boot option
67 *
Rob Clark9975fe92017-09-13 18:05:38 -040068 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020069 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clark9975fe92017-09-13 18:05:38 -040070 * and that the specified file to boot exists.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020071 *
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +020072 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
73 * @handle: on return handle for the newly installed image
74 * @load_options: load options set on the loaded image protocol
75 * Return: status code
Rob Clark9975fe92017-09-13 18:05:38 -040076 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +020077static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
78 void **load_options)
Rob Clark9975fe92017-09-13 18:05:38 -040079{
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090080 struct efi_load_option lo;
Rob Clark9975fe92017-09-13 18:05:38 -040081 u16 varname[] = L"Boot0000";
82 u16 hexmap[] = L"0123456789ABCDEF";
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090083 void *load_option;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +020084 efi_uintn_t size;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090085 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -040086
87 varname[4] = hexmap[(n & 0xf000) >> 12];
88 varname[5] = hexmap[(n & 0x0f00) >> 8];
89 varname[6] = hexmap[(n & 0x00f0) >> 4];
90 varname[7] = hexmap[(n & 0x000f) >> 0];
91
92 load_option = get_var(varname, &efi_global_variable_guid, &size);
93 if (!load_option)
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090094 return EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -040095
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +020096 ret = efi_deserialize_load_option(&lo, load_option, &size);
97 if (ret != EFI_SUCCESS) {
98 log_warning("Invalid load option for %ls\n", varname);
99 goto error;
100 }
Rob Clark9975fe92017-09-13 18:05:38 -0400101
102 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900103 u32 attributes;
Rob Clark9975fe92017-09-13 18:05:38 -0400104
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200105 log_debug("%s: trying to load \"%ls\" from %pD\n",
106 __func__, lo.label, lo.file_path);
Rob Clark9975fe92017-09-13 18:05:38 -0400107
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900108 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
109 NULL, 0, handle));
AKASHI Takahiro94e6e552019-05-29 20:54:25 +0200110 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200111 log_warning("Loading %ls '%ls' failed\n",
112 varname, lo.label);
Rob Clark9975fe92017-09-13 18:05:38 -0400113 goto error;
AKASHI Takahiro94e6e552019-05-29 20:54:25 +0200114 }
Rob Clark9975fe92017-09-13 18:05:38 -0400115
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900116 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
117 EFI_VARIABLE_RUNTIME_ACCESS;
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200118 ret = efi_set_variable_int(L"BootCurrent",
119 &efi_global_variable_guid,
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200120 attributes, sizeof(n), &n, false);
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200121 if (ret != EFI_SUCCESS)
122 goto unload;
123 /* try to register load file2 for initrd's */
124 if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) {
125 ret = efi_initrd_register();
126 if (ret != EFI_SUCCESS)
127 goto unload;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900128 }
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900129
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200130 log_info("Booting: %ls\n", lo.label);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900131 } else {
132 ret = EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400133 }
134
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200135 /* Set load options */
136 if (size) {
137 *load_options = malloc(size);
138 if (!*load_options) {
139 ret = EFI_OUT_OF_RESOURCES;
140 goto error;
141 }
142 memcpy(*load_options, lo.optional_data, size);
143 ret = efi_set_load_options(*handle, size, *load_options);
144 } else {
Heinrich Schuchardte4343112020-12-27 15:46:00 +0100145 *load_options = NULL;
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200146 }
147
Rob Clark9975fe92017-09-13 18:05:38 -0400148error:
149 free(load_option);
150
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900151 return ret;
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200152
153unload:
154 if (EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
155 log_err("Unloading image failed\n");
156 free(load_option);
157
158 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400159}
160
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200161/**
162 * efi_bootmgr_load() - try to load from BootNext or BootOrder
163 *
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900164 * Attempt to load from BootNext or in the order specified by BootOrder
165 * EFI variable, the available load-options, finding and returning
166 * the first one that can be loaded successfully.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200167 *
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200168 * @handle: on return handle for the newly installed image
169 * @load_options: load options set on the loaded image protocol
170 * Return: status code
Rob Clark9975fe92017-09-13 18:05:38 -0400171 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200172efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clark9975fe92017-09-13 18:05:38 -0400173{
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900174 u16 bootnext, *bootorder;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200175 efi_uintn_t size;
Rob Clark9975fe92017-09-13 18:05:38 -0400176 int i, num;
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900177 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400178
Rob Clark9975fe92017-09-13 18:05:38 -0400179 bs = systab.boottime;
180 rs = systab.runtime;
181
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900182 /* BootNext */
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900183 size = sizeof(bootnext);
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200184 ret = efi_get_variable_int(L"BootNext",
185 &efi_global_variable_guid,
186 NULL, &size, &bootnext, NULL);
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900187 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
188 /* BootNext does exist here */
189 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200190 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900191
192 /* delete BootNext */
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200193 ret = efi_set_variable_int(L"BootNext",
194 &efi_global_variable_guid,
195 0, 0, NULL, false);
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900196
197 /* load BootNext */
198 if (ret == EFI_SUCCESS) {
199 if (size == sizeof(u16)) {
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200200 ret = try_load_entry(bootnext, handle,
201 load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900202 if (ret == EFI_SUCCESS)
203 return ret;
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200204 log_warning(
205 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900206 }
207 } else {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200208 log_err("Deleting BootNext failed\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900209 }
210 }
211
212 /* BootOrder */
Rob Clark9975fe92017-09-13 18:05:38 -0400213 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100214 if (!bootorder) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200215 log_info("BootOrder not defined\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900216 ret = EFI_NOT_FOUND;
Rob Clark9975fe92017-09-13 18:05:38 -0400217 goto error;
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100218 }
Rob Clark9975fe92017-09-13 18:05:38 -0400219
220 num = size / sizeof(uint16_t);
221 for (i = 0; i < num; i++) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200222 log_debug("%s trying to load Boot%04X\n", __func__,
223 bootorder[i]);
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200224 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900225 if (ret == EFI_SUCCESS)
Rob Clark9975fe92017-09-13 18:05:38 -0400226 break;
227 }
228
229 free(bootorder);
230
231error:
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900232 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400233}