]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Hand/Handle.c
Add VA_END to end the VA_START.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Hand / Handle.c
1 /** @file
2 UEFI handle & protocol handling.
3
4 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "DxeMain.h"
16
17
18 //
19 // mProtocolDatabase - A list of all protocols in the system. (simple list for now)
20 // gHandleList - A list of all the handles in the system
21 // gProtocolDatabaseLock - Lock to protect the mProtocolDatabase
22 // gHandleDatabaseKey - The Key to show that the handle has been created/modified
23 //
24 LIST_ENTRY mProtocolDatabase = INITIALIZE_LIST_HEAD_VARIABLE (mProtocolDatabase);
25 LIST_ENTRY gHandleList = INITIALIZE_LIST_HEAD_VARIABLE (gHandleList);
26 EFI_LOCK gProtocolDatabaseLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);
27 UINT64 gHandleDatabaseKey = 0;
28
29
30
31 /**
32 Acquire lock on gProtocolDatabaseLock.
33
34 **/
35 VOID
36 CoreAcquireProtocolLock (
37 VOID
38 )
39 {
40 CoreAcquireLock (&gProtocolDatabaseLock);
41 }
42
43
44
45 /**
46 Release lock on gProtocolDatabaseLock.
47
48 **/
49 VOID
50 CoreReleaseProtocolLock (
51 VOID
52 )
53 {
54 CoreReleaseLock (&gProtocolDatabaseLock);
55 }
56
57
58
59 /**
60 Check whether a handle is a valid EFI_HANDLE
61
62 @param UserHandle The handle to check
63
64 @retval EFI_INVALID_PARAMETER The handle is NULL or not a valid EFI_HANDLE.
65 @retval EFI_SUCCESS The handle is valid EFI_HANDLE.
66
67 **/
68 EFI_STATUS
69 CoreValidateHandle (
70 IN EFI_HANDLE UserHandle
71 )
72 {
73 IHANDLE *Handle;
74
75 Handle = (IHANDLE *)UserHandle;
76 if (Handle == NULL) {
77 return EFI_INVALID_PARAMETER;
78 }
79 if (Handle->Signature != EFI_HANDLE_SIGNATURE) {
80 return EFI_INVALID_PARAMETER;
81 }
82 return EFI_SUCCESS;
83 }
84
85
86
87 /**
88 Finds the protocol entry for the requested protocol.
89 The gProtocolDatabaseLock must be owned
90
91 @param Protocol The ID of the protocol
92 @param Create Create a new entry if not found
93
94 @return Protocol entry
95
96 **/
97 PROTOCOL_ENTRY *
98 CoreFindProtocolEntry (
99 IN EFI_GUID *Protocol,
100 IN BOOLEAN Create
101 )
102 {
103 LIST_ENTRY *Link;
104 PROTOCOL_ENTRY *Item;
105 PROTOCOL_ENTRY *ProtEntry;
106
107 ASSERT_LOCKED(&gProtocolDatabaseLock);
108
109 //
110 // Search the database for the matching GUID
111 //
112
113 ProtEntry = NULL;
114 for (Link = mProtocolDatabase.ForwardLink;
115 Link != &mProtocolDatabase;
116 Link = Link->ForwardLink) {
117
118 Item = CR(Link, PROTOCOL_ENTRY, AllEntries, PROTOCOL_ENTRY_SIGNATURE);
119 if (CompareGuid (&Item->ProtocolID, Protocol)) {
120
121 //
122 // This is the protocol entry
123 //
124
125 ProtEntry = Item;
126 break;
127 }
128 }
129
130 //
131 // If the protocol entry was not found and Create is TRUE, then
132 // allocate a new entry
133 //
134 if ((ProtEntry == NULL) && Create) {
135 ProtEntry = AllocatePool (sizeof(PROTOCOL_ENTRY));
136
137 if (ProtEntry != NULL) {
138 //
139 // Initialize new protocol entry structure
140 //
141 ProtEntry->Signature = PROTOCOL_ENTRY_SIGNATURE;
142 CopyGuid ((VOID *)&ProtEntry->ProtocolID, Protocol);
143 InitializeListHead (&ProtEntry->Protocols);
144 InitializeListHead (&ProtEntry->Notify);
145
146 //
147 // Add it to protocol database
148 //
149 InsertTailList (&mProtocolDatabase, &ProtEntry->AllEntries);
150 }
151 }
152
153 return ProtEntry;
154 }
155
156
157
158 /**
159 Finds the protocol instance for the requested handle and protocol.
160 Note: This function doesn't do parameters checking, it's caller's responsibility
161 to pass in valid parameters.
162
163 @param Handle The handle to search the protocol on
164 @param Protocol GUID of the protocol
165 @param Interface The interface for the protocol being searched
166
167 @return Protocol instance (NULL: Not found)
168
169 **/
170 PROTOCOL_INTERFACE *
171 CoreFindProtocolInterface (
172 IN IHANDLE *Handle,
173 IN EFI_GUID *Protocol,
174 IN VOID *Interface
175 )
176 {
177 PROTOCOL_INTERFACE *Prot;
178 PROTOCOL_ENTRY *ProtEntry;
179 LIST_ENTRY *Link;
180
181 ASSERT_LOCKED(&gProtocolDatabaseLock);
182 Prot = NULL;
183
184 //
185 // Lookup the protocol entry for this protocol ID
186 //
187
188 ProtEntry = CoreFindProtocolEntry (Protocol, FALSE);
189 if (ProtEntry != NULL) {
190
191 //
192 // Look at each protocol interface for any matches
193 //
194 for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link=Link->ForwardLink) {
195
196 //
197 // If this protocol interface matches, remove it
198 //
199 Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);
200 if (Prot->Interface == Interface && Prot->Protocol == ProtEntry) {
201 break;
202 }
203
204 Prot = NULL;
205 }
206 }
207
208 return Prot;
209 }
210
211
212 /**
213 Removes an event from a register protocol notify list on a protocol.
214
215 @param Event The event to search for in the protocol
216 database.
217
218 @return EFI_SUCCESS if the event was found and removed.
219 @return EFI_NOT_FOUND if the event was not found in the protocl database.
220
221 **/
222 EFI_STATUS
223 CoreUnregisterProtocolNotifyEvent (
224 IN EFI_EVENT Event
225 )
226 {
227 LIST_ENTRY *Link;
228 PROTOCOL_ENTRY *ProtEntry;
229 LIST_ENTRY *NotifyLink;
230 PROTOCOL_NOTIFY *ProtNotify;
231
232 CoreAcquireProtocolLock ();
233
234 for ( Link = mProtocolDatabase.ForwardLink;
235 Link != &mProtocolDatabase;
236 Link = Link->ForwardLink) {
237
238 ProtEntry = CR(Link, PROTOCOL_ENTRY, AllEntries, PROTOCOL_ENTRY_SIGNATURE);
239
240 for ( NotifyLink = ProtEntry->Notify.ForwardLink;
241 NotifyLink != &ProtEntry->Notify;
242 NotifyLink = NotifyLink->ForwardLink) {
243
244 ProtNotify = CR(NotifyLink, PROTOCOL_NOTIFY, Link, PROTOCOL_NOTIFY_SIGNATURE);
245
246 if (ProtNotify->Event == Event) {
247 RemoveEntryList(&ProtNotify->Link);
248 CoreFreePool(ProtNotify);
249 CoreReleaseProtocolLock ();
250 return EFI_SUCCESS;
251 }
252 }
253 }
254
255 CoreReleaseProtocolLock ();
256 return EFI_NOT_FOUND;
257 }
258
259
260
261 /**
262 Removes all the events in the protocol database that match Event.
263
264 @param Event The event to search for in the protocol
265 database.
266
267 @return EFI_SUCCESS when done searching the entire database.
268
269 **/
270 EFI_STATUS
271 CoreUnregisterProtocolNotify (
272 IN EFI_EVENT Event
273 )
274 {
275 EFI_STATUS Status;
276
277 do {
278 Status = CoreUnregisterProtocolNotifyEvent (Event);
279 } while (!EFI_ERROR (Status));
280
281 return EFI_SUCCESS;
282 }
283
284
285
286
287 /**
288 Wrapper function to CoreInstallProtocolInterfaceNotify. This is the public API which
289 Calls the private one which contains a BOOLEAN parameter for notifications
290
291 @param UserHandle The handle to install the protocol handler on,
292 or NULL if a new handle is to be allocated
293 @param Protocol The protocol to add to the handle
294 @param InterfaceType Indicates whether Interface is supplied in
295 native form.
296 @param Interface The interface for the protocol being added
297
298 @return Status code
299
300 **/
301 EFI_STATUS
302 EFIAPI
303 CoreInstallProtocolInterface (
304 IN OUT EFI_HANDLE *UserHandle,
305 IN EFI_GUID *Protocol,
306 IN EFI_INTERFACE_TYPE InterfaceType,
307 IN VOID *Interface
308 )
309 {
310 return CoreInstallProtocolInterfaceNotify (
311 UserHandle,
312 Protocol,
313 InterfaceType,
314 Interface,
315 TRUE
316 );
317 }
318
319
320 /**
321 Installs a protocol interface into the boot services environment.
322
323 @param UserHandle The handle to install the protocol handler on,
324 or NULL if a new handle is to be allocated
325 @param Protocol The protocol to add to the handle
326 @param InterfaceType Indicates whether Interface is supplied in
327 native form.
328 @param Interface The interface for the protocol being added
329 @param Notify indicates whether notify the notification list
330 for this protocol
331
332 @retval EFI_INVALID_PARAMETER Invalid parameter
333 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
334 @retval EFI_SUCCESS Protocol interface successfully installed
335
336 **/
337 EFI_STATUS
338 CoreInstallProtocolInterfaceNotify (
339 IN OUT EFI_HANDLE *UserHandle,
340 IN EFI_GUID *Protocol,
341 IN EFI_INTERFACE_TYPE InterfaceType,
342 IN VOID *Interface,
343 IN BOOLEAN Notify
344 )
345 {
346 PROTOCOL_INTERFACE *Prot;
347 PROTOCOL_ENTRY *ProtEntry;
348 IHANDLE *Handle;
349 EFI_STATUS Status;
350 VOID *ExistingInterface;
351
352 //
353 // returns EFI_INVALID_PARAMETER if InterfaceType is invalid.
354 // Also added check for invalid UserHandle and Protocol pointers.
355 //
356 if (UserHandle == NULL || Protocol == NULL) {
357 return EFI_INVALID_PARAMETER;
358 }
359
360 if (InterfaceType != EFI_NATIVE_INTERFACE) {
361 return EFI_INVALID_PARAMETER;
362 }
363
364 //
365 // Print debug message
366 //
367 DEBUG((DEBUG_LOAD | DEBUG_INFO, "InstallProtocolInterface: %g %p\n", Protocol, Interface));
368
369 Status = EFI_OUT_OF_RESOURCES;
370 Prot = NULL;
371 Handle = NULL;
372
373 if (*UserHandle != NULL) {
374 Status = CoreHandleProtocol (*UserHandle, Protocol, (VOID **)&ExistingInterface);
375 if (!EFI_ERROR (Status)) {
376 return EFI_INVALID_PARAMETER;
377 }
378 }
379
380 //
381 // Lock the protocol database
382 //
383 CoreAcquireProtocolLock ();
384
385 //
386 // Lookup the Protocol Entry for the requested protocol
387 //
388 ProtEntry = CoreFindProtocolEntry (Protocol, TRUE);
389 if (ProtEntry == NULL) {
390 goto Done;
391 }
392
393 //
394 // Allocate a new protocol interface structure
395 //
396 Prot = AllocateZeroPool (sizeof(PROTOCOL_INTERFACE));
397 if (Prot == NULL) {
398 Status = EFI_OUT_OF_RESOURCES;
399 goto Done;
400 }
401
402 //
403 // If caller didn't supply a handle, allocate a new one
404 //
405 Handle = (IHANDLE *)*UserHandle;
406 if (Handle == NULL) {
407 Handle = AllocateZeroPool (sizeof(IHANDLE));
408 if (Handle == NULL) {
409 Status = EFI_OUT_OF_RESOURCES;
410 goto Done;
411 }
412
413 //
414 // Initialize new handler structure
415 //
416 Handle->Signature = EFI_HANDLE_SIGNATURE;
417 InitializeListHead (&Handle->Protocols);
418
419 //
420 // Initialize the Key to show that the handle has been created/modified
421 //
422 gHandleDatabaseKey++;
423 Handle->Key = gHandleDatabaseKey;
424
425 //
426 // Add this handle to the list global list of all handles
427 // in the system
428 //
429 InsertTailList (&gHandleList, &Handle->AllHandles);
430 }
431
432 Status = CoreValidateHandle (Handle);
433 if (EFI_ERROR (Status)) {
434 goto Done;
435 }
436
437 //
438 // Each interface that is added must be unique
439 //
440 ASSERT (CoreFindProtocolInterface (Handle, Protocol, Interface) == NULL);
441
442 //
443 // Initialize the protocol interface structure
444 //
445 Prot->Signature = PROTOCOL_INTERFACE_SIGNATURE;
446 Prot->Handle = Handle;
447 Prot->Protocol = ProtEntry;
448 Prot->Interface = Interface;
449
450 //
451 // Initalize OpenProtocol Data base
452 //
453 InitializeListHead (&Prot->OpenList);
454 Prot->OpenListCount = 0;
455
456 //
457 // Add this protocol interface to the head of the supported
458 // protocol list for this handle
459 //
460 InsertHeadList (&Handle->Protocols, &Prot->Link);
461
462 //
463 // Add this protocol interface to the tail of the
464 // protocol entry
465 //
466 InsertTailList (&ProtEntry->Protocols, &Prot->ByProtocol);
467
468 //
469 // Notify the notification list for this protocol
470 //
471 if (Notify) {
472 CoreNotifyProtocolEntry (ProtEntry);
473 }
474 Status = EFI_SUCCESS;
475
476 Done:
477 //
478 // Done, unlock the database and return
479 //
480 CoreReleaseProtocolLock ();
481 if (!EFI_ERROR (Status)) {
482 //
483 // Return the new handle back to the caller
484 //
485 *UserHandle = Handle;
486 } else {
487 //
488 // There was an error, clean up
489 //
490 if (Prot != NULL) {
491 CoreFreePool (Prot);
492 }
493 }
494
495 return Status;
496 }
497
498
499
500
501 /**
502 Installs a list of protocol interface into the boot services environment.
503 This function calls InstallProtocolInterface() in a loop. If any error
504 occures all the protocols added by this function are removed. This is
505 basically a lib function to save space.
506
507 @param Handle The handle to install the protocol handlers on,
508 or NULL if a new handle is to be allocated
509 @param ... EFI_GUID followed by protocol instance. A NULL
510 terminates the list. The pairs are the
511 arguments to InstallProtocolInterface(). All the
512 protocols are added to Handle.
513
514 @retval EFI_INVALID_PARAMETER Handle is NULL.
515 @retval EFI_SUCCESS Protocol interfaces successfully installed.
516
517 **/
518 EFI_STATUS
519 EFIAPI
520 CoreInstallMultipleProtocolInterfaces (
521 IN OUT EFI_HANDLE *Handle,
522 ...
523 )
524 {
525 VA_LIST Args;
526 EFI_STATUS Status;
527 EFI_GUID *Protocol;
528 VOID *Interface;
529 EFI_TPL OldTpl;
530 UINTN Index;
531 EFI_HANDLE OldHandle;
532 EFI_HANDLE DeviceHandle;
533 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
534
535 if (Handle == NULL) {
536 return EFI_INVALID_PARAMETER;
537 }
538
539 //
540 // Syncronize with notifcations.
541 //
542 OldTpl = CoreRaiseTpl (TPL_NOTIFY);
543 OldHandle = *Handle;
544
545 //
546 // Check for duplicate device path and install the protocol interfaces
547 //
548 VA_START (Args, Handle);
549 for (Index = 0, Status = EFI_SUCCESS; !EFI_ERROR (Status); Index++) {
550 //
551 // If protocol is NULL, then it's the end of the list
552 //
553 Protocol = VA_ARG (Args, EFI_GUID *);
554 if (Protocol == NULL) {
555 break;
556 }
557
558 Interface = VA_ARG (Args, VOID *);
559
560 //
561 // Make sure you are installing on top a device path that has already been added.
562 //
563 if (CompareGuid (Protocol, &gEfiDevicePathProtocolGuid)) {
564 DeviceHandle = NULL;
565 DevicePath = Interface;
566 Status = CoreLocateDevicePath (&gEfiDevicePathProtocolGuid, &DevicePath, &DeviceHandle);
567 if (!EFI_ERROR (Status) && (DeviceHandle != NULL) && IsDevicePathEnd(DevicePath)) {
568 Status = EFI_ALREADY_STARTED;
569 continue;
570 }
571 }
572
573 //
574 // Install it
575 //
576 Status = CoreInstallProtocolInterface (Handle, Protocol, EFI_NATIVE_INTERFACE, Interface);
577 }
578 VA_END (Args);
579
580 //
581 // If there was an error, remove all the interfaces that were installed without any errors
582 //
583 if (EFI_ERROR (Status)) {
584 //
585 // Reset the va_arg back to the first argument.
586 //
587 VA_START (Args, Handle);
588 for (; Index > 1; Index--) {
589 Protocol = VA_ARG (Args, EFI_GUID *);
590 Interface = VA_ARG (Args, VOID *);
591 CoreUninstallProtocolInterface (*Handle, Protocol, Interface);
592 }
593 VA_END (Args);
594
595 *Handle = OldHandle;
596 }
597
598 //
599 // Done
600 //
601 CoreRestoreTpl (OldTpl);
602 return Status;
603 }
604
605
606 /**
607 Attempts to disconnect all drivers that are using the protocol interface being queried.
608 If failed, reconnect all drivers disconnected.
609 Note: This function doesn't do parameters checking, it's caller's responsibility
610 to pass in valid parameters.
611
612 @param UserHandle The handle on which the protocol is installed
613 @param Prot The protocol to disconnect drivers from
614
615 @retval EFI_SUCCESS Drivers using the protocol interface are all
616 disconnected
617 @retval EFI_ACCESS_DENIED Failed to disconnect one or all of the drivers
618
619 **/
620 EFI_STATUS
621 CoreDisconnectControllersUsingProtocolInterface (
622 IN EFI_HANDLE UserHandle,
623 IN PROTOCOL_INTERFACE *Prot
624 )
625 {
626 EFI_STATUS Status;
627 BOOLEAN ItemFound;
628 LIST_ENTRY *Link;
629 OPEN_PROTOCOL_DATA *OpenData;
630
631 Status = EFI_SUCCESS;
632
633 //
634 // Attempt to disconnect all drivers from this protocol interface
635 //
636 do {
637 ItemFound = FALSE;
638 for ( Link = Prot->OpenList.ForwardLink;
639 (Link != &Prot->OpenList) && !ItemFound;
640 Link = Link->ForwardLink ) {
641 OpenData = CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE);
642 if (OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
643 ItemFound = TRUE;
644 CoreReleaseProtocolLock ();
645 Status = CoreDisconnectController (UserHandle, OpenData->AgentHandle, NULL);
646 CoreAcquireProtocolLock ();
647 if (EFI_ERROR (Status)) {
648 ItemFound = FALSE;
649 break;
650 }
651 }
652 }
653 } while (ItemFound);
654
655 if (!EFI_ERROR (Status)) {
656 //
657 // Attempt to remove BY_HANDLE_PROTOOCL and GET_PROTOCOL and TEST_PROTOCOL Open List items
658 //
659 do {
660 ItemFound = FALSE;
661 for ( Link = Prot->OpenList.ForwardLink;
662 (Link != &Prot->OpenList) && !ItemFound;
663 Link = Link->ForwardLink ) {
664 OpenData = CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE);
665 if (OpenData->Attributes &
666 (EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL | EFI_OPEN_PROTOCOL_GET_PROTOCOL | EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
667 ItemFound = TRUE;
668 RemoveEntryList (&OpenData->Link);
669 Prot->OpenListCount--;
670 CoreFreePool (OpenData);
671 }
672 }
673 } while (ItemFound);
674 }
675
676 //
677 // If there are errors or still has open items in the list, then reconnect all the drivers and return an error
678 //
679 if (EFI_ERROR (Status) || (Prot->OpenListCount > 0)) {
680 CoreReleaseProtocolLock ();
681 CoreConnectController (UserHandle, NULL, NULL, TRUE);
682 CoreAcquireProtocolLock ();
683 Status = EFI_ACCESS_DENIED;
684 }
685
686 return Status;
687 }
688
689
690
691 /**
692 Uninstalls all instances of a protocol:interfacer from a handle.
693 If the last protocol interface is remove from the handle, the
694 handle is freed.
695
696 @param UserHandle The handle to remove the protocol handler from
697 @param Protocol The protocol, of protocol:interface, to remove
698 @param Interface The interface, of protocol:interface, to remove
699
700 @retval EFI_INVALID_PARAMETER Protocol is NULL.
701 @retval EFI_SUCCESS Protocol interface successfully uninstalled.
702
703 **/
704 EFI_STATUS
705 EFIAPI
706 CoreUninstallProtocolInterface (
707 IN EFI_HANDLE UserHandle,
708 IN EFI_GUID *Protocol,
709 IN VOID *Interface
710 )
711 {
712 EFI_STATUS Status;
713 IHANDLE *Handle;
714 PROTOCOL_INTERFACE *Prot;
715
716 //
717 // Check that Protocol is valid
718 //
719 if (Protocol == NULL) {
720 return EFI_INVALID_PARAMETER;
721 }
722
723 //
724 // Check that UserHandle is a valid handle
725 //
726 Status = CoreValidateHandle (UserHandle);
727 if (EFI_ERROR (Status)) {
728 return Status;
729 }
730
731 //
732 // Lock the protocol database
733 //
734 CoreAcquireProtocolLock ();
735
736 //
737 // Check that Protocol exists on UserHandle, and Interface matches the interface in the database
738 //
739 Prot = CoreFindProtocolInterface (UserHandle, Protocol, Interface);
740 if (Prot == NULL) {
741 Status = EFI_NOT_FOUND;
742 goto Done;
743 }
744
745 //
746 // Attempt to disconnect all drivers that are using the protocol interface that is about to be removed
747 //
748 Status = CoreDisconnectControllersUsingProtocolInterface (
749 UserHandle,
750 Prot
751 );
752 if (EFI_ERROR (Status)) {
753 //
754 // One or more drivers refused to release, so return the error
755 //
756 goto Done;
757 }
758
759 //
760 // Remove the protocol interface from the protocol
761 //
762 Status = EFI_NOT_FOUND;
763 Handle = (IHANDLE *)UserHandle;
764 Prot = CoreRemoveInterfaceFromProtocol (Handle, Protocol, Interface);
765
766 if (Prot != NULL) {
767 //
768 // Update the Key to show that the handle has been created/modified
769 //
770 gHandleDatabaseKey++;
771 Handle->Key = gHandleDatabaseKey;
772
773 //
774 // Remove the protocol interface from the handle
775 //
776 RemoveEntryList (&Prot->Link);
777
778 //
779 // Free the memory
780 //
781 Prot->Signature = 0;
782 CoreFreePool (Prot);
783 Status = EFI_SUCCESS;
784 }
785
786 //
787 // If there are no more handlers for the handle, free the handle
788 //
789 if (IsListEmpty (&Handle->Protocols)) {
790 Handle->Signature = 0;
791 RemoveEntryList (&Handle->AllHandles);
792 CoreFreePool (Handle);
793 }
794
795 Done:
796 //
797 // Done, unlock the database and return
798 //
799 CoreReleaseProtocolLock ();
800 return Status;
801 }
802
803
804
805
806 /**
807 Uninstalls a list of protocol interface in the boot services environment.
808 This function calls UnisatllProtocolInterface() in a loop. This is
809 basically a lib function to save space.
810
811 @param Handle The handle to uninstall the protocol
812 @param ... EFI_GUID followed by protocol instance. A NULL
813 terminates the list. The pairs are the
814 arguments to UninstallProtocolInterface(). All
815 the protocols are added to Handle.
816
817 @return Status code
818
819 **/
820 EFI_STATUS
821 EFIAPI
822 CoreUninstallMultipleProtocolInterfaces (
823 IN EFI_HANDLE Handle,
824 ...
825 )
826 {
827 EFI_STATUS Status;
828 VA_LIST Args;
829 EFI_GUID *Protocol;
830 VOID *Interface;
831 UINTN Index;
832
833 VA_START (Args, Handle);
834 for (Index = 0, Status = EFI_SUCCESS; !EFI_ERROR (Status); Index++) {
835 //
836 // If protocol is NULL, then it's the end of the list
837 //
838 Protocol = VA_ARG (Args, EFI_GUID *);
839 if (Protocol == NULL) {
840 break;
841 }
842
843 Interface = VA_ARG (Args, VOID *);
844
845 //
846 // Uninstall it
847 //
848 Status = CoreUninstallProtocolInterface (Handle, Protocol, Interface);
849 }
850 VA_END (Args);
851
852 //
853 // If there was an error, add all the interfaces that were
854 // uninstalled without any errors
855 //
856 if (EFI_ERROR (Status)) {
857 //
858 // Reset the va_arg back to the first argument.
859 //
860 VA_START (Args, Handle);
861 for (; Index > 1; Index--) {
862 Protocol = VA_ARG(Args, EFI_GUID *);
863 Interface = VA_ARG(Args, VOID *);
864 CoreInstallProtocolInterface (&Handle, Protocol, EFI_NATIVE_INTERFACE, Interface);
865 }
866 VA_END (Args);
867 }
868
869 return Status;
870 }
871
872
873 /**
874 Locate a certain GUID protocol interface in a Handle's protocols.
875
876 @param UserHandle The handle to obtain the protocol interface on
877 @param Protocol The GUID of the protocol
878
879 @return The requested protocol interface for the handle
880
881 **/
882 PROTOCOL_INTERFACE *
883 CoreGetProtocolInterface (
884 IN EFI_HANDLE UserHandle,
885 IN EFI_GUID *Protocol
886 )
887 {
888 EFI_STATUS Status;
889 PROTOCOL_ENTRY *ProtEntry;
890 PROTOCOL_INTERFACE *Prot;
891 IHANDLE *Handle;
892 LIST_ENTRY *Link;
893
894 Status = CoreValidateHandle (UserHandle);
895 if (EFI_ERROR (Status)) {
896 return NULL;
897 }
898
899 Handle = (IHANDLE *)UserHandle;
900
901 //
902 // Look at each protocol interface for a match
903 //
904 for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link = Link->ForwardLink) {
905 Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);
906 ProtEntry = Prot->Protocol;
907 if (CompareGuid (&ProtEntry->ProtocolID, Protocol)) {
908 return Prot;
909 }
910 }
911 return NULL;
912 }
913
914
915
916 /**
917 Queries a handle to determine if it supports a specified protocol.
918
919 @param UserHandle The handle being queried.
920 @param Protocol The published unique identifier of the protocol.
921 @param Interface Supplies the address where a pointer to the
922 corresponding Protocol Interface is returned.
923
924 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
925 @retval EFI_UNSUPPORTED The device does not support the specified protocol.
926 @retval EFI_INVALID_PARAMETER Handle is not a valid EFI_HANDLE..
927 @retval EFI_INVALID_PARAMETER Protocol is NULL.
928 @retval EFI_INVALID_PARAMETER Interface is NULL.
929
930 **/
931 EFI_STATUS
932 EFIAPI
933 CoreHandleProtocol (
934 IN EFI_HANDLE UserHandle,
935 IN EFI_GUID *Protocol,
936 OUT VOID **Interface
937 )
938 {
939 return CoreOpenProtocol (
940 UserHandle,
941 Protocol,
942 Interface,
943 gDxeCoreImageHandle,
944 NULL,
945 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
946 );
947 }
948
949
950
951 /**
952 Locates the installed protocol handler for the handle, and
953 invokes it to obtain the protocol interface. Usage information
954 is registered in the protocol data base.
955
956 @param UserHandle The handle to obtain the protocol interface on
957 @param Protocol The ID of the protocol
958 @param Interface The location to return the protocol interface
959 @param ImageHandle The handle of the Image that is opening the
960 protocol interface specified by Protocol and
961 Interface.
962 @param ControllerHandle The controller handle that is requiring this
963 interface.
964 @param Attributes The open mode of the protocol interface
965 specified by Handle and Protocol.
966
967 @retval EFI_INVALID_PARAMETER Protocol is NULL.
968 @retval EFI_SUCCESS Get the protocol interface.
969
970 **/
971 EFI_STATUS
972 EFIAPI
973 CoreOpenProtocol (
974 IN EFI_HANDLE UserHandle,
975 IN EFI_GUID *Protocol,
976 OUT VOID **Interface OPTIONAL,
977 IN EFI_HANDLE ImageHandle,
978 IN EFI_HANDLE ControllerHandle,
979 IN UINT32 Attributes
980 )
981 {
982 EFI_STATUS Status;
983 PROTOCOL_INTERFACE *Prot;
984 LIST_ENTRY *Link;
985 OPEN_PROTOCOL_DATA *OpenData;
986 BOOLEAN ByDriver;
987 BOOLEAN Exclusive;
988 BOOLEAN Disconnect;
989 BOOLEAN ExactMatch;
990
991 //
992 // Check for invalid Protocol
993 //
994 if (Protocol == NULL) {
995 return EFI_INVALID_PARAMETER;
996 }
997
998 //
999 // Check for invalid Interface
1000 //
1001 if (Attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1002 if (Interface == NULL) {
1003 return EFI_INVALID_PARAMETER;
1004 } else {
1005 *Interface = NULL;
1006 }
1007 }
1008
1009 //
1010 // Check for invalid UserHandle
1011 //
1012 Status = CoreValidateHandle (UserHandle);
1013 if (EFI_ERROR (Status)) {
1014 return Status;
1015 }
1016
1017 //
1018 // Check for invalid Attributes
1019 //
1020 switch (Attributes) {
1021 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER :
1022 Status = CoreValidateHandle (ImageHandle);
1023 if (EFI_ERROR (Status)) {
1024 return Status;
1025 }
1026 Status = CoreValidateHandle (ControllerHandle);
1027 if (EFI_ERROR (Status)) {
1028 return Status;
1029 }
1030 if (UserHandle == ControllerHandle) {
1031 return EFI_INVALID_PARAMETER;
1032 }
1033 break;
1034 case EFI_OPEN_PROTOCOL_BY_DRIVER :
1035 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE :
1036 Status = CoreValidateHandle (ImageHandle);
1037 if (EFI_ERROR (Status)) {
1038 return Status;
1039 }
1040 Status = CoreValidateHandle (ControllerHandle);
1041 if (EFI_ERROR (Status)) {
1042 return Status;
1043 }
1044 break;
1045 case EFI_OPEN_PROTOCOL_EXCLUSIVE :
1046 Status = CoreValidateHandle (ImageHandle);
1047 if (EFI_ERROR (Status)) {
1048 return Status;
1049 }
1050 break;
1051 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL :
1052 case EFI_OPEN_PROTOCOL_GET_PROTOCOL :
1053 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL :
1054 break;
1055 default:
1056 return EFI_INVALID_PARAMETER;
1057 }
1058
1059 //
1060 // Lock the protocol database
1061 //
1062 CoreAcquireProtocolLock ();
1063
1064 //
1065 // Look at each protocol interface for a match
1066 //
1067 Prot = CoreGetProtocolInterface (UserHandle, Protocol);
1068 if (Prot == NULL) {
1069 Status = EFI_UNSUPPORTED;
1070 goto Done;
1071 }
1072
1073 //
1074 // This is the protocol interface entry for this protocol
1075 //
1076 if (Attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1077 *Interface = Prot->Interface;
1078 }
1079 Status = EFI_SUCCESS;
1080
1081 ByDriver = FALSE;
1082 Exclusive = FALSE;
1083 for ( Link = Prot->OpenList.ForwardLink; Link != &Prot->OpenList; Link = Link->ForwardLink) {
1084 OpenData = CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE);
1085 ExactMatch = (BOOLEAN)((OpenData->AgentHandle == ImageHandle) &&
1086 (OpenData->Attributes == Attributes) &&
1087 (OpenData->ControllerHandle == ControllerHandle));
1088 if (OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
1089 ByDriver = TRUE;
1090 if (ExactMatch) {
1091 Status = EFI_ALREADY_STARTED;
1092 goto Done;
1093 }
1094 }
1095 if (OpenData->Attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
1096 Exclusive = TRUE;
1097 } else if (ExactMatch) {
1098 OpenData->OpenCount++;
1099 Status = EFI_SUCCESS;
1100 goto Done;
1101 }
1102 }
1103
1104 //
1105 // ByDriver TRUE -> A driver is managing (UserHandle, Protocol)
1106 // ByDriver FALSE -> There are no drivers managing (UserHandle, Protocol)
1107 // Exclusive TRUE -> Something has exclusive access to (UserHandle, Protocol)
1108 // Exclusive FALSE -> Nothing has exclusive access to (UserHandle, Protocol)
1109 //
1110
1111 switch (Attributes) {
1112 case EFI_OPEN_PROTOCOL_BY_DRIVER :
1113 if (Exclusive || ByDriver) {
1114 Status = EFI_ACCESS_DENIED;
1115 goto Done;
1116 }
1117 break;
1118 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE :
1119 case EFI_OPEN_PROTOCOL_EXCLUSIVE :
1120 if (Exclusive) {
1121 Status = EFI_ACCESS_DENIED;
1122 goto Done;
1123 }
1124 if (ByDriver) {
1125 do {
1126 Disconnect = FALSE;
1127 for ( Link = Prot->OpenList.ForwardLink; (Link != &Prot->OpenList) && (!Disconnect); Link = Link->ForwardLink) {
1128 OpenData = CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE);
1129 if (OpenData->Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
1130 Disconnect = TRUE;
1131 CoreReleaseProtocolLock ();
1132 Status = CoreDisconnectController (UserHandle, OpenData->AgentHandle, NULL);
1133 CoreAcquireProtocolLock ();
1134 if (EFI_ERROR (Status)) {
1135 Status = EFI_ACCESS_DENIED;
1136 goto Done;
1137 }
1138 }
1139 }
1140 } while (Disconnect);
1141 }
1142 break;
1143 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER :
1144 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL :
1145 case EFI_OPEN_PROTOCOL_GET_PROTOCOL :
1146 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL :
1147 break;
1148 }
1149
1150 if (ImageHandle == NULL) {
1151 Status = EFI_SUCCESS;
1152 goto Done;
1153 }
1154 //
1155 // Create new entry
1156 //
1157 OpenData = AllocatePool (sizeof(OPEN_PROTOCOL_DATA));
1158 if (OpenData == NULL) {
1159 Status = EFI_OUT_OF_RESOURCES;
1160 } else {
1161 OpenData->Signature = OPEN_PROTOCOL_DATA_SIGNATURE;
1162 OpenData->AgentHandle = ImageHandle;
1163 OpenData->ControllerHandle = ControllerHandle;
1164 OpenData->Attributes = Attributes;
1165 OpenData->OpenCount = 1;
1166 InsertTailList (&Prot->OpenList, &OpenData->Link);
1167 Prot->OpenListCount++;
1168 Status = EFI_SUCCESS;
1169 }
1170
1171 Done:
1172 //
1173 // Done. Release the database lock are return
1174 //
1175 CoreReleaseProtocolLock ();
1176 return Status;
1177 }
1178
1179
1180
1181 /**
1182 Closes a protocol on a handle that was opened using OpenProtocol().
1183
1184 @param UserHandle The handle for the protocol interface that was
1185 previously opened with OpenProtocol(), and is
1186 now being closed.
1187 @param Protocol The published unique identifier of the protocol.
1188 It is the caller's responsibility to pass in a
1189 valid GUID.
1190 @param AgentHandle The handle of the agent that is closing the
1191 protocol interface.
1192 @param ControllerHandle If the agent that opened a protocol is a driver
1193 that follows the EFI Driver Model, then this
1194 parameter is the controller handle that required
1195 the protocol interface. If the agent does not
1196 follow the EFI Driver Model, then this parameter
1197 is optional and may be NULL.
1198
1199 @retval EFI_SUCCESS The protocol instance was closed.
1200 @retval EFI_INVALID_PARAMETER Handle, AgentHandle or ControllerHandle is not a
1201 valid EFI_HANDLE.
1202 @retval EFI_NOT_FOUND Can not find the specified protocol or
1203 AgentHandle.
1204
1205 **/
1206 EFI_STATUS
1207 EFIAPI
1208 CoreCloseProtocol (
1209 IN EFI_HANDLE UserHandle,
1210 IN EFI_GUID *Protocol,
1211 IN EFI_HANDLE AgentHandle,
1212 IN EFI_HANDLE ControllerHandle
1213 )
1214 {
1215 EFI_STATUS Status;
1216 PROTOCOL_INTERFACE *ProtocolInterface;
1217 LIST_ENTRY *Link;
1218 OPEN_PROTOCOL_DATA *OpenData;
1219
1220 //
1221 // Check for invalid parameters
1222 //
1223 Status = CoreValidateHandle (UserHandle);
1224 if (EFI_ERROR (Status)) {
1225 return Status;
1226 }
1227 Status = CoreValidateHandle (AgentHandle);
1228 if (EFI_ERROR (Status)) {
1229 return Status;
1230 }
1231 if (ControllerHandle != NULL) {
1232 Status = CoreValidateHandle (ControllerHandle);
1233 if (EFI_ERROR (Status)) {
1234 return Status;
1235 }
1236 }
1237 if (Protocol == NULL) {
1238 return EFI_INVALID_PARAMETER;
1239 }
1240
1241 //
1242 // Lock the protocol database
1243 //
1244 CoreAcquireProtocolLock ();
1245
1246 //
1247 // Look at each protocol interface for a match
1248 //
1249 Status = EFI_NOT_FOUND;
1250 ProtocolInterface = CoreGetProtocolInterface (UserHandle, Protocol);
1251 if (ProtocolInterface == NULL) {
1252 goto Done;
1253 }
1254
1255 //
1256 // Walk the Open data base looking for AgentHandle
1257 //
1258 Link = ProtocolInterface->OpenList.ForwardLink;
1259 while (Link != &ProtocolInterface->OpenList) {
1260 OpenData = CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE);
1261 Link = Link->ForwardLink;
1262 if ((OpenData->AgentHandle == AgentHandle) && (OpenData->ControllerHandle == ControllerHandle)) {
1263 RemoveEntryList (&OpenData->Link);
1264 ProtocolInterface->OpenListCount--;
1265 CoreFreePool (OpenData);
1266 Status = EFI_SUCCESS;
1267 }
1268 }
1269
1270 Done:
1271 //
1272 // Done. Release the database lock and return.
1273 //
1274 CoreReleaseProtocolLock ();
1275 return Status;
1276 }
1277
1278
1279
1280
1281 /**
1282 Return information about Opened protocols in the system
1283
1284 @param UserHandle The handle to close the protocol interface on
1285 @param Protocol The ID of the protocol
1286 @param EntryBuffer A pointer to a buffer of open protocol information in the
1287 form of EFI_OPEN_PROTOCOL_INFORMATION_ENTRY structures.
1288 @param EntryCount Number of EntryBuffer entries
1289
1290 @retval EFI_SUCCESS The open protocol information was returned in EntryBuffer,
1291 and the number of entries was returned EntryCount.
1292 @retval EFI_NOT_FOUND Handle does not support the protocol specified by Protocol.
1293 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to allocate EntryBuffer.
1294
1295 **/
1296 EFI_STATUS
1297 EFIAPI
1298 CoreOpenProtocolInformation (
1299 IN EFI_HANDLE UserHandle,
1300 IN EFI_GUID *Protocol,
1301 OUT EFI_OPEN_PROTOCOL_INFORMATION_ENTRY **EntryBuffer,
1302 OUT UINTN *EntryCount
1303 )
1304 {
1305 EFI_STATUS Status;
1306 PROTOCOL_INTERFACE *ProtocolInterface;
1307 LIST_ENTRY *Link;
1308 OPEN_PROTOCOL_DATA *OpenData;
1309 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *Buffer;
1310 UINTN Count;
1311 UINTN Size;
1312
1313 *EntryBuffer = NULL;
1314 *EntryCount = 0;
1315
1316 //
1317 // Lock the protocol database
1318 //
1319 CoreAcquireProtocolLock ();
1320
1321 //
1322 // Look at each protocol interface for a match
1323 //
1324 Status = EFI_NOT_FOUND;
1325 ProtocolInterface = CoreGetProtocolInterface (UserHandle, Protocol);
1326 if (ProtocolInterface == NULL) {
1327 goto Done;
1328 }
1329
1330 //
1331 // Count the number of Open Entries
1332 //
1333 for ( Link = ProtocolInterface->OpenList.ForwardLink, Count = 0;
1334 (Link != &ProtocolInterface->OpenList) ;
1335 Link = Link->ForwardLink ) {
1336 Count++;
1337 }
1338
1339 ASSERT (Count == ProtocolInterface->OpenListCount);
1340
1341 if (Count == 0) {
1342 Size = sizeof(EFI_OPEN_PROTOCOL_INFORMATION_ENTRY);
1343 } else {
1344 Size = Count * sizeof(EFI_OPEN_PROTOCOL_INFORMATION_ENTRY);
1345 }
1346
1347 Buffer = AllocatePool (Size);
1348 if (Buffer == NULL) {
1349 Status = EFI_OUT_OF_RESOURCES;
1350 goto Done;
1351 }
1352
1353 Status = EFI_SUCCESS;
1354 for ( Link = ProtocolInterface->OpenList.ForwardLink, Count = 0;
1355 (Link != &ProtocolInterface->OpenList);
1356 Link = Link->ForwardLink, Count++ ) {
1357 OpenData = CR (Link, OPEN_PROTOCOL_DATA, Link, OPEN_PROTOCOL_DATA_SIGNATURE);
1358
1359 Buffer[Count].AgentHandle = OpenData->AgentHandle;
1360 Buffer[Count].ControllerHandle = OpenData->ControllerHandle;
1361 Buffer[Count].Attributes = OpenData->Attributes;
1362 Buffer[Count].OpenCount = OpenData->OpenCount;
1363 }
1364
1365 *EntryBuffer = Buffer;
1366 *EntryCount = Count;
1367
1368 Done:
1369 //
1370 // Done. Release the database lock.
1371 //
1372 CoreReleaseProtocolLock ();
1373 return Status;
1374 }
1375
1376
1377
1378
1379 /**
1380 Retrieves the list of protocol interface GUIDs that are installed on a handle in a buffer allocated
1381 from pool.
1382
1383 @param UserHandle The handle from which to retrieve the list of
1384 protocol interface GUIDs.
1385 @param ProtocolBuffer A pointer to the list of protocol interface GUID
1386 pointers that are installed on Handle.
1387 @param ProtocolBufferCount A pointer to the number of GUID pointers present
1388 in ProtocolBuffer.
1389
1390 @retval EFI_SUCCESS The list of protocol interface GUIDs installed
1391 on Handle was returned in ProtocolBuffer. The
1392 number of protocol interface GUIDs was returned
1393 in ProtocolBufferCount.
1394 @retval EFI_INVALID_PARAMETER Handle is NULL.
1395 @retval EFI_INVALID_PARAMETER Handle is not a valid EFI_HANDLE.
1396 @retval EFI_INVALID_PARAMETER ProtocolBuffer is NULL.
1397 @retval EFI_INVALID_PARAMETER ProtocolBufferCount is NULL.
1398 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the
1399 results.
1400
1401 **/
1402 EFI_STATUS
1403 EFIAPI
1404 CoreProtocolsPerHandle (
1405 IN EFI_HANDLE UserHandle,
1406 OUT EFI_GUID ***ProtocolBuffer,
1407 OUT UINTN *ProtocolBufferCount
1408 )
1409 {
1410 EFI_STATUS Status;
1411 IHANDLE *Handle;
1412 PROTOCOL_INTERFACE *Prot;
1413 LIST_ENTRY *Link;
1414 UINTN ProtocolCount;
1415 EFI_GUID **Buffer;
1416
1417 Status = CoreValidateHandle (UserHandle);
1418 if (EFI_ERROR (Status)) {
1419 return Status;
1420 }
1421
1422 Handle = (IHANDLE *)UserHandle;
1423
1424 if (ProtocolBuffer == NULL) {
1425 return EFI_INVALID_PARAMETER;
1426 }
1427
1428 if (ProtocolBufferCount == NULL) {
1429 return EFI_INVALID_PARAMETER;
1430 }
1431
1432 *ProtocolBufferCount = 0;
1433
1434 ProtocolCount = 0;
1435
1436 CoreAcquireProtocolLock ();
1437
1438 for (Link = Handle->Protocols.ForwardLink; Link != &Handle->Protocols; Link = Link->ForwardLink) {
1439 ProtocolCount++;
1440 }
1441
1442 //
1443 // If there are no protocol interfaces installed on Handle, then Handle is not a valid EFI_HANDLE
1444 //
1445 if (ProtocolCount == 0) {
1446 Status = EFI_INVALID_PARAMETER;
1447 goto Done;
1448 }
1449
1450 Buffer = AllocatePool (sizeof (EFI_GUID *) * ProtocolCount);
1451 if (Buffer == NULL) {
1452 Status = EFI_OUT_OF_RESOURCES;
1453 goto Done;
1454 }
1455
1456 *ProtocolBuffer = Buffer;
1457 *ProtocolBufferCount = ProtocolCount;
1458
1459 for ( Link = Handle->Protocols.ForwardLink, ProtocolCount = 0;
1460 Link != &Handle->Protocols;
1461 Link = Link->ForwardLink, ProtocolCount++) {
1462 Prot = CR(Link, PROTOCOL_INTERFACE, Link, PROTOCOL_INTERFACE_SIGNATURE);
1463 Buffer[ProtocolCount] = &(Prot->Protocol->ProtocolID);
1464 }
1465 Status = EFI_SUCCESS;
1466
1467 Done:
1468 CoreReleaseProtocolLock ();
1469 return Status;
1470 }
1471
1472
1473
1474 /**
1475 return handle database key.
1476
1477
1478 @return Handle database key.
1479
1480 **/
1481 UINT64
1482 CoreGetHandleDatabaseKey (
1483 VOID
1484 )
1485 {
1486 return gHandleDatabaseKey;
1487 }
1488
1489
1490
1491 /**
1492 Go connect any handles that were created or modified while a image executed.
1493
1494 @param Key The Key to show that the handle has been
1495 created/modified
1496
1497 **/
1498 VOID
1499 CoreConnectHandlesByKey (
1500 UINT64 Key
1501 )
1502 {
1503 UINTN Count;
1504 LIST_ENTRY *Link;
1505 EFI_HANDLE *HandleBuffer;
1506 IHANDLE *Handle;
1507 UINTN Index;
1508
1509 //
1510 // Lock the protocol database
1511 //
1512 CoreAcquireProtocolLock ();
1513
1514 for (Link = gHandleList.ForwardLink, Count = 0; Link != &gHandleList; Link = Link->ForwardLink) {
1515 Handle = CR (Link, IHANDLE, AllHandles, EFI_HANDLE_SIGNATURE);
1516 if (Handle->Key > Key) {
1517 Count++;
1518 }
1519 }
1520
1521 HandleBuffer = AllocatePool (Count * sizeof (EFI_HANDLE));
1522 if (HandleBuffer == NULL) {
1523 CoreReleaseProtocolLock ();
1524 return;
1525 }
1526
1527 for (Link = gHandleList.ForwardLink, Count = 0; Link != &gHandleList; Link = Link->ForwardLink) {
1528 Handle = CR (Link, IHANDLE, AllHandles, EFI_HANDLE_SIGNATURE);
1529 if (Handle->Key > Key) {
1530 HandleBuffer[Count++] = Handle;
1531 }
1532 }
1533
1534 //
1535 // Unlock the protocol database
1536 //
1537 CoreReleaseProtocolLock ();
1538
1539 //
1540 // Connect all handles whose Key value is greater than Key
1541 //
1542 for (Index = 0; Index < Count; Index++) {
1543 CoreConnectController (HandleBuffer[Index], NULL, NULL, TRUE);
1544 }
1545
1546 CoreFreePool(HandleBuffer);
1547 }