efi: split efisubsystem from efivars
[linux/fpc-iii.git] / drivers / firmware / efi / efivars.c
blob70635b3b59d39d8751ceb4848d56ca962eb8f210
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_unparse()
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>
71 #define EFIVARS_VERSION "0.08"
72 #define EFIVARS_DATE "2004-May-17"
74 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
75 MODULE_DESCRIPTION("sysfs interface to EFI Variables");
76 MODULE_LICENSE("GPL");
77 MODULE_VERSION(EFIVARS_VERSION);
79 LIST_HEAD(efivar_sysfs_list);
80 EXPORT_SYMBOL_GPL(efivar_sysfs_list);
82 static struct kset *efivars_kset;
84 static struct bin_attribute *efivars_new_var;
85 static struct bin_attribute *efivars_del_var;
87 struct efivar_attribute {
88 struct attribute attr;
89 ssize_t (*show) (struct efivar_entry *entry, char *buf);
90 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
93 #define EFIVAR_ATTR(_name, _mode, _show, _store) \
94 struct efivar_attribute efivar_attr_##_name = { \
95 .attr = {.name = __stringify(_name), .mode = _mode}, \
96 .show = _show, \
97 .store = _store, \
100 #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
101 #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
104 * Prototype for sysfs creation function
106 static int
107 efivar_create_sysfs_entry(struct efivar_entry *new_var);
109 static ssize_t
110 efivar_guid_read(struct efivar_entry *entry, char *buf)
112 struct efi_variable *var = &entry->var;
113 char *str = buf;
115 if (!entry || !buf)
116 return 0;
118 efi_guid_unparse(&var->VendorGuid, str);
119 str += strlen(str);
120 str += sprintf(str, "\n");
122 return str - buf;
125 static ssize_t
126 efivar_attr_read(struct efivar_entry *entry, char *buf)
128 struct efi_variable *var = &entry->var;
129 char *str = buf;
131 if (!entry || !buf)
132 return -EINVAL;
134 var->DataSize = 1024;
135 if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
136 return -EIO;
138 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
139 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
140 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
141 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
142 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
143 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
144 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
145 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
146 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
147 str += sprintf(str,
148 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
149 if (var->Attributes &
150 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
151 str += sprintf(str,
152 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
153 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
154 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
155 return str - buf;
158 static ssize_t
159 efivar_size_read(struct efivar_entry *entry, char *buf)
161 struct efi_variable *var = &entry->var;
162 char *str = buf;
164 if (!entry || !buf)
165 return -EINVAL;
167 var->DataSize = 1024;
168 if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
169 return -EIO;
171 str += sprintf(str, "0x%lx\n", var->DataSize);
172 return str - buf;
175 static ssize_t
176 efivar_data_read(struct efivar_entry *entry, char *buf)
178 struct efi_variable *var = &entry->var;
180 if (!entry || !buf)
181 return -EINVAL;
183 var->DataSize = 1024;
184 if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
185 return -EIO;
187 memcpy(buf, var->Data, var->DataSize);
188 return var->DataSize;
191 * We allow each variable to be edited via rewriting the
192 * entire efi variable structure.
194 static ssize_t
195 efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
197 struct efi_variable *new_var, *var = &entry->var;
198 int err;
200 if (count != sizeof(struct efi_variable))
201 return -EINVAL;
203 new_var = (struct efi_variable *)buf;
205 * If only updating the variable data, then the name
206 * and guid should remain the same
208 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
209 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
210 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
211 return -EINVAL;
214 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
215 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
216 return -EINVAL;
219 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
220 efivar_validate(new_var, new_var->Data, new_var->DataSize) == false) {
221 printk(KERN_ERR "efivars: Malformed variable content\n");
222 return -EINVAL;
225 memcpy(&entry->var, new_var, count);
227 err = efivar_entry_set(entry, new_var->Attributes,
228 new_var->DataSize, new_var->Data, false);
229 if (err) {
230 printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err);
231 return -EIO;
234 return count;
237 static ssize_t
238 efivar_show_raw(struct efivar_entry *entry, char *buf)
240 struct efi_variable *var = &entry->var;
242 if (!entry || !buf)
243 return 0;
245 var->DataSize = 1024;
246 if (efivar_entry_get(entry, &entry->var.Attributes,
247 &entry->var.DataSize, entry->var.Data))
248 return -EIO;
250 memcpy(buf, var, sizeof(*var));
252 return sizeof(*var);
256 * Generic read/write functions that call the specific functions of
257 * the attributes...
259 static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
260 char *buf)
262 struct efivar_entry *var = to_efivar_entry(kobj);
263 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
264 ssize_t ret = -EIO;
266 if (!capable(CAP_SYS_ADMIN))
267 return -EACCES;
269 if (efivar_attr->show) {
270 ret = efivar_attr->show(var, buf);
272 return ret;
275 static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
276 const char *buf, size_t count)
278 struct efivar_entry *var = to_efivar_entry(kobj);
279 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
280 ssize_t ret = -EIO;
282 if (!capable(CAP_SYS_ADMIN))
283 return -EACCES;
285 if (efivar_attr->store)
286 ret = efivar_attr->store(var, buf, count);
288 return ret;
291 static const struct sysfs_ops efivar_attr_ops = {
292 .show = efivar_attr_show,
293 .store = efivar_attr_store,
296 static void efivar_release(struct kobject *kobj)
298 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
299 kfree(var);
302 static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
303 static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
304 static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
305 static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
306 static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
308 static struct attribute *def_attrs[] = {
309 &efivar_attr_guid.attr,
310 &efivar_attr_size.attr,
311 &efivar_attr_attributes.attr,
312 &efivar_attr_data.attr,
313 &efivar_attr_raw_var.attr,
314 NULL,
317 static struct kobj_type efivar_ktype = {
318 .release = efivar_release,
319 .sysfs_ops = &efivar_attr_ops,
320 .default_attrs = def_attrs,
323 static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
324 struct bin_attribute *bin_attr,
325 char *buf, loff_t pos, size_t count)
327 struct efi_variable *new_var = (struct efi_variable *)buf;
328 struct efivar_entry *new_entry;
329 int err;
331 if (!capable(CAP_SYS_ADMIN))
332 return -EACCES;
334 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
335 efivar_validate(new_var, new_var->Data, new_var->DataSize) == false) {
336 printk(KERN_ERR "efivars: Malformed variable content\n");
337 return -EINVAL;
340 new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL);
341 if (!new_entry)
342 return -ENOMEM;
344 memcpy(&new_entry->var, new_var, sizeof(*new_var));
346 err = efivar_entry_set(new_entry, new_var->Attributes, new_var->DataSize,
347 new_var->Data, &efivar_sysfs_list);
348 if (err) {
349 if (err == -EEXIST)
350 err = -EINVAL;
351 goto out;
354 if (efivar_create_sysfs_entry(new_entry)) {
355 printk(KERN_WARNING "efivars: failed to create sysfs entry.\n");
356 kfree(new_entry);
358 return count;
360 out:
361 kfree(new_entry);
362 return err;
365 static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
366 struct bin_attribute *bin_attr,
367 char *buf, loff_t pos, size_t count)
369 struct efi_variable *del_var = (struct efi_variable *)buf;
370 struct efivar_entry *entry;
371 int err = 0;
373 if (!capable(CAP_SYS_ADMIN))
374 return -EACCES;
376 efivar_entry_iter_begin();
377 entry = efivar_entry_find(del_var->VariableName, del_var->VendorGuid,
378 &efivar_sysfs_list, true);
379 if (!entry)
380 err = -EINVAL;
381 else if (__efivar_entry_delete(entry))
382 err = -EIO;
384 efivar_entry_iter_end();
386 if (err)
387 return err;
389 efivar_unregister(entry);
391 /* It's dead Jim.... */
392 return count;
396 * efivar_create_sysfs_entry - create a new entry in sysfs
397 * @new_var: efivar entry to create
399 * Returns 1 on failure, 0 on success
401 static int
402 efivar_create_sysfs_entry(struct efivar_entry *new_var)
404 int i, short_name_size;
405 char *short_name;
406 unsigned long variable_name_size;
407 efi_char16_t *variable_name;
409 variable_name = new_var->var.VariableName;
410 variable_name_size = utf16_strlen(variable_name) * sizeof(efi_char16_t);
413 * Length of the variable bytes in ASCII, plus the '-' separator,
414 * plus the GUID, plus trailing NUL
416 short_name_size = variable_name_size / sizeof(efi_char16_t)
417 + 1 + EFI_VARIABLE_GUID_LEN + 1;
419 short_name = kzalloc(short_name_size, GFP_KERNEL);
421 if (!short_name) {
422 kfree(short_name);
423 return 1;
426 /* Convert Unicode to normal chars (assume top bits are 0),
427 ala UTF-8 */
428 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
429 short_name[i] = variable_name[i] & 0xFF;
431 /* This is ugly, but necessary to separate one vendor's
432 private variables from another's. */
434 *(short_name + strlen(short_name)) = '-';
435 efi_guid_unparse(&new_var->var.VendorGuid,
436 short_name + strlen(short_name));
438 new_var->kobj.kset = efivars_kset;
440 i = kobject_init_and_add(&new_var->kobj, &efivar_ktype,
441 NULL, "%s", short_name);
442 kfree(short_name);
443 if (i)
444 return 1;
446 kobject_uevent(&new_var->kobj, KOBJ_ADD);
447 efivar_entry_add(new_var, &efivar_sysfs_list);
449 return 0;
452 static int
453 create_efivars_bin_attributes(void)
455 struct bin_attribute *attr;
456 int error;
458 /* new_var */
459 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
460 if (!attr)
461 return -ENOMEM;
463 attr->attr.name = "new_var";
464 attr->attr.mode = 0200;
465 attr->write = efivar_create;
466 efivars_new_var = attr;
468 /* del_var */
469 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
470 if (!attr) {
471 error = -ENOMEM;
472 goto out_free;
474 attr->attr.name = "del_var";
475 attr->attr.mode = 0200;
476 attr->write = efivar_delete;
477 efivars_del_var = attr;
479 sysfs_bin_attr_init(efivars_new_var);
480 sysfs_bin_attr_init(efivars_del_var);
482 /* Register */
483 error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_new_var);
484 if (error) {
485 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
486 " due to error %d\n", error);
487 goto out_free;
490 error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_del_var);
491 if (error) {
492 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
493 " due to error %d\n", error);
494 sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
495 goto out_free;
498 return 0;
499 out_free:
500 kfree(efivars_del_var);
501 efivars_del_var = NULL;
502 kfree(efivars_new_var);
503 efivars_new_var = NULL;
504 return error;
507 static int efivar_update_sysfs_entry(efi_char16_t *name, efi_guid_t vendor,
508 unsigned long name_size, void *data)
510 struct efivar_entry *entry = data;
512 if (efivar_entry_find(name, vendor, &efivar_sysfs_list, false))
513 return 0;
515 memcpy(entry->var.VariableName, name, name_size);
516 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
518 return 1;
521 static void efivar_update_sysfs_entries(struct work_struct *work)
523 struct efivar_entry *entry;
524 int err;
526 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
527 if (!entry)
528 return;
530 /* Add new sysfs entries */
531 while (1) {
532 memset(entry, 0, sizeof(*entry));
534 err = efivar_init(efivar_update_sysfs_entry, entry,
535 true, false, &efivar_sysfs_list);
536 if (!err)
537 break;
539 efivar_create_sysfs_entry(entry);
542 kfree(entry);
545 static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
546 unsigned long name_size, void *data)
548 struct efivar_entry *entry;
550 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
551 if (!entry)
552 return -ENOMEM;
554 memcpy(entry->var.VariableName, name, name_size);
555 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
557 efivar_create_sysfs_entry(entry);
559 return 0;
562 static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
564 efivar_entry_remove(entry);
565 efivar_unregister(entry);
566 return 0;
569 void efivars_sysfs_exit(void)
571 /* Remove all entries and destroy */
572 __efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list, NULL, NULL);
574 if (efivars_new_var)
575 sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
576 if (efivars_del_var)
577 sysfs_remove_bin_file(&efivars_kset->kobj, efivars_del_var);
578 kfree(efivars_new_var);
579 kfree(efivars_del_var);
580 kset_unregister(efivars_kset);
583 int efivars_sysfs_init(void)
585 struct kobject *parent_kobj = efivars_kobject();
586 int error = 0;
588 /* No efivars has been registered yet */
589 if (!parent_kobj)
590 return 0;
592 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
593 EFIVARS_DATE);
595 efivars_kset = kset_create_and_add("vars", NULL, parent_kobj);
596 if (!efivars_kset) {
597 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
598 return -ENOMEM;
601 efivar_init(efivars_sysfs_callback, NULL, false,
602 true, &efivar_sysfs_list);
604 error = create_efivars_bin_attributes();
605 if (error) {
606 efivars_sysfs_exit();
607 return error;
610 INIT_WORK(&efivar_work, efivar_update_sysfs_entries);
612 return 0;
614 EXPORT_SYMBOL_GPL(efivars_sysfs_init);
616 module_init(efivars_sysfs_init);
617 module_exit(efivars_sysfs_exit);