efi: be more paranoid about available space when creating variables
[pandora-kernel.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 <asm/uaccess.h>
84
85 #define EFIVARS_VERSION "0.08"
86 #define EFIVARS_DATE "2004-May-17"
87
88 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
89 MODULE_DESCRIPTION("sysfs interface to EFI Variables");
90 MODULE_LICENSE("GPL");
91 MODULE_VERSION(EFIVARS_VERSION);
92
93 #define DUMP_NAME_LEN 52
94
95 /*
96  * The maximum size of VariableName + Data = 1024
97  * Therefore, it's reasonable to save that much
98  * space in each part of the structure,
99  * and we use a page for reading/writing.
100  */
101
102 struct efi_variable {
103         efi_char16_t  VariableName[1024/sizeof(efi_char16_t)];
104         efi_guid_t    VendorGuid;
105         unsigned long DataSize;
106         __u8          Data[1024];
107         efi_status_t  Status;
108         __u32         Attributes;
109 } __attribute__((packed));
110
111
112 struct efivar_entry {
113         struct efivars *efivars;
114         struct efi_variable var;
115         struct list_head list;
116         struct kobject kobj;
117 };
118
119 struct efivar_attribute {
120         struct attribute attr;
121         ssize_t (*show) (struct efivar_entry *entry, char *buf);
122         ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
123 };
124
125 #define PSTORE_EFI_ATTRIBUTES \
126         (EFI_VARIABLE_NON_VOLATILE | \
127          EFI_VARIABLE_BOOTSERVICE_ACCESS | \
128          EFI_VARIABLE_RUNTIME_ACCESS)
129
130 #define EFIVAR_ATTR(_name, _mode, _show, _store) \
131 struct efivar_attribute efivar_attr_##_name = { \
132         .attr = {.name = __stringify(_name), .mode = _mode}, \
133         .show = _show, \
134         .store = _store, \
135 };
136
137 #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
138 #define to_efivar_entry(obj)  container_of(obj, struct efivar_entry, kobj)
139
140 /*
141  * Prototype for sysfs creation function
142  */
143 static int
144 efivar_create_sysfs_entry(struct efivars *efivars,
145                           unsigned long variable_name_size,
146                           efi_char16_t *variable_name,
147                           efi_guid_t *vendor_guid);
148
149 /* Return the number of unicode characters in data */
150 static unsigned long
151 utf16_strnlen(efi_char16_t *s, size_t maxlength)
152 {
153         unsigned long length = 0;
154
155         while (*s++ != 0 && length < maxlength)
156                 length++;
157         return length;
158 }
159
160 static inline unsigned long
161 utf16_strlen(efi_char16_t *s)
162 {
163         return utf16_strnlen(s, ~0UL);
164 }
165
166 /*
167  * Return the number of bytes is the length of this string
168  * Note: this is NOT the same as the number of unicode characters
169  */
170 static inline unsigned long
171 utf16_strsize(efi_char16_t *data, unsigned long maxlength)
172 {
173         return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
174 }
175
176 static inline int
177 utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
178 {
179         while (1) {
180                 if (len == 0)
181                         return 0;
182                 if (*a < *b)
183                         return -1;
184                 if (*a > *b)
185                         return 1;
186                 if (*a == 0) /* implies *b == 0 */
187                         return 0;
188                 a++;
189                 b++;
190                 len--;
191         }
192 }
193
194 static bool
195 validate_device_path(struct efi_variable *var, int match, u8 *buffer,
196                      unsigned long len)
197 {
198         struct efi_generic_dev_path *node;
199         int offset = 0;
200
201         node = (struct efi_generic_dev_path *)buffer;
202
203         if (len < sizeof(*node))
204                 return false;
205
206         while (offset <= len - sizeof(*node) &&
207                node->length >= sizeof(*node) &&
208                 node->length <= len - offset) {
209                 offset += node->length;
210
211                 if ((node->type == EFI_DEV_END_PATH ||
212                      node->type == EFI_DEV_END_PATH2) &&
213                     node->sub_type == EFI_DEV_END_ENTIRE)
214                         return true;
215
216                 node = (struct efi_generic_dev_path *)(buffer + offset);
217         }
218
219         /*
220          * If we're here then either node->length pointed past the end
221          * of the buffer or we reached the end of the buffer without
222          * finding a device path end node.
223          */
224         return false;
225 }
226
227 static bool
228 validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
229                     unsigned long len)
230 {
231         /* An array of 16-bit integers */
232         if ((len % 2) != 0)
233                 return false;
234
235         return true;
236 }
237
238 static bool
239 validate_load_option(struct efi_variable *var, int match, u8 *buffer,
240                      unsigned long len)
241 {
242         u16 filepathlength;
243         int i, desclength = 0, namelen;
244
245         namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
246
247         /* Either "Boot" or "Driver" followed by four digits of hex */
248         for (i = match; i < match+4; i++) {
249                 if (var->VariableName[i] > 127 ||
250                     hex_to_bin(var->VariableName[i] & 0xff) < 0)
251                         return true;
252         }
253
254         /* Reject it if there's 4 digits of hex and then further content */
255         if (namelen > match + 4)
256                 return false;
257
258         /* A valid entry must be at least 8 bytes */
259         if (len < 8)
260                 return false;
261
262         filepathlength = buffer[4] | buffer[5] << 8;
263
264         /*
265          * There's no stored length for the description, so it has to be
266          * found by hand
267          */
268         desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
269
270         /* Each boot entry must have a descriptor */
271         if (!desclength)
272                 return false;
273
274         /*
275          * If the sum of the length of the description, the claimed filepath
276          * length and the original header are greater than the length of the
277          * variable, it's malformed
278          */
279         if ((desclength + filepathlength + 6) > len)
280                 return false;
281
282         /*
283          * And, finally, check the filepath
284          */
285         return validate_device_path(var, match, buffer + desclength + 6,
286                                     filepathlength);
287 }
288
289 static bool
290 validate_uint16(struct efi_variable *var, int match, u8 *buffer,
291                 unsigned long len)
292 {
293         /* A single 16-bit integer */
294         if (len != 2)
295                 return false;
296
297         return true;
298 }
299
300 static bool
301 validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
302                       unsigned long len)
303 {
304         int i;
305
306         for (i = 0; i < len; i++) {
307                 if (buffer[i] > 127)
308                         return false;
309
310                 if (buffer[i] == 0)
311                         return true;
312         }
313
314         return false;
315 }
316
317 struct variable_validate {
318         char *name;
319         bool (*validate)(struct efi_variable *var, int match, u8 *data,
320                          unsigned long len);
321 };
322
323 static const struct variable_validate variable_validate[] = {
324         { "BootNext", validate_uint16 },
325         { "BootOrder", validate_boot_order },
326         { "DriverOrder", validate_boot_order },
327         { "Boot*", validate_load_option },
328         { "Driver*", validate_load_option },
329         { "ConIn", validate_device_path },
330         { "ConInDev", validate_device_path },
331         { "ConOut", validate_device_path },
332         { "ConOutDev", validate_device_path },
333         { "ErrOut", validate_device_path },
334         { "ErrOutDev", validate_device_path },
335         { "Timeout", validate_uint16 },
336         { "Lang", validate_ascii_string },
337         { "PlatformLang", validate_ascii_string },
338         { "", NULL },
339 };
340
341 static bool
342 validate_var(struct efi_variable *var, u8 *data, unsigned long len)
343 {
344         int i;
345         u16 *unicode_name = var->VariableName;
346
347         for (i = 0; variable_validate[i].validate != NULL; i++) {
348                 const char *name = variable_validate[i].name;
349                 int match;
350
351                 for (match = 0; ; match++) {
352                         char c = name[match];
353                         u16 u = unicode_name[match];
354
355                         /* All special variables are plain ascii */
356                         if (u > 127)
357                                 return true;
358
359                         /* Wildcard in the matching name means we've matched */
360                         if (c == '*')
361                                 return variable_validate[i].validate(var,
362                                                              match, data, len);
363
364                         /* Case sensitive match */
365                         if (c != u)
366                                 break;
367
368                         /* Reached the end of the string while matching */
369                         if (!c)
370                                 return variable_validate[i].validate(var,
371                                                              match, data, len);
372                 }
373         }
374
375         return true;
376 }
377
378 static efi_status_t
379 get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
380 {
381         efi_status_t status;
382
383         var->DataSize = 1024;
384         status = efivars->ops->get_variable(var->VariableName,
385                                             &var->VendorGuid,
386                                             &var->Attributes,
387                                             &var->DataSize,
388                                             var->Data);
389         return status;
390 }
391
392 static efi_status_t
393 get_var_data(struct efivars *efivars, struct efi_variable *var)
394 {
395         efi_status_t status;
396         unsigned long flags;
397
398         spin_lock_irqsave(&efivars->lock, flags);
399         status = get_var_data_locked(efivars, var);
400         spin_unlock_irqrestore(&efivars->lock, flags);
401
402         if (status != EFI_SUCCESS) {
403                 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
404                         status);
405         }
406         return status;
407 }
408
409 static efi_status_t
410 check_var_size_locked(struct efivars *efivars, u32 attributes,
411                         unsigned long size)
412 {
413         u64 storage_size, remaining_size, max_size;
414         efi_status_t status;
415         const struct efivar_operations *fops = efivars->ops;
416
417         if (!efivars->ops->query_variable_info)
418                 return EFI_UNSUPPORTED;
419
420         status = fops->query_variable_info(attributes, &storage_size,
421                                            &remaining_size, &max_size);
422
423         if (status != EFI_SUCCESS)
424                 return status;
425
426         if (!storage_size || size > remaining_size || size > max_size ||
427             (remaining_size - size) < (storage_size / 2))
428                 return EFI_OUT_OF_RESOURCES;
429
430         return status;
431 }
432
433 static ssize_t
434 efivar_guid_read(struct efivar_entry *entry, char *buf)
435 {
436         struct efi_variable *var = &entry->var;
437         char *str = buf;
438
439         if (!entry || !buf)
440                 return 0;
441
442         efi_guid_unparse(&var->VendorGuid, str);
443         str += strlen(str);
444         str += sprintf(str, "\n");
445
446         return str - buf;
447 }
448
449 static ssize_t
450 efivar_attr_read(struct efivar_entry *entry, char *buf)
451 {
452         struct efi_variable *var = &entry->var;
453         char *str = buf;
454         efi_status_t status;
455
456         if (!entry || !buf)
457                 return -EINVAL;
458
459         status = get_var_data(entry->efivars, var);
460         if (status != EFI_SUCCESS)
461                 return -EIO;
462
463         if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
464                 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
465         if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
466                 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
467         if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
468                 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
469         if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
470                 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
471         if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
472                 str += sprintf(str,
473                         "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
474         if (var->Attributes &
475                         EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
476                 str += sprintf(str,
477                         "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
478         if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
479                 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
480         return str - buf;
481 }
482
483 static ssize_t
484 efivar_size_read(struct efivar_entry *entry, char *buf)
485 {
486         struct efi_variable *var = &entry->var;
487         char *str = buf;
488         efi_status_t status;
489
490         if (!entry || !buf)
491                 return -EINVAL;
492
493         status = get_var_data(entry->efivars, var);
494         if (status != EFI_SUCCESS)
495                 return -EIO;
496
497         str += sprintf(str, "0x%lx\n", var->DataSize);
498         return str - buf;
499 }
500
501 static ssize_t
502 efivar_data_read(struct efivar_entry *entry, char *buf)
503 {
504         struct efi_variable *var = &entry->var;
505         efi_status_t status;
506
507         if (!entry || !buf)
508                 return -EINVAL;
509
510         status = get_var_data(entry->efivars, var);
511         if (status != EFI_SUCCESS)
512                 return -EIO;
513
514         memcpy(buf, var->Data, var->DataSize);
515         return var->DataSize;
516 }
517 /*
518  * We allow each variable to be edited via rewriting the
519  * entire efi variable structure.
520  */
521 static ssize_t
522 efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
523 {
524         struct efi_variable *new_var, *var = &entry->var;
525         struct efivars *efivars = entry->efivars;
526         efi_status_t status = EFI_NOT_FOUND;
527
528         if (count != sizeof(struct efi_variable))
529                 return -EINVAL;
530
531         new_var = (struct efi_variable *)buf;
532         /*
533          * If only updating the variable data, then the name
534          * and guid should remain the same
535          */
536         if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
537                 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
538                 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
539                 return -EINVAL;
540         }
541
542         if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
543                 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
544                 return -EINVAL;
545         }
546
547         if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
548             validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
549                 printk(KERN_ERR "efivars: Malformed variable content\n");
550                 return -EINVAL;
551         }
552
553         spin_lock_irq(&efivars->lock);
554
555         status = check_var_size_locked(efivars, new_var->Attributes,
556                new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
557
558         if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
559                 status = efivars->ops->set_variable(new_var->VariableName,
560                                                     &new_var->VendorGuid,
561                                                     new_var->Attributes,
562                                                     new_var->DataSize,
563                                                     new_var->Data);
564
565         spin_unlock_irq(&efivars->lock);
566
567         if (status != EFI_SUCCESS) {
568                 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
569                         status);
570                 return -EIO;
571         }
572
573         memcpy(&entry->var, new_var, count);
574         return count;
575 }
576
577 static ssize_t
578 efivar_show_raw(struct efivar_entry *entry, char *buf)
579 {
580         struct efi_variable *var = &entry->var;
581         efi_status_t status;
582
583         if (!entry || !buf)
584                 return 0;
585
586         status = get_var_data(entry->efivars, var);
587         if (status != EFI_SUCCESS)
588                 return -EIO;
589
590         memcpy(buf, var, sizeof(*var));
591         return sizeof(*var);
592 }
593
594 /*
595  * Generic read/write functions that call the specific functions of
596  * the attributes...
597  */
598 static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
599                                 char *buf)
600 {
601         struct efivar_entry *var = to_efivar_entry(kobj);
602         struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
603         ssize_t ret = -EIO;
604
605         if (!capable(CAP_SYS_ADMIN))
606                 return -EACCES;
607
608         if (efivar_attr->show) {
609                 ret = efivar_attr->show(var, buf);
610         }
611         return ret;
612 }
613
614 static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
615                                 const char *buf, size_t count)
616 {
617         struct efivar_entry *var = to_efivar_entry(kobj);
618         struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
619         ssize_t ret = -EIO;
620
621         if (!capable(CAP_SYS_ADMIN))
622                 return -EACCES;
623
624         if (efivar_attr->store)
625                 ret = efivar_attr->store(var, buf, count);
626
627         return ret;
628 }
629
630 static const struct sysfs_ops efivar_attr_ops = {
631         .show = efivar_attr_show,
632         .store = efivar_attr_store,
633 };
634
635 static void efivar_release(struct kobject *kobj)
636 {
637         struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
638         kfree(var);
639 }
640
641 static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
642 static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
643 static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
644 static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
645 static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
646
647 static struct attribute *def_attrs[] = {
648         &efivar_attr_guid.attr,
649         &efivar_attr_size.attr,
650         &efivar_attr_attributes.attr,
651         &efivar_attr_data.attr,
652         &efivar_attr_raw_var.attr,
653         NULL,
654 };
655
656 static struct kobj_type efivar_ktype = {
657         .release = efivar_release,
658         .sysfs_ops = &efivar_attr_ops,
659         .default_attrs = def_attrs,
660 };
661
662 static struct pstore_info efi_pstore_info;
663
664 static inline void
665 efivar_unregister(struct efivar_entry *var)
666 {
667         kobject_put(&var->kobj);
668 }
669
670 static int efi_status_to_err(efi_status_t status)
671 {
672         int err;
673
674         switch (status) {
675         case EFI_INVALID_PARAMETER:
676                 err = -EINVAL;
677                 break;
678         case EFI_OUT_OF_RESOURCES:
679                 err = -ENOSPC;
680                 break;
681         case EFI_DEVICE_ERROR:
682                 err = -EIO;
683                 break;
684         case EFI_WRITE_PROTECTED:
685                 err = -EROFS;
686                 break;
687         case EFI_SECURITY_VIOLATION:
688                 err = -EACCES;
689                 break;
690         case EFI_NOT_FOUND:
691                 err = -ENOENT;
692                 break;
693         default:
694                 err = -EINVAL;
695         }
696
697         return err;
698 }
699
700 #ifdef CONFIG_PSTORE
701
702 static int efi_pstore_open(struct pstore_info *psi)
703 {
704         struct efivars *efivars = psi->data;
705
706         spin_lock_irq(&efivars->lock);
707         efivars->walk_entry = list_first_entry(&efivars->list,
708                                                struct efivar_entry, list);
709         return 0;
710 }
711
712 static int efi_pstore_close(struct pstore_info *psi)
713 {
714         struct efivars *efivars = psi->data;
715
716         spin_unlock_irq(&efivars->lock);
717         return 0;
718 }
719
720 static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
721                                struct timespec *timespec,
722                                char **buf, struct pstore_info *psi)
723 {
724         efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
725         struct efivars *efivars = psi->data;
726         char name[DUMP_NAME_LEN];
727         int i;
728         unsigned int part, size;
729         unsigned long time;
730
731         while (&efivars->walk_entry->list != &efivars->list) {
732                 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
733                                  vendor)) {
734                         for (i = 0; i < DUMP_NAME_LEN; i++) {
735                                 name[i] = efivars->walk_entry->var.VariableName[i];
736                         }
737                         if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
738                                 *id = part;
739                                 timespec->tv_sec = time;
740                                 timespec->tv_nsec = 0;
741                                 get_var_data_locked(efivars, &efivars->walk_entry->var);
742                                 size = efivars->walk_entry->var.DataSize;
743                                 *buf = kmalloc(size, GFP_KERNEL);
744                                 if (*buf == NULL)
745                                         return -ENOMEM;
746                                 memcpy(*buf, efivars->walk_entry->var.Data,
747                                        size);
748                                 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
749                                                    struct efivar_entry, list);
750                                 return size;
751                         }
752                 }
753                 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
754                                                  struct efivar_entry, list);
755         }
756         return 0;
757 }
758
759 static int efi_pstore_write(enum pstore_type_id type, u64 *id,
760                 unsigned int part, size_t size, struct pstore_info *psi)
761 {
762         char name[DUMP_NAME_LEN];
763         char stub_name[DUMP_NAME_LEN];
764         efi_char16_t efi_name[DUMP_NAME_LEN];
765         efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
766         struct efivars *efivars = psi->data;
767         struct efivar_entry *entry, *found = NULL;
768         int i, ret = 0;
769         efi_status_t status = EFI_NOT_FOUND;
770         unsigned long flags;
771
772         sprintf(stub_name, "dump-type%u-%u-", type, part);
773         sprintf(name, "%s%lu", stub_name, get_seconds());
774
775         spin_lock_irqsave(&efivars->lock, flags);
776
777         /*
778          * Check if there is a space enough to log.
779          * size: a size of logging data
780          * DUMP_NAME_LEN * 2: a maximum size of variable name
781          */
782
783         status = check_var_size_locked(efivars, PSTORE_EFI_ATTRIBUTES,
784                                          size + DUMP_NAME_LEN * 2);
785
786         if (status) {
787                 spin_unlock_irqrestore(&efivars->lock, flags);
788                 *id = part;
789                 return -ENOSPC;
790         }
791
792         for (i = 0; i < DUMP_NAME_LEN; i++)
793                 efi_name[i] = stub_name[i];
794
795         /*
796          * Clean up any entries with the same name
797          */
798
799         list_for_each_entry(entry, &efivars->list, list) {
800                 get_var_data_locked(efivars, &entry->var);
801
802                 if (efi_guidcmp(entry->var.VendorGuid, vendor))
803                         continue;
804                 if (utf16_strncmp(entry->var.VariableName, efi_name,
805                                   utf16_strlen(efi_name)))
806                         continue;
807                 /* Needs to be a prefix */
808                 if (entry->var.VariableName[utf16_strlen(efi_name)] == 0)
809                         continue;
810
811                 /* found */
812                 found = entry;
813                 efivars->ops->set_variable(entry->var.VariableName,
814                                            &entry->var.VendorGuid,
815                                            PSTORE_EFI_ATTRIBUTES,
816                                            0, NULL);
817         }
818
819         if (found)
820                 list_del(&found->list);
821
822         for (i = 0; i < DUMP_NAME_LEN; i++)
823                 efi_name[i] = name[i];
824
825         efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
826                                    size, psi->buf);
827
828         spin_unlock_irqrestore(&efivars->lock, flags);
829
830         if (found)
831                 efivar_unregister(found);
832
833         if (size)
834                 ret = efivar_create_sysfs_entry(efivars,
835                                           utf16_strsize(efi_name,
836                                                         DUMP_NAME_LEN * 2),
837                                           efi_name, &vendor);
838
839         *id = part;
840         return ret;
841 };
842
843 static int efi_pstore_erase(enum pstore_type_id type, u64 id,
844                             struct pstore_info *psi)
845 {
846         efi_pstore_write(type, &id, (unsigned int)id, 0, psi);
847
848         return 0;
849 }
850 #else
851 static int efi_pstore_open(struct pstore_info *psi)
852 {
853         return 0;
854 }
855
856 static int efi_pstore_close(struct pstore_info *psi)
857 {
858         return 0;
859 }
860
861 static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
862                                struct timespec *timespec,
863                                char **buf, struct pstore_info *psi)
864 {
865         return -1;
866 }
867
868 static int efi_pstore_write(enum pstore_type_id type, u64 *id,
869                 unsigned int part, size_t size, struct pstore_info *psi)
870 {
871         return 0;
872 }
873
874 static int efi_pstore_erase(enum pstore_type_id type, u64 id,
875                             struct pstore_info *psi)
876 {
877         return 0;
878 }
879 #endif
880
881 static struct pstore_info efi_pstore_info = {
882         .owner          = THIS_MODULE,
883         .name           = "efi",
884         .open           = efi_pstore_open,
885         .close          = efi_pstore_close,
886         .read           = efi_pstore_read,
887         .write          = efi_pstore_write,
888         .erase          = efi_pstore_erase,
889 };
890
891 static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
892                              struct bin_attribute *bin_attr,
893                              char *buf, loff_t pos, size_t count)
894 {
895         struct efi_variable *new_var = (struct efi_variable *)buf;
896         struct efivars *efivars = bin_attr->private;
897         struct efivar_entry *search_efivar, *n;
898         unsigned long strsize1, strsize2;
899         efi_status_t status = EFI_NOT_FOUND;
900         int found = 0;
901
902         if (!capable(CAP_SYS_ADMIN))
903                 return -EACCES;
904
905         if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
906             validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
907                 printk(KERN_ERR "efivars: Malformed variable content\n");
908                 return -EINVAL;
909         }
910
911         spin_lock_irq(&efivars->lock);
912
913         /*
914          * Does this variable already exist?
915          */
916         list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
917                 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
918                 strsize2 = utf16_strsize(new_var->VariableName, 1024);
919                 if (strsize1 == strsize2 &&
920                         !memcmp(&(search_efivar->var.VariableName),
921                                 new_var->VariableName, strsize1) &&
922                         !efi_guidcmp(search_efivar->var.VendorGuid,
923                                 new_var->VendorGuid)) {
924                         found = 1;
925                         break;
926                 }
927         }
928         if (found) {
929                 spin_unlock_irq(&efivars->lock);
930                 return -EINVAL;
931         }
932
933         status = check_var_size_locked(efivars, new_var->Attributes,
934                new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
935
936         if (status && status != EFI_UNSUPPORTED) {
937                 spin_unlock_irq(&efivars->lock);
938                 return efi_status_to_err(status);
939         }
940
941         /* now *really* create the variable via EFI */
942         status = efivars->ops->set_variable(new_var->VariableName,
943                                             &new_var->VendorGuid,
944                                             new_var->Attributes,
945                                             new_var->DataSize,
946                                             new_var->Data);
947
948         if (status != EFI_SUCCESS) {
949                 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
950                         status);
951                 spin_unlock_irq(&efivars->lock);
952                 return -EIO;
953         }
954         spin_unlock_irq(&efivars->lock);
955
956         /* Create the entry in sysfs.  Locking is not required here */
957         status = efivar_create_sysfs_entry(efivars,
958                                            utf16_strsize(new_var->VariableName,
959                                                          1024),
960                                            new_var->VariableName,
961                                            &new_var->VendorGuid);
962         if (status) {
963                 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
964         }
965         return count;
966 }
967
968 static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
969                              struct bin_attribute *bin_attr,
970                              char *buf, loff_t pos, size_t count)
971 {
972         struct efi_variable *del_var = (struct efi_variable *)buf;
973         struct efivars *efivars = bin_attr->private;
974         struct efivar_entry *search_efivar, *n;
975         unsigned long strsize1, strsize2;
976         efi_status_t status = EFI_NOT_FOUND;
977         int found = 0;
978
979         if (!capable(CAP_SYS_ADMIN))
980                 return -EACCES;
981
982         spin_lock_irq(&efivars->lock);
983
984         /*
985          * Does this variable already exist?
986          */
987         list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
988                 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
989                 strsize2 = utf16_strsize(del_var->VariableName, 1024);
990                 if (strsize1 == strsize2 &&
991                         !memcmp(&(search_efivar->var.VariableName),
992                                 del_var->VariableName, strsize1) &&
993                         !efi_guidcmp(search_efivar->var.VendorGuid,
994                                 del_var->VendorGuid)) {
995                         found = 1;
996                         break;
997                 }
998         }
999         if (!found) {
1000                 spin_unlock_irq(&efivars->lock);
1001                 return -EINVAL;
1002         }
1003         /* force the Attributes/DataSize to 0 to ensure deletion */
1004         del_var->Attributes = 0;
1005         del_var->DataSize = 0;
1006
1007         status = efivars->ops->set_variable(del_var->VariableName,
1008                                             &del_var->VendorGuid,
1009                                             del_var->Attributes,
1010                                             del_var->DataSize,
1011                                             del_var->Data);
1012
1013         if (status != EFI_SUCCESS) {
1014                 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1015                         status);
1016                 spin_unlock_irq(&efivars->lock);
1017                 return -EIO;
1018         }
1019         list_del(&search_efivar->list);
1020         /* We need to release this lock before unregistering. */
1021         spin_unlock_irq(&efivars->lock);
1022         efivar_unregister(search_efivar);
1023
1024         /* It's dead Jim.... */
1025         return count;
1026 }
1027
1028 /*
1029  * Let's not leave out systab information that snuck into
1030  * the efivars driver
1031  */
1032 static ssize_t systab_show(struct kobject *kobj,
1033                            struct kobj_attribute *attr, char *buf)
1034 {
1035         char *str = buf;
1036
1037         if (!kobj || !buf)
1038                 return -EINVAL;
1039
1040         if (efi.mps != EFI_INVALID_TABLE_ADDR)
1041                 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
1042         if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
1043                 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
1044         if (efi.acpi != EFI_INVALID_TABLE_ADDR)
1045                 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
1046         if (efi.smbios != EFI_INVALID_TABLE_ADDR)
1047                 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
1048         if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
1049                 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
1050         if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
1051                 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
1052         if (efi.uga != EFI_INVALID_TABLE_ADDR)
1053                 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
1054
1055         return str - buf;
1056 }
1057
1058 static struct kobj_attribute efi_attr_systab =
1059                         __ATTR(systab, 0400, systab_show, NULL);
1060
1061 static struct attribute *efi_subsys_attrs[] = {
1062         &efi_attr_systab.attr,
1063         NULL,   /* maybe more in the future? */
1064 };
1065
1066 static struct attribute_group efi_subsys_attr_group = {
1067         .attrs = efi_subsys_attrs,
1068 };
1069
1070 static struct kobject *efi_kobj;
1071
1072 /*
1073  * efivar_create_sysfs_entry()
1074  * Requires:
1075  *    variable_name_size = number of bytes required to hold
1076  *                         variable_name (not counting the NULL
1077  *                         character at the end.
1078  *    efivars->lock is not held on entry or exit.
1079  * Returns 1 on failure, 0 on success
1080  */
1081 static int
1082 efivar_create_sysfs_entry(struct efivars *efivars,
1083                           unsigned long variable_name_size,
1084                           efi_char16_t *variable_name,
1085                           efi_guid_t *vendor_guid)
1086 {
1087         int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
1088         char *short_name;
1089         struct efivar_entry *new_efivar;
1090
1091         short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
1092         new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
1093
1094         if (!short_name || !new_efivar)  {
1095                 kfree(short_name);
1096                 kfree(new_efivar);
1097                 return 1;
1098         }
1099
1100         new_efivar->efivars = efivars;
1101         memcpy(new_efivar->var.VariableName, variable_name,
1102                 variable_name_size);
1103         memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
1104
1105         /* Convert Unicode to normal chars (assume top bits are 0),
1106            ala UTF-8 */
1107         for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
1108                 short_name[i] = variable_name[i] & 0xFF;
1109         }
1110         /* This is ugly, but necessary to separate one vendor's
1111            private variables from another's.         */
1112
1113         *(short_name + strlen(short_name)) = '-';
1114         efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
1115
1116         new_efivar->kobj.kset = efivars->kset;
1117         i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
1118                                  "%s", short_name);
1119         if (i) {
1120                 kfree(short_name);
1121                 kfree(new_efivar);
1122                 return 1;
1123         }
1124
1125         kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
1126         kfree(short_name);
1127         short_name = NULL;
1128
1129         spin_lock_irq(&efivars->lock);
1130         list_add(&new_efivar->list, &efivars->list);
1131         spin_unlock_irq(&efivars->lock);
1132
1133         return 0;
1134 }
1135
1136 static int
1137 create_efivars_bin_attributes(struct efivars *efivars)
1138 {
1139         struct bin_attribute *attr;
1140         int error;
1141
1142         /* new_var */
1143         attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1144         if (!attr)
1145                 return -ENOMEM;
1146
1147         attr->attr.name = "new_var";
1148         attr->attr.mode = 0200;
1149         attr->write = efivar_create;
1150         attr->private = efivars;
1151         efivars->new_var = attr;
1152
1153         /* del_var */
1154         attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1155         if (!attr) {
1156                 error = -ENOMEM;
1157                 goto out_free;
1158         }
1159         attr->attr.name = "del_var";
1160         attr->attr.mode = 0200;
1161         attr->write = efivar_delete;
1162         attr->private = efivars;
1163         efivars->del_var = attr;
1164
1165         sysfs_bin_attr_init(efivars->new_var);
1166         sysfs_bin_attr_init(efivars->del_var);
1167
1168         /* Register */
1169         error = sysfs_create_bin_file(&efivars->kset->kobj,
1170                                       efivars->new_var);
1171         if (error) {
1172                 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
1173                         " due to error %d\n", error);
1174                 goto out_free;
1175         }
1176         error = sysfs_create_bin_file(&efivars->kset->kobj,
1177                                       efivars->del_var);
1178         if (error) {
1179                 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
1180                         " due to error %d\n", error);
1181                 sysfs_remove_bin_file(&efivars->kset->kobj,
1182                                       efivars->new_var);
1183                 goto out_free;
1184         }
1185
1186         return 0;
1187 out_free:
1188         kfree(efivars->del_var);
1189         efivars->del_var = NULL;
1190         kfree(efivars->new_var);
1191         efivars->new_var = NULL;
1192         return error;
1193 }
1194
1195 void unregister_efivars(struct efivars *efivars)
1196 {
1197         struct efivar_entry *entry, *n;
1198
1199         list_for_each_entry_safe(entry, n, &efivars->list, list) {
1200                 spin_lock_irq(&efivars->lock);
1201                 list_del(&entry->list);
1202                 spin_unlock_irq(&efivars->lock);
1203                 efivar_unregister(entry);
1204         }
1205         if (efivars->new_var)
1206                 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
1207         if (efivars->del_var)
1208                 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
1209         kfree(efivars->new_var);
1210         kfree(efivars->del_var);
1211         kset_unregister(efivars->kset);
1212 }
1213 EXPORT_SYMBOL_GPL(unregister_efivars);
1214
1215 int register_efivars(struct efivars *efivars,
1216                      const struct efivar_operations *ops,
1217                      struct kobject *parent_kobj)
1218 {
1219         efi_status_t status = EFI_NOT_FOUND;
1220         efi_guid_t vendor_guid;
1221         efi_char16_t *variable_name;
1222         unsigned long variable_name_size = 1024;
1223         int error = 0;
1224
1225         variable_name = kzalloc(variable_name_size, GFP_KERNEL);
1226         if (!variable_name) {
1227                 printk(KERN_ERR "efivars: Memory allocation failed.\n");
1228                 return -ENOMEM;
1229         }
1230
1231         spin_lock_init(&efivars->lock);
1232         INIT_LIST_HEAD(&efivars->list);
1233         efivars->ops = ops;
1234
1235         efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
1236         if (!efivars->kset) {
1237                 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
1238                 error = -ENOMEM;
1239                 goto out;
1240         }
1241
1242         /*
1243          * Per EFI spec, the maximum storage allocated for both
1244          * the variable name and variable data is 1024 bytes.
1245          */
1246
1247         do {
1248                 variable_name_size = 1024;
1249
1250                 status = ops->get_next_variable(&variable_name_size,
1251                                                 variable_name,
1252                                                 &vendor_guid);
1253                 switch (status) {
1254                 case EFI_SUCCESS:
1255                         efivar_create_sysfs_entry(efivars,
1256                                                   variable_name_size,
1257                                                   variable_name,
1258                                                   &vendor_guid);
1259                         break;
1260                 case EFI_NOT_FOUND:
1261                         break;
1262                 default:
1263                         printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
1264                                 status);
1265                         status = EFI_NOT_FOUND;
1266                         break;
1267                 }
1268         } while (status != EFI_NOT_FOUND);
1269
1270         error = create_efivars_bin_attributes(efivars);
1271         if (error)
1272                 unregister_efivars(efivars);
1273
1274         efivars->efi_pstore_info = efi_pstore_info;
1275
1276         efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
1277         if (efivars->efi_pstore_info.buf) {
1278                 efivars->efi_pstore_info.bufsize = 1024;
1279                 efivars->efi_pstore_info.data = efivars;
1280                 spin_lock_init(&efivars->efi_pstore_info.buf_lock);
1281                 pstore_register(&efivars->efi_pstore_info);
1282         }
1283
1284 out:
1285         kfree(variable_name);
1286
1287         return error;
1288 }
1289 EXPORT_SYMBOL_GPL(register_efivars);
1290
1291 static struct efivars __efivars;
1292 static struct efivar_operations ops;
1293
1294 /*
1295  * For now we register the efi subsystem with the firmware subsystem
1296  * and the vars subsystem with the efi subsystem.  In the future, it
1297  * might make sense to split off the efi subsystem into its own
1298  * driver, but for now only efivars will register with it, so just
1299  * include it here.
1300  */
1301
1302 static int __init
1303 efivars_init(void)
1304 {
1305         int error = 0;
1306
1307         printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
1308                EFIVARS_DATE);
1309
1310         if (!efi_enabled(EFI_RUNTIME_SERVICES))
1311                 return 0;
1312
1313         /* For now we'll register the efi directory at /sys/firmware/efi */
1314         efi_kobj = kobject_create_and_add("efi", firmware_kobj);
1315         if (!efi_kobj) {
1316                 printk(KERN_ERR "efivars: Firmware registration failed.\n");
1317                 return -ENOMEM;
1318         }
1319
1320         ops.get_variable = efi.get_variable;
1321         ops.set_variable = efi.set_variable;
1322         ops.get_next_variable = efi.get_next_variable;
1323         ops.query_variable_info = efi.query_variable_info;
1324         error = register_efivars(&__efivars, &ops, efi_kobj);
1325         if (error)
1326                 goto err_put;
1327
1328         /* Don't forget the systab entry */
1329         error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
1330         if (error) {
1331                 printk(KERN_ERR
1332                        "efivars: Sysfs attribute export failed with error %d.\n",
1333                        error);
1334                 goto err_unregister;
1335         }
1336
1337         return 0;
1338
1339 err_unregister:
1340         unregister_efivars(&__efivars);
1341 err_put:
1342         kobject_put(efi_kobj);
1343         return error;
1344 }
1345
1346 static void __exit
1347 efivars_exit(void)
1348 {
1349         if (efi_enabled(EFI_RUNTIME_SERVICES)) {
1350                 unregister_efivars(&__efivars);
1351                 kobject_put(efi_kobj);
1352         }
1353 }
1354
1355 module_init(efivars_init);
1356 module_exit(efivars_exit);
1357