1 // SPDX-License-Identifier: GPL-2.0+
3 * UEFI runtime variable services
5 * Copyright (c) 2017 Rob Clark
9 #include <efi_loader.h>
10 #include <env_internal.h>
16 #include <crypto/pkcs7_parser.h>
17 #include <linux/compat.h>
18 #include <u-boot/crc.h>
20 enum efi_secure_mode {
27 const efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID;
28 static bool efi_secure_boot;
29 static int efi_secure_mode;
30 static u8 efi_vendor_keys;
32 #define READ_ONLY BIT(31)
34 static efi_status_t efi_get_variable_common(u16 *variable_name,
35 const efi_guid_t *vendor,
37 efi_uintn_t *data_size, void *data);
39 static efi_status_t efi_set_variable_common(u16 *variable_name,
40 const efi_guid_t *vendor,
42 efi_uintn_t data_size,
47 * Mapping between EFI variables and u-boot variables:
49 * efi_$guid_$varname = {attributes}(type)value
53 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
54 * "{ro,boot,run}(blob)0000000000000000"
55 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
58 * The attributes are a comma separated list of these possible
62 * + boot - boot-services access
63 * + run - runtime access
65 * NOTE: with current implementation, no variables are available after
66 * ExitBootServices, and all are persisted (if possible).
68 * If not specified, the attributes default to "{boot}".
70 * The required type is one of:
72 * + utf8 - raw utf8 string
73 * + blob - arbitrary length hex string
75 * Maybe a utf16 type would be useful to for a string value to be auto
79 #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
82 * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
85 * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
86 * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
87 * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
89 * @native: pointer to pointer to U-Boot variable name
90 * @variable_name: UEFI variable name
91 * @vendor: vendor GUID
94 static efi_status_t efi_to_native(char **native, const u16 *variable_name,
95 const efi_guid_t *vendor)
100 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
101 *native = malloc(len);
103 return EFI_OUT_OF_RESOURCES;
106 pos += sprintf(pos, "efi_%pUl_", vendor);
107 utf16_utf8_strcpy(&pos, variable_name);
113 * prefix() - skip over prefix
115 * Skip over a prefix string.
117 * @str: string with prefix
118 * @prefix: prefix string
119 * Return: string without prefix, or NULL if prefix not found
121 static const char *prefix(const char *str, const char *prefix)
123 size_t n = strlen(prefix);
124 if (!strncmp(prefix, str, n))
130 * parse_attr() - decode attributes part of variable value
132 * Convert the string encoded attributes of a UEFI variable to a bit mask.
133 * TODO: Several attributes are not supported.
135 * @str: value of U-Boot variable
136 * @attrp: pointer to UEFI attributes
137 * @timep: pointer to time attribute
138 * Return: pointer to remainder of U-Boot variable value
140 static const char *parse_attr(const char *str, u32 *attrp, u64 *timep)
146 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
150 while (*str == sep) {
155 if ((s = prefix(str, "ro"))) {
157 } else if ((s = prefix(str, "nv"))) {
158 attr |= EFI_VARIABLE_NON_VOLATILE;
159 } else if ((s = prefix(str, "boot"))) {
160 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
161 } else if ((s = prefix(str, "run"))) {
162 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
163 } else if ((s = prefix(str, "time="))) {
164 attr |= EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
165 hex2bin((u8 *)timep, s, sizeof(*timep));
166 s += sizeof(*timep) * 2;
167 } else if (*str == '}') {
170 printf("invalid attribute: %s\n", str);
186 * efi_set_secure_state - modify secure boot state variables
187 * @sec_boot: value of SecureBoot
188 * @setup_mode: value of SetupMode
189 * @audit_mode: value of AuditMode
190 * @deployed_mode: value of DeployedMode
192 * Modify secure boot stat-related variables as indicated.
194 * Return: status code
196 static efi_status_t efi_set_secure_state(int sec_boot, int setup_mode,
197 int audit_mode, int deployed_mode)
202 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
203 EFI_VARIABLE_RUNTIME_ACCESS |
205 ret = efi_set_variable_common(L"SecureBoot", &efi_global_variable_guid,
206 attributes, sizeof(sec_boot), &sec_boot,
208 if (ret != EFI_SUCCESS)
211 ret = efi_set_variable_common(L"SetupMode", &efi_global_variable_guid,
212 attributes, sizeof(setup_mode),
214 if (ret != EFI_SUCCESS)
217 ret = efi_set_variable_common(L"AuditMode", &efi_global_variable_guid,
218 attributes, sizeof(audit_mode),
220 if (ret != EFI_SUCCESS)
223 ret = efi_set_variable_common(L"DeployedMode",
224 &efi_global_variable_guid, attributes,
225 sizeof(deployed_mode), &deployed_mode,
232 * efi_transfer_secure_state - handle a secure boot state transition
235 * Depending on @mode, secure boot related variables are updated.
236 * Those variables are *read-only* for users, efi_set_variable_common()
239 * Return: status code
241 static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
245 debug("Switching secure state from %d to %d\n", efi_secure_mode, mode);
247 if (mode == EFI_MODE_DEPLOYED) {
248 ret = efi_set_secure_state(1, 0, 0, 1);
249 if (ret != EFI_SUCCESS)
252 efi_secure_boot = true;
253 } else if (mode == EFI_MODE_AUDIT) {
254 ret = efi_set_variable_common(L"PK", &efi_global_variable_guid,
255 EFI_VARIABLE_BOOTSERVICE_ACCESS |
256 EFI_VARIABLE_RUNTIME_ACCESS,
258 if (ret != EFI_SUCCESS)
261 ret = efi_set_secure_state(0, 1, 1, 0);
262 if (ret != EFI_SUCCESS)
265 efi_secure_boot = true;
266 } else if (mode == EFI_MODE_USER) {
267 ret = efi_set_secure_state(1, 0, 0, 0);
268 if (ret != EFI_SUCCESS)
271 efi_secure_boot = true;
272 } else if (mode == EFI_MODE_SETUP) {
273 ret = efi_set_secure_state(0, 1, 0, 0);
274 if (ret != EFI_SUCCESS)
277 return EFI_INVALID_PARAMETER;
280 efi_secure_mode = mode;
285 /* TODO: What action should be taken here? */
286 printf("ERROR: Secure state transition failed\n");
291 * efi_init_secure_state - initialize secure boot state
293 * Return: status code
295 static efi_status_t efi_init_secure_state(void)
297 enum efi_secure_mode mode;
303 * Since there is currently no "platform-specific" installation
304 * method of Platform Key, we can't say if VendorKeys is 0 or 1
309 ret = efi_get_variable_common(L"PK", &efi_global_variable_guid,
311 if (ret == EFI_BUFFER_TOO_SMALL) {
312 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT))
313 mode = EFI_MODE_USER;
315 mode = EFI_MODE_SETUP;
318 } else if (ret == EFI_NOT_FOUND) {
319 mode = EFI_MODE_SETUP;
325 ret = efi_transfer_secure_state(mode);
326 if (ret == EFI_SUCCESS)
327 ret = efi_set_variable_common(L"VendorKeys",
328 &efi_global_variable_guid,
329 EFI_VARIABLE_BOOTSERVICE_ACCESS |
330 EFI_VARIABLE_RUNTIME_ACCESS |
332 sizeof(efi_vendor_keys),
333 &efi_vendor_keys, false);
340 * efi_secure_boot_enabled - return if secure boot is enabled or not
342 * Return: true if enabled, false if disabled
344 bool efi_secure_boot_enabled(void)
346 return efi_secure_boot;
349 #ifdef CONFIG_EFI_SECURE_BOOT
350 static u8 pkcs7_hdr[] = {
352 0x30, 0x82, 0x05, 0xc7,
353 /* OID: pkcs7-signedData */
354 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
355 /* Context Structured? */
356 0xa0, 0x82, 0x05, 0xb8,
360 * efi_variable_parse_signature - parse a signature in variable
361 * @buf: Pointer to variable's value
362 * @buflen: Length of @buf
364 * Parse a signature embedded in variable's value and instantiate
365 * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
366 * pkcs7's signedData, some header needed be prepended for correctly
367 * parsing authentication data, particularly for variable's.
369 * Return: Pointer to pkcs7_message structure on success, NULL on error
371 static struct pkcs7_message *efi_variable_parse_signature(const void *buf,
376 struct pkcs7_message *msg;
379 * This is the best assumption to check if the binary is
380 * already in a form of pkcs7's signedData.
382 if (buflen > sizeof(pkcs7_hdr) &&
383 !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
384 msg = pkcs7_parse_message(buf, buflen);
389 * Otherwise, we should add a dummy prefix sequence for pkcs7
390 * message parser to be able to process.
391 * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
392 * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
394 * The header should be composed in a more refined manner.
396 debug("Makeshift prefix added to authentication data\n");
397 ebuflen = sizeof(pkcs7_hdr) + buflen;
398 if (ebuflen <= 0x7f) {
399 debug("Data is too short\n");
403 ebuf = malloc(ebuflen);
405 debug("Out of memory\n");
409 memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
410 memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
412 ebuf[2] = (len >> 8) & 0xff;
413 ebuf[3] = len & 0xff;
414 len = ebuflen - 0x13;
415 ebuf[0x11] = (len >> 8) & 0xff;
416 ebuf[0x12] = len & 0xff;
418 msg = pkcs7_parse_message(ebuf, ebuflen);
430 * efi_variable_authenticate - authenticate a variable
431 * @variable: Variable name in u16
432 * @vendor: Guid of variable
433 * @data_size: Size of @data
434 * @data: Pointer to variable's value
435 * @given_attr: Attributes to be given at SetVariable()
436 * @env_attr: Attributes that an existing variable holds
437 * @time: signed time that an existing variable holds
439 * Called by efi_set_variable() to verify that the input is correct.
440 * Will replace the given data pointer with another that points to
441 * the actual data to store in the internal memory.
442 * On success, @data and @data_size will be replaced with variable's
443 * actual data, excluding authentication data, and its size, and variable's
444 * attributes and signed time will also be returned in @env_attr and @time,
447 * Return: status code
449 static efi_status_t efi_variable_authenticate(u16 *variable,
450 const efi_guid_t *vendor,
451 efi_uintn_t *data_size,
452 const void **data, u32 given_attr,
453 u32 *env_attr, u64 *time)
455 const struct efi_variable_authentication_2 *auth;
456 struct efi_signature_store *truststore, *truststore2;
457 struct pkcs7_message *var_sig;
458 struct efi_image_regions *regs;
459 struct efi_time timestamp;
468 ret = EFI_SECURITY_VIOLATION;
470 if (*data_size < sizeof(struct efi_variable_authentication_2))
473 /* authentication data */
475 if (*data_size < (sizeof(auth->time_stamp)
476 + auth->auth_info.hdr.dwLength))
479 if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
482 *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
483 *data_size -= (sizeof(auth->time_stamp)
484 + auth->auth_info.hdr.dwLength);
486 memcpy(×tamp, &auth->time_stamp, sizeof(timestamp));
487 memset(&tm, 0, sizeof(tm));
488 tm.tm_year = timestamp.year;
489 tm.tm_mon = timestamp.month;
490 tm.tm_mday = timestamp.day;
491 tm.tm_hour = timestamp.hour;
492 tm.tm_min = timestamp.minute;
493 tm.tm_sec = timestamp.second;
494 new_time = rtc_mktime(&tm);
496 if (!efi_secure_boot_enabled()) {
497 /* finished checking */
502 if (new_time <= *time)
505 /* data to be digested */
506 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
510 efi_image_region_add(regs, (uint8_t *)variable,
512 + u16_strlen(variable) * sizeof(u16), 1);
513 efi_image_region_add(regs, (uint8_t *)vendor,
514 (uint8_t *)vendor + sizeof(*vendor), 1);
515 efi_image_region_add(regs, (uint8_t *)&given_attr,
516 (uint8_t *)&given_attr + sizeof(given_attr), 1);
517 efi_image_region_add(regs, (uint8_t *)×tamp,
518 (uint8_t *)×tamp + sizeof(timestamp), 1);
519 efi_image_region_add(regs, (uint8_t *)*data,
520 (uint8_t *)*data + *data_size, 1);
522 /* variable's signature list */
523 if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
525 var_sig = efi_variable_parse_signature(auth->auth_info.cert_data,
526 auth->auth_info.hdr.dwLength
527 - sizeof(auth->auth_info));
529 debug("Parsing variable's signature failed\n");
533 /* signature database used for authentication */
534 if (u16_strcmp(variable, L"PK") == 0 ||
535 u16_strcmp(variable, L"KEK") == 0) {
537 truststore = efi_sigstore_parse_sigdb(L"PK");
540 } else if (u16_strcmp(variable, L"db") == 0 ||
541 u16_strcmp(variable, L"dbx") == 0) {
542 /* with PK and KEK */
543 truststore = efi_sigstore_parse_sigdb(L"KEK");
544 truststore2 = efi_sigstore_parse_sigdb(L"PK");
550 truststore = truststore2;
554 /* TODO: support private authenticated variables */
558 /* verify signature */
559 if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) {
563 efi_signature_verify_with_sigdb(regs, var_sig,
564 truststore2, NULL)) {
567 debug("Verifying variable's signature failed\n");
572 /* finished checking */
573 *time = rtc_mktime(&tm);
577 efi_sigstore_free(truststore);
578 efi_sigstore_free(truststore2);
579 pkcs7_free_message(var_sig);
585 static efi_status_t efi_variable_authenticate(u16 *variable,
586 const efi_guid_t *vendor,
587 efi_uintn_t *data_size,
588 const void **data, u32 given_attr,
589 u32 *env_attr, u64 *time)
593 #endif /* CONFIG_EFI_SECURE_BOOT */
595 static efi_status_t efi_get_variable_common(u16 *variable_name,
596 const efi_guid_t *vendor,
598 efi_uintn_t *data_size, void *data)
602 unsigned long in_size;
603 const char *val = NULL, *s;
607 if (!variable_name || !vendor || !data_size)
608 return EFI_EXIT(EFI_INVALID_PARAMETER);
610 ret = efi_to_native(&native_name, variable_name, vendor);
614 EFI_PRINT("get '%s'\n", native_name);
616 val = env_get(native_name);
619 return EFI_NOT_FOUND;
621 val = parse_attr(val, &attr, &time);
623 in_size = *data_size;
625 if ((s = prefix(val, "(blob)"))) {
626 size_t len = strlen(s);
628 /* number of hexadecimal digits must be even */
630 return EFI_DEVICE_ERROR;
632 /* two characters per byte: */
637 ret = EFI_BUFFER_TOO_SMALL;
642 debug("Variable with no data shouldn't exist.\n");
643 return EFI_INVALID_PARAMETER;
646 if (hex2bin(data, s, len))
647 return EFI_DEVICE_ERROR;
649 EFI_PRINT("got value: \"%s\"\n", s);
650 } else if ((s = prefix(val, "(utf8)"))) {
651 unsigned len = strlen(s) + 1;
656 ret = EFI_BUFFER_TOO_SMALL;
661 debug("Variable with no data shouldn't exist.\n");
662 return EFI_INVALID_PARAMETER;
665 memcpy(data, s, len);
666 ((char *)data)[len] = '\0';
668 EFI_PRINT("got value: \"%s\"\n", (char *)data);
670 EFI_PRINT("invalid value: '%s'\n", val);
671 return EFI_DEVICE_ERROR;
676 *attributes = attr & EFI_VARIABLE_MASK;
682 * efi_efi_get_variable() - retrieve value of a UEFI variable
684 * This function implements the GetVariable runtime service.
686 * See the Unified Extensible Firmware Interface (UEFI) specification for
689 * @variable_name: name of the variable
690 * @vendor: vendor GUID
691 * @attributes: attributes of the variable
692 * @data_size: size of the buffer to which the variable value is copied
693 * @data: buffer to which the variable value is copied
694 * Return: status code
696 efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
697 const efi_guid_t *vendor, u32 *attributes,
698 efi_uintn_t *data_size, void *data)
702 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
705 ret = efi_get_variable_common(variable_name, vendor, attributes,
707 return EFI_EXIT(ret);
710 static char *efi_variables_list;
711 static char *efi_cur_variable;
714 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
716 * @variable: whole data of u-boot variable (ie. name=value)
717 * @variable_name_size: size of variable_name buffer in byte
718 * @variable_name: name of uefi variable in u16, null-terminated
719 * @vendor: vendor's guid
720 * @attributes: attributes
722 * A uefi variable is encoded into a u-boot variable as described above.
723 * This function parses such a u-boot variable and retrieve uefi-related
724 * information into respective parameters. In return, variable_name_size
725 * is the size of variable name including NULL.
727 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
728 * the entire variable list has been returned,
729 * otherwise non-zero status code
731 static efi_status_t parse_uboot_variable(char *variable,
732 efi_uintn_t *variable_name_size,
734 const efi_guid_t *vendor,
737 char *guid, *name, *end, c;
739 efi_uintn_t old_variable_name_size;
743 guid = strchr(variable, '_');
745 return EFI_INVALID_PARAMETER;
747 name = strchr(guid, '_');
749 return EFI_INVALID_PARAMETER;
751 end = strchr(name, '=');
753 return EFI_INVALID_PARAMETER;
755 name_len = end - name;
756 old_variable_name_size = *variable_name_size;
757 *variable_name_size = sizeof(u16) * (name_len + 1);
758 if (old_variable_name_size < *variable_name_size)
759 return EFI_BUFFER_TOO_SMALL;
761 end++; /* point to value */
765 utf8_utf16_strncpy(&p, name, name_len);
766 variable_name[name_len] = 0;
770 *(name - 1) = '\0'; /* guid need be null-terminated here */
771 if (uuid_str_to_bin(guid, (unsigned char *)vendor,
772 UUID_STR_FORMAT_GUID))
773 /* The only error would be EINVAL. */
774 return EFI_INVALID_PARAMETER;
778 parse_attr(end, attributes, &time);
784 * efi_get_next_variable_name() - enumerate the current variable names
786 * @variable_name_size: size of variable_name buffer in byte
787 * @variable_name: name of uefi variable's name in u16
788 * @vendor: vendor's guid
790 * This function implements the GetNextVariableName service.
792 * See the Unified Extensible Firmware Interface (UEFI) specification for
795 * Return: status code
797 efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
801 char *native_name, *variable;
802 ssize_t name_len, list_len;
804 char * const regexlist[] = {regex};
809 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
811 if (!variable_name_size || !variable_name || !vendor)
812 return EFI_EXIT(EFI_INVALID_PARAMETER);
814 if (variable_name[0]) {
815 /* check null-terminated string */
816 for (i = 0; i < *variable_name_size; i++)
817 if (!variable_name[i])
819 if (i >= *variable_name_size)
820 return EFI_EXIT(EFI_INVALID_PARAMETER);
822 /* search for the last-returned variable */
823 ret = efi_to_native(&native_name, variable_name, vendor);
825 return EFI_EXIT(ret);
827 name_len = strlen(native_name);
828 for (variable = efi_variables_list; variable && *variable;) {
829 if (!strncmp(variable, native_name, name_len) &&
830 variable[name_len] == '=')
833 variable = strchr(variable, '\n');
839 if (!(variable && *variable))
840 return EFI_EXIT(EFI_INVALID_PARAMETER);
843 variable = strchr(variable, '\n');
846 if (!(variable && *variable))
847 return EFI_EXIT(EFI_NOT_FOUND);
850 *new search: free a list used in the previous search
852 free(efi_variables_list);
853 efi_variables_list = NULL;
854 efi_cur_variable = NULL;
856 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
857 list_len = hexport_r(&env_htab, '\n',
858 H_MATCH_REGEX | H_MATCH_KEY,
859 &efi_variables_list, 0, 1, regexlist);
862 return EFI_EXIT(EFI_NOT_FOUND);
864 variable = efi_variables_list;
867 ret = parse_uboot_variable(variable, variable_name_size, variable_name,
868 vendor, &attributes);
870 return EFI_EXIT(ret);
873 static efi_status_t efi_set_variable_common(u16 *variable_name,
874 const efi_guid_t *vendor,
876 efi_uintn_t data_size,
880 char *native_name = NULL, *old_data = NULL, *val = NULL, *s;
881 efi_uintn_t old_size;
885 efi_status_t ret = EFI_SUCCESS;
887 debug("%s: set '%s'\n", __func__, native_name);
889 if (!variable_name || !*variable_name || !vendor ||
890 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
891 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
892 ret = EFI_INVALID_PARAMETER;
896 ret = efi_to_native(&native_name, variable_name, vendor);
900 /* check if a variable exists */
903 ret = efi_get_variable_common(variable_name, vendor, &attr,
905 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
906 attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
907 delete = !append && (!data_size || !attributes);
909 /* check attributes */
911 if (ro_check && (attr & READ_ONLY)) {
912 ret = EFI_WRITE_PROTECTED;
916 /* attributes won't be changed */
918 ((ro_check && attr != attributes) ||
919 (!ro_check && ((attr & ~(u32)READ_ONLY)
920 != (attributes & ~(u32)READ_ONLY))))) {
921 ret = EFI_INVALID_PARAMETER;
925 if (delete || append) {
927 * Trying to delete or to update a non-existent
935 if (((!u16_strcmp(variable_name, L"PK") ||
936 !u16_strcmp(variable_name, L"KEK")) &&
937 !guidcmp(vendor, &efi_global_variable_guid)) ||
938 ((!u16_strcmp(variable_name, L"db") ||
939 !u16_strcmp(variable_name, L"dbx")) &&
940 !guidcmp(vendor, &efi_guid_image_security_database))) {
941 /* authentication is mandatory */
943 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
944 debug("%ls: AUTHENTICATED_WRITE_ACCESS required\n",
946 ret = EFI_INVALID_PARAMETER;
951 /* authenticate a variable */
952 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
953 if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
954 ret = EFI_INVALID_PARAMETER;
958 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
959 ret = efi_variable_authenticate(variable_name, vendor,
963 if (ret != EFI_SUCCESS)
966 /* last chance to check for delete */
972 (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
973 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
974 debug("Secure boot is not configured\n");
975 ret = EFI_INVALID_PARAMETER;
980 /* delete a variable */
982 /* !old_size case has been handled before */
989 old_data = malloc(old_size);
991 ret = EFI_OUT_OF_RESOURCES;
994 ret = efi_get_variable_common(variable_name, vendor,
995 &attr, &old_size, old_data);
996 if (ret != EFI_SUCCESS)
1002 val = malloc(2 * old_size + 2 * data_size
1003 + strlen("{ro,run,boot,nv,time=0123456701234567}(blob)")
1006 ret = EFI_OUT_OF_RESOURCES;
1015 attributes &= (READ_ONLY |
1016 EFI_VARIABLE_NON_VOLATILE |
1017 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1018 EFI_VARIABLE_RUNTIME_ACCESS |
1019 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS);
1020 s += sprintf(s, "{");
1021 while (attributes) {
1022 attr = 1 << (ffs(attributes) - 1);
1024 if (attr == READ_ONLY) {
1025 s += sprintf(s, "ro");
1026 } else if (attr == EFI_VARIABLE_NON_VOLATILE) {
1027 s += sprintf(s, "nv");
1028 } else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) {
1029 s += sprintf(s, "boot");
1030 } else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) {
1031 s += sprintf(s, "run");
1033 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
1034 s += sprintf(s, "time=");
1035 s = bin2hex(s, (u8 *)&time, sizeof(time));
1038 attributes &= ~attr;
1040 s += sprintf(s, ",");
1042 s += sprintf(s, "}");
1043 s += sprintf(s, "(blob)");
1045 /* store payload: */
1047 s = bin2hex(s, old_data, old_size);
1048 s = bin2hex(s, data, data_size);
1051 EFI_PRINT("setting: %s=%s\n", native_name, val);
1054 if (env_set(native_name, val)) {
1055 ret = EFI_DEVICE_ERROR;
1057 bool vendor_keys_modified = false;
1059 if ((u16_strcmp(variable_name, L"PK") == 0 &&
1060 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1061 ret = efi_transfer_secure_state(
1062 (delete ? EFI_MODE_SETUP :
1064 if (ret != EFI_SUCCESS)
1067 if (efi_secure_mode != EFI_MODE_SETUP)
1068 vendor_keys_modified = true;
1069 } else if ((u16_strcmp(variable_name, L"KEK") == 0 &&
1070 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1071 if (efi_secure_mode != EFI_MODE_SETUP)
1072 vendor_keys_modified = true;
1075 /* update VendorKeys */
1076 if (vendor_keys_modified & efi_vendor_keys) {
1077 efi_vendor_keys = 0;
1078 ret = efi_set_variable_common(
1080 &efi_global_variable_guid,
1081 EFI_VARIABLE_BOOTSERVICE_ACCESS
1082 | EFI_VARIABLE_RUNTIME_ACCESS
1084 sizeof(efi_vendor_keys),
1101 * efi_set_variable() - set value of a UEFI variable
1103 * This function implements the SetVariable runtime service.
1105 * See the Unified Extensible Firmware Interface (UEFI) specification for
1108 * @variable_name: name of the variable
1109 * @vendor: vendor GUID
1110 * @attributes: attributes of the variable
1111 * @data_size: size of the buffer with the variable value
1112 * @data: buffer with the variable value
1113 * Return: status code
1115 efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
1116 const efi_guid_t *vendor, u32 attributes,
1117 efi_uintn_t data_size, const void *data)
1119 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
1122 /* READ_ONLY bit is not part of API */
1123 attributes &= ~(u32)READ_ONLY;
1125 return EFI_EXIT(efi_set_variable_common(variable_name, vendor,
1126 attributes, data_size, data,
1131 * efi_query_variable_info() - get information about EFI variables
1133 * This function implements the QueryVariableInfo() runtime service.
1135 * See the Unified Extensible Firmware Interface (UEFI) specification for
1138 * @attributes: bitmask to select variables to be
1140 * @maximum_variable_storage_size: maximum size of storage area for the
1141 * selected variable types
1142 * @remaining_variable_storage_size: remaining size of storage are for the
1143 * selected variable types
1144 * @maximum_variable_size: maximum size of a variable of the
1146 * Returns: status code
1148 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
1150 u64 *maximum_variable_storage_size,
1151 u64 *remaining_variable_storage_size,
1152 u64 *maximum_variable_size)
1154 return EFI_UNSUPPORTED;
1158 * efi_get_variable_runtime() - runtime implementation of GetVariable()
1160 * @variable_name: name of the variable
1161 * @vendor: vendor GUID
1162 * @attributes: attributes of the variable
1163 * @data_size: size of the buffer to which the variable value is copied
1164 * @data: buffer to which the variable value is copied
1165 * Return: status code
1167 static efi_status_t __efi_runtime EFIAPI
1168 efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1169 u32 *attributes, efi_uintn_t *data_size, void *data)
1171 return EFI_UNSUPPORTED;
1175 * efi_get_next_variable_name_runtime() - runtime implementation of
1178 * @variable_name_size: size of variable_name buffer in byte
1179 * @variable_name: name of uefi variable's name in u16
1180 * @vendor: vendor's guid
1181 * Return: status code
1183 static efi_status_t __efi_runtime EFIAPI
1184 efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
1185 u16 *variable_name, efi_guid_t *vendor)
1187 return EFI_UNSUPPORTED;
1191 * efi_set_variable_runtime() - runtime implementation of SetVariable()
1193 * @variable_name: name of the variable
1194 * @vendor: vendor GUID
1195 * @attributes: attributes of the variable
1196 * @data_size: size of the buffer with the variable value
1197 * @data: buffer with the variable value
1198 * Return: status code
1200 static efi_status_t __efi_runtime EFIAPI
1201 efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1202 u32 attributes, efi_uintn_t data_size,
1205 return EFI_UNSUPPORTED;
1209 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
1211 void efi_variables_boot_exit_notify(void)
1213 efi_runtime_services.get_variable = efi_get_variable_runtime;
1214 efi_runtime_services.get_next_variable_name =
1215 efi_get_next_variable_name_runtime;
1216 efi_runtime_services.set_variable = efi_set_variable_runtime;
1217 efi_update_table_header_crc32(&efi_runtime_services.hdr);
1221 * efi_init_variables() - initialize variable services
1223 * Return: status code
1225 efi_status_t efi_init_variables(void)
1229 ret = efi_init_secure_state();