blob: f0a43646a2f3f4b36ddd64af33d39920c3cb006a [file] [log] [blame]
Tom Gundersena9499fa2013-02-08 15:37:06 +00001/*
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>
Matt Fleminga614e192013-04-30 11:30:24 +010035#include <linux/ucs2_string.h>
Tom Gundersena9499fa2013-02-08 15:37:06 +000036
37/* Private pointer to registered efivars */
38static struct efivars *__efivars;
39
40static bool efivar_wq_enabled = true;
41DECLARE_WORK(efivar_work, NULL);
42EXPORT_SYMBOL_GPL(efivar_work);
43
44static bool
Matt Fleminga5d92ad2014-03-17 10:57:00 +000045validate_device_path(efi_char16_t *var_name, int match, u8 *buffer,
Tom Gundersena9499fa2013-02-08 15:37:06 +000046 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
77static bool
Matt Fleminga5d92ad2014-03-17 10:57:00 +000078validate_boot_order(efi_char16_t *var_name, int match, u8 *buffer,
Tom Gundersena9499fa2013-02-08 15:37:06 +000079 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
88static bool
Matt Fleminga5d92ad2014-03-17 10:57:00 +000089validate_load_option(efi_char16_t *var_name, int match, u8 *buffer,
Tom Gundersena9499fa2013-02-08 15:37:06 +000090 unsigned long len)
91{
92 u16 filepathlength;
93 int i, desclength = 0, namelen;
94
Matt Fleminga5d92ad2014-03-17 10:57:00 +000095 namelen = ucs2_strnlen(var_name, EFI_VAR_NAME_LEN);
Tom Gundersena9499fa2013-02-08 15:37:06 +000096
97 /* Either "Boot" or "Driver" followed by four digits of hex */
98 for (i = match; i < match+4; i++) {
Matt Fleminga5d92ad2014-03-17 10:57:00 +000099 if (var_name[i] > 127 ||
100 hex_to_bin(var_name[i] & 0xff) < 0)
Tom Gundersena9499fa2013-02-08 15:37:06 +0000101 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 */
Matt Fleminga614e192013-04-30 11:30:24 +0100118 desclength = ucs2_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
Tom Gundersena9499fa2013-02-08 15:37:06 +0000119
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 */
Matt Fleminga5d92ad2014-03-17 10:57:00 +0000135 return validate_device_path(var_name, match, buffer + desclength + 6,
Tom Gundersena9499fa2013-02-08 15:37:06 +0000136 filepathlength);
137}
138
139static bool
Matt Fleminga5d92ad2014-03-17 10:57:00 +0000140validate_uint16(efi_char16_t *var_name, int match, u8 *buffer,
Tom Gundersena9499fa2013-02-08 15:37:06 +0000141 unsigned long len)
142{
143 /* A single 16-bit integer */
144 if (len != 2)
145 return false;
146
147 return true;
148}
149
150static bool
Matt Fleminga5d92ad2014-03-17 10:57:00 +0000151validate_ascii_string(efi_char16_t *var_name, int match, u8 *buffer,
Tom Gundersena9499fa2013-02-08 15:37:06 +0000152 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
167struct variable_validate {
168 char *name;
Matt Fleminga5d92ad2014-03-17 10:57:00 +0000169 bool (*validate)(efi_char16_t *var_name, int match, u8 *data,
Tom Gundersena9499fa2013-02-08 15:37:06 +0000170 unsigned long len);
171};
172
173static 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
191bool
Matt Fleminga5d92ad2014-03-17 10:57:00 +0000192efivar_validate(efi_char16_t *var_name, u8 *data, unsigned long len)
Tom Gundersena9499fa2013-02-08 15:37:06 +0000193{
194 int i;
Matt Fleminga5d92ad2014-03-17 10:57:00 +0000195 u16 *unicode_name = var_name;
Tom Gundersena9499fa2013-02-08 15:37:06 +0000196
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 == '*')
Matt Fleminga5d92ad2014-03-17 10:57:00 +0000211 return variable_validate[i].validate(var_name,
Tom Gundersena9499fa2013-02-08 15:37:06 +0000212 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)
Matt Fleminga5d92ad2014-03-17 10:57:00 +0000220 return variable_validate[i].validate(var_name,
Tom Gundersena9499fa2013-02-08 15:37:06 +0000221 match, data, len);
222 }
223 }
224
225 return true;
226}
227EXPORT_SYMBOL_GPL(efivar_validate);
228
229static efi_status_t
230check_var_size(u32 attributes, unsigned long size)
231{
Tom Gundersena9499fa2013-02-08 15:37:06 +0000232 const struct efivar_operations *fops = __efivars->ops;
233
Matt Fleminga614e192013-04-30 11:30:24 +0100234 if (!fops->query_variable_store)
Tom Gundersena9499fa2013-02-08 15:37:06 +0000235 return EFI_UNSUPPORTED;
236
Matt Fleminga614e192013-04-30 11:30:24 +0100237 return fops->query_variable_store(attributes, size);
Tom Gundersena9499fa2013-02-08 15:37:06 +0000238}
239
240static int efi_status_to_err(efi_status_t status)
241{
242 int err;
243
244 switch (status) {
245 case EFI_SUCCESS:
246 err = 0;
247 break;
248 case EFI_INVALID_PARAMETER:
249 err = -EINVAL;
250 break;
251 case EFI_OUT_OF_RESOURCES:
252 err = -ENOSPC;
253 break;
254 case EFI_DEVICE_ERROR:
255 err = -EIO;
256 break;
257 case EFI_WRITE_PROTECTED:
258 err = -EROFS;
259 break;
260 case EFI_SECURITY_VIOLATION:
261 err = -EACCES;
262 break;
263 case EFI_NOT_FOUND:
264 err = -ENOENT;
265 break;
266 default:
267 err = -EINVAL;
268 }
269
270 return err;
271}
272
273static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor,
274 struct list_head *head)
275{
276 struct efivar_entry *entry, *n;
277 unsigned long strsize1, strsize2;
278 bool found = false;
279
Matt Fleminga614e192013-04-30 11:30:24 +0100280 strsize1 = ucs2_strsize(variable_name, 1024);
Tom Gundersena9499fa2013-02-08 15:37:06 +0000281 list_for_each_entry_safe(entry, n, head, list) {
Matt Fleminga614e192013-04-30 11:30:24 +0100282 strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
Tom Gundersena9499fa2013-02-08 15:37:06 +0000283 if (strsize1 == strsize2 &&
284 !memcmp(variable_name, &(entry->var.VariableName),
285 strsize2) &&
286 !efi_guidcmp(entry->var.VendorGuid,
287 *vendor)) {
288 found = true;
289 break;
290 }
291 }
292 return found;
293}
294
295/*
296 * Returns the size of variable_name, in bytes, including the
297 * terminating NULL character, or variable_name_size if no NULL
298 * character is found among the first variable_name_size bytes.
299 */
300static unsigned long var_name_strnsize(efi_char16_t *variable_name,
301 unsigned long variable_name_size)
302{
303 unsigned long len;
304 efi_char16_t c;
305
306 /*
307 * The variable name is, by definition, a NULL-terminated
308 * string, so make absolutely sure that variable_name_size is
309 * the value we expect it to be. If not, return the real size.
310 */
311 for (len = 2; len <= variable_name_size; len += sizeof(c)) {
312 c = variable_name[(len / sizeof(c)) - 1];
313 if (!c)
314 break;
315 }
316
317 return min(len, variable_name_size);
318}
319
320/*
321 * Print a warning when duplicate EFI variables are encountered and
322 * disable the sysfs workqueue since the firmware is buggy.
323 */
324static void dup_variable_bug(efi_char16_t *s16, efi_guid_t *vendor_guid,
325 unsigned long len16)
326{
327 size_t i, len8 = len16 / sizeof(efi_char16_t);
328 char *s8;
329
330 /*
331 * Disable the workqueue since the algorithm it uses for
332 * detecting new variables won't work with this buggy
333 * implementation of GetNextVariableName().
334 */
335 efivar_wq_enabled = false;
336
337 s8 = kzalloc(len8, GFP_KERNEL);
338 if (!s8)
339 return;
340
341 for (i = 0; i < len8; i++)
342 s8[i] = s16[i];
343
344 printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
345 s8, vendor_guid);
346 kfree(s8);
347}
348
349/**
350 * efivar_init - build the initial list of EFI variables
351 * @func: callback function to invoke for every variable
352 * @data: function-specific data to pass to @func
353 * @atomic: do we need to execute the @func-loop atomically?
354 * @duplicates: error if we encounter duplicates on @head?
355 * @head: initialised head of variable list
356 *
357 * Get every EFI variable from the firmware and invoke @func. @func
358 * should call efivar_entry_add() to build the list of variables.
359 *
360 * Returns 0 on success, or a kernel error code on failure.
361 */
362int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
363 void *data, bool atomic, bool duplicates,
364 struct list_head *head)
365{
366 const struct efivar_operations *ops = __efivars->ops;
367 unsigned long variable_name_size = 1024;
368 efi_char16_t *variable_name;
369 efi_status_t status;
370 efi_guid_t vendor_guid;
371 int err = 0;
372
373 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
374 if (!variable_name) {
375 printk(KERN_ERR "efivars: Memory allocation failed.\n");
376 return -ENOMEM;
377 }
378
379 spin_lock_irq(&__efivars->lock);
380
381 /*
382 * Per EFI spec, the maximum storage allocated for both
383 * the variable name and variable data is 1024 bytes.
384 */
385
386 do {
387 variable_name_size = 1024;
388
389 status = ops->get_next_variable(&variable_name_size,
390 variable_name,
391 &vendor_guid);
392 switch (status) {
393 case EFI_SUCCESS:
394 if (!atomic)
395 spin_unlock_irq(&__efivars->lock);
396
397 variable_name_size = var_name_strnsize(variable_name,
398 variable_name_size);
399
400 /*
401 * Some firmware implementations return the
402 * same variable name on multiple calls to
403 * get_next_variable(). Terminate the loop
404 * immediately as there is no guarantee that
405 * we'll ever see a different variable name,
406 * and may end up looping here forever.
407 */
408 if (duplicates &&
409 variable_is_present(variable_name, &vendor_guid, head)) {
410 dup_variable_bug(variable_name, &vendor_guid,
411 variable_name_size);
412 if (!atomic)
413 spin_lock_irq(&__efivars->lock);
414
415 status = EFI_NOT_FOUND;
416 break;
417 }
418
419 err = func(variable_name, vendor_guid, variable_name_size, data);
420 if (err)
421 status = EFI_NOT_FOUND;
422
423 if (!atomic)
424 spin_lock_irq(&__efivars->lock);
425
426 break;
427 case EFI_NOT_FOUND:
428 break;
429 default:
430 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
431 status);
432 status = EFI_NOT_FOUND;
433 break;
434 }
435
436 } while (status != EFI_NOT_FOUND);
437
438 spin_unlock_irq(&__efivars->lock);
439
440 kfree(variable_name);
441
442 return err;
443}
444EXPORT_SYMBOL_GPL(efivar_init);
445
446/**
447 * efivar_entry_add - add entry to variable list
448 * @entry: entry to add to list
449 * @head: list head
450 */
451void efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
452{
453 spin_lock_irq(&__efivars->lock);
454 list_add(&entry->list, head);
455 spin_unlock_irq(&__efivars->lock);
456}
457EXPORT_SYMBOL_GPL(efivar_entry_add);
458
459/**
460 * efivar_entry_remove - remove entry from variable list
461 * @entry: entry to remove from list
462 */
463void efivar_entry_remove(struct efivar_entry *entry)
464{
465 spin_lock_irq(&__efivars->lock);
466 list_del(&entry->list);
467 spin_unlock_irq(&__efivars->lock);
468}
469EXPORT_SYMBOL_GPL(efivar_entry_remove);
470
471/*
472 * efivar_entry_list_del_unlock - remove entry from variable list
473 * @entry: entry to remove
474 *
475 * Remove @entry from the variable list and release the list lock.
476 *
477 * NOTE: slightly weird locking semantics here - we expect to be
478 * called with the efivars lock already held, and we release it before
479 * returning. This is because this function is usually called after
480 * set_variable() while the lock is still held.
481 */
482static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
483{
484 WARN_ON(!spin_is_locked(&__efivars->lock));
485
486 list_del(&entry->list);
487 spin_unlock_irq(&__efivars->lock);
488}
489
490/**
491 * __efivar_entry_delete - delete an EFI variable
492 * @entry: entry containing EFI variable to delete
493 *
494 * Delete the variable from the firmware but leave @entry on the
495 * variable list.
496 *
497 * This function differs from efivar_entry_delete() because it does
498 * not remove @entry from the variable list. Also, it is safe to be
499 * called from within a efivar_entry_iter_begin() and
500 * efivar_entry_iter_end() region, unlike efivar_entry_delete().
501 *
502 * Returns 0 on success, or a converted EFI status code if
503 * set_variable() fails.
504 */
505int __efivar_entry_delete(struct efivar_entry *entry)
506{
507 const struct efivar_operations *ops = __efivars->ops;
508 efi_status_t status;
509
510 WARN_ON(!spin_is_locked(&__efivars->lock));
511
512 status = ops->set_variable(entry->var.VariableName,
513 &entry->var.VendorGuid,
514 0, 0, NULL);
515
516 return efi_status_to_err(status);
517}
518EXPORT_SYMBOL_GPL(__efivar_entry_delete);
519
520/**
521 * efivar_entry_delete - delete variable and remove entry from list
522 * @entry: entry containing variable to delete
523 *
524 * Delete the variable from the firmware and remove @entry from the
525 * variable list. It is the caller's responsibility to free @entry
526 * once we return.
527 *
528 * Returns 0 on success, or a converted EFI status code if
529 * set_variable() fails.
530 */
531int efivar_entry_delete(struct efivar_entry *entry)
532{
533 const struct efivar_operations *ops = __efivars->ops;
534 efi_status_t status;
535
536 spin_lock_irq(&__efivars->lock);
537 status = ops->set_variable(entry->var.VariableName,
538 &entry->var.VendorGuid,
539 0, 0, NULL);
540 if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
541 spin_unlock_irq(&__efivars->lock);
542 return efi_status_to_err(status);
543 }
544
545 efivar_entry_list_del_unlock(entry);
546 return 0;
547}
548EXPORT_SYMBOL_GPL(efivar_entry_delete);
549
550/**
551 * efivar_entry_set - call set_variable()
552 * @entry: entry containing the EFI variable to write
553 * @attributes: variable attributes
554 * @size: size of @data buffer
555 * @data: buffer containing variable data
556 * @head: head of variable list
557 *
558 * Calls set_variable() for an EFI variable. If creating a new EFI
559 * variable, this function is usually followed by efivar_entry_add().
560 *
561 * Before writing the variable, the remaining EFI variable storage
562 * space is checked to ensure there is enough room available.
563 *
564 * If @head is not NULL a lookup is performed to determine whether
565 * the entry is already on the list.
566 *
567 * Returns 0 on success, -EEXIST if a lookup is performed and the entry
568 * already exists on the list, or a converted EFI status code if
569 * set_variable() fails.
570 */
571int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
572 unsigned long size, void *data, struct list_head *head)
573{
574 const struct efivar_operations *ops = __efivars->ops;
575 efi_status_t status;
576 efi_char16_t *name = entry->var.VariableName;
577 efi_guid_t vendor = entry->var.VendorGuid;
578
579 spin_lock_irq(&__efivars->lock);
580
581 if (head && efivar_entry_find(name, vendor, head, false)) {
582 spin_unlock_irq(&__efivars->lock);
583 return -EEXIST;
584 }
585
Matt Fleminga614e192013-04-30 11:30:24 +0100586 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
Tom Gundersena9499fa2013-02-08 15:37:06 +0000587 if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
588 status = ops->set_variable(name, &vendor,
589 attributes, size, data);
590
591 spin_unlock_irq(&__efivars->lock);
592
593 return efi_status_to_err(status);
594
595}
596EXPORT_SYMBOL_GPL(efivar_entry_set);
597
598/**
599 * efivar_entry_set_safe - call set_variable() if enough space in firmware
600 * @name: buffer containing the variable name
601 * @vendor: variable vendor guid
602 * @attributes: variable attributes
603 * @block: can we block in this context?
604 * @size: size of @data buffer
605 * @data: buffer containing variable data
606 *
607 * Ensures there is enough free storage in the firmware for this variable, and
608 * if so, calls set_variable(). If creating a new EFI variable, this function
609 * is usually followed by efivar_entry_add().
610 *
611 * Returns 0 on success, -ENOSPC if the firmware does not have enough
612 * space for set_variable() to succeed, or a converted EFI status code
613 * if set_variable() fails.
614 */
615int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
616 bool block, unsigned long size, void *data)
617{
618 const struct efivar_operations *ops = __efivars->ops;
619 unsigned long flags;
620 efi_status_t status;
621
Matt Fleminga614e192013-04-30 11:30:24 +0100622 if (!ops->query_variable_store)
Tom Gundersena9499fa2013-02-08 15:37:06 +0000623 return -ENOSYS;
624
Dan Carpenter85c90712013-04-30 10:43:12 +0300625 if (!block) {
626 if (!spin_trylock_irqsave(&__efivars->lock, flags))
627 return -EBUSY;
628 } else {
Tom Gundersena9499fa2013-02-08 15:37:06 +0000629 spin_lock_irqsave(&__efivars->lock, flags);
Dan Carpenter85c90712013-04-30 10:43:12 +0300630 }
Tom Gundersena9499fa2013-02-08 15:37:06 +0000631
Matt Fleminga614e192013-04-30 11:30:24 +0100632 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
Tom Gundersena9499fa2013-02-08 15:37:06 +0000633 if (status != EFI_SUCCESS) {
634 spin_unlock_irqrestore(&__efivars->lock, flags);
635 return -ENOSPC;
636 }
637
638 status = ops->set_variable(name, &vendor, attributes, size, data);
639
640 spin_unlock_irqrestore(&__efivars->lock, flags);
641
642 return efi_status_to_err(status);
643}
644EXPORT_SYMBOL_GPL(efivar_entry_set_safe);
645
646/**
647 * efivar_entry_find - search for an entry
648 * @name: the EFI variable name
649 * @guid: the EFI variable vendor's guid
650 * @head: head of the variable list
651 * @remove: should we remove the entry from the list?
652 *
653 * Search for an entry on the variable list that has the EFI variable
654 * name @name and vendor guid @guid. If an entry is found on the list
655 * and @remove is true, the entry is removed from the list.
656 *
657 * The caller MUST call efivar_entry_iter_begin() and
658 * efivar_entry_iter_end() before and after the invocation of this
659 * function, respectively.
660 *
661 * Returns the entry if found on the list, %NULL otherwise.
662 */
663struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
664 struct list_head *head, bool remove)
665{
666 struct efivar_entry *entry, *n;
667 int strsize1, strsize2;
668 bool found = false;
669
670 WARN_ON(!spin_is_locked(&__efivars->lock));
671
672 list_for_each_entry_safe(entry, n, head, list) {
Matt Fleminga614e192013-04-30 11:30:24 +0100673 strsize1 = ucs2_strsize(name, 1024);
674 strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
Tom Gundersena9499fa2013-02-08 15:37:06 +0000675 if (strsize1 == strsize2 &&
676 !memcmp(name, &(entry->var.VariableName), strsize1) &&
677 !efi_guidcmp(guid, entry->var.VendorGuid)) {
678 found = true;
679 break;
680 }
681 }
682
683 if (!found)
684 return NULL;
685
Seiji Aguchie0d59732013-10-30 15:27:26 -0400686 if (remove) {
687 if (entry->scanning) {
688 /*
689 * The entry will be deleted
690 * after scanning is completed.
691 */
692 entry->deleting = true;
693 } else
694 list_del(&entry->list);
695 }
Tom Gundersena9499fa2013-02-08 15:37:06 +0000696
697 return entry;
698}
699EXPORT_SYMBOL_GPL(efivar_entry_find);
700
701/**
Tom Gundersena9499fa2013-02-08 15:37:06 +0000702 * efivar_entry_size - obtain the size of a variable
703 * @entry: entry for this variable
704 * @size: location to store the variable's size
705 */
706int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
707{
708 const struct efivar_operations *ops = __efivars->ops;
709 efi_status_t status;
710
711 *size = 0;
712
713 spin_lock_irq(&__efivars->lock);
714 status = ops->get_variable(entry->var.VariableName,
715 &entry->var.VendorGuid, NULL, size, NULL);
716 spin_unlock_irq(&__efivars->lock);
717
718 if (status != EFI_BUFFER_TOO_SMALL)
719 return efi_status_to_err(status);
720
721 return 0;
722}
723EXPORT_SYMBOL_GPL(efivar_entry_size);
724
725/**
Matt Fleming8a415b82013-04-29 20:08:02 +0100726 * __efivar_entry_get - call get_variable()
727 * @entry: read data for this variable
728 * @attributes: variable attributes
729 * @size: size of @data buffer
730 * @data: buffer to store variable data
731 *
732 * The caller MUST call efivar_entry_iter_begin() and
733 * efivar_entry_iter_end() before and after the invocation of this
734 * function, respectively.
735 */
736int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
737 unsigned long *size, void *data)
738{
739 const struct efivar_operations *ops = __efivars->ops;
740 efi_status_t status;
741
742 WARN_ON(!spin_is_locked(&__efivars->lock));
743
744 status = ops->get_variable(entry->var.VariableName,
745 &entry->var.VendorGuid,
746 attributes, size, data);
747
748 return efi_status_to_err(status);
749}
750EXPORT_SYMBOL_GPL(__efivar_entry_get);
751
752/**
Tom Gundersena9499fa2013-02-08 15:37:06 +0000753 * efivar_entry_get - call get_variable()
754 * @entry: read data for this variable
755 * @attributes: variable attributes
756 * @size: size of @data buffer
757 * @data: buffer to store variable data
758 */
759int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
760 unsigned long *size, void *data)
761{
762 const struct efivar_operations *ops = __efivars->ops;
763 efi_status_t status;
764
765 spin_lock_irq(&__efivars->lock);
766 status = ops->get_variable(entry->var.VariableName,
767 &entry->var.VendorGuid,
768 attributes, size, data);
769 spin_unlock_irq(&__efivars->lock);
770
771 return efi_status_to_err(status);
772}
773EXPORT_SYMBOL_GPL(efivar_entry_get);
774
775/**
776 * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
777 * @entry: entry containing variable to set and get
778 * @attributes: attributes of variable to be written
779 * @size: size of data buffer
780 * @data: buffer containing data to write
781 * @set: did the set_variable() call succeed?
782 *
783 * This is a pretty special (complex) function. See efivarfs_file_write().
784 *
785 * Atomically call set_variable() for @entry and if the call is
786 * successful, return the new size of the variable from get_variable()
787 * in @size. The success of set_variable() is indicated by @set.
788 *
789 * Returns 0 on success, -EINVAL if the variable data is invalid,
790 * -ENOSPC if the firmware does not have enough available space, or a
791 * converted EFI status code if either of set_variable() or
792 * get_variable() fail.
793 *
794 * If the EFI variable does not exist when calling set_variable()
795 * (EFI_NOT_FOUND), @entry is removed from the variable list.
796 */
797int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
798 unsigned long *size, void *data, bool *set)
799{
800 const struct efivar_operations *ops = __efivars->ops;
801 efi_char16_t *name = entry->var.VariableName;
802 efi_guid_t *vendor = &entry->var.VendorGuid;
803 efi_status_t status;
804 int err;
805
806 *set = false;
807
Matt Fleminga5d92ad2014-03-17 10:57:00 +0000808 if (efivar_validate(name, data, *size) == false)
Tom Gundersena9499fa2013-02-08 15:37:06 +0000809 return -EINVAL;
810
811 /*
812 * The lock here protects the get_variable call, the conditional
813 * set_variable call, and removal of the variable from the efivars
814 * list (in the case of an authenticated delete).
815 */
816 spin_lock_irq(&__efivars->lock);
817
818 /*
819 * Ensure that the available space hasn't shrunk below the safe level
820 */
Matt Fleminga614e192013-04-30 11:30:24 +0100821 status = check_var_size(attributes, *size + ucs2_strsize(name, 1024));
Tom Gundersena9499fa2013-02-08 15:37:06 +0000822 if (status != EFI_SUCCESS) {
823 if (status != EFI_UNSUPPORTED) {
824 err = efi_status_to_err(status);
825 goto out;
826 }
827
828 if (*size > 65536) {
829 err = -ENOSPC;
830 goto out;
831 }
832 }
833
834 status = ops->set_variable(name, vendor, attributes, *size, data);
835 if (status != EFI_SUCCESS) {
836 err = efi_status_to_err(status);
837 goto out;
838 }
839
840 *set = true;
841
842 /*
843 * Writing to the variable may have caused a change in size (which
844 * could either be an append or an overwrite), or the variable to be
845 * deleted. Perform a GetVariable() so we can tell what actually
846 * happened.
847 */
848 *size = 0;
849 status = ops->get_variable(entry->var.VariableName,
850 &entry->var.VendorGuid,
851 NULL, size, NULL);
852
853 if (status == EFI_NOT_FOUND)
854 efivar_entry_list_del_unlock(entry);
855 else
856 spin_unlock_irq(&__efivars->lock);
857
858 if (status && status != EFI_BUFFER_TOO_SMALL)
859 return efi_status_to_err(status);
860
861 return 0;
862
863out:
864 spin_unlock_irq(&__efivars->lock);
865 return err;
866
867}
868EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
869
870/**
871 * efivar_entry_iter_begin - begin iterating the variable list
872 *
873 * Lock the variable list to prevent entry insertion and removal until
874 * efivar_entry_iter_end() is called. This function is usually used in
875 * conjunction with __efivar_entry_iter() or efivar_entry_iter().
876 */
877void efivar_entry_iter_begin(void)
878{
879 spin_lock_irq(&__efivars->lock);
880}
881EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
882
883/**
884 * efivar_entry_iter_end - finish iterating the variable list
885 *
886 * Unlock the variable list and allow modifications to the list again.
887 */
888void efivar_entry_iter_end(void)
889{
890 spin_unlock_irq(&__efivars->lock);
891}
892EXPORT_SYMBOL_GPL(efivar_entry_iter_end);
893
894/**
895 * __efivar_entry_iter - iterate over variable list
896 * @func: callback function
897 * @head: head of the variable list
898 * @data: function-specific data to pass to callback
899 * @prev: entry to begin iterating from
900 *
901 * Iterate over the list of EFI variables and call @func with every
902 * entry on the list. It is safe for @func to remove entries in the
903 * list via efivar_entry_delete().
904 *
905 * You MUST call efivar_enter_iter_begin() before this function, and
906 * efivar_entry_iter_end() afterwards.
907 *
908 * It is possible to begin iteration from an arbitrary entry within
909 * the list by passing @prev. @prev is updated on return to point to
910 * the last entry passed to @func. To begin iterating from the
911 * beginning of the list @prev must be %NULL.
912 *
913 * The restrictions for @func are the same as documented for
914 * efivar_entry_iter().
915 */
916int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
917 struct list_head *head, void *data,
918 struct efivar_entry **prev)
919{
920 struct efivar_entry *entry, *n;
921 int err = 0;
922
923 if (!prev || !*prev) {
924 list_for_each_entry_safe(entry, n, head, list) {
925 err = func(entry, data);
926 if (err)
927 break;
928 }
929
930 if (prev)
931 *prev = entry;
932
933 return err;
934 }
935
936
937 list_for_each_entry_safe_continue((*prev), n, head, list) {
938 err = func(*prev, data);
939 if (err)
940 break;
941 }
942
943 return err;
944}
945EXPORT_SYMBOL_GPL(__efivar_entry_iter);
946
947/**
948 * efivar_entry_iter - iterate over variable list
949 * @func: callback function
950 * @head: head of variable list
951 * @data: function-specific data to pass to callback
952 *
953 * Iterate over the list of EFI variables and call @func with every
954 * entry on the list. It is safe for @func to remove entries in the
955 * list via efivar_entry_delete() while iterating.
956 *
957 * Some notes for the callback function:
958 * - a non-zero return value indicates an error and terminates the loop
959 * - @func is called from atomic context
960 */
961int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
962 struct list_head *head, void *data)
963{
964 int err = 0;
965
966 efivar_entry_iter_begin();
967 err = __efivar_entry_iter(func, head, data, NULL);
968 efivar_entry_iter_end();
969
970 return err;
971}
972EXPORT_SYMBOL_GPL(efivar_entry_iter);
973
974/**
975 * efivars_kobject - get the kobject for the registered efivars
976 *
977 * If efivars_register() has not been called we return NULL,
978 * otherwise return the kobject used at registration time.
979 */
980struct kobject *efivars_kobject(void)
981{
982 if (!__efivars)
983 return NULL;
984
985 return __efivars->kobject;
986}
987EXPORT_SYMBOL_GPL(efivars_kobject);
988
989/**
990 * efivar_run_worker - schedule the efivar worker thread
991 */
992void efivar_run_worker(void)
993{
994 if (efivar_wq_enabled)
995 schedule_work(&efivar_work);
996}
997EXPORT_SYMBOL_GPL(efivar_run_worker);
998
999/**
1000 * efivars_register - register an efivars
1001 * @efivars: efivars to register
1002 * @ops: efivars operations
1003 * @kobject: @efivars-specific kobject
1004 *
1005 * Only a single efivars can be registered at any time.
1006 */
1007int efivars_register(struct efivars *efivars,
1008 const struct efivar_operations *ops,
1009 struct kobject *kobject)
1010{
1011 spin_lock_init(&efivars->lock);
1012 efivars->ops = ops;
1013 efivars->kobject = kobject;
1014
1015 __efivars = efivars;
1016
1017 return 0;
1018}
1019EXPORT_SYMBOL_GPL(efivars_register);
1020
1021/**
1022 * efivars_unregister - unregister an efivars
1023 * @efivars: efivars to unregister
1024 *
1025 * The caller must have already removed every entry from the list,
1026 * failure to do so is an error.
1027 */
1028int efivars_unregister(struct efivars *efivars)
1029{
1030 int rv;
1031
1032 if (!__efivars) {
1033 printk(KERN_ERR "efivars not registered\n");
1034 rv = -EINVAL;
1035 goto out;
1036 }
1037
1038 if (__efivars != efivars) {
1039 rv = -EINVAL;
1040 goto out;
1041 }
1042
1043 __efivars = NULL;
1044
1045 rv = 0;
1046out:
1047 return rv;
1048}
1049EXPORT_SYMBOL_GPL(efivars_unregister);