blob: 25f5cebfdb67510f3b15713a4938cc6fd83db41d [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clarkc84c1102017-09-13 18:05:38 -04002/*
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02003 * EFI boot manager
Rob Clarkc84c1102017-09-13 18:05:38 -04004 *
5 * Copyright (c) 2017 Rob Clark
Rob Clarkc84c1102017-09-13 18:05:38 -04006 */
7
Heinrich Schuchardt273df962020-05-31 10:07:31 +02008#define LOG_CATEGORY LOGC_EFI
9
Rob Clarkc84c1102017-09-13 18:05:38 -040010#include <common.h>
11#include <charset.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040013#include <malloc.h>
14#include <efi_loader.h>
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +020015#include <efi_variable.h>
AKASHI Takahirobd237742018-11-05 18:06:41 +090016#include <asm/unaligned.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040017
18static const struct efi_boot_services *bs;
19static const struct efi_runtime_services *rs;
20
Rob Clarkc84c1102017-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 Schuchardt25c6be52020-08-07 17:47:13 +020033/**
Heinrich Schuchardte612ba62019-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 Clarkc84c1102017-09-13 18:05:38 -040043static void *get_var(u16 *name, const efi_guid_t *vendor,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +020044 efi_uintn_t *size)
Rob Clarkc84c1102017-09-13 18:05:38 -040045{
Rob Clarkc84c1102017-09-13 18:05:38 -040046 efi_status_t ret;
47 void *buf = NULL;
48
49 *size = 0;
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +020050 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
Rob Clarkc84c1102017-09-13 18:05:38 -040051 if (ret == EFI_BUFFER_TOO_SMALL) {
52 buf = malloc(*size);
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +020053 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
Rob Clarkc84c1102017-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 Schuchardte612ba62019-07-14 13:20:28 +020065/**
66 * try_load_entry() - try to load image for boot option
67 *
Rob Clarkc84c1102017-09-13 18:05:38 -040068 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardte612ba62019-07-14 13:20:28 +020069 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clarkc84c1102017-09-13 18:05:38 -040070 * and that the specified file to boot exists.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +020071 *
Heinrich Schuchardta7647a72020-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 Clarkc84c1102017-09-13 18:05:38 -040076 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +020077static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
78 void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -040079{
AKASHI Takahirobd237742018-11-05 18:06:41 +090080 struct efi_load_option lo;
Rob Clarkc84c1102017-09-13 18:05:38 -040081 u16 varname[] = L"Boot0000";
82 u16 hexmap[] = L"0123456789ABCDEF";
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +090083 void *load_option;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +020084 efi_uintn_t size;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +090085 efi_status_t ret;
Rob Clarkc84c1102017-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 Takahiro14ff23b2019-04-19 12:22:35 +090094 return EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -040095
Heinrich Schuchardt7ca84f82020-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 Clarkc84c1102017-09-13 18:05:38 -0400101
102 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900103 u32 attributes;
Rob Clarkc84c1102017-09-13 18:05:38 -0400104
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200105 log_debug("%s: trying to load \"%ls\" from %pD\n",
106 __func__, lo.label, lo.file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -0400107
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900108 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
109 NULL, 0, handle));
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200110 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200111 log_warning("Loading %ls '%ls' failed\n",
112 varname, lo.label);
Rob Clarkc84c1102017-09-13 18:05:38 -0400113 goto error;
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +0200114 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400115
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900116 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
117 EFI_VARIABLE_RUNTIME_ACCESS;
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200118 ret = efi_set_variable_int(L"BootCurrent",
119 &efi_global_variable_guid,
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200120 attributes, sizeof(n), &n, false);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900121 if (ret != EFI_SUCCESS) {
122 if (EFI_CALL(efi_unload_image(*handle))
123 != EFI_SUCCESS)
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200124 log_err("Unloading image failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900125 goto error;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900126 }
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900127
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200128 log_info("Booting: %ls\n", lo.label);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900129 } else {
130 ret = EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400131 }
132
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200133 /* Set load options */
134 if (size) {
135 *load_options = malloc(size);
136 if (!*load_options) {
137 ret = EFI_OUT_OF_RESOURCES;
138 goto error;
139 }
140 memcpy(*load_options, lo.optional_data, size);
141 ret = efi_set_load_options(*handle, size, *load_options);
142 } else {
Heinrich Schuchardt7c2a8592020-12-27 15:46:00 +0100143 *load_options = NULL;
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200144 }
145
Rob Clarkc84c1102017-09-13 18:05:38 -0400146error:
147 free(load_option);
148
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900149 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400150}
151
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200152/**
153 * efi_bootmgr_load() - try to load from BootNext or BootOrder
154 *
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900155 * Attempt to load from BootNext or in the order specified by BootOrder
156 * EFI variable, the available load-options, finding and returning
157 * the first one that can be loaded successfully.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200158 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200159 * @handle: on return handle for the newly installed image
160 * @load_options: load options set on the loaded image protocol
161 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400162 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200163efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400164{
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900165 u16 bootnext, *bootorder;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200166 efi_uintn_t size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400167 int i, num;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900168 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400169
Rob Clarkc84c1102017-09-13 18:05:38 -0400170 bs = systab.boottime;
171 rs = systab.runtime;
172
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900173 /* BootNext */
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900174 size = sizeof(bootnext);
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200175 ret = efi_get_variable_int(L"BootNext",
176 &efi_global_variable_guid,
177 NULL, &size, &bootnext, NULL);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900178 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
179 /* BootNext does exist here */
180 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200181 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900182
183 /* delete BootNext */
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200184 ret = efi_set_variable_int(L"BootNext",
185 &efi_global_variable_guid,
186 0, 0, NULL, false);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900187
188 /* load BootNext */
189 if (ret == EFI_SUCCESS) {
190 if (size == sizeof(u16)) {
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200191 ret = try_load_entry(bootnext, handle,
192 load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900193 if (ret == EFI_SUCCESS)
194 return ret;
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200195 log_warning(
196 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900197 }
198 } else {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200199 log_err("Deleting BootNext failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900200 }
201 }
202
203 /* BootOrder */
Rob Clarkc84c1102017-09-13 18:05:38 -0400204 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100205 if (!bootorder) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200206 log_info("BootOrder not defined\n");
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900207 ret = EFI_NOT_FOUND;
Rob Clarkc84c1102017-09-13 18:05:38 -0400208 goto error;
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100209 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400210
211 num = size / sizeof(uint16_t);
212 for (i = 0; i < num; i++) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200213 log_debug("%s trying to load Boot%04X\n", __func__,
214 bootorder[i]);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200215 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900216 if (ret == EFI_SUCCESS)
Rob Clarkc84c1102017-09-13 18:05:38 -0400217 break;
218 }
219
220 free(bootorder);
221
222error:
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900223 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400224}