Merge tag 'u-boot-imx-20200716' of https://gitlab.denx.de/u-boot/custodians/u-boot-imx
[platform/kernel/u-boot.git] / lib / efi_loader / efi_bootmgr.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI boot manager
4  *
5  *  Copyright (c) 2017 Rob Clark
6  */
7
8 #define LOG_CATEGORY LOGC_EFI
9
10 #include <common.h>
11 #include <charset.h>
12 #include <log.h>
13 #include <malloc.h>
14 #include <efi_loader.h>
15 #include <efi_variable.h>
16 #include <asm/unaligned.h>
17
18 static const struct efi_boot_services *bs;
19 static const struct efi_runtime_services *rs;
20
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
33
34 /**
35  * efi_deserialize_load_option() - parse serialized data
36  *
37  * Parse serialized data describing a load option and transform it to the
38  * efi_load_option structure.
39  *
40  * @lo:         pointer to target
41  * @data:       serialized data
42  * @size:       size of the load option, on return size of the optional data
43  * Return:      status code
44  */
45 efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
46                                          efi_uintn_t *size)
47 {
48         efi_uintn_t len;
49
50         len = sizeof(u32);
51         if (*size < len + 2 * sizeof(u16))
52                 return EFI_INVALID_PARAMETER;
53         lo->attributes = get_unaligned_le32(data);
54         data += len;
55         *size -= len;
56
57         len = sizeof(u16);
58         lo->file_path_length = get_unaligned_le16(data);
59         data += len;
60         *size -= len;
61
62         lo->label = (u16 *)data;
63         len = u16_strnlen(lo->label, *size / sizeof(u16) - 1);
64         if (lo->label[len])
65                 return EFI_INVALID_PARAMETER;
66         len = (len + 1) * sizeof(u16);
67         if (*size < len)
68                 return EFI_INVALID_PARAMETER;
69         data += len;
70         *size -= len;
71
72         len = lo->file_path_length;
73         if (*size < len)
74                 return EFI_INVALID_PARAMETER;
75         lo->file_path = (struct efi_device_path *)data;
76          /*
77           * TODO: validate device path. There should be an end node within
78           * the indicated file_path_length.
79           */
80         data += len;
81         *size -= len;
82
83         lo->optional_data = data;
84
85         return EFI_SUCCESS;
86 }
87
88 /**
89  * efi_serialize_load_option() - serialize load option
90  *
91  * Serialize efi_load_option structure into byte stream for BootXXXX.
92  *
93  * @data:       buffer for serialized data
94  * @lo:         load option
95  * Return:      size of allocated buffer
96  */
97 unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
98 {
99         unsigned long label_len;
100         unsigned long size;
101         u8 *p;
102
103         label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
104
105         /* total size */
106         size = sizeof(lo->attributes);
107         size += sizeof(lo->file_path_length);
108         size += label_len;
109         size += lo->file_path_length;
110         if (lo->optional_data)
111                 size += (utf8_utf16_strlen((const char *)lo->optional_data)
112                                            + 1) * sizeof(u16);
113         p = malloc(size);
114         if (!p)
115                 return 0;
116
117         /* copy data */
118         *data = p;
119         memcpy(p, &lo->attributes, sizeof(lo->attributes));
120         p += sizeof(lo->attributes);
121
122         memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
123         p += sizeof(lo->file_path_length);
124
125         memcpy(p, lo->label, label_len);
126         p += label_len;
127
128         memcpy(p, lo->file_path, lo->file_path_length);
129         p += lo->file_path_length;
130
131         if (lo->optional_data) {
132                 utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
133                 p += sizeof(u16); /* size of trailing \0 */
134         }
135         return size;
136 }
137
138 /**
139  * get_var() - get UEFI variable
140  *
141  * It is the caller's duty to free the returned buffer.
142  *
143  * @name:       name of variable
144  * @vendor:     vendor GUID of variable
145  * @size:       size of allocated buffer
146  * Return:      buffer with variable data or NULL
147  */
148 static void *get_var(u16 *name, const efi_guid_t *vendor,
149                      efi_uintn_t *size)
150 {
151         efi_status_t ret;
152         void *buf = NULL;
153
154         *size = 0;
155         ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
156         if (ret == EFI_BUFFER_TOO_SMALL) {
157                 buf = malloc(*size);
158                 ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
159         }
160
161         if (ret != EFI_SUCCESS) {
162                 free(buf);
163                 *size = 0;
164                 return NULL;
165         }
166
167         return buf;
168 }
169
170 /**
171  * try_load_entry() - try to load image for boot option
172  *
173  * Attempt to load load-option number 'n', returning device_path and file_path
174  * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
175  * and that the specified file to boot exists.
176  *
177  * @n:          number of the boot option, e.g. 0x0a13 for Boot0A13
178  * @handle:     on return handle for the newly installed image
179  * Return:      status code
180  */
181 static efi_status_t try_load_entry(u16 n, efi_handle_t *handle)
182 {
183         struct efi_load_option lo;
184         u16 varname[] = L"Boot0000";
185         u16 hexmap[] = L"0123456789ABCDEF";
186         void *load_option;
187         efi_uintn_t size;
188         efi_status_t ret;
189
190         varname[4] = hexmap[(n & 0xf000) >> 12];
191         varname[5] = hexmap[(n & 0x0f00) >> 8];
192         varname[6] = hexmap[(n & 0x00f0) >> 4];
193         varname[7] = hexmap[(n & 0x000f) >> 0];
194
195         load_option = get_var(varname, &efi_global_variable_guid, &size);
196         if (!load_option)
197                 return EFI_LOAD_ERROR;
198
199         ret = efi_deserialize_load_option(&lo, load_option, &size);
200         if (ret != EFI_SUCCESS) {
201                 log_warning("Invalid load option for %ls\n", varname);
202                 goto error;
203         }
204
205         if (lo.attributes & LOAD_OPTION_ACTIVE) {
206                 u32 attributes;
207
208                 log_debug("%s: trying to load \"%ls\" from %pD\n",
209                           __func__, lo.label, lo.file_path);
210
211                 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
212                                               NULL, 0, handle));
213                 if (ret != EFI_SUCCESS) {
214                         log_warning("Loading %ls '%ls' failed\n",
215                                     varname, lo.label);
216                         goto error;
217                 }
218
219                 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
220                              EFI_VARIABLE_RUNTIME_ACCESS;
221                 size = sizeof(n);
222                 ret = efi_set_variable_int(L"BootCurrent",
223                                            &efi_global_variable_guid,
224                                            attributes, size, &n, false);
225                 if (ret != EFI_SUCCESS) {
226                         if (EFI_CALL(efi_unload_image(*handle))
227                             != EFI_SUCCESS)
228                                 log_err("Unloading image failed\n");
229                         goto error;
230                 }
231
232                 log_info("Booting: %ls\n", lo.label);
233         } else {
234                 ret = EFI_LOAD_ERROR;
235         }
236
237 error:
238         free(load_option);
239
240         return ret;
241 }
242
243 /**
244  * efi_bootmgr_load() - try to load from BootNext or BootOrder
245  *
246  * Attempt to load from BootNext or in the order specified by BootOrder
247  * EFI variable, the available load-options, finding and returning
248  * the first one that can be loaded successfully.
249  *
250  * @handle:     on return handle for the newly installed image
251  * Return:      status code
252  */
253 efi_status_t efi_bootmgr_load(efi_handle_t *handle)
254 {
255         u16 bootnext, *bootorder;
256         efi_uintn_t size;
257         int i, num;
258         efi_status_t ret;
259
260         bs = systab.boottime;
261         rs = systab.runtime;
262
263         /* BootNext */
264         size = sizeof(bootnext);
265         ret = efi_get_variable_int(L"BootNext",
266                                    &efi_global_variable_guid,
267                                    NULL, &size, &bootnext, NULL);
268         if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
269                 /* BootNext does exist here */
270                 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
271                         log_err("BootNext must be 16-bit integer\n");
272
273                 /* delete BootNext */
274                 ret = efi_set_variable_int(L"BootNext",
275                                            &efi_global_variable_guid,
276                                            0, 0, NULL, false);
277
278                 /* load BootNext */
279                 if (ret == EFI_SUCCESS) {
280                         if (size == sizeof(u16)) {
281                                 ret = try_load_entry(bootnext, handle);
282                                 if (ret == EFI_SUCCESS)
283                                         return ret;
284                                 log_warning(
285                                         "Loading from BootNext failed, falling back to BootOrder\n");
286                         }
287                 } else {
288                         log_err("Deleting BootNext failed\n");
289                 }
290         }
291
292         /* BootOrder */
293         bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
294         if (!bootorder) {
295                 log_info("BootOrder not defined\n");
296                 ret = EFI_NOT_FOUND;
297                 goto error;
298         }
299
300         num = size / sizeof(uint16_t);
301         for (i = 0; i < num; i++) {
302                 log_debug("%s trying to load Boot%04X\n", __func__,
303                           bootorder[i]);
304                 ret = try_load_entry(bootorder[i], handle);
305                 if (ret == EFI_SUCCESS)
306                         break;
307         }
308
309         free(bootorder);
310
311 error:
312         return ret;
313 }