blob: 17f27ca1a0b2e233e466a452f1081ad4b04394b5 [file] [log] [blame]
Ilias Apalodimasaa0f7552021-03-17 21:54:59 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2020, Linaro Limited
4 */
5
6#define LOG_CATEGORY LOGC_EFI
Ilias Apalodimasaa0f7552021-03-17 21:54:59 +02007#include <env.h>
8#include <malloc.h>
9#include <dm.h>
10#include <fs.h>
11#include <efi_load_initrd.h>
12#include <efi_loader.h>
13#include <efi_variable.h>
14
Heinrich Schuchardt6c405cb2021-10-15 02:33:33 +020015#if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI_LOAD_FILE2_INITRD)
16/* GUID used by Linux to identify the LoadFile2 protocol with the initrd */
17const efi_guid_t efi_lf2_initrd_guid = EFI_INITRD_MEDIA_GUID;
18#endif
19
Ilias Apalodimasaa0f7552021-03-17 21:54:59 +020020/**
21 * efi_create_current_boot_var() - Return Boot#### name were #### is replaced by
22 * the value of BootCurrent
23 *
24 * @var_name: variable name
25 * @var_name_size: size of var_name
26 *
27 * Return: Status code
28 */
29static efi_status_t efi_create_current_boot_var(u16 var_name[],
30 size_t var_name_size)
31{
32 efi_uintn_t boot_current_size;
33 efi_status_t ret;
34 u16 boot_current;
35 u16 *pos;
36
37 boot_current_size = sizeof(boot_current);
Simon Glass90975372022-01-23 12:55:12 -070038 ret = efi_get_variable_int(u"BootCurrent",
Ilias Apalodimasaa0f7552021-03-17 21:54:59 +020039 &efi_global_variable_guid, NULL,
40 &boot_current_size, &boot_current, NULL);
41 if (ret != EFI_SUCCESS)
42 goto out;
43
44 pos = efi_create_indexed_name(var_name, var_name_size, "Boot",
45 boot_current);
46 if (!pos) {
47 ret = EFI_OUT_OF_RESOURCES;
48 goto out;
49 }
50
51out:
52 return ret;
53}
54
55/**
56 * efi_get_dp_from_boot() - Retrieve and return a device path from an EFI
57 * Boot### variable.
58 * A boot option may contain an array of device paths.
59 * We use a VenMedia() with a specific GUID to identify
60 * the usage of the array members. This function is
61 * used to extract a specific device path
62 *
63 * @guid: vendor GUID of the VenMedia() device path node identifying the
64 * device path
65 *
66 * Return: device path or NULL. Caller must free the returned value
67 */
68struct efi_device_path *efi_get_dp_from_boot(const efi_guid_t guid)
69{
Ilias Apalodimasaa0f7552021-03-17 21:54:59 +020070 struct efi_load_option lo;
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +020071 void *var_value;
Ilias Apalodimasaa0f7552021-03-17 21:54:59 +020072 efi_uintn_t size;
73 efi_status_t ret;
74 u16 var_name[16];
75
76 ret = efi_create_current_boot_var(var_name, sizeof(var_name));
77 if (ret != EFI_SUCCESS)
78 return NULL;
79
80 var_value = efi_get_var(var_name, &efi_global_variable_guid, &size);
81 if (!var_value)
82 return NULL;
83
84 ret = efi_deserialize_load_option(&lo, var_value, &size);
85 if (ret != EFI_SUCCESS)
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +020086 goto err;
Ilias Apalodimasaa0f7552021-03-17 21:54:59 +020087
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +020088 return efi_dp_from_lo(&lo, &guid);
Ilias Apalodimasaa0f7552021-03-17 21:54:59 +020089
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +020090err:
Ilias Apalodimasaa0f7552021-03-17 21:54:59 +020091 free(var_value);
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +020092 return NULL;
Ilias Apalodimasaa0f7552021-03-17 21:54:59 +020093}
Ilias Apalodimas34db9b12022-05-06 15:36:00 +030094
95const struct guid_to_hash_map {
96 efi_guid_t guid;
97 const char algo[32];
98 u32 bits;
99} guid_to_hash[] = {
100 {
101 EFI_CERT_X509_SHA256_GUID,
102 "sha256",
103 SHA256_SUM_LEN * 8,
104 },
105 {
106 EFI_CERT_SHA256_GUID,
107 "sha256",
108 SHA256_SUM_LEN * 8,
109 },
110 {
111 EFI_CERT_X509_SHA384_GUID,
112 "sha384",
113 SHA384_SUM_LEN * 8,
114 },
115 {
116 EFI_CERT_X509_SHA512_GUID,
117 "sha512",
118 SHA512_SUM_LEN * 8,
119 },
120};
121
122#define MAX_GUID_TO_HASH_COUNT ARRAY_SIZE(guid_to_hash)
123
124/** guid_to_sha_str - return the sha string e.g "sha256" for a given guid
125 * used on EFI security databases
126 *
127 * @guid: guid to check
128 *
129 * Return: len or 0 if no match is found
130 */
131const char *guid_to_sha_str(const efi_guid_t *guid)
132{
133 size_t i;
134
135 for (i = 0; i < MAX_GUID_TO_HASH_COUNT; i++) {
136 if (!guidcmp(guid, &guid_to_hash[i].guid))
137 return guid_to_hash[i].algo;
138 }
139
140 return NULL;
141}
142
143/** algo_to_len - return the sha size in bytes for a given string
144 *
145 * @algo: string indicating hashing algorithm to check
146 *
147 * Return: length of hash in bytes or 0 if no match is found
148 */
149int algo_to_len(const char *algo)
150{
151 size_t i;
152
153 for (i = 0; i < MAX_GUID_TO_HASH_COUNT; i++) {
154 if (!strcmp(algo, guid_to_hash[i].algo))
155 return guid_to_hash[i].bits / 8;
156 }
157
158 return 0;
159}
Masahisa Kojimac9611082022-07-22 11:39:10 +0900160
161/** efi_link_dev - link the efi_handle_t and udevice
162 *
163 * @handle: efi handle to associate with udevice
164 * @dev: udevice to associate with efi handle
165 *
166 * Return: 0 on success, negative on failure
167 */
168int efi_link_dev(efi_handle_t handle, struct udevice *dev)
169{
170 handle->dev = dev;
171 return dev_tag_set_ptr(dev, DM_TAG_EFI, handle);
172}
Heinrich Schuchardt34f34622022-10-03 09:47:51 +0200173
174/**
175 * efi_unlink_dev() - unlink udevice and handle
176 *
177 * @handle: EFI handle to unlink
178 *
179 * Return: 0 on success, negative on failure
180 */
181int efi_unlink_dev(efi_handle_t handle)
182{
183 int ret;
184
185 ret = dev_tag_del(handle->dev, DM_TAG_EFI);
186 if (ret)
187 return ret;
188 handle->dev = NULL;
189
190 return 0;
191}
Masahisa Kojima2f407f02022-12-02 13:59:35 +0900192
193static int u16_tohex(u16 c)
194{
195 if (c >= '0' && c <= '9')
196 return c - '0';
197 if (c >= 'A' && c <= 'F')
198 return c - 'A' + 10;
199
200 /* not hexadecimal */
201 return -1;
202}
203
204bool efi_varname_is_load_option(u16 *var_name16, int *index)
205{
206 int id, i, digit;
207
208 if (memcmp(var_name16, u"Boot", 8))
209 return false;
210
211 for (id = 0, i = 0; i < 4; i++) {
212 digit = u16_tohex(var_name16[4 + i]);
213 if (digit < 0)
214 break;
215 id = (id << 4) + digit;
216 }
217 if (i == 4 && !var_name16[8]) {
218 if (index)
219 *index = id;
220 return true;
221 }
222
223 return false;
224}
Masahisa Kojima7ec3c6f2022-12-19 11:33:12 +0900225
226/**
227 * efi_next_variable_name() - get next variable name
228 *
229 * This function is a wrapper of efi_get_next_variable_name_int().
230 * If efi_get_next_variable_name_int() returns EFI_BUFFER_TOO_SMALL,
231 * @size and @buf are updated by new buffer size and realloced buffer.
232 *
233 * @size: pointer to the buffer size
234 * @buf: pointer to the buffer
235 * @guid: pointer to the guid
236 * Return: status code
237 */
238efi_status_t efi_next_variable_name(efi_uintn_t *size, u16 **buf, efi_guid_t *guid)
239{
240 u16 *p;
241 efi_status_t ret;
242 efi_uintn_t buf_size = *size;
243
244 ret = efi_get_next_variable_name_int(&buf_size, *buf, guid);
245 if (ret == EFI_NOT_FOUND)
246 return ret;
247 if (ret == EFI_BUFFER_TOO_SMALL) {
248 p = realloc(*buf, buf_size);
249 if (!p)
250 return EFI_OUT_OF_RESOURCES;
251
252 *buf = p;
253 *size = buf_size;
254 ret = efi_get_next_variable_name_int(&buf_size, *buf, guid);
255 }
256
257 return ret;
258}
Raymond Mao70a76c52023-06-19 14:22:58 -0700259
260/**
261 * efi_search_bootorder() - search the boot option index in BootOrder
262 *
263 * @bootorder: pointer to the BootOrder variable
264 * @num: number of BootOrder entry
265 * @target: target boot option index to search
266 * @index: pointer to store the index of BootOrder variable
267 * Return: true if exists, false otherwise
268 */
269bool efi_search_bootorder(u16 *bootorder, efi_uintn_t num, u32 target, u32 *index)
270{
271 u32 i;
272
273 for (i = 0; i < num; i++) {
274 if (target == bootorder[i]) {
275 if (index)
276 *index = i;
277
278 return true;
279 }
280 }
281
282 return false;
283}