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
;
41 * efivars_lock protects three things:
42 * 1) efivarfs_list and efivars_sysfs_list
44 * 3) (un)registration of __efivars
46 static DEFINE_SEMAPHORE(efivars_lock
);
48 static bool efivar_wq_enabled
= true;
49 DECLARE_WORK(efivar_work
, NULL
);
50 EXPORT_SYMBOL_GPL(efivar_work
);
53 validate_device_path(efi_char16_t
*var_name
, int match
, u8
*buffer
,
56 struct efi_generic_dev_path
*node
;
59 node
= (struct efi_generic_dev_path
*)buffer
;
61 if (len
< sizeof(*node
))
64 while (offset
<= len
- sizeof(*node
) &&
65 node
->length
>= sizeof(*node
) &&
66 node
->length
<= len
- offset
) {
67 offset
+= node
->length
;
69 if ((node
->type
== EFI_DEV_END_PATH
||
70 node
->type
== EFI_DEV_END_PATH2
) &&
71 node
->sub_type
== EFI_DEV_END_ENTIRE
)
74 node
= (struct efi_generic_dev_path
*)(buffer
+ offset
);
78 * If we're here then either node->length pointed past the end
79 * of the buffer or we reached the end of the buffer without
80 * finding a device path end node.
86 validate_boot_order(efi_char16_t
*var_name
, int match
, u8
*buffer
,
89 /* An array of 16-bit integers */
97 validate_load_option(efi_char16_t
*var_name
, int match
, u8
*buffer
,
101 int i
, desclength
= 0, namelen
;
103 namelen
= ucs2_strnlen(var_name
, EFI_VAR_NAME_LEN
);
105 /* Either "Boot" or "Driver" followed by four digits of hex */
106 for (i
= match
; i
< match
+4; i
++) {
107 if (var_name
[i
] > 127 ||
108 hex_to_bin(var_name
[i
] & 0xff) < 0)
112 /* Reject it if there's 4 digits of hex and then further content */
113 if (namelen
> match
+ 4)
116 /* A valid entry must be at least 8 bytes */
120 filepathlength
= buffer
[4] | buffer
[5] << 8;
123 * There's no stored length for the description, so it has to be
126 desclength
= ucs2_strsize((efi_char16_t
*)(buffer
+ 6), len
- 6) + 2;
128 /* Each boot entry must have a descriptor */
133 * If the sum of the length of the description, the claimed filepath
134 * length and the original header are greater than the length of the
135 * variable, it's malformed
137 if ((desclength
+ filepathlength
+ 6) > len
)
141 * And, finally, check the filepath
143 return validate_device_path(var_name
, match
, buffer
+ desclength
+ 6,
148 validate_uint16(efi_char16_t
*var_name
, int match
, u8
*buffer
,
151 /* A single 16-bit integer */
159 validate_ascii_string(efi_char16_t
*var_name
, int match
, u8
*buffer
,
164 for (i
= 0; i
< len
; i
++) {
175 struct variable_validate
{
178 bool (*validate
)(efi_char16_t
*var_name
, int match
, u8
*data
,
183 * This is the list of variables we need to validate, as well as the
184 * whitelist for what we think is safe not to default to immutable.
186 * If it has a validate() method that's not NULL, it'll go into the
187 * validation routine. If not, it is assumed valid, but still used for
190 * Note that it's sorted by {vendor,name}, but globbed names must come after
191 * any other name with the same prefix.
193 static const struct variable_validate variable_validate
[] = {
194 { EFI_GLOBAL_VARIABLE_GUID
, "BootNext", validate_uint16
},
195 { EFI_GLOBAL_VARIABLE_GUID
, "BootOrder", validate_boot_order
},
196 { EFI_GLOBAL_VARIABLE_GUID
, "Boot*", validate_load_option
},
197 { EFI_GLOBAL_VARIABLE_GUID
, "DriverOrder", validate_boot_order
},
198 { EFI_GLOBAL_VARIABLE_GUID
, "Driver*", validate_load_option
},
199 { EFI_GLOBAL_VARIABLE_GUID
, "ConIn", validate_device_path
},
200 { EFI_GLOBAL_VARIABLE_GUID
, "ConInDev", validate_device_path
},
201 { EFI_GLOBAL_VARIABLE_GUID
, "ConOut", validate_device_path
},
202 { EFI_GLOBAL_VARIABLE_GUID
, "ConOutDev", validate_device_path
},
203 { EFI_GLOBAL_VARIABLE_GUID
, "ErrOut", validate_device_path
},
204 { EFI_GLOBAL_VARIABLE_GUID
, "ErrOutDev", validate_device_path
},
205 { EFI_GLOBAL_VARIABLE_GUID
, "Lang", validate_ascii_string
},
206 { EFI_GLOBAL_VARIABLE_GUID
, "OsIndications", NULL
},
207 { EFI_GLOBAL_VARIABLE_GUID
, "PlatformLang", validate_ascii_string
},
208 { EFI_GLOBAL_VARIABLE_GUID
, "Timeout", validate_uint16
},
209 { LINUX_EFI_CRASH_GUID
, "*", NULL
},
210 { NULL_GUID
, "", NULL
},
214 * Check if @var_name matches the pattern given in @match_name.
216 * @var_name: an array of @len non-NUL characters.
217 * @match_name: a NUL-terminated pattern string, optionally ending in "*". A
218 * final "*" character matches any trailing characters @var_name,
219 * including the case when there are none left in @var_name.
220 * @match: on output, the number of non-wildcard characters in @match_name
221 * that @var_name matches, regardless of the return value.
222 * @return: whether @var_name fully matches @match_name.
225 variable_matches(const char *var_name
, size_t len
, const char *match_name
,
228 for (*match
= 0; ; (*match
)++) {
229 char c
= match_name
[*match
];
233 /* Wildcard in @match_name means we've matched. */
237 /* @match_name has ended. Has @var_name too? */
238 return (*match
== len
);
242 * We've reached a non-wildcard char in @match_name.
243 * Continue only if there's an identical character in
246 if (*match
< len
&& c
== var_name
[*match
])
254 efivar_validate(efi_guid_t vendor
, efi_char16_t
*var_name
, u8
*data
,
255 unsigned long data_size
)
258 unsigned long utf8_size
;
261 utf8_size
= ucs2_utf8size(var_name
);
262 utf8_name
= kmalloc(utf8_size
+ 1, GFP_KERNEL
);
266 ucs2_as_utf8(utf8_name
, var_name
, utf8_size
);
267 utf8_name
[utf8_size
] = '\0';
269 for (i
= 0; variable_validate
[i
].name
[0] != '\0'; i
++) {
270 const char *name
= variable_validate
[i
].name
;
273 if (efi_guidcmp(vendor
, variable_validate
[i
].vendor
))
276 if (variable_matches(utf8_name
, utf8_size
+1, name
, &match
)) {
277 if (variable_validate
[i
].validate
== NULL
)
280 return variable_validate
[i
].validate(var_name
, match
,
287 EXPORT_SYMBOL_GPL(efivar_validate
);
290 efivar_variable_is_removable(efi_guid_t vendor
, const char *var_name
,
298 * Check if our variable is in the validated variables list
300 for (i
= 0; variable_validate
[i
].name
[0] != '\0'; i
++) {
301 if (efi_guidcmp(variable_validate
[i
].vendor
, vendor
))
304 if (variable_matches(var_name
, len
,
305 variable_validate
[i
].name
, &match
)) {
312 * If it's in our list, it is removable.
316 EXPORT_SYMBOL_GPL(efivar_variable_is_removable
);
319 check_var_size(u32 attributes
, unsigned long size
)
321 const struct efivar_operations
*fops
;
324 return EFI_UNSUPPORTED
;
326 fops
= __efivars
->ops
;
328 if (!fops
->query_variable_store
)
329 return EFI_UNSUPPORTED
;
331 return fops
->query_variable_store(attributes
, size
, false);
335 check_var_size_nonblocking(u32 attributes
, unsigned long size
)
337 const struct efivar_operations
*fops
;
340 return EFI_UNSUPPORTED
;
342 fops
= __efivars
->ops
;
344 if (!fops
->query_variable_store
)
345 return EFI_UNSUPPORTED
;
347 return fops
->query_variable_store(attributes
, size
, true);
350 static bool variable_is_present(efi_char16_t
*variable_name
, efi_guid_t
*vendor
,
351 struct list_head
*head
)
353 struct efivar_entry
*entry
, *n
;
354 unsigned long strsize1
, strsize2
;
357 strsize1
= ucs2_strsize(variable_name
, 1024);
358 list_for_each_entry_safe(entry
, n
, head
, list
) {
359 strsize2
= ucs2_strsize(entry
->var
.VariableName
, 1024);
360 if (strsize1
== strsize2
&&
361 !memcmp(variable_name
, &(entry
->var
.VariableName
),
363 !efi_guidcmp(entry
->var
.VendorGuid
,
373 * Returns the size of variable_name, in bytes, including the
374 * terminating NULL character, or variable_name_size if no NULL
375 * character is found among the first variable_name_size bytes.
377 static unsigned long var_name_strnsize(efi_char16_t
*variable_name
,
378 unsigned long variable_name_size
)
384 * The variable name is, by definition, a NULL-terminated
385 * string, so make absolutely sure that variable_name_size is
386 * the value we expect it to be. If not, return the real size.
388 for (len
= 2; len
<= variable_name_size
; len
+= sizeof(c
)) {
389 c
= variable_name
[(len
/ sizeof(c
)) - 1];
394 return min(len
, variable_name_size
);
398 * Print a warning when duplicate EFI variables are encountered and
399 * disable the sysfs workqueue since the firmware is buggy.
401 static void dup_variable_bug(efi_char16_t
*str16
, efi_guid_t
*vendor_guid
,
404 size_t i
, len8
= len16
/ sizeof(efi_char16_t
);
408 * Disable the workqueue since the algorithm it uses for
409 * detecting new variables won't work with this buggy
410 * implementation of GetNextVariableName().
412 efivar_wq_enabled
= false;
414 str8
= kzalloc(len8
, GFP_KERNEL
);
418 for (i
= 0; i
< len8
; i
++)
421 printk(KERN_WARNING
"efivars: duplicate variable: %s-%pUl\n",
427 * efivar_init - build the initial list of EFI variables
428 * @func: callback function to invoke for every variable
429 * @data: function-specific data to pass to @func
430 * @atomic: do we need to execute the @func-loop atomically?
431 * @duplicates: error if we encounter duplicates on @head?
432 * @head: initialised head of variable list
434 * Get every EFI variable from the firmware and invoke @func. @func
435 * should call efivar_entry_add() to build the list of variables.
437 * Returns 0 on success, or a kernel error code on failure.
439 int efivar_init(int (*func
)(efi_char16_t
*, efi_guid_t
, unsigned long, void *),
440 void *data
, bool duplicates
, struct list_head
*head
)
442 const struct efivar_operations
*ops
;
443 unsigned long variable_name_size
= 1024;
444 efi_char16_t
*variable_name
;
446 efi_guid_t vendor_guid
;
452 ops
= __efivars
->ops
;
454 variable_name
= kzalloc(variable_name_size
, GFP_KERNEL
);
455 if (!variable_name
) {
456 printk(KERN_ERR
"efivars: Memory allocation failed.\n");
460 if (down_interruptible(&efivars_lock
)) {
466 * Per EFI spec, the maximum storage allocated for both
467 * the variable name and variable data is 1024 bytes.
471 variable_name_size
= 1024;
473 status
= ops
->get_next_variable(&variable_name_size
,
481 variable_name_size
= var_name_strnsize(variable_name
,
485 * Some firmware implementations return the
486 * same variable name on multiple calls to
487 * get_next_variable(). Terminate the loop
488 * immediately as there is no guarantee that
489 * we'll ever see a different variable name,
490 * and may end up looping here forever.
493 variable_is_present(variable_name
, &vendor_guid
,
495 dup_variable_bug(variable_name
, &vendor_guid
,
497 status
= EFI_NOT_FOUND
;
499 err
= func(variable_name
, vendor_guid
,
500 variable_name_size
, data
);
502 status
= EFI_NOT_FOUND
;
506 if (down_interruptible(&efivars_lock
)) {
516 printk(KERN_WARNING
"efivars: get_next_variable: status=%lx\n",
518 status
= EFI_NOT_FOUND
;
522 } while (status
!= EFI_NOT_FOUND
);
526 kfree(variable_name
);
530 EXPORT_SYMBOL_GPL(efivar_init
);
533 * efivar_entry_add - add entry to variable list
534 * @entry: entry to add to list
537 * Returns 0 on success, or a kernel error code on failure.
539 int efivar_entry_add(struct efivar_entry
*entry
, struct list_head
*head
)
541 if (down_interruptible(&efivars_lock
))
543 list_add(&entry
->list
, head
);
548 EXPORT_SYMBOL_GPL(efivar_entry_add
);
551 * efivar_entry_remove - remove entry from variable list
552 * @entry: entry to remove from list
554 * Returns 0 on success, or a kernel error code on failure.
556 int efivar_entry_remove(struct efivar_entry
*entry
)
558 if (down_interruptible(&efivars_lock
))
560 list_del(&entry
->list
);
565 EXPORT_SYMBOL_GPL(efivar_entry_remove
);
568 * efivar_entry_list_del_unlock - remove entry from variable list
569 * @entry: entry to remove
571 * Remove @entry from the variable list and release the list lock.
573 * NOTE: slightly weird locking semantics here - we expect to be
574 * called with the efivars lock already held, and we release it before
575 * returning. This is because this function is usually called after
576 * set_variable() while the lock is still held.
578 static void efivar_entry_list_del_unlock(struct efivar_entry
*entry
)
580 list_del(&entry
->list
);
585 * __efivar_entry_delete - delete an EFI variable
586 * @entry: entry containing EFI variable to delete
588 * Delete the variable from the firmware but leave @entry on the
591 * This function differs from efivar_entry_delete() because it does
592 * not remove @entry from the variable list. Also, it is safe to be
593 * called from within a efivar_entry_iter_begin() and
594 * efivar_entry_iter_end() region, unlike efivar_entry_delete().
596 * Returns 0 on success, or a converted EFI status code if
597 * set_variable() fails.
599 int __efivar_entry_delete(struct efivar_entry
*entry
)
606 status
= __efivars
->ops
->set_variable(entry
->var
.VariableName
,
607 &entry
->var
.VendorGuid
,
610 return efi_status_to_err(status
);
612 EXPORT_SYMBOL_GPL(__efivar_entry_delete
);
615 * efivar_entry_delete - delete variable and remove entry from list
616 * @entry: entry containing variable to delete
618 * Delete the variable from the firmware and remove @entry from the
619 * variable list. It is the caller's responsibility to free @entry
622 * Returns 0 on success, -EINTR if we can't grab the semaphore,
623 * converted EFI status code if set_variable() fails.
625 int efivar_entry_delete(struct efivar_entry
*entry
)
627 const struct efivar_operations
*ops
;
630 if (down_interruptible(&efivars_lock
))
637 ops
= __efivars
->ops
;
638 status
= ops
->set_variable(entry
->var
.VariableName
,
639 &entry
->var
.VendorGuid
,
641 if (!(status
== EFI_SUCCESS
|| status
== EFI_NOT_FOUND
)) {
643 return efi_status_to_err(status
);
646 efivar_entry_list_del_unlock(entry
);
649 EXPORT_SYMBOL_GPL(efivar_entry_delete
);
652 * efivar_entry_set - call set_variable()
653 * @entry: entry containing the EFI variable to write
654 * @attributes: variable attributes
655 * @size: size of @data buffer
656 * @data: buffer containing variable data
657 * @head: head of variable list
659 * Calls set_variable() for an EFI variable. If creating a new EFI
660 * variable, this function is usually followed by efivar_entry_add().
662 * Before writing the variable, the remaining EFI variable storage
663 * space is checked to ensure there is enough room available.
665 * If @head is not NULL a lookup is performed to determine whether
666 * the entry is already on the list.
668 * Returns 0 on success, -EINTR if we can't grab the semaphore,
669 * -EEXIST if a lookup is performed and the entry already exists on
670 * the list, or a converted EFI status code if set_variable() fails.
672 int efivar_entry_set(struct efivar_entry
*entry
, u32 attributes
,
673 unsigned long size
, void *data
, struct list_head
*head
)
675 const struct efivar_operations
*ops
;
677 efi_char16_t
*name
= entry
->var
.VariableName
;
678 efi_guid_t vendor
= entry
->var
.VendorGuid
;
680 if (down_interruptible(&efivars_lock
))
687 ops
= __efivars
->ops
;
688 if (head
&& efivar_entry_find(name
, vendor
, head
, false)) {
693 status
= check_var_size(attributes
, size
+ ucs2_strsize(name
, 1024));
694 if (status
== EFI_SUCCESS
|| status
== EFI_UNSUPPORTED
)
695 status
= ops
->set_variable(name
, &vendor
,
696 attributes
, size
, data
);
700 return efi_status_to_err(status
);
703 EXPORT_SYMBOL_GPL(efivar_entry_set
);
706 * efivar_entry_set_nonblocking - call set_variable_nonblocking()
708 * This function is guaranteed to not block and is suitable for calling
709 * from crash/panic handlers.
711 * Crucially, this function will not block if it cannot acquire
712 * efivars_lock. Instead, it returns -EBUSY.
715 efivar_entry_set_nonblocking(efi_char16_t
*name
, efi_guid_t vendor
,
716 u32 attributes
, unsigned long size
, void *data
)
718 const struct efivar_operations
*ops
;
721 if (down_trylock(&efivars_lock
))
729 status
= check_var_size_nonblocking(attributes
,
730 size
+ ucs2_strsize(name
, 1024));
731 if (status
!= EFI_SUCCESS
) {
736 ops
= __efivars
->ops
;
737 status
= ops
->set_variable_nonblocking(name
, &vendor
, attributes
,
741 return efi_status_to_err(status
);
745 * efivar_entry_set_safe - call set_variable() if enough space in firmware
746 * @name: buffer containing the variable name
747 * @vendor: variable vendor guid
748 * @attributes: variable attributes
749 * @block: can we block in this context?
750 * @size: size of @data buffer
751 * @data: buffer containing variable data
753 * Ensures there is enough free storage in the firmware for this variable, and
754 * if so, calls set_variable(). If creating a new EFI variable, this function
755 * is usually followed by efivar_entry_add().
757 * Returns 0 on success, -ENOSPC if the firmware does not have enough
758 * space for set_variable() to succeed, or a converted EFI status code
759 * if set_variable() fails.
761 int efivar_entry_set_safe(efi_char16_t
*name
, efi_guid_t vendor
, u32 attributes
,
762 bool block
, unsigned long size
, void *data
)
764 const struct efivar_operations
*ops
;
770 ops
= __efivars
->ops
;
771 if (!ops
->query_variable_store
)
775 * If the EFI variable backend provides a non-blocking
776 * ->set_variable() operation and we're in a context where we
777 * cannot block, then we need to use it to avoid live-locks,
778 * since the implication is that the regular ->set_variable()
781 * If no ->set_variable_nonblocking() is provided then
782 * ->set_variable() is assumed to be non-blocking.
784 if (!block
&& ops
->set_variable_nonblocking
)
785 return efivar_entry_set_nonblocking(name
, vendor
, attributes
,
789 if (down_trylock(&efivars_lock
))
792 if (down_interruptible(&efivars_lock
))
796 status
= check_var_size(attributes
, size
+ ucs2_strsize(name
, 1024));
797 if (status
!= EFI_SUCCESS
) {
802 status
= ops
->set_variable(name
, &vendor
, attributes
, size
, data
);
806 return efi_status_to_err(status
);
808 EXPORT_SYMBOL_GPL(efivar_entry_set_safe
);
811 * efivar_entry_find - search for an entry
812 * @name: the EFI variable name
813 * @guid: the EFI variable vendor's guid
814 * @head: head of the variable list
815 * @remove: should we remove the entry from the list?
817 * Search for an entry on the variable list that has the EFI variable
818 * name @name and vendor guid @guid. If an entry is found on the list
819 * and @remove is true, the entry is removed from the list.
821 * The caller MUST call efivar_entry_iter_begin() and
822 * efivar_entry_iter_end() before and after the invocation of this
823 * function, respectively.
825 * Returns the entry if found on the list, %NULL otherwise.
827 struct efivar_entry
*efivar_entry_find(efi_char16_t
*name
, efi_guid_t guid
,
828 struct list_head
*head
, bool remove
)
830 struct efivar_entry
*entry
, *n
;
831 int strsize1
, strsize2
;
834 list_for_each_entry_safe(entry
, n
, head
, list
) {
835 strsize1
= ucs2_strsize(name
, 1024);
836 strsize2
= ucs2_strsize(entry
->var
.VariableName
, 1024);
837 if (strsize1
== strsize2
&&
838 !memcmp(name
, &(entry
->var
.VariableName
), strsize1
) &&
839 !efi_guidcmp(guid
, entry
->var
.VendorGuid
)) {
849 if (entry
->scanning
) {
851 * The entry will be deleted
852 * after scanning is completed.
854 entry
->deleting
= true;
856 list_del(&entry
->list
);
861 EXPORT_SYMBOL_GPL(efivar_entry_find
);
864 * efivar_entry_size - obtain the size of a variable
865 * @entry: entry for this variable
866 * @size: location to store the variable's size
868 int efivar_entry_size(struct efivar_entry
*entry
, unsigned long *size
)
870 const struct efivar_operations
*ops
;
875 if (down_interruptible(&efivars_lock
))
881 ops
= __efivars
->ops
;
882 status
= ops
->get_variable(entry
->var
.VariableName
,
883 &entry
->var
.VendorGuid
, NULL
, size
, NULL
);
886 if (status
!= EFI_BUFFER_TOO_SMALL
)
887 return efi_status_to_err(status
);
891 EXPORT_SYMBOL_GPL(efivar_entry_size
);
894 * __efivar_entry_get - call get_variable()
895 * @entry: read data for this variable
896 * @attributes: variable attributes
897 * @size: size of @data buffer
898 * @data: buffer to store variable data
900 * The caller MUST call efivar_entry_iter_begin() and
901 * efivar_entry_iter_end() before and after the invocation of this
902 * function, respectively.
904 int __efivar_entry_get(struct efivar_entry
*entry
, u32
*attributes
,
905 unsigned long *size
, void *data
)
912 status
= __efivars
->ops
->get_variable(entry
->var
.VariableName
,
913 &entry
->var
.VendorGuid
,
914 attributes
, size
, data
);
916 return efi_status_to_err(status
);
918 EXPORT_SYMBOL_GPL(__efivar_entry_get
);
921 * efivar_entry_get - call get_variable()
922 * @entry: read data for this variable
923 * @attributes: variable attributes
924 * @size: size of @data buffer
925 * @data: buffer to store variable data
927 int efivar_entry_get(struct efivar_entry
*entry
, u32
*attributes
,
928 unsigned long *size
, void *data
)
932 if (down_interruptible(&efivars_lock
))
940 status
= __efivars
->ops
->get_variable(entry
->var
.VariableName
,
941 &entry
->var
.VendorGuid
,
942 attributes
, size
, data
);
945 return efi_status_to_err(status
);
947 EXPORT_SYMBOL_GPL(efivar_entry_get
);
950 * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
951 * @entry: entry containing variable to set and get
952 * @attributes: attributes of variable to be written
953 * @size: size of data buffer
954 * @data: buffer containing data to write
955 * @set: did the set_variable() call succeed?
957 * This is a pretty special (complex) function. See efivarfs_file_write().
959 * Atomically call set_variable() for @entry and if the call is
960 * successful, return the new size of the variable from get_variable()
961 * in @size. The success of set_variable() is indicated by @set.
963 * Returns 0 on success, -EINVAL if the variable data is invalid,
964 * -ENOSPC if the firmware does not have enough available space, or a
965 * converted EFI status code if either of set_variable() or
966 * get_variable() fail.
968 * If the EFI variable does not exist when calling set_variable()
969 * (EFI_NOT_FOUND), @entry is removed from the variable list.
971 int efivar_entry_set_get_size(struct efivar_entry
*entry
, u32 attributes
,
972 unsigned long *size
, void *data
, bool *set
)
974 const struct efivar_operations
*ops
;
975 efi_char16_t
*name
= entry
->var
.VariableName
;
976 efi_guid_t
*vendor
= &entry
->var
.VendorGuid
;
982 if (efivar_validate(*vendor
, name
, data
, *size
) == false)
986 * The lock here protects the get_variable call, the conditional
987 * set_variable call, and removal of the variable from the efivars
988 * list (in the case of an authenticated delete).
990 if (down_interruptible(&efivars_lock
))
999 * Ensure that the available space hasn't shrunk below the safe level
1001 status
= check_var_size(attributes
, *size
+ ucs2_strsize(name
, 1024));
1002 if (status
!= EFI_SUCCESS
) {
1003 if (status
!= EFI_UNSUPPORTED
) {
1004 err
= efi_status_to_err(status
);
1008 if (*size
> 65536) {
1014 ops
= __efivars
->ops
;
1016 status
= ops
->set_variable(name
, vendor
, attributes
, *size
, data
);
1017 if (status
!= EFI_SUCCESS
) {
1018 err
= efi_status_to_err(status
);
1025 * Writing to the variable may have caused a change in size (which
1026 * could either be an append or an overwrite), or the variable to be
1027 * deleted. Perform a GetVariable() so we can tell what actually
1031 status
= ops
->get_variable(entry
->var
.VariableName
,
1032 &entry
->var
.VendorGuid
,
1035 if (status
== EFI_NOT_FOUND
)
1036 efivar_entry_list_del_unlock(entry
);
1040 if (status
&& status
!= EFI_BUFFER_TOO_SMALL
)
1041 return efi_status_to_err(status
);
1050 EXPORT_SYMBOL_GPL(efivar_entry_set_get_size
);
1053 * efivar_entry_iter_begin - begin iterating the variable list
1055 * Lock the variable list to prevent entry insertion and removal until
1056 * efivar_entry_iter_end() is called. This function is usually used in
1057 * conjunction with __efivar_entry_iter() or efivar_entry_iter().
1059 int efivar_entry_iter_begin(void)
1061 return down_interruptible(&efivars_lock
);
1063 EXPORT_SYMBOL_GPL(efivar_entry_iter_begin
);
1066 * efivar_entry_iter_end - finish iterating the variable list
1068 * Unlock the variable list and allow modifications to the list again.
1070 void efivar_entry_iter_end(void)
1074 EXPORT_SYMBOL_GPL(efivar_entry_iter_end
);
1077 * __efivar_entry_iter - iterate over variable list
1078 * @func: callback function
1079 * @head: head of the variable list
1080 * @data: function-specific data to pass to callback
1081 * @prev: entry to begin iterating from
1083 * Iterate over the list of EFI variables and call @func with every
1084 * entry on the list. It is safe for @func to remove entries in the
1085 * list via efivar_entry_delete().
1087 * You MUST call efivar_enter_iter_begin() before this function, and
1088 * efivar_entry_iter_end() afterwards.
1090 * It is possible to begin iteration from an arbitrary entry within
1091 * the list by passing @prev. @prev is updated on return to point to
1092 * the last entry passed to @func. To begin iterating from the
1093 * beginning of the list @prev must be %NULL.
1095 * The restrictions for @func are the same as documented for
1096 * efivar_entry_iter().
1098 int __efivar_entry_iter(int (*func
)(struct efivar_entry
*, void *),
1099 struct list_head
*head
, void *data
,
1100 struct efivar_entry
**prev
)
1102 struct efivar_entry
*entry
, *n
;
1105 if (!prev
|| !*prev
) {
1106 list_for_each_entry_safe(entry
, n
, head
, list
) {
1107 err
= func(entry
, data
);
1119 list_for_each_entry_safe_continue((*prev
), n
, head
, list
) {
1120 err
= func(*prev
, data
);
1127 EXPORT_SYMBOL_GPL(__efivar_entry_iter
);
1130 * efivar_entry_iter - iterate over variable list
1131 * @func: callback function
1132 * @head: head of variable list
1133 * @data: function-specific data to pass to callback
1135 * Iterate over the list of EFI variables and call @func with every
1136 * entry on the list. It is safe for @func to remove entries in the
1137 * list via efivar_entry_delete() while iterating.
1139 * Some notes for the callback function:
1140 * - a non-zero return value indicates an error and terminates the loop
1141 * - @func is called from atomic context
1143 int efivar_entry_iter(int (*func
)(struct efivar_entry
*, void *),
1144 struct list_head
*head
, void *data
)
1148 err
= efivar_entry_iter_begin();
1151 err
= __efivar_entry_iter(func
, head
, data
, NULL
);
1152 efivar_entry_iter_end();
1156 EXPORT_SYMBOL_GPL(efivar_entry_iter
);
1159 * efivars_kobject - get the kobject for the registered efivars
1161 * If efivars_register() has not been called we return NULL,
1162 * otherwise return the kobject used at registration time.
1164 struct kobject
*efivars_kobject(void)
1169 return __efivars
->kobject
;
1171 EXPORT_SYMBOL_GPL(efivars_kobject
);
1174 * efivar_run_worker - schedule the efivar worker thread
1176 void efivar_run_worker(void)
1178 if (efivar_wq_enabled
)
1179 schedule_work(&efivar_work
);
1181 EXPORT_SYMBOL_GPL(efivar_run_worker
);
1184 * efivars_register - register an efivars
1185 * @efivars: efivars to register
1186 * @ops: efivars operations
1187 * @kobject: @efivars-specific kobject
1189 * Only a single efivars can be registered at any time.
1191 int efivars_register(struct efivars
*efivars
,
1192 const struct efivar_operations
*ops
,
1193 struct kobject
*kobject
)
1195 if (down_interruptible(&efivars_lock
))
1199 efivars
->kobject
= kobject
;
1201 __efivars
= efivars
;
1203 pr_info("Registered efivars operations\n");
1209 EXPORT_SYMBOL_GPL(efivars_register
);
1212 * efivars_unregister - unregister an efivars
1213 * @efivars: efivars to unregister
1215 * The caller must have already removed every entry from the list,
1216 * failure to do so is an error.
1218 int efivars_unregister(struct efivars
*efivars
)
1222 if (down_interruptible(&efivars_lock
))
1226 printk(KERN_ERR
"efivars not registered\n");
1231 if (__efivars
!= efivars
) {
1236 pr_info("Unregistered efivars operations\n");
1244 EXPORT_SYMBOL_GPL(efivars_unregister
);