1 // SPDX-License-Identifier: GPL-2.0+
3 * UEFI runtime variable services
5 * Copyright (c) 2017 Rob Clark
9 #include <efi_loader.h>
11 #include <env_internal.h>
17 #include <crypto/pkcs7_parser.h>
18 #include <linux/bitops.h>
19 #include <linux/compat.h>
20 #include <u-boot/crc.h>
22 enum efi_secure_mode {
29 const efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID;
30 static bool efi_secure_boot;
31 static int efi_secure_mode;
32 static u8 efi_vendor_keys;
34 #define READ_ONLY BIT(31)
36 static efi_status_t efi_get_variable_common(u16 *variable_name,
37 const efi_guid_t *vendor,
39 efi_uintn_t *data_size, void *data);
41 static efi_status_t efi_set_variable_common(u16 *variable_name,
42 const efi_guid_t *vendor,
44 efi_uintn_t data_size,
49 * Mapping between EFI variables and u-boot variables:
51 * efi_$guid_$varname = {attributes}(type)value
55 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
56 * "{ro,boot,run}(blob)0000000000000000"
57 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
60 * The attributes are a comma separated list of these possible
64 * + boot - boot-services access
65 * + run - runtime access
67 * NOTE: with current implementation, no variables are available after
68 * ExitBootServices, and all are persisted (if possible).
70 * If not specified, the attributes default to "{boot}".
72 * The required type is one of:
74 * + utf8 - raw utf8 string
75 * + blob - arbitrary length hex string
77 * Maybe a utf16 type would be useful to for a string value to be auto
81 #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
84 * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
87 * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
88 * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
89 * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
91 * @native: pointer to pointer to U-Boot variable name
92 * @variable_name: UEFI variable name
93 * @vendor: vendor GUID
96 static efi_status_t efi_to_native(char **native, const u16 *variable_name,
97 const efi_guid_t *vendor)
102 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
103 *native = malloc(len);
105 return EFI_OUT_OF_RESOURCES;
108 pos += sprintf(pos, "efi_%pUl_", vendor);
109 utf16_utf8_strcpy(&pos, variable_name);
115 * prefix() - skip over prefix
117 * Skip over a prefix string.
119 * @str: string with prefix
120 * @prefix: prefix string
121 * Return: string without prefix, or NULL if prefix not found
123 static const char *prefix(const char *str, const char *prefix)
125 size_t n = strlen(prefix);
126 if (!strncmp(prefix, str, n))
132 * parse_attr() - decode attributes part of variable value
134 * Convert the string encoded attributes of a UEFI variable to a bit mask.
135 * TODO: Several attributes are not supported.
137 * @str: value of U-Boot variable
138 * @attrp: pointer to UEFI attributes
139 * @timep: pointer to time attribute
140 * Return: pointer to remainder of U-Boot variable value
142 static const char *parse_attr(const char *str, u32 *attrp, u64 *timep)
148 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
152 while (*str == sep) {
157 if ((s = prefix(str, "ro"))) {
159 } else if ((s = prefix(str, "nv"))) {
160 attr |= EFI_VARIABLE_NON_VOLATILE;
161 } else if ((s = prefix(str, "boot"))) {
162 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
163 } else if ((s = prefix(str, "run"))) {
164 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
165 } else if ((s = prefix(str, "time="))) {
166 attr |= EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
167 hex2bin((u8 *)timep, s, sizeof(*timep));
168 s += sizeof(*timep) * 2;
169 } else if (*str == '}') {
172 printf("invalid attribute: %s\n", str);
188 * efi_set_secure_state - modify secure boot state variables
189 * @sec_boot: value of SecureBoot
190 * @setup_mode: value of SetupMode
191 * @audit_mode: value of AuditMode
192 * @deployed_mode: value of DeployedMode
194 * Modify secure boot stat-related variables as indicated.
196 * Return: status code
198 static efi_status_t efi_set_secure_state(int sec_boot, int setup_mode,
199 int audit_mode, int deployed_mode)
204 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
205 EFI_VARIABLE_RUNTIME_ACCESS |
207 ret = efi_set_variable_common(L"SecureBoot", &efi_global_variable_guid,
208 attributes, sizeof(sec_boot), &sec_boot,
210 if (ret != EFI_SUCCESS)
213 ret = efi_set_variable_common(L"SetupMode", &efi_global_variable_guid,
214 attributes, sizeof(setup_mode),
216 if (ret != EFI_SUCCESS)
219 ret = efi_set_variable_common(L"AuditMode", &efi_global_variable_guid,
220 attributes, sizeof(audit_mode),
222 if (ret != EFI_SUCCESS)
225 ret = efi_set_variable_common(L"DeployedMode",
226 &efi_global_variable_guid, attributes,
227 sizeof(deployed_mode), &deployed_mode,
234 * efi_transfer_secure_state - handle a secure boot state transition
237 * Depending on @mode, secure boot related variables are updated.
238 * Those variables are *read-only* for users, efi_set_variable_common()
241 * Return: status code
243 static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
247 debug("Switching secure state from %d to %d\n", efi_secure_mode, mode);
249 if (mode == EFI_MODE_DEPLOYED) {
250 ret = efi_set_secure_state(1, 0, 0, 1);
251 if (ret != EFI_SUCCESS)
254 efi_secure_boot = true;
255 } else if (mode == EFI_MODE_AUDIT) {
256 ret = efi_set_variable_common(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: status code
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_get_variable_common(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_common(L"VendorKeys",
330 &efi_global_variable_guid,
331 EFI_VARIABLE_BOOTSERVICE_ACCESS |
332 EFI_VARIABLE_RUNTIME_ACCESS |
334 sizeof(efi_vendor_keys),
335 &efi_vendor_keys, false);
342 * efi_secure_boot_enabled - return if secure boot is enabled or not
344 * Return: true if enabled, false if disabled
346 bool efi_secure_boot_enabled(void)
348 return efi_secure_boot;
351 #ifdef CONFIG_EFI_SECURE_BOOT
352 static u8 pkcs7_hdr[] = {
354 0x30, 0x82, 0x05, 0xc7,
355 /* OID: pkcs7-signedData */
356 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
357 /* Context Structured? */
358 0xa0, 0x82, 0x05, 0xb8,
362 * efi_variable_parse_signature - parse a signature in variable
363 * @buf: Pointer to variable's value
364 * @buflen: Length of @buf
366 * Parse a signature embedded in variable's value and instantiate
367 * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
368 * pkcs7's signedData, some header needed be prepended for correctly
369 * parsing authentication data, particularly for variable's.
371 * Return: Pointer to pkcs7_message structure on success, NULL on error
373 static struct pkcs7_message *efi_variable_parse_signature(const void *buf,
378 struct pkcs7_message *msg;
381 * This is the best assumption to check if the binary is
382 * already in a form of pkcs7's signedData.
384 if (buflen > sizeof(pkcs7_hdr) &&
385 !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
386 msg = pkcs7_parse_message(buf, buflen);
391 * Otherwise, we should add a dummy prefix sequence for pkcs7
392 * message parser to be able to process.
393 * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
394 * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
396 * The header should be composed in a more refined manner.
398 debug("Makeshift prefix added to authentication data\n");
399 ebuflen = sizeof(pkcs7_hdr) + buflen;
400 if (ebuflen <= 0x7f) {
401 debug("Data is too short\n");
405 ebuf = malloc(ebuflen);
407 debug("Out of memory\n");
411 memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
412 memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
414 ebuf[2] = (len >> 8) & 0xff;
415 ebuf[3] = len & 0xff;
416 len = ebuflen - 0x13;
417 ebuf[0x11] = (len >> 8) & 0xff;
418 ebuf[0x12] = len & 0xff;
420 msg = pkcs7_parse_message(ebuf, ebuflen);
432 * efi_variable_authenticate - authenticate a variable
433 * @variable: Variable name in u16
434 * @vendor: Guid of variable
435 * @data_size: Size of @data
436 * @data: Pointer to variable's value
437 * @given_attr: Attributes to be given at SetVariable()
438 * @env_attr: Attributes that an existing variable holds
439 * @time: signed time that an existing variable holds
441 * Called by efi_set_variable() to verify that the input is correct.
442 * Will replace the given data pointer with another that points to
443 * the actual data to store in the internal memory.
444 * On success, @data and @data_size will be replaced with variable's
445 * actual data, excluding authentication data, and its size, and variable's
446 * attributes and signed time will also be returned in @env_attr and @time,
449 * Return: status code
451 static efi_status_t efi_variable_authenticate(u16 *variable,
452 const efi_guid_t *vendor,
453 efi_uintn_t *data_size,
454 const void **data, u32 given_attr,
455 u32 *env_attr, u64 *time)
457 const struct efi_variable_authentication_2 *auth;
458 struct efi_signature_store *truststore, *truststore2;
459 struct pkcs7_message *var_sig;
460 struct efi_image_regions *regs;
461 struct efi_time timestamp;
470 ret = EFI_SECURITY_VIOLATION;
472 if (*data_size < sizeof(struct efi_variable_authentication_2))
475 /* authentication data */
477 if (*data_size < (sizeof(auth->time_stamp)
478 + auth->auth_info.hdr.dwLength))
481 if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
484 *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
485 *data_size -= (sizeof(auth->time_stamp)
486 + auth->auth_info.hdr.dwLength);
488 memcpy(×tamp, &auth->time_stamp, sizeof(timestamp));
489 memset(&tm, 0, sizeof(tm));
490 tm.tm_year = timestamp.year;
491 tm.tm_mon = timestamp.month;
492 tm.tm_mday = timestamp.day;
493 tm.tm_hour = timestamp.hour;
494 tm.tm_min = timestamp.minute;
495 tm.tm_sec = timestamp.second;
496 new_time = rtc_mktime(&tm);
498 if (!efi_secure_boot_enabled()) {
499 /* finished checking */
504 if (new_time <= *time)
507 /* data to be digested */
508 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
512 efi_image_region_add(regs, (uint8_t *)variable,
514 + u16_strlen(variable) * sizeof(u16), 1);
515 efi_image_region_add(regs, (uint8_t *)vendor,
516 (uint8_t *)vendor + sizeof(*vendor), 1);
517 efi_image_region_add(regs, (uint8_t *)&given_attr,
518 (uint8_t *)&given_attr + sizeof(given_attr), 1);
519 efi_image_region_add(regs, (uint8_t *)×tamp,
520 (uint8_t *)×tamp + sizeof(timestamp), 1);
521 efi_image_region_add(regs, (uint8_t *)*data,
522 (uint8_t *)*data + *data_size, 1);
524 /* variable's signature list */
525 if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
527 var_sig = efi_variable_parse_signature(auth->auth_info.cert_data,
528 auth->auth_info.hdr.dwLength
529 - sizeof(auth->auth_info));
531 debug("Parsing variable's signature failed\n");
535 /* signature database used for authentication */
536 if (u16_strcmp(variable, L"PK") == 0 ||
537 u16_strcmp(variable, L"KEK") == 0) {
539 truststore = efi_sigstore_parse_sigdb(L"PK");
542 } else if (u16_strcmp(variable, L"db") == 0 ||
543 u16_strcmp(variable, L"dbx") == 0) {
544 /* with PK and KEK */
545 truststore = efi_sigstore_parse_sigdb(L"KEK");
546 truststore2 = efi_sigstore_parse_sigdb(L"PK");
552 truststore = truststore2;
556 /* TODO: support private authenticated variables */
560 /* verify signature */
561 if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) {
565 efi_signature_verify_with_sigdb(regs, var_sig,
566 truststore2, NULL)) {
569 debug("Verifying variable's signature failed\n");
574 /* finished checking */
575 *time = rtc_mktime(&tm);
579 efi_sigstore_free(truststore);
580 efi_sigstore_free(truststore2);
581 pkcs7_free_message(var_sig);
587 static efi_status_t efi_variable_authenticate(u16 *variable,
588 const efi_guid_t *vendor,
589 efi_uintn_t *data_size,
590 const void **data, u32 given_attr,
591 u32 *env_attr, u64 *time)
595 #endif /* CONFIG_EFI_SECURE_BOOT */
597 static efi_status_t efi_get_variable_common(u16 *variable_name,
598 const efi_guid_t *vendor,
600 efi_uintn_t *data_size, void *data)
604 unsigned long in_size;
605 const char *val = NULL, *s;
609 if (!variable_name || !vendor || !data_size)
610 return EFI_EXIT(EFI_INVALID_PARAMETER);
612 ret = efi_to_native(&native_name, variable_name, vendor);
616 EFI_PRINT("get '%s'\n", native_name);
618 val = env_get(native_name);
621 return EFI_NOT_FOUND;
623 val = parse_attr(val, &attr, &time);
625 in_size = *data_size;
627 if ((s = prefix(val, "(blob)"))) {
628 size_t len = strlen(s);
630 /* number of hexadecimal digits must be even */
632 return EFI_DEVICE_ERROR;
634 /* two characters per byte: */
639 ret = EFI_BUFFER_TOO_SMALL;
644 debug("Variable with no data shouldn't exist.\n");
645 return EFI_INVALID_PARAMETER;
648 if (hex2bin(data, s, len))
649 return EFI_DEVICE_ERROR;
651 EFI_PRINT("got value: \"%s\"\n", s);
652 } else if ((s = prefix(val, "(utf8)"))) {
653 unsigned len = strlen(s) + 1;
658 ret = EFI_BUFFER_TOO_SMALL;
663 debug("Variable with no data shouldn't exist.\n");
664 return EFI_INVALID_PARAMETER;
667 memcpy(data, s, len);
668 ((char *)data)[len] = '\0';
670 EFI_PRINT("got value: \"%s\"\n", (char *)data);
672 EFI_PRINT("invalid value: '%s'\n", val);
673 return EFI_DEVICE_ERROR;
678 *attributes = attr & EFI_VARIABLE_MASK;
684 * efi_efi_get_variable() - retrieve value of a UEFI variable
686 * This function implements the GetVariable runtime service.
688 * See the Unified Extensible Firmware Interface (UEFI) specification for
691 * @variable_name: name of the variable
692 * @vendor: vendor GUID
693 * @attributes: attributes of the variable
694 * @data_size: size of the buffer to which the variable value is copied
695 * @data: buffer to which the variable value is copied
696 * Return: status code
698 efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
699 const efi_guid_t *vendor, u32 *attributes,
700 efi_uintn_t *data_size, void *data)
704 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
707 ret = efi_get_variable_common(variable_name, vendor, attributes,
709 return EFI_EXIT(ret);
712 static char *efi_variables_list;
713 static char *efi_cur_variable;
716 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
718 * @variable: whole data of u-boot variable (ie. name=value)
719 * @variable_name_size: size of variable_name buffer in byte
720 * @variable_name: name of uefi variable in u16, null-terminated
721 * @vendor: vendor's guid
722 * @attributes: attributes
724 * A uefi variable is encoded into a u-boot variable as described above.
725 * This function parses such a u-boot variable and retrieve uefi-related
726 * information into respective parameters. In return, variable_name_size
727 * is the size of variable name including NULL.
729 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
730 * the entire variable list has been returned,
731 * otherwise non-zero status code
733 static efi_status_t parse_uboot_variable(char *variable,
734 efi_uintn_t *variable_name_size,
736 const efi_guid_t *vendor,
739 char *guid, *name, *end, c;
741 efi_uintn_t old_variable_name_size;
745 guid = strchr(variable, '_');
747 return EFI_INVALID_PARAMETER;
749 name = strchr(guid, '_');
751 return EFI_INVALID_PARAMETER;
753 end = strchr(name, '=');
755 return EFI_INVALID_PARAMETER;
757 name_len = end - name;
758 old_variable_name_size = *variable_name_size;
759 *variable_name_size = sizeof(u16) * (name_len + 1);
760 if (old_variable_name_size < *variable_name_size)
761 return EFI_BUFFER_TOO_SMALL;
763 end++; /* point to value */
767 utf8_utf16_strncpy(&p, name, name_len);
768 variable_name[name_len] = 0;
772 *(name - 1) = '\0'; /* guid need be null-terminated here */
773 if (uuid_str_to_bin(guid, (unsigned char *)vendor,
774 UUID_STR_FORMAT_GUID))
775 /* The only error would be EINVAL. */
776 return EFI_INVALID_PARAMETER;
780 parse_attr(end, attributes, &time);
786 * efi_get_next_variable_name() - enumerate the current variable names
788 * @variable_name_size: size of variable_name buffer in byte
789 * @variable_name: name of uefi variable's name in u16
790 * @vendor: vendor's guid
792 * This function implements the GetNextVariableName service.
794 * See the Unified Extensible Firmware Interface (UEFI) specification for
797 * Return: status code
799 efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
803 char *native_name, *variable;
804 ssize_t name_len, list_len;
806 char * const regexlist[] = {regex};
811 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
813 if (!variable_name_size || !variable_name || !vendor)
814 return EFI_EXIT(EFI_INVALID_PARAMETER);
816 if (variable_name[0]) {
817 /* check null-terminated string */
818 for (i = 0; i < *variable_name_size; i++)
819 if (!variable_name[i])
821 if (i >= *variable_name_size)
822 return EFI_EXIT(EFI_INVALID_PARAMETER);
824 /* search for the last-returned variable */
825 ret = efi_to_native(&native_name, variable_name, vendor);
827 return EFI_EXIT(ret);
829 name_len = strlen(native_name);
830 for (variable = efi_variables_list; variable && *variable;) {
831 if (!strncmp(variable, native_name, name_len) &&
832 variable[name_len] == '=')
835 variable = strchr(variable, '\n');
841 if (!(variable && *variable))
842 return EFI_EXIT(EFI_INVALID_PARAMETER);
845 variable = strchr(variable, '\n');
848 if (!(variable && *variable))
849 return EFI_EXIT(EFI_NOT_FOUND);
852 *new search: free a list used in the previous search
854 free(efi_variables_list);
855 efi_variables_list = NULL;
856 efi_cur_variable = NULL;
858 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
859 list_len = hexport_r(&env_htab, '\n',
860 H_MATCH_REGEX | H_MATCH_KEY,
861 &efi_variables_list, 0, 1, regexlist);
864 return EFI_EXIT(EFI_NOT_FOUND);
866 variable = efi_variables_list;
869 ret = parse_uboot_variable(variable, variable_name_size, variable_name,
870 vendor, &attributes);
872 return EFI_EXIT(ret);
875 static efi_status_t efi_set_variable_common(u16 *variable_name,
876 const efi_guid_t *vendor,
878 efi_uintn_t data_size,
882 char *native_name = NULL, *old_data = NULL, *val = NULL, *s;
883 efi_uintn_t old_size;
887 efi_status_t ret = EFI_SUCCESS;
889 debug("%s: set '%s'\n", __func__, native_name);
891 if (!variable_name || !*variable_name || !vendor ||
892 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
893 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
894 ret = EFI_INVALID_PARAMETER;
898 ret = efi_to_native(&native_name, variable_name, vendor);
902 /* check if a variable exists */
905 ret = efi_get_variable_common(variable_name, vendor, &attr,
907 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
908 attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
909 delete = !append && (!data_size || !attributes);
911 /* check attributes */
913 if (ro_check && (attr & READ_ONLY)) {
914 ret = EFI_WRITE_PROTECTED;
918 /* attributes won't be changed */
920 ((ro_check && attr != attributes) ||
921 (!ro_check && ((attr & ~(u32)READ_ONLY)
922 != (attributes & ~(u32)READ_ONLY))))) {
923 ret = EFI_INVALID_PARAMETER;
927 if (delete || append) {
929 * Trying to delete or to update a non-existent
937 if (((!u16_strcmp(variable_name, L"PK") ||
938 !u16_strcmp(variable_name, L"KEK")) &&
939 !guidcmp(vendor, &efi_global_variable_guid)) ||
940 ((!u16_strcmp(variable_name, L"db") ||
941 !u16_strcmp(variable_name, L"dbx")) &&
942 !guidcmp(vendor, &efi_guid_image_security_database))) {
943 /* authentication is mandatory */
945 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
946 debug("%ls: AUTHENTICATED_WRITE_ACCESS required\n",
948 ret = EFI_INVALID_PARAMETER;
953 /* authenticate a variable */
954 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
955 if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
956 ret = EFI_INVALID_PARAMETER;
960 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
961 ret = efi_variable_authenticate(variable_name, vendor,
965 if (ret != EFI_SUCCESS)
968 /* last chance to check for delete */
974 (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
975 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
976 debug("Secure boot is not configured\n");
977 ret = EFI_INVALID_PARAMETER;
982 /* delete a variable */
984 /* !old_size case has been handled before */
991 old_data = malloc(old_size);
993 ret = EFI_OUT_OF_RESOURCES;
996 ret = efi_get_variable_common(variable_name, vendor,
997 &attr, &old_size, old_data);
998 if (ret != EFI_SUCCESS)
1004 val = malloc(2 * old_size + 2 * data_size
1005 + strlen("{ro,run,boot,nv,time=0123456701234567}(blob)")
1008 ret = EFI_OUT_OF_RESOURCES;
1017 attributes &= (READ_ONLY |
1018 EFI_VARIABLE_NON_VOLATILE |
1019 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1020 EFI_VARIABLE_RUNTIME_ACCESS |
1021 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS);
1022 s += sprintf(s, "{");
1023 while (attributes) {
1024 attr = 1 << (ffs(attributes) - 1);
1026 if (attr == READ_ONLY) {
1027 s += sprintf(s, "ro");
1028 } else if (attr == EFI_VARIABLE_NON_VOLATILE) {
1029 s += sprintf(s, "nv");
1030 } else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) {
1031 s += sprintf(s, "boot");
1032 } else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) {
1033 s += sprintf(s, "run");
1035 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
1036 s += sprintf(s, "time=");
1037 s = bin2hex(s, (u8 *)&time, sizeof(time));
1040 attributes &= ~attr;
1042 s += sprintf(s, ",");
1044 s += sprintf(s, "}");
1045 s += sprintf(s, "(blob)");
1047 /* store payload: */
1049 s = bin2hex(s, old_data, old_size);
1050 s = bin2hex(s, data, data_size);
1053 EFI_PRINT("setting: %s=%s\n", native_name, val);
1056 if (env_set(native_name, val)) {
1057 ret = EFI_DEVICE_ERROR;
1059 bool vendor_keys_modified = false;
1061 if ((u16_strcmp(variable_name, L"PK") == 0 &&
1062 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1063 ret = efi_transfer_secure_state(
1064 (delete ? EFI_MODE_SETUP :
1066 if (ret != EFI_SUCCESS)
1069 if (efi_secure_mode != EFI_MODE_SETUP)
1070 vendor_keys_modified = true;
1071 } else if ((u16_strcmp(variable_name, L"KEK") == 0 &&
1072 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1073 if (efi_secure_mode != EFI_MODE_SETUP)
1074 vendor_keys_modified = true;
1077 /* update VendorKeys */
1078 if (vendor_keys_modified & efi_vendor_keys) {
1079 efi_vendor_keys = 0;
1080 ret = efi_set_variable_common(
1082 &efi_global_variable_guid,
1083 EFI_VARIABLE_BOOTSERVICE_ACCESS
1084 | EFI_VARIABLE_RUNTIME_ACCESS
1086 sizeof(efi_vendor_keys),
1103 * efi_set_variable() - set value of a UEFI variable
1105 * This function implements the SetVariable runtime service.
1107 * See the Unified Extensible Firmware Interface (UEFI) specification for
1110 * @variable_name: name of the variable
1111 * @vendor: vendor GUID
1112 * @attributes: attributes of the variable
1113 * @data_size: size of the buffer with the variable value
1114 * @data: buffer with the variable value
1115 * Return: status code
1117 efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
1118 const efi_guid_t *vendor, u32 attributes,
1119 efi_uintn_t data_size, const void *data)
1121 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
1124 /* READ_ONLY bit is not part of API */
1125 attributes &= ~(u32)READ_ONLY;
1127 return EFI_EXIT(efi_set_variable_common(variable_name, vendor,
1128 attributes, data_size, data,
1133 * efi_query_variable_info() - get information about EFI variables
1135 * This function implements the QueryVariableInfo() runtime service.
1137 * See the Unified Extensible Firmware Interface (UEFI) specification for
1140 * @attributes: bitmask to select variables to be
1142 * @maximum_variable_storage_size: maximum size of storage area for the
1143 * selected variable types
1144 * @remaining_variable_storage_size: remaining size of storage are for the
1145 * selected variable types
1146 * @maximum_variable_size: maximum size of a variable of the
1148 * Returns: status code
1150 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
1152 u64 *maximum_variable_storage_size,
1153 u64 *remaining_variable_storage_size,
1154 u64 *maximum_variable_size)
1156 return EFI_UNSUPPORTED;
1160 * efi_get_variable_runtime() - runtime implementation of GetVariable()
1162 * @variable_name: name of the variable
1163 * @vendor: vendor GUID
1164 * @attributes: attributes of the variable
1165 * @data_size: size of the buffer to which the variable value is copied
1166 * @data: buffer to which the variable value is copied
1167 * Return: status code
1169 static efi_status_t __efi_runtime EFIAPI
1170 efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1171 u32 *attributes, efi_uintn_t *data_size, void *data)
1173 return EFI_UNSUPPORTED;
1177 * efi_get_next_variable_name_runtime() - runtime implementation of
1180 * @variable_name_size: size of variable_name buffer in byte
1181 * @variable_name: name of uefi variable's name in u16
1182 * @vendor: vendor's guid
1183 * Return: status code
1185 static efi_status_t __efi_runtime EFIAPI
1186 efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
1187 u16 *variable_name, efi_guid_t *vendor)
1189 return EFI_UNSUPPORTED;
1193 * efi_set_variable_runtime() - runtime implementation of SetVariable()
1195 * @variable_name: name of the variable
1196 * @vendor: vendor GUID
1197 * @attributes: attributes of the variable
1198 * @data_size: size of the buffer with the variable value
1199 * @data: buffer with the variable value
1200 * Return: status code
1202 static efi_status_t __efi_runtime EFIAPI
1203 efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1204 u32 attributes, efi_uintn_t data_size,
1207 return EFI_UNSUPPORTED;
1211 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
1213 void efi_variables_boot_exit_notify(void)
1215 efi_runtime_services.get_variable = efi_get_variable_runtime;
1216 efi_runtime_services.get_next_variable_name =
1217 efi_get_next_variable_name_runtime;
1218 efi_runtime_services.set_variable = efi_set_variable_runtime;
1219 efi_update_table_header_crc32(&efi_runtime_services.hdr);
1223 * efi_init_variables() - initialize variable services
1225 * Return: status code
1227 efi_status_t efi_init_variables(void)
1231 ret = efi_init_secure_state();