1
2
3
4
5
6
7
8#include <common.h>
9#include <div64.h>
10#include <efi_loader.h>
11#include <environment.h>
12#include <malloc.h>
13#include <linux/libfdt_env.h>
14#include <u-boot/crc.h>
15#include <bootm.h>
16#include <watchdog.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20
21static efi_uintn_t efi_tpl = TPL_APPLICATION;
22
23
24LIST_HEAD(efi_obj_list);
25
26
27LIST_HEAD(efi_events);
28
29
30
31
32
33
34
35static bool efi_is_direct_boot = true;
36
37#ifdef CONFIG_ARM
38
39
40
41
42
43
44static volatile void *efi_gd, *app_gd;
45#endif
46
47static int entry_count;
48static int nesting_level;
49
50const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
51
52const efi_guid_t efi_guid_driver_binding_protocol =
53 EFI_DRIVER_BINDING_PROTOCOL_GUID;
54
55
56const efi_guid_t efi_guid_event_group_exit_boot_services =
57 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
58
59const efi_guid_t efi_guid_event_group_virtual_address_change =
60 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
61
62const efi_guid_t efi_guid_event_group_memory_map_change =
63 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
64
65const efi_guid_t efi_guid_event_group_ready_to_boot =
66 EFI_EVENT_GROUP_READY_TO_BOOT;
67
68const efi_guid_t efi_guid_event_group_reset_system =
69 EFI_EVENT_GROUP_RESET_SYSTEM;
70
71static efi_status_t EFIAPI efi_disconnect_controller(
72 efi_handle_t controller_handle,
73 efi_handle_t driver_image_handle,
74 efi_handle_t child_handle);
75
76
77int __efi_entry_check(void)
78{
79 int ret = entry_count++ == 0;
80#ifdef CONFIG_ARM
81 assert(efi_gd);
82 app_gd = gd;
83 gd = efi_gd;
84#endif
85 return ret;
86}
87
88
89int __efi_exit_check(void)
90{
91 int ret = --entry_count == 0;
92#ifdef CONFIG_ARM
93 gd = app_gd;
94#endif
95 return ret;
96}
97
98
99void efi_save_gd(void)
100{
101#ifdef CONFIG_ARM
102 efi_gd = gd;
103#endif
104}
105
106
107
108
109
110
111void efi_restore_gd(void)
112{
113#ifdef CONFIG_ARM
114
115 if (!efi_gd)
116 return;
117 gd = efi_gd;
118#endif
119}
120
121
122
123
124
125
126
127
128
129
130
131static const char *indent_string(int level)
132{
133 const char *indent = " ";
134 const int max = strlen(indent);
135
136 level = min(max, level * 2);
137 return &indent[max - level];
138}
139
140const char *__efi_nesting(void)
141{
142 return indent_string(nesting_level);
143}
144
145const char *__efi_nesting_inc(void)
146{
147 return indent_string(nesting_level++);
148}
149
150const char *__efi_nesting_dec(void)
151{
152 return indent_string(--nesting_level);
153}
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169static void efi_queue_event(struct efi_event *event, bool check_tpl)
170{
171 if (event->notify_function) {
172 event->is_queued = true;
173
174 if (check_tpl && efi_tpl >= event->notify_tpl)
175 return;
176 EFI_CALL_VOID(event->notify_function(event,
177 event->notify_context));
178 }
179 event->is_queued = false;
180}
181
182
183
184
185
186
187
188efi_status_t is_valid_tpl(efi_uintn_t tpl)
189{
190 switch (tpl) {
191 case TPL_APPLICATION:
192 case TPL_CALLBACK:
193 case TPL_NOTIFY:
194 case TPL_HIGH_LEVEL:
195 return EFI_SUCCESS;
196 default:
197 return EFI_INVALID_PARAMETER;
198 }
199}
200
201
202
203
204
205
206
207
208
209
210
211
212void efi_signal_event(struct efi_event *event, bool check_tpl)
213{
214 if (event->group) {
215 struct efi_event *evt;
216
217
218
219
220
221 list_for_each_entry(evt, &efi_events, link) {
222 if (!evt->group || guidcmp(evt->group, event->group))
223 continue;
224 if (evt->is_signaled)
225 continue;
226 evt->is_signaled = true;
227 if (evt->type & EVT_NOTIFY_SIGNAL &&
228 evt->notify_function)
229 evt->is_queued = true;
230 }
231 list_for_each_entry(evt, &efi_events, link) {
232 if (!evt->group || guidcmp(evt->group, event->group))
233 continue;
234 if (evt->is_queued)
235 efi_queue_event(evt, check_tpl);
236 }
237 } else if (!event->is_signaled) {
238 event->is_signaled = true;
239 if (event->type & EVT_NOTIFY_SIGNAL)
240 efi_queue_event(event, check_tpl);
241 }
242}
243
244
245
246
247
248
249
250
251
252
253
254
255static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
256{
257 efi_uintn_t old_tpl = efi_tpl;
258
259 EFI_ENTRY("0x%zx", new_tpl);
260
261 if (new_tpl < efi_tpl)
262 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
263 efi_tpl = new_tpl;
264 if (efi_tpl > TPL_HIGH_LEVEL)
265 efi_tpl = TPL_HIGH_LEVEL;
266
267 EFI_EXIT(EFI_SUCCESS);
268 return old_tpl;
269}
270
271
272
273
274
275
276
277
278
279
280static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
281{
282 EFI_ENTRY("0x%zx", old_tpl);
283
284 if (old_tpl > efi_tpl)
285 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
286 efi_tpl = old_tpl;
287 if (efi_tpl > TPL_HIGH_LEVEL)
288 efi_tpl = TPL_HIGH_LEVEL;
289
290
291
292
293 efi_timer_check();
294
295 EFI_EXIT(EFI_SUCCESS);
296}
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
313 efi_uintn_t pages,
314 uint64_t *memory)
315{
316 efi_status_t r;
317
318 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
319 r = efi_allocate_pages(type, memory_type, pages, memory);
320 return EFI_EXIT(r);
321}
322
323
324
325
326
327
328
329
330
331
332
333
334
335static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
336 efi_uintn_t pages)
337{
338 efi_status_t r;
339
340 EFI_ENTRY("%llx, 0x%zx", memory, pages);
341 r = efi_free_pages(memory, pages);
342 return EFI_EXIT(r);
343}
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361static efi_status_t EFIAPI efi_get_memory_map_ext(
362 efi_uintn_t *memory_map_size,
363 struct efi_mem_desc *memory_map,
364 efi_uintn_t *map_key,
365 efi_uintn_t *descriptor_size,
366 uint32_t *descriptor_version)
367{
368 efi_status_t r;
369
370 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
371 map_key, descriptor_size, descriptor_version);
372 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
373 descriptor_size, descriptor_version);
374 return EFI_EXIT(r);
375}
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
391 efi_uintn_t size,
392 void **buffer)
393{
394 efi_status_t r;
395
396 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
397 r = efi_allocate_pool(pool_type, size, buffer);
398 return EFI_EXIT(r);
399}
400
401
402
403
404
405
406
407
408
409
410
411
412static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
413{
414 efi_status_t r;
415
416 EFI_ENTRY("%p", buffer);
417 r = efi_free_pool(buffer);
418 return EFI_EXIT(r);
419}
420
421
422
423
424
425
426
427void efi_add_handle(efi_handle_t handle)
428{
429 if (!handle)
430 return;
431 INIT_LIST_HEAD(&handle->protocols);
432 list_add_tail(&handle->link, &efi_obj_list);
433}
434
435
436
437
438
439
440
441efi_status_t efi_create_handle(efi_handle_t *handle)
442{
443 struct efi_object *obj;
444
445 obj = calloc(1, sizeof(struct efi_object));
446 if (!obj)
447 return EFI_OUT_OF_RESOURCES;
448
449 efi_add_handle(obj);
450 *handle = obj;
451
452 return EFI_SUCCESS;
453}
454
455
456
457
458
459
460
461
462
463efi_status_t efi_search_protocol(const efi_handle_t handle,
464 const efi_guid_t *protocol_guid,
465 struct efi_handler **handler)
466{
467 struct efi_object *efiobj;
468 struct list_head *lhandle;
469
470 if (!handle || !protocol_guid)
471 return EFI_INVALID_PARAMETER;
472 efiobj = efi_search_obj(handle);
473 if (!efiobj)
474 return EFI_INVALID_PARAMETER;
475 list_for_each(lhandle, &efiobj->protocols) {
476 struct efi_handler *protocol;
477
478 protocol = list_entry(lhandle, struct efi_handler, link);
479 if (!guidcmp(protocol->guid, protocol_guid)) {
480 if (handler)
481 *handler = protocol;
482 return EFI_SUCCESS;
483 }
484 }
485 return EFI_NOT_FOUND;
486}
487
488
489
490
491
492
493
494
495
496efi_status_t efi_remove_protocol(const efi_handle_t handle,
497 const efi_guid_t *protocol,
498 void *protocol_interface)
499{
500 struct efi_handler *handler;
501 efi_status_t ret;
502
503 ret = efi_search_protocol(handle, protocol, &handler);
504 if (ret != EFI_SUCCESS)
505 return ret;
506 if (guidcmp(handler->guid, protocol))
507 return EFI_INVALID_PARAMETER;
508 if (handler->protocol_interface != protocol_interface)
509 return EFI_INVALID_PARAMETER;
510 list_del(&handler->link);
511 free(handler);
512 return EFI_SUCCESS;
513}
514
515
516
517
518
519
520
521efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
522{
523 struct efi_object *efiobj;
524 struct efi_handler *protocol;
525 struct efi_handler *pos;
526
527 efiobj = efi_search_obj(handle);
528 if (!efiobj)
529 return EFI_INVALID_PARAMETER;
530 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
531 efi_status_t ret;
532
533 ret = efi_remove_protocol(handle, protocol->guid,
534 protocol->protocol_interface);
535 if (ret != EFI_SUCCESS)
536 return ret;
537 }
538 return EFI_SUCCESS;
539}
540
541
542
543
544
545
546void efi_delete_handle(efi_handle_t handle)
547{
548 if (!handle)
549 return;
550 efi_remove_all_protocols(handle);
551 list_del(&handle->link);
552 free(handle);
553}
554
555
556
557
558
559
560
561static efi_status_t efi_is_event(const struct efi_event *event)
562{
563 const struct efi_event *evt;
564
565 if (!event)
566 return EFI_INVALID_PARAMETER;
567 list_for_each_entry(evt, &efi_events, link) {
568 if (evt == event)
569 return EFI_SUCCESS;
570 }
571 return EFI_INVALID_PARAMETER;
572}
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
591 void (EFIAPI *notify_function) (
592 struct efi_event *event,
593 void *context),
594 void *notify_context, efi_guid_t *group,
595 struct efi_event **event)
596{
597 struct efi_event *evt;
598
599 if (event == NULL)
600 return EFI_INVALID_PARAMETER;
601
602 switch (type) {
603 case 0:
604 case EVT_TIMER:
605 case EVT_NOTIFY_SIGNAL:
606 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
607 case EVT_NOTIFY_WAIT:
608 case EVT_TIMER | EVT_NOTIFY_WAIT:
609 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
610 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
611 break;
612 default:
613 return EFI_INVALID_PARAMETER;
614 }
615
616 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
617 (is_valid_tpl(notify_tpl) != EFI_SUCCESS))
618 return EFI_INVALID_PARAMETER;
619
620 evt = calloc(1, sizeof(struct efi_event));
621 if (!evt)
622 return EFI_OUT_OF_RESOURCES;
623 evt->type = type;
624 evt->notify_tpl = notify_tpl;
625 evt->notify_function = notify_function;
626 evt->notify_context = notify_context;
627 evt->group = group;
628
629 evt->trigger_next = -1ULL;
630 evt->is_queued = false;
631 evt->is_signaled = false;
632 list_add_tail(&evt->link, &efi_events);
633 *event = evt;
634 return EFI_SUCCESS;
635}
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
654 void (EFIAPI *notify_function) (
655 struct efi_event *event,
656 void *context),
657 void *notify_context,
658 efi_guid_t *event_group,
659 struct efi_event **event)
660{
661 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
662 notify_context, event_group);
663 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
664 notify_context, event_group, event));
665}
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682static efi_status_t EFIAPI efi_create_event_ext(
683 uint32_t type, efi_uintn_t notify_tpl,
684 void (EFIAPI *notify_function) (
685 struct efi_event *event,
686 void *context),
687 void *notify_context, struct efi_event **event)
688{
689 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
690 notify_context);
691 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
692 notify_context, NULL, event));
693}
694
695
696
697
698
699
700
701
702
703
704void efi_timer_check(void)
705{
706 struct efi_event *evt;
707 u64 now = timer_get_us();
708
709 list_for_each_entry(evt, &efi_events, link) {
710 if (evt->is_queued)
711 efi_queue_event(evt, true);
712 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
713 continue;
714 switch (evt->trigger_type) {
715 case EFI_TIMER_RELATIVE:
716 evt->trigger_type = EFI_TIMER_STOP;
717 break;
718 case EFI_TIMER_PERIODIC:
719 evt->trigger_next += evt->trigger_time;
720 break;
721 default:
722 continue;
723 }
724 evt->is_signaled = false;
725 efi_signal_event(evt, true);
726 }
727 WATCHDOG_RESET();
728}
729
730
731
732
733
734
735
736
737
738
739
740
741efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
742 uint64_t trigger_time)
743{
744
745 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
746 return EFI_INVALID_PARAMETER;
747
748
749
750
751
752 do_div(trigger_time, 10);
753
754 switch (type) {
755 case EFI_TIMER_STOP:
756 event->trigger_next = -1ULL;
757 break;
758 case EFI_TIMER_PERIODIC:
759 case EFI_TIMER_RELATIVE:
760 event->trigger_next = timer_get_us() + trigger_time;
761 break;
762 default:
763 return EFI_INVALID_PARAMETER;
764 }
765 event->trigger_type = type;
766 event->trigger_time = trigger_time;
767 event->is_signaled = false;
768 return EFI_SUCCESS;
769}
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
787 enum efi_timer_delay type,
788 uint64_t trigger_time)
789{
790 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
791 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
792}
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
808 struct efi_event **event,
809 efi_uintn_t *index)
810{
811 int i;
812
813 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
814
815
816 if (!num_events || !event)
817 return EFI_EXIT(EFI_INVALID_PARAMETER);
818
819 if (efi_tpl != TPL_APPLICATION)
820 return EFI_EXIT(EFI_UNSUPPORTED);
821 for (i = 0; i < num_events; ++i) {
822 if (efi_is_event(event[i]) != EFI_SUCCESS)
823 return EFI_EXIT(EFI_INVALID_PARAMETER);
824 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
825 return EFI_EXIT(EFI_INVALID_PARAMETER);
826 if (!event[i]->is_signaled)
827 efi_queue_event(event[i], true);
828 }
829
830
831 for (;;) {
832 for (i = 0; i < num_events; ++i) {
833 if (event[i]->is_signaled)
834 goto out;
835 }
836
837 efi_timer_check();
838 }
839
840out:
841
842
843
844
845 event[i]->is_signaled = false;
846 if (index)
847 *index = i;
848
849 return EFI_EXIT(EFI_SUCCESS);
850}
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
867{
868 EFI_ENTRY("%p", event);
869 if (efi_is_event(event) != EFI_SUCCESS)
870 return EFI_EXIT(EFI_INVALID_PARAMETER);
871 efi_signal_event(event, true);
872 return EFI_EXIT(EFI_SUCCESS);
873}
874
875
876
877
878
879
880
881
882
883
884
885
886static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
887{
888 EFI_ENTRY("%p", event);
889 if (efi_is_event(event) != EFI_SUCCESS)
890 return EFI_EXIT(EFI_INVALID_PARAMETER);
891 list_del(&event->link);
892 free(event);
893 return EFI_EXIT(EFI_SUCCESS);
894}
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
911{
912 EFI_ENTRY("%p", event);
913 efi_timer_check();
914 if (efi_is_event(event) != EFI_SUCCESS ||
915 event->type & EVT_NOTIFY_SIGNAL)
916 return EFI_EXIT(EFI_INVALID_PARAMETER);
917 if (!event->is_signaled)
918 efi_queue_event(event, true);
919 if (event->is_signaled) {
920 event->is_signaled = false;
921 return EFI_EXIT(EFI_SUCCESS);
922 }
923 return EFI_EXIT(EFI_NOT_READY);
924}
925
926
927
928
929
930
931
932struct efi_object *efi_search_obj(const efi_handle_t handle)
933{
934 struct efi_object *efiobj;
935
936 list_for_each_entry(efiobj, &efi_obj_list, link) {
937 if (efiobj == handle)
938 return efiobj;
939 }
940
941 return NULL;
942}
943
944
945
946
947
948
949
950
951static struct efi_open_protocol_info_entry *efi_create_open_info(
952 struct efi_handler *handler)
953{
954 struct efi_open_protocol_info_item *item;
955
956 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
957 if (!item)
958 return NULL;
959
960 list_add_tail(&item->link, &handler->open_infos);
961
962 return &item->info;
963}
964
965
966
967
968
969
970
971static efi_status_t efi_delete_open_info(
972 struct efi_open_protocol_info_item *item)
973{
974 list_del(&item->link);
975 free(item);
976 return EFI_SUCCESS;
977}
978
979
980
981
982
983
984
985
986
987efi_status_t efi_add_protocol(const efi_handle_t handle,
988 const efi_guid_t *protocol,
989 void *protocol_interface)
990{
991 struct efi_object *efiobj;
992 struct efi_handler *handler;
993 efi_status_t ret;
994
995 efiobj = efi_search_obj(handle);
996 if (!efiobj)
997 return EFI_INVALID_PARAMETER;
998 ret = efi_search_protocol(handle, protocol, NULL);
999 if (ret != EFI_NOT_FOUND)
1000 return EFI_INVALID_PARAMETER;
1001 handler = calloc(1, sizeof(struct efi_handler));
1002 if (!handler)
1003 return EFI_OUT_OF_RESOURCES;
1004 handler->guid = protocol;
1005 handler->protocol_interface = protocol_interface;
1006 INIT_LIST_HEAD(&handler->open_infos);
1007 list_add_tail(&handler->link, &efiobj->protocols);
1008 if (!guidcmp(&efi_guid_device_path, protocol))
1009 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
1010 return EFI_SUCCESS;
1011}
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028static efi_status_t EFIAPI efi_install_protocol_interface(
1029 efi_handle_t *handle, const efi_guid_t *protocol,
1030 int protocol_interface_type, void *protocol_interface)
1031{
1032 efi_status_t r;
1033
1034 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1035 protocol_interface);
1036
1037 if (!handle || !protocol ||
1038 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1039 r = EFI_INVALID_PARAMETER;
1040 goto out;
1041 }
1042
1043
1044 if (!*handle) {
1045 r = efi_create_handle(handle);
1046 if (r != EFI_SUCCESS)
1047 goto out;
1048 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1049 *handle);
1050 } else {
1051 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1052 *handle);
1053 }
1054
1055 r = efi_add_protocol(*handle, protocol, protocol_interface);
1056out:
1057 return EFI_EXIT(r);
1058}
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071static efi_status_t efi_get_drivers(efi_handle_t handle,
1072 const efi_guid_t *protocol,
1073 efi_uintn_t *number_of_drivers,
1074 efi_handle_t **driver_handle_buffer)
1075{
1076 struct efi_handler *handler;
1077 struct efi_open_protocol_info_item *item;
1078 efi_uintn_t count = 0, i;
1079 bool duplicate;
1080
1081
1082 list_for_each_entry(handler, &handle->protocols, link) {
1083 if (protocol && guidcmp(handler->guid, protocol))
1084 continue;
1085 list_for_each_entry(item, &handler->open_infos, link) {
1086 if (item->info.attributes &
1087 EFI_OPEN_PROTOCOL_BY_DRIVER)
1088 ++count;
1089 }
1090 }
1091
1092
1093
1094
1095 *number_of_drivers = 0;
1096 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1097 if (!*driver_handle_buffer)
1098 return EFI_OUT_OF_RESOURCES;
1099
1100 list_for_each_entry(handler, &handle->protocols, link) {
1101 if (protocol && guidcmp(handler->guid, protocol))
1102 continue;
1103 list_for_each_entry(item, &handler->open_infos, link) {
1104 if (item->info.attributes &
1105 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1106
1107 duplicate = false;
1108 for (i = 0; i < *number_of_drivers; ++i) {
1109 if ((*driver_handle_buffer)[i] ==
1110 item->info.agent_handle)
1111 duplicate = true;
1112 }
1113
1114 if (!duplicate) {
1115 i = (*number_of_drivers)++;
1116 (*driver_handle_buffer)[i] =
1117 item->info.agent_handle;
1118 }
1119 }
1120 }
1121 }
1122 return EFI_SUCCESS;
1123}
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138static efi_status_t efi_disconnect_all_drivers
1139 (efi_handle_t handle,
1140 const efi_guid_t *protocol,
1141 efi_handle_t child_handle)
1142{
1143 efi_uintn_t number_of_drivers;
1144 efi_handle_t *driver_handle_buffer;
1145 efi_status_t r, ret;
1146
1147 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
1148 &driver_handle_buffer);
1149 if (ret != EFI_SUCCESS)
1150 return ret;
1151
1152 ret = EFI_NOT_FOUND;
1153 while (number_of_drivers) {
1154 r = EFI_CALL(efi_disconnect_controller(
1155 handle,
1156 driver_handle_buffer[--number_of_drivers],
1157 child_handle));
1158 if (r == EFI_SUCCESS)
1159 ret = r;
1160 }
1161 free(driver_handle_buffer);
1162 return ret;
1163}
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176static efi_status_t efi_uninstall_protocol
1177 (efi_handle_t handle, const efi_guid_t *protocol,
1178 void *protocol_interface)
1179{
1180 struct efi_object *efiobj;
1181 struct efi_handler *handler;
1182 struct efi_open_protocol_info_item *item;
1183 struct efi_open_protocol_info_item *pos;
1184 efi_status_t r;
1185
1186
1187 efiobj = efi_search_obj(handle);
1188 if (!efiobj) {
1189 r = EFI_INVALID_PARAMETER;
1190 goto out;
1191 }
1192
1193 r = efi_search_protocol(handle, protocol, &handler);
1194 if (r != EFI_SUCCESS)
1195 goto out;
1196
1197 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1198 if (!list_empty(&handler->open_infos)) {
1199 r = EFI_ACCESS_DENIED;
1200 goto out;
1201 }
1202
1203 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1204 if (item->info.attributes ==
1205 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1206 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1207 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1208 list_del(&item->link);
1209 }
1210 if (!list_empty(&handler->open_infos)) {
1211 r = EFI_ACCESS_DENIED;
1212 goto out;
1213 }
1214 r = efi_remove_protocol(handle, protocol, protocol_interface);
1215out:
1216 return r;
1217}
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232static efi_status_t EFIAPI efi_uninstall_protocol_interface
1233 (efi_handle_t handle, const efi_guid_t *protocol,
1234 void *protocol_interface)
1235{
1236 efi_status_t ret;
1237
1238 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1239
1240 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1241 if (ret != EFI_SUCCESS)
1242 goto out;
1243
1244
1245 if (list_empty(&handle->protocols)) {
1246 list_del(&handle->link);
1247 free(handle);
1248 }
1249out:
1250 return EFI_EXIT(ret);
1251}
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266static efi_status_t EFIAPI efi_register_protocol_notify(
1267 const efi_guid_t *protocol,
1268 struct efi_event *event,
1269 void **registration)
1270{
1271 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1272 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1273}
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286static int efi_search(enum efi_locate_search_type search_type,
1287 const efi_guid_t *protocol, void *search_key,
1288 efi_handle_t handle)
1289{
1290 efi_status_t ret;
1291
1292 switch (search_type) {
1293 case ALL_HANDLES:
1294 return 0;
1295 case BY_REGISTER_NOTIFY:
1296
1297 return -1;
1298 case BY_PROTOCOL:
1299 ret = efi_search_protocol(handle, protocol, NULL);
1300 return (ret != EFI_SUCCESS);
1301 default:
1302
1303 return -1;
1304 }
1305}
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320static efi_status_t efi_locate_handle(
1321 enum efi_locate_search_type search_type,
1322 const efi_guid_t *protocol, void *search_key,
1323 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1324{
1325 struct efi_object *efiobj;
1326 efi_uintn_t size = 0;
1327
1328
1329 switch (search_type) {
1330 case ALL_HANDLES:
1331 break;
1332 case BY_REGISTER_NOTIFY:
1333 if (!search_key)
1334 return EFI_INVALID_PARAMETER;
1335
1336 return EFI_UNSUPPORTED;
1337 case BY_PROTOCOL:
1338 if (!protocol)
1339 return EFI_INVALID_PARAMETER;
1340 break;
1341 default:
1342 return EFI_INVALID_PARAMETER;
1343 }
1344
1345
1346
1347
1348
1349
1350 if (!buffer_size || (*buffer_size && !buffer))
1351 return EFI_INVALID_PARAMETER;
1352
1353
1354 list_for_each_entry(efiobj, &efi_obj_list, link) {
1355 if (!efi_search(search_type, protocol, search_key, efiobj))
1356 size += sizeof(void *);
1357 }
1358
1359 if (*buffer_size < size) {
1360 *buffer_size = size;
1361 return EFI_BUFFER_TOO_SMALL;
1362 }
1363
1364 *buffer_size = size;
1365 if (size == 0)
1366 return EFI_NOT_FOUND;
1367
1368
1369 list_for_each_entry(efiobj, &efi_obj_list, link) {
1370 if (!efi_search(search_type, protocol, search_key, efiobj))
1371 *buffer++ = efiobj;
1372 }
1373
1374 return EFI_SUCCESS;
1375}
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392static efi_status_t EFIAPI efi_locate_handle_ext(
1393 enum efi_locate_search_type search_type,
1394 const efi_guid_t *protocol, void *search_key,
1395 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1396{
1397 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1398 buffer_size, buffer);
1399
1400 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1401 buffer_size, buffer));
1402}
1403
1404
1405
1406
1407
1408
1409
1410static void efi_remove_configuration_table(int i)
1411{
1412 struct efi_configuration_table *this = &systab.tables[i];
1413 struct efi_configuration_table *next = &systab.tables[i + 1];
1414 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1415
1416 memmove(this, next, (ulong)end - (ulong)next);
1417 systab.nr_tables--;
1418}
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1432 void *table)
1433{
1434 struct efi_event *evt;
1435 int i;
1436
1437 if (!guid)
1438 return EFI_INVALID_PARAMETER;
1439
1440
1441 for (i = 0; i < systab.nr_tables; i++) {
1442 if (!guidcmp(guid, &systab.tables[i].guid)) {
1443 if (table)
1444 systab.tables[i].table = table;
1445 else
1446 efi_remove_configuration_table(i);
1447 goto out;
1448 }
1449 }
1450
1451 if (!table)
1452 return EFI_NOT_FOUND;
1453
1454
1455 if (i >= EFI_MAX_CONFIGURATION_TABLES)
1456 return EFI_OUT_OF_RESOURCES;
1457
1458
1459 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1460 systab.tables[i].table = table;
1461 systab.nr_tables = i + 1;
1462
1463out:
1464
1465 efi_update_table_header_crc32(&systab.hdr);
1466
1467
1468 list_for_each_entry(evt, &efi_events, link) {
1469 if (evt->group && !guidcmp(evt->group, guid)) {
1470 efi_signal_event(evt, false);
1471 break;
1472 }
1473 }
1474
1475 return EFI_SUCCESS;
1476}
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1492 void *table)
1493{
1494 EFI_ENTRY("%pUl, %p", guid, table);
1495 return EFI_EXIT(efi_install_configuration_table(guid, table));
1496}
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1511 struct efi_device_path *file_path,
1512 struct efi_loaded_image_obj **handle_ptr,
1513 struct efi_loaded_image **info_ptr)
1514{
1515 efi_status_t ret;
1516 struct efi_loaded_image *info;
1517 struct efi_loaded_image_obj *obj;
1518
1519 info = calloc(1, sizeof(*info));
1520 if (!info)
1521 return EFI_OUT_OF_RESOURCES;
1522 obj = calloc(1, sizeof(*obj));
1523 if (!obj) {
1524 free(info);
1525 return EFI_OUT_OF_RESOURCES;
1526 }
1527
1528
1529 efi_add_handle(&obj->header);
1530
1531 if (info_ptr)
1532 *info_ptr = info;
1533 if (handle_ptr)
1534 *handle_ptr = obj;
1535
1536 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1537 info->file_path = file_path;
1538 info->system_table = &systab;
1539
1540 if (device_path) {
1541 info->device_handle = efi_dp_find_obj(device_path, NULL);
1542
1543
1544
1545
1546 ret = efi_add_protocol(&obj->header,
1547 &efi_guid_device_path, device_path);
1548 if (ret != EFI_SUCCESS)
1549 goto failure;
1550 }
1551
1552
1553
1554
1555
1556 ret = efi_add_protocol(&obj->header,
1557 &efi_guid_loaded_image, info);
1558 if (ret != EFI_SUCCESS)
1559 goto failure;
1560
1561 return ret;
1562failure:
1563 printf("ERROR: Failure to install protocols for loaded image\n");
1564 return ret;
1565}
1566
1567
1568
1569
1570
1571
1572
1573
1574efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1575 void **buffer)
1576{
1577 struct efi_file_info *info = NULL;
1578 struct efi_file_handle *f;
1579 static efi_status_t ret;
1580 efi_uintn_t bs;
1581
1582 f = efi_file_from_path(file_path);
1583 if (!f)
1584 return EFI_DEVICE_ERROR;
1585
1586 bs = 0;
1587 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1588 &bs, info));
1589 if (ret == EFI_BUFFER_TOO_SMALL) {
1590 info = malloc(bs);
1591 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1592 &bs, info));
1593 }
1594 if (ret != EFI_SUCCESS)
1595 goto error;
1596
1597 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1598 if (ret)
1599 goto error;
1600
1601 bs = info->file_size;
1602 EFI_CALL(ret = f->read(f, &bs, *buffer));
1603
1604error:
1605 free(info);
1606 EFI_CALL(f->close(f));
1607
1608 if (ret != EFI_SUCCESS) {
1609 efi_free_pool(*buffer);
1610 *buffer = NULL;
1611 }
1612
1613 return ret;
1614}
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1633 efi_handle_t parent_image,
1634 struct efi_device_path *file_path,
1635 void *source_buffer,
1636 efi_uintn_t source_size,
1637 efi_handle_t *image_handle)
1638{
1639 struct efi_loaded_image *info = NULL;
1640 struct efi_loaded_image_obj **image_obj =
1641 (struct efi_loaded_image_obj **)image_handle;
1642 efi_status_t ret;
1643
1644 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1645 file_path, source_buffer, source_size, image_handle);
1646
1647 if (!image_handle || !parent_image) {
1648 ret = EFI_INVALID_PARAMETER;
1649 goto error;
1650 }
1651
1652 if (!source_buffer && !file_path) {
1653 ret = EFI_NOT_FOUND;
1654 goto error;
1655 }
1656
1657 if (!source_buffer) {
1658 struct efi_device_path *dp, *fp;
1659
1660 ret = efi_load_image_from_path(file_path, &source_buffer);
1661 if (ret != EFI_SUCCESS)
1662 goto failure;
1663
1664
1665
1666
1667 efi_dp_split_file_path(file_path, &dp, &fp);
1668 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
1669 if (ret != EFI_SUCCESS)
1670 goto failure;
1671 } else {
1672
1673
1674
1675 ret = efi_setup_loaded_image(file_path, NULL, image_obj, &info);
1676 if (ret != EFI_SUCCESS)
1677 goto error;
1678 }
1679 (*image_obj)->entry = efi_load_pe(*image_obj, source_buffer, info);
1680 if (!(*image_obj)->entry) {
1681 ret = EFI_UNSUPPORTED;
1682 goto failure;
1683 }
1684 info->system_table = &systab;
1685 info->parent_handle = parent_image;
1686 return EFI_EXIT(EFI_SUCCESS);
1687failure:
1688 efi_delete_handle(*image_handle);
1689 *image_handle = NULL;
1690 free(info);
1691error:
1692 return EFI_EXIT(ret);
1693}
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1709 unsigned long *exit_data_size,
1710 s16 **exit_data)
1711{
1712 struct efi_loaded_image_obj *image_obj =
1713 (struct efi_loaded_image_obj *)image_handle;
1714 efi_status_t ret;
1715
1716 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1717
1718 efi_is_direct_boot = false;
1719
1720
1721 if (setjmp(&image_obj->exit_jmp)) {
1722
1723
1724
1725
1726
1727
1728
1729#ifdef CONFIG_ARM
1730
1731
1732
1733
1734
1735 gd = app_gd;
1736#endif
1737
1738
1739
1740
1741 assert(__efi_entry_check());
1742 debug("%sEFI: %lu returned by started image\n",
1743 __efi_nesting_dec(),
1744 (unsigned long)((uintptr_t)image_obj->exit_status &
1745 ~EFI_ERROR_MASK));
1746 return EFI_EXIT(image_obj->exit_status);
1747 }
1748
1749 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
1750
1751
1752
1753
1754
1755
1756
1757 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
1758}
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1775 efi_status_t exit_status,
1776 unsigned long exit_data_size,
1777 int16_t *exit_data)
1778{
1779
1780
1781
1782
1783 struct efi_loaded_image_obj *image_obj =
1784 (struct efi_loaded_image_obj *)image_handle;
1785
1786 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1787 exit_data_size, exit_data);
1788
1789
1790 EFI_EXIT(exit_status);
1791
1792
1793
1794
1795
1796 efi_restore_gd();
1797
1798 image_obj->exit_status = exit_status;
1799 longjmp(&image_obj->exit_jmp, 1);
1800
1801 panic("EFI application exited");
1802}
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1816{
1817 struct efi_object *efiobj;
1818
1819 EFI_ENTRY("%p", image_handle);
1820 efiobj = efi_search_obj(image_handle);
1821 if (efiobj)
1822 list_del(&efiobj->link);
1823
1824 return EFI_EXIT(EFI_SUCCESS);
1825}
1826
1827
1828
1829
1830static void efi_exit_caches(void)
1831{
1832#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1833
1834
1835
1836
1837 if (efi_is_direct_boot)
1838 cleanup_before_linux();
1839#endif
1840}
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1859 unsigned long map_key)
1860{
1861 struct efi_event *evt;
1862
1863 EFI_ENTRY("%p, %ld", image_handle, map_key);
1864
1865
1866 if (map_key != efi_memory_map_key)
1867 return EFI_INVALID_PARAMETER;
1868
1869
1870 efi_tpl = TPL_HIGH_LEVEL;
1871
1872
1873 if (!systab.boottime)
1874 return EFI_EXIT(EFI_SUCCESS);
1875
1876
1877 list_for_each_entry(evt, &efi_events, link) {
1878 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1879 evt->group = &efi_guid_event_group_exit_boot_services;
1880 }
1881
1882 list_for_each_entry(evt, &efi_events, link) {
1883 if (evt->group &&
1884 !guidcmp(evt->group,
1885 &efi_guid_event_group_exit_boot_services)) {
1886 efi_signal_event(evt, false);
1887 break;
1888 }
1889 }
1890
1891
1892
1893 board_quiesce_devices();
1894
1895
1896 efi_exit_caches();
1897
1898
1899 bootm_disable_interrupts();
1900
1901
1902 systab.con_in_handle = NULL;
1903 systab.con_in = NULL;
1904 systab.con_out_handle = NULL;
1905 systab.con_out = NULL;
1906 systab.stderr_handle = NULL;
1907 systab.std_err = NULL;
1908 systab.boottime = NULL;
1909
1910
1911 efi_update_table_header_crc32(&systab.hdr);
1912
1913
1914 efi_set_watchdog(0);
1915 WATCHDOG_RESET();
1916
1917 return EFI_EXIT(EFI_SUCCESS);
1918}
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1932{
1933 static uint64_t mono;
1934
1935 EFI_ENTRY("%p", count);
1936 *count = mono++;
1937 return EFI_EXIT(EFI_SUCCESS);
1938}
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1952{
1953 EFI_ENTRY("%ld", microseconds);
1954 udelay(microseconds);
1955 return EFI_EXIT(EFI_SUCCESS);
1956}
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1973 uint64_t watchdog_code,
1974 unsigned long data_size,
1975 uint16_t *watchdog_data)
1976{
1977 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
1978 data_size, watchdog_data);
1979 return EFI_EXIT(efi_set_watchdog(timeout));
1980}
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1997 const efi_guid_t *protocol,
1998 efi_handle_t agent_handle,
1999 efi_handle_t controller_handle)
2000{
2001 struct efi_handler *handler;
2002 struct efi_open_protocol_info_item *item;
2003 struct efi_open_protocol_info_item *pos;
2004 efi_status_t r;
2005
2006 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
2007 controller_handle);
2008
2009 if (!agent_handle) {
2010 r = EFI_INVALID_PARAMETER;
2011 goto out;
2012 }
2013 r = efi_search_protocol(handle, protocol, &handler);
2014 if (r != EFI_SUCCESS)
2015 goto out;
2016
2017 r = EFI_NOT_FOUND;
2018 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2019 if (item->info.agent_handle == agent_handle &&
2020 item->info.controller_handle == controller_handle) {
2021 efi_delete_open_info(item);
2022 r = EFI_SUCCESS;
2023 break;
2024 }
2025 }
2026out:
2027 return EFI_EXIT(r);
2028}
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045static efi_status_t EFIAPI efi_open_protocol_information(
2046 efi_handle_t handle, const efi_guid_t *protocol,
2047 struct efi_open_protocol_info_entry **entry_buffer,
2048 efi_uintn_t *entry_count)
2049{
2050 unsigned long buffer_size;
2051 unsigned long count;
2052 struct efi_handler *handler;
2053 struct efi_open_protocol_info_item *item;
2054 efi_status_t r;
2055
2056 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2057 entry_count);
2058
2059
2060 if (!entry_buffer) {
2061 r = EFI_INVALID_PARAMETER;
2062 goto out;
2063 }
2064 r = efi_search_protocol(handle, protocol, &handler);
2065 if (r != EFI_SUCCESS)
2066 goto out;
2067
2068
2069 count = 0;
2070 list_for_each_entry(item, &handler->open_infos, link) {
2071 if (item->info.open_count)
2072 ++count;
2073 }
2074 *entry_count = count;
2075 *entry_buffer = NULL;
2076 if (!count) {
2077 r = EFI_SUCCESS;
2078 goto out;
2079 }
2080
2081
2082 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2083 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2084 (void **)entry_buffer);
2085 if (r != EFI_SUCCESS)
2086 goto out;
2087 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2088 if (item->info.open_count)
2089 (*entry_buffer)[--count] = item->info;
2090 }
2091out:
2092 return EFI_EXIT(r);
2093}
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108static efi_status_t EFIAPI efi_protocols_per_handle(
2109 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2110 efi_uintn_t *protocol_buffer_count)
2111{
2112 unsigned long buffer_size;
2113 struct efi_object *efiobj;
2114 struct list_head *protocol_handle;
2115 efi_status_t r;
2116
2117 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2118 protocol_buffer_count);
2119
2120 if (!handle || !protocol_buffer || !protocol_buffer_count)
2121 return EFI_EXIT(EFI_INVALID_PARAMETER);
2122
2123 *protocol_buffer = NULL;
2124 *protocol_buffer_count = 0;
2125
2126 efiobj = efi_search_obj(handle);
2127 if (!efiobj)
2128 return EFI_EXIT(EFI_INVALID_PARAMETER);
2129
2130
2131 list_for_each(protocol_handle, &efiobj->protocols) {
2132 ++*protocol_buffer_count;
2133 }
2134
2135
2136 if (*protocol_buffer_count) {
2137 size_t j = 0;
2138
2139 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2140 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2141 (void **)protocol_buffer);
2142 if (r != EFI_SUCCESS)
2143 return EFI_EXIT(r);
2144 list_for_each(protocol_handle, &efiobj->protocols) {
2145 struct efi_handler *protocol;
2146
2147 protocol = list_entry(protocol_handle,
2148 struct efi_handler, link);
2149 (*protocol_buffer)[j] = (void *)protocol->guid;
2150 ++j;
2151 }
2152 }
2153
2154 return EFI_EXIT(EFI_SUCCESS);
2155}
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172static efi_status_t EFIAPI efi_locate_handle_buffer(
2173 enum efi_locate_search_type search_type,
2174 const efi_guid_t *protocol, void *search_key,
2175 efi_uintn_t *no_handles, efi_handle_t **buffer)
2176{
2177 efi_status_t r;
2178 efi_uintn_t buffer_size = 0;
2179
2180 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2181 no_handles, buffer);
2182
2183 if (!no_handles || !buffer) {
2184 r = EFI_INVALID_PARAMETER;
2185 goto out;
2186 }
2187 *no_handles = 0;
2188 *buffer = NULL;
2189 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2190 *buffer);
2191 if (r != EFI_BUFFER_TOO_SMALL)
2192 goto out;
2193 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2194 (void **)buffer);
2195 if (r != EFI_SUCCESS)
2196 goto out;
2197 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2198 *buffer);
2199 if (r == EFI_SUCCESS)
2200 *no_handles = buffer_size / sizeof(efi_handle_t);
2201out:
2202 return EFI_EXIT(r);
2203}
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2219 void *registration,
2220 void **protocol_interface)
2221{
2222 struct list_head *lhandle;
2223 efi_status_t ret;
2224
2225 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2226
2227 if (!protocol || !protocol_interface)
2228 return EFI_EXIT(EFI_INVALID_PARAMETER);
2229
2230 list_for_each(lhandle, &efi_obj_list) {
2231 struct efi_object *efiobj;
2232 struct efi_handler *handler;
2233
2234 efiobj = list_entry(lhandle, struct efi_object, link);
2235
2236 ret = efi_search_protocol(efiobj, protocol, &handler);
2237 if (ret == EFI_SUCCESS) {
2238 *protocol_interface = handler->protocol_interface;
2239 return EFI_EXIT(EFI_SUCCESS);
2240 }
2241 }
2242 *protocol_interface = NULL;
2243
2244 return EFI_EXIT(EFI_NOT_FOUND);
2245}
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261static efi_status_t EFIAPI efi_locate_device_path(
2262 const efi_guid_t *protocol,
2263 struct efi_device_path **device_path,
2264 efi_handle_t *device)
2265{
2266 struct efi_device_path *dp;
2267 size_t i;
2268 struct efi_handler *handler;
2269 efi_handle_t *handles;
2270 size_t len, len_dp;
2271 size_t len_best = 0;
2272 efi_uintn_t no_handles;
2273 u8 *remainder;
2274 efi_status_t ret;
2275
2276 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2277
2278 if (!protocol || !device_path || !*device_path || !device) {
2279 ret = EFI_INVALID_PARAMETER;
2280 goto out;
2281 }
2282
2283
2284 len = efi_dp_instance_size(*device_path);
2285
2286
2287 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2288 &no_handles, &handles));
2289 if (ret != EFI_SUCCESS)
2290 goto out;
2291
2292 for (i = 0; i < no_handles; ++i) {
2293
2294 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2295 &handler);
2296 if (ret != EFI_SUCCESS)
2297 continue;
2298 dp = (struct efi_device_path *)handler->protocol_interface;
2299 len_dp = efi_dp_instance_size(dp);
2300
2301
2302
2303
2304
2305
2306 if (len_dp <= len_best || len_dp > len)
2307 continue;
2308
2309 if (memcmp(*device_path, dp, len_dp))
2310 continue;
2311 *device = handles[i];
2312 len_best = len_dp;
2313 }
2314 if (len_best) {
2315 remainder = (u8 *)*device_path + len_best;
2316 *device_path = (struct efi_device_path *)remainder;
2317 ret = EFI_SUCCESS;
2318 } else {
2319 ret = EFI_NOT_FOUND;
2320 }
2321out:
2322 return EFI_EXIT(ret);
2323}
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2340 (efi_handle_t *handle, ...)
2341{
2342 EFI_ENTRY("%p", handle);
2343
2344 efi_va_list argptr;
2345 const efi_guid_t *protocol;
2346 void *protocol_interface;
2347 efi_status_t r = EFI_SUCCESS;
2348 int i = 0;
2349
2350 if (!handle)
2351 return EFI_EXIT(EFI_INVALID_PARAMETER);
2352
2353 efi_va_start(argptr, handle);
2354 for (;;) {
2355 protocol = efi_va_arg(argptr, efi_guid_t*);
2356 if (!protocol)
2357 break;
2358 protocol_interface = efi_va_arg(argptr, void*);
2359 r = EFI_CALL(efi_install_protocol_interface(
2360 handle, protocol,
2361 EFI_NATIVE_INTERFACE,
2362 protocol_interface));
2363 if (r != EFI_SUCCESS)
2364 break;
2365 i++;
2366 }
2367 efi_va_end(argptr);
2368 if (r == EFI_SUCCESS)
2369 return EFI_EXIT(r);
2370
2371
2372 efi_va_start(argptr, handle);
2373 for (; i; --i) {
2374 protocol = efi_va_arg(argptr, efi_guid_t*);
2375 protocol_interface = efi_va_arg(argptr, void*);
2376 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2377 protocol_interface));
2378 }
2379 efi_va_end(argptr);
2380
2381 return EFI_EXIT(r);
2382}
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2399 efi_handle_t handle, ...)
2400{
2401 EFI_ENTRY("%p", handle);
2402
2403 efi_va_list argptr;
2404 const efi_guid_t *protocol;
2405 void *protocol_interface;
2406 efi_status_t r = EFI_SUCCESS;
2407 size_t i = 0;
2408
2409 if (!handle)
2410 return EFI_EXIT(EFI_INVALID_PARAMETER);
2411
2412 efi_va_start(argptr, handle);
2413 for (;;) {
2414 protocol = efi_va_arg(argptr, efi_guid_t*);
2415 if (!protocol)
2416 break;
2417 protocol_interface = efi_va_arg(argptr, void*);
2418 r = efi_uninstall_protocol(handle, protocol,
2419 protocol_interface);
2420 if (r != EFI_SUCCESS)
2421 break;
2422 i++;
2423 }
2424 efi_va_end(argptr);
2425 if (r == EFI_SUCCESS) {
2426
2427 if (list_empty(&handle->protocols)) {
2428 list_del(&handle->link);
2429 free(handle);
2430 }
2431 return EFI_EXIT(r);
2432 }
2433
2434
2435 efi_va_start(argptr, handle);
2436 for (; i; --i) {
2437 protocol = efi_va_arg(argptr, efi_guid_t*);
2438 protocol_interface = efi_va_arg(argptr, void*);
2439 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2440 EFI_NATIVE_INTERFACE,
2441 protocol_interface));
2442 }
2443 efi_va_end(argptr);
2444
2445
2446 return EFI_EXIT(EFI_INVALID_PARAMETER);
2447}
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2463 efi_uintn_t data_size,
2464 u32 *crc32_p)
2465{
2466 EFI_ENTRY("%p, %zu", data, data_size);
2467 *crc32_p = crc32(0, data, data_size);
2468 return EFI_EXIT(EFI_SUCCESS);
2469}
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482static void EFIAPI efi_copy_mem(void *destination, const void *source,
2483 size_t length)
2484{
2485 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2486 memcpy(destination, source, length);
2487 EFI_EXIT(EFI_SUCCESS);
2488}
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2502{
2503 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2504 memset(buffer, value, size);
2505 EFI_EXIT(EFI_SUCCESS);
2506}
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518static efi_status_t efi_protocol_open(
2519 struct efi_handler *handler,
2520 void **protocol_interface, void *agent_handle,
2521 void *controller_handle, uint32_t attributes)
2522{
2523 struct efi_open_protocol_info_item *item;
2524 struct efi_open_protocol_info_entry *match = NULL;
2525 bool opened_by_driver = false;
2526 bool opened_exclusive = false;
2527
2528
2529 if (!agent_handle)
2530 goto out;
2531
2532
2533 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2534 *protocol_interface = NULL;
2535
2536
2537
2538
2539
2540 list_for_each_entry(item, &handler->open_infos, link) {
2541 if (item->info.agent_handle == agent_handle) {
2542 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2543 (item->info.attributes == attributes))
2544 return EFI_ALREADY_STARTED;
2545 }
2546 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2547 opened_exclusive = true;
2548 }
2549
2550
2551 if (opened_exclusive && attributes &
2552 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2553 return EFI_ACCESS_DENIED;
2554
2555
2556 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2557
2558 list_for_each_entry(item, &handler->open_infos, link) {
2559 if (item->info.attributes ==
2560 EFI_OPEN_PROTOCOL_BY_DRIVER)
2561 EFI_CALL(efi_disconnect_controller(
2562 item->info.controller_handle,
2563 item->info.agent_handle,
2564 NULL));
2565 }
2566 opened_by_driver = false;
2567
2568 list_for_each_entry(item, &handler->open_infos, link) {
2569 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2570 opened_by_driver = true;
2571 }
2572
2573 if (opened_by_driver)
2574 return EFI_ACCESS_DENIED;
2575 }
2576
2577
2578 list_for_each_entry(item, &handler->open_infos, link) {
2579 if (item->info.agent_handle == agent_handle &&
2580 item->info.controller_handle == controller_handle)
2581 match = &item->info;
2582 }
2583
2584 if (!match) {
2585 match = efi_create_open_info(handler);
2586 if (!match)
2587 return EFI_OUT_OF_RESOURCES;
2588 }
2589
2590 match->agent_handle = agent_handle;
2591 match->controller_handle = controller_handle;
2592 match->attributes = attributes;
2593 match->open_count++;
2594
2595out:
2596
2597 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2598 *protocol_interface = handler->protocol_interface;
2599
2600 return EFI_SUCCESS;
2601}
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619static efi_status_t EFIAPI efi_open_protocol
2620 (efi_handle_t handle, const efi_guid_t *protocol,
2621 void **protocol_interface, efi_handle_t agent_handle,
2622 efi_handle_t controller_handle, uint32_t attributes)
2623{
2624 struct efi_handler *handler;
2625 efi_status_t r = EFI_INVALID_PARAMETER;
2626
2627 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2628 protocol_interface, agent_handle, controller_handle,
2629 attributes);
2630
2631 if (!handle || !protocol ||
2632 (!protocol_interface && attributes !=
2633 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2634 goto out;
2635 }
2636
2637 switch (attributes) {
2638 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2639 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2640 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2641 break;
2642 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2643 if (controller_handle == handle)
2644 goto out;
2645
2646 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2647 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2648
2649 if (!efi_search_obj(controller_handle))
2650 goto out;
2651
2652 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2653
2654 if (!efi_search_obj(agent_handle))
2655 goto out;
2656 break;
2657 default:
2658 goto out;
2659 }
2660
2661 r = efi_search_protocol(handle, protocol, &handler);
2662 if (r != EFI_SUCCESS)
2663 goto out;
2664
2665 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2666 controller_handle, attributes);
2667out:
2668 return EFI_EXIT(r);
2669}
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2685 const efi_guid_t *protocol,
2686 void **protocol_interface)
2687{
2688 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2689 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2690}
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700static efi_status_t efi_bind_controller(
2701 efi_handle_t controller_handle,
2702 efi_handle_t driver_image_handle,
2703 struct efi_device_path *remain_device_path)
2704{
2705 struct efi_driver_binding_protocol *binding_protocol;
2706 efi_status_t r;
2707
2708 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2709 &efi_guid_driver_binding_protocol,
2710 (void **)&binding_protocol,
2711 driver_image_handle, NULL,
2712 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2713 if (r != EFI_SUCCESS)
2714 return r;
2715 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2716 controller_handle,
2717 remain_device_path));
2718 if (r == EFI_SUCCESS)
2719 r = EFI_CALL(binding_protocol->start(binding_protocol,
2720 controller_handle,
2721 remain_device_path));
2722 EFI_CALL(efi_close_protocol(driver_image_handle,
2723 &efi_guid_driver_binding_protocol,
2724 driver_image_handle, NULL));
2725 return r;
2726}
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736static efi_status_t efi_connect_single_controller(
2737 efi_handle_t controller_handle,
2738 efi_handle_t *driver_image_handle,
2739 struct efi_device_path *remain_device_path)
2740{
2741 efi_handle_t *buffer;
2742 size_t count;
2743 size_t i;
2744 efi_status_t r;
2745 size_t connected = 0;
2746
2747
2748 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2749 &efi_guid_driver_binding_protocol,
2750 NULL, &count, &buffer));
2751 if (r != EFI_SUCCESS)
2752 return r;
2753
2754
2755 if (driver_image_handle) {
2756 for (; *driver_image_handle; ++driver_image_handle) {
2757 for (i = 0; i < count; ++i) {
2758 if (buffer[i] == *driver_image_handle) {
2759 buffer[i] = NULL;
2760 r = efi_bind_controller(
2761 controller_handle,
2762 *driver_image_handle,
2763 remain_device_path);
2764
2765
2766
2767
2768
2769 if (r == EFI_SUCCESS)
2770 ++connected;
2771 }
2772 }
2773 }
2774 }
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784 for (i = 0; i < count; ++i) {
2785 if (buffer[i]) {
2786 r = efi_bind_controller(controller_handle,
2787 buffer[i],
2788 remain_device_path);
2789 if (r == EFI_SUCCESS)
2790 ++connected;
2791 }
2792 }
2793
2794 efi_free_pool(buffer);
2795 if (!connected)
2796 return EFI_NOT_FOUND;
2797 return EFI_SUCCESS;
2798}
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818static efi_status_t EFIAPI efi_connect_controller(
2819 efi_handle_t controller_handle,
2820 efi_handle_t *driver_image_handle,
2821 struct efi_device_path *remain_device_path,
2822 bool recursive)
2823{
2824 efi_status_t r;
2825 efi_status_t ret = EFI_NOT_FOUND;
2826 struct efi_object *efiobj;
2827
2828 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2829 remain_device_path, recursive);
2830
2831 efiobj = efi_search_obj(controller_handle);
2832 if (!efiobj) {
2833 ret = EFI_INVALID_PARAMETER;
2834 goto out;
2835 }
2836
2837 r = efi_connect_single_controller(controller_handle,
2838 driver_image_handle,
2839 remain_device_path);
2840 if (r == EFI_SUCCESS)
2841 ret = EFI_SUCCESS;
2842 if (recursive) {
2843 struct efi_handler *handler;
2844 struct efi_open_protocol_info_item *item;
2845
2846 list_for_each_entry(handler, &efiobj->protocols, link) {
2847 list_for_each_entry(item, &handler->open_infos, link) {
2848 if (item->info.attributes &
2849 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2850 r = EFI_CALL(efi_connect_controller(
2851 item->info.controller_handle,
2852 driver_image_handle,
2853 remain_device_path,
2854 recursive));
2855 if (r == EFI_SUCCESS)
2856 ret = EFI_SUCCESS;
2857 }
2858 }
2859 }
2860 }
2861
2862 if (ret != EFI_SUCCESS && remain_device_path &&
2863 remain_device_path->type == DEVICE_PATH_TYPE_END)
2864 ret = EFI_SUCCESS;
2865out:
2866 return EFI_EXIT(ret);
2867}
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2887 efi_handle_t handle, const efi_guid_t *protocol,
2888 void *old_interface, void *new_interface)
2889{
2890 efi_status_t ret;
2891
2892 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2893 new_interface);
2894
2895
2896 ret = efi_uninstall_protocol(handle, protocol, old_interface);
2897 if (ret != EFI_SUCCESS)
2898 goto out;
2899
2900
2901 ret = efi_add_protocol(handle, protocol, new_interface);
2902
2903
2904
2905
2906
2907 if (ret != EFI_SUCCESS)
2908 goto out;
2909
2910
2911
2912
2913 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2914out:
2915 return EFI_EXIT(ret);
2916}
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929static efi_status_t efi_get_child_controllers(
2930 struct efi_object *efiobj,
2931 efi_handle_t driver_handle,
2932 efi_uintn_t *number_of_children,
2933 efi_handle_t **child_handle_buffer)
2934{
2935 struct efi_handler *handler;
2936 struct efi_open_protocol_info_item *item;
2937 efi_uintn_t count = 0, i;
2938 bool duplicate;
2939
2940
2941 list_for_each_entry(handler, &efiobj->protocols, link) {
2942 list_for_each_entry(item, &handler->open_infos, link) {
2943 if (item->info.agent_handle == driver_handle &&
2944 item->info.attributes &
2945 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2946 ++count;
2947 }
2948 }
2949
2950
2951
2952
2953 *number_of_children = 0;
2954 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2955 if (!*child_handle_buffer)
2956 return EFI_OUT_OF_RESOURCES;
2957
2958 list_for_each_entry(handler, &efiobj->protocols, link) {
2959 list_for_each_entry(item, &handler->open_infos, link) {
2960 if (item->info.agent_handle == driver_handle &&
2961 item->info.attributes &
2962 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2963
2964 duplicate = false;
2965 for (i = 0; i < *number_of_children; ++i) {
2966 if ((*child_handle_buffer)[i] ==
2967 item->info.controller_handle)
2968 duplicate = true;
2969 }
2970
2971 if (!duplicate) {
2972 i = (*number_of_children)++;
2973 (*child_handle_buffer)[i] =
2974 item->info.controller_handle;
2975 }
2976 }
2977 }
2978 }
2979 return EFI_SUCCESS;
2980}
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995static efi_status_t EFIAPI efi_disconnect_controller(
2996 efi_handle_t controller_handle,
2997 efi_handle_t driver_image_handle,
2998 efi_handle_t child_handle)
2999{
3000 struct efi_driver_binding_protocol *binding_protocol;
3001 efi_handle_t *child_handle_buffer = NULL;
3002 size_t number_of_children = 0;
3003 efi_status_t r;
3004 size_t stop_count = 0;
3005 struct efi_object *efiobj;
3006
3007 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3008 child_handle);
3009
3010 efiobj = efi_search_obj(controller_handle);
3011 if (!efiobj) {
3012 r = EFI_INVALID_PARAMETER;
3013 goto out;
3014 }
3015
3016 if (child_handle && !efi_search_obj(child_handle)) {
3017 r = EFI_INVALID_PARAMETER;
3018 goto out;
3019 }
3020
3021
3022 if (!driver_image_handle) {
3023 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3024 goto out;
3025 }
3026
3027
3028 if (child_handle) {
3029 number_of_children = 1;
3030 child_handle_buffer = &child_handle;
3031 } else {
3032 efi_get_child_controllers(efiobj,
3033 driver_image_handle,
3034 &number_of_children,
3035 &child_handle_buffer);
3036 }
3037
3038
3039 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3040 &efi_guid_driver_binding_protocol,
3041 (void **)&binding_protocol,
3042 driver_image_handle, NULL,
3043 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3044 if (r != EFI_SUCCESS)
3045 goto out;
3046
3047 if (number_of_children) {
3048 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3049 controller_handle,
3050 number_of_children,
3051 child_handle_buffer));
3052 if (r == EFI_SUCCESS)
3053 ++stop_count;
3054 }
3055
3056 if (!child_handle)
3057 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3058 controller_handle,
3059 0, NULL));
3060 if (r == EFI_SUCCESS)
3061 ++stop_count;
3062 EFI_CALL(efi_close_protocol(driver_image_handle,
3063 &efi_guid_driver_binding_protocol,
3064 driver_image_handle, NULL));
3065
3066 if (stop_count)
3067 r = EFI_SUCCESS;
3068 else
3069 r = EFI_NOT_FOUND;
3070out:
3071 if (!child_handle)
3072 free(child_handle_buffer);
3073 return EFI_EXIT(r);
3074}
3075
3076static struct efi_boot_services efi_boot_services = {
3077 .hdr = {
3078 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3079 .revision = EFI_SPECIFICATION_VERSION,
3080 .headersize = sizeof(struct efi_boot_services),
3081 },
3082 .raise_tpl = efi_raise_tpl,
3083 .restore_tpl = efi_restore_tpl,
3084 .allocate_pages = efi_allocate_pages_ext,
3085 .free_pages = efi_free_pages_ext,
3086 .get_memory_map = efi_get_memory_map_ext,
3087 .allocate_pool = efi_allocate_pool_ext,
3088 .free_pool = efi_free_pool_ext,
3089 .create_event = efi_create_event_ext,
3090 .set_timer = efi_set_timer_ext,
3091 .wait_for_event = efi_wait_for_event,
3092 .signal_event = efi_signal_event_ext,
3093 .close_event = efi_close_event,
3094 .check_event = efi_check_event,
3095 .install_protocol_interface = efi_install_protocol_interface,
3096 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3097 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3098 .handle_protocol = efi_handle_protocol,
3099 .reserved = NULL,
3100 .register_protocol_notify = efi_register_protocol_notify,
3101 .locate_handle = efi_locate_handle_ext,
3102 .locate_device_path = efi_locate_device_path,
3103 .install_configuration_table = efi_install_configuration_table_ext,
3104 .load_image = efi_load_image,
3105 .start_image = efi_start_image,
3106 .exit = efi_exit,
3107 .unload_image = efi_unload_image,
3108 .exit_boot_services = efi_exit_boot_services,
3109 .get_next_monotonic_count = efi_get_next_monotonic_count,
3110 .stall = efi_stall,
3111 .set_watchdog_timer = efi_set_watchdog_timer,
3112 .connect_controller = efi_connect_controller,
3113 .disconnect_controller = efi_disconnect_controller,
3114 .open_protocol = efi_open_protocol,
3115 .close_protocol = efi_close_protocol,
3116 .open_protocol_information = efi_open_protocol_information,
3117 .protocols_per_handle = efi_protocols_per_handle,
3118 .locate_handle_buffer = efi_locate_handle_buffer,
3119 .locate_protocol = efi_locate_protocol,
3120 .install_multiple_protocol_interfaces =
3121 efi_install_multiple_protocol_interfaces,
3122 .uninstall_multiple_protocol_interfaces =
3123 efi_uninstall_multiple_protocol_interfaces,
3124 .calculate_crc32 = efi_calculate_crc32,
3125 .copy_mem = efi_copy_mem,
3126 .set_mem = efi_set_mem,
3127 .create_event_ex = efi_create_event_ex,
3128};
3129
3130static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3131
3132struct efi_system_table __efi_runtime_data systab = {
3133 .hdr = {
3134 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3135 .revision = EFI_SPECIFICATION_VERSION,
3136 .headersize = sizeof(struct efi_system_table),
3137 },
3138 .fw_vendor = firmware_vendor,
3139 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3140 .con_in = (void *)&efi_con_in,
3141 .con_out = (void *)&efi_con_out,
3142 .std_err = (void *)&efi_con_out,
3143 .runtime = (void *)&efi_runtime_services,
3144 .boottime = (void *)&efi_boot_services,
3145 .nr_tables = 0,
3146 .tables = NULL,
3147};
3148
3149
3150
3151
3152
3153
3154efi_status_t efi_initialize_system_table(void)
3155{
3156 efi_status_t ret;
3157
3158
3159 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3160 EFI_MAX_CONFIGURATION_TABLES *
3161 sizeof(struct efi_configuration_table),
3162 (void **)&systab.tables);
3163
3164
3165 efi_update_table_header_crc32(&systab.hdr);
3166 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3167 efi_update_table_header_crc32(&efi_boot_services.hdr);
3168
3169 return ret;
3170}
3171