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