dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / firmware / efi / vars.c
blob436d1776bc7bed9111124e93d33d77f79014d297
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
7 */
9 #include <linux/capability.h>
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/init.h>
13 #include <linux/mm.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
30 * 2) ->ops calls
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);
39 static bool
40 validate_device_path(efi_char16_t *var_name, int match, u8 *buffer,
41 unsigned long len)
43 struct efi_generic_dev_path *node;
44 int offset = 0;
46 node = (struct efi_generic_dev_path *)buffer;
48 if (len < sizeof(*node))
49 return false;
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)
59 return true;
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.
69 return false;
72 static bool
73 validate_boot_order(efi_char16_t *var_name, int match, u8 *buffer,
74 unsigned long len)
76 /* An array of 16-bit integers */
77 if ((len % 2) != 0)
78 return false;
80 return true;
83 static bool
84 validate_load_option(efi_char16_t *var_name, int match, u8 *buffer,
85 unsigned long len)
87 u16 filepathlength;
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)
96 return true;
99 /* Reject it if there's 4 digits of hex and then further content */
100 if (namelen > match + 4)
101 return false;
103 /* A valid entry must be at least 8 bytes */
104 if (len < 8)
105 return false;
107 filepathlength = buffer[4] | buffer[5] << 8;
110 * There's no stored length for the description, so it has to be
111 * found by hand
113 desclength = ucs2_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
115 /* Each boot entry must have a descriptor */
116 if (!desclength)
117 return false;
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)
125 return false;
128 * And, finally, check the filepath
130 return validate_device_path(var_name, match, buffer + desclength + 6,
131 filepathlength);
134 static bool
135 validate_uint16(efi_char16_t *var_name, int match, u8 *buffer,
136 unsigned long len)
138 /* A single 16-bit integer */
139 if (len != 2)
140 return false;
142 return true;
145 static bool
146 validate_ascii_string(efi_char16_t *var_name, int match, u8 *buffer,
147 unsigned long len)
149 int i;
151 for (i = 0; i < len; i++) {
152 if (buffer[i] > 127)
153 return false;
155 if (buffer[i] == 0)
156 return true;
159 return false;
162 struct variable_validate {
163 efi_guid_t vendor;
164 char *name;
165 bool (*validate)(efi_char16_t *var_name, int match, u8 *data,
166 unsigned long len);
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
175 * whitelisting.
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.
211 static bool
212 variable_matches(const char *var_name, size_t len, const char *match_name,
213 int *match)
215 for (*match = 0; ; (*match)++) {
216 char c = match_name[*match];
218 switch (c) {
219 case '*':
220 /* Wildcard in @match_name means we've matched. */
221 return true;
223 case '\0':
224 /* @match_name has ended. Has @var_name too? */
225 return (*match == len);
227 default:
229 * We've reached a non-wildcard char in @match_name.
230 * Continue only if there's an identical character in
231 * @var_name.
233 if (*match < len && c == var_name[*match])
234 continue;
235 return false;
240 bool
241 efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data,
242 unsigned long data_size)
244 int i;
245 unsigned long utf8_size;
246 u8 *utf8_name;
248 utf8_size = ucs2_utf8size(var_name);
249 utf8_name = kmalloc(utf8_size + 1, GFP_KERNEL);
250 if (!utf8_name)
251 return false;
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;
258 int match = 0;
260 if (efi_guidcmp(vendor, variable_validate[i].vendor))
261 continue;
263 if (variable_matches(utf8_name, utf8_size+1, name, &match)) {
264 if (variable_validate[i].validate == NULL)
265 break;
266 kfree(utf8_name);
267 return variable_validate[i].validate(var_name, match,
268 data, data_size);
271 kfree(utf8_name);
272 return true;
274 EXPORT_SYMBOL_GPL(efivar_validate);
276 bool
277 efivar_variable_is_removable(efi_guid_t vendor, const char *var_name,
278 size_t len)
280 int i;
281 bool found = false;
282 int match = 0;
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))
289 continue;
291 if (variable_matches(var_name, len,
292 variable_validate[i].name, &match)) {
293 found = true;
294 break;
299 * If it's in our list, it is removable.
301 return found;
303 EXPORT_SYMBOL_GPL(efivar_variable_is_removable);
305 static efi_status_t
306 check_var_size(u32 attributes, unsigned long size)
308 const struct efivar_operations *fops;
310 if (!__efivars)
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);
321 static efi_status_t
322 check_var_size_nonblocking(u32 attributes, unsigned long size)
324 const struct efivar_operations *fops;
326 if (!__efivars)
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;
342 bool found = false;
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),
349 strsize2) &&
350 !efi_guidcmp(entry->var.VendorGuid,
351 *vendor)) {
352 found = true;
353 break;
356 return found;
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)
367 unsigned long len;
368 efi_char16_t c;
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];
377 if (!c)
378 break;
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,
389 unsigned long len16)
391 size_t i, len8 = len16 / sizeof(efi_char16_t);
392 char *str8;
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);
402 if (!str8)
403 return;
405 for (i = 0; i < len8; i++)
406 str8[i] = str16[i];
408 printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
409 str8, vendor_guid);
410 kfree(str8);
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;
432 efi_status_t status;
433 efi_guid_t vendor_guid;
434 int err = 0;
436 if (!__efivars)
437 return -EFAULT;
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");
444 return -ENOMEM;
447 if (down_interruptible(&efivars_lock)) {
448 err = -EINTR;
449 goto free;
453 * Per EFI spec, the maximum storage allocated for both
454 * the variable name and variable data is 1024 bytes.
457 do {
458 variable_name_size = 1024;
460 status = ops->get_next_variable(&variable_name_size,
461 variable_name,
462 &vendor_guid);
463 switch (status) {
464 case EFI_SUCCESS:
465 if (duplicates)
466 up(&efivars_lock);
468 variable_name_size = var_name_strnsize(variable_name,
469 variable_name_size);
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.
479 if (duplicates &&
480 variable_is_present(variable_name, &vendor_guid,
481 head)) {
482 dup_variable_bug(variable_name, &vendor_guid,
483 variable_name_size);
484 status = EFI_NOT_FOUND;
485 } else {
486 err = func(variable_name, vendor_guid,
487 variable_name_size, data);
488 if (err)
489 status = EFI_NOT_FOUND;
492 if (duplicates) {
493 if (down_interruptible(&efivars_lock)) {
494 err = -EINTR;
495 goto free;
499 break;
500 case EFI_NOT_FOUND:
501 break;
502 default:
503 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
504 status);
505 status = EFI_NOT_FOUND;
506 break;
509 } while (status != EFI_NOT_FOUND);
511 up(&efivars_lock);
512 free:
513 kfree(variable_name);
515 return err;
517 EXPORT_SYMBOL_GPL(efivar_init);
520 * efivar_entry_add - add entry to variable list
521 * @entry: entry to add to list
522 * @head: list head
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))
529 return -EINTR;
530 list_add(&entry->list, head);
531 up(&efivars_lock);
533 return 0;
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))
546 return -EINTR;
547 list_del(&entry->list);
548 up(&efivars_lock);
550 return 0;
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);
568 up(&efivars_lock);
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
576 * variable list.
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)
588 efi_status_t status;
590 if (!__efivars)
591 return -EINVAL;
593 status = __efivars->ops->set_variable(entry->var.VariableName,
594 &entry->var.VendorGuid,
595 0, 0, NULL);
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
607 * once we return.
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;
615 efi_status_t status;
617 if (down_interruptible(&efivars_lock))
618 return -EINTR;
620 if (!__efivars) {
621 up(&efivars_lock);
622 return -EINVAL;
624 ops = __efivars->ops;
625 status = ops->set_variable(entry->var.VariableName,
626 &entry->var.VendorGuid,
627 0, 0, NULL);
628 if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
629 up(&efivars_lock);
630 return efi_status_to_err(status);
633 efivar_entry_list_del_unlock(entry);
634 return 0;
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;
663 efi_status_t status;
664 efi_char16_t *name = entry->var.VariableName;
665 efi_guid_t vendor = entry->var.VendorGuid;
667 if (down_interruptible(&efivars_lock))
668 return -EINTR;
670 if (!__efivars) {
671 up(&efivars_lock);
672 return -EINVAL;
674 ops = __efivars->ops;
675 if (head && efivar_entry_find(name, vendor, head, false)) {
676 up(&efivars_lock);
677 return -EEXIST;
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);
685 up(&efivars_lock);
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.
701 static int
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;
706 efi_status_t status;
708 if (down_trylock(&efivars_lock))
709 return -EBUSY;
711 if (!__efivars) {
712 up(&efivars_lock);
713 return -EINVAL;
716 status = check_var_size_nonblocking(attributes,
717 size + ucs2_strsize(name, 1024));
718 if (status != EFI_SUCCESS) {
719 up(&efivars_lock);
720 return -ENOSPC;
723 ops = __efivars->ops;
724 status = ops->set_variable_nonblocking(name, &vendor, attributes,
725 size, data);
727 up(&efivars_lock);
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;
752 efi_status_t status;
754 if (!__efivars)
755 return -EINVAL;
757 ops = __efivars->ops;
758 if (!ops->query_variable_store)
759 return -ENOSYS;
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()
766 * will block.
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,
773 size, data);
775 if (!block) {
776 if (down_trylock(&efivars_lock))
777 return -EBUSY;
778 } else {
779 if (down_interruptible(&efivars_lock))
780 return -EINTR;
783 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
784 if (status != EFI_SUCCESS) {
785 up(&efivars_lock);
786 return -ENOSPC;
789 status = ops->set_variable(name, &vendor, attributes, size, data);
791 up(&efivars_lock);
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;
819 bool found = false;
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)) {
827 found = true;
828 break;
832 if (!found)
833 return NULL;
835 if (remove) {
836 if (entry->scanning) {
838 * The entry will be deleted
839 * after scanning is completed.
841 entry->deleting = true;
842 } else
843 list_del(&entry->list);
846 return entry;
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;
858 efi_status_t status;
860 *size = 0;
862 if (down_interruptible(&efivars_lock))
863 return -EINTR;
864 if (!__efivars) {
865 up(&efivars_lock);
866 return -EINVAL;
868 ops = __efivars->ops;
869 status = ops->get_variable(entry->var.VariableName,
870 &entry->var.VendorGuid, NULL, size, NULL);
871 up(&efivars_lock);
873 if (status != EFI_BUFFER_TOO_SMALL)
874 return efi_status_to_err(status);
876 return 0;
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)
894 efi_status_t status;
896 if (!__efivars)
897 return -EINVAL;
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)
917 efi_status_t status;
919 if (down_interruptible(&efivars_lock))
920 return -EINTR;
922 if (!__efivars) {
923 up(&efivars_lock);
924 return -EINVAL;
927 status = __efivars->ops->get_variable(entry->var.VariableName,
928 &entry->var.VendorGuid,
929 attributes, size, data);
930 up(&efivars_lock);
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;
964 efi_status_t status;
965 int err;
967 *set = false;
969 if (efivar_validate(*vendor, name, data, *size) == false)
970 return -EINVAL;
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))
978 return -EINTR;
980 if (!__efivars) {
981 err = -EINVAL;
982 goto out;
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);
992 goto out;
995 if (*size > 65536) {
996 err = -ENOSPC;
997 goto out;
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);
1006 goto out;
1009 *set = true;
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
1015 * happened.
1017 *size = 0;
1018 status = ops->get_variable(entry->var.VariableName,
1019 &entry->var.VendorGuid,
1020 NULL, size, NULL);
1022 if (status == EFI_NOT_FOUND)
1023 efivar_entry_list_del_unlock(entry);
1024 else
1025 up(&efivars_lock);
1027 if (status && status != EFI_BUFFER_TOO_SMALL)
1028 return efi_status_to_err(status);
1030 return 0;
1032 out:
1033 up(&efivars_lock);
1034 return err;
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)
1059 up(&efivars_lock);
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;
1090 int err = 0;
1092 if (!prev || !*prev) {
1093 list_for_each_entry_safe(entry, n, head, list) {
1094 err = func(entry, data);
1095 if (err)
1096 break;
1099 if (prev)
1100 *prev = entry;
1102 return err;
1106 list_for_each_entry_safe_continue((*prev), n, head, list) {
1107 err = func(*prev, data);
1108 if (err)
1109 break;
1112 return err;
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)
1133 int err = 0;
1135 err = efivar_entry_iter_begin();
1136 if (err)
1137 return err;
1138 err = __efivar_entry_iter(func, head, data, NULL);
1139 efivar_entry_iter_end();
1141 return err;
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)
1153 if (!__efivars)
1154 return NULL;
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))
1183 return -EINTR;
1185 efivars->ops = ops;
1186 efivars->kobject = kobject;
1188 __efivars = efivars;
1190 pr_info("Registered efivars operations\n");
1192 up(&efivars_lock);
1194 return 0;
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)
1207 int rv;
1209 if (down_interruptible(&efivars_lock))
1210 return -EINTR;
1212 if (!__efivars) {
1213 printk(KERN_ERR "efivars not registered\n");
1214 rv = -EINVAL;
1215 goto out;
1218 if (__efivars != efivars) {
1219 rv = -EINVAL;
1220 goto out;
1223 pr_info("Unregistered efivars operations\n");
1224 __efivars = NULL;
1226 rv = 0;
1227 out:
1228 up(&efivars_lock);
1229 return rv;
1231 EXPORT_SYMBOL_GPL(efivars_unregister);