1 // SPDX-License-Identifier: GPL-2.0+
3 * Originally from efivars.c
5 * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
6 * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
9 #include <linux/capability.h>
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/string.h>
16 #include <linux/smp.h>
17 #include <linux/efi.h>
18 #include <linux/sysfs.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/ctype.h>
22 #include <linux/ucs2_string.h>
24 /* Private pointer to registered efivars */
25 static struct efivars
*__efivars
;
28 * efivars_lock protects three things:
29 * 1) efivarfs_list and efivars_sysfs_list
31 * 3) (un)registration of __efivars
33 static DEFINE_SEMAPHORE(efivars_lock
);
36 validate_device_path(efi_char16_t
*var_name
, int match
, u8
*buffer
,
39 struct efi_generic_dev_path
*node
;
42 node
= (struct efi_generic_dev_path
*)buffer
;
44 if (len
< sizeof(*node
))
47 while (offset
<= len
- sizeof(*node
) &&
48 node
->length
>= sizeof(*node
) &&
49 node
->length
<= len
- offset
) {
50 offset
+= node
->length
;
52 if ((node
->type
== EFI_DEV_END_PATH
||
53 node
->type
== EFI_DEV_END_PATH2
) &&
54 node
->sub_type
== EFI_DEV_END_ENTIRE
)
57 node
= (struct efi_generic_dev_path
*)(buffer
+ offset
);
61 * If we're here then either node->length pointed past the end
62 * of the buffer or we reached the end of the buffer without
63 * finding a device path end node.
69 validate_boot_order(efi_char16_t
*var_name
, int match
, u8
*buffer
,
72 /* An array of 16-bit integers */
80 validate_load_option(efi_char16_t
*var_name
, int match
, u8
*buffer
,
84 int i
, desclength
= 0, namelen
;
86 namelen
= ucs2_strnlen(var_name
, EFI_VAR_NAME_LEN
);
88 /* Either "Boot" or "Driver" followed by four digits of hex */
89 for (i
= match
; i
< match
+4; i
++) {
90 if (var_name
[i
] > 127 ||
91 hex_to_bin(var_name
[i
] & 0xff) < 0)
95 /* Reject it if there's 4 digits of hex and then further content */
96 if (namelen
> match
+ 4)
99 /* A valid entry must be at least 8 bytes */
103 filepathlength
= buffer
[4] | buffer
[5] << 8;
106 * There's no stored length for the description, so it has to be
109 desclength
= ucs2_strsize((efi_char16_t
*)(buffer
+ 6), len
- 6) + 2;
111 /* Each boot entry must have a descriptor */
116 * If the sum of the length of the description, the claimed filepath
117 * length and the original header are greater than the length of the
118 * variable, it's malformed
120 if ((desclength
+ filepathlength
+ 6) > len
)
124 * And, finally, check the filepath
126 return validate_device_path(var_name
, match
, buffer
+ desclength
+ 6,
131 validate_uint16(efi_char16_t
*var_name
, int match
, u8
*buffer
,
134 /* A single 16-bit integer */
142 validate_ascii_string(efi_char16_t
*var_name
, int match
, u8
*buffer
,
147 for (i
= 0; i
< len
; i
++) {
158 struct variable_validate
{
161 bool (*validate
)(efi_char16_t
*var_name
, int match
, u8
*data
,
166 * This is the list of variables we need to validate, as well as the
167 * whitelist for what we think is safe not to default to immutable.
169 * If it has a validate() method that's not NULL, it'll go into the
170 * validation routine. If not, it is assumed valid, but still used for
173 * Note that it's sorted by {vendor,name}, but globbed names must come after
174 * any other name with the same prefix.
176 static const struct variable_validate variable_validate
[] = {
177 { EFI_GLOBAL_VARIABLE_GUID
, "BootNext", validate_uint16
},
178 { EFI_GLOBAL_VARIABLE_GUID
, "BootOrder", validate_boot_order
},
179 { EFI_GLOBAL_VARIABLE_GUID
, "Boot*", validate_load_option
},
180 { EFI_GLOBAL_VARIABLE_GUID
, "DriverOrder", validate_boot_order
},
181 { EFI_GLOBAL_VARIABLE_GUID
, "Driver*", validate_load_option
},
182 { EFI_GLOBAL_VARIABLE_GUID
, "ConIn", validate_device_path
},
183 { EFI_GLOBAL_VARIABLE_GUID
, "ConInDev", validate_device_path
},
184 { EFI_GLOBAL_VARIABLE_GUID
, "ConOut", validate_device_path
},
185 { EFI_GLOBAL_VARIABLE_GUID
, "ConOutDev", validate_device_path
},
186 { EFI_GLOBAL_VARIABLE_GUID
, "ErrOut", validate_device_path
},
187 { EFI_GLOBAL_VARIABLE_GUID
, "ErrOutDev", validate_device_path
},
188 { EFI_GLOBAL_VARIABLE_GUID
, "Lang", validate_ascii_string
},
189 { EFI_GLOBAL_VARIABLE_GUID
, "OsIndications", NULL
},
190 { EFI_GLOBAL_VARIABLE_GUID
, "PlatformLang", validate_ascii_string
},
191 { EFI_GLOBAL_VARIABLE_GUID
, "Timeout", validate_uint16
},
192 { LINUX_EFI_CRASH_GUID
, "*", NULL
},
193 { NULL_GUID
, "", NULL
},
197 * Check if @var_name matches the pattern given in @match_name.
199 * @var_name: an array of @len non-NUL characters.
200 * @match_name: a NUL-terminated pattern string, optionally ending in "*". A
201 * final "*" character matches any trailing characters @var_name,
202 * including the case when there are none left in @var_name.
203 * @match: on output, the number of non-wildcard characters in @match_name
204 * that @var_name matches, regardless of the return value.
205 * @return: whether @var_name fully matches @match_name.
208 variable_matches(const char *var_name
, size_t len
, const char *match_name
,
211 for (*match
= 0; ; (*match
)++) {
212 char c
= match_name
[*match
];
216 /* Wildcard in @match_name means we've matched. */
220 /* @match_name has ended. Has @var_name too? */
221 return (*match
== len
);
225 * We've reached a non-wildcard char in @match_name.
226 * Continue only if there's an identical character in
229 if (*match
< len
&& c
== var_name
[*match
])
237 efivar_validate(efi_guid_t vendor
, efi_char16_t
*var_name
, u8
*data
,
238 unsigned long data_size
)
241 unsigned long utf8_size
;
244 utf8_size
= ucs2_utf8size(var_name
);
245 utf8_name
= kmalloc(utf8_size
+ 1, GFP_KERNEL
);
249 ucs2_as_utf8(utf8_name
, var_name
, utf8_size
);
250 utf8_name
[utf8_size
] = '\0';
252 for (i
= 0; variable_validate
[i
].name
[0] != '\0'; i
++) {
253 const char *name
= variable_validate
[i
].name
;
256 if (efi_guidcmp(vendor
, variable_validate
[i
].vendor
))
259 if (variable_matches(utf8_name
, utf8_size
+1, name
, &match
)) {
260 if (variable_validate
[i
].validate
== NULL
)
263 return variable_validate
[i
].validate(var_name
, match
,
270 EXPORT_SYMBOL_GPL(efivar_validate
);
273 efivar_variable_is_removable(efi_guid_t vendor
, const char *var_name
,
281 * Check if our variable is in the validated variables list
283 for (i
= 0; variable_validate
[i
].name
[0] != '\0'; i
++) {
284 if (efi_guidcmp(variable_validate
[i
].vendor
, vendor
))
287 if (variable_matches(var_name
, len
,
288 variable_validate
[i
].name
, &match
)) {
295 * If it's in our list, it is removable.
299 EXPORT_SYMBOL_GPL(efivar_variable_is_removable
);
302 check_var_size(u32 attributes
, unsigned long size
)
304 const struct efivar_operations
*fops
;
307 return EFI_UNSUPPORTED
;
309 fops
= __efivars
->ops
;
311 if (!fops
->query_variable_store
)
312 return EFI_UNSUPPORTED
;
314 return fops
->query_variable_store(attributes
, size
, false);
318 check_var_size_nonblocking(u32 attributes
, unsigned long size
)
320 const struct efivar_operations
*fops
;
323 return EFI_UNSUPPORTED
;
325 fops
= __efivars
->ops
;
327 if (!fops
->query_variable_store
)
328 return EFI_UNSUPPORTED
;
330 return fops
->query_variable_store(attributes
, size
, true);
333 static bool variable_is_present(efi_char16_t
*variable_name
, efi_guid_t
*vendor
,
334 struct list_head
*head
)
336 struct efivar_entry
*entry
, *n
;
337 unsigned long strsize1
, strsize2
;
340 strsize1
= ucs2_strsize(variable_name
, 1024);
341 list_for_each_entry_safe(entry
, n
, head
, list
) {
342 strsize2
= ucs2_strsize(entry
->var
.VariableName
, 1024);
343 if (strsize1
== strsize2
&&
344 !memcmp(variable_name
, &(entry
->var
.VariableName
),
346 !efi_guidcmp(entry
->var
.VendorGuid
,
356 * Returns the size of variable_name, in bytes, including the
357 * terminating NULL character, or variable_name_size if no NULL
358 * character is found among the first variable_name_size bytes.
360 static unsigned long var_name_strnsize(efi_char16_t
*variable_name
,
361 unsigned long variable_name_size
)
367 * The variable name is, by definition, a NULL-terminated
368 * string, so make absolutely sure that variable_name_size is
369 * the value we expect it to be. If not, return the real size.
371 for (len
= 2; len
<= variable_name_size
; len
+= sizeof(c
)) {
372 c
= variable_name
[(len
/ sizeof(c
)) - 1];
377 return min(len
, variable_name_size
);
381 * Print a warning when duplicate EFI variables are encountered and
382 * disable the sysfs workqueue since the firmware is buggy.
384 static void dup_variable_bug(efi_char16_t
*str16
, efi_guid_t
*vendor_guid
,
387 size_t i
, len8
= len16
/ sizeof(efi_char16_t
);
390 str8
= kzalloc(len8
, GFP_KERNEL
);
394 for (i
= 0; i
< len8
; i
++)
397 printk(KERN_WARNING
"efivars: duplicate variable: %s-%pUl\n",
403 * efivar_init - build the initial list of EFI variables
404 * @func: callback function to invoke for every variable
405 * @data: function-specific data to pass to @func
406 * @duplicates: error if we encounter duplicates on @head?
407 * @head: initialised head of variable list
409 * Get every EFI variable from the firmware and invoke @func. @func
410 * should call efivar_entry_add() to build the list of variables.
412 * Returns 0 on success, or a kernel error code on failure.
414 int efivar_init(int (*func
)(efi_char16_t
*, efi_guid_t
, unsigned long, void *),
415 void *data
, bool duplicates
, struct list_head
*head
)
417 const struct efivar_operations
*ops
;
418 unsigned long variable_name_size
= 1024;
419 efi_char16_t
*variable_name
;
421 efi_guid_t vendor_guid
;
427 ops
= __efivars
->ops
;
429 variable_name
= kzalloc(variable_name_size
, GFP_KERNEL
);
430 if (!variable_name
) {
431 printk(KERN_ERR
"efivars: Memory allocation failed.\n");
435 if (down_interruptible(&efivars_lock
)) {
441 * Per EFI spec, the maximum storage allocated for both
442 * the variable name and variable data is 1024 bytes.
446 variable_name_size
= 1024;
448 status
= ops
->get_next_variable(&variable_name_size
,
456 variable_name_size
= var_name_strnsize(variable_name
,
460 * Some firmware implementations return the
461 * same variable name on multiple calls to
462 * get_next_variable(). Terminate the loop
463 * immediately as there is no guarantee that
464 * we'll ever see a different variable name,
465 * and may end up looping here forever.
468 variable_is_present(variable_name
, &vendor_guid
,
470 dup_variable_bug(variable_name
, &vendor_guid
,
472 status
= EFI_NOT_FOUND
;
474 err
= func(variable_name
, vendor_guid
,
475 variable_name_size
, data
);
477 status
= EFI_NOT_FOUND
;
481 if (down_interruptible(&efivars_lock
)) {
488 case EFI_UNSUPPORTED
:
490 status
= EFI_NOT_FOUND
;
495 printk(KERN_WARNING
"efivars: get_next_variable: status=%lx\n",
497 status
= EFI_NOT_FOUND
;
501 } while (status
!= EFI_NOT_FOUND
);
505 kfree(variable_name
);
509 EXPORT_SYMBOL_GPL(efivar_init
);
512 * efivar_entry_add - add entry to variable list
513 * @entry: entry to add to list
516 * Returns 0 on success, or a kernel error code on failure.
518 int efivar_entry_add(struct efivar_entry
*entry
, struct list_head
*head
)
520 if (down_interruptible(&efivars_lock
))
522 list_add(&entry
->list
, head
);
527 EXPORT_SYMBOL_GPL(efivar_entry_add
);
530 * efivar_entry_remove - remove entry from variable list
531 * @entry: entry to remove from list
533 * Returns 0 on success, or a kernel error code on failure.
535 int efivar_entry_remove(struct efivar_entry
*entry
)
537 if (down_interruptible(&efivars_lock
))
539 list_del(&entry
->list
);
544 EXPORT_SYMBOL_GPL(efivar_entry_remove
);
547 * efivar_entry_list_del_unlock - remove entry from variable list
548 * @entry: entry to remove
550 * Remove @entry from the variable list and release the list lock.
552 * NOTE: slightly weird locking semantics here - we expect to be
553 * called with the efivars lock already held, and we release it before
554 * returning. This is because this function is usually called after
555 * set_variable() while the lock is still held.
557 static void efivar_entry_list_del_unlock(struct efivar_entry
*entry
)
559 list_del(&entry
->list
);
564 * __efivar_entry_delete - delete an EFI variable
565 * @entry: entry containing EFI variable to delete
567 * Delete the variable from the firmware but leave @entry on the
570 * This function differs from efivar_entry_delete() because it does
571 * not remove @entry from the variable list. Also, it is safe to be
572 * called from within a efivar_entry_iter_begin() and
573 * efivar_entry_iter_end() region, unlike efivar_entry_delete().
575 * Returns 0 on success, or a converted EFI status code if
576 * set_variable() fails.
578 int __efivar_entry_delete(struct efivar_entry
*entry
)
585 status
= __efivars
->ops
->set_variable(entry
->var
.VariableName
,
586 &entry
->var
.VendorGuid
,
589 return efi_status_to_err(status
);
591 EXPORT_SYMBOL_GPL(__efivar_entry_delete
);
594 * efivar_entry_delete - delete variable and remove entry from list
595 * @entry: entry containing variable to delete
597 * Delete the variable from the firmware and remove @entry from the
598 * variable list. It is the caller's responsibility to free @entry
601 * Returns 0 on success, -EINTR if we can't grab the semaphore,
602 * converted EFI status code if set_variable() fails.
604 int efivar_entry_delete(struct efivar_entry
*entry
)
606 const struct efivar_operations
*ops
;
609 if (down_interruptible(&efivars_lock
))
616 ops
= __efivars
->ops
;
617 status
= ops
->set_variable(entry
->var
.VariableName
,
618 &entry
->var
.VendorGuid
,
620 if (!(status
== EFI_SUCCESS
|| status
== EFI_NOT_FOUND
)) {
622 return efi_status_to_err(status
);
625 efivar_entry_list_del_unlock(entry
);
628 EXPORT_SYMBOL_GPL(efivar_entry_delete
);
631 * efivar_entry_set - call set_variable()
632 * @entry: entry containing the EFI variable to write
633 * @attributes: variable attributes
634 * @size: size of @data buffer
635 * @data: buffer containing variable data
636 * @head: head of variable list
638 * Calls set_variable() for an EFI variable. If creating a new EFI
639 * variable, this function is usually followed by efivar_entry_add().
641 * Before writing the variable, the remaining EFI variable storage
642 * space is checked to ensure there is enough room available.
644 * If @head is not NULL a lookup is performed to determine whether
645 * the entry is already on the list.
647 * Returns 0 on success, -EINTR if we can't grab the semaphore,
648 * -EEXIST if a lookup is performed and the entry already exists on
649 * the list, or a converted EFI status code if set_variable() fails.
651 int efivar_entry_set(struct efivar_entry
*entry
, u32 attributes
,
652 unsigned long size
, void *data
, struct list_head
*head
)
654 const struct efivar_operations
*ops
;
656 efi_char16_t
*name
= entry
->var
.VariableName
;
657 efi_guid_t vendor
= entry
->var
.VendorGuid
;
659 if (down_interruptible(&efivars_lock
))
666 ops
= __efivars
->ops
;
667 if (head
&& efivar_entry_find(name
, vendor
, head
, false)) {
672 status
= check_var_size(attributes
, size
+ ucs2_strsize(name
, 1024));
673 if (status
== EFI_SUCCESS
|| status
== EFI_UNSUPPORTED
)
674 status
= ops
->set_variable(name
, &vendor
,
675 attributes
, size
, data
);
679 return efi_status_to_err(status
);
682 EXPORT_SYMBOL_GPL(efivar_entry_set
);
685 * efivar_entry_set_nonblocking - call set_variable_nonblocking()
687 * This function is guaranteed to not block and is suitable for calling
688 * from crash/panic handlers.
690 * Crucially, this function will not block if it cannot acquire
691 * efivars_lock. Instead, it returns -EBUSY.
694 efivar_entry_set_nonblocking(efi_char16_t
*name
, efi_guid_t vendor
,
695 u32 attributes
, unsigned long size
, void *data
)
697 const struct efivar_operations
*ops
;
700 if (down_trylock(&efivars_lock
))
708 status
= check_var_size_nonblocking(attributes
,
709 size
+ ucs2_strsize(name
, 1024));
710 if (status
!= EFI_SUCCESS
) {
715 ops
= __efivars
->ops
;
716 status
= ops
->set_variable_nonblocking(name
, &vendor
, attributes
,
720 return efi_status_to_err(status
);
724 * efivar_entry_set_safe - call set_variable() if enough space in firmware
725 * @name: buffer containing the variable name
726 * @vendor: variable vendor guid
727 * @attributes: variable attributes
728 * @block: can we block in this context?
729 * @size: size of @data buffer
730 * @data: buffer containing variable data
732 * Ensures there is enough free storage in the firmware for this variable, and
733 * if so, calls set_variable(). If creating a new EFI variable, this function
734 * is usually followed by efivar_entry_add().
736 * Returns 0 on success, -ENOSPC if the firmware does not have enough
737 * space for set_variable() to succeed, or a converted EFI status code
738 * if set_variable() fails.
740 int efivar_entry_set_safe(efi_char16_t
*name
, efi_guid_t vendor
, u32 attributes
,
741 bool block
, unsigned long size
, void *data
)
743 const struct efivar_operations
*ops
;
745 unsigned long varsize
;
750 ops
= __efivars
->ops
;
751 if (!ops
->query_variable_store
)
755 * If the EFI variable backend provides a non-blocking
756 * ->set_variable() operation and we're in a context where we
757 * cannot block, then we need to use it to avoid live-locks,
758 * since the implication is that the regular ->set_variable()
761 * If no ->set_variable_nonblocking() is provided then
762 * ->set_variable() is assumed to be non-blocking.
764 if (!block
&& ops
->set_variable_nonblocking
)
765 return efivar_entry_set_nonblocking(name
, vendor
, attributes
,
768 varsize
= size
+ ucs2_strsize(name
, 1024);
770 if (down_trylock(&efivars_lock
))
772 status
= check_var_size_nonblocking(attributes
, varsize
);
774 if (down_interruptible(&efivars_lock
))
776 status
= check_var_size(attributes
, varsize
);
779 if (status
!= EFI_SUCCESS
) {
784 status
= ops
->set_variable(name
, &vendor
, attributes
, size
, data
);
788 return efi_status_to_err(status
);
790 EXPORT_SYMBOL_GPL(efivar_entry_set_safe
);
793 * efivar_entry_find - search for an entry
794 * @name: the EFI variable name
795 * @guid: the EFI variable vendor's guid
796 * @head: head of the variable list
797 * @remove: should we remove the entry from the list?
799 * Search for an entry on the variable list that has the EFI variable
800 * name @name and vendor guid @guid. If an entry is found on the list
801 * and @remove is true, the entry is removed from the list.
803 * The caller MUST call efivar_entry_iter_begin() and
804 * efivar_entry_iter_end() before and after the invocation of this
805 * function, respectively.
807 * Returns the entry if found on the list, %NULL otherwise.
809 struct efivar_entry
*efivar_entry_find(efi_char16_t
*name
, efi_guid_t guid
,
810 struct list_head
*head
, bool remove
)
812 struct efivar_entry
*entry
, *n
;
813 int strsize1
, strsize2
;
816 list_for_each_entry_safe(entry
, n
, head
, list
) {
817 strsize1
= ucs2_strsize(name
, 1024);
818 strsize2
= ucs2_strsize(entry
->var
.VariableName
, 1024);
819 if (strsize1
== strsize2
&&
820 !memcmp(name
, &(entry
->var
.VariableName
), strsize1
) &&
821 !efi_guidcmp(guid
, entry
->var
.VendorGuid
)) {
831 if (entry
->scanning
) {
833 * The entry will be deleted
834 * after scanning is completed.
836 entry
->deleting
= true;
838 list_del(&entry
->list
);
843 EXPORT_SYMBOL_GPL(efivar_entry_find
);
846 * efivar_entry_size - obtain the size of a variable
847 * @entry: entry for this variable
848 * @size: location to store the variable's size
850 int efivar_entry_size(struct efivar_entry
*entry
, unsigned long *size
)
852 const struct efivar_operations
*ops
;
857 if (down_interruptible(&efivars_lock
))
863 ops
= __efivars
->ops
;
864 status
= ops
->get_variable(entry
->var
.VariableName
,
865 &entry
->var
.VendorGuid
, NULL
, size
, NULL
);
868 if (status
!= EFI_BUFFER_TOO_SMALL
)
869 return efi_status_to_err(status
);
873 EXPORT_SYMBOL_GPL(efivar_entry_size
);
876 * __efivar_entry_get - call get_variable()
877 * @entry: read data for this variable
878 * @attributes: variable attributes
879 * @size: size of @data buffer
880 * @data: buffer to store variable data
882 * The caller MUST call efivar_entry_iter_begin() and
883 * efivar_entry_iter_end() before and after the invocation of this
884 * function, respectively.
886 int __efivar_entry_get(struct efivar_entry
*entry
, u32
*attributes
,
887 unsigned long *size
, void *data
)
894 status
= __efivars
->ops
->get_variable(entry
->var
.VariableName
,
895 &entry
->var
.VendorGuid
,
896 attributes
, size
, data
);
898 return efi_status_to_err(status
);
900 EXPORT_SYMBOL_GPL(__efivar_entry_get
);
903 * efivar_entry_get - call get_variable()
904 * @entry: read data for this variable
905 * @attributes: variable attributes
906 * @size: size of @data buffer
907 * @data: buffer to store variable data
909 int efivar_entry_get(struct efivar_entry
*entry
, u32
*attributes
,
910 unsigned long *size
, void *data
)
914 if (down_interruptible(&efivars_lock
))
922 status
= __efivars
->ops
->get_variable(entry
->var
.VariableName
,
923 &entry
->var
.VendorGuid
,
924 attributes
, size
, data
);
927 return efi_status_to_err(status
);
929 EXPORT_SYMBOL_GPL(efivar_entry_get
);
932 * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
933 * @entry: entry containing variable to set and get
934 * @attributes: attributes of variable to be written
935 * @size: size of data buffer
936 * @data: buffer containing data to write
937 * @set: did the set_variable() call succeed?
939 * This is a pretty special (complex) function. See efivarfs_file_write().
941 * Atomically call set_variable() for @entry and if the call is
942 * successful, return the new size of the variable from get_variable()
943 * in @size. The success of set_variable() is indicated by @set.
945 * Returns 0 on success, -EINVAL if the variable data is invalid,
946 * -ENOSPC if the firmware does not have enough available space, or a
947 * converted EFI status code if either of set_variable() or
948 * get_variable() fail.
950 * If the EFI variable does not exist when calling set_variable()
951 * (EFI_NOT_FOUND), @entry is removed from the variable list.
953 int efivar_entry_set_get_size(struct efivar_entry
*entry
, u32 attributes
,
954 unsigned long *size
, void *data
, bool *set
)
956 const struct efivar_operations
*ops
;
957 efi_char16_t
*name
= entry
->var
.VariableName
;
958 efi_guid_t
*vendor
= &entry
->var
.VendorGuid
;
964 if (efivar_validate(*vendor
, name
, data
, *size
) == false)
968 * The lock here protects the get_variable call, the conditional
969 * set_variable call, and removal of the variable from the efivars
970 * list (in the case of an authenticated delete).
972 if (down_interruptible(&efivars_lock
))
981 * Ensure that the available space hasn't shrunk below the safe level
983 status
= check_var_size(attributes
, *size
+ ucs2_strsize(name
, 1024));
984 if (status
!= EFI_SUCCESS
) {
985 if (status
!= EFI_UNSUPPORTED
) {
986 err
= efi_status_to_err(status
);
996 ops
= __efivars
->ops
;
998 status
= ops
->set_variable(name
, vendor
, attributes
, *size
, data
);
999 if (status
!= EFI_SUCCESS
) {
1000 err
= efi_status_to_err(status
);
1007 * Writing to the variable may have caused a change in size (which
1008 * could either be an append or an overwrite), or the variable to be
1009 * deleted. Perform a GetVariable() so we can tell what actually
1013 status
= ops
->get_variable(entry
->var
.VariableName
,
1014 &entry
->var
.VendorGuid
,
1017 if (status
== EFI_NOT_FOUND
)
1018 efivar_entry_list_del_unlock(entry
);
1022 if (status
&& status
!= EFI_BUFFER_TOO_SMALL
)
1023 return efi_status_to_err(status
);
1032 EXPORT_SYMBOL_GPL(efivar_entry_set_get_size
);
1035 * efivar_entry_iter_begin - begin iterating the variable list
1037 * Lock the variable list to prevent entry insertion and removal until
1038 * efivar_entry_iter_end() is called. This function is usually used in
1039 * conjunction with __efivar_entry_iter() or efivar_entry_iter().
1041 int efivar_entry_iter_begin(void)
1043 return down_interruptible(&efivars_lock
);
1045 EXPORT_SYMBOL_GPL(efivar_entry_iter_begin
);
1048 * efivar_entry_iter_end - finish iterating the variable list
1050 * Unlock the variable list and allow modifications to the list again.
1052 void efivar_entry_iter_end(void)
1056 EXPORT_SYMBOL_GPL(efivar_entry_iter_end
);
1059 * __efivar_entry_iter - iterate over variable list
1060 * @func: callback function
1061 * @head: head of the variable list
1062 * @data: function-specific data to pass to callback
1063 * @prev: entry to begin iterating from
1065 * Iterate over the list of EFI variables and call @func with every
1066 * entry on the list. It is safe for @func to remove entries in the
1067 * list via efivar_entry_delete().
1069 * You MUST call efivar_entry_iter_begin() before this function, and
1070 * efivar_entry_iter_end() afterwards.
1072 * It is possible to begin iteration from an arbitrary entry within
1073 * the list by passing @prev. @prev is updated on return to point to
1074 * the last entry passed to @func. To begin iterating from the
1075 * beginning of the list @prev must be %NULL.
1077 * The restrictions for @func are the same as documented for
1078 * efivar_entry_iter().
1080 int __efivar_entry_iter(int (*func
)(struct efivar_entry
*, void *),
1081 struct list_head
*head
, void *data
,
1082 struct efivar_entry
**prev
)
1084 struct efivar_entry
*entry
, *n
;
1087 if (!prev
|| !*prev
) {
1088 list_for_each_entry_safe(entry
, n
, head
, list
) {
1089 err
= func(entry
, data
);
1101 list_for_each_entry_safe_continue((*prev
), n
, head
, list
) {
1102 err
= func(*prev
, data
);
1109 EXPORT_SYMBOL_GPL(__efivar_entry_iter
);
1112 * efivar_entry_iter - iterate over variable list
1113 * @func: callback function
1114 * @head: head of variable list
1115 * @data: function-specific data to pass to callback
1117 * Iterate over the list of EFI variables and call @func with every
1118 * entry on the list. It is safe for @func to remove entries in the
1119 * list via efivar_entry_delete() while iterating.
1121 * Some notes for the callback function:
1122 * - a non-zero return value indicates an error and terminates the loop
1123 * - @func is called from atomic context
1125 int efivar_entry_iter(int (*func
)(struct efivar_entry
*, void *),
1126 struct list_head
*head
, void *data
)
1130 err
= efivar_entry_iter_begin();
1133 err
= __efivar_entry_iter(func
, head
, data
, NULL
);
1134 efivar_entry_iter_end();
1138 EXPORT_SYMBOL_GPL(efivar_entry_iter
);
1141 * efivars_kobject - get the kobject for the registered efivars
1143 * If efivars_register() has not been called we return NULL,
1144 * otherwise return the kobject used at registration time.
1146 struct kobject
*efivars_kobject(void)
1151 return __efivars
->kobject
;
1153 EXPORT_SYMBOL_GPL(efivars_kobject
);
1156 * efivars_register - register an efivars
1157 * @efivars: efivars to register
1158 * @ops: efivars operations
1159 * @kobject: @efivars-specific kobject
1161 * Only a single efivars can be registered at any time.
1163 int efivars_register(struct efivars
*efivars
,
1164 const struct efivar_operations
*ops
,
1165 struct kobject
*kobject
)
1167 if (down_interruptible(&efivars_lock
))
1171 efivars
->kobject
= kobject
;
1173 __efivars
= efivars
;
1175 pr_info("Registered efivars operations\n");
1181 EXPORT_SYMBOL_GPL(efivars_register
);
1184 * efivars_unregister - unregister an efivars
1185 * @efivars: efivars to unregister
1187 * The caller must have already removed every entry from the list,
1188 * failure to do so is an error.
1190 int efivars_unregister(struct efivars
*efivars
)
1194 if (down_interruptible(&efivars_lock
))
1198 printk(KERN_ERR
"efivars not registered\n");
1203 if (__efivars
!= efivars
) {
1208 pr_info("Unregistered efivars operations\n");
1216 EXPORT_SYMBOL_GPL(efivars_unregister
);
1218 int efivar_supports_writes(void)
1220 return __efivars
&& __efivars
->ops
->set_variable
;
1222 EXPORT_SYMBOL_GPL(efivar_supports_writes
);