2 Locate handle functions
4 Copyright (c) 2006 - 2014, 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.
19 // ProtocolRequest - Last LocateHandle request ID
21 UINTN mEfiLocateHandleRequest
= 0;
24 // Internal prototypes
31 PROTOCOL_ENTRY
*ProtEntry
;
37 IN OUT LOCATE_POSITION
*Position
,
42 Routine to get the next Handle, when you are searching for all handles.
44 @param Position Information about which Handle to seach for.
45 @param Interface Return the interface structure for the matching
48 @return An pointer to IHANDLE if the next Position is not the end of the list.
49 Otherwise,NULL is returned.
53 CoreGetNextLocateAllHandles (
54 IN OUT LOCATE_POSITION
*Position
,
59 Routine to get the next Handle, when you are searching for register protocol
62 @param Position Information about which Handle to seach for.
63 @param Interface Return the interface structure for the matching
66 @return An pointer to IHANDLE if the next Position is not the end of the list.
67 Otherwise,NULL is returned.
71 CoreGetNextLocateByRegisterNotify (
72 IN OUT LOCATE_POSITION
*Position
,
77 Routine to get the next Handle, when you are searching for a given protocol.
79 @param Position Information about which Handle to seach for.
80 @param Interface Return the interface structure for the matching
83 @return An pointer to IHANDLE if the next Position is not the end of the list.
84 Otherwise,NULL is returned.
88 CoreGetNextLocateByProtocol (
89 IN OUT LOCATE_POSITION
*Position
,
95 Locates the requested handle(s) and returns them in Buffer.
97 @param SearchType The type of search to perform to locate the
99 @param Protocol The protocol to search for
100 @param SearchKey Dependant on SearchType
101 @param BufferSize On input the size of Buffer. On output the
102 size of data returned.
103 @param Buffer The buffer to return the results in
105 @retval EFI_BUFFER_TOO_SMALL Buffer too small, required buffer size is
106 returned in BufferSize.
107 @retval EFI_INVALID_PARAMETER Invalid parameter
108 @retval EFI_SUCCESS Successfully found the requested handle(s) and
109 returns them in Buffer.
115 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
116 IN EFI_GUID
*Protocol OPTIONAL
,
117 IN VOID
*SearchKey OPTIONAL
,
118 IN OUT UINTN
*BufferSize
,
119 OUT EFI_HANDLE
*Buffer
123 LOCATE_POSITION Position
;
124 PROTOCOL_NOTIFY
*ProtNotify
;
125 CORE_GET_NEXT GetNext
;
128 IHANDLE
**ResultBuffer
;
131 if (BufferSize
== NULL
) {
132 return EFI_INVALID_PARAMETER
;
135 if ((*BufferSize
> 0) && (Buffer
== NULL
)) {
136 return EFI_INVALID_PARAMETER
;
142 // Set initial position
144 Position
.Protocol
= Protocol
;
145 Position
.SearchKey
= SearchKey
;
146 Position
.Position
= &gHandleList
;
149 ResultBuffer
= (IHANDLE
**) Buffer
;
150 Status
= EFI_SUCCESS
;
153 // Lock the protocol database
155 CoreAcquireProtocolLock ();
158 // Get the search function based on type
160 switch (SearchType
) {
162 GetNext
= CoreGetNextLocateAllHandles
;
165 case ByRegisterNotify
:
167 // Must have SearchKey for locate ByRegisterNotify
169 if (SearchKey
== NULL
) {
170 Status
= EFI_INVALID_PARAMETER
;
173 GetNext
= CoreGetNextLocateByRegisterNotify
;
177 GetNext
= CoreGetNextLocateByProtocol
;
178 if (Protocol
== NULL
) {
179 Status
= EFI_INVALID_PARAMETER
;
183 // Look up the protocol entry and set the head pointer
185 Position
.ProtEntry
= CoreFindProtocolEntry (Protocol
, FALSE
);
186 if (Position
.ProtEntry
== NULL
) {
187 Status
= EFI_NOT_FOUND
;
190 Position
.Position
= &Position
.ProtEntry
->Protocols
;
194 Status
= EFI_INVALID_PARAMETER
;
198 if (EFI_ERROR(Status
)) {
199 CoreReleaseProtocolLock ();
203 ASSERT (GetNext
!= NULL
);
205 // Enumerate out the matching handles
207 mEfiLocateHandleRequest
+= 1;
210 // Get the next handle. If no more handles, stop
212 Handle
= GetNext (&Position
, &Interface
);
213 if (NULL
== Handle
) {
218 // Increase the resulting buffer size, and if this handle
221 ResultSize
+= sizeof(Handle
);
222 if (ResultSize
<= *BufferSize
) {
223 *ResultBuffer
= Handle
;
229 // If the result is a zero length buffer, then there were no
232 if (ResultSize
== 0) {
233 Status
= EFI_NOT_FOUND
;
236 // Return the resulting buffer size. If it's larger than what
237 // was passed, then set the error code
239 if (ResultSize
> *BufferSize
) {
240 Status
= EFI_BUFFER_TOO_SMALL
;
243 *BufferSize
= ResultSize
;
245 if (SearchType
== ByRegisterNotify
&& !EFI_ERROR(Status
)) {
247 // If this is a search by register notify and a handle was
248 // returned, update the register notification position
250 ASSERT (SearchKey
!= NULL
);
251 ProtNotify
= SearchKey
;
252 ProtNotify
->Position
= ProtNotify
->Position
->ForwardLink
;
256 CoreReleaseProtocolLock ();
263 Routine to get the next Handle, when you are searching for all handles.
265 @param Position Information about which Handle to seach for.
266 @param Interface Return the interface structure for the matching
269 @return An pointer to IHANDLE if the next Position is not the end of the list.
270 Otherwise,NULL is returned.
274 CoreGetNextLocateAllHandles (
275 IN OUT LOCATE_POSITION
*Position
,
284 Position
->Position
= Position
->Position
->ForwardLink
;
287 // If not at the end of the list, get the handle
291 if (Position
->Position
!= &gHandleList
) {
292 Handle
= CR (Position
->Position
, IHANDLE
, AllHandles
, EFI_HANDLE_SIGNATURE
);
301 Routine to get the next Handle, when you are searching for register protocol
304 @param Position Information about which Handle to seach for.
305 @param Interface Return the interface structure for the matching
308 @return An pointer to IHANDLE if the next Position is not the end of the list.
309 Otherwise,NULL is returned.
313 CoreGetNextLocateByRegisterNotify (
314 IN OUT LOCATE_POSITION
*Position
,
319 PROTOCOL_NOTIFY
*ProtNotify
;
320 PROTOCOL_INTERFACE
*Prot
;
325 ProtNotify
= Position
->SearchKey
;
328 // If this is the first request, get the next handle
330 if (ProtNotify
!= NULL
) {
331 ASSERT(ProtNotify
->Signature
== PROTOCOL_NOTIFY_SIGNATURE
);
332 Position
->SearchKey
= NULL
;
335 // If not at the end of the list, get the next handle
337 Link
= ProtNotify
->Position
->ForwardLink
;
338 if (Link
!= &ProtNotify
->Protocol
->Protocols
) {
339 Prot
= CR (Link
, PROTOCOL_INTERFACE
, ByProtocol
, PROTOCOL_INTERFACE_SIGNATURE
);
340 Handle
= Prot
->Handle
;
341 *Interface
= Prot
->Interface
;
350 Routine to get the next Handle, when you are searching for a given protocol.
352 @param Position Information about which Handle to seach for.
353 @param Interface Return the interface structure for the matching
356 @return An pointer to IHANDLE if the next Position is not the end of the list.
357 Otherwise,NULL is returned.
361 CoreGetNextLocateByProtocol (
362 IN OUT LOCATE_POSITION
*Position
,
368 PROTOCOL_INTERFACE
*Prot
;
376 Link
= Position
->Position
->ForwardLink
;
377 Position
->Position
= Link
;
380 // If not at the end, return the handle
382 if (Link
== &Position
->ProtEntry
->Protocols
) {
390 Prot
= CR(Link
, PROTOCOL_INTERFACE
, ByProtocol
, PROTOCOL_INTERFACE_SIGNATURE
);
391 Handle
= Prot
->Handle
;
392 *Interface
= Prot
->Interface
;
395 // If this handle has not been returned this request, then
398 if (Handle
->LocateRequest
!= mEfiLocateHandleRequest
) {
399 Handle
->LocateRequest
= mEfiLocateHandleRequest
;
409 Locates the handle to a device on the device path that supports the specified protocol.
411 @param Protocol Specifies the protocol to search for.
412 @param DevicePath On input, a pointer to a pointer to the device path. On output, the device
413 path pointer is modified to point to the remaining part of the device
415 @param Device A pointer to the returned device handle.
417 @retval EFI_SUCCESS The resulting handle was returned.
418 @retval EFI_NOT_FOUND No handles match the search.
419 @retval EFI_INVALID_PARAMETER Protocol is NULL.
420 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
421 @retval EFI_INVALID_PARAMETER A handle matched the search and Device is NULL.
426 CoreLocateDevicePath (
427 IN EFI_GUID
*Protocol
,
428 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
429 OUT EFI_HANDLE
*Device
440 EFI_HANDLE BestDevice
;
441 EFI_DEVICE_PATH_PROTOCOL
*SourcePath
;
442 EFI_DEVICE_PATH_PROTOCOL
*TmpDevicePath
;
444 if (Protocol
== NULL
) {
445 return EFI_INVALID_PARAMETER
;
448 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
449 return EFI_INVALID_PARAMETER
;
454 SourcePath
= *DevicePath
;
455 TmpDevicePath
= SourcePath
;
456 while (!IsDevicePathEnd (TmpDevicePath
)) {
457 if (IsDevicePathEndInstance (TmpDevicePath
)) {
459 // If DevicePath is a multi-instance device path,
460 // the function will operate on the first instance
464 TmpDevicePath
= NextDevicePathNode (TmpDevicePath
);
467 SourceSize
= (UINTN
) TmpDevicePath
- (UINTN
) SourcePath
;
470 // Get a list of all handles that support the requested protocol
472 Status
= CoreLocateHandleBuffer (ByProtocol
, Protocol
, NULL
, &HandleCount
, &Handles
);
473 if (EFI_ERROR (Status
) || HandleCount
== 0) {
474 return EFI_NOT_FOUND
;
478 for(Index
= 0; Index
< HandleCount
; Index
+= 1) {
479 Handle
= Handles
[Index
];
480 Status
= CoreHandleProtocol (Handle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&TmpDevicePath
);
481 if (EFI_ERROR (Status
)) {
483 // If this handle doesn't support device path, then skip it
489 // Check if DevicePath is first part of SourcePath
491 Size
= GetDevicePathSize (TmpDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
493 if ((Size
<= SourceSize
) && CompareMem (SourcePath
, TmpDevicePath
, (UINTN
) Size
) == 0) {
495 // If the size is equal to the best match, then we
496 // have a duplicate device path for 2 different device
499 ASSERT (Size
!= BestMatch
);
502 // We've got a match, see if it's the best match so far
504 if (Size
> BestMatch
) {
511 CoreFreePool (Handles
);
514 // If there wasn't any match, then no parts of the device path was found.
515 // Which is strange since there is likely a "root level" device path in the system.
517 if (BestMatch
== -1) {
518 return EFI_NOT_FOUND
;
521 if (Device
== NULL
) {
522 return EFI_INVALID_PARAMETER
;
524 *Device
= BestDevice
;
527 // Return the remaining part of the device path
529 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) (((UINT8
*) SourcePath
) + BestMatch
);
535 Return the first Protocol Interface that matches the Protocol GUID. If
536 Registration is passed in, return a Protocol Instance that was just add
537 to the system. If Registration is NULL return the first Protocol Interface
540 @param Protocol The protocol to search for
541 @param Registration Optional Registration Key returned from
542 RegisterProtocolNotify()
543 @param Interface Return the Protocol interface (instance).
545 @retval EFI_SUCCESS If a valid Interface is returned
546 @retval EFI_INVALID_PARAMETER Invalid parameter
547 @retval EFI_NOT_FOUND Protocol interface not found
553 IN EFI_GUID
*Protocol
,
554 IN VOID
*Registration OPTIONAL
,
559 LOCATE_POSITION Position
;
560 PROTOCOL_NOTIFY
*ProtNotify
;
563 if (Interface
== NULL
) {
564 return EFI_INVALID_PARAMETER
;
567 if (Protocol
== NULL
) {
568 return EFI_NOT_FOUND
;
572 Status
= EFI_SUCCESS
;
575 // Set initial position
577 Position
.Protocol
= Protocol
;
578 Position
.SearchKey
= Registration
;
579 Position
.Position
= &gHandleList
;
582 // Lock the protocol database
584 CoreAcquireProtocolLock ();
586 mEfiLocateHandleRequest
+= 1;
588 if (Registration
== NULL
) {
590 // Look up the protocol entry and set the head pointer
592 Position
.ProtEntry
= CoreFindProtocolEntry (Protocol
, FALSE
);
593 if (Position
.ProtEntry
== NULL
) {
594 Status
= EFI_NOT_FOUND
;
597 Position
.Position
= &Position
.ProtEntry
->Protocols
;
599 Handle
= CoreGetNextLocateByProtocol (&Position
, Interface
);
601 Handle
= CoreGetNextLocateByRegisterNotify (&Position
, Interface
);
604 if (Handle
== NULL
) {
605 Status
= EFI_NOT_FOUND
;
606 } else if (Registration
!= NULL
) {
608 // If this is a search by register notify and a handle was
609 // returned, update the register notification position
611 ProtNotify
= Registration
;
612 ProtNotify
->Position
= ProtNotify
->Position
->ForwardLink
;
616 CoreReleaseProtocolLock ();
622 Function returns an array of handles that support the requested protocol
623 in a buffer allocated from pool. This is a version of CoreLocateHandle()
624 that allocates a buffer for the caller.
626 @param SearchType Specifies which handle(s) are to be returned.
627 @param Protocol Provides the protocol to search by. This
628 parameter is only valid for SearchType
630 @param SearchKey Supplies the search key depending on the
632 @param NumberHandles The number of handles returned in Buffer.
633 @param Buffer A pointer to the buffer to return the requested
634 array of handles that support Protocol.
636 @retval EFI_SUCCESS The result array of handles was returned.
637 @retval EFI_NOT_FOUND No handles match the search.
638 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the
640 @retval EFI_INVALID_PARAMETER One or more paramters are not valid.
645 CoreLocateHandleBuffer (
646 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
647 IN EFI_GUID
*Protocol OPTIONAL
,
648 IN VOID
*SearchKey OPTIONAL
,
649 IN OUT UINTN
*NumberHandles
,
650 OUT EFI_HANDLE
**Buffer
656 if (NumberHandles
== NULL
) {
657 return EFI_INVALID_PARAMETER
;
660 if (Buffer
== NULL
) {
661 return EFI_INVALID_PARAMETER
;
667 Status
= CoreLocateHandle (
675 // LocateHandleBuffer() returns incorrect status code if SearchType is
678 // Add code to correctly handle expected errors from CoreLocateHandle().
680 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
681 if (Status
!= EFI_INVALID_PARAMETER
) {
682 Status
= EFI_NOT_FOUND
;
687 *Buffer
= AllocatePool (BufferSize
);
688 if (*Buffer
== NULL
) {
689 return EFI_OUT_OF_RESOURCES
;
692 Status
= CoreLocateHandle (
700 *NumberHandles
= BufferSize
/ sizeof(EFI_HANDLE
);
701 if (EFI_ERROR(Status
)) {