]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c
ArmPlatformPkg: Fixed build
[mirror_edk2.git] / ShellPkg / Library / UefiHandleParsingLib / UefiHandleParsingLib.c
1 /** @file
2 Provides interface to advanced shell functionality for parsing both handle and protocol database.
3
4 Copyright (c) 2013 - 2014, Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "UefiHandleParsingLib.h"
17 #include "IndustryStandard/Acpi10.h"
18
19 EFI_HANDLE mHandleParsingHiiHandle = NULL;
20 HANDLE_INDEX_LIST mHandleList = {{{NULL,NULL},0,0},0};
21 GUID_INFO_BLOCK *GuidList;
22 UINTN GuidListCount;
23 /**
24 Function to translate the EFI_MEMORY_TYPE into a string.
25
26 @param[in] Memory The memory type.
27
28 @retval A string representation of the type allocated from BS Pool.
29 **/
30 CHAR16*
31 EFIAPI
32 ConvertMemoryType (
33 IN CONST EFI_MEMORY_TYPE Memory
34 )
35 {
36 CHAR16 *RetVal;
37 RetVal = NULL;
38
39 switch (Memory) {
40 case EfiReservedMemoryType: StrnCatGrow(&RetVal, NULL, L"EfiReservedMemoryType", 0); break;
41 case EfiLoaderCode: StrnCatGrow(&RetVal, NULL, L"EfiLoaderCode", 0); break;
42 case EfiLoaderData: StrnCatGrow(&RetVal, NULL, L"EfiLoaderData", 0); break;
43 case EfiBootServicesCode: StrnCatGrow(&RetVal, NULL, L"EfiBootServicesCode", 0); break;
44 case EfiBootServicesData: StrnCatGrow(&RetVal, NULL, L"EfiBootServicesData", 0); break;
45 case EfiRuntimeServicesCode: StrnCatGrow(&RetVal, NULL, L"EfiRuntimeServicesCode", 0); break;
46 case EfiRuntimeServicesData: StrnCatGrow(&RetVal, NULL, L"EfiRuntimeServicesData", 0); break;
47 case EfiConventionalMemory: StrnCatGrow(&RetVal, NULL, L"EfiConventionalMemory", 0); break;
48 case EfiUnusableMemory: StrnCatGrow(&RetVal, NULL, L"EfiUnusableMemory", 0); break;
49 case EfiACPIReclaimMemory: StrnCatGrow(&RetVal, NULL, L"EfiACPIReclaimMemory", 0); break;
50 case EfiACPIMemoryNVS: StrnCatGrow(&RetVal, NULL, L"EfiACPIMemoryNVS", 0); break;
51 case EfiMemoryMappedIO: StrnCatGrow(&RetVal, NULL, L"EfiMemoryMappedIO", 0); break;
52 case EfiMemoryMappedIOPortSpace: StrnCatGrow(&RetVal, NULL, L"EfiMemoryMappedIOPortSpace", 0); break;
53 case EfiPalCode: StrnCatGrow(&RetVal, NULL, L"EfiPalCode", 0); break;
54 case EfiMaxMemoryType: StrnCatGrow(&RetVal, NULL, L"EfiMaxMemoryType", 0); break;
55 default: ASSERT(FALSE);
56 }
57 return (RetVal);
58 }
59
60 /**
61 Function to translate the EFI_GRAPHICS_PIXEL_FORMAT into a string.
62
63 @param[in] Fmt The format type.
64
65 @retval A string representation of the type allocated from BS Pool.
66 **/
67 CHAR16*
68 EFIAPI
69 ConvertPixelFormat (
70 IN CONST EFI_GRAPHICS_PIXEL_FORMAT Fmt
71 )
72 {
73 CHAR16 *RetVal;
74 RetVal = NULL;
75
76 switch (Fmt) {
77 case PixelRedGreenBlueReserved8BitPerColor: StrnCatGrow(&RetVal, NULL, L"PixelRedGreenBlueReserved8BitPerColor", 0); break;
78 case PixelBlueGreenRedReserved8BitPerColor: StrnCatGrow(&RetVal, NULL, L"PixelBlueGreenRedReserved8BitPerColor", 0); break;
79 case PixelBitMask: StrnCatGrow(&RetVal, NULL, L"PixelBitMask", 0); break;
80 case PixelBltOnly: StrnCatGrow(&RetVal, NULL, L"PixelBltOnly", 0); break;
81 case PixelFormatMax: StrnCatGrow(&RetVal, NULL, L"PixelFormatMax", 0); break;
82 default: ASSERT(FALSE);
83 }
84 return (RetVal);
85 }
86
87 /**
88 Constructor for the library.
89
90 @param[in] ImageHandle Ignored.
91 @param[in] SystemTable Ignored.
92
93 @retval EFI_SUCCESS The operation was successful.
94 **/
95 EFI_STATUS
96 EFIAPI
97 HandleParsingLibConstructor (
98 IN EFI_HANDLE ImageHandle,
99 IN EFI_SYSTEM_TABLE *SystemTable
100 )
101 {
102 GuidListCount = 0;
103 GuidList = NULL;
104
105 //
106 // Do nothing with mHandleParsingHiiHandle. Initialize HII as needed.
107 //
108 return (EFI_SUCCESS);
109 }
110
111 /**
112 Initialization function for HII packages.
113
114 **/
115 VOID
116 HandleParsingHiiInit (VOID)
117 {
118 if (mHandleParsingHiiHandle == NULL) {
119 mHandleParsingHiiHandle = HiiAddPackages (&gHandleParsingHiiGuid, gImageHandle, UefiHandleParsingLibStrings, NULL);
120 ASSERT (mHandleParsingHiiHandle != NULL);
121 }
122 }
123
124 /**
125 Destructor for the library. free any resources.
126
127 @param[in] ImageHandle Ignored.
128 @param[in] SystemTable Ignored.
129
130 @retval EFI_SUCCESS The operation was successful.
131 **/
132 EFI_STATUS
133 EFIAPI
134 HandleParsingLibDestructor (
135 IN EFI_HANDLE ImageHandle,
136 IN EFI_SYSTEM_TABLE *SystemTable
137 )
138 {
139 UINTN LoopCount;
140
141 for (LoopCount = 0; GuidList != NULL && LoopCount < GuidListCount; LoopCount++) {
142 SHELL_FREE_NON_NULL(GuidList[LoopCount].GuidId);
143 }
144
145 SHELL_FREE_NON_NULL(GuidList);
146 if (mHandleParsingHiiHandle != NULL) {
147 HiiRemovePackages(mHandleParsingHiiHandle);
148 }
149 return (EFI_SUCCESS);
150 }
151
152 /**
153 Function to dump information about LoadedImage.
154
155 This will allocate the return buffer from boot services pool.
156
157 @param[in] TheHandle The handle that has LoadedImage installed.
158 @param[in] Verbose TRUE for additional information, FALSE otherwise.
159
160 @retval A poitner to a string containing the information.
161 **/
162 CHAR16*
163 EFIAPI
164 LoadedImageProtocolDumpInformation(
165 IN CONST EFI_HANDLE TheHandle,
166 IN CONST BOOLEAN Verbose
167 )
168 {
169 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
170 EFI_STATUS Status;
171 CHAR16 *RetVal;
172 CHAR16 *Temp;
173 CHAR16 *CodeType;
174 CHAR16 *DataType;
175
176 if (!Verbose) {
177 return (CatSPrint(NULL, L"LoadedImage"));
178 }
179
180 HandleParsingHiiInit();
181
182 Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_LI_DUMP_MAIN), NULL);
183 RetVal = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
184 if (Temp == NULL || RetVal == NULL) {
185 SHELL_FREE_NON_NULL(Temp);
186 SHELL_FREE_NON_NULL(RetVal);
187 return NULL;
188 }
189
190 Status = gBS->OpenProtocol (
191 TheHandle,
192 &gEfiLoadedImageProtocolGuid,
193 (VOID**)&LoadedImage,
194 gImageHandle,
195 NULL,
196 EFI_OPEN_PROTOCOL_GET_PROTOCOL
197 );
198
199 if (EFI_ERROR (Status)) {
200 SHELL_FREE_NON_NULL (Temp);
201 SHELL_FREE_NON_NULL (RetVal);
202 return NULL;
203 }
204
205 DataType = ConvertMemoryType(LoadedImage->ImageDataType);
206 CodeType = ConvertMemoryType(LoadedImage->ImageCodeType);
207
208 RetVal = CatSPrint(RetVal,
209 Temp,
210 LoadedImage->Revision,
211 LoadedImage->ParentHandle,
212 LoadedImage->SystemTable,
213 LoadedImage->DeviceHandle,
214 LoadedImage->FilePath,
215 LoadedImage->LoadOptionsSize,
216 LoadedImage->LoadOptions,
217 LoadedImage->ImageBase,
218 LoadedImage->ImageSize,
219 CodeType,
220 DataType,
221 LoadedImage->Unload);
222
223
224 SHELL_FREE_NON_NULL(Temp);
225 SHELL_FREE_NON_NULL(CodeType);
226 SHELL_FREE_NON_NULL(DataType);
227
228 return RetVal;
229 }
230
231 /**
232 Function to dump information about GOP.
233
234 This will allocate the return buffer from boot services pool.
235
236 @param[in] TheHandle The handle that has LoadedImage installed.
237 @param[in] Verbose TRUE for additional information, FALSE otherwise.
238
239 @retval A poitner to a string containing the information.
240 **/
241 CHAR16*
242 EFIAPI
243 GraphicsOutputProtocolDumpInformation(
244 IN CONST EFI_HANDLE TheHandle,
245 IN CONST BOOLEAN Verbose
246 )
247 {
248 EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
249 EFI_STATUS Status;
250 CHAR16 *RetVal;
251 CHAR16 *Temp;
252 CHAR16 *Fmt;
253
254 if (!Verbose) {
255 return (CatSPrint(NULL, L"GraphicsOutput"));
256 }
257
258 HandleParsingHiiInit();
259
260 Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_GOP_DUMP_MAIN), NULL);
261 RetVal = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
262 if (Temp == NULL || RetVal == NULL) {
263 SHELL_FREE_NON_NULL(Temp);
264 SHELL_FREE_NON_NULL(RetVal);
265 return NULL;
266 }
267
268 Status = gBS->OpenProtocol (
269 TheHandle,
270 &gEfiGraphicsOutputProtocolGuid,
271 (VOID**)&GraphicsOutput,
272 gImageHandle,
273 NULL,
274 EFI_OPEN_PROTOCOL_GET_PROTOCOL
275 );
276
277 if (EFI_ERROR (Status)) {
278 SHELL_FREE_NON_NULL (Temp);
279 SHELL_FREE_NON_NULL (RetVal);
280 return NULL;
281 }
282
283 Fmt = ConvertPixelFormat(GraphicsOutput->Mode->Info->PixelFormat);
284
285 RetVal = CatSPrint(RetVal,
286 Temp,
287 GraphicsOutput->Mode->MaxMode,
288 GraphicsOutput->Mode->Mode,
289 GraphicsOutput->Mode->FrameBufferBase,
290 (UINT64)GraphicsOutput->Mode->FrameBufferSize,
291 (UINT64)GraphicsOutput->Mode->SizeOfInfo,
292 GraphicsOutput->Mode->Info->Version,
293 GraphicsOutput->Mode->Info->HorizontalResolution,
294 GraphicsOutput->Mode->Info->VerticalResolution,
295 Fmt,
296 GraphicsOutput->Mode->Info->PixelsPerScanLine,
297 GraphicsOutput->Mode->Info->PixelFormat!=PixelBitMask?0:GraphicsOutput->Mode->Info->PixelInformation.RedMask,
298 GraphicsOutput->Mode->Info->PixelFormat!=PixelBitMask?0:GraphicsOutput->Mode->Info->PixelInformation.GreenMask,
299 GraphicsOutput->Mode->Info->PixelFormat!=PixelBitMask?0:GraphicsOutput->Mode->Info->PixelInformation.BlueMask
300 );
301
302 SHELL_FREE_NON_NULL(Temp);
303 SHELL_FREE_NON_NULL(Fmt);
304
305 return RetVal;
306 }
307
308 /**
309 Function to dump information about PciRootBridgeIo.
310
311 This will allocate the return buffer from boot services pool.
312
313 @param[in] TheHandle The handle that has PciRootBridgeIo installed.
314 @param[in] Verbose TRUE for additional information, FALSE otherwise.
315
316 @retval A poitner to a string containing the information.
317 **/
318 CHAR16*
319 EFIAPI
320 PciRootBridgeIoDumpInformation(
321 IN CONST EFI_HANDLE TheHandle,
322 IN CONST BOOLEAN Verbose
323 )
324 {
325 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;
326 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Configuration;
327 UINT64 Supports;
328 UINT64 Attributes;
329 CHAR16 *Temp;
330 CHAR16 *Temp2;
331 CHAR16 *RetVal;
332 EFI_STATUS Status;
333
334 RetVal = NULL;
335
336 if (!Verbose) {
337 return (CatSPrint(NULL, L"PciRootBridgeIo"));
338 }
339
340 HandleParsingHiiInit();
341
342 Status = gBS->HandleProtocol(
343 TheHandle,
344 &gEfiPciRootBridgeIoProtocolGuid,
345 (VOID**)&PciRootBridgeIo);
346
347 if (EFI_ERROR(Status)) {
348 return NULL;
349 }
350
351 Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_PCIRB_DUMP_PH), NULL);
352 ASSERT (Temp != NULL);
353 Temp2 = CatSPrint(L"\r\n", Temp, PciRootBridgeIo->ParentHandle);
354 FreePool(Temp);
355 RetVal = Temp2;
356 Temp2 = NULL;
357
358 Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_PCIRB_DUMP_SEG), NULL);
359 ASSERT (Temp != NULL);
360 Temp2 = CatSPrint(RetVal, Temp, PciRootBridgeIo->SegmentNumber);
361 FreePool(Temp);
362 FreePool(RetVal);
363 RetVal = Temp2;
364 Temp2 = NULL;
365
366 Supports = 0;
367 Attributes = 0;
368 Status = PciRootBridgeIo->GetAttributes (PciRootBridgeIo, &Supports, &Attributes);
369 if (!EFI_ERROR(Status)) {
370 Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_PCIRB_DUMP_ATT), NULL);
371 ASSERT (Temp != NULL);
372 Temp2 = CatSPrint(RetVal, Temp, Attributes);
373 FreePool(Temp);
374 FreePool(RetVal);
375 RetVal = Temp2;
376 Temp2 = NULL;
377
378 Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_PCIRB_DUMP_SUPPORTS), NULL);
379 ASSERT (Temp != NULL);
380 Temp2 = CatSPrint(RetVal, Temp, Supports);
381 FreePool(Temp);
382 FreePool(RetVal);
383 RetVal = Temp2;
384 Temp2 = NULL;
385 }
386
387 Configuration = NULL;
388 Status = PciRootBridgeIo->Configuration (PciRootBridgeIo, (VOID **) &Configuration);
389 if (!EFI_ERROR(Status) && Configuration != NULL) {
390 Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_PCIRB_DUMP_TITLE), NULL);
391 ASSERT (Temp != NULL);
392 Temp2 = CatSPrint(RetVal, Temp, Supports);
393 FreePool(Temp);
394 FreePool(RetVal);
395 RetVal = Temp2;
396 Temp2 = NULL;
397 while (Configuration->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR) {
398 Temp = NULL;
399 switch (Configuration->ResType) {
400 case ACPI_ADDRESS_SPACE_TYPE_MEM:
401 Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_PCIRB_DUMP_MEM), NULL);
402 break;
403 case ACPI_ADDRESS_SPACE_TYPE_IO:
404 Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_PCIRB_DUMP_IO), NULL);
405 break;
406 case ACPI_ADDRESS_SPACE_TYPE_BUS:
407 Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_PCIRB_DUMP_BUS), NULL);
408 break;
409 }
410 if (Temp != NULL) {
411 Temp2 = CatSPrint(RetVal, L"%s", Temp);
412 FreePool(Temp);
413 FreePool(RetVal);
414 RetVal = Temp2;
415 Temp2 = NULL;
416 }
417
418 Temp2 = CatSPrint(RetVal,
419 L"%H%02x %016lx %016lx %02x%N\r\n",
420 Configuration->SpecificFlag,
421 Configuration->AddrRangeMin,
422 Configuration->AddrRangeMax,
423 Configuration->AddrSpaceGranularity
424 );
425 FreePool(RetVal);
426 RetVal = Temp2;
427 Temp2 = NULL;
428 Configuration++;
429 }
430 }
431 return (RetVal);
432 }
433
434 /**
435 Function to dump information about SimpleTextOut.
436
437 This will allocate the return buffer from boot services pool.
438
439 @param[in] TheHandle The handle that has SimpleTextOut installed.
440 @param[in] Verbose TRUE for additional information, FALSE otherwise.
441
442 @retval A poitner to a string containing the information.
443 **/
444 CHAR16*
445 EFIAPI
446 TxtOutProtocolDumpInformation(
447 IN CONST EFI_HANDLE TheHandle,
448 IN CONST BOOLEAN Verbose
449 )
450 {
451 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *Dev;
452 INTN Index;
453 UINTN Col;
454 UINTN Row;
455 EFI_STATUS Status;
456 CHAR16 *RetVal;
457 UINTN Size;
458 CHAR16 *Temp;
459 UINTN NewSize;
460
461 if (!Verbose) {
462 return (NULL);
463 }
464
465 HandleParsingHiiInit();
466
467 RetVal = NULL;
468 Size = 0;
469
470 Status = gBS->HandleProtocol(
471 TheHandle,
472 &gEfiSimpleTextOutProtocolGuid,
473 (VOID**)&Dev);
474
475 ASSERT_EFI_ERROR(Status);
476 ASSERT (Dev != NULL && Dev->Mode != NULL);
477
478 Size = (Dev->Mode->MaxMode + 1) * 80;
479 RetVal = AllocateZeroPool(Size);
480
481 Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_TXT_OUT_DUMP_HEADER), NULL);
482 if (Temp != NULL) {
483 UnicodeSPrint(RetVal, Size, Temp, Dev, Dev->Mode->Attribute);
484 FreePool(Temp);
485 }
486
487 //
488 // Dump TextOut Info
489 //
490 Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_TXT_OUT_DUMP_LINE), NULL);
491 for (Index = 0; Index < Dev->Mode->MaxMode; Index++) {
492 Status = Dev->QueryMode (Dev, Index, &Col, &Row);
493 NewSize = Size - StrSize(RetVal);
494 UnicodeSPrint(
495 RetVal + StrLen(RetVal),
496 NewSize,
497 Temp == NULL?L"":Temp,
498 Index == Dev->Mode->Mode ? L'*' : L' ',
499 Index,
500 !EFI_ERROR(Status)?(INTN)Col:-1,
501 !EFI_ERROR(Status)?(INTN)Row:-1
502 );
503 }
504 FreePool(Temp);
505 return (RetVal);
506 }
507
508 STATIC CONST UINTN VersionStringSize = 60;
509
510 /**
511 Function to dump information about EfiDriverSupportedEfiVersion protocol.
512
513 This will allocate the return buffer from boot services pool.
514
515 @param[in] TheHandle The handle that has the protocol installed.
516 @param[in] Verbose TRUE for additional information, FALSE otherwise.
517
518 @retval A poitner to a string containing the information.
519 **/
520 CHAR16*
521 EFIAPI
522 DriverEfiVersionProtocolDumpInformation(
523 IN CONST EFI_HANDLE TheHandle,
524 IN CONST BOOLEAN Verbose
525 )
526 {
527 EFI_DRIVER_SUPPORTED_EFI_VERSION_PROTOCOL *DriverEfiVersion;
528 EFI_STATUS Status;
529 CHAR16 *RetVal;
530
531 Status = gBS->HandleProtocol(
532 TheHandle,
533 &gEfiDriverSupportedEfiVersionProtocolGuid,
534 (VOID**)&DriverEfiVersion);
535
536 ASSERT_EFI_ERROR(Status);
537
538 RetVal = AllocateZeroPool(VersionStringSize);
539 ASSERT(RetVal != NULL);
540 UnicodeSPrint(RetVal, VersionStringSize, L"0x%08x", DriverEfiVersion->FirmwareVersion);
541 return (RetVal);
542 }
543
544 /**
545 Function to dump information about DevicePath protocol.
546
547 This will allocate the return buffer from boot services pool.
548
549 @param[in] TheHandle The handle that has the protocol installed.
550 @param[in] Verbose TRUE for additional information, FALSE otherwise.
551
552 @retval A poitner to a string containing the information.
553 **/
554 CHAR16*
555 EFIAPI
556 DevicePathProtocolDumpInformation(
557 IN CONST EFI_HANDLE TheHandle,
558 IN CONST BOOLEAN Verbose
559 )
560 {
561 EFI_DEVICE_PATH_PROTOCOL *DevPath;
562 CHAR16 *Temp;
563 CHAR16 *Temp2;
564 EFI_STATUS Status;
565 Temp = NULL;
566
567 Status = gBS->OpenProtocol(TheHandle, &gEfiDevicePathProtocolGuid, (VOID**)&DevPath, gImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
568 if (!EFI_ERROR(Status)) {
569 //
570 // I cannot decide whether to allow shortcuts here (the second BOOLEAN on the next line)
571 //
572 Temp = ConvertDevicePathToText(DevPath, TRUE, TRUE);
573 gBS->CloseProtocol(TheHandle, &gEfiDevicePathProtocolGuid, gImageHandle, NULL);
574 }
575 if (!Verbose && Temp != NULL && StrLen(Temp) > 30) {
576 Temp2 = NULL;
577 Temp2 = StrnCatGrow(&Temp2, NULL, Temp+(StrLen(Temp) - 30), 30);
578 FreePool(Temp);
579 Temp = Temp2;
580 }
581 return (Temp);
582 }
583
584 /**
585 Function to dump information about EfiAdapterInformation Protocol.
586
587 @param[in] TheHandle The handle that has the protocol installed.
588 @param[in] Verbose TRUE for additional information, FALSE otherwise.
589
590 @retval A pointer to a string containing the information.
591 **/
592 CHAR16*
593 EFIAPI
594 AdapterInformationDumpInformation (
595 IN CONST EFI_HANDLE TheHandle,
596 IN CONST BOOLEAN Verbose
597 )
598 {
599 EFI_STATUS Status;
600 EFI_ADAPTER_INFORMATION_PROTOCOL *EfiAdptrInfoProtocol;
601 UINTN InfoTypesBufferCount;
602 UINTN GuidIndex;
603 EFI_GUID *InfoTypesBuffer;
604 CHAR16 *GuidStr;
605 CHAR16 *TempStr;
606 CHAR16 *RetVal;
607 VOID *InformationBlock;
608 UINTN InformationBlockSize;
609
610 if (!Verbose) {
611 return (CatSPrint(NULL, L"AdapterInfo"));
612 }
613
614 //
615 // Allocate print buffer to store data
616 //
617 RetVal = AllocateZeroPool (PcdGet16(PcdShellPrintBufferSize));
618 if (RetVal == NULL) {
619 return NULL;
620 }
621
622 Status = gBS->OpenProtocol (
623 (EFI_HANDLE) (TheHandle),
624 &gEfiAdapterInformationProtocolGuid,
625 (VOID **) &EfiAdptrInfoProtocol,
626 NULL,
627 NULL,
628 EFI_OPEN_PROTOCOL_GET_PROTOCOL
629 );
630
631 if (EFI_ERROR (Status)) {
632 SHELL_FREE_NON_NULL (RetVal);
633 return NULL;
634 }
635
636 //
637 // Get a list of supported information types for this instance of the protocol.
638 //
639 Status = EfiAdptrInfoProtocol->GetSupportedTypes (
640 EfiAdptrInfoProtocol,
641 &InfoTypesBuffer,
642 &InfoTypesBufferCount
643 );
644 if (EFI_ERROR (Status)) {
645 TempStr = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN(STR_GET_SUPP_TYPES_FAILED), NULL);
646 RetVal = CatSPrint (RetVal, TempStr, Status);
647 } else {
648 TempStr = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN(STR_SUPP_TYPE_HEADER), NULL);
649 RetVal = CatSPrint (RetVal, TempStr);
650 SHELL_FREE_NON_NULL (TempStr);
651
652 for (GuidIndex = 0; GuidIndex < InfoTypesBufferCount; GuidIndex++) {
653 TempStr = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN(STR_GUID_NUMBER), NULL);
654 RetVal = CatSPrint (RetVal, TempStr, (GuidIndex + 1), InfoTypesBuffer[GuidIndex]);
655 SHELL_FREE_NON_NULL (TempStr);
656
657 TempStr = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN(STR_GUID_STRING), NULL);
658
659 if (CompareGuid (&InfoTypesBuffer[GuidIndex], &gEfiAdapterInfoMediaStateGuid)) {
660 RetVal = CatSPrint (RetVal, TempStr, L"gEfiAdapterInfoMediaStateGuid");
661 } else if (CompareGuid (&InfoTypesBuffer[GuidIndex], &gEfiAdapterInfoNetworkBootGuid)) {
662 RetVal = CatSPrint (RetVal, TempStr, L"gEfiAdapterInfoNetworkBootGuid");
663 } else if (CompareGuid (&InfoTypesBuffer[GuidIndex], &gEfiAdapterInfoSanMacAddressGuid)) {
664 RetVal = CatSPrint (RetVal, TempStr, L"gEfiAdapterInfoSanMacAddressGuid");
665 } else {
666
667 GuidStr = GetStringNameFromGuid (&InfoTypesBuffer[GuidIndex], NULL);
668
669 if (GuidStr != NULL) {
670 if (StrCmp(GuidStr, L"UnknownDevice") == 0) {
671 RetVal = CatSPrint (RetVal, TempStr, L"UnknownInfoType");
672
673 SHELL_FREE_NON_NULL (TempStr);
674 SHELL_FREE_NON_NULL(GuidStr);
675 //
676 // So that we never have to pass this UnknownInfoType to the parsing function "GetInformation" service of AIP
677 //
678 continue;
679 } else {
680 RetVal = CatSPrint (RetVal, TempStr, GuidStr);
681 SHELL_FREE_NON_NULL(GuidStr);
682 }
683 }
684 }
685
686 SHELL_FREE_NON_NULL (TempStr);
687
688 Status = EfiAdptrInfoProtocol->GetInformation (
689 EfiAdptrInfoProtocol,
690 &InfoTypesBuffer[GuidIndex],
691 &InformationBlock,
692 &InformationBlockSize
693 );
694
695 if (EFI_ERROR (Status)) {
696 TempStr = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN(STR_GETINFO_FAILED), NULL);
697 RetVal = CatSPrint (RetVal, TempStr, Status);
698 } else {
699 if (CompareGuid (&InfoTypesBuffer[GuidIndex], &gEfiAdapterInfoMediaStateGuid)) {
700 TempStr = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN(STR_MEDIA_STATE), NULL);
701 RetVal = CatSPrint (
702 RetVal,
703 TempStr,
704 ((EFI_ADAPTER_INFO_MEDIA_STATE *)InformationBlock)->MediaState,
705 ((EFI_ADAPTER_INFO_MEDIA_STATE *)InformationBlock)->MediaState
706 );
707 } else if (CompareGuid (&InfoTypesBuffer[GuidIndex], &gEfiAdapterInfoNetworkBootGuid)) {
708 TempStr = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN(STR_NETWORK_BOOT_INFO), NULL);
709 RetVal = CatSPrint (
710 RetVal,
711 TempStr,
712 ((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->iScsiIpv4BootCapablity,
713 ((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->iScsiIpv6BootCapablity,
714 ((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->FCoeBootCapablity,
715 ((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->OffloadCapability,
716 ((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->iScsiMpioCapability,
717 ((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->iScsiIpv4Boot,
718 ((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->iScsiIpv6Boot,
719 ((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->FCoeBoot
720 );
721 } else if (CompareGuid (&InfoTypesBuffer[GuidIndex], &gEfiAdapterInfoSanMacAddressGuid) == TRUE) {
722 TempStr = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN(STR_SAN_MAC_ADDRESS_INFO), NULL);
723 RetVal = CatSPrint (
724 RetVal,
725 TempStr,
726 ((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[0],
727 ((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[1],
728 ((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[2],
729 ((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[3],
730 ((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[4],
731 ((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[5]
732 );
733 } else {
734 TempStr = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN(STR_UNKNOWN_INFO_TYPE), NULL);
735 RetVal = CatSPrint (RetVal, TempStr, &InfoTypesBuffer[GuidIndex]);
736 }
737 }
738 SHELL_FREE_NON_NULL (TempStr);
739 SHELL_FREE_NON_NULL (InformationBlock);
740 }
741 }
742
743 return RetVal;
744 }
745 //
746 // Put the information on the NT32 protocol GUIDs here so we are not dependant on the Nt32Pkg
747 //
748 #define LOCAL_EFI_WIN_NT_THUNK_PROTOCOL_GUID \
749 { \
750 0x58c518b1, 0x76f3, 0x11d4, { 0xbc, 0xea, 0x0, 0x80, 0xc7, 0x3c, 0x88, 0x81 } \
751 }
752
753 #define LOCAL_EFI_WIN_NT_BUS_DRIVER_IO_PROTOCOL_GUID \
754 { \
755 0x96eb4ad6, 0xa32a, 0x11d4, { 0xbc, 0xfd, 0x0, 0x80, 0xc7, 0x3c, 0x88, 0x81 } \
756 }
757
758 #define LOCAL_EFI_WIN_NT_SERIAL_PORT_GUID \
759 { \
760 0xc95a93d, 0xa006, 0x11d4, { 0xbc, 0xfa, 0x0, 0x80, 0xc7, 0x3c, 0x88, 0x81 } \
761 }
762 STATIC CONST EFI_GUID WinNtThunkProtocolGuid = LOCAL_EFI_WIN_NT_THUNK_PROTOCOL_GUID;
763 STATIC CONST EFI_GUID WinNtIoProtocolGuid = LOCAL_EFI_WIN_NT_BUS_DRIVER_IO_PROTOCOL_GUID;
764 STATIC CONST EFI_GUID WinNtSerialPortGuid = LOCAL_EFI_WIN_NT_SERIAL_PORT_GUID;
765
766 STATIC CONST GUID_INFO_BLOCK mGuidStringListNT[] = {
767 {STRING_TOKEN(STR_WINNT_THUNK), (EFI_GUID*)&WinNtThunkProtocolGuid, NULL},
768 {STRING_TOKEN(STR_WINNT_DRIVER_IO), (EFI_GUID*)&WinNtIoProtocolGuid, NULL},
769 {STRING_TOKEN(STR_WINNT_SERIAL_PORT), (EFI_GUID*)&WinNtSerialPortGuid, NULL},
770 {STRING_TOKEN(STR_UNKNOWN_DEVICE), NULL, NULL},
771 };
772
773 STATIC CONST GUID_INFO_BLOCK mGuidStringList[] = {
774 {STRING_TOKEN(STR_LOADED_IMAGE), &gEfiLoadedImageProtocolGuid, LoadedImageProtocolDumpInformation},
775 {STRING_TOKEN(STR_DEVICE_PATH), &gEfiDevicePathProtocolGuid, DevicePathProtocolDumpInformation},
776 {STRING_TOKEN(STR_IMAGE_PATH), &gEfiLoadedImageDevicePathProtocolGuid, DevicePathProtocolDumpInformation},
777 {STRING_TOKEN(STR_DEVICE_PATH_UTIL), &gEfiDevicePathUtilitiesProtocolGuid, NULL},
778 {STRING_TOKEN(STR_DEVICE_PATH_TXT), &gEfiDevicePathToTextProtocolGuid, NULL},
779 {STRING_TOKEN(STR_DEVICE_PATH_FTXT), &gEfiDevicePathFromTextProtocolGuid, NULL},
780 {STRING_TOKEN(STR_DEVICE_PATH_PC), &gEfiPcAnsiGuid, NULL},
781 {STRING_TOKEN(STR_DEVICE_PATH_VT100), &gEfiVT100Guid, NULL},
782 {STRING_TOKEN(STR_DEVICE_PATH_VT100P), &gEfiVT100PlusGuid, NULL},
783 {STRING_TOKEN(STR_DEVICE_PATH_VTUTF8), &gEfiVTUTF8Guid, NULL},
784 {STRING_TOKEN(STR_DRIVER_BINDING), &gEfiDriverBindingProtocolGuid, NULL},
785 {STRING_TOKEN(STR_PLATFORM_OVERRIDE), &gEfiPlatformDriverOverrideProtocolGuid, NULL},
786 {STRING_TOKEN(STR_BUS_OVERRIDE), &gEfiBusSpecificDriverOverrideProtocolGuid, NULL},
787 {STRING_TOKEN(STR_DRIVER_DIAG), &gEfiDriverDiagnosticsProtocolGuid, NULL},
788 {STRING_TOKEN(STR_DRIVER_DIAG2), &gEfiDriverDiagnostics2ProtocolGuid, NULL},
789 {STRING_TOKEN(STR_DRIVER_CN), &gEfiComponentNameProtocolGuid, NULL},
790 {STRING_TOKEN(STR_DRIVER_CN2), &gEfiComponentName2ProtocolGuid, NULL},
791 {STRING_TOKEN(STR_PLAT_DRV_CFG), &gEfiPlatformToDriverConfigurationProtocolGuid, NULL},
792 {STRING_TOKEN(STR_DRIVER_VERSION), &gEfiDriverSupportedEfiVersionProtocolGuid, DriverEfiVersionProtocolDumpInformation},
793 {STRING_TOKEN(STR_TXT_IN), &gEfiSimpleTextInProtocolGuid, NULL},
794 {STRING_TOKEN(STR_TXT_IN_EX), &gEfiSimpleTextInputExProtocolGuid, NULL},
795 {STRING_TOKEN(STR_TXT_OUT), &gEfiSimpleTextOutProtocolGuid, TxtOutProtocolDumpInformation},
796 {STRING_TOKEN(STR_SIM_POINTER), &gEfiSimplePointerProtocolGuid, NULL},
797 {STRING_TOKEN(STR_ABS_POINTER), &gEfiAbsolutePointerProtocolGuid, NULL},
798 {STRING_TOKEN(STR_SERIAL_IO), &gEfiSerialIoProtocolGuid, NULL},
799 {STRING_TOKEN(STR_GRAPHICS_OUTPUT), &gEfiGraphicsOutputProtocolGuid, GraphicsOutputProtocolDumpInformation},
800 {STRING_TOKEN(STR_EDID_DISCOVERED), &gEfiEdidDiscoveredProtocolGuid, NULL},
801 {STRING_TOKEN(STR_EDID_ACTIVE), &gEfiEdidActiveProtocolGuid, NULL},
802 {STRING_TOKEN(STR_EDID_OVERRIDE), &gEfiEdidOverrideProtocolGuid, NULL},
803 {STRING_TOKEN(STR_CON_IN), &gEfiConsoleInDeviceGuid, NULL},
804 {STRING_TOKEN(STR_CON_OUT), &gEfiConsoleOutDeviceGuid, NULL},
805 {STRING_TOKEN(STR_STD_ERR), &gEfiStandardErrorDeviceGuid, NULL},
806 {STRING_TOKEN(STR_LOAD_FILE), &gEfiLoadFileProtocolGuid, NULL},
807 {STRING_TOKEN(STR_LOAD_FILE2), &gEfiLoadFile2ProtocolGuid, NULL},
808 {STRING_TOKEN(STR_SIMPLE_FILE_SYS), &gEfiSimpleFileSystemProtocolGuid, NULL},
809 {STRING_TOKEN(STR_TAPE_IO), &gEfiTapeIoProtocolGuid, NULL},
810 {STRING_TOKEN(STR_DISK_IO), &gEfiDiskIoProtocolGuid, NULL},
811 {STRING_TOKEN(STR_BLK_IO), &gEfiBlockIoProtocolGuid, NULL},
812 {STRING_TOKEN(STR_UC), &gEfiUnicodeCollationProtocolGuid, NULL},
813 {STRING_TOKEN(STR_UC2), &gEfiUnicodeCollation2ProtocolGuid, NULL},
814 {STRING_TOKEN(STR_PCIRB_IO), &gEfiPciRootBridgeIoProtocolGuid, PciRootBridgeIoDumpInformation},
815 {STRING_TOKEN(STR_PCI_IO), &gEfiPciIoProtocolGuid, NULL},
816 {STRING_TOKEN(STR_SCSI_PT), &gEfiScsiPassThruProtocolGuid, NULL},
817 {STRING_TOKEN(STR_SCSI_IO), &gEfiScsiIoProtocolGuid, NULL},
818 {STRING_TOKEN(STR_SCSI_PT_EXT), &gEfiExtScsiPassThruProtocolGuid, NULL},
819 {STRING_TOKEN(STR_ISCSI), &gEfiIScsiInitiatorNameProtocolGuid, NULL},
820 {STRING_TOKEN(STR_USB_IO), &gEfiUsbIoProtocolGuid, NULL},
821 {STRING_TOKEN(STR_USB_HC), &gEfiUsbHcProtocolGuid, NULL},
822 {STRING_TOKEN(STR_USB_HC2), &gEfiUsb2HcProtocolGuid, NULL},
823 {STRING_TOKEN(STR_DEBUG_SUPPORT), &gEfiDebugSupportProtocolGuid, NULL},
824 {STRING_TOKEN(STR_DEBUG_PORT), &gEfiDebugPortProtocolGuid, NULL},
825 {STRING_TOKEN(STR_DECOMPRESS), &gEfiDecompressProtocolGuid, NULL},
826 {STRING_TOKEN(STR_ACPI_TABLE), &gEfiAcpiTableProtocolGuid, NULL},
827 {STRING_TOKEN(STR_EBC_INTERPRETER), &gEfiEbcProtocolGuid, NULL},
828 {STRING_TOKEN(STR_SNP), &gEfiSimpleNetworkProtocolGuid, NULL},
829 {STRING_TOKEN(STR_NII), &gEfiNetworkInterfaceIdentifierProtocolGuid, NULL},
830 {STRING_TOKEN(STR_NII_31), &gEfiNetworkInterfaceIdentifierProtocolGuid_31, NULL},
831 {STRING_TOKEN(STR_PXE_BC), &gEfiPxeBaseCodeProtocolGuid, NULL},
832 {STRING_TOKEN(STR_PXE_CB), &gEfiPxeBaseCodeCallbackProtocolGuid, NULL},
833 {STRING_TOKEN(STR_BIS), &gEfiBisProtocolGuid, NULL},
834 {STRING_TOKEN(STR_MNP_SB), &gEfiManagedNetworkServiceBindingProtocolGuid, NULL},
835 {STRING_TOKEN(STR_MNP), &gEfiManagedNetworkProtocolGuid, NULL},
836 {STRING_TOKEN(STR_ARP_SB), &gEfiArpServiceBindingProtocolGuid, NULL},
837 {STRING_TOKEN(STR_ARP), &gEfiArpProtocolGuid, NULL},
838 {STRING_TOKEN(STR_DHCPV4_SB), &gEfiDhcp4ServiceBindingProtocolGuid, NULL},
839 {STRING_TOKEN(STR_DHCPV4), &gEfiDhcp4ProtocolGuid, NULL},
840 {STRING_TOKEN(STR_TCPV4_SB), &gEfiTcp4ServiceBindingProtocolGuid, NULL},
841 {STRING_TOKEN(STR_TCPV4), &gEfiTcp4ProtocolGuid, NULL},
842 {STRING_TOKEN(STR_IPV4_SB), &gEfiIp4ServiceBindingProtocolGuid, NULL},
843 {STRING_TOKEN(STR_IPV4), &gEfiIp4ProtocolGuid, NULL},
844 {STRING_TOKEN(STR_IPV4_CFG), &gEfiIp4ConfigProtocolGuid, NULL},
845 {STRING_TOKEN(STR_UDPV4_SB), &gEfiUdp4ServiceBindingProtocolGuid, NULL},
846 {STRING_TOKEN(STR_UDPV4), &gEfiUdp4ProtocolGuid, NULL},
847 {STRING_TOKEN(STR_MTFTPV4_SB), &gEfiMtftp4ServiceBindingProtocolGuid, NULL},
848 {STRING_TOKEN(STR_MTFTPV4), &gEfiMtftp4ProtocolGuid, NULL},
849 {STRING_TOKEN(STR_AUTH_INFO), &gEfiAuthenticationInfoProtocolGuid, NULL},
850 {STRING_TOKEN(STR_HASH_SB), &gEfiHashServiceBindingProtocolGuid, NULL},
851 {STRING_TOKEN(STR_HASH), &gEfiHashProtocolGuid, NULL},
852 {STRING_TOKEN(STR_HII_FONT), &gEfiHiiFontProtocolGuid, NULL},
853 {STRING_TOKEN(STR_HII_STRING), &gEfiHiiStringProtocolGuid, NULL},
854 {STRING_TOKEN(STR_HII_IMAGE), &gEfiHiiImageProtocolGuid, NULL},
855 {STRING_TOKEN(STR_HII_DATABASE), &gEfiHiiDatabaseProtocolGuid, NULL},
856 {STRING_TOKEN(STR_HII_CONFIG_ROUT), &gEfiHiiConfigRoutingProtocolGuid, NULL},
857 {STRING_TOKEN(STR_HII_CONFIG_ACC), &gEfiHiiConfigAccessProtocolGuid, NULL},
858 {STRING_TOKEN(STR_HII_FORM_BROWSER2), &gEfiFormBrowser2ProtocolGuid, NULL},
859 {STRING_TOKEN(STR_DRIVER_FAM_OVERRIDE), &gEfiDriverFamilyOverrideProtocolGuid, NULL},
860 {STRING_TOKEN(STR_PCD), &gPcdProtocolGuid, NULL},
861 {STRING_TOKEN(STR_TCG), &gEfiTcgProtocolGuid, NULL},
862 {STRING_TOKEN(STR_HII_PACKAGE_LIST), &gEfiHiiPackageListProtocolGuid, NULL},
863
864 //
865 // the ones under this are deprecated by the current UEFI Spec, but may be found anyways...
866 //
867 {STRING_TOKEN(STR_SHELL_INTERFACE), &gEfiShellInterfaceGuid, NULL},
868 {STRING_TOKEN(STR_SHELL_ENV2), &gEfiShellEnvironment2Guid, NULL},
869 {STRING_TOKEN(STR_SHELL_ENV), &gEfiShellEnvironment2Guid, NULL},
870 {STRING_TOKEN(STR_DEVICE_IO), &gEfiDeviceIoProtocolGuid, NULL},
871 {STRING_TOKEN(STR_UGA_DRAW), &gEfiUgaDrawProtocolGuid, NULL},
872 {STRING_TOKEN(STR_UGA_IO), &gEfiUgaIoProtocolGuid, NULL},
873 {STRING_TOKEN(STR_ESP), &gEfiPartTypeSystemPartGuid, NULL},
874 {STRING_TOKEN(STR_GPT_NBR), &gEfiPartTypeLegacyMbrGuid, NULL},
875 {STRING_TOKEN(STR_DRIVER_CONFIG), &gEfiDriverConfigurationProtocolGuid, NULL},
876 {STRING_TOKEN(STR_DRIVER_CONFIG2), &gEfiDriverConfiguration2ProtocolGuid, NULL},
877 {STRING_TOKEN(STR_ISA_IO), &gEfiIsaIoProtocolGuid, NULL},
878 {STRING_TOKEN(STR_ISA_ACPI), &gEfiIsaAcpiProtocolGuid, NULL},
879
880 //
881 // the ones under this are GUID identified structs, not protocols
882 //
883 {STRING_TOKEN(STR_FILE_INFO), &gEfiFileInfoGuid, NULL},
884 {STRING_TOKEN(STR_FILE_SYS_INFO), &gEfiFileSystemInfoGuid, NULL},
885
886 //
887 // the ones under this are misc GUIDS.
888 //
889 {STRING_TOKEN(STR_EFI_GLOBAL_VARIABLE), &gEfiGlobalVariableGuid, NULL},
890
891 //
892 // UEFI 2.2
893 //
894 {STRING_TOKEN(STR_IP6_SB), &gEfiIp6ServiceBindingProtocolGuid, NULL},
895 {STRING_TOKEN(STR_IP6), &gEfiIp6ProtocolGuid, NULL},
896 {STRING_TOKEN(STR_IP6_CONFIG), &gEfiIp6ConfigProtocolGuid, NULL},
897 {STRING_TOKEN(STR_MTFTP6_SB), &gEfiMtftp6ServiceBindingProtocolGuid, NULL},
898 {STRING_TOKEN(STR_MTFTP6), &gEfiMtftp6ProtocolGuid, NULL},
899 {STRING_TOKEN(STR_DHCP6_SB), &gEfiDhcp6ServiceBindingProtocolGuid, NULL},
900 {STRING_TOKEN(STR_DHCP6), &gEfiDhcp6ProtocolGuid, NULL},
901 {STRING_TOKEN(STR_UDP6_SB), &gEfiUdp6ServiceBindingProtocolGuid, NULL},
902 {STRING_TOKEN(STR_UDP6), &gEfiUdp6ProtocolGuid, NULL},
903 {STRING_TOKEN(STR_TCP6_SB), &gEfiTcp6ServiceBindingProtocolGuid, NULL},
904 {STRING_TOKEN(STR_TCP6), &gEfiTcp6ProtocolGuid, NULL},
905 {STRING_TOKEN(STR_VLAN_CONFIG), &gEfiVlanConfigProtocolGuid, NULL},
906 {STRING_TOKEN(STR_EAP), &gEfiEapProtocolGuid, NULL},
907 {STRING_TOKEN(STR_EAP_MGMT), &gEfiEapManagementProtocolGuid, NULL},
908 {STRING_TOKEN(STR_FTP4_SB), &gEfiFtp4ServiceBindingProtocolGuid, NULL},
909 {STRING_TOKEN(STR_FTP4), &gEfiFtp4ProtocolGuid, NULL},
910 {STRING_TOKEN(STR_IP_SEC_CONFIG), &gEfiIpSecConfigProtocolGuid, NULL},
911 {STRING_TOKEN(STR_DH), &gEfiDriverHealthProtocolGuid, NULL},
912 {STRING_TOKEN(STR_DEF_IMG_LOAD), &gEfiDeferredImageLoadProtocolGuid, NULL},
913 {STRING_TOKEN(STR_USER_CRED), &gEfiUserCredentialProtocolGuid, NULL},
914 {STRING_TOKEN(STR_USER_MNGR), &gEfiUserManagerProtocolGuid, NULL},
915 {STRING_TOKEN(STR_ATA_PASS_THRU), &gEfiAtaPassThruProtocolGuid, NULL},
916
917 //
918 // UEFI 2.3
919 //
920 {STRING_TOKEN(STR_FW_MGMT), &gEfiFirmwareManagementProtocolGuid, NULL},
921 {STRING_TOKEN(STR_IP_SEC), &gEfiIpSecProtocolGuid, NULL},
922 {STRING_TOKEN(STR_IP_SEC2), &gEfiIpSec2ProtocolGuid, NULL},
923
924 //
925 // UEFI 2.3.1
926 //
927 {STRING_TOKEN(STR_KMS), &gEfiKmsProtocolGuid, NULL},
928 {STRING_TOKEN(STR_BLK_IO2), &gEfiBlockIo2ProtocolGuid, NULL},
929 {STRING_TOKEN(STR_SSC), &gEfiStorageSecurityCommandProtocolGuid, NULL},
930 {STRING_TOKEN(STR_UCRED2), &gEfiUserCredential2ProtocolGuid, NULL},
931
932 //
933 // UEFI 2.4
934 //
935 {STRING_TOKEN(STR_DISK_IO2), &gEfiDiskIo2ProtocolGuid, NULL},
936 {STRING_TOKEN(STR_ADAPTER_INFO), &gEfiAdapterInformationProtocolGuid, AdapterInformationDumpInformation},
937
938 //
939 // PI Spec ones
940 //
941 {STRING_TOKEN(STR_IDE_CONT_INIT), &gEfiIdeControllerInitProtocolGuid, NULL},
942
943 //
944 // UEFI Shell Spec 2.0
945 //
946 {STRING_TOKEN(STR_SHELL_PARAMETERS), &gEfiShellParametersProtocolGuid, NULL},
947 {STRING_TOKEN(STR_SHELL), &gEfiShellProtocolGuid, NULL},
948
949 //
950 // UEFI Shell Spec 2.1
951 //
952 {STRING_TOKEN(STR_SHELL_DYNAMIC), &gEfiShellDynamicCommandProtocolGuid, NULL},
953
954 //
955 // terminator
956 //
957 {STRING_TOKEN(STR_UNKNOWN_DEVICE), NULL, NULL},
958 };
959
960 /**
961 Function to get the node for a protocol or struct from it's GUID.
962
963 if Guid is NULL, then ASSERT.
964
965 @param[in] Guid The GUID to look for the name of.
966
967 @return The node.
968 **/
969 CONST GUID_INFO_BLOCK *
970 EFIAPI
971 InternalShellGetNodeFromGuid(
972 IN CONST EFI_GUID* Guid
973 )
974 {
975 CONST GUID_INFO_BLOCK *ListWalker;
976 UINTN LoopCount;
977
978 ASSERT(Guid != NULL);
979
980 for (LoopCount = 0, ListWalker = GuidList; GuidList != NULL && LoopCount < GuidListCount; LoopCount++, ListWalker++) {
981 if (CompareGuid(ListWalker->GuidId, Guid)) {
982 return (ListWalker);
983 }
984 }
985
986 if (PcdGetBool(PcdShellIncludeNtGuids)) {
987 for (ListWalker = mGuidStringListNT ; ListWalker != NULL && ListWalker->GuidId != NULL ; ListWalker++) {
988 if (CompareGuid(ListWalker->GuidId, Guid)) {
989 return (ListWalker);
990 }
991 }
992 }
993 for (ListWalker = mGuidStringList ; ListWalker != NULL && ListWalker->GuidId != NULL ; ListWalker++) {
994 if (CompareGuid(ListWalker->GuidId, Guid)) {
995 return (ListWalker);
996 }
997 }
998 return (NULL);
999 }
1000
1001 /**
1002 Function to add a new GUID/Name mapping.
1003
1004 @param[in] Guid The Guid
1005 @param[in] NameID The STRING id of the HII string to use
1006 @param[in] DumpFunc The pointer to the dump function
1007
1008
1009 @retval EFI_SUCCESS The operation was sucessful
1010 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1011 @retval EFI_INVALID_PARAMETER Guid NameId was invalid
1012 **/
1013 EFI_STATUS
1014 EFIAPI
1015 InsertNewGuidNameMapping(
1016 IN CONST EFI_GUID *Guid,
1017 IN CONST EFI_STRING_ID NameID,
1018 IN CONST DUMP_PROTOCOL_INFO DumpFunc OPTIONAL
1019 )
1020 {
1021 ASSERT(Guid != NULL);
1022 ASSERT(NameID != 0);
1023
1024 GuidList = ReallocatePool(GuidListCount * sizeof(GUID_INFO_BLOCK), GuidListCount+1 * sizeof(GUID_INFO_BLOCK), GuidList);
1025 if (GuidList == NULL) {
1026 GuidListCount = 0;
1027 return (EFI_OUT_OF_RESOURCES);
1028 }
1029 GuidListCount++;
1030
1031 GuidList[GuidListCount - 1].GuidId = AllocateCopyPool(sizeof(EFI_GUID), Guid);
1032 GuidList[GuidListCount - 1].StringId = NameID;
1033 GuidList[GuidListCount - 1].DumpInfo = DumpFunc;
1034
1035 if (GuidList[GuidListCount - 1].GuidId == NULL) {
1036 return (EFI_OUT_OF_RESOURCES);
1037 }
1038
1039 return (EFI_SUCCESS);
1040 }
1041
1042 /**
1043 Function to add a new GUID/Name mapping.
1044
1045 This cannot overwrite an existing mapping.
1046
1047 @param[in] Guid The Guid
1048 @param[in] TheName The Guid's name
1049 @param[in] Lang RFC4646 language code list or NULL
1050
1051 @retval EFI_SUCCESS The operation was sucessful
1052 @retval EFI_ACCESS_DENIED There was a duplicate
1053 @retval EFI_OUT_OF_RESOURCES A memory allocation failed
1054 @retval EFI_INVALID_PARAMETER Guid or TheName was NULL
1055 **/
1056 EFI_STATUS
1057 EFIAPI
1058 AddNewGuidNameMapping(
1059 IN CONST EFI_GUID *Guid,
1060 IN CONST CHAR16 *TheName,
1061 IN CONST CHAR8 *Lang OPTIONAL
1062 )
1063 {
1064 EFI_STRING_ID NameID;
1065
1066 HandleParsingHiiInit();
1067
1068 if (Guid == NULL || TheName == NULL){
1069 return (EFI_INVALID_PARAMETER);
1070 }
1071
1072 if ((InternalShellGetNodeFromGuid(Guid)) != NULL) {
1073 return (EFI_ACCESS_DENIED);
1074 }
1075
1076 NameID = HiiSetString(mHandleParsingHiiHandle, 0, (CHAR16*)TheName, Lang);
1077 if (NameID == 0) {
1078 return (EFI_OUT_OF_RESOURCES);
1079 }
1080
1081 return (InsertNewGuidNameMapping(Guid, NameID, NULL));
1082 }
1083
1084 /**
1085 Function to get the name of a protocol or struct from it's GUID.
1086
1087 if Guid is NULL, then ASSERT.
1088
1089 @param[in] Guid The GUID to look for the name of.
1090 @param[in] Lang The language to use.
1091
1092 @return pointer to string of the name. The caller
1093 is responsible to free this memory.
1094 **/
1095 CHAR16*
1096 EFIAPI
1097 GetStringNameFromGuid(
1098 IN CONST EFI_GUID *Guid,
1099 IN CONST CHAR8 *Lang OPTIONAL
1100 )
1101 {
1102 CONST GUID_INFO_BLOCK *Id;
1103
1104 HandleParsingHiiInit();
1105
1106 Id = InternalShellGetNodeFromGuid(Guid);
1107 return (HiiGetString(mHandleParsingHiiHandle, Id==NULL?STRING_TOKEN(STR_UNKNOWN_DEVICE):Id->StringId, Lang));
1108 }
1109
1110 /**
1111 Function to dump protocol information from a handle.
1112
1113 This function will return a allocated string buffer containing the
1114 information. The caller is responsible for freeing the memory.
1115
1116 If Guid is NULL, ASSERT().
1117 If TheHandle is NULL, ASSERT().
1118
1119 @param[in] TheHandle The handle to dump information from.
1120 @param[in] Guid The GUID of the protocol to dump.
1121 @param[in] Verbose TRUE for extra info. FALSE otherwise.
1122
1123 @return The pointer to string.
1124 @retval NULL An error was encountered.
1125 **/
1126 CHAR16*
1127 EFIAPI
1128 GetProtocolInformationDump(
1129 IN CONST EFI_HANDLE TheHandle,
1130 IN CONST EFI_GUID *Guid,
1131 IN CONST BOOLEAN Verbose
1132 )
1133 {
1134 CONST GUID_INFO_BLOCK *Id;
1135
1136 ASSERT(TheHandle != NULL);
1137 ASSERT(Guid != NULL);
1138
1139 if (TheHandle == NULL || Guid == NULL) {
1140 return (NULL);
1141 }
1142
1143 Id = InternalShellGetNodeFromGuid(Guid);
1144 if (Id != NULL && Id->DumpInfo != NULL) {
1145 return (Id->DumpInfo(TheHandle, Verbose));
1146 }
1147 return (NULL);
1148 }
1149
1150 /**
1151 Function to get the Guid for a protocol or struct based on it's string name.
1152
1153 do not modify the returned Guid.
1154
1155 @param[in] Name The pointer to the string name.
1156 @param[in] Lang The pointer to the language code.
1157 @param[out] Guid The pointer to the Guid.
1158
1159 @retval EFI_SUCCESS The operation was sucessful.
1160 **/
1161 EFI_STATUS
1162 EFIAPI
1163 GetGuidFromStringName(
1164 IN CONST CHAR16 *Name,
1165 IN CONST CHAR8 *Lang OPTIONAL,
1166 OUT EFI_GUID **Guid
1167 )
1168 {
1169 CONST GUID_INFO_BLOCK *ListWalker;
1170 CHAR16 *String;
1171 UINTN LoopCount;
1172
1173 HandleParsingHiiInit();
1174
1175 ASSERT(Guid != NULL);
1176 if (Guid == NULL) {
1177 return (EFI_INVALID_PARAMETER);
1178 }
1179 *Guid = NULL;
1180
1181 if (PcdGetBool(PcdShellIncludeNtGuids)) {
1182 for (ListWalker = mGuidStringListNT ; ListWalker != NULL && ListWalker->GuidId != NULL ; ListWalker++) {
1183 String = HiiGetString(mHandleParsingHiiHandle, ListWalker->StringId, Lang);
1184 if (Name != NULL && String != NULL && StringNoCaseCompare (&Name, &String) == 0) {
1185 *Guid = ListWalker->GuidId;
1186 }
1187 SHELL_FREE_NON_NULL(String);
1188 if (*Guid != NULL) {
1189 return (EFI_SUCCESS);
1190 }
1191 }
1192 }
1193 for (ListWalker = mGuidStringList ; ListWalker != NULL && ListWalker->GuidId != NULL ; ListWalker++) {
1194 String = HiiGetString(mHandleParsingHiiHandle, ListWalker->StringId, Lang);
1195 if (Name != NULL && String != NULL && StringNoCaseCompare (&Name, &String) == 0) {
1196 *Guid = ListWalker->GuidId;
1197 }
1198 SHELL_FREE_NON_NULL(String);
1199 if (*Guid != NULL) {
1200 return (EFI_SUCCESS);
1201 }
1202 }
1203
1204 for (LoopCount = 0, ListWalker = GuidList; GuidList != NULL && LoopCount < GuidListCount; LoopCount++, ListWalker++) {
1205 String = HiiGetString(mHandleParsingHiiHandle, ListWalker->StringId, Lang);
1206 if (Name != NULL && String != NULL && StringNoCaseCompare (&Name, &String) == 0) {
1207 *Guid = ListWalker->GuidId;
1208 }
1209 SHELL_FREE_NON_NULL(String);
1210 if (*Guid != NULL) {
1211 return (EFI_SUCCESS);
1212 }
1213 }
1214
1215 return (EFI_NOT_FOUND);
1216 }
1217
1218 /**
1219 Get best support language for this driver.
1220
1221 First base on the user input language to search, second base on the current
1222 platform used language to search, third get the first language from the
1223 support language list. The caller need to free the buffer of the best language.
1224
1225 @param[in] SupportedLanguages The support languages for this driver.
1226 @param[in] InputLanguage The user input language.
1227 @param[in] Iso639Language Whether get language for ISO639.
1228
1229 @return The best support language for this driver.
1230 **/
1231 CHAR8 *
1232 EFIAPI
1233 GetBestLanguageForDriver (
1234 IN CONST CHAR8 *SupportedLanguages,
1235 IN CONST CHAR8 *InputLanguage,
1236 IN BOOLEAN Iso639Language
1237 )
1238 {
1239 CHAR8 *LanguageVariable;
1240 CHAR8 *BestLanguage;
1241
1242 LanguageVariable = GetVariable (Iso639Language ? L"Lang" : L"PlatformLang", &gEfiGlobalVariableGuid);
1243
1244 BestLanguage = GetBestLanguage(
1245 SupportedLanguages,
1246 Iso639Language,
1247 (InputLanguage != NULL) ? InputLanguage : "",
1248 (LanguageVariable != NULL) ? LanguageVariable : "",
1249 SupportedLanguages,
1250 NULL
1251 );
1252
1253 if (LanguageVariable != NULL) {
1254 FreePool (LanguageVariable);
1255 }
1256
1257 return BestLanguage;
1258 }
1259
1260 /**
1261 Function to retrieve the driver name (if possible) from the ComponentName or
1262 ComponentName2 protocol
1263
1264 @param[in] TheHandle The driver handle to get the name of.
1265 @param[in] Language The language to use.
1266
1267 @retval NULL The name could not be found.
1268 @return A pointer to the string name. Do not de-allocate the memory.
1269 **/
1270 CONST CHAR16*
1271 EFIAPI
1272 GetStringNameFromHandle(
1273 IN CONST EFI_HANDLE TheHandle,
1274 IN CONST CHAR8 *Language
1275 )
1276 {
1277 EFI_COMPONENT_NAME2_PROTOCOL *CompNameStruct;
1278 EFI_STATUS Status;
1279 CHAR16 *RetVal;
1280 CHAR8 *BestLang;
1281
1282 BestLang = NULL;
1283
1284 Status = gBS->OpenProtocol(
1285 TheHandle,
1286 &gEfiComponentName2ProtocolGuid,
1287 (VOID**)&CompNameStruct,
1288 gImageHandle,
1289 NULL,
1290 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1291 if (!EFI_ERROR(Status)) {
1292 BestLang = GetBestLanguageForDriver (CompNameStruct->SupportedLanguages, Language, FALSE);
1293 Status = CompNameStruct->GetDriverName(CompNameStruct, BestLang, &RetVal);
1294 if (BestLang != NULL) {
1295 FreePool (BestLang);
1296 BestLang = NULL;
1297 }
1298 if (!EFI_ERROR(Status)) {
1299 return (RetVal);
1300 }
1301 }
1302 Status = gBS->OpenProtocol(
1303 TheHandle,
1304 &gEfiComponentNameProtocolGuid,
1305 (VOID**)&CompNameStruct,
1306 gImageHandle,
1307 NULL,
1308 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1309 if (!EFI_ERROR(Status)) {
1310 BestLang = GetBestLanguageForDriver (CompNameStruct->SupportedLanguages, Language, FALSE);
1311 Status = CompNameStruct->GetDriverName(CompNameStruct, BestLang, &RetVal);
1312 if (BestLang != NULL) {
1313 FreePool (BestLang);
1314 }
1315 if (!EFI_ERROR(Status)) {
1316 return (RetVal);
1317 }
1318 }
1319 return (NULL);
1320 }
1321
1322 /**
1323 Function to initialize the file global mHandleList object for use in
1324 vonverting handles to index and index to handle.
1325
1326 @retval EFI_SUCCESS The operation was successful.
1327 **/
1328 EFI_STATUS
1329 EFIAPI
1330 InternalShellInitHandleList(
1331 VOID
1332 )
1333 {
1334 EFI_STATUS Status;
1335 EFI_HANDLE *HandleBuffer;
1336 UINTN HandleCount;
1337 HANDLE_LIST *ListWalker;
1338
1339 if (mHandleList.NextIndex != 0) {
1340 return EFI_SUCCESS;
1341 }
1342 InitializeListHead(&mHandleList.List.Link);
1343 mHandleList.NextIndex = 1;
1344 Status = gBS->LocateHandleBuffer (
1345 AllHandles,
1346 NULL,
1347 NULL,
1348 &HandleCount,
1349 &HandleBuffer
1350 );
1351 ASSERT_EFI_ERROR(Status);
1352 if (EFI_ERROR(Status)) {
1353 return (Status);
1354 }
1355 for (mHandleList.NextIndex = 1 ; mHandleList.NextIndex <= HandleCount ; mHandleList.NextIndex++){
1356 ListWalker = AllocateZeroPool(sizeof(HANDLE_LIST));
1357 ASSERT(ListWalker != NULL);
1358 ListWalker->TheHandle = HandleBuffer[mHandleList.NextIndex-1];
1359 ListWalker->TheIndex = mHandleList.NextIndex;
1360 InsertTailList(&mHandleList.List.Link,&ListWalker->Link);
1361 }
1362 FreePool(HandleBuffer);
1363 return (EFI_SUCCESS);
1364 }
1365
1366 /**
1367 Function to retrieve the human-friendly index of a given handle. If the handle
1368 does not have a index one will be automatically assigned. The index value is valid
1369 until the termination of the shell application.
1370
1371 @param[in] TheHandle The handle to retrieve an index for.
1372
1373 @retval 0 A memory allocation failed.
1374 @return The index of the handle.
1375
1376 **/
1377 UINTN
1378 EFIAPI
1379 ConvertHandleToHandleIndex(
1380 IN CONST EFI_HANDLE TheHandle
1381 )
1382 {
1383 EFI_STATUS Status;
1384 EFI_GUID **ProtocolBuffer;
1385 UINTN ProtocolCount;
1386 HANDLE_LIST *ListWalker;
1387
1388 if (TheHandle == NULL) {
1389 return 0;
1390 }
1391
1392 InternalShellInitHandleList();
1393
1394 for (ListWalker = (HANDLE_LIST*)GetFirstNode(&mHandleList.List.Link)
1395 ; !IsNull(&mHandleList.List.Link,&ListWalker->Link)
1396 ; ListWalker = (HANDLE_LIST*)GetNextNode(&mHandleList.List.Link,&ListWalker->Link)
1397 ){
1398 if (ListWalker->TheHandle == TheHandle) {
1399 //
1400 // Verify that TheHandle is still present in the Handle Database
1401 //
1402 Status = gBS->ProtocolsPerHandle(TheHandle, &ProtocolBuffer, &ProtocolCount);
1403 if (EFI_ERROR (Status)) {
1404 //
1405 // TheHandle is not present in the Handle Database, so delete from the handle list
1406 //
1407 RemoveEntryList (&ListWalker->Link);
1408 return 0;
1409 }
1410 FreePool (ProtocolBuffer);
1411 return (ListWalker->TheIndex);
1412 }
1413 }
1414
1415 //
1416 // Verify that TheHandle is valid handle
1417 //
1418 Status = gBS->ProtocolsPerHandle(TheHandle, &ProtocolBuffer, &ProtocolCount);
1419 if (EFI_ERROR (Status)) {
1420 //
1421 // TheHandle is not valid, so do not add to handle list
1422 //
1423 return 0;
1424 }
1425 FreePool (ProtocolBuffer);
1426
1427 ListWalker = AllocateZeroPool(sizeof(HANDLE_LIST));
1428 ASSERT(ListWalker != NULL);
1429 ListWalker->TheHandle = TheHandle;
1430 ListWalker->TheIndex = mHandleList.NextIndex++;
1431 InsertTailList(&mHandleList.List.Link,&ListWalker->Link);
1432 return (ListWalker->TheIndex);
1433 }
1434
1435
1436
1437 /**
1438 Function to retrieve the EFI_HANDLE from the human-friendly index.
1439
1440 @param[in] TheIndex The index to retrieve the EFI_HANDLE for.
1441
1442 @retval NULL The index was invalid.
1443 @return The EFI_HANDLE that index represents.
1444
1445 **/
1446 EFI_HANDLE
1447 EFIAPI
1448 ConvertHandleIndexToHandle(
1449 IN CONST UINTN TheIndex
1450 )
1451 {
1452 EFI_STATUS Status;
1453 EFI_GUID **ProtocolBuffer;
1454 UINTN ProtocolCount;
1455 HANDLE_LIST *ListWalker;
1456
1457 InternalShellInitHandleList();
1458
1459 if (TheIndex >= mHandleList.NextIndex) {
1460 return NULL;
1461 }
1462
1463 for (ListWalker = (HANDLE_LIST*)GetFirstNode(&mHandleList.List.Link)
1464 ; !IsNull(&mHandleList.List.Link,&ListWalker->Link)
1465 ; ListWalker = (HANDLE_LIST*)GetNextNode(&mHandleList.List.Link,&ListWalker->Link)
1466 ){
1467 if (ListWalker->TheIndex == TheIndex && ListWalker->TheHandle != NULL) {
1468 //
1469 // Verify that LinkWalker->TheHandle is valid handle
1470 //
1471 Status = gBS->ProtocolsPerHandle(ListWalker->TheHandle, &ProtocolBuffer, &ProtocolCount);
1472 if (EFI_ERROR (Status)) {
1473 //
1474 // TheHandle is not valid, so do not add to handle list
1475 //
1476 ListWalker->TheHandle = NULL;
1477 }
1478 return (ListWalker->TheHandle);
1479 }
1480 }
1481 return NULL;
1482 }
1483
1484 /**
1485 Gets all the related EFI_HANDLEs based on the mask supplied.
1486
1487 This function scans all EFI_HANDLES in the UEFI environment's handle database
1488 and returns the ones with the specified relationship (Mask) to the specified
1489 controller handle.
1490
1491 If both DriverBindingHandle and ControllerHandle are NULL, then ASSERT.
1492 If MatchingHandleCount is NULL, then ASSERT.
1493
1494 If MatchingHandleBuffer is not NULL upon a successful return the memory must be
1495 caller freed.
1496
1497 @param[in] DriverBindingHandle The handle with Driver Binding protocol on it.
1498 @param[in] ControllerHandle The handle with Device Path protocol on it.
1499 @param[in] MatchingHandleCount The pointer to UINTN that specifies the number of HANDLES in
1500 MatchingHandleBuffer.
1501 @param[out] MatchingHandleBuffer On a successful return, a buffer of MatchingHandleCount
1502 EFI_HANDLEs with a terminating NULL EFI_HANDLE.
1503 @param[out] HandleType An array of type information.
1504
1505 @retval EFI_SUCCESS The operation was successful, and any related handles
1506 are in MatchingHandleBuffer.
1507 @retval EFI_NOT_FOUND No matching handles were found.
1508 @retval EFI_INVALID_PARAMETER A parameter was invalid or out of range.
1509 **/
1510 EFI_STATUS
1511 EFIAPI
1512 ParseHandleDatabaseByRelationshipWithType (
1513 IN CONST EFI_HANDLE DriverBindingHandle OPTIONAL,
1514 IN CONST EFI_HANDLE ControllerHandle OPTIONAL,
1515 IN UINTN *HandleCount,
1516 OUT EFI_HANDLE **HandleBuffer,
1517 OUT UINTN **HandleType
1518 )
1519 {
1520 EFI_STATUS Status;
1521 UINTN HandleIndex;
1522 EFI_GUID **ProtocolGuidArray;
1523 UINTN ArrayCount;
1524 UINTN ProtocolIndex;
1525 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;
1526 UINTN OpenInfoCount;
1527 UINTN OpenInfoIndex;
1528 UINTN ChildIndex;
1529 INTN DriverBindingHandleIndex;
1530
1531 ASSERT(HandleCount != NULL);
1532 ASSERT(HandleBuffer != NULL);
1533 ASSERT(HandleType != NULL);
1534 ASSERT(DriverBindingHandle != NULL || ControllerHandle != NULL);
1535
1536 *HandleCount = 0;
1537 *HandleBuffer = NULL;
1538 *HandleType = NULL;
1539
1540 //
1541 // Retrieve the list of all handles from the handle database
1542 //
1543 Status = gBS->LocateHandleBuffer (
1544 AllHandles,
1545 NULL,
1546 NULL,
1547 HandleCount,
1548 HandleBuffer
1549 );
1550 if (EFI_ERROR (Status)) {
1551 return (Status);
1552 }
1553
1554 *HandleType = AllocateZeroPool (*HandleCount * sizeof (UINTN));
1555 ASSERT(*HandleType != NULL);
1556
1557 DriverBindingHandleIndex = -1;
1558 for (HandleIndex = 0; HandleIndex < *HandleCount; HandleIndex++) {
1559 if (DriverBindingHandle != NULL && (*HandleBuffer)[HandleIndex] == DriverBindingHandle) {
1560 DriverBindingHandleIndex = (INTN)HandleIndex;
1561 }
1562 }
1563
1564 for (HandleIndex = 0; HandleIndex < *HandleCount; HandleIndex++) {
1565 //
1566 // Retrieve the list of all the protocols on each handle
1567 //
1568 Status = gBS->ProtocolsPerHandle (
1569 (*HandleBuffer)[HandleIndex],
1570 &ProtocolGuidArray,
1571 &ArrayCount
1572 );
1573 if (EFI_ERROR (Status)) {
1574 continue;
1575 }
1576
1577 for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {
1578
1579 //
1580 // Set the bit describing what this handle has
1581 //
1582 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiLoadedImageProtocolGuid) ) {
1583 (*HandleType)[HandleIndex] |= (UINTN)HR_IMAGE_HANDLE;
1584 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverBindingProtocolGuid) ) {
1585 (*HandleType)[HandleIndex] |= (UINTN)HR_DRIVER_BINDING_HANDLE;
1586 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverConfiguration2ProtocolGuid)) {
1587 (*HandleType)[HandleIndex] |= (UINTN)HR_DRIVER_CONFIGURATION_HANDLE;
1588 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverConfigurationProtocolGuid) ) {
1589 (*HandleType)[HandleIndex] |= (UINTN)HR_DRIVER_CONFIGURATION_HANDLE;
1590 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverDiagnostics2ProtocolGuid) ) {
1591 (*HandleType)[HandleIndex] |= (UINTN)HR_DRIVER_DIAGNOSTICS_HANDLE;
1592 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverDiagnosticsProtocolGuid) ) {
1593 (*HandleType)[HandleIndex] |= (UINTN)HR_DRIVER_DIAGNOSTICS_HANDLE;
1594 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiComponentName2ProtocolGuid) ) {
1595 (*HandleType)[HandleIndex] |= (UINTN)HR_COMPONENT_NAME_HANDLE;
1596 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiComponentNameProtocolGuid) ) {
1597 (*HandleType)[HandleIndex] |= (UINTN)HR_COMPONENT_NAME_HANDLE;
1598 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDevicePathProtocolGuid) ) {
1599 (*HandleType)[HandleIndex] |= (UINTN)HR_DEVICE_HANDLE;
1600 } else {
1601 DEBUG_CODE_BEGIN();
1602 ASSERT((*HandleType)[HandleIndex] == (*HandleType)[HandleIndex]);
1603 DEBUG_CODE_END();
1604 }
1605 //
1606 // Retrieve the list of agents that have opened each protocol
1607 //
1608 Status = gBS->OpenProtocolInformation (
1609 (*HandleBuffer)[HandleIndex],
1610 ProtocolGuidArray[ProtocolIndex],
1611 &OpenInfo,
1612 &OpenInfoCount
1613 );
1614 if (EFI_ERROR (Status)) {
1615 continue;
1616 }
1617
1618 if (ControllerHandle == NULL) {
1619 //
1620 // ControllerHandle == NULL and DriverBindingHandle != NULL.
1621 // Return information on all the controller handles that the driver specified by DriverBindingHandle is managing
1622 //
1623 for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
1624 if (OpenInfo[OpenInfoIndex].AgentHandle == DriverBindingHandle && (OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) {
1625 (*HandleType)[HandleIndex] |= (UINTN)(HR_DEVICE_HANDLE | HR_CONTROLLER_HANDLE);
1626 if (DriverBindingHandleIndex != -1) {
1627 (*HandleType)[DriverBindingHandleIndex] |= (UINTN)HR_DEVICE_DRIVER;
1628 }
1629 }
1630 if (OpenInfo[OpenInfoIndex].AgentHandle == DriverBindingHandle && (OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {
1631 (*HandleType)[HandleIndex] |= (UINTN)(HR_DEVICE_HANDLE | HR_CONTROLLER_HANDLE);
1632 if (DriverBindingHandleIndex != -1) {
1633 (*HandleType)[DriverBindingHandleIndex] |= (UINTN)(HR_BUS_DRIVER | HR_DEVICE_DRIVER);
1634 }
1635 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
1636 if (OpenInfo[OpenInfoIndex].ControllerHandle == (*HandleBuffer)[ChildIndex]) {
1637 (*HandleType)[ChildIndex] |= (UINTN)(HR_DEVICE_HANDLE | HR_CHILD_HANDLE);
1638 }
1639 }
1640 }
1641 }
1642 }
1643 if (DriverBindingHandle == NULL && ControllerHandle != NULL) {
1644 if (ControllerHandle == (*HandleBuffer)[HandleIndex]) {
1645 (*HandleType)[HandleIndex] |= (UINTN)(HR_DEVICE_HANDLE | HR_CONTROLLER_HANDLE);
1646 for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
1647 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) {
1648 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
1649 if (OpenInfo[OpenInfoIndex].AgentHandle == (*HandleBuffer)[ChildIndex]) {
1650 (*HandleType)[ChildIndex] |= (UINTN)HR_DEVICE_DRIVER;
1651 }
1652 }
1653 }
1654 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {
1655 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
1656 if (OpenInfo[OpenInfoIndex].AgentHandle == (*HandleBuffer)[ChildIndex]) {
1657 (*HandleType)[ChildIndex] |= (UINTN)(HR_BUS_DRIVER | HR_DEVICE_DRIVER);
1658 }
1659 if (OpenInfo[OpenInfoIndex].ControllerHandle == (*HandleBuffer)[ChildIndex]) {
1660 (*HandleType)[ChildIndex] |= (UINTN)(HR_DEVICE_HANDLE | HR_CHILD_HANDLE);
1661 }
1662 }
1663 }
1664 }
1665 } else {
1666 for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
1667 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {
1668 if (OpenInfo[OpenInfoIndex].ControllerHandle == ControllerHandle) {
1669 (*HandleType)[HandleIndex] |= (UINTN)(HR_DEVICE_HANDLE | HR_PARENT_HANDLE);
1670 }
1671 }
1672 }
1673 }
1674 }
1675 if (DriverBindingHandle != NULL && ControllerHandle != NULL) {
1676 if (ControllerHandle == (*HandleBuffer)[HandleIndex]) {
1677 (*HandleType)[HandleIndex] |= (UINTN)(HR_DEVICE_HANDLE | HR_CONTROLLER_HANDLE);
1678 for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
1679 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) {
1680 if (OpenInfo[OpenInfoIndex].AgentHandle == DriverBindingHandle) {
1681 if (DriverBindingHandleIndex != -1) {
1682 (*HandleType)[DriverBindingHandleIndex] |= (UINTN)HR_DEVICE_DRIVER;
1683 }
1684 }
1685 }
1686 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {
1687 if (OpenInfo[OpenInfoIndex].AgentHandle == DriverBindingHandle) {
1688 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
1689 if (OpenInfo[OpenInfoIndex].ControllerHandle == (*HandleBuffer)[ChildIndex]) {
1690 (*HandleType)[ChildIndex] |= (UINTN)(HR_DEVICE_HANDLE | HR_CHILD_HANDLE);
1691 }
1692 }
1693 }
1694
1695 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
1696 if (OpenInfo[OpenInfoIndex].AgentHandle == (*HandleBuffer)[ChildIndex]) {
1697 (*HandleType)[ChildIndex] |= (UINTN)(HR_BUS_DRIVER | HR_DEVICE_DRIVER);
1698 }
1699 }
1700 }
1701 }
1702 } else {
1703 for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
1704 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {
1705 if (OpenInfo[OpenInfoIndex].ControllerHandle == ControllerHandle) {
1706 (*HandleType)[HandleIndex] |= (UINTN)(HR_DEVICE_HANDLE | HR_PARENT_HANDLE);
1707 }
1708 }
1709 }
1710 }
1711 }
1712 FreePool (OpenInfo);
1713 }
1714 FreePool (ProtocolGuidArray);
1715 }
1716 return EFI_SUCCESS;
1717 }
1718
1719 /**
1720 Gets all the related EFI_HANDLEs based on the single EFI_HANDLE and the mask
1721 supplied.
1722
1723 This function will scan all EFI_HANDLES in the UEFI environment's handle database
1724 and return all the ones with the specified relationship (Mask) to the specified
1725 controller handle.
1726
1727 If both DriverBindingHandle and ControllerHandle are NULL, then ASSERT.
1728 If MatchingHandleCount is NULL, then ASSERT.
1729
1730 If MatchingHandleBuffer is not NULL upon a sucessful return the memory must be
1731 caller freed.
1732
1733 @param[in] DriverBindingHandle Handle to a object with Driver Binding protocol
1734 on it.
1735 @param[in] ControllerHandle Handle to a device with Device Path protocol on it.
1736 @param[in] Mask Mask of what relationship(s) is desired.
1737 @param[in] MatchingHandleCount Poitner to UINTN specifying number of HANDLES in
1738 MatchingHandleBuffer.
1739 @param[out] MatchingHandleBuffer On a sucessful return a buffer of MatchingHandleCount
1740 EFI_HANDLEs and a terminating NULL EFI_HANDLE.
1741
1742 @retval EFI_SUCCESS The operation was sucessful and any related handles
1743 are in MatchingHandleBuffer;
1744 @retval EFI_NOT_FOUND No matching handles were found.
1745 @retval EFI_INVALID_PARAMETER A parameter was invalid or out of range.
1746 **/
1747 EFI_STATUS
1748 EFIAPI
1749 ParseHandleDatabaseByRelationship (
1750 IN CONST EFI_HANDLE DriverBindingHandle OPTIONAL,
1751 IN CONST EFI_HANDLE ControllerHandle OPTIONAL,
1752 IN CONST UINTN Mask,
1753 IN UINTN *MatchingHandleCount,
1754 OUT EFI_HANDLE **MatchingHandleBuffer OPTIONAL
1755 )
1756 {
1757 EFI_STATUS Status;
1758 UINTN HandleCount;
1759 EFI_HANDLE *HandleBuffer;
1760 UINTN *HandleType;
1761 UINTN HandleIndex;
1762
1763 ASSERT(MatchingHandleCount != NULL);
1764 ASSERT(DriverBindingHandle != NULL || ControllerHandle != NULL);
1765
1766 if ((Mask & HR_VALID_MASK) != Mask) {
1767 return (EFI_INVALID_PARAMETER);
1768 }
1769
1770 if ((Mask & HR_CHILD_HANDLE) != 0 && DriverBindingHandle == NULL) {
1771 return (EFI_INVALID_PARAMETER);
1772 }
1773
1774 *MatchingHandleCount = 0;
1775 if (MatchingHandleBuffer != NULL) {
1776 *MatchingHandleBuffer = NULL;
1777 }
1778
1779 HandleBuffer = NULL;
1780 HandleType = NULL;
1781
1782 Status = ParseHandleDatabaseByRelationshipWithType (
1783 DriverBindingHandle,
1784 ControllerHandle,
1785 &HandleCount,
1786 &HandleBuffer,
1787 &HandleType
1788 );
1789 if (!EFI_ERROR (Status)) {
1790 //
1791 // Count the number of handles that match the attributes in Mask
1792 //
1793 for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
1794 if ((HandleType[HandleIndex] & Mask) == Mask) {
1795 (*MatchingHandleCount)++;
1796 }
1797 }
1798 //
1799 // If no handles match the attributes in Mask then return EFI_NOT_FOUND
1800 //
1801 if (*MatchingHandleCount == 0) {
1802 Status = EFI_NOT_FOUND;
1803 } else {
1804
1805 if (MatchingHandleBuffer == NULL) {
1806 //
1807 // Someone just wanted the count...
1808 //
1809 Status = EFI_SUCCESS;
1810 } else {
1811 //
1812 // Allocate a handle buffer for the number of handles that matched the attributes in Mask
1813 //
1814 *MatchingHandleBuffer = AllocateZeroPool ((*MatchingHandleCount +1)* sizeof (EFI_HANDLE));
1815 ASSERT(*MatchingHandleBuffer != NULL);
1816
1817 for (HandleIndex = 0,*MatchingHandleCount = 0
1818 ; HandleIndex < HandleCount
1819 ; HandleIndex++
1820 ){
1821 //
1822 // Fill the allocated buffer with the handles that matched the attributes in Mask
1823 //
1824 if ((HandleType[HandleIndex] & Mask) == Mask) {
1825 (*MatchingHandleBuffer)[(*MatchingHandleCount)++] = HandleBuffer[HandleIndex];
1826 }
1827 }
1828
1829 //
1830 // Make the last one NULL
1831 //
1832 (*MatchingHandleBuffer)[*MatchingHandleCount] = NULL;
1833
1834 Status = EFI_SUCCESS;
1835 } // MacthingHandleBuffer == NULL (ELSE)
1836 } // *MatchingHandleCount == 0 (ELSE)
1837 } // no error on ParseHandleDatabaseByRelationshipWithType
1838
1839 if (HandleBuffer != NULL) {
1840 FreePool (HandleBuffer);
1841 }
1842
1843 if (HandleType != NULL) {
1844 FreePool (HandleType);
1845 }
1846
1847 return Status;
1848 }
1849
1850 /**
1851 Gets handles for any child controllers of the passed in controller.
1852
1853 @param[in] ControllerHandle The handle of the "parent controller"
1854 @param[in] MatchingHandleCount Pointer to the number of handles in
1855 MatchingHandleBuffer on return.
1856 @param[out] MatchingHandleBuffer Buffer containing handles on a successful
1857 return.
1858
1859
1860 @retval EFI_SUCCESS The operation was sucessful.
1861 **/
1862 EFI_STATUS
1863 EFIAPI
1864 ParseHandleDatabaseForChildControllers(
1865 IN CONST EFI_HANDLE ControllerHandle,
1866 IN UINTN *MatchingHandleCount,
1867 OUT EFI_HANDLE **MatchingHandleBuffer OPTIONAL
1868 )
1869 {
1870 EFI_STATUS Status;
1871 UINTN HandleIndex;
1872 UINTN DriverBindingHandleCount;
1873 EFI_HANDLE *DriverBindingHandleBuffer;
1874 UINTN DriverBindingHandleIndex;
1875 UINTN ChildControllerHandleCount;
1876 EFI_HANDLE *ChildControllerHandleBuffer;
1877 UINTN ChildControllerHandleIndex;
1878 EFI_HANDLE *HandleBufferForReturn;
1879
1880 if (MatchingHandleCount == NULL) {
1881 return (EFI_INVALID_PARAMETER);
1882 }
1883 *MatchingHandleCount = 0;
1884
1885 Status = PARSE_HANDLE_DATABASE_UEFI_DRIVERS (
1886 ControllerHandle,
1887 &DriverBindingHandleCount,
1888 &DriverBindingHandleBuffer
1889 );
1890 if (EFI_ERROR (Status)) {
1891 return Status;
1892 }
1893
1894 //
1895 // Get a buffer big enough for all the controllers.
1896 //
1897 HandleBufferForReturn = GetHandleListByProtocol(NULL);
1898 if (HandleBufferForReturn == NULL) {
1899 FreePool (DriverBindingHandleBuffer);
1900 return (EFI_NOT_FOUND);
1901 }
1902
1903 for (DriverBindingHandleIndex = 0; DriverBindingHandleIndex < DriverBindingHandleCount; DriverBindingHandleIndex++) {
1904 Status = PARSE_HANDLE_DATABASE_MANAGED_CHILDREN (
1905 DriverBindingHandleBuffer[DriverBindingHandleIndex],
1906 ControllerHandle,
1907 &ChildControllerHandleCount,
1908 &ChildControllerHandleBuffer
1909 );
1910 if (EFI_ERROR (Status)) {
1911 continue;
1912 }
1913
1914 for (ChildControllerHandleIndex = 0;
1915 ChildControllerHandleIndex < ChildControllerHandleCount;
1916 ChildControllerHandleIndex++
1917 ) {
1918 for (HandleIndex = 0; HandleIndex < *MatchingHandleCount; HandleIndex++) {
1919 if (HandleBufferForReturn[HandleIndex] == ChildControllerHandleBuffer[ChildControllerHandleIndex]) {
1920 break;
1921 }
1922 }
1923 if (HandleIndex >= *MatchingHandleCount) {
1924 HandleBufferForReturn[(*MatchingHandleCount)++] = ChildControllerHandleBuffer[ChildControllerHandleIndex];
1925 }
1926 }
1927
1928 FreePool (ChildControllerHandleBuffer);
1929 }
1930
1931 FreePool (DriverBindingHandleBuffer);
1932
1933 if (MatchingHandleBuffer != NULL) {
1934 *MatchingHandleBuffer = HandleBufferForReturn;
1935 } else {
1936 FreePool(HandleBufferForReturn);
1937 }
1938
1939 return (EFI_SUCCESS);
1940 }
1941
1942 /**
1943 Appends 1 buffer to another buffer. This will re-allocate the destination buffer
1944 if necessary to fit all of the data.
1945
1946 If DestinationBuffer is NULL, then ASSERT().
1947
1948 @param[in, out] DestinationBuffer The pointer to the pointer to the buffer to append onto.
1949 @param[in, out] DestinationSize The pointer to the size of DestinationBuffer.
1950 @param[in] SourceBuffer The pointer to the buffer to append onto DestinationBuffer.
1951 @param[in] SourceSize The number of bytes of SourceBuffer to append.
1952
1953 @retval NULL A memory allocation failed.
1954 @retval NULL A parameter was invalid.
1955 @return A pointer to (*DestinationBuffer).
1956 **/
1957 VOID*
1958 EFIAPI
1959 BuffernCatGrow (
1960 IN OUT VOID **DestinationBuffer,
1961 IN OUT UINTN *DestinationSize,
1962 IN VOID *SourceBuffer,
1963 IN UINTN SourceSize
1964 )
1965 {
1966 UINTN LocalDestinationSize;
1967 UINTN LocalDestinationFinalSize;
1968
1969 ASSERT(DestinationBuffer != NULL);
1970
1971 if (SourceSize == 0 || SourceBuffer == NULL) {
1972 return (*DestinationBuffer);
1973 }
1974
1975 if (DestinationSize == NULL) {
1976 LocalDestinationSize = 0;
1977 } else {
1978 LocalDestinationSize = *DestinationSize;
1979 }
1980
1981 LocalDestinationFinalSize = LocalDestinationSize + SourceSize;
1982
1983 if (DestinationSize != NULL) {
1984 *DestinationSize = LocalDestinationSize;
1985 }
1986
1987 if (LocalDestinationSize == 0) {
1988 // allcoate
1989 *DestinationBuffer = AllocateZeroPool(LocalDestinationFinalSize);
1990 } else {
1991 // reallocate
1992 *DestinationBuffer = ReallocatePool(LocalDestinationSize, LocalDestinationFinalSize, *DestinationBuffer);
1993 }
1994
1995 ASSERT(*DestinationBuffer != NULL);
1996
1997 // copy
1998 return (CopyMem(((UINT8*)(*DestinationBuffer)) + LocalDestinationSize, SourceBuffer, SourceSize));
1999 }
2000
2001 /**
2002 Gets handles for any child devices produced by the passed in driver.
2003
2004 @param[in] DriverHandle The handle of the driver.
2005 @param[in] MatchingHandleCount Pointer to the number of handles in
2006 MatchingHandleBuffer on return.
2007 @param[out] MatchingHandleBuffer Buffer containing handles on a successful
2008 return.
2009 @retval EFI_SUCCESS The operation was sucessful.
2010 @sa ParseHandleDatabaseByRelationship
2011 **/
2012 EFI_STATUS
2013 EFIAPI
2014 ParseHandleDatabaseForChildDevices(
2015 IN CONST EFI_HANDLE DriverHandle,
2016 IN UINTN *MatchingHandleCount,
2017 OUT EFI_HANDLE **MatchingHandleBuffer OPTIONAL
2018 )
2019 {
2020 EFI_HANDLE *Buffer;
2021 EFI_HANDLE *Buffer2;
2022 UINTN Count1;
2023 UINTN Count2;
2024 UINTN HandleIndex;
2025 EFI_STATUS Status;
2026 UINTN HandleBufferSize;
2027
2028 ASSERT(MatchingHandleCount != NULL);
2029
2030 HandleBufferSize = 0;
2031 Buffer = NULL;
2032 Buffer2 = NULL;
2033 *MatchingHandleCount = 0;
2034
2035 Status = PARSE_HANDLE_DATABASE_DEVICES (
2036 DriverHandle,
2037 &Count1,
2038 &Buffer
2039 );
2040 if (!EFI_ERROR (Status)) {
2041 for (HandleIndex = 0; HandleIndex < Count1; HandleIndex++) {
2042 //
2043 // now find the children
2044 //
2045 Status = PARSE_HANDLE_DATABASE_MANAGED_CHILDREN (
2046 DriverHandle,
2047 Buffer[HandleIndex],
2048 &Count2,
2049 &Buffer2
2050 );
2051 if (EFI_ERROR(Status)) {
2052 break;
2053 }
2054 //
2055 // save out required and optional data elements
2056 //
2057 *MatchingHandleCount += Count2;
2058 if (MatchingHandleBuffer != NULL) {
2059 *MatchingHandleBuffer = BuffernCatGrow((VOID**)MatchingHandleBuffer, &HandleBufferSize, Buffer2, Count2 * sizeof(Buffer2[0]));
2060 }
2061
2062 //
2063 // free the memory
2064 //
2065 if (Buffer2 != NULL) {
2066 FreePool(Buffer2);
2067 }
2068 }
2069 }
2070
2071 if (Buffer != NULL) {
2072 FreePool(Buffer);
2073 }
2074 return (Status);
2075 }
2076
2077 /**
2078 Function to get all handles that support a given protocol or all handles.
2079
2080 @param[in] ProtocolGuid The guid of the protocol to get handles for. If NULL
2081 then the function will return all handles.
2082
2083 @retval NULL A memory allocation failed.
2084 @return A NULL terminated list of handles.
2085 **/
2086 EFI_HANDLE*
2087 EFIAPI
2088 GetHandleListByProtocol (
2089 IN CONST EFI_GUID *ProtocolGuid OPTIONAL
2090 )
2091 {
2092 EFI_HANDLE *HandleList;
2093 UINTN Size;
2094 EFI_STATUS Status;
2095
2096 Size = 0;
2097 HandleList = NULL;
2098
2099 //
2100 // We cannot use LocateHandleBuffer since we need that NULL item on the ends of the list!
2101 //
2102 if (ProtocolGuid == NULL) {
2103 Status = gBS->LocateHandle(AllHandles, NULL, NULL, &Size, HandleList);
2104 if (Status == EFI_BUFFER_TOO_SMALL) {
2105 HandleList = AllocateZeroPool(Size + sizeof(EFI_HANDLE));
2106 if (HandleList == NULL) {
2107 return (NULL);
2108 }
2109 Status = gBS->LocateHandle(AllHandles, NULL, NULL, &Size, HandleList);
2110 HandleList[Size/sizeof(EFI_HANDLE)] = NULL;
2111 }
2112 } else {
2113 Status = gBS->LocateHandle(ByProtocol, (EFI_GUID*)ProtocolGuid, NULL, &Size, HandleList);
2114 if (Status == EFI_BUFFER_TOO_SMALL) {
2115 HandleList = AllocateZeroPool(Size + sizeof(EFI_HANDLE));
2116 if (HandleList == NULL) {
2117 return (NULL);
2118 }
2119 Status = gBS->LocateHandle(ByProtocol, (EFI_GUID*)ProtocolGuid, NULL, &Size, HandleList);
2120 HandleList[Size/sizeof(EFI_HANDLE)] = NULL;
2121 }
2122 }
2123 if (EFI_ERROR(Status)) {
2124 if (HandleList != NULL) {
2125 FreePool(HandleList);
2126 }
2127 return (NULL);
2128 }
2129 return (HandleList);
2130 }
2131
2132 /**
2133 Function to get all handles that support some protocols.
2134
2135 @param[in] ProtocolGuids A NULL terminated list of protocol GUIDs.
2136
2137 @retval NULL A memory allocation failed.
2138 @retval NULL ProtocolGuids was NULL.
2139 @return A NULL terminated list of EFI_HANDLEs.
2140 **/
2141 EFI_HANDLE*
2142 EFIAPI
2143 GetHandleListByProtocolList (
2144 IN CONST EFI_GUID **ProtocolGuids
2145 )
2146 {
2147 EFI_HANDLE *HandleList;
2148 UINTN Size;
2149 UINTN TotalSize;
2150 UINTN TempSize;
2151 EFI_STATUS Status;
2152 CONST EFI_GUID **GuidWalker;
2153 EFI_HANDLE *HandleWalker1;
2154 EFI_HANDLE *HandleWalker2;
2155
2156 Size = 0;
2157 HandleList = NULL;
2158 TotalSize = sizeof(EFI_HANDLE);
2159
2160 for (GuidWalker = ProtocolGuids ; GuidWalker != NULL && *GuidWalker != NULL ; GuidWalker++,Size = 0){
2161 Status = gBS->LocateHandle(ByProtocol, (EFI_GUID*)(*GuidWalker), NULL, &Size, NULL);
2162 if (Status == EFI_BUFFER_TOO_SMALL) {
2163 TotalSize += Size;
2164 }
2165 }
2166
2167 //
2168 // No handles were found...
2169 //
2170 if (TotalSize == sizeof(EFI_HANDLE)) {
2171 return (NULL);
2172 }
2173
2174 HandleList = AllocateZeroPool(TotalSize);
2175 if (HandleList == NULL) {
2176 return (NULL);
2177 }
2178
2179 Size = 0;
2180 for (GuidWalker = ProtocolGuids ; GuidWalker != NULL && *GuidWalker != NULL ; GuidWalker++){
2181 TempSize = TotalSize - Size;
2182 Status = gBS->LocateHandle(ByProtocol, (EFI_GUID*)(*GuidWalker), NULL, &TempSize, HandleList+(Size/sizeof(EFI_HANDLE)));
2183
2184 //
2185 // Allow for missing protocols... Only update the 'used' size upon success.
2186 //
2187 if (!EFI_ERROR(Status)) {
2188 Size += TempSize;
2189 }
2190 }
2191 ASSERT(HandleList[(TotalSize/sizeof(EFI_HANDLE))-1] == NULL);
2192
2193 for (HandleWalker1 = HandleList ; HandleWalker1 != NULL && *HandleWalker1 != NULL ; HandleWalker1++) {
2194 for (HandleWalker2 = HandleWalker1 + 1; HandleWalker2 != NULL && *HandleWalker2 != NULL ; HandleWalker2++) {
2195 if (*HandleWalker1 == *HandleWalker2) {
2196 //
2197 // copy memory back 1 handle width.
2198 //
2199 CopyMem(HandleWalker2, HandleWalker2 + 1, TotalSize - ((HandleWalker2-HandleList+1)*sizeof(EFI_HANDLE)));
2200 }
2201 }
2202 }
2203
2204 return (HandleList);
2205 }
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215