2 UEFI handle & protocol handling.
4 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
5 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
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.
20 // mProtocolDatabase - A list of all protocols in the system. (simple list for now)
21 // gHandleList - A list of all the handles in the system
22 // gProtocolDatabaseLock - Lock to protect the mProtocolDatabase
23 // gHandleDatabaseKey - The Key to show that the handle has been created/modified
25 LIST_ENTRY mProtocolDatabase
= INITIALIZE_LIST_HEAD_VARIABLE (mProtocolDatabase
);
26 LIST_ENTRY gHandleList
= INITIALIZE_LIST_HEAD_VARIABLE (gHandleList
);
27 EFI_LOCK gProtocolDatabaseLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY
);
28 UINT64 gHandleDatabaseKey
= 0;
33 Acquire lock on gProtocolDatabaseLock.
37 CoreAcquireProtocolLock (
41 CoreAcquireLock (&gProtocolDatabaseLock
);
47 Release lock on gProtocolDatabaseLock.
51 CoreReleaseProtocolLock (
55 CoreReleaseLock (&gProtocolDatabaseLock
);
61 Check whether a handle is a valid EFI_HANDLE
63 @param UserHandle The handle to check
65 @retval EFI_INVALID_PARAMETER The handle is NULL or not a valid EFI_HANDLE.
66 @retval EFI_SUCCESS The handle is valid EFI_HANDLE.
71 IN EFI_HANDLE UserHandle
76 Handle
= (IHANDLE
*)UserHandle
;
78 return EFI_INVALID_PARAMETER
;
80 if (Handle
->Signature
!= EFI_HANDLE_SIGNATURE
) {
81 return EFI_INVALID_PARAMETER
;
89 Finds the protocol entry for the requested protocol.
90 The gProtocolDatabaseLock must be owned
92 @param Protocol The ID of the protocol
93 @param Create Create a new entry if not found
95 @return Protocol entry
99 CoreFindProtocolEntry (
100 IN EFI_GUID
*Protocol
,
105 PROTOCOL_ENTRY
*Item
;
106 PROTOCOL_ENTRY
*ProtEntry
;
108 ASSERT_LOCKED(&gProtocolDatabaseLock
);
111 // Search the database for the matching GUID
115 for (Link
= mProtocolDatabase
.ForwardLink
;
116 Link
!= &mProtocolDatabase
;
117 Link
= Link
->ForwardLink
) {
119 Item
= CR(Link
, PROTOCOL_ENTRY
, AllEntries
, PROTOCOL_ENTRY_SIGNATURE
);
120 if (CompareGuid (&Item
->ProtocolID
, Protocol
)) {
123 // This is the protocol entry
132 // If the protocol entry was not found and Create is TRUE, then
133 // allocate a new entry
135 if ((ProtEntry
== NULL
) && Create
) {
136 ProtEntry
= AllocatePool (sizeof(PROTOCOL_ENTRY
));
138 if (ProtEntry
!= NULL
) {
140 // Initialize new protocol entry structure
142 ProtEntry
->Signature
= PROTOCOL_ENTRY_SIGNATURE
;
143 CopyGuid ((VOID
*)&ProtEntry
->ProtocolID
, Protocol
);
144 InitializeListHead (&ProtEntry
->Protocols
);
145 InitializeListHead (&ProtEntry
->Notify
);
148 // Add it to protocol database
150 InsertTailList (&mProtocolDatabase
, &ProtEntry
->AllEntries
);
160 Finds the protocol instance for the requested handle and protocol.
161 Note: This function doesn't do parameters checking, it's caller's responsibility
162 to pass in valid parameters.
164 @param Handle The handle to search the protocol on
165 @param Protocol GUID of the protocol
166 @param Interface The interface for the protocol being searched
168 @return Protocol instance (NULL: Not found)
172 CoreFindProtocolInterface (
174 IN EFI_GUID
*Protocol
,
178 PROTOCOL_INTERFACE
*Prot
;
179 PROTOCOL_ENTRY
*ProtEntry
;
182 ASSERT_LOCKED(&gProtocolDatabaseLock
);
186 // Lookup the protocol entry for this protocol ID
189 ProtEntry
= CoreFindProtocolEntry (Protocol
, FALSE
);
190 if (ProtEntry
!= NULL
) {
193 // Look at each protocol interface for any matches
195 for (Link
= Handle
->Protocols
.ForwardLink
; Link
!= &Handle
->Protocols
; Link
=Link
->ForwardLink
) {
198 // If this protocol interface matches, remove it
200 Prot
= CR(Link
, PROTOCOL_INTERFACE
, Link
, PROTOCOL_INTERFACE_SIGNATURE
);
201 if (Prot
->Interface
== Interface
&& Prot
->Protocol
== ProtEntry
) {
214 Removes an event from a register protocol notify list on a protocol.
216 @param Event The event to search for in the protocol
219 @return EFI_SUCCESS if the event was found and removed.
220 @return EFI_NOT_FOUND if the event was not found in the protocl database.
224 CoreUnregisterProtocolNotifyEvent (
229 PROTOCOL_ENTRY
*ProtEntry
;
230 LIST_ENTRY
*NotifyLink
;
231 PROTOCOL_NOTIFY
*ProtNotify
;
233 CoreAcquireProtocolLock ();
235 for ( Link
= mProtocolDatabase
.ForwardLink
;
236 Link
!= &mProtocolDatabase
;
237 Link
= Link
->ForwardLink
) {
239 ProtEntry
= CR(Link
, PROTOCOL_ENTRY
, AllEntries
, PROTOCOL_ENTRY_SIGNATURE
);
241 for ( NotifyLink
= ProtEntry
->Notify
.ForwardLink
;
242 NotifyLink
!= &ProtEntry
->Notify
;
243 NotifyLink
= NotifyLink
->ForwardLink
) {
245 ProtNotify
= CR(NotifyLink
, PROTOCOL_NOTIFY
, Link
, PROTOCOL_NOTIFY_SIGNATURE
);
247 if (ProtNotify
->Event
== Event
) {
248 RemoveEntryList(&ProtNotify
->Link
);
249 CoreFreePool(ProtNotify
);
250 CoreReleaseProtocolLock ();
256 CoreReleaseProtocolLock ();
257 return EFI_NOT_FOUND
;
263 Removes all the events in the protocol database that match Event.
265 @param Event The event to search for in the protocol
268 @return EFI_SUCCESS when done searching the entire database.
272 CoreUnregisterProtocolNotify (
279 Status
= CoreUnregisterProtocolNotifyEvent (Event
);
280 } while (!EFI_ERROR (Status
));
289 Wrapper function to CoreInstallProtocolInterfaceNotify. This is the public API which
290 Calls the private one which contains a BOOLEAN parameter for notifications
292 @param UserHandle The handle to install the protocol handler on,
293 or NULL if a new handle is to be allocated
294 @param Protocol The protocol to add to the handle
295 @param InterfaceType Indicates whether Interface is supplied in
297 @param Interface The interface for the protocol being added
304 CoreInstallProtocolInterface (
305 IN OUT EFI_HANDLE
*UserHandle
,
306 IN EFI_GUID
*Protocol
,
307 IN EFI_INTERFACE_TYPE InterfaceType
,
311 return CoreInstallProtocolInterfaceNotify (
322 Installs a protocol interface into the boot services environment.
324 @param UserHandle The handle to install the protocol handler on,
325 or NULL if a new handle is to be allocated
326 @param Protocol The protocol to add to the handle
327 @param InterfaceType Indicates whether Interface is supplied in
329 @param Interface The interface for the protocol being added
330 @param Notify indicates whether notify the notification list
333 @retval EFI_INVALID_PARAMETER Invalid parameter
334 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
335 @retval EFI_SUCCESS Protocol interface successfully installed
339 CoreInstallProtocolInterfaceNotify (
340 IN OUT EFI_HANDLE
*UserHandle
,
341 IN EFI_GUID
*Protocol
,
342 IN EFI_INTERFACE_TYPE InterfaceType
,
347 PROTOCOL_INTERFACE
*Prot
;
348 PROTOCOL_ENTRY
*ProtEntry
;
351 VOID
*ExistingInterface
;
354 // returns EFI_INVALID_PARAMETER if InterfaceType is invalid.
355 // Also added check for invalid UserHandle and Protocol pointers.
357 if (UserHandle
== NULL
|| Protocol
== NULL
) {
358 return EFI_INVALID_PARAMETER
;
361 if (InterfaceType
!= EFI_NATIVE_INTERFACE
) {
362 return EFI_INVALID_PARAMETER
;
366 // Print debug message
368 DEBUG((DEBUG_INFO
, "InstallProtocolInterface: %g %p\n", Protocol
, Interface
));
370 Status
= EFI_OUT_OF_RESOURCES
;
374 if (*UserHandle
!= NULL
) {
375 Status
= CoreHandleProtocol (*UserHandle
, Protocol
, (VOID
**)&ExistingInterface
);
376 if (!EFI_ERROR (Status
)) {
377 return EFI_INVALID_PARAMETER
;
382 // Lock the protocol database
384 CoreAcquireProtocolLock ();
387 // Lookup the Protocol Entry for the requested protocol
389 ProtEntry
= CoreFindProtocolEntry (Protocol
, TRUE
);
390 if (ProtEntry
== NULL
) {
395 // Allocate a new protocol interface structure
397 Prot
= AllocateZeroPool (sizeof(PROTOCOL_INTERFACE
));
399 Status
= EFI_OUT_OF_RESOURCES
;
404 // If caller didn't supply a handle, allocate a new one
406 Handle
= (IHANDLE
*)*UserHandle
;
407 if (Handle
== NULL
) {
408 Handle
= AllocateZeroPool (sizeof(IHANDLE
));
409 if (Handle
== NULL
) {
410 Status
= EFI_OUT_OF_RESOURCES
;
415 // Initialize new handler structure
417 Handle
->Signature
= EFI_HANDLE_SIGNATURE
;
418 InitializeListHead (&Handle
->Protocols
);
421 // Initialize the Key to show that the handle has been created/modified
423 gHandleDatabaseKey
++;
424 Handle
->Key
= gHandleDatabaseKey
;
427 // Add this handle to the list global list of all handles
430 InsertTailList (&gHandleList
, &Handle
->AllHandles
);
433 Status
= CoreValidateHandle (Handle
);
434 if (EFI_ERROR (Status
)) {
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
503 Installs a list of protocol interface into the boot services environment.
504 This function calls InstallProtocolInterface() in a loop. If any error
505 occures all the protocols added by this function are removed. This is
506 basically a lib function to save space.
508 @param Handle The pointer to a handle to install the new
509 protocol interfaces on, or a pointer to NULL
510 if a new handle is to be allocated.
511 @param ... EFI_GUID followed by protocol instance. A NULL
512 terminates the list. The pairs are the
513 arguments to InstallProtocolInterface(). All the
514 protocols are added to Handle.
516 @retval EFI_INVALID_PARAMETER Handle is NULL.
517 @retval EFI_SUCCESS Protocol interfaces successfully installed.
522 CoreInstallMultipleProtocolInterfaces (
523 IN OUT EFI_HANDLE
*Handle
,
533 EFI_HANDLE OldHandle
;
534 EFI_HANDLE DeviceHandle
;
535 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
537 if (Handle
== NULL
) {
538 return EFI_INVALID_PARAMETER
;
542 // Syncronize with notifcations.
544 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
548 // Check for duplicate device path and install the protocol interfaces
550 VA_START (Args
, Handle
);
551 for (Index
= 0, Status
= EFI_SUCCESS
; !EFI_ERROR (Status
); Index
++) {
553 // If protocol is NULL, then it's the end of the list
555 Protocol
= VA_ARG (Args
, EFI_GUID
*);
556 if (Protocol
== NULL
) {
560 Interface
= VA_ARG (Args
, VOID
*);
563 // Make sure you are installing on top a device path that has already been added.
565 if (CompareGuid (Protocol
, &gEfiDevicePathProtocolGuid
)) {
567 DevicePath
= Interface
;
568 Status
= CoreLocateDevicePath (&gEfiDevicePathProtocolGuid
, &DevicePath
, &DeviceHandle
);
569 if (!EFI_ERROR (Status
) && (DeviceHandle
!= NULL
) && IsDevicePathEnd(DevicePath
)) {
570 Status
= EFI_ALREADY_STARTED
;
578 Status
= CoreInstallProtocolInterface (Handle
, Protocol
, EFI_NATIVE_INTERFACE
, Interface
);
583 // If there was an error, remove all the interfaces that were installed without any errors
585 if (EFI_ERROR (Status
)) {
587 // Reset the va_arg back to the first argument.
589 VA_START (Args
, Handle
);
590 for (; Index
> 1; Index
--) {
591 Protocol
= VA_ARG (Args
, EFI_GUID
*);
592 Interface
= VA_ARG (Args
, VOID
*);
593 CoreUninstallProtocolInterface (*Handle
, Protocol
, Interface
);
603 CoreRestoreTpl (OldTpl
);
609 Attempts to disconnect all drivers that are using the protocol interface being queried.
610 If failed, reconnect all drivers disconnected.
611 Note: This function doesn't do parameters checking, it's caller's responsibility
612 to pass in valid parameters.
614 @param UserHandle The handle on which the protocol is installed
615 @param Prot The protocol to disconnect drivers from
617 @retval EFI_SUCCESS Drivers using the protocol interface are all
619 @retval EFI_ACCESS_DENIED Failed to disconnect one or all of the drivers
623 CoreDisconnectControllersUsingProtocolInterface (
624 IN EFI_HANDLE UserHandle
,
625 IN PROTOCOL_INTERFACE
*Prot
631 OPEN_PROTOCOL_DATA
*OpenData
;
633 Status
= EFI_SUCCESS
;
636 // Attempt to disconnect all drivers from this protocol interface
640 for ( Link
= Prot
->OpenList
.ForwardLink
;
641 (Link
!= &Prot
->OpenList
) && !ItemFound
;
642 Link
= Link
->ForwardLink
) {
643 OpenData
= CR (Link
, OPEN_PROTOCOL_DATA
, Link
, OPEN_PROTOCOL_DATA_SIGNATURE
);
644 if ((OpenData
->Attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) != 0) {
646 CoreReleaseProtocolLock ();
647 Status
= CoreDisconnectController (UserHandle
, OpenData
->AgentHandle
, NULL
);
648 CoreAcquireProtocolLock ();
649 if (EFI_ERROR (Status
)) {
657 if (!EFI_ERROR (Status
)) {
659 // Attempt to remove BY_HANDLE_PROTOOCL and GET_PROTOCOL and TEST_PROTOCOL Open List items
663 for ( Link
= Prot
->OpenList
.ForwardLink
;
664 (Link
!= &Prot
->OpenList
) && !ItemFound
;
665 Link
= Link
->ForwardLink
) {
666 OpenData
= CR (Link
, OPEN_PROTOCOL_DATA
, Link
, OPEN_PROTOCOL_DATA_SIGNATURE
);
667 if ((OpenData
->Attributes
&
668 (EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
| EFI_OPEN_PROTOCOL_GET_PROTOCOL
| EFI_OPEN_PROTOCOL_TEST_PROTOCOL
)) != 0) {
670 RemoveEntryList (&OpenData
->Link
);
671 Prot
->OpenListCount
--;
672 CoreFreePool (OpenData
);
679 // If there are errors or still has open items in the list, then reconnect all the drivers and return an error
681 if (EFI_ERROR (Status
) || (Prot
->OpenListCount
> 0)) {
682 CoreReleaseProtocolLock ();
683 CoreConnectController (UserHandle
, NULL
, NULL
, TRUE
);
684 CoreAcquireProtocolLock ();
685 Status
= EFI_ACCESS_DENIED
;
694 Uninstalls all instances of a protocol:interfacer from a handle.
695 If the last protocol interface is remove from the handle, the
698 @param UserHandle The handle to remove the protocol handler from
699 @param Protocol The protocol, of protocol:interface, to remove
700 @param Interface The interface, of protocol:interface, to remove
702 @retval EFI_INVALID_PARAMETER Protocol is NULL.
703 @retval EFI_SUCCESS Protocol interface successfully uninstalled.
708 CoreUninstallProtocolInterface (
709 IN EFI_HANDLE UserHandle
,
710 IN EFI_GUID
*Protocol
,
716 PROTOCOL_INTERFACE
*Prot
;
719 // Check that Protocol is valid
721 if (Protocol
== NULL
) {
722 return EFI_INVALID_PARAMETER
;
726 // Check that UserHandle is a valid handle
728 Status
= CoreValidateHandle (UserHandle
);
729 if (EFI_ERROR (Status
)) {
734 // Lock the protocol database
736 CoreAcquireProtocolLock ();
739 // Check that Protocol exists on UserHandle, and Interface matches the interface in the database
741 Prot
= CoreFindProtocolInterface (UserHandle
, Protocol
, Interface
);
743 Status
= EFI_NOT_FOUND
;
748 // Attempt to disconnect all drivers that are using the protocol interface that is about to be removed
750 Status
= CoreDisconnectControllersUsingProtocolInterface (
754 if (EFI_ERROR (Status
)) {
756 // One or more drivers refused to release, so return the error
762 // Remove the protocol interface from the protocol
764 Status
= EFI_NOT_FOUND
;
765 Handle
= (IHANDLE
*)UserHandle
;
766 Prot
= CoreRemoveInterfaceFromProtocol (Handle
, Protocol
, Interface
);
770 // Update the Key to show that the handle has been created/modified
772 gHandleDatabaseKey
++;
773 Handle
->Key
= gHandleDatabaseKey
;
776 // Remove the protocol interface from the handle
778 RemoveEntryList (&Prot
->Link
);
785 Status
= EFI_SUCCESS
;
789 // If there are no more handlers for the handle, free the handle
791 if (IsListEmpty (&Handle
->Protocols
)) {
792 Handle
->Signature
= 0;
793 RemoveEntryList (&Handle
->AllHandles
);
794 CoreFreePool (Handle
);
799 // Done, unlock the database and return
801 CoreReleaseProtocolLock ();
809 Uninstalls a list of protocol interface in the boot services environment.
810 This function calls UnisatllProtocolInterface() in a loop. This is
811 basically a lib function to save space.
813 @param Handle The handle to uninstall the protocol
814 @param ... EFI_GUID followed by protocol instance. A NULL
815 terminates the list. The pairs are the
816 arguments to UninstallProtocolInterface(). All
817 the protocols are added to Handle.
824 CoreUninstallMultipleProtocolInterfaces (
825 IN EFI_HANDLE Handle
,
835 VA_START (Args
, Handle
);
836 for (Index
= 0, Status
= EFI_SUCCESS
; !EFI_ERROR (Status
); Index
++) {
838 // If protocol is NULL, then it's the end of the list
840 Protocol
= VA_ARG (Args
, EFI_GUID
*);
841 if (Protocol
== NULL
) {
845 Interface
= VA_ARG (Args
, VOID
*);
850 Status
= CoreUninstallProtocolInterface (Handle
, Protocol
, Interface
);
855 // If there was an error, add all the interfaces that were
856 // uninstalled without any errors
858 if (EFI_ERROR (Status
)) {
860 // Reset the va_arg back to the first argument.
862 VA_START (Args
, Handle
);
863 for (; Index
> 1; Index
--) {
864 Protocol
= VA_ARG(Args
, EFI_GUID
*);
865 Interface
= VA_ARG(Args
, VOID
*);
866 CoreInstallProtocolInterface (&Handle
, Protocol
, EFI_NATIVE_INTERFACE
, Interface
);
876 Locate a certain GUID protocol interface in a Handle's protocols.
878 @param UserHandle The handle to obtain the protocol interface on
879 @param Protocol The GUID of the protocol
881 @return The requested protocol interface for the handle
885 CoreGetProtocolInterface (
886 IN EFI_HANDLE UserHandle
,
887 IN EFI_GUID
*Protocol
891 PROTOCOL_ENTRY
*ProtEntry
;
892 PROTOCOL_INTERFACE
*Prot
;
896 Status
= CoreValidateHandle (UserHandle
);
897 if (EFI_ERROR (Status
)) {
901 Handle
= (IHANDLE
*)UserHandle
;
904 // Look at each protocol interface for a match
906 for (Link
= Handle
->Protocols
.ForwardLink
; Link
!= &Handle
->Protocols
; Link
= Link
->ForwardLink
) {
907 Prot
= CR(Link
, PROTOCOL_INTERFACE
, Link
, PROTOCOL_INTERFACE_SIGNATURE
);
908 ProtEntry
= Prot
->Protocol
;
909 if (CompareGuid (&ProtEntry
->ProtocolID
, Protocol
)) {
919 Queries a handle to determine if it supports a specified protocol.
921 @param UserHandle The handle being queried.
922 @param Protocol The published unique identifier of the protocol.
923 @param Interface Supplies the address where a pointer to the
924 corresponding Protocol Interface is returned.
926 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
927 @retval EFI_UNSUPPORTED The device does not support the specified protocol.
928 @retval EFI_INVALID_PARAMETER Handle is NULL..
929 @retval EFI_INVALID_PARAMETER Protocol is NULL.
930 @retval EFI_INVALID_PARAMETER Interface is NULL.
936 IN EFI_HANDLE UserHandle
,
937 IN EFI_GUID
*Protocol
,
941 return CoreOpenProtocol (
947 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
954 Locates the installed protocol handler for the handle, and
955 invokes it to obtain the protocol interface. Usage information
956 is registered in the protocol data base.
958 @param UserHandle The handle to obtain the protocol interface on
959 @param Protocol The ID of the protocol
960 @param Interface The location to return the protocol interface
961 @param ImageHandle The handle of the Image that is opening the
962 protocol interface specified by Protocol and
964 @param ControllerHandle The controller handle that is requiring this
966 @param Attributes The open mode of the protocol interface
967 specified by Handle and Protocol.
969 @retval EFI_INVALID_PARAMETER Protocol is NULL.
970 @retval EFI_SUCCESS Get the protocol interface.
976 IN EFI_HANDLE UserHandle
,
977 IN EFI_GUID
*Protocol
,
978 OUT VOID
**Interface OPTIONAL
,
979 IN EFI_HANDLE ImageHandle
,
980 IN EFI_HANDLE ControllerHandle
,
985 PROTOCOL_INTERFACE
*Prot
;
987 OPEN_PROTOCOL_DATA
*OpenData
;
994 // Check for invalid Protocol
996 if (Protocol
== NULL
) {
997 return EFI_INVALID_PARAMETER
;
1001 // Check for invalid Interface
1003 if (Attributes
!= EFI_OPEN_PROTOCOL_TEST_PROTOCOL
) {
1004 if (Interface
== NULL
) {
1005 return EFI_INVALID_PARAMETER
;
1012 // Check for invalid UserHandle
1014 Status
= CoreValidateHandle (UserHandle
);
1015 if (EFI_ERROR (Status
)) {
1020 // Check for invalid Attributes
1022 switch (Attributes
) {
1023 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
:
1024 Status
= CoreValidateHandle (ImageHandle
);
1025 if (EFI_ERROR (Status
)) {
1028 Status
= CoreValidateHandle (ControllerHandle
);
1029 if (EFI_ERROR (Status
)) {
1032 if (UserHandle
== ControllerHandle
) {
1033 return EFI_INVALID_PARAMETER
;
1036 case EFI_OPEN_PROTOCOL_BY_DRIVER
:
1037 case EFI_OPEN_PROTOCOL_BY_DRIVER
| EFI_OPEN_PROTOCOL_EXCLUSIVE
:
1038 Status
= CoreValidateHandle (ImageHandle
);
1039 if (EFI_ERROR (Status
)) {
1042 Status
= CoreValidateHandle (ControllerHandle
);
1043 if (EFI_ERROR (Status
)) {
1047 case EFI_OPEN_PROTOCOL_EXCLUSIVE
:
1048 Status
= CoreValidateHandle (ImageHandle
);
1049 if (EFI_ERROR (Status
)) {
1053 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
:
1054 case EFI_OPEN_PROTOCOL_GET_PROTOCOL
:
1055 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL
:
1058 return EFI_INVALID_PARAMETER
;
1062 // Lock the protocol database
1064 CoreAcquireProtocolLock ();
1067 // Look at each protocol interface for a match
1069 Prot
= CoreGetProtocolInterface (UserHandle
, Protocol
);
1071 Status
= EFI_UNSUPPORTED
;
1076 // This is the protocol interface entry for this protocol
1078 if (Attributes
!= EFI_OPEN_PROTOCOL_TEST_PROTOCOL
) {
1079 *Interface
= Prot
->Interface
;
1081 Status
= EFI_SUCCESS
;
1085 for ( Link
= Prot
->OpenList
.ForwardLink
; Link
!= &Prot
->OpenList
; Link
= Link
->ForwardLink
) {
1086 OpenData
= CR (Link
, OPEN_PROTOCOL_DATA
, Link
, OPEN_PROTOCOL_DATA_SIGNATURE
);
1087 ExactMatch
= (BOOLEAN
)((OpenData
->AgentHandle
== ImageHandle
) &&
1088 (OpenData
->Attributes
== Attributes
) &&
1089 (OpenData
->ControllerHandle
== ControllerHandle
));
1090 if ((OpenData
->Attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) != 0) {
1093 Status
= EFI_ALREADY_STARTED
;
1097 if ((OpenData
->Attributes
& EFI_OPEN_PROTOCOL_EXCLUSIVE
) != 0) {
1099 } else if (ExactMatch
) {
1100 OpenData
->OpenCount
++;
1101 Status
= EFI_SUCCESS
;
1107 // ByDriver TRUE -> A driver is managing (UserHandle, Protocol)
1108 // ByDriver FALSE -> There are no drivers managing (UserHandle, Protocol)
1109 // Exclusive TRUE -> Something has exclusive access to (UserHandle, Protocol)
1110 // Exclusive FALSE -> Nothing has exclusive access to (UserHandle, Protocol)
1113 switch (Attributes
) {
1114 case EFI_OPEN_PROTOCOL_BY_DRIVER
:
1115 if (Exclusive
|| ByDriver
) {
1116 Status
= EFI_ACCESS_DENIED
;
1120 case EFI_OPEN_PROTOCOL_BY_DRIVER
| EFI_OPEN_PROTOCOL_EXCLUSIVE
:
1121 case EFI_OPEN_PROTOCOL_EXCLUSIVE
:
1123 Status
= EFI_ACCESS_DENIED
;
1129 for ( Link
= Prot
->OpenList
.ForwardLink
; (Link
!= &Prot
->OpenList
) && (!Disconnect
); Link
= Link
->ForwardLink
) {
1130 OpenData
= CR (Link
, OPEN_PROTOCOL_DATA
, Link
, OPEN_PROTOCOL_DATA_SIGNATURE
);
1131 if ((OpenData
->Attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) != 0) {
1133 CoreReleaseProtocolLock ();
1134 Status
= CoreDisconnectController (UserHandle
, OpenData
->AgentHandle
, NULL
);
1135 CoreAcquireProtocolLock ();
1136 if (EFI_ERROR (Status
)) {
1137 Status
= EFI_ACCESS_DENIED
;
1142 } while (Disconnect
);
1145 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
:
1146 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
:
1147 case EFI_OPEN_PROTOCOL_GET_PROTOCOL
:
1148 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL
:
1152 if (ImageHandle
== NULL
) {
1153 Status
= EFI_SUCCESS
;
1159 OpenData
= AllocatePool (sizeof(OPEN_PROTOCOL_DATA
));
1160 if (OpenData
== NULL
) {
1161 Status
= EFI_OUT_OF_RESOURCES
;
1163 OpenData
->Signature
= OPEN_PROTOCOL_DATA_SIGNATURE
;
1164 OpenData
->AgentHandle
= ImageHandle
;
1165 OpenData
->ControllerHandle
= ControllerHandle
;
1166 OpenData
->Attributes
= Attributes
;
1167 OpenData
->OpenCount
= 1;
1168 InsertTailList (&Prot
->OpenList
, &OpenData
->Link
);
1169 Prot
->OpenListCount
++;
1170 Status
= EFI_SUCCESS
;
1175 // Done. Release the database lock are return
1177 CoreReleaseProtocolLock ();
1184 Closes a protocol on a handle that was opened using OpenProtocol().
1186 @param UserHandle The handle for the protocol interface that was
1187 previously opened with OpenProtocol(), and is
1189 @param Protocol The published unique identifier of the protocol.
1190 It is the caller's responsibility to pass in a
1192 @param AgentHandle The handle of the agent that is closing the
1194 @param ControllerHandle If the agent that opened a protocol is a driver
1195 that follows the EFI Driver Model, then this
1196 parameter is the controller handle that required
1197 the protocol interface. If the agent does not
1198 follow the EFI Driver Model, then this parameter
1199 is optional and may be NULL.
1201 @retval EFI_SUCCESS The protocol instance was closed.
1202 @retval EFI_INVALID_PARAMETER Handle, AgentHandle or ControllerHandle is not a
1204 @retval EFI_NOT_FOUND Can not find the specified protocol or
1211 IN EFI_HANDLE UserHandle
,
1212 IN EFI_GUID
*Protocol
,
1213 IN EFI_HANDLE AgentHandle
,
1214 IN EFI_HANDLE ControllerHandle
1218 PROTOCOL_INTERFACE
*ProtocolInterface
;
1220 OPEN_PROTOCOL_DATA
*OpenData
;
1223 // Check for invalid parameters
1225 Status
= CoreValidateHandle (UserHandle
);
1226 if (EFI_ERROR (Status
)) {
1229 Status
= CoreValidateHandle (AgentHandle
);
1230 if (EFI_ERROR (Status
)) {
1233 if (ControllerHandle
!= NULL
) {
1234 Status
= CoreValidateHandle (ControllerHandle
);
1235 if (EFI_ERROR (Status
)) {
1239 if (Protocol
== NULL
) {
1240 return EFI_INVALID_PARAMETER
;
1244 // Lock the protocol database
1246 CoreAcquireProtocolLock ();
1249 // Look at each protocol interface for a match
1251 Status
= EFI_NOT_FOUND
;
1252 ProtocolInterface
= CoreGetProtocolInterface (UserHandle
, Protocol
);
1253 if (ProtocolInterface
== NULL
) {
1258 // Walk the Open data base looking for AgentHandle
1260 Link
= ProtocolInterface
->OpenList
.ForwardLink
;
1261 while (Link
!= &ProtocolInterface
->OpenList
) {
1262 OpenData
= CR (Link
, OPEN_PROTOCOL_DATA
, Link
, OPEN_PROTOCOL_DATA_SIGNATURE
);
1263 Link
= Link
->ForwardLink
;
1264 if ((OpenData
->AgentHandle
== AgentHandle
) && (OpenData
->ControllerHandle
== ControllerHandle
)) {
1265 RemoveEntryList (&OpenData
->Link
);
1266 ProtocolInterface
->OpenListCount
--;
1267 CoreFreePool (OpenData
);
1268 Status
= EFI_SUCCESS
;
1274 // Done. Release the database lock and return.
1276 CoreReleaseProtocolLock ();
1284 Return information about Opened protocols in the system
1286 @param UserHandle The handle to close the protocol interface on
1287 @param Protocol The ID of the protocol
1288 @param EntryBuffer A pointer to a buffer of open protocol information in the
1289 form of EFI_OPEN_PROTOCOL_INFORMATION_ENTRY structures.
1290 @param EntryCount Number of EntryBuffer entries
1292 @retval EFI_SUCCESS The open protocol information was returned in EntryBuffer,
1293 and the number of entries was returned EntryCount.
1294 @retval EFI_NOT_FOUND Handle does not support the protocol specified by Protocol.
1295 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to allocate EntryBuffer.
1300 CoreOpenProtocolInformation (
1301 IN EFI_HANDLE UserHandle
,
1302 IN EFI_GUID
*Protocol
,
1303 OUT EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
**EntryBuffer
,
1304 OUT UINTN
*EntryCount
1308 PROTOCOL_INTERFACE
*ProtocolInterface
;
1310 OPEN_PROTOCOL_DATA
*OpenData
;
1311 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*Buffer
;
1315 *EntryBuffer
= NULL
;
1319 // Lock the protocol database
1321 CoreAcquireProtocolLock ();
1324 // Look at each protocol interface for a match
1326 Status
= EFI_NOT_FOUND
;
1327 ProtocolInterface
= CoreGetProtocolInterface (UserHandle
, Protocol
);
1328 if (ProtocolInterface
== NULL
) {
1333 // Count the number of Open Entries
1335 for ( Link
= ProtocolInterface
->OpenList
.ForwardLink
, Count
= 0;
1336 (Link
!= &ProtocolInterface
->OpenList
) ;
1337 Link
= Link
->ForwardLink
) {
1341 ASSERT (Count
== ProtocolInterface
->OpenListCount
);
1344 Size
= sizeof(EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
);
1346 Size
= Count
* sizeof(EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
);
1349 Buffer
= AllocatePool (Size
);
1350 if (Buffer
== NULL
) {
1351 Status
= EFI_OUT_OF_RESOURCES
;
1355 Status
= EFI_SUCCESS
;
1356 for ( Link
= ProtocolInterface
->OpenList
.ForwardLink
, Count
= 0;
1357 (Link
!= &ProtocolInterface
->OpenList
);
1358 Link
= Link
->ForwardLink
, Count
++ ) {
1359 OpenData
= CR (Link
, OPEN_PROTOCOL_DATA
, Link
, OPEN_PROTOCOL_DATA_SIGNATURE
);
1361 Buffer
[Count
].AgentHandle
= OpenData
->AgentHandle
;
1362 Buffer
[Count
].ControllerHandle
= OpenData
->ControllerHandle
;
1363 Buffer
[Count
].Attributes
= OpenData
->Attributes
;
1364 Buffer
[Count
].OpenCount
= OpenData
->OpenCount
;
1367 *EntryBuffer
= Buffer
;
1368 *EntryCount
= Count
;
1372 // Done. Release the database lock.
1374 CoreReleaseProtocolLock ();
1382 Retrieves the list of protocol interface GUIDs that are installed on a handle in a buffer allocated
1385 @param UserHandle The handle from which to retrieve the list of
1386 protocol interface GUIDs.
1387 @param ProtocolBuffer A pointer to the list of protocol interface GUID
1388 pointers that are installed on Handle.
1389 @param ProtocolBufferCount A pointer to the number of GUID pointers present
1392 @retval EFI_SUCCESS The list of protocol interface GUIDs installed
1393 on Handle was returned in ProtocolBuffer. The
1394 number of protocol interface GUIDs was returned
1395 in ProtocolBufferCount.
1396 @retval EFI_INVALID_PARAMETER Handle is NULL.
1397 @retval EFI_INVALID_PARAMETER Handle is not a valid EFI_HANDLE.
1398 @retval EFI_INVALID_PARAMETER ProtocolBuffer is NULL.
1399 @retval EFI_INVALID_PARAMETER ProtocolBufferCount is NULL.
1400 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the
1406 CoreProtocolsPerHandle (
1407 IN EFI_HANDLE UserHandle
,
1408 OUT EFI_GUID
***ProtocolBuffer
,
1409 OUT UINTN
*ProtocolBufferCount
1414 PROTOCOL_INTERFACE
*Prot
;
1416 UINTN ProtocolCount
;
1419 Status
= CoreValidateHandle (UserHandle
);
1420 if (EFI_ERROR (Status
)) {
1424 Handle
= (IHANDLE
*)UserHandle
;
1426 if (ProtocolBuffer
== NULL
) {
1427 return EFI_INVALID_PARAMETER
;
1430 if (ProtocolBufferCount
== NULL
) {
1431 return EFI_INVALID_PARAMETER
;
1434 *ProtocolBufferCount
= 0;
1438 CoreAcquireProtocolLock ();
1440 for (Link
= Handle
->Protocols
.ForwardLink
; Link
!= &Handle
->Protocols
; Link
= Link
->ForwardLink
) {
1445 // If there are no protocol interfaces installed on Handle, then Handle is not a valid EFI_HANDLE
1447 if (ProtocolCount
== 0) {
1448 Status
= EFI_INVALID_PARAMETER
;
1452 Buffer
= AllocatePool (sizeof (EFI_GUID
*) * ProtocolCount
);
1453 if (Buffer
== NULL
) {
1454 Status
= EFI_OUT_OF_RESOURCES
;
1458 *ProtocolBuffer
= Buffer
;
1459 *ProtocolBufferCount
= ProtocolCount
;
1461 for ( Link
= Handle
->Protocols
.ForwardLink
, ProtocolCount
= 0;
1462 Link
!= &Handle
->Protocols
;
1463 Link
= Link
->ForwardLink
, ProtocolCount
++) {
1464 Prot
= CR(Link
, PROTOCOL_INTERFACE
, Link
, PROTOCOL_INTERFACE_SIGNATURE
);
1465 Buffer
[ProtocolCount
] = &(Prot
->Protocol
->ProtocolID
);
1467 Status
= EFI_SUCCESS
;
1470 CoreReleaseProtocolLock ();
1477 return handle database key.
1480 @return Handle database key.
1484 CoreGetHandleDatabaseKey (
1488 return gHandleDatabaseKey
;
1494 Go connect any handles that were created or modified while a image executed.
1496 @param Key The Key to show that the handle has been
1501 CoreConnectHandlesByKey (
1507 EFI_HANDLE
*HandleBuffer
;
1512 // Lock the protocol database
1514 CoreAcquireProtocolLock ();
1516 for (Link
= gHandleList
.ForwardLink
, Count
= 0; Link
!= &gHandleList
; Link
= Link
->ForwardLink
) {
1517 Handle
= CR (Link
, IHANDLE
, AllHandles
, EFI_HANDLE_SIGNATURE
);
1518 if (Handle
->Key
> Key
) {
1523 HandleBuffer
= AllocatePool (Count
* sizeof (EFI_HANDLE
));
1524 if (HandleBuffer
== NULL
) {
1525 CoreReleaseProtocolLock ();
1529 for (Link
= gHandleList
.ForwardLink
, Count
= 0; Link
!= &gHandleList
; Link
= Link
->ForwardLink
) {
1530 Handle
= CR (Link
, IHANDLE
, AllHandles
, EFI_HANDLE_SIGNATURE
);
1531 if (Handle
->Key
> Key
) {
1532 HandleBuffer
[Count
++] = Handle
;
1537 // Unlock the protocol database
1539 CoreReleaseProtocolLock ();
1542 // Connect all handles whose Key value is greater than Key
1544 for (Index
= 0; Index
< Count
; Index
++) {
1545 CoreConnectController (HandleBuffer
[Index
], NULL
, NULL
, TRUE
);
1548 CoreFreePool(HandleBuffer
);