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/ctype.h>
84 #include <linux/ramfs.h>
85 #include <linux/pagemap.h>
87 #include <asm/uaccess.h>
89 #define EFIVARS_VERSION "0.08"
90 #define EFIVARS_DATE "2004-May-17"
92 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
93 MODULE_DESCRIPTION("sysfs interface to EFI Variables");
94 MODULE_LICENSE("GPL");
95 MODULE_VERSION(EFIVARS_VERSION
);
97 static LIST_HEAD(efivarfs_list
);
98 LIST_HEAD(efivar_sysfs_list
);
99 EXPORT_SYMBOL_GPL(efivar_sysfs_list
);
101 struct efivar_attribute
{
102 struct attribute attr
;
103 ssize_t (*show
) (struct efivar_entry
*entry
, char *buf
);
104 ssize_t (*store
)(struct efivar_entry
*entry
, const char *buf
, size_t count
);
107 /* Private pointer to registered efivars */
108 static struct efivars
*__efivars
;
110 static struct kset
*efivars_kset
;
112 static struct bin_attribute
*efivars_new_var
;
113 static struct bin_attribute
*efivars_del_var
;
115 #define EFIVAR_ATTR(_name, _mode, _show, _store) \
116 struct efivar_attribute efivar_attr_##_name = { \
117 .attr = {.name = __stringify(_name), .mode = _mode}, \
122 #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
123 #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
126 * Prototype for sysfs creation function
129 efivar_create_sysfs_entry(struct efivar_entry
*new_var
);
132 * Prototype for workqueue functions updating sysfs entry
135 static void efivar_update_sysfs_entries(struct work_struct
*);
136 static DECLARE_WORK(efivar_work
, efivar_update_sysfs_entries
);
137 static bool efivar_wq_enabled
= true;
140 validate_device_path(struct efi_variable
*var
, int match
, u8
*buffer
,
143 struct efi_generic_dev_path
*node
;
146 node
= (struct efi_generic_dev_path
*)buffer
;
148 if (len
< sizeof(*node
))
151 while (offset
<= len
- sizeof(*node
) &&
152 node
->length
>= sizeof(*node
) &&
153 node
->length
<= len
- offset
) {
154 offset
+= node
->length
;
156 if ((node
->type
== EFI_DEV_END_PATH
||
157 node
->type
== EFI_DEV_END_PATH2
) &&
158 node
->sub_type
== EFI_DEV_END_ENTIRE
)
161 node
= (struct efi_generic_dev_path
*)(buffer
+ offset
);
165 * If we're here then either node->length pointed past the end
166 * of the buffer or we reached the end of the buffer without
167 * finding a device path end node.
173 validate_boot_order(struct efi_variable
*var
, int match
, u8
*buffer
,
176 /* An array of 16-bit integers */
184 validate_load_option(struct efi_variable
*var
, int match
, u8
*buffer
,
188 int i
, desclength
= 0, namelen
;
190 namelen
= utf16_strnlen(var
->VariableName
, sizeof(var
->VariableName
));
192 /* Either "Boot" or "Driver" followed by four digits of hex */
193 for (i
= match
; i
< match
+4; i
++) {
194 if (var
->VariableName
[i
] > 127 ||
195 hex_to_bin(var
->VariableName
[i
] & 0xff) < 0)
199 /* Reject it if there's 4 digits of hex and then further content */
200 if (namelen
> match
+ 4)
203 /* A valid entry must be at least 8 bytes */
207 filepathlength
= buffer
[4] | buffer
[5] << 8;
210 * There's no stored length for the description, so it has to be
213 desclength
= utf16_strsize((efi_char16_t
*)(buffer
+ 6), len
- 6) + 2;
215 /* Each boot entry must have a descriptor */
220 * If the sum of the length of the description, the claimed filepath
221 * length and the original header are greater than the length of the
222 * variable, it's malformed
224 if ((desclength
+ filepathlength
+ 6) > len
)
228 * And, finally, check the filepath
230 return validate_device_path(var
, match
, buffer
+ desclength
+ 6,
235 validate_uint16(struct efi_variable
*var
, int match
, u8
*buffer
,
238 /* A single 16-bit integer */
246 validate_ascii_string(struct efi_variable
*var
, int match
, u8
*buffer
,
251 for (i
= 0; i
< len
; i
++) {
262 struct variable_validate
{
264 bool (*validate
)(struct efi_variable
*var
, int match
, u8
*data
,
268 static const struct variable_validate variable_validate
[] = {
269 { "BootNext", validate_uint16
},
270 { "BootOrder", validate_boot_order
},
271 { "DriverOrder", validate_boot_order
},
272 { "Boot*", validate_load_option
},
273 { "Driver*", validate_load_option
},
274 { "ConIn", validate_device_path
},
275 { "ConInDev", validate_device_path
},
276 { "ConOut", validate_device_path
},
277 { "ConOutDev", validate_device_path
},
278 { "ErrOut", validate_device_path
},
279 { "ErrOutDev", validate_device_path
},
280 { "Timeout", validate_uint16
},
281 { "Lang", validate_ascii_string
},
282 { "PlatformLang", validate_ascii_string
},
287 efivar_validate(struct efi_variable
*var
, u8
*data
, unsigned long len
)
290 u16
*unicode_name
= var
->VariableName
;
292 for (i
= 0; variable_validate
[i
].validate
!= NULL
; i
++) {
293 const char *name
= variable_validate
[i
].name
;
296 for (match
= 0; ; match
++) {
297 char c
= name
[match
];
298 u16 u
= unicode_name
[match
];
300 /* All special variables are plain ascii */
304 /* Wildcard in the matching name means we've matched */
306 return variable_validate
[i
].validate(var
,
309 /* Case sensitive match */
313 /* Reached the end of the string while matching */
315 return variable_validate
[i
].validate(var
,
322 EXPORT_SYMBOL_GPL(efivar_validate
);
325 check_var_size(u32 attributes
, unsigned long size
)
327 u64 storage_size
, remaining_size
, max_size
;
329 const struct efivar_operations
*fops
= __efivars
->ops
;
331 if (!fops
->query_variable_info
)
332 return EFI_UNSUPPORTED
;
334 status
= fops
->query_variable_info(attributes
, &storage_size
,
335 &remaining_size
, &max_size
);
337 if (status
!= EFI_SUCCESS
)
340 if (!storage_size
|| size
> remaining_size
|| size
> max_size
||
341 (remaining_size
- size
) < (storage_size
/ 2))
342 return EFI_OUT_OF_RESOURCES
;
348 efivar_guid_read(struct efivar_entry
*entry
, char *buf
)
350 struct efi_variable
*var
= &entry
->var
;
356 efi_guid_unparse(&var
->VendorGuid
, str
);
358 str
+= sprintf(str
, "\n");
364 efivar_attr_read(struct efivar_entry
*entry
, char *buf
)
366 struct efi_variable
*var
= &entry
->var
;
372 var
->DataSize
= 1024;
373 if (efivar_entry_get(entry
, &var
->Attributes
, &var
->DataSize
, var
->Data
))
376 if (var
->Attributes
& EFI_VARIABLE_NON_VOLATILE
)
377 str
+= sprintf(str
, "EFI_VARIABLE_NON_VOLATILE\n");
378 if (var
->Attributes
& EFI_VARIABLE_BOOTSERVICE_ACCESS
)
379 str
+= sprintf(str
, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
380 if (var
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)
381 str
+= sprintf(str
, "EFI_VARIABLE_RUNTIME_ACCESS\n");
382 if (var
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
)
383 str
+= sprintf(str
, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
384 if (var
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
386 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
387 if (var
->Attributes
&
388 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)
390 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
391 if (var
->Attributes
& EFI_VARIABLE_APPEND_WRITE
)
392 str
+= sprintf(str
, "EFI_VARIABLE_APPEND_WRITE\n");
397 efivar_size_read(struct efivar_entry
*entry
, char *buf
)
399 struct efi_variable
*var
= &entry
->var
;
405 var
->DataSize
= 1024;
406 if (efivar_entry_get(entry
, &var
->Attributes
, &var
->DataSize
, var
->Data
))
409 str
+= sprintf(str
, "0x%lx\n", var
->DataSize
);
414 efivar_data_read(struct efivar_entry
*entry
, char *buf
)
416 struct efi_variable
*var
= &entry
->var
;
421 var
->DataSize
= 1024;
422 if (efivar_entry_get(entry
, &var
->Attributes
, &var
->DataSize
, var
->Data
))
425 memcpy(buf
, var
->Data
, var
->DataSize
);
426 return var
->DataSize
;
429 * We allow each variable to be edited via rewriting the
430 * entire efi variable structure.
433 efivar_store_raw(struct efivar_entry
*entry
, const char *buf
, size_t count
)
435 struct efi_variable
*new_var
, *var
= &entry
->var
;
438 if (count
!= sizeof(struct efi_variable
))
441 new_var
= (struct efi_variable
*)buf
;
443 * If only updating the variable data, then the name
444 * and guid should remain the same
446 if (memcmp(new_var
->VariableName
, var
->VariableName
, sizeof(var
->VariableName
)) ||
447 efi_guidcmp(new_var
->VendorGuid
, var
->VendorGuid
)) {
448 printk(KERN_ERR
"efivars: Cannot edit the wrong variable!\n");
452 if ((new_var
->DataSize
<= 0) || (new_var
->Attributes
== 0)){
453 printk(KERN_ERR
"efivars: DataSize & Attributes must be valid!\n");
457 if ((new_var
->Attributes
& ~EFI_VARIABLE_MASK
) != 0 ||
458 efivar_validate(new_var
, new_var
->Data
, new_var
->DataSize
) == false) {
459 printk(KERN_ERR
"efivars: Malformed variable content\n");
463 memcpy(&entry
->var
, new_var
, count
);
465 err
= efivar_entry_set(entry
, new_var
->Attributes
,
466 new_var
->DataSize
, new_var
->Data
, false);
468 printk(KERN_WARNING
"efivars: set_variable() failed: status=%d\n", err
);
476 efivar_show_raw(struct efivar_entry
*entry
, char *buf
)
478 struct efi_variable
*var
= &entry
->var
;
483 var
->DataSize
= 1024;
484 if (efivar_entry_get(entry
, &entry
->var
.Attributes
,
485 &entry
->var
.DataSize
, entry
->var
.Data
))
488 memcpy(buf
, var
, sizeof(*var
));
494 * Generic read/write functions that call the specific functions of
497 static ssize_t
efivar_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
500 struct efivar_entry
*var
= to_efivar_entry(kobj
);
501 struct efivar_attribute
*efivar_attr
= to_efivar_attr(attr
);
504 if (!capable(CAP_SYS_ADMIN
))
507 if (efivar_attr
->show
) {
508 ret
= efivar_attr
->show(var
, buf
);
513 static ssize_t
efivar_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
514 const char *buf
, size_t count
)
516 struct efivar_entry
*var
= to_efivar_entry(kobj
);
517 struct efivar_attribute
*efivar_attr
= to_efivar_attr(attr
);
520 if (!capable(CAP_SYS_ADMIN
))
523 if (efivar_attr
->store
)
524 ret
= efivar_attr
->store(var
, buf
, count
);
529 static const struct sysfs_ops efivar_attr_ops
= {
530 .show
= efivar_attr_show
,
531 .store
= efivar_attr_store
,
534 static void efivar_release(struct kobject
*kobj
)
536 struct efivar_entry
*var
= container_of(kobj
, struct efivar_entry
, kobj
);
540 static EFIVAR_ATTR(guid
, 0400, efivar_guid_read
, NULL
);
541 static EFIVAR_ATTR(attributes
, 0400, efivar_attr_read
, NULL
);
542 static EFIVAR_ATTR(size
, 0400, efivar_size_read
, NULL
);
543 static EFIVAR_ATTR(data
, 0400, efivar_data_read
, NULL
);
544 static EFIVAR_ATTR(raw_var
, 0600, efivar_show_raw
, efivar_store_raw
);
546 static struct attribute
*def_attrs
[] = {
547 &efivar_attr_guid
.attr
,
548 &efivar_attr_size
.attr
,
549 &efivar_attr_attributes
.attr
,
550 &efivar_attr_data
.attr
,
551 &efivar_attr_raw_var
.attr
,
555 static struct kobj_type efivar_ktype
= {
556 .release
= efivar_release
,
557 .sysfs_ops
= &efivar_attr_ops
,
558 .default_attrs
= def_attrs
,
561 static int efivarfs_file_open(struct inode
*inode
, struct file
*file
)
563 file
->private_data
= inode
->i_private
;
567 static int efi_status_to_err(efi_status_t status
)
575 case EFI_INVALID_PARAMETER
:
578 case EFI_OUT_OF_RESOURCES
:
581 case EFI_DEVICE_ERROR
:
584 case EFI_WRITE_PROTECTED
:
587 case EFI_SECURITY_VIOLATION
:
600 static ssize_t
efivarfs_file_write(struct file
*file
,
601 const char __user
*userbuf
, size_t count
, loff_t
*ppos
)
603 struct efivar_entry
*var
= file
->private_data
;
606 struct inode
*inode
= file
->f_mapping
->host
;
607 unsigned long datasize
= count
- sizeof(attributes
);
611 if (count
< sizeof(attributes
))
614 if (copy_from_user(&attributes
, userbuf
, sizeof(attributes
)))
617 if (attributes
& ~(EFI_VARIABLE_MASK
))
620 data
= kmalloc(datasize
, GFP_KERNEL
);
624 if (copy_from_user(data
, userbuf
+ sizeof(attributes
), datasize
)) {
629 bytes
= efivar_entry_set_get_size(var
, attributes
, &datasize
,
635 mutex_lock(&inode
->i_mutex
);
636 i_size_write(inode
, datasize
+ sizeof(attributes
));
637 mutex_unlock(&inode
->i_mutex
);
638 } else if (bytes
== -ENOENT
) {
640 d_delete(file
->f_dentry
);
641 dput(file
->f_dentry
);
643 pr_warn("efivarfs: inconsistent EFI variable implementation? "
644 "status=%zu\n", bytes
);
654 static ssize_t
efivarfs_file_read(struct file
*file
, char __user
*userbuf
,
655 size_t count
, loff_t
*ppos
)
657 struct efivar_entry
*var
= file
->private_data
;
658 unsigned long datasize
= 0;
664 err
= efivar_entry_size(var
, &datasize
);
668 data
= kmalloc(datasize
+ sizeof(attributes
), GFP_KERNEL
);
673 size
= efivar_entry_get(var
, &attributes
, &datasize
,
674 data
+ sizeof(attributes
));
678 memcpy(data
, &attributes
, sizeof(attributes
));
679 size
= simple_read_from_buffer(userbuf
, count
, ppos
,
680 data
, datasize
+ sizeof(attributes
));
687 static void efivarfs_evict_inode(struct inode
*inode
)
692 static const struct super_operations efivarfs_ops
= {
693 .statfs
= simple_statfs
,
694 .drop_inode
= generic_delete_inode
,
695 .evict_inode
= efivarfs_evict_inode
,
696 .show_options
= generic_show_options
,
699 static struct super_block
*efivarfs_sb
;
701 static const struct inode_operations efivarfs_dir_inode_operations
;
703 static const struct file_operations efivarfs_file_operations
= {
704 .open
= efivarfs_file_open
,
705 .read
= efivarfs_file_read
,
706 .write
= efivarfs_file_write
,
710 static struct inode
*efivarfs_get_inode(struct super_block
*sb
,
711 const struct inode
*dir
, int mode
, dev_t dev
)
713 struct inode
*inode
= new_inode(sb
);
716 inode
->i_ino
= get_next_ino();
717 inode
->i_mode
= mode
;
718 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
719 switch (mode
& S_IFMT
) {
721 inode
->i_fop
= &efivarfs_file_operations
;
724 inode
->i_op
= &efivarfs_dir_inode_operations
;
725 inode
->i_fop
= &simple_dir_operations
;
734 * Return true if 'str' is a valid efivarfs filename of the form,
736 * VariableName-12345678-1234-1234-1234-1234567891bc
738 static bool efivarfs_valid_name(const char *str
, int len
)
740 static const char dashes
[EFI_VARIABLE_GUID_LEN
] = {
741 [8] = 1, [13] = 1, [18] = 1, [23] = 1
743 const char *s
= str
+ len
- EFI_VARIABLE_GUID_LEN
;
747 * We need a GUID, plus at least one letter for the variable name,
748 * plus the '-' separator
750 if (len
< EFI_VARIABLE_GUID_LEN
+ 2)
753 /* GUID must be preceded by a '-' */
758 * Validate that 's' is of the correct format, e.g.
760 * 12345678-1234-1234-1234-123456789abc
762 for (i
= 0; i
< EFI_VARIABLE_GUID_LEN
; i
++) {
775 static void efivarfs_hex_to_guid(const char *str
, efi_guid_t
*guid
)
777 guid
->b
[0] = hex_to_bin(str
[6]) << 4 | hex_to_bin(str
[7]);
778 guid
->b
[1] = hex_to_bin(str
[4]) << 4 | hex_to_bin(str
[5]);
779 guid
->b
[2] = hex_to_bin(str
[2]) << 4 | hex_to_bin(str
[3]);
780 guid
->b
[3] = hex_to_bin(str
[0]) << 4 | hex_to_bin(str
[1]);
781 guid
->b
[4] = hex_to_bin(str
[11]) << 4 | hex_to_bin(str
[12]);
782 guid
->b
[5] = hex_to_bin(str
[9]) << 4 | hex_to_bin(str
[10]);
783 guid
->b
[6] = hex_to_bin(str
[16]) << 4 | hex_to_bin(str
[17]);
784 guid
->b
[7] = hex_to_bin(str
[14]) << 4 | hex_to_bin(str
[15]);
785 guid
->b
[8] = hex_to_bin(str
[19]) << 4 | hex_to_bin(str
[20]);
786 guid
->b
[9] = hex_to_bin(str
[21]) << 4 | hex_to_bin(str
[22]);
787 guid
->b
[10] = hex_to_bin(str
[24]) << 4 | hex_to_bin(str
[25]);
788 guid
->b
[11] = hex_to_bin(str
[26]) << 4 | hex_to_bin(str
[27]);
789 guid
->b
[12] = hex_to_bin(str
[28]) << 4 | hex_to_bin(str
[29]);
790 guid
->b
[13] = hex_to_bin(str
[30]) << 4 | hex_to_bin(str
[31]);
791 guid
->b
[14] = hex_to_bin(str
[32]) << 4 | hex_to_bin(str
[33]);
792 guid
->b
[15] = hex_to_bin(str
[34]) << 4 | hex_to_bin(str
[35]);
795 static int efivarfs_create(struct inode
*dir
, struct dentry
*dentry
,
796 umode_t mode
, bool excl
)
799 struct efivar_entry
*var
;
800 int namelen
, i
= 0, err
= 0;
802 if (!efivarfs_valid_name(dentry
->d_name
.name
, dentry
->d_name
.len
))
805 inode
= efivarfs_get_inode(dir
->i_sb
, dir
, mode
, 0);
809 var
= kzalloc(sizeof(struct efivar_entry
), GFP_KERNEL
);
815 /* length of the variable name itself: remove GUID and separator */
816 namelen
= dentry
->d_name
.len
- EFI_VARIABLE_GUID_LEN
- 1;
818 efivarfs_hex_to_guid(dentry
->d_name
.name
+ namelen
+ 1,
819 &var
->var
.VendorGuid
);
821 for (i
= 0; i
< namelen
; i
++)
822 var
->var
.VariableName
[i
] = dentry
->d_name
.name
[i
];
824 var
->var
.VariableName
[i
] = '\0';
826 inode
->i_private
= var
;
828 efivar_entry_add(var
, &efivarfs_list
);
829 d_instantiate(dentry
, inode
);
839 static int efivarfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
841 struct efivar_entry
*var
= dentry
->d_inode
->i_private
;
843 if (efivar_entry_delete(var
))
846 drop_nlink(dentry
->d_inode
);
852 * Compare two efivarfs file names.
854 * An efivarfs filename is composed of two parts,
856 * 1. A case-sensitive variable name
857 * 2. A case-insensitive GUID
859 * So we need to perform a case-sensitive match on part 1 and a
860 * case-insensitive match on part 2.
862 static int efivarfs_d_compare(const struct dentry
*parent
, const struct inode
*pinode
,
863 const struct dentry
*dentry
, const struct inode
*inode
,
864 unsigned int len
, const char *str
,
865 const struct qstr
*name
)
867 int guid
= len
- EFI_VARIABLE_GUID_LEN
;
869 if (name
->len
!= len
)
872 /* Case-sensitive compare for the variable name */
873 if (memcmp(str
, name
->name
, guid
))
876 /* Case-insensitive compare for the GUID */
877 return strncasecmp(name
->name
+ guid
, str
+ guid
, EFI_VARIABLE_GUID_LEN
);
880 static int efivarfs_d_hash(const struct dentry
*dentry
,
881 const struct inode
*inode
, struct qstr
*qstr
)
883 unsigned long hash
= init_name_hash();
884 const unsigned char *s
= qstr
->name
;
885 unsigned int len
= qstr
->len
;
887 if (!efivarfs_valid_name(s
, len
))
890 while (len
-- > EFI_VARIABLE_GUID_LEN
)
891 hash
= partial_name_hash(*s
++, hash
);
893 /* GUID is case-insensitive. */
895 hash
= partial_name_hash(tolower(*s
++), hash
);
897 qstr
->hash
= end_name_hash(hash
);
902 * Retaining negative dentries for an in-memory filesystem just wastes
903 * memory and lookup time: arrange for them to be deleted immediately.
905 static int efivarfs_delete_dentry(const struct dentry
*dentry
)
910 static struct dentry_operations efivarfs_d_ops
= {
911 .d_compare
= efivarfs_d_compare
,
912 .d_hash
= efivarfs_d_hash
,
913 .d_delete
= efivarfs_delete_dentry
,
916 static struct dentry
*efivarfs_alloc_dentry(struct dentry
*parent
, char *name
)
923 q
.len
= strlen(name
);
925 err
= efivarfs_d_hash(NULL
, NULL
, &q
);
929 d
= d_alloc(parent
, &q
);
933 return ERR_PTR(-ENOMEM
);
936 static int efivarfs_callback(efi_char16_t
*name16
, efi_guid_t vendor
,
937 unsigned long name_size
, void *data
)
939 struct super_block
*sb
= (struct super_block
*)data
;
940 struct efivar_entry
*entry
;
941 struct inode
*inode
= NULL
;
942 struct dentry
*dentry
, *root
= sb
->s_root
;
943 unsigned long size
= 0;
948 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
952 memcpy(entry
->var
.VariableName
, name16
, name_size
);
953 memcpy(&(entry
->var
.VendorGuid
), &vendor
, sizeof(efi_guid_t
));
955 len
= utf16_strlen(entry
->var
.VariableName
);
957 /* name, plus '-', plus GUID, plus NUL*/
958 name
= kmalloc(len
+ 1 + EFI_VARIABLE_GUID_LEN
+ 1, GFP_KERNEL
);
962 for (i
= 0; i
< len
; i
++)
963 name
[i
] = entry
->var
.VariableName
[i
] & 0xFF;
967 efi_guid_unparse(&entry
->var
.VendorGuid
, name
+ len
+ 1);
969 name
[len
+ EFI_VARIABLE_GUID_LEN
+1] = '\0';
971 inode
= efivarfs_get_inode(sb
, root
->d_inode
, S_IFREG
| 0644, 0);
975 dentry
= efivarfs_alloc_dentry(root
, name
);
976 if (IS_ERR(dentry
)) {
977 err
= PTR_ERR(dentry
);
981 /* copied by the above to local storage in the dentry. */
984 efivar_entry_size(entry
, &size
);
985 efivar_entry_add(entry
, &efivarfs_list
);
987 mutex_lock(&inode
->i_mutex
);
988 inode
->i_private
= entry
;
989 i_size_write(inode
, size
+ sizeof(entry
->var
.Attributes
));
990 mutex_unlock(&inode
->i_mutex
);
991 d_add(dentry
, inode
);
1004 static int efivarfs_destroy(struct efivar_entry
*entry
, void *data
)
1006 efivar_entry_remove(entry
);
1011 static int efivarfs_fill_super(struct super_block
*sb
, void *data
, int silent
)
1013 struct inode
*inode
= NULL
;
1014 struct dentry
*root
;
1019 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1020 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1021 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1022 sb
->s_magic
= EFIVARFS_MAGIC
;
1023 sb
->s_op
= &efivarfs_ops
;
1024 sb
->s_d_op
= &efivarfs_d_ops
;
1025 sb
->s_time_gran
= 1;
1027 inode
= efivarfs_get_inode(sb
, NULL
, S_IFDIR
| 0755, 0);
1030 inode
->i_op
= &efivarfs_dir_inode_operations
;
1032 root
= d_make_root(inode
);
1037 INIT_LIST_HEAD(&efivarfs_list
);
1039 err
= efivar_init(efivarfs_callback
, (void *)sb
, false,
1040 true, &efivarfs_list
);
1042 __efivar_entry_iter(efivarfs_destroy
, &efivarfs_list
, NULL
, NULL
);
1047 static struct dentry
*efivarfs_mount(struct file_system_type
*fs_type
,
1048 int flags
, const char *dev_name
, void *data
)
1050 return mount_single(fs_type
, flags
, data
, efivarfs_fill_super
);
1053 static void efivarfs_kill_sb(struct super_block
*sb
)
1055 kill_litter_super(sb
);
1058 /* Remove all entries and destroy */
1059 __efivar_entry_iter(efivarfs_destroy
, &efivarfs_list
, NULL
, NULL
);
1062 static struct file_system_type efivarfs_type
= {
1064 .mount
= efivarfs_mount
,
1065 .kill_sb
= efivarfs_kill_sb
,
1067 MODULE_ALIAS_FS("efivarfs");
1070 * Handle negative dentry.
1072 static struct dentry
*efivarfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1075 if (dentry
->d_name
.len
> NAME_MAX
)
1076 return ERR_PTR(-ENAMETOOLONG
);
1077 d_add(dentry
, NULL
);
1081 static const struct inode_operations efivarfs_dir_inode_operations
= {
1082 .lookup
= efivarfs_lookup
,
1083 .unlink
= efivarfs_unlink
,
1084 .create
= efivarfs_create
,
1087 static ssize_t
efivar_create(struct file
*filp
, struct kobject
*kobj
,
1088 struct bin_attribute
*bin_attr
,
1089 char *buf
, loff_t pos
, size_t count
)
1091 struct efi_variable
*new_var
= (struct efi_variable
*)buf
;
1092 struct efivar_entry
*new_entry
;
1095 if (!capable(CAP_SYS_ADMIN
))
1098 if ((new_var
->Attributes
& ~EFI_VARIABLE_MASK
) != 0 ||
1099 efivar_validate(new_var
, new_var
->Data
, new_var
->DataSize
) == false) {
1100 printk(KERN_ERR
"efivars: Malformed variable content\n");
1104 new_entry
= kzalloc(sizeof(*new_entry
), GFP_KERNEL
);
1108 memcpy(&new_entry
->var
, new_var
, sizeof(*new_var
));
1110 err
= efivar_entry_set(new_entry
, new_var
->Attributes
, new_var
->DataSize
,
1111 new_var
->Data
, &efivar_sysfs_list
);
1118 if (efivar_create_sysfs_entry(new_entry
)) {
1119 printk(KERN_WARNING
"efivars: failed to create sysfs entry.\n");
1129 static ssize_t
efivar_delete(struct file
*filp
, struct kobject
*kobj
,
1130 struct bin_attribute
*bin_attr
,
1131 char *buf
, loff_t pos
, size_t count
)
1133 struct efi_variable
*del_var
= (struct efi_variable
*)buf
;
1134 struct efivar_entry
*entry
;
1137 if (!capable(CAP_SYS_ADMIN
))
1140 efivar_entry_iter_begin();
1141 entry
= efivar_entry_find(del_var
->VariableName
, del_var
->VendorGuid
,
1142 &efivar_sysfs_list
, true);
1145 else if (__efivar_entry_delete(entry
))
1148 efivar_entry_iter_end();
1153 efivar_unregister(entry
);
1155 /* It's dead Jim.... */
1159 static bool variable_is_present(efi_char16_t
*variable_name
, efi_guid_t
*vendor
,
1160 struct list_head
*head
)
1162 struct efivar_entry
*entry
, *n
;
1163 unsigned long strsize1
, strsize2
;
1166 strsize1
= utf16_strsize(variable_name
, 1024);
1167 list_for_each_entry_safe(entry
, n
, head
, list
) {
1168 strsize2
= utf16_strsize(entry
->var
.VariableName
, 1024);
1169 if (strsize1
== strsize2
&&
1170 !memcmp(variable_name
, &(entry
->var
.VariableName
),
1172 !efi_guidcmp(entry
->var
.VendorGuid
,
1181 static int efivar_update_sysfs_entry(efi_char16_t
*name
, efi_guid_t vendor
,
1182 unsigned long name_size
, void *data
)
1184 struct efivar_entry
*entry
= data
;
1186 if (efivar_entry_find(name
, vendor
, &efivar_sysfs_list
, false))
1189 memcpy(entry
->var
.VariableName
, name
, name_size
);
1190 memcpy(&(entry
->var
.VendorGuid
), &vendor
, sizeof(efi_guid_t
));
1196 * Returns the size of variable_name, in bytes, including the
1197 * terminating NULL character, or variable_name_size if no NULL
1198 * character is found among the first variable_name_size bytes.
1200 static unsigned long var_name_strnsize(efi_char16_t
*variable_name
,
1201 unsigned long variable_name_size
)
1207 * The variable name is, by definition, a NULL-terminated
1208 * string, so make absolutely sure that variable_name_size is
1209 * the value we expect it to be. If not, return the real size.
1211 for (len
= 2; len
<= variable_name_size
; len
+= sizeof(c
)) {
1212 c
= variable_name
[(len
/ sizeof(c
)) - 1];
1217 return min(len
, variable_name_size
);
1220 static void efivar_update_sysfs_entries(struct work_struct
*work
)
1222 struct efivar_entry
*entry
;
1225 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
1229 /* Add new sysfs entries */
1231 memset(entry
, 0, sizeof(*entry
));
1233 err
= efivar_init(efivar_update_sysfs_entry
, entry
,
1234 true, false, &efivar_sysfs_list
);
1238 efivar_create_sysfs_entry(entry
);
1245 * Let's not leave out systab information that snuck into
1246 * the efivars driver
1248 static ssize_t
systab_show(struct kobject
*kobj
,
1249 struct kobj_attribute
*attr
, char *buf
)
1256 if (efi
.mps
!= EFI_INVALID_TABLE_ADDR
)
1257 str
+= sprintf(str
, "MPS=0x%lx\n", efi
.mps
);
1258 if (efi
.acpi20
!= EFI_INVALID_TABLE_ADDR
)
1259 str
+= sprintf(str
, "ACPI20=0x%lx\n", efi
.acpi20
);
1260 if (efi
.acpi
!= EFI_INVALID_TABLE_ADDR
)
1261 str
+= sprintf(str
, "ACPI=0x%lx\n", efi
.acpi
);
1262 if (efi
.smbios
!= EFI_INVALID_TABLE_ADDR
)
1263 str
+= sprintf(str
, "SMBIOS=0x%lx\n", efi
.smbios
);
1264 if (efi
.hcdp
!= EFI_INVALID_TABLE_ADDR
)
1265 str
+= sprintf(str
, "HCDP=0x%lx\n", efi
.hcdp
);
1266 if (efi
.boot_info
!= EFI_INVALID_TABLE_ADDR
)
1267 str
+= sprintf(str
, "BOOTINFO=0x%lx\n", efi
.boot_info
);
1268 if (efi
.uga
!= EFI_INVALID_TABLE_ADDR
)
1269 str
+= sprintf(str
, "UGA=0x%lx\n", efi
.uga
);
1274 static struct kobj_attribute efi_attr_systab
=
1275 __ATTR(systab
, 0400, systab_show
, NULL
);
1277 static struct attribute
*efi_subsys_attrs
[] = {
1278 &efi_attr_systab
.attr
,
1279 NULL
, /* maybe more in the future? */
1282 static struct attribute_group efi_subsys_attr_group
= {
1283 .attrs
= efi_subsys_attrs
,
1286 static struct kobject
*efi_kobj
;
1289 * efivar_create_sysfs_entry - create a new entry in sysfs
1290 * @new_var: efivar entry to create
1292 * Returns 1 on failure, 0 on success
1295 efivar_create_sysfs_entry(struct efivar_entry
*new_var
)
1297 int i
, short_name_size
;
1299 unsigned long variable_name_size
;
1300 efi_char16_t
*variable_name
;
1302 variable_name
= new_var
->var
.VariableName
;
1303 variable_name_size
= utf16_strlen(variable_name
) * sizeof(efi_char16_t
);
1306 * Length of the variable bytes in ASCII, plus the '-' separator,
1307 * plus the GUID, plus trailing NUL
1309 short_name_size
= variable_name_size
/ sizeof(efi_char16_t
)
1310 + 1 + EFI_VARIABLE_GUID_LEN
+ 1;
1312 short_name
= kzalloc(short_name_size
, GFP_KERNEL
);
1319 /* Convert Unicode to normal chars (assume top bits are 0),
1321 for (i
=0; i
< (int)(variable_name_size
/ sizeof(efi_char16_t
)); i
++) {
1322 short_name
[i
] = variable_name
[i
] & 0xFF;
1324 /* This is ugly, but necessary to separate one vendor's
1325 private variables from another's. */
1327 *(short_name
+ strlen(short_name
)) = '-';
1328 efi_guid_unparse(&new_var
->var
.VendorGuid
,
1329 short_name
+ strlen(short_name
));
1331 new_var
->kobj
.kset
= efivars_kset
;
1333 i
= kobject_init_and_add(&new_var
->kobj
, &efivar_ktype
,
1334 NULL
, "%s", short_name
);
1339 kobject_uevent(&new_var
->kobj
, KOBJ_ADD
);
1340 efivar_entry_add(new_var
, &efivar_sysfs_list
);
1346 create_efivars_bin_attributes(void)
1348 struct bin_attribute
*attr
;
1352 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
1356 attr
->attr
.name
= "new_var";
1357 attr
->attr
.mode
= 0200;
1358 attr
->write
= efivar_create
;
1359 efivars_new_var
= attr
;
1362 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
1367 attr
->attr
.name
= "del_var";
1368 attr
->attr
.mode
= 0200;
1369 attr
->write
= efivar_delete
;
1370 efivars_del_var
= attr
;
1372 sysfs_bin_attr_init(efivars_new_var
);
1373 sysfs_bin_attr_init(efivars_del_var
);
1376 error
= sysfs_create_bin_file(&efivars_kset
->kobj
, efivars_new_var
);
1378 printk(KERN_ERR
"efivars: unable to create new_var sysfs file"
1379 " due to error %d\n", error
);
1383 error
= sysfs_create_bin_file(&efivars_kset
->kobj
, efivars_del_var
);
1385 printk(KERN_ERR
"efivars: unable to create del_var sysfs file"
1386 " due to error %d\n", error
);
1387 sysfs_remove_bin_file(&efivars_kset
->kobj
, efivars_new_var
);
1393 kfree(efivars_del_var
);
1394 efivars_del_var
= NULL
;
1395 kfree(efivars_new_var
);
1396 efivars_new_var
= NULL
;
1400 static int efivars_sysfs_callback(efi_char16_t
*name
, efi_guid_t vendor
,
1401 unsigned long name_size
, void *data
)
1403 struct efivar_entry
*entry
;
1405 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
1409 memcpy(entry
->var
.VariableName
, name
, name_size
);
1410 memcpy(&(entry
->var
.VendorGuid
), &vendor
, sizeof(efi_guid_t
));
1412 efivar_create_sysfs_entry(entry
);
1417 static int efivar_sysfs_destroy(struct efivar_entry
*entry
, void *data
)
1419 efivar_entry_remove(entry
);
1420 efivar_unregister(entry
);
1425 * Print a warning when duplicate EFI variables are encountered and
1426 * disable the sysfs workqueue since the firmware is buggy.
1428 static void dup_variable_bug(efi_char16_t
*s16
, efi_guid_t
*vendor_guid
,
1429 unsigned long len16
)
1431 size_t i
, len8
= len16
/ sizeof(efi_char16_t
);
1435 * Disable the workqueue since the algorithm it uses for
1436 * detecting new variables won't work with this buggy
1437 * implementation of GetNextVariableName().
1439 efivar_wq_enabled
= false;
1441 s8
= kzalloc(len8
, GFP_KERNEL
);
1445 for (i
= 0; i
< len8
; i
++)
1448 printk(KERN_WARNING
"efivars: duplicate variable: %s-%pUl\n",
1453 static struct kobject
*efivars_kobj
;
1455 void efivars_sysfs_exit(void)
1457 /* Remove all entries and destroy */
1458 __efivar_entry_iter(efivar_sysfs_destroy
, &efivar_sysfs_list
, NULL
, NULL
);
1460 if (efivars_new_var
)
1461 sysfs_remove_bin_file(&efivars_kset
->kobj
, efivars_new_var
);
1462 if (efivars_del_var
)
1463 sysfs_remove_bin_file(&efivars_kset
->kobj
, efivars_del_var
);
1464 kfree(efivars_new_var
);
1465 kfree(efivars_del_var
);
1466 kobject_put(efivars_kobj
);
1467 kset_unregister(efivars_kset
);
1470 int efivars_sysfs_init(void)
1472 struct kobject
*parent_kobj
= efivars_kobject();
1475 /* No efivars has been registered yet */
1479 printk(KERN_INFO
"EFI Variables Facility v%s %s\n", EFIVARS_VERSION
,
1482 efivars_kset
= kset_create_and_add("vars", NULL
, parent_kobj
);
1483 if (!efivars_kset
) {
1484 printk(KERN_ERR
"efivars: Subsystem registration failed.\n");
1488 efivars_kobj
= kobject_create_and_add("efivars", parent_kobj
);
1489 if (!efivars_kobj
) {
1490 pr_err("efivars: Subsystem registration failed.\n");
1491 kset_unregister(efivars_kset
);
1495 efivar_init(efivars_sysfs_callback
, NULL
, false,
1496 true, &efivar_sysfs_list
);
1498 error
= create_efivars_bin_attributes();
1500 efivars_sysfs_exit();
1504 EXPORT_SYMBOL_GPL(efivars_sysfs_init
);
1507 * efivar_init - build the initial list of EFI variables
1508 * @func: callback function to invoke for every variable
1509 * @data: function-specific data to pass to @func
1510 * @atomic: do we need to execute the @func-loop atomically?
1511 * @duplicates: error if we encounter duplicates on @head?
1512 * @head: initialised head of variable list
1514 * Get every EFI variable from the firmware and invoke @func. @func
1515 * should call efivar_entry_add() to build the list of variables.
1517 * Returns 0 on success, or a kernel error code on failure.
1519 int efivar_init(int (*func
)(efi_char16_t
*, efi_guid_t
, unsigned long, void *),
1520 void *data
, bool atomic
, bool duplicates
,
1521 struct list_head
*head
)
1523 const struct efivar_operations
*ops
= __efivars
->ops
;
1524 unsigned long variable_name_size
= 1024;
1525 efi_char16_t
*variable_name
;
1526 efi_status_t status
;
1527 efi_guid_t vendor_guid
;
1530 variable_name
= kzalloc(variable_name_size
, GFP_KERNEL
);
1531 if (!variable_name
) {
1532 printk(KERN_ERR
"efivars: Memory allocation failed.\n");
1536 spin_lock_irq(&__efivars
->lock
);
1539 * Per EFI spec, the maximum storage allocated for both
1540 * the variable name and variable data is 1024 bytes.
1544 variable_name_size
= 1024;
1546 status
= ops
->get_next_variable(&variable_name_size
,
1552 spin_unlock_irq(&__efivars
->lock
);
1554 variable_name_size
= var_name_strnsize(variable_name
,
1555 variable_name_size
);
1558 * Some firmware implementations return the
1559 * same variable name on multiple calls to
1560 * get_next_variable(). Terminate the loop
1561 * immediately as there is no guarantee that
1562 * we'll ever see a different variable name,
1563 * and may end up looping here forever.
1566 variable_is_present(variable_name
, &vendor_guid
, head
)) {
1567 dup_variable_bug(variable_name
, &vendor_guid
,
1568 variable_name_size
);
1570 spin_lock_irq(&__efivars
->lock
);
1572 status
= EFI_NOT_FOUND
;
1576 err
= func(variable_name
, vendor_guid
, variable_name_size
, data
);
1578 status
= EFI_NOT_FOUND
;
1581 spin_lock_irq(&__efivars
->lock
);
1587 printk(KERN_WARNING
"efivars: get_next_variable: status=%lx\n",
1589 status
= EFI_NOT_FOUND
;
1593 } while (status
!= EFI_NOT_FOUND
);
1595 spin_unlock_irq(&__efivars
->lock
);
1597 kfree(variable_name
);
1601 EXPORT_SYMBOL_GPL(efivar_init
);
1604 * efivar_entry_add - add entry to variable list
1605 * @entry: entry to add to list
1608 void efivar_entry_add(struct efivar_entry
*entry
, struct list_head
*head
)
1610 spin_lock_irq(&__efivars
->lock
);
1611 list_add(&entry
->list
, head
);
1612 spin_unlock_irq(&__efivars
->lock
);
1614 EXPORT_SYMBOL_GPL(efivar_entry_add
);
1617 * efivar_entry_remove - remove entry from variable list
1618 * @entry: entry to remove from list
1620 void efivar_entry_remove(struct efivar_entry
*entry
)
1622 spin_lock_irq(&__efivars
->lock
);
1623 list_del(&entry
->list
);
1624 spin_unlock_irq(&__efivars
->lock
);
1626 EXPORT_SYMBOL_GPL(efivar_entry_remove
);
1629 * efivar_entry_list_del_unlock - remove entry from variable list
1630 * @entry: entry to remove
1632 * Remove @entry from the variable list and release the list lock.
1634 * NOTE: slightly weird locking semantics here - we expect to be
1635 * called with the efivars lock already held, and we release it before
1636 * returning. This is because this function is usually called after
1637 * set_variable() while the lock is still held.
1639 static void efivar_entry_list_del_unlock(struct efivar_entry
*entry
)
1641 WARN_ON(!spin_is_locked(&__efivars
->lock
));
1643 list_del(&entry
->list
);
1644 spin_unlock_irq(&__efivars
->lock
);
1648 * __efivar_entry_delete - delete an EFI variable
1649 * @entry: entry containing EFI variable to delete
1651 * Delete the variable from the firmware and remove @entry from the
1652 * variable list. It is the caller's responsibility to free @entry
1655 * This function differs from efivar_entry_delete() because it is
1656 * safe to be called from within a efivar_entry_iter_begin() and
1657 * efivar_entry_iter_end() region, unlike efivar_entry_delete().
1659 * Returns 0 on success, or a converted EFI status code if
1660 * set_variable() fails. If set_variable() fails the entry remains
1663 int __efivar_entry_delete(struct efivar_entry
*entry
)
1665 const struct efivar_operations
*ops
= __efivars
->ops
;
1666 efi_status_t status
;
1668 WARN_ON(!spin_is_locked(&__efivars
->lock
));
1670 status
= ops
->set_variable(entry
->var
.VariableName
,
1671 &entry
->var
.VendorGuid
,
1674 return efi_status_to_err(status
);
1676 list_del(&entry
->list
);
1680 EXPORT_SYMBOL_GPL(__efivar_entry_delete
);
1683 * efivar_entry_delete - delete variable and remove entry from list
1684 * @entry: entry containing variable to delete
1686 * Delete the variable from the firmware and remove @entry from the
1687 * variable list. It is the caller's responsibility to free @entry
1690 * Returns 0 on success, or a converted EFI status code if
1691 * set_variable() fails.
1693 int efivar_entry_delete(struct efivar_entry
*entry
)
1695 const struct efivar_operations
*ops
= __efivars
->ops
;
1696 efi_status_t status
;
1698 spin_lock_irq(&__efivars
->lock
);
1699 status
= ops
->set_variable(entry
->var
.VariableName
,
1700 &entry
->var
.VendorGuid
,
1702 if (!(status
== EFI_SUCCESS
|| status
== EFI_NOT_FOUND
)) {
1703 spin_unlock_irq(&__efivars
->lock
);
1704 return efi_status_to_err(status
);
1707 efivar_entry_list_del_unlock(entry
);
1710 EXPORT_SYMBOL_GPL(efivar_entry_delete
);
1713 * efivar_entry_set - call set_variable()
1714 * @entry: entry containing the EFI variable to write
1715 * @attributes: variable attributes
1716 * @size: size of @data buffer
1717 * @data: buffer containing variable data
1718 * @head: head of variable list
1720 * Calls set_variable() for an EFI variable. If creating a new EFI
1721 * variable, this function is usually followed by efivar_entry_add().
1723 * Before writing the variable, the remaining EFI variable storage
1724 * space is checked to ensure there is enough room available.
1726 * If @head is not NULL a lookup is performed to determine whether
1727 * the entry is already on the list.
1729 * Returns 0 on success, -EEXIST if a lookup is performed and the entry
1730 * already exists on the list, or a converted EFI status code if
1731 * set_variable() fails.
1733 int efivar_entry_set(struct efivar_entry
*entry
, u32 attributes
,
1734 unsigned long size
, void *data
, struct list_head
*head
)
1736 const struct efivar_operations
*ops
= __efivars
->ops
;
1737 efi_status_t status
;
1738 efi_char16_t
*name
= entry
->var
.VariableName
;
1739 efi_guid_t vendor
= entry
->var
.VendorGuid
;
1741 spin_lock_irq(&__efivars
->lock
);
1743 if (head
&& efivar_entry_find(name
, vendor
, head
, false)) {
1744 spin_unlock_irq(&__efivars
->lock
);
1748 status
= check_var_size(attributes
, size
+ utf16_strsize(name
, 1024));
1749 if (status
== EFI_SUCCESS
|| status
== EFI_UNSUPPORTED
)
1750 status
= ops
->set_variable(name
, &vendor
,
1751 attributes
, size
, data
);
1753 spin_unlock_irq(&__efivars
->lock
);
1755 return efi_status_to_err(status
);
1757 EXPORT_SYMBOL_GPL(efivar_entry_set
);
1760 * efivar_entry_set_safe - call set_variable() if enough space in firmware
1761 * @name: buffer containing the variable name
1762 * @vendor: variable vendor guid
1763 * @attributes: variable attributes
1764 * @block: can we block in this context?
1765 * @size: size of @data buffer
1766 * @data: buffer containing variable data
1768 * Ensures there is enough free storage in the firmware for this variable, and
1769 * if so, calls set_variable(). If creating a new EFI variable, this function
1770 * is usually followed by efivar_entry_add().
1772 * Returns 0 on success, -ENOSPC if the firmware does not have enough
1773 * space for set_variable() to succeed, or a converted EFI status code
1774 * if set_variable() fails.
1776 int efivar_entry_set_safe(efi_char16_t
*name
, efi_guid_t vendor
, u32 attributes
,
1777 bool block
, unsigned long size
, void *data
)
1779 const struct efivar_operations
*ops
= __efivars
->ops
;
1780 unsigned long flags
;
1781 efi_status_t status
;
1783 if (!ops
->query_variable_info
)
1786 if (!block
&& !spin_trylock_irqsave(&__efivars
->lock
, flags
))
1789 spin_lock_irqsave(&__efivars
->lock
, flags
);
1791 status
= check_var_size(attributes
, size
+ utf16_strsize(name
, 1024));
1792 if (status
!= EFI_SUCCESS
) {
1793 spin_unlock_irqrestore(&__efivars
->lock
, flags
);
1797 status
= ops
->set_variable(name
, &vendor
, attributes
, size
, data
);
1799 spin_unlock_irqrestore(&__efivars
->lock
, flags
);
1801 return efi_status_to_err(status
);
1803 EXPORT_SYMBOL_GPL(efivar_entry_set_safe
);
1806 * efivar_entry_find - search for an entry
1807 * @name: the EFI variable name
1808 * @guid: the EFI variable vendor's guid
1809 * @head: head of the variable list
1810 * @remove: should we remove the entry from the list?
1812 * Search for an entry on the variable list that has the EFI variable
1813 * name @name and vendor guid @guid. If an entry is found on the list
1814 * and @remove is true, the entry is removed from the list.
1816 * The caller MUST call efivar_entry_iter_begin() and
1817 * efivar_entry_iter_end() before and after the invocation of this
1818 * function, respectively.
1820 * Returns the entry if found on the list, %NULL otherwise.
1822 struct efivar_entry
*efivar_entry_find(efi_char16_t
*name
, efi_guid_t guid
,
1823 struct list_head
*head
, bool remove
)
1825 struct efivar_entry
*entry
, *n
;
1826 int strsize1
, strsize2
;
1829 WARN_ON(!spin_is_locked(&__efivars
->lock
));
1831 list_for_each_entry_safe(entry
, n
, head
, list
) {
1832 strsize1
= utf16_strsize(name
, 1024);
1833 strsize2
= utf16_strsize(entry
->var
.VariableName
, 1024);
1834 if (strsize1
== strsize2
&&
1835 !memcmp(name
, &(entry
->var
.VariableName
), strsize1
) &&
1836 !efi_guidcmp(guid
, entry
->var
.VendorGuid
)) {
1846 list_del(&entry
->list
);
1850 EXPORT_SYMBOL_GPL(efivar_entry_find
);
1853 * __efivar_entry_size - obtain the size of a variable
1854 * @entry: entry for this variable
1855 * @size: location to store the variable's size
1857 * The caller MUST call efivar_entry_iter_begin() and
1858 * efivar_entry_iter_end() before and after the invocation of this
1859 * function, respectively.
1861 int __efivar_entry_size(struct efivar_entry
*entry
, unsigned long *size
)
1863 const struct efivar_operations
*ops
= __efivars
->ops
;
1864 efi_status_t status
;
1866 WARN_ON(!spin_is_locked(&__efivars
->lock
));
1869 status
= ops
->get_variable(entry
->var
.VariableName
,
1870 &entry
->var
.VendorGuid
, NULL
, size
, NULL
);
1871 if (status
!= EFI_BUFFER_TOO_SMALL
)
1872 return efi_status_to_err(status
);
1876 EXPORT_SYMBOL_GPL(__efivar_entry_size
);
1879 * efivar_entry_size - obtain the size of a variable
1880 * @entry: entry for this variable
1881 * @size: location to store the variable's size
1883 int efivar_entry_size(struct efivar_entry
*entry
, unsigned long *size
)
1885 const struct efivar_operations
*ops
= __efivars
->ops
;
1886 efi_status_t status
;
1890 spin_lock_irq(&__efivars
->lock
);
1891 status
= ops
->get_variable(entry
->var
.VariableName
,
1892 &entry
->var
.VendorGuid
, NULL
, size
, NULL
);
1893 spin_unlock_irq(&__efivars
->lock
);
1895 if (status
!= EFI_BUFFER_TOO_SMALL
)
1896 return efi_status_to_err(status
);
1900 EXPORT_SYMBOL_GPL(efivar_entry_size
);
1903 * efivar_entry_get - call get_variable()
1904 * @entry: read data for this variable
1905 * @attributes: variable attributes
1906 * @size: size of @data buffer
1907 * @data: buffer to store variable data
1909 int efivar_entry_get(struct efivar_entry
*entry
, u32
*attributes
,
1910 unsigned long *size
, void *data
)
1912 const struct efivar_operations
*ops
= __efivars
->ops
;
1913 efi_status_t status
;
1915 spin_lock_irq(&__efivars
->lock
);
1916 status
= ops
->get_variable(entry
->var
.VariableName
,
1917 &entry
->var
.VendorGuid
,
1918 attributes
, size
, data
);
1919 spin_unlock_irq(&__efivars
->lock
);
1921 return efi_status_to_err(status
);
1923 EXPORT_SYMBOL_GPL(efivar_entry_get
);
1926 * efivar_entry_set_get_size - call set_variable() and get new size (atomic)
1927 * @entry: entry containing variable to set and get
1928 * @attributes: attributes of variable to be written
1929 * @size: size of data buffer
1930 * @data: buffer containing data to write
1931 * @set: did the set_variable() call succeed?
1933 * This is a pretty special (complex) function. See efivarfs_file_write().
1935 * Atomically call set_variable() for @entry and if the call is
1936 * successful, return the new size of the variable from get_variable()
1937 * in @size. The success of set_variable() is indicated by @set.
1939 * Returns 0 on success, -EINVAL if the variable data is invalid,
1940 * -ENOSPC if the firmware does not have enough available space, or a
1941 * converted EFI status code if either of set_variable() or
1942 * get_variable() fail.
1944 * If the EFI variable does not exist when calling set_variable()
1945 * (EFI_NOT_FOUND), @entry is removed from the variable list.
1947 int efivar_entry_set_get_size(struct efivar_entry
*entry
, u32 attributes
,
1948 unsigned long *size
, void *data
, bool *set
)
1950 const struct efivar_operations
*ops
= __efivars
->ops
;
1951 efi_char16_t
*name
= entry
->var
.VariableName
;
1952 efi_guid_t
*vendor
= &entry
->var
.VendorGuid
;
1953 efi_status_t status
;
1958 if (efivar_validate(&entry
->var
, data
, *size
) == false)
1962 * The lock here protects the get_variable call, the conditional
1963 * set_variable call, and removal of the variable from the efivars
1964 * list (in the case of an authenticated delete).
1966 spin_lock_irq(&__efivars
->lock
);
1969 * Ensure that the available space hasn't shrunk below the safe level
1971 status
= check_var_size(attributes
, *size
+ utf16_strsize(name
, 1024));
1972 if (status
!= EFI_SUCCESS
) {
1973 if (status
!= EFI_UNSUPPORTED
) {
1974 err
= efi_status_to_err(status
);
1978 if (*size
> 65536) {
1984 status
= ops
->set_variable(name
, vendor
, attributes
, *size
, data
);
1985 if (status
!= EFI_SUCCESS
) {
1986 err
= efi_status_to_err(status
);
1993 * Writing to the variable may have caused a change in size (which
1994 * could either be an append or an overwrite), or the variable to be
1995 * deleted. Perform a GetVariable() so we can tell what actually
1999 status
= ops
->get_variable(entry
->var
.VariableName
,
2000 &entry
->var
.VendorGuid
,
2003 if (status
== EFI_NOT_FOUND
)
2004 efivar_entry_list_del_unlock(entry
);
2006 spin_unlock_irq(&__efivars
->lock
);
2008 if (status
&& status
!= EFI_BUFFER_TOO_SMALL
)
2009 return efi_status_to_err(status
);
2014 spin_unlock_irq(&__efivars
->lock
);
2018 EXPORT_SYMBOL_GPL(efivar_entry_set_get_size
);
2021 * efivar_entry_iter_begin - begin iterating the variable list
2023 * Lock the variable list to prevent entry insertion and removal until
2024 * efivar_entry_iter_end() is called. This function is usually used in
2025 * conjunction with __efivar_entry_iter() or efivar_entry_iter().
2027 void efivar_entry_iter_begin(void)
2029 spin_lock_irq(&__efivars
->lock
);
2031 EXPORT_SYMBOL_GPL(efivar_entry_iter_begin
);
2034 * efivar_entry_iter_end - finish iterating the variable list
2036 * Unlock the variable list and allow modifications to the list again.
2038 void efivar_entry_iter_end(void)
2040 spin_unlock_irq(&__efivars
->lock
);
2042 EXPORT_SYMBOL_GPL(efivar_entry_iter_end
);
2045 * __efivar_entry_iter - iterate over variable list
2046 * @func: callback function
2047 * @head: head of the variable list
2048 * @data: function-specific data to pass to callback
2049 * @prev: entry to begin iterating from
2051 * Iterate over the list of EFI variables and call @func with every
2052 * entry on the list. It is safe for @func to remove entries in the
2053 * list via efivar_entry_delete().
2055 * You MUST call efivar_enter_iter_begin() before this function, and
2056 * efivar_entry_iter_end() afterwards.
2058 * It is possible to begin iteration from an arbitrary entry within
2059 * the list by passing @prev. @prev is updated on return to point to
2060 * the last entry passed to @func. To begin iterating from the
2061 * beginning of the list @prev must be %NULL.
2063 * The restrictions for @func are the same as documented for
2064 * efivar_entry_iter().
2066 int __efivar_entry_iter(int (*func
)(struct efivar_entry
*, void *),
2067 struct list_head
*head
, void *data
,
2068 struct efivar_entry
**prev
)
2070 struct efivar_entry
*entry
, *n
;
2073 if (!prev
|| !*prev
) {
2074 list_for_each_entry_safe(entry
, n
, head
, list
) {
2075 err
= func(entry
, data
);
2087 list_for_each_entry_safe_continue((*prev
), n
, head
, list
) {
2088 err
= func(*prev
, data
);
2095 EXPORT_SYMBOL_GPL(__efivar_entry_iter
);
2098 * efivar_entry_iter - iterate over variable list
2099 * @func: callback function
2100 * @head: head of variable list
2101 * @data: function-specific data to pass to callback
2103 * Iterate over the list of EFI variables and call @func with every
2104 * entry on the list. It is safe for @func to remove entries in the
2105 * list via efivar_entry_delete() while iterating.
2107 * Some notes for the callback function:
2108 * - a non-zero return value indicates an error and terminates the loop
2109 * - @func is called from atomic context
2111 int efivar_entry_iter(int (*func
)(struct efivar_entry
*, void *),
2112 struct list_head
*head
, void *data
)
2116 efivar_entry_iter_begin();
2117 err
= __efivar_entry_iter(func
, head
, data
, NULL
);
2118 efivar_entry_iter_end();
2122 EXPORT_SYMBOL_GPL(efivar_entry_iter
);
2125 * efivars_kobject - get the kobject for the registered efivars
2127 * If efivars_register() has not been called we return NULL,
2128 * otherwise return the kobject used at registration time.
2130 struct kobject
*efivars_kobject(void)
2135 return __efivars
->kobject
;
2137 EXPORT_SYMBOL_GPL(efivars_kobject
);
2140 * efivar_run_worker - schedule the efivar worker thread
2142 void efivar_run_worker(void)
2144 if (efivar_wq_enabled
)
2145 schedule_work(&efivar_work
);
2147 EXPORT_SYMBOL_GPL(efivar_run_worker
);
2150 * efivars_register - register an efivars
2151 * @efivars: efivars to register
2152 * @ops: efivars operations
2153 * @kobject: @efivars-specific kobject
2155 * Only a single efivars can be registered at any time.
2157 int efivars_register(struct efivars
*efivars
,
2158 const struct efivar_operations
*ops
,
2159 struct kobject
*kobject
)
2161 spin_lock_init(&efivars
->lock
);
2163 efivars
->kobject
= kobject
;
2165 __efivars
= efivars
;
2167 register_filesystem(&efivarfs_type
);
2171 EXPORT_SYMBOL_GPL(efivars_register
);
2174 * efivars_unregister - unregister an efivars
2175 * @efivars: efivars to unregister
2177 * The caller must have already removed every entry from the list,
2178 * failure to do so is an error.
2180 int efivars_unregister(struct efivars
*efivars
)
2185 printk(KERN_ERR
"efivars not registered\n");
2190 if (__efivars
!= efivars
) {
2201 EXPORT_SYMBOL_GPL(efivars_unregister
);
2203 static struct efivars generic_efivars
;
2204 static struct efivar_operations generic_ops
;
2206 static int generic_ops_register(void)
2210 generic_ops
.get_variable
= efi
.get_variable
;
2211 generic_ops
.set_variable
= efi
.set_variable
;
2212 generic_ops
.get_next_variable
= efi
.get_next_variable
;
2213 generic_ops
.query_variable_info
= efi
.query_variable_info
;
2215 error
= efivars_register(&generic_efivars
, &generic_ops
, efi_kobj
);
2219 error
= efivars_sysfs_init();
2221 efivars_unregister(&generic_efivars
);
2226 static void generic_ops_unregister(void)
2228 efivars_sysfs_exit();
2229 efivars_unregister(&generic_efivars
);
2233 * For now we register the efi subsystem with the firmware subsystem
2234 * and the vars subsystem with the efi subsystem. In the future, it
2235 * might make sense to split off the efi subsystem into its own
2236 * driver, but for now only efivars will register with it, so just
2245 if (!efi_enabled(EFI_RUNTIME_SERVICES
))
2248 /* Register the efi directory at /sys/firmware/efi */
2249 efi_kobj
= kobject_create_and_add("efi", firmware_kobj
);
2251 printk(KERN_ERR
"efivars: Firmware registration failed.\n");
2255 error
= generic_ops_register();
2259 /* Don't forget the systab entry */
2260 error
= sysfs_create_group(efi_kobj
, &efi_subsys_attr_group
);
2263 "efivars: Sysfs attribute export failed with error %d.\n",
2265 goto err_unregister
;
2271 generic_ops_unregister();
2273 kobject_put(efi_kobj
);
2280 cancel_work_sync(&efivar_work
);
2282 if (efi_enabled(EFI_RUNTIME_SERVICES
)) {
2283 generic_ops_unregister();
2284 kobject_put(efi_kobj
);
2288 module_init(efivars_init
);
2289 module_exit(efivars_exit
);