Linux 4.2.1
[linux/fpc-iii.git] / drivers / firmware / efi / efivars.c
blob756eca8c4cf8f291025a3ad44f7cbb9981aeb5fd
1 /*
2 * Originally from efivars.c,
4 * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
5 * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
7 * This code takes all variables accessible from EFI runtime and
8 * exports them via sysfs
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.
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.
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
24 * Changelog:
26 * 17 May 2004 - Matt Domsch <Matt_Domsch@dell.com>
27 * remove check for efi_enabled in exit
28 * add MODULE_VERSION
30 * 26 Apr 2004 - Matt Domsch <Matt_Domsch@dell.com>
31 * minor bug fixes
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
38 * 10 Dec 2002 - Matt Domsch <Matt_Domsch@dell.com>
39 * fix locking per Peter Chubb's findings
41 * 25 Mar 2002 - Matt Domsch <Matt_Domsch@dell.com>
42 * move uuid_unparse() to include/asm-ia64/efi.h:efi_guid_to_str()
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
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
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.
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
64 * 26 February 2001 - Matt Domsch <Matt_Domsch@dell.com>
65 * v0.01 release to linux-ia64@linuxia64.org
68 #include <linux/efi.h>
69 #include <linux/module.h>
70 #include <linux/slab.h>
71 #include <linux/ucs2_string.h>
72 #include <linux/compat.h>
74 #define EFIVARS_VERSION "0.08"
75 #define EFIVARS_DATE "2004-May-17"
77 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
78 MODULE_DESCRIPTION("sysfs interface to EFI Variables");
79 MODULE_LICENSE("GPL");
80 MODULE_VERSION(EFIVARS_VERSION);
81 MODULE_ALIAS("platform:efivars");
83 LIST_HEAD(efivar_sysfs_list);
84 EXPORT_SYMBOL_GPL(efivar_sysfs_list);
86 static struct kset *efivars_kset;
88 static struct bin_attribute *efivars_new_var;
89 static struct bin_attribute *efivars_del_var;
91 struct compat_efi_variable {
92 efi_char16_t VariableName[EFI_VAR_NAME_LEN/sizeof(efi_char16_t)];
93 efi_guid_t VendorGuid;
94 __u32 DataSize;
95 __u8 Data[1024];
96 __u32 Status;
97 __u32 Attributes;
98 } __packed;
100 struct efivar_attribute {
101 struct attribute attr;
102 ssize_t (*show) (struct efivar_entry *entry, char *buf);
103 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
106 #define EFIVAR_ATTR(_name, _mode, _show, _store) \
107 struct efivar_attribute efivar_attr_##_name = { \
108 .attr = {.name = __stringify(_name), .mode = _mode}, \
109 .show = _show, \
110 .store = _store, \
113 #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
114 #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
117 * Prototype for sysfs creation function
119 static int
120 efivar_create_sysfs_entry(struct efivar_entry *new_var);
122 static ssize_t
123 efivar_guid_read(struct efivar_entry *entry, char *buf)
125 struct efi_variable *var = &entry->var;
126 char *str = buf;
128 if (!entry || !buf)
129 return 0;
131 efi_guid_to_str(&var->VendorGuid, str);
132 str += strlen(str);
133 str += sprintf(str, "\n");
135 return str - buf;
138 static ssize_t
139 efivar_attr_read(struct efivar_entry *entry, char *buf)
141 struct efi_variable *var = &entry->var;
142 char *str = buf;
144 if (!entry || !buf)
145 return -EINVAL;
147 var->DataSize = 1024;
148 if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
149 return -EIO;
151 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
152 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
153 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
154 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
155 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
156 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
157 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
158 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
159 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
160 str += sprintf(str,
161 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
162 if (var->Attributes &
163 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
164 str += sprintf(str,
165 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
166 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
167 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
168 return str - buf;
171 static ssize_t
172 efivar_size_read(struct efivar_entry *entry, char *buf)
174 struct efi_variable *var = &entry->var;
175 char *str = buf;
177 if (!entry || !buf)
178 return -EINVAL;
180 var->DataSize = 1024;
181 if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
182 return -EIO;
184 str += sprintf(str, "0x%lx\n", var->DataSize);
185 return str - buf;
188 static ssize_t
189 efivar_data_read(struct efivar_entry *entry, char *buf)
191 struct efi_variable *var = &entry->var;
193 if (!entry || !buf)
194 return -EINVAL;
196 var->DataSize = 1024;
197 if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
198 return -EIO;
200 memcpy(buf, var->Data, var->DataSize);
201 return var->DataSize;
204 static inline int
205 sanity_check(struct efi_variable *var, efi_char16_t *name, efi_guid_t vendor,
206 unsigned long size, u32 attributes, u8 *data)
209 * If only updating the variable data, then the name
210 * and guid should remain the same
212 if (memcmp(name, var->VariableName, sizeof(var->VariableName)) ||
213 efi_guidcmp(vendor, var->VendorGuid)) {
214 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
215 return -EINVAL;
218 if ((size <= 0) || (attributes == 0)){
219 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
220 return -EINVAL;
223 if ((attributes & ~EFI_VARIABLE_MASK) != 0 ||
224 efivar_validate(name, data, size) == false) {
225 printk(KERN_ERR "efivars: Malformed variable content\n");
226 return -EINVAL;
229 return 0;
232 static inline bool is_compat(void)
234 if (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())
235 return true;
237 return false;
240 static void
241 copy_out_compat(struct efi_variable *dst, struct compat_efi_variable *src)
243 memcpy(dst->VariableName, src->VariableName, EFI_VAR_NAME_LEN);
244 memcpy(dst->Data, src->Data, sizeof(src->Data));
246 dst->VendorGuid = src->VendorGuid;
247 dst->DataSize = src->DataSize;
248 dst->Attributes = src->Attributes;
252 * We allow each variable to be edited via rewriting the
253 * entire efi variable structure.
255 static ssize_t
256 efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
258 struct efi_variable *new_var, *var = &entry->var;
259 efi_char16_t *name;
260 unsigned long size;
261 efi_guid_t vendor;
262 u32 attributes;
263 u8 *data;
264 int err;
266 if (is_compat()) {
267 struct compat_efi_variable *compat;
269 if (count != sizeof(*compat))
270 return -EINVAL;
272 compat = (struct compat_efi_variable *)buf;
273 attributes = compat->Attributes;
274 vendor = compat->VendorGuid;
275 name = compat->VariableName;
276 size = compat->DataSize;
277 data = compat->Data;
279 err = sanity_check(var, name, vendor, size, attributes, data);
280 if (err)
281 return err;
283 copy_out_compat(&entry->var, compat);
284 } else {
285 if (count != sizeof(struct efi_variable))
286 return -EINVAL;
288 new_var = (struct efi_variable *)buf;
290 attributes = new_var->Attributes;
291 vendor = new_var->VendorGuid;
292 name = new_var->VariableName;
293 size = new_var->DataSize;
294 data = new_var->Data;
296 err = sanity_check(var, name, vendor, size, attributes, data);
297 if (err)
298 return err;
300 memcpy(&entry->var, new_var, count);
303 err = efivar_entry_set(entry, attributes, size, data, NULL);
304 if (err) {
305 printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err);
306 return -EIO;
309 return count;
312 static ssize_t
313 efivar_show_raw(struct efivar_entry *entry, char *buf)
315 struct efi_variable *var = &entry->var;
316 struct compat_efi_variable *compat;
317 size_t size;
319 if (!entry || !buf)
320 return 0;
322 var->DataSize = 1024;
323 if (efivar_entry_get(entry, &entry->var.Attributes,
324 &entry->var.DataSize, entry->var.Data))
325 return -EIO;
327 if (is_compat()) {
328 compat = (struct compat_efi_variable *)buf;
330 size = sizeof(*compat);
331 memcpy(compat->VariableName, var->VariableName,
332 EFI_VAR_NAME_LEN);
333 memcpy(compat->Data, var->Data, sizeof(compat->Data));
335 compat->VendorGuid = var->VendorGuid;
336 compat->DataSize = var->DataSize;
337 compat->Attributes = var->Attributes;
338 } else {
339 size = sizeof(*var);
340 memcpy(buf, var, size);
343 return size;
347 * Generic read/write functions that call the specific functions of
348 * the attributes...
350 static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
351 char *buf)
353 struct efivar_entry *var = to_efivar_entry(kobj);
354 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
355 ssize_t ret = -EIO;
357 if (!capable(CAP_SYS_ADMIN))
358 return -EACCES;
360 if (efivar_attr->show) {
361 ret = efivar_attr->show(var, buf);
363 return ret;
366 static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
367 const char *buf, size_t count)
369 struct efivar_entry *var = to_efivar_entry(kobj);
370 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
371 ssize_t ret = -EIO;
373 if (!capable(CAP_SYS_ADMIN))
374 return -EACCES;
376 if (efivar_attr->store)
377 ret = efivar_attr->store(var, buf, count);
379 return ret;
382 static const struct sysfs_ops efivar_attr_ops = {
383 .show = efivar_attr_show,
384 .store = efivar_attr_store,
387 static void efivar_release(struct kobject *kobj)
389 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
390 kfree(var);
393 static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
394 static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
395 static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
396 static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
397 static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
399 static struct attribute *def_attrs[] = {
400 &efivar_attr_guid.attr,
401 &efivar_attr_size.attr,
402 &efivar_attr_attributes.attr,
403 &efivar_attr_data.attr,
404 &efivar_attr_raw_var.attr,
405 NULL,
408 static struct kobj_type efivar_ktype = {
409 .release = efivar_release,
410 .sysfs_ops = &efivar_attr_ops,
411 .default_attrs = def_attrs,
414 static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
415 struct bin_attribute *bin_attr,
416 char *buf, loff_t pos, size_t count)
418 struct compat_efi_variable *compat = (struct compat_efi_variable *)buf;
419 struct efi_variable *new_var = (struct efi_variable *)buf;
420 struct efivar_entry *new_entry;
421 bool need_compat = is_compat();
422 efi_char16_t *name;
423 unsigned long size;
424 u32 attributes;
425 u8 *data;
426 int err;
428 if (!capable(CAP_SYS_ADMIN))
429 return -EACCES;
431 if (need_compat) {
432 if (count != sizeof(*compat))
433 return -EINVAL;
435 attributes = compat->Attributes;
436 name = compat->VariableName;
437 size = compat->DataSize;
438 data = compat->Data;
439 } else {
440 if (count != sizeof(*new_var))
441 return -EINVAL;
443 attributes = new_var->Attributes;
444 name = new_var->VariableName;
445 size = new_var->DataSize;
446 data = new_var->Data;
449 if ((attributes & ~EFI_VARIABLE_MASK) != 0 ||
450 efivar_validate(name, data, size) == false) {
451 printk(KERN_ERR "efivars: Malformed variable content\n");
452 return -EINVAL;
455 new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL);
456 if (!new_entry)
457 return -ENOMEM;
459 if (need_compat)
460 copy_out_compat(&new_entry->var, compat);
461 else
462 memcpy(&new_entry->var, new_var, sizeof(*new_var));
464 err = efivar_entry_set(new_entry, attributes, size,
465 data, &efivar_sysfs_list);
466 if (err) {
467 if (err == -EEXIST)
468 err = -EINVAL;
469 goto out;
472 if (efivar_create_sysfs_entry(new_entry)) {
473 printk(KERN_WARNING "efivars: failed to create sysfs entry.\n");
474 kfree(new_entry);
476 return count;
478 out:
479 kfree(new_entry);
480 return err;
483 static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
484 struct bin_attribute *bin_attr,
485 char *buf, loff_t pos, size_t count)
487 struct efi_variable *del_var = (struct efi_variable *)buf;
488 struct compat_efi_variable *compat;
489 struct efivar_entry *entry;
490 efi_char16_t *name;
491 efi_guid_t vendor;
492 int err = 0;
494 if (!capable(CAP_SYS_ADMIN))
495 return -EACCES;
497 if (is_compat()) {
498 if (count != sizeof(*compat))
499 return -EINVAL;
501 compat = (struct compat_efi_variable *)buf;
502 name = compat->VariableName;
503 vendor = compat->VendorGuid;
504 } else {
505 if (count != sizeof(*del_var))
506 return -EINVAL;
508 name = del_var->VariableName;
509 vendor = del_var->VendorGuid;
512 efivar_entry_iter_begin();
513 entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true);
514 if (!entry)
515 err = -EINVAL;
516 else if (__efivar_entry_delete(entry))
517 err = -EIO;
519 if (err) {
520 efivar_entry_iter_end();
521 return err;
524 if (!entry->scanning) {
525 efivar_entry_iter_end();
526 efivar_unregister(entry);
527 } else
528 efivar_entry_iter_end();
530 /* It's dead Jim.... */
531 return count;
535 * efivar_create_sysfs_entry - create a new entry in sysfs
536 * @new_var: efivar entry to create
538 * Returns 0 on success, negative error code on failure
540 static int
541 efivar_create_sysfs_entry(struct efivar_entry *new_var)
543 int i, short_name_size;
544 char *short_name;
545 unsigned long variable_name_size;
546 efi_char16_t *variable_name;
547 int ret;
549 variable_name = new_var->var.VariableName;
550 variable_name_size = ucs2_strlen(variable_name) * sizeof(efi_char16_t);
553 * Length of the variable bytes in ASCII, plus the '-' separator,
554 * plus the GUID, plus trailing NUL
556 short_name_size = variable_name_size / sizeof(efi_char16_t)
557 + 1 + EFI_VARIABLE_GUID_LEN + 1;
559 short_name = kzalloc(short_name_size, GFP_KERNEL);
561 if (!short_name)
562 return -ENOMEM;
564 /* Convert Unicode to normal chars (assume top bits are 0),
565 ala UTF-8 */
566 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
567 short_name[i] = variable_name[i] & 0xFF;
569 /* This is ugly, but necessary to separate one vendor's
570 private variables from another's. */
572 *(short_name + strlen(short_name)) = '-';
573 efi_guid_to_str(&new_var->var.VendorGuid,
574 short_name + strlen(short_name));
576 new_var->kobj.kset = efivars_kset;
578 ret = kobject_init_and_add(&new_var->kobj, &efivar_ktype,
579 NULL, "%s", short_name);
580 kfree(short_name);
581 if (ret)
582 return ret;
584 kobject_uevent(&new_var->kobj, KOBJ_ADD);
585 efivar_entry_add(new_var, &efivar_sysfs_list);
587 return 0;
590 static int
591 create_efivars_bin_attributes(void)
593 struct bin_attribute *attr;
594 int error;
596 /* new_var */
597 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
598 if (!attr)
599 return -ENOMEM;
601 attr->attr.name = "new_var";
602 attr->attr.mode = 0200;
603 attr->write = efivar_create;
604 efivars_new_var = attr;
606 /* del_var */
607 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
608 if (!attr) {
609 error = -ENOMEM;
610 goto out_free;
612 attr->attr.name = "del_var";
613 attr->attr.mode = 0200;
614 attr->write = efivar_delete;
615 efivars_del_var = attr;
617 sysfs_bin_attr_init(efivars_new_var);
618 sysfs_bin_attr_init(efivars_del_var);
620 /* Register */
621 error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_new_var);
622 if (error) {
623 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
624 " due to error %d\n", error);
625 goto out_free;
628 error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_del_var);
629 if (error) {
630 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
631 " due to error %d\n", error);
632 sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
633 goto out_free;
636 return 0;
637 out_free:
638 kfree(efivars_del_var);
639 efivars_del_var = NULL;
640 kfree(efivars_new_var);
641 efivars_new_var = NULL;
642 return error;
645 static int efivar_update_sysfs_entry(efi_char16_t *name, efi_guid_t vendor,
646 unsigned long name_size, void *data)
648 struct efivar_entry *entry = data;
650 if (efivar_entry_find(name, vendor, &efivar_sysfs_list, false))
651 return 0;
653 memcpy(entry->var.VariableName, name, name_size);
654 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
656 return 1;
659 static void efivar_update_sysfs_entries(struct work_struct *work)
661 struct efivar_entry *entry;
662 int err;
664 /* Add new sysfs entries */
665 while (1) {
666 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
667 if (!entry)
668 return;
670 err = efivar_init(efivar_update_sysfs_entry, entry,
671 true, false, &efivar_sysfs_list);
672 if (!err)
673 break;
675 efivar_create_sysfs_entry(entry);
678 kfree(entry);
681 static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
682 unsigned long name_size, void *data)
684 struct efivar_entry *entry;
686 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
687 if (!entry)
688 return -ENOMEM;
690 memcpy(entry->var.VariableName, name, name_size);
691 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
693 efivar_create_sysfs_entry(entry);
695 return 0;
698 static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
700 efivar_entry_remove(entry);
701 efivar_unregister(entry);
702 return 0;
705 static void efivars_sysfs_exit(void)
707 /* Remove all entries and destroy */
708 __efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list, NULL, NULL);
710 if (efivars_new_var)
711 sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
712 if (efivars_del_var)
713 sysfs_remove_bin_file(&efivars_kset->kobj, efivars_del_var);
714 kfree(efivars_new_var);
715 kfree(efivars_del_var);
716 kset_unregister(efivars_kset);
719 int efivars_sysfs_init(void)
721 struct kobject *parent_kobj = efivars_kobject();
722 int error = 0;
724 if (!efi_enabled(EFI_RUNTIME_SERVICES))
725 return -ENODEV;
727 /* No efivars has been registered yet */
728 if (!parent_kobj)
729 return 0;
731 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
732 EFIVARS_DATE);
734 efivars_kset = kset_create_and_add("vars", NULL, parent_kobj);
735 if (!efivars_kset) {
736 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
737 return -ENOMEM;
740 efivar_init(efivars_sysfs_callback, NULL, false,
741 true, &efivar_sysfs_list);
743 error = create_efivars_bin_attributes();
744 if (error) {
745 efivars_sysfs_exit();
746 return error;
749 INIT_WORK(&efivar_work, efivar_update_sysfs_entries);
751 return 0;
753 EXPORT_SYMBOL_GPL(efivars_sysfs_init);
755 module_init(efivars_sysfs_init);
756 module_exit(efivars_sysfs_exit);