1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2020, Linaro Limited
6 #define LOG_CATEGORY LOGC_EFI
12 #include <efi_load_initrd.h>
13 #include <efi_loader.h>
14 #include <efi_variable.h>
16 #if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI_LOAD_FILE2_INITRD)
17 /* GUID used by Linux to identify the LoadFile2 protocol with the initrd */
18 const efi_guid_t efi_lf2_initrd_guid = EFI_INITRD_MEDIA_GUID;
22 * efi_create_current_boot_var() - Return Boot#### name were #### is replaced by
23 * the value of BootCurrent
25 * @var_name: variable name
26 * @var_name_size: size of var_name
30 static efi_status_t efi_create_current_boot_var(u16 var_name[],
33 efi_uintn_t boot_current_size;
38 boot_current_size = sizeof(boot_current);
39 ret = efi_get_variable_int(u"BootCurrent",
40 &efi_global_variable_guid, NULL,
41 &boot_current_size, &boot_current, NULL);
42 if (ret != EFI_SUCCESS)
45 pos = efi_create_indexed_name(var_name, var_name_size, "Boot",
48 ret = EFI_OUT_OF_RESOURCES;
57 * efi_get_dp_from_boot() - Retrieve and return a device path from an EFI
59 * A boot option may contain an array of device paths.
60 * We use a VenMedia() with a specific GUID to identify
61 * the usage of the array members. This function is
62 * used to extract a specific device path
64 * @guid: vendor GUID of the VenMedia() device path node identifying the
67 * Return: device path or NULL. Caller must free the returned value
69 struct efi_device_path *efi_get_dp_from_boot(const efi_guid_t guid)
71 struct efi_load_option lo;
77 ret = efi_create_current_boot_var(var_name, sizeof(var_name));
78 if (ret != EFI_SUCCESS)
81 var_value = efi_get_var(var_name, &efi_global_variable_guid, &size);
85 ret = efi_deserialize_load_option(&lo, var_value, &size);
86 if (ret != EFI_SUCCESS)
89 return efi_dp_from_lo(&lo, &guid);
96 const struct guid_to_hash_map {
102 EFI_CERT_X509_SHA256_GUID,
107 EFI_CERT_SHA256_GUID,
112 EFI_CERT_X509_SHA384_GUID,
117 EFI_CERT_X509_SHA512_GUID,
123 #define MAX_GUID_TO_HASH_COUNT ARRAY_SIZE(guid_to_hash)
125 /** guid_to_sha_str - return the sha string e.g "sha256" for a given guid
126 * used on EFI security databases
128 * @guid: guid to check
130 * Return: len or 0 if no match is found
132 const char *guid_to_sha_str(const efi_guid_t *guid)
136 for (i = 0; i < MAX_GUID_TO_HASH_COUNT; i++) {
137 if (!guidcmp(guid, &guid_to_hash[i].guid))
138 return guid_to_hash[i].algo;
144 /** algo_to_len - return the sha size in bytes for a given string
146 * @algo: string indicating hashing algorithm to check
148 * Return: length of hash in bytes or 0 if no match is found
150 int algo_to_len(const char *algo)
154 for (i = 0; i < MAX_GUID_TO_HASH_COUNT; i++) {
155 if (!strcmp(algo, guid_to_hash[i].algo))
156 return guid_to_hash[i].bits / 8;
162 /** efi_link_dev - link the efi_handle_t and udevice
164 * @handle: efi handle to associate with udevice
165 * @dev: udevice to associate with efi handle
167 * Return: 0 on success, negative on failure
169 int efi_link_dev(efi_handle_t handle, struct udevice *dev)
172 return dev_tag_set_ptr(dev, DM_TAG_EFI, handle);
176 * efi_unlink_dev() - unlink udevice and handle
178 * @handle: EFI handle to unlink
180 * Return: 0 on success, negative on failure
182 int efi_unlink_dev(efi_handle_t handle)
186 ret = dev_tag_del(handle->dev, DM_TAG_EFI);
194 static int u16_tohex(u16 c)
196 if (c >= '0' && c <= '9')
198 if (c >= 'A' && c <= 'F')
201 /* not hexadecimal */
205 bool efi_varname_is_load_option(u16 *var_name16, int *index)
209 if (memcmp(var_name16, u"Boot", 8))
212 for (id = 0, i = 0; i < 4; i++) {
213 digit = u16_tohex(var_name16[4 + i]);
216 id = (id << 4) + digit;
218 if (i == 4 && !var_name16[8]) {
228 * efi_next_variable_name() - get next variable name
230 * This function is a wrapper of efi_get_next_variable_name_int().
231 * If efi_get_next_variable_name_int() returns EFI_BUFFER_TOO_SMALL,
232 * @size and @buf are updated by new buffer size and realloced buffer.
234 * @size: pointer to the buffer size
235 * @buf: pointer to the buffer
236 * @guid: pointer to the guid
237 * Return: status code
239 efi_status_t efi_next_variable_name(efi_uintn_t *size, u16 **buf, efi_guid_t *guid)
243 efi_uintn_t buf_size = *size;
245 ret = efi_get_next_variable_name_int(&buf_size, *buf, guid);
246 if (ret == EFI_NOT_FOUND)
248 if (ret == EFI_BUFFER_TOO_SMALL) {
249 p = realloc(*buf, buf_size);
251 return EFI_OUT_OF_RESOURCES;
255 ret = efi_get_next_variable_name_int(&buf_size, *buf, guid);