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