TianoCore EDK2 master
Loading...
Searching...
No Matches
Locate.c
Go to the documentation of this file.
1
9#include "DxeMain.h"
10#include "Handle.h"
11
12//
13// ProtocolRequest - Last LocateHandle request ID
14//
15UINTN mEfiLocateHandleRequest = 0;
16
17//
18// Internal prototypes
19//
20
21typedef struct {
22 EFI_GUID *Protocol;
23 VOID *SearchKey;
24 LIST_ENTRY *Position;
25 PROTOCOL_ENTRY *ProtEntry;
27
28typedef
29IHANDLE *
30(*CORE_GET_NEXT) (
31 IN OUT LOCATE_POSITION *Position,
32 OUT VOID **Interface
33 );
34
46IHANDLE *
48 IN OUT LOCATE_POSITION *Position,
49 OUT VOID **Interface
50 );
51
64IHANDLE *
66 IN OUT LOCATE_POSITION *Position,
67 OUT VOID **Interface
68 );
69
81IHANDLE *
83 IN OUT LOCATE_POSITION *Position,
84 OUT VOID **Interface
85 );
86
108 IN EFI_LOCATE_SEARCH_TYPE SearchType,
109 IN EFI_GUID *Protocol OPTIONAL,
110 IN VOID *SearchKey OPTIONAL,
111 IN OUT UINTN *BufferSize,
112 OUT EFI_HANDLE *Buffer
113 )
114{
115 EFI_STATUS Status;
116 LOCATE_POSITION Position;
117 PROTOCOL_NOTIFY *ProtNotify;
118 CORE_GET_NEXT GetNext;
119 UINTN ResultSize;
120 IHANDLE *Handle;
121 IHANDLE **ResultBuffer;
122 VOID *Interface;
123
124 if (BufferSize == NULL) {
125 return EFI_INVALID_PARAMETER;
126 }
127
128 if ((*BufferSize > 0) && (Buffer == NULL)) {
129 return EFI_INVALID_PARAMETER;
130 }
131
132 GetNext = NULL;
133
134 //
135 // Set initial position
136 //
137 Position.Protocol = Protocol;
138 Position.SearchKey = SearchKey;
139 Position.Position = &gHandleList;
140
141 ResultSize = 0;
142 ResultBuffer = (IHANDLE **)Buffer;
143 Status = EFI_SUCCESS;
144
145 //
146 // Get the search function based on type
147 //
148 switch (SearchType) {
149 case AllHandles:
151 break;
152
153 case ByRegisterNotify:
154 //
155 // Must have SearchKey for locate ByRegisterNotify
156 //
157 if (SearchKey == NULL) {
158 Status = EFI_INVALID_PARAMETER;
159 break;
160 }
161
163 break;
164
165 case ByProtocol:
167 if (Protocol == NULL) {
168 Status = EFI_INVALID_PARAMETER;
169 break;
170 }
171
172 //
173 // Look up the protocol entry and set the head pointer
174 //
175 Position.ProtEntry = CoreFindProtocolEntry (Protocol, FALSE);
176 if (Position.ProtEntry == NULL) {
177 Status = EFI_NOT_FOUND;
178 break;
179 }
180
181 Position.Position = &Position.ProtEntry->Protocols;
182 break;
183
184 default:
185 Status = EFI_INVALID_PARAMETER;
186 break;
187 }
188
189 if (EFI_ERROR (Status)) {
190 return Status;
191 }
192
193 ASSERT (GetNext != NULL);
194 //
195 // Enumerate out the matching handles
196 //
197 mEfiLocateHandleRequest += 1;
198 for ( ; ;) {
199 //
200 // Get the next handle. If no more handles, stop
201 //
202 Handle = GetNext (&Position, &Interface);
203 if (NULL == Handle) {
204 break;
205 }
206
207 //
208 // Increase the resulting buffer size, and if this handle
209 // fits return it
210 //
211 ResultSize += sizeof (Handle);
212 if (ResultSize <= *BufferSize) {
213 *ResultBuffer = Handle;
214 ResultBuffer += 1;
215 }
216 }
217
218 //
219 // If the result is a zero length buffer, then there were no
220 // matching handles
221 //
222 if (ResultSize == 0) {
223 Status = EFI_NOT_FOUND;
224 } else {
225 //
226 // Return the resulting buffer size. If it's larger than what
227 // was passed, then set the error code
228 //
229 if (ResultSize > *BufferSize) {
230 Status = EFI_BUFFER_TOO_SMALL;
231 }
232
233 *BufferSize = ResultSize;
234
235 if ((SearchType == ByRegisterNotify) && !EFI_ERROR (Status)) {
236 //
237 // If this is a search by register notify and a handle was
238 // returned, update the register notification position
239 //
240 ASSERT (SearchKey != NULL);
241 ProtNotify = SearchKey;
242 ProtNotify->Position = ProtNotify->Position->ForwardLink;
243 }
244 }
245
246 return Status;
247}
248
268EFIAPI
270 IN EFI_LOCATE_SEARCH_TYPE SearchType,
271 IN EFI_GUID *Protocol OPTIONAL,
272 IN VOID *SearchKey OPTIONAL,
273 IN OUT UINTN *BufferSize,
274 OUT EFI_HANDLE *Buffer
275 )
276{
277 EFI_STATUS Status;
278
279 //
280 // Lock the protocol database
281 //
283 Status = InternalCoreLocateHandle (SearchType, Protocol, SearchKey, BufferSize, Buffer);
285 return Status;
286}
287
299IHANDLE *
301 IN OUT LOCATE_POSITION *Position,
302 OUT VOID **Interface
303 )
304{
305 IHANDLE *Handle;
306
307 //
308 // Next handle
309 //
310 Position->Position = Position->Position->ForwardLink;
311
312 //
313 // If not at the end of the list, get the handle
314 //
315 Handle = NULL;
316 *Interface = NULL;
317 if (Position->Position != &gHandleList) {
318 Handle = CR (Position->Position, IHANDLE, AllHandles, EFI_HANDLE_SIGNATURE);
319 }
320
321 return Handle;
322}
323
336IHANDLE *
338 IN OUT LOCATE_POSITION *Position,
339 OUT VOID **Interface
340 )
341{
342 IHANDLE *Handle;
343 PROTOCOL_NOTIFY *ProtNotify;
344 PROTOCOL_INTERFACE *Prot;
345 LIST_ENTRY *Link;
346
347 Handle = NULL;
348 *Interface = NULL;
349 ProtNotify = Position->SearchKey;
350
351 //
352 // If this is the first request, get the next handle
353 //
354 if (ProtNotify != NULL) {
355 ASSERT (ProtNotify->Signature == PROTOCOL_NOTIFY_SIGNATURE);
356 Position->SearchKey = NULL;
357
358 //
359 // If not at the end of the list, get the next handle
360 //
361 Link = ProtNotify->Position->ForwardLink;
362 if (Link != &ProtNotify->Protocol->Protocols) {
363 Prot = CR (Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_INTERFACE_SIGNATURE);
364 Handle = Prot->Handle;
365 *Interface = Prot->Interface;
366 }
367 }
368
369 return Handle;
370}
371
383IHANDLE *
385 IN OUT LOCATE_POSITION *Position,
386 OUT VOID **Interface
387 )
388{
389 IHANDLE *Handle;
390 LIST_ENTRY *Link;
391 PROTOCOL_INTERFACE *Prot;
392
393 Handle = NULL;
394 *Interface = NULL;
395 for ( ; ;) {
396 //
397 // Next entry
398 //
399 Link = Position->Position->ForwardLink;
400 Position->Position = Link;
401
402 //
403 // If not at the end, return the handle
404 //
405 if (Link == &Position->ProtEntry->Protocols) {
406 Handle = NULL;
407 break;
408 }
409
410 //
411 // Get the handle
412 //
413 Prot = CR (Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_INTERFACE_SIGNATURE);
414 Handle = Prot->Handle;
415 *Interface = Prot->Interface;
416
417 //
418 // If this handle has not been returned this request, then
419 // return it now
420 //
421 if (Handle->LocateRequest != mEfiLocateHandleRequest) {
422 Handle->LocateRequest = mEfiLocateHandleRequest;
423 break;
424 }
425 }
426
427 return Handle;
428}
429
447EFIAPI
449 IN EFI_GUID *Protocol,
450 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,
451 OUT EFI_HANDLE *Device
452 )
453{
454 INTN SourceSize;
455 INTN Size;
456 INTN BestMatch;
457 UINTN HandleCount;
458 UINTN Index;
459 EFI_STATUS Status;
460 EFI_HANDLE *Handles;
461 EFI_HANDLE Handle;
462 EFI_HANDLE BestDevice;
463 EFI_DEVICE_PATH_PROTOCOL *SourcePath;
464 EFI_DEVICE_PATH_PROTOCOL *TmpDevicePath;
465
466 if (Protocol == NULL) {
467 return EFI_INVALID_PARAMETER;
468 }
469
470 if ((DevicePath == NULL) || (*DevicePath == NULL)) {
471 return EFI_INVALID_PARAMETER;
472 }
473
474 Handles = NULL;
475 BestDevice = NULL;
476 SourcePath = *DevicePath;
477 TmpDevicePath = SourcePath;
478 while (!IsDevicePathEnd (TmpDevicePath)) {
479 if (IsDevicePathEndInstance (TmpDevicePath)) {
480 //
481 // If DevicePath is a multi-instance device path,
482 // the function will operate on the first instance
483 //
484 break;
485 }
486
487 TmpDevicePath = NextDevicePathNode (TmpDevicePath);
488 }
489
490 SourceSize = (UINTN)TmpDevicePath - (UINTN)SourcePath;
491
492 //
493 // Get a list of all handles that support the requested protocol
494 //
495 Status = CoreLocateHandleBuffer (ByProtocol, Protocol, NULL, &HandleCount, &Handles);
496 if (EFI_ERROR (Status) || (HandleCount == 0)) {
497 return EFI_NOT_FOUND;
498 }
499
500 BestMatch = -1;
501 for (Index = 0; Index < HandleCount; Index += 1) {
502 Handle = Handles[Index];
503 Status = CoreHandleProtocol (Handle, &gEfiDevicePathProtocolGuid, (VOID **)&TmpDevicePath);
504 if (EFI_ERROR (Status)) {
505 //
506 // If this handle doesn't support device path, then skip it
507 //
508 continue;
509 }
510
511 //
512 // Check if DevicePath is first part of SourcePath
513 //
514 Size = GetDevicePathSize (TmpDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL);
515 ASSERT (Size >= 0);
516 if ((Size <= SourceSize) && (CompareMem (SourcePath, TmpDevicePath, (UINTN)Size) == 0)) {
517 //
518 // If the size is equal to the best match, then we
519 // have a duplicate device path for 2 different device
520 // handles
521 //
522 ASSERT (Size != BestMatch);
523
524 //
525 // We've got a match, see if it's the best match so far
526 //
527 if (Size > BestMatch) {
528 BestMatch = Size;
529 BestDevice = Handle;
530 }
531 }
532 }
533
534 CoreFreePool (Handles);
535
536 //
537 // If there wasn't any match, then no parts of the device path was found.
538 // Which is strange since there is likely a "root level" device path in the system.
539 //
540 if (BestMatch == -1) {
541 return EFI_NOT_FOUND;
542 }
543
544 if (Device == NULL) {
545 return EFI_INVALID_PARAMETER;
546 }
547
548 *Device = BestDevice;
549
550 //
551 // Return the remaining part of the device path
552 //
553 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)(((UINT8 *)SourcePath) + BestMatch);
554 return EFI_SUCCESS;
555}
556
574EFIAPI
576 IN EFI_GUID *Protocol,
577 IN VOID *Registration OPTIONAL,
578 OUT VOID **Interface
579 )
580{
581 EFI_STATUS Status;
582 LOCATE_POSITION Position;
583 PROTOCOL_NOTIFY *ProtNotify;
584 IHANDLE *Handle;
585
586 if ((Interface == NULL) || (Protocol == NULL)) {
587 return EFI_INVALID_PARAMETER;
588 }
589
590 *Interface = NULL;
591 Status = EFI_SUCCESS;
592
593 //
594 // Set initial position
595 //
596 Position.Protocol = Protocol;
597 Position.SearchKey = Registration;
598 Position.Position = &gHandleList;
599
600 //
601 // Lock the protocol database
602 //
603 Status = CoreAcquireLockOrFail (&gProtocolDatabaseLock);
604 if (EFI_ERROR (Status)) {
605 return EFI_NOT_FOUND;
606 }
607
608 mEfiLocateHandleRequest += 1;
609
610 if (Registration == NULL) {
611 //
612 // Look up the protocol entry and set the head pointer
613 //
614 Position.ProtEntry = CoreFindProtocolEntry (Protocol, FALSE);
615 if (Position.ProtEntry == NULL) {
616 Status = EFI_NOT_FOUND;
617 goto Done;
618 }
619
620 Position.Position = &Position.ProtEntry->Protocols;
621
622 Handle = CoreGetNextLocateByProtocol (&Position, Interface);
623 } else {
624 Handle = CoreGetNextLocateByRegisterNotify (&Position, Interface);
625 }
626
627 if (Handle == NULL) {
628 Status = EFI_NOT_FOUND;
629 } else if (Registration != NULL) {
630 //
631 // If this is a search by register notify and a handle was
632 // returned, update the register notification position
633 //
634 ProtNotify = Registration;
635 ProtNotify->Position = ProtNotify->Position->ForwardLink;
636 }
637
638Done:
640 return Status;
641}
642
666EFIAPI
668 IN EFI_LOCATE_SEARCH_TYPE SearchType,
669 IN EFI_GUID *Protocol OPTIONAL,
670 IN VOID *SearchKey OPTIONAL,
671 IN OUT UINTN *NumberHandles,
672 OUT EFI_HANDLE **Buffer
673 )
674{
675 EFI_STATUS Status;
676 UINTN BufferSize;
677
678 if (NumberHandles == NULL) {
679 return EFI_INVALID_PARAMETER;
680 }
681
682 if (Buffer == NULL) {
683 return EFI_INVALID_PARAMETER;
684 }
685
686 BufferSize = 0;
687 *NumberHandles = 0;
688 *Buffer = NULL;
689
690 //
691 // Lock the protocol database
692 //
694 Status = InternalCoreLocateHandle (
695 SearchType,
696 Protocol,
697 SearchKey,
698 &BufferSize,
699 *Buffer
700 );
701 //
702 // LocateHandleBuffer() returns incorrect status code if SearchType is
703 // invalid.
704 //
705 // Add code to correctly handle expected errors from CoreLocateHandle().
706 //
707 if (EFI_ERROR (Status) && (Status != EFI_BUFFER_TOO_SMALL)) {
708 if (Status != EFI_INVALID_PARAMETER) {
709 Status = EFI_NOT_FOUND;
710 }
711
713 return Status;
714 }
715
716 *Buffer = AllocatePool (BufferSize);
717 if (*Buffer == NULL) {
719 return EFI_OUT_OF_RESOURCES;
720 }
721
722 Status = InternalCoreLocateHandle (
723 SearchType,
724 Protocol,
725 SearchKey,
726 &BufferSize,
727 *Buffer
728 );
729
730 *NumberHandles = BufferSize / sizeof (EFI_HANDLE);
731 if (EFI_ERROR (Status)) {
732 *NumberHandles = 0;
733 if (*Buffer != NULL) {
734 CoreFreePool (*Buffer);
735 *Buffer = NULL;
736 }
737 }
738
740 return Status;
741}
UINT64 UINTN
INT64 INTN
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
BOOLEAN EFIAPI IsDevicePathEnd(IN CONST VOID *Node)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI NextDevicePathNode(IN CONST VOID *Node)
UINTN EFIAPI GetDevicePathSize(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath)
BOOLEAN EFIAPI IsDevicePathEndInstance(IN CONST VOID *Node)
EFI_STATUS CoreAcquireLockOrFail(IN EFI_LOCK *Lock)
Definition: Library.c:29
EFI_STATUS EFIAPI CoreHandleProtocol(IN EFI_HANDLE UserHandle, IN EFI_GUID *Protocol, OUT VOID **Interface)
Definition: Handle.c:1040
EFI_STATUS EFIAPI CoreFreePool(IN VOID *Buffer)
Definition: Pool.c:595
PROTOCOL_ENTRY * CoreFindProtocolEntry(IN EFI_GUID *Protocol, IN BOOLEAN Create)
Definition: Handle.c:144
VOID CoreReleaseProtocolLock(VOID)
Definition: Handle.c:41
VOID CoreAcquireProtocolLock(VOID)
Definition: Handle.c:29
IHANDLE * CoreGetNextLocateAllHandles(IN OUT LOCATE_POSITION *Position, OUT VOID **Interface)
Definition: Locate.c:300
EFI_STATUS InternalCoreLocateHandle(IN EFI_LOCATE_SEARCH_TYPE SearchType, IN EFI_GUID *Protocol OPTIONAL, IN VOID *SearchKey OPTIONAL, IN OUT UINTN *BufferSize, OUT EFI_HANDLE *Buffer)
Definition: Locate.c:107
EFI_STATUS EFIAPI CoreLocateProtocol(IN EFI_GUID *Protocol, IN VOID *Registration OPTIONAL, OUT VOID **Interface)
Definition: Locate.c:575
EFI_STATUS EFIAPI CoreLocateHandleBuffer(IN EFI_LOCATE_SEARCH_TYPE SearchType, IN EFI_GUID *Protocol OPTIONAL, IN VOID *SearchKey OPTIONAL, IN OUT UINTN *NumberHandles, OUT EFI_HANDLE **Buffer)
Definition: Locate.c:667
IHANDLE * CoreGetNextLocateByProtocol(IN OUT LOCATE_POSITION *Position, OUT VOID **Interface)
Definition: Locate.c:384
EFI_STATUS EFIAPI CoreLocateDevicePath(IN EFI_GUID *Protocol, IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath, OUT EFI_HANDLE *Device)
Definition: Locate.c:448
EFI_STATUS EFIAPI CoreLocateHandle(IN EFI_LOCATE_SEARCH_TYPE SearchType, IN EFI_GUID *Protocol OPTIONAL, IN VOID *SearchKey OPTIONAL, IN OUT UINTN *BufferSize, OUT EFI_HANDLE *Buffer)
Definition: Locate.c:269
IHANDLE * CoreGetNextLocateByRegisterNotify(IN OUT LOCATE_POSITION *Position, OUT VOID **Interface)
Definition: Locate.c:337
#define NULL
Definition: Base.h:319
#define VOID
Definition: Base.h:269
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define CR(Record, TYPE, Field, TestSignature)
Definition: DebugLib.h:660
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_LOCATE_SEARCH_TYPE
Definition: UefiSpec.h:1515
@ ByProtocol
Definition: UefiSpec.h:1528
@ ByRegisterNotify
Definition: UefiSpec.h:1523
@ AllHandles
Definition: UefiSpec.h:1519
Definition: Base.h:213
Definition: Handle.h:17
LIST_ENTRY Protocols
All protocol interfaces.
Definition: Handle.h:44
IHANDLE * Handle
Back pointer.
Definition: Handle.h:60
VOID * Interface
The interface value.
Definition: Handle.h:66
LIST_ENTRY * Position
Last position notified.
Definition: Handle.h:98