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