ddr: Rework errata A008109, A008378, 009942 workaround
[oweals/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 #include <common.h>
9 #include <charset.h>
10 #include <log.h>
11 #include <malloc.h>
12 #include <efi_loader.h>
13 #include <asm/unaligned.h>
14
15 static const struct efi_boot_services *bs;
16 static const struct efi_runtime_services *rs;
17
18 /*
19  * bootmgr implements the logic of trying to find a payload to boot
20  * based on the BootOrder + BootXXXX variables, and then loading it.
21  *
22  * TODO detecting a special key held (f9?) and displaying a boot menu
23  * like you would get on a PC would be clever.
24  *
25  * TODO if we had a way to write and persist variables after the OS
26  * has started, we'd also want to check OsIndications to see if we
27  * should do normal or recovery boot.
28  */
29
30
31 /**
32  * efi_deserialize_load_option() - parse serialized data
33  *
34  * Parse serialized data describing a load option and transform it to the
35  * efi_load_option structure.
36  *
37  * @lo:         pointer to target
38  * @data:       serialized data
39  */
40 void efi_deserialize_load_option(struct efi_load_option *lo, u8 *data)
41 {
42         lo->attributes = get_unaligned_le32(data);
43         data += sizeof(u32);
44
45         lo->file_path_length = get_unaligned_le16(data);
46         data += sizeof(u16);
47
48         /* FIXME */
49         lo->label = (u16 *)data;
50         data += (u16_strlen(lo->label) + 1) * sizeof(u16);
51
52         /* FIXME */
53         lo->file_path = (struct efi_device_path *)data;
54         data += lo->file_path_length;
55
56         lo->optional_data = data;
57 }
58
59 /**
60  * efi_serialize_load_option() - serialize load option
61  *
62  * Serialize efi_load_option structure into byte stream for BootXXXX.
63  *
64  * @data:       buffer for serialized data
65  * @lo:         load option
66  * Return:      size of allocated buffer
67  */
68 unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
69 {
70         unsigned long label_len;
71         unsigned long size;
72         u8 *p;
73
74         label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
75
76         /* total size */
77         size = sizeof(lo->attributes);
78         size += sizeof(lo->file_path_length);
79         size += label_len;
80         size += lo->file_path_length;
81         if (lo->optional_data)
82                 size += (utf8_utf16_strlen((const char *)lo->optional_data)
83                                            + 1) * sizeof(u16);
84         p = malloc(size);
85         if (!p)
86                 return 0;
87
88         /* copy data */
89         *data = p;
90         memcpy(p, &lo->attributes, sizeof(lo->attributes));
91         p += sizeof(lo->attributes);
92
93         memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
94         p += sizeof(lo->file_path_length);
95
96         memcpy(p, lo->label, label_len);
97         p += label_len;
98
99         memcpy(p, lo->file_path, lo->file_path_length);
100         p += lo->file_path_length;
101
102         if (lo->optional_data) {
103                 utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
104                 p += sizeof(u16); /* size of trailing \0 */
105         }
106         return size;
107 }
108
109 /**
110  * get_var() - get UEFI variable
111  *
112  * It is the caller's duty to free the returned buffer.
113  *
114  * @name:       name of variable
115  * @vendor:     vendor GUID of variable
116  * @size:       size of allocated buffer
117  * Return:      buffer with variable data or NULL
118  */
119 static void *get_var(u16 *name, const efi_guid_t *vendor,
120                      efi_uintn_t *size)
121 {
122         efi_guid_t *v = (efi_guid_t *)vendor;
123         efi_status_t ret;
124         void *buf = NULL;
125
126         *size = 0;
127         EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
128         if (ret == EFI_BUFFER_TOO_SMALL) {
129                 buf = malloc(*size);
130                 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
131         }
132
133         if (ret != EFI_SUCCESS) {
134                 free(buf);
135                 *size = 0;
136                 return NULL;
137         }
138
139         return buf;
140 }
141
142 /**
143  * try_load_entry() - try to load image for boot option
144  *
145  * Attempt to load load-option number 'n', returning device_path and file_path
146  * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
147  * and that the specified file to boot exists.
148  *
149  * @n:          number of the boot option, e.g. 0x0a13 for Boot0A13
150  * @handle:     on return handle for the newly installed image
151  * Return:      status code
152  */
153 static efi_status_t try_load_entry(u16 n, efi_handle_t *handle)
154 {
155         struct efi_load_option lo;
156         u16 varname[] = L"Boot0000";
157         u16 hexmap[] = L"0123456789ABCDEF";
158         void *load_option;
159         efi_uintn_t size;
160         efi_status_t ret;
161
162         varname[4] = hexmap[(n & 0xf000) >> 12];
163         varname[5] = hexmap[(n & 0x0f00) >> 8];
164         varname[6] = hexmap[(n & 0x00f0) >> 4];
165         varname[7] = hexmap[(n & 0x000f) >> 0];
166
167         load_option = get_var(varname, &efi_global_variable_guid, &size);
168         if (!load_option)
169                 return EFI_LOAD_ERROR;
170
171         efi_deserialize_load_option(&lo, load_option);
172
173         if (lo.attributes & LOAD_OPTION_ACTIVE) {
174                 u32 attributes;
175
176                 debug("%s: trying to load \"%ls\" from %pD\n",
177                       __func__, lo.label, lo.file_path);
178
179                 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
180                                               NULL, 0, handle));
181                 if (ret != EFI_SUCCESS) {
182                         printf("Loading from Boot%04X '%ls' failed\n", n,
183                                lo.label);
184                         goto error;
185                 }
186
187                 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
188                              EFI_VARIABLE_RUNTIME_ACCESS;
189                 size = sizeof(n);
190                 ret = EFI_CALL(efi_set_variable(
191                                 L"BootCurrent",
192                                 (efi_guid_t *)&efi_global_variable_guid,
193                                 attributes, size, &n));
194                 if (ret != EFI_SUCCESS) {
195                         if (EFI_CALL(efi_unload_image(*handle))
196                             != EFI_SUCCESS)
197                                 printf("Unloading image failed\n");
198                         goto error;
199                 }
200
201                 printf("Booting: %ls\n", lo.label);
202         } else {
203                 ret = EFI_LOAD_ERROR;
204         }
205
206 error:
207         free(load_option);
208
209         return ret;
210 }
211
212 /**
213  * efi_bootmgr_load() - try to load from BootNext or BootOrder
214  *
215  * Attempt to load from BootNext or in the order specified by BootOrder
216  * EFI variable, the available load-options, finding and returning
217  * the first one that can be loaded successfully.
218  *
219  * @handle:     on return handle for the newly installed image
220  * Return:      status code
221  */
222 efi_status_t efi_bootmgr_load(efi_handle_t *handle)
223 {
224         u16 bootnext, *bootorder;
225         efi_uintn_t size;
226         int i, num;
227         efi_status_t ret;
228
229         bs = systab.boottime;
230         rs = systab.runtime;
231
232         /* BootNext */
233         bootnext = 0;
234         size = sizeof(bootnext);
235         ret = EFI_CALL(efi_get_variable(L"BootNext",
236                                         (efi_guid_t *)&efi_global_variable_guid,
237                                         NULL, &size, &bootnext));
238         if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
239                 /* BootNext does exist here */
240                 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
241                         printf("BootNext must be 16-bit integer\n");
242
243                 /* delete BootNext */
244                 ret = EFI_CALL(efi_set_variable(
245                                         L"BootNext",
246                                         (efi_guid_t *)&efi_global_variable_guid,
247                                         EFI_VARIABLE_NON_VOLATILE, 0,
248                                         &bootnext));
249
250                 /* load BootNext */
251                 if (ret == EFI_SUCCESS) {
252                         if (size == sizeof(u16)) {
253                                 ret = try_load_entry(bootnext, handle);
254                                 if (ret == EFI_SUCCESS)
255                                         return ret;
256                                 printf("Loading from BootNext failed, falling back to BootOrder\n");
257                         }
258                 } else {
259                         printf("Deleting BootNext failed\n");
260                 }
261         }
262
263         /* BootOrder */
264         bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
265         if (!bootorder) {
266                 printf("BootOrder not defined\n");
267                 ret = EFI_NOT_FOUND;
268                 goto error;
269         }
270
271         num = size / sizeof(uint16_t);
272         for (i = 0; i < num; i++) {
273                 debug("%s: trying to load Boot%04X\n", __func__, bootorder[i]);
274                 ret = try_load_entry(bootorder[i], handle);
275                 if (ret == EFI_SUCCESS)
276                         break;
277         }
278
279         free(bootorder);
280
281 error:
282         return ret;
283 }