2 * EFI Variables - efivars.c
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
27 * remove check for efi_enabled in exit
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
39 * fix locking per Peter Chubb's findings
42 * move uuid_unparse() to include/asm-ia64/efi.h:efi_guid_unparse()
45 * use list_for_each_safe when deleting vars.
46 * remove ifdef CONFIG_SMP around include <linux/smp.h>
50 * Moved vars from /proc/efi to /proc/efi/vars, and made
51 * efi.c own the /proc/efi directory.
55 * At the request of Stephane, moved ownership of /proc/efi
56 * to efi.c, and now efivars lives under /proc/efi/vars.
59 * Feedback received from Stephane Eranian incorporated.
60 * efivar_write() checks copy_from_user() return value.
61 * efivar_read/write() returns proper errno.
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>
82 #include <linux/ctype.h>
85 #include <linux/ramfs.h>
86 #include <linux/pagemap.h>
88 #include <asm/uaccess.h>
90 #define EFIVARS_VERSION "0.08"
91 #define EFIVARS_DATE "2004-May-17"
94 MODULE_DESCRIPTION("sysfs interface to EFI Variables");
95 MODULE_LICENSE("GPL");
96 MODULE_VERSION(EFIVARS_VERSION);
98 #define DUMP_NAME_LEN 52
101 * Length of a GUID string (strlen("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))
102 * not including trailing NUL
106 static bool efivars_pstore_disable =
107 IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
109 module_param_named(pstore_disable, efivars_pstore_disable, bool, 0644);
112 * The maximum size of VariableName + Data = 1024
113 * Therefore, it's reasonable to save that much
114 * space in each part of the structure,
115 * and we use a page for reading/writing.
118 struct efi_variable {
119 efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
120 efi_guid_t VendorGuid;
121 unsigned long DataSize;
125 } __attribute__((packed));
127 struct efivar_entry {
128 struct efivars *efivars;
129 struct efi_variable var;
130 struct list_head list;
134 struct efivar_attribute {
135 struct attribute attr;
136 ssize_t (*show) (struct efivar_entry *entry, char *buf);
137 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
140 static struct efivars __efivars;
141 static struct efivar_operations ops;
143 #define PSTORE_EFI_ATTRIBUTES \
144 (EFI_VARIABLE_NON_VOLATILE | \
145 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
146 EFI_VARIABLE_RUNTIME_ACCESS)
148 #define EFIVAR_ATTR(_name, _mode, _show, _store) \
149 struct efivar_attribute efivar_attr_##_name = { \
150 .attr = {.name = __stringify(_name), .mode = _mode}, \
155 #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
156 #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
159 * Prototype for sysfs creation function
162 efivar_create_sysfs_entry(struct efivars *efivars,
163 unsigned long variable_name_size,
164 efi_char16_t *variable_name,
165 efi_guid_t *vendor_guid);
168 * Prototype for workqueue functions updating sysfs entry
171 static void efivar_update_sysfs_entries(struct work_struct *);
172 static DECLARE_WORK(efivar_work, efivar_update_sysfs_entries);
173 static bool efivar_wq_enabled = true;
175 /* Return the number of unicode characters in data */
177 utf16_strnlen(efi_char16_t *s, size_t maxlength)
179 unsigned long length = 0;
181 while (*s++ != 0 && length < maxlength)
186 static inline unsigned long
187 utf16_strlen(efi_char16_t *s)
189 return utf16_strnlen(s, ~0UL);
193 * Return the number of bytes is the length of this string
194 * Note: this is NOT the same as the number of unicode characters
196 static inline unsigned long
197 utf16_strsize(efi_char16_t *data, unsigned long maxlength)
199 return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t);
203 utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len)
212 if (*a == 0) /* implies *b == 0 */
221 validate_device_path(struct efi_variable *var, int match, u8 *buffer,
224 struct efi_generic_dev_path *node;
227 node = (struct efi_generic_dev_path *)buffer;
229 if (len < sizeof(*node))
232 while (offset <= len - sizeof(*node) &&
233 node->length >= sizeof(*node) &&
234 node->length <= len - offset) {
235 offset += node->length;
237 if ((node->type == EFI_DEV_END_PATH ||
238 node->type == EFI_DEV_END_PATH2) &&
239 node->sub_type == EFI_DEV_END_ENTIRE)
242 node = (struct efi_generic_dev_path *)(buffer + offset);
246 * If we're here then either node->length pointed past the end
247 * of the buffer or we reached the end of the buffer without
248 * finding a device path end node.
254 validate_boot_order(struct efi_variable *var, int match, u8 *buffer,
257 /* An array of 16-bit integers */
265 validate_load_option(struct efi_variable *var, int match, u8 *buffer,
269 int i, desclength = 0, namelen;
271 namelen = utf16_strnlen(var->VariableName, sizeof(var->VariableName));
273 /* Either "Boot" or "Driver" followed by four digits of hex */
274 for (i = match; i < match+4; i++) {
275 if (var->VariableName[i] > 127 ||
276 hex_to_bin(var->VariableName[i] & 0xff) < 0)
280 /* Reject it if there's 4 digits of hex and then further content */
281 if (namelen > match + 4)
284 /* A valid entry must be at least 8 bytes */
288 filepathlength = buffer[4] | buffer[5] << 8;
291 * There's no stored length for the description, so it has to be
294 desclength = utf16_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
296 /* Each boot entry must have a descriptor */
301 * If the sum of the length of the description, the claimed filepath
302 * length and the original header are greater than the length of the
303 * variable, it's malformed
305 if ((desclength + filepathlength + 6) > len)
309 * And, finally, check the filepath
311 return validate_device_path(var, match, buffer + desclength + 6,
316 validate_uint16(struct efi_variable *var, int match, u8 *buffer,
319 /* A single 16-bit integer */
327 validate_ascii_string(struct efi_variable *var, int match, u8 *buffer,
332 for (i = 0; i < len; i++) {
343 struct variable_validate {
345 bool (*validate)(struct efi_variable *var, int match, u8 *data,
349 static const struct variable_validate variable_validate[] = {
350 { "BootNext", validate_uint16 },
351 { "BootOrder", validate_boot_order },
352 { "DriverOrder", validate_boot_order },
353 { "Boot*", validate_load_option },
354 { "Driver*", validate_load_option },
355 { "ConIn", validate_device_path },
356 { "ConInDev", validate_device_path },
357 { "ConOut", validate_device_path },
358 { "ConOutDev", validate_device_path },
359 { "ErrOut", validate_device_path },
360 { "ErrOutDev", validate_device_path },
361 { "Timeout", validate_uint16 },
362 { "Lang", validate_ascii_string },
363 { "PlatformLang", validate_ascii_string },
368 validate_var(struct efi_variable *var, u8 *data, unsigned long len)
371 u16 *unicode_name = var->VariableName;
373 for (i = 0; variable_validate[i].validate != NULL; i++) {
374 const char *name = variable_validate[i].name;
377 for (match = 0; ; match++) {
378 char c = name[match];
379 u16 u = unicode_name[match];
381 /* All special variables are plain ascii */
385 /* Wildcard in the matching name means we've matched */
387 return variable_validate[i].validate(var,
390 /* Case sensitive match */
394 /* Reached the end of the string while matching */
396 return variable_validate[i].validate(var,
405 get_var_data_locked(struct efivars *efivars, struct efi_variable *var)
409 var->DataSize = 1024;
410 status = efivars->ops->get_variable(var->VariableName,
419 get_var_data(struct efivars *efivars, struct efi_variable *var)
424 spin_lock_irqsave(&efivars->lock, flags);
425 status = get_var_data_locked(efivars, var);
426 spin_unlock_irqrestore(&efivars->lock, flags);
428 if (status != EFI_SUCCESS) {
429 printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
436 check_var_size_locked(struct efivars *efivars, u32 attributes,
439 u64 storage_size, remaining_size, max_size;
441 const struct efivar_operations *fops = efivars->ops;
443 if (!efivars->ops->query_variable_info)
444 return EFI_UNSUPPORTED;
446 status = fops->query_variable_info(attributes, &storage_size,
447 &remaining_size, &max_size);
449 if (status != EFI_SUCCESS)
452 if (!storage_size || size > remaining_size || size > max_size ||
453 (remaining_size - size) < (storage_size / 2))
454 return EFI_OUT_OF_RESOURCES;
461 check_var_size(struct efivars *efivars, u32 attributes, unsigned long size)
466 spin_lock_irqsave(&efivars->lock, flags);
467 status = check_var_size_locked(efivars, attributes, size);
468 spin_unlock_irqrestore(&efivars->lock, flags);
474 efivar_guid_read(struct efivar_entry *entry, char *buf)
476 struct efi_variable *var = &entry->var;
482 efi_guid_unparse(&var->VendorGuid, str);
484 str += sprintf(str, "\n");
490 efivar_attr_read(struct efivar_entry *entry, char *buf)
492 struct efi_variable *var = &entry->var;
499 status = get_var_data(entry->efivars, var);
500 if (status != EFI_SUCCESS)
503 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
504 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
505 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
506 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
507 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
508 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
509 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
510 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
511 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
513 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
514 if (var->Attributes &
515 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
517 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
518 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
519 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
524 efivar_size_read(struct efivar_entry *entry, char *buf)
526 struct efi_variable *var = &entry->var;
533 status = get_var_data(entry->efivars, var);
534 if (status != EFI_SUCCESS)
537 str += sprintf(str, "0x%lx\n", var->DataSize);
542 efivar_data_read(struct efivar_entry *entry, char *buf)
544 struct efi_variable *var = &entry->var;
550 status = get_var_data(entry->efivars, var);
551 if (status != EFI_SUCCESS)
554 memcpy(buf, var->Data, var->DataSize);
555 return var->DataSize;
558 * We allow each variable to be edited via rewriting the
559 * entire efi variable structure.
562 efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
564 struct efi_variable *new_var, *var = &entry->var;
565 struct efivars *efivars = entry->efivars;
566 efi_status_t status = EFI_NOT_FOUND;
568 if (count != sizeof(struct efi_variable))
571 new_var = (struct efi_variable *)buf;
573 * If only updating the variable data, then the name
574 * and guid should remain the same
576 if (memcmp(new_var->VariableName, var->VariableName, sizeof(var->VariableName)) ||
577 efi_guidcmp(new_var->VendorGuid, var->VendorGuid)) {
578 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
582 if ((new_var->DataSize <= 0) || (new_var->Attributes == 0)){
583 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
587 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
588 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
589 printk(KERN_ERR "efivars: Malformed variable content\n");
593 spin_lock_irq(&efivars->lock);
595 status = check_var_size_locked(efivars, new_var->Attributes,
596 new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
598 if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
599 status = efivars->ops->set_variable(new_var->VariableName,
600 &new_var->VendorGuid,
605 spin_unlock_irq(&efivars->lock);
607 if (status != EFI_SUCCESS) {
608 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
613 memcpy(&entry->var, new_var, count);
618 efivar_show_raw(struct efivar_entry *entry, char *buf)
620 struct efi_variable *var = &entry->var;
626 status = get_var_data(entry->efivars, var);
627 if (status != EFI_SUCCESS)
630 memcpy(buf, var, sizeof(*var));
635 * Generic read/write functions that call the specific functions of
638 static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
641 struct efivar_entry *var = to_efivar_entry(kobj);
642 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
645 if (!capable(CAP_SYS_ADMIN))
648 if (efivar_attr->show) {
649 ret = efivar_attr->show(var, buf);
654 static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
655 const char *buf, size_t count)
657 struct efivar_entry *var = to_efivar_entry(kobj);
658 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
661 if (!capable(CAP_SYS_ADMIN))
664 if (efivar_attr->store)
665 ret = efivar_attr->store(var, buf, count);
670 static const struct sysfs_ops efivar_attr_ops = {
671 .show = efivar_attr_show,
672 .store = efivar_attr_store,
675 static void efivar_release(struct kobject *kobj)
677 struct efivar_entry *var = container_of(kobj, struct efivar_entry, kobj);
681 static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
682 static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
683 static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
684 static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
685 static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
687 static struct attribute *def_attrs[] = {
688 &efivar_attr_guid.attr,
689 &efivar_attr_size.attr,
690 &efivar_attr_attributes.attr,
691 &efivar_attr_data.attr,
692 &efivar_attr_raw_var.attr,
696 static struct kobj_type efivar_ktype = {
697 .release = efivar_release,
698 .sysfs_ops = &efivar_attr_ops,
699 .default_attrs = def_attrs,
703 efivar_unregister(struct efivar_entry *var)
705 kobject_put(&var->kobj);
708 static int efivarfs_file_open(struct inode *inode, struct file *file)
710 file->private_data = inode->i_private;
714 static int efi_status_to_err(efi_status_t status)
719 case EFI_INVALID_PARAMETER:
722 case EFI_OUT_OF_RESOURCES:
725 case EFI_DEVICE_ERROR:
728 case EFI_WRITE_PROTECTED:
731 case EFI_SECURITY_VIOLATION:
744 static ssize_t efivarfs_file_write(struct file *file,
745 const char __user *userbuf, size_t count, loff_t *ppos)
747 struct efivar_entry *var = file->private_data;
748 struct efivars *efivars;
752 struct inode *inode = file->f_mapping->host;
753 unsigned long datasize = count - sizeof(attributes);
754 unsigned long newdatasize, varsize;
757 if (count < sizeof(attributes))
760 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
763 if (attributes & ~(EFI_VARIABLE_MASK))
766 efivars = var->efivars;
769 * Ensure that the user can't allocate arbitrarily large
770 * amounts of memory. Pick a default size of 64K if
771 * QueryVariableInfo() isn't supported by the firmware.
774 varsize = datasize + utf16_strsize(var->var.VariableName, 1024);
775 status = check_var_size(efivars, attributes, varsize);
777 if (status != EFI_SUCCESS) {
778 if (status != EFI_UNSUPPORTED)
779 return efi_status_to_err(status);
781 if (datasize > 65536)
785 data = kmalloc(datasize, GFP_KERNEL);
789 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
794 if (validate_var(&var->var, data, datasize) == false) {
800 * The lock here protects the get_variable call, the conditional
801 * set_variable call, and removal of the variable from the efivars
802 * list (in the case of an authenticated delete).
804 spin_lock_irq(&efivars->lock);
807 * Ensure that the available space hasn't shrunk below the safe level
810 status = check_var_size_locked(efivars, attributes, varsize);
812 if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED) {
813 spin_unlock_irq(&efivars->lock);
816 return efi_status_to_err(status);
819 status = efivars->ops->set_variable(var->var.VariableName,
820 &var->var.VendorGuid,
821 attributes, datasize,
824 if (status != EFI_SUCCESS) {
825 spin_unlock_irq(&efivars->lock);
828 return efi_status_to_err(status);
834 * Writing to the variable may have caused a change in size (which
835 * could either be an append or an overwrite), or the variable to be
836 * deleted. Perform a GetVariable() so we can tell what actually
840 status = efivars->ops->get_variable(var->var.VariableName,
841 &var->var.VendorGuid,
845 if (status == EFI_BUFFER_TOO_SMALL) {
846 spin_unlock_irq(&efivars->lock);
847 mutex_lock(&inode->i_mutex);
848 i_size_write(inode, newdatasize + sizeof(attributes));
849 mutex_unlock(&inode->i_mutex);
851 } else if (status == EFI_NOT_FOUND) {
852 list_del(&var->list);
853 spin_unlock_irq(&efivars->lock);
854 efivar_unregister(var);
856 d_delete(file->f_dentry);
857 dput(file->f_dentry);
860 spin_unlock_irq(&efivars->lock);
861 pr_warn("efivarfs: inconsistent EFI variable implementation? "
862 "status = %lx\n", status);
871 static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
872 size_t count, loff_t *ppos)
874 struct efivar_entry *var = file->private_data;
875 struct efivars *efivars = var->efivars;
877 unsigned long datasize = 0;
882 spin_lock_irq(&efivars->lock);
883 status = efivars->ops->get_variable(var->var.VariableName,
884 &var->var.VendorGuid,
885 &attributes, &datasize, NULL);
886 spin_unlock_irq(&efivars->lock);
888 if (status != EFI_BUFFER_TOO_SMALL)
889 return efi_status_to_err(status);
891 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
896 spin_lock_irq(&efivars->lock);
897 status = efivars->ops->get_variable(var->var.VariableName,
898 &var->var.VendorGuid,
899 &attributes, &datasize,
900 (data + sizeof(attributes)));
901 spin_unlock_irq(&efivars->lock);
903 if (status != EFI_SUCCESS) {
904 size = efi_status_to_err(status);
908 memcpy(data, &attributes, sizeof(attributes));
909 size = simple_read_from_buffer(userbuf, count, ppos,
910 data, datasize + sizeof(attributes));
917 static void efivarfs_evict_inode(struct inode *inode)
922 static const struct super_operations efivarfs_ops = {
923 .statfs = simple_statfs,
924 .drop_inode = generic_delete_inode,
925 .evict_inode = efivarfs_evict_inode,
926 .show_options = generic_show_options,
929 static struct super_block *efivarfs_sb;
931 static const struct inode_operations efivarfs_dir_inode_operations;
933 static const struct file_operations efivarfs_file_operations = {
934 .open = efivarfs_file_open,
935 .read = efivarfs_file_read,
936 .write = efivarfs_file_write,
940 static struct inode *efivarfs_get_inode(struct super_block *sb,
941 const struct inode *dir, int mode, dev_t dev)
943 struct inode *inode = new_inode(sb);
946 inode->i_ino = get_next_ino();
947 inode->i_mode = mode;
948 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
949 switch (mode & S_IFMT) {
951 inode->i_fop = &efivarfs_file_operations;
954 inode->i_op = &efivarfs_dir_inode_operations;
955 inode->i_fop = &simple_dir_operations;
964 * Return true if 'str' is a valid efivarfs filename of the form,
966 * VariableName-12345678-1234-1234-1234-1234567891bc
968 static bool efivarfs_valid_name(const char *str, int len)
970 static const char dashes[GUID_LEN] = {
971 [8] = 1, [13] = 1, [18] = 1, [23] = 1
973 const char *s = str + len - GUID_LEN;
977 * We need a GUID, plus at least one letter for the variable name,
978 * plus the '-' separator
980 if (len < GUID_LEN + 2)
983 /* GUID must be preceded by a '-' */
988 * Validate that 's' is of the correct format, e.g.
990 * 12345678-1234-1234-1234-123456789abc
992 for (i = 0; i < GUID_LEN; i++) {
1005 static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
1007 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
1008 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
1009 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
1010 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
1011 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
1012 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
1013 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
1014 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
1015 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
1016 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
1017 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
1018 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
1019 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
1020 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
1021 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
1022 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
1025 static int efivarfs_create(struct inode *dir, struct dentry *dentry,
1026 umode_t mode, bool excl)
1028 struct inode *inode;
1029 struct efivars *efivars = &__efivars;
1030 struct efivar_entry *var;
1031 int namelen, i = 0, err = 0;
1033 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
1036 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
1040 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
1046 /* length of the variable name itself: remove GUID and separator */
1047 namelen = dentry->d_name.len - GUID_LEN - 1;
1049 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
1050 &var->var.VendorGuid);
1052 for (i = 0; i < namelen; i++)
1053 var->var.VariableName[i] = dentry->d_name.name[i];
1055 var->var.VariableName[i] = '\0';
1057 inode->i_private = var;
1058 var->efivars = efivars;
1059 var->kobj.kset = efivars->kset;
1061 err = kobject_init_and_add(&var->kobj, &efivar_ktype, NULL, "%s",
1062 dentry->d_name.name);
1066 kobject_uevent(&var->kobj, KOBJ_ADD);
1067 spin_lock_irq(&efivars->lock);
1068 list_add(&var->list, &efivars->list);
1069 spin_unlock_irq(&efivars->lock);
1070 d_instantiate(dentry, inode);
1080 static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
1082 struct efivar_entry *var = dentry->d_inode->i_private;
1083 struct efivars *efivars = var->efivars;
1084 efi_status_t status;
1086 spin_lock_irq(&efivars->lock);
1088 status = efivars->ops->set_variable(var->var.VariableName,
1089 &var->var.VendorGuid,
1092 if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
1093 list_del(&var->list);
1094 spin_unlock_irq(&efivars->lock);
1095 efivar_unregister(var);
1096 drop_nlink(dentry->d_inode);
1101 spin_unlock_irq(&efivars->lock);
1106 * Compare two efivarfs file names.
1108 * An efivarfs filename is composed of two parts,
1110 * 1. A case-sensitive variable name
1111 * 2. A case-insensitive GUID
1113 * So we need to perform a case-sensitive match on part 1 and a
1114 * case-insensitive match on part 2.
1116 static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
1117 const struct dentry *dentry, const struct inode *inode,
1118 unsigned int len, const char *str,
1119 const struct qstr *name)
1121 int guid = len - GUID_LEN;
1123 if (name->len != len)
1126 /* Case-sensitive compare for the variable name */
1127 if (memcmp(str, name->name, guid))
1130 /* Case-insensitive compare for the GUID */
1131 return strncasecmp(name->name + guid, str + guid, GUID_LEN);
1134 static int efivarfs_d_hash(const struct dentry *dentry,
1135 const struct inode *inode, struct qstr *qstr)
1137 unsigned long hash = init_name_hash();
1138 const unsigned char *s = qstr->name;
1139 unsigned int len = qstr->len;
1141 if (!efivarfs_valid_name(s, len))
1144 while (len-- > GUID_LEN)
1145 hash = partial_name_hash(*s++, hash);
1147 /* GUID is case-insensitive. */
1149 hash = partial_name_hash(tolower(*s++), hash);
1151 qstr->hash = end_name_hash(hash);
1156 * Retaining negative dentries for an in-memory filesystem just wastes
1157 * memory and lookup time: arrange for them to be deleted immediately.
1159 static int efivarfs_delete_dentry(const struct dentry *dentry)
1164 static struct dentry_operations efivarfs_d_ops = {
1165 .d_compare = efivarfs_d_compare,
1166 .d_hash = efivarfs_d_hash,
1167 .d_delete = efivarfs_delete_dentry,
1170 static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
1177 q.len = strlen(name);
1179 err = efivarfs_d_hash(NULL, NULL, &q);
1181 return ERR_PTR(err);
1183 d = d_alloc(parent, &q);
1187 return ERR_PTR(-ENOMEM);
1190 static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
1192 struct inode *inode = NULL;
1193 struct dentry *root;
1194 struct efivar_entry *entry, *n;
1195 struct efivars *efivars = &__efivars;
1201 sb->s_maxbytes = MAX_LFS_FILESIZE;
1202 sb->s_blocksize = PAGE_CACHE_SIZE;
1203 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1204 sb->s_magic = EFIVARFS_MAGIC;
1205 sb->s_op = &efivarfs_ops;
1206 sb->s_d_op = &efivarfs_d_ops;
1207 sb->s_time_gran = 1;
1209 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
1212 inode->i_op = &efivarfs_dir_inode_operations;
1214 root = d_make_root(inode);
1219 list_for_each_entry_safe(entry, n, &efivars->list, list) {
1220 struct dentry *dentry, *root = efivarfs_sb->s_root;
1221 unsigned long size = 0;
1226 len = utf16_strlen(entry->var.VariableName);
1228 /* name, plus '-', plus GUID, plus NUL*/
1229 name = kmalloc(len + 1 + GUID_LEN + 1, GFP_ATOMIC);
1233 for (i = 0; i < len; i++)
1234 name[i] = entry->var.VariableName[i] & 0xFF;
1238 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
1240 name[len+GUID_LEN+1] = '\0';
1242 inode = efivarfs_get_inode(efivarfs_sb, root->d_inode,
1247 dentry = efivarfs_alloc_dentry(root, name);
1248 if (IS_ERR(dentry)) {
1249 err = PTR_ERR(dentry);
1253 /* copied by the above to local storage in the dentry. */
1256 spin_lock_irq(&efivars->lock);
1257 efivars->ops->get_variable(entry->var.VariableName,
1258 &entry->var.VendorGuid,
1259 &entry->var.Attributes,
1262 spin_unlock_irq(&efivars->lock);
1264 mutex_lock(&inode->i_mutex);
1265 inode->i_private = entry;
1266 i_size_write(inode, size + sizeof(entry->var.Attributes));
1267 mutex_unlock(&inode->i_mutex);
1268 d_add(dentry, inode);
1281 static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
1282 int flags, const char *dev_name, void *data)
1284 return mount_single(fs_type, flags, data, efivarfs_fill_super);
1287 static void efivarfs_kill_sb(struct super_block *sb)
1289 kill_litter_super(sb);
1293 static struct file_system_type efivarfs_type = {
1295 .mount = efivarfs_mount,
1296 .kill_sb = efivarfs_kill_sb,
1298 MODULE_ALIAS_FS("efivarfs");
1301 * Handle negative dentry.
1303 static struct dentry *efivarfs_lookup(struct inode *dir, struct dentry *dentry,
1306 if (dentry->d_name.len > NAME_MAX)
1307 return ERR_PTR(-ENAMETOOLONG);
1308 d_add(dentry, NULL);
1312 static const struct inode_operations efivarfs_dir_inode_operations = {
1313 .lookup = efivarfs_lookup,
1314 .unlink = efivarfs_unlink,
1315 .create = efivarfs_create,
1318 #ifdef CONFIG_EFI_VARS_PSTORE
1320 static int efi_pstore_open(struct pstore_info *psi)
1322 struct efivars *efivars = psi->data;
1324 spin_lock_irq(&efivars->lock);
1325 efivars->walk_entry = list_first_entry(&efivars->list,
1326 struct efivar_entry, list);
1330 static int efi_pstore_close(struct pstore_info *psi)
1332 struct efivars *efivars = psi->data;
1334 spin_unlock_irq(&efivars->lock);
1338 static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
1339 int *count, struct timespec *timespec,
1340 char **buf, struct pstore_info *psi)
1342 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1343 struct efivars *efivars = psi->data;
1344 char name[DUMP_NAME_LEN];
1347 unsigned int part, size;
1350 while (&efivars->walk_entry->list != &efivars->list) {
1351 if (!efi_guidcmp(efivars->walk_entry->var.VendorGuid,
1353 for (i = 0; i < DUMP_NAME_LEN; i++) {
1354 name[i] = efivars->walk_entry->var.VariableName[i];
1356 if (sscanf(name, "dump-type%u-%u-%d-%lu",
1357 type, &part, &cnt, &time) == 4) {
1360 timespec->tv_sec = time;
1361 timespec->tv_nsec = 0;
1362 } else if (sscanf(name, "dump-type%u-%u-%lu",
1363 type, &part, &time) == 3) {
1365 * Check if an old format,
1366 * which doesn't support holding
1367 * multiple logs, remains.
1371 timespec->tv_sec = time;
1372 timespec->tv_nsec = 0;
1374 efivars->walk_entry = list_entry(
1375 efivars->walk_entry->list.next,
1376 struct efivar_entry, list);
1380 get_var_data_locked(efivars, &efivars->walk_entry->var);
1381 size = efivars->walk_entry->var.DataSize;
1382 *buf = kmalloc(size, GFP_KERNEL);
1385 memcpy(*buf, efivars->walk_entry->var.Data,
1387 efivars->walk_entry = list_entry(
1388 efivars->walk_entry->list.next,
1389 struct efivar_entry, list);
1392 efivars->walk_entry = list_entry(efivars->walk_entry->list.next,
1393 struct efivar_entry, list);
1398 static int efi_pstore_write(enum pstore_type_id type,
1399 enum kmsg_dump_reason reason, u64 *id,
1400 unsigned int part, int count, size_t size,
1401 struct pstore_info *psi)
1403 char name[DUMP_NAME_LEN];
1404 efi_char16_t efi_name[DUMP_NAME_LEN];
1405 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1406 struct efivars *efivars = psi->data;
1408 efi_status_t status = EFI_NOT_FOUND;
1409 unsigned long flags;
1411 if (pstore_cannot_block_path(reason)) {
1413 * If the lock is taken by another cpu in non-blocking path,
1414 * this driver returns without entering firmware to avoid
1417 if (!spin_trylock_irqsave(&efivars->lock, flags))
1420 spin_lock_irqsave(&efivars->lock, flags);
1423 * Check if there is a space enough to log.
1424 * size: a size of logging data
1425 * DUMP_NAME_LEN * 2: a maximum size of variable name
1428 status = check_var_size_locked(efivars, PSTORE_EFI_ATTRIBUTES,
1429 size + DUMP_NAME_LEN * 2);
1432 spin_unlock_irqrestore(&efivars->lock, flags);
1437 sprintf(name, "dump-type%u-%u-%d-%lu", type, part, count,
1440 for (i = 0; i < DUMP_NAME_LEN; i++)
1441 efi_name[i] = name[i];
1443 efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
1446 spin_unlock_irqrestore(&efivars->lock, flags);
1448 if (reason == KMSG_DUMP_OOPS && efivar_wq_enabled)
1449 schedule_work(&efivar_work);
1455 static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
1456 struct timespec time, struct pstore_info *psi)
1458 char name[DUMP_NAME_LEN];
1459 efi_char16_t efi_name[DUMP_NAME_LEN];
1460 char name_old[DUMP_NAME_LEN];
1461 efi_char16_t efi_name_old[DUMP_NAME_LEN];
1462 efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
1463 struct efivars *efivars = psi->data;
1464 struct efivar_entry *entry, *found = NULL;
1467 sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
1470 spin_lock_irq(&efivars->lock);
1472 for (i = 0; i < DUMP_NAME_LEN; i++)
1473 efi_name[i] = name[i];
1476 * Clean up an entry with the same name
1479 list_for_each_entry(entry, &efivars->list, list) {
1480 get_var_data_locked(efivars, &entry->var);
1482 if (efi_guidcmp(entry->var.VendorGuid, vendor))
1484 if (utf16_strncmp(entry->var.VariableName, efi_name,
1485 utf16_strlen(efi_name))) {
1487 * Check if an old format,
1488 * which doesn't support holding
1489 * multiple logs, remains.
1491 sprintf(name_old, "dump-type%u-%u-%lu", type,
1492 (unsigned int)id, time.tv_sec);
1494 for (i = 0; i < DUMP_NAME_LEN; i++)
1495 efi_name_old[i] = name_old[i];
1497 if (utf16_strncmp(entry->var.VariableName, efi_name_old,
1498 utf16_strlen(efi_name_old)))
1504 efivars->ops->set_variable(entry->var.VariableName,
1505 &entry->var.VendorGuid,
1506 PSTORE_EFI_ATTRIBUTES,
1512 list_del(&found->list);
1514 spin_unlock_irq(&efivars->lock);
1517 efivar_unregister(found);
1522 static struct pstore_info efi_pstore_info = {
1523 .owner = THIS_MODULE,
1525 .open = efi_pstore_open,
1526 .close = efi_pstore_close,
1527 .read = efi_pstore_read,
1528 .write = efi_pstore_write,
1529 .erase = efi_pstore_erase,
1532 static void efivar_pstore_register(struct efivars *efivars)
1534 efivars->efi_pstore_info = efi_pstore_info;
1535 efivars->efi_pstore_info.buf = kmalloc(4096, GFP_KERNEL);
1536 if (efivars->efi_pstore_info.buf) {
1537 efivars->efi_pstore_info.bufsize = 1024;
1538 efivars->efi_pstore_info.data = efivars;
1539 spin_lock_init(&efivars->efi_pstore_info.buf_lock);
1540 pstore_register(&efivars->efi_pstore_info);
1544 static void efivar_pstore_register(struct efivars *efivars)
1550 static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
1551 struct bin_attribute *bin_attr,
1552 char *buf, loff_t pos, size_t count)
1554 struct efi_variable *new_var = (struct efi_variable *)buf;
1555 struct efivars *efivars = bin_attr->private;
1556 struct efivar_entry *search_efivar, *n;
1557 unsigned long strsize1, strsize2;
1558 efi_status_t status = EFI_NOT_FOUND;
1561 if (!capable(CAP_SYS_ADMIN))
1564 if ((new_var->Attributes & ~EFI_VARIABLE_MASK) != 0 ||
1565 validate_var(new_var, new_var->Data, new_var->DataSize) == false) {
1566 printk(KERN_ERR "efivars: Malformed variable content\n");
1570 spin_lock_irq(&efivars->lock);
1573 * Does this variable already exist?
1575 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
1576 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1577 strsize2 = utf16_strsize(new_var->VariableName, 1024);
1578 if (strsize1 == strsize2 &&
1579 !memcmp(&(search_efivar->var.VariableName),
1580 new_var->VariableName, strsize1) &&
1581 !efi_guidcmp(search_efivar->var.VendorGuid,
1582 new_var->VendorGuid)) {
1588 spin_unlock_irq(&efivars->lock);
1592 status = check_var_size_locked(efivars, new_var->Attributes,
1593 new_var->DataSize + utf16_strsize(new_var->VariableName, 1024));
1595 if (status && status != EFI_UNSUPPORTED) {
1596 spin_unlock_irq(&efivars->lock);
1597 return efi_status_to_err(status);
1600 /* now *really* create the variable via EFI */
1601 status = efivars->ops->set_variable(new_var->VariableName,
1602 &new_var->VendorGuid,
1603 new_var->Attributes,
1607 if (status != EFI_SUCCESS) {
1608 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1610 spin_unlock_irq(&efivars->lock);
1613 spin_unlock_irq(&efivars->lock);
1615 /* Create the entry in sysfs. Locking is not required here */
1616 status = efivar_create_sysfs_entry(efivars,
1617 utf16_strsize(new_var->VariableName,
1619 new_var->VariableName,
1620 &new_var->VendorGuid);
1622 printk(KERN_WARNING "efivars: variable created, but sysfs entry wasn't.\n");
1627 static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
1628 struct bin_attribute *bin_attr,
1629 char *buf, loff_t pos, size_t count)
1631 struct efi_variable *del_var = (struct efi_variable *)buf;
1632 struct efivars *efivars = bin_attr->private;
1633 struct efivar_entry *search_efivar, *n;
1634 unsigned long strsize1, strsize2;
1635 efi_status_t status = EFI_NOT_FOUND;
1638 if (!capable(CAP_SYS_ADMIN))
1641 spin_lock_irq(&efivars->lock);
1644 * Does this variable already exist?
1646 list_for_each_entry_safe(search_efivar, n, &efivars->list, list) {
1647 strsize1 = utf16_strsize(search_efivar->var.VariableName, 1024);
1648 strsize2 = utf16_strsize(del_var->VariableName, 1024);
1649 if (strsize1 == strsize2 &&
1650 !memcmp(&(search_efivar->var.VariableName),
1651 del_var->VariableName, strsize1) &&
1652 !efi_guidcmp(search_efivar->var.VendorGuid,
1653 del_var->VendorGuid)) {
1659 spin_unlock_irq(&efivars->lock);
1662 /* force the Attributes/DataSize to 0 to ensure deletion */
1663 del_var->Attributes = 0;
1664 del_var->DataSize = 0;
1666 status = efivars->ops->set_variable(del_var->VariableName,
1667 &del_var->VendorGuid,
1668 del_var->Attributes,
1672 if (status != EFI_SUCCESS) {
1673 printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
1675 spin_unlock_irq(&efivars->lock);
1678 list_del(&search_efivar->list);
1679 /* We need to release this lock before unregistering. */
1680 spin_unlock_irq(&efivars->lock);
1681 efivar_unregister(search_efivar);
1683 /* It's dead Jim.... */
1687 static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor)
1689 struct efivar_entry *entry, *n;
1690 struct efivars *efivars = &__efivars;
1691 unsigned long strsize1, strsize2;
1694 strsize1 = utf16_strsize(variable_name, 1024);
1695 list_for_each_entry_safe(entry, n, &efivars->list, list) {
1696 strsize2 = utf16_strsize(entry->var.VariableName, 1024);
1697 if (strsize1 == strsize2 &&
1698 !memcmp(variable_name, &(entry->var.VariableName),
1700 !efi_guidcmp(entry->var.VendorGuid,
1710 * Returns the size of variable_name, in bytes, including the
1711 * terminating NULL character, or variable_name_size if no NULL
1712 * character is found among the first variable_name_size bytes.
1714 static unsigned long var_name_strnsize(efi_char16_t *variable_name,
1715 unsigned long variable_name_size)
1721 * The variable name is, by definition, a NULL-terminated
1722 * string, so make absolutely sure that variable_name_size is
1723 * the value we expect it to be. If not, return the real size.
1725 for (len = 2; len <= variable_name_size; len += sizeof(c)) {
1726 c = variable_name[(len / sizeof(c)) - 1];
1731 return min(len, variable_name_size);
1734 static void efivar_update_sysfs_entries(struct work_struct *work)
1736 struct efivars *efivars = &__efivars;
1738 efi_char16_t *variable_name;
1739 unsigned long variable_name_size = 1024;
1740 efi_status_t status = EFI_NOT_FOUND;
1743 /* Add new sysfs entries */
1745 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
1746 if (!variable_name) {
1747 pr_err("efivars: Memory allocation failed.\n");
1751 spin_lock_irq(&efivars->lock);
1754 variable_name_size = 1024;
1755 status = efivars->ops->get_next_variable(
1756 &variable_name_size,
1759 if (status != EFI_SUCCESS) {
1762 if (!variable_is_present(variable_name,
1769 spin_unlock_irq(&efivars->lock);
1772 kfree(variable_name);
1775 variable_name_size = var_name_strnsize(variable_name,
1776 variable_name_size);
1777 efivar_create_sysfs_entry(efivars,
1779 variable_name, &vendor);
1785 * Let's not leave out systab information that snuck into
1786 * the efivars driver
1788 static ssize_t systab_show(struct kobject *kobj,
1789 struct kobj_attribute *attr, char *buf)
1796 if (efi.mps != EFI_INVALID_TABLE_ADDR)
1797 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
1798 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
1799 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
1800 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
1801 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
1802 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
1803 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
1804 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
1805 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
1806 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
1807 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
1808 if (efi.uga != EFI_INVALID_TABLE_ADDR)
1809 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
1814 static struct kobj_attribute efi_attr_systab =
1815 __ATTR(systab, 0400, systab_show, NULL);
1817 static struct attribute *efi_subsys_attrs[] = {
1818 &efi_attr_systab.attr,
1819 NULL, /* maybe more in the future? */
1822 static struct attribute_group efi_subsys_attr_group = {
1823 .attrs = efi_subsys_attrs,
1826 static struct kobject *efi_kobj;
1829 * efivar_create_sysfs_entry()
1831 * variable_name_size = number of bytes required to hold
1832 * variable_name (not counting the NULL
1833 * character at the end.
1834 * efivars->lock is not held on entry or exit.
1835 * Returns 1 on failure, 0 on success
1838 efivar_create_sysfs_entry(struct efivars *efivars,
1839 unsigned long variable_name_size,
1840 efi_char16_t *variable_name,
1841 efi_guid_t *vendor_guid)
1843 int i, short_name_size;
1845 struct efivar_entry *new_efivar;
1848 * Length of the variable bytes in ASCII, plus the '-' separator,
1849 * plus the GUID, plus trailing NUL
1851 short_name_size = variable_name_size / sizeof(efi_char16_t)
1854 short_name = kzalloc(short_name_size, GFP_KERNEL);
1855 new_efivar = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
1857 if (!short_name || !new_efivar) {
1863 new_efivar->efivars = efivars;
1864 memcpy(new_efivar->var.VariableName, variable_name,
1865 variable_name_size);
1866 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
1868 /* Convert Unicode to normal chars (assume top bits are 0),
1870 for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
1871 short_name[i] = variable_name[i] & 0xFF;
1873 /* This is ugly, but necessary to separate one vendor's
1874 private variables from another's. */
1876 *(short_name + strlen(short_name)) = '-';
1877 efi_guid_unparse(vendor_guid, short_name + strlen(short_name));
1879 new_efivar->kobj.kset = efivars->kset;
1880 i = kobject_init_and_add(&new_efivar->kobj, &efivar_ktype, NULL,
1888 kobject_uevent(&new_efivar->kobj, KOBJ_ADD);
1892 spin_lock_irq(&efivars->lock);
1893 list_add(&new_efivar->list, &efivars->list);
1894 spin_unlock_irq(&efivars->lock);
1900 create_efivars_bin_attributes(struct efivars *efivars)
1902 struct bin_attribute *attr;
1906 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1910 attr->attr.name = "new_var";
1911 attr->attr.mode = 0200;
1912 attr->write = efivar_create;
1913 attr->private = efivars;
1914 efivars->new_var = attr;
1917 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1922 attr->attr.name = "del_var";
1923 attr->attr.mode = 0200;
1924 attr->write = efivar_delete;
1925 attr->private = efivars;
1926 efivars->del_var = attr;
1928 sysfs_bin_attr_init(efivars->new_var);
1929 sysfs_bin_attr_init(efivars->del_var);
1932 error = sysfs_create_bin_file(&efivars->kset->kobj,
1935 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
1936 " due to error %d\n", error);
1939 error = sysfs_create_bin_file(&efivars->kset->kobj,
1942 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
1943 " due to error %d\n", error);
1944 sysfs_remove_bin_file(&efivars->kset->kobj,
1951 kfree(efivars->del_var);
1952 efivars->del_var = NULL;
1953 kfree(efivars->new_var);
1954 efivars->new_var = NULL;
1958 void unregister_efivars(struct efivars *efivars)
1960 struct efivar_entry *entry, *n;
1962 list_for_each_entry_safe(entry, n, &efivars->list, list) {
1963 spin_lock_irq(&efivars->lock);
1964 list_del(&entry->list);
1965 spin_unlock_irq(&efivars->lock);
1966 efivar_unregister(entry);
1968 if (efivars->new_var)
1969 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->new_var);
1970 if (efivars->del_var)
1971 sysfs_remove_bin_file(&efivars->kset->kobj, efivars->del_var);
1972 kfree(efivars->new_var);
1973 kfree(efivars->del_var);
1974 kobject_put(efivars->kobject);
1975 kset_unregister(efivars->kset);
1977 EXPORT_SYMBOL_GPL(unregister_efivars);
1980 * Print a warning when duplicate EFI variables are encountered and
1981 * disable the sysfs workqueue since the firmware is buggy.
1983 static void dup_variable_bug(efi_char16_t *s16, efi_guid_t *vendor_guid,
1984 unsigned long len16)
1986 size_t i, len8 = len16 / sizeof(efi_char16_t);
1990 * Disable the workqueue since the algorithm it uses for
1991 * detecting new variables won't work with this buggy
1992 * implementation of GetNextVariableName().
1994 efivar_wq_enabled = false;
1996 s8 = kzalloc(len8, GFP_KERNEL);
2000 for (i = 0; i < len8; i++)
2003 printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
2008 int register_efivars(struct efivars *efivars,
2009 const struct efivar_operations *ops,
2010 struct kobject *parent_kobj)
2012 efi_status_t status = EFI_NOT_FOUND;
2013 efi_guid_t vendor_guid;
2014 efi_char16_t *variable_name;
2015 unsigned long variable_name_size = 1024;
2018 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
2019 if (!variable_name) {
2020 printk(KERN_ERR "efivars: Memory allocation failed.\n");
2024 spin_lock_init(&efivars->lock);
2025 INIT_LIST_HEAD(&efivars->list);
2028 efivars->kset = kset_create_and_add("vars", NULL, parent_kobj);
2029 if (!efivars->kset) {
2030 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
2035 efivars->kobject = kobject_create_and_add("efivars", parent_kobj);
2036 if (!efivars->kobject) {
2037 pr_err("efivars: Subsystem registration failed.\n");
2039 kset_unregister(efivars->kset);
2044 * Per EFI spec, the maximum storage allocated for both
2045 * the variable name and variable data is 1024 bytes.
2049 variable_name_size = 1024;
2051 status = ops->get_next_variable(&variable_name_size,
2056 variable_name_size = var_name_strnsize(variable_name,
2057 variable_name_size);
2060 * Some firmware implementations return the
2061 * same variable name on multiple calls to
2062 * get_next_variable(). Terminate the loop
2063 * immediately as there is no guarantee that
2064 * we'll ever see a different variable name,
2065 * and may end up looping here forever.
2067 if (variable_is_present(variable_name, &vendor_guid)) {
2068 dup_variable_bug(variable_name, &vendor_guid,
2069 variable_name_size);
2070 status = EFI_NOT_FOUND;
2074 efivar_create_sysfs_entry(efivars,
2082 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
2084 status = EFI_NOT_FOUND;
2087 } while (status != EFI_NOT_FOUND);
2089 error = create_efivars_bin_attributes(efivars);
2091 unregister_efivars(efivars);
2093 if (!efivars_pstore_disable)
2094 efivar_pstore_register(efivars);
2096 register_filesystem(&efivarfs_type);
2099 kfree(variable_name);
2103 EXPORT_SYMBOL_GPL(register_efivars);
2106 * For now we register the efi subsystem with the firmware subsystem
2107 * and the vars subsystem with the efi subsystem. In the future, it
2108 * might make sense to split off the efi subsystem into its own
2109 * driver, but for now only efivars will register with it, so just
2118 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
2121 if (!efi_enabled(EFI_RUNTIME_SERVICES))
2124 /* For now we'll register the efi directory at /sys/firmware/efi */
2125 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
2127 printk(KERN_ERR "efivars: Firmware registration failed.\n");
2131 ops.get_variable = efi.get_variable;
2132 ops.set_variable = efi.set_variable;
2133 ops.get_next_variable = efi.get_next_variable;
2134 ops.query_variable_info = efi.query_variable_info;
2136 error = register_efivars(&__efivars, &ops, efi_kobj);
2140 /* Don't forget the systab entry */
2141 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
2144 "efivars: Sysfs attribute export failed with error %d.\n",
2146 goto err_unregister;
2152 unregister_efivars(&__efivars);
2154 kobject_put(efi_kobj);
2161 cancel_work_sync(&efivar_work);
2163 if (efi_enabled(EFI_RUNTIME_SERVICES)) {
2164 unregister_efivars(&__efivars);
2165 kobject_put(efi_kobj);
2169 module_init(efivars_init);
2170 module_exit(efivars_exit);