1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Rob Clark
10 #include <efi_loader.h>
12 #include <environment.h>
16 #define READ_ONLY BIT(31)
19 * Mapping between EFI variables and u-boot variables:
21 * efi_$guid_$varname = {attributes}(type)value
25 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
26 * "{ro,boot,run}(blob)0000000000000000"
27 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
30 * The attributes are a comma separated list of these possible
34 * + boot - boot-services access
35 * + run - runtime access
37 * NOTE: with current implementation, no variables are available after
38 * ExitBootServices, and all are persisted (if possible).
40 * If not specified, the attributes default to "{boot}".
42 * The required type is one of:
44 * + utf8 - raw utf8 string
45 * + blob - arbitrary length hex string
47 * Maybe a utf16 type would be useful to for a string value to be auto
51 #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
54 * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
57 * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
58 * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
59 * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
61 * @native: pointer to pointer to U-Boot variable name
62 * @variable_name: UEFI variable name
63 * @vendor: vendor GUID
66 static efi_status_t efi_to_native(char **native, const u16 *variable_name,
67 const efi_guid_t *vendor)
72 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
73 *native = malloc(len);
75 return EFI_OUT_OF_RESOURCES;
78 pos += sprintf(pos, "efi_%pUl_", vendor);
79 utf16_utf8_strcpy(&pos, variable_name);
85 * prefix() - skip over prefix
87 * Skip over a prefix string.
89 * @str: string with prefix
90 * @prefix: prefix string
91 * Return: string without prefix, or NULL if prefix not found
93 static const char *prefix(const char *str, const char *prefix)
95 size_t n = strlen(prefix);
96 if (!strncmp(prefix, str, n))
102 * parse_attr() - decode attributes part of variable value
104 * Convert the string encoded attributes of a UEFI variable to a bit mask.
105 * TODO: Several attributes are not supported.
107 * @str: value of U-Boot variable
108 * @attrp: pointer to UEFI attributes
109 * Return: pointer to remainder of U-Boot variable value
111 static const char *parse_attr(const char *str, u32 *attrp)
117 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
121 while (*str == sep) {
126 if ((s = prefix(str, "ro"))) {
128 } else if ((s = prefix(str, "boot"))) {
129 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
130 } else if ((s = prefix(str, "run"))) {
131 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
133 printf("invalid attribute: %s\n", str);
149 * efi_efi_get_variable() - retrieve value of a UEFI variable
151 * This function implements the GetVariable runtime service.
153 * See the Unified Extensible Firmware Interface (UEFI) specification for
156 * @variable_name: name of the variable
157 * @vendor: vendor GUID
158 * @attributes: attributes of the variable
159 * @data_size: size of the buffer to which the variable value is copied
160 * @data: buffer to which the variable value is copied
161 * Return: status code
163 efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
164 const efi_guid_t *vendor, u32 *attributes,
165 efi_uintn_t *data_size, void *data)
169 unsigned long in_size;
173 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
176 if (!variable_name || !vendor || !data_size)
177 return EFI_EXIT(EFI_INVALID_PARAMETER);
179 ret = efi_to_native(&native_name, variable_name, vendor);
181 return EFI_EXIT(ret);
183 EFI_PRINT("get '%s'\n", native_name);
185 val = env_get(native_name);
188 return EFI_EXIT(EFI_NOT_FOUND);
190 val = parse_attr(val, &attr);
192 in_size = *data_size;
194 if ((s = prefix(val, "(blob)"))) {
195 size_t len = strlen(s);
197 /* number of hexadecimal digits must be even */
199 return EFI_EXIT(EFI_DEVICE_ERROR);
201 /* two characters per byte: */
206 ret = EFI_BUFFER_TOO_SMALL;
211 return EFI_EXIT(EFI_INVALID_PARAMETER);
213 if (hex2bin(data, s, len))
214 return EFI_EXIT(EFI_DEVICE_ERROR);
216 EFI_PRINT("got value: \"%s\"\n", s);
217 } else if ((s = prefix(val, "(utf8)"))) {
218 unsigned len = strlen(s) + 1;
223 ret = EFI_BUFFER_TOO_SMALL;
228 return EFI_EXIT(EFI_INVALID_PARAMETER);
230 memcpy(data, s, len);
231 ((char *)data)[len] = '\0';
233 EFI_PRINT("got value: \"%s\"\n", (char *)data);
235 EFI_PRINT("invalid value: '%s'\n", val);
236 return EFI_EXIT(EFI_DEVICE_ERROR);
241 *attributes = attr & EFI_VARIABLE_MASK;
243 return EFI_EXIT(ret);
246 static char *efi_variables_list;
247 static char *efi_cur_variable;
250 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
252 * @variable: whole data of u-boot variable (ie. name=value)
253 * @variable_name_size: size of variable_name buffer in byte
254 * @variable_name: name of uefi variable in u16, null-terminated
255 * @vendor: vendor's guid
256 * @attributes: attributes
258 * A uefi variable is encoded into a u-boot variable as described above.
259 * This function parses such a u-boot variable and retrieve uefi-related
260 * information into respective parameters. In return, variable_name_size
261 * is the size of variable name including NULL.
263 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
264 the entire variable list has been returned,
265 otherwise non-zero status code
267 static efi_status_t parse_uboot_variable(char *variable,
268 efi_uintn_t *variable_name_size,
270 const efi_guid_t *vendor,
273 char *guid, *name, *end, c;
274 unsigned long name_len;
277 guid = strchr(variable, '_');
279 return EFI_INVALID_PARAMETER;
281 name = strchr(guid, '_');
283 return EFI_INVALID_PARAMETER;
285 end = strchr(name, '=');
287 return EFI_INVALID_PARAMETER;
289 name_len = end - name;
290 if (*variable_name_size < (name_len + 1)) {
291 *variable_name_size = name_len + 1;
292 return EFI_BUFFER_TOO_SMALL;
294 end++; /* point to value */
298 utf8_utf16_strncpy(&p, name, name_len);
299 variable_name[name_len] = 0;
300 *variable_name_size = name_len + 1;
304 *(name - 1) = '\0'; /* guid need be null-terminated here */
305 uuid_str_to_bin(guid, (unsigned char *)vendor, UUID_STR_FORMAT_GUID);
309 parse_attr(end, attributes);
315 * efi_get_next_variable_name() - enumerate the current variable names
316 * @variable_name_size: size of variable_name buffer in byte
317 * @variable_name: name of uefi variable's name in u16
318 * @vendor: vendor's guid
320 * This function implements the GetNextVariableName service.
322 * See the Unified Extensible Firmware Interface (UEFI) specification for
323 * details: http://wiki.phoenix.com/wiki/index.php/
324 * EFI_RUNTIME_SERVICES#GetNextVariableName.28.29
326 * Return: status code
328 efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
330 const efi_guid_t *vendor)
332 char *native_name, *variable;
333 ssize_t name_len, list_len;
335 char * const regexlist[] = {regex};
340 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
342 if (!variable_name_size || !variable_name || !vendor)
343 return EFI_EXIT(EFI_INVALID_PARAMETER);
345 if (variable_name[0]) {
346 /* check null-terminated string */
347 for (i = 0; i < *variable_name_size; i++)
348 if (!variable_name[i])
350 if (i >= *variable_name_size)
351 return EFI_EXIT(EFI_INVALID_PARAMETER);
353 /* search for the last-returned variable */
354 ret = efi_to_native(&native_name, variable_name, vendor);
356 return EFI_EXIT(ret);
358 name_len = strlen(native_name);
359 for (variable = efi_variables_list; variable && *variable;) {
360 if (!strncmp(variable, native_name, name_len) &&
361 variable[name_len] == '=')
364 variable = strchr(variable, '\n');
370 if (!(variable && *variable))
371 return EFI_EXIT(EFI_INVALID_PARAMETER);
374 variable = strchr(variable, '\n');
377 if (!(variable && *variable))
378 return EFI_EXIT(EFI_NOT_FOUND);
381 *new search: free a list used in the previous search
383 free(efi_variables_list);
384 efi_variables_list = NULL;
385 efi_cur_variable = NULL;
387 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
388 list_len = hexport_r(&env_htab, '\n',
389 H_MATCH_REGEX | H_MATCH_KEY,
390 &efi_variables_list, 0, 1, regexlist);
391 /* 1 indicates that no match was found */
393 return EFI_EXIT(EFI_NOT_FOUND);
395 variable = efi_variables_list;
398 ret = parse_uboot_variable(variable, variable_name_size, variable_name,
399 vendor, &attributes);
401 return EFI_EXIT(ret);
405 * efi_efi_set_variable() - set value of a UEFI variable
407 * This function implements the SetVariable runtime service.
409 * See the Unified Extensible Firmware Interface (UEFI) specification for
412 * @variable_name: name of the variable
413 * @vendor: vendor GUID
414 * @attributes: attributes of the variable
415 * @data_size: size of the buffer with the variable value
416 * @data: buffer with the variable value
417 * Return: status code
419 efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
420 const efi_guid_t *vendor, u32 attributes,
421 efi_uintn_t data_size, const void *data)
423 char *native_name = NULL, *val = NULL, *s;
424 efi_status_t ret = EFI_SUCCESS;
427 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
430 if (!variable_name || !vendor) {
431 ret = EFI_INVALID_PARAMETER;
435 ret = efi_to_native(&native_name, variable_name, vendor);
439 #define ACCESS_ATTR (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)
441 if ((data_size == 0) || !(attributes & ACCESS_ATTR)) {
442 /* delete the variable: */
443 env_set(native_name, NULL);
448 val = env_get(native_name);
450 parse_attr(val, &attr);
452 if (attr & READ_ONLY) {
453 /* We should not free val */
455 ret = EFI_WRITE_PROTECTED;
460 val = malloc(2 * data_size + strlen("{ro,run,boot}(blob)") + 1);
462 ret = EFI_OUT_OF_RESOURCES;
470 * TODO: several attributes are not supported
472 attributes &= (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS);
473 s += sprintf(s, "{");
475 u32 attr = 1 << (ffs(attributes) - 1);
477 if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS)
478 s += sprintf(s, "boot");
479 else if (attr == EFI_VARIABLE_RUNTIME_ACCESS)
480 s += sprintf(s, "run");
484 s += sprintf(s, ",");
486 s += sprintf(s, "}");
489 s += sprintf(s, "(blob)");
490 s = bin2hex(s, data, data_size);
493 EFI_PRINT("setting: %s=%s\n", native_name, val);
495 if (env_set(native_name, val))
496 ret = EFI_DEVICE_ERROR;
502 return EFI_EXIT(ret);