1 // SPDX-License-Identifier: GPL-2.0+
3 * Originally from efivars.c,
5 * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
6 * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
8 * This code takes all variables accessible from EFI runtime and
9 * exports them via sysfs
12 #include <linux/efi.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/ucs2_string.h>
16 #include <linux/compat.h>
18 #define EFIVARS_VERSION "0.08"
19 #define EFIVARS_DATE "2004-May-17"
21 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
22 MODULE_DESCRIPTION("sysfs interface to EFI Variables");
23 MODULE_LICENSE("GPL");
24 MODULE_VERSION(EFIVARS_VERSION);
25 MODULE_ALIAS("platform:efivars");
27 LIST_HEAD(efivar_sysfs_list);
28 EXPORT_SYMBOL_GPL(efivar_sysfs_list);
30 static struct kset *efivars_kset;
32 static struct bin_attribute *efivars_new_var;
33 static struct bin_attribute *efivars_del_var;
35 struct compat_efi_variable {
36 efi_char16_t VariableName[EFI_VAR_NAME_LEN/sizeof(efi_char16_t)];
37 efi_guid_t VendorGuid;
44 struct efivar_attribute {
45 struct attribute attr;
46 ssize_t (*show) (struct efivar_entry *entry, char *buf);
47 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
50 #define EFIVAR_ATTR(_name, _mode, _show, _store) \
51 struct efivar_attribute efivar_attr_##_name = { \
52 .attr = {.name = __stringify(_name), .mode = _mode}, \
57 #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
58 #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
61 * Prototype for sysfs creation function
64 efivar_create_sysfs_entry(struct efivar_entry *new_var);
67 efivar_guid_read(struct efivar_entry *entry, char *buf)
69 struct efi_variable *var = &entry->var;
75 efi_guid_to_str(&var->VendorGuid, str);
77 str += sprintf(str, "\n");
83 efivar_attr_read(struct efivar_entry *entry, char *buf)
85 struct efi_variable *var = &entry->var;
86 unsigned long size = sizeof(var->Data);
93 ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
98 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
99 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
100 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
101 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
102 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
103 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
104 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
105 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
106 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
108 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
109 if (var->Attributes &
110 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
112 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
113 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
114 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
119 efivar_size_read(struct efivar_entry *entry, char *buf)
121 struct efi_variable *var = &entry->var;
122 unsigned long size = sizeof(var->Data);
129 ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
130 var->DataSize = size;
134 str += sprintf(str, "0x%lx\n", var->DataSize);
139 efivar_data_read(struct efivar_entry *entry, char *buf)
141 struct efi_variable *var = &entry->var;
142 unsigned long size = sizeof(var->Data);
148 ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
149 var->DataSize = size;
153 memcpy(buf, var->Data, var->DataSize);
154 return var->DataSize;
158 sanity_check(struct efi_variable *var, efi_char16_t *name, efi_guid_t vendor,
159 unsigned long size, u32 attributes, u8 *data)
162 * If only updating the variable data, then the name
163 * and guid should remain the same
165 if (memcmp(name, var->VariableName, sizeof(var->VariableName)) ||
166 efi_guidcmp(vendor, var->VendorGuid)) {
167 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
171 if ((size <= 0) || (attributes == 0)){
172 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
176 if ((attributes & ~EFI_VARIABLE_MASK) != 0 ||
177 efivar_validate(vendor, name, data, size) == false) {
178 printk(KERN_ERR "efivars: Malformed variable content\n");
186 copy_out_compat(struct efi_variable *dst, struct compat_efi_variable *src)
188 memcpy(dst->VariableName, src->VariableName, EFI_VAR_NAME_LEN);
189 memcpy(dst->Data, src->Data, sizeof(src->Data));
191 dst->VendorGuid = src->VendorGuid;
192 dst->DataSize = src->DataSize;
193 dst->Attributes = src->Attributes;
197 * We allow each variable to be edited via rewriting the
198 * entire efi variable structure.
201 efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
203 struct efi_variable *new_var, *var = &entry->var;
211 if (in_compat_syscall()) {
212 struct compat_efi_variable *compat;
214 if (count != sizeof(*compat))
217 compat = (struct compat_efi_variable *)buf;
218 attributes = compat->Attributes;
219 vendor = compat->VendorGuid;
220 name = compat->VariableName;
221 size = compat->DataSize;
224 err = sanity_check(var, name, vendor, size, attributes, data);
228 copy_out_compat(&entry->var, compat);
230 if (count != sizeof(struct efi_variable))
233 new_var = (struct efi_variable *)buf;
235 attributes = new_var->Attributes;
236 vendor = new_var->VendorGuid;
237 name = new_var->VariableName;
238 size = new_var->DataSize;
239 data = new_var->Data;
241 err = sanity_check(var, name, vendor, size, attributes, data);
245 memcpy(&entry->var, new_var, count);
248 err = efivar_entry_set(entry, attributes, size, data, NULL);
250 printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err);
258 efivar_show_raw(struct efivar_entry *entry, char *buf)
260 struct efi_variable *var = &entry->var;
261 struct compat_efi_variable *compat;
262 unsigned long datasize = sizeof(var->Data);
269 ret = efivar_entry_get(entry, &var->Attributes, &datasize, var->Data);
270 var->DataSize = datasize;
274 if (in_compat_syscall()) {
275 compat = (struct compat_efi_variable *)buf;
277 size = sizeof(*compat);
278 memcpy(compat->VariableName, var->VariableName,
280 memcpy(compat->Data, var->Data, sizeof(compat->Data));
282 compat->VendorGuid = var->VendorGuid;
283 compat->DataSize = var->DataSize;
284 compat->Attributes = var->Attributes;
287 memcpy(buf, var, size);
294 * Generic read/write functions that call the specific functions of
297 static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
300 struct efivar_entry *var = to_efivar_entry(kobj);
301 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
304 if (!capable(CAP_SYS_ADMIN))
307 if (efivar_attr->show) {
308 ret = efivar_attr->show(var, buf);
313 static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
314 const char *buf, size_t count)
316 struct efivar_entry *var = to_efivar_entry(kobj);
317 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
320 if (!capable(CAP_SYS_ADMIN))
323 if (efivar_attr->store)
324 ret = efivar_attr->store(var, buf, count);
329 static const struct sysfs_ops efivar_attr_ops = {
330 .show = efivar_attr_show,
331 .store = efivar_attr_store,
334 static void efivar_release(struct kobject *kobj)
336 struct efivar_entry *var = to_efivar_entry(kobj);
340 static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
341 static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
342 static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
343 static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
344 static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
346 static struct attribute *def_attrs[] = {
347 &efivar_attr_guid.attr,
348 &efivar_attr_size.attr,
349 &efivar_attr_attributes.attr,
350 &efivar_attr_data.attr,
351 &efivar_attr_raw_var.attr,
355 static struct kobj_type efivar_ktype = {
356 .release = efivar_release,
357 .sysfs_ops = &efivar_attr_ops,
358 .default_attrs = def_attrs,
361 static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
362 struct bin_attribute *bin_attr,
363 char *buf, loff_t pos, size_t count)
365 struct compat_efi_variable *compat = (struct compat_efi_variable *)buf;
366 struct efi_variable *new_var = (struct efi_variable *)buf;
367 struct efivar_entry *new_entry;
368 bool need_compat = in_compat_syscall();
375 if (!capable(CAP_SYS_ADMIN))
379 if (count != sizeof(*compat))
382 attributes = compat->Attributes;
383 name = compat->VariableName;
384 size = compat->DataSize;
387 if (count != sizeof(*new_var))
390 attributes = new_var->Attributes;
391 name = new_var->VariableName;
392 size = new_var->DataSize;
393 data = new_var->Data;
396 if ((attributes & ~EFI_VARIABLE_MASK) != 0 ||
397 efivar_validate(new_var->VendorGuid, name, data,
399 printk(KERN_ERR "efivars: Malformed variable content\n");
403 new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL);
408 copy_out_compat(&new_entry->var, compat);
410 memcpy(&new_entry->var, new_var, sizeof(*new_var));
412 err = efivar_entry_set(new_entry, attributes, size,
413 data, &efivar_sysfs_list);
420 if (efivar_create_sysfs_entry(new_entry)) {
421 printk(KERN_WARNING "efivars: failed to create sysfs entry.\n");
431 static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
432 struct bin_attribute *bin_attr,
433 char *buf, loff_t pos, size_t count)
435 struct efi_variable *del_var = (struct efi_variable *)buf;
436 struct compat_efi_variable *compat;
437 struct efivar_entry *entry;
442 if (!capable(CAP_SYS_ADMIN))
445 if (in_compat_syscall()) {
446 if (count != sizeof(*compat))
449 compat = (struct compat_efi_variable *)buf;
450 name = compat->VariableName;
451 vendor = compat->VendorGuid;
453 if (count != sizeof(*del_var))
456 name = del_var->VariableName;
457 vendor = del_var->VendorGuid;
460 if (efivar_entry_iter_begin())
462 entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true);
465 else if (__efivar_entry_delete(entry))
469 efivar_entry_iter_end();
473 if (!entry->scanning) {
474 efivar_entry_iter_end();
475 efivar_unregister(entry);
477 efivar_entry_iter_end();
479 /* It's dead Jim.... */
484 * efivar_create_sysfs_entry - create a new entry in sysfs
485 * @new_var: efivar entry to create
487 * Returns 0 on success, negative error code on failure
490 efivar_create_sysfs_entry(struct efivar_entry *new_var)
494 unsigned long utf8_name_size;
495 efi_char16_t *variable_name = new_var->var.VariableName;
499 * Length of the variable bytes in UTF8, plus the '-' separator,
500 * plus the GUID, plus trailing NUL
502 utf8_name_size = ucs2_utf8size(variable_name);
503 short_name_size = utf8_name_size + 1 + EFI_VARIABLE_GUID_LEN + 1;
505 short_name = kmalloc(short_name_size, GFP_KERNEL);
509 ucs2_as_utf8(short_name, variable_name, short_name_size);
511 /* This is ugly, but necessary to separate one vendor's
512 private variables from another's. */
513 short_name[utf8_name_size] = '-';
514 efi_guid_to_str(&new_var->var.VendorGuid,
515 short_name + utf8_name_size + 1);
517 new_var->kobj.kset = efivars_kset;
519 ret = kobject_init_and_add(&new_var->kobj, &efivar_ktype,
520 NULL, "%s", short_name);
525 kobject_uevent(&new_var->kobj, KOBJ_ADD);
526 if (efivar_entry_add(new_var, &efivar_sysfs_list)) {
527 efivar_unregister(new_var);
535 create_efivars_bin_attributes(void)
537 struct bin_attribute *attr;
541 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
545 attr->attr.name = "new_var";
546 attr->attr.mode = 0200;
547 attr->write = efivar_create;
548 efivars_new_var = attr;
551 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
556 attr->attr.name = "del_var";
557 attr->attr.mode = 0200;
558 attr->write = efivar_delete;
559 efivars_del_var = attr;
561 sysfs_bin_attr_init(efivars_new_var);
562 sysfs_bin_attr_init(efivars_del_var);
565 error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_new_var);
567 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
568 " due to error %d\n", error);
572 error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_del_var);
574 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
575 " due to error %d\n", error);
576 sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
582 kfree(efivars_del_var);
583 efivars_del_var = NULL;
584 kfree(efivars_new_var);
585 efivars_new_var = NULL;
589 static int efivar_update_sysfs_entry(efi_char16_t *name, efi_guid_t vendor,
590 unsigned long name_size, void *data)
592 struct efivar_entry *entry = data;
594 if (efivar_entry_find(name, vendor, &efivar_sysfs_list, false))
597 memcpy(entry->var.VariableName, name, name_size);
598 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
603 static void efivar_update_sysfs_entries(struct work_struct *work)
605 struct efivar_entry *entry;
608 /* Add new sysfs entries */
610 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
614 err = efivar_init(efivar_update_sysfs_entry, entry,
615 false, &efivar_sysfs_list);
619 efivar_create_sysfs_entry(entry);
625 static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
626 unsigned long name_size, void *data)
628 struct efivar_entry *entry;
630 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
634 memcpy(entry->var.VariableName, name, name_size);
635 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
637 efivar_create_sysfs_entry(entry);
642 static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
644 int err = efivar_entry_remove(entry);
648 efivar_unregister(entry);
652 static void efivars_sysfs_exit(void)
654 /* Remove all entries and destroy */
657 err = __efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list,
660 pr_err("efivars: Failed to destroy sysfs entries\n");
665 sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
667 sysfs_remove_bin_file(&efivars_kset->kobj, efivars_del_var);
668 kfree(efivars_new_var);
669 kfree(efivars_del_var);
670 kset_unregister(efivars_kset);
673 int efivars_sysfs_init(void)
675 struct kobject *parent_kobj = efivars_kobject();
678 if (!efi_enabled(EFI_RUNTIME_SERVICES))
681 /* No efivars has been registered yet */
685 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
688 efivars_kset = kset_create_and_add("vars", NULL, parent_kobj);
690 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
694 efivar_init(efivars_sysfs_callback, NULL, true, &efivar_sysfs_list);
696 error = create_efivars_bin_attributes();
698 efivars_sysfs_exit();
702 INIT_WORK(&efivar_work, efivar_update_sysfs_entries);
706 EXPORT_SYMBOL_GPL(efivars_sysfs_init);
708 module_init(efivars_sysfs_init);
709 module_exit(efivars_sysfs_exit);