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
);
35 static bool efivar_wq_enabled
= true;
36 DECLARE_WORK(efivar_work
, NULL
);
37 EXPORT_SYMBOL_GPL(efivar_work
);
40 validate_device_path(efi_char16_t
*var_name
, int match
, u8
*buffer
,
43 struct efi_generic_dev_path
*node
;
46 node
= (struct efi_generic_dev_path
*)buffer
;
48 if (len
< sizeof(*node
))
51 while (offset
<= len
- sizeof(*node
) &&
52 node
->length
>= sizeof(*node
) &&
53 node
->length
<= len
- offset
) {
54 offset
+= node
->length
;
56 if ((node
->type
== EFI_DEV_END_PATH
||
57 node
->type
== EFI_DEV_END_PATH2
) &&
58 node
->sub_type
== EFI_DEV_END_ENTIRE
)
61 node
= (struct efi_generic_dev_path
*)(buffer
+ offset
);
65 * If we're here then either node->length pointed past the end
66 * of the buffer or we reached the end of the buffer without
67 * finding a device path end node.
73 validate_boot_order(efi_char16_t
*var_name
, int match
, u8
*buffer
,
76 /* An array of 16-bit integers */
84 validate_load_option(efi_char16_t
*var_name
, int match
, u8
*buffer
,
88 int i
, desclength
= 0, namelen
;
90 namelen
= ucs2_strnlen(var_name
, EFI_VAR_NAME_LEN
);
92 /* Either "Boot" or "Driver" followed by four digits of hex */
93 for (i
= match
; i
< match
+4; i
++) {
94 if (var_name
[i
] > 127 ||
95 hex_to_bin(var_name
[i
] & 0xff) < 0)
99 /* Reject it if there's 4 digits of hex and then further content */
100 if (namelen
> match
+ 4)
103 /* A valid entry must be at least 8 bytes */
107 filepathlength
= buffer
[4] | buffer
[5] << 8;
110 * There's no stored length for the description, so it has to be
113 desclength
= ucs2_strsize((efi_char16_t
*)(buffer
+ 6), len
- 6) + 2;
115 /* Each boot entry must have a descriptor */
120 * If the sum of the length of the description, the claimed filepath
121 * length and the original header are greater than the length of the
122 * variable, it's malformed
124 if ((desclength
+ filepathlength
+ 6) > len
)
128 * And, finally, check the filepath
130 return validate_device_path(var_name
, match
, buffer
+ desclength
+ 6,
135 validate_uint16(efi_char16_t
*var_name
, int match
, u8
*buffer
,
138 /* A single 16-bit integer */
146 validate_ascii_string(efi_char16_t
*var_name
, int match
, u8
*buffer
,
151 for (i
= 0; i
< len
; i
++) {
162 struct variable_validate
{
165 bool (*validate
)(efi_char16_t
*var_name
, int match
, u8
*data
,
170 * This is the list of variables we need to validate, as well as the
171 * whitelist for what we think is safe not to default to immutable.
173 * If it has a validate() method that's not NULL, it'll go into the
174 * validation routine. If not, it is assumed valid, but still used for
177 * Note that it's sorted by {vendor,name}, but globbed names must come after
178 * any other name with the same prefix.
180 static const struct variable_validate variable_validate
[] = {
181 { EFI_GLOBAL_VARIABLE_GUID
, "BootNext", validate_uint16
},
182 { EFI_GLOBAL_VARIABLE_GUID
, "BootOrder", validate_boot_order
},
183 { EFI_GLOBAL_VARIABLE_GUID
, "Boot*", validate_load_option
},
184 { EFI_GLOBAL_VARIABLE_GUID
, "DriverOrder", validate_boot_order
},
185 { EFI_GLOBAL_VARIABLE_GUID
, "Driver*", validate_load_option
},
186 { EFI_GLOBAL_VARIABLE_GUID
, "ConIn", validate_device_path
},
187 { EFI_GLOBAL_VARIABLE_GUID
, "ConInDev", validate_device_path
},
188 { EFI_GLOBAL_VARIABLE_GUID
, "ConOut", validate_device_path
},
189 { EFI_GLOBAL_VARIABLE_GUID
, "ConOutDev", validate_device_path
},
190 { EFI_GLOBAL_VARIABLE_GUID
, "ErrOut", validate_device_path
},
191 { EFI_GLOBAL_VARIABLE_GUID
, "ErrOutDev", validate_device_path
},
192 { EFI_GLOBAL_VARIABLE_GUID
, "Lang", validate_ascii_string
},
193 { EFI_GLOBAL_VARIABLE_GUID
, "OsIndications", NULL
},
194 { EFI_GLOBAL_VARIABLE_GUID
, "PlatformLang", validate_ascii_string
},
195 { EFI_GLOBAL_VARIABLE_GUID
, "Timeout", validate_uint16
},
196 { LINUX_EFI_CRASH_GUID
, "*", NULL
},
197 { NULL_GUID
, "", NULL
},
201 * Check if @var_name matches the pattern given in @match_name.
203 * @var_name: an array of @len non-NUL characters.
204 * @match_name: a NUL-terminated pattern string, optionally ending in "*". A
205 * final "*" character matches any trailing characters @var_name,
206 * including the case when there are none left in @var_name.
207 * @match: on output, the number of non-wildcard characters in @match_name
208 * that @var_name matches, regardless of the return value.
209 * @return: whether @var_name fully matches @match_name.
212 variable_matches(const char *var_name
, size_t len
, const char *match_name
,
215 for (*match
= 0; ; (*match
)++) {
216 char c
= match_name
[*match
];
220 /* Wildcard in @match_name means we've matched. */
224 /* @match_name has ended. Has @var_name too? */
225 return (*match
== len
);
229 * We've reached a non-wildcard char in @match_name.
230 * Continue only if there's an identical character in
233 if (*match
< len
&& c
== var_name
[*match
])
241 efivar_validate(efi_guid_t vendor
, efi_char16_t
*var_name
, u8
*data
,
242 unsigned long data_size
)
245 unsigned long utf8_size
;
248 utf8_size
= ucs2_utf8size(var_name
);
249 utf8_name
= kmalloc(utf8_size
+ 1, GFP_KERNEL
);
253 ucs2_as_utf8(utf8_name
, var_name
, utf8_size
);
254 utf8_name
[utf8_size
] = '\0';
256 for (i
= 0; variable_validate
[i
].name
[0] != '\0'; i
++) {
257 const char *name
= variable_validate
[i
].name
;
260 if (efi_guidcmp(vendor
, variable_validate
[i
].vendor
))
263 if (variable_matches(utf8_name
, utf8_size
+1, name
, &match
)) {
264 if (variable_validate
[i
].validate
== NULL
)
267 return variable_validate
[i
].validate(var_name
, match
,
274 EXPORT_SYMBOL_GPL(efivar_validate
);
277 efivar_variable_is_removable(efi_guid_t vendor
, const char *var_name
,
285 * Check if our variable is in the validated variables list
287 for (i
= 0; variable_validate
[i
].name
[0] != '\0'; i
++) {
288 if (efi_guidcmp(variable_validate
[i
].vendor
, vendor
))
291 if (variable_matches(var_name
, len
,
292 variable_validate
[i
].name
, &match
)) {
299 * If it's in our list, it is removable.
303 EXPORT_SYMBOL_GPL(efivar_variable_is_removable
);
306 check_var_size(u32 attributes
, unsigned long size
)
308 const struct efivar_operations
*fops
;
311 return EFI_UNSUPPORTED
;
313 fops
= __efivars
->ops
;
315 if (!fops
->query_variable_store
)
316 return EFI_UNSUPPORTED
;
318 return fops
->query_variable_store(attributes
, size
, false);
322 check_var_size_nonblocking(u32 attributes
, unsigned long size
)
324 const struct efivar_operations
*fops
;
327 return EFI_UNSUPPORTED
;
329 fops
= __efivars
->ops
;
331 if (!fops
->query_variable_store
)
332 return EFI_UNSUPPORTED
;
334 return fops
->query_variable_store(attributes
, size
, true);
337 static bool variable_is_present(efi_char16_t
*variable_name
, efi_guid_t
*vendor
,
338 struct list_head
*head
)
340 struct efivar_entry
*entry
, *n
;
341 unsigned long strsize1
, strsize2
;
344 strsize1
= ucs2_strsize(variable_name
, 1024);
345 list_for_each_entry_safe(entry
, n
, head
, list
) {
346 strsize2
= ucs2_strsize(entry
->var
.VariableName
, 1024);
347 if (strsize1
== strsize2
&&
348 !memcmp(variable_name
, &(entry
->var
.VariableName
),
350 !efi_guidcmp(entry
->var
.VendorGuid
,
360 * Returns the size of variable_name, in bytes, including the
361 * terminating NULL character, or variable_name_size if no NULL
362 * character is found among the first variable_name_size bytes.
364 static unsigned long var_name_strnsize(efi_char16_t
*variable_name
,
365 unsigned long variable_name_size
)
371 * The variable name is, by definition, a NULL-terminated
372 * string, so make absolutely sure that variable_name_size is
373 * the value we expect it to be. If not, return the real size.
375 for (len
= 2; len
<= variable_name_size
; len
+= sizeof(c
)) {
376 c
= variable_name
[(len
/ sizeof(c
)) - 1];
381 return min(len
, variable_name_size
);
385 * Print a warning when duplicate EFI variables are encountered and
386 * disable the sysfs workqueue since the firmware is buggy.
388 static void dup_variable_bug(efi_char16_t
*str16
, efi_guid_t
*vendor_guid
,
391 size_t i
, len8
= len16
/ sizeof(efi_char16_t
);
395 * Disable the workqueue since the algorithm it uses for
396 * detecting new variables won't work with this buggy
397 * implementation of GetNextVariableName().
399 efivar_wq_enabled
= false;
401 str8
= kzalloc(len8
, GFP_KERNEL
);
405 for (i
= 0; i
< len8
; i
++)
408 printk(KERN_WARNING
"efivars: duplicate variable: %s-%pUl\n",
414 * efivar_init - build the initial list of EFI variables
415 * @func: callback function to invoke for every variable
416 * @data: function-specific data to pass to @func
417 * @atomic: do we need to execute the @func-loop atomically?
418 * @duplicates: error if we encounter duplicates on @head?
419 * @head: initialised head of variable list
421 * Get every EFI variable from the firmware and invoke @func. @func
422 * should call efivar_entry_add() to build the list of variables.
424 * Returns 0 on success, or a kernel error code on failure.
426 int efivar_init(int (*func
)(efi_char16_t
*, efi_guid_t
, unsigned long, void *),
427 void *data
, bool duplicates
, struct list_head
*head
)
429 const struct efivar_operations
*ops
;
430 unsigned long variable_name_size
= 1024;
431 efi_char16_t
*variable_name
;
433 efi_guid_t vendor_guid
;
439 ops
= __efivars
->ops
;
441 variable_name
= kzalloc(variable_name_size
, GFP_KERNEL
);
442 if (!variable_name
) {
443 printk(KERN_ERR
"efivars: Memory allocation failed.\n");
447 if (down_interruptible(&efivars_lock
)) {
453 * Per EFI spec, the maximum storage allocated for both
454 * the variable name and variable data is 1024 bytes.
458 variable_name_size
= 1024;
460 status
= ops
->get_next_variable(&variable_name_size
,
468 variable_name_size
= var_name_strnsize(variable_name
,
472 * Some firmware implementations return the
473 * same variable name on multiple calls to
474 * get_next_variable(). Terminate the loop
475 * immediately as there is no guarantee that
476 * we'll ever see a different variable name,
477 * and may end up looping here forever.
480 variable_is_present(variable_name
, &vendor_guid
,
482 dup_variable_bug(variable_name
, &vendor_guid
,
484 status
= EFI_NOT_FOUND
;
486 err
= func(variable_name
, vendor_guid
,
487 variable_name_size
, data
);
489 status
= EFI_NOT_FOUND
;
493 if (down_interruptible(&efivars_lock
)) {
503 printk(KERN_WARNING
"efivars: get_next_variable: status=%lx\n",
505 status
= EFI_NOT_FOUND
;
509 } while (status
!= EFI_NOT_FOUND
);
513 kfree(variable_name
);
517 EXPORT_SYMBOL_GPL(efivar_init
);
520 * efivar_entry_add - add entry to variable list
521 * @entry: entry to add to list
524 * Returns 0 on success, or a kernel error code on failure.
526 int efivar_entry_add(struct efivar_entry
*entry
, struct list_head
*head
)
528 if (down_interruptible(&efivars_lock
))
530 list_add(&entry
->list
, head
);
535 EXPORT_SYMBOL_GPL(efivar_entry_add
);
538 * efivar_entry_remove - remove entry from variable list
539 * @entry: entry to remove from list
541 * Returns 0 on success, or a kernel error code on failure.
543 int efivar_entry_remove(struct efivar_entry
*entry
)
545 if (down_interruptible(&efivars_lock
))
547 list_del(&entry
->list
);
552 EXPORT_SYMBOL_GPL(efivar_entry_remove
);
555 * efivar_entry_list_del_unlock - remove entry from variable list
556 * @entry: entry to remove
558 * Remove @entry from the variable list and release the list lock.
560 * NOTE: slightly weird locking semantics here - we expect to be
561 * called with the efivars lock already held, and we release it before
562 * returning. This is because this function is usually called after
563 * set_variable() while the lock is still held.
565 static void efivar_entry_list_del_unlock(struct efivar_entry
*entry
)
567 list_del(&entry
->list
);
572 * __efivar_entry_delete - delete an EFI variable
573 * @entry: entry containing EFI variable to delete
575 * Delete the variable from the firmware but leave @entry on the
578 * This function differs from efivar_entry_delete() because it does
579 * not remove @entry from the variable list. Also, it is safe to be
580 * called from within a efivar_entry_iter_begin() and
581 * efivar_entry_iter_end() region, unlike efivar_entry_delete().
583 * Returns 0 on success, or a converted EFI status code if
584 * set_variable() fails.
586 int __efivar_entry_delete(struct efivar_entry
*entry
)
593 status
= __efivars
->ops
->set_variable(entry
->var
.VariableName
,
594 &entry
->var
.VendorGuid
,
597 return efi_status_to_err(status
);
599 EXPORT_SYMBOL_GPL(__efivar_entry_delete
);
602 * efivar_entry_delete - delete variable and remove entry from list
603 * @entry: entry containing variable to delete
605 * Delete the variable from the firmware and remove @entry from the
606 * variable list. It is the caller's responsibility to free @entry
609 * Returns 0 on success, -EINTR if we can't grab the semaphore,
610 * converted EFI status code if set_variable() fails.
612 int efivar_entry_delete(struct efivar_entry
*entry
)
614 const struct efivar_operations
*ops
;
617 if (down_interruptible(&efivars_lock
))
624 ops
= __efivars
->ops
;
625 status
= ops
->set_variable(entry
->var
.VariableName
,
626 &entry
->var
.VendorGuid
,
628 if (!(status
== EFI_SUCCESS
|| status
== EFI_NOT_FOUND
)) {
630 return efi_status_to_err(status
);
633 efivar_entry_list_del_unlock(entry
);
636 EXPORT_SYMBOL_GPL(efivar_entry_delete
);
639 * efivar_entry_set - call set_variable()
640 * @entry: entry containing the EFI variable to write
641 * @attributes: variable attributes
642 * @size: size of @data buffer
643 * @data: buffer containing variable data
644 * @head: head of variable list
646 * Calls set_variable() for an EFI variable. If creating a new EFI
647 * variable, this function is usually followed by efivar_entry_add().
649 * Before writing the variable, the remaining EFI variable storage
650 * space is checked to ensure there is enough room available.
652 * If @head is not NULL a lookup is performed to determine whether
653 * the entry is already on the list.
655 * Returns 0 on success, -EINTR if we can't grab the semaphore,
656 * -EEXIST if a lookup is performed and the entry already exists on
657 * the list, or a converted EFI status code if set_variable() fails.
659 int efivar_entry_set(struct efivar_entry
*entry
, u32 attributes
,
660 unsigned long size
, void *data
, struct list_head
*head
)
662 const struct efivar_operations
*ops
;
664 efi_char16_t
*name
= entry
->var
.VariableName
;
665 efi_guid_t vendor
= entry
->var
.VendorGuid
;
667 if (down_interruptible(&efivars_lock
))
674 ops
= __efivars
->ops
;
675 if (head
&& efivar_entry_find(name
, vendor
, head
, false)) {
680 status
= check_var_size(attributes
, size
+ ucs2_strsize(name
, 1024));
681 if (status
== EFI_SUCCESS
|| status
== EFI_UNSUPPORTED
)
682 status
= ops
->set_variable(name
, &vendor
,
683 attributes
, size
, data
);
687 return efi_status_to_err(status
);
690 EXPORT_SYMBOL_GPL(efivar_entry_set
);
693 * efivar_entry_set_nonblocking - call set_variable_nonblocking()
695 * This function is guaranteed to not block and is suitable for calling
696 * from crash/panic handlers.
698 * Crucially, this function will not block if it cannot acquire
699 * efivars_lock. Instead, it returns -EBUSY.
702 efivar_entry_set_nonblocking(efi_char16_t
*name
, efi_guid_t vendor
,
703 u32 attributes
, unsigned long size
, void *data
)
705 const struct efivar_operations
*ops
;
708 if (down_trylock(&efivars_lock
))
716 status
= check_var_size_nonblocking(attributes
,
717 size
+ ucs2_strsize(name
, 1024));
718 if (status
!= EFI_SUCCESS
) {
723 ops
= __efivars
->ops
;
724 status
= ops
->set_variable_nonblocking(name
, &vendor
, attributes
,
728 return efi_status_to_err(status
);
732 * efivar_entry_set_safe - call set_variable() if enough space in firmware
733 * @name: buffer containing the variable name
734 * @vendor: variable vendor guid
735 * @attributes: variable attributes
736 * @block: can we block in this context?
737 * @size: size of @data buffer
738 * @data: buffer containing variable data
740 * Ensures there is enough free storage in the firmware for this variable, and
741 * if so, calls set_variable(). If creating a new EFI variable, this function
742 * is usually followed by efivar_entry_add().
744 * Returns 0 on success, -ENOSPC if the firmware does not have enough
745 * space for set_variable() to succeed, or a converted EFI status code
746 * if set_variable() fails.
748 int efivar_entry_set_safe(efi_char16_t
*name
, efi_guid_t vendor
, u32 attributes
,
749 bool block
, unsigned long size
, void *data
)
751 const struct efivar_operations
*ops
;
757 ops
= __efivars
->ops
;
758 if (!ops
->query_variable_store
)
762 * If the EFI variable backend provides a non-blocking
763 * ->set_variable() operation and we're in a context where we
764 * cannot block, then we need to use it to avoid live-locks,
765 * since the implication is that the regular ->set_variable()
768 * If no ->set_variable_nonblocking() is provided then
769 * ->set_variable() is assumed to be non-blocking.
771 if (!block
&& ops
->set_variable_nonblocking
)
772 return efivar_entry_set_nonblocking(name
, vendor
, attributes
,
776 if (down_trylock(&efivars_lock
))
779 if (down_interruptible(&efivars_lock
))
783 status
= check_var_size(attributes
, size
+ ucs2_strsize(name
, 1024));
784 if (status
!= EFI_SUCCESS
) {
789 status
= ops
->set_variable(name
, &vendor
, attributes
, size
, data
);
793 return efi_status_to_err(status
);
795 EXPORT_SYMBOL_GPL(efivar_entry_set_safe
);
798 * efivar_entry_find - search for an entry
799 * @name: the EFI variable name
800 * @guid: the EFI variable vendor's guid
801 * @head: head of the variable list
802 * @remove: should we remove the entry from the list?
804 * Search for an entry on the variable list that has the EFI variable
805 * name @name and vendor guid @guid. If an entry is found on the list
806 * and @remove is true, the entry is removed from the list.
808 * The caller MUST call efivar_entry_iter_begin() and
809 * efivar_entry_iter_end() before and after the invocation of this
810 * function, respectively.
812 * Returns the entry if found on the list, %NULL otherwise.
814 struct efivar_entry
*efivar_entry_find(efi_char16_t
*name
, efi_guid_t guid
,
815 struct list_head
*head
, bool remove
)
817 struct efivar_entry
*entry
, *n
;
818 int strsize1
, strsize2
;
821 list_for_each_entry_safe(entry
, n
, head
, list
) {
822 strsize1
= ucs2_strsize(name
, 1024);
823 strsize2
= ucs2_strsize(entry
->var
.VariableName
, 1024);
824 if (strsize1
== strsize2
&&
825 !memcmp(name
, &(entry
->var
.VariableName
), strsize1
) &&
826 !efi_guidcmp(guid
, entry
->var
.VendorGuid
)) {
836 if (entry
->scanning
) {
838 * The entry will be deleted
839 * after scanning is completed.
841 entry
->deleting
= true;
843 list_del(&entry
->list
);
848 EXPORT_SYMBOL_GPL(efivar_entry_find
);
851 * efivar_entry_size - obtain the size of a variable
852 * @entry: entry for this variable
853 * @size: location to store the variable's size
855 int efivar_entry_size(struct efivar_entry
*entry
, unsigned long *size
)
857 const struct efivar_operations
*ops
;
862 if (down_interruptible(&efivars_lock
))
868 ops
= __efivars
->ops
;
869 status
= ops
->get_variable(entry
->var
.VariableName
,
870 &entry
->var
.VendorGuid
, NULL
, size
, NULL
);
873 if (status
!= EFI_BUFFER_TOO_SMALL
)
874 return efi_status_to_err(status
);
878 EXPORT_SYMBOL_GPL(efivar_entry_size
);
881 * __efivar_entry_get - call get_variable()
882 * @entry: read data for this variable
883 * @attributes: variable attributes
884 * @size: size of @data buffer
885 * @data: buffer to store variable data
887 * The caller MUST call efivar_entry_iter_begin() and
888 * efivar_entry_iter_end() before and after the invocation of this
889 * function, respectively.
891 int __efivar_entry_get(struct efivar_entry
*entry
, u32
*attributes
,
892 unsigned long *size
, void *data
)
899 status
= __efivars
->ops
->get_variable(entry
->var
.VariableName
,
900 &entry
->var
.VendorGuid
,
901 attributes
, size
, data
);
903 return efi_status_to_err(status
);
905 EXPORT_SYMBOL_GPL(__efivar_entry_get
);
908 * efivar_entry_get - call get_variable()
909 * @entry: read data for this variable
910 * @attributes: variable attributes
911 * @size: size of @data buffer
912 * @data: buffer to store variable data
914 int efivar_entry_get(struct efivar_entry
*entry
, u32
*attributes
,
915 unsigned long *size
, void *data
)
919 if (down_interruptible(&efivars_lock
))
927 status
= __efivars
->ops
->get_variable(entry
->var
.VariableName
,
928 &entry
->var
.VendorGuid
,
929 attributes
, size
, data
);
932 return efi_status_to_err(status
);
934 EXPORT_SYMBOL_GPL(efivar_entry_get
);
937 * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
938 * @entry: entry containing variable to set and get
939 * @attributes: attributes of variable to be written
940 * @size: size of data buffer
941 * @data: buffer containing data to write
942 * @set: did the set_variable() call succeed?
944 * This is a pretty special (complex) function. See efivarfs_file_write().
946 * Atomically call set_variable() for @entry and if the call is
947 * successful, return the new size of the variable from get_variable()
948 * in @size. The success of set_variable() is indicated by @set.
950 * Returns 0 on success, -EINVAL if the variable data is invalid,
951 * -ENOSPC if the firmware does not have enough available space, or a
952 * converted EFI status code if either of set_variable() or
953 * get_variable() fail.
955 * If the EFI variable does not exist when calling set_variable()
956 * (EFI_NOT_FOUND), @entry is removed from the variable list.
958 int efivar_entry_set_get_size(struct efivar_entry
*entry
, u32 attributes
,
959 unsigned long *size
, void *data
, bool *set
)
961 const struct efivar_operations
*ops
;
962 efi_char16_t
*name
= entry
->var
.VariableName
;
963 efi_guid_t
*vendor
= &entry
->var
.VendorGuid
;
969 if (efivar_validate(*vendor
, name
, data
, *size
) == false)
973 * The lock here protects the get_variable call, the conditional
974 * set_variable call, and removal of the variable from the efivars
975 * list (in the case of an authenticated delete).
977 if (down_interruptible(&efivars_lock
))
986 * Ensure that the available space hasn't shrunk below the safe level
988 status
= check_var_size(attributes
, *size
+ ucs2_strsize(name
, 1024));
989 if (status
!= EFI_SUCCESS
) {
990 if (status
!= EFI_UNSUPPORTED
) {
991 err
= efi_status_to_err(status
);
1001 ops
= __efivars
->ops
;
1003 status
= ops
->set_variable(name
, vendor
, attributes
, *size
, data
);
1004 if (status
!= EFI_SUCCESS
) {
1005 err
= efi_status_to_err(status
);
1012 * Writing to the variable may have caused a change in size (which
1013 * could either be an append or an overwrite), or the variable to be
1014 * deleted. Perform a GetVariable() so we can tell what actually
1018 status
= ops
->get_variable(entry
->var
.VariableName
,
1019 &entry
->var
.VendorGuid
,
1022 if (status
== EFI_NOT_FOUND
)
1023 efivar_entry_list_del_unlock(entry
);
1027 if (status
&& status
!= EFI_BUFFER_TOO_SMALL
)
1028 return efi_status_to_err(status
);
1037 EXPORT_SYMBOL_GPL(efivar_entry_set_get_size
);
1040 * efivar_entry_iter_begin - begin iterating the variable list
1042 * Lock the variable list to prevent entry insertion and removal until
1043 * efivar_entry_iter_end() is called. This function is usually used in
1044 * conjunction with __efivar_entry_iter() or efivar_entry_iter().
1046 int efivar_entry_iter_begin(void)
1048 return down_interruptible(&efivars_lock
);
1050 EXPORT_SYMBOL_GPL(efivar_entry_iter_begin
);
1053 * efivar_entry_iter_end - finish iterating the variable list
1055 * Unlock the variable list and allow modifications to the list again.
1057 void efivar_entry_iter_end(void)
1061 EXPORT_SYMBOL_GPL(efivar_entry_iter_end
);
1064 * __efivar_entry_iter - iterate over variable list
1065 * @func: callback function
1066 * @head: head of the variable list
1067 * @data: function-specific data to pass to callback
1068 * @prev: entry to begin iterating from
1070 * Iterate over the list of EFI variables and call @func with every
1071 * entry on the list. It is safe for @func to remove entries in the
1072 * list via efivar_entry_delete().
1074 * You MUST call efivar_enter_iter_begin() before this function, and
1075 * efivar_entry_iter_end() afterwards.
1077 * It is possible to begin iteration from an arbitrary entry within
1078 * the list by passing @prev. @prev is updated on return to point to
1079 * the last entry passed to @func. To begin iterating from the
1080 * beginning of the list @prev must be %NULL.
1082 * The restrictions for @func are the same as documented for
1083 * efivar_entry_iter().
1085 int __efivar_entry_iter(int (*func
)(struct efivar_entry
*, void *),
1086 struct list_head
*head
, void *data
,
1087 struct efivar_entry
**prev
)
1089 struct efivar_entry
*entry
, *n
;
1092 if (!prev
|| !*prev
) {
1093 list_for_each_entry_safe(entry
, n
, head
, list
) {
1094 err
= func(entry
, data
);
1106 list_for_each_entry_safe_continue((*prev
), n
, head
, list
) {
1107 err
= func(*prev
, data
);
1114 EXPORT_SYMBOL_GPL(__efivar_entry_iter
);
1117 * efivar_entry_iter - iterate over variable list
1118 * @func: callback function
1119 * @head: head of variable list
1120 * @data: function-specific data to pass to callback
1122 * Iterate over the list of EFI variables and call @func with every
1123 * entry on the list. It is safe for @func to remove entries in the
1124 * list via efivar_entry_delete() while iterating.
1126 * Some notes for the callback function:
1127 * - a non-zero return value indicates an error and terminates the loop
1128 * - @func is called from atomic context
1130 int efivar_entry_iter(int (*func
)(struct efivar_entry
*, void *),
1131 struct list_head
*head
, void *data
)
1135 err
= efivar_entry_iter_begin();
1138 err
= __efivar_entry_iter(func
, head
, data
, NULL
);
1139 efivar_entry_iter_end();
1143 EXPORT_SYMBOL_GPL(efivar_entry_iter
);
1146 * efivars_kobject - get the kobject for the registered efivars
1148 * If efivars_register() has not been called we return NULL,
1149 * otherwise return the kobject used at registration time.
1151 struct kobject
*efivars_kobject(void)
1156 return __efivars
->kobject
;
1158 EXPORT_SYMBOL_GPL(efivars_kobject
);
1161 * efivar_run_worker - schedule the efivar worker thread
1163 void efivar_run_worker(void)
1165 if (efivar_wq_enabled
)
1166 schedule_work(&efivar_work
);
1168 EXPORT_SYMBOL_GPL(efivar_run_worker
);
1171 * efivars_register - register an efivars
1172 * @efivars: efivars to register
1173 * @ops: efivars operations
1174 * @kobject: @efivars-specific kobject
1176 * Only a single efivars can be registered at any time.
1178 int efivars_register(struct efivars
*efivars
,
1179 const struct efivar_operations
*ops
,
1180 struct kobject
*kobject
)
1182 if (down_interruptible(&efivars_lock
))
1186 efivars
->kobject
= kobject
;
1188 __efivars
= efivars
;
1190 pr_info("Registered efivars operations\n");
1196 EXPORT_SYMBOL_GPL(efivars_register
);
1199 * efivars_unregister - unregister an efivars
1200 * @efivars: efivars to unregister
1202 * The caller must have already removed every entry from the list,
1203 * failure to do so is an error.
1205 int efivars_unregister(struct efivars
*efivars
)
1209 if (down_interruptible(&efivars_lock
))
1213 printk(KERN_ERR
"efivars not registered\n");
1218 if (__efivars
!= efivars
) {
1223 pr_info("Unregistered efivars operations\n");
1231 EXPORT_SYMBOL_GPL(efivars_unregister
);