2 * Originally from efivars.c
4 * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
5 * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/capability.h>
23 #include <linux/types.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/string.h>
29 #include <linux/smp.h>
30 #include <linux/efi.h>
31 #include <linux/sysfs.h>
32 #include <linux/device.h>
33 #include <linux/slab.h>
34 #include <linux/ctype.h>
35 #include <linux/ucs2_string.h>
37 /* Private pointer to registered efivars */
38 static struct efivars
*__efivars
;
40 static bool efivar_wq_enabled
= true;
41 DECLARE_WORK(efivar_work
, NULL
);
42 EXPORT_SYMBOL_GPL(efivar_work
);
45 validate_device_path(efi_char16_t
*var_name
, int match
, u8
*buffer
,
48 struct efi_generic_dev_path
*node
;
51 node
= (struct efi_generic_dev_path
*)buffer
;
53 if (len
< sizeof(*node
))
56 while (offset
<= len
- sizeof(*node
) &&
57 node
->length
>= sizeof(*node
) &&
58 node
->length
<= len
- offset
) {
59 offset
+= node
->length
;
61 if ((node
->type
== EFI_DEV_END_PATH
||
62 node
->type
== EFI_DEV_END_PATH2
) &&
63 node
->sub_type
== EFI_DEV_END_ENTIRE
)
66 node
= (struct efi_generic_dev_path
*)(buffer
+ offset
);
70 * If we're here then either node->length pointed past the end
71 * of the buffer or we reached the end of the buffer without
72 * finding a device path end node.
78 validate_boot_order(efi_char16_t
*var_name
, int match
, u8
*buffer
,
81 /* An array of 16-bit integers */
89 validate_load_option(efi_char16_t
*var_name
, int match
, u8
*buffer
,
93 int i
, desclength
= 0, namelen
;
95 namelen
= ucs2_strnlen(var_name
, EFI_VAR_NAME_LEN
);
97 /* Either "Boot" or "Driver" followed by four digits of hex */
98 for (i
= match
; i
< match
+4; i
++) {
99 if (var_name
[i
] > 127 ||
100 hex_to_bin(var_name
[i
] & 0xff) < 0)
104 /* Reject it if there's 4 digits of hex and then further content */
105 if (namelen
> match
+ 4)
108 /* A valid entry must be at least 8 bytes */
112 filepathlength
= buffer
[4] | buffer
[5] << 8;
115 * There's no stored length for the description, so it has to be
118 desclength
= ucs2_strsize((efi_char16_t
*)(buffer
+ 6), len
- 6) + 2;
120 /* Each boot entry must have a descriptor */
125 * If the sum of the length of the description, the claimed filepath
126 * length and the original header are greater than the length of the
127 * variable, it's malformed
129 if ((desclength
+ filepathlength
+ 6) > len
)
133 * And, finally, check the filepath
135 return validate_device_path(var_name
, match
, buffer
+ desclength
+ 6,
140 validate_uint16(efi_char16_t
*var_name
, int match
, u8
*buffer
,
143 /* A single 16-bit integer */
151 validate_ascii_string(efi_char16_t
*var_name
, int match
, u8
*buffer
,
156 for (i
= 0; i
< len
; i
++) {
167 struct variable_validate
{
169 bool (*validate
)(efi_char16_t
*var_name
, int match
, u8
*data
,
173 static const struct variable_validate variable_validate
[] = {
174 { "BootNext", validate_uint16
},
175 { "BootOrder", validate_boot_order
},
176 { "DriverOrder", validate_boot_order
},
177 { "Boot*", validate_load_option
},
178 { "Driver*", validate_load_option
},
179 { "ConIn", validate_device_path
},
180 { "ConInDev", validate_device_path
},
181 { "ConOut", validate_device_path
},
182 { "ConOutDev", validate_device_path
},
183 { "ErrOut", validate_device_path
},
184 { "ErrOutDev", validate_device_path
},
185 { "Timeout", validate_uint16
},
186 { "Lang", validate_ascii_string
},
187 { "PlatformLang", validate_ascii_string
},
192 efivar_validate(efi_char16_t
*var_name
, u8
*data
, unsigned long len
)
195 u16
*unicode_name
= var_name
;
197 for (i
= 0; variable_validate
[i
].validate
!= NULL
; i
++) {
198 const char *name
= variable_validate
[i
].name
;
201 for (match
= 0; ; match
++) {
202 char c
= name
[match
];
203 u16 u
= unicode_name
[match
];
205 /* All special variables are plain ascii */
209 /* Wildcard in the matching name means we've matched */
211 return variable_validate
[i
].validate(var_name
,
214 /* Case sensitive match */
218 /* Reached the end of the string while matching */
220 return variable_validate
[i
].validate(var_name
,
227 EXPORT_SYMBOL_GPL(efivar_validate
);
230 check_var_size(u32 attributes
, unsigned long size
)
232 const struct efivar_operations
*fops
= __efivars
->ops
;
234 if (!fops
->query_variable_store
)
235 return EFI_UNSUPPORTED
;
237 return fops
->query_variable_store(attributes
, size
);
240 static int efi_status_to_err(efi_status_t status
)
248 case EFI_INVALID_PARAMETER
:
251 case EFI_OUT_OF_RESOURCES
:
254 case EFI_DEVICE_ERROR
:
257 case EFI_WRITE_PROTECTED
:
260 case EFI_SECURITY_VIOLATION
:
273 static bool variable_is_present(efi_char16_t
*variable_name
, efi_guid_t
*vendor
,
274 struct list_head
*head
)
276 struct efivar_entry
*entry
, *n
;
277 unsigned long strsize1
, strsize2
;
280 strsize1
= ucs2_strsize(variable_name
, 1024);
281 list_for_each_entry_safe(entry
, n
, head
, list
) {
282 strsize2
= ucs2_strsize(entry
->var
.VariableName
, 1024);
283 if (strsize1
== strsize2
&&
284 !memcmp(variable_name
, &(entry
->var
.VariableName
),
286 !efi_guidcmp(entry
->var
.VendorGuid
,
296 * Returns the size of variable_name, in bytes, including the
297 * terminating NULL character, or variable_name_size if no NULL
298 * character is found among the first variable_name_size bytes.
300 static unsigned long var_name_strnsize(efi_char16_t
*variable_name
,
301 unsigned long variable_name_size
)
307 * The variable name is, by definition, a NULL-terminated
308 * string, so make absolutely sure that variable_name_size is
309 * the value we expect it to be. If not, return the real size.
311 for (len
= 2; len
<= variable_name_size
; len
+= sizeof(c
)) {
312 c
= variable_name
[(len
/ sizeof(c
)) - 1];
317 return min(len
, variable_name_size
);
321 * Print a warning when duplicate EFI variables are encountered and
322 * disable the sysfs workqueue since the firmware is buggy.
324 static void dup_variable_bug(efi_char16_t
*str16
, efi_guid_t
*vendor_guid
,
327 size_t i
, len8
= len16
/ sizeof(efi_char16_t
);
331 * Disable the workqueue since the algorithm it uses for
332 * detecting new variables won't work with this buggy
333 * implementation of GetNextVariableName().
335 efivar_wq_enabled
= false;
337 str8
= kzalloc(len8
, GFP_KERNEL
);
341 for (i
= 0; i
< len8
; i
++)
344 printk(KERN_WARNING
"efivars: duplicate variable: %s-%pUl\n",
350 * efivar_init - build the initial list of EFI variables
351 * @func: callback function to invoke for every variable
352 * @data: function-specific data to pass to @func
353 * @atomic: do we need to execute the @func-loop atomically?
354 * @duplicates: error if we encounter duplicates on @head?
355 * @head: initialised head of variable list
357 * Get every EFI variable from the firmware and invoke @func. @func
358 * should call efivar_entry_add() to build the list of variables.
360 * Returns 0 on success, or a kernel error code on failure.
362 int efivar_init(int (*func
)(efi_char16_t
*, efi_guid_t
, unsigned long, void *),
363 void *data
, bool atomic
, bool duplicates
,
364 struct list_head
*head
)
366 const struct efivar_operations
*ops
= __efivars
->ops
;
367 unsigned long variable_name_size
= 1024;
368 efi_char16_t
*variable_name
;
370 efi_guid_t vendor_guid
;
373 variable_name
= kzalloc(variable_name_size
, GFP_KERNEL
);
374 if (!variable_name
) {
375 printk(KERN_ERR
"efivars: Memory allocation failed.\n");
379 spin_lock_irq(&__efivars
->lock
);
382 * Per EFI spec, the maximum storage allocated for both
383 * the variable name and variable data is 1024 bytes.
387 variable_name_size
= 1024;
389 status
= ops
->get_next_variable(&variable_name_size
,
395 spin_unlock_irq(&__efivars
->lock
);
397 variable_name_size
= var_name_strnsize(variable_name
,
401 * Some firmware implementations return the
402 * same variable name on multiple calls to
403 * get_next_variable(). Terminate the loop
404 * immediately as there is no guarantee that
405 * we'll ever see a different variable name,
406 * and may end up looping here forever.
409 variable_is_present(variable_name
, &vendor_guid
, head
)) {
410 dup_variable_bug(variable_name
, &vendor_guid
,
413 spin_lock_irq(&__efivars
->lock
);
415 status
= EFI_NOT_FOUND
;
419 err
= func(variable_name
, vendor_guid
, variable_name_size
, data
);
421 status
= EFI_NOT_FOUND
;
424 spin_lock_irq(&__efivars
->lock
);
430 printk(KERN_WARNING
"efivars: get_next_variable: status=%lx\n",
432 status
= EFI_NOT_FOUND
;
436 } while (status
!= EFI_NOT_FOUND
);
438 spin_unlock_irq(&__efivars
->lock
);
440 kfree(variable_name
);
444 EXPORT_SYMBOL_GPL(efivar_init
);
447 * efivar_entry_add - add entry to variable list
448 * @entry: entry to add to list
451 void efivar_entry_add(struct efivar_entry
*entry
, struct list_head
*head
)
453 spin_lock_irq(&__efivars
->lock
);
454 list_add(&entry
->list
, head
);
455 spin_unlock_irq(&__efivars
->lock
);
457 EXPORT_SYMBOL_GPL(efivar_entry_add
);
460 * efivar_entry_remove - remove entry from variable list
461 * @entry: entry to remove from list
463 void efivar_entry_remove(struct efivar_entry
*entry
)
465 spin_lock_irq(&__efivars
->lock
);
466 list_del(&entry
->list
);
467 spin_unlock_irq(&__efivars
->lock
);
469 EXPORT_SYMBOL_GPL(efivar_entry_remove
);
472 * efivar_entry_list_del_unlock - remove entry from variable list
473 * @entry: entry to remove
475 * Remove @entry from the variable list and release the list lock.
477 * NOTE: slightly weird locking semantics here - we expect to be
478 * called with the efivars lock already held, and we release it before
479 * returning. This is because this function is usually called after
480 * set_variable() while the lock is still held.
482 static void efivar_entry_list_del_unlock(struct efivar_entry
*entry
)
484 lockdep_assert_held(&__efivars
->lock
);
486 list_del(&entry
->list
);
487 spin_unlock_irq(&__efivars
->lock
);
491 * __efivar_entry_delete - delete an EFI variable
492 * @entry: entry containing EFI variable to delete
494 * Delete the variable from the firmware but leave @entry on the
497 * This function differs from efivar_entry_delete() because it does
498 * not remove @entry from the variable list. Also, it is safe to be
499 * called from within a efivar_entry_iter_begin() and
500 * efivar_entry_iter_end() region, unlike efivar_entry_delete().
502 * Returns 0 on success, or a converted EFI status code if
503 * set_variable() fails.
505 int __efivar_entry_delete(struct efivar_entry
*entry
)
507 const struct efivar_operations
*ops
= __efivars
->ops
;
510 lockdep_assert_held(&__efivars
->lock
);
512 status
= ops
->set_variable(entry
->var
.VariableName
,
513 &entry
->var
.VendorGuid
,
516 return efi_status_to_err(status
);
518 EXPORT_SYMBOL_GPL(__efivar_entry_delete
);
521 * efivar_entry_delete - delete variable and remove entry from list
522 * @entry: entry containing variable to delete
524 * Delete the variable from the firmware and remove @entry from the
525 * variable list. It is the caller's responsibility to free @entry
528 * Returns 0 on success, or a converted EFI status code if
529 * set_variable() fails.
531 int efivar_entry_delete(struct efivar_entry
*entry
)
533 const struct efivar_operations
*ops
= __efivars
->ops
;
536 spin_lock_irq(&__efivars
->lock
);
537 status
= ops
->set_variable(entry
->var
.VariableName
,
538 &entry
->var
.VendorGuid
,
540 if (!(status
== EFI_SUCCESS
|| status
== EFI_NOT_FOUND
)) {
541 spin_unlock_irq(&__efivars
->lock
);
542 return efi_status_to_err(status
);
545 efivar_entry_list_del_unlock(entry
);
548 EXPORT_SYMBOL_GPL(efivar_entry_delete
);
551 * efivar_entry_set - call set_variable()
552 * @entry: entry containing the EFI variable to write
553 * @attributes: variable attributes
554 * @size: size of @data buffer
555 * @data: buffer containing variable data
556 * @head: head of variable list
558 * Calls set_variable() for an EFI variable. If creating a new EFI
559 * variable, this function is usually followed by efivar_entry_add().
561 * Before writing the variable, the remaining EFI variable storage
562 * space is checked to ensure there is enough room available.
564 * If @head is not NULL a lookup is performed to determine whether
565 * the entry is already on the list.
567 * Returns 0 on success, -EEXIST if a lookup is performed and the entry
568 * already exists on the list, or a converted EFI status code if
569 * set_variable() fails.
571 int efivar_entry_set(struct efivar_entry
*entry
, u32 attributes
,
572 unsigned long size
, void *data
, struct list_head
*head
)
574 const struct efivar_operations
*ops
= __efivars
->ops
;
576 efi_char16_t
*name
= entry
->var
.VariableName
;
577 efi_guid_t vendor
= entry
->var
.VendorGuid
;
579 spin_lock_irq(&__efivars
->lock
);
581 if (head
&& efivar_entry_find(name
, vendor
, head
, false)) {
582 spin_unlock_irq(&__efivars
->lock
);
586 status
= check_var_size(attributes
, size
+ ucs2_strsize(name
, 1024));
587 if (status
== EFI_SUCCESS
|| status
== EFI_UNSUPPORTED
)
588 status
= ops
->set_variable(name
, &vendor
,
589 attributes
, size
, data
);
591 spin_unlock_irq(&__efivars
->lock
);
593 return efi_status_to_err(status
);
596 EXPORT_SYMBOL_GPL(efivar_entry_set
);
599 * efivar_entry_set_nonblocking - call set_variable_nonblocking()
601 * This function is guaranteed to not block and is suitable for calling
602 * from crash/panic handlers.
604 * Crucially, this function will not block if it cannot acquire
605 * __efivars->lock. Instead, it returns -EBUSY.
608 efivar_entry_set_nonblocking(efi_char16_t
*name
, efi_guid_t vendor
,
609 u32 attributes
, unsigned long size
, void *data
)
611 const struct efivar_operations
*ops
= __efivars
->ops
;
615 if (!spin_trylock_irqsave(&__efivars
->lock
, flags
))
618 status
= check_var_size(attributes
, size
+ ucs2_strsize(name
, 1024));
619 if (status
!= EFI_SUCCESS
) {
620 spin_unlock_irqrestore(&__efivars
->lock
, flags
);
624 status
= ops
->set_variable_nonblocking(name
, &vendor
, attributes
,
627 spin_unlock_irqrestore(&__efivars
->lock
, flags
);
628 return efi_status_to_err(status
);
632 * efivar_entry_set_safe - call set_variable() if enough space in firmware
633 * @name: buffer containing the variable name
634 * @vendor: variable vendor guid
635 * @attributes: variable attributes
636 * @block: can we block in this context?
637 * @size: size of @data buffer
638 * @data: buffer containing variable data
640 * Ensures there is enough free storage in the firmware for this variable, and
641 * if so, calls set_variable(). If creating a new EFI variable, this function
642 * is usually followed by efivar_entry_add().
644 * Returns 0 on success, -ENOSPC if the firmware does not have enough
645 * space for set_variable() to succeed, or a converted EFI status code
646 * if set_variable() fails.
648 int efivar_entry_set_safe(efi_char16_t
*name
, efi_guid_t vendor
, u32 attributes
,
649 bool block
, unsigned long size
, void *data
)
651 const struct efivar_operations
*ops
= __efivars
->ops
;
655 if (!ops
->query_variable_store
)
659 * If the EFI variable backend provides a non-blocking
660 * ->set_variable() operation and we're in a context where we
661 * cannot block, then we need to use it to avoid live-locks,
662 * since the implication is that the regular ->set_variable()
665 * If no ->set_variable_nonblocking() is provided then
666 * ->set_variable() is assumed to be non-blocking.
668 if (!block
&& ops
->set_variable_nonblocking
)
669 return efivar_entry_set_nonblocking(name
, vendor
, attributes
,
673 if (!spin_trylock_irqsave(&__efivars
->lock
, flags
))
676 spin_lock_irqsave(&__efivars
->lock
, flags
);
679 status
= check_var_size(attributes
, size
+ ucs2_strsize(name
, 1024));
680 if (status
!= EFI_SUCCESS
) {
681 spin_unlock_irqrestore(&__efivars
->lock
, flags
);
685 status
= ops
->set_variable(name
, &vendor
, attributes
, size
, data
);
687 spin_unlock_irqrestore(&__efivars
->lock
, flags
);
689 return efi_status_to_err(status
);
691 EXPORT_SYMBOL_GPL(efivar_entry_set_safe
);
694 * efivar_entry_find - search for an entry
695 * @name: the EFI variable name
696 * @guid: the EFI variable vendor's guid
697 * @head: head of the variable list
698 * @remove: should we remove the entry from the list?
700 * Search for an entry on the variable list that has the EFI variable
701 * name @name and vendor guid @guid. If an entry is found on the list
702 * and @remove is true, the entry is removed from the list.
704 * The caller MUST call efivar_entry_iter_begin() and
705 * efivar_entry_iter_end() before and after the invocation of this
706 * function, respectively.
708 * Returns the entry if found on the list, %NULL otherwise.
710 struct efivar_entry
*efivar_entry_find(efi_char16_t
*name
, efi_guid_t guid
,
711 struct list_head
*head
, bool remove
)
713 struct efivar_entry
*entry
, *n
;
714 int strsize1
, strsize2
;
717 lockdep_assert_held(&__efivars
->lock
);
719 list_for_each_entry_safe(entry
, n
, head
, list
) {
720 strsize1
= ucs2_strsize(name
, 1024);
721 strsize2
= ucs2_strsize(entry
->var
.VariableName
, 1024);
722 if (strsize1
== strsize2
&&
723 !memcmp(name
, &(entry
->var
.VariableName
), strsize1
) &&
724 !efi_guidcmp(guid
, entry
->var
.VendorGuid
)) {
734 if (entry
->scanning
) {
736 * The entry will be deleted
737 * after scanning is completed.
739 entry
->deleting
= true;
741 list_del(&entry
->list
);
746 EXPORT_SYMBOL_GPL(efivar_entry_find
);
749 * efivar_entry_size - obtain the size of a variable
750 * @entry: entry for this variable
751 * @size: location to store the variable's size
753 int efivar_entry_size(struct efivar_entry
*entry
, unsigned long *size
)
755 const struct efivar_operations
*ops
= __efivars
->ops
;
760 spin_lock_irq(&__efivars
->lock
);
761 status
= ops
->get_variable(entry
->var
.VariableName
,
762 &entry
->var
.VendorGuid
, NULL
, size
, NULL
);
763 spin_unlock_irq(&__efivars
->lock
);
765 if (status
!= EFI_BUFFER_TOO_SMALL
)
766 return efi_status_to_err(status
);
770 EXPORT_SYMBOL_GPL(efivar_entry_size
);
773 * __efivar_entry_get - call get_variable()
774 * @entry: read data for this variable
775 * @attributes: variable attributes
776 * @size: size of @data buffer
777 * @data: buffer to store variable data
779 * The caller MUST call efivar_entry_iter_begin() and
780 * efivar_entry_iter_end() before and after the invocation of this
781 * function, respectively.
783 int __efivar_entry_get(struct efivar_entry
*entry
, u32
*attributes
,
784 unsigned long *size
, void *data
)
786 const struct efivar_operations
*ops
= __efivars
->ops
;
789 lockdep_assert_held(&__efivars
->lock
);
791 status
= ops
->get_variable(entry
->var
.VariableName
,
792 &entry
->var
.VendorGuid
,
793 attributes
, size
, data
);
795 return efi_status_to_err(status
);
797 EXPORT_SYMBOL_GPL(__efivar_entry_get
);
800 * efivar_entry_get - call get_variable()
801 * @entry: read data for this variable
802 * @attributes: variable attributes
803 * @size: size of @data buffer
804 * @data: buffer to store variable data
806 int efivar_entry_get(struct efivar_entry
*entry
, u32
*attributes
,
807 unsigned long *size
, void *data
)
809 const struct efivar_operations
*ops
= __efivars
->ops
;
812 spin_lock_irq(&__efivars
->lock
);
813 status
= ops
->get_variable(entry
->var
.VariableName
,
814 &entry
->var
.VendorGuid
,
815 attributes
, size
, data
);
816 spin_unlock_irq(&__efivars
->lock
);
818 return efi_status_to_err(status
);
820 EXPORT_SYMBOL_GPL(efivar_entry_get
);
823 * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
824 * @entry: entry containing variable to set and get
825 * @attributes: attributes of variable to be written
826 * @size: size of data buffer
827 * @data: buffer containing data to write
828 * @set: did the set_variable() call succeed?
830 * This is a pretty special (complex) function. See efivarfs_file_write().
832 * Atomically call set_variable() for @entry and if the call is
833 * successful, return the new size of the variable from get_variable()
834 * in @size. The success of set_variable() is indicated by @set.
836 * Returns 0 on success, -EINVAL if the variable data is invalid,
837 * -ENOSPC if the firmware does not have enough available space, or a
838 * converted EFI status code if either of set_variable() or
839 * get_variable() fail.
841 * If the EFI variable does not exist when calling set_variable()
842 * (EFI_NOT_FOUND), @entry is removed from the variable list.
844 int efivar_entry_set_get_size(struct efivar_entry
*entry
, u32 attributes
,
845 unsigned long *size
, void *data
, bool *set
)
847 const struct efivar_operations
*ops
= __efivars
->ops
;
848 efi_char16_t
*name
= entry
->var
.VariableName
;
849 efi_guid_t
*vendor
= &entry
->var
.VendorGuid
;
855 if (efivar_validate(name
, data
, *size
) == false)
859 * The lock here protects the get_variable call, the conditional
860 * set_variable call, and removal of the variable from the efivars
861 * list (in the case of an authenticated delete).
863 spin_lock_irq(&__efivars
->lock
);
866 * Ensure that the available space hasn't shrunk below the safe level
868 status
= check_var_size(attributes
, *size
+ ucs2_strsize(name
, 1024));
869 if (status
!= EFI_SUCCESS
) {
870 if (status
!= EFI_UNSUPPORTED
) {
871 err
= efi_status_to_err(status
);
881 status
= ops
->set_variable(name
, vendor
, attributes
, *size
, data
);
882 if (status
!= EFI_SUCCESS
) {
883 err
= efi_status_to_err(status
);
890 * Writing to the variable may have caused a change in size (which
891 * could either be an append or an overwrite), or the variable to be
892 * deleted. Perform a GetVariable() so we can tell what actually
896 status
= ops
->get_variable(entry
->var
.VariableName
,
897 &entry
->var
.VendorGuid
,
900 if (status
== EFI_NOT_FOUND
)
901 efivar_entry_list_del_unlock(entry
);
903 spin_unlock_irq(&__efivars
->lock
);
905 if (status
&& status
!= EFI_BUFFER_TOO_SMALL
)
906 return efi_status_to_err(status
);
911 spin_unlock_irq(&__efivars
->lock
);
915 EXPORT_SYMBOL_GPL(efivar_entry_set_get_size
);
918 * efivar_entry_iter_begin - begin iterating the variable list
920 * Lock the variable list to prevent entry insertion and removal until
921 * efivar_entry_iter_end() is called. This function is usually used in
922 * conjunction with __efivar_entry_iter() or efivar_entry_iter().
924 void efivar_entry_iter_begin(void)
926 spin_lock_irq(&__efivars
->lock
);
928 EXPORT_SYMBOL_GPL(efivar_entry_iter_begin
);
931 * efivar_entry_iter_end - finish iterating the variable list
933 * Unlock the variable list and allow modifications to the list again.
935 void efivar_entry_iter_end(void)
937 spin_unlock_irq(&__efivars
->lock
);
939 EXPORT_SYMBOL_GPL(efivar_entry_iter_end
);
942 * __efivar_entry_iter - iterate over variable list
943 * @func: callback function
944 * @head: head of the variable list
945 * @data: function-specific data to pass to callback
946 * @prev: entry to begin iterating from
948 * Iterate over the list of EFI variables and call @func with every
949 * entry on the list. It is safe for @func to remove entries in the
950 * list via efivar_entry_delete().
952 * You MUST call efivar_enter_iter_begin() before this function, and
953 * efivar_entry_iter_end() afterwards.
955 * It is possible to begin iteration from an arbitrary entry within
956 * the list by passing @prev. @prev is updated on return to point to
957 * the last entry passed to @func. To begin iterating from the
958 * beginning of the list @prev must be %NULL.
960 * The restrictions for @func are the same as documented for
961 * efivar_entry_iter().
963 int __efivar_entry_iter(int (*func
)(struct efivar_entry
*, void *),
964 struct list_head
*head
, void *data
,
965 struct efivar_entry
**prev
)
967 struct efivar_entry
*entry
, *n
;
970 if (!prev
|| !*prev
) {
971 list_for_each_entry_safe(entry
, n
, head
, list
) {
972 err
= func(entry
, data
);
984 list_for_each_entry_safe_continue((*prev
), n
, head
, list
) {
985 err
= func(*prev
, data
);
992 EXPORT_SYMBOL_GPL(__efivar_entry_iter
);
995 * efivar_entry_iter - iterate over variable list
996 * @func: callback function
997 * @head: head of variable list
998 * @data: function-specific data to pass to callback
1000 * Iterate over the list of EFI variables and call @func with every
1001 * entry on the list. It is safe for @func to remove entries in the
1002 * list via efivar_entry_delete() while iterating.
1004 * Some notes for the callback function:
1005 * - a non-zero return value indicates an error and terminates the loop
1006 * - @func is called from atomic context
1008 int efivar_entry_iter(int (*func
)(struct efivar_entry
*, void *),
1009 struct list_head
*head
, void *data
)
1013 efivar_entry_iter_begin();
1014 err
= __efivar_entry_iter(func
, head
, data
, NULL
);
1015 efivar_entry_iter_end();
1019 EXPORT_SYMBOL_GPL(efivar_entry_iter
);
1022 * efivars_kobject - get the kobject for the registered efivars
1024 * If efivars_register() has not been called we return NULL,
1025 * otherwise return the kobject used at registration time.
1027 struct kobject
*efivars_kobject(void)
1032 return __efivars
->kobject
;
1034 EXPORT_SYMBOL_GPL(efivars_kobject
);
1037 * efivar_run_worker - schedule the efivar worker thread
1039 void efivar_run_worker(void)
1041 if (efivar_wq_enabled
)
1042 schedule_work(&efivar_work
);
1044 EXPORT_SYMBOL_GPL(efivar_run_worker
);
1047 * efivars_register - register an efivars
1048 * @efivars: efivars to register
1049 * @ops: efivars operations
1050 * @kobject: @efivars-specific kobject
1052 * Only a single efivars can be registered at any time.
1054 int efivars_register(struct efivars
*efivars
,
1055 const struct efivar_operations
*ops
,
1056 struct kobject
*kobject
)
1058 spin_lock_init(&efivars
->lock
);
1060 efivars
->kobject
= kobject
;
1062 __efivars
= efivars
;
1066 EXPORT_SYMBOL_GPL(efivars_register
);
1069 * efivars_unregister - unregister an efivars
1070 * @efivars: efivars to unregister
1072 * The caller must have already removed every entry from the list,
1073 * failure to do so is an error.
1075 int efivars_unregister(struct efivars
*efivars
)
1080 printk(KERN_ERR
"efivars not registered\n");
1085 if (__efivars
!= efivars
) {
1096 EXPORT_SYMBOL_GPL(efivars_unregister
);