2 UEFI handle & protocol handling.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
14 // mProtocolDatabase - A list of all protocols in the system. (simple list for now)
15 // gHandleList - A list of all the handles in the system
16 // gProtocolDatabaseLock - Lock to protect the mProtocolDatabase
17 // gHandleDatabaseKey - The Key to show that the handle has been created/modified
19 LIST_ENTRY mProtocolDatabase
= INITIALIZE_LIST_HEAD_VARIABLE (mProtocolDatabase
);
20 LIST_ENTRY gHandleList
= INITIALIZE_LIST_HEAD_VARIABLE (gHandleList
);
21 EFI_LOCK gProtocolDatabaseLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY
);
22 UINT64 gHandleDatabaseKey
= 0;
27 Acquire lock on gProtocolDatabaseLock.
31 CoreAcquireProtocolLock (
35 CoreAcquireLock (&gProtocolDatabaseLock
);
41 Release lock on gProtocolDatabaseLock.
45 CoreReleaseProtocolLock (
49 CoreReleaseLock (&gProtocolDatabaseLock
);
55 Check whether a handle is a valid EFI_HANDLE
57 @param UserHandle The handle to check
59 @retval EFI_INVALID_PARAMETER The handle is NULL or not a valid EFI_HANDLE.
60 @retval EFI_SUCCESS The handle is valid EFI_HANDLE.
65 IN EFI_HANDLE UserHandle
71 if (UserHandle
== NULL
) {
72 return EFI_INVALID_PARAMETER
;
75 for (Link
= gHandleList
.BackLink
; Link
!= &gHandleList
; Link
= Link
->BackLink
) {
76 Handle
= CR (Link
, IHANDLE
, AllHandles
, EFI_HANDLE_SIGNATURE
);
77 if (Handle
== (IHANDLE
*) UserHandle
) {
82 return EFI_INVALID_PARAMETER
;
88 Finds the protocol entry for the requested protocol.
89 The gProtocolDatabaseLock must be owned
91 @param Protocol The ID of the protocol
92 @param Create Create a new entry if not found
94 @return Protocol entry
98 CoreFindProtocolEntry (
99 IN EFI_GUID
*Protocol
,
104 PROTOCOL_ENTRY
*Item
;
105 PROTOCOL_ENTRY
*ProtEntry
;
107 ASSERT_LOCKED(&gProtocolDatabaseLock
);
110 // Search the database for the matching GUID
114 for (Link
= mProtocolDatabase
.ForwardLink
;
115 Link
!= &mProtocolDatabase
;
116 Link
= Link
->ForwardLink
) {
118 Item
= CR(Link
, PROTOCOL_ENTRY
, AllEntries
, PROTOCOL_ENTRY_SIGNATURE
);
119 if (CompareGuid (&Item
->ProtocolID
, Protocol
)) {
122 // This is the protocol entry
131 // If the protocol entry was not found and Create is TRUE, then
132 // allocate a new entry
134 if ((ProtEntry
== NULL
) && Create
) {
135 ProtEntry
= AllocatePool (sizeof(PROTOCOL_ENTRY
));
137 if (ProtEntry
!= NULL
) {
139 // Initialize new protocol entry structure
141 ProtEntry
->Signature
= PROTOCOL_ENTRY_SIGNATURE
;
142 CopyGuid ((VOID
*)&ProtEntry
->ProtocolID
, Protocol
);
143 InitializeListHead (&ProtEntry
->Protocols
);
144 InitializeListHead (&ProtEntry
->Notify
);
147 // Add it to protocol database
149 InsertTailList (&mProtocolDatabase
, &ProtEntry
->AllEntries
);
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.
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
167 @return Protocol instance (NULL: Not found)
171 CoreFindProtocolInterface (
173 IN EFI_GUID
*Protocol
,
177 PROTOCOL_INTERFACE
*Prot
;
178 PROTOCOL_ENTRY
*ProtEntry
;
181 ASSERT_LOCKED(&gProtocolDatabaseLock
);
185 // Lookup the protocol entry for this protocol ID
188 ProtEntry
= CoreFindProtocolEntry (Protocol
, FALSE
);
189 if (ProtEntry
!= NULL
) {
192 // Look at each protocol interface for any matches
194 for (Link
= Handle
->Protocols
.ForwardLink
; Link
!= &Handle
->Protocols
; Link
=Link
->ForwardLink
) {
197 // If this protocol interface matches, remove it
199 Prot
= CR(Link
, PROTOCOL_INTERFACE
, Link
, PROTOCOL_INTERFACE_SIGNATURE
);
200 if (Prot
->Interface
== Interface
&& Prot
->Protocol
== ProtEntry
) {
213 Removes an event from a register protocol notify list on a protocol.
215 @param Event The event to search for in the protocol
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.
223 CoreUnregisterProtocolNotifyEvent (
228 PROTOCOL_ENTRY
*ProtEntry
;
229 LIST_ENTRY
*NotifyLink
;
230 PROTOCOL_NOTIFY
*ProtNotify
;
232 CoreAcquireProtocolLock ();
234 for ( Link
= mProtocolDatabase
.ForwardLink
;
235 Link
!= &mProtocolDatabase
;
236 Link
= Link
->ForwardLink
) {
238 ProtEntry
= CR(Link
, PROTOCOL_ENTRY
, AllEntries
, PROTOCOL_ENTRY_SIGNATURE
);
240 for ( NotifyLink
= ProtEntry
->Notify
.ForwardLink
;
241 NotifyLink
!= &ProtEntry
->Notify
;
242 NotifyLink
= NotifyLink
->ForwardLink
) {
244 ProtNotify
= CR(NotifyLink
, PROTOCOL_NOTIFY
, Link
, PROTOCOL_NOTIFY_SIGNATURE
);
246 if (ProtNotify
->Event
== Event
) {
247 RemoveEntryList(&ProtNotify
->Link
);
248 CoreFreePool(ProtNotify
);
249 CoreReleaseProtocolLock ();
255 CoreReleaseProtocolLock ();
256 return EFI_NOT_FOUND
;
262 Removes all the events in the protocol database that match Event.
264 @param Event The event to search for in the protocol
267 @return EFI_SUCCESS when done searching the entire database.
271 CoreUnregisterProtocolNotify (
278 Status
= CoreUnregisterProtocolNotifyEvent (Event
);
279 } while (!EFI_ERROR (Status
));
288 Wrapper function to CoreInstallProtocolInterfaceNotify. This is the public API which
289 Calls the private one which contains a BOOLEAN parameter for notifications
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
296 @param Interface The interface for the protocol being added
303 CoreInstallProtocolInterface (
304 IN OUT EFI_HANDLE
*UserHandle
,
305 IN EFI_GUID
*Protocol
,
306 IN EFI_INTERFACE_TYPE InterfaceType
,
310 return CoreInstallProtocolInterfaceNotify (
321 Installs a protocol interface into the boot services environment.
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
328 @param Interface The interface for the protocol being added
329 @param Notify indicates whether notify the notification list
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
338 CoreInstallProtocolInterfaceNotify (
339 IN OUT EFI_HANDLE
*UserHandle
,
340 IN EFI_GUID
*Protocol
,
341 IN EFI_INTERFACE_TYPE InterfaceType
,
346 PROTOCOL_INTERFACE
*Prot
;
347 PROTOCOL_ENTRY
*ProtEntry
;
350 VOID
*ExistingInterface
;
353 // returns EFI_INVALID_PARAMETER if InterfaceType is invalid.
354 // Also added check for invalid UserHandle and Protocol pointers.
356 if (UserHandle
== NULL
|| Protocol
== NULL
) {
357 return EFI_INVALID_PARAMETER
;
360 if (InterfaceType
!= EFI_NATIVE_INTERFACE
) {
361 return EFI_INVALID_PARAMETER
;
365 // Print debug message
367 DEBUG((DEBUG_INFO
, "InstallProtocolInterface: %g %p\n", Protocol
, Interface
));
369 Status
= EFI_OUT_OF_RESOURCES
;
373 if (*UserHandle
!= NULL
) {
374 Status
= CoreHandleProtocol (*UserHandle
, Protocol
, (VOID
**)&ExistingInterface
);
375 if (!EFI_ERROR (Status
)) {
376 return EFI_INVALID_PARAMETER
;
381 // Lock the protocol database
383 CoreAcquireProtocolLock ();
386 // Lookup the Protocol Entry for the requested protocol
388 ProtEntry
= CoreFindProtocolEntry (Protocol
, TRUE
);
389 if (ProtEntry
== NULL
) {
394 // Allocate a new protocol interface structure
396 Prot
= AllocateZeroPool (sizeof(PROTOCOL_INTERFACE
));
398 Status
= EFI_OUT_OF_RESOURCES
;
403 // If caller didn't supply a handle, allocate a new one
405 Handle
= (IHANDLE
*)*UserHandle
;
406 if (Handle
== NULL
) {
407 Handle
= AllocateZeroPool (sizeof(IHANDLE
));
408 if (Handle
== NULL
) {
409 Status
= EFI_OUT_OF_RESOURCES
;
414 // Initialize new handler structure
416 Handle
->Signature
= EFI_HANDLE_SIGNATURE
;
417 InitializeListHead (&Handle
->Protocols
);
420 // Initialize the Key to show that the handle has been created/modified
422 gHandleDatabaseKey
++;
423 Handle
->Key
= gHandleDatabaseKey
;
426 // Add this handle to the list global list of all handles
429 InsertTailList (&gHandleList
, &Handle
->AllHandles
);
431 Status
= CoreValidateHandle (Handle
);
432 if (EFI_ERROR (Status
)) {
433 DEBUG((DEBUG_ERROR
, "InstallProtocolInterface: input handle at 0x%x is invalid\n", Handle
));
439 // Each interface that is added must be unique
441 ASSERT (CoreFindProtocolInterface (Handle
, Protocol
, Interface
) == NULL
);
444 // Initialize the protocol interface structure
446 Prot
->Signature
= PROTOCOL_INTERFACE_SIGNATURE
;
447 Prot
->Handle
= Handle
;
448 Prot
->Protocol
= ProtEntry
;
449 Prot
->Interface
= Interface
;
452 // Initalize OpenProtocol Data base
454 InitializeListHead (&Prot
->OpenList
);
455 Prot
->OpenListCount
= 0;
458 // Add this protocol interface to the head of the supported
459 // protocol list for this handle
461 InsertHeadList (&Handle
->Protocols
, &Prot
->Link
);
464 // Add this protocol interface to the tail of the
467 InsertTailList (&ProtEntry
->Protocols
, &Prot
->ByProtocol
);
470 // Notify the notification list for this protocol
473 CoreNotifyProtocolEntry (ProtEntry
);
475 Status
= EFI_SUCCESS
;
479 // Done, unlock the database and return
481 CoreReleaseProtocolLock ();
482 if (!EFI_ERROR (Status
)) {
484 // Return the new handle back to the caller
486 *UserHandle
= Handle
;
489 // There was an error, clean up
494 DEBUG((DEBUG_ERROR
, "InstallProtocolInterface: %g %p failed with %r\n", Protocol
, Interface
, Status
));
504 Installs a list of protocol interface into the boot services environment.
505 This function calls InstallProtocolInterface() in a loop. If any error
506 occures all the protocols added by this function are removed. This is
507 basically a lib function to save space.
509 @param Handle The pointer to a handle to install the new
510 protocol interfaces on, or a pointer to NULL
511 if a new handle is to be allocated.
512 @param ... EFI_GUID followed by protocol instance. A NULL
513 terminates the list. The pairs are the
514 arguments to InstallProtocolInterface(). All the
515 protocols are added to Handle.
517 @retval EFI_SUCCESS All the protocol interface was installed.
518 @retval EFI_OUT_OF_RESOURCES There was not enough memory in pool to install all the protocols.
519 @retval EFI_ALREADY_STARTED A Device Path Protocol instance was passed in that is already present in
521 @retval EFI_INVALID_PARAMETER Handle is NULL.
522 @retval EFI_INVALID_PARAMETER Protocol is already installed on the handle specified by Handle.
527 CoreInstallMultipleProtocolInterfaces (
528 IN OUT EFI_HANDLE
*Handle
,
538 EFI_HANDLE OldHandle
;
539 EFI_HANDLE DeviceHandle
;
540 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
542 if (Handle
== NULL
) {
543 return EFI_INVALID_PARAMETER
;
547 // Syncronize with notifcations.
549 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
553 // Check for duplicate device path and install the protocol interfaces
555 VA_START (Args
, Handle
);
556 for (Index
= 0, Status
= EFI_SUCCESS
; !EFI_ERROR (Status
); Index
++) {
558 // If protocol is NULL, then it's the end of the list
560 Protocol
= VA_ARG (Args
, EFI_GUID
*);
561 if (Protocol
== NULL
) {
565 Interface
= VA_ARG (Args
, VOID
*);
568 // Make sure you are installing on top a device path that has already been added.
570 if (CompareGuid (Protocol
, &gEfiDevicePathProtocolGuid
)) {
572 DevicePath
= Interface
;
573 Status
= CoreLocateDevicePath (&gEfiDevicePathProtocolGuid
, &DevicePath
, &DeviceHandle
);
574 if (!EFI_ERROR (Status
) && (DeviceHandle
!= NULL
) && IsDevicePathEnd(DevicePath
)) {
575 Status
= EFI_ALREADY_STARTED
;
583 Status
= CoreInstallProtocolInterface (Handle
, Protocol
, EFI_NATIVE_INTERFACE
, Interface
);
588 // If there was an error, remove all the interfaces that were installed without any errors
590 if (EFI_ERROR (Status
)) {
592 // Reset the va_arg back to the first argument.
594 VA_START (Args
, Handle
);
595 for (; Index
> 1; Index
--) {
596 Protocol
= VA_ARG (Args
, EFI_GUID
*);
597 Interface
= VA_ARG (Args
, VOID
*);
598 CoreUninstallProtocolInterface (*Handle
, Protocol
, Interface
);
608 CoreRestoreTpl (OldTpl
);
614 Attempts to disconnect all drivers that are using the protocol interface being queried.
615 If failed, reconnect all drivers disconnected.
616 Note: This function doesn't do parameters checking, it's caller's responsibility
617 to pass in valid parameters.
619 @param UserHandle The handle on which the protocol is installed
620 @param Prot The protocol to disconnect drivers from
622 @retval EFI_SUCCESS Drivers using the protocol interface are all
624 @retval EFI_ACCESS_DENIED Failed to disconnect one or all of the drivers
628 CoreDisconnectControllersUsingProtocolInterface (
629 IN EFI_HANDLE UserHandle
,
630 IN PROTOCOL_INTERFACE
*Prot
636 OPEN_PROTOCOL_DATA
*OpenData
;
638 Status
= EFI_SUCCESS
;
641 // Attempt to disconnect all drivers from this protocol interface
645 for (Link
= Prot
->OpenList
.ForwardLink
; Link
!= &Prot
->OpenList
; Link
= Link
->ForwardLink
) {
646 OpenData
= CR (Link
, OPEN_PROTOCOL_DATA
, Link
, OPEN_PROTOCOL_DATA_SIGNATURE
);
647 if ((OpenData
->Attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) != 0) {
648 CoreReleaseProtocolLock ();
649 Status
= CoreDisconnectController (UserHandle
, OpenData
->AgentHandle
, NULL
);
650 CoreAcquireProtocolLock ();
651 if (!EFI_ERROR (Status
)) {
659 if (!EFI_ERROR (Status
)) {
661 // Attempt to remove BY_HANDLE_PROTOOCL and GET_PROTOCOL and TEST_PROTOCOL Open List items
663 for (Link
= Prot
->OpenList
.ForwardLink
; Link
!= &Prot
->OpenList
;) {
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
)) != 0) {
667 Link
= RemoveEntryList (&OpenData
->Link
);
668 Prot
->OpenListCount
--;
669 CoreFreePool (OpenData
);
671 Link
= Link
->ForwardLink
;
677 // If there are errors or still has open items in the list, then reconnect all the drivers and return an error
679 if (EFI_ERROR (Status
) || (Prot
->OpenListCount
> 0)) {
680 CoreReleaseProtocolLock ();
681 CoreConnectController (UserHandle
, NULL
, NULL
, TRUE
);
682 CoreAcquireProtocolLock ();
683 Status
= EFI_ACCESS_DENIED
;
692 Uninstalls all instances of a protocol:interfacer from a handle.
693 If the last protocol interface is remove from the handle, the
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
700 @retval EFI_INVALID_PARAMETER Protocol is NULL.
701 @retval EFI_SUCCESS Protocol interface successfully uninstalled.
706 CoreUninstallProtocolInterface (
707 IN EFI_HANDLE UserHandle
,
708 IN EFI_GUID
*Protocol
,
714 PROTOCOL_INTERFACE
*Prot
;
717 // Check that Protocol is valid
719 if (Protocol
== NULL
) {
720 return EFI_INVALID_PARAMETER
;
724 // Check that UserHandle is a valid handle
726 Status
= CoreValidateHandle (UserHandle
);
727 if (EFI_ERROR (Status
)) {
732 // Lock the protocol database
734 CoreAcquireProtocolLock ();
737 // Check that Protocol exists on UserHandle, and Interface matches the interface in the database
739 Prot
= CoreFindProtocolInterface (UserHandle
, Protocol
, Interface
);
741 Status
= EFI_NOT_FOUND
;
746 // Attempt to disconnect all drivers that are using the protocol interface that is about to be removed
748 Status
= CoreDisconnectControllersUsingProtocolInterface (
752 if (EFI_ERROR (Status
)) {
754 // One or more drivers refused to release, so return the error
760 // Remove the protocol interface from the protocol
762 Status
= EFI_NOT_FOUND
;
763 Handle
= (IHANDLE
*)UserHandle
;
764 Prot
= CoreRemoveInterfaceFromProtocol (Handle
, Protocol
, Interface
);
768 // Update the Key to show that the handle has been created/modified
770 gHandleDatabaseKey
++;
771 Handle
->Key
= gHandleDatabaseKey
;
774 // Remove the protocol interface from the handle
776 RemoveEntryList (&Prot
->Link
);
783 Status
= EFI_SUCCESS
;
787 // If there are no more handlers for the handle, free the handle
789 if (IsListEmpty (&Handle
->Protocols
)) {
790 Handle
->Signature
= 0;
791 RemoveEntryList (&Handle
->AllHandles
);
792 CoreFreePool (Handle
);
797 // Done, unlock the database and return
799 CoreReleaseProtocolLock ();
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.
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.
822 CoreUninstallMultipleProtocolInterfaces (
823 IN EFI_HANDLE Handle
,
833 VA_START (Args
, Handle
);
834 for (Index
= 0, Status
= EFI_SUCCESS
; !EFI_ERROR (Status
); Index
++) {
836 // If protocol is NULL, then it's the end of the list
838 Protocol
= VA_ARG (Args
, EFI_GUID
*);
839 if (Protocol
== NULL
) {
843 Interface
= VA_ARG (Args
, VOID
*);
848 Status
= CoreUninstallProtocolInterface (Handle
, Protocol
, Interface
);
853 // If there was an error, add all the interfaces that were
854 // uninstalled without any errors
856 if (EFI_ERROR (Status
)) {
858 // Reset the va_arg back to the first argument.
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
);
874 Locate a certain GUID protocol interface in a Handle's protocols.
876 @param UserHandle The handle to obtain the protocol interface on
877 @param Protocol The GUID of the protocol
879 @return The requested protocol interface for the handle
883 CoreGetProtocolInterface (
884 IN EFI_HANDLE UserHandle
,
885 IN EFI_GUID
*Protocol
889 PROTOCOL_ENTRY
*ProtEntry
;
890 PROTOCOL_INTERFACE
*Prot
;
894 Status
= CoreValidateHandle (UserHandle
);
895 if (EFI_ERROR (Status
)) {
899 Handle
= (IHANDLE
*)UserHandle
;
902 // Look at each protocol interface for a match
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
)) {
917 Queries a handle to determine if it supports a specified protocol.
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.
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 NULL..
927 @retval EFI_INVALID_PARAMETER Protocol is NULL.
928 @retval EFI_INVALID_PARAMETER Interface is NULL.
934 IN EFI_HANDLE UserHandle
,
935 IN EFI_GUID
*Protocol
,
939 return CoreOpenProtocol (
945 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
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.
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
962 @param ControllerHandle The controller handle that is requiring this
964 @param Attributes The open mode of the protocol interface
965 specified by Handle and Protocol.
967 @retval EFI_INVALID_PARAMETER Protocol is NULL.
968 @retval EFI_SUCCESS Get the protocol interface.
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
,
983 PROTOCOL_INTERFACE
*Prot
;
985 OPEN_PROTOCOL_DATA
*OpenData
;
992 // Check for invalid Protocol
994 if (Protocol
== NULL
) {
995 return EFI_INVALID_PARAMETER
;
999 // Check for invalid Interface
1001 if ((Attributes
!= EFI_OPEN_PROTOCOL_TEST_PROTOCOL
) && (Interface
== NULL
)) {
1002 return EFI_INVALID_PARAMETER
;
1006 // Check for invalid UserHandle
1008 Status
= CoreValidateHandle (UserHandle
);
1009 if (EFI_ERROR (Status
)) {
1014 // Check for invalid Attributes
1016 switch (Attributes
) {
1017 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
:
1018 Status
= CoreValidateHandle (ImageHandle
);
1019 if (EFI_ERROR (Status
)) {
1022 Status
= CoreValidateHandle (ControllerHandle
);
1023 if (EFI_ERROR (Status
)) {
1026 if (UserHandle
== ControllerHandle
) {
1027 return EFI_INVALID_PARAMETER
;
1030 case EFI_OPEN_PROTOCOL_BY_DRIVER
:
1031 case EFI_OPEN_PROTOCOL_BY_DRIVER
| EFI_OPEN_PROTOCOL_EXCLUSIVE
:
1032 Status
= CoreValidateHandle (ImageHandle
);
1033 if (EFI_ERROR (Status
)) {
1036 Status
= CoreValidateHandle (ControllerHandle
);
1037 if (EFI_ERROR (Status
)) {
1041 case EFI_OPEN_PROTOCOL_EXCLUSIVE
:
1042 Status
= CoreValidateHandle (ImageHandle
);
1043 if (EFI_ERROR (Status
)) {
1047 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
:
1048 case EFI_OPEN_PROTOCOL_GET_PROTOCOL
:
1049 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL
:
1052 return EFI_INVALID_PARAMETER
;
1056 // Lock the protocol database
1058 CoreAcquireProtocolLock ();
1061 // Look at each protocol interface for a match
1063 Prot
= CoreGetProtocolInterface (UserHandle
, Protocol
);
1065 Status
= EFI_UNSUPPORTED
;
1069 Status
= EFI_SUCCESS
;
1073 for ( Link
= Prot
->OpenList
.ForwardLink
; Link
!= &Prot
->OpenList
; Link
= Link
->ForwardLink
) {
1074 OpenData
= CR (Link
, OPEN_PROTOCOL_DATA
, Link
, OPEN_PROTOCOL_DATA_SIGNATURE
);
1075 ExactMatch
= (BOOLEAN
)((OpenData
->AgentHandle
== ImageHandle
) &&
1076 (OpenData
->Attributes
== Attributes
) &&
1077 (OpenData
->ControllerHandle
== ControllerHandle
));
1078 if ((OpenData
->Attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) != 0) {
1081 Status
= EFI_ALREADY_STARTED
;
1085 if ((OpenData
->Attributes
& EFI_OPEN_PROTOCOL_EXCLUSIVE
) != 0) {
1087 } else if (ExactMatch
) {
1088 OpenData
->OpenCount
++;
1089 Status
= EFI_SUCCESS
;
1095 // ByDriver TRUE -> A driver is managing (UserHandle, Protocol)
1096 // ByDriver FALSE -> There are no drivers managing (UserHandle, Protocol)
1097 // Exclusive TRUE -> Something has exclusive access to (UserHandle, Protocol)
1098 // Exclusive FALSE -> Nothing has exclusive access to (UserHandle, Protocol)
1101 switch (Attributes
) {
1102 case EFI_OPEN_PROTOCOL_BY_DRIVER
:
1103 if (Exclusive
|| ByDriver
) {
1104 Status
= EFI_ACCESS_DENIED
;
1108 case EFI_OPEN_PROTOCOL_BY_DRIVER
| EFI_OPEN_PROTOCOL_EXCLUSIVE
:
1109 case EFI_OPEN_PROTOCOL_EXCLUSIVE
:
1111 Status
= EFI_ACCESS_DENIED
;
1117 for (Link
= Prot
->OpenList
.ForwardLink
; Link
!= &Prot
->OpenList
; Link
= Link
->ForwardLink
) {
1118 OpenData
= CR (Link
, OPEN_PROTOCOL_DATA
, Link
, OPEN_PROTOCOL_DATA_SIGNATURE
);
1119 if ((OpenData
->Attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) != 0) {
1121 CoreReleaseProtocolLock ();
1122 Status
= CoreDisconnectController (UserHandle
, OpenData
->AgentHandle
, NULL
);
1123 CoreAcquireProtocolLock ();
1124 if (EFI_ERROR (Status
)) {
1125 Status
= EFI_ACCESS_DENIED
;
1132 } while (Disconnect
);
1135 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
:
1136 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
:
1137 case EFI_OPEN_PROTOCOL_GET_PROTOCOL
:
1138 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL
:
1142 if (ImageHandle
== NULL
) {
1143 Status
= EFI_SUCCESS
;
1149 OpenData
= AllocatePool (sizeof(OPEN_PROTOCOL_DATA
));
1150 if (OpenData
== NULL
) {
1151 Status
= EFI_OUT_OF_RESOURCES
;
1153 OpenData
->Signature
= OPEN_PROTOCOL_DATA_SIGNATURE
;
1154 OpenData
->AgentHandle
= ImageHandle
;
1155 OpenData
->ControllerHandle
= ControllerHandle
;
1156 OpenData
->Attributes
= Attributes
;
1157 OpenData
->OpenCount
= 1;
1158 InsertTailList (&Prot
->OpenList
, &OpenData
->Link
);
1159 Prot
->OpenListCount
++;
1160 Status
= EFI_SUCCESS
;
1165 if (Attributes
!= EFI_OPEN_PROTOCOL_TEST_PROTOCOL
) {
1167 // Keep Interface unmodified in case of any Error
1168 // except EFI_ALREADY_STARTED and EFI_UNSUPPORTED.
1170 if (!EFI_ERROR (Status
) || Status
== EFI_ALREADY_STARTED
) {
1172 // According to above logic, if 'Prot' is NULL, then the 'Status' must be
1173 // EFI_UNSUPPORTED. Here the 'Status' is not EFI_UNSUPPORTED, so 'Prot'
1174 // must be not NULL.
1176 // The ASSERT here is for addressing a false positive NULL pointer
1177 // dereference issue raised from static analysis.
1179 ASSERT (Prot
!= NULL
);
1181 // EFI_ALREADY_STARTED is not an error for bus driver.
1182 // Return the corresponding protocol interface.
1184 *Interface
= Prot
->Interface
;
1185 } else if (Status
== EFI_UNSUPPORTED
) {
1187 // Return NULL Interface if Unsupported Protocol.
1194 // Done. Release the database lock and return
1196 CoreReleaseProtocolLock ();
1203 Closes a protocol on a handle that was opened using OpenProtocol().
1205 @param UserHandle The handle for the protocol interface that was
1206 previously opened with OpenProtocol(), and is
1208 @param Protocol The published unique identifier of the protocol.
1209 It is the caller's responsibility to pass in a
1211 @param AgentHandle The handle of the agent that is closing the
1213 @param ControllerHandle If the agent that opened a protocol is a driver
1214 that follows the EFI Driver Model, then this
1215 parameter is the controller handle that required
1216 the protocol interface. If the agent does not
1217 follow the EFI Driver Model, then this parameter
1218 is optional and may be NULL.
1220 @retval EFI_SUCCESS The protocol instance was closed.
1221 @retval EFI_INVALID_PARAMETER Handle, AgentHandle or ControllerHandle is not a
1223 @retval EFI_NOT_FOUND Can not find the specified protocol or
1230 IN EFI_HANDLE UserHandle
,
1231 IN EFI_GUID
*Protocol
,
1232 IN EFI_HANDLE AgentHandle
,
1233 IN EFI_HANDLE ControllerHandle
1237 PROTOCOL_INTERFACE
*ProtocolInterface
;
1239 OPEN_PROTOCOL_DATA
*OpenData
;
1242 // Check for invalid parameters
1244 Status
= CoreValidateHandle (UserHandle
);
1245 if (EFI_ERROR (Status
)) {
1248 Status
= CoreValidateHandle (AgentHandle
);
1249 if (EFI_ERROR (Status
)) {
1252 if (ControllerHandle
!= NULL
) {
1253 Status
= CoreValidateHandle (ControllerHandle
);
1254 if (EFI_ERROR (Status
)) {
1258 if (Protocol
== NULL
) {
1259 return EFI_INVALID_PARAMETER
;
1263 // Lock the protocol database
1265 CoreAcquireProtocolLock ();
1268 // Look at each protocol interface for a match
1270 Status
= EFI_NOT_FOUND
;
1271 ProtocolInterface
= CoreGetProtocolInterface (UserHandle
, Protocol
);
1272 if (ProtocolInterface
== NULL
) {
1277 // Walk the Open data base looking for AgentHandle
1279 Link
= ProtocolInterface
->OpenList
.ForwardLink
;
1280 while (Link
!= &ProtocolInterface
->OpenList
) {
1281 OpenData
= CR (Link
, OPEN_PROTOCOL_DATA
, Link
, OPEN_PROTOCOL_DATA_SIGNATURE
);
1282 Link
= Link
->ForwardLink
;
1283 if ((OpenData
->AgentHandle
== AgentHandle
) && (OpenData
->ControllerHandle
== ControllerHandle
)) {
1284 RemoveEntryList (&OpenData
->Link
);
1285 ProtocolInterface
->OpenListCount
--;
1286 CoreFreePool (OpenData
);
1287 Status
= EFI_SUCCESS
;
1293 // Done. Release the database lock and return.
1295 CoreReleaseProtocolLock ();
1303 Return information about Opened protocols in the system
1305 @param UserHandle The handle to close the protocol interface on
1306 @param Protocol The ID of the protocol
1307 @param EntryBuffer A pointer to a buffer of open protocol information in the
1308 form of EFI_OPEN_PROTOCOL_INFORMATION_ENTRY structures.
1309 @param EntryCount Number of EntryBuffer entries
1311 @retval EFI_SUCCESS The open protocol information was returned in EntryBuffer,
1312 and the number of entries was returned EntryCount.
1313 @retval EFI_NOT_FOUND Handle does not support the protocol specified by Protocol.
1314 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to allocate EntryBuffer.
1319 CoreOpenProtocolInformation (
1320 IN EFI_HANDLE UserHandle
,
1321 IN EFI_GUID
*Protocol
,
1322 OUT EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
**EntryBuffer
,
1323 OUT UINTN
*EntryCount
1327 PROTOCOL_INTERFACE
*ProtocolInterface
;
1329 OPEN_PROTOCOL_DATA
*OpenData
;
1330 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*Buffer
;
1334 *EntryBuffer
= NULL
;
1338 // Lock the protocol database
1340 CoreAcquireProtocolLock ();
1343 // Look at each protocol interface for a match
1345 Status
= EFI_NOT_FOUND
;
1346 ProtocolInterface
= CoreGetProtocolInterface (UserHandle
, Protocol
);
1347 if (ProtocolInterface
== NULL
) {
1352 // Count the number of Open Entries
1354 for ( Link
= ProtocolInterface
->OpenList
.ForwardLink
, Count
= 0;
1355 (Link
!= &ProtocolInterface
->OpenList
) ;
1356 Link
= Link
->ForwardLink
) {
1360 ASSERT (Count
== ProtocolInterface
->OpenListCount
);
1363 Size
= sizeof(EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
);
1365 Size
= Count
* sizeof(EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
);
1368 Buffer
= AllocatePool (Size
);
1369 if (Buffer
== NULL
) {
1370 Status
= EFI_OUT_OF_RESOURCES
;
1374 Status
= EFI_SUCCESS
;
1375 for ( Link
= ProtocolInterface
->OpenList
.ForwardLink
, Count
= 0;
1376 (Link
!= &ProtocolInterface
->OpenList
);
1377 Link
= Link
->ForwardLink
, Count
++ ) {
1378 OpenData
= CR (Link
, OPEN_PROTOCOL_DATA
, Link
, OPEN_PROTOCOL_DATA_SIGNATURE
);
1380 Buffer
[Count
].AgentHandle
= OpenData
->AgentHandle
;
1381 Buffer
[Count
].ControllerHandle
= OpenData
->ControllerHandle
;
1382 Buffer
[Count
].Attributes
= OpenData
->Attributes
;
1383 Buffer
[Count
].OpenCount
= OpenData
->OpenCount
;
1386 *EntryBuffer
= Buffer
;
1387 *EntryCount
= Count
;
1391 // Done. Release the database lock.
1393 CoreReleaseProtocolLock ();
1401 Retrieves the list of protocol interface GUIDs that are installed on a handle in a buffer allocated
1404 @param UserHandle The handle from which to retrieve the list of
1405 protocol interface GUIDs.
1406 @param ProtocolBuffer A pointer to the list of protocol interface GUID
1407 pointers that are installed on Handle.
1408 @param ProtocolBufferCount A pointer to the number of GUID pointers present
1411 @retval EFI_SUCCESS The list of protocol interface GUIDs installed
1412 on Handle was returned in ProtocolBuffer. The
1413 number of protocol interface GUIDs was returned
1414 in ProtocolBufferCount.
1415 @retval EFI_INVALID_PARAMETER Handle is NULL.
1416 @retval EFI_INVALID_PARAMETER Handle is not a valid EFI_HANDLE.
1417 @retval EFI_INVALID_PARAMETER ProtocolBuffer is NULL.
1418 @retval EFI_INVALID_PARAMETER ProtocolBufferCount is NULL.
1419 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the
1425 CoreProtocolsPerHandle (
1426 IN EFI_HANDLE UserHandle
,
1427 OUT EFI_GUID
***ProtocolBuffer
,
1428 OUT UINTN
*ProtocolBufferCount
1433 PROTOCOL_INTERFACE
*Prot
;
1435 UINTN ProtocolCount
;
1438 Status
= CoreValidateHandle (UserHandle
);
1439 if (EFI_ERROR (Status
)) {
1443 Handle
= (IHANDLE
*)UserHandle
;
1445 if (ProtocolBuffer
== NULL
) {
1446 return EFI_INVALID_PARAMETER
;
1449 if (ProtocolBufferCount
== NULL
) {
1450 return EFI_INVALID_PARAMETER
;
1453 *ProtocolBufferCount
= 0;
1457 CoreAcquireProtocolLock ();
1459 for (Link
= Handle
->Protocols
.ForwardLink
; Link
!= &Handle
->Protocols
; Link
= Link
->ForwardLink
) {
1464 // If there are no protocol interfaces installed on Handle, then Handle is not a valid EFI_HANDLE
1466 if (ProtocolCount
== 0) {
1467 Status
= EFI_INVALID_PARAMETER
;
1471 Buffer
= AllocatePool (sizeof (EFI_GUID
*) * ProtocolCount
);
1472 if (Buffer
== NULL
) {
1473 Status
= EFI_OUT_OF_RESOURCES
;
1477 *ProtocolBuffer
= Buffer
;
1478 *ProtocolBufferCount
= ProtocolCount
;
1480 for ( Link
= Handle
->Protocols
.ForwardLink
, ProtocolCount
= 0;
1481 Link
!= &Handle
->Protocols
;
1482 Link
= Link
->ForwardLink
, ProtocolCount
++) {
1483 Prot
= CR(Link
, PROTOCOL_INTERFACE
, Link
, PROTOCOL_INTERFACE_SIGNATURE
);
1484 Buffer
[ProtocolCount
] = &(Prot
->Protocol
->ProtocolID
);
1486 Status
= EFI_SUCCESS
;
1489 CoreReleaseProtocolLock ();
1496 return handle database key.
1499 @return Handle database key.
1503 CoreGetHandleDatabaseKey (
1507 return gHandleDatabaseKey
;
1513 Go connect any handles that were created or modified while a image executed.
1515 @param Key The Key to show that the handle has been
1520 CoreConnectHandlesByKey (
1526 EFI_HANDLE
*HandleBuffer
;
1531 // Lock the protocol database
1533 CoreAcquireProtocolLock ();
1535 for (Link
= gHandleList
.ForwardLink
, Count
= 0; Link
!= &gHandleList
; Link
= Link
->ForwardLink
) {
1536 Handle
= CR (Link
, IHANDLE
, AllHandles
, EFI_HANDLE_SIGNATURE
);
1537 if (Handle
->Key
> Key
) {
1542 HandleBuffer
= AllocatePool (Count
* sizeof (EFI_HANDLE
));
1543 if (HandleBuffer
== NULL
) {
1544 CoreReleaseProtocolLock ();
1548 for (Link
= gHandleList
.ForwardLink
, Count
= 0; Link
!= &gHandleList
; Link
= Link
->ForwardLink
) {
1549 Handle
= CR (Link
, IHANDLE
, AllHandles
, EFI_HANDLE_SIGNATURE
);
1550 if (Handle
->Key
> Key
) {
1551 HandleBuffer
[Count
++] = Handle
;
1556 // Unlock the protocol database
1558 CoreReleaseProtocolLock ();
1561 // Connect all handles whose Key value is greater than Key
1563 for (Index
= 0; Index
< Count
; Index
++) {
1564 CoreConnectController (HandleBuffer
[Index
], NULL
, NULL
, TRUE
);
1567 CoreFreePool(HandleBuffer
);