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