WIP FPC-III support
[linux/fpc-iii.git] / drivers / firmware / efi / vars.c
blob41c1d00bf933c682d45144880c0ba1099057603b
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
36 validate_device_path(efi_char16_t *var_name, int match, u8 *buffer,
37 unsigned long len)
39 struct efi_generic_dev_path *node;
40 int offset = 0;
42 node = (struct efi_generic_dev_path *)buffer;
44 if (len < sizeof(*node))
45 return false;
47 while (offset <= len - sizeof(*node) &&
48 node->length >= sizeof(*node) &&
49 node->length <= len - offset) {
50 offset += node->length;
52 if ((node->type == EFI_DEV_END_PATH ||
53 node->type == EFI_DEV_END_PATH2) &&
54 node->sub_type == EFI_DEV_END_ENTIRE)
55 return true;
57 node = (struct efi_generic_dev_path *)(buffer + offset);
61 * If we're here then either node->length pointed past the end
62 * of the buffer or we reached the end of the buffer without
63 * finding a device path end node.
65 return false;
68 static bool
69 validate_boot_order(efi_char16_t *var_name, int match, u8 *buffer,
70 unsigned long len)
72 /* An array of 16-bit integers */
73 if ((len % 2) != 0)
74 return false;
76 return true;
79 static bool
80 validate_load_option(efi_char16_t *var_name, int match, u8 *buffer,
81 unsigned long len)
83 u16 filepathlength;
84 int i, desclength = 0, namelen;
86 namelen = ucs2_strnlen(var_name, EFI_VAR_NAME_LEN);
88 /* Either "Boot" or "Driver" followed by four digits of hex */
89 for (i = match; i < match+4; i++) {
90 if (var_name[i] > 127 ||
91 hex_to_bin(var_name[i] & 0xff) < 0)
92 return true;
95 /* Reject it if there's 4 digits of hex and then further content */
96 if (namelen > match + 4)
97 return false;
99 /* A valid entry must be at least 8 bytes */
100 if (len < 8)
101 return false;
103 filepathlength = buffer[4] | buffer[5] << 8;
106 * There's no stored length for the description, so it has to be
107 * found by hand
109 desclength = ucs2_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
111 /* Each boot entry must have a descriptor */
112 if (!desclength)
113 return false;
116 * If the sum of the length of the description, the claimed filepath
117 * length and the original header are greater than the length of the
118 * variable, it's malformed
120 if ((desclength + filepathlength + 6) > len)
121 return false;
124 * And, finally, check the filepath
126 return validate_device_path(var_name, match, buffer + desclength + 6,
127 filepathlength);
130 static bool
131 validate_uint16(efi_char16_t *var_name, int match, u8 *buffer,
132 unsigned long len)
134 /* A single 16-bit integer */
135 if (len != 2)
136 return false;
138 return true;
141 static bool
142 validate_ascii_string(efi_char16_t *var_name, int match, u8 *buffer,
143 unsigned long len)
145 int i;
147 for (i = 0; i < len; i++) {
148 if (buffer[i] > 127)
149 return false;
151 if (buffer[i] == 0)
152 return true;
155 return false;
158 struct variable_validate {
159 efi_guid_t vendor;
160 char *name;
161 bool (*validate)(efi_char16_t *var_name, int match, u8 *data,
162 unsigned long len);
166 * This is the list of variables we need to validate, as well as the
167 * whitelist for what we think is safe not to default to immutable.
169 * If it has a validate() method that's not NULL, it'll go into the
170 * validation routine. If not, it is assumed valid, but still used for
171 * whitelisting.
173 * Note that it's sorted by {vendor,name}, but globbed names must come after
174 * any other name with the same prefix.
176 static const struct variable_validate variable_validate[] = {
177 { EFI_GLOBAL_VARIABLE_GUID, "BootNext", validate_uint16 },
178 { EFI_GLOBAL_VARIABLE_GUID, "BootOrder", validate_boot_order },
179 { EFI_GLOBAL_VARIABLE_GUID, "Boot*", validate_load_option },
180 { EFI_GLOBAL_VARIABLE_GUID, "DriverOrder", validate_boot_order },
181 { EFI_GLOBAL_VARIABLE_GUID, "Driver*", validate_load_option },
182 { EFI_GLOBAL_VARIABLE_GUID, "ConIn", validate_device_path },
183 { EFI_GLOBAL_VARIABLE_GUID, "ConInDev", validate_device_path },
184 { EFI_GLOBAL_VARIABLE_GUID, "ConOut", validate_device_path },
185 { EFI_GLOBAL_VARIABLE_GUID, "ConOutDev", validate_device_path },
186 { EFI_GLOBAL_VARIABLE_GUID, "ErrOut", validate_device_path },
187 { EFI_GLOBAL_VARIABLE_GUID, "ErrOutDev", validate_device_path },
188 { EFI_GLOBAL_VARIABLE_GUID, "Lang", validate_ascii_string },
189 { EFI_GLOBAL_VARIABLE_GUID, "OsIndications", NULL },
190 { EFI_GLOBAL_VARIABLE_GUID, "PlatformLang", validate_ascii_string },
191 { EFI_GLOBAL_VARIABLE_GUID, "Timeout", validate_uint16 },
192 { LINUX_EFI_CRASH_GUID, "*", NULL },
193 { NULL_GUID, "", NULL },
197 * Check if @var_name matches the pattern given in @match_name.
199 * @var_name: an array of @len non-NUL characters.
200 * @match_name: a NUL-terminated pattern string, optionally ending in "*". A
201 * final "*" character matches any trailing characters @var_name,
202 * including the case when there are none left in @var_name.
203 * @match: on output, the number of non-wildcard characters in @match_name
204 * that @var_name matches, regardless of the return value.
205 * @return: whether @var_name fully matches @match_name.
207 static bool
208 variable_matches(const char *var_name, size_t len, const char *match_name,
209 int *match)
211 for (*match = 0; ; (*match)++) {
212 char c = match_name[*match];
214 switch (c) {
215 case '*':
216 /* Wildcard in @match_name means we've matched. */
217 return true;
219 case '\0':
220 /* @match_name has ended. Has @var_name too? */
221 return (*match == len);
223 default:
225 * We've reached a non-wildcard char in @match_name.
226 * Continue only if there's an identical character in
227 * @var_name.
229 if (*match < len && c == var_name[*match])
230 continue;
231 return false;
236 bool
237 efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data,
238 unsigned long data_size)
240 int i;
241 unsigned long utf8_size;
242 u8 *utf8_name;
244 utf8_size = ucs2_utf8size(var_name);
245 utf8_name = kmalloc(utf8_size + 1, GFP_KERNEL);
246 if (!utf8_name)
247 return false;
249 ucs2_as_utf8(utf8_name, var_name, utf8_size);
250 utf8_name[utf8_size] = '\0';
252 for (i = 0; variable_validate[i].name[0] != '\0'; i++) {
253 const char *name = variable_validate[i].name;
254 int match = 0;
256 if (efi_guidcmp(vendor, variable_validate[i].vendor))
257 continue;
259 if (variable_matches(utf8_name, utf8_size+1, name, &match)) {
260 if (variable_validate[i].validate == NULL)
261 break;
262 kfree(utf8_name);
263 return variable_validate[i].validate(var_name, match,
264 data, data_size);
267 kfree(utf8_name);
268 return true;
270 EXPORT_SYMBOL_GPL(efivar_validate);
272 bool
273 efivar_variable_is_removable(efi_guid_t vendor, const char *var_name,
274 size_t len)
276 int i;
277 bool found = false;
278 int match = 0;
281 * Check if our variable is in the validated variables list
283 for (i = 0; variable_validate[i].name[0] != '\0'; i++) {
284 if (efi_guidcmp(variable_validate[i].vendor, vendor))
285 continue;
287 if (variable_matches(var_name, len,
288 variable_validate[i].name, &match)) {
289 found = true;
290 break;
295 * If it's in our list, it is removable.
297 return found;
299 EXPORT_SYMBOL_GPL(efivar_variable_is_removable);
301 static efi_status_t
302 check_var_size(u32 attributes, unsigned long size)
304 const struct efivar_operations *fops;
306 if (!__efivars)
307 return EFI_UNSUPPORTED;
309 fops = __efivars->ops;
311 if (!fops->query_variable_store)
312 return EFI_UNSUPPORTED;
314 return fops->query_variable_store(attributes, size, false);
317 static efi_status_t
318 check_var_size_nonblocking(u32 attributes, unsigned long size)
320 const struct efivar_operations *fops;
322 if (!__efivars)
323 return EFI_UNSUPPORTED;
325 fops = __efivars->ops;
327 if (!fops->query_variable_store)
328 return EFI_UNSUPPORTED;
330 return fops->query_variable_store(attributes, size, true);
333 static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor,
334 struct list_head *head)
336 struct efivar_entry *entry, *n;
337 unsigned long strsize1, strsize2;
338 bool found = false;
340 strsize1 = ucs2_strsize(variable_name, 1024);
341 list_for_each_entry_safe(entry, n, head, list) {
342 strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
343 if (strsize1 == strsize2 &&
344 !memcmp(variable_name, &(entry->var.VariableName),
345 strsize2) &&
346 !efi_guidcmp(entry->var.VendorGuid,
347 *vendor)) {
348 found = true;
349 break;
352 return found;
356 * Returns the size of variable_name, in bytes, including the
357 * terminating NULL character, or variable_name_size if no NULL
358 * character is found among the first variable_name_size bytes.
360 static unsigned long var_name_strnsize(efi_char16_t *variable_name,
361 unsigned long variable_name_size)
363 unsigned long len;
364 efi_char16_t c;
367 * The variable name is, by definition, a NULL-terminated
368 * string, so make absolutely sure that variable_name_size is
369 * the value we expect it to be. If not, return the real size.
371 for (len = 2; len <= variable_name_size; len += sizeof(c)) {
372 c = variable_name[(len / sizeof(c)) - 1];
373 if (!c)
374 break;
377 return min(len, variable_name_size);
381 * Print a warning when duplicate EFI variables are encountered and
382 * disable the sysfs workqueue since the firmware is buggy.
384 static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid,
385 unsigned long len16)
387 size_t i, len8 = len16 / sizeof(efi_char16_t);
388 char *str8;
390 str8 = kzalloc(len8, GFP_KERNEL);
391 if (!str8)
392 return;
394 for (i = 0; i < len8; i++)
395 str8[i] = str16[i];
397 printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
398 str8, vendor_guid);
399 kfree(str8);
403 * efivar_init - build the initial list of EFI variables
404 * @func: callback function to invoke for every variable
405 * @data: function-specific data to pass to @func
406 * @duplicates: error if we encounter duplicates on @head?
407 * @head: initialised head of variable list
409 * Get every EFI variable from the firmware and invoke @func. @func
410 * should call efivar_entry_add() to build the list of variables.
412 * Returns 0 on success, or a kernel error code on failure.
414 int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
415 void *data, bool duplicates, struct list_head *head)
417 const struct efivar_operations *ops;
418 unsigned long variable_name_size = 1024;
419 efi_char16_t *variable_name;
420 efi_status_t status;
421 efi_guid_t vendor_guid;
422 int err = 0;
424 if (!__efivars)
425 return -EFAULT;
427 ops = __efivars->ops;
429 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
430 if (!variable_name) {
431 printk(KERN_ERR "efivars: Memory allocation failed.\n");
432 return -ENOMEM;
435 if (down_interruptible(&efivars_lock)) {
436 err = -EINTR;
437 goto free;
441 * Per EFI spec, the maximum storage allocated for both
442 * the variable name and variable data is 1024 bytes.
445 do {
446 variable_name_size = 1024;
448 status = ops->get_next_variable(&variable_name_size,
449 variable_name,
450 &vendor_guid);
451 switch (status) {
452 case EFI_SUCCESS:
453 if (duplicates)
454 up(&efivars_lock);
456 variable_name_size = var_name_strnsize(variable_name,
457 variable_name_size);
460 * Some firmware implementations return the
461 * same variable name on multiple calls to
462 * get_next_variable(). Terminate the loop
463 * immediately as there is no guarantee that
464 * we'll ever see a different variable name,
465 * and may end up looping here forever.
467 if (duplicates &&
468 variable_is_present(variable_name, &vendor_guid,
469 head)) {
470 dup_variable_bug(variable_name, &vendor_guid,
471 variable_name_size);
472 status = EFI_NOT_FOUND;
473 } else {
474 err = func(variable_name, vendor_guid,
475 variable_name_size, data);
476 if (err)
477 status = EFI_NOT_FOUND;
480 if (duplicates) {
481 if (down_interruptible(&efivars_lock)) {
482 err = -EINTR;
483 goto free;
487 break;
488 case EFI_NOT_FOUND:
489 break;
490 default:
491 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
492 status);
493 status = EFI_NOT_FOUND;
494 break;
497 } while (status != EFI_NOT_FOUND);
499 up(&efivars_lock);
500 free:
501 kfree(variable_name);
503 return err;
505 EXPORT_SYMBOL_GPL(efivar_init);
508 * efivar_entry_add - add entry to variable list
509 * @entry: entry to add to list
510 * @head: list head
512 * Returns 0 on success, or a kernel error code on failure.
514 int efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
516 if (down_interruptible(&efivars_lock))
517 return -EINTR;
518 list_add(&entry->list, head);
519 up(&efivars_lock);
521 return 0;
523 EXPORT_SYMBOL_GPL(efivar_entry_add);
526 * efivar_entry_remove - remove entry from variable list
527 * @entry: entry to remove from list
529 * Returns 0 on success, or a kernel error code on failure.
531 int efivar_entry_remove(struct efivar_entry *entry)
533 if (down_interruptible(&efivars_lock))
534 return -EINTR;
535 list_del(&entry->list);
536 up(&efivars_lock);
538 return 0;
540 EXPORT_SYMBOL_GPL(efivar_entry_remove);
543 * efivar_entry_list_del_unlock - remove entry from variable list
544 * @entry: entry to remove
546 * Remove @entry from the variable list and release the list lock.
548 * NOTE: slightly weird locking semantics here - we expect to be
549 * called with the efivars lock already held, and we release it before
550 * returning. This is because this function is usually called after
551 * set_variable() while the lock is still held.
553 static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
555 list_del(&entry->list);
556 up(&efivars_lock);
560 * __efivar_entry_delete - delete an EFI variable
561 * @entry: entry containing EFI variable to delete
563 * Delete the variable from the firmware but leave @entry on the
564 * variable list.
566 * This function differs from efivar_entry_delete() because it does
567 * not remove @entry from the variable list. Also, it is safe to be
568 * called from within a efivar_entry_iter_begin() and
569 * efivar_entry_iter_end() region, unlike efivar_entry_delete().
571 * Returns 0 on success, or a converted EFI status code if
572 * set_variable() fails.
574 int __efivar_entry_delete(struct efivar_entry *entry)
576 efi_status_t status;
578 if (!__efivars)
579 return -EINVAL;
581 status = __efivars->ops->set_variable(entry->var.VariableName,
582 &entry->var.VendorGuid,
583 0, 0, NULL);
585 return efi_status_to_err(status);
587 EXPORT_SYMBOL_GPL(__efivar_entry_delete);
590 * efivar_entry_delete - delete variable and remove entry from list
591 * @entry: entry containing variable to delete
593 * Delete the variable from the firmware and remove @entry from the
594 * variable list. It is the caller's responsibility to free @entry
595 * once we return.
597 * Returns 0 on success, -EINTR if we can't grab the semaphore,
598 * converted EFI status code if set_variable() fails.
600 int efivar_entry_delete(struct efivar_entry *entry)
602 const struct efivar_operations *ops;
603 efi_status_t status;
605 if (down_interruptible(&efivars_lock))
606 return -EINTR;
608 if (!__efivars) {
609 up(&efivars_lock);
610 return -EINVAL;
612 ops = __efivars->ops;
613 status = ops->set_variable(entry->var.VariableName,
614 &entry->var.VendorGuid,
615 0, 0, NULL);
616 if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
617 up(&efivars_lock);
618 return efi_status_to_err(status);
621 efivar_entry_list_del_unlock(entry);
622 return 0;
624 EXPORT_SYMBOL_GPL(efivar_entry_delete);
627 * efivar_entry_set - call set_variable()
628 * @entry: entry containing the EFI variable to write
629 * @attributes: variable attributes
630 * @size: size of @data buffer
631 * @data: buffer containing variable data
632 * @head: head of variable list
634 * Calls set_variable() for an EFI variable. If creating a new EFI
635 * variable, this function is usually followed by efivar_entry_add().
637 * Before writing the variable, the remaining EFI variable storage
638 * space is checked to ensure there is enough room available.
640 * If @head is not NULL a lookup is performed to determine whether
641 * the entry is already on the list.
643 * Returns 0 on success, -EINTR if we can't grab the semaphore,
644 * -EEXIST if a lookup is performed and the entry already exists on
645 * the list, or a converted EFI status code if set_variable() fails.
647 int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
648 unsigned long size, void *data, struct list_head *head)
650 const struct efivar_operations *ops;
651 efi_status_t status;
652 efi_char16_t *name = entry->var.VariableName;
653 efi_guid_t vendor = entry->var.VendorGuid;
655 if (down_interruptible(&efivars_lock))
656 return -EINTR;
658 if (!__efivars) {
659 up(&efivars_lock);
660 return -EINVAL;
662 ops = __efivars->ops;
663 if (head && efivar_entry_find(name, vendor, head, false)) {
664 up(&efivars_lock);
665 return -EEXIST;
668 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
669 if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
670 status = ops->set_variable(name, &vendor,
671 attributes, size, data);
673 up(&efivars_lock);
675 return efi_status_to_err(status);
678 EXPORT_SYMBOL_GPL(efivar_entry_set);
681 * efivar_entry_set_nonblocking - call set_variable_nonblocking()
683 * This function is guaranteed to not block and is suitable for calling
684 * from crash/panic handlers.
686 * Crucially, this function will not block if it cannot acquire
687 * efivars_lock. Instead, it returns -EBUSY.
689 static int
690 efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
691 u32 attributes, unsigned long size, void *data)
693 const struct efivar_operations *ops;
694 efi_status_t status;
696 if (down_trylock(&efivars_lock))
697 return -EBUSY;
699 if (!__efivars) {
700 up(&efivars_lock);
701 return -EINVAL;
704 status = check_var_size_nonblocking(attributes,
705 size + ucs2_strsize(name, 1024));
706 if (status != EFI_SUCCESS) {
707 up(&efivars_lock);
708 return -ENOSPC;
711 ops = __efivars->ops;
712 status = ops->set_variable_nonblocking(name, &vendor, attributes,
713 size, data);
715 up(&efivars_lock);
716 return efi_status_to_err(status);
720 * efivar_entry_set_safe - call set_variable() if enough space in firmware
721 * @name: buffer containing the variable name
722 * @vendor: variable vendor guid
723 * @attributes: variable attributes
724 * @block: can we block in this context?
725 * @size: size of @data buffer
726 * @data: buffer containing variable data
728 * Ensures there is enough free storage in the firmware for this variable, and
729 * if so, calls set_variable(). If creating a new EFI variable, this function
730 * is usually followed by efivar_entry_add().
732 * Returns 0 on success, -ENOSPC if the firmware does not have enough
733 * space for set_variable() to succeed, or a converted EFI status code
734 * if set_variable() fails.
736 int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
737 bool block, unsigned long size, void *data)
739 const struct efivar_operations *ops;
740 efi_status_t status;
742 if (!__efivars)
743 return -EINVAL;
745 ops = __efivars->ops;
746 if (!ops->query_variable_store)
747 return -ENOSYS;
750 * If the EFI variable backend provides a non-blocking
751 * ->set_variable() operation and we're in a context where we
752 * cannot block, then we need to use it to avoid live-locks,
753 * since the implication is that the regular ->set_variable()
754 * will block.
756 * If no ->set_variable_nonblocking() is provided then
757 * ->set_variable() is assumed to be non-blocking.
759 if (!block && ops->set_variable_nonblocking)
760 return efivar_entry_set_nonblocking(name, vendor, attributes,
761 size, data);
763 if (!block) {
764 if (down_trylock(&efivars_lock))
765 return -EBUSY;
766 } else {
767 if (down_interruptible(&efivars_lock))
768 return -EINTR;
771 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
772 if (status != EFI_SUCCESS) {
773 up(&efivars_lock);
774 return -ENOSPC;
777 status = ops->set_variable(name, &vendor, attributes, size, data);
779 up(&efivars_lock);
781 return efi_status_to_err(status);
783 EXPORT_SYMBOL_GPL(efivar_entry_set_safe);
786 * efivar_entry_find - search for an entry
787 * @name: the EFI variable name
788 * @guid: the EFI variable vendor's guid
789 * @head: head of the variable list
790 * @remove: should we remove the entry from the list?
792 * Search for an entry on the variable list that has the EFI variable
793 * name @name and vendor guid @guid. If an entry is found on the list
794 * and @remove is true, the entry is removed from the list.
796 * The caller MUST call efivar_entry_iter_begin() and
797 * efivar_entry_iter_end() before and after the invocation of this
798 * function, respectively.
800 * Returns the entry if found on the list, %NULL otherwise.
802 struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
803 struct list_head *head, bool remove)
805 struct efivar_entry *entry, *n;
806 int strsize1, strsize2;
807 bool found = false;
809 list_for_each_entry_safe(entry, n, head, list) {
810 strsize1 = ucs2_strsize(name, 1024);
811 strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
812 if (strsize1 == strsize2 &&
813 !memcmp(name, &(entry->var.VariableName), strsize1) &&
814 !efi_guidcmp(guid, entry->var.VendorGuid)) {
815 found = true;
816 break;
820 if (!found)
821 return NULL;
823 if (remove) {
824 if (entry->scanning) {
826 * The entry will be deleted
827 * after scanning is completed.
829 entry->deleting = true;
830 } else
831 list_del(&entry->list);
834 return entry;
836 EXPORT_SYMBOL_GPL(efivar_entry_find);
839 * efivar_entry_size - obtain the size of a variable
840 * @entry: entry for this variable
841 * @size: location to store the variable's size
843 int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
845 const struct efivar_operations *ops;
846 efi_status_t status;
848 *size = 0;
850 if (down_interruptible(&efivars_lock))
851 return -EINTR;
852 if (!__efivars) {
853 up(&efivars_lock);
854 return -EINVAL;
856 ops = __efivars->ops;
857 status = ops->get_variable(entry->var.VariableName,
858 &entry->var.VendorGuid, NULL, size, NULL);
859 up(&efivars_lock);
861 if (status != EFI_BUFFER_TOO_SMALL)
862 return efi_status_to_err(status);
864 return 0;
866 EXPORT_SYMBOL_GPL(efivar_entry_size);
869 * __efivar_entry_get - call get_variable()
870 * @entry: read data for this variable
871 * @attributes: variable attributes
872 * @size: size of @data buffer
873 * @data: buffer to store variable data
875 * The caller MUST call efivar_entry_iter_begin() and
876 * efivar_entry_iter_end() before and after the invocation of this
877 * function, respectively.
879 int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
880 unsigned long *size, void *data)
882 efi_status_t status;
884 if (!__efivars)
885 return -EINVAL;
887 status = __efivars->ops->get_variable(entry->var.VariableName,
888 &entry->var.VendorGuid,
889 attributes, size, data);
891 return efi_status_to_err(status);
893 EXPORT_SYMBOL_GPL(__efivar_entry_get);
896 * efivar_entry_get - call get_variable()
897 * @entry: read data for this variable
898 * @attributes: variable attributes
899 * @size: size of @data buffer
900 * @data: buffer to store variable data
902 int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
903 unsigned long *size, void *data)
905 efi_status_t status;
907 if (down_interruptible(&efivars_lock))
908 return -EINTR;
910 if (!__efivars) {
911 up(&efivars_lock);
912 return -EINVAL;
915 status = __efivars->ops->get_variable(entry->var.VariableName,
916 &entry->var.VendorGuid,
917 attributes, size, data);
918 up(&efivars_lock);
920 return efi_status_to_err(status);
922 EXPORT_SYMBOL_GPL(efivar_entry_get);
925 * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
926 * @entry: entry containing variable to set and get
927 * @attributes: attributes of variable to be written
928 * @size: size of data buffer
929 * @data: buffer containing data to write
930 * @set: did the set_variable() call succeed?
932 * This is a pretty special (complex) function. See efivarfs_file_write().
934 * Atomically call set_variable() for @entry and if the call is
935 * successful, return the new size of the variable from get_variable()
936 * in @size. The success of set_variable() is indicated by @set.
938 * Returns 0 on success, -EINVAL if the variable data is invalid,
939 * -ENOSPC if the firmware does not have enough available space, or a
940 * converted EFI status code if either of set_variable() or
941 * get_variable() fail.
943 * If the EFI variable does not exist when calling set_variable()
944 * (EFI_NOT_FOUND), @entry is removed from the variable list.
946 int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
947 unsigned long *size, void *data, bool *set)
949 const struct efivar_operations *ops;
950 efi_char16_t *name = entry->var.VariableName;
951 efi_guid_t *vendor = &entry->var.VendorGuid;
952 efi_status_t status;
953 int err;
955 *set = false;
957 if (efivar_validate(*vendor, name, data, *size) == false)
958 return -EINVAL;
961 * The lock here protects the get_variable call, the conditional
962 * set_variable call, and removal of the variable from the efivars
963 * list (in the case of an authenticated delete).
965 if (down_interruptible(&efivars_lock))
966 return -EINTR;
968 if (!__efivars) {
969 err = -EINVAL;
970 goto out;
974 * Ensure that the available space hasn't shrunk below the safe level
976 status = check_var_size(attributes, *size + ucs2_strsize(name, 1024));
977 if (status != EFI_SUCCESS) {
978 if (status != EFI_UNSUPPORTED) {
979 err = efi_status_to_err(status);
980 goto out;
983 if (*size > 65536) {
984 err = -ENOSPC;
985 goto out;
989 ops = __efivars->ops;
991 status = ops->set_variable(name, vendor, attributes, *size, data);
992 if (status != EFI_SUCCESS) {
993 err = efi_status_to_err(status);
994 goto out;
997 *set = true;
1000 * Writing to the variable may have caused a change in size (which
1001 * could either be an append or an overwrite), or the variable to be
1002 * deleted. Perform a GetVariable() so we can tell what actually
1003 * happened.
1005 *size = 0;
1006 status = ops->get_variable(entry->var.VariableName,
1007 &entry->var.VendorGuid,
1008 NULL, size, NULL);
1010 if (status == EFI_NOT_FOUND)
1011 efivar_entry_list_del_unlock(entry);
1012 else
1013 up(&efivars_lock);
1015 if (status && status != EFI_BUFFER_TOO_SMALL)
1016 return efi_status_to_err(status);
1018 return 0;
1020 out:
1021 up(&efivars_lock);
1022 return err;
1025 EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
1028 * efivar_entry_iter_begin - begin iterating the variable list
1030 * Lock the variable list to prevent entry insertion and removal until
1031 * efivar_entry_iter_end() is called. This function is usually used in
1032 * conjunction with __efivar_entry_iter() or efivar_entry_iter().
1034 int efivar_entry_iter_begin(void)
1036 return down_interruptible(&efivars_lock);
1038 EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
1041 * efivar_entry_iter_end - finish iterating the variable list
1043 * Unlock the variable list and allow modifications to the list again.
1045 void efivar_entry_iter_end(void)
1047 up(&efivars_lock);
1049 EXPORT_SYMBOL_GPL(efivar_entry_iter_end);
1052 * __efivar_entry_iter - iterate over variable list
1053 * @func: callback function
1054 * @head: head of the variable list
1055 * @data: function-specific data to pass to callback
1056 * @prev: entry to begin iterating from
1058 * Iterate over the list of EFI variables and call @func with every
1059 * entry on the list. It is safe for @func to remove entries in the
1060 * list via efivar_entry_delete().
1062 * You MUST call efivar_entry_iter_begin() before this function, and
1063 * efivar_entry_iter_end() afterwards.
1065 * It is possible to begin iteration from an arbitrary entry within
1066 * the list by passing @prev. @prev is updated on return to point to
1067 * the last entry passed to @func. To begin iterating from the
1068 * beginning of the list @prev must be %NULL.
1070 * The restrictions for @func are the same as documented for
1071 * efivar_entry_iter().
1073 int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
1074 struct list_head *head, void *data,
1075 struct efivar_entry **prev)
1077 struct efivar_entry *entry, *n;
1078 int err = 0;
1080 if (!prev || !*prev) {
1081 list_for_each_entry_safe(entry, n, head, list) {
1082 err = func(entry, data);
1083 if (err)
1084 break;
1087 if (prev)
1088 *prev = entry;
1090 return err;
1094 list_for_each_entry_safe_continue((*prev), n, head, list) {
1095 err = func(*prev, data);
1096 if (err)
1097 break;
1100 return err;
1102 EXPORT_SYMBOL_GPL(__efivar_entry_iter);
1105 * efivar_entry_iter - iterate over variable list
1106 * @func: callback function
1107 * @head: head of variable list
1108 * @data: function-specific data to pass to callback
1110 * Iterate over the list of EFI variables and call @func with every
1111 * entry on the list. It is safe for @func to remove entries in the
1112 * list via efivar_entry_delete() while iterating.
1114 * Some notes for the callback function:
1115 * - a non-zero return value indicates an error and terminates the loop
1116 * - @func is called from atomic context
1118 int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
1119 struct list_head *head, void *data)
1121 int err = 0;
1123 err = efivar_entry_iter_begin();
1124 if (err)
1125 return err;
1126 err = __efivar_entry_iter(func, head, data, NULL);
1127 efivar_entry_iter_end();
1129 return err;
1131 EXPORT_SYMBOL_GPL(efivar_entry_iter);
1134 * efivars_kobject - get the kobject for the registered efivars
1136 * If efivars_register() has not been called we return NULL,
1137 * otherwise return the kobject used at registration time.
1139 struct kobject *efivars_kobject(void)
1141 if (!__efivars)
1142 return NULL;
1144 return __efivars->kobject;
1146 EXPORT_SYMBOL_GPL(efivars_kobject);
1149 * efivars_register - register an efivars
1150 * @efivars: efivars to register
1151 * @ops: efivars operations
1152 * @kobject: @efivars-specific kobject
1154 * Only a single efivars can be registered at any time.
1156 int efivars_register(struct efivars *efivars,
1157 const struct efivar_operations *ops,
1158 struct kobject *kobject)
1160 if (down_interruptible(&efivars_lock))
1161 return -EINTR;
1163 efivars->ops = ops;
1164 efivars->kobject = kobject;
1166 __efivars = efivars;
1168 pr_info("Registered efivars operations\n");
1170 up(&efivars_lock);
1172 return 0;
1174 EXPORT_SYMBOL_GPL(efivars_register);
1177 * efivars_unregister - unregister an efivars
1178 * @efivars: efivars to unregister
1180 * The caller must have already removed every entry from the list,
1181 * failure to do so is an error.
1183 int efivars_unregister(struct efivars *efivars)
1185 int rv;
1187 if (down_interruptible(&efivars_lock))
1188 return -EINTR;
1190 if (!__efivars) {
1191 printk(KERN_ERR "efivars not registered\n");
1192 rv = -EINVAL;
1193 goto out;
1196 if (__efivars != efivars) {
1197 rv = -EINVAL;
1198 goto out;
1201 pr_info("Unregistered efivars operations\n");
1202 __efivars = NULL;
1204 rv = 0;
1205 out:
1206 up(&efivars_lock);
1207 return rv;
1209 EXPORT_SYMBOL_GPL(efivars_unregister);
1211 int efivar_supports_writes(void)
1213 return __efivars && __efivars->ops->set_variable;
1215 EXPORT_SYMBOL_GPL(efivar_supports_writes);