Merge tag 'u-boot-imx-20190426' of git://git.denx.de/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 #include <common.h>
9 #include <charset.h>
10 #include <malloc.h>
11 #include <efi_loader.h>
12 #include <asm/unaligned.h>
13
14 static const struct efi_boot_services *bs;
15 static const struct efi_runtime_services *rs;
16
17 /*
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
30 /* Parse serialized data and transform it into efi_load_option structure */
31 void efi_deserialize_load_option(struct efi_load_option *lo, u8 *data)
32 {
33         lo->attributes = get_unaligned_le32(data);
34         data += sizeof(u32);
35
36         lo->file_path_length = get_unaligned_le16(data);
37         data += sizeof(u16);
38
39         /* FIXME */
40         lo->label = (u16 *)data;
41         data += (u16_strlen(lo->label) + 1) * sizeof(u16);
42
43         /* FIXME */
44         lo->file_path = (struct efi_device_path *)data;
45         data += lo->file_path_length;
46
47         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  */
54 unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
55 {
56         unsigned long label_len, option_len;
57         unsigned long size;
58         u8 *p;
59
60         label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
61         option_len = strlen((char *)lo->optional_data);
62
63         /* total size */
64         size = sizeof(lo->attributes);
65         size += sizeof(lo->file_path_length);
66         size += label_len;
67         size += lo->file_path_length;
68         size += option_len + 1;
69         p = malloc(size);
70         if (!p)
71                 return 0;
72
73         /* copy data */
74         *data = p;
75         memcpy(p, &lo->attributes, sizeof(lo->attributes));
76         p += sizeof(lo->attributes);
77
78         memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
79         p += sizeof(lo->file_path_length);
80
81         memcpy(p, lo->label, label_len);
82         p += label_len;
83
84         memcpy(p, lo->file_path, lo->file_path_length);
85         p += lo->file_path_length;
86
87         memcpy(p, lo->optional_data, option_len);
88         p += option_len;
89         *(char *)p = '\0';
90
91         return size;
92 }
93
94 /* free() the result */
95 static void *get_var(u16 *name, const efi_guid_t *vendor,
96                      efi_uintn_t *size)
97 {
98         efi_guid_t *v = (efi_guid_t *)vendor;
99         efi_status_t ret;
100         void *buf = NULL;
101
102         *size = 0;
103         EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
104         if (ret == EFI_BUFFER_TOO_SMALL) {
105                 buf = malloc(*size);
106                 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
107         }
108
109         if (ret != EFI_SUCCESS) {
110                 free(buf);
111                 *size = 0;
112                 return NULL;
113         }
114
115         return buf;
116 }
117
118 /*
119  * Attempt to load load-option number 'n', returning device_path and file_path
120  * if successful.  This checks that the EFI_LOAD_OPTION is active (enabled)
121  * and that the specified file to boot exists.
122  */
123 static efi_status_t try_load_entry(u16 n, efi_handle_t *handle)
124 {
125         struct efi_load_option lo;
126         u16 varname[] = L"Boot0000";
127         u16 hexmap[] = L"0123456789ABCDEF";
128         void *load_option;
129         efi_uintn_t size;
130         efi_status_t ret;
131
132         varname[4] = hexmap[(n & 0xf000) >> 12];
133         varname[5] = hexmap[(n & 0x0f00) >> 8];
134         varname[6] = hexmap[(n & 0x00f0) >> 4];
135         varname[7] = hexmap[(n & 0x000f) >> 0];
136
137         load_option = get_var(varname, &efi_global_variable_guid, &size);
138         if (!load_option)
139                 return EFI_LOAD_ERROR;
140
141         efi_deserialize_load_option(&lo, load_option);
142
143         if (lo.attributes & LOAD_OPTION_ACTIVE) {
144                 u32 attributes;
145
146                 debug("%s: trying to load \"%ls\" from %pD\n",
147                       __func__, lo.label, lo.file_path);
148
149                 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
150                                               NULL, 0, handle));
151                 if (ret != EFI_SUCCESS)
152                         goto error;
153
154                 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
155                              EFI_VARIABLE_RUNTIME_ACCESS;
156                 size = sizeof(n);
157                 ret = EFI_CALL(efi_set_variable(
158                                 L"BootCurrent",
159                                 (efi_guid_t *)&efi_global_variable_guid,
160                                 attributes, size, &n));
161                 if (ret != EFI_SUCCESS) {
162                         if (EFI_CALL(efi_unload_image(*handle))
163                             != EFI_SUCCESS)
164                                 printf("Unloading image failed\n");
165                         goto error;
166                 }
167
168                 printf("Booting: %ls\n", lo.label);
169         } else {
170                 ret = EFI_LOAD_ERROR;
171         }
172
173 error:
174         free(load_option);
175
176         return ret;
177 }
178
179 /*
180  * Attempt to load from BootNext or in the order specified by BootOrder
181  * EFI variable, the available load-options, finding and returning
182  * the first one that can be loaded successfully.
183  */
184 efi_status_t efi_bootmgr_load(efi_handle_t *handle)
185 {
186         u16 bootnext, *bootorder;
187         efi_uintn_t size;
188         int i, num;
189         efi_status_t ret;
190
191         bs = systab.boottime;
192         rs = systab.runtime;
193
194         /* BootNext */
195         bootnext = 0;
196         size = sizeof(bootnext);
197         ret = EFI_CALL(efi_get_variable(L"BootNext",
198                                         (efi_guid_t *)&efi_global_variable_guid,
199                                         NULL, &size, &bootnext));
200         if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
201                 /* BootNext does exist here */
202                 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
203                         printf("BootNext must be 16-bit integer\n");
204
205                 /* delete BootNext */
206                 ret = EFI_CALL(efi_set_variable(
207                                         L"BootNext",
208                                         (efi_guid_t *)&efi_global_variable_guid,
209                                         0, 0, &bootnext));
210
211                 /* load BootNext */
212                 if (ret == EFI_SUCCESS) {
213                         if (size == sizeof(u16)) {
214                                 ret = try_load_entry(bootnext, handle);
215                                 if (ret == EFI_SUCCESS)
216                                         return ret;
217                         }
218                 } else {
219                         printf("Deleting BootNext failed\n");
220                 }
221         }
222
223         /* BootOrder */
224         bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
225         if (!bootorder) {
226                 printf("BootOrder not defined\n");
227                 ret = EFI_NOT_FOUND;
228                 goto error;
229         }
230
231         num = size / sizeof(uint16_t);
232         for (i = 0; i < num; i++) {
233                 debug("%s: trying to load Boot%04X\n", __func__, bootorder[i]);
234                 ret = try_load_entry(bootorder[i], handle);
235                 if (ret == EFI_SUCCESS)
236                         break;
237         }
238
239         free(bootorder);
240
241 error:
242         return ret;
243 }