0012331d5a3d03e6836967016e2195537d22641b
[deliverable/linux.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 efi_guid_t vendor;
169 char *name;
170 bool (*validate)(efi_char16_t *var_name, int match, u8 *data,
171 unsigned long len);
172 };
173
174 /*
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.
177 *
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.
181 *
182 * Note that it's sorted by {vendor,name}, but globbed names must come after
183 * any other name with the same prefix.
184 */
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 },
203 };
204
205 /*
206 * Check if @var_name matches the pattern given in @match_name.
207 *
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.
215 */
216 static bool
217 variable_matches(const char *var_name, size_t len, const char *match_name,
218 int *match)
219 {
220 for (*match = 0; ; (*match)++) {
221 char c = match_name[*match];
222
223 switch (c) {
224 case '*':
225 /* Wildcard in @match_name means we've matched. */
226 return true;
227
228 case '\0':
229 /* @match_name has ended. Has @var_name too? */
230 return (*match == len);
231
232 default:
233 /*
234 * We've reached a non-wildcard char in @match_name.
235 * Continue only if there's an identical character in
236 * @var_name.
237 */
238 if (*match < len && c == var_name[*match])
239 continue;
240 return false;
241 }
242 }
243 }
244
245 bool
246 efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data,
247 unsigned long data_size)
248 {
249 int i;
250 unsigned long utf8_size;
251 u8 *utf8_name;
252
253 utf8_size = ucs2_utf8size(var_name);
254 utf8_name = kmalloc(utf8_size + 1, GFP_KERNEL);
255 if (!utf8_name)
256 return false;
257
258 ucs2_as_utf8(utf8_name, var_name, utf8_size);
259 utf8_name[utf8_size] = '\0';
260
261 for (i = 0; variable_validate[i].name[0] != '\0'; i++) {
262 const char *name = variable_validate[i].name;
263 int match = 0;
264
265 if (efi_guidcmp(vendor, variable_validate[i].vendor))
266 continue;
267
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);
274 }
275 }
276 kfree(utf8_name);
277 return true;
278 }
279 EXPORT_SYMBOL_GPL(efivar_validate);
280
281 bool
282 efivar_variable_is_removable(efi_guid_t vendor, const char *var_name,
283 size_t len)
284 {
285 int i;
286 bool found = false;
287 int match = 0;
288
289 /*
290 * Check if our variable is in the validated variables list
291 */
292 for (i = 0; variable_validate[i].name[0] != '\0'; i++) {
293 if (efi_guidcmp(variable_validate[i].vendor, vendor))
294 continue;
295
296 if (variable_matches(var_name, len,
297 variable_validate[i].name, &match)) {
298 found = true;
299 break;
300 }
301 }
302
303 /*
304 * If it's in our list, it is removable.
305 */
306 return found;
307 }
308 EXPORT_SYMBOL_GPL(efivar_variable_is_removable);
309
310 static efi_status_t
311 check_var_size(u32 attributes, unsigned long size)
312 {
313 const struct efivar_operations *fops = __efivars->ops;
314
315 if (!fops->query_variable_store)
316 return EFI_UNSUPPORTED;
317
318 return fops->query_variable_store(attributes, size, false);
319 }
320
321 static efi_status_t
322 check_var_size_nonblocking(u32 attributes, unsigned long size)
323 {
324 const struct efivar_operations *fops = __efivars->ops;
325
326 if (!fops->query_variable_store)
327 return EFI_UNSUPPORTED;
328
329 return fops->query_variable_store(attributes, size, true);
330 }
331
332 static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor,
333 struct list_head *head)
334 {
335 struct efivar_entry *entry, *n;
336 unsigned long strsize1, strsize2;
337 bool found = false;
338
339 strsize1 = ucs2_strsize(variable_name, 1024);
340 list_for_each_entry_safe(entry, n, head, list) {
341 strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
342 if (strsize1 == strsize2 &&
343 !memcmp(variable_name, &(entry->var.VariableName),
344 strsize2) &&
345 !efi_guidcmp(entry->var.VendorGuid,
346 *vendor)) {
347 found = true;
348 break;
349 }
350 }
351 return found;
352 }
353
354 /*
355 * Returns the size of variable_name, in bytes, including the
356 * terminating NULL character, or variable_name_size if no NULL
357 * character is found among the first variable_name_size bytes.
358 */
359 static unsigned long var_name_strnsize(efi_char16_t *variable_name,
360 unsigned long variable_name_size)
361 {
362 unsigned long len;
363 efi_char16_t c;
364
365 /*
366 * The variable name is, by definition, a NULL-terminated
367 * string, so make absolutely sure that variable_name_size is
368 * the value we expect it to be. If not, return the real size.
369 */
370 for (len = 2; len <= variable_name_size; len += sizeof(c)) {
371 c = variable_name[(len / sizeof(c)) - 1];
372 if (!c)
373 break;
374 }
375
376 return min(len, variable_name_size);
377 }
378
379 /*
380 * Print a warning when duplicate EFI variables are encountered and
381 * disable the sysfs workqueue since the firmware is buggy.
382 */
383 static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid,
384 unsigned long len16)
385 {
386 size_t i, len8 = len16 / sizeof(efi_char16_t);
387 char *str8;
388
389 /*
390 * Disable the workqueue since the algorithm it uses for
391 * detecting new variables won't work with this buggy
392 * implementation of GetNextVariableName().
393 */
394 efivar_wq_enabled = false;
395
396 str8 = kzalloc(len8, GFP_KERNEL);
397 if (!str8)
398 return;
399
400 for (i = 0; i < len8; i++)
401 str8[i] = str16[i];
402
403 printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
404 str8, vendor_guid);
405 kfree(str8);
406 }
407
408 /**
409 * efivar_init - build the initial list of EFI variables
410 * @func: callback function to invoke for every variable
411 * @data: function-specific data to pass to @func
412 * @atomic: do we need to execute the @func-loop atomically?
413 * @duplicates: error if we encounter duplicates on @head?
414 * @head: initialised head of variable list
415 *
416 * Get every EFI variable from the firmware and invoke @func. @func
417 * should call efivar_entry_add() to build the list of variables.
418 *
419 * Returns 0 on success, or a kernel error code on failure.
420 */
421 int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
422 void *data, bool atomic, bool duplicates,
423 struct list_head *head)
424 {
425 const struct efivar_operations *ops = __efivars->ops;
426 unsigned long variable_name_size = 1024;
427 efi_char16_t *variable_name;
428 efi_status_t status;
429 efi_guid_t vendor_guid;
430 int err = 0;
431
432 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
433 if (!variable_name) {
434 printk(KERN_ERR "efivars: Memory allocation failed.\n");
435 return -ENOMEM;
436 }
437
438 spin_lock_irq(&__efivars->lock);
439
440 /*
441 * Per EFI spec, the maximum storage allocated for both
442 * the variable name and variable data is 1024 bytes.
443 */
444
445 do {
446 variable_name_size = 1024;
447
448 status = ops->get_next_variable(&variable_name_size,
449 variable_name,
450 &vendor_guid);
451 switch (status) {
452 case EFI_SUCCESS:
453 if (!atomic)
454 spin_unlock_irq(&__efivars->lock);
455
456 variable_name_size = var_name_strnsize(variable_name,
457 variable_name_size);
458
459 /*
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.
466 */
467 if (duplicates &&
468 variable_is_present(variable_name, &vendor_guid, head)) {
469 dup_variable_bug(variable_name, &vendor_guid,
470 variable_name_size);
471 if (!atomic)
472 spin_lock_irq(&__efivars->lock);
473
474 status = EFI_NOT_FOUND;
475 break;
476 }
477
478 err = func(variable_name, vendor_guid, variable_name_size, data);
479 if (err)
480 status = EFI_NOT_FOUND;
481
482 if (!atomic)
483 spin_lock_irq(&__efivars->lock);
484
485 break;
486 case EFI_NOT_FOUND:
487 break;
488 default:
489 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
490 status);
491 status = EFI_NOT_FOUND;
492 break;
493 }
494
495 } while (status != EFI_NOT_FOUND);
496
497 spin_unlock_irq(&__efivars->lock);
498
499 kfree(variable_name);
500
501 return err;
502 }
503 EXPORT_SYMBOL_GPL(efivar_init);
504
505 /**
506 * efivar_entry_add - add entry to variable list
507 * @entry: entry to add to list
508 * @head: list head
509 */
510 void efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
511 {
512 spin_lock_irq(&__efivars->lock);
513 list_add(&entry->list, head);
514 spin_unlock_irq(&__efivars->lock);
515 }
516 EXPORT_SYMBOL_GPL(efivar_entry_add);
517
518 /**
519 * efivar_entry_remove - remove entry from variable list
520 * @entry: entry to remove from list
521 */
522 void efivar_entry_remove(struct efivar_entry *entry)
523 {
524 spin_lock_irq(&__efivars->lock);
525 list_del(&entry->list);
526 spin_unlock_irq(&__efivars->lock);
527 }
528 EXPORT_SYMBOL_GPL(efivar_entry_remove);
529
530 /*
531 * efivar_entry_list_del_unlock - remove entry from variable list
532 * @entry: entry to remove
533 *
534 * Remove @entry from the variable list and release the list lock.
535 *
536 * NOTE: slightly weird locking semantics here - we expect to be
537 * called with the efivars lock already held, and we release it before
538 * returning. This is because this function is usually called after
539 * set_variable() while the lock is still held.
540 */
541 static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
542 {
543 lockdep_assert_held(&__efivars->lock);
544
545 list_del(&entry->list);
546 spin_unlock_irq(&__efivars->lock);
547 }
548
549 /**
550 * __efivar_entry_delete - delete an EFI variable
551 * @entry: entry containing EFI variable to delete
552 *
553 * Delete the variable from the firmware but leave @entry on the
554 * variable list.
555 *
556 * This function differs from efivar_entry_delete() because it does
557 * not remove @entry from the variable list. Also, it is safe to be
558 * called from within a efivar_entry_iter_begin() and
559 * efivar_entry_iter_end() region, unlike efivar_entry_delete().
560 *
561 * Returns 0 on success, or a converted EFI status code if
562 * set_variable() fails.
563 */
564 int __efivar_entry_delete(struct efivar_entry *entry)
565 {
566 const struct efivar_operations *ops = __efivars->ops;
567 efi_status_t status;
568
569 lockdep_assert_held(&__efivars->lock);
570
571 status = ops->set_variable(entry->var.VariableName,
572 &entry->var.VendorGuid,
573 0, 0, NULL);
574
575 return efi_status_to_err(status);
576 }
577 EXPORT_SYMBOL_GPL(__efivar_entry_delete);
578
579 /**
580 * efivar_entry_delete - delete variable and remove entry from list
581 * @entry: entry containing variable to delete
582 *
583 * Delete the variable from the firmware and remove @entry from the
584 * variable list. It is the caller's responsibility to free @entry
585 * once we return.
586 *
587 * Returns 0 on success, or a converted EFI status code if
588 * set_variable() fails.
589 */
590 int efivar_entry_delete(struct efivar_entry *entry)
591 {
592 const struct efivar_operations *ops = __efivars->ops;
593 efi_status_t status;
594
595 spin_lock_irq(&__efivars->lock);
596 status = ops->set_variable(entry->var.VariableName,
597 &entry->var.VendorGuid,
598 0, 0, NULL);
599 if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
600 spin_unlock_irq(&__efivars->lock);
601 return efi_status_to_err(status);
602 }
603
604 efivar_entry_list_del_unlock(entry);
605 return 0;
606 }
607 EXPORT_SYMBOL_GPL(efivar_entry_delete);
608
609 /**
610 * efivar_entry_set - call set_variable()
611 * @entry: entry containing the EFI variable to write
612 * @attributes: variable attributes
613 * @size: size of @data buffer
614 * @data: buffer containing variable data
615 * @head: head of variable list
616 *
617 * Calls set_variable() for an EFI variable. If creating a new EFI
618 * variable, this function is usually followed by efivar_entry_add().
619 *
620 * Before writing the variable, the remaining EFI variable storage
621 * space is checked to ensure there is enough room available.
622 *
623 * If @head is not NULL a lookup is performed to determine whether
624 * the entry is already on the list.
625 *
626 * Returns 0 on success, -EEXIST if a lookup is performed and the entry
627 * already exists on the list, or a converted EFI status code if
628 * set_variable() fails.
629 */
630 int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
631 unsigned long size, void *data, struct list_head *head)
632 {
633 const struct efivar_operations *ops = __efivars->ops;
634 efi_status_t status;
635 efi_char16_t *name = entry->var.VariableName;
636 efi_guid_t vendor = entry->var.VendorGuid;
637
638 spin_lock_irq(&__efivars->lock);
639
640 if (head && efivar_entry_find(name, vendor, head, false)) {
641 spin_unlock_irq(&__efivars->lock);
642 return -EEXIST;
643 }
644
645 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
646 if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
647 status = ops->set_variable(name, &vendor,
648 attributes, size, data);
649
650 spin_unlock_irq(&__efivars->lock);
651
652 return efi_status_to_err(status);
653
654 }
655 EXPORT_SYMBOL_GPL(efivar_entry_set);
656
657 /*
658 * efivar_entry_set_nonblocking - call set_variable_nonblocking()
659 *
660 * This function is guaranteed to not block and is suitable for calling
661 * from crash/panic handlers.
662 *
663 * Crucially, this function will not block if it cannot acquire
664 * __efivars->lock. Instead, it returns -EBUSY.
665 */
666 static int
667 efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
668 u32 attributes, unsigned long size, void *data)
669 {
670 const struct efivar_operations *ops = __efivars->ops;
671 unsigned long flags;
672 efi_status_t status;
673
674 if (!spin_trylock_irqsave(&__efivars->lock, flags))
675 return -EBUSY;
676
677 status = check_var_size_nonblocking(attributes,
678 size + ucs2_strsize(name, 1024));
679 if (status != EFI_SUCCESS) {
680 spin_unlock_irqrestore(&__efivars->lock, flags);
681 return -ENOSPC;
682 }
683
684 status = ops->set_variable_nonblocking(name, &vendor, attributes,
685 size, data);
686
687 spin_unlock_irqrestore(&__efivars->lock, flags);
688 return efi_status_to_err(status);
689 }
690
691 /**
692 * efivar_entry_set_safe - call set_variable() if enough space in firmware
693 * @name: buffer containing the variable name
694 * @vendor: variable vendor guid
695 * @attributes: variable attributes
696 * @block: can we block in this context?
697 * @size: size of @data buffer
698 * @data: buffer containing variable data
699 *
700 * Ensures there is enough free storage in the firmware for this variable, and
701 * if so, calls set_variable(). If creating a new EFI variable, this function
702 * is usually followed by efivar_entry_add().
703 *
704 * Returns 0 on success, -ENOSPC if the firmware does not have enough
705 * space for set_variable() to succeed, or a converted EFI status code
706 * if set_variable() fails.
707 */
708 int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
709 bool block, unsigned long size, void *data)
710 {
711 const struct efivar_operations *ops = __efivars->ops;
712 unsigned long flags;
713 efi_status_t status;
714
715 if (!ops->query_variable_store)
716 return -ENOSYS;
717
718 /*
719 * If the EFI variable backend provides a non-blocking
720 * ->set_variable() operation and we're in a context where we
721 * cannot block, then we need to use it to avoid live-locks,
722 * since the implication is that the regular ->set_variable()
723 * will block.
724 *
725 * If no ->set_variable_nonblocking() is provided then
726 * ->set_variable() is assumed to be non-blocking.
727 */
728 if (!block && ops->set_variable_nonblocking)
729 return efivar_entry_set_nonblocking(name, vendor, attributes,
730 size, data);
731
732 if (!block) {
733 if (!spin_trylock_irqsave(&__efivars->lock, flags))
734 return -EBUSY;
735 } else {
736 spin_lock_irqsave(&__efivars->lock, flags);
737 }
738
739 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
740 if (status != EFI_SUCCESS) {
741 spin_unlock_irqrestore(&__efivars->lock, flags);
742 return -ENOSPC;
743 }
744
745 status = ops->set_variable(name, &vendor, attributes, size, data);
746
747 spin_unlock_irqrestore(&__efivars->lock, flags);
748
749 return efi_status_to_err(status);
750 }
751 EXPORT_SYMBOL_GPL(efivar_entry_set_safe);
752
753 /**
754 * efivar_entry_find - search for an entry
755 * @name: the EFI variable name
756 * @guid: the EFI variable vendor's guid
757 * @head: head of the variable list
758 * @remove: should we remove the entry from the list?
759 *
760 * Search for an entry on the variable list that has the EFI variable
761 * name @name and vendor guid @guid. If an entry is found on the list
762 * and @remove is true, the entry is removed from the list.
763 *
764 * The caller MUST call efivar_entry_iter_begin() and
765 * efivar_entry_iter_end() before and after the invocation of this
766 * function, respectively.
767 *
768 * Returns the entry if found on the list, %NULL otherwise.
769 */
770 struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
771 struct list_head *head, bool remove)
772 {
773 struct efivar_entry *entry, *n;
774 int strsize1, strsize2;
775 bool found = false;
776
777 lockdep_assert_held(&__efivars->lock);
778
779 list_for_each_entry_safe(entry, n, head, list) {
780 strsize1 = ucs2_strsize(name, 1024);
781 strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
782 if (strsize1 == strsize2 &&
783 !memcmp(name, &(entry->var.VariableName), strsize1) &&
784 !efi_guidcmp(guid, entry->var.VendorGuid)) {
785 found = true;
786 break;
787 }
788 }
789
790 if (!found)
791 return NULL;
792
793 if (remove) {
794 if (entry->scanning) {
795 /*
796 * The entry will be deleted
797 * after scanning is completed.
798 */
799 entry->deleting = true;
800 } else
801 list_del(&entry->list);
802 }
803
804 return entry;
805 }
806 EXPORT_SYMBOL_GPL(efivar_entry_find);
807
808 /**
809 * efivar_entry_size - obtain the size of a variable
810 * @entry: entry for this variable
811 * @size: location to store the variable's size
812 */
813 int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
814 {
815 const struct efivar_operations *ops = __efivars->ops;
816 efi_status_t status;
817
818 *size = 0;
819
820 spin_lock_irq(&__efivars->lock);
821 status = ops->get_variable(entry->var.VariableName,
822 &entry->var.VendorGuid, NULL, size, NULL);
823 spin_unlock_irq(&__efivars->lock);
824
825 if (status != EFI_BUFFER_TOO_SMALL)
826 return efi_status_to_err(status);
827
828 return 0;
829 }
830 EXPORT_SYMBOL_GPL(efivar_entry_size);
831
832 /**
833 * __efivar_entry_get - call get_variable()
834 * @entry: read data for this variable
835 * @attributes: variable attributes
836 * @size: size of @data buffer
837 * @data: buffer to store variable data
838 *
839 * The caller MUST call efivar_entry_iter_begin() and
840 * efivar_entry_iter_end() before and after the invocation of this
841 * function, respectively.
842 */
843 int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
844 unsigned long *size, void *data)
845 {
846 const struct efivar_operations *ops = __efivars->ops;
847 efi_status_t status;
848
849 lockdep_assert_held(&__efivars->lock);
850
851 status = ops->get_variable(entry->var.VariableName,
852 &entry->var.VendorGuid,
853 attributes, size, data);
854
855 return efi_status_to_err(status);
856 }
857 EXPORT_SYMBOL_GPL(__efivar_entry_get);
858
859 /**
860 * efivar_entry_get - call get_variable()
861 * @entry: read data for this variable
862 * @attributes: variable attributes
863 * @size: size of @data buffer
864 * @data: buffer to store variable data
865 */
866 int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
867 unsigned long *size, void *data)
868 {
869 const struct efivar_operations *ops = __efivars->ops;
870 efi_status_t status;
871
872 spin_lock_irq(&__efivars->lock);
873 status = ops->get_variable(entry->var.VariableName,
874 &entry->var.VendorGuid,
875 attributes, size, data);
876 spin_unlock_irq(&__efivars->lock);
877
878 return efi_status_to_err(status);
879 }
880 EXPORT_SYMBOL_GPL(efivar_entry_get);
881
882 /**
883 * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
884 * @entry: entry containing variable to set and get
885 * @attributes: attributes of variable to be written
886 * @size: size of data buffer
887 * @data: buffer containing data to write
888 * @set: did the set_variable() call succeed?
889 *
890 * This is a pretty special (complex) function. See efivarfs_file_write().
891 *
892 * Atomically call set_variable() for @entry and if the call is
893 * successful, return the new size of the variable from get_variable()
894 * in @size. The success of set_variable() is indicated by @set.
895 *
896 * Returns 0 on success, -EINVAL if the variable data is invalid,
897 * -ENOSPC if the firmware does not have enough available space, or a
898 * converted EFI status code if either of set_variable() or
899 * get_variable() fail.
900 *
901 * If the EFI variable does not exist when calling set_variable()
902 * (EFI_NOT_FOUND), @entry is removed from the variable list.
903 */
904 int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
905 unsigned long *size, void *data, bool *set)
906 {
907 const struct efivar_operations *ops = __efivars->ops;
908 efi_char16_t *name = entry->var.VariableName;
909 efi_guid_t *vendor = &entry->var.VendorGuid;
910 efi_status_t status;
911 int err;
912
913 *set = false;
914
915 if (efivar_validate(*vendor, name, data, *size) == false)
916 return -EINVAL;
917
918 /*
919 * The lock here protects the get_variable call, the conditional
920 * set_variable call, and removal of the variable from the efivars
921 * list (in the case of an authenticated delete).
922 */
923 spin_lock_irq(&__efivars->lock);
924
925 /*
926 * Ensure that the available space hasn't shrunk below the safe level
927 */
928 status = check_var_size(attributes, *size + ucs2_strsize(name, 1024));
929 if (status != EFI_SUCCESS) {
930 if (status != EFI_UNSUPPORTED) {
931 err = efi_status_to_err(status);
932 goto out;
933 }
934
935 if (*size > 65536) {
936 err = -ENOSPC;
937 goto out;
938 }
939 }
940
941 status = ops->set_variable(name, vendor, attributes, *size, data);
942 if (status != EFI_SUCCESS) {
943 err = efi_status_to_err(status);
944 goto out;
945 }
946
947 *set = true;
948
949 /*
950 * Writing to the variable may have caused a change in size (which
951 * could either be an append or an overwrite), or the variable to be
952 * deleted. Perform a GetVariable() so we can tell what actually
953 * happened.
954 */
955 *size = 0;
956 status = ops->get_variable(entry->var.VariableName,
957 &entry->var.VendorGuid,
958 NULL, size, NULL);
959
960 if (status == EFI_NOT_FOUND)
961 efivar_entry_list_del_unlock(entry);
962 else
963 spin_unlock_irq(&__efivars->lock);
964
965 if (status && status != EFI_BUFFER_TOO_SMALL)
966 return efi_status_to_err(status);
967
968 return 0;
969
970 out:
971 spin_unlock_irq(&__efivars->lock);
972 return err;
973
974 }
975 EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
976
977 /**
978 * efivar_entry_iter_begin - begin iterating the variable list
979 *
980 * Lock the variable list to prevent entry insertion and removal until
981 * efivar_entry_iter_end() is called. This function is usually used in
982 * conjunction with __efivar_entry_iter() or efivar_entry_iter().
983 */
984 void efivar_entry_iter_begin(void)
985 {
986 spin_lock_irq(&__efivars->lock);
987 }
988 EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
989
990 /**
991 * efivar_entry_iter_end - finish iterating the variable list
992 *
993 * Unlock the variable list and allow modifications to the list again.
994 */
995 void efivar_entry_iter_end(void)
996 {
997 spin_unlock_irq(&__efivars->lock);
998 }
999 EXPORT_SYMBOL_GPL(efivar_entry_iter_end);
1000
1001 /**
1002 * __efivar_entry_iter - iterate over variable list
1003 * @func: callback function
1004 * @head: head of the variable list
1005 * @data: function-specific data to pass to callback
1006 * @prev: entry to begin iterating from
1007 *
1008 * Iterate over the list of EFI variables and call @func with every
1009 * entry on the list. It is safe for @func to remove entries in the
1010 * list via efivar_entry_delete().
1011 *
1012 * You MUST call efivar_enter_iter_begin() before this function, and
1013 * efivar_entry_iter_end() afterwards.
1014 *
1015 * It is possible to begin iteration from an arbitrary entry within
1016 * the list by passing @prev. @prev is updated on return to point to
1017 * the last entry passed to @func. To begin iterating from the
1018 * beginning of the list @prev must be %NULL.
1019 *
1020 * The restrictions for @func are the same as documented for
1021 * efivar_entry_iter().
1022 */
1023 int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
1024 struct list_head *head, void *data,
1025 struct efivar_entry **prev)
1026 {
1027 struct efivar_entry *entry, *n;
1028 int err = 0;
1029
1030 if (!prev || !*prev) {
1031 list_for_each_entry_safe(entry, n, head, list) {
1032 err = func(entry, data);
1033 if (err)
1034 break;
1035 }
1036
1037 if (prev)
1038 *prev = entry;
1039
1040 return err;
1041 }
1042
1043
1044 list_for_each_entry_safe_continue((*prev), n, head, list) {
1045 err = func(*prev, data);
1046 if (err)
1047 break;
1048 }
1049
1050 return err;
1051 }
1052 EXPORT_SYMBOL_GPL(__efivar_entry_iter);
1053
1054 /**
1055 * efivar_entry_iter - iterate over variable list
1056 * @func: callback function
1057 * @head: head of variable list
1058 * @data: function-specific data to pass to callback
1059 *
1060 * Iterate over the list of EFI variables and call @func with every
1061 * entry on the list. It is safe for @func to remove entries in the
1062 * list via efivar_entry_delete() while iterating.
1063 *
1064 * Some notes for the callback function:
1065 * - a non-zero return value indicates an error and terminates the loop
1066 * - @func is called from atomic context
1067 */
1068 int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
1069 struct list_head *head, void *data)
1070 {
1071 int err = 0;
1072
1073 efivar_entry_iter_begin();
1074 err = __efivar_entry_iter(func, head, data, NULL);
1075 efivar_entry_iter_end();
1076
1077 return err;
1078 }
1079 EXPORT_SYMBOL_GPL(efivar_entry_iter);
1080
1081 /**
1082 * efivars_kobject - get the kobject for the registered efivars
1083 *
1084 * If efivars_register() has not been called we return NULL,
1085 * otherwise return the kobject used at registration time.
1086 */
1087 struct kobject *efivars_kobject(void)
1088 {
1089 if (!__efivars)
1090 return NULL;
1091
1092 return __efivars->kobject;
1093 }
1094 EXPORT_SYMBOL_GPL(efivars_kobject);
1095
1096 /**
1097 * efivar_run_worker - schedule the efivar worker thread
1098 */
1099 void efivar_run_worker(void)
1100 {
1101 if (efivar_wq_enabled)
1102 schedule_work(&efivar_work);
1103 }
1104 EXPORT_SYMBOL_GPL(efivar_run_worker);
1105
1106 /**
1107 * efivars_register - register an efivars
1108 * @efivars: efivars to register
1109 * @ops: efivars operations
1110 * @kobject: @efivars-specific kobject
1111 *
1112 * Only a single efivars can be registered at any time.
1113 */
1114 int efivars_register(struct efivars *efivars,
1115 const struct efivar_operations *ops,
1116 struct kobject *kobject)
1117 {
1118 spin_lock_init(&efivars->lock);
1119 efivars->ops = ops;
1120 efivars->kobject = kobject;
1121
1122 __efivars = efivars;
1123
1124 return 0;
1125 }
1126 EXPORT_SYMBOL_GPL(efivars_register);
1127
1128 /**
1129 * efivars_unregister - unregister an efivars
1130 * @efivars: efivars to unregister
1131 *
1132 * The caller must have already removed every entry from the list,
1133 * failure to do so is an error.
1134 */
1135 int efivars_unregister(struct efivars *efivars)
1136 {
1137 int rv;
1138
1139 if (!__efivars) {
1140 printk(KERN_ERR "efivars not registered\n");
1141 rv = -EINVAL;
1142 goto out;
1143 }
1144
1145 if (__efivars != efivars) {
1146 rv = -EINVAL;
1147 goto out;
1148 }
1149
1150 __efivars = NULL;
1151
1152 rv = 0;
1153 out:
1154 return rv;
1155 }
1156 EXPORT_SYMBOL_GPL(efivars_unregister);
This page took 0.052397 seconds and 4 git commands to generate.