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)
33 static efi_status_t efi_get_variable_common(u16 *variable_name,
34 const efi_guid_t *vendor,
36 efi_uintn_t *data_size, void *data);
38 static efi_status_t efi_set_variable_common(u16 *variable_name,
39 const efi_guid_t *vendor,
41 efi_uintn_t data_size,
46 * Mapping between EFI variables and u-boot variables:
48 * efi_$guid_$varname = {attributes}(type)value
52 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
53 * "{ro,boot,run}(blob)0000000000000000"
54 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
57 * The attributes are a comma separated list of these possible
61 * + boot - boot-services access
62 * + run - runtime access
64 * NOTE: with current implementation, no variables are available after
65 * ExitBootServices, and all are persisted (if possible).
67 * If not specified, the attributes default to "{boot}".
69 * The required type is one of:
71 * + utf8 - raw utf8 string
72 * + blob - arbitrary length hex string
74 * Maybe a utf16 type would be useful to for a string value to be auto
78 #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
81 * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
84 * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
85 * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
86 * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
88 * @native: pointer to pointer to U-Boot variable name
89 * @variable_name: UEFI variable name
90 * @vendor: vendor GUID
93 static efi_status_t efi_to_native(char **native, const u16 *variable_name,
94 const efi_guid_t *vendor)
99 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
100 *native = malloc(len);
102 return EFI_OUT_OF_RESOURCES;
105 pos += sprintf(pos, "efi_%pUl_", vendor);
106 utf16_utf8_strcpy(&pos, variable_name);
112 * prefix() - skip over prefix
114 * Skip over a prefix string.
116 * @str: string with prefix
117 * @prefix: prefix string
118 * Return: string without prefix, or NULL if prefix not found
120 static const char *prefix(const char *str, const char *prefix)
122 size_t n = strlen(prefix);
123 if (!strncmp(prefix, str, n))
129 * parse_attr() - decode attributes part of variable value
131 * Convert the string encoded attributes of a UEFI variable to a bit mask.
132 * TODO: Several attributes are not supported.
134 * @str: value of U-Boot variable
135 * @attrp: pointer to UEFI attributes
136 * @timep: pointer to time attribute
137 * Return: pointer to remainder of U-Boot variable value
139 static const char *parse_attr(const char *str, u32 *attrp, u64 *timep)
145 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
149 while (*str == sep) {
154 if ((s = prefix(str, "ro"))) {
156 } else if ((s = prefix(str, "nv"))) {
157 attr |= EFI_VARIABLE_NON_VOLATILE;
158 } else if ((s = prefix(str, "boot"))) {
159 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
160 } else if ((s = prefix(str, "run"))) {
161 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
162 } else if ((s = prefix(str, "time="))) {
163 attr |= EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
164 hex2bin((u8 *)timep, s, sizeof(*timep));
165 s += sizeof(*timep) * 2;
166 } else if (*str == '}') {
169 printf("invalid attribute: %s\n", str);
185 * efi_set_secure_state - modify secure boot state variables
186 * @sec_boot: value of SecureBoot
187 * @setup_mode: value of SetupMode
188 * @audit_mode: value of AuditMode
189 * @deployed_mode: value of DeployedMode
191 * Modify secure boot stat-related variables as indicated.
193 * Return: status code
195 static efi_status_t efi_set_secure_state(int sec_boot, int setup_mode,
196 int audit_mode, int deployed_mode)
201 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
202 EFI_VARIABLE_RUNTIME_ACCESS |
204 ret = efi_set_variable_common(L"SecureBoot", &efi_global_variable_guid,
205 attributes, sizeof(sec_boot), &sec_boot,
207 if (ret != EFI_SUCCESS)
210 ret = efi_set_variable_common(L"SetupMode", &efi_global_variable_guid,
211 attributes, sizeof(setup_mode),
213 if (ret != EFI_SUCCESS)
216 ret = efi_set_variable_common(L"AuditMode", &efi_global_variable_guid,
217 attributes, sizeof(audit_mode),
219 if (ret != EFI_SUCCESS)
222 ret = efi_set_variable_common(L"DeployedMode",
223 &efi_global_variable_guid, attributes,
224 sizeof(deployed_mode), &deployed_mode,
231 * efi_transfer_secure_state - handle a secure boot state transition
234 * Depending on @mode, secure boot related variables are updated.
235 * Those variables are *read-only* for users, efi_set_variable_common()
238 * Return: status code
240 static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
244 debug("Switching secure state from %d to %d\n", efi_secure_mode, mode);
246 if (mode == EFI_MODE_DEPLOYED) {
247 ret = efi_set_secure_state(1, 0, 0, 1);
248 if (ret != EFI_SUCCESS)
251 efi_secure_boot = true;
252 } else if (mode == EFI_MODE_AUDIT) {
253 ret = efi_set_variable_common(L"PK", &efi_global_variable_guid,
254 EFI_VARIABLE_BOOTSERVICE_ACCESS |
255 EFI_VARIABLE_RUNTIME_ACCESS,
257 if (ret != EFI_SUCCESS)
260 ret = efi_set_secure_state(0, 1, 1, 0);
261 if (ret != EFI_SUCCESS)
264 efi_secure_boot = true;
265 } else if (mode == EFI_MODE_USER) {
266 ret = efi_set_secure_state(1, 0, 0, 0);
267 if (ret != EFI_SUCCESS)
270 efi_secure_boot = true;
271 } else if (mode == EFI_MODE_SETUP) {
272 ret = efi_set_secure_state(0, 1, 0, 0);
273 if (ret != EFI_SUCCESS)
276 return EFI_INVALID_PARAMETER;
279 efi_secure_mode = mode;
284 /* TODO: What action should be taken here? */
285 printf("ERROR: Secure state transition failed\n");
290 * efi_init_secure_state - initialize secure boot state
292 * Return: status code
294 static efi_status_t efi_init_secure_state(void)
296 enum efi_secure_mode mode;
302 * Since there is currently no "platform-specific" installation
303 * method of Platform Key, we can't say if VendorKeys is 0 or 1
308 ret = efi_get_variable_common(L"PK", &efi_global_variable_guid,
310 if (ret == EFI_BUFFER_TOO_SMALL) {
311 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT))
312 mode = EFI_MODE_USER;
314 mode = EFI_MODE_SETUP;
317 } else if (ret == EFI_NOT_FOUND) {
318 mode = EFI_MODE_SETUP;
324 ret = efi_transfer_secure_state(mode);
325 if (ret == EFI_SUCCESS)
326 ret = efi_set_variable_common(L"VendorKeys",
327 &efi_global_variable_guid,
328 EFI_VARIABLE_BOOTSERVICE_ACCESS |
329 EFI_VARIABLE_RUNTIME_ACCESS |
331 sizeof(efi_vendor_keys),
332 &efi_vendor_keys, false);
339 * efi_secure_boot_enabled - return if secure boot is enabled or not
341 * Return: true if enabled, false if disabled
343 bool efi_secure_boot_enabled(void)
345 return efi_secure_boot;
348 #ifdef CONFIG_EFI_SECURE_BOOT
349 static u8 pkcs7_hdr[] = {
351 0x30, 0x82, 0x05, 0xc7,
352 /* OID: pkcs7-signedData */
353 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
354 /* Context Structured? */
355 0xa0, 0x82, 0x05, 0xb8,
359 * efi_variable_parse_signature - parse a signature in variable
360 * @buf: Pointer to variable's value
361 * @buflen: Length of @buf
363 * Parse a signature embedded in variable's value and instantiate
364 * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
365 * pkcs7's signedData, some header needed be prepended for correctly
366 * parsing authentication data, particularly for variable's.
368 * Return: Pointer to pkcs7_message structure on success, NULL on error
370 static struct pkcs7_message *efi_variable_parse_signature(const void *buf,
375 struct pkcs7_message *msg;
378 * This is the best assumption to check if the binary is
379 * already in a form of pkcs7's signedData.
381 if (buflen > sizeof(pkcs7_hdr) &&
382 !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
383 msg = pkcs7_parse_message(buf, buflen);
388 * Otherwise, we should add a dummy prefix sequence for pkcs7
389 * message parser to be able to process.
390 * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
391 * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
393 * The header should be composed in a more refined manner.
395 debug("Makeshift prefix added to authentication data\n");
396 ebuflen = sizeof(pkcs7_hdr) + buflen;
397 if (ebuflen <= 0x7f) {
398 debug("Data is too short\n");
402 ebuf = malloc(ebuflen);
404 debug("Out of memory\n");
408 memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
409 memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
411 ebuf[2] = (len >> 8) & 0xff;
412 ebuf[3] = len & 0xff;
413 len = ebuflen - 0x13;
414 ebuf[0x11] = (len >> 8) & 0xff;
415 ebuf[0x12] = len & 0xff;
417 msg = pkcs7_parse_message(ebuf, ebuflen);
429 * efi_variable_authenticate - authenticate a variable
430 * @variable: Variable name in u16
431 * @vendor: Guid of variable
432 * @data_size: Size of @data
433 * @data: Pointer to variable's value
434 * @given_attr: Attributes to be given at SetVariable()
435 * @env_attr: Attributes that an existing variable holds
436 * @time: signed time that an existing variable holds
438 * Called by efi_set_variable() to verify that the input is correct.
439 * Will replace the given data pointer with another that points to
440 * the actual data to store in the internal memory.
441 * On success, @data and @data_size will be replaced with variable's
442 * actual data, excluding authentication data, and its size, and variable's
443 * attributes and signed time will also be returned in @env_attr and @time,
446 * Return: status code
448 static efi_status_t efi_variable_authenticate(u16 *variable,
449 const efi_guid_t *vendor,
450 efi_uintn_t *data_size,
451 const void **data, u32 given_attr,
452 u32 *env_attr, u64 *time)
454 const struct efi_variable_authentication_2 *auth;
455 struct efi_signature_store *truststore, *truststore2;
456 struct pkcs7_message *var_sig;
457 struct efi_image_regions *regs;
458 struct efi_time timestamp;
467 ret = EFI_SECURITY_VIOLATION;
469 if (*data_size < sizeof(struct efi_variable_authentication_2))
472 /* authentication data */
474 if (*data_size < (sizeof(auth->time_stamp)
475 + auth->auth_info.hdr.dwLength))
478 if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
481 *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
482 *data_size -= (sizeof(auth->time_stamp)
483 + auth->auth_info.hdr.dwLength);
485 memcpy(×tamp, &auth->time_stamp, sizeof(timestamp));
486 memset(&tm, 0, sizeof(tm));
487 tm.tm_year = timestamp.year;
488 tm.tm_mon = timestamp.month;
489 tm.tm_mday = timestamp.day;
490 tm.tm_hour = timestamp.hour;
491 tm.tm_min = timestamp.minute;
492 tm.tm_sec = timestamp.second;
493 new_time = rtc_mktime(&tm);
495 if (!efi_secure_boot_enabled()) {
496 /* finished checking */
501 if (new_time <= *time)
504 /* data to be digested */
505 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
509 efi_image_region_add(regs, (uint8_t *)variable,
511 + u16_strlen(variable) * sizeof(u16), 1);
512 efi_image_region_add(regs, (uint8_t *)vendor,
513 (uint8_t *)vendor + sizeof(*vendor), 1);
514 efi_image_region_add(regs, (uint8_t *)&given_attr,
515 (uint8_t *)&given_attr + sizeof(given_attr), 1);
516 efi_image_region_add(regs, (uint8_t *)×tamp,
517 (uint8_t *)×tamp + sizeof(timestamp), 1);
518 efi_image_region_add(regs, (uint8_t *)*data,
519 (uint8_t *)*data + *data_size, 1);
521 /* variable's signature list */
522 if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
524 var_sig = efi_variable_parse_signature(auth->auth_info.cert_data,
525 auth->auth_info.hdr.dwLength
526 - sizeof(auth->auth_info));
528 debug("Parsing variable's signature failed\n");
532 /* signature database used for authentication */
533 if (u16_strcmp(variable, L"PK") == 0 ||
534 u16_strcmp(variable, L"KEK") == 0) {
536 truststore = efi_sigstore_parse_sigdb(L"PK");
539 } else if (u16_strcmp(variable, L"db") == 0 ||
540 u16_strcmp(variable, L"dbx") == 0) {
541 /* with PK and KEK */
542 truststore = efi_sigstore_parse_sigdb(L"KEK");
543 truststore2 = efi_sigstore_parse_sigdb(L"PK");
549 truststore = truststore2;
553 /* TODO: support private authenticated variables */
557 /* verify signature */
558 if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) {
562 efi_signature_verify_with_sigdb(regs, var_sig,
563 truststore2, NULL)) {
566 debug("Verifying variable's signature failed\n");
571 /* finished checking */
572 *time = rtc_mktime(&tm);
576 efi_sigstore_free(truststore);
577 efi_sigstore_free(truststore2);
578 pkcs7_free_message(var_sig);
584 static efi_status_t efi_variable_authenticate(u16 *variable,
585 const efi_guid_t *vendor,
586 efi_uintn_t *data_size,
587 const void **data, u32 given_attr,
588 u32 *env_attr, u64 *time)
592 #endif /* CONFIG_EFI_SECURE_BOOT */
594 static efi_status_t efi_get_variable_common(u16 *variable_name,
595 const efi_guid_t *vendor,
597 efi_uintn_t *data_size, void *data)
601 unsigned long in_size;
602 const char *val = NULL, *s;
606 if (!variable_name || !vendor || !data_size)
607 return EFI_EXIT(EFI_INVALID_PARAMETER);
609 ret = efi_to_native(&native_name, variable_name, vendor);
613 EFI_PRINT("get '%s'\n", native_name);
615 val = env_get(native_name);
618 return EFI_NOT_FOUND;
620 val = parse_attr(val, &attr, &time);
622 in_size = *data_size;
624 if ((s = prefix(val, "(blob)"))) {
625 size_t len = strlen(s);
627 /* number of hexadecimal digits must be even */
629 return EFI_DEVICE_ERROR;
631 /* two characters per byte: */
636 ret = EFI_BUFFER_TOO_SMALL;
641 debug("Variable with no data shouldn't exist.\n");
642 return EFI_INVALID_PARAMETER;
645 if (hex2bin(data, s, len))
646 return EFI_DEVICE_ERROR;
648 EFI_PRINT("got value: \"%s\"\n", s);
649 } else if ((s = prefix(val, "(utf8)"))) {
650 unsigned len = strlen(s) + 1;
655 ret = EFI_BUFFER_TOO_SMALL;
660 debug("Variable with no data shouldn't exist.\n");
661 return EFI_INVALID_PARAMETER;
664 memcpy(data, s, len);
665 ((char *)data)[len] = '\0';
667 EFI_PRINT("got value: \"%s\"\n", (char *)data);
669 EFI_PRINT("invalid value: '%s'\n", val);
670 return EFI_DEVICE_ERROR;
675 *attributes = attr & EFI_VARIABLE_MASK;
681 * efi_efi_get_variable() - retrieve value of a UEFI variable
683 * This function implements the GetVariable runtime service.
685 * See the Unified Extensible Firmware Interface (UEFI) specification for
688 * @variable_name: name of the variable
689 * @vendor: vendor GUID
690 * @attributes: attributes of the variable
691 * @data_size: size of the buffer to which the variable value is copied
692 * @data: buffer to which the variable value is copied
693 * Return: status code
695 efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
696 const efi_guid_t *vendor, u32 *attributes,
697 efi_uintn_t *data_size, void *data)
701 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
704 ret = efi_get_variable_common(variable_name, vendor, attributes,
706 return EFI_EXIT(ret);
709 static char *efi_variables_list;
710 static char *efi_cur_variable;
713 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
715 * @variable: whole data of u-boot variable (ie. name=value)
716 * @variable_name_size: size of variable_name buffer in byte
717 * @variable_name: name of uefi variable in u16, null-terminated
718 * @vendor: vendor's guid
719 * @attributes: attributes
721 * A uefi variable is encoded into a u-boot variable as described above.
722 * This function parses such a u-boot variable and retrieve uefi-related
723 * information into respective parameters. In return, variable_name_size
724 * is the size of variable name including NULL.
726 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
727 * the entire variable list has been returned,
728 * otherwise non-zero status code
730 static efi_status_t parse_uboot_variable(char *variable,
731 efi_uintn_t *variable_name_size,
733 const efi_guid_t *vendor,
736 char *guid, *name, *end, c;
738 efi_uintn_t old_variable_name_size;
742 guid = strchr(variable, '_');
744 return EFI_INVALID_PARAMETER;
746 name = strchr(guid, '_');
748 return EFI_INVALID_PARAMETER;
750 end = strchr(name, '=');
752 return EFI_INVALID_PARAMETER;
754 name_len = end - name;
755 old_variable_name_size = *variable_name_size;
756 *variable_name_size = sizeof(u16) * (name_len + 1);
757 if (old_variable_name_size < *variable_name_size)
758 return EFI_BUFFER_TOO_SMALL;
760 end++; /* point to value */
764 utf8_utf16_strncpy(&p, name, name_len);
765 variable_name[name_len] = 0;
769 *(name - 1) = '\0'; /* guid need be null-terminated here */
770 uuid_str_to_bin(guid, (unsigned char *)vendor, UUID_STR_FORMAT_GUID);
774 parse_attr(end, attributes, &time);
780 * efi_get_next_variable_name() - enumerate the current variable names
782 * @variable_name_size: size of variable_name buffer in byte
783 * @variable_name: name of uefi variable's name in u16
784 * @vendor: vendor's guid
786 * This function implements the GetNextVariableName service.
788 * See the Unified Extensible Firmware Interface (UEFI) specification for
791 * Return: status code
793 efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
797 char *native_name, *variable;
798 ssize_t name_len, list_len;
800 char * const regexlist[] = {regex};
805 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
807 if (!variable_name_size || !variable_name || !vendor)
808 return EFI_EXIT(EFI_INVALID_PARAMETER);
810 if (variable_name[0]) {
811 /* check null-terminated string */
812 for (i = 0; i < *variable_name_size; i++)
813 if (!variable_name[i])
815 if (i >= *variable_name_size)
816 return EFI_EXIT(EFI_INVALID_PARAMETER);
818 /* search for the last-returned variable */
819 ret = efi_to_native(&native_name, variable_name, vendor);
821 return EFI_EXIT(ret);
823 name_len = strlen(native_name);
824 for (variable = efi_variables_list; variable && *variable;) {
825 if (!strncmp(variable, native_name, name_len) &&
826 variable[name_len] == '=')
829 variable = strchr(variable, '\n');
835 if (!(variable && *variable))
836 return EFI_EXIT(EFI_INVALID_PARAMETER);
839 variable = strchr(variable, '\n');
842 if (!(variable && *variable))
843 return EFI_EXIT(EFI_NOT_FOUND);
846 *new search: free a list used in the previous search
848 free(efi_variables_list);
849 efi_variables_list = NULL;
850 efi_cur_variable = NULL;
852 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
853 list_len = hexport_r(&env_htab, '\n',
854 H_MATCH_REGEX | H_MATCH_KEY,
855 &efi_variables_list, 0, 1, regexlist);
858 return EFI_EXIT(EFI_NOT_FOUND);
860 variable = efi_variables_list;
863 ret = parse_uboot_variable(variable, variable_name_size, variable_name,
864 vendor, &attributes);
866 return EFI_EXIT(ret);
869 static efi_status_t efi_set_variable_common(u16 *variable_name,
870 const efi_guid_t *vendor,
872 efi_uintn_t data_size,
876 char *native_name = NULL, *old_data = NULL, *val = NULL, *s;
877 efi_uintn_t old_size;
881 efi_status_t ret = EFI_SUCCESS;
883 debug("%s: set '%s'\n", __func__, native_name);
885 if (!variable_name || !*variable_name || !vendor ||
886 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
887 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
888 ret = EFI_INVALID_PARAMETER;
892 ret = efi_to_native(&native_name, variable_name, vendor);
896 /* check if a variable exists */
899 ret = efi_get_variable_common(variable_name, vendor, &attr,
901 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
902 attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
903 delete = !append && (!data_size || !attributes);
905 /* check attributes */
907 if (ro_check && (attr & READ_ONLY)) {
908 ret = EFI_WRITE_PROTECTED;
912 /* attributes won't be changed */
914 ((ro_check && attr != attributes) ||
915 (!ro_check && ((attr & ~(u32)READ_ONLY)
916 != (attributes & ~(u32)READ_ONLY))))) {
917 ret = EFI_INVALID_PARAMETER;
921 if (delete || append) {
923 * Trying to delete or to update a non-existent
931 if (((!u16_strcmp(variable_name, L"PK") ||
932 !u16_strcmp(variable_name, L"KEK")) &&
933 !guidcmp(vendor, &efi_global_variable_guid)) ||
934 ((!u16_strcmp(variable_name, L"db") ||
935 !u16_strcmp(variable_name, L"dbx")) &&
936 !guidcmp(vendor, &efi_guid_image_security_database))) {
937 /* authentication is mandatory */
939 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
940 debug("%ls: AUTHENTICATED_WRITE_ACCESS required\n",
942 ret = EFI_INVALID_PARAMETER;
947 /* authenticate a variable */
948 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
949 if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
950 ret = EFI_INVALID_PARAMETER;
954 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
955 ret = efi_variable_authenticate(variable_name, vendor,
959 if (ret != EFI_SUCCESS)
962 /* last chance to check for delete */
968 (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
969 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
970 debug("Secure boot is not configured\n");
971 ret = EFI_INVALID_PARAMETER;
976 /* delete a variable */
978 /* !old_size case has been handled before */
985 old_data = malloc(old_size);
987 ret = EFI_OUT_OF_RESOURCES;
990 ret = efi_get_variable_common(variable_name, vendor,
991 &attr, &old_size, old_data);
992 if (ret != EFI_SUCCESS)
998 val = malloc(2 * old_size + 2 * data_size
999 + strlen("{ro,run,boot,nv,time=0123456701234567}(blob)")
1002 ret = EFI_OUT_OF_RESOURCES;
1011 attributes &= (READ_ONLY |
1012 EFI_VARIABLE_NON_VOLATILE |
1013 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1014 EFI_VARIABLE_RUNTIME_ACCESS |
1015 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS);
1016 s += sprintf(s, "{");
1017 while (attributes) {
1018 attr = 1 << (ffs(attributes) - 1);
1020 if (attr == READ_ONLY) {
1021 s += sprintf(s, "ro");
1022 } else if (attr == EFI_VARIABLE_NON_VOLATILE) {
1023 s += sprintf(s, "nv");
1024 } else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) {
1025 s += sprintf(s, "boot");
1026 } else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) {
1027 s += sprintf(s, "run");
1029 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
1030 s += sprintf(s, "time=");
1031 s = bin2hex(s, (u8 *)&time, sizeof(time));
1034 attributes &= ~attr;
1036 s += sprintf(s, ",");
1038 s += sprintf(s, "}");
1039 s += sprintf(s, "(blob)");
1041 /* store payload: */
1043 s = bin2hex(s, old_data, old_size);
1044 s = bin2hex(s, data, data_size);
1047 EFI_PRINT("setting: %s=%s\n", native_name, val);
1050 if (env_set(native_name, val)) {
1051 ret = EFI_DEVICE_ERROR;
1053 bool vendor_keys_modified = false;
1055 if ((u16_strcmp(variable_name, L"PK") == 0 &&
1056 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1057 ret = efi_transfer_secure_state(
1058 (delete ? EFI_MODE_SETUP :
1060 if (ret != EFI_SUCCESS)
1063 if (efi_secure_mode != EFI_MODE_SETUP)
1064 vendor_keys_modified = true;
1065 } else if ((u16_strcmp(variable_name, L"KEK") == 0 &&
1066 guidcmp(vendor, &efi_global_variable_guid) == 0)) {
1067 if (efi_secure_mode != EFI_MODE_SETUP)
1068 vendor_keys_modified = true;
1071 /* update VendorKeys */
1072 if (vendor_keys_modified & efi_vendor_keys) {
1073 efi_vendor_keys = 0;
1074 ret = efi_set_variable_common(
1076 &efi_global_variable_guid,
1077 EFI_VARIABLE_BOOTSERVICE_ACCESS
1078 | EFI_VARIABLE_RUNTIME_ACCESS
1080 sizeof(efi_vendor_keys),
1097 * efi_set_variable() - set value of a UEFI variable
1099 * This function implements the SetVariable runtime service.
1101 * See the Unified Extensible Firmware Interface (UEFI) specification for
1104 * @variable_name: name of the variable
1105 * @vendor: vendor GUID
1106 * @attributes: attributes of the variable
1107 * @data_size: size of the buffer with the variable value
1108 * @data: buffer with the variable value
1109 * Return: status code
1111 efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
1112 const efi_guid_t *vendor, u32 attributes,
1113 efi_uintn_t data_size, const void *data)
1115 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
1118 /* READ_ONLY bit is not part of API */
1119 attributes &= ~(u32)READ_ONLY;
1121 return EFI_EXIT(efi_set_variable_common(variable_name, vendor,
1122 attributes, data_size, data,
1127 * efi_query_variable_info() - get information about EFI variables
1129 * This function implements the QueryVariableInfo() runtime service.
1131 * See the Unified Extensible Firmware Interface (UEFI) specification for
1134 * @attributes: bitmask to select variables to be
1136 * @maximum_variable_storage_size: maximum size of storage area for the
1137 * selected variable types
1138 * @remaining_variable_storage_size: remaining size of storage are for the
1139 * selected variable types
1140 * @maximum_variable_size: maximum size of a variable of the
1142 * Returns: status code
1144 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
1146 u64 *maximum_variable_storage_size,
1147 u64 *remaining_variable_storage_size,
1148 u64 *maximum_variable_size)
1150 return EFI_UNSUPPORTED;
1154 * efi_get_variable_runtime() - runtime implementation of GetVariable()
1156 * @variable_name: name of the variable
1157 * @vendor: vendor GUID
1158 * @attributes: attributes of the variable
1159 * @data_size: size of the buffer to which the variable value is copied
1160 * @data: buffer to which the variable value is copied
1161 * Return: status code
1163 static efi_status_t __efi_runtime EFIAPI
1164 efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1165 u32 *attributes, efi_uintn_t *data_size, void *data)
1167 return EFI_UNSUPPORTED;
1171 * efi_get_next_variable_name_runtime() - runtime implementation of
1174 * @variable_name_size: size of variable_name buffer in byte
1175 * @variable_name: name of uefi variable's name in u16
1176 * @vendor: vendor's guid
1177 * Return: status code
1179 static efi_status_t __efi_runtime EFIAPI
1180 efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
1181 u16 *variable_name, efi_guid_t *vendor)
1183 return EFI_UNSUPPORTED;
1187 * efi_set_variable_runtime() - runtime implementation of SetVariable()
1189 * @variable_name: name of the variable
1190 * @vendor: vendor GUID
1191 * @attributes: attributes of the variable
1192 * @data_size: size of the buffer with the variable value
1193 * @data: buffer with the variable value
1194 * Return: status code
1196 static efi_status_t __efi_runtime EFIAPI
1197 efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1198 u32 attributes, efi_uintn_t data_size,
1201 return EFI_UNSUPPORTED;
1205 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
1207 void efi_variables_boot_exit_notify(void)
1209 efi_runtime_services.get_variable = efi_get_variable_runtime;
1210 efi_runtime_services.get_next_variable_name =
1211 efi_get_next_variable_name_runtime;
1212 efi_runtime_services.set_variable = efi_set_variable_runtime;
1213 efi_update_table_header_crc32(&efi_runtime_services.hdr);
1217 * efi_init_variables() - initialize variable services
1219 * Return: status code
1221 efi_status_t efi_init_variables(void)
1225 ret = efi_init_secure_state();