1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/capability.h>
23#include <linux/types.h>
24#include <linux/errno.h>
25#include <linux/init.h>
26#include <linux/mm.h>
27#include <linux/module.h>
28#include <linux/string.h>
29#include <linux/smp.h>
30#include <linux/efi.h>
31#include <linux/sysfs.h>
32#include <linux/device.h>
33#include <linux/slab.h>
34#include <linux/ctype.h>
35#include <linux/ucs2_string.h>
36
37
38static struct efivars *__efivars;
39
40static bool efivar_wq_enabled = true;
41DECLARE_WORK(efivar_work, NULL);
42EXPORT_SYMBOL_GPL(efivar_work);
43
44static bool
45validate_device_path(efi_char16_t *var_name, int match, u8 *buffer,
46 unsigned long len)
47{
48 struct efi_generic_dev_path *node;
49 int offset = 0;
50
51 node = (struct efi_generic_dev_path *)buffer;
52
53 if (len < sizeof(*node))
54 return false;
55
56 while (offset <= len - sizeof(*node) &&
57 node->length >= sizeof(*node) &&
58 node->length <= len - offset) {
59 offset += node->length;
60
61 if ((node->type == EFI_DEV_END_PATH ||
62 node->type == EFI_DEV_END_PATH2) &&
63 node->sub_type == EFI_DEV_END_ENTIRE)
64 return true;
65
66 node = (struct efi_generic_dev_path *)(buffer + offset);
67 }
68
69
70
71
72
73
74 return false;
75}
76
77static bool
78validate_boot_order(efi_char16_t *var_name, int match, u8 *buffer,
79 unsigned long len)
80{
81
82 if ((len % 2) != 0)
83 return false;
84
85 return true;
86}
87
88static bool
89validate_load_option(efi_char16_t *var_name, int match, u8 *buffer,
90 unsigned long len)
91{
92 u16 filepathlength;
93 int i, desclength = 0, namelen;
94
95 namelen = ucs2_strnlen(var_name, EFI_VAR_NAME_LEN);
96
97
98 for (i = match; i < match+4; i++) {
99 if (var_name[i] > 127 ||
100 hex_to_bin(var_name[i] & 0xff) < 0)
101 return true;
102 }
103
104
105 if (namelen > match + 4)
106 return false;
107
108
109 if (len < 8)
110 return false;
111
112 filepathlength = buffer[4] | buffer[5] << 8;
113
114
115
116
117
118 desclength = ucs2_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2;
119
120
121 if (!desclength)
122 return false;
123
124
125
126
127
128
129 if ((desclength + filepathlength + 6) > len)
130 return false;
131
132
133
134
135 return validate_device_path(var_name, match, buffer + desclength + 6,
136 filepathlength);
137}
138
139static bool
140validate_uint16(efi_char16_t *var_name, int match, u8 *buffer,
141 unsigned long len)
142{
143
144 if (len != 2)
145 return false;
146
147 return true;
148}
149
150static bool
151validate_ascii_string(efi_char16_t *var_name, int match, u8 *buffer,
152 unsigned long len)
153{
154 int i;
155
156 for (i = 0; i < len; i++) {
157 if (buffer[i] > 127)
158 return false;
159
160 if (buffer[i] == 0)
161 return true;
162 }
163
164 return false;
165}
166
167struct variable_validate {
168 char *name;
169 bool (*validate)(efi_char16_t *var_name, int match, u8 *data,
170 unsigned long len);
171};
172
173static const struct variable_validate variable_validate[] = {
174 { "BootNext", validate_uint16 },
175 { "BootOrder", validate_boot_order },
176 { "DriverOrder", validate_boot_order },
177 { "Boot*", validate_load_option },
178 { "Driver*", validate_load_option },
179 { "ConIn", validate_device_path },
180 { "ConInDev", validate_device_path },
181 { "ConOut", validate_device_path },
182 { "ConOutDev", validate_device_path },
183 { "ErrOut", validate_device_path },
184 { "ErrOutDev", validate_device_path },
185 { "Timeout", validate_uint16 },
186 { "Lang", validate_ascii_string },
187 { "PlatformLang", validate_ascii_string },
188 { "", NULL },
189};
190
191bool
192efivar_validate(efi_char16_t *var_name, u8 *data, unsigned long len)
193{
194 int i;
195 u16 *unicode_name = var_name;
196
197 for (i = 0; variable_validate[i].validate != NULL; i++) {
198 const char *name = variable_validate[i].name;
199 int match;
200
201 for (match = 0; ; match++) {
202 char c = name[match];
203 u16 u = unicode_name[match];
204
205
206 if (u > 127)
207 return true;
208
209
210 if (c == '*')
211 return variable_validate[i].validate(var_name,
212 match, data, len);
213
214
215 if (c != u)
216 break;
217
218
219 if (!c)
220 return variable_validate[i].validate(var_name,
221 match, data, len);
222 }
223 }
224
225 return true;
226}
227EXPORT_SYMBOL_GPL(efivar_validate);
228
229static efi_status_t
230check_var_size(u32 attributes, unsigned long size)
231{
232 const struct efivar_operations *fops = __efivars->ops;
233
234 if (!fops->query_variable_store)
235 return EFI_UNSUPPORTED;
236
237 return fops->query_variable_store(attributes, size);
238}
239
240static int efi_status_to_err(efi_status_t status)
241{
242 int err;
243
244 switch (status) {
245 case EFI_SUCCESS:
246 err = 0;
247 break;
248 case EFI_INVALID_PARAMETER:
249 err = -EINVAL;
250 break;
251 case EFI_OUT_OF_RESOURCES:
252 err = -ENOSPC;
253 break;
254 case EFI_DEVICE_ERROR:
255 err = -EIO;
256 break;
257 case EFI_WRITE_PROTECTED:
258 err = -EROFS;
259 break;
260 case EFI_SECURITY_VIOLATION:
261 err = -EACCES;
262 break;
263 case EFI_NOT_FOUND:
264 err = -ENOENT;
265 break;
266 default:
267 err = -EINVAL;
268 }
269
270 return err;
271}
272
273static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor,
274 struct list_head *head)
275{
276 struct efivar_entry *entry, *n;
277 unsigned long strsize1, strsize2;
278 bool found = false;
279
280 strsize1 = ucs2_strsize(variable_name, 1024);
281 list_for_each_entry_safe(entry, n, head, list) {
282 strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
283 if (strsize1 == strsize2 &&
284 !memcmp(variable_name, &(entry->var.VariableName),
285 strsize2) &&
286 !efi_guidcmp(entry->var.VendorGuid,
287 *vendor)) {
288 found = true;
289 break;
290 }
291 }
292 return found;
293}
294
295
296
297
298
299
300static unsigned long var_name_strnsize(efi_char16_t *variable_name,
301 unsigned long variable_name_size)
302{
303 unsigned long len;
304 efi_char16_t c;
305
306
307
308
309
310
311 for (len = 2; len <= variable_name_size; len += sizeof(c)) {
312 c = variable_name[(len / sizeof(c)) - 1];
313 if (!c)
314 break;
315 }
316
317 return min(len, variable_name_size);
318}
319
320
321
322
323
324static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid,
325 unsigned long len16)
326{
327 size_t i, len8 = len16 / sizeof(efi_char16_t);
328 char *str8;
329
330
331
332
333
334
335 efivar_wq_enabled = false;
336
337 str8 = kzalloc(len8, GFP_KERNEL);
338 if (!str8)
339 return;
340
341 for (i = 0; i < len8; i++)
342 str8[i] = str16[i];
343
344 printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n",
345 str8, vendor_guid);
346 kfree(str8);
347}
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
363 void *data, bool atomic, bool duplicates,
364 struct list_head *head)
365{
366 const struct efivar_operations *ops = __efivars->ops;
367 unsigned long variable_name_size = 1024;
368 efi_char16_t *variable_name;
369 efi_status_t status;
370 efi_guid_t vendor_guid;
371 int err = 0;
372
373 variable_name = kzalloc(variable_name_size, GFP_KERNEL);
374 if (!variable_name) {
375 printk(KERN_ERR "efivars: Memory allocation failed.\n");
376 return -ENOMEM;
377 }
378
379 spin_lock_irq(&__efivars->lock);
380
381
382
383
384
385
386 do {
387 variable_name_size = 1024;
388
389 status = ops->get_next_variable(&variable_name_size,
390 variable_name,
391 &vendor_guid);
392 switch (status) {
393 case EFI_SUCCESS:
394 if (!atomic)
395 spin_unlock_irq(&__efivars->lock);
396
397 variable_name_size = var_name_strnsize(variable_name,
398 variable_name_size);
399
400
401
402
403
404
405
406
407
408 if (duplicates &&
409 variable_is_present(variable_name, &vendor_guid, head)) {
410 dup_variable_bug(variable_name, &vendor_guid,
411 variable_name_size);
412 if (!atomic)
413 spin_lock_irq(&__efivars->lock);
414
415 status = EFI_NOT_FOUND;
416 break;
417 }
418
419 err = func(variable_name, vendor_guid, variable_name_size, data);
420 if (err)
421 status = EFI_NOT_FOUND;
422
423 if (!atomic)
424 spin_lock_irq(&__efivars->lock);
425
426 break;
427 case EFI_NOT_FOUND:
428 break;
429 default:
430 printk(KERN_WARNING "efivars: get_next_variable: status=%lx\n",
431 status);
432 status = EFI_NOT_FOUND;
433 break;
434 }
435
436 } while (status != EFI_NOT_FOUND);
437
438 spin_unlock_irq(&__efivars->lock);
439
440 kfree(variable_name);
441
442 return err;
443}
444EXPORT_SYMBOL_GPL(efivar_init);
445
446
447
448
449
450
451void efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
452{
453 spin_lock_irq(&__efivars->lock);
454 list_add(&entry->list, head);
455 spin_unlock_irq(&__efivars->lock);
456}
457EXPORT_SYMBOL_GPL(efivar_entry_add);
458
459
460
461
462
463void efivar_entry_remove(struct efivar_entry *entry)
464{
465 spin_lock_irq(&__efivars->lock);
466 list_del(&entry->list);
467 spin_unlock_irq(&__efivars->lock);
468}
469EXPORT_SYMBOL_GPL(efivar_entry_remove);
470
471
472
473
474
475
476
477
478
479
480
481
482static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
483{
484 lockdep_assert_held(&__efivars->lock);
485
486 list_del(&entry->list);
487 spin_unlock_irq(&__efivars->lock);
488}
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505int __efivar_entry_delete(struct efivar_entry *entry)
506{
507 const struct efivar_operations *ops = __efivars->ops;
508 efi_status_t status;
509
510 lockdep_assert_held(&__efivars->lock);
511
512 status = ops->set_variable(entry->var.VariableName,
513 &entry->var.VendorGuid,
514 0, 0, NULL);
515
516 return efi_status_to_err(status);
517}
518EXPORT_SYMBOL_GPL(__efivar_entry_delete);
519
520
521
522
523
524
525
526
527
528
529
530
531int efivar_entry_delete(struct efivar_entry *entry)
532{
533 const struct efivar_operations *ops = __efivars->ops;
534 efi_status_t status;
535
536 spin_lock_irq(&__efivars->lock);
537 status = ops->set_variable(entry->var.VariableName,
538 &entry->var.VendorGuid,
539 0, 0, NULL);
540 if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
541 spin_unlock_irq(&__efivars->lock);
542 return efi_status_to_err(status);
543 }
544
545 efivar_entry_list_del_unlock(entry);
546 return 0;
547}
548EXPORT_SYMBOL_GPL(efivar_entry_delete);
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
572 unsigned long size, void *data, struct list_head *head)
573{
574 const struct efivar_operations *ops = __efivars->ops;
575 efi_status_t status;
576 efi_char16_t *name = entry->var.VariableName;
577 efi_guid_t vendor = entry->var.VendorGuid;
578
579 spin_lock_irq(&__efivars->lock);
580
581 if (head && efivar_entry_find(name, vendor, head, false)) {
582 spin_unlock_irq(&__efivars->lock);
583 return -EEXIST;
584 }
585
586 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
587 if (status == EFI_SUCCESS || status == EFI_UNSUPPORTED)
588 status = ops->set_variable(name, &vendor,
589 attributes, size, data);
590
591 spin_unlock_irq(&__efivars->lock);
592
593 return efi_status_to_err(status);
594
595}
596EXPORT_SYMBOL_GPL(efivar_entry_set);
597
598
599
600
601
602
603
604
605
606
607static int
608efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
609 u32 attributes, unsigned long size, void *data)
610{
611 const struct efivar_operations *ops = __efivars->ops;
612 unsigned long flags;
613 efi_status_t status;
614
615 if (!spin_trylock_irqsave(&__efivars->lock, flags))
616 return -EBUSY;
617
618 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
619 if (status != EFI_SUCCESS) {
620 spin_unlock_irqrestore(&__efivars->lock, flags);
621 return -ENOSPC;
622 }
623
624 status = ops->set_variable_nonblocking(name, &vendor, attributes,
625 size, data);
626
627 spin_unlock_irqrestore(&__efivars->lock, flags);
628 return efi_status_to_err(status);
629}
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
649 bool block, unsigned long size, void *data)
650{
651 const struct efivar_operations *ops = __efivars->ops;
652 unsigned long flags;
653 efi_status_t status;
654
655 if (!ops->query_variable_store)
656 return -ENOSYS;
657
658
659
660
661
662
663
664
665
666
667
668 if (!block && ops->set_variable_nonblocking)
669 return efivar_entry_set_nonblocking(name, vendor, attributes,
670 size, data);
671
672 if (!block) {
673 if (!spin_trylock_irqsave(&__efivars->lock, flags))
674 return -EBUSY;
675 } else {
676 spin_lock_irqsave(&__efivars->lock, flags);
677 }
678
679 status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
680 if (status != EFI_SUCCESS) {
681 spin_unlock_irqrestore(&__efivars->lock, flags);
682 return -ENOSPC;
683 }
684
685 status = ops->set_variable(name, &vendor, attributes, size, data);
686
687 spin_unlock_irqrestore(&__efivars->lock, flags);
688
689 return efi_status_to_err(status);
690}
691EXPORT_SYMBOL_GPL(efivar_entry_set_safe);
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
711 struct list_head *head, bool remove)
712{
713 struct efivar_entry *entry, *n;
714 int strsize1, strsize2;
715 bool found = false;
716
717 lockdep_assert_held(&__efivars->lock);
718
719 list_for_each_entry_safe(entry, n, head, list) {
720 strsize1 = ucs2_strsize(name, 1024);
721 strsize2 = ucs2_strsize(entry->var.VariableName, 1024);
722 if (strsize1 == strsize2 &&
723 !memcmp(name, &(entry->var.VariableName), strsize1) &&
724 !efi_guidcmp(guid, entry->var.VendorGuid)) {
725 found = true;
726 break;
727 }
728 }
729
730 if (!found)
731 return NULL;
732
733 if (remove) {
734 if (entry->scanning) {
735
736
737
738
739 entry->deleting = true;
740 } else
741 list_del(&entry->list);
742 }
743
744 return entry;
745}
746EXPORT_SYMBOL_GPL(efivar_entry_find);
747
748
749
750
751
752
753int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
754{
755 const struct efivar_operations *ops = __efivars->ops;
756 efi_status_t status;
757
758 *size = 0;
759
760 spin_lock_irq(&__efivars->lock);
761 status = ops->get_variable(entry->var.VariableName,
762 &entry->var.VendorGuid, NULL, size, NULL);
763 spin_unlock_irq(&__efivars->lock);
764
765 if (status != EFI_BUFFER_TOO_SMALL)
766 return efi_status_to_err(status);
767
768 return 0;
769}
770EXPORT_SYMBOL_GPL(efivar_entry_size);
771
772
773
774
775
776
777
778
779
780
781
782
783int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
784 unsigned long *size, void *data)
785{
786 const struct efivar_operations *ops = __efivars->ops;
787 efi_status_t status;
788
789 lockdep_assert_held(&__efivars->lock);
790
791 status = ops->get_variable(entry->var.VariableName,
792 &entry->var.VendorGuid,
793 attributes, size, data);
794
795 return efi_status_to_err(status);
796}
797EXPORT_SYMBOL_GPL(__efivar_entry_get);
798
799
800
801
802
803
804
805
806int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
807 unsigned long *size, void *data)
808{
809 const struct efivar_operations *ops = __efivars->ops;
810 efi_status_t status;
811
812 spin_lock_irq(&__efivars->lock);
813 status = ops->get_variable(entry->var.VariableName,
814 &entry->var.VendorGuid,
815 attributes, size, data);
816 spin_unlock_irq(&__efivars->lock);
817
818 return efi_status_to_err(status);
819}
820EXPORT_SYMBOL_GPL(efivar_entry_get);
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
845 unsigned long *size, void *data, bool *set)
846{
847 const struct efivar_operations *ops = __efivars->ops;
848 efi_char16_t *name = entry->var.VariableName;
849 efi_guid_t *vendor = &entry->var.VendorGuid;
850 efi_status_t status;
851 int err;
852
853 *set = false;
854
855 if (efivar_validate(name, data, *size) == false)
856 return -EINVAL;
857
858
859
860
861
862
863 spin_lock_irq(&__efivars->lock);
864
865
866
867
868 status = check_var_size(attributes, *size + ucs2_strsize(name, 1024));
869 if (status != EFI_SUCCESS) {
870 if (status != EFI_UNSUPPORTED) {
871 err = efi_status_to_err(status);
872 goto out;
873 }
874
875 if (*size > 65536) {
876 err = -ENOSPC;
877 goto out;
878 }
879 }
880
881 status = ops->set_variable(name, vendor, attributes, *size, data);
882 if (status != EFI_SUCCESS) {
883 err = efi_status_to_err(status);
884 goto out;
885 }
886
887 *set = true;
888
889
890
891
892
893
894
895 *size = 0;
896 status = ops->get_variable(entry->var.VariableName,
897 &entry->var.VendorGuid,
898 NULL, size, NULL);
899
900 if (status == EFI_NOT_FOUND)
901 efivar_entry_list_del_unlock(entry);
902 else
903 spin_unlock_irq(&__efivars->lock);
904
905 if (status && status != EFI_BUFFER_TOO_SMALL)
906 return efi_status_to_err(status);
907
908 return 0;
909
910out:
911 spin_unlock_irq(&__efivars->lock);
912 return err;
913
914}
915EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
916
917
918
919
920
921
922
923
924void efivar_entry_iter_begin(void)
925{
926 spin_lock_irq(&__efivars->lock);
927}
928EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
929
930
931
932
933
934
935void efivar_entry_iter_end(void)
936{
937 spin_unlock_irq(&__efivars->lock);
938}
939EXPORT_SYMBOL_GPL(efivar_entry_iter_end);
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
964 struct list_head *head, void *data,
965 struct efivar_entry **prev)
966{
967 struct efivar_entry *entry, *n;
968 int err = 0;
969
970 if (!prev || !*prev) {
971 list_for_each_entry_safe(entry, n, head, list) {
972 err = func(entry, data);
973 if (err)
974 break;
975 }
976
977 if (prev)
978 *prev = entry;
979
980 return err;
981 }
982
983
984 list_for_each_entry_safe_continue((*prev), n, head, list) {
985 err = func(*prev, data);
986 if (err)
987 break;
988 }
989
990 return err;
991}
992EXPORT_SYMBOL_GPL(__efivar_entry_iter);
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
1009 struct list_head *head, void *data)
1010{
1011 int err = 0;
1012
1013 efivar_entry_iter_begin();
1014 err = __efivar_entry_iter(func, head, data, NULL);
1015 efivar_entry_iter_end();
1016
1017 return err;
1018}
1019EXPORT_SYMBOL_GPL(efivar_entry_iter);
1020
1021
1022
1023
1024
1025
1026
1027struct kobject *efivars_kobject(void)
1028{
1029 if (!__efivars)
1030 return NULL;
1031
1032 return __efivars->kobject;
1033}
1034EXPORT_SYMBOL_GPL(efivars_kobject);
1035
1036
1037
1038
1039void efivar_run_worker(void)
1040{
1041 if (efivar_wq_enabled)
1042 schedule_work(&efivar_work);
1043}
1044EXPORT_SYMBOL_GPL(efivar_run_worker);
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054int efivars_register(struct efivars *efivars,
1055 const struct efivar_operations *ops,
1056 struct kobject *kobject)
1057{
1058 spin_lock_init(&efivars->lock);
1059 efivars->ops = ops;
1060 efivars->kobject = kobject;
1061
1062 __efivars = efivars;
1063
1064 return 0;
1065}
1066EXPORT_SYMBOL_GPL(efivars_register);
1067
1068
1069
1070
1071
1072
1073
1074
1075int efivars_unregister(struct efivars *efivars)
1076{
1077 int rv;
1078
1079 if (!__efivars) {
1080 printk(KERN_ERR "efivars not registered\n");
1081 rv = -EINVAL;
1082 goto out;
1083 }
1084
1085 if (__efivars != efivars) {
1086 rv = -EINVAL;
1087 goto out;
1088 }
1089
1090 __efivars = NULL;
1091
1092 rv = 0;
1093out:
1094 return rv;
1095}
1096EXPORT_SYMBOL_GPL(efivars_unregister);
1097