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
{
170 bool (*validate
)(efi_char16_t
*var_name
, int match
, u8
*data
,
175 * This is the list of variables we need to validate, as well as the
176 * whitelist for what we think is safe not to default to immutable.
178 * If it has a validate() method that's not NULL, it'll go into the
179 * validation routine. If not, it is assumed valid, but still used for
182 * Note that it's sorted by {vendor,name}, but globbed names must come after
183 * any other name with the same prefix.
185 static const struct variable_validate variable_validate
[] = {
186 { EFI_GLOBAL_VARIABLE_GUID
, "BootNext", validate_uint16
},
187 { EFI_GLOBAL_VARIABLE_GUID
, "BootOrder", validate_boot_order
},
188 { EFI_GLOBAL_VARIABLE_GUID
, "Boot*", validate_load_option
},
189 { EFI_GLOBAL_VARIABLE_GUID
, "DriverOrder", validate_boot_order
},
190 { EFI_GLOBAL_VARIABLE_GUID
, "Driver*", validate_load_option
},
191 { EFI_GLOBAL_VARIABLE_GUID
, "ConIn", validate_device_path
},
192 { EFI_GLOBAL_VARIABLE_GUID
, "ConInDev", validate_device_path
},
193 { EFI_GLOBAL_VARIABLE_GUID
, "ConOut", validate_device_path
},
194 { EFI_GLOBAL_VARIABLE_GUID
, "ConOutDev", validate_device_path
},
195 { EFI_GLOBAL_VARIABLE_GUID
, "ErrOut", validate_device_path
},
196 { EFI_GLOBAL_VARIABLE_GUID
, "ErrOutDev", validate_device_path
},
197 { EFI_GLOBAL_VARIABLE_GUID
, "Lang", validate_ascii_string
},
198 { EFI_GLOBAL_VARIABLE_GUID
, "OsIndications", NULL
},
199 { EFI_GLOBAL_VARIABLE_GUID
, "PlatformLang", validate_ascii_string
},
200 { EFI_GLOBAL_VARIABLE_GUID
, "Timeout", validate_uint16
},
201 { LINUX_EFI_CRASH_GUID
, "*", NULL
},
202 { NULL_GUID
, "", NULL
},
206 variable_matches(const char *var_name
, size_t len
, const char *match_name
,
209 for (*match
= 0; ; (*match
)++) {
210 char c
= match_name
[*match
];
211 char u
= var_name
[*match
];
213 /* Wildcard in the matching name means we've matched */
217 /* Case sensitive match */
218 if (!c
&& *match
== len
)
231 efivar_validate(efi_guid_t vendor
, efi_char16_t
*var_name
, u8
*data
,
232 unsigned long data_size
)
235 unsigned long utf8_size
;
238 utf8_size
= ucs2_utf8size(var_name
);
239 utf8_name
= kmalloc(utf8_size
+ 1, GFP_KERNEL
);
243 ucs2_as_utf8(utf8_name
, var_name
, utf8_size
);
244 utf8_name
[utf8_size
] = '\0';
246 for (i
= 0; variable_validate
[i
].name
[0] != '\0'; i
++) {
247 const char *name
= variable_validate
[i
].name
;
250 if (efi_guidcmp(vendor
, variable_validate
[i
].vendor
))
253 if (variable_matches(utf8_name
, utf8_size
+1, name
, &match
)) {
254 if (variable_validate
[i
].validate
== NULL
)
257 return variable_validate
[i
].validate(var_name
, match
,
264 EXPORT_SYMBOL_GPL(efivar_validate
);
267 efivar_variable_is_removable(efi_guid_t vendor
, const char *var_name
,
275 * Check if our variable is in the validated variables list
277 for (i
= 0; variable_validate
[i
].name
[0] != '\0'; i
++) {
278 if (efi_guidcmp(variable_validate
[i
].vendor
, vendor
))
281 if (variable_matches(var_name
, len
,
282 variable_validate
[i
].name
, &match
)) {
289 * If it's in our list, it is removable.
293 EXPORT_SYMBOL_GPL(efivar_variable_is_removable
);
296 check_var_size(u32 attributes
, unsigned long size
)
298 const struct efivar_operations
*fops
= __efivars
->ops
;
300 if (!fops
->query_variable_store
)
301 return EFI_UNSUPPORTED
;
303 return fops
->query_variable_store(attributes
, size
);
306 static int efi_status_to_err(efi_status_t status
)
314 case EFI_INVALID_PARAMETER
:
317 case EFI_OUT_OF_RESOURCES
:
320 case EFI_DEVICE_ERROR
:
323 case EFI_WRITE_PROTECTED
:
326 case EFI_SECURITY_VIOLATION
:
339 static bool variable_is_present(efi_char16_t
*variable_name
, efi_guid_t
*vendor
,
340 struct list_head
*head
)
342 struct efivar_entry
*entry
, *n
;
343 unsigned long strsize1
, strsize2
;
346 strsize1
= ucs2_strsize(variable_name
, 1024);
347 list_for_each_entry_safe(entry
, n
, head
, list
) {
348 strsize2
= ucs2_strsize(entry
->var
.VariableName
, 1024);
349 if (strsize1
== strsize2
&&
350 !memcmp(variable_name
, &(entry
->var
.VariableName
),
352 !efi_guidcmp(entry
->var
.VendorGuid
,
362 * Returns the size of variable_name, in bytes, including the
363 * terminating NULL character, or variable_name_size if no NULL
364 * character is found among the first variable_name_size bytes.
366 static unsigned long var_name_strnsize(efi_char16_t
*variable_name
,
367 unsigned long variable_name_size
)
373 * The variable name is, by definition, a NULL-terminated
374 * string, so make absolutely sure that variable_name_size is
375 * the value we expect it to be. If not, return the real size.
377 for (len
= 2; len
<= variable_name_size
; len
+= sizeof(c
)) {
378 c
= variable_name
[(len
/ sizeof(c
)) - 1];
383 return min(len
, variable_name_size
);
387 * Print a warning when duplicate EFI variables are encountered and
388 * disable the sysfs workqueue since the firmware is buggy.
390 static void dup_variable_bug(efi_char16_t
*str16
, efi_guid_t
*vendor_guid
,
393 size_t i
, len8
= len16
/ sizeof(efi_char16_t
);
397 * Disable the workqueue since the algorithm it uses for
398 * detecting new variables won't work with this buggy
399 * implementation of GetNextVariableName().
401 efivar_wq_enabled
= false;
403 str8
= kzalloc(len8
, GFP_KERNEL
);
407 for (i
= 0; i
< len8
; i
++)
410 printk(KERN_WARNING
"efivars: duplicate variable: %s-%pUl\n",
416 * efivar_init - build the initial list of EFI variables
417 * @func: callback function to invoke for every variable
418 * @data: function-specific data to pass to @func
419 * @atomic: do we need to execute the @func-loop atomically?
420 * @duplicates: error if we encounter duplicates on @head?
421 * @head: initialised head of variable list
423 * Get every EFI variable from the firmware and invoke @func. @func
424 * should call efivar_entry_add() to build the list of variables.
426 * Returns 0 on success, or a kernel error code on failure.
428 int efivar_init(int (*func
)(efi_char16_t
*, efi_guid_t
, unsigned long, void *),
429 void *data
, bool atomic
, bool duplicates
,
430 struct list_head
*head
)
432 const struct efivar_operations
*ops
= __efivars
->ops
;
433 unsigned long variable_name_size
= 1024;
434 efi_char16_t
*variable_name
;
436 efi_guid_t vendor_guid
;
439 variable_name
= kzalloc(variable_name_size
, GFP_KERNEL
);
440 if (!variable_name
) {
441 printk(KERN_ERR
"efivars: Memory allocation failed.\n");
445 spin_lock_irq(&__efivars
->lock
);
448 * Per EFI spec, the maximum storage allocated for both
449 * the variable name and variable data is 1024 bytes.
453 variable_name_size
= 1024;
455 status
= ops
->get_next_variable(&variable_name_size
,
461 spin_unlock_irq(&__efivars
->lock
);
463 variable_name_size
= var_name_strnsize(variable_name
,
467 * Some firmware implementations return the
468 * same variable name on multiple calls to
469 * get_next_variable(). Terminate the loop
470 * immediately as there is no guarantee that
471 * we'll ever see a different variable name,
472 * and may end up looping here forever.
475 variable_is_present(variable_name
, &vendor_guid
, head
)) {
476 dup_variable_bug(variable_name
, &vendor_guid
,
479 spin_lock_irq(&__efivars
->lock
);
481 status
= EFI_NOT_FOUND
;
485 err
= func(variable_name
, vendor_guid
, variable_name_size
, data
);
487 status
= EFI_NOT_FOUND
;
490 spin_lock_irq(&__efivars
->lock
);
496 printk(KERN_WARNING
"efivars: get_next_variable: status=%lx\n",
498 status
= EFI_NOT_FOUND
;
502 } while (status
!= EFI_NOT_FOUND
);
504 spin_unlock_irq(&__efivars
->lock
);
506 kfree(variable_name
);
510 EXPORT_SYMBOL_GPL(efivar_init
);
513 * efivar_entry_add - add entry to variable list
514 * @entry: entry to add to list
517 void efivar_entry_add(struct efivar_entry
*entry
, struct list_head
*head
)
519 spin_lock_irq(&__efivars
->lock
);
520 list_add(&entry
->list
, head
);
521 spin_unlock_irq(&__efivars
->lock
);
523 EXPORT_SYMBOL_GPL(efivar_entry_add
);
526 * efivar_entry_remove - remove entry from variable list
527 * @entry: entry to remove from list
529 void efivar_entry_remove(struct efivar_entry
*entry
)
531 spin_lock_irq(&__efivars
->lock
);
532 list_del(&entry
->list
);
533 spin_unlock_irq(&__efivars
->lock
);
535 EXPORT_SYMBOL_GPL(efivar_entry_remove
);
538 * efivar_entry_list_del_unlock - remove entry from variable list
539 * @entry: entry to remove
541 * Remove @entry from the variable list and release the list lock.
543 * NOTE: slightly weird locking semantics here - we expect to be
544 * called with the efivars lock already held, and we release it before
545 * returning. This is because this function is usually called after
546 * set_variable() while the lock is still held.
548 static void efivar_entry_list_del_unlock(struct efivar_entry
*entry
)
550 lockdep_assert_held(&__efivars
->lock
);
552 list_del(&entry
->list
);
553 spin_unlock_irq(&__efivars
->lock
);
557 * __efivar_entry_delete - delete an EFI variable
558 * @entry: entry containing EFI variable to delete
560 * Delete the variable from the firmware but leave @entry on the
563 * This function differs from efivar_entry_delete() because it does
564 * not remove @entry from the variable list. Also, it is safe to be
565 * called from within a efivar_entry_iter_begin() and
566 * efivar_entry_iter_end() region, unlike efivar_entry_delete().
568 * Returns 0 on success, or a converted EFI status code if
569 * set_variable() fails.
571 int __efivar_entry_delete(struct efivar_entry
*entry
)
573 const struct efivar_operations
*ops
= __efivars
->ops
;
576 lockdep_assert_held(&__efivars
->lock
);
578 status
= ops
->set_variable(entry
->var
.VariableName
,
579 &entry
->var
.VendorGuid
,
582 return efi_status_to_err(status
);
584 EXPORT_SYMBOL_GPL(__efivar_entry_delete
);
587 * efivar_entry_delete - delete variable and remove entry from list
588 * @entry: entry containing variable to delete
590 * Delete the variable from the firmware and remove @entry from the
591 * variable list. It is the caller's responsibility to free @entry
594 * Returns 0 on success, or a converted EFI status code if
595 * set_variable() fails.
597 int efivar_entry_delete(struct efivar_entry
*entry
)
599 const struct efivar_operations
*ops
= __efivars
->ops
;
602 spin_lock_irq(&__efivars
->lock
);
603 status
= ops
->set_variable(entry
->var
.VariableName
,
604 &entry
->var
.VendorGuid
,
606 if (!(status
== EFI_SUCCESS
|| status
== EFI_NOT_FOUND
)) {
607 spin_unlock_irq(&__efivars
->lock
);
608 return efi_status_to_err(status
);
611 efivar_entry_list_del_unlock(entry
);
614 EXPORT_SYMBOL_GPL(efivar_entry_delete
);
617 * efivar_entry_set - call set_variable()
618 * @entry: entry containing the EFI variable to write
619 * @attributes: variable attributes
620 * @size: size of @data buffer
621 * @data: buffer containing variable data
622 * @head: head of variable list
624 * Calls set_variable() for an EFI variable. If creating a new EFI
625 * variable, this function is usually followed by efivar_entry_add().
627 * Before writing the variable, the remaining EFI variable storage
628 * space is checked to ensure there is enough room available.
630 * If @head is not NULL a lookup is performed to determine whether
631 * the entry is already on the list.
633 * Returns 0 on success, -EEXIST if a lookup is performed and the entry
634 * already exists on the list, or a converted EFI status code if
635 * set_variable() fails.
637 int efivar_entry_set(struct efivar_entry
*entry
, u32 attributes
,
638 unsigned long size
, void *data
, struct list_head
*head
)
640 const struct efivar_operations
*ops
= __efivars
->ops
;
642 efi_char16_t
*name
= entry
->var
.VariableName
;
643 efi_guid_t vendor
= entry
->var
.VendorGuid
;
645 spin_lock_irq(&__efivars
->lock
);
647 if (head
&& efivar_entry_find(name
, vendor
, head
, false)) {
648 spin_unlock_irq(&__efivars
->lock
);
652 status
= check_var_size(attributes
, size
+ ucs2_strsize(name
, 1024));
653 if (status
== EFI_SUCCESS
|| status
== EFI_UNSUPPORTED
)
654 status
= ops
->set_variable(name
, &vendor
,
655 attributes
, size
, data
);
657 spin_unlock_irq(&__efivars
->lock
);
659 return efi_status_to_err(status
);
662 EXPORT_SYMBOL_GPL(efivar_entry_set
);
665 * efivar_entry_set_nonblocking - call set_variable_nonblocking()
667 * This function is guaranteed to not block and is suitable for calling
668 * from crash/panic handlers.
670 * Crucially, this function will not block if it cannot acquire
671 * __efivars->lock. Instead, it returns -EBUSY.
674 efivar_entry_set_nonblocking(efi_char16_t
*name
, efi_guid_t vendor
,
675 u32 attributes
, unsigned long size
, void *data
)
677 const struct efivar_operations
*ops
= __efivars
->ops
;
681 if (!spin_trylock_irqsave(&__efivars
->lock
, flags
))
684 status
= check_var_size(attributes
, size
+ ucs2_strsize(name
, 1024));
685 if (status
!= EFI_SUCCESS
) {
686 spin_unlock_irqrestore(&__efivars
->lock
, flags
);
690 status
= ops
->set_variable_nonblocking(name
, &vendor
, attributes
,
693 spin_unlock_irqrestore(&__efivars
->lock
, flags
);
694 return efi_status_to_err(status
);
698 * efivar_entry_set_safe - call set_variable() if enough space in firmware
699 * @name: buffer containing the variable name
700 * @vendor: variable vendor guid
701 * @attributes: variable attributes
702 * @block: can we block in this context?
703 * @size: size of @data buffer
704 * @data: buffer containing variable data
706 * Ensures there is enough free storage in the firmware for this variable, and
707 * if so, calls set_variable(). If creating a new EFI variable, this function
708 * is usually followed by efivar_entry_add().
710 * Returns 0 on success, -ENOSPC if the firmware does not have enough
711 * space for set_variable() to succeed, or a converted EFI status code
712 * if set_variable() fails.
714 int efivar_entry_set_safe(efi_char16_t
*name
, efi_guid_t vendor
, u32 attributes
,
715 bool block
, unsigned long size
, void *data
)
717 const struct efivar_operations
*ops
= __efivars
->ops
;
721 if (!ops
->query_variable_store
)
725 * If the EFI variable backend provides a non-blocking
726 * ->set_variable() operation and we're in a context where we
727 * cannot block, then we need to use it to avoid live-locks,
728 * since the implication is that the regular ->set_variable()
731 * If no ->set_variable_nonblocking() is provided then
732 * ->set_variable() is assumed to be non-blocking.
734 if (!block
&& ops
->set_variable_nonblocking
)
735 return efivar_entry_set_nonblocking(name
, vendor
, attributes
,
739 if (!spin_trylock_irqsave(&__efivars
->lock
, flags
))
742 spin_lock_irqsave(&__efivars
->lock
, flags
);
745 status
= check_var_size(attributes
, size
+ ucs2_strsize(name
, 1024));
746 if (status
!= EFI_SUCCESS
) {
747 spin_unlock_irqrestore(&__efivars
->lock
, flags
);
751 status
= ops
->set_variable(name
, &vendor
, attributes
, size
, data
);
753 spin_unlock_irqrestore(&__efivars
->lock
, flags
);
755 return efi_status_to_err(status
);
757 EXPORT_SYMBOL_GPL(efivar_entry_set_safe
);
760 * efivar_entry_find - search for an entry
761 * @name: the EFI variable name
762 * @guid: the EFI variable vendor's guid
763 * @head: head of the variable list
764 * @remove: should we remove the entry from the list?
766 * Search for an entry on the variable list that has the EFI variable
767 * name @name and vendor guid @guid. If an entry is found on the list
768 * and @remove is true, the entry is removed from the list.
770 * The caller MUST call efivar_entry_iter_begin() and
771 * efivar_entry_iter_end() before and after the invocation of this
772 * function, respectively.
774 * Returns the entry if found on the list, %NULL otherwise.
776 struct efivar_entry
*efivar_entry_find(efi_char16_t
*name
, efi_guid_t guid
,
777 struct list_head
*head
, bool remove
)
779 struct efivar_entry
*entry
, *n
;
780 int strsize1
, strsize2
;
783 lockdep_assert_held(&__efivars
->lock
);
785 list_for_each_entry_safe(entry
, n
, head
, list
) {
786 strsize1
= ucs2_strsize(name
, 1024);
787 strsize2
= ucs2_strsize(entry
->var
.VariableName
, 1024);
788 if (strsize1
== strsize2
&&
789 !memcmp(name
, &(entry
->var
.VariableName
), strsize1
) &&
790 !efi_guidcmp(guid
, entry
->var
.VendorGuid
)) {
800 if (entry
->scanning
) {
802 * The entry will be deleted
803 * after scanning is completed.
805 entry
->deleting
= true;
807 list_del(&entry
->list
);
812 EXPORT_SYMBOL_GPL(efivar_entry_find
);
815 * efivar_entry_size - obtain the size of a variable
816 * @entry: entry for this variable
817 * @size: location to store the variable's size
819 int efivar_entry_size(struct efivar_entry
*entry
, unsigned long *size
)
821 const struct efivar_operations
*ops
= __efivars
->ops
;
826 spin_lock_irq(&__efivars
->lock
);
827 status
= ops
->get_variable(entry
->var
.VariableName
,
828 &entry
->var
.VendorGuid
, NULL
, size
, NULL
);
829 spin_unlock_irq(&__efivars
->lock
);
831 if (status
!= EFI_BUFFER_TOO_SMALL
)
832 return efi_status_to_err(status
);
836 EXPORT_SYMBOL_GPL(efivar_entry_size
);
839 * __efivar_entry_get - call get_variable()
840 * @entry: read data for this variable
841 * @attributes: variable attributes
842 * @size: size of @data buffer
843 * @data: buffer to store variable data
845 * The caller MUST call efivar_entry_iter_begin() and
846 * efivar_entry_iter_end() before and after the invocation of this
847 * function, respectively.
849 int __efivar_entry_get(struct efivar_entry
*entry
, u32
*attributes
,
850 unsigned long *size
, void *data
)
852 const struct efivar_operations
*ops
= __efivars
->ops
;
855 lockdep_assert_held(&__efivars
->lock
);
857 status
= ops
->get_variable(entry
->var
.VariableName
,
858 &entry
->var
.VendorGuid
,
859 attributes
, size
, data
);
861 return efi_status_to_err(status
);
863 EXPORT_SYMBOL_GPL(__efivar_entry_get
);
866 * efivar_entry_get - call get_variable()
867 * @entry: read data for this variable
868 * @attributes: variable attributes
869 * @size: size of @data buffer
870 * @data: buffer to store variable data
872 int efivar_entry_get(struct efivar_entry
*entry
, u32
*attributes
,
873 unsigned long *size
, void *data
)
875 const struct efivar_operations
*ops
= __efivars
->ops
;
878 spin_lock_irq(&__efivars
->lock
);
879 status
= ops
->get_variable(entry
->var
.VariableName
,
880 &entry
->var
.VendorGuid
,
881 attributes
, size
, data
);
882 spin_unlock_irq(&__efivars
->lock
);
884 return efi_status_to_err(status
);
886 EXPORT_SYMBOL_GPL(efivar_entry_get
);
889 * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
890 * @entry: entry containing variable to set and get
891 * @attributes: attributes of variable to be written
892 * @size: size of data buffer
893 * @data: buffer containing data to write
894 * @set: did the set_variable() call succeed?
896 * This is a pretty special (complex) function. See efivarfs_file_write().
898 * Atomically call set_variable() for @entry and if the call is
899 * successful, return the new size of the variable from get_variable()
900 * in @size. The success of set_variable() is indicated by @set.
902 * Returns 0 on success, -EINVAL if the variable data is invalid,
903 * -ENOSPC if the firmware does not have enough available space, or a
904 * converted EFI status code if either of set_variable() or
905 * get_variable() fail.
907 * If the EFI variable does not exist when calling set_variable()
908 * (EFI_NOT_FOUND), @entry is removed from the variable list.
910 int efivar_entry_set_get_size(struct efivar_entry
*entry
, u32 attributes
,
911 unsigned long *size
, void *data
, bool *set
)
913 const struct efivar_operations
*ops
= __efivars
->ops
;
914 efi_char16_t
*name
= entry
->var
.VariableName
;
915 efi_guid_t
*vendor
= &entry
->var
.VendorGuid
;
921 if (efivar_validate(*vendor
, name
, data
, *size
) == false)
925 * The lock here protects the get_variable call, the conditional
926 * set_variable call, and removal of the variable from the efivars
927 * list (in the case of an authenticated delete).
929 spin_lock_irq(&__efivars
->lock
);
932 * Ensure that the available space hasn't shrunk below the safe level
934 status
= check_var_size(attributes
, *size
+ ucs2_strsize(name
, 1024));
935 if (status
!= EFI_SUCCESS
) {
936 if (status
!= EFI_UNSUPPORTED
) {
937 err
= efi_status_to_err(status
);
947 status
= ops
->set_variable(name
, vendor
, attributes
, *size
, data
);
948 if (status
!= EFI_SUCCESS
) {
949 err
= efi_status_to_err(status
);
956 * Writing to the variable may have caused a change in size (which
957 * could either be an append or an overwrite), or the variable to be
958 * deleted. Perform a GetVariable() so we can tell what actually
962 status
= ops
->get_variable(entry
->var
.VariableName
,
963 &entry
->var
.VendorGuid
,
966 if (status
== EFI_NOT_FOUND
)
967 efivar_entry_list_del_unlock(entry
);
969 spin_unlock_irq(&__efivars
->lock
);
971 if (status
&& status
!= EFI_BUFFER_TOO_SMALL
)
972 return efi_status_to_err(status
);
977 spin_unlock_irq(&__efivars
->lock
);
981 EXPORT_SYMBOL_GPL(efivar_entry_set_get_size
);
984 * efivar_entry_iter_begin - begin iterating the variable list
986 * Lock the variable list to prevent entry insertion and removal until
987 * efivar_entry_iter_end() is called. This function is usually used in
988 * conjunction with __efivar_entry_iter() or efivar_entry_iter().
990 void efivar_entry_iter_begin(void)
992 spin_lock_irq(&__efivars
->lock
);
994 EXPORT_SYMBOL_GPL(efivar_entry_iter_begin
);
997 * efivar_entry_iter_end - finish iterating the variable list
999 * Unlock the variable list and allow modifications to the list again.
1001 void efivar_entry_iter_end(void)
1003 spin_unlock_irq(&__efivars
->lock
);
1005 EXPORT_SYMBOL_GPL(efivar_entry_iter_end
);
1008 * __efivar_entry_iter - iterate over variable list
1009 * @func: callback function
1010 * @head: head of the variable list
1011 * @data: function-specific data to pass to callback
1012 * @prev: entry to begin iterating from
1014 * Iterate over the list of EFI variables and call @func with every
1015 * entry on the list. It is safe for @func to remove entries in the
1016 * list via efivar_entry_delete().
1018 * You MUST call efivar_enter_iter_begin() before this function, and
1019 * efivar_entry_iter_end() afterwards.
1021 * It is possible to begin iteration from an arbitrary entry within
1022 * the list by passing @prev. @prev is updated on return to point to
1023 * the last entry passed to @func. To begin iterating from the
1024 * beginning of the list @prev must be %NULL.
1026 * The restrictions for @func are the same as documented for
1027 * efivar_entry_iter().
1029 int __efivar_entry_iter(int (*func
)(struct efivar_entry
*, void *),
1030 struct list_head
*head
, void *data
,
1031 struct efivar_entry
**prev
)
1033 struct efivar_entry
*entry
, *n
;
1036 if (!prev
|| !*prev
) {
1037 list_for_each_entry_safe(entry
, n
, head
, list
) {
1038 err
= func(entry
, data
);
1050 list_for_each_entry_safe_continue((*prev
), n
, head
, list
) {
1051 err
= func(*prev
, data
);
1058 EXPORT_SYMBOL_GPL(__efivar_entry_iter
);
1061 * efivar_entry_iter - iterate over variable list
1062 * @func: callback function
1063 * @head: head of variable list
1064 * @data: function-specific data to pass to callback
1066 * Iterate over the list of EFI variables and call @func with every
1067 * entry on the list. It is safe for @func to remove entries in the
1068 * list via efivar_entry_delete() while iterating.
1070 * Some notes for the callback function:
1071 * - a non-zero return value indicates an error and terminates the loop
1072 * - @func is called from atomic context
1074 int efivar_entry_iter(int (*func
)(struct efivar_entry
*, void *),
1075 struct list_head
*head
, void *data
)
1079 efivar_entry_iter_begin();
1080 err
= __efivar_entry_iter(func
, head
, data
, NULL
);
1081 efivar_entry_iter_end();
1085 EXPORT_SYMBOL_GPL(efivar_entry_iter
);
1088 * efivars_kobject - get the kobject for the registered efivars
1090 * If efivars_register() has not been called we return NULL,
1091 * otherwise return the kobject used at registration time.
1093 struct kobject
*efivars_kobject(void)
1098 return __efivars
->kobject
;
1100 EXPORT_SYMBOL_GPL(efivars_kobject
);
1103 * efivar_run_worker - schedule the efivar worker thread
1105 void efivar_run_worker(void)
1107 if (efivar_wq_enabled
)
1108 schedule_work(&efivar_work
);
1110 EXPORT_SYMBOL_GPL(efivar_run_worker
);
1113 * efivars_register - register an efivars
1114 * @efivars: efivars to register
1115 * @ops: efivars operations
1116 * @kobject: @efivars-specific kobject
1118 * Only a single efivars can be registered at any time.
1120 int efivars_register(struct efivars
*efivars
,
1121 const struct efivar_operations
*ops
,
1122 struct kobject
*kobject
)
1124 spin_lock_init(&efivars
->lock
);
1126 efivars
->kobject
= kobject
;
1128 __efivars
= efivars
;
1132 EXPORT_SYMBOL_GPL(efivars_register
);
1135 * efivars_unregister - unregister an efivars
1136 * @efivars: efivars to unregister
1138 * The caller must have already removed every entry from the list,
1139 * failure to do so is an error.
1141 int efivars_unregister(struct efivars
*efivars
)
1146 printk(KERN_ERR
"efivars not registered\n");
1151 if (__efivars
!= efivars
) {
1162 EXPORT_SYMBOL_GPL(efivars_unregister
);