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