2 * EFI Variables - 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
26 * 17 May 2004 - Matt Domsch <Matt_Domsch@dell.com>
27 * remove check for efi_enabled in exit
30 * 26 Apr 2004 - Matt Domsch <Matt_Domsch@dell.com>
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/capability.h>
69 #include <linux/types.h>
70 #include <linux/errno.h>
71 #include <linux/init.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>
84 #include <linux/ramfs.h>
85 #include <linux/pagemap.h>
87 #include <asm/uaccess.h>
89 #define EFIVARS_VERSION "0.08"
90 #define EFIVARS_DATE "2004-May-17"
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);
97 #define DUMP_NAME_LEN 52
101 * The maximum size of VariableName + Data = 1024
102 * Therefore, it's reasonable to save that much
103 * space in each part of the structure,
104 * and we use a page for reading/writing.
107 struct efi_variable {
108 efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
109 efi_guid_t VendorGuid;
110 unsigned long DataSize;
114 } __attribute__((packed));
116 struct efivar_entry {
117 struct efivars *efivars;
118 struct efi_variable var;
119 struct list_head list;
123 struct efivar_attribute {
124 struct attribute attr;
125 ssize_t (*show) (struct efivar_entry *entry, char *buf);
126 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
129 static struct efivars __efivars;
130 static struct efivar_operations ops;
132 #define PSTORE_EFI_ATTRIBUTES \
133 (EFI_VARIABLE_NON_VOLATILE | \
134 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
135 EFI_VARIABLE_RUNTIME_ACCESS)
137 #define EFIVAR_ATTR(_name, _mode, _show, _store) \
138 struct efivar_attribute efivar_attr_##_name = { \
139 .attr = {.name = __stringify(_name), .mode = _mode}, \
144 #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
145 #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
148 * Prototype for sysfs creation function
151 efivar_create_sysfs_entry(struct efivars *efivars,
152 unsigned long variable_name_size,
153 efi_char16_t *variable_name,
154 efi_guid_t *vendor_guid);
156 /* Return the number of unicode characters in data */
158 utf16_strnlen(efi_char16_t *s, size_t maxlength)
160 unsigned long length = 0;
162 while (*s++ != 0 && length < maxlength)
167 static inline unsigned long
168 utf16_strlen(efi_char16_t *s)
170 return utf16_strnlen(s, ~0UL);
174 * Return the number of bytes is the length of this string
175 * Note: this is NOT the same as the number of unicode characters
177 static inline unsigned long
178 utf16_strsize(efi_char16_t *data, unsigned long maxlength)
180 return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
184 utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
193 if (*a == 0) /* implies *b == 0 */
202 validate_device_path(struct efi_variable *var, int match, u8 *buffer,
205 struct efi_generic_dev_path *node;
208 node = (struct efi_generic_dev_path *)buffer;
210 if (len < sizeof(*node))
213 while (offset <= len - sizeof(*node) &&
214 node->length >= sizeof(*node) &&
215 node->length <= len - offset) {
216 offset += node->length;
218 if ((node->type == EFI_DEV_END_PATH ||
219 node->type == EFI_DEV_END_PATH2) &&
220 node->sub_type == EFI_DEV_END_ENTIRE)
223 node = (struct efi_generic_dev_path *)(buffer + offset);
227 * If we're here then either node->length pointed past the end
228 * of the buffer or we reached the end of the buffer without
229 * finding a device path end node.
235 validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
238 /* An array of 16-bit integers */
246 validate_load_option(struct efi_variable *var, int match, u8 *buffer,
250 int i, desclength = 0, namelen;
252 namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
254 /* Either "Boot" or "Driver" followed by four digits of hex */
255 for (i = match; i < match+4; i++) {
256 if (var->VariableName[i] > 127 ||
257 hex_to_bin(var->VariableName[i] & 0xff) < 0)
261 /* Reject it if there's 4 digits of hex and then further content */
262 if (namelen > match + 4)
265 /* A valid entry must be at least 8 bytes */
269 filepathlength = buffer[4] | buffer[5] << 8;
272 * There's no stored length for the description, so it has to be
275 desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
277 /* Each boot entry must have a descriptor */
282 * If the sum of the length of the description, the claimed filepath
283 * length and the original header are greater than the length of the
284 * variable, it's malformed
286 if ((desclength + filepathlength + 6) > len)
290 * And, finally, check the filepath
292 return validate_device_path(var, match, buffer + desclength + 6,
297 validate_uint16(struct efi_variable *var, int match, u8 *buffer,
300 /* A single 16-bit integer */
308 validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
313 for (i = 0; i < len; i++) {
324 struct variable_validate {
326 bool (*validate)(struct efi_variable *var, int match, u8 *data,
330 static const struct variable_validate variable_validate[] = {
331 { "BootNext", validate_uint16 },
332 { "BootOrder", validate_boot_order },
333 { "DriverOrder", validate_boot_order },
334 { "Boot*", validate_load_option },
335 { "Driver*", validate_load_option },
336 { "ConIn", validate_device_path },
337 { "ConInDev", validate_device_path },
338 { "ConOut", validate_device_path },
339 { "ConOutDev", validate_device_path },
340 { "ErrOut", validate_device_path },
341 { "ErrOutDev", validate_device_path },
342 { "Timeout", validate_uint16 },
343 { "Lang", validate_ascii_string },
344 { "PlatformLang", validate_ascii_string },
349 validate_var(struct efi_variable *var, u8 *data, unsigned long len)
352 u16 *unicode_name = var->VariableName;
354 for (i = 0; variable_validate[i].validate != NULL; i++) {
355 const char *name = variable_validate[i].name;
358 for (match = 0; ; match++) {
359 char c = name[match];
360 u16 u = unicode_name[match];
362 /* All special variables are plain ascii */
366 /* Wildcard in the matching name means we've matched */
368 return variable_validate[i].validate(var,
371 /* Case sensitive match */
375 /* Reached the end of the string while matching */
377 return variable_validate[i].validate(var,
386 get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
390 var->DataSize = 1024;
391 status = efivars->ops->get_variable(var->VariableName,
400 get_var_data(struct efivars *efivars, struct efi_variable *var)
404 spin_lock(&efivars->lock);
405 status = get_var_data_locked(efivars, var);
406 spin_unlock(&efivars->lock);
408 if (status != EFI_SUCCESS) {
409 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
416 efivar_guid_read(struct efivar_entry *entry, char *buf)
418 struct efi_variable *var = &entry->var;
424 efi_guid_unparse(&var->VendorGuid, str);
426 str += sprintf(str, "\n");
432 efivar_attr_read(struct efivar_entry *entry, char *buf)
434 struct efi_variable *var = &entry->var;
441 status = get_var_data(entry->efivars, var);
442 if (status != EFI_SUCCESS)
445 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
446 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
447 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
448 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
449 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
450 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
451 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
452 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
453 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
455 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
456 if (var->Attributes &
457 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
459 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
460 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
461 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
466 efivar_size_read(struct efivar_entry *entry, char *buf)
468 struct efi_variable *var = &entry->var;
475 status = get_var_data(entry->efivars, var);
476 if (status != EFI_SUCCESS)
479 str += sprintf(str, "0x%lx\n", var->DataSize);
484 efivar_data_read(struct efivar_entry *entry, char *buf)
486 struct efi_variable *var = &entry->var;
492 status = get_var_data(entry->efivars, var);
493 if (status != EFI_SUCCESS)
496 memcpy(buf, var->Data, var->DataSize);
497 return var->DataSize;
500 * We allow each variable to be edited via rewriting the
501 * entire efi variable structure.
504 efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
506 struct efi_variable *new_var, *var = &entry->var;
507 struct efivars *efivars = entry->efivars;
508 efi_status_t status = EFI_NOT_FOUND;
510 if (count != sizeof(struct efi_variable))
513 new_var = (struct efi_variable *)buf;
515 * If only updating the variable data, then the name
516 * and guid should remain the same
518 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
519 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
520 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
524 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
525 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
529 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
530 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
531 printk(KERN_ERR "efivars: Malformed variable content\n");
535 spin_lock(&efivars->lock);
536 status = efivars->ops->set_variable(new_var->VariableName,
537 &new_var->VendorGuid,
542 spin_unlock(&efivars->lock);
544 if (status != EFI_SUCCESS) {
545 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
550 memcpy(&entry->var, new_var, count);
555 efivar_show_raw(struct efivar_entry *entry, char *buf)
557 struct efi_variable *var = &entry->var;
563 status = get_var_data(entry->efivars, var);
564 if (status != EFI_SUCCESS)
567 memcpy(buf, var, sizeof(*var));
572 * Generic read/write functions that call the specific functions of
575 static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
578 struct efivar_entry *var = to_efivar_entry(kobj);
579 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
582 if (!capable(CAP_SYS_ADMIN))
585 if (efivar_attr->show) {
586 ret = efivar_attr->show(var, buf);
591 static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
592 const char *buf, size_t count)
594 struct efivar_entry *var = to_efivar_entry(kobj);
595 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
598 if (!capable(CAP_SYS_ADMIN))
601 if (efivar_attr->store)
602 ret = efivar_attr->store(var, buf, count);
607 static const struct sysfs_ops efivar_attr_ops = {
608 .show = efivar_attr_show,
609 .store = efivar_attr_store,
612 static void efivar_release(struct kobject *kobj)
614 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
618 static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
619 static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
620 static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
621 static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
622 static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
624 static struct attribute *def_attrs[] = {
625 &efivar_attr_guid.attr,
626 &efivar_attr_size.attr,
627 &efivar_attr_attributes.attr,
628 &efivar_attr_data.attr,
629 &efivar_attr_raw_var.attr,
633 static struct kobj_type efivar_ktype = {
634 .release = efivar_release,
635 .sysfs_ops = &efivar_attr_ops,
636 .default_attrs = def_attrs,
640 efivar_unregister(struct efivar_entry *var)
642 kobject_put(&var->kobj);
645 static int efivarfs_file_open(struct inode *inode, struct file *file)
647 file->private_data = inode->i_private;
651 static ssize_t efivarfs_file_write(struct file *file,
652 const char __user *userbuf, size_t count, loff_t *ppos)
654 struct efivar_entry *var = file->private_data;
655 struct efivars *efivars;
659 struct inode *inode = file->f_mapping->host;
660 int datasize = count - sizeof(attributes);
661 unsigned long newdatasize;
663 if (count < sizeof(attributes))
666 data = kmalloc(datasize, GFP_KERNEL);
671 efivars = var->efivars;
673 if (copy_from_user(&attributes, userbuf, sizeof(attributes))) {
678 if (attributes & ~(EFI_VARIABLE_MASK)) {
683 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
688 if (validate_var(&var->var, data, datasize) == false) {
693 status = efivars->ops->set_variable(var->var.VariableName,
694 &var->var.VendorGuid,
695 attributes, datasize,
701 case EFI_INVALID_PARAMETER:
704 case EFI_OUT_OF_RESOURCES:
707 case EFI_DEVICE_ERROR:
710 case EFI_WRITE_PROTECTED:
713 case EFI_SECURITY_VIOLATION:
725 * Writing to the variable may have caused a change in size (which
726 * could either be an append or an overwrite), or the variable to be
727 * deleted. Perform a GetVariable() so we can tell what actually
731 status = efivars->ops->get_variable(var->var.VariableName,
732 &var->var.VendorGuid,
736 if (status == EFI_BUFFER_TOO_SMALL) {
737 mutex_lock(&inode->i_mutex);
738 i_size_write(inode, newdatasize + sizeof(attributes));
739 mutex_unlock(&inode->i_mutex);
741 } else if (status == EFI_NOT_FOUND) {
742 spin_lock(&efivars->lock);
743 list_del(&var->list);
744 spin_unlock(&efivars->lock);
745 efivar_unregister(var);
747 dput(file->f_dentry);
750 pr_warn("efivarfs: inconsistent EFI variable implementation? "
751 "status = %lx\n", status);
760 static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
761 size_t count, loff_t *ppos)
763 struct efivar_entry *var = file->private_data;
764 struct efivars *efivars = var->efivars;
766 unsigned long datasize = 0;
771 status = efivars->ops->get_variable(var->var.VariableName,
772 &var->var.VendorGuid,
773 &attributes, &datasize, NULL);
775 if (status != EFI_BUFFER_TOO_SMALL)
778 data = kmalloc(datasize + 4, GFP_KERNEL);
783 status = efivars->ops->get_variable(var->var.VariableName,
784 &var->var.VendorGuid,
785 &attributes, &datasize,
787 if (status != EFI_SUCCESS)
790 memcpy(data, &attributes, 4);
791 size = simple_read_from_buffer(userbuf, count, ppos,
799 static void efivarfs_evict_inode(struct inode *inode)
804 static const struct super_operations efivarfs_ops = {
805 .statfs = simple_statfs,
806 .drop_inode = generic_delete_inode,
807 .evict_inode = efivarfs_evict_inode,
808 .show_options = generic_show_options,
811 static struct super_block *efivarfs_sb;
813 static const struct inode_operations efivarfs_dir_inode_operations;
815 static const struct file_operations efivarfs_file_operations = {
816 .open = efivarfs_file_open,
817 .read = efivarfs_file_read,
818 .write = efivarfs_file_write,
822 static struct inode *efivarfs_get_inode(struct super_block *sb,
823 const struct inode *dir, int mode, dev_t dev)
825 struct inode *inode = new_inode(sb);
828 inode->i_ino = get_next_ino();
829 inode->i_uid = inode->i_gid = 0;
830 inode->i_mode = mode;
831 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
832 switch (mode & S_IFMT) {
834 inode->i_fop = &efivarfs_file_operations;
837 inode->i_op = &efivarfs_dir_inode_operations;
838 inode->i_fop = &simple_dir_operations;
846 static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
848 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
849 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
850 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
851 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
852 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
853 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
854 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
855 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
856 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
857 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
858 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
859 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
860 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
861 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
862 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
863 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
866 static int efivarfs_create(struct inode *dir, struct dentry *dentry,
867 umode_t mode, bool excl)
870 struct efivars *efivars = &__efivars;
871 struct efivar_entry *var;
872 int namelen, i = 0, err = 0;
874 if (dentry->d_name.len < 38)
877 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
881 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
887 namelen = dentry->d_name.len - GUID_LEN;
889 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
890 &var->var.VendorGuid);
892 for (i = 0; i < namelen; i++)
893 var->var.VariableName[i] = dentry->d_name.name[i];
895 var->var.VariableName[i] = '\0';
897 inode->i_private = var;
898 var->efivars = efivars;
899 var->kobj.kset = efivars->kset;
901 err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
902 dentry->d_name.name);
906 kobject_uevent(&var->kobj, KOBJ_ADD);
907 spin_lock(&efivars->lock);
908 list_add(&var->list, &efivars->list);
909 spin_unlock(&efivars->lock);
910 d_instantiate(dentry, inode);
920 static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
922 struct efivar_entry *var = dentry->d_inode->i_private;
923 struct efivars *efivars = var->efivars;
926 spin_lock(&efivars->lock);
928 status = efivars->ops->set_variable(var->var.VariableName,
929 &var->var.VendorGuid,
932 if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
933 list_del(&var->list);
934 spin_unlock(&efivars->lock);
935 efivar_unregister(var);
941 spin_unlock(&efivars->lock);
945 int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
947 struct inode *inode = NULL;
949 struct efivar_entry *entry, *n;
950 struct efivars *efivars = &__efivars;
955 sb->s_maxbytes = MAX_LFS_FILESIZE;
956 sb->s_blocksize = PAGE_CACHE_SIZE;
957 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
958 sb->s_magic = PSTOREFS_MAGIC;
959 sb->s_op = &efivarfs_ops;
962 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
967 inode->i_op = &efivarfs_dir_inode_operations;
969 root = d_make_root(inode);
976 list_for_each_entry_safe(entry, n, &efivars->list, list) {
978 struct dentry *dentry, *root = efivarfs_sb->s_root;
980 unsigned long size = 0;
983 len = utf16_strlen(entry->var.VariableName);
985 /* GUID plus trailing NULL */
986 name = kmalloc(len + 38, GFP_ATOMIC);
988 for (i = 0; i < len; i++)
989 name[i] = entry->var.VariableName[i] & 0xFF;
993 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
995 name[len+GUID_LEN] = '\0';
997 inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
999 dentry = d_alloc_name(root, name);
1001 efivars->ops->get_variable(entry->var.VariableName,
1002 &entry->var.VendorGuid,
1003 &entry->var.Attributes,
1007 mutex_lock(&inode->i_mutex);
1008 inode->i_private = entry;
1009 i_size_write(inode, size+4);
1010 mutex_unlock(&inode->i_mutex);
1011 d_add(dentry, inode);
1020 static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
1021 int flags, const char *dev_name, void *data)
1023 return mount_single(fs_type, flags, data, efivarfs_fill_super);
1026 static void efivarfs_kill_sb(struct super_block *sb)
1028 kill_litter_super(sb);
1032 static struct file_system_type efivarfs_type = {
1034 .mount = efivarfs_mount,
1035 .kill_sb = efivarfs_kill_sb,
1038 static const struct inode_operations efivarfs_dir_inode_operations = {
1039 .lookup = simple_lookup,
1040 .unlink = efivarfs_unlink,
1041 .create = efivarfs_create,
1044 static struct pstore_info efi_pstore_info;
1046 #ifdef CONFIG_PSTORE
1048 static int efi_pstore_open(struct pstore_info *psi)
1050 struct efivars *efivars = psi->data;
1052 spin_lock(&efivars->lock);
1053 efivars->walk_entry = list_first_entry(&efivars->list,
1054 struct efivar_entry, list);
1058 static int efi_pstore_close(struct pstore_info *psi)
1060 struct efivars *efivars = psi->data;
1062 spin_unlock(&efivars->lock);
1066 static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
1067 struct timespec *timespec,
1068 char **buf, struct pstore_info *psi)
1070 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1071 struct efivars *efivars = psi->data;
1072 char name[DUMP_NAME_LEN];
1074 unsigned int part, size;
1077 while (&efivars->walk_entry->list != &efivars->list) {
1078 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
1080 for (i = 0; i < DUMP_NAME_LEN; i++) {
1081 name[i] = efivars->walk_entry->var.VariableName[i];
1083 if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
1085 timespec->tv_sec = time;
1086 timespec->tv_nsec = 0;
1087 get_var_data_locked(efivars, &efivars->walk_entry->var);
1088 size = efivars->walk_entry->var.DataSize;
1089 *buf = kmalloc(size, GFP_KERNEL);
1092 memcpy(*buf, efivars->walk_entry->var.Data,
1094 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1095 struct efivar_entry, list);
1099 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1100 struct efivar_entry, list);
1105 static int efi_pstore_write(enum pstore_type_id type,
1106 enum kmsg_dump_reason reason, u64 *id,
1107 unsigned int part, size_t size, struct pstore_info *psi)
1109 char name[DUMP_NAME_LEN];
1110 char stub_name[DUMP_NAME_LEN];
1111 efi_char16_t efi_name[DUMP_NAME_LEN];
1112 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1113 struct efivars *efivars = psi->data;
1114 struct efivar_entry *entry, *found = NULL;
1117 sprintf(stub_name, "dump-type%u-%u-", type, part);
1118 sprintf(name, "%s%lu", stub_name, get_seconds());
1120 spin_lock(&efivars->lock);
1122 for (i = 0; i < DUMP_NAME_LEN; i++)
1123 efi_name[i] = stub_name[i];
1126 * Clean up any entries with the same name
1129 list_for_each_entry(entry, &efivars->list, list) {
1130 get_var_data_locked(efivars, &entry->var);
1132 if (efi_guidcmp(entry->var.VendorGuid, vendor))
1134 if (utf16_strncmp(entry->var.VariableName, efi_name,
1135 utf16_strlen(efi_name)))
1137 /* Needs to be a prefix */
1138 if (entry->var.VariableName[utf16_strlen(efi_name)] == 0)
1143 efivars->ops->set_variable(entry->var.VariableName,
1144 &entry->var.VendorGuid,
1145 PSTORE_EFI_ATTRIBUTES,
1150 list_del(&found->list);
1152 for (i = 0; i < DUMP_NAME_LEN; i++)
1153 efi_name[i] = name[i];
1155 efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
1158 spin_unlock(&efivars->lock);
1161 efivar_unregister(found);
1164 ret = efivar_create_sysfs_entry(efivars,
1165 utf16_strsize(efi_name,
1173 static int efi_pstore_erase(enum pstore_type_id type, u64 id,
1174 struct pstore_info *psi)
1176 efi_pstore_write(type, 0, &id, (unsigned int)id, 0, psi);
1181 static int efi_pstore_open(struct pstore_info *psi)
1186 static int efi_pstore_close(struct pstore_info *psi)
1191 static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
1192 struct timespec *timespec,
1193 char **buf, struct pstore_info *psi)
1198 static int efi_pstore_write(enum pstore_type_id type,
1199 enum kmsg_dump_reason reason, u64 *id,
1200 unsigned int part, size_t size, struct pstore_info *psi)
1205 static int efi_pstore_erase(enum pstore_type_id type, u64 id,
1206 struct pstore_info *psi)
1212 static struct pstore_info efi_pstore_info = {
1213 .owner = THIS_MODULE,
1215 .open = efi_pstore_open,
1216 .close = efi_pstore_close,
1217 .read = efi_pstore_read,
1218 .write = efi_pstore_write,
1219 .erase = efi_pstore_erase,
1222 static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
1223 struct bin_attribute *bin_attr,
1224 char *buf, loff_t pos, size_t count)
1226 struct efi_variable *new_var = (struct efi_variable *)buf;
1227 struct efivars *efivars = bin_attr->private;
1228 struct efivar_entry *search_efivar, *n;
1229 unsigned long strsize1, strsize2;
1230 efi_status_t status = EFI_NOT_FOUND;
1233 if (!capable(CAP_SYS_ADMIN))
1236 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
1237 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
1238 printk(KERN_ERR "efivars: Malformed variable content\n");
1242 spin_lock(&efivars->lock);
1245 * Does this variable already exist?
1247 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
1248 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1249 strsize2 = utf16_strsize(new_var->VariableName, 1024);
1250 if (strsize1 == strsize2 &&
1251 !memcmp(&(search_efivar->var.VariableName),
1252 new_var->VariableName, strsize1) &&
1253 !efi_guidcmp(search_efivar->var.VendorGuid,
1254 new_var->VendorGuid)) {
1260 spin_unlock(&efivars->lock);
1264 /* now *really* create the variable via EFI */
1265 status = efivars->ops->set_variable(new_var->VariableName,
1266 &new_var->VendorGuid,
1267 new_var->Attributes,
1271 if (status != EFI_SUCCESS) {
1272 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1274 spin_unlock(&efivars->lock);
1277 spin_unlock(&efivars->lock);
1279 /* Create the entry in sysfs. Locking is not required here */
1280 status = efivar_create_sysfs_entry(efivars,
1281 utf16_strsize(new_var->VariableName,
1283 new_var->VariableName,
1284 &new_var->VendorGuid);
1286 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
1291 static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
1292 struct bin_attribute *bin_attr,
1293 char *buf, loff_t pos, size_t count)
1295 struct efi_variable *del_var = (struct efi_variable *)buf;
1296 struct efivars *efivars = bin_attr->private;
1297 struct efivar_entry *search_efivar, *n;
1298 unsigned long strsize1, strsize2;
1299 efi_status_t status = EFI_NOT_FOUND;
1302 if (!capable(CAP_SYS_ADMIN))
1305 spin_lock(&efivars->lock);
1308 * Does this variable already exist?
1310 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
1311 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1312 strsize2 = utf16_strsize(del_var->VariableName, 1024);
1313 if (strsize1 == strsize2 &&
1314 !memcmp(&(search_efivar->var.VariableName),
1315 del_var->VariableName, strsize1) &&
1316 !efi_guidcmp(search_efivar->var.VendorGuid,
1317 del_var->VendorGuid)) {
1323 spin_unlock(&efivars->lock);
1326 /* force the Attributes/DataSize to 0 to ensure deletion */
1327 del_var->Attributes = 0;
1328 del_var->DataSize = 0;
1330 status = efivars->ops->set_variable(del_var->VariableName,
1331 &del_var->VendorGuid,
1332 del_var->Attributes,
1336 if (status != EFI_SUCCESS) {
1337 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1339 spin_unlock(&efivars->lock);
1342 list_del(&search_efivar->list);
1343 /* We need to release this lock before unregistering. */
1344 spin_unlock(&efivars->lock);
1345 efivar_unregister(search_efivar);
1347 /* It's dead Jim.... */
1352 * Let's not leave out systab information that snuck into
1353 * the efivars driver
1355 static ssize_t systab_show(struct kobject *kobj,
1356 struct kobj_attribute *attr, char *buf)
1363 if (efi.mps != EFI_INVALID_TABLE_ADDR)
1364 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
1365 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
1366 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
1367 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
1368 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
1369 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
1370 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
1371 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
1372 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
1373 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
1374 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
1375 if (efi.uga != EFI_INVALID_TABLE_ADDR)
1376 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
1381 static struct kobj_attribute efi_attr_systab =
1382 __ATTR(systab, 0400, systab_show, NULL);
1384 static struct attribute *efi_subsys_attrs[] = {
1385 &efi_attr_systab.attr,
1386 NULL, /* maybe more in the future? */
1389 static struct attribute_group efi_subsys_attr_group = {
1390 .attrs = efi_subsys_attrs,
1393 static struct kobject *efi_kobj;
1396 * efivar_create_sysfs_entry()
1398 * variable_name_size = number of bytes required to hold
1399 * variable_name (not counting the NULL
1400 * character at the end.
1401 * efivars->lock is not held on entry or exit.
1402 * Returns 1 on failure, 0 on success
1405 efivar_create_sysfs_entry(struct efivars *efivars,
1406 unsigned long variable_name_size,
1407 efi_char16_t *variable_name,
1408 efi_guid_t *vendor_guid)
1410 int i, short_name_size = variable_name_size / sizeof(efi_char16_t) + 38;
1412 struct efivar_entry *new_efivar;
1414 short_name = kzalloc(short_name_size + 1, GFP_KERNEL);
1415 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
1417 if (!short_name || !new_efivar) {
1423 new_efivar->efivars = efivars;
1424 memcpy(new_efivar->var.VariableName, variable_name,
1425 variable_name_size);
1426 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
1428 /* Convert Unicode to normal chars (assume top bits are 0),
1430 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
1431 short_name[i] = variable_name[i] & 0xFF;
1433 /* This is ugly, but necessary to separate one vendor's
1434 private variables from another's. */
1436 *(short_name + strlen(short_name)) = '-';
1437 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
1439 new_efivar->kobj.kset = efivars->kset;
1440 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
1448 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
1452 spin_lock(&efivars->lock);
1453 list_add(&new_efivar->list, &efivars->list);
1454 spin_unlock(&efivars->lock);
1460 create_efivars_bin_attributes(struct efivars *efivars)
1462 struct bin_attribute *attr;
1466 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1470 attr->attr.name = "new_var";
1471 attr->attr.mode = 0200;
1472 attr->write = efivar_create;
1473 attr->private = efivars;
1474 efivars->new_var = attr;
1477 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1482 attr->attr.name = "del_var";
1483 attr->attr.mode = 0200;
1484 attr->write = efivar_delete;
1485 attr->private = efivars;
1486 efivars->del_var = attr;
1488 sysfs_bin_attr_init(efivars->new_var);
1489 sysfs_bin_attr_init(efivars->del_var);
1492 error = sysfs_create_bin_file(&efivars->kset->kobj,
1495 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
1496 " due to error %d\n", error);
1499 error = sysfs_create_bin_file(&efivars->kset->kobj,
1502 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
1503 " due to error %d\n", error);
1504 sysfs_remove_bin_file(&efivars->kset->kobj,
1511 kfree(efivars->del_var);
1512 efivars->del_var = NULL;
1513 kfree(efivars->new_var);
1514 efivars->new_var = NULL;
1518 void unregister_efivars(struct efivars *efivars)
1520 struct efivar_entry *entry, *n;
1522 list_for_each_entry_safe(entry, n, &efivars->list, list) {
1523 spin_lock(&efivars->lock);
1524 list_del(&entry->list);
1525 spin_unlock(&efivars->lock);
1526 efivar_unregister(entry);
1528 if (efivars->new_var)
1529 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
1530 if (efivars->del_var)
1531 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
1532 kfree(efivars->new_var);
1533 kfree(efivars->del_var);
1534 kobject_put(efivars->kobject);
1535 kset_unregister(efivars->kset);
1537 EXPORT_SYMBOL_GPL(unregister_efivars);
1539 int register_efivars(struct efivars *efivars,
1540 const struct efivar_operations *ops,
1541 struct kobject *parent_kobj)
1543 efi_status_t status = EFI_NOT_FOUND;
1544 efi_guid_t vendor_guid;
1545 efi_char16_t *variable_name;
1546 unsigned long variable_name_size = 1024;
1549 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
1550 if (!variable_name) {
1551 printk(KERN_ERR "efivars: Memory allocation failed.\n");
1555 spin_lock_init(&efivars->lock);
1556 INIT_LIST_HEAD(&efivars->list);
1559 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
1560 if (!efivars->kset) {
1561 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
1566 efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
1567 if (!efivars->kobject) {
1568 pr_err("efivars: Subsystem registration failed.\n");
1570 kset_unregister(efivars->kset);
1575 * Per EFI spec, the maximum storage allocated for both
1576 * the variable name and variable data is 1024 bytes.
1580 variable_name_size = 1024;
1582 status = ops->get_next_variable(&variable_name_size,
1587 efivar_create_sysfs_entry(efivars,
1595 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
1597 status = EFI_NOT_FOUND;
1600 } while (status != EFI_NOT_FOUND);
1602 error = create_efivars_bin_attributes(efivars);
1604 unregister_efivars(efivars);
1606 efivars->efi_pstore_info = efi_pstore_info;
1608 efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
1609 if (efivars->efi_pstore_info.buf) {
1610 efivars->efi_pstore_info.bufsize = 1024;
1611 efivars->efi_pstore_info.data = efivars;
1612 spin_lock_init(&efivars->efi_pstore_info.buf_lock);
1613 pstore_register(&efivars->efi_pstore_info);
1616 register_filesystem(&efivarfs_type);
1619 kfree(variable_name);
1623 EXPORT_SYMBOL_GPL(register_efivars);
1626 * For now we register the efi subsystem with the firmware subsystem
1627 * and the vars subsystem with the efi subsystem. In the future, it
1628 * might make sense to split off the efi subsystem into its own
1629 * driver, but for now only efivars will register with it, so just
1638 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
1644 /* For now we'll register the efi directory at /sys/firmware/efi */
1645 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
1647 printk(KERN_ERR "efivars: Firmware registration failed.\n");
1651 ops.get_variable = efi.get_variable;
1652 ops.set_variable = efi.set_variable;
1653 ops.get_next_variable = efi.get_next_variable;
1654 error = register_efivars(&__efivars, &ops, efi_kobj);
1658 /* Don't forget the systab entry */
1659 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
1662 "efivars: Sysfs attribute export failed with error %d.\n",
1664 goto err_unregister;
1670 unregister_efivars(&__efivars);
1672 kobject_put(efi_kobj);
1680 unregister_efivars(&__efivars);
1681 kobject_put(efi_kobj);
1685 module_init(efivars_init);
1686 module_exit(efivars_exit);