1 // SPDX-License-Identifier: GPL-2.0+
3 * Originally from efivars.c,
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"
22 MODULE_DESCRIPTION("sysfs interface to EFI Variables");
23 MODULE_LICENSE("GPL");
24 MODULE_VERSION(EFIVARS_VERSION);
26 static LIST_HEAD(efivar_sysfs_list);
28 static struct kset *efivars_kset;
30 static struct bin_attribute *efivars_new_var;
31 static struct bin_attribute *efivars_del_var;
33 struct compat_efi_variable {
34 efi_char16_t VariableName[EFI_VAR_NAME_LEN/sizeof(efi_char16_t)];
35 efi_guid_t VendorGuid;
42 struct efivar_attribute {
43 struct attribute attr;
44 ssize_t (*show) (struct efivar_entry *entry, char *buf);
45 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
48 #define EFIVAR_ATTR(_name, _mode, _show, _store) \
49 struct efivar_attribute efivar_attr_##_name = { \
50 .attr = {.name = __stringify(_name), .mode = _mode}, \
55 #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
56 #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
59 * Prototype for sysfs creation function
62 efivar_create_sysfs_entry(struct efivar_entry *new_var);
65 efivar_guid_read(struct efivar_entry *entry, char *buf)
67 struct efi_variable *var = &entry->var;
73 efi_guid_to_str(&var->VendorGuid, str);
75 str += sprintf(str, "\n");
81 efivar_attr_read(struct efivar_entry *entry, char *buf)
83 struct efi_variable *var = &entry->var;
84 unsigned long size = sizeof(var->Data);
91 ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
96 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
97 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
98 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
99 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
100 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
101 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
102 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
103 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
104 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
106 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
107 if (var->Attributes &
108 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
110 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
111 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
112 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
117 efivar_size_read(struct efivar_entry *entry, char *buf)
119 struct efi_variable *var = &entry->var;
120 unsigned long size = sizeof(var->Data);
127 ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
128 var->DataSize = size;
132 str += sprintf(str, "0x%lx\n", var->DataSize);
137 efivar_data_read(struct efivar_entry *entry, char *buf)
139 struct efi_variable *var = &entry->var;
140 unsigned long size = sizeof(var->Data);
146 ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
147 var->DataSize = size;
151 memcpy(buf, var->Data, var->DataSize);
152 return var->DataSize;
156 sanity_check(struct efi_variable *var, efi_char16_t *name, efi_guid_t vendor,
157 unsigned long size, u32 attributes, u8 *data)
160 * If only updating the variable data, then the name
161 * and guid should remain the same
163 if (memcmp(name, var->VariableName, sizeof(var->VariableName)) ||
164 efi_guidcmp(vendor, var->VendorGuid)) {
165 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
169 if ((size <= 0) || (attributes == 0)){
170 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
174 if ((attributes & ~EFI_VARIABLE_MASK) != 0 ||
175 efivar_validate(vendor, name, data, size) == false) {
176 printk(KERN_ERR "efivars: Malformed variable content\n");
184 copy_out_compat(struct efi_variable *dst, struct compat_efi_variable *src)
186 memcpy(dst->VariableName, src->VariableName, EFI_VAR_NAME_LEN);
187 memcpy(dst->Data, src->Data, sizeof(src->Data));
189 dst->VendorGuid = src->VendorGuid;
190 dst->DataSize = src->DataSize;
191 dst->Attributes = src->Attributes;
195 * We allow each variable to be edited via rewriting the
196 * entire efi variable structure.
199 efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
201 struct efi_variable *new_var, *var = &entry->var;
212 if (in_compat_syscall()) {
213 struct compat_efi_variable *compat;
215 if (count != sizeof(*compat))
218 compat = (struct compat_efi_variable *)buf;
219 attributes = compat->Attributes;
220 vendor = compat->VendorGuid;
221 name = compat->VariableName;
222 size = compat->DataSize;
225 err = sanity_check(var, name, vendor, size, attributes, data);
229 copy_out_compat(&entry->var, compat);
231 if (count != sizeof(struct efi_variable))
234 new_var = (struct efi_variable *)buf;
236 attributes = new_var->Attributes;
237 vendor = new_var->VendorGuid;
238 name = new_var->VariableName;
239 size = new_var->DataSize;
240 data = new_var->Data;
242 err = sanity_check(var, name, vendor, size, attributes, data);
246 memcpy(&entry->var, new_var, count);
249 err = efivar_entry_set(entry, attributes, size, data, NULL);
251 printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err);
259 efivar_show_raw(struct efivar_entry *entry, char *buf)
261 struct efi_variable *var = &entry->var;
262 struct compat_efi_variable *compat;
263 unsigned long datasize = sizeof(var->Data);
270 ret = efivar_entry_get(entry, &var->Attributes, &datasize, var->Data);
271 var->DataSize = datasize;
275 if (in_compat_syscall()) {
276 compat = (struct compat_efi_variable *)buf;
278 size = sizeof(*compat);
279 memcpy(compat->VariableName, var->VariableName,
281 memcpy(compat->Data, var->Data, sizeof(compat->Data));
283 compat->VendorGuid = var->VendorGuid;
284 compat->DataSize = var->DataSize;
285 compat->Attributes = var->Attributes;
288 memcpy(buf, var, size);
295 * Generic read/write functions that call the specific functions of
298 static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
301 struct efivar_entry *var = to_efivar_entry(kobj);
302 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
305 if (!capable(CAP_SYS_ADMIN))
308 if (efivar_attr->show) {
309 ret = efivar_attr->show(var, buf);
314 static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
315 const char *buf, size_t count)
317 struct efivar_entry *var = to_efivar_entry(kobj);
318 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
321 if (!capable(CAP_SYS_ADMIN))
324 if (efivar_attr->store)
325 ret = efivar_attr->store(var, buf, count);
330 static const struct sysfs_ops efivar_attr_ops = {
331 .show = efivar_attr_show,
332 .store = efivar_attr_store,
335 static void efivar_release(struct kobject *kobj)
337 struct efivar_entry *var = to_efivar_entry(kobj);
341 static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
342 static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
343 static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
344 static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
345 static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
347 static struct attribute *def_attrs[] = {
348 &efivar_attr_guid.attr,
349 &efivar_attr_size.attr,
350 &efivar_attr_attributes.attr,
351 &efivar_attr_data.attr,
352 &efivar_attr_raw_var.attr,
356 static struct kobj_type efivar_ktype = {
357 .release = efivar_release,
358 .sysfs_ops = &efivar_attr_ops,
359 .default_attrs = def_attrs,
362 static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
363 struct bin_attribute *bin_attr,
364 char *buf, loff_t pos, size_t count)
366 struct compat_efi_variable *compat = (struct compat_efi_variable *)buf;
367 struct efi_variable *new_var = (struct efi_variable *)buf;
368 struct efivar_entry *new_entry;
369 bool need_compat = in_compat_syscall();
376 if (!capable(CAP_SYS_ADMIN))
380 if (count != sizeof(*compat))
383 attributes = compat->Attributes;
384 name = compat->VariableName;
385 size = compat->DataSize;
388 if (count != sizeof(*new_var))
391 attributes = new_var->Attributes;
392 name = new_var->VariableName;
393 size = new_var->DataSize;
394 data = new_var->Data;
397 if ((attributes & ~EFI_VARIABLE_MASK) != 0 ||
398 efivar_validate(new_var->VendorGuid, name, data,
400 printk(KERN_ERR "efivars: Malformed variable content\n");
404 new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL);
409 copy_out_compat(&new_entry->var, compat);
411 memcpy(&new_entry->var, new_var, sizeof(*new_var));
413 err = efivar_entry_set(new_entry, attributes, size,
414 data, &efivar_sysfs_list);
421 if (efivar_create_sysfs_entry(new_entry)) {
422 printk(KERN_WARNING "efivars: failed to create sysfs entry.\n");
432 static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
433 struct bin_attribute *bin_attr,
434 char *buf, loff_t pos, size_t count)
436 struct efi_variable *del_var = (struct efi_variable *)buf;
437 struct compat_efi_variable *compat;
438 struct efivar_entry *entry;
443 if (!capable(CAP_SYS_ADMIN))
446 if (in_compat_syscall()) {
447 if (count != sizeof(*compat))
450 compat = (struct compat_efi_variable *)buf;
451 name = compat->VariableName;
452 vendor = compat->VendorGuid;
454 if (count != sizeof(*del_var))
457 name = del_var->VariableName;
458 vendor = del_var->VendorGuid;
461 if (efivar_entry_iter_begin())
463 entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true);
466 else if (__efivar_entry_delete(entry))
470 efivar_entry_iter_end();
474 if (!entry->scanning) {
475 efivar_entry_iter_end();
476 efivar_unregister(entry);
478 efivar_entry_iter_end();
480 /* It's dead Jim.... */
485 * efivar_create_sysfs_entry - create a new entry in sysfs
486 * @new_var: efivar entry to create
488 * Returns 0 on success, negative error code on failure
491 efivar_create_sysfs_entry(struct efivar_entry *new_var)
495 unsigned long utf8_name_size;
496 efi_char16_t *variable_name = new_var->var.VariableName;
500 * Length of the variable bytes in UTF8, plus the '-' separator,
501 * plus the GUID, plus trailing NUL
503 utf8_name_size = ucs2_utf8size(variable_name);
504 short_name_size = utf8_name_size + 1 + EFI_VARIABLE_GUID_LEN + 1;
506 short_name = kmalloc(short_name_size, GFP_KERNEL);
510 ucs2_as_utf8(short_name, variable_name, short_name_size);
512 /* This is ugly, but necessary to separate one vendor's
513 private variables from another's. */
514 short_name[utf8_name_size] = '-';
515 efi_guid_to_str(&new_var->var.VendorGuid,
516 short_name + utf8_name_size + 1);
518 new_var->kobj.kset = efivars_kset;
520 ret = kobject_init_and_add(&new_var->kobj, &efivar_ktype,
521 NULL, "%s", short_name);
524 kobject_put(&new_var->kobj);
528 kobject_uevent(&new_var->kobj, KOBJ_ADD);
529 if (efivar_entry_add(new_var, &efivar_sysfs_list)) {
530 efivar_unregister(new_var);
538 create_efivars_bin_attributes(void)
540 struct bin_attribute *attr;
544 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
548 attr->attr.name = "new_var";
549 attr->attr.mode = 0200;
550 attr->write = efivar_create;
551 efivars_new_var = attr;
554 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
559 attr->attr.name = "del_var";
560 attr->attr.mode = 0200;
561 attr->write = efivar_delete;
562 efivars_del_var = attr;
564 sysfs_bin_attr_init(efivars_new_var);
565 sysfs_bin_attr_init(efivars_del_var);
568 error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_new_var);
570 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
571 " due to error %d\n", error);
575 error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_del_var);
577 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
578 " due to error %d\n", error);
579 sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
585 kfree(efivars_del_var);
586 efivars_del_var = NULL;
587 kfree(efivars_new_var);
588 efivars_new_var = NULL;
592 static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
593 unsigned long name_size, void *data)
595 struct efivar_entry *entry;
597 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
601 memcpy(entry->var.VariableName, name, name_size);
602 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
604 efivar_create_sysfs_entry(entry);
609 static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
611 int err = efivar_entry_remove(entry);
615 efivar_unregister(entry);
619 static void efivars_sysfs_exit(void)
621 /* Remove all entries and destroy */
624 err = __efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list,
627 pr_err("efivars: Failed to destroy sysfs entries\n");
632 sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
634 sysfs_remove_bin_file(&efivars_kset->kobj, efivars_del_var);
635 kfree(efivars_new_var);
636 kfree(efivars_del_var);
637 kset_unregister(efivars_kset);
640 static int efivars_sysfs_init(void)
642 struct kobject *parent_kobj = efivars_kobject();
645 /* No efivars has been registered yet */
646 if (!parent_kobj || !efivar_supports_writes())
649 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
652 efivars_kset = kset_create_and_add("vars", NULL, parent_kobj);
654 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
658 efivar_init(efivars_sysfs_callback, NULL, true, &efivar_sysfs_list);
660 error = create_efivars_bin_attributes();
662 efivars_sysfs_exit();
669 module_init(efivars_sysfs_init);
670 module_exit(efivars_sysfs_exit);