blob: 7bf51874c1c12443ac858ecbbb96e275be3da402 [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
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090030/* Parse serialized data and transform it into efi_load_option structure */
31void efi_deserialize_load_option(struct efi_load_option *lo, u8 *data)
Rob Clark9975fe92017-09-13 18:05:38 -040032{
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090033 lo->attributes = get_unaligned_le32(data);
34 data += sizeof(u32);
Rob Clark9975fe92017-09-13 18:05:38 -040035
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090036 lo->file_path_length = get_unaligned_le16(data);
37 data += sizeof(u16);
Rob Clark9975fe92017-09-13 18:05:38 -040038
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090039 /* FIXME */
40 lo->label = (u16 *)data;
41 data += (u16_strlen(lo->label) + 1) * sizeof(u16);
Rob Clark9975fe92017-09-13 18:05:38 -040042
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090043 /* FIXME */
44 lo->file_path = (struct efi_device_path *)data;
45 data += lo->file_path_length;
Rob Clark9975fe92017-09-13 18:05:38 -040046
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090047 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 */
54unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
55{
Heinrich Schuchardt39a1ff82019-04-29 13:51:45 +020056 unsigned long label_len;
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090057 unsigned long size;
58 u8 *p;
59
60 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090061
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 Schuchardt39a1ff82019-04-29 13:51:45 +020067 if (lo->optional_data)
68 size += (utf8_utf16_strlen((const char *)lo->optional_data)
69 + 1) * sizeof(u16);
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090070 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 Schuchardt39a1ff82019-04-29 13:51:45 +020088 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 Takahiro1a82b342018-11-05 18:06:41 +090092 return size;
Rob Clark9975fe92017-09-13 18:05:38 -040093}
94
95/* free() the result */
96static void *get_var(u16 *name, const efi_guid_t *vendor,
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +020097 efi_uintn_t *size)
Rob Clark9975fe92017-09-13 18:05:38 -040098{
99 efi_guid_t *v = (efi_guid_t *)vendor;
100 efi_status_t ret;
101 void *buf = NULL;
102
103 *size = 0;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200104 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
Rob Clark9975fe92017-09-13 18:05:38 -0400105 if (ret == EFI_BUFFER_TOO_SMALL) {
106 buf = malloc(*size);
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200107 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
Rob Clark9975fe92017-09-13 18:05:38 -0400108 }
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 Takahiro6b95b382019-04-19 12:22:35 +0900124static efi_status_t try_load_entry(u16 n, efi_handle_t *handle)
Rob Clark9975fe92017-09-13 18:05:38 -0400125{
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900126 struct efi_load_option lo;
Rob Clark9975fe92017-09-13 18:05:38 -0400127 u16 varname[] = L"Boot0000";
128 u16 hexmap[] = L"0123456789ABCDEF";
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900129 void *load_option;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200130 efi_uintn_t size;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900131 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400132
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 Takahiro6b95b382019-04-19 12:22:35 +0900140 return EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400141
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900142 efi_deserialize_load_option(&lo, load_option);
Rob Clark9975fe92017-09-13 18:05:38 -0400143
144 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900145 u32 attributes;
Rob Clark9975fe92017-09-13 18:05:38 -0400146
Heinrich Schuchardt4a8b5e72018-01-26 07:20:54 +0100147 debug("%s: trying to load \"%ls\" from %pD\n",
148 __func__, lo.label, lo.file_path);
Rob Clark9975fe92017-09-13 18:05:38 -0400149
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900150 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
151 NULL, 0, handle));
Rob Clark9975fe92017-09-13 18:05:38 -0400152 if (ret != EFI_SUCCESS)
153 goto error;
154
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900155 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
156 EFI_VARIABLE_RUNTIME_ACCESS;
157 size = sizeof(n);
158 ret = EFI_CALL(efi_set_variable(
159 L"BootCurrent",
160 (efi_guid_t *)&efi_global_variable_guid,
161 attributes, size, &n));
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900162 if (ret != EFI_SUCCESS) {
163 if (EFI_CALL(efi_unload_image(*handle))
164 != EFI_SUCCESS)
165 printf("Unloading image failed\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900166 goto error;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900167 }
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900168
Rob Clark9975fe92017-09-13 18:05:38 -0400169 printf("Booting: %ls\n", lo.label);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900170 } else {
171 ret = EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -0400172 }
173
174error:
175 free(load_option);
176
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900177 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400178}
179
180/*
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900181 * Attempt to load from BootNext or in the order specified by BootOrder
182 * EFI variable, the available load-options, finding and returning
183 * the first one that can be loaded successfully.
Rob Clark9975fe92017-09-13 18:05:38 -0400184 */
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900185efi_status_t efi_bootmgr_load(efi_handle_t *handle)
Rob Clark9975fe92017-09-13 18:05:38 -0400186{
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900187 u16 bootnext, *bootorder;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200188 efi_uintn_t size;
Rob Clark9975fe92017-09-13 18:05:38 -0400189 int i, num;
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900190 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400191
Rob Clark9975fe92017-09-13 18:05:38 -0400192 bs = systab.boottime;
193 rs = systab.runtime;
194
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900195 /* BootNext */
196 bootnext = 0;
197 size = sizeof(bootnext);
198 ret = EFI_CALL(efi_get_variable(L"BootNext",
199 (efi_guid_t *)&efi_global_variable_guid,
200 NULL, &size, &bootnext));
201 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
202 /* BootNext does exist here */
203 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
204 printf("BootNext must be 16-bit integer\n");
205
206 /* delete BootNext */
207 ret = EFI_CALL(efi_set_variable(
208 L"BootNext",
209 (efi_guid_t *)&efi_global_variable_guid,
210 0, 0, &bootnext));
211
212 /* load BootNext */
213 if (ret == EFI_SUCCESS) {
214 if (size == sizeof(u16)) {
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900215 ret = try_load_entry(bootnext, handle);
216 if (ret == EFI_SUCCESS)
217 return ret;
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900218 }
219 } else {
220 printf("Deleting BootNext failed\n");
221 }
222 }
223
224 /* BootOrder */
Rob Clark9975fe92017-09-13 18:05:38 -0400225 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100226 if (!bootorder) {
227 printf("BootOrder not defined\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900228 ret = EFI_NOT_FOUND;
Rob Clark9975fe92017-09-13 18:05:38 -0400229 goto error;
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100230 }
Rob Clark9975fe92017-09-13 18:05:38 -0400231
232 num = size / sizeof(uint16_t);
233 for (i = 0; i < num; i++) {
234 debug("%s: trying to load Boot%04X\n", __func__, bootorder[i]);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900235 ret = try_load_entry(bootorder[i], handle);
236 if (ret == EFI_SUCCESS)
Rob Clark9975fe92017-09-13 18:05:38 -0400237 break;
238 }
239
240 free(bootorder);
241
242error:
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900243 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400244}