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>
82 #include <linux/ctype.h>
83 #include <linux/ucs2_string.h>
86 #include <linux/ramfs.h>
87 #include <linux/pagemap.h>
89 #include <asm/uaccess.h>
91 #define EFIVARS_VERSION "0.08"
92 #define EFIVARS_DATE "2004-May-17"
94 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
95 MODULE_DESCRIPTION("sysfs interface to EFI Variables");
96 MODULE_LICENSE("GPL");
97 MODULE_VERSION(EFIVARS_VERSION
);
99 #define DUMP_NAME_LEN 52
102 * Length of a GUID string (strlen("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))
103 * not including trailing NUL
107 static bool efivars_pstore_disable
=
108 IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE
);
110 module_param_named(pstore_disable
, efivars_pstore_disable
, bool, 0644);
113 * The maximum size of VariableName + Data = 1024
114 * Therefore, it's reasonable to save that much
115 * space in each part of the structure,
116 * and we use a page for reading/writing.
119 struct efi_variable
{
120 efi_char16_t VariableName
[1024/sizeof(efi_char16_t
)];
121 efi_guid_t VendorGuid
;
122 unsigned long DataSize
;
126 } __attribute__((packed
));
128 struct efivar_entry
{
129 struct efivars
*efivars
;
130 struct efi_variable var
;
131 struct list_head list
;
135 struct efivar_attribute
{
136 struct attribute attr
;
137 ssize_t (*show
) (struct efivar_entry
*entry
, char *buf
);
138 ssize_t (*store
)(struct efivar_entry
*entry
, const char *buf
, size_t count
);
141 static struct efivars __efivars
;
142 static struct efivar_operations ops
;
144 #define PSTORE_EFI_ATTRIBUTES \
145 (EFI_VARIABLE_NON_VOLATILE | \
146 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
147 EFI_VARIABLE_RUNTIME_ACCESS)
149 #define EFIVAR_ATTR(_name, _mode, _show, _store) \
150 struct efivar_attribute efivar_attr_##_name = { \
151 .attr = {.name = __stringify(_name), .mode = _mode}, \
156 #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
157 #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
160 * Prototype for sysfs creation function
163 efivar_create_sysfs_entry(struct efivars
*efivars
,
164 unsigned long variable_name_size
,
165 efi_char16_t
*variable_name
,
166 efi_guid_t
*vendor_guid
);
169 * Prototype for workqueue functions updating sysfs entry
172 static void efivar_update_sysfs_entries(struct work_struct
*);
173 static DECLARE_WORK(efivar_work
, efivar_update_sysfs_entries
);
174 static bool efivar_wq_enabled
= true;
177 validate_device_path(struct efi_variable
*var
, int match
, u8
*buffer
,
180 struct efi_generic_dev_path
*node
;
183 node
= (struct efi_generic_dev_path
*)buffer
;
185 if (len
< sizeof(*node
))
188 while (offset
<= len
- sizeof(*node
) &&
189 node
->length
>= sizeof(*node
) &&
190 node
->length
<= len
- offset
) {
191 offset
+= node
->length
;
193 if ((node
->type
== EFI_DEV_END_PATH
||
194 node
->type
== EFI_DEV_END_PATH2
) &&
195 node
->sub_type
== EFI_DEV_END_ENTIRE
)
198 node
= (struct efi_generic_dev_path
*)(buffer
+ offset
);
202 * If we're here then either node->length pointed past the end
203 * of the buffer or we reached the end of the buffer without
204 * finding a device path end node.
210 validate_boot_order(struct efi_variable
*var
, int match
, u8
*buffer
,
213 /* An array of 16-bit integers */
221 validate_load_option(struct efi_variable
*var
, int match
, u8
*buffer
,
225 int i
, desclength
= 0, namelen
;
227 namelen
= ucs2_strnlen(var
->VariableName
, sizeof(var
->VariableName
));
229 /* Either "Boot" or "Driver" followed by four digits of hex */
230 for (i
= match
; i
< match
+4; i
++) {
231 if (var
->VariableName
[i
] > 127 ||
232 hex_to_bin(var
->VariableName
[i
] & 0xff) < 0)
236 /* Reject it if there's 4 digits of hex and then further content */
237 if (namelen
> match
+ 4)
240 /* A valid entry must be at least 8 bytes */
244 filepathlength
= buffer
[4] | buffer
[5] << 8;
247 * There's no stored length for the description, so it has to be
250 desclength
= ucs2_strsize((efi_char16_t
*)(buffer
+ 6), len
- 6) + 2;
252 /* Each boot entry must have a descriptor */
257 * If the sum of the length of the description, the claimed filepath
258 * length and the original header are greater than the length of the
259 * variable, it's malformed
261 if ((desclength
+ filepathlength
+ 6) > len
)
265 * And, finally, check the filepath
267 return validate_device_path(var
, match
, buffer
+ desclength
+ 6,
272 validate_uint16(struct efi_variable
*var
, int match
, u8
*buffer
,
275 /* A single 16-bit integer */
283 validate_ascii_string(struct efi_variable
*var
, int match
, u8
*buffer
,
288 for (i
= 0; i
< len
; i
++) {
299 struct variable_validate
{
301 bool (*validate
)(struct efi_variable
*var
, int match
, u8
*data
,
305 static const struct variable_validate variable_validate
[] = {
306 { "BootNext", validate_uint16
},
307 { "BootOrder", validate_boot_order
},
308 { "DriverOrder", validate_boot_order
},
309 { "Boot*", validate_load_option
},
310 { "Driver*", validate_load_option
},
311 { "ConIn", validate_device_path
},
312 { "ConInDev", validate_device_path
},
313 { "ConOut", validate_device_path
},
314 { "ConOutDev", validate_device_path
},
315 { "ErrOut", validate_device_path
},
316 { "ErrOutDev", validate_device_path
},
317 { "Timeout", validate_uint16
},
318 { "Lang", validate_ascii_string
},
319 { "PlatformLang", validate_ascii_string
},
324 validate_var(struct efi_variable
*var
, u8
*data
, unsigned long len
)
327 u16
*unicode_name
= var
->VariableName
;
329 for (i
= 0; variable_validate
[i
].validate
!= NULL
; i
++) {
330 const char *name
= variable_validate
[i
].name
;
333 for (match
= 0; ; match
++) {
334 char c
= name
[match
];
335 u16 u
= unicode_name
[match
];
337 /* All special variables are plain ascii */
341 /* Wildcard in the matching name means we've matched */
343 return variable_validate
[i
].validate(var
,
346 /* Case sensitive match */
350 /* Reached the end of the string while matching */
352 return variable_validate
[i
].validate(var
,
361 get_var_data_locked(struct efivars
*efivars
, struct efi_variable
*var
)
365 var
->DataSize
= 1024;
366 status
= efivars
->ops
->get_variable(var
->VariableName
,
375 get_var_data(struct efivars
*efivars
, struct efi_variable
*var
)
380 spin_lock_irqsave(&efivars
->lock
, flags
);
381 status
= get_var_data_locked(efivars
, var
);
382 spin_unlock_irqrestore(&efivars
->lock
, flags
);
384 if (status
!= EFI_SUCCESS
) {
385 printk(KERN_WARNING
"efivars: get_variable() failed 0x%lx!\n",
392 check_var_size_locked(struct efivars
*efivars
, u32 attributes
,
395 const struct efivar_operations
*fops
= efivars
->ops
;
397 if (!efivars
->ops
->query_variable_store
)
398 return EFI_UNSUPPORTED
;
400 return fops
->query_variable_store(attributes
, size
);
405 check_var_size(struct efivars
*efivars
, u32 attributes
, unsigned long size
)
410 spin_lock_irqsave(&efivars
->lock
, flags
);
411 status
= check_var_size_locked(efivars
, attributes
, size
);
412 spin_unlock_irqrestore(&efivars
->lock
, flags
);
418 efivar_guid_read(struct efivar_entry
*entry
, char *buf
)
420 struct efi_variable
*var
= &entry
->var
;
426 efi_guid_unparse(&var
->VendorGuid
, str
);
428 str
+= sprintf(str
, "\n");
434 efivar_attr_read(struct efivar_entry
*entry
, char *buf
)
436 struct efi_variable
*var
= &entry
->var
;
443 status
= get_var_data(entry
->efivars
, var
);
444 if (status
!= EFI_SUCCESS
)
447 if (var
->Attributes
& EFI_VARIABLE_NON_VOLATILE
)
448 str
+= sprintf(str
, "EFI_VARIABLE_NON_VOLATILE\n");
449 if (var
->Attributes
& EFI_VARIABLE_BOOTSERVICE_ACCESS
)
450 str
+= sprintf(str
, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
451 if (var
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)
452 str
+= sprintf(str
, "EFI_VARIABLE_RUNTIME_ACCESS\n");
453 if (var
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
)
454 str
+= sprintf(str
, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
455 if (var
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
457 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
458 if (var
->Attributes
&
459 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)
461 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
462 if (var
->Attributes
& EFI_VARIABLE_APPEND_WRITE
)
463 str
+= sprintf(str
, "EFI_VARIABLE_APPEND_WRITE\n");
468 efivar_size_read(struct efivar_entry
*entry
, char *buf
)
470 struct efi_variable
*var
= &entry
->var
;
477 status
= get_var_data(entry
->efivars
, var
);
478 if (status
!= EFI_SUCCESS
)
481 str
+= sprintf(str
, "0x%lx\n", var
->DataSize
);
486 efivar_data_read(struct efivar_entry
*entry
, char *buf
)
488 struct efi_variable
*var
= &entry
->var
;
494 status
= get_var_data(entry
->efivars
, var
);
495 if (status
!= EFI_SUCCESS
)
498 memcpy(buf
, var
->Data
, var
->DataSize
);
499 return var
->DataSize
;
502 * We allow each variable to be edited via rewriting the
503 * entire efi variable structure.
506 efivar_store_raw(struct efivar_entry
*entry
, const char *buf
, size_t count
)
508 struct efi_variable
*new_var
, *var
= &entry
->var
;
509 struct efivars
*efivars
= entry
->efivars
;
510 efi_status_t status
= EFI_NOT_FOUND
;
512 if (count
!= sizeof(struct efi_variable
))
515 new_var
= (struct efi_variable
*)buf
;
517 * If only updating the variable data, then the name
518 * and guid should remain the same
520 if (memcmp(new_var
->VariableName
, var
->VariableName
, sizeof(var
->VariableName
)) ||
521 efi_guidcmp(new_var
->VendorGuid
, var
->VendorGuid
)) {
522 printk(KERN_ERR
"efivars: Cannot edit the wrong variable!\n");
526 if ((new_var
->DataSize
<= 0) || (new_var
->Attributes
== 0)){
527 printk(KERN_ERR
"efivars: DataSize & Attributes must be valid!\n");
531 if ((new_var
->Attributes
& ~EFI_VARIABLE_MASK
) != 0 ||
532 validate_var(new_var
, new_var
->Data
, new_var
->DataSize
) == false) {
533 printk(KERN_ERR
"efivars: Malformed variable content\n");
537 spin_lock_irq(&efivars
->lock
);
539 status
= check_var_size_locked(efivars
, new_var
->Attributes
,
540 new_var
->DataSize
+ ucs2_strsize(new_var
->VariableName
, 1024));
542 if (status
== EFI_SUCCESS
|| status
== EFI_UNSUPPORTED
)
543 status
= efivars
->ops
->set_variable(new_var
->VariableName
,
544 &new_var
->VendorGuid
,
549 spin_unlock_irq(&efivars
->lock
);
551 if (status
!= EFI_SUCCESS
) {
552 printk(KERN_WARNING
"efivars: set_variable() failed: status=%lx\n",
557 memcpy(&entry
->var
, new_var
, count
);
562 efivar_show_raw(struct efivar_entry
*entry
, char *buf
)
564 struct efi_variable
*var
= &entry
->var
;
570 status
= get_var_data(entry
->efivars
, var
);
571 if (status
!= EFI_SUCCESS
)
574 memcpy(buf
, var
, sizeof(*var
));
579 * Generic read/write functions that call the specific functions of
582 static ssize_t
efivar_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
585 struct efivar_entry
*var
= to_efivar_entry(kobj
);
586 struct efivar_attribute
*efivar_attr
= to_efivar_attr(attr
);
589 if (!capable(CAP_SYS_ADMIN
))
592 if (efivar_attr
->show
) {
593 ret
= efivar_attr
->show(var
, buf
);
598 static ssize_t
efivar_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
599 const char *buf
, size_t count
)
601 struct efivar_entry
*var
= to_efivar_entry(kobj
);
602 struct efivar_attribute
*efivar_attr
= to_efivar_attr(attr
);
605 if (!capable(CAP_SYS_ADMIN
))
608 if (efivar_attr
->store
)
609 ret
= efivar_attr
->store(var
, buf
, count
);
614 static const struct sysfs_ops efivar_attr_ops
= {
615 .show
= efivar_attr_show
,
616 .store
= efivar_attr_store
,
619 static void efivar_release(struct kobject
*kobj
)
621 struct efivar_entry
*var
= container_of(kobj
, struct efivar_entry
, kobj
);
625 static EFIVAR_ATTR(guid
, 0400, efivar_guid_read
, NULL
);
626 static EFIVAR_ATTR(attributes
, 0400, efivar_attr_read
, NULL
);
627 static EFIVAR_ATTR(size
, 0400, efivar_size_read
, NULL
);
628 static EFIVAR_ATTR(data
, 0400, efivar_data_read
, NULL
);
629 static EFIVAR_ATTR(raw_var
, 0600, efivar_show_raw
, efivar_store_raw
);
631 static struct attribute
*def_attrs
[] = {
632 &efivar_attr_guid
.attr
,
633 &efivar_attr_size
.attr
,
634 &efivar_attr_attributes
.attr
,
635 &efivar_attr_data
.attr
,
636 &efivar_attr_raw_var
.attr
,
640 static struct kobj_type efivar_ktype
= {
641 .release
= efivar_release
,
642 .sysfs_ops
= &efivar_attr_ops
,
643 .default_attrs
= def_attrs
,
647 efivar_unregister(struct efivar_entry
*var
)
649 kobject_put(&var
->kobj
);
652 static int efivarfs_file_open(struct inode
*inode
, struct file
*file
)
654 file
->private_data
= inode
->i_private
;
658 static int efi_status_to_err(efi_status_t status
)
663 case EFI_INVALID_PARAMETER
:
666 case EFI_OUT_OF_RESOURCES
:
669 case EFI_DEVICE_ERROR
:
672 case EFI_WRITE_PROTECTED
:
675 case EFI_SECURITY_VIOLATION
:
688 static ssize_t
efivarfs_file_write(struct file
*file
,
689 const char __user
*userbuf
, size_t count
, loff_t
*ppos
)
691 struct efivar_entry
*var
= file
->private_data
;
692 struct efivars
*efivars
;
696 struct inode
*inode
= file
->f_mapping
->host
;
697 unsigned long datasize
= count
- sizeof(attributes
);
698 unsigned long newdatasize
, varsize
;
701 if (count
< sizeof(attributes
))
704 if (copy_from_user(&attributes
, userbuf
, sizeof(attributes
)))
707 if (attributes
& ~(EFI_VARIABLE_MASK
))
710 efivars
= var
->efivars
;
713 * Ensure that the user can't allocate arbitrarily large
714 * amounts of memory. Pick a default size of 64K if
715 * QueryVariableInfo() isn't supported by the firmware.
718 varsize
= datasize
+ ucs2_strsize(var
->var
.VariableName
, 1024);
719 status
= check_var_size(efivars
, attributes
, varsize
);
721 if (status
!= EFI_SUCCESS
) {
722 if (status
!= EFI_UNSUPPORTED
)
723 return efi_status_to_err(status
);
725 if (datasize
> 65536)
729 data
= kmalloc(datasize
, GFP_KERNEL
);
733 if (copy_from_user(data
, userbuf
+ sizeof(attributes
), datasize
)) {
738 if (validate_var(&var
->var
, data
, datasize
) == false) {
744 * The lock here protects the get_variable call, the conditional
745 * set_variable call, and removal of the variable from the efivars
746 * list (in the case of an authenticated delete).
748 spin_lock_irq(&efivars
->lock
);
751 * Ensure that the available space hasn't shrunk below the safe level
754 status
= check_var_size_locked(efivars
, attributes
, varsize
);
756 if (status
!= EFI_SUCCESS
&& status
!= EFI_UNSUPPORTED
) {
757 spin_unlock_irq(&efivars
->lock
);
760 return efi_status_to_err(status
);
763 status
= efivars
->ops
->set_variable(var
->var
.VariableName
,
764 &var
->var
.VendorGuid
,
765 attributes
, datasize
,
768 if (status
!= EFI_SUCCESS
) {
769 spin_unlock_irq(&efivars
->lock
);
772 return efi_status_to_err(status
);
778 * Writing to the variable may have caused a change in size (which
779 * could either be an append or an overwrite), or the variable to be
780 * deleted. Perform a GetVariable() so we can tell what actually
784 status
= efivars
->ops
->get_variable(var
->var
.VariableName
,
785 &var
->var
.VendorGuid
,
789 if (status
== EFI_BUFFER_TOO_SMALL
) {
790 spin_unlock_irq(&efivars
->lock
);
791 mutex_lock(&inode
->i_mutex
);
792 i_size_write(inode
, newdatasize
+ sizeof(attributes
));
793 mutex_unlock(&inode
->i_mutex
);
795 } else if (status
== EFI_NOT_FOUND
) {
796 list_del(&var
->list
);
797 spin_unlock_irq(&efivars
->lock
);
798 efivar_unregister(var
);
800 d_delete(file
->f_dentry
);
801 dput(file
->f_dentry
);
804 spin_unlock_irq(&efivars
->lock
);
805 pr_warn("efivarfs: inconsistent EFI variable implementation? "
806 "status = %lx\n", status
);
815 static ssize_t
efivarfs_file_read(struct file
*file
, char __user
*userbuf
,
816 size_t count
, loff_t
*ppos
)
818 struct efivar_entry
*var
= file
->private_data
;
819 struct efivars
*efivars
= var
->efivars
;
821 unsigned long datasize
= 0;
826 spin_lock_irq(&efivars
->lock
);
827 status
= efivars
->ops
->get_variable(var
->var
.VariableName
,
828 &var
->var
.VendorGuid
,
829 &attributes
, &datasize
, NULL
);
830 spin_unlock_irq(&efivars
->lock
);
832 if (status
!= EFI_BUFFER_TOO_SMALL
)
833 return efi_status_to_err(status
);
835 data
= kmalloc(datasize
+ sizeof(attributes
), GFP_KERNEL
);
840 spin_lock_irq(&efivars
->lock
);
841 status
= efivars
->ops
->get_variable(var
->var
.VariableName
,
842 &var
->var
.VendorGuid
,
843 &attributes
, &datasize
,
844 (data
+ sizeof(attributes
)));
845 spin_unlock_irq(&efivars
->lock
);
847 if (status
!= EFI_SUCCESS
) {
848 size
= efi_status_to_err(status
);
852 memcpy(data
, &attributes
, sizeof(attributes
));
853 size
= simple_read_from_buffer(userbuf
, count
, ppos
,
854 data
, datasize
+ sizeof(attributes
));
861 static void efivarfs_evict_inode(struct inode
*inode
)
866 static const struct super_operations efivarfs_ops
= {
867 .statfs
= simple_statfs
,
868 .drop_inode
= generic_delete_inode
,
869 .evict_inode
= efivarfs_evict_inode
,
870 .show_options
= generic_show_options
,
873 static struct super_block
*efivarfs_sb
;
875 static const struct inode_operations efivarfs_dir_inode_operations
;
877 static const struct file_operations efivarfs_file_operations
= {
878 .open
= efivarfs_file_open
,
879 .read
= efivarfs_file_read
,
880 .write
= efivarfs_file_write
,
884 static struct inode
*efivarfs_get_inode(struct super_block
*sb
,
885 const struct inode
*dir
, int mode
, dev_t dev
)
887 struct inode
*inode
= new_inode(sb
);
890 inode
->i_ino
= get_next_ino();
891 inode
->i_mode
= mode
;
892 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
893 switch (mode
& S_IFMT
) {
895 inode
->i_fop
= &efivarfs_file_operations
;
898 inode
->i_op
= &efivarfs_dir_inode_operations
;
899 inode
->i_fop
= &simple_dir_operations
;
908 * Return true if 'str' is a valid efivarfs filename of the form,
910 * VariableName-12345678-1234-1234-1234-1234567891bc
912 static bool efivarfs_valid_name(const char *str
, int len
)
914 static const char dashes
[GUID_LEN
] = {
915 [8] = 1, [13] = 1, [18] = 1, [23] = 1
917 const char *s
= str
+ len
- GUID_LEN
;
921 * We need a GUID, plus at least one letter for the variable name,
922 * plus the '-' separator
924 if (len
< GUID_LEN
+ 2)
927 /* GUID must be preceded by a '-' */
932 * Validate that 's' is of the correct format, e.g.
934 * 12345678-1234-1234-1234-123456789abc
936 for (i
= 0; i
< GUID_LEN
; i
++) {
949 static void efivarfs_hex_to_guid(const char *str
, efi_guid_t
*guid
)
951 guid
->b
[0] = hex_to_bin(str
[6]) << 4 | hex_to_bin(str
[7]);
952 guid
->b
[1] = hex_to_bin(str
[4]) << 4 | hex_to_bin(str
[5]);
953 guid
->b
[2] = hex_to_bin(str
[2]) << 4 | hex_to_bin(str
[3]);
954 guid
->b
[3] = hex_to_bin(str
[0]) << 4 | hex_to_bin(str
[1]);
955 guid
->b
[4] = hex_to_bin(str
[11]) << 4 | hex_to_bin(str
[12]);
956 guid
->b
[5] = hex_to_bin(str
[9]) << 4 | hex_to_bin(str
[10]);
957 guid
->b
[6] = hex_to_bin(str
[16]) << 4 | hex_to_bin(str
[17]);
958 guid
->b
[7] = hex_to_bin(str
[14]) << 4 | hex_to_bin(str
[15]);
959 guid
->b
[8] = hex_to_bin(str
[19]) << 4 | hex_to_bin(str
[20]);
960 guid
->b
[9] = hex_to_bin(str
[21]) << 4 | hex_to_bin(str
[22]);
961 guid
->b
[10] = hex_to_bin(str
[24]) << 4 | hex_to_bin(str
[25]);
962 guid
->b
[11] = hex_to_bin(str
[26]) << 4 | hex_to_bin(str
[27]);
963 guid
->b
[12] = hex_to_bin(str
[28]) << 4 | hex_to_bin(str
[29]);
964 guid
->b
[13] = hex_to_bin(str
[30]) << 4 | hex_to_bin(str
[31]);
965 guid
->b
[14] = hex_to_bin(str
[32]) << 4 | hex_to_bin(str
[33]);
966 guid
->b
[15] = hex_to_bin(str
[34]) << 4 | hex_to_bin(str
[35]);
969 static int efivarfs_create(struct inode
*dir
, struct dentry
*dentry
,
970 umode_t mode
, bool excl
)
973 struct efivars
*efivars
= &__efivars
;
974 struct efivar_entry
*var
;
975 int namelen
, i
= 0, err
= 0;
977 if (!efivarfs_valid_name(dentry
->d_name
.name
, dentry
->d_name
.len
))
980 inode
= efivarfs_get_inode(dir
->i_sb
, dir
, mode
, 0);
984 var
= kzalloc(sizeof(struct efivar_entry
), GFP_KERNEL
);
990 /* length of the variable name itself: remove GUID and separator */
991 namelen
= dentry
->d_name
.len
- GUID_LEN
- 1;
993 efivarfs_hex_to_guid(dentry
->d_name
.name
+ namelen
+ 1,
994 &var
->var
.VendorGuid
);
996 for (i
= 0; i
< namelen
; i
++)
997 var
->var
.VariableName
[i
] = dentry
->d_name
.name
[i
];
999 var
->var
.VariableName
[i
] = '\0';
1001 inode
->i_private
= var
;
1002 var
->efivars
= efivars
;
1003 var
->kobj
.kset
= efivars
->kset
;
1005 err
= kobject_init_and_add(&var
->kobj
, &efivar_ktype
, NULL
, "%s",
1006 dentry
->d_name
.name
);
1010 kobject_uevent(&var
->kobj
, KOBJ_ADD
);
1011 spin_lock_irq(&efivars
->lock
);
1012 list_add(&var
->list
, &efivars
->list
);
1013 spin_unlock_irq(&efivars
->lock
);
1014 d_instantiate(dentry
, inode
);
1024 static int efivarfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1026 struct efivar_entry
*var
= dentry
->d_inode
->i_private
;
1027 struct efivars
*efivars
= var
->efivars
;
1028 efi_status_t status
;
1030 spin_lock_irq(&efivars
->lock
);
1032 status
= efivars
->ops
->set_variable(var
->var
.VariableName
,
1033 &var
->var
.VendorGuid
,
1036 if (status
== EFI_SUCCESS
|| status
== EFI_NOT_FOUND
) {
1037 list_del(&var
->list
);
1038 spin_unlock_irq(&efivars
->lock
);
1039 efivar_unregister(var
);
1040 drop_nlink(dentry
->d_inode
);
1045 spin_unlock_irq(&efivars
->lock
);
1050 * Compare two efivarfs file names.
1052 * An efivarfs filename is composed of two parts,
1054 * 1. A case-sensitive variable name
1055 * 2. A case-insensitive GUID
1057 * So we need to perform a case-sensitive match on part 1 and a
1058 * case-insensitive match on part 2.
1060 static int efivarfs_d_compare(const struct dentry
*parent
, const struct inode
*pinode
,
1061 const struct dentry
*dentry
, const struct inode
*inode
,
1062 unsigned int len
, const char *str
,
1063 const struct qstr
*name
)
1065 int guid
= len
- GUID_LEN
;
1067 if (name
->len
!= len
)
1070 /* Case-sensitive compare for the variable name */
1071 if (memcmp(str
, name
->name
, guid
))
1074 /* Case-insensitive compare for the GUID */
1075 return strncasecmp(name
->name
+ guid
, str
+ guid
, GUID_LEN
);
1078 static int efivarfs_d_hash(const struct dentry
*dentry
,
1079 const struct inode
*inode
, struct qstr
*qstr
)
1081 unsigned long hash
= init_name_hash();
1082 const unsigned char *s
= qstr
->name
;
1083 unsigned int len
= qstr
->len
;
1085 if (!efivarfs_valid_name(s
, len
))
1088 while (len
-- > GUID_LEN
)
1089 hash
= partial_name_hash(*s
++, hash
);
1091 /* GUID is case-insensitive. */
1093 hash
= partial_name_hash(tolower(*s
++), hash
);
1095 qstr
->hash
= end_name_hash(hash
);
1100 * Retaining negative dentries for an in-memory filesystem just wastes
1101 * memory and lookup time: arrange for them to be deleted immediately.
1103 static int efivarfs_delete_dentry(const struct dentry
*dentry
)
1108 static struct dentry_operations efivarfs_d_ops
= {
1109 .d_compare
= efivarfs_d_compare
,
1110 .d_hash
= efivarfs_d_hash
,
1111 .d_delete
= efivarfs_delete_dentry
,
1114 static struct dentry
*efivarfs_alloc_dentry(struct dentry
*parent
, char *name
)
1121 q
.len
= strlen(name
);
1123 err
= efivarfs_d_hash(NULL
, NULL
, &q
);
1125 return ERR_PTR(err
);
1127 d
= d_alloc(parent
, &q
);
1131 return ERR_PTR(-ENOMEM
);
1134 static int efivarfs_fill_super(struct super_block
*sb
, void *data
, int silent
)
1136 struct inode
*inode
= NULL
;
1137 struct dentry
*root
;
1138 struct efivar_entry
*entry
, *n
;
1139 struct efivars
*efivars
= &__efivars
;
1145 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1146 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1147 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1148 sb
->s_magic
= EFIVARFS_MAGIC
;
1149 sb
->s_op
= &efivarfs_ops
;
1150 sb
->s_d_op
= &efivarfs_d_ops
;
1151 sb
->s_time_gran
= 1;
1153 inode
= efivarfs_get_inode(sb
, NULL
, S_IFDIR
| 0755, 0);
1156 inode
->i_op
= &efivarfs_dir_inode_operations
;
1158 root
= d_make_root(inode
);
1163 list_for_each_entry_safe(entry
, n
, &efivars
->list
, list
) {
1164 struct dentry
*dentry
, *root
= efivarfs_sb
->s_root
;
1165 unsigned long size
= 0;
1170 len
= ucs2_strlen(entry
->var
.VariableName
);
1172 /* name, plus '-', plus GUID, plus NUL*/
1173 name
= kmalloc(len
+ 1 + GUID_LEN
+ 1, GFP_ATOMIC
);
1177 for (i
= 0; i
< len
; i
++)
1178 name
[i
] = entry
->var
.VariableName
[i
] & 0xFF;
1182 efi_guid_unparse(&entry
->var
.VendorGuid
, name
+ len
+ 1);
1184 name
[len
+GUID_LEN
+1] = '\0';
1186 inode
= efivarfs_get_inode(efivarfs_sb
, root
->d_inode
,
1191 dentry
= efivarfs_alloc_dentry(root
, name
);
1192 if (IS_ERR(dentry
)) {
1193 err
= PTR_ERR(dentry
);
1197 /* copied by the above to local storage in the dentry. */
1200 spin_lock_irq(&efivars
->lock
);
1201 efivars
->ops
->get_variable(entry
->var
.VariableName
,
1202 &entry
->var
.VendorGuid
,
1203 &entry
->var
.Attributes
,
1206 spin_unlock_irq(&efivars
->lock
);
1208 mutex_lock(&inode
->i_mutex
);
1209 inode
->i_private
= entry
;
1210 i_size_write(inode
, size
+ sizeof(entry
->var
.Attributes
));
1211 mutex_unlock(&inode
->i_mutex
);
1212 d_add(dentry
, inode
);
1225 static struct dentry
*efivarfs_mount(struct file_system_type
*fs_type
,
1226 int flags
, const char *dev_name
, void *data
)
1228 return mount_single(fs_type
, flags
, data
, efivarfs_fill_super
);
1231 static void efivarfs_kill_sb(struct super_block
*sb
)
1233 kill_litter_super(sb
);
1237 static struct file_system_type efivarfs_type
= {
1239 .mount
= efivarfs_mount
,
1240 .kill_sb
= efivarfs_kill_sb
,
1242 MODULE_ALIAS_FS("efivarfs");
1245 * Handle negative dentry.
1247 static struct dentry
*efivarfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1250 if (dentry
->d_name
.len
> NAME_MAX
)
1251 return ERR_PTR(-ENAMETOOLONG
);
1252 d_add(dentry
, NULL
);
1256 static const struct inode_operations efivarfs_dir_inode_operations
= {
1257 .lookup
= efivarfs_lookup
,
1258 .unlink
= efivarfs_unlink
,
1259 .create
= efivarfs_create
,
1262 #ifdef CONFIG_EFI_VARS_PSTORE
1264 static int efi_pstore_open(struct pstore_info
*psi
)
1266 struct efivars
*efivars
= psi
->data
;
1268 spin_lock_irq(&efivars
->lock
);
1269 efivars
->walk_entry
= list_first_entry(&efivars
->list
,
1270 struct efivar_entry
, list
);
1274 static int efi_pstore_close(struct pstore_info
*psi
)
1276 struct efivars
*efivars
= psi
->data
;
1278 spin_unlock_irq(&efivars
->lock
);
1282 static ssize_t
efi_pstore_read(u64
*id
, enum pstore_type_id
*type
,
1283 int *count
, struct timespec
*timespec
,
1284 char **buf
, struct pstore_info
*psi
)
1286 efi_guid_t vendor
= LINUX_EFI_CRASH_GUID
;
1287 struct efivars
*efivars
= psi
->data
;
1288 char name
[DUMP_NAME_LEN
];
1291 unsigned int part
, size
;
1294 while (&efivars
->walk_entry
->list
!= &efivars
->list
) {
1295 if (!efi_guidcmp(efivars
->walk_entry
->var
.VendorGuid
,
1297 for (i
= 0; i
< DUMP_NAME_LEN
; i
++) {
1298 name
[i
] = efivars
->walk_entry
->var
.VariableName
[i
];
1300 if (sscanf(name
, "dump-type%u-%u-%d-%lu",
1301 type
, &part
, &cnt
, &time
) == 4) {
1304 timespec
->tv_sec
= time
;
1305 timespec
->tv_nsec
= 0;
1306 } else if (sscanf(name
, "dump-type%u-%u-%lu",
1307 type
, &part
, &time
) == 3) {
1309 * Check if an old format,
1310 * which doesn't support holding
1311 * multiple logs, remains.
1315 timespec
->tv_sec
= time
;
1316 timespec
->tv_nsec
= 0;
1318 efivars
->walk_entry
= list_entry(
1319 efivars
->walk_entry
->list
.next
,
1320 struct efivar_entry
, list
);
1324 get_var_data_locked(efivars
, &efivars
->walk_entry
->var
);
1325 size
= efivars
->walk_entry
->var
.DataSize
;
1326 *buf
= kmalloc(size
, GFP_KERNEL
);
1329 memcpy(*buf
, efivars
->walk_entry
->var
.Data
,
1331 efivars
->walk_entry
= list_entry(
1332 efivars
->walk_entry
->list
.next
,
1333 struct efivar_entry
, list
);
1336 efivars
->walk_entry
= list_entry(efivars
->walk_entry
->list
.next
,
1337 struct efivar_entry
, list
);
1342 static int efi_pstore_write(enum pstore_type_id type
,
1343 enum kmsg_dump_reason reason
, u64
*id
,
1344 unsigned int part
, int count
, size_t size
,
1345 struct pstore_info
*psi
)
1347 char name
[DUMP_NAME_LEN
];
1348 efi_char16_t efi_name
[DUMP_NAME_LEN
];
1349 efi_guid_t vendor
= LINUX_EFI_CRASH_GUID
;
1350 struct efivars
*efivars
= psi
->data
;
1352 efi_status_t status
= EFI_NOT_FOUND
;
1353 unsigned long flags
;
1355 if (pstore_cannot_block_path(reason
)) {
1357 * If the lock is taken by another cpu in non-blocking path,
1358 * this driver returns without entering firmware to avoid
1361 if (!spin_trylock_irqsave(&efivars
->lock
, flags
))
1364 spin_lock_irqsave(&efivars
->lock
, flags
);
1367 * Check if there is a space enough to log.
1368 * size: a size of logging data
1369 * DUMP_NAME_LEN * 2: a maximum size of variable name
1372 status
= check_var_size_locked(efivars
, PSTORE_EFI_ATTRIBUTES
,
1373 size
+ DUMP_NAME_LEN
* 2);
1376 spin_unlock_irqrestore(&efivars
->lock
, flags
);
1381 sprintf(name
, "dump-type%u-%u-%d-%lu", type
, part
, count
,
1384 for (i
= 0; i
< DUMP_NAME_LEN
; i
++)
1385 efi_name
[i
] = name
[i
];
1387 efivars
->ops
->set_variable(efi_name
, &vendor
, PSTORE_EFI_ATTRIBUTES
,
1390 spin_unlock_irqrestore(&efivars
->lock
, flags
);
1392 if (reason
== KMSG_DUMP_OOPS
&& efivar_wq_enabled
)
1393 schedule_work(&efivar_work
);
1399 static int efi_pstore_erase(enum pstore_type_id type
, u64 id
, int count
,
1400 struct timespec time
, struct pstore_info
*psi
)
1402 char name
[DUMP_NAME_LEN
];
1403 efi_char16_t efi_name
[DUMP_NAME_LEN
];
1404 char name_old
[DUMP_NAME_LEN
];
1405 efi_char16_t efi_name_old
[DUMP_NAME_LEN
];
1406 efi_guid_t vendor
= LINUX_EFI_CRASH_GUID
;
1407 struct efivars
*efivars
= psi
->data
;
1408 struct efivar_entry
*entry
, *found
= NULL
;
1411 sprintf(name
, "dump-type%u-%u-%d-%lu", type
, (unsigned int)id
, count
,
1414 spin_lock_irq(&efivars
->lock
);
1416 for (i
= 0; i
< DUMP_NAME_LEN
; i
++)
1417 efi_name
[i
] = name
[i
];
1420 * Clean up an entry with the same name
1423 list_for_each_entry(entry
, &efivars
->list
, list
) {
1424 get_var_data_locked(efivars
, &entry
->var
);
1426 if (efi_guidcmp(entry
->var
.VendorGuid
, vendor
))
1428 if (ucs2_strncmp(entry
->var
.VariableName
, efi_name
,
1429 ucs2_strlen(efi_name
))) {
1431 * Check if an old format,
1432 * which doesn't support holding
1433 * multiple logs, remains.
1435 sprintf(name_old
, "dump-type%u-%u-%lu", type
,
1436 (unsigned int)id
, time
.tv_sec
);
1438 for (i
= 0; i
< DUMP_NAME_LEN
; i
++)
1439 efi_name_old
[i
] = name_old
[i
];
1441 if (ucs2_strncmp(entry
->var
.VariableName
, efi_name_old
,
1442 ucs2_strlen(efi_name_old
)))
1448 efivars
->ops
->set_variable(entry
->var
.VariableName
,
1449 &entry
->var
.VendorGuid
,
1450 PSTORE_EFI_ATTRIBUTES
,
1456 list_del(&found
->list
);
1458 spin_unlock_irq(&efivars
->lock
);
1461 efivar_unregister(found
);
1466 static struct pstore_info efi_pstore_info
= {
1467 .owner
= THIS_MODULE
,
1469 .open
= efi_pstore_open
,
1470 .close
= efi_pstore_close
,
1471 .read
= efi_pstore_read
,
1472 .write
= efi_pstore_write
,
1473 .erase
= efi_pstore_erase
,
1476 static void efivar_pstore_register(struct efivars
*efivars
)
1478 efivars
->efi_pstore_info
= efi_pstore_info
;
1479 efivars
->efi_pstore_info
.buf
= kmalloc(4096, GFP_KERNEL
);
1480 if (efivars
->efi_pstore_info
.buf
) {
1481 efivars
->efi_pstore_info
.bufsize
= 1024;
1482 efivars
->efi_pstore_info
.data
= efivars
;
1483 spin_lock_init(&efivars
->efi_pstore_info
.buf_lock
);
1484 pstore_register(&efivars
->efi_pstore_info
);
1488 static void efivar_pstore_register(struct efivars
*efivars
)
1494 static ssize_t
efivar_create(struct file
*filp
, struct kobject
*kobj
,
1495 struct bin_attribute
*bin_attr
,
1496 char *buf
, loff_t pos
, size_t count
)
1498 struct efi_variable
*new_var
= (struct efi_variable
*)buf
;
1499 struct efivars
*efivars
= bin_attr
->private;
1500 struct efivar_entry
*search_efivar
, *n
;
1501 unsigned long strsize1
, strsize2
;
1502 efi_status_t status
= EFI_NOT_FOUND
;
1505 if (!capable(CAP_SYS_ADMIN
))
1508 if ((new_var
->Attributes
& ~EFI_VARIABLE_MASK
) != 0 ||
1509 validate_var(new_var
, new_var
->Data
, new_var
->DataSize
) == false) {
1510 printk(KERN_ERR
"efivars: Malformed variable content\n");
1514 spin_lock_irq(&efivars
->lock
);
1517 * Does this variable already exist?
1519 list_for_each_entry_safe(search_efivar
, n
, &efivars
->list
, list
) {
1520 strsize1
= ucs2_strsize(search_efivar
->var
.VariableName
, 1024);
1521 strsize2
= ucs2_strsize(new_var
->VariableName
, 1024);
1522 if (strsize1
== strsize2
&&
1523 !memcmp(&(search_efivar
->var
.VariableName
),
1524 new_var
->VariableName
, strsize1
) &&
1525 !efi_guidcmp(search_efivar
->var
.VendorGuid
,
1526 new_var
->VendorGuid
)) {
1532 spin_unlock_irq(&efivars
->lock
);
1536 status
= check_var_size_locked(efivars
, new_var
->Attributes
,
1537 new_var
->DataSize
+ ucs2_strsize(new_var
->VariableName
, 1024));
1539 if (status
&& status
!= EFI_UNSUPPORTED
) {
1540 spin_unlock_irq(&efivars
->lock
);
1541 return efi_status_to_err(status
);
1544 /* now *really* create the variable via EFI */
1545 status
= efivars
->ops
->set_variable(new_var
->VariableName
,
1546 &new_var
->VendorGuid
,
1547 new_var
->Attributes
,
1551 if (status
!= EFI_SUCCESS
) {
1552 printk(KERN_WARNING
"efivars: set_variable() failed: status=%lx\n",
1554 spin_unlock_irq(&efivars
->lock
);
1557 spin_unlock_irq(&efivars
->lock
);
1559 /* Create the entry in sysfs. Locking is not required here */
1560 status
= efivar_create_sysfs_entry(efivars
,
1561 ucs2_strsize(new_var
->VariableName
,
1563 new_var
->VariableName
,
1564 &new_var
->VendorGuid
);
1566 printk(KERN_WARNING
"efivars: variable created, but sysfs entry wasn't.\n");
1571 static ssize_t
efivar_delete(struct file
*filp
, struct kobject
*kobj
,
1572 struct bin_attribute
*bin_attr
,
1573 char *buf
, loff_t pos
, size_t count
)
1575 struct efi_variable
*del_var
= (struct efi_variable
*)buf
;
1576 struct efivars
*efivars
= bin_attr
->private;
1577 struct efivar_entry
*search_efivar
, *n
;
1578 unsigned long strsize1
, strsize2
;
1579 efi_status_t status
= EFI_NOT_FOUND
;
1582 if (!capable(CAP_SYS_ADMIN
))
1585 spin_lock_irq(&efivars
->lock
);
1588 * Does this variable already exist?
1590 list_for_each_entry_safe(search_efivar
, n
, &efivars
->list
, list
) {
1591 strsize1
= ucs2_strsize(search_efivar
->var
.VariableName
, 1024);
1592 strsize2
= ucs2_strsize(del_var
->VariableName
, 1024);
1593 if (strsize1
== strsize2
&&
1594 !memcmp(&(search_efivar
->var
.VariableName
),
1595 del_var
->VariableName
, strsize1
) &&
1596 !efi_guidcmp(search_efivar
->var
.VendorGuid
,
1597 del_var
->VendorGuid
)) {
1603 spin_unlock_irq(&efivars
->lock
);
1606 /* force the Attributes/DataSize to 0 to ensure deletion */
1607 del_var
->Attributes
= 0;
1608 del_var
->DataSize
= 0;
1610 status
= efivars
->ops
->set_variable(del_var
->VariableName
,
1611 &del_var
->VendorGuid
,
1612 del_var
->Attributes
,
1616 if (status
!= EFI_SUCCESS
) {
1617 printk(KERN_WARNING
"efivars: set_variable() failed: status=%lx\n",
1619 spin_unlock_irq(&efivars
->lock
);
1622 list_del(&search_efivar
->list
);
1623 /* We need to release this lock before unregistering. */
1624 spin_unlock_irq(&efivars
->lock
);
1625 efivar_unregister(search_efivar
);
1627 /* It's dead Jim.... */
1631 static bool variable_is_present(efi_char16_t
*variable_name
, efi_guid_t
*vendor
)
1633 struct efivar_entry
*entry
, *n
;
1634 struct efivars
*efivars
= &__efivars
;
1635 unsigned long strsize1
, strsize2
;
1638 strsize1
= ucs2_strsize(variable_name
, 1024);
1639 list_for_each_entry_safe(entry
, n
, &efivars
->list
, list
) {
1640 strsize2
= ucs2_strsize(entry
->var
.VariableName
, 1024);
1641 if (strsize1
== strsize2
&&
1642 !memcmp(variable_name
, &(entry
->var
.VariableName
),
1644 !efi_guidcmp(entry
->var
.VendorGuid
,
1654 * Returns the size of variable_name, in bytes, including the
1655 * terminating NULL character, or variable_name_size if no NULL
1656 * character is found among the first variable_name_size bytes.
1658 static unsigned long var_name_strnsize(efi_char16_t
*variable_name
,
1659 unsigned long variable_name_size
)
1665 * The variable name is, by definition, a NULL-terminated
1666 * string, so make absolutely sure that variable_name_size is
1667 * the value we expect it to be. If not, return the real size.
1669 for (len
= 2; len
<= variable_name_size
; len
+= sizeof(c
)) {
1670 c
= variable_name
[(len
/ sizeof(c
)) - 1];
1675 return min(len
, variable_name_size
);
1678 static void efivar_update_sysfs_entries(struct work_struct
*work
)
1680 struct efivars
*efivars
= &__efivars
;
1682 efi_char16_t
*variable_name
;
1683 unsigned long variable_name_size
= 1024;
1684 efi_status_t status
= EFI_NOT_FOUND
;
1687 /* Add new sysfs entries */
1689 variable_name
= kzalloc(variable_name_size
, GFP_KERNEL
);
1690 if (!variable_name
) {
1691 pr_err("efivars: Memory allocation failed.\n");
1695 spin_lock_irq(&efivars
->lock
);
1698 variable_name_size
= 1024;
1699 status
= efivars
->ops
->get_next_variable(
1700 &variable_name_size
,
1703 if (status
!= EFI_SUCCESS
) {
1706 if (!variable_is_present(variable_name
,
1713 spin_unlock_irq(&efivars
->lock
);
1716 kfree(variable_name
);
1719 variable_name_size
= var_name_strnsize(variable_name
,
1720 variable_name_size
);
1721 efivar_create_sysfs_entry(efivars
,
1723 variable_name
, &vendor
);
1729 * Let's not leave out systab information that snuck into
1730 * the efivars driver
1732 static ssize_t
systab_show(struct kobject
*kobj
,
1733 struct kobj_attribute
*attr
, char *buf
)
1740 if (efi
.mps
!= EFI_INVALID_TABLE_ADDR
)
1741 str
+= sprintf(str
, "MPS=0x%lx\n", efi
.mps
);
1742 if (efi
.acpi20
!= EFI_INVALID_TABLE_ADDR
)
1743 str
+= sprintf(str
, "ACPI20=0x%lx\n", efi
.acpi20
);
1744 if (efi
.acpi
!= EFI_INVALID_TABLE_ADDR
)
1745 str
+= sprintf(str
, "ACPI=0x%lx\n", efi
.acpi
);
1746 if (efi
.smbios
!= EFI_INVALID_TABLE_ADDR
)
1747 str
+= sprintf(str
, "SMBIOS=0x%lx\n", efi
.smbios
);
1748 if (efi
.hcdp
!= EFI_INVALID_TABLE_ADDR
)
1749 str
+= sprintf(str
, "HCDP=0x%lx\n", efi
.hcdp
);
1750 if (efi
.boot_info
!= EFI_INVALID_TABLE_ADDR
)
1751 str
+= sprintf(str
, "BOOTINFO=0x%lx\n", efi
.boot_info
);
1752 if (efi
.uga
!= EFI_INVALID_TABLE_ADDR
)
1753 str
+= sprintf(str
, "UGA=0x%lx\n", efi
.uga
);
1758 static struct kobj_attribute efi_attr_systab
=
1759 __ATTR(systab
, 0400, systab_show
, NULL
);
1761 static struct attribute
*efi_subsys_attrs
[] = {
1762 &efi_attr_systab
.attr
,
1763 NULL
, /* maybe more in the future? */
1766 static struct attribute_group efi_subsys_attr_group
= {
1767 .attrs
= efi_subsys_attrs
,
1770 static struct kobject
*efi_kobj
;
1773 * efivar_create_sysfs_entry()
1775 * variable_name_size = number of bytes required to hold
1776 * variable_name (not counting the NULL
1777 * character at the end.
1778 * efivars->lock is not held on entry or exit.
1779 * Returns 1 on failure, 0 on success
1782 efivar_create_sysfs_entry(struct efivars
*efivars
,
1783 unsigned long variable_name_size
,
1784 efi_char16_t
*variable_name
,
1785 efi_guid_t
*vendor_guid
)
1787 int i
, short_name_size
;
1789 struct efivar_entry
*new_efivar
;
1792 * Length of the variable bytes in ASCII, plus the '-' separator,
1793 * plus the GUID, plus trailing NUL
1795 short_name_size
= variable_name_size
/ sizeof(efi_char16_t
)
1798 short_name
= kzalloc(short_name_size
, GFP_KERNEL
);
1799 new_efivar
= kzalloc(sizeof(struct efivar_entry
), GFP_KERNEL
);
1801 if (!short_name
|| !new_efivar
) {
1807 new_efivar
->efivars
= efivars
;
1808 memcpy(new_efivar
->var
.VariableName
, variable_name
,
1809 variable_name_size
);
1810 memcpy(&(new_efivar
->var
.VendorGuid
), vendor_guid
, sizeof(efi_guid_t
));
1812 /* Convert Unicode to normal chars (assume top bits are 0),
1814 for (i
=0; i
< (int)(variable_name_size
/ sizeof(efi_char16_t
)); i
++) {
1815 short_name
[i
] = variable_name
[i
] & 0xFF;
1817 /* This is ugly, but necessary to separate one vendor's
1818 private variables from another's. */
1820 *(short_name
+ strlen(short_name
)) = '-';
1821 efi_guid_unparse(vendor_guid
, short_name
+ strlen(short_name
));
1823 new_efivar
->kobj
.kset
= efivars
->kset
;
1824 i
= kobject_init_and_add(&new_efivar
->kobj
, &efivar_ktype
, NULL
,
1832 kobject_uevent(&new_efivar
->kobj
, KOBJ_ADD
);
1836 spin_lock_irq(&efivars
->lock
);
1837 list_add(&new_efivar
->list
, &efivars
->list
);
1838 spin_unlock_irq(&efivars
->lock
);
1844 create_efivars_bin_attributes(struct efivars
*efivars
)
1846 struct bin_attribute
*attr
;
1850 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
1854 attr
->attr
.name
= "new_var";
1855 attr
->attr
.mode
= 0200;
1856 attr
->write
= efivar_create
;
1857 attr
->private = efivars
;
1858 efivars
->new_var
= attr
;
1861 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
1866 attr
->attr
.name
= "del_var";
1867 attr
->attr
.mode
= 0200;
1868 attr
->write
= efivar_delete
;
1869 attr
->private = efivars
;
1870 efivars
->del_var
= attr
;
1872 sysfs_bin_attr_init(efivars
->new_var
);
1873 sysfs_bin_attr_init(efivars
->del_var
);
1876 error
= sysfs_create_bin_file(&efivars
->kset
->kobj
,
1879 printk(KERN_ERR
"efivars: unable to create new_var sysfs file"
1880 " due to error %d\n", error
);
1883 error
= sysfs_create_bin_file(&efivars
->kset
->kobj
,
1886 printk(KERN_ERR
"efivars: unable to create del_var sysfs file"
1887 " due to error %d\n", error
);
1888 sysfs_remove_bin_file(&efivars
->kset
->kobj
,
1895 kfree(efivars
->del_var
);
1896 efivars
->del_var
= NULL
;
1897 kfree(efivars
->new_var
);
1898 efivars
->new_var
= NULL
;
1902 void unregister_efivars(struct efivars
*efivars
)
1904 struct efivar_entry
*entry
, *n
;
1906 list_for_each_entry_safe(entry
, n
, &efivars
->list
, list
) {
1907 spin_lock_irq(&efivars
->lock
);
1908 list_del(&entry
->list
);
1909 spin_unlock_irq(&efivars
->lock
);
1910 efivar_unregister(entry
);
1912 if (efivars
->new_var
)
1913 sysfs_remove_bin_file(&efivars
->kset
->kobj
, efivars
->new_var
);
1914 if (efivars
->del_var
)
1915 sysfs_remove_bin_file(&efivars
->kset
->kobj
, efivars
->del_var
);
1916 kfree(efivars
->new_var
);
1917 kfree(efivars
->del_var
);
1918 kobject_put(efivars
->kobject
);
1919 kset_unregister(efivars
->kset
);
1921 EXPORT_SYMBOL_GPL(unregister_efivars
);
1924 * Print a warning when duplicate EFI variables are encountered and
1925 * disable the sysfs workqueue since the firmware is buggy.
1927 static void dup_variable_bug(efi_char16_t
*s16
, efi_guid_t
*vendor_guid
,
1928 unsigned long len16
)
1930 size_t i
, len8
= len16
/ sizeof(efi_char16_t
);
1934 * Disable the workqueue since the algorithm it uses for
1935 * detecting new variables won't work with this buggy
1936 * implementation of GetNextVariableName().
1938 efivar_wq_enabled
= false;
1940 s8
= kzalloc(len8
, GFP_KERNEL
);
1944 for (i
= 0; i
< len8
; i
++)
1947 printk(KERN_WARNING
"efivars: duplicate variable: %s-%pUl\n",
1952 int register_efivars(struct efivars
*efivars
,
1953 const struct efivar_operations
*ops
,
1954 struct kobject
*parent_kobj
)
1956 efi_status_t status
= EFI_NOT_FOUND
;
1957 efi_guid_t vendor_guid
;
1958 efi_char16_t
*variable_name
;
1959 unsigned long variable_name_size
= 1024;
1962 variable_name
= kzalloc(variable_name_size
, GFP_KERNEL
);
1963 if (!variable_name
) {
1964 printk(KERN_ERR
"efivars: Memory allocation failed.\n");
1968 spin_lock_init(&efivars
->lock
);
1969 INIT_LIST_HEAD(&efivars
->list
);
1972 efivars
->kset
= kset_create_and_add("vars", NULL
, parent_kobj
);
1973 if (!efivars
->kset
) {
1974 printk(KERN_ERR
"efivars: Subsystem registration failed.\n");
1979 efivars
->kobject
= kobject_create_and_add("efivars", parent_kobj
);
1980 if (!efivars
->kobject
) {
1981 pr_err("efivars: Subsystem registration failed.\n");
1983 kset_unregister(efivars
->kset
);
1988 * Per EFI spec, the maximum storage allocated for both
1989 * the variable name and variable data is 1024 bytes.
1993 variable_name_size
= 1024;
1995 status
= ops
->get_next_variable(&variable_name_size
,
2000 variable_name_size
= var_name_strnsize(variable_name
,
2001 variable_name_size
);
2004 * Some firmware implementations return the
2005 * same variable name on multiple calls to
2006 * get_next_variable(). Terminate the loop
2007 * immediately as there is no guarantee that
2008 * we'll ever see a different variable name,
2009 * and may end up looping here forever.
2011 if (variable_is_present(variable_name
, &vendor_guid
)) {
2012 dup_variable_bug(variable_name
, &vendor_guid
,
2013 variable_name_size
);
2014 status
= EFI_NOT_FOUND
;
2018 efivar_create_sysfs_entry(efivars
,
2026 printk(KERN_WARNING
"efivars: get_next_variable: status=%lx\n",
2028 status
= EFI_NOT_FOUND
;
2031 } while (status
!= EFI_NOT_FOUND
);
2033 error
= create_efivars_bin_attributes(efivars
);
2035 unregister_efivars(efivars
);
2037 if (!efivars_pstore_disable
)
2038 efivar_pstore_register(efivars
);
2040 register_filesystem(&efivarfs_type
);
2043 kfree(variable_name
);
2047 EXPORT_SYMBOL_GPL(register_efivars
);
2050 * For now we register the efi subsystem with the firmware subsystem
2051 * and the vars subsystem with the efi subsystem. In the future, it
2052 * might make sense to split off the efi subsystem into its own
2053 * driver, but for now only efivars will register with it, so just
2062 printk(KERN_INFO
"EFI Variables Facility v%s %s\n", EFIVARS_VERSION
,
2065 if (!efi_enabled(EFI_RUNTIME_SERVICES
))
2068 /* For now we'll register the efi directory at /sys/firmware/efi */
2069 efi_kobj
= kobject_create_and_add("efi", firmware_kobj
);
2071 printk(KERN_ERR
"efivars: Firmware registration failed.\n");
2075 ops
.get_variable
= efi
.get_variable
;
2076 ops
.set_variable
= efi
.set_variable
;
2077 ops
.get_next_variable
= efi
.get_next_variable
;
2078 ops
.query_variable_store
= efi_query_variable_store
;
2080 error
= register_efivars(&__efivars
, &ops
, efi_kobj
);
2084 /* Don't forget the systab entry */
2085 error
= sysfs_create_group(efi_kobj
, &efi_subsys_attr_group
);
2088 "efivars: Sysfs attribute export failed with error %d.\n",
2090 goto err_unregister
;
2096 unregister_efivars(&__efivars
);
2098 kobject_put(efi_kobj
);
2105 cancel_work_sync(&efivar_work
);
2107 if (efi_enabled(EFI_RUNTIME_SERVICES
)) {
2108 unregister_efivars(&__efivars
);
2109 kobject_put(efi_kobj
);
2113 module_init(efivars_init
);
2114 module_exit(efivars_exit
);