2 Locate handle functions
4 Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 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.
15 #include "PiSmmCore.h"
18 // ProtocolRequest - Last LocateHandle request ID
20 UINTN mEfiLocateHandleRequest
= 0;
23 // Internal prototypes
30 PROTOCOL_ENTRY
*ProtEntry
;
36 IN OUT LOCATE_POSITION
*Position
,
41 Routine to get the next Handle, when you are searching for all handles.
43 @param Position Information about which Handle to seach for.
44 @param Interface Return the interface structure for the matching
47 @return An pointer to IHANDLE if the next Position is not the end of the list.
48 Otherwise,NULL is returned.
52 SmmGetNextLocateAllHandles (
53 IN OUT LOCATE_POSITION
*Position
,
62 Position
->Position
= Position
->Position
->ForwardLink
;
65 // If not at the end of the list, get the handle
69 if (Position
->Position
!= &gHandleList
) {
70 Handle
= CR (Position
->Position
, IHANDLE
, AllHandles
, EFI_HANDLE_SIGNATURE
);
76 Routine to get the next Handle, when you are searching for register 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 SmmGetNextLocateByRegisterNotify (
89 IN OUT LOCATE_POSITION
*Position
,
94 PROTOCOL_NOTIFY
*ProtNotify
;
95 PROTOCOL_INTERFACE
*Prot
;
100 ProtNotify
= Position
->SearchKey
;
103 // If this is the first request, get the next handle
105 if (ProtNotify
!= NULL
) {
106 ASSERT(ProtNotify
->Signature
== PROTOCOL_NOTIFY_SIGNATURE
);
107 Position
->SearchKey
= NULL
;
110 // If not at the end of the list, get the next handle
112 Link
= ProtNotify
->Position
->ForwardLink
;
113 if (Link
!= &ProtNotify
->Protocol
->Protocols
) {
114 Prot
= CR (Link
, PROTOCOL_INTERFACE
, ByProtocol
, PROTOCOL_INTERFACE_SIGNATURE
);
115 Handle
= Prot
->Handle
;
116 *Interface
= Prot
->Interface
;
123 Routine to get the next Handle, when you are searching for a given protocol.
125 @param Position Information about which Handle to seach for.
126 @param Interface Return the interface structure for the matching
129 @return An pointer to IHANDLE if the next Position is not the end of the list.
130 Otherwise,NULL is returned.
134 SmmGetNextLocateByProtocol (
135 IN OUT LOCATE_POSITION
*Position
,
141 PROTOCOL_INTERFACE
*Prot
;
149 Link
= Position
->Position
->ForwardLink
;
150 Position
->Position
= Link
;
153 // If not at the end, return the handle
155 if (Link
== &Position
->ProtEntry
->Protocols
) {
163 Prot
= CR(Link
, PROTOCOL_INTERFACE
, ByProtocol
, PROTOCOL_INTERFACE_SIGNATURE
);
164 Handle
= Prot
->Handle
;
165 *Interface
= Prot
->Interface
;
168 // If this handle has not been returned this request, then
171 if (Handle
->LocateRequest
!= mEfiLocateHandleRequest
) {
172 Handle
->LocateRequest
= mEfiLocateHandleRequest
;
180 Return the first Protocol Interface that matches the Protocol GUID. If
181 Registration is pasased in return a Protocol Instance that was just add
182 to the system. If Retistration is NULL return the first Protocol Interface
185 @param Protocol The protocol to search for
186 @param Registration Optional Registration Key returned from
187 RegisterProtocolNotify()
188 @param Interface Return the Protocol interface (instance).
190 @retval EFI_SUCCESS If a valid Interface is returned
191 @retval EFI_INVALID_PARAMETER Invalid parameter
192 @retval EFI_NOT_FOUND Protocol interface not found
198 IN EFI_GUID
*Protocol
,
199 IN VOID
*Registration OPTIONAL
,
204 LOCATE_POSITION Position
;
205 PROTOCOL_NOTIFY
*ProtNotify
;
208 if ((Interface
== NULL
) || (Protocol
== NULL
)) {
209 return EFI_INVALID_PARAMETER
;
213 Status
= EFI_SUCCESS
;
216 // Set initial position
218 Position
.Protocol
= Protocol
;
219 Position
.SearchKey
= Registration
;
220 Position
.Position
= &gHandleList
;
222 mEfiLocateHandleRequest
+= 1;
224 if (Registration
== NULL
) {
226 // Look up the protocol entry and set the head pointer
228 Position
.ProtEntry
= SmmFindProtocolEntry (Protocol
, FALSE
);
229 if (Position
.ProtEntry
== NULL
) {
230 return EFI_NOT_FOUND
;
232 Position
.Position
= &Position
.ProtEntry
->Protocols
;
234 Handle
= SmmGetNextLocateByProtocol (&Position
, Interface
);
236 Handle
= SmmGetNextLocateByRegisterNotify (&Position
, Interface
);
239 if (Handle
== NULL
) {
240 Status
= EFI_NOT_FOUND
;
241 } else if (Registration
!= NULL
) {
243 // If this is a search by register notify and a handle was
244 // returned, update the register notification position
246 ProtNotify
= Registration
;
247 ProtNotify
->Position
= ProtNotify
->Position
->ForwardLink
;
254 Locates the requested handle(s) and returns them in Buffer.
256 @param SearchType The type of search to perform to locate the
258 @param Protocol The protocol to search for
259 @param SearchKey Dependant on SearchType
260 @param BufferSize On input the size of Buffer. On output the
261 size of data returned.
262 @param Buffer The buffer to return the results in
264 @retval EFI_BUFFER_TOO_SMALL Buffer too small, required buffer size is
265 returned in BufferSize.
266 @retval EFI_INVALID_PARAMETER Invalid parameter
267 @retval EFI_SUCCESS Successfully found the requested handle(s) and
268 returns them in Buffer.
274 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
275 IN EFI_GUID
*Protocol OPTIONAL
,
276 IN VOID
*SearchKey OPTIONAL
,
277 IN OUT UINTN
*BufferSize
,
278 OUT EFI_HANDLE
*Buffer
282 LOCATE_POSITION Position
;
283 PROTOCOL_NOTIFY
*ProtNotify
;
284 CORE_GET_NEXT GetNext
;
287 IHANDLE
**ResultBuffer
;
290 if (BufferSize
== NULL
) {
291 return EFI_INVALID_PARAMETER
;
294 if ((*BufferSize
> 0) && (Buffer
== NULL
)) {
295 return EFI_INVALID_PARAMETER
;
301 // Set initial position
303 Position
.Protocol
= Protocol
;
304 Position
.SearchKey
= SearchKey
;
305 Position
.Position
= &gHandleList
;
308 ResultBuffer
= (IHANDLE
**) Buffer
;
309 Status
= EFI_SUCCESS
;
312 // Get the search function based on type
314 switch (SearchType
) {
316 GetNext
= SmmGetNextLocateAllHandles
;
319 case ByRegisterNotify
:
320 GetNext
= SmmGetNextLocateByRegisterNotify
;
322 // Must have SearchKey for locate ByRegisterNotify
324 if (SearchKey
== NULL
) {
325 Status
= EFI_INVALID_PARAMETER
;
330 GetNext
= SmmGetNextLocateByProtocol
;
331 if (Protocol
== NULL
) {
332 Status
= EFI_INVALID_PARAMETER
;
336 // Look up the protocol entry and set the head pointer
338 Position
.ProtEntry
= SmmFindProtocolEntry (Protocol
, FALSE
);
339 if (Position
.ProtEntry
== NULL
) {
340 Status
= EFI_NOT_FOUND
;
343 Position
.Position
= &Position
.ProtEntry
->Protocols
;
347 Status
= EFI_INVALID_PARAMETER
;
351 if (EFI_ERROR(Status
)) {
356 // Enumerate out the matching handles
358 mEfiLocateHandleRequest
+= 1;
361 // Get the next handle. If no more handles, stop
363 Handle
= GetNext (&Position
, &Interface
);
364 if (NULL
== Handle
) {
369 // Increase the resulting buffer size, and if this handle
372 ResultSize
+= sizeof(Handle
);
373 if (ResultSize
<= *BufferSize
) {
374 *ResultBuffer
= Handle
;
380 // If the result is a zero length buffer, then there were no
383 if (ResultSize
== 0) {
384 Status
= EFI_NOT_FOUND
;
387 // Return the resulting buffer size. If it's larger than what
388 // was passed, then set the error code
390 if (ResultSize
> *BufferSize
) {
391 Status
= EFI_BUFFER_TOO_SMALL
;
394 *BufferSize
= ResultSize
;
396 if (SearchType
== ByRegisterNotify
&& !EFI_ERROR(Status
)) {
397 ASSERT (SearchKey
!= NULL
);
399 // If this is a search by register notify and a handle was
400 // returned, update the register notification position
402 ProtNotify
= SearchKey
;
403 ProtNotify
->Position
= ProtNotify
->Position
->ForwardLink
;
411 Function returns an array of handles that support the requested protocol
412 in a buffer allocated from pool. This is a version of SmmLocateHandle()
413 that allocates a buffer for the caller.
415 @param SearchType Specifies which handle(s) are to be returned.
416 @param Protocol Provides the protocol to search by. This
417 parameter is only valid for SearchType
419 @param SearchKey Supplies the search key depending on the
421 @param NumberHandles The number of handles returned in Buffer.
422 @param Buffer A pointer to the buffer to return the requested
423 array of handles that support Protocol.
425 @retval EFI_SUCCESS The result array of handles was returned.
426 @retval EFI_NOT_FOUND No handles match the search.
427 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the
429 @retval EFI_INVALID_PARAMETER One or more parameters are not valid.
434 SmmLocateHandleBuffer (
435 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
436 IN EFI_GUID
*Protocol OPTIONAL
,
437 IN VOID
*SearchKey OPTIONAL
,
438 IN OUT UINTN
*NumberHandles
,
439 OUT EFI_HANDLE
**Buffer
445 if (NumberHandles
== NULL
) {
446 return EFI_INVALID_PARAMETER
;
449 if (Buffer
== NULL
) {
450 return EFI_INVALID_PARAMETER
;
456 Status
= SmmLocateHandle (
464 // LocateHandleBuffer() returns incorrect status code if SearchType is
467 // Add code to correctly handle expected errors from SmmLocateHandle().
469 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
470 if (Status
!= EFI_INVALID_PARAMETER
) {
471 Status
= EFI_NOT_FOUND
;
476 *Buffer
= AllocatePool (BufferSize
);
477 if (*Buffer
== NULL
) {
478 return EFI_OUT_OF_RESOURCES
;
481 Status
= SmmLocateHandle (
489 *NumberHandles
= BufferSize
/ sizeof(EFI_HANDLE
);
490 if (EFI_ERROR(Status
)) {