dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / firmware / efi / vars.c
blob6f182fd91a6d0dbf713f7b56c4449dda8b795b77
1 /*
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>
26 #include <linux/mm.h>
27 #include <linux/module.h>
28 #include <linux/string.h>
29 #include <linux/smp.h>
30 #include <linux/efi.h>
31 #include <linux/sysfs.h>
32 #include <linux/device.h>
33 #include <linux/slab.h>
34 #include <linux/ctype.h>
35 #include <linux/ucs2_string.h>
37 /* Private pointer to registered efivars */
38 static struct efivars *__efivars;
40 static bool efivar_wq_enabled = true;
41 DECLARE_WORK(efivar_work, NULL);
42 EXPORT_SYMBOL_GPL(efivar_work);
44 static bool
45 validate_device_path(efi_char16_t *var_name, int match, u8 *buffer,
46 unsigned long len)
48 struct efi_generic_dev_path *node;
49 int offset = 0;
51 node = (struct efi_generic_dev_path *)buffer;
53 if (len < sizeof(*node))
54 return false;
56 while (offset <= len - sizeof(*node) &&
57 node->length >= sizeof(*node) &&
58 node->length <= len - offset) {
59 offset += node->length;
61 if ((node->type == EFI_DEV_END_PATH ||
62 node->type == EFI_DEV_END_PATH2) &&
63 node->sub_type == EFI_DEV_END_ENTIRE)
64 return true;
66 node = (struct efi_generic_dev_path *)(buffer + offset);
70 * If we're here then either node->length pointed past the end
71 * of the buffer or we reached the end of the buffer without
72 * finding a device path end node.
74 return false;
77 static bool
78 validate_boot_order(efi_char16_t *var_name, int match, u8 *buffer,
79 unsigned long len)
81 /* An array of 16-bit integers */
82 if ((len % 2) != 0)
83 return false;
85 return true;
88 static bool
89 validate_load_option(efi_char16_t *var_name, int match, u8 *buffer,
90 unsigned long len)
92 u16 filepathlength;
93 int i, desclength = 0, namelen;
95 namelen = ucs2_strnlen(var_name, EFI_VAR_NAME_LEN);
97 /* Either "Boot" or "Driver" followed by four digits of hex */
98 for (i = match; i < match+4; i++) {
99 if (var_name[i] > 127 ||
100 hex_to_bin(var_name[i] & 0xff) < 0)
101 return true;
104 /* Reject it if there's 4 digits of hex and then further content */
105 if (namelen > match + 4)
106 return false;
108 /* A valid entry must be at least 8 bytes */
109 if (len < 8)
110 return false;
112 filepathlength = buffer[4] | buffer[5] << 8;
115 * There's no stored length for the description, so it has to be
116 * found by hand
118 desclength = ucs2_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
120 /* Each boot entry must have a descriptor */
121 if (!desclength)
122 return false;
125 * If the sum of the length of the description, the claimed filepath
126 * length and the original header are greater than the length of the
127 * variable, it's malformed
129 if ((desclength + filepathlength + 6) > len)
130 return false;
133 * And, finally, check the filepath
135 return validate_device_path(var_name, match, buffer + desclength + 6,
136 filepathlength);
139 static bool
140 validate_uint16(efi_char16_t *var_name, int match, u8 *buffer,
141 unsigned long len)
143 /* A single 16-bit integer */
144 if (len != 2)
145 return false;
147 return true;
150 static bool
151 validate_ascii_string(efi_char16_t *var_name, int match, u8 *buffer,
152 unsigned long len)
154 int i;
156 for (i = 0; i < len; i++) {
157 if (buffer[i] > 127)
158 return false;
160 if (buffer[i] == 0)
161 return true;
164 return false;
167 struct variable_validate {
168 efi_guid_t vendor;
169 char *name;
170 bool (*validate)(efi_char16_t *var_name, int match, u8 *data,
171 unsigned long len);
175 * This is the list of variables we need to validate, as well as the
176 * whitelist for what we think is safe not to default to immutable.
178 * If it has a validate() method that's not NULL, it'll go into the
179 * validation routine. If not, it is assumed valid, but still used for
180 * whitelisting.
182 * Note that it's sorted by {vendor,name}, but globbed names must come after
183 * any other name with the same prefix.
185 static const struct variable_validate variable_validate[] = {
186 { EFI_GLOBAL_VARIABLE_GUID, "BootNext", validate_uint16 },
187 { EFI_GLOBAL_VARIABLE_GUID, "BootOrder", validate_boot_order },
188 { EFI_GLOBAL_VARIABLE_GUID, "Boot*", validate_load_option },
189 { EFI_GLOBAL_VARIABLE_GUID, "DriverOrder", validate_boot_order },
190 { EFI_GLOBAL_VARIABLE_GUID, "Driver*", validate_load_option },
191 { EFI_GLOBAL_VARIABLE_GUID, "ConIn", validate_device_path },
192 { EFI_GLOBAL_VARIABLE_GUID, "ConInDev", validate_device_path },
193 { EFI_GLOBAL_VARIABLE_GUID, "ConOut", validate_device_path },
194 { EFI_GLOBAL_VARIABLE_GUID, "ConOutDev", validate_device_path },
195 { EFI_GLOBAL_VARIABLE_GUID, "ErrOut", validate_device_path },
196 { EFI_GLOBAL_VARIABLE_GUID, "ErrOutDev", validate_device_path },
197 { EFI_GLOBAL_VARIABLE_GUID, "Lang", validate_ascii_string },
198 { EFI_GLOBAL_VARIABLE_GUID, "OsIndications", NULL },
199 { EFI_GLOBAL_VARIABLE_GUID, "PlatformLang", validate_ascii_string },
200 { EFI_GLOBAL_VARIABLE_GUID, "Timeout", validate_uint16 },
201 { LINUX_EFI_CRASH_GUID, "*", NULL },
202 { NULL_GUID, "", NULL },
206 * Check if @var_name matches the pattern given in @match_name.
208 * @var_name: an array of @len non-NUL characters.
209 * @match_name: a NUL-terminated pattern string, optionally ending in "*". A
210 * final "*" character matches any trailing characters @var_name,
211 * including the case when there are none left in @var_name.
212 * @match: on output, the number of non-wildcard characters in @match_name
213 * that @var_name matches, regardless of the return value.
214 * @return: whether @var_name fully matches @match_name.
216 static bool
217 variable_matches(const char *var_name, size_t len, const char *match_name,
218 int *match)
220 for (*match = 0; ; (*match)++) {
221 char c = match_name[*match];
223 switch (c) {
224 case '*':
225 /* Wildcard in @match_name means we've matched. */
226 return true;
228 case '\0':
229 /* @match_name has ended. Has @var_name too? */
230 return (*match == len);
232 default:
234 * We've reached a non-wildcard char in @match_name.
235 * Continue only if there's an identical character in
236 * @var_name.
238 if (*match < len && c == var_name[*match])
239 continue;
240 return false;
245 bool
246 efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data,
247 unsigned long data_size)
249 int i;
250 unsigned long utf8_size;
251 u8 *utf8_name;
253 utf8_size = ucs2_utf8size(var_name);
254 utf8_name = kmalloc(utf8_size + 1, GFP_KERNEL);
255 if (!utf8_name)
256 return false;
258 ucs2_as_utf8(utf8_name, var_name, utf8_size);
259 utf8_name[utf8_size] = '\0';
261 for (i = 0; variable_validate[i].name[0] != '\0'; i++) {
262 const char *name = variable_validate[i].name;
263 int match = 0;
265 if (efi_guidcmp(vendor, variable_validate[i].vendor))
266 continue;
268 if (variable_matches(utf8_name, utf8_size+1, name, &match)) {
269 if (variable_validate[i].validate == NULL)
270 break;
271 kfree(utf8_name);
272 return variable_validate[i].validate(var_name, match,
273 data, data_size);
276 kfree(utf8_name);
277 return true;
279 EXPORT_SYMBOL_GPL(efivar_validate);
281 bool
282 efivar_variable_is_removable(efi_guid_t vendor, const char *var_name,
283 size_t len)
285 int i;
286 bool found = false;
287 int match = 0;
290 * Check if our variable is in the validated variables list
292 for (i = 0; variable_validate[i].name[0] != '\0'; i++) {
293 if (efi_guidcmp(variable_validate[i].vendor, vendor))
294 continue;
296 if (variable_matches(var_name, len,
297 variable_validate[i].name, &match)) {
298 found = true;
299 break;
304 * If it's in our list, it is removable.
306 return found;
308 EXPORT_SYMBOL_GPL(efivar_variable_is_removable);
310 static efi_status_t
311 check_var_size(u32 attributes, unsigned long size)
313 const struct efivar_operations *fops = __efivars->ops;
315 if (!fops->query_variable_store)
316 return EFI_UNSUPPORTED;
318 return fops->query_variable_store(attributes, size);
321 static int efi_status_to_err(efi_status_t status)
323 int err;
325 switch (status) {
326 case EFI_SUCCESS:
327 err = 0;
328 break;
329 case EFI_INVALID_PARAMETER:
330 err = -EINVAL;
331 break;
332 case EFI_OUT_OF_RESOURCES:
333 err = -ENOSPC;
334 break;
335 case EFI_DEVICE_ERROR:
336 err = -EIO;
337 break;
338 case EFI_WRITE_PROTECTED:
339 err = -EROFS;
340 break;
341 case EFI_SECURITY_VIOLATION:
342 err = -EACCES;
343 break;
344 case EFI_NOT_FOUND:
345 err = -ENOENT;
346 break;
347 default:
348 err = -EINVAL;
351 return err;
354 static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor,
355 struct list_head *head)
357 struct efivar_entry *entry, *n;
358 unsigned long strsize1, strsize2;
359 bool found = false;
361 strsize1 = ucs2_strsize(variable_name, 1024);
362 list_for_each_entry_safe(entry, n, head, list) {
363 strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
364 if (strsize1 == strsize2 &&
365 !memcmp(variable_name, &(entry->var.VariableName),
366 strsize2) &&
367 !efi_guidcmp(entry->var.VendorGuid,
368 *vendor)) {
369 found = true;
370 break;
373 return found;
377 * Returns the size of variable_name, in bytes, including the
378 * terminating NULL character, or variable_name_size if no NULL
379 * character is found among the first variable_name_size bytes.
381 static unsigned long var_name_strnsize(efi_char16_t *variable_name,
382 unsigned long variable_name_size)
384 unsigned long len;
385 efi_char16_t c;
388 * The variable name is, by definition, a NULL-terminated
389 * string, so make absolutely sure that variable_name_size is
390 * the value we expect it to be. If not, return the real size.
392 for (len = 2; len <= variable_name_size; len += sizeof(c)) {
393 c = variable_name[(len / sizeof(c)) - 1];
394 if (!c)
395 break;
398 return min(len, variable_name_size);
402 * Print a warning when duplicate EFI variables are encountered and
403 * disable the sysfs workqueue since the firmware is buggy.
405 static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid,
406 unsigned long len16)
408 size_t i, len8 = len16 / sizeof(efi_char16_t);
409 char *str8;
412 * Disable the workqueue since the algorithm it uses for
413 * detecting new variables won't work with this buggy
414 * implementation of GetNextVariableName().
416 efivar_wq_enabled = false;
418 str8 = kzalloc(len8, GFP_KERNEL);
419 if (!str8)
420 return;
422 for (i = 0; i < len8; i++)
423 str8[i] = str16[i];
425 printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
426 str8, vendor_guid);
427 kfree(str8);
431 * efivar_init - build the initial list of EFI variables
432 * @func: callback function to invoke for every variable
433 * @data: function-specific data to pass to @func
434 * @atomic: do we need to execute the @func-loop atomically?
435 * @duplicates: error if we encounter duplicates on @head?
436 * @head: initialised head of variable list
438 * Get every EFI variable from the firmware and invoke @func. @func
439 * should call efivar_entry_add() to build the list of variables.
441 * Returns 0 on success, or a kernel error code on failure.
443 int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
444 void *data, bool atomic, bool duplicates,
445 struct list_head *head)
447 const struct efivar_operations *ops = __efivars->ops;
448 unsigned long variable_name_size = 1024;
449 efi_char16_t *variable_name;
450 efi_status_t status;
451 efi_guid_t vendor_guid;
452 int err = 0;
454 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
455 if (!variable_name) {
456 printk(KERN_ERR "efivars: Memory allocation failed.\n");
457 return -ENOMEM;
460 spin_lock_irq(&__efivars->lock);
463 * Per EFI spec, the maximum storage allocated for both
464 * the variable name and variable data is 1024 bytes.
467 do {
468 variable_name_size = 1024;
470 status = ops->get_next_variable(&variable_name_size,
471 variable_name,
472 &vendor_guid);
473 switch (status) {
474 case EFI_SUCCESS:
475 if (!atomic)
476 spin_unlock_irq(&__efivars->lock);
478 variable_name_size = var_name_strnsize(variable_name,
479 variable_name_size);
482 * Some firmware implementations return the
483 * same variable name on multiple calls to
484 * get_next_variable(). Terminate the loop
485 * immediately as there is no guarantee that
486 * we'll ever see a different variable name,
487 * and may end up looping here forever.
489 if (duplicates &&
490 variable_is_present(variable_name, &vendor_guid, head)) {
491 dup_variable_bug(variable_name, &vendor_guid,
492 variable_name_size);
493 if (!atomic)
494 spin_lock_irq(&__efivars->lock);
496 status = EFI_NOT_FOUND;
497 break;
500 err = func(variable_name, vendor_guid, variable_name_size, data);
501 if (err)
502 status = EFI_NOT_FOUND;
504 if (!atomic)
505 spin_lock_irq(&__efivars->lock);
507 break;
508 case EFI_NOT_FOUND:
509 break;
510 default:
511 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
512 status);
513 status = EFI_NOT_FOUND;
514 break;
517 } while (status != EFI_NOT_FOUND);
519 spin_unlock_irq(&__efivars->lock);
521 kfree(variable_name);
523 return err;
525 EXPORT_SYMBOL_GPL(efivar_init);
528 * efivar_entry_add - add entry to variable list
529 * @entry: entry to add to list
530 * @head: list head
532 void efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
534 spin_lock_irq(&__efivars->lock);
535 list_add(&entry->list, head);
536 spin_unlock_irq(&__efivars->lock);
538 EXPORT_SYMBOL_GPL(efivar_entry_add);
541 * efivar_entry_remove - remove entry from variable list
542 * @entry: entry to remove from list
544 void efivar_entry_remove(struct efivar_entry *entry)
546 spin_lock_irq(&__efivars->lock);
547 list_del(&entry->list);
548 spin_unlock_irq(&__efivars->lock);
550 EXPORT_SYMBOL_GPL(efivar_entry_remove);
553 * efivar_entry_list_del_unlock - remove entry from variable list
554 * @entry: entry to remove
556 * Remove @entry from the variable list and release the list lock.
558 * NOTE: slightly weird locking semantics here - we expect to be
559 * called with the efivars lock already held, and we release it before
560 * returning. This is because this function is usually called after
561 * set_variable() while the lock is still held.
563 static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
565 lockdep_assert_held(&__efivars->lock);
567 list_del(&entry->list);
568 spin_unlock_irq(&__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 const struct efivar_operations *ops = __efivars->ops;
589 efi_status_t status;
591 lockdep_assert_held(&__efivars->lock);
593 status = 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, or a converted EFI status code if
610 * set_variable() fails.
612 int efivar_entry_delete(struct efivar_entry *entry)
614 const struct efivar_operations *ops = __efivars->ops;
615 efi_status_t status;
617 spin_lock_irq(&__efivars->lock);
618 status = ops->set_variable(entry->var.VariableName,
619 &entry->var.VendorGuid,
620 0, 0, NULL);
621 if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
622 spin_unlock_irq(&__efivars->lock);
623 return efi_status_to_err(status);
626 efivar_entry_list_del_unlock(entry);
627 return 0;
629 EXPORT_SYMBOL_GPL(efivar_entry_delete);
632 * efivar_entry_set - call set_variable()
633 * @entry: entry containing the EFI variable to write
634 * @attributes: variable attributes
635 * @size: size of @data buffer
636 * @data: buffer containing variable data
637 * @head: head of variable list
639 * Calls set_variable() for an EFI variable. If creating a new EFI
640 * variable, this function is usually followed by efivar_entry_add().
642 * Before writing the variable, the remaining EFI variable storage
643 * space is checked to ensure there is enough room available.
645 * If @head is not NULL a lookup is performed to determine whether
646 * the entry is already on the list.
648 * Returns 0 on success, -EEXIST if a lookup is performed and the entry
649 * already exists on the list, or a converted EFI status code if
650 * set_variable() fails.
652 int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
653 unsigned long size, void *data, struct list_head *head)
655 const struct efivar_operations *ops = __efivars->ops;
656 efi_status_t status;
657 efi_char16_t *name = entry->var.VariableName;
658 efi_guid_t vendor = entry->var.VendorGuid;
660 spin_lock_irq(&__efivars->lock);
662 if (head && efivar_entry_find(name, vendor, head, false)) {
663 spin_unlock_irq(&__efivars->lock);
664 return -EEXIST;
667 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
668 if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
669 status = ops->set_variable(name, &vendor,
670 attributes, size, data);
672 spin_unlock_irq(&__efivars->lock);
674 return efi_status_to_err(status);
677 EXPORT_SYMBOL_GPL(efivar_entry_set);
680 * efivar_entry_set_nonblocking - call set_variable_nonblocking()
682 * This function is guaranteed to not block and is suitable for calling
683 * from crash/panic handlers.
685 * Crucially, this function will not block if it cannot acquire
686 * __efivars->lock. Instead, it returns -EBUSY.
688 static int
689 efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
690 u32 attributes, unsigned long size, void *data)
692 const struct efivar_operations *ops = __efivars->ops;
693 unsigned long flags;
694 efi_status_t status;
696 if (!spin_trylock_irqsave(&__efivars->lock, flags))
697 return -EBUSY;
699 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
700 if (status != EFI_SUCCESS) {
701 spin_unlock_irqrestore(&__efivars->lock, flags);
702 return -ENOSPC;
705 status = ops->set_variable_nonblocking(name, &vendor, attributes,
706 size, data);
708 spin_unlock_irqrestore(&__efivars->lock, flags);
709 return efi_status_to_err(status);
713 * efivar_entry_set_safe - call set_variable() if enough space in firmware
714 * @name: buffer containing the variable name
715 * @vendor: variable vendor guid
716 * @attributes: variable attributes
717 * @block: can we block in this context?
718 * @size: size of @data buffer
719 * @data: buffer containing variable data
721 * Ensures there is enough free storage in the firmware for this variable, and
722 * if so, calls set_variable(). If creating a new EFI variable, this function
723 * is usually followed by efivar_entry_add().
725 * Returns 0 on success, -ENOSPC if the firmware does not have enough
726 * space for set_variable() to succeed, or a converted EFI status code
727 * if set_variable() fails.
729 int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
730 bool block, unsigned long size, void *data)
732 const struct efivar_operations *ops = __efivars->ops;
733 unsigned long flags;
734 efi_status_t status;
736 if (!ops->query_variable_store)
737 return -ENOSYS;
740 * If the EFI variable backend provides a non-blocking
741 * ->set_variable() operation and we're in a context where we
742 * cannot block, then we need to use it to avoid live-locks,
743 * since the implication is that the regular ->set_variable()
744 * will block.
746 * If no ->set_variable_nonblocking() is provided then
747 * ->set_variable() is assumed to be non-blocking.
749 if (!block && ops->set_variable_nonblocking)
750 return efivar_entry_set_nonblocking(name, vendor, attributes,
751 size, data);
753 if (!block) {
754 if (!spin_trylock_irqsave(&__efivars->lock, flags))
755 return -EBUSY;
756 } else {
757 spin_lock_irqsave(&__efivars->lock, flags);
760 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
761 if (status != EFI_SUCCESS) {
762 spin_unlock_irqrestore(&__efivars->lock, flags);
763 return -ENOSPC;
766 status = ops->set_variable(name, &vendor, attributes, size, data);
768 spin_unlock_irqrestore(&__efivars->lock, flags);
770 return efi_status_to_err(status);
772 EXPORT_SYMBOL_GPL(efivar_entry_set_safe);
775 * efivar_entry_find - search for an entry
776 * @name: the EFI variable name
777 * @guid: the EFI variable vendor's guid
778 * @head: head of the variable list
779 * @remove: should we remove the entry from the list?
781 * Search for an entry on the variable list that has the EFI variable
782 * name @name and vendor guid @guid. If an entry is found on the list
783 * and @remove is true, the entry is removed from the list.
785 * The caller MUST call efivar_entry_iter_begin() and
786 * efivar_entry_iter_end() before and after the invocation of this
787 * function, respectively.
789 * Returns the entry if found on the list, %NULL otherwise.
791 struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
792 struct list_head *head, bool remove)
794 struct efivar_entry *entry, *n;
795 int strsize1, strsize2;
796 bool found = false;
798 lockdep_assert_held(&__efivars->lock);
800 list_for_each_entry_safe(entry, n, head, list) {
801 strsize1 = ucs2_strsize(name, 1024);
802 strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
803 if (strsize1 == strsize2 &&
804 !memcmp(name, &(entry->var.VariableName), strsize1) &&
805 !efi_guidcmp(guid, entry->var.VendorGuid)) {
806 found = true;
807 break;
811 if (!found)
812 return NULL;
814 if (remove) {
815 if (entry->scanning) {
817 * The entry will be deleted
818 * after scanning is completed.
820 entry->deleting = true;
821 } else
822 list_del(&entry->list);
825 return entry;
827 EXPORT_SYMBOL_GPL(efivar_entry_find);
830 * efivar_entry_size - obtain the size of a variable
831 * @entry: entry for this variable
832 * @size: location to store the variable's size
834 int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
836 const struct efivar_operations *ops = __efivars->ops;
837 efi_status_t status;
839 *size = 0;
841 spin_lock_irq(&__efivars->lock);
842 status = ops->get_variable(entry->var.VariableName,
843 &entry->var.VendorGuid, NULL, size, NULL);
844 spin_unlock_irq(&__efivars->lock);
846 if (status != EFI_BUFFER_TOO_SMALL)
847 return efi_status_to_err(status);
849 return 0;
851 EXPORT_SYMBOL_GPL(efivar_entry_size);
854 * __efivar_entry_get - call get_variable()
855 * @entry: read data for this variable
856 * @attributes: variable attributes
857 * @size: size of @data buffer
858 * @data: buffer to store variable data
860 * The caller MUST call efivar_entry_iter_begin() and
861 * efivar_entry_iter_end() before and after the invocation of this
862 * function, respectively.
864 int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
865 unsigned long *size, void *data)
867 const struct efivar_operations *ops = __efivars->ops;
868 efi_status_t status;
870 lockdep_assert_held(&__efivars->lock);
872 status = ops->get_variable(entry->var.VariableName,
873 &entry->var.VendorGuid,
874 attributes, size, data);
876 return efi_status_to_err(status);
878 EXPORT_SYMBOL_GPL(__efivar_entry_get);
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 int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
888 unsigned long *size, void *data)
890 const struct efivar_operations *ops = __efivars->ops;
891 efi_status_t status;
893 spin_lock_irq(&__efivars->lock);
894 status = ops->get_variable(entry->var.VariableName,
895 &entry->var.VendorGuid,
896 attributes, size, data);
897 spin_unlock_irq(&__efivars->lock);
899 return efi_status_to_err(status);
901 EXPORT_SYMBOL_GPL(efivar_entry_get);
904 * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
905 * @entry: entry containing variable to set and get
906 * @attributes: attributes of variable to be written
907 * @size: size of data buffer
908 * @data: buffer containing data to write
909 * @set: did the set_variable() call succeed?
911 * This is a pretty special (complex) function. See efivarfs_file_write().
913 * Atomically call set_variable() for @entry and if the call is
914 * successful, return the new size of the variable from get_variable()
915 * in @size. The success of set_variable() is indicated by @set.
917 * Returns 0 on success, -EINVAL if the variable data is invalid,
918 * -ENOSPC if the firmware does not have enough available space, or a
919 * converted EFI status code if either of set_variable() or
920 * get_variable() fail.
922 * If the EFI variable does not exist when calling set_variable()
923 * (EFI_NOT_FOUND), @entry is removed from the variable list.
925 int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
926 unsigned long *size, void *data, bool *set)
928 const struct efivar_operations *ops = __efivars->ops;
929 efi_char16_t *name = entry->var.VariableName;
930 efi_guid_t *vendor = &entry->var.VendorGuid;
931 efi_status_t status;
932 int err;
934 *set = false;
936 if (efivar_validate(*vendor, name, data, *size) == false)
937 return -EINVAL;
940 * The lock here protects the get_variable call, the conditional
941 * set_variable call, and removal of the variable from the efivars
942 * list (in the case of an authenticated delete).
944 spin_lock_irq(&__efivars->lock);
947 * Ensure that the available space hasn't shrunk below the safe level
949 status = check_var_size(attributes, *size + ucs2_strsize(name, 1024));
950 if (status != EFI_SUCCESS) {
951 if (status != EFI_UNSUPPORTED) {
952 err = efi_status_to_err(status);
953 goto out;
956 if (*size > 65536) {
957 err = -ENOSPC;
958 goto out;
962 status = ops->set_variable(name, vendor, attributes, *size, data);
963 if (status != EFI_SUCCESS) {
964 err = efi_status_to_err(status);
965 goto out;
968 *set = true;
971 * Writing to the variable may have caused a change in size (which
972 * could either be an append or an overwrite), or the variable to be
973 * deleted. Perform a GetVariable() so we can tell what actually
974 * happened.
976 *size = 0;
977 status = ops->get_variable(entry->var.VariableName,
978 &entry->var.VendorGuid,
979 NULL, size, NULL);
981 if (status == EFI_NOT_FOUND)
982 efivar_entry_list_del_unlock(entry);
983 else
984 spin_unlock_irq(&__efivars->lock);
986 if (status && status != EFI_BUFFER_TOO_SMALL)
987 return efi_status_to_err(status);
989 return 0;
991 out:
992 spin_unlock_irq(&__efivars->lock);
993 return err;
996 EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
999 * efivar_entry_iter_begin - begin iterating the variable list
1001 * Lock the variable list to prevent entry insertion and removal until
1002 * efivar_entry_iter_end() is called. This function is usually used in
1003 * conjunction with __efivar_entry_iter() or efivar_entry_iter().
1005 void efivar_entry_iter_begin(void)
1007 spin_lock_irq(&__efivars->lock);
1009 EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
1012 * efivar_entry_iter_end - finish iterating the variable list
1014 * Unlock the variable list and allow modifications to the list again.
1016 void efivar_entry_iter_end(void)
1018 spin_unlock_irq(&__efivars->lock);
1020 EXPORT_SYMBOL_GPL(efivar_entry_iter_end);
1023 * __efivar_entry_iter - iterate over variable list
1024 * @func: callback function
1025 * @head: head of the variable list
1026 * @data: function-specific data to pass to callback
1027 * @prev: entry to begin iterating from
1029 * Iterate over the list of EFI variables and call @func with every
1030 * entry on the list. It is safe for @func to remove entries in the
1031 * list via efivar_entry_delete().
1033 * You MUST call efivar_enter_iter_begin() before this function, and
1034 * efivar_entry_iter_end() afterwards.
1036 * It is possible to begin iteration from an arbitrary entry within
1037 * the list by passing @prev. @prev is updated on return to point to
1038 * the last entry passed to @func. To begin iterating from the
1039 * beginning of the list @prev must be %NULL.
1041 * The restrictions for @func are the same as documented for
1042 * efivar_entry_iter().
1044 int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
1045 struct list_head *head, void *data,
1046 struct efivar_entry **prev)
1048 struct efivar_entry *entry, *n;
1049 int err = 0;
1051 if (!prev || !*prev) {
1052 list_for_each_entry_safe(entry, n, head, list) {
1053 err = func(entry, data);
1054 if (err)
1055 break;
1058 if (prev)
1059 *prev = entry;
1061 return err;
1065 list_for_each_entry_safe_continue((*prev), n, head, list) {
1066 err = func(*prev, data);
1067 if (err)
1068 break;
1071 return err;
1073 EXPORT_SYMBOL_GPL(__efivar_entry_iter);
1076 * efivar_entry_iter - iterate over variable list
1077 * @func: callback function
1078 * @head: head of variable list
1079 * @data: function-specific data to pass to callback
1081 * Iterate over the list of EFI variables and call @func with every
1082 * entry on the list. It is safe for @func to remove entries in the
1083 * list via efivar_entry_delete() while iterating.
1085 * Some notes for the callback function:
1086 * - a non-zero return value indicates an error and terminates the loop
1087 * - @func is called from atomic context
1089 int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
1090 struct list_head *head, void *data)
1092 int err = 0;
1094 efivar_entry_iter_begin();
1095 err = __efivar_entry_iter(func, head, data, NULL);
1096 efivar_entry_iter_end();
1098 return err;
1100 EXPORT_SYMBOL_GPL(efivar_entry_iter);
1103 * efivars_kobject - get the kobject for the registered efivars
1105 * If efivars_register() has not been called we return NULL,
1106 * otherwise return the kobject used at registration time.
1108 struct kobject *efivars_kobject(void)
1110 if (!__efivars)
1111 return NULL;
1113 return __efivars->kobject;
1115 EXPORT_SYMBOL_GPL(efivars_kobject);
1118 * efivar_run_worker - schedule the efivar worker thread
1120 void efivar_run_worker(void)
1122 if (efivar_wq_enabled)
1123 schedule_work(&efivar_work);
1125 EXPORT_SYMBOL_GPL(efivar_run_worker);
1128 * efivars_register - register an efivars
1129 * @efivars: efivars to register
1130 * @ops: efivars operations
1131 * @kobject: @efivars-specific kobject
1133 * Only a single efivars can be registered at any time.
1135 int efivars_register(struct efivars *efivars,
1136 const struct efivar_operations *ops,
1137 struct kobject *kobject)
1139 spin_lock_init(&efivars->lock);
1140 efivars->ops = ops;
1141 efivars->kobject = kobject;
1143 __efivars = efivars;
1145 return 0;
1147 EXPORT_SYMBOL_GPL(efivars_register);
1150 * efivars_unregister - unregister an efivars
1151 * @efivars: efivars to unregister
1153 * The caller must have already removed every entry from the list,
1154 * failure to do so is an error.
1156 int efivars_unregister(struct efivars *efivars)
1158 int rv;
1160 if (!__efivars) {
1161 printk(KERN_ERR "efivars not registered\n");
1162 rv = -EINVAL;
1163 goto out;
1166 if (__efivars != efivars) {
1167 rv = -EINVAL;
1168 goto out;
1171 __efivars = NULL;
1173 rv = 0;
1174 out:
1175 return rv;
1177 EXPORT_SYMBOL_GPL(efivars_unregister);