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
100 * Length of a GUID string (strlen("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))
101 * not including trailing NUL
106 * The maximum size of VariableName + Data = 1024
107 * Therefore, it's reasonable to save that much
108 * space in each part of the structure,
109 * and we use a page for reading/writing.
112 struct efi_variable {
113 efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
114 efi_guid_t VendorGuid;
115 unsigned long DataSize;
119 } __attribute__((packed));
121 struct efivar_entry {
122 struct efivars *efivars;
123 struct efi_variable var;
124 struct list_head list;
128 struct efivar_attribute {
129 struct attribute attr;
130 ssize_t (*show) (struct efivar_entry *entry, char *buf);
131 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
134 static struct efivars __efivars;
135 static struct efivar_operations ops;
137 #define PSTORE_EFI_ATTRIBUTES \
138 (EFI_VARIABLE_NON_VOLATILE | \
139 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
140 EFI_VARIABLE_RUNTIME_ACCESS)
142 #define EFIVAR_ATTR(_name, _mode, _show, _store) \
143 struct efivar_attribute efivar_attr_##_name = { \
144 .attr = {.name = __stringify(_name), .mode = _mode}, \
149 #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
150 #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
153 * Prototype for sysfs creation function
156 efivar_create_sysfs_entry(struct efivars *efivars,
157 unsigned long variable_name_size,
158 efi_char16_t *variable_name,
159 efi_guid_t *vendor_guid);
161 /* Return the number of unicode characters in data */
163 utf16_strnlen(efi_char16_t *s, size_t maxlength)
165 unsigned long length = 0;
167 while (*s++ != 0 && length < maxlength)
172 static inline unsigned long
173 utf16_strlen(efi_char16_t *s)
175 return utf16_strnlen(s, ~0UL);
179 * Return the number of bytes is the length of this string
180 * Note: this is NOT the same as the number of unicode characters
182 static inline unsigned long
183 utf16_strsize(efi_char16_t *data, unsigned long maxlength)
185 return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
189 utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
198 if (*a == 0) /* implies *b == 0 */
207 validate_device_path(struct efi_variable *var, int match, u8 *buffer,
210 struct efi_generic_dev_path *node;
213 node = (struct efi_generic_dev_path *)buffer;
215 if (len < sizeof(*node))
218 while (offset <= len - sizeof(*node) &&
219 node->length >= sizeof(*node) &&
220 node->length <= len - offset) {
221 offset += node->length;
223 if ((node->type == EFI_DEV_END_PATH ||
224 node->type == EFI_DEV_END_PATH2) &&
225 node->sub_type == EFI_DEV_END_ENTIRE)
228 node = (struct efi_generic_dev_path *)(buffer + offset);
232 * If we're here then either node->length pointed past the end
233 * of the buffer or we reached the end of the buffer without
234 * finding a device path end node.
240 validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
243 /* An array of 16-bit integers */
251 validate_load_option(struct efi_variable *var, int match, u8 *buffer,
255 int i, desclength = 0, namelen;
257 namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
259 /* Either "Boot" or "Driver" followed by four digits of hex */
260 for (i = match; i < match+4; i++) {
261 if (var->VariableName[i] > 127 ||
262 hex_to_bin(var->VariableName[i] & 0xff) < 0)
266 /* Reject it if there's 4 digits of hex and then further content */
267 if (namelen > match + 4)
270 /* A valid entry must be at least 8 bytes */
274 filepathlength = buffer[4] | buffer[5] << 8;
277 * There's no stored length for the description, so it has to be
280 desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
282 /* Each boot entry must have a descriptor */
287 * If the sum of the length of the description, the claimed filepath
288 * length and the original header are greater than the length of the
289 * variable, it's malformed
291 if ((desclength + filepathlength + 6) > len)
295 * And, finally, check the filepath
297 return validate_device_path(var, match, buffer + desclength + 6,
302 validate_uint16(struct efi_variable *var, int match, u8 *buffer,
305 /* A single 16-bit integer */
313 validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
318 for (i = 0; i < len; i++) {
329 struct variable_validate {
331 bool (*validate)(struct efi_variable *var, int match, u8 *data,
335 static const struct variable_validate variable_validate[] = {
336 { "BootNext", validate_uint16 },
337 { "BootOrder", validate_boot_order },
338 { "DriverOrder", validate_boot_order },
339 { "Boot*", validate_load_option },
340 { "Driver*", validate_load_option },
341 { "ConIn", validate_device_path },
342 { "ConInDev", validate_device_path },
343 { "ConOut", validate_device_path },
344 { "ConOutDev", validate_device_path },
345 { "ErrOut", validate_device_path },
346 { "ErrOutDev", validate_device_path },
347 { "Timeout", validate_uint16 },
348 { "Lang", validate_ascii_string },
349 { "PlatformLang", validate_ascii_string },
354 validate_var(struct efi_variable *var, u8 *data, unsigned long len)
357 u16 *unicode_name = var->VariableName;
359 for (i = 0; variable_validate[i].validate != NULL; i++) {
360 const char *name = variable_validate[i].name;
363 for (match = 0; ; match++) {
364 char c = name[match];
365 u16 u = unicode_name[match];
367 /* All special variables are plain ascii */
371 /* Wildcard in the matching name means we've matched */
373 return variable_validate[i].validate(var,
376 /* Case sensitive match */
380 /* Reached the end of the string while matching */
382 return variable_validate[i].validate(var,
391 get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
395 var->DataSize = 1024;
396 status = efivars->ops->get_variable(var->VariableName,
405 get_var_data(struct efivars *efivars, struct efi_variable *var)
409 spin_lock(&efivars->lock);
410 status = get_var_data_locked(efivars, var);
411 spin_unlock(&efivars->lock);
413 if (status != EFI_SUCCESS) {
414 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
421 efivar_guid_read(struct efivar_entry *entry, char *buf)
423 struct efi_variable *var = &entry->var;
429 efi_guid_unparse(&var->VendorGuid, str);
431 str += sprintf(str, "\n");
437 efivar_attr_read(struct efivar_entry *entry, char *buf)
439 struct efi_variable *var = &entry->var;
446 status = get_var_data(entry->efivars, var);
447 if (status != EFI_SUCCESS)
450 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
451 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
452 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
453 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
454 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
455 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
456 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
457 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
458 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
460 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
461 if (var->Attributes &
462 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
464 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
465 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
466 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
471 efivar_size_read(struct efivar_entry *entry, char *buf)
473 struct efi_variable *var = &entry->var;
480 status = get_var_data(entry->efivars, var);
481 if (status != EFI_SUCCESS)
484 str += sprintf(str, "0x%lx\n", var->DataSize);
489 efivar_data_read(struct efivar_entry *entry, char *buf)
491 struct efi_variable *var = &entry->var;
497 status = get_var_data(entry->efivars, var);
498 if (status != EFI_SUCCESS)
501 memcpy(buf, var->Data, var->DataSize);
502 return var->DataSize;
505 * We allow each variable to be edited via rewriting the
506 * entire efi variable structure.
509 efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
511 struct efi_variable *new_var, *var = &entry->var;
512 struct efivars *efivars = entry->efivars;
513 efi_status_t status = EFI_NOT_FOUND;
515 if (count != sizeof(struct efi_variable))
518 new_var = (struct efi_variable *)buf;
520 * If only updating the variable data, then the name
521 * and guid should remain the same
523 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
524 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
525 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
529 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
530 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
534 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
535 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
536 printk(KERN_ERR "efivars: Malformed variable content\n");
540 spin_lock(&efivars->lock);
541 status = efivars->ops->set_variable(new_var->VariableName,
542 &new_var->VendorGuid,
547 spin_unlock(&efivars->lock);
549 if (status != EFI_SUCCESS) {
550 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
555 memcpy(&entry->var, new_var, count);
560 efivar_show_raw(struct efivar_entry *entry, char *buf)
562 struct efi_variable *var = &entry->var;
568 status = get_var_data(entry->efivars, var);
569 if (status != EFI_SUCCESS)
572 memcpy(buf, var, sizeof(*var));
577 * Generic read/write functions that call the specific functions of
580 static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
583 struct efivar_entry *var = to_efivar_entry(kobj);
584 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
587 if (!capable(CAP_SYS_ADMIN))
590 if (efivar_attr->show) {
591 ret = efivar_attr->show(var, buf);
596 static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
597 const char *buf, size_t count)
599 struct efivar_entry *var = to_efivar_entry(kobj);
600 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
603 if (!capable(CAP_SYS_ADMIN))
606 if (efivar_attr->store)
607 ret = efivar_attr->store(var, buf, count);
612 static const struct sysfs_ops efivar_attr_ops = {
613 .show = efivar_attr_show,
614 .store = efivar_attr_store,
617 static void efivar_release(struct kobject *kobj)
619 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
623 static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
624 static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
625 static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
626 static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
627 static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
629 static struct attribute *def_attrs[] = {
630 &efivar_attr_guid.attr,
631 &efivar_attr_size.attr,
632 &efivar_attr_attributes.attr,
633 &efivar_attr_data.attr,
634 &efivar_attr_raw_var.attr,
638 static struct kobj_type efivar_ktype = {
639 .release = efivar_release,
640 .sysfs_ops = &efivar_attr_ops,
641 .default_attrs = def_attrs,
645 efivar_unregister(struct efivar_entry *var)
647 kobject_put(&var->kobj);
650 static int efivarfs_file_open(struct inode *inode, struct file *file)
652 file->private_data = inode->i_private;
656 static ssize_t efivarfs_file_write(struct file *file,
657 const char __user *userbuf, size_t count, loff_t *ppos)
659 struct efivar_entry *var = file->private_data;
660 struct efivars *efivars;
664 struct inode *inode = file->f_mapping->host;
665 int datasize = count - sizeof(attributes);
666 unsigned long newdatasize;
668 if (count < sizeof(attributes))
671 data = kmalloc(datasize, GFP_KERNEL);
676 efivars = var->efivars;
678 if (copy_from_user(&attributes, userbuf, sizeof(attributes))) {
683 if (attributes & ~(EFI_VARIABLE_MASK)) {
688 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
693 if (validate_var(&var->var, data, datasize) == false) {
699 * The lock here protects the get_variable call, the conditional
700 * set_variable call, and removal of the variable from the efivars
701 * list (in the case of an authenticated delete).
703 spin_lock(&efivars->lock);
705 status = efivars->ops->set_variable(var->var.VariableName,
706 &var->var.VendorGuid,
707 attributes, datasize,
710 if (status != EFI_SUCCESS) {
711 spin_unlock(&efivars->lock);
715 case EFI_INVALID_PARAMETER:
718 case EFI_OUT_OF_RESOURCES:
721 case EFI_DEVICE_ERROR:
724 case EFI_WRITE_PROTECTED:
727 case EFI_SECURITY_VIOLATION:
740 * Writing to the variable may have caused a change in size (which
741 * could either be an append or an overwrite), or the variable to be
742 * deleted. Perform a GetVariable() so we can tell what actually
746 status = efivars->ops->get_variable(var->var.VariableName,
747 &var->var.VendorGuid,
751 if (status == EFI_BUFFER_TOO_SMALL) {
752 spin_unlock(&efivars->lock);
753 mutex_lock(&inode->i_mutex);
754 i_size_write(inode, newdatasize + sizeof(attributes));
755 mutex_unlock(&inode->i_mutex);
757 } else if (status == EFI_NOT_FOUND) {
758 list_del(&var->list);
759 spin_unlock(&efivars->lock);
760 efivar_unregister(var);
762 dput(file->f_dentry);
765 spin_unlock(&efivars->lock);
766 pr_warn("efivarfs: inconsistent EFI variable implementation? "
767 "status = %lx\n", status);
776 static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
777 size_t count, loff_t *ppos)
779 struct efivar_entry *var = file->private_data;
780 struct efivars *efivars = var->efivars;
782 unsigned long datasize = 0;
787 spin_lock(&efivars->lock);
788 status = efivars->ops->get_variable(var->var.VariableName,
789 &var->var.VendorGuid,
790 &attributes, &datasize, NULL);
791 spin_unlock(&efivars->lock);
793 if (status != EFI_BUFFER_TOO_SMALL)
796 data = kmalloc(datasize + 4, GFP_KERNEL);
801 spin_lock(&efivars->lock);
802 status = efivars->ops->get_variable(var->var.VariableName,
803 &var->var.VendorGuid,
804 &attributes, &datasize,
806 spin_unlock(&efivars->lock);
808 if (status != EFI_SUCCESS)
811 memcpy(data, &attributes, 4);
812 size = simple_read_from_buffer(userbuf, count, ppos,
820 static void efivarfs_evict_inode(struct inode *inode)
825 static const struct super_operations efivarfs_ops = {
826 .statfs = simple_statfs,
827 .drop_inode = generic_delete_inode,
828 .evict_inode = efivarfs_evict_inode,
829 .show_options = generic_show_options,
832 static struct super_block *efivarfs_sb;
834 static const struct inode_operations efivarfs_dir_inode_operations;
836 static const struct file_operations efivarfs_file_operations = {
837 .open = efivarfs_file_open,
838 .read = efivarfs_file_read,
839 .write = efivarfs_file_write,
843 static struct inode *efivarfs_get_inode(struct super_block *sb,
844 const struct inode *dir, int mode, dev_t dev)
846 struct inode *inode = new_inode(sb);
849 inode->i_ino = get_next_ino();
850 inode->i_uid = inode->i_gid = 0;
851 inode->i_mode = mode;
852 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
853 switch (mode & S_IFMT) {
855 inode->i_fop = &efivarfs_file_operations;
858 inode->i_op = &efivarfs_dir_inode_operations;
859 inode->i_fop = &simple_dir_operations;
867 static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
869 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
870 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
871 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
872 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
873 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
874 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
875 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
876 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
877 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
878 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
879 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
880 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
881 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
882 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
883 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
884 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
887 static int efivarfs_create(struct inode *dir, struct dentry *dentry,
888 umode_t mode, bool excl)
891 struct efivars *efivars = &__efivars;
892 struct efivar_entry *var;
893 int namelen, i = 0, err = 0;
896 * We need a GUID, plus at least one letter for the variable name,
897 * plus the '-' separator
899 if (dentry->d_name.len < GUID_LEN + 2)
902 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
906 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
912 /* length of the variable name itself: remove GUID and separator */
913 namelen = dentry->d_name.len - GUID_LEN - 1;
915 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
916 &var->var.VendorGuid);
918 for (i = 0; i < namelen; i++)
919 var->var.VariableName[i] = dentry->d_name.name[i];
921 var->var.VariableName[i] = '\0';
923 inode->i_private = var;
924 var->efivars = efivars;
925 var->kobj.kset = efivars->kset;
927 err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
928 dentry->d_name.name);
932 kobject_uevent(&var->kobj, KOBJ_ADD);
933 spin_lock(&efivars->lock);
934 list_add(&var->list, &efivars->list);
935 spin_unlock(&efivars->lock);
936 d_instantiate(dentry, inode);
946 static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
948 struct efivar_entry *var = dentry->d_inode->i_private;
949 struct efivars *efivars = var->efivars;
952 spin_lock(&efivars->lock);
954 status = efivars->ops->set_variable(var->var.VariableName,
955 &var->var.VendorGuid,
958 if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
959 list_del(&var->list);
960 spin_unlock(&efivars->lock);
961 efivar_unregister(var);
967 spin_unlock(&efivars->lock);
971 int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
973 struct inode *inode = NULL;
975 struct efivar_entry *entry, *n;
976 struct efivars *efivars = &__efivars;
981 sb->s_maxbytes = MAX_LFS_FILESIZE;
982 sb->s_blocksize = PAGE_CACHE_SIZE;
983 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
984 sb->s_magic = PSTOREFS_MAGIC;
985 sb->s_op = &efivarfs_ops;
988 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
991 inode->i_op = &efivarfs_dir_inode_operations;
993 root = d_make_root(inode);
998 list_for_each_entry_safe(entry, n, &efivars->list, list) {
999 struct dentry *dentry, *root = efivarfs_sb->s_root;
1000 unsigned long size = 0;
1005 len = utf16_strlen(entry->var.VariableName);
1007 /* name, plus '-', plus GUID, plus NUL*/
1008 name = kmalloc(len + 1 + GUID_LEN + 1, GFP_ATOMIC);
1012 for (i = 0; i < len; i++)
1013 name[i] = entry->var.VariableName[i] & 0xFF;
1017 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
1019 name[len+GUID_LEN+1] = '\0';
1021 inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
1026 dentry = d_alloc_name(root, name);
1030 /* copied by the above to local storage in the dentry. */
1033 spin_lock(&efivars->lock);
1034 efivars->ops->get_variable(entry->var.VariableName,
1035 &entry->var.VendorGuid,
1036 &entry->var.Attributes,
1039 spin_unlock(&efivars->lock);
1041 mutex_lock(&inode->i_mutex);
1042 inode->i_private = entry;
1043 i_size_write(inode, size+4);
1044 mutex_unlock(&inode->i_mutex);
1045 d_add(dentry, inode);
1058 static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
1059 int flags, const char *dev_name, void *data)
1061 return mount_single(fs_type, flags, data, efivarfs_fill_super);
1064 static void efivarfs_kill_sb(struct super_block *sb)
1066 kill_litter_super(sb);
1070 static struct file_system_type efivarfs_type = {
1072 .mount = efivarfs_mount,
1073 .kill_sb = efivarfs_kill_sb,
1076 static const struct inode_operations efivarfs_dir_inode_operations = {
1077 .lookup = simple_lookup,
1078 .unlink = efivarfs_unlink,
1079 .create = efivarfs_create,
1082 static struct pstore_info efi_pstore_info;
1084 #ifdef CONFIG_PSTORE
1086 static int efi_pstore_open(struct pstore_info *psi)
1088 struct efivars *efivars = psi->data;
1090 spin_lock(&efivars->lock);
1091 efivars->walk_entry = list_first_entry(&efivars->list,
1092 struct efivar_entry, list);
1096 static int efi_pstore_close(struct pstore_info *psi)
1098 struct efivars *efivars = psi->data;
1100 spin_unlock(&efivars->lock);
1104 static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
1105 struct timespec *timespec,
1106 char **buf, struct pstore_info *psi)
1108 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1109 struct efivars *efivars = psi->data;
1110 char name[DUMP_NAME_LEN];
1112 unsigned int part, size;
1115 while (&efivars->walk_entry->list != &efivars->list) {
1116 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
1118 for (i = 0; i < DUMP_NAME_LEN; i++) {
1119 name[i] = efivars->walk_entry->var.VariableName[i];
1121 if (sscanf(name, "dump-type%u-%u-%lu", type, &part, &time) == 3) {
1123 timespec->tv_sec = time;
1124 timespec->tv_nsec = 0;
1125 get_var_data_locked(efivars, &efivars->walk_entry->var);
1126 size = efivars->walk_entry->var.DataSize;
1127 *buf = kmalloc(size, GFP_KERNEL);
1130 memcpy(*buf, efivars->walk_entry->var.Data,
1132 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1133 struct efivar_entry, list);
1137 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1138 struct efivar_entry, list);
1143 static int efi_pstore_write(enum pstore_type_id type,
1144 enum kmsg_dump_reason reason, u64 *id,
1145 unsigned int part, size_t size, struct pstore_info *psi)
1147 char name[DUMP_NAME_LEN];
1148 char stub_name[DUMP_NAME_LEN];
1149 efi_char16_t efi_name[DUMP_NAME_LEN];
1150 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1151 struct efivars *efivars = psi->data;
1152 struct efivar_entry *entry, *found = NULL;
1155 sprintf(stub_name, "dump-type%u-%u-", type, part);
1156 sprintf(name, "%s%lu", stub_name, get_seconds());
1158 spin_lock(&efivars->lock);
1160 for (i = 0; i < DUMP_NAME_LEN; i++)
1161 efi_name[i] = stub_name[i];
1164 * Clean up any entries with the same name
1167 list_for_each_entry(entry, &efivars->list, list) {
1168 get_var_data_locked(efivars, &entry->var);
1170 if (efi_guidcmp(entry->var.VendorGuid, vendor))
1172 if (utf16_strncmp(entry->var.VariableName, efi_name,
1173 utf16_strlen(efi_name)))
1175 /* Needs to be a prefix */
1176 if (entry->var.VariableName[utf16_strlen(efi_name)] == 0)
1181 efivars->ops->set_variable(entry->var.VariableName,
1182 &entry->var.VendorGuid,
1183 PSTORE_EFI_ATTRIBUTES,
1188 list_del(&found->list);
1190 for (i = 0; i < DUMP_NAME_LEN; i++)
1191 efi_name[i] = name[i];
1193 efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
1196 spin_unlock(&efivars->lock);
1199 efivar_unregister(found);
1202 ret = efivar_create_sysfs_entry(efivars,
1203 utf16_strsize(efi_name,
1211 static int efi_pstore_erase(enum pstore_type_id type, u64 id,
1212 struct pstore_info *psi)
1214 efi_pstore_write(type, 0, &id, (unsigned int)id, 0, psi);
1219 static int efi_pstore_open(struct pstore_info *psi)
1224 static int efi_pstore_close(struct pstore_info *psi)
1229 static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
1230 struct timespec *timespec,
1231 char **buf, struct pstore_info *psi)
1236 static int efi_pstore_write(enum pstore_type_id type,
1237 enum kmsg_dump_reason reason, u64 *id,
1238 unsigned int part, size_t size, struct pstore_info *psi)
1243 static int efi_pstore_erase(enum pstore_type_id type, u64 id,
1244 struct pstore_info *psi)
1250 static struct pstore_info efi_pstore_info = {
1251 .owner = THIS_MODULE,
1253 .open = efi_pstore_open,
1254 .close = efi_pstore_close,
1255 .read = efi_pstore_read,
1256 .write = efi_pstore_write,
1257 .erase = efi_pstore_erase,
1260 static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
1261 struct bin_attribute *bin_attr,
1262 char *buf, loff_t pos, size_t count)
1264 struct efi_variable *new_var = (struct efi_variable *)buf;
1265 struct efivars *efivars = bin_attr->private;
1266 struct efivar_entry *search_efivar, *n;
1267 unsigned long strsize1, strsize2;
1268 efi_status_t status = EFI_NOT_FOUND;
1271 if (!capable(CAP_SYS_ADMIN))
1274 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
1275 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
1276 printk(KERN_ERR "efivars: Malformed variable content\n");
1280 spin_lock(&efivars->lock);
1283 * Does this variable already exist?
1285 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
1286 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1287 strsize2 = utf16_strsize(new_var->VariableName, 1024);
1288 if (strsize1 == strsize2 &&
1289 !memcmp(&(search_efivar->var.VariableName),
1290 new_var->VariableName, strsize1) &&
1291 !efi_guidcmp(search_efivar->var.VendorGuid,
1292 new_var->VendorGuid)) {
1298 spin_unlock(&efivars->lock);
1302 /* now *really* create the variable via EFI */
1303 status = efivars->ops->set_variable(new_var->VariableName,
1304 &new_var->VendorGuid,
1305 new_var->Attributes,
1309 if (status != EFI_SUCCESS) {
1310 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1312 spin_unlock(&efivars->lock);
1315 spin_unlock(&efivars->lock);
1317 /* Create the entry in sysfs. Locking is not required here */
1318 status = efivar_create_sysfs_entry(efivars,
1319 utf16_strsize(new_var->VariableName,
1321 new_var->VariableName,
1322 &new_var->VendorGuid);
1324 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
1329 static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
1330 struct bin_attribute *bin_attr,
1331 char *buf, loff_t pos, size_t count)
1333 struct efi_variable *del_var = (struct efi_variable *)buf;
1334 struct efivars *efivars = bin_attr->private;
1335 struct efivar_entry *search_efivar, *n;
1336 unsigned long strsize1, strsize2;
1337 efi_status_t status = EFI_NOT_FOUND;
1340 if (!capable(CAP_SYS_ADMIN))
1343 spin_lock(&efivars->lock);
1346 * Does this variable already exist?
1348 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
1349 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1350 strsize2 = utf16_strsize(del_var->VariableName, 1024);
1351 if (strsize1 == strsize2 &&
1352 !memcmp(&(search_efivar->var.VariableName),
1353 del_var->VariableName, strsize1) &&
1354 !efi_guidcmp(search_efivar->var.VendorGuid,
1355 del_var->VendorGuid)) {
1361 spin_unlock(&efivars->lock);
1364 /* force the Attributes/DataSize to 0 to ensure deletion */
1365 del_var->Attributes = 0;
1366 del_var->DataSize = 0;
1368 status = efivars->ops->set_variable(del_var->VariableName,
1369 &del_var->VendorGuid,
1370 del_var->Attributes,
1374 if (status != EFI_SUCCESS) {
1375 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1377 spin_unlock(&efivars->lock);
1380 list_del(&search_efivar->list);
1381 /* We need to release this lock before unregistering. */
1382 spin_unlock(&efivars->lock);
1383 efivar_unregister(search_efivar);
1385 /* It's dead Jim.... */
1390 * Let's not leave out systab information that snuck into
1391 * the efivars driver
1393 static ssize_t systab_show(struct kobject *kobj,
1394 struct kobj_attribute *attr, char *buf)
1401 if (efi.mps != EFI_INVALID_TABLE_ADDR)
1402 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
1403 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
1404 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
1405 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
1406 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
1407 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
1408 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
1409 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
1410 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
1411 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
1412 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
1413 if (efi.uga != EFI_INVALID_TABLE_ADDR)
1414 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
1419 static struct kobj_attribute efi_attr_systab =
1420 __ATTR(systab, 0400, systab_show, NULL);
1422 static struct attribute *efi_subsys_attrs[] = {
1423 &efi_attr_systab.attr,
1424 NULL, /* maybe more in the future? */
1427 static struct attribute_group efi_subsys_attr_group = {
1428 .attrs = efi_subsys_attrs,
1431 static struct kobject *efi_kobj;
1434 * efivar_create_sysfs_entry()
1436 * variable_name_size = number of bytes required to hold
1437 * variable_name (not counting the NULL
1438 * character at the end.
1439 * efivars->lock is not held on entry or exit.
1440 * Returns 1 on failure, 0 on success
1443 efivar_create_sysfs_entry(struct efivars *efivars,
1444 unsigned long variable_name_size,
1445 efi_char16_t *variable_name,
1446 efi_guid_t *vendor_guid)
1448 int i, short_name_size;
1450 struct efivar_entry *new_efivar;
1453 * Length of the variable bytes in ASCII, plus the '-' separator,
1454 * plus the GUID, plus trailing NUL
1456 short_name_size = variable_name_size / sizeof(efi_char16_t)
1459 short_name = kzalloc(short_name_size, GFP_KERNEL);
1460 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
1462 if (!short_name || !new_efivar) {
1468 new_efivar->efivars = efivars;
1469 memcpy(new_efivar->var.VariableName, variable_name,
1470 variable_name_size);
1471 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
1473 /* Convert Unicode to normal chars (assume top bits are 0),
1475 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
1476 short_name[i] = variable_name[i] & 0xFF;
1478 /* This is ugly, but necessary to separate one vendor's
1479 private variables from another's. */
1481 *(short_name + strlen(short_name)) = '-';
1482 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
1484 new_efivar->kobj.kset = efivars->kset;
1485 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
1493 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
1497 spin_lock(&efivars->lock);
1498 list_add(&new_efivar->list, &efivars->list);
1499 spin_unlock(&efivars->lock);
1505 create_efivars_bin_attributes(struct efivars *efivars)
1507 struct bin_attribute *attr;
1511 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1515 attr->attr.name = "new_var";
1516 attr->attr.mode = 0200;
1517 attr->write = efivar_create;
1518 attr->private = efivars;
1519 efivars->new_var = attr;
1522 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1527 attr->attr.name = "del_var";
1528 attr->attr.mode = 0200;
1529 attr->write = efivar_delete;
1530 attr->private = efivars;
1531 efivars->del_var = attr;
1533 sysfs_bin_attr_init(efivars->new_var);
1534 sysfs_bin_attr_init(efivars->del_var);
1537 error = sysfs_create_bin_file(&efivars->kset->kobj,
1540 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
1541 " due to error %d\n", error);
1544 error = sysfs_create_bin_file(&efivars->kset->kobj,
1547 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
1548 " due to error %d\n", error);
1549 sysfs_remove_bin_file(&efivars->kset->kobj,
1556 kfree(efivars->del_var);
1557 efivars->del_var = NULL;
1558 kfree(efivars->new_var);
1559 efivars->new_var = NULL;
1563 void unregister_efivars(struct efivars *efivars)
1565 struct efivar_entry *entry, *n;
1567 list_for_each_entry_safe(entry, n, &efivars->list, list) {
1568 spin_lock(&efivars->lock);
1569 list_del(&entry->list);
1570 spin_unlock(&efivars->lock);
1571 efivar_unregister(entry);
1573 if (efivars->new_var)
1574 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
1575 if (efivars->del_var)
1576 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
1577 kfree(efivars->new_var);
1578 kfree(efivars->del_var);
1579 kobject_put(efivars->kobject);
1580 kset_unregister(efivars->kset);
1582 EXPORT_SYMBOL_GPL(unregister_efivars);
1584 int register_efivars(struct efivars *efivars,
1585 const struct efivar_operations *ops,
1586 struct kobject *parent_kobj)
1588 efi_status_t status = EFI_NOT_FOUND;
1589 efi_guid_t vendor_guid;
1590 efi_char16_t *variable_name;
1591 unsigned long variable_name_size = 1024;
1594 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
1595 if (!variable_name) {
1596 printk(KERN_ERR "efivars: Memory allocation failed.\n");
1600 spin_lock_init(&efivars->lock);
1601 INIT_LIST_HEAD(&efivars->list);
1604 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
1605 if (!efivars->kset) {
1606 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
1611 efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
1612 if (!efivars->kobject) {
1613 pr_err("efivars: Subsystem registration failed.\n");
1615 kset_unregister(efivars->kset);
1620 * Per EFI spec, the maximum storage allocated for both
1621 * the variable name and variable data is 1024 bytes.
1625 variable_name_size = 1024;
1627 status = ops->get_next_variable(&variable_name_size,
1632 efivar_create_sysfs_entry(efivars,
1640 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
1642 status = EFI_NOT_FOUND;
1645 } while (status != EFI_NOT_FOUND);
1647 error = create_efivars_bin_attributes(efivars);
1649 unregister_efivars(efivars);
1651 efivars->efi_pstore_info = efi_pstore_info;
1653 efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
1654 if (efivars->efi_pstore_info.buf) {
1655 efivars->efi_pstore_info.bufsize = 1024;
1656 efivars->efi_pstore_info.data = efivars;
1657 spin_lock_init(&efivars->efi_pstore_info.buf_lock);
1658 pstore_register(&efivars->efi_pstore_info);
1661 register_filesystem(&efivarfs_type);
1664 kfree(variable_name);
1668 EXPORT_SYMBOL_GPL(register_efivars);
1671 * For now we register the efi subsystem with the firmware subsystem
1672 * and the vars subsystem with the efi subsystem. In the future, it
1673 * might make sense to split off the efi subsystem into its own
1674 * driver, but for now only efivars will register with it, so just
1683 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
1689 /* For now we'll register the efi directory at /sys/firmware/efi */
1690 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
1692 printk(KERN_ERR "efivars: Firmware registration failed.\n");
1696 ops.get_variable = efi.get_variable;
1697 ops.set_variable = efi.set_variable;
1698 ops.get_next_variable = efi.get_next_variable;
1699 error = register_efivars(&__efivars, &ops, efi_kobj);
1703 /* Don't forget the systab entry */
1704 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
1707 "efivars: Sysfs attribute export failed with error %d.\n",
1709 goto err_unregister;
1715 unregister_efivars(&__efivars);
1717 kobject_put(efi_kobj);
1725 unregister_efivars(&__efivars);
1726 kobject_put(efi_kobj);
1730 module_init(efivars_init);
1731 module_exit(efivars_exit);