]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/firmware/efivars.c
Merge branch 'vfree' into for-next
[karo-tx-linux.git] / drivers / firmware / efivars.c
1 /*
2  * EFI Variables - 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 code takes all variables accessible from EFI runtime and
8  *  exports them via sysfs
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  * Changelog:
25  *
26  *  17 May 2004 - Matt Domsch <Matt_Domsch@dell.com>
27  *   remove check for efi_enabled in exit
28  *   add MODULE_VERSION
29  *
30  *  26 Apr 2004 - Matt Domsch <Matt_Domsch@dell.com>
31  *   minor bug fixes
32  *
33  *  21 Apr 2004 - Matt Tolentino <matthew.e.tolentino@intel.com)
34  *   converted driver to export variable information via sysfs
35  *   and moved to drivers/firmware directory
36  *   bumped revision number to v0.07 to reflect conversion & move
37  *
38  *  10 Dec 2002 - Matt Domsch <Matt_Domsch@dell.com>
39  *   fix locking per Peter Chubb's findings
40  *
41  *  25 Mar 2002 - Matt Domsch <Matt_Domsch@dell.com>
42  *   move uuid_unparse() to include/asm-ia64/efi.h:efi_guid_unparse()
43  *
44  *  12 Feb 2002 - Matt Domsch <Matt_Domsch@dell.com>
45  *   use list_for_each_safe when deleting vars.
46  *   remove ifdef CONFIG_SMP around include <linux/smp.h>
47  *   v0.04 release to linux-ia64@linuxia64.org
48  *
49  *  20 April 2001 - Matt Domsch <Matt_Domsch@dell.com>
50  *   Moved vars from /proc/efi to /proc/efi/vars, and made
51  *   efi.c own the /proc/efi directory.
52  *   v0.03 release to linux-ia64@linuxia64.org
53  *
54  *  26 March 2001 - Matt Domsch <Matt_Domsch@dell.com>
55  *   At the request of Stephane, moved ownership of /proc/efi
56  *   to efi.c, and now efivars lives under /proc/efi/vars.
57  *
58  *  12 March 2001 - Matt Domsch <Matt_Domsch@dell.com>
59  *   Feedback received from Stephane Eranian incorporated.
60  *   efivar_write() checks copy_from_user() return value.
61  *   efivar_read/write() returns proper errno.
62  *   v0.02 release to linux-ia64@linuxia64.org
63  *
64  *  26 February 2001 - Matt Domsch <Matt_Domsch@dell.com>
65  *   v0.01 release to linux-ia64@linuxia64.org
66  */
67
68 #include <linux/capability.h>
69 #include <linux/types.h>
70 #include <linux/errno.h>
71 #include <linux/init.h>
72 #include <linux/mm.h>
73 #include <linux/module.h>
74 #include <linux/string.h>
75 #include <linux/smp.h>
76 #include <linux/efi.h>
77 #include <linux/sysfs.h>
78 #include <linux/kobject.h>
79 #include <linux/device.h>
80 #include <linux/slab.h>
81 #include <linux/pstore.h>
82 #include <linux/ctype.h>
83 #include <linux/magic.h>
84
85 #include <linux/fs.h>
86 #include <linux/ramfs.h>
87 #include <linux/pagemap.h>
88
89 #include <asm/uaccess.h>
90
91 #define EFIVARS_VERSION "0.08"
92 #define EFIVARS_DATE "2004-May-17"
93
94 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
95 MODULE_DESCRIPTION("sysfs interface to EFI Variables");
96 MODULE_LICENSE("GPL");
97 MODULE_VERSION(EFIVARS_VERSION);
98
99 #define DUMP_NAME_LEN 52
100
101 /*
102  * Length of a GUID string (strlen("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))
103  * not including trailing NUL
104  */
105 #define GUID_LEN 36
106
107 static bool efivars_pstore_disable =
108         IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
109
110 module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
111
112 /*
113  * The maximum size of VariableName + Data = 1024
114  * Therefore, it's reasonable to save that much
115  * space in each part of the structure,
116  * and we use a page for reading/writing.
117  */
118
119 struct efi_variable {
120         efi_char16_t  VariableName[1024/sizeof(efi_char16_t)];
121         efi_guid_t    VendorGuid;
122         unsigned long DataSize;
123         __u8          Data[1024];
124         efi_status_t  Status;
125         __u32         Attributes;
126 } __attribute__((packed));
127
128 struct efivar_entry {
129         struct efivars *efivars;
130         struct efi_variable var;
131         struct list_head list;
132         struct kobject kobj;
133 };
134
135 struct efivar_attribute {
136         struct attribute attr;
137         ssize_t (*show) (struct efivar_entry *entry, char *buf);
138         ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
139 };
140
141 static struct efivars __efivars;
142 static struct efivar_operations ops;
143
144 #define PSTORE_EFI_ATTRIBUTES \
145         (EFI_VARIABLE_NON_VOLATILE | \
146          EFI_VARIABLE_BOOTSERVICE_ACCESS | \
147          EFI_VARIABLE_RUNTIME_ACCESS)
148
149 #define EFIVAR_ATTR(_name, _mode, _show, _store) \
150 struct efivar_attribute efivar_attr_##_name = { \
151         .attr = {.name = __stringify(_name), .mode = _mode}, \
152         .show = _show, \
153         .store = _store, \
154 };
155
156 #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
157 #define to_efivar_entry(obj)  container_of(obj, struct efivar_entry, kobj)
158
159 /*
160  * Prototype for sysfs creation function
161  */
162 static int
163 efivar_create_sysfs_entry(struct efivars *efivars,
164                           unsigned long variable_name_size,
165                           efi_char16_t *variable_name,
166                           efi_guid_t *vendor_guid);
167
168 /*
169  * Prototype for workqueue functions updating sysfs entry
170  */
171
172 static void efivar_update_sysfs_entries(struct work_struct *);
173 static DECLARE_WORK(efivar_work, efivar_update_sysfs_entries);
174 static bool efivar_wq_enabled = true;
175
176 /* Return the number of unicode characters in data */
177 static unsigned long
178 utf16_strnlen(efi_char16_t *s, size_t maxlength)
179 {
180         unsigned long length = 0;
181
182         while (*s++ != 0 && length < maxlength)
183                 length++;
184         return length;
185 }
186
187 static inline unsigned long
188 utf16_strlen(efi_char16_t *s)
189 {
190         return utf16_strnlen(s, ~0UL);
191 }
192
193 /*
194  * Return the number of bytes is the length of this string
195  * Note: this is NOT the same as the number of unicode characters
196  */
197 static inline unsigned long
198 utf16_strsize(efi_char16_t *data, unsigned long maxlength)
199 {
200         return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
201 }
202
203 static inline int
204 utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
205 {
206         while (1) {
207                 if (len == 0)
208                         return 0;
209                 if (*a < *b)
210                         return -1;
211                 if (*a > *b)
212                         return 1;
213                 if (*a == 0) /* implies *b == 0 */
214                         return 0;
215                 a++;
216                 b++;
217                 len--;
218         }
219 }
220
221 static bool
222 validate_device_path(struct efi_variable *var, int match, u8 *buffer,
223                      unsigned long len)
224 {
225         struct efi_generic_dev_path *node;
226         int offset = 0;
227
228         node = (struct efi_generic_dev_path *)buffer;
229
230         if (len < sizeof(*node))
231                 return false;
232
233         while (offset <= len - sizeof(*node) &&
234                node->length >= sizeof(*node) &&
235                 node->length <= len - offset) {
236                 offset += node->length;
237
238                 if ((node->type == EFI_DEV_END_PATH ||
239                      node->type == EFI_DEV_END_PATH2) &&
240                     node->sub_type == EFI_DEV_END_ENTIRE)
241                         return true;
242
243                 node = (struct efi_generic_dev_path *)(buffer + offset);
244         }
245
246         /*
247          * If we're here then either node->length pointed past the end
248          * of the buffer or we reached the end of the buffer without
249          * finding a device path end node.
250          */
251         return false;
252 }
253
254 static bool
255 validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
256                     unsigned long len)
257 {
258         /* An array of 16-bit integers */
259         if ((len % 2) != 0)
260                 return false;
261
262         return true;
263 }
264
265 static bool
266 validate_load_option(struct efi_variable *var, int match, u8 *buffer,
267                      unsigned long len)
268 {
269         u16 filepathlength;
270         int i, desclength = 0, namelen;
271
272         namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
273
274         /* Either "Boot" or "Driver" followed by four digits of hex */
275         for (i = match; i < match+4; i++) {
276                 if (var->VariableName[i] > 127 ||
277                     hex_to_bin(var->VariableName[i] & 0xff) < 0)
278                         return true;
279         }
280
281         /* Reject it if there's 4 digits of hex and then further content */
282         if (namelen > match + 4)
283                 return false;
284
285         /* A valid entry must be at least 8 bytes */
286         if (len < 8)
287                 return false;
288
289         filepathlength = buffer[4] | buffer[5] << 8;
290
291         /*
292          * There's no stored length for the description, so it has to be
293          * found by hand
294          */
295         desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
296
297         /* Each boot entry must have a descriptor */
298         if (!desclength)
299                 return false;
300
301         /*
302          * If the sum of the length of the description, the claimed filepath
303          * length and the original header are greater than the length of the
304          * variable, it's malformed
305          */
306         if ((desclength + filepathlength + 6) > len)
307                 return false;
308
309         /*
310          * And, finally, check the filepath
311          */
312         return validate_device_path(var, match, buffer + desclength + 6,
313                                     filepathlength);
314 }
315
316 static bool
317 validate_uint16(struct efi_variable *var, int match, u8 *buffer,
318                 unsigned long len)
319 {
320         /* A single 16-bit integer */
321         if (len != 2)
322                 return false;
323
324         return true;
325 }
326
327 static bool
328 validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
329                       unsigned long len)
330 {
331         int i;
332
333         for (i = 0; i < len; i++) {
334                 if (buffer[i] > 127)
335                         return false;
336
337                 if (buffer[i] == 0)
338                         return true;
339         }
340
341         return false;
342 }
343
344 struct variable_validate {
345         char *name;
346         bool (*validate)(struct efi_variable *var, int match, u8 *data,
347                          unsigned long len);
348 };
349
350 static const struct variable_validate variable_validate[] = {
351         { "BootNext", validate_uint16 },
352         { "BootOrder", validate_boot_order },
353         { "DriverOrder", validate_boot_order },
354         { "Boot*", validate_load_option },
355         { "Driver*", validate_load_option },
356         { "ConIn", validate_device_path },
357         { "ConInDev", validate_device_path },
358         { "ConOut", validate_device_path },
359         { "ConOutDev", validate_device_path },
360         { "ErrOut", validate_device_path },
361         { "ErrOutDev", validate_device_path },
362         { "Timeout", validate_uint16 },
363         { "Lang", validate_ascii_string },
364         { "PlatformLang", validate_ascii_string },
365         { "", NULL },
366 };
367
368 static bool
369 validate_var(struct efi_variable *var, u8 *data, unsigned long len)
370 {
371         int i;
372         u16 *unicode_name = var->VariableName;
373
374         for (i = 0; variable_validate[i].validate != NULL; i++) {
375                 const char *name = variable_validate[i].name;
376                 int match;
377
378                 for (match = 0; ; match++) {
379                         char c = name[match];
380                         u16 u = unicode_name[match];
381
382                         /* All special variables are plain ascii */
383                         if (u > 127)
384                                 return true;
385
386                         /* Wildcard in the matching name means we've matched */
387                         if (c == '*')
388                                 return variable_validate[i].validate(var,
389                                                              match, data, len);
390
391                         /* Case sensitive match */
392                         if (c != u)
393                                 break;
394
395                         /* Reached the end of the string while matching */
396                         if (!c)
397                                 return variable_validate[i].validate(var,
398                                                              match, data, len);
399                 }
400         }
401
402         return true;
403 }
404
405 static efi_status_t
406 get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
407 {
408         efi_status_t status;
409
410         var->DataSize = 1024;
411         status = efivars->ops->get_variable(var->VariableName,
412                                             &var->VendorGuid,
413                                             &var->Attributes,
414                                             &var->DataSize,
415                                             var->Data);
416         return status;
417 }
418
419 static efi_status_t
420 get_var_data(struct efivars *efivars, struct efi_variable *var)
421 {
422         efi_status_t status;
423         unsigned long flags;
424
425         spin_lock_irqsave(&efivars->lock, flags);
426         status = get_var_data_locked(efivars, var);
427         spin_unlock_irqrestore(&efivars->lock, flags);
428
429         if (status != EFI_SUCCESS) {
430                 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
431                         status);
432         }
433         return status;
434 }
435
436 static efi_status_t
437 check_var_size_locked(struct efivars *efivars, u32 attributes,
438                         unsigned long size)
439 {
440         u64 storage_size, remaining_size, max_size;
441         efi_status_t status;
442         const struct efivar_operations *fops = efivars->ops;
443
444         if (!efivars->ops->query_variable_info)
445                 return EFI_UNSUPPORTED;
446
447         status = fops->query_variable_info(attributes, &storage_size,
448                                            &remaining_size, &max_size);
449
450         if (status != EFI_SUCCESS)
451                 return status;
452
453         if (!storage_size || size > remaining_size || size > max_size ||
454             (remaining_size - size) < (storage_size / 2))
455                 return EFI_OUT_OF_RESOURCES;
456
457         return status;
458 }
459
460
461 static efi_status_t
462 check_var_size(struct efivars *efivars, u32 attributes, unsigned long size)
463 {
464         efi_status_t status;
465         unsigned long flags;
466
467         spin_lock_irqsave(&efivars->lock, flags);
468         status = check_var_size_locked(efivars, attributes, size);
469         spin_unlock_irqrestore(&efivars->lock, flags);
470
471         return status;
472 }
473
474 static ssize_t
475 efivar_guid_read(struct efivar_entry *entry, char *buf)
476 {
477         struct efi_variable *var = &entry->var;
478         char *str = buf;
479
480         if (!entry || !buf)
481                 return 0;
482
483         efi_guid_unparse(&var->VendorGuid, str);
484         str += strlen(str);
485         str += sprintf(str, "\n");
486
487         return str - buf;
488 }
489
490 static ssize_t
491 efivar_attr_read(struct efivar_entry *entry, char *buf)
492 {
493         struct efi_variable *var = &entry->var;
494         char *str = buf;
495         efi_status_t status;
496
497         if (!entry || !buf)
498                 return -EINVAL;
499
500         status = get_var_data(entry->efivars, var);
501         if (status != EFI_SUCCESS)
502                 return -EIO;
503
504         if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
505                 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
506         if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
507                 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
508         if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
509                 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
510         if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
511                 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
512         if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
513                 str += sprintf(str,
514                         "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
515         if (var->Attributes &
516                         EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
517                 str += sprintf(str,
518                         "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
519         if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
520                 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
521         return str - buf;
522 }
523
524 static ssize_t
525 efivar_size_read(struct efivar_entry *entry, char *buf)
526 {
527         struct efi_variable *var = &entry->var;
528         char *str = buf;
529         efi_status_t status;
530
531         if (!entry || !buf)
532                 return -EINVAL;
533
534         status = get_var_data(entry->efivars, var);
535         if (status != EFI_SUCCESS)
536                 return -EIO;
537
538         str += sprintf(str, "0x%lx\n", var->DataSize);
539         return str - buf;
540 }
541
542 static ssize_t
543 efivar_data_read(struct efivar_entry *entry, char *buf)
544 {
545         struct efi_variable *var = &entry->var;
546         efi_status_t status;
547
548         if (!entry || !buf)
549                 return -EINVAL;
550
551         status = get_var_data(entry->efivars, var);
552         if (status != EFI_SUCCESS)
553                 return -EIO;
554
555         memcpy(buf, var->Data, var->DataSize);
556         return var->DataSize;
557 }
558 /*
559  * We allow each variable to be edited via rewriting the
560  * entire efi variable structure.
561  */
562 static ssize_t
563 efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
564 {
565         struct efi_variable *new_var, *var = &entry->var;
566         struct efivars *efivars = entry->efivars;
567         efi_status_t status = EFI_NOT_FOUND;
568
569         if (count != sizeof(struct efi_variable))
570                 return -EINVAL;
571
572         new_var = (struct efi_variable *)buf;
573         /*
574          * If only updating the variable data, then the name
575          * and guid should remain the same
576          */
577         if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
578                 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
579                 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
580                 return -EINVAL;
581         }
582
583         if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
584                 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
585                 return -EINVAL;
586         }
587
588         if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
589             validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
590                 printk(KERN_ERR "efivars: Malformed variable content\n");
591                 return -EINVAL;
592         }
593
594         spin_lock_irq(&efivars->lock);
595
596         status = check_var_size_locked(efivars, new_var->Attributes,
597                new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
598
599         if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
600                 status = efivars->ops->set_variable(new_var->VariableName,
601                                                     &new_var->VendorGuid,
602                                                     new_var->Attributes,
603                                                     new_var->DataSize,
604                                                     new_var->Data);
605
606         spin_unlock_irq(&efivars->lock);
607
608         if (status != EFI_SUCCESS) {
609                 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
610                         status);
611                 return -EIO;
612         }
613
614         memcpy(&entry->var, new_var, count);
615         return count;
616 }
617
618 static ssize_t
619 efivar_show_raw(struct efivar_entry *entry, char *buf)
620 {
621         struct efi_variable *var = &entry->var;
622         efi_status_t status;
623
624         if (!entry || !buf)
625                 return 0;
626
627         status = get_var_data(entry->efivars, var);
628         if (status != EFI_SUCCESS)
629                 return -EIO;
630
631         memcpy(buf, var, sizeof(*var));
632         return sizeof(*var);
633 }
634
635 /*
636  * Generic read/write functions that call the specific functions of
637  * the attributes...
638  */
639 static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
640                                 char *buf)
641 {
642         struct efivar_entry *var = to_efivar_entry(kobj);
643         struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
644         ssize_t ret = -EIO;
645
646         if (!capable(CAP_SYS_ADMIN))
647                 return -EACCES;
648
649         if (efivar_attr->show) {
650                 ret = efivar_attr->show(var, buf);
651         }
652         return ret;
653 }
654
655 static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
656                                 const char *buf, size_t count)
657 {
658         struct efivar_entry *var = to_efivar_entry(kobj);
659         struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
660         ssize_t ret = -EIO;
661
662         if (!capable(CAP_SYS_ADMIN))
663                 return -EACCES;
664
665         if (efivar_attr->store)
666                 ret = efivar_attr->store(var, buf, count);
667
668         return ret;
669 }
670
671 static const struct sysfs_ops efivar_attr_ops = {
672         .show = efivar_attr_show,
673         .store = efivar_attr_store,
674 };
675
676 static void efivar_release(struct kobject *kobj)
677 {
678         struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
679         kfree(var);
680 }
681
682 static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
683 static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
684 static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
685 static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
686 static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
687
688 static struct attribute *def_attrs[] = {
689         &efivar_attr_guid.attr,
690         &efivar_attr_size.attr,
691         &efivar_attr_attributes.attr,
692         &efivar_attr_data.attr,
693         &efivar_attr_raw_var.attr,
694         NULL,
695 };
696
697 static struct kobj_type efivar_ktype = {
698         .release = efivar_release,
699         .sysfs_ops = &efivar_attr_ops,
700         .default_attrs = def_attrs,
701 };
702
703 static inline void
704 efivar_unregister(struct efivar_entry *var)
705 {
706         kobject_put(&var->kobj);
707 }
708
709 static int efivarfs_file_open(struct inode *inode, struct file *file)
710 {
711         file->private_data = inode->i_private;
712         return 0;
713 }
714
715 static int efi_status_to_err(efi_status_t status)
716 {
717         int err;
718
719         switch (status) {
720         case EFI_INVALID_PARAMETER:
721                 err = -EINVAL;
722                 break;
723         case EFI_OUT_OF_RESOURCES:
724                 err = -ENOSPC;
725                 break;
726         case EFI_DEVICE_ERROR:
727                 err = -EIO;
728                 break;
729         case EFI_WRITE_PROTECTED:
730                 err = -EROFS;
731                 break;
732         case EFI_SECURITY_VIOLATION:
733                 err = -EACCES;
734                 break;
735         case EFI_NOT_FOUND:
736                 err = -EIO;
737                 break;
738         default:
739                 err = -EINVAL;
740         }
741
742         return err;
743 }
744
745 static ssize_t efivarfs_file_write(struct file *file,
746                 const char __user *userbuf, size_t count, loff_t *ppos)
747 {
748         struct efivar_entry *var = file->private_data;
749         struct efivars *efivars;
750         efi_status_t status;
751         void *data;
752         u32 attributes;
753         struct inode *inode = file->f_mapping->host;
754         unsigned long datasize = count - sizeof(attributes);
755         unsigned long newdatasize, varsize;
756         ssize_t bytes = 0;
757
758         if (count < sizeof(attributes))
759                 return -EINVAL;
760
761         if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
762                 return -EFAULT;
763
764         if (attributes & ~(EFI_VARIABLE_MASK))
765                 return -EINVAL;
766
767         efivars = var->efivars;
768
769         /*
770          * Ensure that the user can't allocate arbitrarily large
771          * amounts of memory. Pick a default size of 64K if
772          * QueryVariableInfo() isn't supported by the firmware.
773          */
774
775         varsize = datasize + utf16_strsize(var->var.VariableName, 1024);
776         status = check_var_size(efivars, attributes, varsize);
777
778         if (status != EFI_SUCCESS) {
779                 if (status != EFI_UNSUPPORTED)
780                         return efi_status_to_err(status);
781
782                 if (datasize > 65536)
783                         return -ENOSPC;
784         }
785
786         data = kmalloc(datasize, GFP_KERNEL);
787         if (!data)
788                 return -ENOMEM;
789
790         if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
791                 bytes = -EFAULT;
792                 goto out;
793         }
794
795         if (validate_var(&var->var, data, datasize) == false) {
796                 bytes = -EINVAL;
797                 goto out;
798         }
799
800         /*
801          * The lock here protects the get_variable call, the conditional
802          * set_variable call, and removal of the variable from the efivars
803          * list (in the case of an authenticated delete).
804          */
805         spin_lock_irq(&efivars->lock);
806
807         /*
808          * Ensure that the available space hasn't shrunk below the safe level
809          */
810
811         status = check_var_size_locked(efivars, attributes, varsize);
812
813         if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED) {
814                 spin_unlock_irq(&efivars->lock);
815                 kfree(data);
816
817                 return efi_status_to_err(status);
818         }
819
820         status = efivars->ops->set_variable(var->var.VariableName,
821                                             &var->var.VendorGuid,
822                                             attributes, datasize,
823                                             data);
824
825         if (status != EFI_SUCCESS) {
826                 spin_unlock_irq(&efivars->lock);
827                 kfree(data);
828
829                 return efi_status_to_err(status);
830         }
831
832         bytes = count;
833
834         /*
835          * Writing to the variable may have caused a change in size (which
836          * could either be an append or an overwrite), or the variable to be
837          * deleted. Perform a GetVariable() so we can tell what actually
838          * happened.
839          */
840         newdatasize = 0;
841         status = efivars->ops->get_variable(var->var.VariableName,
842                                             &var->var.VendorGuid,
843                                             NULL, &newdatasize,
844                                             NULL);
845
846         if (status == EFI_BUFFER_TOO_SMALL) {
847                 spin_unlock_irq(&efivars->lock);
848                 mutex_lock(&inode->i_mutex);
849                 i_size_write(inode, newdatasize + sizeof(attributes));
850                 mutex_unlock(&inode->i_mutex);
851
852         } else if (status == EFI_NOT_FOUND) {
853                 list_del(&var->list);
854                 spin_unlock_irq(&efivars->lock);
855                 efivar_unregister(var);
856                 drop_nlink(inode);
857                 d_delete(file->f_dentry);
858                 dput(file->f_dentry);
859
860         } else {
861                 spin_unlock_irq(&efivars->lock);
862                 pr_warn("efivarfs: inconsistent EFI variable implementation? "
863                                 "status = %lx\n", status);
864         }
865
866 out:
867         kfree(data);
868
869         return bytes;
870 }
871
872 static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
873                 size_t count, loff_t *ppos)
874 {
875         struct efivar_entry *var = file->private_data;
876         struct efivars *efivars = var->efivars;
877         efi_status_t status;
878         unsigned long datasize = 0;
879         u32 attributes;
880         void *data;
881         ssize_t size = 0;
882
883         spin_lock_irq(&efivars->lock);
884         status = efivars->ops->get_variable(var->var.VariableName,
885                                             &var->var.VendorGuid,
886                                             &attributes, &datasize, NULL);
887         spin_unlock_irq(&efivars->lock);
888
889         if (status != EFI_BUFFER_TOO_SMALL)
890                 return efi_status_to_err(status);
891
892         data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
893
894         if (!data)
895                 return -ENOMEM;
896
897         spin_lock_irq(&efivars->lock);
898         status = efivars->ops->get_variable(var->var.VariableName,
899                                             &var->var.VendorGuid,
900                                             &attributes, &datasize,
901                                             (data + sizeof(attributes)));
902         spin_unlock_irq(&efivars->lock);
903
904         if (status != EFI_SUCCESS) {
905                 size = efi_status_to_err(status);
906                 goto out_free;
907         }
908
909         memcpy(data, &attributes, sizeof(attributes));
910         size = simple_read_from_buffer(userbuf, count, ppos,
911                                        data, datasize + sizeof(attributes));
912 out_free:
913         kfree(data);
914
915         return size;
916 }
917
918 static void efivarfs_evict_inode(struct inode *inode)
919 {
920         clear_inode(inode);
921 }
922
923 static const struct super_operations efivarfs_ops = {
924         .statfs = simple_statfs,
925         .drop_inode = generic_delete_inode,
926         .evict_inode = efivarfs_evict_inode,
927         .show_options = generic_show_options,
928 };
929
930 static struct super_block *efivarfs_sb;
931
932 static const struct inode_operations efivarfs_dir_inode_operations;
933
934 static const struct file_operations efivarfs_file_operations = {
935         .open   = efivarfs_file_open,
936         .read   = efivarfs_file_read,
937         .write  = efivarfs_file_write,
938         .llseek = no_llseek,
939 };
940
941 static struct inode *efivarfs_get_inode(struct super_block *sb,
942                                 const struct inode *dir, int mode, dev_t dev)
943 {
944         struct inode *inode = new_inode(sb);
945
946         if (inode) {
947                 inode->i_ino = get_next_ino();
948                 inode->i_mode = mode;
949                 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
950                 switch (mode & S_IFMT) {
951                 case S_IFREG:
952                         inode->i_fop = &efivarfs_file_operations;
953                         break;
954                 case S_IFDIR:
955                         inode->i_op = &efivarfs_dir_inode_operations;
956                         inode->i_fop = &simple_dir_operations;
957                         inc_nlink(inode);
958                         break;
959                 }
960         }
961         return inode;
962 }
963
964 /*
965  * Return true if 'str' is a valid efivarfs filename of the form,
966  *
967  *      VariableName-12345678-1234-1234-1234-1234567891bc
968  */
969 static bool efivarfs_valid_name(const char *str, int len)
970 {
971         static const char dashes[GUID_LEN] = {
972                 [8] = 1, [13] = 1, [18] = 1, [23] = 1
973         };
974         const char *s = str + len - GUID_LEN;
975         int i;
976
977         /*
978          * We need a GUID, plus at least one letter for the variable name,
979          * plus the '-' separator
980          */
981         if (len < GUID_LEN + 2)
982                 return false;
983
984         /* GUID must be preceded by a '-' */
985         if (*(s - 1) != '-')
986                 return false;
987
988         /*
989          * Validate that 's' is of the correct format, e.g.
990          *
991          *      12345678-1234-1234-1234-123456789abc
992          */
993         for (i = 0; i < GUID_LEN; i++) {
994                 if (dashes[i]) {
995                         if (*s++ != '-')
996                                 return false;
997                 } else {
998                         if (!isxdigit(*s++))
999                                 return false;
1000                 }
1001         }
1002
1003         return true;
1004 }
1005
1006 static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
1007 {
1008         guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
1009         guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
1010         guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
1011         guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
1012         guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
1013         guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
1014         guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
1015         guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
1016         guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
1017         guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
1018         guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
1019         guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
1020         guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
1021         guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
1022         guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
1023         guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
1024 }
1025
1026 static int efivarfs_create(struct inode *dir, struct dentry *dentry,
1027                           umode_t mode, bool excl)
1028 {
1029         struct inode *inode;
1030         struct efivars *efivars = &__efivars;
1031         struct efivar_entry *var;
1032         int namelen, i = 0, err = 0;
1033
1034         if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
1035                 return -EINVAL;
1036
1037         inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
1038         if (!inode)
1039                 return -ENOMEM;
1040
1041         var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
1042         if (!var) {
1043                 err = -ENOMEM;
1044                 goto out;
1045         }
1046
1047         /* length of the variable name itself: remove GUID and separator */
1048         namelen = dentry->d_name.len - GUID_LEN - 1;
1049
1050         efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
1051                         &var->var.VendorGuid);
1052
1053         for (i = 0; i < namelen; i++)
1054                 var->var.VariableName[i] = dentry->d_name.name[i];
1055
1056         var->var.VariableName[i] = '\0';
1057
1058         inode->i_private = var;
1059         var->efivars = efivars;
1060         var->kobj.kset = efivars->kset;
1061
1062         err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
1063                              dentry->d_name.name);
1064         if (err)
1065                 goto out;
1066
1067         kobject_uevent(&var->kobj, KOBJ_ADD);
1068         spin_lock_irq(&efivars->lock);
1069         list_add(&var->list, &efivars->list);
1070         spin_unlock_irq(&efivars->lock);
1071         d_instantiate(dentry, inode);
1072         dget(dentry);
1073 out:
1074         if (err) {
1075                 kfree(var);
1076                 iput(inode);
1077         }
1078         return err;
1079 }
1080
1081 static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
1082 {
1083         struct efivar_entry *var = dentry->d_inode->i_private;
1084         struct efivars *efivars = var->efivars;
1085         efi_status_t status;
1086
1087         spin_lock_irq(&efivars->lock);
1088
1089         status = efivars->ops->set_variable(var->var.VariableName,
1090                                             &var->var.VendorGuid,
1091                                             0, 0, NULL);
1092
1093         if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
1094                 list_del(&var->list);
1095                 spin_unlock_irq(&efivars->lock);
1096                 efivar_unregister(var);
1097                 drop_nlink(dentry->d_inode);
1098                 dput(dentry);
1099                 return 0;
1100         }
1101
1102         spin_unlock_irq(&efivars->lock);
1103         return -EINVAL;
1104 };
1105
1106 /*
1107  * Compare two efivarfs file names.
1108  *
1109  * An efivarfs filename is composed of two parts,
1110  *
1111  *      1. A case-sensitive variable name
1112  *      2. A case-insensitive GUID
1113  *
1114  * So we need to perform a case-sensitive match on part 1 and a
1115  * case-insensitive match on part 2.
1116  */
1117 static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
1118                               const struct dentry *dentry, const struct inode *inode,
1119                               unsigned int len, const char *str,
1120                               const struct qstr *name)
1121 {
1122         int guid = len - GUID_LEN;
1123
1124         if (name->len != len)
1125                 return 1;
1126
1127         /* Case-sensitive compare for the variable name */
1128         if (memcmp(str, name->name, guid))
1129                 return 1;
1130
1131         /* Case-insensitive compare for the GUID */
1132         return strncasecmp(name->name + guid, str + guid, GUID_LEN);
1133 }
1134
1135 static int efivarfs_d_hash(const struct dentry *dentry,
1136                            const struct inode *inode, struct qstr *qstr)
1137 {
1138         unsigned long hash = init_name_hash();
1139         const unsigned char *s = qstr->name;
1140         unsigned int len = qstr->len;
1141
1142         if (!efivarfs_valid_name(s, len))
1143                 return -EINVAL;
1144
1145         while (len-- > GUID_LEN)
1146                 hash = partial_name_hash(*s++, hash);
1147
1148         /* GUID is case-insensitive. */
1149         while (len--)
1150                 hash = partial_name_hash(tolower(*s++), hash);
1151
1152         qstr->hash = end_name_hash(hash);
1153         return 0;
1154 }
1155
1156 /*
1157  * Retaining negative dentries for an in-memory filesystem just wastes
1158  * memory and lookup time: arrange for them to be deleted immediately.
1159  */
1160 static int efivarfs_delete_dentry(const struct dentry *dentry)
1161 {
1162         return 1;
1163 }
1164
1165 static struct dentry_operations efivarfs_d_ops = {
1166         .d_compare = efivarfs_d_compare,
1167         .d_hash = efivarfs_d_hash,
1168         .d_delete = efivarfs_delete_dentry,
1169 };
1170
1171 static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
1172 {
1173         struct dentry *d;
1174         struct qstr q;
1175         int err;
1176
1177         q.name = name;
1178         q.len = strlen(name);
1179
1180         err = efivarfs_d_hash(NULL, NULL, &q);
1181         if (err)
1182                 return ERR_PTR(err);
1183
1184         d = d_alloc(parent, &q);
1185         if (d)
1186                 return d;
1187
1188         return ERR_PTR(-ENOMEM);
1189 }
1190
1191 static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
1192 {
1193         struct inode *inode = NULL;
1194         struct dentry *root;
1195         struct efivar_entry *entry, *n;
1196         struct efivars *efivars = &__efivars;
1197         char *name;
1198         int err = -ENOMEM;
1199
1200         efivarfs_sb = sb;
1201
1202         sb->s_maxbytes          = MAX_LFS_FILESIZE;
1203         sb->s_blocksize         = PAGE_CACHE_SIZE;
1204         sb->s_blocksize_bits    = PAGE_CACHE_SHIFT;
1205         sb->s_magic             = EFIVARFS_MAGIC;
1206         sb->s_op                = &efivarfs_ops;
1207         sb->s_d_op              = &efivarfs_d_ops;
1208         sb->s_time_gran         = 1;
1209
1210         inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
1211         if (!inode)
1212                 return -ENOMEM;
1213         inode->i_op = &efivarfs_dir_inode_operations;
1214
1215         root = d_make_root(inode);
1216         sb->s_root = root;
1217         if (!root)
1218                 return -ENOMEM;
1219
1220         list_for_each_entry_safe(entry, n, &efivars->list, list) {
1221                 struct dentry *dentry, *root = efivarfs_sb->s_root;
1222                 unsigned long size = 0;
1223                 int len, i;
1224
1225                 inode = NULL;
1226
1227                 len = utf16_strlen(entry->var.VariableName);
1228
1229                 /* name, plus '-', plus GUID, plus NUL*/
1230                 name = kmalloc(len + 1 + GUID_LEN + 1, GFP_ATOMIC);
1231                 if (!name)
1232                         goto fail;
1233
1234                 for (i = 0; i < len; i++)
1235                         name[i] = entry->var.VariableName[i] & 0xFF;
1236
1237                 name[len] = '-';
1238
1239                 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
1240
1241                 name[len+GUID_LEN+1] = '\0';
1242
1243                 inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
1244                                           S_IFREG | 0644, 0);
1245                 if (!inode)
1246                         goto fail_name;
1247
1248                 dentry = efivarfs_alloc_dentry(root, name);
1249                 if (IS_ERR(dentry)) {
1250                         err = PTR_ERR(dentry);
1251                         goto fail_inode;
1252                 }
1253
1254                 /* copied by the above to local storage in the dentry. */
1255                 kfree(name);
1256
1257                 spin_lock_irq(&efivars->lock);
1258                 efivars->ops->get_variable(entry->var.VariableName,
1259                                            &entry->var.VendorGuid,
1260                                            &entry->var.Attributes,
1261                                            &size,
1262                                            NULL);
1263                 spin_unlock_irq(&efivars->lock);
1264
1265                 mutex_lock(&inode->i_mutex);
1266                 inode->i_private = entry;
1267                 i_size_write(inode, size + sizeof(entry->var.Attributes));
1268                 mutex_unlock(&inode->i_mutex);
1269                 d_add(dentry, inode);
1270         }
1271
1272         return 0;
1273
1274 fail_inode:
1275         iput(inode);
1276 fail_name:
1277         kfree(name);
1278 fail:
1279         return err;
1280 }
1281
1282 static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
1283                                     int flags, const char *dev_name, void *data)
1284 {
1285         return mount_single(fs_type, flags, data, efivarfs_fill_super);
1286 }
1287
1288 static void efivarfs_kill_sb(struct super_block *sb)
1289 {
1290         kill_litter_super(sb);
1291         efivarfs_sb = NULL;
1292 }
1293
1294 static struct file_system_type efivarfs_type = {
1295         .name    = "efivarfs",
1296         .mount   = efivarfs_mount,
1297         .kill_sb = efivarfs_kill_sb,
1298 };
1299 MODULE_ALIAS_FS("efivarfs");
1300
1301 /*
1302  * Handle negative dentry.
1303  */
1304 static struct dentry *efivarfs_lookup(struct inode *dir, struct dentry *dentry,
1305                                       unsigned int flags)
1306 {
1307         if (dentry->d_name.len > NAME_MAX)
1308                 return ERR_PTR(-ENAMETOOLONG);
1309         d_add(dentry, NULL);
1310         return NULL;
1311 }
1312
1313 static const struct inode_operations efivarfs_dir_inode_operations = {
1314         .lookup = efivarfs_lookup,
1315         .unlink = efivarfs_unlink,
1316         .create = efivarfs_create,
1317 };
1318
1319 #ifdef CONFIG_EFI_VARS_PSTORE
1320
1321 static int efi_pstore_open(struct pstore_info *psi)
1322 {
1323         struct efivars *efivars = psi->data;
1324
1325         spin_lock_irq(&efivars->lock);
1326         efivars->walk_entry = list_first_entry(&efivars->list,
1327                                                struct efivar_entry, list);
1328         return 0;
1329 }
1330
1331 static int efi_pstore_close(struct pstore_info *psi)
1332 {
1333         struct efivars *efivars = psi->data;
1334
1335         spin_unlock_irq(&efivars->lock);
1336         return 0;
1337 }
1338
1339 static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
1340                                int *count, struct timespec *timespec,
1341                                char **buf, struct pstore_info *psi)
1342 {
1343         efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1344         struct efivars *efivars = psi->data;
1345         char name[DUMP_NAME_LEN];
1346         int i;
1347         int cnt;
1348         unsigned int part, size;
1349         unsigned long time;
1350
1351         while (&efivars->walk_entry->list != &efivars->list) {
1352                 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
1353                                  vendor)) {
1354                         for (i = 0; i < DUMP_NAME_LEN; i++) {
1355                                 name[i] = efivars->walk_entry->var.VariableName[i];
1356                         }
1357                         if (sscanf(name, "dump-type%u-%u-%d-%lu",
1358                                    type, &part, &cnt, &time) == 4) {
1359                                 *id = part;
1360                                 *count = cnt;
1361                                 timespec->tv_sec = time;
1362                                 timespec->tv_nsec = 0;
1363                         } else if (sscanf(name, "dump-type%u-%u-%lu",
1364                                    type, &part, &time) == 3) {
1365                                 /*
1366                                  * Check if an old format,
1367                                  * which doesn't support holding
1368                                  * multiple logs, remains.
1369                                  */
1370                                 *id = part;
1371                                 *count = 0;
1372                                 timespec->tv_sec = time;
1373                                 timespec->tv_nsec = 0;
1374                         } else {
1375                                 efivars->walk_entry = list_entry(
1376                                                 efivars->walk_entry->list.next,
1377                                                 struct efivar_entry, list);
1378                                 continue;
1379                         }
1380
1381                         get_var_data_locked(efivars, &efivars->walk_entry->var);
1382                         size = efivars->walk_entry->var.DataSize;
1383                         *buf = kmalloc(size, GFP_KERNEL);
1384                         if (*buf == NULL)
1385                                 return -ENOMEM;
1386                         memcpy(*buf, efivars->walk_entry->var.Data,
1387                                size);
1388                         efivars->walk_entry = list_entry(
1389                                         efivars->walk_entry->list.next,
1390                                         struct efivar_entry, list);
1391                         return size;
1392                 }
1393                 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1394                                                  struct efivar_entry, list);
1395         }
1396         return 0;
1397 }
1398
1399 static int efi_pstore_write(enum pstore_type_id type,
1400                 enum kmsg_dump_reason reason, u64 *id,
1401                 unsigned int part, int count, size_t size,
1402                 struct pstore_info *psi)
1403 {
1404         char name[DUMP_NAME_LEN];
1405         efi_char16_t efi_name[DUMP_NAME_LEN];
1406         efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1407         struct efivars *efivars = psi->data;
1408         int i, ret = 0;
1409         efi_status_t status = EFI_NOT_FOUND;
1410         unsigned long flags;
1411
1412         if (pstore_cannot_block_path(reason)) {
1413                 /*
1414                  * If the lock is taken by another cpu in non-blocking path,
1415                  * this driver returns without entering firmware to avoid
1416                  * hanging up.
1417                  */
1418                 if (!spin_trylock_irqsave(&efivars->lock, flags))
1419                         return -EBUSY;
1420         } else
1421                 spin_lock_irqsave(&efivars->lock, flags);
1422
1423         /*
1424          * Check if there is a space enough to log.
1425          * size: a size of logging data
1426          * DUMP_NAME_LEN * 2: a maximum size of variable name
1427          */
1428
1429         status = check_var_size_locked(efivars, PSTORE_EFI_ATTRIBUTES,
1430                                          size + DUMP_NAME_LEN * 2);
1431
1432         if (status) {
1433                 spin_unlock_irqrestore(&efivars->lock, flags);
1434                 *id = part;
1435                 return -ENOSPC;
1436         }
1437
1438         sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
1439                 get_seconds());
1440
1441         for (i = 0; i < DUMP_NAME_LEN; i++)
1442                 efi_name[i] = name[i];
1443
1444         efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
1445                                    size, psi->buf);
1446
1447         spin_unlock_irqrestore(&efivars->lock, flags);
1448
1449         if (reason == KMSG_DUMP_OOPS && efivar_wq_enabled)
1450                 schedule_work(&efivar_work);
1451
1452         *id = part;
1453         return ret;
1454 };
1455
1456 static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
1457                             struct timespec time, struct pstore_info *psi)
1458 {
1459         char name[DUMP_NAME_LEN];
1460         efi_char16_t efi_name[DUMP_NAME_LEN];
1461         char name_old[DUMP_NAME_LEN];
1462         efi_char16_t efi_name_old[DUMP_NAME_LEN];
1463         efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1464         struct efivars *efivars = psi->data;
1465         struct efivar_entry *entry, *found = NULL;
1466         int i;
1467
1468         sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
1469                 time.tv_sec);
1470
1471         spin_lock_irq(&efivars->lock);
1472
1473         for (i = 0; i < DUMP_NAME_LEN; i++)
1474                 efi_name[i] = name[i];
1475
1476         /*
1477          * Clean up an entry with the same name
1478          */
1479
1480         list_for_each_entry(entry, &efivars->list, list) {
1481                 get_var_data_locked(efivars, &entry->var);
1482
1483                 if (efi_guidcmp(entry->var.VendorGuid, vendor))
1484                         continue;
1485                 if (utf16_strncmp(entry->var.VariableName, efi_name,
1486                                   utf16_strlen(efi_name))) {
1487                         /*
1488                          * Check if an old format,
1489                          * which doesn't support holding
1490                          * multiple logs, remains.
1491                          */
1492                         sprintf(name_old, "dump-type%u-%u-%lu", type,
1493                                 (unsigned int)id, time.tv_sec);
1494
1495                         for (i = 0; i < DUMP_NAME_LEN; i++)
1496                                 efi_name_old[i] = name_old[i];
1497
1498                         if (utf16_strncmp(entry->var.VariableName, efi_name_old,
1499                                           utf16_strlen(efi_name_old)))
1500                                 continue;
1501                 }
1502
1503                 /* found */
1504                 found = entry;
1505                 efivars->ops->set_variable(entry->var.VariableName,
1506                                            &entry->var.VendorGuid,
1507                                            PSTORE_EFI_ATTRIBUTES,
1508                                            0, NULL);
1509                 break;
1510         }
1511
1512         if (found)
1513                 list_del(&found->list);
1514
1515         spin_unlock_irq(&efivars->lock);
1516
1517         if (found)
1518                 efivar_unregister(found);
1519
1520         return 0;
1521 }
1522
1523 static struct pstore_info efi_pstore_info = {
1524         .owner          = THIS_MODULE,
1525         .name           = "efi",
1526         .open           = efi_pstore_open,
1527         .close          = efi_pstore_close,
1528         .read           = efi_pstore_read,
1529         .write          = efi_pstore_write,
1530         .erase          = efi_pstore_erase,
1531 };
1532
1533 static void efivar_pstore_register(struct efivars *efivars)
1534 {
1535         efivars->efi_pstore_info = efi_pstore_info;
1536         efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
1537         if (efivars->efi_pstore_info.buf) {
1538                 efivars->efi_pstore_info.bufsize = 1024;
1539                 efivars->efi_pstore_info.data = efivars;
1540                 spin_lock_init(&efivars->efi_pstore_info.buf_lock);
1541                 pstore_register(&efivars->efi_pstore_info);
1542         }
1543 }
1544 #else
1545 static void efivar_pstore_register(struct efivars *efivars)
1546 {
1547         return;
1548 }
1549 #endif
1550
1551 static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
1552                              struct bin_attribute *bin_attr,
1553                              char *buf, loff_t pos, size_t count)
1554 {
1555         struct efi_variable *new_var = (struct efi_variable *)buf;
1556         struct efivars *efivars = bin_attr->private;
1557         struct efivar_entry *search_efivar, *n;
1558         unsigned long strsize1, strsize2;
1559         efi_status_t status = EFI_NOT_FOUND;
1560         int found = 0;
1561
1562         if (!capable(CAP_SYS_ADMIN))
1563                 return -EACCES;
1564
1565         if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
1566             validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
1567                 printk(KERN_ERR "efivars: Malformed variable content\n");
1568                 return -EINVAL;
1569         }
1570
1571         spin_lock_irq(&efivars->lock);
1572
1573         /*
1574          * Does this variable already exist?
1575          */
1576         list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
1577                 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1578                 strsize2 = utf16_strsize(new_var->VariableName, 1024);
1579                 if (strsize1 == strsize2 &&
1580                         !memcmp(&(search_efivar->var.VariableName),
1581                                 new_var->VariableName, strsize1) &&
1582                         !efi_guidcmp(search_efivar->var.VendorGuid,
1583                                 new_var->VendorGuid)) {
1584                         found = 1;
1585                         break;
1586                 }
1587         }
1588         if (found) {
1589                 spin_unlock_irq(&efivars->lock);
1590                 return -EINVAL;
1591         }
1592
1593         status = check_var_size_locked(efivars, new_var->Attributes,
1594                new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
1595
1596         if (status && status != EFI_UNSUPPORTED) {
1597                 spin_unlock_irq(&efivars->lock);
1598                 return efi_status_to_err(status);
1599         }
1600
1601         /* now *really* create the variable via EFI */
1602         status = efivars->ops->set_variable(new_var->VariableName,
1603                                             &new_var->VendorGuid,
1604                                             new_var->Attributes,
1605                                             new_var->DataSize,
1606                                             new_var->Data);
1607
1608         if (status != EFI_SUCCESS) {
1609                 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1610                         status);
1611                 spin_unlock_irq(&efivars->lock);
1612                 return -EIO;
1613         }
1614         spin_unlock_irq(&efivars->lock);
1615
1616         /* Create the entry in sysfs.  Locking is not required here */
1617         status = efivar_create_sysfs_entry(efivars,
1618                                            utf16_strsize(new_var->VariableName,
1619                                                          1024),
1620                                            new_var->VariableName,
1621                                            &new_var->VendorGuid);
1622         if (status) {
1623                 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
1624         }
1625         return count;
1626 }
1627
1628 static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
1629                              struct bin_attribute *bin_attr,
1630                              char *buf, loff_t pos, size_t count)
1631 {
1632         struct efi_variable *del_var = (struct efi_variable *)buf;
1633         struct efivars *efivars = bin_attr->private;
1634         struct efivar_entry *search_efivar, *n;
1635         unsigned long strsize1, strsize2;
1636         efi_status_t status = EFI_NOT_FOUND;
1637         int found = 0;
1638
1639         if (!capable(CAP_SYS_ADMIN))
1640                 return -EACCES;
1641
1642         spin_lock_irq(&efivars->lock);
1643
1644         /*
1645          * Does this variable already exist?
1646          */
1647         list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
1648                 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1649                 strsize2 = utf16_strsize(del_var->VariableName, 1024);
1650                 if (strsize1 == strsize2 &&
1651                         !memcmp(&(search_efivar->var.VariableName),
1652                                 del_var->VariableName, strsize1) &&
1653                         !efi_guidcmp(search_efivar->var.VendorGuid,
1654                                 del_var->VendorGuid)) {
1655                         found = 1;
1656                         break;
1657                 }
1658         }
1659         if (!found) {
1660                 spin_unlock_irq(&efivars->lock);
1661                 return -EINVAL;
1662         }
1663         /* force the Attributes/DataSize to 0 to ensure deletion */
1664         del_var->Attributes = 0;
1665         del_var->DataSize = 0;
1666
1667         status = efivars->ops->set_variable(del_var->VariableName,
1668                                             &del_var->VendorGuid,
1669                                             del_var->Attributes,
1670                                             del_var->DataSize,
1671                                             del_var->Data);
1672
1673         if (status != EFI_SUCCESS) {
1674                 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1675                         status);
1676                 spin_unlock_irq(&efivars->lock);
1677                 return -EIO;
1678         }
1679         list_del(&search_efivar->list);
1680         /* We need to release this lock before unregistering. */
1681         spin_unlock_irq(&efivars->lock);
1682         efivar_unregister(search_efivar);
1683
1684         /* It's dead Jim.... */
1685         return count;
1686 }
1687
1688 static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor)
1689 {
1690         struct efivar_entry *entry, *n;
1691         struct efivars *efivars = &__efivars;
1692         unsigned long strsize1, strsize2;
1693         bool found = false;
1694
1695         strsize1 = utf16_strsize(variable_name, 1024);
1696         list_for_each_entry_safe(entry, n, &efivars->list, list) {
1697                 strsize2 = utf16_strsize(entry->var.VariableName, 1024);
1698                 if (strsize1 == strsize2 &&
1699                         !memcmp(variable_name, &(entry->var.VariableName),
1700                                 strsize2) &&
1701                         !efi_guidcmp(entry->var.VendorGuid,
1702                                 *vendor)) {
1703                         found = true;
1704                         break;
1705                 }
1706         }
1707         return found;
1708 }
1709
1710 /*
1711  * Returns the size of variable_name, in bytes, including the
1712  * terminating NULL character, or variable_name_size if no NULL
1713  * character is found among the first variable_name_size bytes.
1714  */
1715 static unsigned long var_name_strnsize(efi_char16_t *variable_name,
1716                                        unsigned long variable_name_size)
1717 {
1718         unsigned long len;
1719         efi_char16_t c;
1720
1721         /*
1722          * The variable name is, by definition, a NULL-terminated
1723          * string, so make absolutely sure that variable_name_size is
1724          * the value we expect it to be. If not, return the real size.
1725          */
1726         for (len = 2; len <= variable_name_size; len += sizeof(c)) {
1727                 c = variable_name[(len / sizeof(c)) - 1];
1728                 if (!c)
1729                         break;
1730         }
1731
1732         return min(len, variable_name_size);
1733 }
1734
1735 static void efivar_update_sysfs_entries(struct work_struct *work)
1736 {
1737         struct efivars *efivars = &__efivars;
1738         efi_guid_t vendor;
1739         efi_char16_t *variable_name;
1740         unsigned long variable_name_size = 1024;
1741         efi_status_t status = EFI_NOT_FOUND;
1742         bool found;
1743
1744         /* Add new sysfs entries */
1745         while (1) {
1746                 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
1747                 if (!variable_name) {
1748                         pr_err("efivars: Memory allocation failed.\n");
1749                         return;
1750                 }
1751
1752                 spin_lock_irq(&efivars->lock);
1753                 found = false;
1754                 while (1) {
1755                         variable_name_size = 1024;
1756                         status = efivars->ops->get_next_variable(
1757                                                         &variable_name_size,
1758                                                         variable_name,
1759                                                         &vendor);
1760                         if (status != EFI_SUCCESS) {
1761                                 break;
1762                         } else {
1763                                 if (!variable_is_present(variable_name,
1764                                     &vendor)) {
1765                                         found = true;
1766                                         break;
1767                                 }
1768                         }
1769                 }
1770                 spin_unlock_irq(&efivars->lock);
1771
1772                 if (!found) {
1773                         kfree(variable_name);
1774                         break;
1775                 } else {
1776                         variable_name_size = var_name_strnsize(variable_name,
1777                                                                variable_name_size);
1778                         efivar_create_sysfs_entry(efivars,
1779                                                   variable_name_size,
1780                                                   variable_name, &vendor);
1781                 }
1782         }
1783 }
1784
1785 /*
1786  * Let's not leave out systab information that snuck into
1787  * the efivars driver
1788  */
1789 static ssize_t systab_show(struct kobject *kobj,
1790                            struct kobj_attribute *attr, char *buf)
1791 {
1792         char *str = buf;
1793
1794         if (!kobj || !buf)
1795                 return -EINVAL;
1796
1797         if (efi.mps != EFI_INVALID_TABLE_ADDR)
1798                 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
1799         if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
1800                 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
1801         if (efi.acpi != EFI_INVALID_TABLE_ADDR)
1802                 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
1803         if (efi.smbios != EFI_INVALID_TABLE_ADDR)
1804                 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
1805         if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
1806                 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
1807         if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
1808                 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
1809         if (efi.uga != EFI_INVALID_TABLE_ADDR)
1810                 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
1811
1812         return str - buf;
1813 }
1814
1815 static struct kobj_attribute efi_attr_systab =
1816                         __ATTR(systab, 0400, systab_show, NULL);
1817
1818 static struct attribute *efi_subsys_attrs[] = {
1819         &efi_attr_systab.attr,
1820         NULL,   /* maybe more in the future? */
1821 };
1822
1823 static struct attribute_group efi_subsys_attr_group = {
1824         .attrs = efi_subsys_attrs,
1825 };
1826
1827 static struct kobject *efi_kobj;
1828
1829 /*
1830  * efivar_create_sysfs_entry()
1831  * Requires:
1832  *    variable_name_size = number of bytes required to hold
1833  *                         variable_name (not counting the NULL
1834  *                         character at the end.
1835  *    efivars->lock is not held on entry or exit.
1836  * Returns 1 on failure, 0 on success
1837  */
1838 static int
1839 efivar_create_sysfs_entry(struct efivars *efivars,
1840                           unsigned long variable_name_size,
1841                           efi_char16_t *variable_name,
1842                           efi_guid_t *vendor_guid)
1843 {
1844         int i, short_name_size;
1845         char *short_name;
1846         struct efivar_entry *new_efivar;
1847
1848         /*
1849          * Length of the variable bytes in ASCII, plus the '-' separator,
1850          * plus the GUID, plus trailing NUL
1851          */
1852         short_name_size = variable_name_size / sizeof(efi_char16_t)
1853                                 + 1 + GUID_LEN + 1;
1854
1855         short_name = kzalloc(short_name_size, GFP_KERNEL);
1856         new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
1857
1858         if (!short_name || !new_efivar)  {
1859                 kfree(short_name);
1860                 kfree(new_efivar);
1861                 return 1;
1862         }
1863
1864         new_efivar->efivars = efivars;
1865         memcpy(new_efivar->var.VariableName, variable_name,
1866                 variable_name_size);
1867         memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
1868
1869         /* Convert Unicode to normal chars (assume top bits are 0),
1870            ala UTF-8 */
1871         for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
1872                 short_name[i] = variable_name[i] & 0xFF;
1873         }
1874         /* This is ugly, but necessary to separate one vendor's
1875            private variables from another's.         */
1876
1877         *(short_name + strlen(short_name)) = '-';
1878         efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
1879
1880         new_efivar->kobj.kset = efivars->kset;
1881         i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
1882                                  "%s", short_name);
1883         if (i) {
1884                 kfree(short_name);
1885                 kfree(new_efivar);
1886                 return 1;
1887         }
1888
1889         kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
1890         kfree(short_name);
1891         short_name = NULL;
1892
1893         spin_lock_irq(&efivars->lock);
1894         list_add(&new_efivar->list, &efivars->list);
1895         spin_unlock_irq(&efivars->lock);
1896
1897         return 0;
1898 }
1899
1900 static int
1901 create_efivars_bin_attributes(struct efivars *efivars)
1902 {
1903         struct bin_attribute *attr;
1904         int error;
1905
1906         /* new_var */
1907         attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1908         if (!attr)
1909                 return -ENOMEM;
1910
1911         attr->attr.name = "new_var";
1912         attr->attr.mode = 0200;
1913         attr->write = efivar_create;
1914         attr->private = efivars;
1915         efivars->new_var = attr;
1916
1917         /* del_var */
1918         attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1919         if (!attr) {
1920                 error = -ENOMEM;
1921                 goto out_free;
1922         }
1923         attr->attr.name = "del_var";
1924         attr->attr.mode = 0200;
1925         attr->write = efivar_delete;
1926         attr->private = efivars;
1927         efivars->del_var = attr;
1928
1929         sysfs_bin_attr_init(efivars->new_var);
1930         sysfs_bin_attr_init(efivars->del_var);
1931
1932         /* Register */
1933         error = sysfs_create_bin_file(&efivars->kset->kobj,
1934                                       efivars->new_var);
1935         if (error) {
1936                 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
1937                         " due to error %d\n", error);
1938                 goto out_free;
1939         }
1940         error = sysfs_create_bin_file(&efivars->kset->kobj,
1941                                       efivars->del_var);
1942         if (error) {
1943                 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
1944                         " due to error %d\n", error);
1945                 sysfs_remove_bin_file(&efivars->kset->kobj,
1946                                       efivars->new_var);
1947                 goto out_free;
1948         }
1949
1950         return 0;
1951 out_free:
1952         kfree(efivars->del_var);
1953         efivars->del_var = NULL;
1954         kfree(efivars->new_var);
1955         efivars->new_var = NULL;
1956         return error;
1957 }
1958
1959 void unregister_efivars(struct efivars *efivars)
1960 {
1961         struct efivar_entry *entry, *n;
1962
1963         list_for_each_entry_safe(entry, n, &efivars->list, list) {
1964                 spin_lock_irq(&efivars->lock);
1965                 list_del(&entry->list);
1966                 spin_unlock_irq(&efivars->lock);
1967                 efivar_unregister(entry);
1968         }
1969         if (efivars->new_var)
1970                 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
1971         if (efivars->del_var)
1972                 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
1973         kfree(efivars->new_var);
1974         kfree(efivars->del_var);
1975         kobject_put(efivars->kobject);
1976         kset_unregister(efivars->kset);
1977 }
1978 EXPORT_SYMBOL_GPL(unregister_efivars);
1979
1980 /*
1981  * Print a warning when duplicate EFI variables are encountered and
1982  * disable the sysfs workqueue since the firmware is buggy.
1983  */
1984 static void dup_variable_bug(efi_char16_t *s16, efi_guid_t *vendor_guid,
1985                              unsigned long len16)
1986 {
1987         size_t i, len8 = len16 / sizeof(efi_char16_t);
1988         char *s8;
1989
1990         /*
1991          * Disable the workqueue since the algorithm it uses for
1992          * detecting new variables won't work with this buggy
1993          * implementation of GetNextVariableName().
1994          */
1995         efivar_wq_enabled = false;
1996
1997         s8 = kzalloc(len8, GFP_KERNEL);
1998         if (!s8)
1999                 return;
2000
2001         for (i = 0; i < len8; i++)
2002                 s8[i] = s16[i];
2003
2004         printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
2005                s8, vendor_guid);
2006         kfree(s8);
2007 }
2008
2009 int register_efivars(struct efivars *efivars,
2010                      const struct efivar_operations *ops,
2011                      struct kobject *parent_kobj)
2012 {
2013         efi_status_t status = EFI_NOT_FOUND;
2014         efi_guid_t vendor_guid;
2015         efi_char16_t *variable_name;
2016         unsigned long variable_name_size = 1024;
2017         int error = 0;
2018
2019         variable_name = kzalloc(variable_name_size, GFP_KERNEL);
2020         if (!variable_name) {
2021                 printk(KERN_ERR "efivars: Memory allocation failed.\n");
2022                 return -ENOMEM;
2023         }
2024
2025         spin_lock_init(&efivars->lock);
2026         INIT_LIST_HEAD(&efivars->list);
2027         efivars->ops = ops;
2028
2029         efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
2030         if (!efivars->kset) {
2031                 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
2032                 error = -ENOMEM;
2033                 goto out;
2034         }
2035
2036         efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
2037         if (!efivars->kobject) {
2038                 pr_err("efivars: Subsystem registration failed.\n");
2039                 error = -ENOMEM;
2040                 kset_unregister(efivars->kset);
2041                 goto out;
2042         }
2043
2044         /*
2045          * Per EFI spec, the maximum storage allocated for both
2046          * the variable name and variable data is 1024 bytes.
2047          */
2048
2049         do {
2050                 variable_name_size = 1024;
2051
2052                 status = ops->get_next_variable(&variable_name_size,
2053                                                 variable_name,
2054                                                 &vendor_guid);
2055                 switch (status) {
2056                 case EFI_SUCCESS:
2057                         variable_name_size = var_name_strnsize(variable_name,
2058                                                                variable_name_size);
2059
2060                         /*
2061                          * Some firmware implementations return the
2062                          * same variable name on multiple calls to
2063                          * get_next_variable(). Terminate the loop
2064                          * immediately as there is no guarantee that
2065                          * we'll ever see a different variable name,
2066                          * and may end up looping here forever.
2067                          */
2068                         if (variable_is_present(variable_name, &vendor_guid)) {
2069                                 dup_variable_bug(variable_name, &vendor_guid,
2070                                                  variable_name_size);
2071                                 status = EFI_NOT_FOUND;
2072                                 break;
2073                         }
2074
2075                         efivar_create_sysfs_entry(efivars,
2076                                                   variable_name_size,
2077                                                   variable_name,
2078                                                   &vendor_guid);
2079                         break;
2080                 case EFI_NOT_FOUND:
2081                         break;
2082                 default:
2083                         printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
2084                                 status);
2085                         status = EFI_NOT_FOUND;
2086                         break;
2087                 }
2088         } while (status != EFI_NOT_FOUND);
2089
2090         error = create_efivars_bin_attributes(efivars);
2091         if (error)
2092                 unregister_efivars(efivars);
2093
2094         if (!efivars_pstore_disable)
2095                 efivar_pstore_register(efivars);
2096
2097         register_filesystem(&efivarfs_type);
2098
2099 out:
2100         kfree(variable_name);
2101
2102         return error;
2103 }
2104 EXPORT_SYMBOL_GPL(register_efivars);
2105
2106 /*
2107  * For now we register the efi subsystem with the firmware subsystem
2108  * and the vars subsystem with the efi subsystem.  In the future, it
2109  * might make sense to split off the efi subsystem into its own
2110  * driver, but for now only efivars will register with it, so just
2111  * include it here.
2112  */
2113
2114 static int __init
2115 efivars_init(void)
2116 {
2117         int error = 0;
2118
2119         printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
2120                EFIVARS_DATE);
2121
2122         if (!efi_enabled(EFI_RUNTIME_SERVICES))
2123                 return 0;
2124
2125         /* For now we'll register the efi directory at /sys/firmware/efi */
2126         efi_kobj = kobject_create_and_add("efi", firmware_kobj);
2127         if (!efi_kobj) {
2128                 printk(KERN_ERR "efivars: Firmware registration failed.\n");
2129                 return -ENOMEM;
2130         }
2131
2132         ops.get_variable = efi.get_variable;
2133         ops.set_variable = efi.set_variable;
2134         ops.get_next_variable = efi.get_next_variable;
2135         ops.query_variable_info = efi.query_variable_info;
2136
2137         error = register_efivars(&__efivars, &ops, efi_kobj);
2138         if (error)
2139                 goto err_put;
2140
2141         /* Don't forget the systab entry */
2142         error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
2143         if (error) {
2144                 printk(KERN_ERR
2145                        "efivars: Sysfs attribute export failed with error %d.\n",
2146                        error);
2147                 goto err_unregister;
2148         }
2149
2150         return 0;
2151
2152 err_unregister:
2153         unregister_efivars(&__efivars);
2154 err_put:
2155         kobject_put(efi_kobj);
2156         return error;
2157 }
2158
2159 static void __exit
2160 efivars_exit(void)
2161 {
2162         cancel_work_sync(&efivar_work);
2163
2164         if (efi_enabled(EFI_RUNTIME_SERVICES)) {
2165                 unregister_efivars(&__efivars);
2166                 kobject_put(efi_kobj);
2167         }
2168 }
2169
2170 module_init(efivars_init);
2171 module_exit(efivars_exit);
2172