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