]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/firmware/efi/vars.c
efi: Add nonblocking option to efi_query_variable_store()
[mirror_ubuntu-bionic-kernel.git] / drivers / firmware / efi / vars.c
1 /*
2 * Originally from efivars.c
3 *
4 * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
5 * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
6 *
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.
11 *
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.
16 *
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
20 */
21
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>
36
37 /* Private pointer to registered efivars */
38 static struct efivars *__efivars;
39
40 static bool efivar_wq_enabled = true;
41 DECLARE_WORK(efivar_work, NULL);
42 EXPORT_SYMBOL_GPL(efivar_work);
43
44 static bool
45 validate_device_path(efi_char16_t *var_name, int match, u8 *buffer,
46 unsigned long len)
47 {
48 struct efi_generic_dev_path *node;
49 int offset = 0;
50
51 node = (struct efi_generic_dev_path *)buffer;
52
53 if (len < sizeof(*node))
54 return false;
55
56 while (offset <= len - sizeof(*node) &&
57 node->length >= sizeof(*node) &&
58 node->length <= len - offset) {
59 offset += node->length;
60
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;
65
66 node = (struct efi_generic_dev_path *)(buffer + offset);
67 }
68
69 /*
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.
73 */
74 return false;
75 }
76
77 static bool
78 validate_boot_order(efi_char16_t *var_name, int match, u8 *buffer,
79 unsigned long len)
80 {
81 /* An array of 16-bit integers */
82 if ((len % 2) != 0)
83 return false;
84
85 return true;
86 }
87
88 static bool
89 validate_load_option(efi_char16_t *var_name, int match, u8 *buffer,
90 unsigned long len)
91 {
92 u16 filepathlength;
93 int i, desclength = 0, namelen;
94
95 namelen = ucs2_strnlen(var_name, EFI_VAR_NAME_LEN);
96
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;
102 }
103
104 /* Reject it if there's 4 digits of hex and then further content */
105 if (namelen > match + 4)
106 return false;
107
108 /* A valid entry must be at least 8 bytes */
109 if (len < 8)
110 return false;
111
112 filepathlength = buffer[4] | buffer[5] << 8;
113
114 /*
115 * There's no stored length for the description, so it has to be
116 * found by hand
117 */
118 desclength = ucs2_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
119
120 /* Each boot entry must have a descriptor */
121 if (!desclength)
122 return false;
123
124 /*
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
128 */
129 if ((desclength + filepathlength + 6) > len)
130 return false;
131
132 /*
133 * And, finally, check the filepath
134 */
135 return validate_device_path(var_name, match, buffer + desclength + 6,
136 filepathlength);
137 }
138
139 static bool
140 validate_uint16(efi_char16_t *var_name, int match, u8 *buffer,
141 unsigned long len)
142 {
143 /* A single 16-bit integer */
144 if (len != 2)
145 return false;
146
147 return true;
148 }
149
150 static bool
151 validate_ascii_string(efi_char16_t *var_name, int match, u8 *buffer,
152 unsigned long len)
153 {
154 int i;
155
156 for (i = 0; i < len; i++) {
157 if (buffer[i] > 127)
158 return false;
159
160 if (buffer[i] == 0)
161 return true;
162 }
163
164 return false;
165 }
166
167 struct variable_validate {
168 char *name;
169 bool (*validate)(efi_char16_t *var_name, int match, u8 *data,
170 unsigned long len);
171 };
172
173 static const struct variable_validate variable_validate[] = {
174 { "BootNext", validate_uint16 },
175 { "BootOrder", validate_boot_order },
176 { "DriverOrder", validate_boot_order },
177 { "Boot*", validate_load_option },
178 { "Driver*", validate_load_option },
179 { "ConIn", validate_device_path },
180 { "ConInDev", validate_device_path },
181 { "ConOut", validate_device_path },
182 { "ConOutDev", validate_device_path },
183 { "ErrOut", validate_device_path },
184 { "ErrOutDev", validate_device_path },
185 { "Timeout", validate_uint16 },
186 { "Lang", validate_ascii_string },
187 { "PlatformLang", validate_ascii_string },
188 { "", NULL },
189 };
190
191 bool
192 efivar_validate(efi_char16_t *var_name, u8 *data, unsigned long len)
193 {
194 int i;
195 u16 *unicode_name = var_name;
196
197 for (i = 0; variable_validate[i].validate != NULL; i++) {
198 const char *name = variable_validate[i].name;
199 int match;
200
201 for (match = 0; ; match++) {
202 char c = name[match];
203 u16 u = unicode_name[match];
204
205 /* All special variables are plain ascii */
206 if (u > 127)
207 return true;
208
209 /* Wildcard in the matching name means we've matched */
210 if (c == '*')
211 return variable_validate[i].validate(var_name,
212 match, data, len);
213
214 /* Case sensitive match */
215 if (c != u)
216 break;
217
218 /* Reached the end of the string while matching */
219 if (!c)
220 return variable_validate[i].validate(var_name,
221 match, data, len);
222 }
223 }
224
225 return true;
226 }
227 EXPORT_SYMBOL_GPL(efivar_validate);
228
229 static efi_status_t
230 check_var_size(u32 attributes, unsigned long size)
231 {
232 const struct efivar_operations *fops = __efivars->ops;
233
234 if (!fops->query_variable_store)
235 return EFI_UNSUPPORTED;
236
237 return fops->query_variable_store(attributes, size, false);
238 }
239
240 static efi_status_t
241 check_var_size_nonblocking(u32 attributes, unsigned long size)
242 {
243 const struct efivar_operations *fops = __efivars->ops;
244
245 if (!fops->query_variable_store)
246 return EFI_UNSUPPORTED;
247
248 return fops->query_variable_store(attributes, size, true);
249 }
250
251 static int efi_status_to_err(efi_status_t status)
252 {
253 int err;
254
255 switch (status) {
256 case EFI_SUCCESS:
257 err = 0;
258 break;
259 case EFI_INVALID_PARAMETER:
260 err = -EINVAL;
261 break;
262 case EFI_OUT_OF_RESOURCES:
263 err = -ENOSPC;
264 break;
265 case EFI_DEVICE_ERROR:
266 err = -EIO;
267 break;
268 case EFI_WRITE_PROTECTED:
269 err = -EROFS;
270 break;
271 case EFI_SECURITY_VIOLATION:
272 err = -EACCES;
273 break;
274 case EFI_NOT_FOUND:
275 err = -ENOENT;
276 break;
277 default:
278 err = -EINVAL;
279 }
280
281 return err;
282 }
283
284 static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor,
285 struct list_head *head)
286 {
287 struct efivar_entry *entry, *n;
288 unsigned long strsize1, strsize2;
289 bool found = false;
290
291 strsize1 = ucs2_strsize(variable_name, 1024);
292 list_for_each_entry_safe(entry, n, head, list) {
293 strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
294 if (strsize1 == strsize2 &&
295 !memcmp(variable_name, &(entry->var.VariableName),
296 strsize2) &&
297 !efi_guidcmp(entry->var.VendorGuid,
298 *vendor)) {
299 found = true;
300 break;
301 }
302 }
303 return found;
304 }
305
306 /*
307 * Returns the size of variable_name, in bytes, including the
308 * terminating NULL character, or variable_name_size if no NULL
309 * character is found among the first variable_name_size bytes.
310 */
311 static unsigned long var_name_strnsize(efi_char16_t *variable_name,
312 unsigned long variable_name_size)
313 {
314 unsigned long len;
315 efi_char16_t c;
316
317 /*
318 * The variable name is, by definition, a NULL-terminated
319 * string, so make absolutely sure that variable_name_size is
320 * the value we expect it to be. If not, return the real size.
321 */
322 for (len = 2; len <= variable_name_size; len += sizeof(c)) {
323 c = variable_name[(len / sizeof(c)) - 1];
324 if (!c)
325 break;
326 }
327
328 return min(len, variable_name_size);
329 }
330
331 /*
332 * Print a warning when duplicate EFI variables are encountered and
333 * disable the sysfs workqueue since the firmware is buggy.
334 */
335 static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid,
336 unsigned long len16)
337 {
338 size_t i, len8 = len16 / sizeof(efi_char16_t);
339 char *str8;
340
341 /*
342 * Disable the workqueue since the algorithm it uses for
343 * detecting new variables won't work with this buggy
344 * implementation of GetNextVariableName().
345 */
346 efivar_wq_enabled = false;
347
348 str8 = kzalloc(len8, GFP_KERNEL);
349 if (!str8)
350 return;
351
352 for (i = 0; i < len8; i++)
353 str8[i] = str16[i];
354
355 printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
356 str8, vendor_guid);
357 kfree(str8);
358 }
359
360 /**
361 * efivar_init - build the initial list of EFI variables
362 * @func: callback function to invoke for every variable
363 * @data: function-specific data to pass to @func
364 * @atomic: do we need to execute the @func-loop atomically?
365 * @duplicates: error if we encounter duplicates on @head?
366 * @head: initialised head of variable list
367 *
368 * Get every EFI variable from the firmware and invoke @func. @func
369 * should call efivar_entry_add() to build the list of variables.
370 *
371 * Returns 0 on success, or a kernel error code on failure.
372 */
373 int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
374 void *data, bool atomic, bool duplicates,
375 struct list_head *head)
376 {
377 const struct efivar_operations *ops = __efivars->ops;
378 unsigned long variable_name_size = 1024;
379 efi_char16_t *variable_name;
380 efi_status_t status;
381 efi_guid_t vendor_guid;
382 int err = 0;
383
384 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
385 if (!variable_name) {
386 printk(KERN_ERR "efivars: Memory allocation failed.\n");
387 return -ENOMEM;
388 }
389
390 spin_lock_irq(&__efivars->lock);
391
392 /*
393 * Per EFI spec, the maximum storage allocated for both
394 * the variable name and variable data is 1024 bytes.
395 */
396
397 do {
398 variable_name_size = 1024;
399
400 status = ops->get_next_variable(&variable_name_size,
401 variable_name,
402 &vendor_guid);
403 switch (status) {
404 case EFI_SUCCESS:
405 if (!atomic)
406 spin_unlock_irq(&__efivars->lock);
407
408 variable_name_size = var_name_strnsize(variable_name,
409 variable_name_size);
410
411 /*
412 * Some firmware implementations return the
413 * same variable name on multiple calls to
414 * get_next_variable(). Terminate the loop
415 * immediately as there is no guarantee that
416 * we'll ever see a different variable name,
417 * and may end up looping here forever.
418 */
419 if (duplicates &&
420 variable_is_present(variable_name, &vendor_guid, head)) {
421 dup_variable_bug(variable_name, &vendor_guid,
422 variable_name_size);
423 if (!atomic)
424 spin_lock_irq(&__efivars->lock);
425
426 status = EFI_NOT_FOUND;
427 break;
428 }
429
430 err = func(variable_name, vendor_guid, variable_name_size, data);
431 if (err)
432 status = EFI_NOT_FOUND;
433
434 if (!atomic)
435 spin_lock_irq(&__efivars->lock);
436
437 break;
438 case EFI_NOT_FOUND:
439 break;
440 default:
441 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
442 status);
443 status = EFI_NOT_FOUND;
444 break;
445 }
446
447 } while (status != EFI_NOT_FOUND);
448
449 spin_unlock_irq(&__efivars->lock);
450
451 kfree(variable_name);
452
453 return err;
454 }
455 EXPORT_SYMBOL_GPL(efivar_init);
456
457 /**
458 * efivar_entry_add - add entry to variable list
459 * @entry: entry to add to list
460 * @head: list head
461 */
462 void efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
463 {
464 spin_lock_irq(&__efivars->lock);
465 list_add(&entry->list, head);
466 spin_unlock_irq(&__efivars->lock);
467 }
468 EXPORT_SYMBOL_GPL(efivar_entry_add);
469
470 /**
471 * efivar_entry_remove - remove entry from variable list
472 * @entry: entry to remove from list
473 */
474 void efivar_entry_remove(struct efivar_entry *entry)
475 {
476 spin_lock_irq(&__efivars->lock);
477 list_del(&entry->list);
478 spin_unlock_irq(&__efivars->lock);
479 }
480 EXPORT_SYMBOL_GPL(efivar_entry_remove);
481
482 /*
483 * efivar_entry_list_del_unlock - remove entry from variable list
484 * @entry: entry to remove
485 *
486 * Remove @entry from the variable list and release the list lock.
487 *
488 * NOTE: slightly weird locking semantics here - we expect to be
489 * called with the efivars lock already held, and we release it before
490 * returning. This is because this function is usually called after
491 * set_variable() while the lock is still held.
492 */
493 static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
494 {
495 lockdep_assert_held(&__efivars->lock);
496
497 list_del(&entry->list);
498 spin_unlock_irq(&__efivars->lock);
499 }
500
501 /**
502 * __efivar_entry_delete - delete an EFI variable
503 * @entry: entry containing EFI variable to delete
504 *
505 * Delete the variable from the firmware but leave @entry on the
506 * variable list.
507 *
508 * This function differs from efivar_entry_delete() because it does
509 * not remove @entry from the variable list. Also, it is safe to be
510 * called from within a efivar_entry_iter_begin() and
511 * efivar_entry_iter_end() region, unlike efivar_entry_delete().
512 *
513 * Returns 0 on success, or a converted EFI status code if
514 * set_variable() fails.
515 */
516 int __efivar_entry_delete(struct efivar_entry *entry)
517 {
518 const struct efivar_operations *ops = __efivars->ops;
519 efi_status_t status;
520
521 lockdep_assert_held(&__efivars->lock);
522
523 status = ops->set_variable(entry->var.VariableName,
524 &entry->var.VendorGuid,
525 0, 0, NULL);
526
527 return efi_status_to_err(status);
528 }
529 EXPORT_SYMBOL_GPL(__efivar_entry_delete);
530
531 /**
532 * efivar_entry_delete - delete variable and remove entry from list
533 * @entry: entry containing variable to delete
534 *
535 * Delete the variable from the firmware and remove @entry from the
536 * variable list. It is the caller's responsibility to free @entry
537 * once we return.
538 *
539 * Returns 0 on success, or a converted EFI status code if
540 * set_variable() fails.
541 */
542 int efivar_entry_delete(struct efivar_entry *entry)
543 {
544 const struct efivar_operations *ops = __efivars->ops;
545 efi_status_t status;
546
547 spin_lock_irq(&__efivars->lock);
548 status = ops->set_variable(entry->var.VariableName,
549 &entry->var.VendorGuid,
550 0, 0, NULL);
551 if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
552 spin_unlock_irq(&__efivars->lock);
553 return efi_status_to_err(status);
554 }
555
556 efivar_entry_list_del_unlock(entry);
557 return 0;
558 }
559 EXPORT_SYMBOL_GPL(efivar_entry_delete);
560
561 /**
562 * efivar_entry_set - call set_variable()
563 * @entry: entry containing the EFI variable to write
564 * @attributes: variable attributes
565 * @size: size of @data buffer
566 * @data: buffer containing variable data
567 * @head: head of variable list
568 *
569 * Calls set_variable() for an EFI variable. If creating a new EFI
570 * variable, this function is usually followed by efivar_entry_add().
571 *
572 * Before writing the variable, the remaining EFI variable storage
573 * space is checked to ensure there is enough room available.
574 *
575 * If @head is not NULL a lookup is performed to determine whether
576 * the entry is already on the list.
577 *
578 * Returns 0 on success, -EEXIST if a lookup is performed and the entry
579 * already exists on the list, or a converted EFI status code if
580 * set_variable() fails.
581 */
582 int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
583 unsigned long size, void *data, struct list_head *head)
584 {
585 const struct efivar_operations *ops = __efivars->ops;
586 efi_status_t status;
587 efi_char16_t *name = entry->var.VariableName;
588 efi_guid_t vendor = entry->var.VendorGuid;
589
590 spin_lock_irq(&__efivars->lock);
591
592 if (head && efivar_entry_find(name, vendor, head, false)) {
593 spin_unlock_irq(&__efivars->lock);
594 return -EEXIST;
595 }
596
597 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
598 if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
599 status = ops->set_variable(name, &vendor,
600 attributes, size, data);
601
602 spin_unlock_irq(&__efivars->lock);
603
604 return efi_status_to_err(status);
605
606 }
607 EXPORT_SYMBOL_GPL(efivar_entry_set);
608
609 /*
610 * efivar_entry_set_nonblocking - call set_variable_nonblocking()
611 *
612 * This function is guaranteed to not block and is suitable for calling
613 * from crash/panic handlers.
614 *
615 * Crucially, this function will not block if it cannot acquire
616 * __efivars->lock. Instead, it returns -EBUSY.
617 */
618 static int
619 efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
620 u32 attributes, unsigned long size, void *data)
621 {
622 const struct efivar_operations *ops = __efivars->ops;
623 unsigned long flags;
624 efi_status_t status;
625
626 if (!spin_trylock_irqsave(&__efivars->lock, flags))
627 return -EBUSY;
628
629 status = check_var_size_nonblocking(attributes,
630 size + ucs2_strsize(name, 1024));
631 if (status != EFI_SUCCESS) {
632 spin_unlock_irqrestore(&__efivars->lock, flags);
633 return -ENOSPC;
634 }
635
636 status = ops->set_variable_nonblocking(name, &vendor, attributes,
637 size, data);
638
639 spin_unlock_irqrestore(&__efivars->lock, flags);
640 return efi_status_to_err(status);
641 }
642
643 /**
644 * efivar_entry_set_safe - call set_variable() if enough space in firmware
645 * @name: buffer containing the variable name
646 * @vendor: variable vendor guid
647 * @attributes: variable attributes
648 * @block: can we block in this context?
649 * @size: size of @data buffer
650 * @data: buffer containing variable data
651 *
652 * Ensures there is enough free storage in the firmware for this variable, and
653 * if so, calls set_variable(). If creating a new EFI variable, this function
654 * is usually followed by efivar_entry_add().
655 *
656 * Returns 0 on success, -ENOSPC if the firmware does not have enough
657 * space for set_variable() to succeed, or a converted EFI status code
658 * if set_variable() fails.
659 */
660 int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
661 bool block, unsigned long size, void *data)
662 {
663 const struct efivar_operations *ops = __efivars->ops;
664 unsigned long flags;
665 efi_status_t status;
666
667 if (!ops->query_variable_store)
668 return -ENOSYS;
669
670 /*
671 * If the EFI variable backend provides a non-blocking
672 * ->set_variable() operation and we're in a context where we
673 * cannot block, then we need to use it to avoid live-locks,
674 * since the implication is that the regular ->set_variable()
675 * will block.
676 *
677 * If no ->set_variable_nonblocking() is provided then
678 * ->set_variable() is assumed to be non-blocking.
679 */
680 if (!block && ops->set_variable_nonblocking)
681 return efivar_entry_set_nonblocking(name, vendor, attributes,
682 size, data);
683
684 if (!block) {
685 if (!spin_trylock_irqsave(&__efivars->lock, flags))
686 return -EBUSY;
687 } else {
688 spin_lock_irqsave(&__efivars->lock, flags);
689 }
690
691 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
692 if (status != EFI_SUCCESS) {
693 spin_unlock_irqrestore(&__efivars->lock, flags);
694 return -ENOSPC;
695 }
696
697 status = ops->set_variable(name, &vendor, attributes, size, data);
698
699 spin_unlock_irqrestore(&__efivars->lock, flags);
700
701 return efi_status_to_err(status);
702 }
703 EXPORT_SYMBOL_GPL(efivar_entry_set_safe);
704
705 /**
706 * efivar_entry_find - search for an entry
707 * @name: the EFI variable name
708 * @guid: the EFI variable vendor's guid
709 * @head: head of the variable list
710 * @remove: should we remove the entry from the list?
711 *
712 * Search for an entry on the variable list that has the EFI variable
713 * name @name and vendor guid @guid. If an entry is found on the list
714 * and @remove is true, the entry is removed from the list.
715 *
716 * The caller MUST call efivar_entry_iter_begin() and
717 * efivar_entry_iter_end() before and after the invocation of this
718 * function, respectively.
719 *
720 * Returns the entry if found on the list, %NULL otherwise.
721 */
722 struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
723 struct list_head *head, bool remove)
724 {
725 struct efivar_entry *entry, *n;
726 int strsize1, strsize2;
727 bool found = false;
728
729 lockdep_assert_held(&__efivars->lock);
730
731 list_for_each_entry_safe(entry, n, head, list) {
732 strsize1 = ucs2_strsize(name, 1024);
733 strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
734 if (strsize1 == strsize2 &&
735 !memcmp(name, &(entry->var.VariableName), strsize1) &&
736 !efi_guidcmp(guid, entry->var.VendorGuid)) {
737 found = true;
738 break;
739 }
740 }
741
742 if (!found)
743 return NULL;
744
745 if (remove) {
746 if (entry->scanning) {
747 /*
748 * The entry will be deleted
749 * after scanning is completed.
750 */
751 entry->deleting = true;
752 } else
753 list_del(&entry->list);
754 }
755
756 return entry;
757 }
758 EXPORT_SYMBOL_GPL(efivar_entry_find);
759
760 /**
761 * efivar_entry_size - obtain the size of a variable
762 * @entry: entry for this variable
763 * @size: location to store the variable's size
764 */
765 int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
766 {
767 const struct efivar_operations *ops = __efivars->ops;
768 efi_status_t status;
769
770 *size = 0;
771
772 spin_lock_irq(&__efivars->lock);
773 status = ops->get_variable(entry->var.VariableName,
774 &entry->var.VendorGuid, NULL, size, NULL);
775 spin_unlock_irq(&__efivars->lock);
776
777 if (status != EFI_BUFFER_TOO_SMALL)
778 return efi_status_to_err(status);
779
780 return 0;
781 }
782 EXPORT_SYMBOL_GPL(efivar_entry_size);
783
784 /**
785 * __efivar_entry_get - call get_variable()
786 * @entry: read data for this variable
787 * @attributes: variable attributes
788 * @size: size of @data buffer
789 * @data: buffer to store variable data
790 *
791 * The caller MUST call efivar_entry_iter_begin() and
792 * efivar_entry_iter_end() before and after the invocation of this
793 * function, respectively.
794 */
795 int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
796 unsigned long *size, void *data)
797 {
798 const struct efivar_operations *ops = __efivars->ops;
799 efi_status_t status;
800
801 lockdep_assert_held(&__efivars->lock);
802
803 status = ops->get_variable(entry->var.VariableName,
804 &entry->var.VendorGuid,
805 attributes, size, data);
806
807 return efi_status_to_err(status);
808 }
809 EXPORT_SYMBOL_GPL(__efivar_entry_get);
810
811 /**
812 * efivar_entry_get - call get_variable()
813 * @entry: read data for this variable
814 * @attributes: variable attributes
815 * @size: size of @data buffer
816 * @data: buffer to store variable data
817 */
818 int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
819 unsigned long *size, void *data)
820 {
821 const struct efivar_operations *ops = __efivars->ops;
822 efi_status_t status;
823
824 spin_lock_irq(&__efivars->lock);
825 status = ops->get_variable(entry->var.VariableName,
826 &entry->var.VendorGuid,
827 attributes, size, data);
828 spin_unlock_irq(&__efivars->lock);
829
830 return efi_status_to_err(status);
831 }
832 EXPORT_SYMBOL_GPL(efivar_entry_get);
833
834 /**
835 * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
836 * @entry: entry containing variable to set and get
837 * @attributes: attributes of variable to be written
838 * @size: size of data buffer
839 * @data: buffer containing data to write
840 * @set: did the set_variable() call succeed?
841 *
842 * This is a pretty special (complex) function. See efivarfs_file_write().
843 *
844 * Atomically call set_variable() for @entry and if the call is
845 * successful, return the new size of the variable from get_variable()
846 * in @size. The success of set_variable() is indicated by @set.
847 *
848 * Returns 0 on success, -EINVAL if the variable data is invalid,
849 * -ENOSPC if the firmware does not have enough available space, or a
850 * converted EFI status code if either of set_variable() or
851 * get_variable() fail.
852 *
853 * If the EFI variable does not exist when calling set_variable()
854 * (EFI_NOT_FOUND), @entry is removed from the variable list.
855 */
856 int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
857 unsigned long *size, void *data, bool *set)
858 {
859 const struct efivar_operations *ops = __efivars->ops;
860 efi_char16_t *name = entry->var.VariableName;
861 efi_guid_t *vendor = &entry->var.VendorGuid;
862 efi_status_t status;
863 int err;
864
865 *set = false;
866
867 if (efivar_validate(name, data, *size) == false)
868 return -EINVAL;
869
870 /*
871 * The lock here protects the get_variable call, the conditional
872 * set_variable call, and removal of the variable from the efivars
873 * list (in the case of an authenticated delete).
874 */
875 spin_lock_irq(&__efivars->lock);
876
877 /*
878 * Ensure that the available space hasn't shrunk below the safe level
879 */
880 status = check_var_size(attributes, *size + ucs2_strsize(name, 1024));
881 if (status != EFI_SUCCESS) {
882 if (status != EFI_UNSUPPORTED) {
883 err = efi_status_to_err(status);
884 goto out;
885 }
886
887 if (*size > 65536) {
888 err = -ENOSPC;
889 goto out;
890 }
891 }
892
893 status = ops->set_variable(name, vendor, attributes, *size, data);
894 if (status != EFI_SUCCESS) {
895 err = efi_status_to_err(status);
896 goto out;
897 }
898
899 *set = true;
900
901 /*
902 * Writing to the variable may have caused a change in size (which
903 * could either be an append or an overwrite), or the variable to be
904 * deleted. Perform a GetVariable() so we can tell what actually
905 * happened.
906 */
907 *size = 0;
908 status = ops->get_variable(entry->var.VariableName,
909 &entry->var.VendorGuid,
910 NULL, size, NULL);
911
912 if (status == EFI_NOT_FOUND)
913 efivar_entry_list_del_unlock(entry);
914 else
915 spin_unlock_irq(&__efivars->lock);
916
917 if (status && status != EFI_BUFFER_TOO_SMALL)
918 return efi_status_to_err(status);
919
920 return 0;
921
922 out:
923 spin_unlock_irq(&__efivars->lock);
924 return err;
925
926 }
927 EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
928
929 /**
930 * efivar_entry_iter_begin - begin iterating the variable list
931 *
932 * Lock the variable list to prevent entry insertion and removal until
933 * efivar_entry_iter_end() is called. This function is usually used in
934 * conjunction with __efivar_entry_iter() or efivar_entry_iter().
935 */
936 void efivar_entry_iter_begin(void)
937 {
938 spin_lock_irq(&__efivars->lock);
939 }
940 EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
941
942 /**
943 * efivar_entry_iter_end - finish iterating the variable list
944 *
945 * Unlock the variable list and allow modifications to the list again.
946 */
947 void efivar_entry_iter_end(void)
948 {
949 spin_unlock_irq(&__efivars->lock);
950 }
951 EXPORT_SYMBOL_GPL(efivar_entry_iter_end);
952
953 /**
954 * __efivar_entry_iter - iterate over variable list
955 * @func: callback function
956 * @head: head of the variable list
957 * @data: function-specific data to pass to callback
958 * @prev: entry to begin iterating from
959 *
960 * Iterate over the list of EFI variables and call @func with every
961 * entry on the list. It is safe for @func to remove entries in the
962 * list via efivar_entry_delete().
963 *
964 * You MUST call efivar_enter_iter_begin() before this function, and
965 * efivar_entry_iter_end() afterwards.
966 *
967 * It is possible to begin iteration from an arbitrary entry within
968 * the list by passing @prev. @prev is updated on return to point to
969 * the last entry passed to @func. To begin iterating from the
970 * beginning of the list @prev must be %NULL.
971 *
972 * The restrictions for @func are the same as documented for
973 * efivar_entry_iter().
974 */
975 int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
976 struct list_head *head, void *data,
977 struct efivar_entry **prev)
978 {
979 struct efivar_entry *entry, *n;
980 int err = 0;
981
982 if (!prev || !*prev) {
983 list_for_each_entry_safe(entry, n, head, list) {
984 err = func(entry, data);
985 if (err)
986 break;
987 }
988
989 if (prev)
990 *prev = entry;
991
992 return err;
993 }
994
995
996 list_for_each_entry_safe_continue((*prev), n, head, list) {
997 err = func(*prev, data);
998 if (err)
999 break;
1000 }
1001
1002 return err;
1003 }
1004 EXPORT_SYMBOL_GPL(__efivar_entry_iter);
1005
1006 /**
1007 * efivar_entry_iter - iterate over variable list
1008 * @func: callback function
1009 * @head: head of variable list
1010 * @data: function-specific data to pass to callback
1011 *
1012 * Iterate over the list of EFI variables and call @func with every
1013 * entry on the list. It is safe for @func to remove entries in the
1014 * list via efivar_entry_delete() while iterating.
1015 *
1016 * Some notes for the callback function:
1017 * - a non-zero return value indicates an error and terminates the loop
1018 * - @func is called from atomic context
1019 */
1020 int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
1021 struct list_head *head, void *data)
1022 {
1023 int err = 0;
1024
1025 efivar_entry_iter_begin();
1026 err = __efivar_entry_iter(func, head, data, NULL);
1027 efivar_entry_iter_end();
1028
1029 return err;
1030 }
1031 EXPORT_SYMBOL_GPL(efivar_entry_iter);
1032
1033 /**
1034 * efivars_kobject - get the kobject for the registered efivars
1035 *
1036 * If efivars_register() has not been called we return NULL,
1037 * otherwise return the kobject used at registration time.
1038 */
1039 struct kobject *efivars_kobject(void)
1040 {
1041 if (!__efivars)
1042 return NULL;
1043
1044 return __efivars->kobject;
1045 }
1046 EXPORT_SYMBOL_GPL(efivars_kobject);
1047
1048 /**
1049 * efivar_run_worker - schedule the efivar worker thread
1050 */
1051 void efivar_run_worker(void)
1052 {
1053 if (efivar_wq_enabled)
1054 schedule_work(&efivar_work);
1055 }
1056 EXPORT_SYMBOL_GPL(efivar_run_worker);
1057
1058 /**
1059 * efivars_register - register an efivars
1060 * @efivars: efivars to register
1061 * @ops: efivars operations
1062 * @kobject: @efivars-specific kobject
1063 *
1064 * Only a single efivars can be registered at any time.
1065 */
1066 int efivars_register(struct efivars *efivars,
1067 const struct efivar_operations *ops,
1068 struct kobject *kobject)
1069 {
1070 spin_lock_init(&efivars->lock);
1071 efivars->ops = ops;
1072 efivars->kobject = kobject;
1073
1074 __efivars = efivars;
1075
1076 return 0;
1077 }
1078 EXPORT_SYMBOL_GPL(efivars_register);
1079
1080 /**
1081 * efivars_unregister - unregister an efivars
1082 * @efivars: efivars to unregister
1083 *
1084 * The caller must have already removed every entry from the list,
1085 * failure to do so is an error.
1086 */
1087 int efivars_unregister(struct efivars *efivars)
1088 {
1089 int rv;
1090
1091 if (!__efivars) {
1092 printk(KERN_ERR "efivars not registered\n");
1093 rv = -EINVAL;
1094 goto out;
1095 }
1096
1097 if (__efivars != efivars) {
1098 rv = -EINVAL;
1099 goto out;
1100 }
1101
1102 __efivars = NULL;
1103
1104 rv = 0;
1105 out:
1106 return rv;
1107 }
1108 EXPORT_SYMBOL_GPL(efivars_unregister);