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