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