Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/j.anaszewski...
[linux-2.6-block.git] / drivers / firmware / efi / vars.c
CommitLineData
1da177e4 1/*
a9499fa7 2 * Originally from efivars.c
1da177e4
LT
3 *
4 * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
5 * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
6 *
1da177e4
LT
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
1da177e4
LT
20 */
21
c59ede7b 22#include <linux/capability.h>
1da177e4
LT
23#include <linux/types.h>
24#include <linux/errno.h>
25#include <linux/init.h>
1da177e4
LT
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>
1da177e4 32#include <linux/device.h>
5a0e3ad6 33#include <linux/slab.h>
47f531e8 34#include <linux/ctype.h>
a614e192 35#include <linux/ucs2_string.h>
1da177e4 36
4423d779
MF
37/* Private pointer to registered efivars */
38static struct efivars *__efivars;
5d9db883 39
e971318b 40static bool efivar_wq_enabled = true;
a9499fa7
TG
41DECLARE_WORK(efivar_work, NULL);
42EXPORT_SYMBOL_GPL(efivar_work);
a93bc0c6 43
fec6c20b 44static bool
a5d92ad3 45validate_device_path(efi_char16_t *var_name, int match, u8 *buffer,
54b3a4d3 46 unsigned long len)
fec6c20b
MG
47{
48 struct efi_generic_dev_path *node;
49 int offset = 0;
50
51 node = (struct efi_generic_dev_path *)buffer;
52
54b3a4d3
MG
53 if (len < sizeof(*node))
54 return false;
fec6c20b 55
54b3a4d3
MG
56 while (offset <= len - sizeof(*node) &&
57 node->length >= sizeof(*node) &&
58 node->length <= len - offset) {
59 offset += node->length;
fec6c20b
MG
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
a5d92ad3 78validate_boot_order(efi_char16_t *var_name, int match, u8 *buffer,
54b3a4d3 79 unsigned long len)
fec6c20b
MG
80{
81 /* An array of 16-bit integers */
82 if ((len % 2) != 0)
83 return false;
84
85 return true;
86}
87
88static bool
a5d92ad3 89validate_load_option(efi_char16_t *var_name, int match, u8 *buffer,
54b3a4d3 90 unsigned long len)
fec6c20b
MG
91{
92 u16 filepathlength;
54b3a4d3
MG
93 int i, desclength = 0, namelen;
94
a5d92ad3 95 namelen = ucs2_strnlen(var_name, EFI_VAR_NAME_LEN);
fec6c20b
MG
96
97 /* Either "Boot" or "Driver" followed by four digits of hex */
98 for (i = match; i < match+4; i++) {
a5d92ad3
MF
99 if (var_name[i] > 127 ||
100 hex_to_bin(var_name[i] & 0xff) < 0)
fec6c20b
MG
101 return true;
102 }
103
54b3a4d3
MG
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)
fec6c20b
MG
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 */
a614e192 118 desclength = ucs2_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
fec6c20b
MG
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 */
a5d92ad3 135 return validate_device_path(var_name, match, buffer + desclength + 6,
fec6c20b
MG
136 filepathlength);
137}
138
139static bool
a5d92ad3 140validate_uint16(efi_char16_t *var_name, int match, u8 *buffer,
54b3a4d3 141 unsigned long len)
fec6c20b
MG
142{
143 /* A single 16-bit integer */
144 if (len != 2)
145 return false;
146
147 return true;
148}
149
150static bool
a5d92ad3 151validate_ascii_string(efi_char16_t *var_name, int match, u8 *buffer,
54b3a4d3 152 unsigned long len)
fec6c20b
MG
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 {
8282f5d9 168 efi_guid_t vendor;
fec6c20b 169 char *name;
a5d92ad3 170 bool (*validate)(efi_char16_t *var_name, int match, u8 *data,
54b3a4d3 171 unsigned long len);
fec6c20b
MG
172};
173
8282f5d9 174/*
ed8b0de5
PJ
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.
8282f5d9
PJ
177 *
178 * If it has a validate() method that's not NULL, it'll go into the
ed8b0de5
PJ
179 * validation routine. If not, it is assumed valid, but still used for
180 * whitelisting.
8282f5d9
PJ
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 */
fec6c20b 185static const struct variable_validate variable_validate[] = {
8282f5d9
PJ
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 },
ed8b0de5 198 { EFI_GLOBAL_VARIABLE_GUID, "OsIndications", NULL },
8282f5d9
PJ
199 { EFI_GLOBAL_VARIABLE_GUID, "PlatformLang", validate_ascii_string },
200 { EFI_GLOBAL_VARIABLE_GUID, "Timeout", validate_uint16 },
e246eb56 201 { LINUX_EFI_CRASH_GUID, "*", NULL },
8282f5d9 202 { NULL_GUID, "", NULL },
fec6c20b
MG
203};
204
630ba0cc
LE
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 */
ed8b0de5
PJ
216static bool
217variable_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];
ed8b0de5 222
630ba0cc
LE
223 switch (c) {
224 case '*':
225 /* Wildcard in @match_name means we've matched. */
ed8b0de5
PJ
226 return true;
227
630ba0cc
LE
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;
ed8b0de5 240 return false;
630ba0cc 241 }
ed8b0de5 242 }
ed8b0de5
PJ
243}
244
e14ab23d 245bool
8282f5d9
PJ
246efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data,
247 unsigned long data_size)
fec6c20b
MG
248{
249 int i;
3dcb1f55
PJ
250 unsigned long utf8_size;
251 u8 *utf8_name;
fec6c20b 252
3dcb1f55
PJ
253 utf8_size = ucs2_utf8size(var_name);
254 utf8_name = kmalloc(utf8_size + 1, GFP_KERNEL);
255 if (!utf8_name)
256 return false;
fec6c20b 257
3dcb1f55
PJ
258 ucs2_as_utf8(utf8_name, var_name, utf8_size);
259 utf8_name[utf8_size] = '\0';
fec6c20b 260
8282f5d9 261 for (i = 0; variable_validate[i].name[0] != '\0'; i++) {
fec6c20b 262 const char *name = variable_validate[i].name;
8282f5d9 263 int match = 0;
fec6c20b 264
8282f5d9
PJ
265 if (efi_guidcmp(vendor, variable_validate[i].vendor))
266 continue;
fec6c20b 267
ed8b0de5
PJ
268 if (variable_matches(utf8_name, utf8_size+1, name, &match)) {
269 if (variable_validate[i].validate == NULL)
fec6c20b 270 break;
ed8b0de5
PJ
271 kfree(utf8_name);
272 return variable_validate[i].validate(var_name, match,
273 data, data_size);
fec6c20b
MG
274 }
275 }
3dcb1f55 276 kfree(utf8_name);
fec6c20b
MG
277 return true;
278}
e14ab23d 279EXPORT_SYMBOL_GPL(efivar_validate);
fec6c20b 280
ed8b0de5
PJ
281bool
282efivar_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}
308EXPORT_SYMBOL_GPL(efivar_variable_is_removable);
309
1da177e4 310static efi_status_t
e14ab23d 311check_var_size(u32 attributes, unsigned long size)
68d92986 312{
e14ab23d 313 const struct efivar_operations *fops = __efivars->ops;
68d92986 314
a614e192 315 if (!fops->query_variable_store)
68d92986
MG
316 return EFI_UNSUPPORTED;
317
ca0e30dc
AB
318 return fops->query_variable_store(attributes, size, false);
319}
320
321static efi_status_t
322check_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);
68d92986
MG
330}
331
e14ab23d
MF
332static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor,
333 struct list_head *head)
a93bc0c6
SA
334{
335 struct efivar_entry *entry, *n;
a93bc0c6
SA
336 unsigned long strsize1, strsize2;
337 bool found = false;
338
a614e192 339 strsize1 = ucs2_strsize(variable_name, 1024);
e14ab23d 340 list_for_each_entry_safe(entry, n, head, list) {
a614e192 341 strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
a93bc0c6
SA
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
ec50bd32
MF
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 */
359static 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
e971318b
MF
379/*
380 * Print a warning when duplicate EFI variables are encountered and
381 * disable the sysfs workqueue since the firmware is buggy.
382 */
b2fce819 383static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid,
e971318b
MF
384 unsigned long len16)
385{
386 size_t i, len8 = len16 / sizeof(efi_char16_t);
b2fce819 387 char *str8;
e971318b
MF
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
b2fce819
MR
396 str8 = kzalloc(len8, GFP_KERNEL);
397 if (!str8)
e971318b
MF
398 return;
399
400 for (i = 0; i < len8; i++)
b2fce819 401 str8[i] = str16[i];
e971318b
MF
402
403 printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
b2fce819
MR
404 str8, vendor_guid);
405 kfree(str8);
e971318b
MF
406}
407
e14ab23d
MF
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 */
421int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
1cfd6316 422 void *data, bool duplicates, struct list_head *head)
e14ab23d
MF
423{
424 const struct efivar_operations *ops = __efivars->ops;
425 unsigned long variable_name_size = 1024;
426 efi_char16_t *variable_name;
427 efi_status_t status;
428 efi_guid_t vendor_guid;
429 int err = 0;
430
431 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
432 if (!variable_name) {
433 printk(KERN_ERR "efivars: Memory allocation failed.\n");
434 return -ENOMEM;
605e70c7
LCY
435 }
436
e14ab23d
MF
437 spin_lock_irq(&__efivars->lock);
438
1da177e4
LT
439 /*
440 * Per EFI spec, the maximum storage allocated for both
441 * the variable name and variable data is 1024 bytes.
442 */
443
444 do {
445 variable_name_size = 1024;
446
3295814d 447 status = ops->get_next_variable(&variable_name_size,
1da177e4
LT
448 variable_name,
449 &vendor_guid);
450 switch (status) {
451 case EFI_SUCCESS:
1cfd6316 452 if (duplicates)
e14ab23d
MF
453 spin_unlock_irq(&__efivars->lock);
454
ec50bd32
MF
455 variable_name_size = var_name_strnsize(variable_name,
456 variable_name_size);
e971318b
MF
457
458 /*
459 * Some firmware implementations return the
460 * same variable name on multiple calls to
461 * get_next_variable(). Terminate the loop
462 * immediately as there is no guarantee that
463 * we'll ever see a different variable name,
464 * and may end up looping here forever.
465 */
e14ab23d 466 if (duplicates &&
1cfd6316
JL
467 variable_is_present(variable_name, &vendor_guid,
468 head)) {
e971318b
MF
469 dup_variable_bug(variable_name, &vendor_guid,
470 variable_name_size);
471 status = EFI_NOT_FOUND;
1cfd6316
JL
472 } else {
473 err = func(variable_name, vendor_guid,
474 variable_name_size, data);
475 if (err)
476 status = EFI_NOT_FOUND;
e971318b
MF
477 }
478
1cfd6316 479 if (duplicates)
e14ab23d
MF
480 spin_lock_irq(&__efivars->lock);
481
1da177e4
LT
482 break;
483 case EFI_NOT_FOUND:
484 break;
485 default:
486 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
487 status);
488 status = EFI_NOT_FOUND;
489 break;
490 }
e14ab23d 491
1da177e4
LT
492 } while (status != EFI_NOT_FOUND);
493
e14ab23d
MF
494 spin_unlock_irq(&__efivars->lock);
495
496 kfree(variable_name);
497
498 return err;
499}
500EXPORT_SYMBOL_GPL(efivar_init);
501
502/**
503 * efivar_entry_add - add entry to variable list
504 * @entry: entry to add to list
505 * @head: list head
506 */
507void efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
508{
509 spin_lock_irq(&__efivars->lock);
510 list_add(&entry->list, head);
511 spin_unlock_irq(&__efivars->lock);
512}
513EXPORT_SYMBOL_GPL(efivar_entry_add);
514
515/**
516 * efivar_entry_remove - remove entry from variable list
517 * @entry: entry to remove from list
518 */
519void efivar_entry_remove(struct efivar_entry *entry)
520{
521 spin_lock_irq(&__efivars->lock);
522 list_del(&entry->list);
523 spin_unlock_irq(&__efivars->lock);
524}
525EXPORT_SYMBOL_GPL(efivar_entry_remove);
526
527/*
528 * efivar_entry_list_del_unlock - remove entry from variable list
529 * @entry: entry to remove
530 *
531 * Remove @entry from the variable list and release the list lock.
532 *
533 * NOTE: slightly weird locking semantics here - we expect to be
534 * called with the efivars lock already held, and we release it before
535 * returning. This is because this function is usually called after
536 * set_variable() while the lock is still held.
537 */
538static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
539{
aee530cf 540 lockdep_assert_held(&__efivars->lock);
e14ab23d
MF
541
542 list_del(&entry->list);
543 spin_unlock_irq(&__efivars->lock);
544}
545
546/**
547 * __efivar_entry_delete - delete an EFI variable
548 * @entry: entry containing EFI variable to delete
549 *
a9499fa7
TG
550 * Delete the variable from the firmware but leave @entry on the
551 * variable list.
e14ab23d 552 *
a9499fa7
TG
553 * This function differs from efivar_entry_delete() because it does
554 * not remove @entry from the variable list. Also, it is safe to be
555 * called from within a efivar_entry_iter_begin() and
e14ab23d
MF
556 * efivar_entry_iter_end() region, unlike efivar_entry_delete().
557 *
558 * Returns 0 on success, or a converted EFI status code if
a9499fa7 559 * set_variable() fails.
e14ab23d
MF
560 */
561int __efivar_entry_delete(struct efivar_entry *entry)
562{
563 const struct efivar_operations *ops = __efivars->ops;
564 efi_status_t status;
565
aee530cf 566 lockdep_assert_held(&__efivars->lock);
e14ab23d
MF
567
568 status = ops->set_variable(entry->var.VariableName,
569 &entry->var.VendorGuid,
570 0, 0, NULL);
e14ab23d 571
a9499fa7 572 return efi_status_to_err(status);
e14ab23d
MF
573}
574EXPORT_SYMBOL_GPL(__efivar_entry_delete);
575
576/**
577 * efivar_entry_delete - delete variable and remove entry from list
578 * @entry: entry containing variable to delete
579 *
580 * Delete the variable from the firmware and remove @entry from the
581 * variable list. It is the caller's responsibility to free @entry
582 * once we return.
583 *
584 * Returns 0 on success, or a converted EFI status code if
585 * set_variable() fails.
586 */
587int efivar_entry_delete(struct efivar_entry *entry)
588{
589 const struct efivar_operations *ops = __efivars->ops;
590 efi_status_t status;
591
592 spin_lock_irq(&__efivars->lock);
593 status = ops->set_variable(entry->var.VariableName,
594 &entry->var.VendorGuid,
595 0, 0, NULL);
596 if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
597 spin_unlock_irq(&__efivars->lock);
598 return efi_status_to_err(status);
599 }
600
601 efivar_entry_list_del_unlock(entry);
602 return 0;
603}
604EXPORT_SYMBOL_GPL(efivar_entry_delete);
605
606/**
607 * efivar_entry_set - call set_variable()
608 * @entry: entry containing the EFI variable to write
609 * @attributes: variable attributes
610 * @size: size of @data buffer
611 * @data: buffer containing variable data
612 * @head: head of variable list
613 *
614 * Calls set_variable() for an EFI variable. If creating a new EFI
615 * variable, this function is usually followed by efivar_entry_add().
616 *
617 * Before writing the variable, the remaining EFI variable storage
618 * space is checked to ensure there is enough room available.
619 *
620 * If @head is not NULL a lookup is performed to determine whether
621 * the entry is already on the list.
622 *
623 * Returns 0 on success, -EEXIST if a lookup is performed and the entry
624 * already exists on the list, or a converted EFI status code if
625 * set_variable() fails.
626 */
627int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
628 unsigned long size, void *data, struct list_head *head)
629{
630 const struct efivar_operations *ops = __efivars->ops;
631 efi_status_t status;
632 efi_char16_t *name = entry->var.VariableName;
633 efi_guid_t vendor = entry->var.VendorGuid;
634
635 spin_lock_irq(&__efivars->lock);
636
637 if (head && efivar_entry_find(name, vendor, head, false)) {
638 spin_unlock_irq(&__efivars->lock);
639 return -EEXIST;
640 }
641
a614e192 642 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
e14ab23d
MF
643 if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
644 status = ops->set_variable(name, &vendor,
645 attributes, size, data);
646
647 spin_unlock_irq(&__efivars->lock);
648
649 return efi_status_to_err(status);
a9499fa7 650
e14ab23d
MF
651}
652EXPORT_SYMBOL_GPL(efivar_entry_set);
653
6d80dba1
MF
654/*
655 * efivar_entry_set_nonblocking - call set_variable_nonblocking()
656 *
657 * This function is guaranteed to not block and is suitable for calling
658 * from crash/panic handlers.
659 *
660 * Crucially, this function will not block if it cannot acquire
661 * __efivars->lock. Instead, it returns -EBUSY.
662 */
663static int
664efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
665 u32 attributes, unsigned long size, void *data)
666{
667 const struct efivar_operations *ops = __efivars->ops;
668 unsigned long flags;
669 efi_status_t status;
670
671 if (!spin_trylock_irqsave(&__efivars->lock, flags))
672 return -EBUSY;
673
ca0e30dc
AB
674 status = check_var_size_nonblocking(attributes,
675 size + ucs2_strsize(name, 1024));
6d80dba1
MF
676 if (status != EFI_SUCCESS) {
677 spin_unlock_irqrestore(&__efivars->lock, flags);
678 return -ENOSPC;
679 }
680
681 status = ops->set_variable_nonblocking(name, &vendor, attributes,
682 size, data);
683
684 spin_unlock_irqrestore(&__efivars->lock, flags);
685 return efi_status_to_err(status);
686}
687
e14ab23d
MF
688/**
689 * efivar_entry_set_safe - call set_variable() if enough space in firmware
690 * @name: buffer containing the variable name
691 * @vendor: variable vendor guid
692 * @attributes: variable attributes
693 * @block: can we block in this context?
694 * @size: size of @data buffer
695 * @data: buffer containing variable data
696 *
697 * Ensures there is enough free storage in the firmware for this variable, and
698 * if so, calls set_variable(). If creating a new EFI variable, this function
699 * is usually followed by efivar_entry_add().
700 *
701 * Returns 0 on success, -ENOSPC if the firmware does not have enough
702 * space for set_variable() to succeed, or a converted EFI status code
703 * if set_variable() fails.
704 */
705int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
706 bool block, unsigned long size, void *data)
707{
708 const struct efivar_operations *ops = __efivars->ops;
709 unsigned long flags;
710 efi_status_t status;
711
a614e192 712 if (!ops->query_variable_store)
e14ab23d
MF
713 return -ENOSYS;
714
6d80dba1
MF
715 /*
716 * If the EFI variable backend provides a non-blocking
717 * ->set_variable() operation and we're in a context where we
718 * cannot block, then we need to use it to avoid live-locks,
719 * since the implication is that the regular ->set_variable()
720 * will block.
721 *
722 * If no ->set_variable_nonblocking() is provided then
723 * ->set_variable() is assumed to be non-blocking.
724 */
725 if (!block && ops->set_variable_nonblocking)
726 return efivar_entry_set_nonblocking(name, vendor, attributes,
727 size, data);
728
85c90716
DC
729 if (!block) {
730 if (!spin_trylock_irqsave(&__efivars->lock, flags))
731 return -EBUSY;
732 } else {
e14ab23d 733 spin_lock_irqsave(&__efivars->lock, flags);
85c90716 734 }
e14ab23d 735
a614e192 736 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
e14ab23d
MF
737 if (status != EFI_SUCCESS) {
738 spin_unlock_irqrestore(&__efivars->lock, flags);
739 return -ENOSPC;
740 }
741
742 status = ops->set_variable(name, &vendor, attributes, size, data);
743
744 spin_unlock_irqrestore(&__efivars->lock, flags);
745
746 return efi_status_to_err(status);
747}
748EXPORT_SYMBOL_GPL(efivar_entry_set_safe);
749
750/**
751 * efivar_entry_find - search for an entry
752 * @name: the EFI variable name
753 * @guid: the EFI variable vendor's guid
754 * @head: head of the variable list
755 * @remove: should we remove the entry from the list?
756 *
757 * Search for an entry on the variable list that has the EFI variable
758 * name @name and vendor guid @guid. If an entry is found on the list
759 * and @remove is true, the entry is removed from the list.
760 *
761 * The caller MUST call efivar_entry_iter_begin() and
762 * efivar_entry_iter_end() before and after the invocation of this
763 * function, respectively.
764 *
765 * Returns the entry if found on the list, %NULL otherwise.
766 */
767struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
768 struct list_head *head, bool remove)
769{
770 struct efivar_entry *entry, *n;
771 int strsize1, strsize2;
772 bool found = false;
773
aee530cf 774 lockdep_assert_held(&__efivars->lock);
e14ab23d
MF
775
776 list_for_each_entry_safe(entry, n, head, list) {
a614e192
MF
777 strsize1 = ucs2_strsize(name, 1024);
778 strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
e14ab23d
MF
779 if (strsize1 == strsize2 &&
780 !memcmp(name, &(entry->var.VariableName), strsize1) &&
781 !efi_guidcmp(guid, entry->var.VendorGuid)) {
782 found = true;
783 break;
784 }
785 }
786
787 if (!found)
788 return NULL;
789
e0d59733
SA
790 if (remove) {
791 if (entry->scanning) {
792 /*
793 * The entry will be deleted
794 * after scanning is completed.
795 */
796 entry->deleting = true;
797 } else
798 list_del(&entry->list);
799 }
e14ab23d
MF
800
801 return entry;
802}
803EXPORT_SYMBOL_GPL(efivar_entry_find);
804
805/**
8a415b8c 806 * efivar_entry_size - obtain the size of a variable
e14ab23d
MF
807 * @entry: entry for this variable
808 * @size: location to store the variable's size
e14ab23d 809 */
8a415b8c 810int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
e14ab23d
MF
811{
812 const struct efivar_operations *ops = __efivars->ops;
813 efi_status_t status;
814
e14ab23d 815 *size = 0;
8a415b8c
MF
816
817 spin_lock_irq(&__efivars->lock);
e14ab23d
MF
818 status = ops->get_variable(entry->var.VariableName,
819 &entry->var.VendorGuid, NULL, size, NULL);
8a415b8c
MF
820 spin_unlock_irq(&__efivars->lock);
821
e14ab23d
MF
822 if (status != EFI_BUFFER_TOO_SMALL)
823 return efi_status_to_err(status);
824
825 return 0;
826}
8a415b8c 827EXPORT_SYMBOL_GPL(efivar_entry_size);
e14ab23d
MF
828
829/**
8a415b8c
MF
830 * __efivar_entry_get - call get_variable()
831 * @entry: read data for this variable
832 * @attributes: variable attributes
833 * @size: size of @data buffer
834 * @data: buffer to store variable data
835 *
836 * The caller MUST call efivar_entry_iter_begin() and
837 * efivar_entry_iter_end() before and after the invocation of this
838 * function, respectively.
e14ab23d 839 */
8a415b8c
MF
840int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
841 unsigned long *size, void *data)
e14ab23d
MF
842{
843 const struct efivar_operations *ops = __efivars->ops;
844 efi_status_t status;
845
aee530cf 846 lockdep_assert_held(&__efivars->lock);
e14ab23d 847
e14ab23d 848 status = ops->get_variable(entry->var.VariableName,
8a415b8c
MF
849 &entry->var.VendorGuid,
850 attributes, size, data);
e14ab23d 851
8a415b8c 852 return efi_status_to_err(status);
e14ab23d 853}
8a415b8c 854EXPORT_SYMBOL_GPL(__efivar_entry_get);
e14ab23d
MF
855
856/**
857 * efivar_entry_get - call get_variable()
858 * @entry: read data for this variable
859 * @attributes: variable attributes
860 * @size: size of @data buffer
861 * @data: buffer to store variable data
862 */
863int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
864 unsigned long *size, void *data)
865{
866 const struct efivar_operations *ops = __efivars->ops;
867 efi_status_t status;
868
869 spin_lock_irq(&__efivars->lock);
870 status = ops->get_variable(entry->var.VariableName,
871 &entry->var.VendorGuid,
872 attributes, size, data);
873 spin_unlock_irq(&__efivars->lock);
874
875 return efi_status_to_err(status);
876}
877EXPORT_SYMBOL_GPL(efivar_entry_get);
878
879/**
880 * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
881 * @entry: entry containing variable to set and get
882 * @attributes: attributes of variable to be written
883 * @size: size of data buffer
884 * @data: buffer containing data to write
885 * @set: did the set_variable() call succeed?
886 *
887 * This is a pretty special (complex) function. See efivarfs_file_write().
888 *
889 * Atomically call set_variable() for @entry and if the call is
890 * successful, return the new size of the variable from get_variable()
891 * in @size. The success of set_variable() is indicated by @set.
892 *
893 * Returns 0 on success, -EINVAL if the variable data is invalid,
894 * -ENOSPC if the firmware does not have enough available space, or a
895 * converted EFI status code if either of set_variable() or
896 * get_variable() fail.
897 *
898 * If the EFI variable does not exist when calling set_variable()
899 * (EFI_NOT_FOUND), @entry is removed from the variable list.
900 */
901int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
902 unsigned long *size, void *data, bool *set)
903{
904 const struct efivar_operations *ops = __efivars->ops;
905 efi_char16_t *name = entry->var.VariableName;
906 efi_guid_t *vendor = &entry->var.VendorGuid;
907 efi_status_t status;
908 int err;
909
910 *set = false;
911
8282f5d9 912 if (efivar_validate(*vendor, name, data, *size) == false)
e14ab23d
MF
913 return -EINVAL;
914
915 /*
916 * The lock here protects the get_variable call, the conditional
917 * set_variable call, and removal of the variable from the efivars
918 * list (in the case of an authenticated delete).
919 */
920 spin_lock_irq(&__efivars->lock);
921
922 /*
923 * Ensure that the available space hasn't shrunk below the safe level
924 */
a614e192 925 status = check_var_size(attributes, *size + ucs2_strsize(name, 1024));
e14ab23d
MF
926 if (status != EFI_SUCCESS) {
927 if (status != EFI_UNSUPPORTED) {
928 err = efi_status_to_err(status);
929 goto out;
930 }
931
932 if (*size > 65536) {
933 err = -ENOSPC;
934 goto out;
935 }
936 }
937
938 status = ops->set_variable(name, vendor, attributes, *size, data);
939 if (status != EFI_SUCCESS) {
940 err = efi_status_to_err(status);
941 goto out;
942 }
943
944 *set = true;
945
946 /*
947 * Writing to the variable may have caused a change in size (which
948 * could either be an append or an overwrite), or the variable to be
949 * deleted. Perform a GetVariable() so we can tell what actually
950 * happened.
951 */
952 *size = 0;
953 status = ops->get_variable(entry->var.VariableName,
954 &entry->var.VendorGuid,
955 NULL, size, NULL);
956
957 if (status == EFI_NOT_FOUND)
958 efivar_entry_list_del_unlock(entry);
959 else
960 spin_unlock_irq(&__efivars->lock);
961
962 if (status && status != EFI_BUFFER_TOO_SMALL)
963 return efi_status_to_err(status);
964
965 return 0;
966
967out:
968 spin_unlock_irq(&__efivars->lock);
969 return err;
970
971}
972EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
973
974/**
975 * efivar_entry_iter_begin - begin iterating the variable list
976 *
977 * Lock the variable list to prevent entry insertion and removal until
978 * efivar_entry_iter_end() is called. This function is usually used in
979 * conjunction with __efivar_entry_iter() or efivar_entry_iter().
980 */
981void efivar_entry_iter_begin(void)
982{
983 spin_lock_irq(&__efivars->lock);
984}
985EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
986
987/**
988 * efivar_entry_iter_end - finish iterating the variable list
989 *
990 * Unlock the variable list and allow modifications to the list again.
991 */
992void efivar_entry_iter_end(void)
993{
994 spin_unlock_irq(&__efivars->lock);
995}
996EXPORT_SYMBOL_GPL(efivar_entry_iter_end);
997
998/**
999 * __efivar_entry_iter - iterate over variable list
1000 * @func: callback function
1001 * @head: head of the variable list
1002 * @data: function-specific data to pass to callback
1003 * @prev: entry to begin iterating from
1004 *
1005 * Iterate over the list of EFI variables and call @func with every
1006 * entry on the list. It is safe for @func to remove entries in the
1007 * list via efivar_entry_delete().
1008 *
1009 * You MUST call efivar_enter_iter_begin() before this function, and
1010 * efivar_entry_iter_end() afterwards.
1011 *
1012 * It is possible to begin iteration from an arbitrary entry within
1013 * the list by passing @prev. @prev is updated on return to point to
1014 * the last entry passed to @func. To begin iterating from the
1015 * beginning of the list @prev must be %NULL.
1016 *
1017 * The restrictions for @func are the same as documented for
1018 * efivar_entry_iter().
1019 */
1020int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
1021 struct list_head *head, void *data,
1022 struct efivar_entry **prev)
1023{
1024 struct efivar_entry *entry, *n;
1025 int err = 0;
1026
1027 if (!prev || !*prev) {
1028 list_for_each_entry_safe(entry, n, head, list) {
1029 err = func(entry, data);
1030 if (err)
1031 break;
1032 }
1033
1034 if (prev)
1035 *prev = entry;
1036
1037 return err;
1038 }
1039
1040
1041 list_for_each_entry_safe_continue((*prev), n, head, list) {
1042 err = func(*prev, data);
1043 if (err)
1044 break;
1045 }
1046
1047 return err;
1048}
1049EXPORT_SYMBOL_GPL(__efivar_entry_iter);
1050
1051/**
1052 * efivar_entry_iter - iterate over variable list
1053 * @func: callback function
1054 * @head: head of variable list
1055 * @data: function-specific data to pass to callback
1056 *
1057 * Iterate over the list of EFI variables and call @func with every
1058 * entry on the list. It is safe for @func to remove entries in the
1059 * list via efivar_entry_delete() while iterating.
1060 *
1061 * Some notes for the callback function:
1062 * - a non-zero return value indicates an error and terminates the loop
1063 * - @func is called from atomic context
1064 */
1065int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
1066 struct list_head *head, void *data)
1067{
1068 int err = 0;
1069
1070 efivar_entry_iter_begin();
1071 err = __efivar_entry_iter(func, head, data, NULL);
1072 efivar_entry_iter_end();
1073
1074 return err;
1075}
1076EXPORT_SYMBOL_GPL(efivar_entry_iter);
1077
1078/**
1079 * efivars_kobject - get the kobject for the registered efivars
1080 *
1081 * If efivars_register() has not been called we return NULL,
1082 * otherwise return the kobject used at registration time.
1083 */
1084struct kobject *efivars_kobject(void)
1085{
1086 if (!__efivars)
1087 return NULL;
1088
1089 return __efivars->kobject;
1090}
1091EXPORT_SYMBOL_GPL(efivars_kobject);
1092
04851772
MF
1093/**
1094 * efivar_run_worker - schedule the efivar worker thread
1095 */
1096void efivar_run_worker(void)
1097{
1098 if (efivar_wq_enabled)
1099 schedule_work(&efivar_work);
1100}
1101EXPORT_SYMBOL_GPL(efivar_run_worker);
1102
e14ab23d
MF
1103/**
1104 * efivars_register - register an efivars
1105 * @efivars: efivars to register
1106 * @ops: efivars operations
1107 * @kobject: @efivars-specific kobject
1108 *
1109 * Only a single efivars can be registered at any time.
1110 */
1111int efivars_register(struct efivars *efivars,
1112 const struct efivar_operations *ops,
1113 struct kobject *kobject)
1114{
1115 spin_lock_init(&efivars->lock);
1116 efivars->ops = ops;
1117 efivars->kobject = kobject;
1118
1119 __efivars = efivars;
1da177e4 1120
e14ab23d
MF
1121 return 0;
1122}
1123EXPORT_SYMBOL_GPL(efivars_register);
1da177e4 1124
e14ab23d
MF
1125/**
1126 * efivars_unregister - unregister an efivars
1127 * @efivars: efivars to unregister
1128 *
1129 * The caller must have already removed every entry from the list,
1130 * failure to do so is an error.
1131 */
1132int efivars_unregister(struct efivars *efivars)
1133{
1134 int rv;
1135
1136 if (!__efivars) {
1137 printk(KERN_ERR "efivars not registered\n");
1138 rv = -EINVAL;
1139 goto out;
1140 }
1141
1142 if (__efivars != efivars) {
1143 rv = -EINVAL;
1144 goto out;
1145 }
1146
1147 __efivars = NULL;
1148
1149 rv = 0;
1150out:
1151 return rv;
76b53f7c 1152}
e14ab23d 1153EXPORT_SYMBOL_GPL(efivars_unregister);