1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68#include <linux/efi.h>
69#include <linux/module.h>
70#include <linux/slab.h>
71#include <linux/ucs2_string.h>
72#include <linux/compat.h>
73
74#define EFIVARS_VERSION "0.08"
75#define EFIVARS_DATE "2004-May-17"
76
77MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
78MODULE_DESCRIPTION("sysfs interface to EFI Variables");
79MODULE_LICENSE("GPL");
80MODULE_VERSION(EFIVARS_VERSION);
81MODULE_ALIAS("platform:efivars");
82
83LIST_HEAD(efivar_sysfs_list);
84EXPORT_SYMBOL_GPL(efivar_sysfs_list);
85
86static struct kset *efivars_kset;
87
88static struct bin_attribute *efivars_new_var;
89static struct bin_attribute *efivars_del_var;
90
91struct compat_efi_variable {
92 efi_char16_t VariableName[EFI_VAR_NAME_LEN/sizeof(efi_char16_t)];
93 efi_guid_t VendorGuid;
94 __u32 DataSize;
95 __u8 Data[1024];
96 __u32 Status;
97 __u32 Attributes;
98} __packed;
99
100struct efivar_attribute {
101 struct attribute attr;
102 ssize_t (*show) (struct efivar_entry *entry, char *buf);
103 ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
104};
105
106#define EFIVAR_ATTR(_name, _mode, _show, _store) \
107struct efivar_attribute efivar_attr_##_name = { \
108 .attr = {.name = __stringify(_name), .mode = _mode}, \
109 .show = _show, \
110 .store = _store, \
111};
112
113#define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
114#define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
115
116
117
118
119static int
120efivar_create_sysfs_entry(struct efivar_entry *new_var);
121
122static ssize_t
123efivar_guid_read(struct efivar_entry *entry, char *buf)
124{
125 struct efi_variable *var = &entry->var;
126 char *str = buf;
127
128 if (!entry || !buf)
129 return 0;
130
131 efi_guid_to_str(&var->VendorGuid, str);
132 str += strlen(str);
133 str += sprintf(str, "\n");
134
135 return str - buf;
136}
137
138static ssize_t
139efivar_attr_read(struct efivar_entry *entry, char *buf)
140{
141 struct efi_variable *var = &entry->var;
142 char *str = buf;
143
144 if (!entry || !buf)
145 return -EINVAL;
146
147 var->DataSize = 1024;
148 if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
149 return -EIO;
150
151 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
152 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
153 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
154 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
155 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
156 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
157 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
158 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
159 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
160 str += sprintf(str,
161 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
162 if (var->Attributes &
163 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
164 str += sprintf(str,
165 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
166 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
167 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
168 return str - buf;
169}
170
171static ssize_t
172efivar_size_read(struct efivar_entry *entry, char *buf)
173{
174 struct efi_variable *var = &entry->var;
175 char *str = buf;
176
177 if (!entry || !buf)
178 return -EINVAL;
179
180 var->DataSize = 1024;
181 if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
182 return -EIO;
183
184 str += sprintf(str, "0x%lx\n", var->DataSize);
185 return str - buf;
186}
187
188static ssize_t
189efivar_data_read(struct efivar_entry *entry, char *buf)
190{
191 struct efi_variable *var = &entry->var;
192
193 if (!entry || !buf)
194 return -EINVAL;
195
196 var->DataSize = 1024;
197 if (efivar_entry_get(entry, &var->Attributes, &var->DataSize, var->Data))
198 return -EIO;
199
200 memcpy(buf, var->Data, var->DataSize);
201 return var->DataSize;
202}
203
204static inline int
205sanity_check(struct efi_variable *var, efi_char16_t *name, efi_guid_t vendor,
206 unsigned long size, u32 attributes, u8 *data)
207{
208
209
210
211
212 if (memcmp(name, var->VariableName, sizeof(var->VariableName)) ||
213 efi_guidcmp(vendor, var->VendorGuid)) {
214 printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
215 return -EINVAL;
216 }
217
218 if ((size <= 0) || (attributes == 0)){
219 printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
220 return -EINVAL;
221 }
222
223 if ((attributes & ~EFI_VARIABLE_MASK) != 0 ||
224 efivar_validate(vendor, name, data, size) == false) {
225 printk(KERN_ERR "efivars: Malformed variable content\n");
226 return -EINVAL;
227 }
228
229 return 0;
230}
231
232static inline bool is_compat(void)
233{
234 if (IS_ENABLED(CONFIG_COMPAT) && in_compat_syscall())
235 return true;
236
237 return false;
238}
239
240static void
241copy_out_compat(struct efi_variable *dst, struct compat_efi_variable *src)
242{
243 memcpy(dst->VariableName, src->VariableName, EFI_VAR_NAME_LEN);
244 memcpy(dst->Data, src->Data, sizeof(src->Data));
245
246 dst->VendorGuid = src->VendorGuid;
247 dst->DataSize = src->DataSize;
248 dst->Attributes = src->Attributes;
249}
250
251
252
253
254
255static ssize_t
256efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
257{
258 struct efi_variable *new_var, *var = &entry->var;
259 efi_char16_t *name;
260 unsigned long size;
261 efi_guid_t vendor;
262 u32 attributes;
263 u8 *data;
264 int err;
265
266 if (is_compat()) {
267 struct compat_efi_variable *compat;
268
269 if (count != sizeof(*compat))
270 return -EINVAL;
271
272 compat = (struct compat_efi_variable *)buf;
273 attributes = compat->Attributes;
274 vendor = compat->VendorGuid;
275 name = compat->VariableName;
276 size = compat->DataSize;
277 data = compat->Data;
278
279 err = sanity_check(var, name, vendor, size, attributes, data);
280 if (err)
281 return err;
282
283 copy_out_compat(&entry->var, compat);
284 } else {
285 if (count != sizeof(struct efi_variable))
286 return -EINVAL;
287
288 new_var = (struct efi_variable *)buf;
289
290 attributes = new_var->Attributes;
291 vendor = new_var->VendorGuid;
292 name = new_var->VariableName;
293 size = new_var->DataSize;
294 data = new_var->Data;
295
296 err = sanity_check(var, name, vendor, size, attributes, data);
297 if (err)
298 return err;
299
300 memcpy(&entry->var, new_var, count);
301 }
302
303 err = efivar_entry_set(entry, attributes, size, data, NULL);
304 if (err) {
305 printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err);
306 return -EIO;
307 }
308
309 return count;
310}
311
312static ssize_t
313efivar_show_raw(struct efivar_entry *entry, char *buf)
314{
315 struct efi_variable *var = &entry->var;
316 struct compat_efi_variable *compat;
317 size_t size;
318
319 if (!entry || !buf)
320 return 0;
321
322 var->DataSize = 1024;
323 if (efivar_entry_get(entry, &entry->var.Attributes,
324 &entry->var.DataSize, entry->var.Data))
325 return -EIO;
326
327 if (is_compat()) {
328 compat = (struct compat_efi_variable *)buf;
329
330 size = sizeof(*compat);
331 memcpy(compat->VariableName, var->VariableName,
332 EFI_VAR_NAME_LEN);
333 memcpy(compat->Data, var->Data, sizeof(compat->Data));
334
335 compat->VendorGuid = var->VendorGuid;
336 compat->DataSize = var->DataSize;
337 compat->Attributes = var->Attributes;
338 } else {
339 size = sizeof(*var);
340 memcpy(buf, var, size);
341 }
342
343 return size;
344}
345
346
347
348
349
350static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
351 char *buf)
352{
353 struct efivar_entry *var = to_efivar_entry(kobj);
354 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
355 ssize_t ret = -EIO;
356
357 if (!capable(CAP_SYS_ADMIN))
358 return -EACCES;
359
360 if (efivar_attr->show) {
361 ret = efivar_attr->show(var, buf);
362 }
363 return ret;
364}
365
366static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
367 const char *buf, size_t count)
368{
369 struct efivar_entry *var = to_efivar_entry(kobj);
370 struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
371 ssize_t ret = -EIO;
372
373 if (!capable(CAP_SYS_ADMIN))
374 return -EACCES;
375
376 if (efivar_attr->store)
377 ret = efivar_attr->store(var, buf, count);
378
379 return ret;
380}
381
382static const struct sysfs_ops efivar_attr_ops = {
383 .show = efivar_attr_show,
384 .store = efivar_attr_store,
385};
386
387static void efivar_release(struct kobject *kobj)
388{
389 struct efivar_entry *var = to_efivar_entry(kobj);
390 kfree(var);
391}
392
393static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
394static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
395static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
396static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
397static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
398
399static struct attribute *def_attrs[] = {
400 &efivar_attr_guid.attr,
401 &efivar_attr_size.attr,
402 &efivar_attr_attributes.attr,
403 &efivar_attr_data.attr,
404 &efivar_attr_raw_var.attr,
405 NULL,
406};
407
408static struct kobj_type efivar_ktype = {
409 .release = efivar_release,
410 .sysfs_ops = &efivar_attr_ops,
411 .default_attrs = def_attrs,
412};
413
414static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
415 struct bin_attribute *bin_attr,
416 char *buf, loff_t pos, size_t count)
417{
418 struct compat_efi_variable *compat = (struct compat_efi_variable *)buf;
419 struct efi_variable *new_var = (struct efi_variable *)buf;
420 struct efivar_entry *new_entry;
421 bool need_compat = is_compat();
422 efi_char16_t *name;
423 unsigned long size;
424 u32 attributes;
425 u8 *data;
426 int err;
427
428 if (!capable(CAP_SYS_ADMIN))
429 return -EACCES;
430
431 if (need_compat) {
432 if (count != sizeof(*compat))
433 return -EINVAL;
434
435 attributes = compat->Attributes;
436 name = compat->VariableName;
437 size = compat->DataSize;
438 data = compat->Data;
439 } else {
440 if (count != sizeof(*new_var))
441 return -EINVAL;
442
443 attributes = new_var->Attributes;
444 name = new_var->VariableName;
445 size = new_var->DataSize;
446 data = new_var->Data;
447 }
448
449 if ((attributes & ~EFI_VARIABLE_MASK) != 0 ||
450 efivar_validate(new_var->VendorGuid, name, data,
451 size) == false) {
452 printk(KERN_ERR "efivars: Malformed variable content\n");
453 return -EINVAL;
454 }
455
456 new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL);
457 if (!new_entry)
458 return -ENOMEM;
459
460 if (need_compat)
461 copy_out_compat(&new_entry->var, compat);
462 else
463 memcpy(&new_entry->var, new_var, sizeof(*new_var));
464
465 err = efivar_entry_set(new_entry, attributes, size,
466 data, &efivar_sysfs_list);
467 if (err) {
468 if (err == -EEXIST)
469 err = -EINVAL;
470 goto out;
471 }
472
473 if (efivar_create_sysfs_entry(new_entry)) {
474 printk(KERN_WARNING "efivars: failed to create sysfs entry.\n");
475 kfree(new_entry);
476 }
477 return count;
478
479out:
480 kfree(new_entry);
481 return err;
482}
483
484static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
485 struct bin_attribute *bin_attr,
486 char *buf, loff_t pos, size_t count)
487{
488 struct efi_variable *del_var = (struct efi_variable *)buf;
489 struct compat_efi_variable *compat;
490 struct efivar_entry *entry;
491 efi_char16_t *name;
492 efi_guid_t vendor;
493 int err = 0;
494
495 if (!capable(CAP_SYS_ADMIN))
496 return -EACCES;
497
498 if (is_compat()) {
499 if (count != sizeof(*compat))
500 return -EINVAL;
501
502 compat = (struct compat_efi_variable *)buf;
503 name = compat->VariableName;
504 vendor = compat->VendorGuid;
505 } else {
506 if (count != sizeof(*del_var))
507 return -EINVAL;
508
509 name = del_var->VariableName;
510 vendor = del_var->VendorGuid;
511 }
512
513 if (efivar_entry_iter_begin())
514 return -EINTR;
515 entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true);
516 if (!entry)
517 err = -EINVAL;
518 else if (__efivar_entry_delete(entry))
519 err = -EIO;
520
521 if (err) {
522 efivar_entry_iter_end();
523 return err;
524 }
525
526 if (!entry->scanning) {
527 efivar_entry_iter_end();
528 efivar_unregister(entry);
529 } else
530 efivar_entry_iter_end();
531
532
533 return count;
534}
535
536
537
538
539
540
541
542static int
543efivar_create_sysfs_entry(struct efivar_entry *new_var)
544{
545 int short_name_size;
546 char *short_name;
547 unsigned long utf8_name_size;
548 efi_char16_t *variable_name = new_var->var.VariableName;
549 int ret;
550
551
552
553
554
555 utf8_name_size = ucs2_utf8size(variable_name);
556 short_name_size = utf8_name_size + 1 + EFI_VARIABLE_GUID_LEN + 1;
557
558 short_name = kmalloc(short_name_size, GFP_KERNEL);
559 if (!short_name)
560 return -ENOMEM;
561
562 ucs2_as_utf8(short_name, variable_name, short_name_size);
563
564
565
566 short_name[utf8_name_size] = '-';
567 efi_guid_to_str(&new_var->var.VendorGuid,
568 short_name + utf8_name_size + 1);
569
570 new_var->kobj.kset = efivars_kset;
571
572 ret = kobject_init_and_add(&new_var->kobj, &efivar_ktype,
573 NULL, "%s", short_name);
574 kfree(short_name);
575 if (ret)
576 return ret;
577
578 kobject_uevent(&new_var->kobj, KOBJ_ADD);
579 if (efivar_entry_add(new_var, &efivar_sysfs_list)) {
580 efivar_unregister(new_var);
581 return -EINTR;
582 }
583
584 return 0;
585}
586
587static int
588create_efivars_bin_attributes(void)
589{
590 struct bin_attribute *attr;
591 int error;
592
593
594 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
595 if (!attr)
596 return -ENOMEM;
597
598 attr->attr.name = "new_var";
599 attr->attr.mode = 0200;
600 attr->write = efivar_create;
601 efivars_new_var = attr;
602
603
604 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
605 if (!attr) {
606 error = -ENOMEM;
607 goto out_free;
608 }
609 attr->attr.name = "del_var";
610 attr->attr.mode = 0200;
611 attr->write = efivar_delete;
612 efivars_del_var = attr;
613
614 sysfs_bin_attr_init(efivars_new_var);
615 sysfs_bin_attr_init(efivars_del_var);
616
617
618 error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_new_var);
619 if (error) {
620 printk(KERN_ERR "efivars: unable to create new_var sysfs file"
621 " due to error %d\n", error);
622 goto out_free;
623 }
624
625 error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_del_var);
626 if (error) {
627 printk(KERN_ERR "efivars: unable to create del_var sysfs file"
628 " due to error %d\n", error);
629 sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
630 goto out_free;
631 }
632
633 return 0;
634out_free:
635 kfree(efivars_del_var);
636 efivars_del_var = NULL;
637 kfree(efivars_new_var);
638 efivars_new_var = NULL;
639 return error;
640}
641
642static int efivar_update_sysfs_entry(efi_char16_t *name, efi_guid_t vendor,
643 unsigned long name_size, void *data)
644{
645 struct efivar_entry *entry = data;
646
647 if (efivar_entry_find(name, vendor, &efivar_sysfs_list, false))
648 return 0;
649
650 memcpy(entry->var.VariableName, name, name_size);
651 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
652
653 return 1;
654}
655
656static void efivar_update_sysfs_entries(struct work_struct *work)
657{
658 struct efivar_entry *entry;
659 int err;
660
661
662 while (1) {
663 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
664 if (!entry)
665 return;
666
667 err = efivar_init(efivar_update_sysfs_entry, entry,
668 false, &efivar_sysfs_list);
669 if (!err)
670 break;
671
672 efivar_create_sysfs_entry(entry);
673 }
674
675 kfree(entry);
676}
677
678static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
679 unsigned long name_size, void *data)
680{
681 struct efivar_entry *entry;
682
683 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
684 if (!entry)
685 return -ENOMEM;
686
687 memcpy(entry->var.VariableName, name, name_size);
688 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
689
690 efivar_create_sysfs_entry(entry);
691
692 return 0;
693}
694
695static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
696{
697 int err = efivar_entry_remove(entry);
698
699 if (err)
700 return err;
701 efivar_unregister(entry);
702 return 0;
703}
704
705static void efivars_sysfs_exit(void)
706{
707
708 int err;
709
710 err = __efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list,
711 NULL, NULL);
712 if (err) {
713 pr_err("efivars: Failed to destroy sysfs entries\n");
714 return;
715 }
716
717 if (efivars_new_var)
718 sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
719 if (efivars_del_var)
720 sysfs_remove_bin_file(&efivars_kset->kobj, efivars_del_var);
721 kfree(efivars_new_var);
722 kfree(efivars_del_var);
723 kset_unregister(efivars_kset);
724}
725
726int efivars_sysfs_init(void)
727{
728 struct kobject *parent_kobj = efivars_kobject();
729 int error = 0;
730
731 if (!efi_enabled(EFI_RUNTIME_SERVICES))
732 return -ENODEV;
733
734
735 if (!parent_kobj)
736 return 0;
737
738 printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
739 EFIVARS_DATE);
740
741 efivars_kset = kset_create_and_add("vars", NULL, parent_kobj);
742 if (!efivars_kset) {
743 printk(KERN_ERR "efivars: Subsystem registration failed.\n");
744 return -ENOMEM;
745 }
746
747 efivar_init(efivars_sysfs_callback, NULL, true, &efivar_sysfs_list);
748
749 error = create_efivars_bin_attributes();
750 if (error) {
751 efivars_sysfs_exit();
752 return error;
753 }
754
755 INIT_WORK(&efivar_work, efivar_update_sysfs_entries);
756
757 return 0;
758}
759EXPORT_SYMBOL_GPL(efivars_sysfs_init);
760
761module_init(efivars_sysfs_init);
762module_exit(efivars_sysfs_exit);
763