1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Rob Clark
10 #include <efi_loader.h>
13 #define READ_ONLY BIT(31)
16 * Mapping between EFI variables and u-boot variables:
18 * efi_$guid_$varname = {attributes}(type)value
22 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
23 * "{ro,boot,run}(blob)0000000000000000"
24 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
27 * The attributes are a comma separated list of these possible
31 * + boot - boot-services access
32 * + run - runtime access
34 * NOTE: with current implementation, no variables are available after
35 * ExitBootServices, and all are persisted (if possible).
37 * If not specified, the attributes default to "{boot}".
39 * The required type is one of:
41 * + utf8 - raw utf8 string
42 * + blob - arbitrary length hex string
44 * Maybe a utf16 type would be useful to for a string value to be auto
48 #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
51 * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
54 * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
55 * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
56 * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
58 * @native: pointer to pointer to U-Boot variable name
59 * @variable_name: UEFI variable name
60 * @vendor: vendor GUID
63 static efi_status_t efi_to_native(char **native, const u16 *variable_name,
64 const efi_guid_t *vendor)
69 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
70 *native = malloc(len);
72 return EFI_OUT_OF_RESOURCES;
75 pos += sprintf(pos, "efi_%pUl_", vendor);
76 utf16_utf8_strcpy(&pos, variable_name);
82 * prefix() - skip over prefix
84 * Skip over a prefix string.
86 * @str: string with prefix
87 * @prefix: prefix string
88 * Return: string without prefix, or NULL if prefix not found
90 static const char *prefix(const char *str, const char *prefix)
92 size_t n = strlen(prefix);
93 if (!strncmp(prefix, str, n))
99 * parse_attr() - decode attributes part of variable value
101 * Convert the string encoded attributes of a UEFI variable to a bit mask.
102 * TODO: Several attributes are not supported.
104 * @str: value of U-Boot variable
105 * @attrp: pointer to UEFI attributes
106 * Return: pointer to remainder of U-Boot variable value
108 static const char *parse_attr(const char *str, u32 *attrp)
114 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
118 while (*str == sep) {
123 if ((s = prefix(str, "ro"))) {
125 } else if ((s = prefix(str, "boot"))) {
126 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
127 } else if ((s = prefix(str, "run"))) {
128 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
130 printf("invalid attribute: %s\n", str);
146 * efi_efi_get_variable() - retrieve value of a UEFI variable
148 * This function implements the GetVariable runtime service.
150 * See the Unified Extensible Firmware Interface (UEFI) specification for
153 * @variable_name: name of the variable
154 * @vendor: vendor GUID
155 * @attributes: attributes of the variable
156 * @data_size: size of the buffer to which the variable value is copied
157 * @data: buffer to which the variable value is copied
158 * Return: status code
160 efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
161 const efi_guid_t *vendor, u32 *attributes,
162 efi_uintn_t *data_size, void *data)
166 unsigned long in_size;
170 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
173 if (!variable_name || !vendor || !data_size)
174 return EFI_EXIT(EFI_INVALID_PARAMETER);
176 ret = efi_to_native(&native_name, variable_name, vendor);
178 return EFI_EXIT(ret);
180 debug("%s: get '%s'\n", __func__, native_name);
182 val = env_get(native_name);
185 return EFI_EXIT(EFI_NOT_FOUND);
187 val = parse_attr(val, &attr);
189 in_size = *data_size;
191 if ((s = prefix(val, "(blob)"))) {
192 size_t len = strlen(s);
194 /* number of hexadecimal digits must be even */
196 return EFI_EXIT(EFI_DEVICE_ERROR);
198 /* two characters per byte: */
203 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
206 return EFI_EXIT(EFI_INVALID_PARAMETER);
208 if (hex2bin(data, s, len))
209 return EFI_EXIT(EFI_DEVICE_ERROR);
211 debug("%s: got value: \"%s\"\n", __func__, s);
212 } else if ((s = prefix(val, "(utf8)"))) {
213 unsigned len = strlen(s) + 1;
218 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
221 return EFI_EXIT(EFI_INVALID_PARAMETER);
223 memcpy(data, s, len);
224 ((char *)data)[len] = '\0';
226 debug("%s: got value: \"%s\"\n", __func__, (char *)data);
228 debug("%s: invalid value: '%s'\n", __func__, val);
229 return EFI_EXIT(EFI_DEVICE_ERROR);
233 *attributes = attr & EFI_VARIABLE_MASK;
235 return EFI_EXIT(EFI_SUCCESS);
239 * efi_efi_get_next_variable() - get next UEFI variable
241 * This function implements the GetNextVariable runtime service.
243 * See the Unified Extensible Firmware Interface (UEFI) specification for
246 * @variable_name_size: on entry size of the buffer for the variable name, on
247 * exit the length of the name of the next variable
248 * @variable_name: on entry name of the current variable, on exit the name
249 * of the next variable
250 * @vendor: vendor GUID
251 * Return: status code
253 efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
255 const efi_guid_t *vendor)
257 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
259 return EFI_EXIT(EFI_DEVICE_ERROR);
263 * efi_efi_set_variable() - set value of a UEFI variable
265 * This function implements the SetVariable runtime service.
267 * See the Unified Extensible Firmware Interface (UEFI) specification for
270 * @variable_name: name of the variable
271 * @vendor: vendor GUID
272 * @attributes: attributes of the variable
273 * @data_size: size of the buffer with the variable value
274 * @data: buffer with the variable value
275 * Return: status code
277 efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
278 const efi_guid_t *vendor, u32 attributes,
279 efi_uintn_t data_size, const void *data)
281 char *native_name = NULL, *val = NULL, *s;
282 efi_status_t ret = EFI_SUCCESS;
285 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
288 if (!variable_name || !vendor) {
289 ret = EFI_INVALID_PARAMETER;
293 ret = efi_to_native(&native_name, variable_name, vendor);
297 #define ACCESS_ATTR (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)
299 if ((data_size == 0) || !(attributes & ACCESS_ATTR)) {
300 /* delete the variable: */
301 env_set(native_name, NULL);
306 val = env_get(native_name);
308 parse_attr(val, &attr);
310 if (attr & READ_ONLY) {
311 /* We should not free val */
313 ret = EFI_WRITE_PROTECTED;
318 val = malloc(2 * data_size + strlen("{ro,run,boot}(blob)") + 1);
320 ret = EFI_OUT_OF_RESOURCES;
328 * TODO: several attributes are not supported
330 attributes &= (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS);
331 s += sprintf(s, "{");
333 u32 attr = 1 << (ffs(attributes) - 1);
335 if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS)
336 s += sprintf(s, "boot");
337 else if (attr == EFI_VARIABLE_RUNTIME_ACCESS)
338 s += sprintf(s, "run");
342 s += sprintf(s, ",");
344 s += sprintf(s, "}");
347 s += sprintf(s, "(blob)");
348 s = bin2hex(s, data, data_size);
351 debug("%s: setting: %s=%s\n", __func__, native_name, val);
353 if (env_set(native_name, val))
354 ret = EFI_DEVICE_ERROR;
360 return EFI_EXIT(ret);