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