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>
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"
93 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
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 efi_variable var
;
129 struct list_head list
;
133 struct efivar_attribute
{
134 struct attribute attr
;
135 ssize_t (*show
) (struct efivar_entry
*entry
, char *buf
);
136 ssize_t (*store
)(struct efivar_entry
*entry
, const char *buf
, size_t count
);
139 /* Private pointer to registered efivars */
140 static struct efivars
*__efivars
;
142 #define PSTORE_EFI_ATTRIBUTES \
143 (EFI_VARIABLE_NON_VOLATILE | \
144 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
145 EFI_VARIABLE_RUNTIME_ACCESS)
147 #define EFIVAR_ATTR(_name, _mode, _show, _store) \
148 struct efivar_attribute efivar_attr_##_name = { \
149 .attr = {.name = __stringify(_name), .mode = _mode}, \
154 #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
155 #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
158 * Prototype for sysfs creation function
161 efivar_create_sysfs_entry(struct efivars
*efivars
,
162 unsigned long variable_name_size
,
163 efi_char16_t
*variable_name
,
164 efi_guid_t
*vendor_guid
);
167 * Prototype for workqueue functions updating sysfs entry
170 static void efivar_update_sysfs_entries(struct work_struct
*);
171 static DECLARE_WORK(efivar_work
, efivar_update_sysfs_entries
);
172 static bool efivar_wq_enabled
= true;
175 * Return the number of bytes is the length of this string
176 * Note: this is NOT the same as the number of unicode characters
178 static inline unsigned long
179 utf16_strsize(efi_char16_t
*data
, unsigned long maxlength
)
181 return utf16_strnlen(data
, maxlength
/sizeof(efi_char16_t
)) * sizeof(efi_char16_t
);
185 utf16_strncmp(const efi_char16_t
*a
, const efi_char16_t
*b
, size_t len
)
194 if (*a
== 0) /* implies *b == 0 */
203 validate_device_path(struct efi_variable
*var
, int match
, u8
*buffer
,
206 struct efi_generic_dev_path
*node
;
209 node
= (struct efi_generic_dev_path
*)buffer
;
211 if (len
< sizeof(*node
))
214 while (offset
<= len
- sizeof(*node
) &&
215 node
->length
>= sizeof(*node
) &&
216 node
->length
<= len
- offset
) {
217 offset
+= node
->length
;
219 if ((node
->type
== EFI_DEV_END_PATH
||
220 node
->type
== EFI_DEV_END_PATH2
) &&
221 node
->sub_type
== EFI_DEV_END_ENTIRE
)
224 node
= (struct efi_generic_dev_path
*)(buffer
+ offset
);
228 * If we're here then either node->length pointed past the end
229 * of the buffer or we reached the end of the buffer without
230 * finding a device path end node.
236 validate_boot_order(struct efi_variable
*var
, int match
, u8
*buffer
,
239 /* An array of 16-bit integers */
247 validate_load_option(struct efi_variable
*var
, int match
, u8
*buffer
,
251 int i
, desclength
= 0, namelen
;
253 namelen
= utf16_strnlen(var
->VariableName
, sizeof(var
->VariableName
));
255 /* Either "Boot" or "Driver" followed by four digits of hex */
256 for (i
= match
; i
< match
+4; i
++) {
257 if (var
->VariableName
[i
] > 127 ||
258 hex_to_bin(var
->VariableName
[i
] & 0xff) < 0)
262 /* Reject it if there's 4 digits of hex and then further content */
263 if (namelen
> match
+ 4)
266 /* A valid entry must be at least 8 bytes */
270 filepathlength
= buffer
[4] | buffer
[5] << 8;
273 * There's no stored length for the description, so it has to be
276 desclength
= utf16_strsize((efi_char16_t
*)(buffer
+ 6), len
- 6) + 2;
278 /* Each boot entry must have a descriptor */
283 * If the sum of the length of the description, the claimed filepath
284 * length and the original header are greater than the length of the
285 * variable, it's malformed
287 if ((desclength
+ filepathlength
+ 6) > len
)
291 * And, finally, check the filepath
293 return validate_device_path(var
, match
, buffer
+ desclength
+ 6,
298 validate_uint16(struct efi_variable
*var
, int match
, u8
*buffer
,
301 /* A single 16-bit integer */
309 validate_ascii_string(struct efi_variable
*var
, int match
, u8
*buffer
,
314 for (i
= 0; i
< len
; i
++) {
325 struct variable_validate
{
327 bool (*validate
)(struct efi_variable
*var
, int match
, u8
*data
,
331 static const struct variable_validate variable_validate
[] = {
332 { "BootNext", validate_uint16
},
333 { "BootOrder", validate_boot_order
},
334 { "DriverOrder", validate_boot_order
},
335 { "Boot*", validate_load_option
},
336 { "Driver*", validate_load_option
},
337 { "ConIn", validate_device_path
},
338 { "ConInDev", validate_device_path
},
339 { "ConOut", validate_device_path
},
340 { "ConOutDev", validate_device_path
},
341 { "ErrOut", validate_device_path
},
342 { "ErrOutDev", validate_device_path
},
343 { "Timeout", validate_uint16
},
344 { "Lang", validate_ascii_string
},
345 { "PlatformLang", validate_ascii_string
},
350 validate_var(struct efi_variable
*var
, u8
*data
, unsigned long len
)
353 u16
*unicode_name
= var
->VariableName
;
355 for (i
= 0; variable_validate
[i
].validate
!= NULL
; i
++) {
356 const char *name
= variable_validate
[i
].name
;
359 for (match
= 0; ; match
++) {
360 char c
= name
[match
];
361 u16 u
= unicode_name
[match
];
363 /* All special variables are plain ascii */
367 /* Wildcard in the matching name means we've matched */
369 return variable_validate
[i
].validate(var
,
372 /* Case sensitive match */
376 /* Reached the end of the string while matching */
378 return variable_validate
[i
].validate(var
,
387 get_var_data_locked(struct efivars
*efivars
, struct efi_variable
*var
)
391 var
->DataSize
= 1024;
392 status
= efivars
->ops
->get_variable(var
->VariableName
,
401 get_var_data(struct efivars
*efivars
, struct efi_variable
*var
)
406 spin_lock_irqsave(&efivars
->lock
, flags
);
407 status
= get_var_data_locked(efivars
, var
);
408 spin_unlock_irqrestore(&efivars
->lock
, flags
);
410 if (status
!= EFI_SUCCESS
) {
411 printk(KERN_WARNING
"efivars: get_variable() failed 0x%lx!\n",
418 check_var_size_locked(struct efivars
*efivars
, u32 attributes
,
421 u64 storage_size
, remaining_size
, max_size
;
423 const struct efivar_operations
*fops
= efivars
->ops
;
425 if (!efivars
->ops
->query_variable_info
)
426 return EFI_UNSUPPORTED
;
428 status
= fops
->query_variable_info(attributes
, &storage_size
,
429 &remaining_size
, &max_size
);
431 if (status
!= EFI_SUCCESS
)
434 if (!storage_size
|| size
> remaining_size
|| size
> max_size
||
435 (remaining_size
- size
) < (storage_size
/ 2))
436 return EFI_OUT_OF_RESOURCES
;
443 check_var_size(struct efivars
*efivars
, u32 attributes
, unsigned long size
)
448 spin_lock_irqsave(&efivars
->lock
, flags
);
449 status
= check_var_size_locked(efivars
, attributes
, size
);
450 spin_unlock_irqrestore(&efivars
->lock
, flags
);
456 efivar_guid_read(struct efivar_entry
*entry
, char *buf
)
458 struct efi_variable
*var
= &entry
->var
;
464 efi_guid_unparse(&var
->VendorGuid
, str
);
466 str
+= sprintf(str
, "\n");
472 efivar_attr_read(struct efivar_entry
*entry
, char *buf
)
474 struct efi_variable
*var
= &entry
->var
;
481 status
= get_var_data(__efivars
, var
);
482 if (status
!= EFI_SUCCESS
)
485 if (var
->Attributes
& EFI_VARIABLE_NON_VOLATILE
)
486 str
+= sprintf(str
, "EFI_VARIABLE_NON_VOLATILE\n");
487 if (var
->Attributes
& EFI_VARIABLE_BOOTSERVICE_ACCESS
)
488 str
+= sprintf(str
, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
489 if (var
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)
490 str
+= sprintf(str
, "EFI_VARIABLE_RUNTIME_ACCESS\n");
491 if (var
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
)
492 str
+= sprintf(str
, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
493 if (var
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
495 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
496 if (var
->Attributes
&
497 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)
499 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
500 if (var
->Attributes
& EFI_VARIABLE_APPEND_WRITE
)
501 str
+= sprintf(str
, "EFI_VARIABLE_APPEND_WRITE\n");
506 efivar_size_read(struct efivar_entry
*entry
, char *buf
)
508 struct efi_variable
*var
= &entry
->var
;
515 status
= get_var_data(__efivars
, var
);
516 if (status
!= EFI_SUCCESS
)
519 str
+= sprintf(str
, "0x%lx\n", var
->DataSize
);
524 efivar_data_read(struct efivar_entry
*entry
, char *buf
)
526 struct efi_variable
*var
= &entry
->var
;
532 status
= get_var_data(__efivars
, var
);
533 if (status
!= EFI_SUCCESS
)
536 memcpy(buf
, var
->Data
, var
->DataSize
);
537 return var
->DataSize
;
540 * We allow each variable to be edited via rewriting the
541 * entire efi variable structure.
544 efivar_store_raw(struct efivar_entry
*entry
, const char *buf
, size_t count
)
546 struct efi_variable
*new_var
, *var
= &entry
->var
;
547 struct efivars
*efivars
= __efivars
;
548 efi_status_t status
= EFI_NOT_FOUND
;
550 if (count
!= sizeof(struct efi_variable
))
553 new_var
= (struct efi_variable
*)buf
;
555 * If only updating the variable data, then the name
556 * and guid should remain the same
558 if (memcmp(new_var
->VariableName
, var
->VariableName
, sizeof(var
->VariableName
)) ||
559 efi_guidcmp(new_var
->VendorGuid
, var
->VendorGuid
)) {
560 printk(KERN_ERR
"efivars: Cannot edit the wrong variable!\n");
564 if ((new_var
->DataSize
<= 0) || (new_var
->Attributes
== 0)){
565 printk(KERN_ERR
"efivars: DataSize & Attributes must be valid!\n");
569 if ((new_var
->Attributes
& ~EFI_VARIABLE_MASK
) != 0 ||
570 validate_var(new_var
, new_var
->Data
, new_var
->DataSize
) == false) {
571 printk(KERN_ERR
"efivars: Malformed variable content\n");
575 spin_lock_irq(&efivars
->lock
);
577 status
= check_var_size_locked(efivars
, new_var
->Attributes
,
578 new_var
->DataSize
+ utf16_strsize(new_var
->VariableName
, 1024));
580 if (status
== EFI_SUCCESS
|| status
== EFI_UNSUPPORTED
)
581 status
= efivars
->ops
->set_variable(new_var
->VariableName
,
582 &new_var
->VendorGuid
,
587 spin_unlock_irq(&efivars
->lock
);
589 if (status
!= EFI_SUCCESS
) {
590 printk(KERN_WARNING
"efivars: set_variable() failed: status=%lx\n",
595 memcpy(&entry
->var
, new_var
, count
);
600 efivar_show_raw(struct efivar_entry
*entry
, char *buf
)
602 struct efi_variable
*var
= &entry
->var
;
608 status
= get_var_data(__efivars
, var
);
609 if (status
!= EFI_SUCCESS
)
612 memcpy(buf
, var
, sizeof(*var
));
617 * Generic read/write functions that call the specific functions of
620 static ssize_t
efivar_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
623 struct efivar_entry
*var
= to_efivar_entry(kobj
);
624 struct efivar_attribute
*efivar_attr
= to_efivar_attr(attr
);
627 if (!capable(CAP_SYS_ADMIN
))
630 if (efivar_attr
->show
) {
631 ret
= efivar_attr
->show(var
, buf
);
636 static ssize_t
efivar_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
637 const char *buf
, size_t count
)
639 struct efivar_entry
*var
= to_efivar_entry(kobj
);
640 struct efivar_attribute
*efivar_attr
= to_efivar_attr(attr
);
643 if (!capable(CAP_SYS_ADMIN
))
646 if (efivar_attr
->store
)
647 ret
= efivar_attr
->store(var
, buf
, count
);
652 static const struct sysfs_ops efivar_attr_ops
= {
653 .show
= efivar_attr_show
,
654 .store
= efivar_attr_store
,
657 static void efivar_release(struct kobject
*kobj
)
659 struct efivar_entry
*var
= container_of(kobj
, struct efivar_entry
, kobj
);
663 static EFIVAR_ATTR(guid
, 0400, efivar_guid_read
, NULL
);
664 static EFIVAR_ATTR(attributes
, 0400, efivar_attr_read
, NULL
);
665 static EFIVAR_ATTR(size
, 0400, efivar_size_read
, NULL
);
666 static EFIVAR_ATTR(data
, 0400, efivar_data_read
, NULL
);
667 static EFIVAR_ATTR(raw_var
, 0600, efivar_show_raw
, efivar_store_raw
);
669 static struct attribute
*def_attrs
[] = {
670 &efivar_attr_guid
.attr
,
671 &efivar_attr_size
.attr
,
672 &efivar_attr_attributes
.attr
,
673 &efivar_attr_data
.attr
,
674 &efivar_attr_raw_var
.attr
,
678 static struct kobj_type efivar_ktype
= {
679 .release
= efivar_release
,
680 .sysfs_ops
= &efivar_attr_ops
,
681 .default_attrs
= def_attrs
,
685 efivar_unregister(struct efivar_entry
*var
)
687 kobject_put(&var
->kobj
);
690 static int efivarfs_file_open(struct inode
*inode
, struct file
*file
)
692 file
->private_data
= inode
->i_private
;
696 static int efi_status_to_err(efi_status_t status
)
701 case EFI_INVALID_PARAMETER
:
704 case EFI_OUT_OF_RESOURCES
:
707 case EFI_DEVICE_ERROR
:
710 case EFI_WRITE_PROTECTED
:
713 case EFI_SECURITY_VIOLATION
:
726 static ssize_t
efivarfs_file_write(struct file
*file
,
727 const char __user
*userbuf
, size_t count
, loff_t
*ppos
)
729 struct efivar_entry
*var
= file
->private_data
;
730 struct efivars
*efivars
= __efivars
;
734 struct inode
*inode
= file
->f_mapping
->host
;
735 unsigned long datasize
= count
- sizeof(attributes
);
736 unsigned long newdatasize
, varsize
;
739 if (count
< sizeof(attributes
))
742 if (copy_from_user(&attributes
, userbuf
, sizeof(attributes
)))
745 if (attributes
& ~(EFI_VARIABLE_MASK
))
749 * Ensure that the user can't allocate arbitrarily large
750 * amounts of memory. Pick a default size of 64K if
751 * QueryVariableInfo() isn't supported by the firmware.
754 varsize
= datasize
+ utf16_strsize(var
->var
.VariableName
, 1024);
755 status
= check_var_size(efivars
, attributes
, varsize
);
757 if (status
!= EFI_SUCCESS
) {
758 if (status
!= EFI_UNSUPPORTED
)
759 return efi_status_to_err(status
);
761 if (datasize
> 65536)
765 data
= kmalloc(datasize
, GFP_KERNEL
);
769 if (copy_from_user(data
, userbuf
+ sizeof(attributes
), datasize
)) {
774 if (validate_var(&var
->var
, data
, datasize
) == false) {
780 * The lock here protects the get_variable call, the conditional
781 * set_variable call, and removal of the variable from the efivars
782 * list (in the case of an authenticated delete).
784 spin_lock_irq(&efivars
->lock
);
787 * Ensure that the available space hasn't shrunk below the safe level
790 status
= check_var_size_locked(efivars
, attributes
, varsize
);
792 if (status
!= EFI_SUCCESS
&& status
!= EFI_UNSUPPORTED
) {
793 spin_unlock_irq(&efivars
->lock
);
796 return efi_status_to_err(status
);
799 status
= efivars
->ops
->set_variable(var
->var
.VariableName
,
800 &var
->var
.VendorGuid
,
801 attributes
, datasize
,
804 if (status
!= EFI_SUCCESS
) {
805 spin_unlock_irq(&efivars
->lock
);
808 return efi_status_to_err(status
);
814 * Writing to the variable may have caused a change in size (which
815 * could either be an append or an overwrite), or the variable to be
816 * deleted. Perform a GetVariable() so we can tell what actually
820 status
= efivars
->ops
->get_variable(var
->var
.VariableName
,
821 &var
->var
.VendorGuid
,
825 if (status
== EFI_BUFFER_TOO_SMALL
) {
826 spin_unlock_irq(&efivars
->lock
);
827 mutex_lock(&inode
->i_mutex
);
828 i_size_write(inode
, newdatasize
+ sizeof(attributes
));
829 mutex_unlock(&inode
->i_mutex
);
831 } else if (status
== EFI_NOT_FOUND
) {
832 list_del(&var
->list
);
833 spin_unlock_irq(&efivars
->lock
);
834 efivar_unregister(var
);
836 d_delete(file
->f_dentry
);
837 dput(file
->f_dentry
);
840 spin_unlock_irq(&efivars
->lock
);
841 pr_warn("efivarfs: inconsistent EFI variable implementation? "
842 "status = %lx\n", status
);
851 static ssize_t
efivarfs_file_read(struct file
*file
, char __user
*userbuf
,
852 size_t count
, loff_t
*ppos
)
854 struct efivar_entry
*var
= file
->private_data
;
855 struct efivars
*efivars
= __efivars
;
857 unsigned long datasize
= 0;
862 spin_lock_irq(&efivars
->lock
);
863 status
= efivars
->ops
->get_variable(var
->var
.VariableName
,
864 &var
->var
.VendorGuid
,
865 &attributes
, &datasize
, NULL
);
866 spin_unlock_irq(&efivars
->lock
);
868 if (status
!= EFI_BUFFER_TOO_SMALL
)
869 return efi_status_to_err(status
);
871 data
= kmalloc(datasize
+ sizeof(attributes
), GFP_KERNEL
);
876 spin_lock_irq(&efivars
->lock
);
877 status
= efivars
->ops
->get_variable(var
->var
.VariableName
,
878 &var
->var
.VendorGuid
,
879 &attributes
, &datasize
,
880 (data
+ sizeof(attributes
)));
881 spin_unlock_irq(&efivars
->lock
);
883 if (status
!= EFI_SUCCESS
) {
884 size
= efi_status_to_err(status
);
888 memcpy(data
, &attributes
, sizeof(attributes
));
889 size
= simple_read_from_buffer(userbuf
, count
, ppos
,
890 data
, datasize
+ sizeof(attributes
));
897 static void efivarfs_evict_inode(struct inode
*inode
)
902 static const struct super_operations efivarfs_ops
= {
903 .statfs
= simple_statfs
,
904 .drop_inode
= generic_delete_inode
,
905 .evict_inode
= efivarfs_evict_inode
,
906 .show_options
= generic_show_options
,
909 static struct super_block
*efivarfs_sb
;
911 static const struct inode_operations efivarfs_dir_inode_operations
;
913 static const struct file_operations efivarfs_file_operations
= {
914 .open
= efivarfs_file_open
,
915 .read
= efivarfs_file_read
,
916 .write
= efivarfs_file_write
,
920 static struct inode
*efivarfs_get_inode(struct super_block
*sb
,
921 const struct inode
*dir
, int mode
, dev_t dev
)
923 struct inode
*inode
= new_inode(sb
);
926 inode
->i_ino
= get_next_ino();
927 inode
->i_mode
= mode
;
928 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
929 switch (mode
& S_IFMT
) {
931 inode
->i_fop
= &efivarfs_file_operations
;
934 inode
->i_op
= &efivarfs_dir_inode_operations
;
935 inode
->i_fop
= &simple_dir_operations
;
944 * Return true if 'str' is a valid efivarfs filename of the form,
946 * VariableName-12345678-1234-1234-1234-1234567891bc
948 static bool efivarfs_valid_name(const char *str
, int len
)
950 static const char dashes
[GUID_LEN
] = {
951 [8] = 1, [13] = 1, [18] = 1, [23] = 1
953 const char *s
= str
+ len
- GUID_LEN
;
957 * We need a GUID, plus at least one letter for the variable name,
958 * plus the '-' separator
960 if (len
< GUID_LEN
+ 2)
963 /* GUID must be preceded by a '-' */
968 * Validate that 's' is of the correct format, e.g.
970 * 12345678-1234-1234-1234-123456789abc
972 for (i
= 0; i
< GUID_LEN
; i
++) {
985 static void efivarfs_hex_to_guid(const char *str
, efi_guid_t
*guid
)
987 guid
->b
[0] = hex_to_bin(str
[6]) << 4 | hex_to_bin(str
[7]);
988 guid
->b
[1] = hex_to_bin(str
[4]) << 4 | hex_to_bin(str
[5]);
989 guid
->b
[2] = hex_to_bin(str
[2]) << 4 | hex_to_bin(str
[3]);
990 guid
->b
[3] = hex_to_bin(str
[0]) << 4 | hex_to_bin(str
[1]);
991 guid
->b
[4] = hex_to_bin(str
[11]) << 4 | hex_to_bin(str
[12]);
992 guid
->b
[5] = hex_to_bin(str
[9]) << 4 | hex_to_bin(str
[10]);
993 guid
->b
[6] = hex_to_bin(str
[16]) << 4 | hex_to_bin(str
[17]);
994 guid
->b
[7] = hex_to_bin(str
[14]) << 4 | hex_to_bin(str
[15]);
995 guid
->b
[8] = hex_to_bin(str
[19]) << 4 | hex_to_bin(str
[20]);
996 guid
->b
[9] = hex_to_bin(str
[21]) << 4 | hex_to_bin(str
[22]);
997 guid
->b
[10] = hex_to_bin(str
[24]) << 4 | hex_to_bin(str
[25]);
998 guid
->b
[11] = hex_to_bin(str
[26]) << 4 | hex_to_bin(str
[27]);
999 guid
->b
[12] = hex_to_bin(str
[28]) << 4 | hex_to_bin(str
[29]);
1000 guid
->b
[13] = hex_to_bin(str
[30]) << 4 | hex_to_bin(str
[31]);
1001 guid
->b
[14] = hex_to_bin(str
[32]) << 4 | hex_to_bin(str
[33]);
1002 guid
->b
[15] = hex_to_bin(str
[34]) << 4 | hex_to_bin(str
[35]);
1005 static int efivarfs_create(struct inode
*dir
, struct dentry
*dentry
,
1006 umode_t mode
, bool excl
)
1008 struct inode
*inode
;
1009 struct efivars
*efivars
= __efivars
;
1010 struct efivar_entry
*var
;
1011 int namelen
, i
= 0, err
= 0;
1013 if (!efivarfs_valid_name(dentry
->d_name
.name
, dentry
->d_name
.len
))
1016 inode
= efivarfs_get_inode(dir
->i_sb
, dir
, mode
, 0);
1020 var
= kzalloc(sizeof(struct efivar_entry
), GFP_KERNEL
);
1026 /* length of the variable name itself: remove GUID and separator */
1027 namelen
= dentry
->d_name
.len
- GUID_LEN
- 1;
1029 efivarfs_hex_to_guid(dentry
->d_name
.name
+ namelen
+ 1,
1030 &var
->var
.VendorGuid
);
1032 for (i
= 0; i
< namelen
; i
++)
1033 var
->var
.VariableName
[i
] = dentry
->d_name
.name
[i
];
1035 var
->var
.VariableName
[i
] = '\0';
1037 inode
->i_private
= var
;
1038 var
->kobj
.kset
= efivars
->kset
;
1040 err
= kobject_init_and_add(&var
->kobj
, &efivar_ktype
, NULL
, "%s",
1041 dentry
->d_name
.name
);
1045 kobject_uevent(&var
->kobj
, KOBJ_ADD
);
1046 spin_lock_irq(&efivars
->lock
);
1047 list_add(&var
->list
, &efivars
->list
);
1048 spin_unlock_irq(&efivars
->lock
);
1049 d_instantiate(dentry
, inode
);
1059 static int efivarfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1061 struct efivar_entry
*var
= dentry
->d_inode
->i_private
;
1062 struct efivars
*efivars
= __efivars
;
1063 efi_status_t status
;
1065 spin_lock_irq(&efivars
->lock
);
1067 status
= efivars
->ops
->set_variable(var
->var
.VariableName
,
1068 &var
->var
.VendorGuid
,
1071 if (status
== EFI_SUCCESS
|| status
== EFI_NOT_FOUND
) {
1072 list_del(&var
->list
);
1073 spin_unlock_irq(&efivars
->lock
);
1074 efivar_unregister(var
);
1075 drop_nlink(dentry
->d_inode
);
1080 spin_unlock_irq(&efivars
->lock
);
1085 * Compare two efivarfs file names.
1087 * An efivarfs filename is composed of two parts,
1089 * 1. A case-sensitive variable name
1090 * 2. A case-insensitive GUID
1092 * So we need to perform a case-sensitive match on part 1 and a
1093 * case-insensitive match on part 2.
1095 static int efivarfs_d_compare(const struct dentry
*parent
, const struct inode
*pinode
,
1096 const struct dentry
*dentry
, const struct inode
*inode
,
1097 unsigned int len
, const char *str
,
1098 const struct qstr
*name
)
1100 int guid
= len
- GUID_LEN
;
1102 if (name
->len
!= len
)
1105 /* Case-sensitive compare for the variable name */
1106 if (memcmp(str
, name
->name
, guid
))
1109 /* Case-insensitive compare for the GUID */
1110 return strncasecmp(name
->name
+ guid
, str
+ guid
, GUID_LEN
);
1113 static int efivarfs_d_hash(const struct dentry
*dentry
,
1114 const struct inode
*inode
, struct qstr
*qstr
)
1116 unsigned long hash
= init_name_hash();
1117 const unsigned char *s
= qstr
->name
;
1118 unsigned int len
= qstr
->len
;
1120 if (!efivarfs_valid_name(s
, len
))
1123 while (len
-- > GUID_LEN
)
1124 hash
= partial_name_hash(*s
++, hash
);
1126 /* GUID is case-insensitive. */
1128 hash
= partial_name_hash(tolower(*s
++), hash
);
1130 qstr
->hash
= end_name_hash(hash
);
1135 * Retaining negative dentries for an in-memory filesystem just wastes
1136 * memory and lookup time: arrange for them to be deleted immediately.
1138 static int efivarfs_delete_dentry(const struct dentry
*dentry
)
1143 static struct dentry_operations efivarfs_d_ops
= {
1144 .d_compare
= efivarfs_d_compare
,
1145 .d_hash
= efivarfs_d_hash
,
1146 .d_delete
= efivarfs_delete_dentry
,
1149 static struct dentry
*efivarfs_alloc_dentry(struct dentry
*parent
, char *name
)
1156 q
.len
= strlen(name
);
1158 err
= efivarfs_d_hash(NULL
, NULL
, &q
);
1160 return ERR_PTR(err
);
1162 d
= d_alloc(parent
, &q
);
1166 return ERR_PTR(-ENOMEM
);
1169 static int efivarfs_fill_super(struct super_block
*sb
, void *data
, int silent
)
1171 struct inode
*inode
= NULL
;
1172 struct dentry
*root
;
1173 struct efivar_entry
*entry
, *n
;
1174 struct efivars
*efivars
= __efivars
;
1180 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1181 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1182 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1183 sb
->s_magic
= EFIVARFS_MAGIC
;
1184 sb
->s_op
= &efivarfs_ops
;
1185 sb
->s_d_op
= &efivarfs_d_ops
;
1186 sb
->s_time_gran
= 1;
1188 inode
= efivarfs_get_inode(sb
, NULL
, S_IFDIR
| 0755, 0);
1191 inode
->i_op
= &efivarfs_dir_inode_operations
;
1193 root
= d_make_root(inode
);
1198 list_for_each_entry_safe(entry
, n
, &efivars
->list
, list
) {
1199 struct dentry
*dentry
, *root
= efivarfs_sb
->s_root
;
1200 unsigned long size
= 0;
1205 len
= utf16_strlen(entry
->var
.VariableName
);
1207 /* name, plus '-', plus GUID, plus NUL*/
1208 name
= kmalloc(len
+ 1 + GUID_LEN
+ 1, GFP_ATOMIC
);
1212 for (i
= 0; i
< len
; i
++)
1213 name
[i
] = entry
->var
.VariableName
[i
] & 0xFF;
1217 efi_guid_unparse(&entry
->var
.VendorGuid
, name
+ len
+ 1);
1219 name
[len
+GUID_LEN
+1] = '\0';
1221 inode
= efivarfs_get_inode(efivarfs_sb
, root
->d_inode
,
1226 dentry
= efivarfs_alloc_dentry(root
, name
);
1227 if (IS_ERR(dentry
)) {
1228 err
= PTR_ERR(dentry
);
1232 /* copied by the above to local storage in the dentry. */
1235 spin_lock_irq(&efivars
->lock
);
1236 efivars
->ops
->get_variable(entry
->var
.VariableName
,
1237 &entry
->var
.VendorGuid
,
1238 &entry
->var
.Attributes
,
1241 spin_unlock_irq(&efivars
->lock
);
1243 mutex_lock(&inode
->i_mutex
);
1244 inode
->i_private
= entry
;
1245 i_size_write(inode
, size
+ sizeof(entry
->var
.Attributes
));
1246 mutex_unlock(&inode
->i_mutex
);
1247 d_add(dentry
, inode
);
1260 static struct dentry
*efivarfs_mount(struct file_system_type
*fs_type
,
1261 int flags
, const char *dev_name
, void *data
)
1263 return mount_single(fs_type
, flags
, data
, efivarfs_fill_super
);
1266 static void efivarfs_kill_sb(struct super_block
*sb
)
1268 kill_litter_super(sb
);
1272 static struct file_system_type efivarfs_type
= {
1274 .mount
= efivarfs_mount
,
1275 .kill_sb
= efivarfs_kill_sb
,
1277 MODULE_ALIAS_FS("efivarfs");
1280 * Handle negative dentry.
1282 static struct dentry
*efivarfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1285 if (dentry
->d_name
.len
> NAME_MAX
)
1286 return ERR_PTR(-ENAMETOOLONG
);
1287 d_add(dentry
, NULL
);
1291 static const struct inode_operations efivarfs_dir_inode_operations
= {
1292 .lookup
= efivarfs_lookup
,
1293 .unlink
= efivarfs_unlink
,
1294 .create
= efivarfs_create
,
1297 #ifdef CONFIG_EFI_VARS_PSTORE
1299 static int efi_pstore_open(struct pstore_info
*psi
)
1301 struct efivars
*efivars
= __efivars
;
1303 spin_lock_irq(&efivars
->lock
);
1304 efivars
->walk_entry
= list_first_entry(&efivars
->list
,
1305 struct efivar_entry
, list
);
1309 static int efi_pstore_close(struct pstore_info
*psi
)
1311 struct efivars
*efivars
= __efivars
;
1313 spin_unlock_irq(&efivars
->lock
);
1317 static ssize_t
efi_pstore_read(u64
*id
, enum pstore_type_id
*type
,
1318 int *count
, struct timespec
*timespec
,
1319 char **buf
, struct pstore_info
*psi
)
1321 efi_guid_t vendor
= LINUX_EFI_CRASH_GUID
;
1322 struct efivars
*efivars
= __efivars
;
1323 char name
[DUMP_NAME_LEN
];
1326 unsigned int part
, size
;
1329 while (&efivars
->walk_entry
->list
!= &efivars
->list
) {
1330 if (!efi_guidcmp(efivars
->walk_entry
->var
.VendorGuid
,
1332 for (i
= 0; i
< DUMP_NAME_LEN
; i
++) {
1333 name
[i
] = efivars
->walk_entry
->var
.VariableName
[i
];
1335 if (sscanf(name
, "dump-type%u-%u-%d-%lu",
1336 type
, &part
, &cnt
, &time
) == 4) {
1339 timespec
->tv_sec
= time
;
1340 timespec
->tv_nsec
= 0;
1341 } else if (sscanf(name
, "dump-type%u-%u-%lu",
1342 type
, &part
, &time
) == 3) {
1344 * Check if an old format,
1345 * which doesn't support holding
1346 * multiple logs, remains.
1350 timespec
->tv_sec
= time
;
1351 timespec
->tv_nsec
= 0;
1353 efivars
->walk_entry
= list_entry(
1354 efivars
->walk_entry
->list
.next
,
1355 struct efivar_entry
, list
);
1359 get_var_data_locked(efivars
, &efivars
->walk_entry
->var
);
1360 size
= efivars
->walk_entry
->var
.DataSize
;
1361 *buf
= kmalloc(size
, GFP_KERNEL
);
1364 memcpy(*buf
, efivars
->walk_entry
->var
.Data
,
1366 efivars
->walk_entry
= list_entry(
1367 efivars
->walk_entry
->list
.next
,
1368 struct efivar_entry
, list
);
1371 efivars
->walk_entry
= list_entry(efivars
->walk_entry
->list
.next
,
1372 struct efivar_entry
, list
);
1377 static int efi_pstore_write(enum pstore_type_id type
,
1378 enum kmsg_dump_reason reason
, u64
*id
,
1379 unsigned int part
, int count
, size_t size
,
1380 struct pstore_info
*psi
)
1382 char name
[DUMP_NAME_LEN
];
1383 efi_char16_t efi_name
[DUMP_NAME_LEN
];
1384 efi_guid_t vendor
= LINUX_EFI_CRASH_GUID
;
1385 struct efivars
*efivars
= __efivars
;
1387 efi_status_t status
= EFI_NOT_FOUND
;
1388 unsigned long flags
;
1390 if (pstore_cannot_block_path(reason
)) {
1392 * If the lock is taken by another cpu in non-blocking path,
1393 * this driver returns without entering firmware to avoid
1396 if (!spin_trylock_irqsave(&efivars
->lock
, flags
))
1399 spin_lock_irqsave(&efivars
->lock
, flags
);
1402 * Check if there is a space enough to log.
1403 * size: a size of logging data
1404 * DUMP_NAME_LEN * 2: a maximum size of variable name
1407 status
= check_var_size_locked(efivars
, PSTORE_EFI_ATTRIBUTES
,
1408 size
+ DUMP_NAME_LEN
* 2);
1411 spin_unlock_irqrestore(&efivars
->lock
, flags
);
1416 sprintf(name
, "dump-type%u-%u-%d-%lu", type
, part
, count
,
1419 for (i
= 0; i
< DUMP_NAME_LEN
; i
++)
1420 efi_name
[i
] = name
[i
];
1422 efivars
->ops
->set_variable(efi_name
, &vendor
, PSTORE_EFI_ATTRIBUTES
,
1425 spin_unlock_irqrestore(&efivars
->lock
, flags
);
1427 if (reason
== KMSG_DUMP_OOPS
&& efivar_wq_enabled
)
1428 schedule_work(&efivar_work
);
1434 static int efi_pstore_erase(enum pstore_type_id type
, u64 id
, int count
,
1435 struct timespec time
, struct pstore_info
*psi
)
1437 char name
[DUMP_NAME_LEN
];
1438 efi_char16_t efi_name
[DUMP_NAME_LEN
];
1439 char name_old
[DUMP_NAME_LEN
];
1440 efi_char16_t efi_name_old
[DUMP_NAME_LEN
];
1441 efi_guid_t vendor
= LINUX_EFI_CRASH_GUID
;
1442 struct efivars
*efivars
= __efivars
;
1443 struct efivar_entry
*entry
, *found
= NULL
;
1446 sprintf(name
, "dump-type%u-%u-%d-%lu", type
, (unsigned int)id
, count
,
1449 spin_lock_irq(&efivars
->lock
);
1451 for (i
= 0; i
< DUMP_NAME_LEN
; i
++)
1452 efi_name
[i
] = name
[i
];
1455 * Clean up an entry with the same name
1458 list_for_each_entry(entry
, &efivars
->list
, list
) {
1459 get_var_data_locked(efivars
, &entry
->var
);
1461 if (efi_guidcmp(entry
->var
.VendorGuid
, vendor
))
1463 if (utf16_strncmp(entry
->var
.VariableName
, efi_name
,
1464 utf16_strlen(efi_name
))) {
1466 * Check if an old format,
1467 * which doesn't support holding
1468 * multiple logs, remains.
1470 sprintf(name_old
, "dump-type%u-%u-%lu", type
,
1471 (unsigned int)id
, time
.tv_sec
);
1473 for (i
= 0; i
< DUMP_NAME_LEN
; i
++)
1474 efi_name_old
[i
] = name_old
[i
];
1476 if (utf16_strncmp(entry
->var
.VariableName
, efi_name_old
,
1477 utf16_strlen(efi_name_old
)))
1483 efivars
->ops
->set_variable(entry
->var
.VariableName
,
1484 &entry
->var
.VendorGuid
,
1485 PSTORE_EFI_ATTRIBUTES
,
1491 list_del(&found
->list
);
1493 spin_unlock_irq(&efivars
->lock
);
1496 efivar_unregister(found
);
1501 static struct pstore_info efi_pstore_info
= {
1502 .owner
= THIS_MODULE
,
1504 .open
= efi_pstore_open
,
1505 .close
= efi_pstore_close
,
1506 .read
= efi_pstore_read
,
1507 .write
= efi_pstore_write
,
1508 .erase
= efi_pstore_erase
,
1511 static void efivar_pstore_register(struct efivars
*efivars
)
1513 efivars
->efi_pstore_info
= efi_pstore_info
;
1514 efivars
->efi_pstore_info
.buf
= kmalloc(4096, GFP_KERNEL
);
1515 if (efivars
->efi_pstore_info
.buf
) {
1516 efivars
->efi_pstore_info
.bufsize
= 1024;
1517 efivars
->efi_pstore_info
.data
= efivars
;
1518 spin_lock_init(&efivars
->efi_pstore_info
.buf_lock
);
1519 pstore_register(&efivars
->efi_pstore_info
);
1523 static void efivar_pstore_register(struct efivars
*efivars
)
1529 static ssize_t
efivar_create(struct file
*filp
, struct kobject
*kobj
,
1530 struct bin_attribute
*bin_attr
,
1531 char *buf
, loff_t pos
, size_t count
)
1533 struct efi_variable
*new_var
= (struct efi_variable
*)buf
;
1534 struct efivars
*efivars
= __efivars
;
1535 struct efivar_entry
*search_efivar
, *n
;
1536 unsigned long strsize1
, strsize2
;
1537 efi_status_t status
= EFI_NOT_FOUND
;
1540 if (!capable(CAP_SYS_ADMIN
))
1543 if ((new_var
->Attributes
& ~EFI_VARIABLE_MASK
) != 0 ||
1544 validate_var(new_var
, new_var
->Data
, new_var
->DataSize
) == false) {
1545 printk(KERN_ERR
"efivars: Malformed variable content\n");
1549 spin_lock_irq(&efivars
->lock
);
1552 * Does this variable already exist?
1554 list_for_each_entry_safe(search_efivar
, n
, &efivars
->list
, list
) {
1555 strsize1
= utf16_strsize(search_efivar
->var
.VariableName
, 1024);
1556 strsize2
= utf16_strsize(new_var
->VariableName
, 1024);
1557 if (strsize1
== strsize2
&&
1558 !memcmp(&(search_efivar
->var
.VariableName
),
1559 new_var
->VariableName
, strsize1
) &&
1560 !efi_guidcmp(search_efivar
->var
.VendorGuid
,
1561 new_var
->VendorGuid
)) {
1567 spin_unlock_irq(&efivars
->lock
);
1571 status
= check_var_size_locked(efivars
, new_var
->Attributes
,
1572 new_var
->DataSize
+ utf16_strsize(new_var
->VariableName
, 1024));
1574 if (status
&& status
!= EFI_UNSUPPORTED
) {
1575 spin_unlock_irq(&efivars
->lock
);
1576 return efi_status_to_err(status
);
1579 /* now *really* create the variable via EFI */
1580 status
= efivars
->ops
->set_variable(new_var
->VariableName
,
1581 &new_var
->VendorGuid
,
1582 new_var
->Attributes
,
1586 if (status
!= EFI_SUCCESS
) {
1587 printk(KERN_WARNING
"efivars: set_variable() failed: status=%lx\n",
1589 spin_unlock_irq(&efivars
->lock
);
1592 spin_unlock_irq(&efivars
->lock
);
1594 /* Create the entry in sysfs. Locking is not required here */
1595 status
= efivar_create_sysfs_entry(efivars
,
1596 utf16_strsize(new_var
->VariableName
,
1598 new_var
->VariableName
,
1599 &new_var
->VendorGuid
);
1601 printk(KERN_WARNING
"efivars: variable created, but sysfs entry wasn't.\n");
1606 static ssize_t
efivar_delete(struct file
*filp
, struct kobject
*kobj
,
1607 struct bin_attribute
*bin_attr
,
1608 char *buf
, loff_t pos
, size_t count
)
1610 struct efi_variable
*del_var
= (struct efi_variable
*)buf
;
1611 struct efivars
*efivars
= __efivars
;
1612 struct efivar_entry
*search_efivar
, *n
;
1613 unsigned long strsize1
, strsize2
;
1614 efi_status_t status
= EFI_NOT_FOUND
;
1617 if (!capable(CAP_SYS_ADMIN
))
1620 spin_lock_irq(&efivars
->lock
);
1623 * Does this variable already exist?
1625 list_for_each_entry_safe(search_efivar
, n
, &efivars
->list
, list
) {
1626 strsize1
= utf16_strsize(search_efivar
->var
.VariableName
, 1024);
1627 strsize2
= utf16_strsize(del_var
->VariableName
, 1024);
1628 if (strsize1
== strsize2
&&
1629 !memcmp(&(search_efivar
->var
.VariableName
),
1630 del_var
->VariableName
, strsize1
) &&
1631 !efi_guidcmp(search_efivar
->var
.VendorGuid
,
1632 del_var
->VendorGuid
)) {
1638 spin_unlock_irq(&efivars
->lock
);
1641 /* force the Attributes/DataSize to 0 to ensure deletion */
1642 del_var
->Attributes
= 0;
1643 del_var
->DataSize
= 0;
1645 status
= efivars
->ops
->set_variable(del_var
->VariableName
,
1646 &del_var
->VendorGuid
,
1647 del_var
->Attributes
,
1651 if (status
!= EFI_SUCCESS
) {
1652 printk(KERN_WARNING
"efivars: set_variable() failed: status=%lx\n",
1654 spin_unlock_irq(&efivars
->lock
);
1657 list_del(&search_efivar
->list
);
1658 /* We need to release this lock before unregistering. */
1659 spin_unlock_irq(&efivars
->lock
);
1660 efivar_unregister(search_efivar
);
1662 /* It's dead Jim.... */
1666 static bool variable_is_present(efi_char16_t
*variable_name
, efi_guid_t
*vendor
)
1668 struct efivar_entry
*entry
, *n
;
1669 struct efivars
*efivars
= __efivars
;
1670 unsigned long strsize1
, strsize2
;
1673 strsize1
= utf16_strsize(variable_name
, 1024);
1674 list_for_each_entry_safe(entry
, n
, &efivars
->list
, list
) {
1675 strsize2
= utf16_strsize(entry
->var
.VariableName
, 1024);
1676 if (strsize1
== strsize2
&&
1677 !memcmp(variable_name
, &(entry
->var
.VariableName
),
1679 !efi_guidcmp(entry
->var
.VendorGuid
,
1689 * Returns the size of variable_name, in bytes, including the
1690 * terminating NULL character, or variable_name_size if no NULL
1691 * character is found among the first variable_name_size bytes.
1693 static unsigned long var_name_strnsize(efi_char16_t
*variable_name
,
1694 unsigned long variable_name_size
)
1700 * The variable name is, by definition, a NULL-terminated
1701 * string, so make absolutely sure that variable_name_size is
1702 * the value we expect it to be. If not, return the real size.
1704 for (len
= 2; len
<= variable_name_size
; len
+= sizeof(c
)) {
1705 c
= variable_name
[(len
/ sizeof(c
)) - 1];
1710 return min(len
, variable_name_size
);
1713 static void efivar_update_sysfs_entries(struct work_struct
*work
)
1715 struct efivars
*efivars
= __efivars
;
1717 efi_char16_t
*variable_name
;
1718 unsigned long variable_name_size
= 1024;
1719 efi_status_t status
= EFI_NOT_FOUND
;
1722 /* Add new sysfs entries */
1724 variable_name
= kzalloc(variable_name_size
, GFP_KERNEL
);
1725 if (!variable_name
) {
1726 pr_err("efivars: Memory allocation failed.\n");
1730 spin_lock_irq(&efivars
->lock
);
1733 variable_name_size
= 1024;
1734 status
= efivars
->ops
->get_next_variable(
1735 &variable_name_size
,
1738 if (status
!= EFI_SUCCESS
) {
1741 if (!variable_is_present(variable_name
,
1748 spin_unlock_irq(&efivars
->lock
);
1751 kfree(variable_name
);
1754 variable_name_size
= var_name_strnsize(variable_name
,
1755 variable_name_size
);
1756 efivar_create_sysfs_entry(efivars
,
1758 variable_name
, &vendor
);
1764 * Let's not leave out systab information that snuck into
1765 * the efivars driver
1767 static ssize_t
systab_show(struct kobject
*kobj
,
1768 struct kobj_attribute
*attr
, char *buf
)
1775 if (efi
.mps
!= EFI_INVALID_TABLE_ADDR
)
1776 str
+= sprintf(str
, "MPS=0x%lx\n", efi
.mps
);
1777 if (efi
.acpi20
!= EFI_INVALID_TABLE_ADDR
)
1778 str
+= sprintf(str
, "ACPI20=0x%lx\n", efi
.acpi20
);
1779 if (efi
.acpi
!= EFI_INVALID_TABLE_ADDR
)
1780 str
+= sprintf(str
, "ACPI=0x%lx\n", efi
.acpi
);
1781 if (efi
.smbios
!= EFI_INVALID_TABLE_ADDR
)
1782 str
+= sprintf(str
, "SMBIOS=0x%lx\n", efi
.smbios
);
1783 if (efi
.hcdp
!= EFI_INVALID_TABLE_ADDR
)
1784 str
+= sprintf(str
, "HCDP=0x%lx\n", efi
.hcdp
);
1785 if (efi
.boot_info
!= EFI_INVALID_TABLE_ADDR
)
1786 str
+= sprintf(str
, "BOOTINFO=0x%lx\n", efi
.boot_info
);
1787 if (efi
.uga
!= EFI_INVALID_TABLE_ADDR
)
1788 str
+= sprintf(str
, "UGA=0x%lx\n", efi
.uga
);
1793 static struct kobj_attribute efi_attr_systab
=
1794 __ATTR(systab
, 0400, systab_show
, NULL
);
1796 static struct attribute
*efi_subsys_attrs
[] = {
1797 &efi_attr_systab
.attr
,
1798 NULL
, /* maybe more in the future? */
1801 static struct attribute_group efi_subsys_attr_group
= {
1802 .attrs
= efi_subsys_attrs
,
1805 static struct kobject
*efi_kobj
;
1808 * efivar_create_sysfs_entry()
1810 * variable_name_size = number of bytes required to hold
1811 * variable_name (not counting the NULL
1812 * character at the end.
1813 * efivars->lock is not held on entry or exit.
1814 * Returns 1 on failure, 0 on success
1817 efivar_create_sysfs_entry(struct efivars
*efivars
,
1818 unsigned long variable_name_size
,
1819 efi_char16_t
*variable_name
,
1820 efi_guid_t
*vendor_guid
)
1822 int i
, short_name_size
;
1824 struct efivar_entry
*new_efivar
;
1827 * Length of the variable bytes in ASCII, plus the '-' separator,
1828 * plus the GUID, plus trailing NUL
1830 short_name_size
= variable_name_size
/ sizeof(efi_char16_t
)
1833 short_name
= kzalloc(short_name_size
, GFP_KERNEL
);
1834 new_efivar
= kzalloc(sizeof(struct efivar_entry
), GFP_KERNEL
);
1836 if (!short_name
|| !new_efivar
) {
1842 memcpy(new_efivar
->var
.VariableName
, variable_name
,
1843 variable_name_size
);
1844 memcpy(&(new_efivar
->var
.VendorGuid
), vendor_guid
, sizeof(efi_guid_t
));
1846 /* Convert Unicode to normal chars (assume top bits are 0),
1848 for (i
=0; i
< (int)(variable_name_size
/ sizeof(efi_char16_t
)); i
++) {
1849 short_name
[i
] = variable_name
[i
] & 0xFF;
1851 /* This is ugly, but necessary to separate one vendor's
1852 private variables from another's. */
1854 *(short_name
+ strlen(short_name
)) = '-';
1855 efi_guid_unparse(vendor_guid
, short_name
+ strlen(short_name
));
1857 new_efivar
->kobj
.kset
= efivars
->kset
;
1858 i
= kobject_init_and_add(&new_efivar
->kobj
, &efivar_ktype
, NULL
,
1866 kobject_uevent(&new_efivar
->kobj
, KOBJ_ADD
);
1870 spin_lock_irq(&efivars
->lock
);
1871 list_add(&new_efivar
->list
, &efivars
->list
);
1872 spin_unlock_irq(&efivars
->lock
);
1878 create_efivars_bin_attributes(struct efivars
*efivars
)
1880 struct bin_attribute
*attr
;
1884 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
1888 attr
->attr
.name
= "new_var";
1889 attr
->attr
.mode
= 0200;
1890 attr
->write
= efivar_create
;
1891 attr
->private = efivars
;
1892 efivars
->new_var
= attr
;
1895 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
1900 attr
->attr
.name
= "del_var";
1901 attr
->attr
.mode
= 0200;
1902 attr
->write
= efivar_delete
;
1903 attr
->private = efivars
;
1904 efivars
->del_var
= attr
;
1906 sysfs_bin_attr_init(efivars
->new_var
);
1907 sysfs_bin_attr_init(efivars
->del_var
);
1910 error
= sysfs_create_bin_file(&efivars
->kset
->kobj
,
1913 printk(KERN_ERR
"efivars: unable to create new_var sysfs file"
1914 " due to error %d\n", error
);
1917 error
= sysfs_create_bin_file(&efivars
->kset
->kobj
,
1920 printk(KERN_ERR
"efivars: unable to create del_var sysfs file"
1921 " due to error %d\n", error
);
1922 sysfs_remove_bin_file(&efivars
->kset
->kobj
,
1929 kfree(efivars
->del_var
);
1930 efivars
->del_var
= NULL
;
1931 kfree(efivars
->new_var
);
1932 efivars
->new_var
= NULL
;
1936 void unregister_efivars(struct efivars
*efivars
)
1938 struct efivar_entry
*entry
, *n
;
1942 list_for_each_entry_safe(entry
, n
, &efivars
->list
, list
) {
1943 spin_lock_irq(&efivars
->lock
);
1944 list_del(&entry
->list
);
1945 spin_unlock_irq(&efivars
->lock
);
1946 efivar_unregister(entry
);
1948 if (efivars
->new_var
)
1949 sysfs_remove_bin_file(&efivars
->kset
->kobj
, efivars
->new_var
);
1950 if (efivars
->del_var
)
1951 sysfs_remove_bin_file(&efivars
->kset
->kobj
, efivars
->del_var
);
1952 kfree(efivars
->new_var
);
1953 kfree(efivars
->del_var
);
1954 kobject_put(efivars
->kobject
);
1955 kset_unregister(efivars
->kset
);
1957 EXPORT_SYMBOL_GPL(unregister_efivars
);
1960 * Print a warning when duplicate EFI variables are encountered and
1961 * disable the sysfs workqueue since the firmware is buggy.
1963 static void dup_variable_bug(efi_char16_t
*s16
, efi_guid_t
*vendor_guid
,
1964 unsigned long len16
)
1966 size_t i
, len8
= len16
/ sizeof(efi_char16_t
);
1970 * Disable the workqueue since the algorithm it uses for
1971 * detecting new variables won't work with this buggy
1972 * implementation of GetNextVariableName().
1974 efivar_wq_enabled
= false;
1976 s8
= kzalloc(len8
, GFP_KERNEL
);
1980 for (i
= 0; i
< len8
; i
++)
1983 printk(KERN_WARNING
"efivars: duplicate variable: %s-%pUl\n",
1988 int register_efivars(struct efivars
*efivars
,
1989 const struct efivar_operations
*ops
,
1990 struct kobject
*parent_kobj
)
1992 efi_status_t status
= EFI_NOT_FOUND
;
1993 efi_guid_t vendor_guid
;
1994 efi_char16_t
*variable_name
;
1995 unsigned long variable_name_size
= 1024;
1998 __efivars
= efivars
;
2000 variable_name
= kzalloc(variable_name_size
, GFP_KERNEL
);
2001 if (!variable_name
) {
2002 printk(KERN_ERR
"efivars: Memory allocation failed.\n");
2006 spin_lock_init(&efivars
->lock
);
2007 INIT_LIST_HEAD(&efivars
->list
);
2010 efivars
->kset
= kset_create_and_add("vars", NULL
, parent_kobj
);
2011 if (!efivars
->kset
) {
2012 printk(KERN_ERR
"efivars: Subsystem registration failed.\n");
2017 efivars
->kobject
= kobject_create_and_add("efivars", parent_kobj
);
2018 if (!efivars
->kobject
) {
2019 pr_err("efivars: Subsystem registration failed.\n");
2021 kset_unregister(efivars
->kset
);
2026 * Per EFI spec, the maximum storage allocated for both
2027 * the variable name and variable data is 1024 bytes.
2031 variable_name_size
= 1024;
2033 status
= ops
->get_next_variable(&variable_name_size
,
2038 variable_name_size
= var_name_strnsize(variable_name
,
2039 variable_name_size
);
2042 * Some firmware implementations return the
2043 * same variable name on multiple calls to
2044 * get_next_variable(). Terminate the loop
2045 * immediately as there is no guarantee that
2046 * we'll ever see a different variable name,
2047 * and may end up looping here forever.
2049 if (variable_is_present(variable_name
, &vendor_guid
)) {
2050 dup_variable_bug(variable_name
, &vendor_guid
,
2051 variable_name_size
);
2052 status
= EFI_NOT_FOUND
;
2056 efivar_create_sysfs_entry(efivars
,
2064 printk(KERN_WARNING
"efivars: get_next_variable: status=%lx\n",
2066 status
= EFI_NOT_FOUND
;
2069 } while (status
!= EFI_NOT_FOUND
);
2071 error
= create_efivars_bin_attributes(efivars
);
2073 unregister_efivars(efivars
);
2075 if (!efivars_pstore_disable
)
2076 efivar_pstore_register(efivars
);
2078 register_filesystem(&efivarfs_type
);
2081 kfree(variable_name
);
2085 EXPORT_SYMBOL_GPL(register_efivars
);
2087 static struct efivars generic_efivars
;
2088 static struct efivar_operations generic_ops
;
2090 static int generic_ops_register(void)
2092 generic_ops
.get_variable
= efi
.get_variable
;
2093 generic_ops
.set_variable
= efi
.set_variable
;
2094 generic_ops
.get_next_variable
= efi
.get_next_variable
;
2095 generic_ops
.query_variable_info
= efi
.query_variable_info
;
2097 return register_efivars(&generic_efivars
, &generic_ops
, efi_kobj
);
2100 static void generic_ops_unregister(void)
2102 unregister_efivars(&generic_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 error
= generic_ops_register();
2135 /* Don't forget the systab entry */
2136 error
= sysfs_create_group(efi_kobj
, &efi_subsys_attr_group
);
2139 "efivars: Sysfs attribute export failed with error %d.\n",
2141 goto err_unregister
;
2147 generic_ops_unregister();
2149 kobject_put(efi_kobj
);
2156 cancel_work_sync(&efivar_work
);
2158 if (efi_enabled(EFI_RUNTIME_SERVICES
)) {
2159 generic_ops_unregister();
2160 kobject_put(efi_kobj
);
2164 module_init(efivars_init
);
2165 module_exit(efivars_exit
);