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