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>
15 #include <linux/compat.h>
16 #include <u-boot/crc.h>
17 #include "../lib/crypto/pkcs7_parser.h"
19 enum efi_secure_mode {
26 const efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID;
27 static bool efi_secure_boot;
28 static int efi_secure_mode;
30 #define READ_ONLY BIT(31)
33 * Mapping between EFI variables and u-boot variables:
35 * efi_$guid_$varname = {attributes}(type)value
39 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
40 * "{ro,boot,run}(blob)0000000000000000"
41 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
44 * The attributes are a comma separated list of these possible
48 * + boot - boot-services access
49 * + run - runtime access
51 * NOTE: with current implementation, no variables are available after
52 * ExitBootServices, and all are persisted (if possible).
54 * If not specified, the attributes default to "{boot}".
56 * The required type is one of:
58 * + utf8 - raw utf8 string
59 * + blob - arbitrary length hex string
61 * Maybe a utf16 type would be useful to for a string value to be auto
65 #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
68 * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
71 * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
72 * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
73 * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
75 * @native: pointer to pointer to U-Boot variable name
76 * @variable_name: UEFI variable name
77 * @vendor: vendor GUID
80 static efi_status_t efi_to_native(char **native, const u16 *variable_name,
81 const efi_guid_t *vendor)
86 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
87 *native = malloc(len);
89 return EFI_OUT_OF_RESOURCES;
92 pos += sprintf(pos, "efi_%pUl_", vendor);
93 utf16_utf8_strcpy(&pos, variable_name);
99 * prefix() - skip over prefix
101 * Skip over a prefix string.
103 * @str: string with prefix
104 * @prefix: prefix string
105 * Return: string without prefix, or NULL if prefix not found
107 static const char *prefix(const char *str, const char *prefix)
109 size_t n = strlen(prefix);
110 if (!strncmp(prefix, str, n))
116 * parse_attr() - decode attributes part of variable value
118 * Convert the string encoded attributes of a UEFI variable to a bit mask.
119 * TODO: Several attributes are not supported.
121 * @str: value of U-Boot variable
122 * @attrp: pointer to UEFI attributes
123 * @timep: pointer to time attribute
124 * Return: pointer to remainder of U-Boot variable value
126 static const char *parse_attr(const char *str, u32 *attrp, u64 *timep)
132 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
136 while (*str == sep) {
141 if ((s = prefix(str, "ro"))) {
143 } else if ((s = prefix(str, "nv"))) {
144 attr |= EFI_VARIABLE_NON_VOLATILE;
145 } else if ((s = prefix(str, "boot"))) {
146 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
147 } else if ((s = prefix(str, "run"))) {
148 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
149 } else if ((s = prefix(str, "time="))) {
150 attr |= EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
151 hex2bin((u8 *)timep, s, sizeof(*timep));
152 s += sizeof(*timep) * 2;
153 } else if (*str == '}') {
156 printf("invalid attribute: %s\n", str);
171 static efi_status_t efi_set_variable_internal(u16 *variable_name,
172 const efi_guid_t *vendor,
174 efi_uintn_t data_size,
179 * efi_transfer_secure_state - handle a secure boot state transition
182 * Depending on @mode, secure boot related variables are updated.
183 * Those variables are *read-only* for users, efi_set_variable_internal()
186 * Return: EFI_SUCCESS on success, status code (negative) on error
188 static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
194 debug("Secure state from %d to %d\n", efi_secure_mode, mode);
196 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
197 EFI_VARIABLE_RUNTIME_ACCESS;
198 if (mode == EFI_MODE_DEPLOYED) {
200 ret = efi_set_variable_internal(L"SecureBoot",
201 &efi_global_variable_guid,
202 attributes | READ_ONLY,
205 if (ret != EFI_SUCCESS)
208 ret = efi_set_variable_internal(L"SetupMode",
209 &efi_global_variable_guid,
210 attributes | READ_ONLY,
213 if (ret != EFI_SUCCESS)
216 ret = efi_set_variable_internal(L"AuditMode",
217 &efi_global_variable_guid,
218 attributes | READ_ONLY,
221 if (ret != EFI_SUCCESS)
224 ret = efi_set_variable_internal(L"DeployedMode",
225 &efi_global_variable_guid,
226 attributes | READ_ONLY,
229 if (ret != EFI_SUCCESS)
232 efi_secure_boot = true;
233 } else if (mode == EFI_MODE_AUDIT) {
234 ret = efi_set_variable_internal(L"PK",
235 &efi_global_variable_guid,
239 if (ret != EFI_SUCCESS)
242 ret = efi_set_variable_internal(L"SecureBoot",
243 &efi_global_variable_guid,
244 attributes | READ_ONLY,
247 if (ret != EFI_SUCCESS)
250 ret = efi_set_variable_internal(L"SetupMode",
251 &efi_global_variable_guid,
252 attributes | READ_ONLY,
255 if (ret != EFI_SUCCESS)
258 ret = efi_set_variable_internal(L"AuditMode",
259 &efi_global_variable_guid,
260 attributes | READ_ONLY,
263 if (ret != EFI_SUCCESS)
266 ret = efi_set_variable_internal(L"DeployedMode",
267 &efi_global_variable_guid,
268 attributes | READ_ONLY,
271 if (ret != EFI_SUCCESS)
274 efi_secure_boot = true;
275 } else if (mode == EFI_MODE_USER) {
277 ret = efi_set_variable_internal(L"SecureBoot",
278 &efi_global_variable_guid,
279 attributes | READ_ONLY,
282 if (ret != EFI_SUCCESS)
285 ret = efi_set_variable_internal(L"SetupMode",
286 &efi_global_variable_guid,
287 attributes | READ_ONLY,
290 if (ret != EFI_SUCCESS)
293 ret = efi_set_variable_internal(L"AuditMode",
294 &efi_global_variable_guid,
298 if (ret != EFI_SUCCESS)
301 ret = efi_set_variable_internal(L"DeployedMode",
302 &efi_global_variable_guid,
306 if (ret != EFI_SUCCESS)
309 efi_secure_boot = true;
310 } else if (mode == EFI_MODE_SETUP) {
312 ret = efi_set_variable_internal(L"SecureBoot",
313 &efi_global_variable_guid,
314 attributes | READ_ONLY,
317 if (ret != EFI_SUCCESS)
320 ret = efi_set_variable_internal(L"SetupMode",
321 &efi_global_variable_guid,
322 attributes | READ_ONLY,
325 if (ret != EFI_SUCCESS)
328 ret = efi_set_variable_internal(L"AuditMode",
329 &efi_global_variable_guid,
333 if (ret != EFI_SUCCESS)
336 ret = efi_set_variable_internal(L"DeployedMode",
337 &efi_global_variable_guid,
338 attributes | READ_ONLY,
341 if (ret != EFI_SUCCESS)
344 return EFI_INVALID_PARAMETER;
350 /* TODO: What action should be taken here? */
351 printf("ERROR: Secure state transition failed\n");
356 * efi_init_secure_state - initialize secure boot state
358 * Return: EFI_SUCCESS on success, status code (negative) on error
360 static efi_status_t efi_init_secure_state(void)
362 efi_uintn_t size = 0;
365 ret = EFI_CALL(efi_get_variable(L"PK", &efi_global_variable_guid,
367 if (ret == EFI_BUFFER_TOO_SMALL && IS_ENABLED(CONFIG_EFI_SECURE_BOOT))
368 ret = efi_transfer_secure_state(EFI_MODE_USER);
370 ret = efi_transfer_secure_state(EFI_MODE_SETUP);
376 * efi_secure_boot_enabled - return if secure boot is enabled or not
378 * Return: true if enabled, false if disabled
380 bool efi_secure_boot_enabled(void)
382 return efi_secure_boot;
385 #ifdef CONFIG_EFI_SECURE_BOOT
386 static u8 pkcs7_hdr[] = {
388 0x30, 0x82, 0x05, 0xc7,
389 /* OID: pkcs7-signedData */
390 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
391 /* Context Structured? */
392 0xa0, 0x82, 0x05, 0xb8,
396 * efi_variable_parse_signature - parse a signature in variable
397 * @buf: Pointer to variable's value
398 * @buflen: Length of @buf
400 * Parse a signature embedded in variable's value and instantiate
401 * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
402 * pkcs7's signedData, some header needed be prepended for correctly
403 * parsing authentication data, particularly for variable's.
405 * Return: Pointer to pkcs7_message structure on success, NULL on error
407 static struct pkcs7_message *efi_variable_parse_signature(const void *buf,
412 struct pkcs7_message *msg;
415 * This is the best assumption to check if the binary is
416 * already in a form of pkcs7's signedData.
418 if (buflen > sizeof(pkcs7_hdr) &&
419 !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
420 msg = pkcs7_parse_message(buf, buflen);
425 * Otherwise, we should add a dummy prefix sequence for pkcs7
426 * message parser to be able to process.
427 * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
428 * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
430 * The header should be composed in a more refined manner.
432 debug("Makeshift prefix added to authentication data\n");
433 ebuflen = sizeof(pkcs7_hdr) + buflen;
434 if (ebuflen <= 0x7f) {
435 debug("Data is too short\n");
439 ebuf = malloc(ebuflen);
441 debug("Out of memory\n");
445 memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
446 memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
448 ebuf[2] = (len >> 8) & 0xff;
449 ebuf[3] = len & 0xff;
450 len = ebuflen - 0x13;
451 ebuf[0x11] = (len >> 8) & 0xff;
452 ebuf[0x12] = len & 0xff;
454 msg = pkcs7_parse_message(ebuf, ebuflen);
466 * efi_variable_authenticate - authenticate a variable
467 * @variable: Variable name in u16
468 * @vendor: Guid of variable
469 * @data_size: Size of @data
470 * @data: Pointer to variable's value
471 * @given_attr: Attributes to be given at SetVariable()
472 * @env_attr: Attributes that an existing variable holds
473 * @time: signed time that an existing variable holds
475 * Called by efi_set_variable() to verify that the input is correct.
476 * Will replace the given data pointer with another that points to
477 * the actual data to store in the internal memory.
478 * On success, @data and @data_size will be replaced with variable's
479 * actual data, excluding authentication data, and its size, and variable's
480 * attributes and signed time will also be returned in @env_attr and @time,
483 * Return: EFI_SUCCESS on success, status code (negative) on error
485 static efi_status_t efi_variable_authenticate(u16 *variable,
486 const efi_guid_t *vendor,
487 efi_uintn_t *data_size,
488 const void **data, u32 given_attr,
489 u32 *env_attr, u64 *time)
491 const struct efi_variable_authentication_2 *auth;
492 struct efi_signature_store *truststore, *truststore2;
493 struct pkcs7_message *var_sig;
494 struct efi_image_regions *regs;
495 struct efi_time timestamp;
504 ret = EFI_SECURITY_VIOLATION;
506 if (*data_size < sizeof(struct efi_variable_authentication_2))
509 /* authentication data */
511 if (*data_size < (sizeof(auth->time_stamp)
512 + auth->auth_info.hdr.dwLength))
515 if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
518 *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
519 *data_size -= (sizeof(auth->time_stamp)
520 + auth->auth_info.hdr.dwLength);
522 memcpy(×tamp, &auth->time_stamp, sizeof(timestamp));
523 memset(&tm, 0, sizeof(tm));
524 tm.tm_year = timestamp.year;
525 tm.tm_mon = timestamp.month;
526 tm.tm_mday = timestamp.day;
527 tm.tm_hour = timestamp.hour;
528 tm.tm_min = timestamp.minute;
529 tm.tm_sec = timestamp.second;
530 new_time = rtc_mktime(&tm);
532 if (!efi_secure_boot_enabled()) {
533 /* finished checking */
538 if (new_time <= *time)
541 /* data to be digested */
542 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
546 efi_image_region_add(regs, (uint8_t *)variable,
548 + u16_strlen(variable) * sizeof(u16), 1);
549 efi_image_region_add(regs, (uint8_t *)vendor,
550 (uint8_t *)vendor + sizeof(*vendor), 1);
551 efi_image_region_add(regs, (uint8_t *)&given_attr,
552 (uint8_t *)&given_attr + sizeof(given_attr), 1);
553 efi_image_region_add(regs, (uint8_t *)×tamp,
554 (uint8_t *)×tamp + sizeof(timestamp), 1);
555 efi_image_region_add(regs, (uint8_t *)*data,
556 (uint8_t *)*data + *data_size, 1);
558 /* variable's signature list */
559 if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
561 var_sig = efi_variable_parse_signature(auth->auth_info.cert_data,
562 auth->auth_info.hdr.dwLength
563 - sizeof(auth->auth_info));
564 if (IS_ERR(var_sig)) {
565 debug("Parsing variable's signature failed\n");
570 /* signature database used for authentication */
571 if (u16_strcmp(variable, L"PK") == 0 ||
572 u16_strcmp(variable, L"KEK") == 0) {
574 truststore = efi_sigstore_parse_sigdb(L"PK");
577 } else if (u16_strcmp(variable, L"db") == 0 ||
578 u16_strcmp(variable, L"dbx") == 0) {
579 /* with PK and KEK */
580 truststore = efi_sigstore_parse_sigdb(L"KEK");
581 truststore2 = efi_sigstore_parse_sigdb(L"PK");
587 truststore = truststore2;
591 /* TODO: support private authenticated variables */
595 /* verify signature */
596 if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) {
600 efi_signature_verify_with_sigdb(regs, var_sig,
601 truststore2, NULL)) {
604 debug("Verifying variable's signature failed\n");
609 /* finished checking */
610 *time = rtc_mktime(&tm);
614 efi_sigstore_free(truststore);
615 efi_sigstore_free(truststore2);
616 pkcs7_free_message(var_sig);
622 static efi_status_t efi_variable_authenticate(u16 *variable,
623 const efi_guid_t *vendor,
624 efi_uintn_t *data_size,
625 const void **data, u32 given_attr,
626 u32 *env_attr, u64 *time)
630 #endif /* CONFIG_EFI_SECURE_BOOT */
633 efi_status_t EFIAPI efi_get_variable_common(u16 *variable_name,
634 const efi_guid_t *vendor,
636 efi_uintn_t *data_size, void *data,
637 bool is_non_volatile)
641 unsigned long in_size;
642 const char *val = NULL, *s;
646 if (!variable_name || !vendor || !data_size)
647 return EFI_EXIT(EFI_INVALID_PARAMETER);
649 ret = efi_to_native(&native_name, variable_name, vendor);
653 EFI_PRINT("get '%s'\n", native_name);
655 val = env_get(native_name);
658 return EFI_NOT_FOUND;
660 val = parse_attr(val, &attr, &time);
662 in_size = *data_size;
664 if ((s = prefix(val, "(blob)"))) {
665 size_t len = strlen(s);
667 /* number of hexadecimal digits must be even */
669 return EFI_DEVICE_ERROR;
671 /* two characters per byte: */
676 ret = EFI_BUFFER_TOO_SMALL;
681 debug("Variable with no data shouldn't exist.\n");
682 return EFI_INVALID_PARAMETER;
685 if (hex2bin(data, s, len))
686 return EFI_DEVICE_ERROR;
688 EFI_PRINT("got value: \"%s\"\n", s);
689 } else if ((s = prefix(val, "(utf8)"))) {
690 unsigned len = strlen(s) + 1;
695 ret = EFI_BUFFER_TOO_SMALL;
700 debug("Variable with no data shouldn't exist.\n");
701 return EFI_INVALID_PARAMETER;
704 memcpy(data, s, len);
705 ((char *)data)[len] = '\0';
707 EFI_PRINT("got value: \"%s\"\n", (char *)data);
709 EFI_PRINT("invalid value: '%s'\n", val);
710 return EFI_DEVICE_ERROR;
715 *attributes = attr & EFI_VARIABLE_MASK;
721 efi_status_t EFIAPI efi_get_volatile_variable(u16 *variable_name,
722 const efi_guid_t *vendor,
724 efi_uintn_t *data_size,
727 return efi_get_variable_common(variable_name, vendor, attributes,
728 data_size, data, false);
731 efi_status_t EFIAPI efi_get_nonvolatile_variable(u16 *variable_name,
732 const efi_guid_t *vendor,
734 efi_uintn_t *data_size,
737 return efi_get_variable_common(variable_name, vendor, attributes,
738 data_size, data, true);
742 * efi_efi_get_variable() - retrieve value of a UEFI variable
744 * This function implements the GetVariable runtime service.
746 * See the Unified Extensible Firmware Interface (UEFI) specification for
749 * @variable_name: name of the variable
750 * @vendor: vendor GUID
751 * @attributes: attributes of the variable
752 * @data_size: size of the buffer to which the variable value is copied
753 * @data: buffer to which the variable value is copied
754 * Return: status code
756 efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
757 const efi_guid_t *vendor, u32 *attributes,
758 efi_uintn_t *data_size, void *data)
762 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
765 ret = efi_get_volatile_variable(variable_name, vendor, attributes,
767 if (ret == EFI_NOT_FOUND)
768 ret = efi_get_nonvolatile_variable(variable_name, vendor,
769 attributes, data_size, data);
771 return EFI_EXIT(ret);
774 static char *efi_variables_list;
775 static char *efi_cur_variable;
778 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
780 * @variable: whole data of u-boot variable (ie. name=value)
781 * @variable_name_size: size of variable_name buffer in byte
782 * @variable_name: name of uefi variable in u16, null-terminated
783 * @vendor: vendor's guid
784 * @attributes: attributes
786 * A uefi variable is encoded into a u-boot variable as described above.
787 * This function parses such a u-boot variable and retrieve uefi-related
788 * information into respective parameters. In return, variable_name_size
789 * is the size of variable name including NULL.
791 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
792 * the entire variable list has been returned,
793 * otherwise non-zero status code
795 static efi_status_t parse_uboot_variable(char *variable,
796 efi_uintn_t *variable_name_size,
798 const efi_guid_t *vendor,
801 char *guid, *name, *end, c;
803 efi_uintn_t old_variable_name_size;
807 guid = strchr(variable, '_');
809 return EFI_INVALID_PARAMETER;
811 name = strchr(guid, '_');
813 return EFI_INVALID_PARAMETER;
815 end = strchr(name, '=');
817 return EFI_INVALID_PARAMETER;
819 name_len = end - name;
820 old_variable_name_size = *variable_name_size;
821 *variable_name_size = sizeof(u16) * (name_len + 1);
822 if (old_variable_name_size < *variable_name_size)
823 return EFI_BUFFER_TOO_SMALL;
825 end++; /* point to value */
829 utf8_utf16_strncpy(&p, name, name_len);
830 variable_name[name_len] = 0;
834 *(name - 1) = '\0'; /* guid need be null-terminated here */
835 uuid_str_to_bin(guid, (unsigned char *)vendor, UUID_STR_FORMAT_GUID);
839 parse_attr(end, attributes, &time);
845 * efi_get_next_variable_name() - enumerate the current variable names
847 * @variable_name_size: size of variable_name buffer in byte
848 * @variable_name: name of uefi variable's name in u16
849 * @vendor: vendor's guid
851 * This function implements the GetNextVariableName service.
853 * See the Unified Extensible Firmware Interface (UEFI) specification for
856 * Return: status code
858 efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
862 char *native_name, *variable;
863 ssize_t name_len, list_len;
865 char * const regexlist[] = {regex};
870 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
872 if (!variable_name_size || !variable_name || !vendor)
873 return EFI_EXIT(EFI_INVALID_PARAMETER);
875 if (variable_name[0]) {
876 /* check null-terminated string */
877 for (i = 0; i < *variable_name_size; i++)
878 if (!variable_name[i])
880 if (i >= *variable_name_size)
881 return EFI_EXIT(EFI_INVALID_PARAMETER);
883 /* search for the last-returned variable */
884 ret = efi_to_native(&native_name, variable_name, vendor);
886 return EFI_EXIT(ret);
888 name_len = strlen(native_name);
889 for (variable = efi_variables_list; variable && *variable;) {
890 if (!strncmp(variable, native_name, name_len) &&
891 variable[name_len] == '=')
894 variable = strchr(variable, '\n');
900 if (!(variable && *variable))
901 return EFI_EXIT(EFI_INVALID_PARAMETER);
904 variable = strchr(variable, '\n');
907 if (!(variable && *variable))
908 return EFI_EXIT(EFI_NOT_FOUND);
911 *new search: free a list used in the previous search
913 free(efi_variables_list);
914 efi_variables_list = NULL;
915 efi_cur_variable = NULL;
917 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
918 list_len = hexport_r(&env_htab, '\n',
919 H_MATCH_REGEX | H_MATCH_KEY,
920 &efi_variables_list, 0, 1, regexlist);
923 return EFI_EXIT(EFI_NOT_FOUND);
925 variable = efi_variables_list;
928 ret = parse_uboot_variable(variable, variable_name_size, variable_name,
929 vendor, &attributes);
931 return EFI_EXIT(ret);
935 efi_status_t EFIAPI efi_set_variable_common(u16 *variable_name,
936 const efi_guid_t *vendor,
938 efi_uintn_t data_size,
941 bool is_non_volatile)
943 char *native_name = NULL, *old_data = NULL, *val = NULL, *s;
944 efi_uintn_t old_size;
948 efi_status_t ret = EFI_SUCCESS;
950 debug("%s: set '%s'\n", __func__, native_name);
952 if (!variable_name || !*variable_name || !vendor ||
953 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
954 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
955 ret = EFI_INVALID_PARAMETER;
959 ret = efi_to_native(&native_name, variable_name, vendor);
963 /* check if a variable exists */
966 ret = EFI_CALL(efi_get_variable(variable_name, vendor, &attr,
968 if (ret == EFI_BUFFER_TOO_SMALL) {
969 if ((is_non_volatile && !(attr & EFI_VARIABLE_NON_VOLATILE)) ||
970 (!is_non_volatile && (attr & EFI_VARIABLE_NON_VOLATILE))) {
971 ret = EFI_INVALID_PARAMETER;
976 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
977 attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
978 delete = !append && (!data_size || !attributes);
980 /* check attributes */
982 if (ro_check && (attr & READ_ONLY)) {
983 ret = EFI_WRITE_PROTECTED;
987 /* attributes won't be changed */
989 ((ro_check && attr != attributes) ||
990 (!ro_check && ((attr & ~(u32)READ_ONLY)
991 != (attributes & ~(u32)READ_ONLY))))) {
992 ret = EFI_INVALID_PARAMETER;
996 if (delete || append) {
998 * Trying to delete or to update a non-existent
1001 ret = EFI_NOT_FOUND;
1006 if (((!u16_strcmp(variable_name, L"PK") ||
1007 !u16_strcmp(variable_name, L"KEK")) &&
1008 !guidcmp(vendor, &efi_global_variable_guid)) ||
1009 ((!u16_strcmp(variable_name, L"db") ||
1010 !u16_strcmp(variable_name, L"dbx")) &&
1011 !guidcmp(vendor, &efi_guid_image_security_database))) {
1012 /* authentication is mandatory */
1014 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
1015 debug("%ls: AUTHENTICATED_WRITE_ACCESS required\n",
1017 ret = EFI_INVALID_PARAMETER;
1022 /* authenticate a variable */
1023 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
1024 if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
1025 ret = EFI_INVALID_PARAMETER;
1029 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
1030 ret = efi_variable_authenticate(variable_name, vendor,
1034 if (ret != EFI_SUCCESS)
1037 /* last chance to check for delete */
1043 (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
1044 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
1045 debug("Secure boot is not configured\n");
1046 ret = EFI_INVALID_PARAMETER;
1051 /* delete a variable */
1053 /* !old_size case has been handled before */
1060 old_data = malloc(old_size);
1062 return EFI_OUT_OF_RESOURCES;
1065 ret = EFI_CALL(efi_get_variable(variable_name, vendor,
1066 &attr, &old_size, old_data));
1067 if (ret != EFI_SUCCESS)
1073 val = malloc(2 * old_size + 2 * data_size
1074 + strlen("{ro,run,boot,nv,time=0123456701234567}(blob)")
1077 ret = EFI_OUT_OF_RESOURCES;
1086 attributes &= (READ_ONLY |
1087 EFI_VARIABLE_NON_VOLATILE |
1088 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1089 EFI_VARIABLE_RUNTIME_ACCESS |
1090 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS);
1091 s += sprintf(s, "{");
1092 while (attributes) {
1093 attr = 1 << (ffs(attributes) - 1);
1095 if (attr == READ_ONLY) {
1096 s += sprintf(s, "ro");
1097 } else if (attr == EFI_VARIABLE_NON_VOLATILE) {
1098 s += sprintf(s, "nv");
1099 } else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) {
1100 s += sprintf(s, "boot");
1101 } else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) {
1102 s += sprintf(s, "run");
1104 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
1105 s += sprintf(s, "time=");
1106 s = bin2hex(s, (u8 *)&time, sizeof(time));
1109 attributes &= ~attr;
1111 s += sprintf(s, ",");
1113 s += sprintf(s, "}");
1114 s += sprintf(s, "(blob)");
1116 /* store payload: */
1118 s = bin2hex(s, old_data, old_size);
1119 s = bin2hex(s, data, data_size);
1122 EFI_PRINT("setting: %s=%s\n", native_name, val);
1125 if (env_set(native_name, val)) {
1126 ret = EFI_DEVICE_ERROR;
1128 if ((u16_strcmp(variable_name, L"PK") == 0 &&
1129 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1130 ret = efi_transfer_secure_state(
1131 (delete ? EFI_MODE_SETUP :
1133 if (ret != EFI_SUCCESS)
1148 efi_status_t EFIAPI efi_set_volatile_variable(u16 *variable_name,
1149 const efi_guid_t *vendor,
1151 efi_uintn_t data_size,
1155 return efi_set_variable_common(variable_name, vendor, attributes,
1156 data_size, data, ro_check, false);
1159 efi_status_t EFIAPI efi_set_nonvolatile_variable(u16 *variable_name,
1160 const efi_guid_t *vendor,
1162 efi_uintn_t data_size,
1168 ret = efi_set_variable_common(variable_name, vendor, attributes,
1169 data_size, data, ro_check, true);
1174 static efi_status_t efi_set_variable_internal(u16 *variable_name,
1175 const efi_guid_t *vendor,
1177 efi_uintn_t data_size,
1183 if (attributes & EFI_VARIABLE_NON_VOLATILE)
1184 ret = efi_set_nonvolatile_variable(variable_name, vendor,
1186 data_size, data, ro_check);
1188 ret = efi_set_volatile_variable(variable_name, vendor,
1189 attributes, data_size, data,
1196 * efi_set_variable() - set value of a UEFI variable
1198 * This function implements the SetVariable runtime service.
1200 * See the Unified Extensible Firmware Interface (UEFI) specification for
1203 * @variable_name: name of the variable
1204 * @vendor: vendor GUID
1205 * @attributes: attributes of the variable
1206 * @data_size: size of the buffer with the variable value
1207 * @data: buffer with the variable value
1208 * Return: status code
1210 efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
1211 const efi_guid_t *vendor, u32 attributes,
1212 efi_uintn_t data_size, const void *data)
1214 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
1217 /* READ_ONLY bit is not part of API */
1218 attributes &= ~(u32)READ_ONLY;
1220 return EFI_EXIT(efi_set_variable_internal(variable_name, vendor,
1221 attributes, data_size, data,
1226 * efi_query_variable_info() - get information about EFI variables
1228 * This function implements the QueryVariableInfo() runtime service.
1230 * See the Unified Extensible Firmware Interface (UEFI) specification for
1233 * @attributes: bitmask to select variables to be
1235 * @maximum_variable_storage_size: maximum size of storage area for the
1236 * selected variable types
1237 * @remaining_variable_storage_size: remaining size of storage are for the
1238 * selected variable types
1239 * @maximum_variable_size: maximum size of a variable of the
1241 * Returns: status code
1243 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
1245 u64 *maximum_variable_storage_size,
1246 u64 *remaining_variable_storage_size,
1247 u64 *maximum_variable_size)
1249 return EFI_UNSUPPORTED;
1253 * efi_get_variable_runtime() - runtime implementation of GetVariable()
1255 * @variable_name: name of the variable
1256 * @vendor: vendor GUID
1257 * @attributes: attributes of the variable
1258 * @data_size: size of the buffer to which the variable value is copied
1259 * @data: buffer to which the variable value is copied
1260 * Return: status code
1262 static efi_status_t __efi_runtime EFIAPI
1263 efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1264 u32 *attributes, efi_uintn_t *data_size, void *data)
1266 return EFI_UNSUPPORTED;
1270 * efi_get_next_variable_name_runtime() - runtime implementation of
1273 * @variable_name_size: size of variable_name buffer in byte
1274 * @variable_name: name of uefi variable's name in u16
1275 * @vendor: vendor's guid
1276 * Return: status code
1278 static efi_status_t __efi_runtime EFIAPI
1279 efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
1280 u16 *variable_name, efi_guid_t *vendor)
1282 return EFI_UNSUPPORTED;
1286 * efi_set_variable_runtime() - runtime implementation of SetVariable()
1288 * @variable_name: name of the variable
1289 * @vendor: vendor GUID
1290 * @attributes: attributes of the variable
1291 * @data_size: size of the buffer with the variable value
1292 * @data: buffer with the variable value
1293 * Return: status code
1295 static efi_status_t __efi_runtime EFIAPI
1296 efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1297 u32 attributes, efi_uintn_t data_size,
1300 return EFI_UNSUPPORTED;
1304 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
1306 void efi_variables_boot_exit_notify(void)
1308 efi_runtime_services.get_variable = efi_get_variable_runtime;
1309 efi_runtime_services.get_next_variable_name =
1310 efi_get_next_variable_name_runtime;
1311 efi_runtime_services.set_variable = efi_set_variable_runtime;
1312 efi_update_table_header_crc32(&efi_runtime_services.hdr);
1316 * efi_init_variables() - initialize variable services
1318 * Return: status code
1320 efi_status_t efi_init_variables(void)
1324 ret = efi_init_secure_state();