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(EFI_VARS_PSTORE_DEFAULT_DISABLE
);
109 module_param_named(pstore_disable
, efivars_pstore_disable
, bool, 0644);
112 * The maximum size of VariableName + Data = 1024
113 * Therefore, it's reasonable to save that much
114 * space in each part of the structure,
115 * and we use a page for reading/writing.
118 struct efi_variable
{
119 efi_char16_t VariableName
[1024/sizeof(efi_char16_t
)];
120 efi_guid_t VendorGuid
;
121 unsigned long DataSize
;
125 } __attribute__((packed
));
127 struct efivar_entry
{
128 struct efivars
*efivars
;
129 struct efi_variable var
;
130 struct list_head list
;
134 struct efivar_attribute
{
135 struct attribute attr
;
136 ssize_t (*show
) (struct efivar_entry
*entry
, char *buf
);
137 ssize_t (*store
)(struct efivar_entry
*entry
, const char *buf
, size_t count
);
140 static struct efivars __efivars
;
141 static struct efivar_operations ops
;
143 #define PSTORE_EFI_ATTRIBUTES \
144 (EFI_VARIABLE_NON_VOLATILE | \
145 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
146 EFI_VARIABLE_RUNTIME_ACCESS)
148 #define EFIVAR_ATTR(_name, _mode, _show, _store) \
149 struct efivar_attribute efivar_attr_##_name = { \
150 .attr = {.name = __stringify(_name), .mode = _mode}, \
155 #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
156 #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
159 * Prototype for sysfs creation function
162 efivar_create_sysfs_entry(struct efivars
*efivars
,
163 unsigned long variable_name_size
,
164 efi_char16_t
*variable_name
,
165 efi_guid_t
*vendor_guid
);
168 * Prototype for workqueue functions updating sysfs entry
171 static void efivar_update_sysfs_entries(struct work_struct
*);
172 static DECLARE_WORK(efivar_work
, efivar_update_sysfs_entries
);
174 /* Return the number of unicode characters in data */
176 utf16_strnlen(efi_char16_t
*s
, size_t maxlength
)
178 unsigned long length
= 0;
180 while (*s
++ != 0 && length
< maxlength
)
185 static inline unsigned long
186 utf16_strlen(efi_char16_t
*s
)
188 return utf16_strnlen(s
, ~0UL);
192 * Return the number of bytes is the length of this string
193 * Note: this is NOT the same as the number of unicode characters
195 static inline unsigned long
196 utf16_strsize(efi_char16_t
*data
, unsigned long maxlength
)
198 return utf16_strnlen(data
, maxlength
/sizeof(efi_char16_t
)) * sizeof(efi_char16_t
);
202 utf16_strncmp(const efi_char16_t
*a
, const efi_char16_t
*b
, size_t len
)
211 if (*a
== 0) /* implies *b == 0 */
220 validate_device_path(struct efi_variable
*var
, int match
, u8
*buffer
,
223 struct efi_generic_dev_path
*node
;
226 node
= (struct efi_generic_dev_path
*)buffer
;
228 if (len
< sizeof(*node
))
231 while (offset
<= len
- sizeof(*node
) &&
232 node
->length
>= sizeof(*node
) &&
233 node
->length
<= len
- offset
) {
234 offset
+= node
->length
;
236 if ((node
->type
== EFI_DEV_END_PATH
||
237 node
->type
== EFI_DEV_END_PATH2
) &&
238 node
->sub_type
== EFI_DEV_END_ENTIRE
)
241 node
= (struct efi_generic_dev_path
*)(buffer
+ offset
);
245 * If we're here then either node->length pointed past the end
246 * of the buffer or we reached the end of the buffer without
247 * finding a device path end node.
253 validate_boot_order(struct efi_variable
*var
, int match
, u8
*buffer
,
256 /* An array of 16-bit integers */
264 validate_load_option(struct efi_variable
*var
, int match
, u8
*buffer
,
268 int i
, desclength
= 0, namelen
;
270 namelen
= utf16_strnlen(var
->VariableName
, sizeof(var
->VariableName
));
272 /* Either "Boot" or "Driver" followed by four digits of hex */
273 for (i
= match
; i
< match
+4; i
++) {
274 if (var
->VariableName
[i
] > 127 ||
275 hex_to_bin(var
->VariableName
[i
] & 0xff) < 0)
279 /* Reject it if there's 4 digits of hex and then further content */
280 if (namelen
> match
+ 4)
283 /* A valid entry must be at least 8 bytes */
287 filepathlength
= buffer
[4] | buffer
[5] << 8;
290 * There's no stored length for the description, so it has to be
293 desclength
= utf16_strsize((efi_char16_t
*)(buffer
+ 6), len
- 6) + 2;
295 /* Each boot entry must have a descriptor */
300 * If the sum of the length of the description, the claimed filepath
301 * length and the original header are greater than the length of the
302 * variable, it's malformed
304 if ((desclength
+ filepathlength
+ 6) > len
)
308 * And, finally, check the filepath
310 return validate_device_path(var
, match
, buffer
+ desclength
+ 6,
315 validate_uint16(struct efi_variable
*var
, int match
, u8
*buffer
,
318 /* A single 16-bit integer */
326 validate_ascii_string(struct efi_variable
*var
, int match
, u8
*buffer
,
331 for (i
= 0; i
< len
; i
++) {
342 struct variable_validate
{
344 bool (*validate
)(struct efi_variable
*var
, int match
, u8
*data
,
348 static const struct variable_validate variable_validate
[] = {
349 { "BootNext", validate_uint16
},
350 { "BootOrder", validate_boot_order
},
351 { "DriverOrder", validate_boot_order
},
352 { "Boot*", validate_load_option
},
353 { "Driver*", validate_load_option
},
354 { "ConIn", validate_device_path
},
355 { "ConInDev", validate_device_path
},
356 { "ConOut", validate_device_path
},
357 { "ConOutDev", validate_device_path
},
358 { "ErrOut", validate_device_path
},
359 { "ErrOutDev", validate_device_path
},
360 { "Timeout", validate_uint16
},
361 { "Lang", validate_ascii_string
},
362 { "PlatformLang", validate_ascii_string
},
367 validate_var(struct efi_variable
*var
, u8
*data
, unsigned long len
)
370 u16
*unicode_name
= var
->VariableName
;
372 for (i
= 0; variable_validate
[i
].validate
!= NULL
; i
++) {
373 const char *name
= variable_validate
[i
].name
;
376 for (match
= 0; ; match
++) {
377 char c
= name
[match
];
378 u16 u
= unicode_name
[match
];
380 /* All special variables are plain ascii */
384 /* Wildcard in the matching name means we've matched */
386 return variable_validate
[i
].validate(var
,
389 /* Case sensitive match */
393 /* Reached the end of the string while matching */
395 return variable_validate
[i
].validate(var
,
404 get_var_data_locked(struct efivars
*efivars
, struct efi_variable
*var
)
408 var
->DataSize
= 1024;
409 status
= efivars
->ops
->get_variable(var
->VariableName
,
418 get_var_data(struct efivars
*efivars
, struct efi_variable
*var
)
423 spin_lock_irqsave(&efivars
->lock
, flags
);
424 status
= get_var_data_locked(efivars
, var
);
425 spin_unlock_irqrestore(&efivars
->lock
, flags
);
427 if (status
!= EFI_SUCCESS
) {
428 printk(KERN_WARNING
"efivars: get_variable() failed 0x%lx!\n",
435 check_var_size_locked(struct efivars
*efivars
, u32 attributes
,
438 u64 storage_size
, remaining_size
, max_size
;
440 const struct efivar_operations
*fops
= efivars
->ops
;
442 if (!efivars
->ops
->query_variable_info
)
443 return EFI_UNSUPPORTED
;
445 status
= fops
->query_variable_info(attributes
, &storage_size
,
446 &remaining_size
, &max_size
);
448 if (status
!= EFI_SUCCESS
)
451 if (!storage_size
|| size
> remaining_size
|| size
> max_size
||
452 (remaining_size
- size
) < (storage_size
/ 2))
453 return EFI_OUT_OF_RESOURCES
;
460 check_var_size(struct efivars
*efivars
, u32 attributes
, unsigned long size
)
465 spin_lock_irqsave(&efivars
->lock
, flags
);
466 status
= check_var_size_locked(efivars
, attributes
, size
);
467 spin_unlock_irqrestore(&efivars
->lock
, flags
);
473 efivar_guid_read(struct efivar_entry
*entry
, char *buf
)
475 struct efi_variable
*var
= &entry
->var
;
481 efi_guid_unparse(&var
->VendorGuid
, str
);
483 str
+= sprintf(str
, "\n");
489 efivar_attr_read(struct efivar_entry
*entry
, char *buf
)
491 struct efi_variable
*var
= &entry
->var
;
498 status
= get_var_data(entry
->efivars
, var
);
499 if (status
!= EFI_SUCCESS
)
502 if (var
->Attributes
& EFI_VARIABLE_NON_VOLATILE
)
503 str
+= sprintf(str
, "EFI_VARIABLE_NON_VOLATILE\n");
504 if (var
->Attributes
& EFI_VARIABLE_BOOTSERVICE_ACCESS
)
505 str
+= sprintf(str
, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
506 if (var
->Attributes
& EFI_VARIABLE_RUNTIME_ACCESS
)
507 str
+= sprintf(str
, "EFI_VARIABLE_RUNTIME_ACCESS\n");
508 if (var
->Attributes
& EFI_VARIABLE_HARDWARE_ERROR_RECORD
)
509 str
+= sprintf(str
, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
510 if (var
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
)
512 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
513 if (var
->Attributes
&
514 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)
516 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
517 if (var
->Attributes
& EFI_VARIABLE_APPEND_WRITE
)
518 str
+= sprintf(str
, "EFI_VARIABLE_APPEND_WRITE\n");
523 efivar_size_read(struct efivar_entry
*entry
, char *buf
)
525 struct efi_variable
*var
= &entry
->var
;
532 status
= get_var_data(entry
->efivars
, var
);
533 if (status
!= EFI_SUCCESS
)
536 str
+= sprintf(str
, "0x%lx\n", var
->DataSize
);
541 efivar_data_read(struct efivar_entry
*entry
, char *buf
)
543 struct efi_variable
*var
= &entry
->var
;
549 status
= get_var_data(entry
->efivars
, var
);
550 if (status
!= EFI_SUCCESS
)
553 memcpy(buf
, var
->Data
, var
->DataSize
);
554 return var
->DataSize
;
557 * We allow each variable to be edited via rewriting the
558 * entire efi variable structure.
561 efivar_store_raw(struct efivar_entry
*entry
, const char *buf
, size_t count
)
563 struct efi_variable
*new_var
, *var
= &entry
->var
;
564 struct efivars
*efivars
= entry
->efivars
;
565 efi_status_t status
= EFI_NOT_FOUND
;
567 if (count
!= sizeof(struct efi_variable
))
570 new_var
= (struct efi_variable
*)buf
;
572 * If only updating the variable data, then the name
573 * and guid should remain the same
575 if (memcmp(new_var
->VariableName
, var
->VariableName
, sizeof(var
->VariableName
)) ||
576 efi_guidcmp(new_var
->VendorGuid
, var
->VendorGuid
)) {
577 printk(KERN_ERR
"efivars: Cannot edit the wrong variable!\n");
581 if ((new_var
->DataSize
<= 0) || (new_var
->Attributes
== 0)){
582 printk(KERN_ERR
"efivars: DataSize & Attributes must be valid!\n");
586 if ((new_var
->Attributes
& ~EFI_VARIABLE_MASK
) != 0 ||
587 validate_var(new_var
, new_var
->Data
, new_var
->DataSize
) == false) {
588 printk(KERN_ERR
"efivars: Malformed variable content\n");
592 spin_lock_irq(&efivars
->lock
);
594 status
= check_var_size_locked(efivars
, new_var
->Attributes
,
595 new_var
->DataSize
+ utf16_strsize(new_var
->VariableName
, 1024));
597 if (status
== EFI_SUCCESS
|| status
== EFI_UNSUPPORTED
)
598 status
= efivars
->ops
->set_variable(new_var
->VariableName
,
599 &new_var
->VendorGuid
,
604 spin_unlock_irq(&efivars
->lock
);
606 if (status
!= EFI_SUCCESS
) {
607 printk(KERN_WARNING
"efivars: set_variable() failed: status=%lx\n",
612 memcpy(&entry
->var
, new_var
, count
);
617 efivar_show_raw(struct efivar_entry
*entry
, char *buf
)
619 struct efi_variable
*var
= &entry
->var
;
625 status
= get_var_data(entry
->efivars
, var
);
626 if (status
!= EFI_SUCCESS
)
629 memcpy(buf
, var
, sizeof(*var
));
634 * Generic read/write functions that call the specific functions of
637 static ssize_t
efivar_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
640 struct efivar_entry
*var
= to_efivar_entry(kobj
);
641 struct efivar_attribute
*efivar_attr
= to_efivar_attr(attr
);
644 if (!capable(CAP_SYS_ADMIN
))
647 if (efivar_attr
->show
) {
648 ret
= efivar_attr
->show(var
, buf
);
653 static ssize_t
efivar_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
654 const char *buf
, size_t count
)
656 struct efivar_entry
*var
= to_efivar_entry(kobj
);
657 struct efivar_attribute
*efivar_attr
= to_efivar_attr(attr
);
660 if (!capable(CAP_SYS_ADMIN
))
663 if (efivar_attr
->store
)
664 ret
= efivar_attr
->store(var
, buf
, count
);
669 static const struct sysfs_ops efivar_attr_ops
= {
670 .show
= efivar_attr_show
,
671 .store
= efivar_attr_store
,
674 static void efivar_release(struct kobject
*kobj
)
676 struct efivar_entry
*var
= container_of(kobj
, struct efivar_entry
, kobj
);
680 static EFIVAR_ATTR(guid
, 0400, efivar_guid_read
, NULL
);
681 static EFIVAR_ATTR(attributes
, 0400, efivar_attr_read
, NULL
);
682 static EFIVAR_ATTR(size
, 0400, efivar_size_read
, NULL
);
683 static EFIVAR_ATTR(data
, 0400, efivar_data_read
, NULL
);
684 static EFIVAR_ATTR(raw_var
, 0600, efivar_show_raw
, efivar_store_raw
);
686 static struct attribute
*def_attrs
[] = {
687 &efivar_attr_guid
.attr
,
688 &efivar_attr_size
.attr
,
689 &efivar_attr_attributes
.attr
,
690 &efivar_attr_data
.attr
,
691 &efivar_attr_raw_var
.attr
,
695 static struct kobj_type efivar_ktype
= {
696 .release
= efivar_release
,
697 .sysfs_ops
= &efivar_attr_ops
,
698 .default_attrs
= def_attrs
,
702 efivar_unregister(struct efivar_entry
*var
)
704 kobject_put(&var
->kobj
);
707 static int efivarfs_file_open(struct inode
*inode
, struct file
*file
)
709 file
->private_data
= inode
->i_private
;
713 static int efi_status_to_err(efi_status_t status
)
718 case EFI_INVALID_PARAMETER
:
721 case EFI_OUT_OF_RESOURCES
:
724 case EFI_DEVICE_ERROR
:
727 case EFI_WRITE_PROTECTED
:
730 case EFI_SECURITY_VIOLATION
:
743 static ssize_t
efivarfs_file_write(struct file
*file
,
744 const char __user
*userbuf
, size_t count
, loff_t
*ppos
)
746 struct efivar_entry
*var
= file
->private_data
;
747 struct efivars
*efivars
;
751 struct inode
*inode
= file
->f_mapping
->host
;
752 unsigned long datasize
= count
- sizeof(attributes
);
753 unsigned long newdatasize
, varsize
;
756 if (count
< sizeof(attributes
))
759 if (copy_from_user(&attributes
, userbuf
, sizeof(attributes
)))
762 if (attributes
& ~(EFI_VARIABLE_MASK
))
765 efivars
= var
->efivars
;
768 * Ensure that the user can't allocate arbitrarily large
769 * amounts of memory. Pick a default size of 64K if
770 * QueryVariableInfo() isn't supported by the firmware.
773 varsize
= datasize
+ utf16_strsize(var
->var
.VariableName
, 1024);
774 status
= check_var_size(efivars
, attributes
, varsize
);
776 if (status
!= EFI_SUCCESS
) {
777 if (status
!= EFI_UNSUPPORTED
)
778 return efi_status_to_err(status
);
780 if (datasize
> 65536)
784 data
= kmalloc(datasize
, GFP_KERNEL
);
788 if (copy_from_user(data
, userbuf
+ sizeof(attributes
), datasize
)) {
793 if (validate_var(&var
->var
, data
, datasize
) == false) {
799 * The lock here protects the get_variable call, the conditional
800 * set_variable call, and removal of the variable from the efivars
801 * list (in the case of an authenticated delete).
803 spin_lock_irq(&efivars
->lock
);
806 * Ensure that the available space hasn't shrunk below the safe level
809 status
= check_var_size_locked(efivars
, attributes
, varsize
);
811 if (status
!= EFI_SUCCESS
&& status
!= EFI_UNSUPPORTED
) {
812 spin_unlock_irq(&efivars
->lock
);
815 return efi_status_to_err(status
);
818 status
= efivars
->ops
->set_variable(var
->var
.VariableName
,
819 &var
->var
.VendorGuid
,
820 attributes
, datasize
,
823 if (status
!= EFI_SUCCESS
) {
824 spin_unlock_irq(&efivars
->lock
);
827 return efi_status_to_err(status
);
833 * Writing to the variable may have caused a change in size (which
834 * could either be an append or an overwrite), or the variable to be
835 * deleted. Perform a GetVariable() so we can tell what actually
839 status
= efivars
->ops
->get_variable(var
->var
.VariableName
,
840 &var
->var
.VendorGuid
,
844 if (status
== EFI_BUFFER_TOO_SMALL
) {
845 spin_unlock_irq(&efivars
->lock
);
846 mutex_lock(&inode
->i_mutex
);
847 i_size_write(inode
, newdatasize
+ sizeof(attributes
));
848 mutex_unlock(&inode
->i_mutex
);
850 } else if (status
== EFI_NOT_FOUND
) {
851 list_del(&var
->list
);
852 spin_unlock_irq(&efivars
->lock
);
853 efivar_unregister(var
);
855 d_delete(file
->f_dentry
);
856 dput(file
->f_dentry
);
859 spin_unlock_irq(&efivars
->lock
);
860 pr_warn("efivarfs: inconsistent EFI variable implementation? "
861 "status = %lx\n", status
);
870 static ssize_t
efivarfs_file_read(struct file
*file
, char __user
*userbuf
,
871 size_t count
, loff_t
*ppos
)
873 struct efivar_entry
*var
= file
->private_data
;
874 struct efivars
*efivars
= var
->efivars
;
876 unsigned long datasize
= 0;
881 spin_lock_irq(&efivars
->lock
);
882 status
= efivars
->ops
->get_variable(var
->var
.VariableName
,
883 &var
->var
.VendorGuid
,
884 &attributes
, &datasize
, NULL
);
885 spin_unlock_irq(&efivars
->lock
);
887 if (status
!= EFI_BUFFER_TOO_SMALL
)
888 return efi_status_to_err(status
);
890 data
= kmalloc(datasize
+ sizeof(attributes
), GFP_KERNEL
);
895 spin_lock_irq(&efivars
->lock
);
896 status
= efivars
->ops
->get_variable(var
->var
.VariableName
,
897 &var
->var
.VendorGuid
,
898 &attributes
, &datasize
,
899 (data
+ sizeof(attributes
)));
900 spin_unlock_irq(&efivars
->lock
);
902 if (status
!= EFI_SUCCESS
) {
903 size
= efi_status_to_err(status
);
907 memcpy(data
, &attributes
, sizeof(attributes
));
908 size
= simple_read_from_buffer(userbuf
, count
, ppos
,
909 data
, datasize
+ sizeof(attributes
));
916 static void efivarfs_evict_inode(struct inode
*inode
)
921 static const struct super_operations efivarfs_ops
= {
922 .statfs
= simple_statfs
,
923 .drop_inode
= generic_delete_inode
,
924 .evict_inode
= efivarfs_evict_inode
,
925 .show_options
= generic_show_options
,
928 static struct super_block
*efivarfs_sb
;
930 static const struct inode_operations efivarfs_dir_inode_operations
;
932 static const struct file_operations efivarfs_file_operations
= {
933 .open
= efivarfs_file_open
,
934 .read
= efivarfs_file_read
,
935 .write
= efivarfs_file_write
,
939 static struct inode
*efivarfs_get_inode(struct super_block
*sb
,
940 const struct inode
*dir
, int mode
, dev_t dev
)
942 struct inode
*inode
= new_inode(sb
);
945 inode
->i_ino
= get_next_ino();
946 inode
->i_mode
= mode
;
947 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
948 switch (mode
& S_IFMT
) {
950 inode
->i_fop
= &efivarfs_file_operations
;
953 inode
->i_op
= &efivarfs_dir_inode_operations
;
954 inode
->i_fop
= &simple_dir_operations
;
963 * Return true if 'str' is a valid efivarfs filename of the form,
965 * VariableName-12345678-1234-1234-1234-1234567891bc
967 static bool efivarfs_valid_name(const char *str
, int len
)
969 static const char dashes
[GUID_LEN
] = {
970 [8] = 1, [13] = 1, [18] = 1, [23] = 1
972 const char *s
= str
+ len
- GUID_LEN
;
976 * We need a GUID, plus at least one letter for the variable name,
977 * plus the '-' separator
979 if (len
< GUID_LEN
+ 2)
982 /* GUID must be preceded by a '-' */
987 * Validate that 's' is of the correct format, e.g.
989 * 12345678-1234-1234-1234-123456789abc
991 for (i
= 0; i
< GUID_LEN
; i
++) {
1004 static void efivarfs_hex_to_guid(const char *str
, efi_guid_t
*guid
)
1006 guid
->b
[0] = hex_to_bin(str
[6]) << 4 | hex_to_bin(str
[7]);
1007 guid
->b
[1] = hex_to_bin(str
[4]) << 4 | hex_to_bin(str
[5]);
1008 guid
->b
[2] = hex_to_bin(str
[2]) << 4 | hex_to_bin(str
[3]);
1009 guid
->b
[3] = hex_to_bin(str
[0]) << 4 | hex_to_bin(str
[1]);
1010 guid
->b
[4] = hex_to_bin(str
[11]) << 4 | hex_to_bin(str
[12]);
1011 guid
->b
[5] = hex_to_bin(str
[9]) << 4 | hex_to_bin(str
[10]);
1012 guid
->b
[6] = hex_to_bin(str
[16]) << 4 | hex_to_bin(str
[17]);
1013 guid
->b
[7] = hex_to_bin(str
[14]) << 4 | hex_to_bin(str
[15]);
1014 guid
->b
[8] = hex_to_bin(str
[19]) << 4 | hex_to_bin(str
[20]);
1015 guid
->b
[9] = hex_to_bin(str
[21]) << 4 | hex_to_bin(str
[22]);
1016 guid
->b
[10] = hex_to_bin(str
[24]) << 4 | hex_to_bin(str
[25]);
1017 guid
->b
[11] = hex_to_bin(str
[26]) << 4 | hex_to_bin(str
[27]);
1018 guid
->b
[12] = hex_to_bin(str
[28]) << 4 | hex_to_bin(str
[29]);
1019 guid
->b
[13] = hex_to_bin(str
[30]) << 4 | hex_to_bin(str
[31]);
1020 guid
->b
[14] = hex_to_bin(str
[32]) << 4 | hex_to_bin(str
[33]);
1021 guid
->b
[15] = hex_to_bin(str
[34]) << 4 | hex_to_bin(str
[35]);
1024 static int efivarfs_create(struct inode
*dir
, struct dentry
*dentry
,
1025 umode_t mode
, bool excl
)
1027 struct inode
*inode
;
1028 struct efivars
*efivars
= &__efivars
;
1029 struct efivar_entry
*var
;
1030 int namelen
, i
= 0, err
= 0;
1032 if (!efivarfs_valid_name(dentry
->d_name
.name
, dentry
->d_name
.len
))
1035 inode
= efivarfs_get_inode(dir
->i_sb
, dir
, mode
, 0);
1039 var
= kzalloc(sizeof(struct efivar_entry
), GFP_KERNEL
);
1045 /* length of the variable name itself: remove GUID and separator */
1046 namelen
= dentry
->d_name
.len
- GUID_LEN
- 1;
1048 efivarfs_hex_to_guid(dentry
->d_name
.name
+ namelen
+ 1,
1049 &var
->var
.VendorGuid
);
1051 for (i
= 0; i
< namelen
; i
++)
1052 var
->var
.VariableName
[i
] = dentry
->d_name
.name
[i
];
1054 var
->var
.VariableName
[i
] = '\0';
1056 inode
->i_private
= var
;
1057 var
->efivars
= efivars
;
1058 var
->kobj
.kset
= efivars
->kset
;
1060 err
= kobject_init_and_add(&var
->kobj
, &efivar_ktype
, NULL
, "%s",
1061 dentry
->d_name
.name
);
1065 kobject_uevent(&var
->kobj
, KOBJ_ADD
);
1066 spin_lock_irq(&efivars
->lock
);
1067 list_add(&var
->list
, &efivars
->list
);
1068 spin_unlock_irq(&efivars
->lock
);
1069 d_instantiate(dentry
, inode
);
1079 static int efivarfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1081 struct efivar_entry
*var
= dentry
->d_inode
->i_private
;
1082 struct efivars
*efivars
= var
->efivars
;
1083 efi_status_t status
;
1085 spin_lock_irq(&efivars
->lock
);
1087 status
= efivars
->ops
->set_variable(var
->var
.VariableName
,
1088 &var
->var
.VendorGuid
,
1091 if (status
== EFI_SUCCESS
|| status
== EFI_NOT_FOUND
) {
1092 list_del(&var
->list
);
1093 spin_unlock_irq(&efivars
->lock
);
1094 efivar_unregister(var
);
1095 drop_nlink(dentry
->d_inode
);
1100 spin_unlock_irq(&efivars
->lock
);
1105 * Compare two efivarfs file names.
1107 * An efivarfs filename is composed of two parts,
1109 * 1. A case-sensitive variable name
1110 * 2. A case-insensitive GUID
1112 * So we need to perform a case-sensitive match on part 1 and a
1113 * case-insensitive match on part 2.
1115 static int efivarfs_d_compare(const struct dentry
*parent
, const struct inode
*pinode
,
1116 const struct dentry
*dentry
, const struct inode
*inode
,
1117 unsigned int len
, const char *str
,
1118 const struct qstr
*name
)
1120 int guid
= len
- GUID_LEN
;
1122 if (name
->len
!= len
)
1125 /* Case-sensitive compare for the variable name */
1126 if (memcmp(str
, name
->name
, guid
))
1129 /* Case-insensitive compare for the GUID */
1130 return strncasecmp(name
->name
+ guid
, str
+ guid
, GUID_LEN
);
1133 static int efivarfs_d_hash(const struct dentry
*dentry
,
1134 const struct inode
*inode
, struct qstr
*qstr
)
1136 unsigned long hash
= init_name_hash();
1137 const unsigned char *s
= qstr
->name
;
1138 unsigned int len
= qstr
->len
;
1140 if (!efivarfs_valid_name(s
, len
))
1143 while (len
-- > GUID_LEN
)
1144 hash
= partial_name_hash(*s
++, hash
);
1146 /* GUID is case-insensitive. */
1148 hash
= partial_name_hash(tolower(*s
++), hash
);
1150 qstr
->hash
= end_name_hash(hash
);
1155 * Retaining negative dentries for an in-memory filesystem just wastes
1156 * memory and lookup time: arrange for them to be deleted immediately.
1158 static int efivarfs_delete_dentry(const struct dentry
*dentry
)
1163 static struct dentry_operations efivarfs_d_ops
= {
1164 .d_compare
= efivarfs_d_compare
,
1165 .d_hash
= efivarfs_d_hash
,
1166 .d_delete
= efivarfs_delete_dentry
,
1169 static struct dentry
*efivarfs_alloc_dentry(struct dentry
*parent
, char *name
)
1176 q
.len
= strlen(name
);
1178 err
= efivarfs_d_hash(NULL
, NULL
, &q
);
1180 return ERR_PTR(err
);
1182 d
= d_alloc(parent
, &q
);
1186 return ERR_PTR(-ENOMEM
);
1189 static int efivarfs_fill_super(struct super_block
*sb
, void *data
, int silent
)
1191 struct inode
*inode
= NULL
;
1192 struct dentry
*root
;
1193 struct efivar_entry
*entry
, *n
;
1194 struct efivars
*efivars
= &__efivars
;
1200 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1201 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1202 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1203 sb
->s_magic
= EFIVARFS_MAGIC
;
1204 sb
->s_op
= &efivarfs_ops
;
1205 sb
->s_d_op
= &efivarfs_d_ops
;
1206 sb
->s_time_gran
= 1;
1208 inode
= efivarfs_get_inode(sb
, NULL
, S_IFDIR
| 0755, 0);
1211 inode
->i_op
= &efivarfs_dir_inode_operations
;
1213 root
= d_make_root(inode
);
1218 list_for_each_entry_safe(entry
, n
, &efivars
->list
, list
) {
1219 struct dentry
*dentry
, *root
= efivarfs_sb
->s_root
;
1220 unsigned long size
= 0;
1225 len
= utf16_strlen(entry
->var
.VariableName
);
1227 /* name, plus '-', plus GUID, plus NUL*/
1228 name
= kmalloc(len
+ 1 + GUID_LEN
+ 1, GFP_ATOMIC
);
1232 for (i
= 0; i
< len
; i
++)
1233 name
[i
] = entry
->var
.VariableName
[i
] & 0xFF;
1237 efi_guid_unparse(&entry
->var
.VendorGuid
, name
+ len
+ 1);
1239 name
[len
+GUID_LEN
+1] = '\0';
1241 inode
= efivarfs_get_inode(efivarfs_sb
, root
->d_inode
,
1246 dentry
= efivarfs_alloc_dentry(root
, name
);
1247 if (IS_ERR(dentry
)) {
1248 err
= PTR_ERR(dentry
);
1252 /* copied by the above to local storage in the dentry. */
1255 spin_lock_irq(&efivars
->lock
);
1256 efivars
->ops
->get_variable(entry
->var
.VariableName
,
1257 &entry
->var
.VendorGuid
,
1258 &entry
->var
.Attributes
,
1261 spin_unlock_irq(&efivars
->lock
);
1263 mutex_lock(&inode
->i_mutex
);
1264 inode
->i_private
= entry
;
1265 i_size_write(inode
, size
+ sizeof(entry
->var
.Attributes
));
1266 mutex_unlock(&inode
->i_mutex
);
1267 d_add(dentry
, inode
);
1280 static struct dentry
*efivarfs_mount(struct file_system_type
*fs_type
,
1281 int flags
, const char *dev_name
, void *data
)
1283 return mount_single(fs_type
, flags
, data
, efivarfs_fill_super
);
1286 static void efivarfs_kill_sb(struct super_block
*sb
)
1288 kill_litter_super(sb
);
1292 static struct file_system_type efivarfs_type
= {
1294 .mount
= efivarfs_mount
,
1295 .kill_sb
= efivarfs_kill_sb
,
1297 MODULE_ALIAS_FS("efivarfs");
1300 * Handle negative dentry.
1302 static struct dentry
*efivarfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1305 if (dentry
->d_name
.len
> NAME_MAX
)
1306 return ERR_PTR(-ENAMETOOLONG
);
1307 d_add(dentry
, NULL
);
1311 static const struct inode_operations efivarfs_dir_inode_operations
= {
1312 .lookup
= efivarfs_lookup
,
1313 .unlink
= efivarfs_unlink
,
1314 .create
= efivarfs_create
,
1317 #ifdef CONFIG_EFI_VARS_PSTORE
1319 static int efi_pstore_open(struct pstore_info
*psi
)
1321 struct efivars
*efivars
= psi
->data
;
1323 spin_lock_irq(&efivars
->lock
);
1324 efivars
->walk_entry
= list_first_entry(&efivars
->list
,
1325 struct efivar_entry
, list
);
1329 static int efi_pstore_close(struct pstore_info
*psi
)
1331 struct efivars
*efivars
= psi
->data
;
1333 spin_unlock_irq(&efivars
->lock
);
1337 static ssize_t
efi_pstore_read(u64
*id
, enum pstore_type_id
*type
,
1338 int *count
, struct timespec
*timespec
,
1339 char **buf
, struct pstore_info
*psi
)
1341 efi_guid_t vendor
= LINUX_EFI_CRASH_GUID
;
1342 struct efivars
*efivars
= psi
->data
;
1343 char name
[DUMP_NAME_LEN
];
1346 unsigned int part
, size
;
1349 while (&efivars
->walk_entry
->list
!= &efivars
->list
) {
1350 if (!efi_guidcmp(efivars
->walk_entry
->var
.VendorGuid
,
1352 for (i
= 0; i
< DUMP_NAME_LEN
; i
++) {
1353 name
[i
] = efivars
->walk_entry
->var
.VariableName
[i
];
1355 if (sscanf(name
, "dump-type%u-%u-%d-%lu",
1356 type
, &part
, &cnt
, &time
) == 4) {
1359 timespec
->tv_sec
= time
;
1360 timespec
->tv_nsec
= 0;
1361 } else if (sscanf(name
, "dump-type%u-%u-%lu",
1362 type
, &part
, &time
) == 3) {
1364 * Check if an old format,
1365 * which doesn't support holding
1366 * multiple logs, remains.
1370 timespec
->tv_sec
= time
;
1371 timespec
->tv_nsec
= 0;
1373 efivars
->walk_entry
= list_entry(
1374 efivars
->walk_entry
->list
.next
,
1375 struct efivar_entry
, list
);
1379 get_var_data_locked(efivars
, &efivars
->walk_entry
->var
);
1380 size
= efivars
->walk_entry
->var
.DataSize
;
1381 *buf
= kmalloc(size
, GFP_KERNEL
);
1384 memcpy(*buf
, efivars
->walk_entry
->var
.Data
,
1386 efivars
->walk_entry
= list_entry(
1387 efivars
->walk_entry
->list
.next
,
1388 struct efivar_entry
, list
);
1391 efivars
->walk_entry
= list_entry(efivars
->walk_entry
->list
.next
,
1392 struct efivar_entry
, list
);
1397 static int efi_pstore_write(enum pstore_type_id type
,
1398 enum kmsg_dump_reason reason
, u64
*id
,
1399 unsigned int part
, int count
, size_t size
,
1400 struct pstore_info
*psi
)
1402 char name
[DUMP_NAME_LEN
];
1403 efi_char16_t efi_name
[DUMP_NAME_LEN
];
1404 efi_guid_t vendor
= LINUX_EFI_CRASH_GUID
;
1405 struct efivars
*efivars
= psi
->data
;
1407 efi_status_t status
= EFI_NOT_FOUND
;
1408 unsigned long flags
;
1410 if (pstore_cannot_block_path(reason
)) {
1412 * If the lock is taken by another cpu in non-blocking path,
1413 * this driver returns without entering firmware to avoid
1416 if (!spin_trylock_irqsave(&efivars
->lock
, flags
))
1419 spin_lock_irqsave(&efivars
->lock
, flags
);
1422 * Check if there is a space enough to log.
1423 * size: a size of logging data
1424 * DUMP_NAME_LEN * 2: a maximum size of variable name
1427 status
= check_var_size_locked(efivars
, PSTORE_EFI_ATTRIBUTES
,
1428 size
+ DUMP_NAME_LEN
* 2);
1431 spin_unlock_irqrestore(&efivars
->lock
, flags
);
1436 sprintf(name
, "dump-type%u-%u-%d-%lu", type
, part
, count
,
1439 for (i
= 0; i
< DUMP_NAME_LEN
; i
++)
1440 efi_name
[i
] = name
[i
];
1442 efivars
->ops
->set_variable(efi_name
, &vendor
, PSTORE_EFI_ATTRIBUTES
,
1445 spin_unlock_irqrestore(&efivars
->lock
, flags
);
1447 if (reason
== KMSG_DUMP_OOPS
)
1448 schedule_work(&efivar_work
);
1454 static int efi_pstore_erase(enum pstore_type_id type
, u64 id
, int count
,
1455 struct timespec time
, struct pstore_info
*psi
)
1457 char name
[DUMP_NAME_LEN
];
1458 efi_char16_t efi_name
[DUMP_NAME_LEN
];
1459 char name_old
[DUMP_NAME_LEN
];
1460 efi_char16_t efi_name_old
[DUMP_NAME_LEN
];
1461 efi_guid_t vendor
= LINUX_EFI_CRASH_GUID
;
1462 struct efivars
*efivars
= psi
->data
;
1463 struct efivar_entry
*entry
, *found
= NULL
;
1466 sprintf(name
, "dump-type%u-%u-%d-%lu", type
, (unsigned int)id
, count
,
1469 spin_lock_irq(&efivars
->lock
);
1471 for (i
= 0; i
< DUMP_NAME_LEN
; i
++)
1472 efi_name
[i
] = name
[i
];
1475 * Clean up an entry with the same name
1478 list_for_each_entry(entry
, &efivars
->list
, list
) {
1479 get_var_data_locked(efivars
, &entry
->var
);
1481 if (efi_guidcmp(entry
->var
.VendorGuid
, vendor
))
1483 if (utf16_strncmp(entry
->var
.VariableName
, efi_name
,
1484 utf16_strlen(efi_name
))) {
1486 * Check if an old format,
1487 * which doesn't support holding
1488 * multiple logs, remains.
1490 sprintf(name_old
, "dump-type%u-%u-%lu", type
,
1491 (unsigned int)id
, time
.tv_sec
);
1493 for (i
= 0; i
< DUMP_NAME_LEN
; i
++)
1494 efi_name_old
[i
] = name_old
[i
];
1496 if (utf16_strncmp(entry
->var
.VariableName
, efi_name_old
,
1497 utf16_strlen(efi_name_old
)))
1503 efivars
->ops
->set_variable(entry
->var
.VariableName
,
1504 &entry
->var
.VendorGuid
,
1505 PSTORE_EFI_ATTRIBUTES
,
1511 list_del(&found
->list
);
1513 spin_unlock_irq(&efivars
->lock
);
1516 efivar_unregister(found
);
1521 static struct pstore_info efi_pstore_info
= {
1522 .owner
= THIS_MODULE
,
1524 .open
= efi_pstore_open
,
1525 .close
= efi_pstore_close
,
1526 .read
= efi_pstore_read
,
1527 .write
= efi_pstore_write
,
1528 .erase
= efi_pstore_erase
,
1531 static void efivar_pstore_register(struct efivars
*efivars
)
1533 efivars
->efi_pstore_info
= efi_pstore_info
;
1534 efivars
->efi_pstore_info
.buf
= kmalloc(4096, GFP_KERNEL
);
1535 if (efivars
->efi_pstore_info
.buf
) {
1536 efivars
->efi_pstore_info
.bufsize
= 1024;
1537 efivars
->efi_pstore_info
.data
= efivars
;
1538 spin_lock_init(&efivars
->efi_pstore_info
.buf_lock
);
1539 pstore_register(&efivars
->efi_pstore_info
);
1543 static void efivar_pstore_register(struct efivars
*efivars
)
1549 static ssize_t
efivar_create(struct file
*filp
, struct kobject
*kobj
,
1550 struct bin_attribute
*bin_attr
,
1551 char *buf
, loff_t pos
, size_t count
)
1553 struct efi_variable
*new_var
= (struct efi_variable
*)buf
;
1554 struct efivars
*efivars
= bin_attr
->private;
1555 struct efivar_entry
*search_efivar
, *n
;
1556 unsigned long strsize1
, strsize2
;
1557 efi_status_t status
= EFI_NOT_FOUND
;
1560 if (!capable(CAP_SYS_ADMIN
))
1563 if ((new_var
->Attributes
& ~EFI_VARIABLE_MASK
) != 0 ||
1564 validate_var(new_var
, new_var
->Data
, new_var
->DataSize
) == false) {
1565 printk(KERN_ERR
"efivars: Malformed variable content\n");
1569 spin_lock_irq(&efivars
->lock
);
1572 * Does this variable already exist?
1574 list_for_each_entry_safe(search_efivar
, n
, &efivars
->list
, list
) {
1575 strsize1
= utf16_strsize(search_efivar
->var
.VariableName
, 1024);
1576 strsize2
= utf16_strsize(new_var
->VariableName
, 1024);
1577 if (strsize1
== strsize2
&&
1578 !memcmp(&(search_efivar
->var
.VariableName
),
1579 new_var
->VariableName
, strsize1
) &&
1580 !efi_guidcmp(search_efivar
->var
.VendorGuid
,
1581 new_var
->VendorGuid
)) {
1587 spin_unlock_irq(&efivars
->lock
);
1591 status
= check_var_size_locked(efivars
, new_var
->Attributes
,
1592 new_var
->DataSize
+ utf16_strsize(new_var
->VariableName
, 1024));
1594 if (status
&& status
!= EFI_UNSUPPORTED
) {
1595 spin_unlock_irq(&efivars
->lock
);
1596 return efi_status_to_err(status
);
1599 /* now *really* create the variable via EFI */
1600 status
= efivars
->ops
->set_variable(new_var
->VariableName
,
1601 &new_var
->VendorGuid
,
1602 new_var
->Attributes
,
1606 if (status
!= EFI_SUCCESS
) {
1607 printk(KERN_WARNING
"efivars: set_variable() failed: status=%lx\n",
1609 spin_unlock_irq(&efivars
->lock
);
1612 spin_unlock_irq(&efivars
->lock
);
1614 /* Create the entry in sysfs. Locking is not required here */
1615 status
= efivar_create_sysfs_entry(efivars
,
1616 utf16_strsize(new_var
->VariableName
,
1618 new_var
->VariableName
,
1619 &new_var
->VendorGuid
);
1621 printk(KERN_WARNING
"efivars: variable created, but sysfs entry wasn't.\n");
1626 static ssize_t
efivar_delete(struct file
*filp
, struct kobject
*kobj
,
1627 struct bin_attribute
*bin_attr
,
1628 char *buf
, loff_t pos
, size_t count
)
1630 struct efi_variable
*del_var
= (struct efi_variable
*)buf
;
1631 struct efivars
*efivars
= bin_attr
->private;
1632 struct efivar_entry
*search_efivar
, *n
;
1633 unsigned long strsize1
, strsize2
;
1634 efi_status_t status
= EFI_NOT_FOUND
;
1637 if (!capable(CAP_SYS_ADMIN
))
1640 spin_lock_irq(&efivars
->lock
);
1643 * Does this variable already exist?
1645 list_for_each_entry_safe(search_efivar
, n
, &efivars
->list
, list
) {
1646 strsize1
= utf16_strsize(search_efivar
->var
.VariableName
, 1024);
1647 strsize2
= utf16_strsize(del_var
->VariableName
, 1024);
1648 if (strsize1
== strsize2
&&
1649 !memcmp(&(search_efivar
->var
.VariableName
),
1650 del_var
->VariableName
, strsize1
) &&
1651 !efi_guidcmp(search_efivar
->var
.VendorGuid
,
1652 del_var
->VendorGuid
)) {
1658 spin_unlock_irq(&efivars
->lock
);
1661 /* force the Attributes/DataSize to 0 to ensure deletion */
1662 del_var
->Attributes
= 0;
1663 del_var
->DataSize
= 0;
1665 status
= efivars
->ops
->set_variable(del_var
->VariableName
,
1666 &del_var
->VendorGuid
,
1667 del_var
->Attributes
,
1671 if (status
!= EFI_SUCCESS
) {
1672 printk(KERN_WARNING
"efivars: set_variable() failed: status=%lx\n",
1674 spin_unlock_irq(&efivars
->lock
);
1677 list_del(&search_efivar
->list
);
1678 /* We need to release this lock before unregistering. */
1679 spin_unlock_irq(&efivars
->lock
);
1680 efivar_unregister(search_efivar
);
1682 /* It's dead Jim.... */
1686 static bool variable_is_present(efi_char16_t
*variable_name
, efi_guid_t
*vendor
)
1688 struct efivar_entry
*entry
, *n
;
1689 struct efivars
*efivars
= &__efivars
;
1690 unsigned long strsize1
, strsize2
;
1693 strsize1
= utf16_strsize(variable_name
, 1024);
1694 list_for_each_entry_safe(entry
, n
, &efivars
->list
, list
) {
1695 strsize2
= utf16_strsize(entry
->var
.VariableName
, 1024);
1696 if (strsize1
== strsize2
&&
1697 !memcmp(variable_name
, &(entry
->var
.VariableName
),
1699 !efi_guidcmp(entry
->var
.VendorGuid
,
1708 static void efivar_update_sysfs_entries(struct work_struct
*work
)
1710 struct efivars
*efivars
= &__efivars
;
1712 efi_char16_t
*variable_name
;
1713 unsigned long variable_name_size
= 1024;
1714 efi_status_t status
= EFI_NOT_FOUND
;
1717 /* Add new sysfs entries */
1719 variable_name
= kzalloc(variable_name_size
, GFP_KERNEL
);
1720 if (!variable_name
) {
1721 pr_err("efivars: Memory allocation failed.\n");
1725 spin_lock_irq(&efivars
->lock
);
1728 variable_name_size
= 1024;
1729 status
= efivars
->ops
->get_next_variable(
1730 &variable_name_size
,
1733 if (status
!= EFI_SUCCESS
) {
1736 if (!variable_is_present(variable_name
,
1743 spin_unlock_irq(&efivars
->lock
);
1746 kfree(variable_name
);
1749 efivar_create_sysfs_entry(efivars
,
1751 variable_name
, &vendor
);
1756 * Let's not leave out systab information that snuck into
1757 * the efivars driver
1759 static ssize_t
systab_show(struct kobject
*kobj
,
1760 struct kobj_attribute
*attr
, char *buf
)
1767 if (efi
.mps
!= EFI_INVALID_TABLE_ADDR
)
1768 str
+= sprintf(str
, "MPS=0x%lx\n", efi
.mps
);
1769 if (efi
.acpi20
!= EFI_INVALID_TABLE_ADDR
)
1770 str
+= sprintf(str
, "ACPI20=0x%lx\n", efi
.acpi20
);
1771 if (efi
.acpi
!= EFI_INVALID_TABLE_ADDR
)
1772 str
+= sprintf(str
, "ACPI=0x%lx\n", efi
.acpi
);
1773 if (efi
.smbios
!= EFI_INVALID_TABLE_ADDR
)
1774 str
+= sprintf(str
, "SMBIOS=0x%lx\n", efi
.smbios
);
1775 if (efi
.hcdp
!= EFI_INVALID_TABLE_ADDR
)
1776 str
+= sprintf(str
, "HCDP=0x%lx\n", efi
.hcdp
);
1777 if (efi
.boot_info
!= EFI_INVALID_TABLE_ADDR
)
1778 str
+= sprintf(str
, "BOOTINFO=0x%lx\n", efi
.boot_info
);
1779 if (efi
.uga
!= EFI_INVALID_TABLE_ADDR
)
1780 str
+= sprintf(str
, "UGA=0x%lx\n", efi
.uga
);
1785 static struct kobj_attribute efi_attr_systab
=
1786 __ATTR(systab
, 0400, systab_show
, NULL
);
1788 static struct attribute
*efi_subsys_attrs
[] = {
1789 &efi_attr_systab
.attr
,
1790 NULL
, /* maybe more in the future? */
1793 static struct attribute_group efi_subsys_attr_group
= {
1794 .attrs
= efi_subsys_attrs
,
1797 static struct kobject
*efi_kobj
;
1800 * efivar_create_sysfs_entry()
1802 * variable_name_size = number of bytes required to hold
1803 * variable_name (not counting the NULL
1804 * character at the end.
1805 * efivars->lock is not held on entry or exit.
1806 * Returns 1 on failure, 0 on success
1809 efivar_create_sysfs_entry(struct efivars
*efivars
,
1810 unsigned long variable_name_size
,
1811 efi_char16_t
*variable_name
,
1812 efi_guid_t
*vendor_guid
)
1814 int i
, short_name_size
;
1816 struct efivar_entry
*new_efivar
;
1819 * Length of the variable bytes in ASCII, plus the '-' separator,
1820 * plus the GUID, plus trailing NUL
1822 short_name_size
= variable_name_size
/ sizeof(efi_char16_t
)
1825 short_name
= kzalloc(short_name_size
, GFP_KERNEL
);
1826 new_efivar
= kzalloc(sizeof(struct efivar_entry
), GFP_KERNEL
);
1828 if (!short_name
|| !new_efivar
) {
1834 new_efivar
->efivars
= efivars
;
1835 memcpy(new_efivar
->var
.VariableName
, variable_name
,
1836 variable_name_size
);
1837 memcpy(&(new_efivar
->var
.VendorGuid
), vendor_guid
, sizeof(efi_guid_t
));
1839 /* Convert Unicode to normal chars (assume top bits are 0),
1841 for (i
=0; i
< (int)(variable_name_size
/ sizeof(efi_char16_t
)); i
++) {
1842 short_name
[i
] = variable_name
[i
] & 0xFF;
1844 /* This is ugly, but necessary to separate one vendor's
1845 private variables from another's. */
1847 *(short_name
+ strlen(short_name
)) = '-';
1848 efi_guid_unparse(vendor_guid
, short_name
+ strlen(short_name
));
1850 new_efivar
->kobj
.kset
= efivars
->kset
;
1851 i
= kobject_init_and_add(&new_efivar
->kobj
, &efivar_ktype
, NULL
,
1859 kobject_uevent(&new_efivar
->kobj
, KOBJ_ADD
);
1863 spin_lock_irq(&efivars
->lock
);
1864 list_add(&new_efivar
->list
, &efivars
->list
);
1865 spin_unlock_irq(&efivars
->lock
);
1871 create_efivars_bin_attributes(struct efivars
*efivars
)
1873 struct bin_attribute
*attr
;
1877 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
1881 attr
->attr
.name
= "new_var";
1882 attr
->attr
.mode
= 0200;
1883 attr
->write
= efivar_create
;
1884 attr
->private = efivars
;
1885 efivars
->new_var
= attr
;
1888 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
1893 attr
->attr
.name
= "del_var";
1894 attr
->attr
.mode
= 0200;
1895 attr
->write
= efivar_delete
;
1896 attr
->private = efivars
;
1897 efivars
->del_var
= attr
;
1899 sysfs_bin_attr_init(efivars
->new_var
);
1900 sysfs_bin_attr_init(efivars
->del_var
);
1903 error
= sysfs_create_bin_file(&efivars
->kset
->kobj
,
1906 printk(KERN_ERR
"efivars: unable to create new_var sysfs file"
1907 " due to error %d\n", error
);
1910 error
= sysfs_create_bin_file(&efivars
->kset
->kobj
,
1913 printk(KERN_ERR
"efivars: unable to create del_var sysfs file"
1914 " due to error %d\n", error
);
1915 sysfs_remove_bin_file(&efivars
->kset
->kobj
,
1922 kfree(efivars
->del_var
);
1923 efivars
->del_var
= NULL
;
1924 kfree(efivars
->new_var
);
1925 efivars
->new_var
= NULL
;
1929 void unregister_efivars(struct efivars
*efivars
)
1931 struct efivar_entry
*entry
, *n
;
1933 list_for_each_entry_safe(entry
, n
, &efivars
->list
, list
) {
1934 spin_lock_irq(&efivars
->lock
);
1935 list_del(&entry
->list
);
1936 spin_unlock_irq(&efivars
->lock
);
1937 efivar_unregister(entry
);
1939 if (efivars
->new_var
)
1940 sysfs_remove_bin_file(&efivars
->kset
->kobj
, efivars
->new_var
);
1941 if (efivars
->del_var
)
1942 sysfs_remove_bin_file(&efivars
->kset
->kobj
, efivars
->del_var
);
1943 kfree(efivars
->new_var
);
1944 kfree(efivars
->del_var
);
1945 kobject_put(efivars
->kobject
);
1946 kset_unregister(efivars
->kset
);
1948 EXPORT_SYMBOL_GPL(unregister_efivars
);
1950 int register_efivars(struct efivars
*efivars
,
1951 const struct efivar_operations
*ops
,
1952 struct kobject
*parent_kobj
)
1954 efi_status_t status
= EFI_NOT_FOUND
;
1955 efi_guid_t vendor_guid
;
1956 efi_char16_t
*variable_name
;
1957 unsigned long variable_name_size
= 1024;
1960 variable_name
= kzalloc(variable_name_size
, GFP_KERNEL
);
1961 if (!variable_name
) {
1962 printk(KERN_ERR
"efivars: Memory allocation failed.\n");
1966 spin_lock_init(&efivars
->lock
);
1967 INIT_LIST_HEAD(&efivars
->list
);
1970 efivars
->kset
= kset_create_and_add("vars", NULL
, parent_kobj
);
1971 if (!efivars
->kset
) {
1972 printk(KERN_ERR
"efivars: Subsystem registration failed.\n");
1977 efivars
->kobject
= kobject_create_and_add("efivars", parent_kobj
);
1978 if (!efivars
->kobject
) {
1979 pr_err("efivars: Subsystem registration failed.\n");
1981 kset_unregister(efivars
->kset
);
1986 * Per EFI spec, the maximum storage allocated for both
1987 * the variable name and variable data is 1024 bytes.
1991 variable_name_size
= 1024;
1993 status
= ops
->get_next_variable(&variable_name_size
,
1998 efivar_create_sysfs_entry(efivars
,
2006 printk(KERN_WARNING
"efivars: get_next_variable: status=%lx\n",
2008 status
= EFI_NOT_FOUND
;
2011 } while (status
!= EFI_NOT_FOUND
);
2013 error
= create_efivars_bin_attributes(efivars
);
2015 unregister_efivars(efivars
);
2017 if (!efivars_pstore_disable
)
2018 efivar_pstore_register(efivars
);
2020 register_filesystem(&efivarfs_type
);
2023 kfree(variable_name
);
2027 EXPORT_SYMBOL_GPL(register_efivars
);
2030 * For now we register the efi subsystem with the firmware subsystem
2031 * and the vars subsystem with the efi subsystem. In the future, it
2032 * might make sense to split off the efi subsystem into its own
2033 * driver, but for now only efivars will register with it, so just
2042 printk(KERN_INFO
"EFI Variables Facility v%s %s\n", EFIVARS_VERSION
,
2045 if (!efi_enabled(EFI_RUNTIME_SERVICES
))
2048 /* For now we'll register the efi directory at /sys/firmware/efi */
2049 efi_kobj
= kobject_create_and_add("efi", firmware_kobj
);
2051 printk(KERN_ERR
"efivars: Firmware registration failed.\n");
2055 ops
.get_variable
= efi
.get_variable
;
2056 ops
.set_variable
= efi
.set_variable
;
2057 ops
.get_next_variable
= efi
.get_next_variable
;
2058 ops
.query_variable_info
= efi
.query_variable_info
;
2060 error
= register_efivars(&__efivars
, &ops
, efi_kobj
);
2064 /* Don't forget the systab entry */
2065 error
= sysfs_create_group(efi_kobj
, &efi_subsys_attr_group
);
2068 "efivars: Sysfs attribute export failed with error %d.\n",
2070 goto err_unregister
;
2076 unregister_efivars(&__efivars
);
2078 kobject_put(efi_kobj
);
2085 cancel_work_sync(&efivar_work
);
2087 if (efi_enabled(EFI_RUNTIME_SERVICES
)) {
2088 unregister_efivars(&__efivars
);
2089 kobject_put(efi_kobj
);
2093 module_init(efivars_init
);
2094 module_exit(efivars_exit
);