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 <crypto/pkcs7_parser.h>
16 #include <linux/compat.h>
17 #include <u-boot/crc.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;
29 static u8 efi_vendor_keys;
31 #define READ_ONLY BIT(31)
34 * Mapping between EFI variables and u-boot variables:
36 * efi_$guid_$varname = {attributes}(type)value
40 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
41 * "{ro,boot,run}(blob)0000000000000000"
42 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
45 * The attributes are a comma separated list of these possible
49 * + boot - boot-services access
50 * + run - runtime access
52 * NOTE: with current implementation, no variables are available after
53 * ExitBootServices, and all are persisted (if possible).
55 * If not specified, the attributes default to "{boot}".
57 * The required type is one of:
59 * + utf8 - raw utf8 string
60 * + blob - arbitrary length hex string
62 * Maybe a utf16 type would be useful to for a string value to be auto
66 #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
69 * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
72 * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
73 * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
74 * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
76 * @native: pointer to pointer to U-Boot variable name
77 * @variable_name: UEFI variable name
78 * @vendor: vendor GUID
81 static efi_status_t efi_to_native(char **native, const u16 *variable_name,
82 const efi_guid_t *vendor)
87 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
88 *native = malloc(len);
90 return EFI_OUT_OF_RESOURCES;
93 pos += sprintf(pos, "efi_%pUl_", vendor);
94 utf16_utf8_strcpy(&pos, variable_name);
100 * prefix() - skip over prefix
102 * Skip over a prefix string.
104 * @str: string with prefix
105 * @prefix: prefix string
106 * Return: string without prefix, or NULL if prefix not found
108 static const char *prefix(const char *str, const char *prefix)
110 size_t n = strlen(prefix);
111 if (!strncmp(prefix, str, n))
117 * parse_attr() - decode attributes part of variable value
119 * Convert the string encoded attributes of a UEFI variable to a bit mask.
120 * TODO: Several attributes are not supported.
122 * @str: value of U-Boot variable
123 * @attrp: pointer to UEFI attributes
124 * @timep: pointer to time attribute
125 * Return: pointer to remainder of U-Boot variable value
127 static const char *parse_attr(const char *str, u32 *attrp, u64 *timep)
133 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
137 while (*str == sep) {
142 if ((s = prefix(str, "ro"))) {
144 } else if ((s = prefix(str, "nv"))) {
145 attr |= EFI_VARIABLE_NON_VOLATILE;
146 } else if ((s = prefix(str, "boot"))) {
147 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
148 } else if ((s = prefix(str, "run"))) {
149 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
150 } else if ((s = prefix(str, "time="))) {
151 attr |= EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
152 hex2bin((u8 *)timep, s, sizeof(*timep));
153 s += sizeof(*timep) * 2;
154 } else if (*str == '}') {
157 printf("invalid attribute: %s\n", str);
172 static efi_status_t efi_set_variable_internal(u16 *variable_name,
173 const efi_guid_t *vendor,
175 efi_uintn_t data_size,
180 * efi_set_secure_state - modify secure boot state variables
181 * @sec_boot: value of SecureBoot
182 * @setup_mode: value of SetupMode
183 * @audit_mode: value of AuditMode
184 * @deployed_mode: value of DeployedMode
186 * Modify secure boot stat-related variables as indicated.
188 * Return: status code
190 static efi_status_t efi_set_secure_state(int sec_boot, int setup_mode,
191 int audit_mode, int deployed_mode)
196 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
197 EFI_VARIABLE_RUNTIME_ACCESS |
199 ret = efi_set_variable_internal(L"SecureBoot",
200 &efi_global_variable_guid,
202 sizeof(sec_boot), &sec_boot,
204 if (ret != EFI_SUCCESS)
207 ret = efi_set_variable_internal(L"SetupMode",
208 &efi_global_variable_guid,
210 sizeof(setup_mode), &setup_mode,
212 if (ret != EFI_SUCCESS)
215 ret = efi_set_variable_internal(L"AuditMode",
216 &efi_global_variable_guid,
218 sizeof(audit_mode), &audit_mode,
220 if (ret != EFI_SUCCESS)
223 ret = efi_set_variable_internal(L"DeployedMode",
224 &efi_global_variable_guid,
226 sizeof(deployed_mode), &deployed_mode,
233 * efi_transfer_secure_state - handle a secure boot state transition
236 * Depending on @mode, secure boot related variables are updated.
237 * Those variables are *read-only* for users, efi_set_variable_internal()
240 * Return: EFI_SUCCESS on success, status code (negative) on error
242 static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
246 debug("Switching secure state from %d to %d\n", efi_secure_mode, mode);
248 if (mode == EFI_MODE_DEPLOYED) {
249 ret = efi_set_secure_state(1, 0, 0, 1);
250 if (ret != EFI_SUCCESS)
253 efi_secure_boot = true;
254 } else if (mode == EFI_MODE_AUDIT) {
255 ret = efi_set_variable_internal(
256 L"PK", &efi_global_variable_guid,
257 EFI_VARIABLE_BOOTSERVICE_ACCESS |
258 EFI_VARIABLE_RUNTIME_ACCESS,
260 if (ret != EFI_SUCCESS)
263 ret = efi_set_secure_state(0, 1, 1, 0);
264 if (ret != EFI_SUCCESS)
267 efi_secure_boot = true;
268 } else if (mode == EFI_MODE_USER) {
269 ret = efi_set_secure_state(1, 0, 0, 0);
270 if (ret != EFI_SUCCESS)
273 efi_secure_boot = true;
274 } else if (mode == EFI_MODE_SETUP) {
275 ret = efi_set_secure_state(0, 1, 0, 0);
276 if (ret != EFI_SUCCESS)
279 return EFI_INVALID_PARAMETER;
282 efi_secure_mode = mode;
287 /* TODO: What action should be taken here? */
288 printf("ERROR: Secure state transition failed\n");
293 * efi_init_secure_state - initialize secure boot state
295 * Return: EFI_SUCCESS on success, status code (negative) on error
297 static efi_status_t efi_init_secure_state(void)
299 enum efi_secure_mode mode;
305 * Since there is currently no "platform-specific" installation
306 * method of Platform Key, we can't say if VendorKeys is 0 or 1
311 ret = EFI_CALL(efi_get_variable(L"PK", &efi_global_variable_guid,
313 if (ret == EFI_BUFFER_TOO_SMALL) {
314 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT))
315 mode = EFI_MODE_USER;
317 mode = EFI_MODE_SETUP;
320 } else if (ret == EFI_NOT_FOUND) {
321 mode = EFI_MODE_SETUP;
327 ret = efi_transfer_secure_state(mode);
328 if (ret == EFI_SUCCESS)
329 ret = efi_set_variable_internal(L"VendorKeys",
330 &efi_global_variable_guid,
331 EFI_VARIABLE_BOOTSERVICE_ACCESS
332 | EFI_VARIABLE_RUNTIME_ACCESS
334 sizeof(efi_vendor_keys),
343 * efi_secure_boot_enabled - return if secure boot is enabled or not
345 * Return: true if enabled, false if disabled
347 bool efi_secure_boot_enabled(void)
349 return efi_secure_boot;
352 #ifdef CONFIG_EFI_SECURE_BOOT
353 static u8 pkcs7_hdr[] = {
355 0x30, 0x82, 0x05, 0xc7,
356 /* OID: pkcs7-signedData */
357 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
358 /* Context Structured? */
359 0xa0, 0x82, 0x05, 0xb8,
363 * efi_variable_parse_signature - parse a signature in variable
364 * @buf: Pointer to variable's value
365 * @buflen: Length of @buf
367 * Parse a signature embedded in variable's value and instantiate
368 * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
369 * pkcs7's signedData, some header needed be prepended for correctly
370 * parsing authentication data, particularly for variable's.
372 * Return: Pointer to pkcs7_message structure on success, NULL on error
374 static struct pkcs7_message *efi_variable_parse_signature(const void *buf,
379 struct pkcs7_message *msg;
382 * This is the best assumption to check if the binary is
383 * already in a form of pkcs7's signedData.
385 if (buflen > sizeof(pkcs7_hdr) &&
386 !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
387 msg = pkcs7_parse_message(buf, buflen);
392 * Otherwise, we should add a dummy prefix sequence for pkcs7
393 * message parser to be able to process.
394 * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
395 * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
397 * The header should be composed in a more refined manner.
399 debug("Makeshift prefix added to authentication data\n");
400 ebuflen = sizeof(pkcs7_hdr) + buflen;
401 if (ebuflen <= 0x7f) {
402 debug("Data is too short\n");
406 ebuf = malloc(ebuflen);
408 debug("Out of memory\n");
412 memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
413 memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
415 ebuf[2] = (len >> 8) & 0xff;
416 ebuf[3] = len & 0xff;
417 len = ebuflen - 0x13;
418 ebuf[0x11] = (len >> 8) & 0xff;
419 ebuf[0x12] = len & 0xff;
421 msg = pkcs7_parse_message(ebuf, ebuflen);
433 * efi_variable_authenticate - authenticate a variable
434 * @variable: Variable name in u16
435 * @vendor: Guid of variable
436 * @data_size: Size of @data
437 * @data: Pointer to variable's value
438 * @given_attr: Attributes to be given at SetVariable()
439 * @env_attr: Attributes that an existing variable holds
440 * @time: signed time that an existing variable holds
442 * Called by efi_set_variable() to verify that the input is correct.
443 * Will replace the given data pointer with another that points to
444 * the actual data to store in the internal memory.
445 * On success, @data and @data_size will be replaced with variable's
446 * actual data, excluding authentication data, and its size, and variable's
447 * attributes and signed time will also be returned in @env_attr and @time,
450 * Return: EFI_SUCCESS on success, status code (negative) on error
452 static efi_status_t efi_variable_authenticate(u16 *variable,
453 const efi_guid_t *vendor,
454 efi_uintn_t *data_size,
455 const void **data, u32 given_attr,
456 u32 *env_attr, u64 *time)
458 const struct efi_variable_authentication_2 *auth;
459 struct efi_signature_store *truststore, *truststore2;
460 struct pkcs7_message *var_sig;
461 struct efi_image_regions *regs;
462 struct efi_time timestamp;
471 ret = EFI_SECURITY_VIOLATION;
473 if (*data_size < sizeof(struct efi_variable_authentication_2))
476 /* authentication data */
478 if (*data_size < (sizeof(auth->time_stamp)
479 + auth->auth_info.hdr.dwLength))
482 if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
485 *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
486 *data_size -= (sizeof(auth->time_stamp)
487 + auth->auth_info.hdr.dwLength);
489 memcpy(×tamp, &auth->time_stamp, sizeof(timestamp));
490 memset(&tm, 0, sizeof(tm));
491 tm.tm_year = timestamp.year;
492 tm.tm_mon = timestamp.month;
493 tm.tm_mday = timestamp.day;
494 tm.tm_hour = timestamp.hour;
495 tm.tm_min = timestamp.minute;
496 tm.tm_sec = timestamp.second;
497 new_time = rtc_mktime(&tm);
499 if (!efi_secure_boot_enabled()) {
500 /* finished checking */
505 if (new_time <= *time)
508 /* data to be digested */
509 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
513 efi_image_region_add(regs, (uint8_t *)variable,
515 + u16_strlen(variable) * sizeof(u16), 1);
516 efi_image_region_add(regs, (uint8_t *)vendor,
517 (uint8_t *)vendor + sizeof(*vendor), 1);
518 efi_image_region_add(regs, (uint8_t *)&given_attr,
519 (uint8_t *)&given_attr + sizeof(given_attr), 1);
520 efi_image_region_add(regs, (uint8_t *)×tamp,
521 (uint8_t *)×tamp + sizeof(timestamp), 1);
522 efi_image_region_add(regs, (uint8_t *)*data,
523 (uint8_t *)*data + *data_size, 1);
525 /* variable's signature list */
526 if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
528 var_sig = efi_variable_parse_signature(auth->auth_info.cert_data,
529 auth->auth_info.hdr.dwLength
530 - sizeof(auth->auth_info));
531 if (IS_ERR(var_sig)) {
532 debug("Parsing variable's signature failed\n");
537 /* signature database used for authentication */
538 if (u16_strcmp(variable, L"PK") == 0 ||
539 u16_strcmp(variable, L"KEK") == 0) {
541 truststore = efi_sigstore_parse_sigdb(L"PK");
544 } else if (u16_strcmp(variable, L"db") == 0 ||
545 u16_strcmp(variable, L"dbx") == 0) {
546 /* with PK and KEK */
547 truststore = efi_sigstore_parse_sigdb(L"KEK");
548 truststore2 = efi_sigstore_parse_sigdb(L"PK");
554 truststore = truststore2;
558 /* TODO: support private authenticated variables */
562 /* verify signature */
563 if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) {
567 efi_signature_verify_with_sigdb(regs, var_sig,
568 truststore2, NULL)) {
571 debug("Verifying variable's signature failed\n");
576 /* finished checking */
577 *time = rtc_mktime(&tm);
581 efi_sigstore_free(truststore);
582 efi_sigstore_free(truststore2);
583 pkcs7_free_message(var_sig);
589 static efi_status_t efi_variable_authenticate(u16 *variable,
590 const efi_guid_t *vendor,
591 efi_uintn_t *data_size,
592 const void **data, u32 given_attr,
593 u32 *env_attr, u64 *time)
597 #endif /* CONFIG_EFI_SECURE_BOOT */
600 efi_status_t EFIAPI efi_get_variable_common(u16 *variable_name,
601 const efi_guid_t *vendor,
603 efi_uintn_t *data_size, void *data,
604 bool is_non_volatile)
608 unsigned long in_size;
609 const char *val = NULL, *s;
613 if (!variable_name || !vendor || !data_size)
614 return EFI_EXIT(EFI_INVALID_PARAMETER);
616 ret = efi_to_native(&native_name, variable_name, vendor);
620 EFI_PRINT("get '%s'\n", native_name);
622 val = env_get(native_name);
625 return EFI_NOT_FOUND;
627 val = parse_attr(val, &attr, &time);
629 in_size = *data_size;
631 if ((s = prefix(val, "(blob)"))) {
632 size_t len = strlen(s);
634 /* number of hexadecimal digits must be even */
636 return EFI_DEVICE_ERROR;
638 /* two characters per byte: */
643 ret = EFI_BUFFER_TOO_SMALL;
648 debug("Variable with no data shouldn't exist.\n");
649 return EFI_INVALID_PARAMETER;
652 if (hex2bin(data, s, len))
653 return EFI_DEVICE_ERROR;
655 EFI_PRINT("got value: \"%s\"\n", s);
656 } else if ((s = prefix(val, "(utf8)"))) {
657 unsigned len = strlen(s) + 1;
662 ret = EFI_BUFFER_TOO_SMALL;
667 debug("Variable with no data shouldn't exist.\n");
668 return EFI_INVALID_PARAMETER;
671 memcpy(data, s, len);
672 ((char *)data)[len] = '\0';
674 EFI_PRINT("got value: \"%s\"\n", (char *)data);
676 EFI_PRINT("invalid value: '%s'\n", val);
677 return EFI_DEVICE_ERROR;
682 *attributes = attr & EFI_VARIABLE_MASK;
688 efi_status_t EFIAPI efi_get_volatile_variable(u16 *variable_name,
689 const efi_guid_t *vendor,
691 efi_uintn_t *data_size,
694 return efi_get_variable_common(variable_name, vendor, attributes,
695 data_size, data, false);
698 efi_status_t EFIAPI efi_get_nonvolatile_variable(u16 *variable_name,
699 const efi_guid_t *vendor,
701 efi_uintn_t *data_size,
704 return efi_get_variable_common(variable_name, vendor, attributes,
705 data_size, data, true);
709 * efi_efi_get_variable() - retrieve value of a UEFI variable
711 * This function implements the GetVariable runtime service.
713 * See the Unified Extensible Firmware Interface (UEFI) specification for
716 * @variable_name: name of the variable
717 * @vendor: vendor GUID
718 * @attributes: attributes of the variable
719 * @data_size: size of the buffer to which the variable value is copied
720 * @data: buffer to which the variable value is copied
721 * Return: status code
723 efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
724 const efi_guid_t *vendor, u32 *attributes,
725 efi_uintn_t *data_size, void *data)
729 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
732 ret = efi_get_volatile_variable(variable_name, vendor, attributes,
734 if (ret == EFI_NOT_FOUND)
735 ret = efi_get_nonvolatile_variable(variable_name, vendor,
736 attributes, data_size, data);
738 return EFI_EXIT(ret);
741 static char *efi_variables_list;
742 static char *efi_cur_variable;
745 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
747 * @variable: whole data of u-boot variable (ie. name=value)
748 * @variable_name_size: size of variable_name buffer in byte
749 * @variable_name: name of uefi variable in u16, null-terminated
750 * @vendor: vendor's guid
751 * @attributes: attributes
753 * A uefi variable is encoded into a u-boot variable as described above.
754 * This function parses such a u-boot variable and retrieve uefi-related
755 * information into respective parameters. In return, variable_name_size
756 * is the size of variable name including NULL.
758 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
759 * the entire variable list has been returned,
760 * otherwise non-zero status code
762 static efi_status_t parse_uboot_variable(char *variable,
763 efi_uintn_t *variable_name_size,
765 const efi_guid_t *vendor,
768 char *guid, *name, *end, c;
770 efi_uintn_t old_variable_name_size;
774 guid = strchr(variable, '_');
776 return EFI_INVALID_PARAMETER;
778 name = strchr(guid, '_');
780 return EFI_INVALID_PARAMETER;
782 end = strchr(name, '=');
784 return EFI_INVALID_PARAMETER;
786 name_len = end - name;
787 old_variable_name_size = *variable_name_size;
788 *variable_name_size = sizeof(u16) * (name_len + 1);
789 if (old_variable_name_size < *variable_name_size)
790 return EFI_BUFFER_TOO_SMALL;
792 end++; /* point to value */
796 utf8_utf16_strncpy(&p, name, name_len);
797 variable_name[name_len] = 0;
801 *(name - 1) = '\0'; /* guid need be null-terminated here */
802 uuid_str_to_bin(guid, (unsigned char *)vendor, UUID_STR_FORMAT_GUID);
806 parse_attr(end, attributes, &time);
812 * efi_get_next_variable_name() - enumerate the current variable names
814 * @variable_name_size: size of variable_name buffer in byte
815 * @variable_name: name of uefi variable's name in u16
816 * @vendor: vendor's guid
818 * This function implements the GetNextVariableName service.
820 * See the Unified Extensible Firmware Interface (UEFI) specification for
823 * Return: status code
825 efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
829 char *native_name, *variable;
830 ssize_t name_len, list_len;
832 char * const regexlist[] = {regex};
837 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
839 if (!variable_name_size || !variable_name || !vendor)
840 return EFI_EXIT(EFI_INVALID_PARAMETER);
842 if (variable_name[0]) {
843 /* check null-terminated string */
844 for (i = 0; i < *variable_name_size; i++)
845 if (!variable_name[i])
847 if (i >= *variable_name_size)
848 return EFI_EXIT(EFI_INVALID_PARAMETER);
850 /* search for the last-returned variable */
851 ret = efi_to_native(&native_name, variable_name, vendor);
853 return EFI_EXIT(ret);
855 name_len = strlen(native_name);
856 for (variable = efi_variables_list; variable && *variable;) {
857 if (!strncmp(variable, native_name, name_len) &&
858 variable[name_len] == '=')
861 variable = strchr(variable, '\n');
867 if (!(variable && *variable))
868 return EFI_EXIT(EFI_INVALID_PARAMETER);
871 variable = strchr(variable, '\n');
874 if (!(variable && *variable))
875 return EFI_EXIT(EFI_NOT_FOUND);
878 *new search: free a list used in the previous search
880 free(efi_variables_list);
881 efi_variables_list = NULL;
882 efi_cur_variable = NULL;
884 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
885 list_len = hexport_r(&env_htab, '\n',
886 H_MATCH_REGEX | H_MATCH_KEY,
887 &efi_variables_list, 0, 1, regexlist);
890 return EFI_EXIT(EFI_NOT_FOUND);
892 variable = efi_variables_list;
895 ret = parse_uboot_variable(variable, variable_name_size, variable_name,
896 vendor, &attributes);
898 return EFI_EXIT(ret);
902 efi_status_t EFIAPI efi_set_variable_common(u16 *variable_name,
903 const efi_guid_t *vendor,
905 efi_uintn_t data_size,
908 bool is_non_volatile)
910 char *native_name = NULL, *old_data = NULL, *val = NULL, *s;
911 efi_uintn_t old_size;
915 efi_status_t ret = EFI_SUCCESS;
917 debug("%s: set '%s'\n", __func__, native_name);
919 if (!variable_name || !*variable_name || !vendor ||
920 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
921 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
922 ret = EFI_INVALID_PARAMETER;
926 ret = efi_to_native(&native_name, variable_name, vendor);
930 /* check if a variable exists */
933 ret = EFI_CALL(efi_get_variable(variable_name, vendor, &attr,
935 if (ret == EFI_BUFFER_TOO_SMALL) {
936 if ((is_non_volatile && !(attr & EFI_VARIABLE_NON_VOLATILE)) ||
937 (!is_non_volatile && (attr & EFI_VARIABLE_NON_VOLATILE))) {
938 ret = EFI_INVALID_PARAMETER;
943 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
944 attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
945 delete = !append && (!data_size || !attributes);
947 /* check attributes */
949 if (ro_check && (attr & READ_ONLY)) {
950 ret = EFI_WRITE_PROTECTED;
954 /* attributes won't be changed */
956 ((ro_check && attr != attributes) ||
957 (!ro_check && ((attr & ~(u32)READ_ONLY)
958 != (attributes & ~(u32)READ_ONLY))))) {
959 ret = EFI_INVALID_PARAMETER;
963 if (delete || append) {
965 * Trying to delete or to update a non-existent
973 if (((!u16_strcmp(variable_name, L"PK") ||
974 !u16_strcmp(variable_name, L"KEK")) &&
975 !guidcmp(vendor, &efi_global_variable_guid)) ||
976 ((!u16_strcmp(variable_name, L"db") ||
977 !u16_strcmp(variable_name, L"dbx")) &&
978 !guidcmp(vendor, &efi_guid_image_security_database))) {
979 /* authentication is mandatory */
981 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
982 debug("%ls: AUTHENTICATED_WRITE_ACCESS required\n",
984 ret = EFI_INVALID_PARAMETER;
989 /* authenticate a variable */
990 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
991 if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
992 ret = EFI_INVALID_PARAMETER;
996 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
997 ret = efi_variable_authenticate(variable_name, vendor,
1001 if (ret != EFI_SUCCESS)
1004 /* last chance to check for delete */
1010 (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
1011 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
1012 debug("Secure boot is not configured\n");
1013 ret = EFI_INVALID_PARAMETER;
1018 /* delete a variable */
1020 /* !old_size case has been handled before */
1027 old_data = malloc(old_size);
1029 return EFI_OUT_OF_RESOURCES;
1032 ret = EFI_CALL(efi_get_variable(variable_name, vendor,
1033 &attr, &old_size, old_data));
1034 if (ret != EFI_SUCCESS)
1040 val = malloc(2 * old_size + 2 * data_size
1041 + strlen("{ro,run,boot,nv,time=0123456701234567}(blob)")
1044 ret = EFI_OUT_OF_RESOURCES;
1053 attributes &= (READ_ONLY |
1054 EFI_VARIABLE_NON_VOLATILE |
1055 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1056 EFI_VARIABLE_RUNTIME_ACCESS |
1057 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS);
1058 s += sprintf(s, "{");
1059 while (attributes) {
1060 attr = 1 << (ffs(attributes) - 1);
1062 if (attr == READ_ONLY) {
1063 s += sprintf(s, "ro");
1064 } else if (attr == EFI_VARIABLE_NON_VOLATILE) {
1065 s += sprintf(s, "nv");
1066 } else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) {
1067 s += sprintf(s, "boot");
1068 } else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) {
1069 s += sprintf(s, "run");
1071 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
1072 s += sprintf(s, "time=");
1073 s = bin2hex(s, (u8 *)&time, sizeof(time));
1076 attributes &= ~attr;
1078 s += sprintf(s, ",");
1080 s += sprintf(s, "}");
1081 s += sprintf(s, "(blob)");
1083 /* store payload: */
1085 s = bin2hex(s, old_data, old_size);
1086 s = bin2hex(s, data, data_size);
1089 EFI_PRINT("setting: %s=%s\n", native_name, val);
1092 if (env_set(native_name, val)) {
1093 ret = EFI_DEVICE_ERROR;
1095 bool vendor_keys_modified = false;
1097 if ((u16_strcmp(variable_name, L"PK") == 0 &&
1098 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1099 ret = efi_transfer_secure_state(
1100 (delete ? EFI_MODE_SETUP :
1102 if (ret != EFI_SUCCESS)
1105 if (efi_secure_mode != EFI_MODE_SETUP)
1106 vendor_keys_modified = true;
1107 } else if ((u16_strcmp(variable_name, L"KEK") == 0 &&
1108 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1109 if (efi_secure_mode != EFI_MODE_SETUP)
1110 vendor_keys_modified = true;
1113 /* update VendorKeys */
1114 if (vendor_keys_modified & efi_vendor_keys) {
1115 efi_vendor_keys = 0;
1116 ret = efi_set_variable_internal(
1118 &efi_global_variable_guid,
1119 EFI_VARIABLE_BOOTSERVICE_ACCESS
1120 | EFI_VARIABLE_RUNTIME_ACCESS
1122 sizeof(efi_vendor_keys),
1139 efi_status_t EFIAPI efi_set_volatile_variable(u16 *variable_name,
1140 const efi_guid_t *vendor,
1142 efi_uintn_t data_size,
1146 return efi_set_variable_common(variable_name, vendor, attributes,
1147 data_size, data, ro_check, false);
1150 efi_status_t EFIAPI efi_set_nonvolatile_variable(u16 *variable_name,
1151 const efi_guid_t *vendor,
1153 efi_uintn_t data_size,
1159 ret = efi_set_variable_common(variable_name, vendor, attributes,
1160 data_size, data, ro_check, true);
1165 static efi_status_t efi_set_variable_internal(u16 *variable_name,
1166 const efi_guid_t *vendor,
1168 efi_uintn_t data_size,
1174 if (attributes & EFI_VARIABLE_NON_VOLATILE)
1175 ret = efi_set_nonvolatile_variable(variable_name, vendor,
1177 data_size, data, ro_check);
1179 ret = efi_set_volatile_variable(variable_name, vendor,
1180 attributes, data_size, data,
1187 * efi_set_variable() - set value of a UEFI variable
1189 * This function implements the SetVariable runtime service.
1191 * See the Unified Extensible Firmware Interface (UEFI) specification for
1194 * @variable_name: name of the variable
1195 * @vendor: vendor GUID
1196 * @attributes: attributes of the variable
1197 * @data_size: size of the buffer with the variable value
1198 * @data: buffer with the variable value
1199 * Return: status code
1201 efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
1202 const efi_guid_t *vendor, u32 attributes,
1203 efi_uintn_t data_size, const void *data)
1205 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
1208 /* READ_ONLY bit is not part of API */
1209 attributes &= ~(u32)READ_ONLY;
1211 return EFI_EXIT(efi_set_variable_internal(variable_name, vendor,
1212 attributes, data_size, data,
1217 * efi_query_variable_info() - get information about EFI variables
1219 * This function implements the QueryVariableInfo() runtime service.
1221 * See the Unified Extensible Firmware Interface (UEFI) specification for
1224 * @attributes: bitmask to select variables to be
1226 * @maximum_variable_storage_size: maximum size of storage area for the
1227 * selected variable types
1228 * @remaining_variable_storage_size: remaining size of storage are for the
1229 * selected variable types
1230 * @maximum_variable_size: maximum size of a variable of the
1232 * Returns: status code
1234 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
1236 u64 *maximum_variable_storage_size,
1237 u64 *remaining_variable_storage_size,
1238 u64 *maximum_variable_size)
1240 return EFI_UNSUPPORTED;
1244 * efi_get_variable_runtime() - runtime implementation of GetVariable()
1246 * @variable_name: name of the variable
1247 * @vendor: vendor GUID
1248 * @attributes: attributes of the variable
1249 * @data_size: size of the buffer to which the variable value is copied
1250 * @data: buffer to which the variable value is copied
1251 * Return: status code
1253 static efi_status_t __efi_runtime EFIAPI
1254 efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1255 u32 *attributes, efi_uintn_t *data_size, void *data)
1257 return EFI_UNSUPPORTED;
1261 * efi_get_next_variable_name_runtime() - runtime implementation of
1264 * @variable_name_size: size of variable_name buffer in byte
1265 * @variable_name: name of uefi variable's name in u16
1266 * @vendor: vendor's guid
1267 * Return: status code
1269 static efi_status_t __efi_runtime EFIAPI
1270 efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
1271 u16 *variable_name, efi_guid_t *vendor)
1273 return EFI_UNSUPPORTED;
1277 * efi_set_variable_runtime() - runtime implementation of SetVariable()
1279 * @variable_name: name of the variable
1280 * @vendor: vendor GUID
1281 * @attributes: attributes of the variable
1282 * @data_size: size of the buffer with the variable value
1283 * @data: buffer with the variable value
1284 * Return: status code
1286 static efi_status_t __efi_runtime EFIAPI
1287 efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1288 u32 attributes, efi_uintn_t data_size,
1291 return EFI_UNSUPPORTED;
1295 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
1297 void efi_variables_boot_exit_notify(void)
1299 efi_runtime_services.get_variable = efi_get_variable_runtime;
1300 efi_runtime_services.get_next_variable_name =
1301 efi_get_next_variable_name_runtime;
1302 efi_runtime_services.set_variable = efi_set_variable_runtime;
1303 efi_update_table_header_crc32(&efi_runtime_services.hdr);
1307 * efi_init_variables() - initialize variable services
1309 * Return: status code
1311 efi_status_t efi_init_variables(void)
1315 ret = efi_init_secure_state();