blob: 2ea21448f03d195eeeaa4c8b033f41c901ad70d6 [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
8#include <common.h>
9#include <charset.h>
10#include <malloc.h>
11#include <efi_loader.h>
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090012#include <asm/unaligned.h>
Rob Clark9975fe92017-09-13 18:05:38 -040013
14static const struct efi_boot_services *bs;
15static const struct efi_runtime_services *rs;
16
Rob Clark9975fe92017-09-13 18:05:38 -040017/*
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
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020030/**
31 * efi_deserialize_load_option() - parse serialized data
32 *
33 * Parse serialized data describing a load option and transform it to the
34 * efi_load_option structure.
35 *
36 * @lo: pointer to target
37 * @data: serialized data
38 */
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090039void efi_deserialize_load_option(struct efi_load_option *lo, u8 *data)
Rob Clark9975fe92017-09-13 18:05:38 -040040{
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090041 lo->attributes = get_unaligned_le32(data);
42 data += sizeof(u32);
Rob Clark9975fe92017-09-13 18:05:38 -040043
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090044 lo->file_path_length = get_unaligned_le16(data);
45 data += sizeof(u16);
Rob Clark9975fe92017-09-13 18:05:38 -040046
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090047 /* FIXME */
48 lo->label = (u16 *)data;
49 data += (u16_strlen(lo->label) + 1) * sizeof(u16);
Rob Clark9975fe92017-09-13 18:05:38 -040050
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090051 /* FIXME */
52 lo->file_path = (struct efi_device_path *)data;
53 data += lo->file_path_length;
Rob Clark9975fe92017-09-13 18:05:38 -040054
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090055 lo->optional_data = data;
56}
57
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020058/**
59 * efi_serialize_load_option() - serialize load option
60 *
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090061 * Serialize efi_load_option structure into byte stream for BootXXXX.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020062 *
63 * @data: buffer for serialized data
64 * @lo: load option
65 * Return: size of allocated buffer
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090066 */
67unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
68{
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +020069 unsigned long label_len;
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090070 unsigned long size;
71 u8 *p;
72
73 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090074
75 /* total size */
76 size = sizeof(lo->attributes);
77 size += sizeof(lo->file_path_length);
78 size += label_len;
79 size += lo->file_path_length;
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +020080 if (lo->optional_data)
81 size += (utf8_utf16_strlen((const char *)lo->optional_data)
82 + 1) * sizeof(u16);
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090083 p = malloc(size);
84 if (!p)
85 return 0;
86
87 /* copy data */
88 *data = p;
89 memcpy(p, &lo->attributes, sizeof(lo->attributes));
90 p += sizeof(lo->attributes);
91
92 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
93 p += sizeof(lo->file_path_length);
94
95 memcpy(p, lo->label, label_len);
96 p += label_len;
97
98 memcpy(p, lo->file_path, lo->file_path_length);
99 p += lo->file_path_length;
100
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +0200101 if (lo->optional_data) {
102 utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
103 p += sizeof(u16); /* size of trailing \0 */
104 }
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900105 return size;
Rob Clark9975fe92017-09-13 18:05:38 -0400106}
107
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200108/**
109 * get_var() - get UEFI variable
110 *
111 * It is the caller's duty to free the returned buffer.
112 *
113 * @name: name of variable
114 * @vendor: vendor GUID of variable
115 * @size: size of allocated buffer
116 * Return: buffer with variable data or NULL
117 */
Rob Clark9975fe92017-09-13 18:05:38 -0400118static void *get_var(u16 *name, const efi_guid_t *vendor,
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200119 efi_uintn_t *size)
Rob Clark9975fe92017-09-13 18:05:38 -0400120{
121 efi_guid_t *v = (efi_guid_t *)vendor;
122 efi_status_t ret;
123 void *buf = NULL;
124
125 *size = 0;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200126 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
Rob Clark9975fe92017-09-13 18:05:38 -0400127 if (ret == EFI_BUFFER_TOO_SMALL) {
128 buf = malloc(*size);
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200129 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
Rob Clark9975fe92017-09-13 18:05:38 -0400130 }
131
132 if (ret != EFI_SUCCESS) {
133 free(buf);
134 *size = 0;
135 return NULL;
136 }
137
138 return buf;
139}
140
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200141/**
142 * try_load_entry() - try to load image for boot option
143 *
Rob Clark9975fe92017-09-13 18:05:38 -0400144 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200145 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clark9975fe92017-09-13 18:05:38 -0400146 * and that the specified file to boot exists.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200147 *
148 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
149 * @handle: on return handle for the newly installed image
150 * Return: status code
Rob Clark9975fe92017-09-13 18:05:38 -0400151 */
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900152static efi_status_t try_load_entry(u16 n, efi_handle_t *handle)
Rob Clark9975fe92017-09-13 18:05:38 -0400153{
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900154 struct efi_load_option lo;
Rob Clark9975fe92017-09-13 18:05:38 -0400155 u16 varname[] = L"Boot0000";
156 u16 hexmap[] = L"0123456789ABCDEF";
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900157 void *load_option;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200158 efi_uintn_t size;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900159 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400160
161 varname[4] = hexmap[(n & 0xf000) >> 12];
162 varname[5] = hexmap[(n & 0x0f00) >> 8];
163 varname[6] = hexmap[(n & 0x00f0) >> 4];
164 varname[7] = hexmap[(n & 0x000f) >> 0];
165
166 load_option = get_var(varname, &efi_global_variable_guid, &size);
167 if (!load_option)
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900168 return EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400169
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900170 efi_deserialize_load_option(&lo, load_option);
Rob Clark9975fe92017-09-13 18:05:38 -0400171
172 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900173 u32 attributes;
Rob Clark9975fe92017-09-13 18:05:38 -0400174
Heinrich Schuchardt4a8b5e72018-01-26 07:20:54 +0100175 debug("%s: trying to load \"%ls\" from %pD\n",
176 __func__, lo.label, lo.file_path);
Rob Clark9975fe92017-09-13 18:05:38 -0400177
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900178 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
179 NULL, 0, handle));
AKASHI Takahiro94e6e552019-05-29 20:54:25 +0200180 if (ret != EFI_SUCCESS) {
181 printf("Loading from Boot%04X '%ls' failed\n", n,
182 lo.label);
Rob Clark9975fe92017-09-13 18:05:38 -0400183 goto error;
AKASHI Takahiro94e6e552019-05-29 20:54:25 +0200184 }
Rob Clark9975fe92017-09-13 18:05:38 -0400185
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900186 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
187 EFI_VARIABLE_RUNTIME_ACCESS;
188 size = sizeof(n);
189 ret = EFI_CALL(efi_set_variable(
190 L"BootCurrent",
191 (efi_guid_t *)&efi_global_variable_guid,
192 attributes, size, &n));
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900193 if (ret != EFI_SUCCESS) {
194 if (EFI_CALL(efi_unload_image(*handle))
195 != EFI_SUCCESS)
196 printf("Unloading image failed\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900197 goto error;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900198 }
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900199
Rob Clark9975fe92017-09-13 18:05:38 -0400200 printf("Booting: %ls\n", lo.label);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900201 } else {
202 ret = EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400203 }
204
205error:
206 free(load_option);
207
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900208 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400209}
210
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200211/**
212 * efi_bootmgr_load() - try to load from BootNext or BootOrder
213 *
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900214 * Attempt to load from BootNext or in the order specified by BootOrder
215 * EFI variable, the available load-options, finding and returning
216 * the first one that can be loaded successfully.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200217 *
218 * @handle: on return handle for the newly installed image
219 * Return: status code
Rob Clark9975fe92017-09-13 18:05:38 -0400220 */
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900221efi_status_t efi_bootmgr_load(efi_handle_t *handle)
Rob Clark9975fe92017-09-13 18:05:38 -0400222{
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900223 u16 bootnext, *bootorder;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200224 efi_uintn_t size;
Rob Clark9975fe92017-09-13 18:05:38 -0400225 int i, num;
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900226 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400227
Rob Clark9975fe92017-09-13 18:05:38 -0400228 bs = systab.boottime;
229 rs = systab.runtime;
230
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900231 /* BootNext */
232 bootnext = 0;
233 size = sizeof(bootnext);
234 ret = EFI_CALL(efi_get_variable(L"BootNext",
235 (efi_guid_t *)&efi_global_variable_guid,
236 NULL, &size, &bootnext));
237 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
238 /* BootNext does exist here */
239 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
240 printf("BootNext must be 16-bit integer\n");
241
242 /* delete BootNext */
243 ret = EFI_CALL(efi_set_variable(
244 L"BootNext",
245 (efi_guid_t *)&efi_global_variable_guid,
AKASHI Takahiro366161c2019-06-04 15:52:09 +0900246 EFI_VARIABLE_NON_VOLATILE, 0,
247 &bootnext));
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900248
249 /* load BootNext */
250 if (ret == EFI_SUCCESS) {
251 if (size == sizeof(u16)) {
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900252 ret = try_load_entry(bootnext, handle);
253 if (ret == EFI_SUCCESS)
254 return ret;
AKASHI Takahiro94e6e552019-05-29 20:54:25 +0200255 printf("Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900256 }
257 } else {
258 printf("Deleting BootNext failed\n");
259 }
260 }
261
262 /* BootOrder */
Rob Clark9975fe92017-09-13 18:05:38 -0400263 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100264 if (!bootorder) {
265 printf("BootOrder not defined\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900266 ret = EFI_NOT_FOUND;
Rob Clark9975fe92017-09-13 18:05:38 -0400267 goto error;
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100268 }
Rob Clark9975fe92017-09-13 18:05:38 -0400269
270 num = size / sizeof(uint16_t);
271 for (i = 0; i < num; i++) {
272 debug("%s: trying to load Boot%04X\n", __func__, bootorder[i]);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900273 ret = try_load_entry(bootorder[i], handle);
274 if (ret == EFI_SUCCESS)
Rob Clark9975fe92017-09-13 18:05:38 -0400275 break;
276 }
277
278 free(bootorder);
279
280error:
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900281 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400282}