Fix bug where open() mode was not being set based on type of device being emulated.
[mirror_edk2.git] / ShellPkg / Library / UefiHandleParsingLib / UefiHandleParsingLib.c
CommitLineData
a405b86d 1/** @file\r
2 Provides interface to advanced shell functionality for parsing both handle and protocol database.\r
3\r
78ed876b 4 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
a405b86d 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "UefiHandleParsingLib.h"\r
16\r
a405b86d 17EFI_HANDLE mHandleParsingHiiHandle;\r
18HANDLE_INDEX_LIST mHandleList = {{{NULL,NULL},0,0},0};\r
19\r
20/**\r
21 Constructor for the library.\r
22\r
23 @param[in] ImageHandle Ignored.\r
24 @param[in] SystemTable Ignored.\r
25\r
26 @retval EFI_SUCCESS The operation was successful.\r
27**/\r
28EFI_STATUS\r
29EFIAPI\r
30HandleParsingLibConstructor (\r
31 IN EFI_HANDLE ImageHandle,\r
32 IN EFI_SYSTEM_TABLE *SystemTable\r
33 )\r
34{\r
bca163ff 35 mHandleParsingHiiHandle = HiiAddPackages (&gHandleParsingHiiGuid, gImageHandle, UefiHandleParsingLibStrings, NULL);\r
a405b86d 36 if (mHandleParsingHiiHandle == NULL) {\r
37 return (EFI_DEVICE_ERROR);\r
38 }\r
39\r
40 return (EFI_SUCCESS);\r
41}\r
42\r
43/**\r
44 Destructor for the library. free any resources.\r
45\r
46 @param[in] ImageHandle Ignored.\r
47 @param[in] SystemTable Ignored.\r
48\r
49 @retval EFI_SUCCESS The operation was successful.\r
50**/\r
51EFI_STATUS\r
52EFIAPI\r
53HandleParsingLibDestructor (\r
54 IN EFI_HANDLE ImageHandle,\r
55 IN EFI_SYSTEM_TABLE *SystemTable\r
56 )\r
57{\r
58 if (mHandleParsingHiiHandle != NULL) {\r
59 HiiRemovePackages(mHandleParsingHiiHandle);\r
60 }\r
61 return (EFI_SUCCESS);\r
62}\r
63\r
64/*\r
65CHAR16*\r
66EFIAPI\r
67LoadedImageProtocolDumpInformation(\r
68 IN CONST EFI_HANDLE TheHandle,\r
69 IN CONST BOOLEAN Verbose\r
70 )\r
71{\r
72 EFI_LOADED_IMAGE_PROTOCOL *Image;\r
73 EFI_STATUS Status;\r
74 EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
75 EFI_DEVICE_PATH_PROTOCOL *DevPathNode;\r
76 VOID *Buffer;\r
77 UINTN BufferSize;\r
78 UINT32 AuthenticationStatus;\r
79 EFI_GUID *NameGuid;\r
80 EFI_FIRMWARE_VOLUME_PROTOCOL *FV;\r
81 EFI_FIRMWARE_VOLUME2_PROTOCOL *FV2;\r
82\r
83 FV = NULL;\r
84 FV2 = NULL;\r
85 Buffer = NULL;\r
86 BufferSize = 0;\r
87\r
88 Status = HandleProtocol (\r
89 TheHandle,\r
90 &gEfiLoadedImageProtocolGuid,\r
91 &Image);\r
92 ASSERT_EFI_ERROR(Status);\r
93\r
94 DevPath = UnpackDevicePath (Image->FilePath);\r
95\r
96 if (DevPath == NULL) {\r
97 return NULL;\r
98 }\r
99\r
100 DevPathNode = DevPath;\r
101\r
102 while (!IsDevicePathEnd (DevPathNode)) {\r
103 //\r
104 // Find the Fv File path\r
105 //\r
106 NameGuid = GetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)DevPathNode);\r
107 if (NameGuid != NULL) {\r
108 Status = BS->HandleProtocol (\r
109 Image->DeviceHandle,\r
110 &gEfiFirmwareVolumeProtocolGuid,\r
111 &FV\r
112 );\r
113 if (!EFI_ERROR (Status)) {\r
114 Status = FV->ReadSection (\r
115 FV,\r
116 NameGuid,\r
117 EFI_SECTION_USER_INTERFACE,\r
118 0,\r
119 &Buffer,\r
120 &BufferSize,\r
121 &AuthenticationStatus\r
122 );\r
123 if (!EFI_ERROR (Status)) {\r
124 break;\r
125 }\r
126\r
127 Buffer = NULL;\r
128 } else {\r
129 Status = BS->HandleProtocol (\r
130 Image->DeviceHandle,\r
131 &gEfiFirmwareVolume2ProtocolGuid,\r
132 &FV2\r
133 );\r
134 if (!EFI_ERROR (Status)) {\r
135 Status = FV2->ReadSection (\r
136 FV2,\r
137 NameGuid,\r
138 EFI_SECTION_USER_INTERFACE,\r
139 0,\r
140 &Buffer,\r
141 &BufferSize,\r
142 &AuthenticationStatus\r
143 );\r
144 if (!EFI_ERROR (Status)) {\r
145 break;\r
146 }\r
147\r
148 Buffer = NULL;\r
149 }\r
150 }\r
151 }\r
152 //\r
153 // Next device path node\r
154 //\r
155 DevPathNode = NextDevicePathNode (DevPathNode);\r
156 }\r
157\r
158 FreePool (DevPath);\r
159 return Buffer;\r
160}\r
161*/\r
162\r
163/**\r
164 Function to dump information about SimpleTextOut.\r
165\r
166 This will allocate the return buffer from boot services pool.\r
167\r
168 @param[in] TheHandle The handle that has SimpleTextOut installed.\r
169 @param[in] Verbose TRUE for additional information, FALSE otherwise.\r
170\r
171 @retval A poitner to a string containing the information.\r
172**/\r
173CHAR16*\r
174EFIAPI\r
175TxtOutProtocolDumpInformation(\r
176 IN CONST EFI_HANDLE TheHandle,\r
177 IN CONST BOOLEAN Verbose\r
178 )\r
179{\r
180 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *Dev;\r
181 INTN Index;\r
182 UINTN Col;\r
183 UINTN Row;\r
184 EFI_STATUS Status;\r
185 CHAR16 *RetVal;\r
186 UINTN Size;\r
187 CHAR16 *Temp;\r
188 UINTN NewSize;\r
189\r
190 if (!Verbose) {\r
191 return (NULL);\r
192 }\r
193\r
194 RetVal = NULL;\r
195 Size = 0;\r
196\r
197 Status = gBS->HandleProtocol(\r
198 TheHandle,\r
199 &gEfiSimpleTextOutProtocolGuid,\r
200 (VOID**)&Dev);\r
201\r
202 ASSERT_EFI_ERROR(Status);\r
203 ASSERT (Dev != NULL && Dev->Mode != NULL);\r
204\r
205 Size = (Dev->Mode->MaxMode + 1) * 80;\r
206 RetVal = AllocateZeroPool(Size);\r
207\r
208 Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_TXT_OUT_DUMP_HEADER), NULL);\r
ecae5117 209 if (Temp != NULL) {\r
210 UnicodeSPrint(RetVal, Size, Temp, Dev, Dev->Mode->Attribute);\r
211 FreePool(Temp);\r
212 }\r
a405b86d 213\r
214 //\r
215 // Dump TextOut Info\r
216 //\r
217 Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_TXT_OUT_DUMP_LINE), NULL);\r
218 for (Index = 0; Index < Dev->Mode->MaxMode; Index++) {\r
219 Status = Dev->QueryMode (Dev, Index, &Col, &Row);\r
220 NewSize = Size - StrSize(RetVal);\r
221 UnicodeSPrint(\r
222 RetVal + StrLen(RetVal),\r
223 NewSize,\r
ecae5117 224 Temp == NULL?L"":Temp,\r
a405b86d 225 Index == Dev->Mode->Mode ? L'*' : L' ',\r
226 Index,\r
227 !EFI_ERROR(Status)?Col:-1,\r
228 !EFI_ERROR(Status)?Row:-1\r
229 );\r
230 }\r
231 FreePool(Temp);\r
232 return (RetVal);\r
233}\r
234\r
235STATIC CONST UINTN VersionStringSize = 60;\r
236\r
237/**\r
238 Function to dump information about EfiDriverSupportedEfiVersion protocol.\r
239\r
240 This will allocate the return buffer from boot services pool.\r
241\r
242 @param[in] TheHandle The handle that has the protocol installed.\r
243 @param[in] Verbose TRUE for additional information, FALSE otherwise.\r
244\r
245 @retval A poitner to a string containing the information.\r
246**/\r
247CHAR16*\r
248EFIAPI\r
249DriverEfiVersionProtocolDumpInformation(\r
250 IN CONST EFI_HANDLE TheHandle,\r
251 IN CONST BOOLEAN Verbose\r
252 )\r
253{\r
254 EFI_DRIVER_SUPPORTED_EFI_VERSION_PROTOCOL *DriverEfiVersion;\r
255 EFI_STATUS Status;\r
256 CHAR16 *RetVal;\r
257\r
258 Status = gBS->HandleProtocol(\r
259 TheHandle,\r
260 &gEfiDriverSupportedEfiVersionProtocolGuid,\r
261 (VOID**)&DriverEfiVersion);\r
262\r
263 ASSERT_EFI_ERROR(Status);\r
264\r
78ed876b 265 RetVal = AllocateZeroPool(VersionStringSize);\r
a405b86d 266 ASSERT(RetVal != NULL);\r
267 UnicodeSPrint(RetVal, VersionStringSize, L"0x%08x", DriverEfiVersion->FirmwareVersion);\r
268 return (RetVal);\r
269}\r
270\r
271/**\r
272 Function to dump information about DevicePath protocol.\r
273\r
274 This will allocate the return buffer from boot services pool.\r
275\r
276 @param[in] TheHandle The handle that has the protocol installed.\r
277 @param[in] Verbose TRUE for additional information, FALSE otherwise.\r
278\r
279 @retval A poitner to a string containing the information.\r
280**/\r
281CHAR16*\r
282EFIAPI\r
283DevicePathProtocolDumpInformation(\r
284 IN CONST EFI_HANDLE TheHandle,\r
285 IN CONST BOOLEAN Verbose\r
286 )\r
287{\r
288 EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
289 CHAR16 *Temp;\r
290 CHAR16 *Temp2;\r
291 EFI_STATUS Status;\r
292 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *DevPathToText;\r
293 Temp = NULL;\r
294\r
295 Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID**)&DevPathToText);\r
296 if (!EFI_ERROR(Status)) {\r
297 Status = gBS->OpenProtocol(TheHandle, &gEfiDevicePathProtocolGuid, (VOID**)&DevPath, gImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
298 if (!EFI_ERROR(Status)) {\r
299 //\r
300 // I cannot decide whether to allow shortcuts here (the second BOOLEAN on the next line)\r
301 //\r
302 Temp = DevPathToText->ConvertDevicePathToText(DevPath, TRUE, TRUE);\r
303 gBS->CloseProtocol(TheHandle, &gEfiDevicePathProtocolGuid, gImageHandle, NULL);\r
304 }\r
305 }\r
78ed876b 306 if (!Verbose && Temp != NULL && StrLen(Temp) > 30) {\r
a405b86d 307 Temp2 = NULL;\r
308 Temp2 = StrnCatGrow(&Temp2, NULL, Temp+(StrLen(Temp) - 30), 30);\r
309 FreePool(Temp);\r
310 Temp = Temp2;\r
311 }\r
312 return (Temp);\r
313}\r
314\r
315//\r
316// Put the information on the NT32 protocol GUIDs here so we are not dependant on the Nt32Pkg\r
317//\r
318#define LOCAL_EFI_WIN_NT_THUNK_PROTOCOL_GUID \\r
319 { \\r
320 0x58c518b1, 0x76f3, 0x11d4, 0xbc, 0xea, 0x0, 0x80, 0xc7, 0x3c, 0x88, 0x81 \\r
321 }\r
322\r
323#define LOCAL_EFI_WIN_NT_BUS_DRIVER_IO_PROTOCOL_GUID \\r
324 { \\r
325 0x96eb4ad6, 0xa32a, 0x11d4, 0xbc, 0xfd, 0x0, 0x80, 0xc7, 0x3c, 0x88, 0x81 \\r
326 }\r
327\r
328#define LOCAL_EFI_WIN_NT_SERIAL_PORT_GUID \\r
329 { \\r
330 0xc95a93d, 0xa006, 0x11d4, 0xbc, 0xfa, 0x0, 0x80, 0xc7, 0x3c, 0x88, 0x81 \\r
331 }\r
332STATIC CONST EFI_GUID WinNtThunkProtocolGuid = LOCAL_EFI_WIN_NT_THUNK_PROTOCOL_GUID;\r
333STATIC CONST EFI_GUID WinNtIoProtocolGuid = LOCAL_EFI_WIN_NT_BUS_DRIVER_IO_PROTOCOL_GUID;\r
334STATIC CONST EFI_GUID WinNtSerialPortGuid = LOCAL_EFI_WIN_NT_SERIAL_PORT_GUID;\r
335\r
336STATIC CONST PROTOCOL_INFO_BLOCK mGuidStringListNT[] = {\r
337 {STRING_TOKEN(STR_WINNT_THUNK), (EFI_GUID*)&WinNtThunkProtocolGuid, NULL},\r
338 {STRING_TOKEN(STR_WINNT_DRIVER_IO), (EFI_GUID*)&WinNtIoProtocolGuid, NULL},\r
339 {STRING_TOKEN(STR_WINNT_SERIAL_PORT), (EFI_GUID*)&WinNtSerialPortGuid, NULL},\r
340 {STRING_TOKEN(STR_UNKNOWN_DEVICE), NULL, NULL},\r
341};\r
342\r
343STATIC CONST PROTOCOL_INFO_BLOCK mGuidStringList[] = {\r
344 {STRING_TOKEN(STR_LOADED_IMAGE), &gEfiLoadedImageProtocolGuid, NULL},\r
345 {STRING_TOKEN(STR_DEVICE_PATH), &gEfiDevicePathProtocolGuid, DevicePathProtocolDumpInformation},\r
346 {STRING_TOKEN(STR_IMAGE_PATH), &gEfiLoadedImageDevicePathProtocolGuid, DevicePathProtocolDumpInformation},\r
347 {STRING_TOKEN(STR_DEVICE_PATH_UTIL), &gEfiDevicePathUtilitiesProtocolGuid, NULL},\r
348 {STRING_TOKEN(STR_DEVICE_PATH_TXT), &gEfiDevicePathToTextProtocolGuid, NULL},\r
349 {STRING_TOKEN(STR_DEVICE_PATH_FTXT), &gEfiDevicePathFromTextProtocolGuid, NULL},\r
350 {STRING_TOKEN(STR_DEVICE_PATH_PC), &gEfiPcAnsiGuid, NULL},\r
351 {STRING_TOKEN(STR_DEVICE_PATH_VT100), &gEfiVT100Guid, NULL},\r
352 {STRING_TOKEN(STR_DEVICE_PATH_VT100P), &gEfiVT100PlusGuid, NULL},\r
353 {STRING_TOKEN(STR_DEVICE_PATH_VTUTF8), &gEfiVTUTF8Guid, NULL},\r
354 {STRING_TOKEN(STR_DRIVER_BINDING), &gEfiDriverBindingProtocolGuid, NULL},\r
355 {STRING_TOKEN(STR_PLATFORM_OVERRIDE), &gEfiPlatformDriverOverrideProtocolGuid, NULL},\r
356 {STRING_TOKEN(STR_BUS_OVERRIDE), &gEfiBusSpecificDriverOverrideProtocolGuid, NULL},\r
357 {STRING_TOKEN(STR_DRIVER_DIAG), &gEfiDriverDiagnosticsProtocolGuid, NULL},\r
358 {STRING_TOKEN(STR_DRIVER_DIAG2), &gEfiDriverDiagnostics2ProtocolGuid, NULL},\r
359 {STRING_TOKEN(STR_DRIVER_CN), &gEfiComponentNameProtocolGuid, NULL},\r
360 {STRING_TOKEN(STR_DRIVER_CN2), &gEfiComponentName2ProtocolGuid, NULL},\r
361 {STRING_TOKEN(STR_PLAT_DRV_CFG), &gEfiPlatformToDriverConfigurationProtocolGuid, NULL},\r
362 {STRING_TOKEN(STR_DRIVER_VERSION), &gEfiDriverSupportedEfiVersionProtocolGuid, DriverEfiVersionProtocolDumpInformation},\r
363 {STRING_TOKEN(STR_TXT_IN), &gEfiSimpleTextInProtocolGuid, NULL},\r
364 {STRING_TOKEN(STR_TXT_IN_EX), &gEfiSimpleTextInputExProtocolGuid, NULL},\r
365 {STRING_TOKEN(STR_TXT_OUT), &gEfiSimpleTextOutProtocolGuid, TxtOutProtocolDumpInformation},\r
366 {STRING_TOKEN(STR_SIM_POINTER), &gEfiSimplePointerProtocolGuid, NULL},\r
367 {STRING_TOKEN(STR_ABS_POINTER), &gEfiAbsolutePointerProtocolGuid, NULL},\r
368 {STRING_TOKEN(STR_SERIAL_IO), &gEfiSerialIoProtocolGuid, NULL},\r
369 {STRING_TOKEN(STR_GRAPHICS_OUTPUT), &gEfiGraphicsOutputProtocolGuid, NULL},\r
370 {STRING_TOKEN(STR_EDID_DISCOVERED), &gEfiEdidDiscoveredProtocolGuid, NULL},\r
371 {STRING_TOKEN(STR_EDID_ACTIVE), &gEfiEdidActiveProtocolGuid, NULL},\r
372 {STRING_TOKEN(STR_EDID_OVERRIDE), &gEfiEdidOverrideProtocolGuid, NULL},\r
373 {STRING_TOKEN(STR_CON_IN), &gEfiConsoleInDeviceGuid, NULL},\r
374 {STRING_TOKEN(STR_CON_OUT), &gEfiConsoleOutDeviceGuid, NULL},\r
375 {STRING_TOKEN(STR_STD_ERR), &gEfiStandardErrorDeviceGuid, NULL},\r
376 {STRING_TOKEN(STR_LOAD_FILE), &gEfiLoadFileProtocolGuid, NULL},\r
377 {STRING_TOKEN(STR_LOAD_FILE2), &gEfiLoadFile2ProtocolGuid, NULL},\r
378 {STRING_TOKEN(STR_SIMPLE_FILE_SYS), &gEfiSimpleFileSystemProtocolGuid, NULL},\r
379 {STRING_TOKEN(STR_FILE_INFO), &gEfiFileInfoGuid, NULL},\r
380 {STRING_TOKEN(STR_FILE_SYS_INFO), &gEfiFileSystemInfoGuid, NULL},\r
381 {STRING_TOKEN(STR_TAPE_IO), &gEfiTapeIoProtocolGuid, NULL},\r
382 {STRING_TOKEN(STR_DISK_IO), &gEfiDiskIoProtocolGuid, NULL},\r
383 {STRING_TOKEN(STR_BLK_IO), &gEfiBlockIoProtocolGuid, NULL},\r
384 {STRING_TOKEN(STR_UC), &gEfiUnicodeCollationProtocolGuid, NULL},\r
385 {STRING_TOKEN(STR_UC2), &gEfiUnicodeCollation2ProtocolGuid, NULL},\r
386 {STRING_TOKEN(STR_PCIRB_IO), &gEfiPciRootBridgeIoProtocolGuid, NULL},\r
387 {STRING_TOKEN(STR_PCI_IO), &gEfiPciIoProtocolGuid, NULL},\r
388 {STRING_TOKEN(STR_SCSI_PT), &gEfiScsiPassThruProtocolGuid, NULL},\r
389 {STRING_TOKEN(STR_SCSI_IO), &gEfiScsiIoProtocolGuid, NULL},\r
390 {STRING_TOKEN(STR_SCSI_PT_EXT), &gEfiExtScsiPassThruProtocolGuid, NULL},\r
391 {STRING_TOKEN(STR_ISCSI), &gEfiIScsiInitiatorNameProtocolGuid, NULL},\r
392 {STRING_TOKEN(STR_USB_IO), &gEfiUsbIoProtocolGuid, NULL},\r
393 {STRING_TOKEN(STR_USB_HC), &gEfiUsbHcProtocolGuid, NULL},\r
394 {STRING_TOKEN(STR_USB_HC2), &gEfiUsb2HcProtocolGuid, NULL},\r
395 {STRING_TOKEN(STR_DEBUG_SUPPORT), &gEfiDebugSupportProtocolGuid, NULL},\r
396 {STRING_TOKEN(STR_DEBUG_PORT), &gEfiDebugPortProtocolGuid, NULL},\r
397 {STRING_TOKEN(STR_DECOMPRESS), &gEfiDecompressProtocolGuid, NULL},\r
398 {STRING_TOKEN(STR_ACPI_TABLE), &gEfiAcpiTableProtocolGuid, NULL},\r
399 {STRING_TOKEN(STR_EBC_INTERPRETER), &gEfiEbcProtocolGuid, NULL},\r
400 {STRING_TOKEN(STR_SNP), &gEfiSimpleNetworkProtocolGuid, NULL},\r
401 {STRING_TOKEN(STR_NII), &gEfiNetworkInterfaceIdentifierProtocolGuid, NULL},\r
402 {STRING_TOKEN(STR_NII_31), &gEfiNetworkInterfaceIdentifierProtocolGuid_31, NULL},\r
403 {STRING_TOKEN(STR_PXE_BC), &gEfiPxeBaseCodeProtocolGuid, NULL},\r
404 {STRING_TOKEN(STR_PXE_CB), &gEfiPxeBaseCodeCallbackProtocolGuid, NULL},\r
405 {STRING_TOKEN(STR_BIS), &gEfiBisProtocolGuid, NULL},\r
406 {STRING_TOKEN(STR_MNP_SB), &gEfiManagedNetworkServiceBindingProtocolGuid, NULL},\r
407 {STRING_TOKEN(STR_MNP), &gEfiManagedNetworkProtocolGuid, NULL},\r
408 {STRING_TOKEN(STR_ARP_SB), &gEfiArpServiceBindingProtocolGuid, NULL},\r
409 {STRING_TOKEN(STR_ARP), &gEfiArpProtocolGuid, NULL},\r
410 {STRING_TOKEN(STR_DHCPV4_SB), &gEfiDhcp4ServiceBindingProtocolGuid, NULL},\r
411 {STRING_TOKEN(STR_DHCPV4), &gEfiDhcp4ProtocolGuid, NULL},\r
412 {STRING_TOKEN(STR_TCPV4_SB), &gEfiTcp4ServiceBindingProtocolGuid, NULL},\r
413 {STRING_TOKEN(STR_TCPV4), &gEfiTcp4ProtocolGuid, NULL},\r
414 {STRING_TOKEN(STR_IPV4_SB), &gEfiIp4ServiceBindingProtocolGuid, NULL},\r
415 {STRING_TOKEN(STR_IPV4), &gEfiIp4ProtocolGuid, NULL},\r
416 {STRING_TOKEN(STR_IPV4_CFG), &gEfiIp4ConfigProtocolGuid, NULL},\r
417 {STRING_TOKEN(STR_SHELL_PARAMETERS), &gEfiShellParametersProtocolGuid, NULL},\r
418 {STRING_TOKEN(STR_SHELL), &gEfiShellProtocolGuid, NULL},\r
419 {STRING_TOKEN(STR_EFI_GLOBAL_VARIABLE), &gEfiGlobalVariableGuid, NULL},\r
420 {STRING_TOKEN(STR_UDPV4_SB), &gEfiUdp4ServiceBindingProtocolGuid, NULL},\r
421 {STRING_TOKEN(STR_UDPV4), &gEfiUdp4ProtocolGuid, NULL},\r
422 {STRING_TOKEN(STR_MTFTPV4_SB), &gEfiMtftp4ServiceBindingProtocolGuid, NULL},\r
423 {STRING_TOKEN(STR_MTFTPV4), &gEfiMtftp4ProtocolGuid, NULL},\r
424 {STRING_TOKEN(STR_AUTH_INFO), &gEfiAuthenticationInfoProtocolGuid, NULL},\r
425 {STRING_TOKEN(STR_HASH_SB), &gEfiHashServiceBindingProtocolGuid, NULL},\r
426 {STRING_TOKEN(STR_HASH), &gEfiHashProtocolGuid, NULL},\r
427 {STRING_TOKEN(STR_HII_FONT), &gEfiHiiFontProtocolGuid, NULL},\r
428 {STRING_TOKEN(STR_HII_STRING), &gEfiHiiStringProtocolGuid, NULL},\r
429 {STRING_TOKEN(STR_HII_IMAGE), &gEfiHiiImageProtocolGuid, NULL},\r
430 {STRING_TOKEN(STR_HII_DATABASE), &gEfiHiiDatabaseProtocolGuid, NULL},\r
431 {STRING_TOKEN(STR_HII_CONFIG_ROUT), &gEfiHiiConfigRoutingProtocolGuid, NULL},\r
432 {STRING_TOKEN(STR_HII_CONFIG_ACC), &gEfiHiiConfigAccessProtocolGuid, NULL},\r
433 {STRING_TOKEN(STR_HII_FORM_BROWSER2), &gEfiFormBrowser2ProtocolGuid, NULL},\r
434 {STRING_TOKEN(STR_SHELL_INTERFACE), &gEfiShellInterfaceGuid, NULL},\r
435 {STRING_TOKEN(STR_SHELL_ENV2), &gEfiShellEnvironment2Guid, NULL},\r
436 {STRING_TOKEN(STR_SHELL_ENV), &gEfiShellEnvironment2Guid, NULL},\r
437 {STRING_TOKEN(STR_DEVICE_IO), &gEfiDeviceIoProtocolGuid, NULL},\r
438 {STRING_TOKEN(STR_UGA_DRAW), &gEfiUgaDrawProtocolGuid, NULL},\r
439 {STRING_TOKEN(STR_UGA_IO), &gEfiUgaIoProtocolGuid, NULL},\r
440 {STRING_TOKEN(STR_ESP), &gEfiPartTypeSystemPartGuid, NULL},\r
441 {STRING_TOKEN(STR_GPT_NBR), &gEfiPartTypeLegacyMbrGuid, NULL},\r
442 {STRING_TOKEN(STR_DRIVER_CONFIG), &gEfiDriverConfigurationProtocolGuid, NULL},\r
443 {STRING_TOKEN(STR_DRIVER_CONFIG2), &gEfiDriverConfiguration2ProtocolGuid, NULL},\r
444 {STRING_TOKEN(STR_UNKNOWN_DEVICE), NULL, NULL},\r
445};\r
446\r
447/**\r
448 Function to get the node for a protocol or struct from it's GUID.\r
449\r
450 if Guid is NULL, then ASSERT.\r
451\r
452 @param[in] Guid The GUID to look for the name of.\r
453\r
454 @return The node.\r
455**/\r
456CONST PROTOCOL_INFO_BLOCK *\r
457EFIAPI\r
458InternalShellGetNodeFromGuid(\r
459 IN CONST EFI_GUID* Guid\r
460 )\r
461{\r
462 CONST PROTOCOL_INFO_BLOCK *ListWalker;\r
463\r
464 ASSERT(Guid != NULL);\r
465\r
466 if (PcdGetBool(PcdShellIncludeNtGuids)) {\r
467 for (ListWalker = mGuidStringListNT ; ListWalker != NULL && ListWalker->GuidId != NULL ; ListWalker++) {\r
468 if (CompareGuid(ListWalker->GuidId, Guid)) {\r
469 return (ListWalker);\r
470 }\r
471 }\r
472 }\r
473 for (ListWalker = mGuidStringList ; ListWalker != NULL && ListWalker->GuidId != NULL ; ListWalker++) {\r
474 if (CompareGuid(ListWalker->GuidId, Guid)) {\r
475 return (ListWalker);\r
476 }\r
477 }\r
478 return (ListWalker);\r
479}\r
480\r
481/**\r
482 Function to get the name of a protocol or struct from it's GUID.\r
483\r
484 if Guid is NULL, then ASSERT.\r
485\r
486 @param[in] Guid The GUID to look for the name of.\r
487 @param[in] Lang The language to use.\r
488\r
489 @return pointer to string of the name. The caller\r
490 is responsible to free this memory.\r
491**/\r
492CHAR16*\r
493EFIAPI\r
494GetStringNameFromGuid(\r
495 IN CONST EFI_GUID *Guid,\r
496 IN CONST CHAR8 *Lang OPTIONAL\r
497 )\r
498{\r
499 CONST PROTOCOL_INFO_BLOCK *Id;\r
500\r
501 Id = InternalShellGetNodeFromGuid(Guid);\r
502 return (HiiGetString(mHandleParsingHiiHandle, Id->StringId, Lang));\r
503}\r
504\r
505/**\r
506 Function to dump protocol information from a handle.\r
507\r
508 This function will return a allocated string buffer containing the\r
509 information. The caller is responsible for freeing the memory.\r
510\r
511 If Guid is NULL, ASSERT().\r
512 If TheHandle is NULL, ASSERT().\r
513\r
514 @param[in] TheHandle The handle to dump information from.\r
515 @param[in] Guid The GUID of the protocol to dump.\r
516 @param[in] Verbose TRUE for extra info. FALSE otherwise.\r
517\r
518 @return The pointer to string.\r
519 @retval NULL An error was encountered.\r
520**/\r
521CHAR16*\r
522EFIAPI\r
523GetProtocolInformationDump(\r
524 IN CONST EFI_HANDLE TheHandle,\r
525 IN CONST EFI_GUID *Guid,\r
526 IN CONST BOOLEAN Verbose\r
527 )\r
528{\r
529 CONST PROTOCOL_INFO_BLOCK *Id;\r
530\r
531 ASSERT(TheHandle != NULL);\r
532 ASSERT(Guid != NULL);\r
533\r
534 if (TheHandle == NULL || Guid == NULL) {\r
535 return (NULL);\r
536 }\r
537\r
538 Id = InternalShellGetNodeFromGuid(Guid);\r
539 if (Id != NULL && Id->DumpInfo != NULL) {\r
540 return (Id->DumpInfo(TheHandle, Verbose));\r
541 }\r
542 return (NULL);\r
543}\r
544\r
545/**\r
546 Function to get the Guid for a protocol or struct based on it's string name.\r
547\r
548 @param[in] Name The pointer to the string name.\r
549 @param[in] Lang The pointer to the language code.\r
550 @param[in] Guid The pointer to the Guid.\r
551\r
552 @retval EFI_SUCCESS The operation was sucessful.\r
553**/\r
554EFI_STATUS\r
555EFIAPI\r
556GetGuidFromStringName(\r
557 IN CONST CHAR16 *Name,\r
558 IN CONST CHAR8 *Lang OPTIONAL,\r
559 IN EFI_GUID **Guid\r
560 )\r
561{\r
562 CONST PROTOCOL_INFO_BLOCK *ListWalker;\r
563 CHAR16 *String;\r
564\r
565 ASSERT(Guid != NULL);\r
566 if (Guid == NULL) {\r
567 return (EFI_INVALID_PARAMETER);\r
568 }\r
569 *Guid = NULL;\r
570\r
571 if (PcdGetBool(PcdShellIncludeNtGuids)) {\r
572 for (ListWalker = mGuidStringListNT ; ListWalker != NULL && ListWalker->GuidId != NULL ; ListWalker++) {\r
573 String = HiiGetString(mHandleParsingHiiHandle, ListWalker->StringId, Lang);\r
574 if (Name != NULL && String != NULL && StrCmp(Name, String)==0) {\r
575 *Guid = ListWalker->GuidId;\r
576 }\r
577 SHELL_FREE_NON_NULL(String);\r
578 if (*Guid != NULL) {\r
579 return (EFI_SUCCESS);\r
580 }\r
581 }\r
582 }\r
583 for (ListWalker = mGuidStringList ; ListWalker != NULL && ListWalker->GuidId != NULL ; ListWalker++) {\r
584 String = HiiGetString(mHandleParsingHiiHandle, ListWalker->StringId, Lang);\r
585 if (Name != NULL && String != NULL && StrCmp(Name, String)==0) {\r
586 *Guid = ListWalker->GuidId;\r
587 }\r
588 SHELL_FREE_NON_NULL(String);\r
589 if (*Guid != NULL) {\r
590 return (EFI_SUCCESS);\r
591 }\r
592 }\r
593 return (EFI_NOT_FOUND);\r
594}\r
595\r
596/**\r
597 Function to retrieve the driver name (if possible) from the ComponentName or\r
598 ComponentName2 protocol\r
599\r
600 @param[in] TheHandle The driver handle to get the name of.\r
601 @param[in] Language The language to use.\r
602\r
603 @retval NULL The name could not be found.\r
604 @return A pointer to the string name. Do not de-allocate the memory.\r
605**/\r
606CONST CHAR16*\r
607EFIAPI\r
608GetStringNameFromHandle(\r
609 IN CONST EFI_HANDLE TheHandle,\r
610 IN CONST CHAR8 *Language\r
611 )\r
612{\r
613 EFI_COMPONENT_NAME2_PROTOCOL *CompNameStruct;\r
614 EFI_STATUS Status;\r
615 CHAR16 *RetVal;\r
616\r
617 Status = gBS->OpenProtocol(\r
618 TheHandle,\r
619 &gEfiComponentName2ProtocolGuid,\r
620 (VOID**)&CompNameStruct,\r
621 gImageHandle,\r
622 NULL,\r
623 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
624 if (!EFI_ERROR(Status)) {\r
625 Status = CompNameStruct->GetDriverName(CompNameStruct, (CHAR8*)Language, &RetVal);\r
626 if (!EFI_ERROR(Status)) {\r
627 return (RetVal);\r
628 }\r
629 }\r
630 Status = gBS->OpenProtocol(\r
631 TheHandle,\r
632 &gEfiComponentNameProtocolGuid,\r
633 (VOID**)&CompNameStruct,\r
634 gImageHandle,\r
635 NULL,\r
636 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
637 if (!EFI_ERROR(Status)) {\r
638 Status = CompNameStruct->GetDriverName(CompNameStruct, (CHAR8*)Language, &RetVal);\r
639 if (!EFI_ERROR(Status)) {\r
640 return (RetVal);\r
641 }\r
642 }\r
643 return (NULL);\r
644}\r
645\r
646/**\r
647 Function to initialize the file global mHandleList object for use in\r
648 vonverting handles to index and index to handle.\r
649\r
650 @retval EFI_SUCCESS The operation was successful.\r
651**/\r
652EFI_STATUS\r
653EFIAPI\r
654InternalShellInitHandleList(\r
655 VOID\r
656 )\r
657{\r
658 EFI_STATUS Status;\r
659 EFI_HANDLE *HandleBuffer;\r
660 UINTN HandleCount;\r
661 HANDLE_LIST *ListWalker;\r
662\r
663 if (mHandleList.NextIndex != 0) {\r
664 return EFI_SUCCESS;\r
665 }\r
666 InitializeListHead(&mHandleList.List.Link);\r
667 mHandleList.NextIndex = 1;\r
668 Status = gBS->LocateHandleBuffer (\r
669 AllHandles,\r
670 NULL,\r
671 NULL,\r
672 &HandleCount,\r
673 &HandleBuffer\r
674 );\r
675 ASSERT_EFI_ERROR(Status);\r
676 if (EFI_ERROR(Status)) {\r
677 return (Status);\r
678 }\r
679 for (mHandleList.NextIndex = 1 ; mHandleList.NextIndex <= HandleCount ; mHandleList.NextIndex++){\r
78ed876b 680 ListWalker = AllocateZeroPool(sizeof(HANDLE_LIST));\r
a405b86d 681 ASSERT(ListWalker != NULL);\r
682 ListWalker->TheHandle = HandleBuffer[mHandleList.NextIndex-1];\r
683 ListWalker->TheIndex = mHandleList.NextIndex;\r
684 InsertTailList(&mHandleList.List.Link,&ListWalker->Link);\r
685 }\r
686 FreePool(HandleBuffer);\r
687 return (EFI_SUCCESS);\r
688}\r
689\r
690/**\r
691 Function to retrieve the human-friendly index of a given handle. If the handle\r
692 does not have a index one will be automatically assigned. The index value is valid\r
693 until the termination of the shell application.\r
694\r
695 @param[in] TheHandle The handle to retrieve an index for.\r
696\r
697 @retval 0 A memory allocation failed.\r
698 @return The index of the handle.\r
699\r
700**/\r
701UINTN\r
702EFIAPI\r
703ConvertHandleToHandleIndex(\r
704 IN CONST EFI_HANDLE TheHandle\r
705 )\r
706{\r
707 HANDLE_LIST *ListWalker;\r
78ed876b 708 if (TheHandle == NULL) {\r
709 return 0;\r
710 }\r
a405b86d 711\r
712 InternalShellInitHandleList();\r
713\r
714 for (ListWalker = (HANDLE_LIST*)GetFirstNode(&mHandleList.List.Link)\r
715 ; !IsNull(&mHandleList.List.Link,&ListWalker->Link)\r
716 ; ListWalker = (HANDLE_LIST*)GetNextNode(&mHandleList.List.Link,&ListWalker->Link)\r
717 ){\r
718 if (ListWalker->TheHandle == TheHandle) {\r
719 return (ListWalker->TheIndex);\r
720 }\r
721 }\r
78ed876b 722 ListWalker = AllocateZeroPool(sizeof(HANDLE_LIST));\r
a405b86d 723 ASSERT(ListWalker != NULL);\r
724 ListWalker->TheHandle = TheHandle;\r
725 ListWalker->TheIndex = mHandleList.NextIndex++;\r
726 InsertTailList(&mHandleList.List.Link,&ListWalker->Link);\r
727 return (ListWalker->TheIndex);\r
728}\r
729\r
730\r
731\r
732/**\r
733 Function to retrieve the EFI_HANDLE from the human-friendly index.\r
734\r
735 @param[in] TheIndex The index to retrieve the EFI_HANDLE for.\r
736\r
737 @retval NULL The index was invalid.\r
738 @return The EFI_HANDLE that index represents.\r
739\r
740**/\r
741EFI_HANDLE\r
742EFIAPI\r
743ConvertHandleIndexToHandle(\r
744 IN CONST UINTN TheIndex\r
745 )\r
746{\r
747 HANDLE_LIST *ListWalker;\r
748\r
749 InternalShellInitHandleList();\r
750\r
751 if (TheIndex >= mHandleList.NextIndex) {\r
752 return (NULL);\r
753 }\r
754\r
755 for (ListWalker = (HANDLE_LIST*)GetFirstNode(&mHandleList.List.Link)\r
756 ; !IsNull(&mHandleList.List.Link,&ListWalker->Link)\r
757 ; ListWalker = (HANDLE_LIST*)GetNextNode(&mHandleList.List.Link,&ListWalker->Link)\r
758 ){\r
759 if (ListWalker->TheIndex == TheIndex) {\r
760 return (ListWalker->TheHandle);\r
761 }\r
762 }\r
763 return (NULL);\r
764}\r
765\r
766/**\r
767 Gets all the related EFI_HANDLEs based on the mask supplied.\r
768\r
769 This function scans all EFI_HANDLES in the UEFI environment's handle database\r
770 and returns the ones with the specified relationship (Mask) to the specified\r
771 controller handle.\r
772\r
773 If both DriverBindingHandle and ControllerHandle are NULL, then ASSERT.\r
774 If MatchingHandleCount is NULL, then ASSERT.\r
775\r
776 If MatchingHandleBuffer is not NULL upon a successful return the memory must be\r
777 caller freed.\r
778\r
779 @param[in] DriverBindingHandle The handle with Driver Binding protocol on it.\r
780 @param[in] ControllerHandle The handle with Device Path protocol on it.\r
781 @param[in] MatchingHandleCount The pointer to UINTN that specifies the number of HANDLES in\r
782 MatchingHandleBuffer.\r
783 @param[out] MatchingHandleBuffer On a successful return, a buffer of MatchingHandleCount\r
784 EFI_HANDLEs with a terminating NULL EFI_HANDLE.\r
785 @param[out] HandleType An array of type information.\r
786\r
787 @retval EFI_SUCCESS The operation was successful, and any related handles\r
788 are in MatchingHandleBuffer.\r
789 @retval EFI_NOT_FOUND No matching handles were found.\r
790 @retval EFI_INVALID_PARAMETER A parameter was invalid or out of range.\r
791**/\r
792EFI_STATUS\r
793EFIAPI\r
794ParseHandleDatabaseByRelationshipWithType (\r
795 IN CONST EFI_HANDLE DriverBindingHandle OPTIONAL,\r
796 IN CONST EFI_HANDLE ControllerHandle OPTIONAL,\r
797 IN UINTN *HandleCount,\r
798 OUT EFI_HANDLE **HandleBuffer,\r
799 OUT UINTN **HandleType\r
800 )\r
801{\r
802 EFI_STATUS Status;\r
803 UINTN HandleIndex;\r
804 EFI_GUID **ProtocolGuidArray;\r
805 UINTN ArrayCount;\r
806 UINTN ProtocolIndex;\r
807 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;\r
808 UINTN OpenInfoCount;\r
809 UINTN OpenInfoIndex;\r
810 UINTN ChildIndex;\r
811\r
812 ASSERT(HandleCount != NULL);\r
813 ASSERT(HandleBuffer != NULL);\r
814 ASSERT(HandleType != NULL);\r
815 ASSERT(DriverBindingHandle != NULL || ControllerHandle != NULL);\r
816\r
817 *HandleCount = 0;\r
818 *HandleBuffer = NULL;\r
819 *HandleType = NULL;\r
820\r
821 //\r
822 // Retrieve the list of all handles from the handle database\r
823 //\r
824 Status = gBS->LocateHandleBuffer (\r
825 AllHandles,\r
826 NULL,\r
827 NULL,\r
828 HandleCount,\r
829 HandleBuffer\r
830 );\r
831 if (EFI_ERROR (Status)) {\r
832 return (Status);\r
833 }\r
834\r
835 *HandleType = AllocateZeroPool (*HandleCount * sizeof (UINTN));\r
836 ASSERT(*HandleType != NULL);\r
837\r
838 for (HandleIndex = 0; HandleIndex < *HandleCount; HandleIndex++) {\r
839 //\r
840 // Retrieve the list of all the protocols on each handle\r
841 //\r
842 Status = gBS->ProtocolsPerHandle (\r
843 (*HandleBuffer)[HandleIndex],\r
844 &ProtocolGuidArray,\r
845 &ArrayCount\r
846 );\r
847 if (!EFI_ERROR (Status)) {\r
848\r
849 for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {\r
850\r
851 //\r
852 // Set the bit describing what this handle has\r
853 //\r
854 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiLoadedImageProtocolGuid) ) {\r
855 (*HandleType)[HandleIndex] |= HR_IMAGE_HANDLE;\r
856 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverBindingProtocolGuid) ) {\r
857 (*HandleType)[HandleIndex] |= HR_DRIVER_BINDING_HANDLE;\r
858 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverConfiguration2ProtocolGuid)) {\r
859 (*HandleType)[HandleIndex] |= HR_DRIVER_CONFIGURATION_HANDLE;\r
860 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverConfigurationProtocolGuid) ) {\r
861 (*HandleType)[HandleIndex] |= HR_DRIVER_CONFIGURATION_HANDLE;\r
862 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverDiagnostics2ProtocolGuid) ) {\r
863 (*HandleType)[HandleIndex] |= HR_DRIVER_DIAGNOSTICS_HANDLE;\r
864 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverDiagnosticsProtocolGuid) ) {\r
865 (*HandleType)[HandleIndex] |= HR_DRIVER_DIAGNOSTICS_HANDLE;\r
866 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiComponentName2ProtocolGuid) ) {\r
867 (*HandleType)[HandleIndex] |= HR_COMPONENT_NAME_HANDLE;\r
868 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiComponentNameProtocolGuid) ) {\r
869 (*HandleType)[HandleIndex] |= HR_COMPONENT_NAME_HANDLE;\r
870 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDevicePathProtocolGuid) ) {\r
871 (*HandleType)[HandleIndex] |= HR_DEVICE_HANDLE;\r
872 } else {\r
873 DEBUG_CODE_BEGIN();\r
874 ASSERT((*HandleType)[HandleIndex] == (*HandleType)[HandleIndex]);\r
875 DEBUG_CODE_END();\r
876 }\r
877 //\r
878 // Retrieve the list of agents that have opened each protocol\r
879 //\r
880 Status = gBS->OpenProtocolInformation (\r
881 (*HandleBuffer)[HandleIndex],\r
882 ProtocolGuidArray[ProtocolIndex],\r
883 &OpenInfo,\r
884 &OpenInfoCount\r
885 );\r
886 if (!EFI_ERROR (Status)) {\r
887 for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {\r
888 if (DriverBindingHandle != NULL && OpenInfo[OpenInfoIndex].AgentHandle == DriverBindingHandle) {\r
889 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) == EFI_OPEN_PROTOCOL_BY_DRIVER) {\r
890 (*HandleType)[HandleIndex] |= (HR_DEVICE_HANDLE | HR_CONTROLLER_HANDLE);\r
891 }\r
892 if (ControllerHandle != NULL && (*HandleBuffer)[HandleIndex] == ControllerHandle) {\r
893 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {\r
894 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {\r
895 if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].ControllerHandle) {\r
896 (*HandleType)[ChildIndex] |= (HR_DEVICE_HANDLE | HR_CHILD_HANDLE);\r
897 }\r
898 }\r
899 }\r
900 }\r
901 }\r
902 if (DriverBindingHandle == NULL && OpenInfo[OpenInfoIndex].ControllerHandle == ControllerHandle) {\r
903 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) == EFI_OPEN_PROTOCOL_BY_DRIVER) {\r
904 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {\r
905 if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].AgentHandle) {\r
906 (*HandleType)[ChildIndex] |= HR_DEVICE_DRIVER;\r
907 }\r
908 }\r
909 }\r
910 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {\r
911 (*HandleType)[HandleIndex] |= HR_PARENT_HANDLE;\r
912 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {\r
913 if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].AgentHandle) {\r
914 (*HandleType)[ChildIndex] |= HR_BUS_DRIVER;\r
915 }\r
916 }\r
917 }\r
918 }\r
919 }\r
920\r
921 FreePool (OpenInfo);\r
922 }\r
923 }\r
924\r
925 FreePool (ProtocolGuidArray);\r
926 }\r
927 }\r
928\r
929 if (EFI_ERROR(Status)) {\r
930 if (*HandleType != NULL) {\r
931 FreePool (*HandleType);\r
932 }\r
933 if (*HandleBuffer != NULL) {\r
934 FreePool (*HandleBuffer);\r
935 }\r
936\r
937 *HandleCount = 0;\r
938 *HandleBuffer = NULL;\r
939 *HandleType = NULL;\r
940 }\r
941\r
942 return Status;\r
943}\r
944\r
945/**\r
946 Gets all the related EFI_HANDLEs based on the single EFI_HANDLE and the mask\r
947 supplied.\r
948\r
949 This function will scan all EFI_HANDLES in the UEFI environment's handle database\r
950 and return all the ones with the specified relationship (Mask) to the specified\r
951 controller handle.\r
952\r
953 If both DriverBindingHandle and ControllerHandle are NULL, then ASSERT.\r
954 If MatchingHandleCount is NULL, then ASSERT.\r
955\r
956 If MatchingHandleBuffer is not NULL upon a sucessful return the memory must be\r
957 caller freed.\r
958\r
959 @param[in] DriverBindingHandle Handle to a object with Driver Binding protocol\r
960 on it.\r
961 @param[in] ControllerHandle Handle to a device with Device Path protocol on it.\r
962 @param[in] Mask Mask of what relationship(s) is desired.\r
963 @param[in] MatchingHandleCount Poitner to UINTN specifying number of HANDLES in\r
964 MatchingHandleBuffer.\r
965 @param[out] MatchingHandleBuffer On a sucessful return a buffer of MatchingHandleCount\r
966 EFI_HANDLEs and a terminating NULL EFI_HANDLE.\r
967\r
968 @retval EFI_SUCCESS The operation was sucessful and any related handles\r
969 are in MatchingHandleBuffer;\r
970 @retval EFI_NOT_FOUND No matching handles were found.\r
971 @retval EFI_INVALID_PARAMETER A parameter was invalid or out of range.\r
972**/\r
973EFI_STATUS\r
974EFIAPI\r
975ParseHandleDatabaseByRelationship (\r
976 IN CONST EFI_HANDLE DriverBindingHandle OPTIONAL,\r
977 IN CONST EFI_HANDLE ControllerHandle OPTIONAL,\r
978 IN CONST UINTN Mask,\r
979 IN UINTN *MatchingHandleCount,\r
980 OUT EFI_HANDLE **MatchingHandleBuffer OPTIONAL\r
981 )\r
982{\r
983 EFI_STATUS Status;\r
984 UINTN HandleCount;\r
985 EFI_HANDLE *HandleBuffer;\r
986 UINTN *HandleType;\r
987 UINTN HandleIndex;\r
988\r
989 ASSERT(MatchingHandleCount != NULL);\r
990 ASSERT(DriverBindingHandle != NULL || ControllerHandle != NULL);\r
991\r
992 if ((Mask & HR_VALID_MASK) != Mask) {\r
993 return (EFI_INVALID_PARAMETER);\r
994 }\r
995\r
996 if ((Mask & HR_CHILD_HANDLE) != 0 && DriverBindingHandle == NULL) {\r
997 return (EFI_INVALID_PARAMETER);\r
998 }\r
999\r
1000 *MatchingHandleCount = 0;\r
1001 if (MatchingHandleBuffer != NULL) {\r
1002 *MatchingHandleBuffer = NULL;\r
1003 }\r
1004\r
1005 HandleBuffer = NULL;\r
1006 HandleType = NULL;\r
1007\r
1008 Status = ParseHandleDatabaseByRelationshipWithType (\r
1009 DriverBindingHandle,\r
1010 ControllerHandle,\r
1011 &HandleCount,\r
1012 &HandleBuffer,\r
1013 &HandleType\r
1014 );\r
1015 if (!EFI_ERROR (Status)) {\r
1016 //\r
1017 // Count the number of handles that match the attributes in Mask\r
1018 //\r
1019 for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {\r
1020 if ((HandleType[HandleIndex] & Mask) == Mask) {\r
1021 (*MatchingHandleCount)++;\r
1022 }\r
1023 }\r
1024 //\r
1025 // If no handles match the attributes in Mask then return EFI_NOT_FOUND\r
1026 //\r
1027 if (*MatchingHandleCount == 0) {\r
1028 Status = EFI_NOT_FOUND;\r
1029 } else {\r
1030\r
1031 if (MatchingHandleBuffer == NULL) {\r
1032 //\r
1033 // Someone just wanted the count...\r
1034 //\r
1035 Status = EFI_SUCCESS;\r
1036 } else {\r
1037 //\r
1038 // Allocate a handle buffer for the number of handles that matched the attributes in Mask\r
1039 //\r
78ed876b 1040 *MatchingHandleBuffer = AllocateZeroPool ((*MatchingHandleCount +1)* sizeof (EFI_HANDLE));\r
a405b86d 1041 ASSERT(*MatchingHandleBuffer != NULL);\r
1042\r
1043 for (HandleIndex = 0,*MatchingHandleCount = 0\r
1044 ; HandleIndex < HandleCount\r
1045 ; HandleIndex++\r
1046 ){\r
1047 //\r
1048 // Fill the allocated buffer with the handles that matched the attributes in Mask\r
1049 //\r
1050 if ((HandleType[HandleIndex] & Mask) == Mask) {\r
1051 (*MatchingHandleBuffer)[(*MatchingHandleCount)++] = HandleBuffer[HandleIndex];\r
1052 }\r
1053 }\r
1054\r
1055 //\r
1056 // Make the last one NULL\r
1057 //\r
1058 (*MatchingHandleBuffer)[*MatchingHandleCount] = NULL;\r
1059\r
1060 Status = EFI_SUCCESS;\r
1061 } // MacthingHandleBuffer == NULL (ELSE)\r
1062 } // *MatchingHandleCount == 0 (ELSE)\r
1063 } // no error on ParseHandleDatabaseByRelationshipWithType\r
1064\r
1065 if (HandleBuffer != NULL) {\r
1066 FreePool (HandleBuffer);\r
1067 }\r
1068\r
1069 if (HandleType != NULL) {\r
1070 FreePool (HandleType);\r
1071 }\r
1072\r
1073 return Status;\r
1074}\r
1075\r
1076/**\r
1077 Gets handles for any child controllers of the passed in controller.\r
1078\r
1079 @param[in] ControllerHandle The handle of the "parent controller"\r
1080 @param[in] MatchingHandleCount Pointer to the number of handles in\r
1081 MatchingHandleBuffer on return.\r
1082 @param[out] MatchingHandleBuffer Buffer containing handles on a successful\r
1083 return.\r
1084\r
1085\r
1086 @retval EFI_SUCCESS The operation was sucessful.\r
1087**/\r
1088EFI_STATUS\r
1089EFIAPI\r
1090ParseHandleDatabaseForChildControllers(\r
1091 IN CONST EFI_HANDLE ControllerHandle,\r
1092 IN UINTN *MatchingHandleCount,\r
1093 OUT EFI_HANDLE **MatchingHandleBuffer OPTIONAL\r
1094 )\r
1095{\r
1096 EFI_STATUS Status;\r
78ed876b 1097// UINTN HandleIndex;\r
a405b86d 1098 UINTN DriverBindingHandleCount;\r
1099 EFI_HANDLE *DriverBindingHandleBuffer;\r
1100 UINTN DriverBindingHandleIndex;\r
1101 UINTN ChildControllerHandleCount;\r
1102 EFI_HANDLE *ChildControllerHandleBuffer;\r
1103 UINTN ChildControllerHandleIndex;\r
78ed876b 1104// BOOLEAN Found;\r
a405b86d 1105 EFI_HANDLE *HandleBufferForReturn;\r
1106\r
ff51746b 1107 if (MatchingHandleCount == NULL) {\r
1108 return (EFI_INVALID_PARAMETER);\r
1109 }\r
64d753f1 1110 *MatchingHandleCount = 0;\r
a405b86d 1111\r
1112 Status = PARSE_HANDLE_DATABASE_UEFI_DRIVERS (\r
1113 ControllerHandle,\r
1114 &DriverBindingHandleCount,\r
1115 &DriverBindingHandleBuffer\r
1116 );\r
1117 if (EFI_ERROR (Status)) {\r
1118 return Status;\r
1119 }\r
1120\r
ff51746b 1121 //\r
1122 // Get a buffer big enough for all the controllers.\r
1123 //\r
1124 HandleBufferForReturn = GetHandleListByProtocol(&gEfiDevicePathProtocolGuid);\r
a405b86d 1125 if (HandleBufferForReturn == NULL) {\r
1126 FreePool (DriverBindingHandleBuffer);\r
ff51746b 1127 return (EFI_NOT_FOUND);\r
a405b86d 1128 }\r
1129\r
a405b86d 1130 for (DriverBindingHandleIndex = 0; DriverBindingHandleIndex < DriverBindingHandleCount; DriverBindingHandleIndex++) {\r
1131 Status = PARSE_HANDLE_DATABASE_MANAGED_CHILDREN (\r
1132 DriverBindingHandleBuffer[DriverBindingHandleIndex],\r
1133 ControllerHandle,\r
1134 &ChildControllerHandleCount,\r
1135 &ChildControllerHandleBuffer\r
1136 );\r
1137 if (EFI_ERROR (Status)) {\r
1138 continue;\r
1139 }\r
1140\r
1141 for (ChildControllerHandleIndex = 0;\r
1142 ChildControllerHandleIndex < ChildControllerHandleCount;\r
1143 ChildControllerHandleIndex++\r
1144 ) {\r
78ed876b 1145// Found = FALSE;\r
1146 HandleBufferForReturn[(*MatchingHandleCount)++] = ChildControllerHandleBuffer[ChildControllerHandleIndex];\r
1147// for (HandleIndex = 0; HandleBufferForReturn[HandleIndex] != NULL; HandleIndex++) {\r
1148// if (HandleBufferForReturn[HandleIndex] == ChildControllerHandleBuffer[ChildControllerHandleIndex]) {\r
1149// Found = TRUE;\r
1150// break;\r
1151// }\r
1152// }\r
1153\r
1154// if (Found) {\r
1155// HandleBufferForReturn[(*MatchingHandleCount)++] = ChildControllerHandleBuffer[ChildControllerHandleIndex];\r
1156// }\r
a405b86d 1157 }\r
1158\r
1159 FreePool (ChildControllerHandleBuffer);\r
1160 }\r
1161\r
1162 FreePool (DriverBindingHandleBuffer);\r
1163\r
1164 if (MatchingHandleBuffer != NULL) {\r
1165 *MatchingHandleBuffer = HandleBufferForReturn;\r
1166 } else {\r
1167 FreePool(HandleBufferForReturn);\r
1168 }\r
1169\r
1170 return (EFI_SUCCESS);\r
1171}\r
1172\r
1173/**\r
1174 Appends 1 buffer to another buffer. This will re-allocate the destination buffer\r
1175 if necessary to fit all of the data.\r
1176\r
1177 If DestinationBuffer is NULL, then ASSERT().\r
1178\r
4ff7e37b
ED
1179 @param[in, out] DestinationBuffer The pointer to the pointer to the buffer to append onto.\r
1180 @param[in, out] DestinationSize The pointer to the size of DestinationBuffer.\r
1181 @param[in] SourceBuffer The pointer to the buffer to append onto DestinationBuffer.\r
1182 @param[in] SourceSize The number of bytes of SourceBuffer to append.\r
a405b86d 1183\r
1184 @retval NULL A memory allocation failed.\r
1185 @retval NULL A parameter was invalid.\r
1186 @return A pointer to (*DestinationBuffer).\r
1187**/\r
1188VOID*\r
1189EFIAPI\r
1190BuffernCatGrow (\r
1191 IN OUT VOID **DestinationBuffer,\r
1192 IN OUT UINTN *DestinationSize,\r
1193 IN VOID *SourceBuffer,\r
1194 IN UINTN SourceSize\r
1195 )\r
1196{\r
1197 UINTN LocalDestinationSize;\r
1198 UINTN LocalDestinationFinalSize;\r
1199\r
1200 ASSERT(DestinationBuffer != NULL);\r
1201\r
1202 if (SourceSize == 0 || SourceBuffer == NULL) {\r
1203 return (*DestinationBuffer);\r
1204 }\r
1205\r
1206 if (DestinationSize == NULL) {\r
1207 LocalDestinationSize = 0;\r
1208 } else {\r
1209 LocalDestinationSize = *DestinationSize;\r
1210 }\r
1211\r
1212 LocalDestinationFinalSize = LocalDestinationSize + SourceSize;\r
1213\r
1214 if (DestinationSize != NULL) {\r
1215 *DestinationSize = LocalDestinationSize;\r
1216 }\r
1217\r
1218 if (LocalDestinationSize == 0) {\r
1219 // allcoate\r
78ed876b 1220 *DestinationBuffer = AllocateZeroPool(LocalDestinationFinalSize);\r
a405b86d 1221 } else {\r
1222 // reallocate\r
1223 *DestinationBuffer = ReallocatePool(LocalDestinationSize, LocalDestinationFinalSize, *DestinationBuffer);\r
1224 }\r
1225\r
1226 ASSERT(*DestinationBuffer != NULL);\r
1227\r
1228 // copy\r
1229 return (CopyMem(((UINT8*)(*DestinationBuffer)) + LocalDestinationSize, SourceBuffer, SourceSize));\r
1230}\r
1231\r
1232/**\r
1233 Gets handles for any child devices produced by the passed in driver.\r
1234\r
1235 @param[in] DriverHandle The handle of the driver.\r
1236 @param[in] MatchingHandleCount Pointer to the number of handles in\r
1237 MatchingHandleBuffer on return.\r
1238 @param[out] MatchingHandleBuffer Buffer containing handles on a successful\r
1239 return.\r
1240 @retval EFI_SUCCESS The operation was sucessful.\r
1241 @sa ParseHandleDatabaseByRelationship\r
1242**/\r
1243EFI_STATUS\r
1244EFIAPI\r
1245ParseHandleDatabaseForChildDevices(\r
1246 IN CONST EFI_HANDLE DriverHandle,\r
1247 IN UINTN *MatchingHandleCount,\r
1248 OUT EFI_HANDLE **MatchingHandleBuffer OPTIONAL\r
1249 )\r
1250{\r
1251 EFI_HANDLE *Buffer;\r
1252 EFI_HANDLE *Buffer2;\r
1253 UINTN Count1;\r
1254 UINTN Count2;\r
1255 UINTN HandleIndex;\r
1256 EFI_STATUS Status;\r
1257 UINTN HandleBufferSize;\r
1258\r
1259 ASSERT(MatchingHandleCount != NULL);\r
1260\r
1261 HandleBufferSize = 0;\r
1262 Buffer = NULL;\r
1263 Buffer2 = NULL;\r
1264 *MatchingHandleCount = 0;\r
1265\r
1266 Status = PARSE_HANDLE_DATABASE_DEVICES (\r
1267 DriverHandle,\r
1268 &Count1,\r
1269 &Buffer\r
1270 );\r
1271 if (!EFI_ERROR (Status)) {\r
1272 for (HandleIndex = 0; HandleIndex < Count1; HandleIndex++) {\r
1273 //\r
1274 // now find the children\r
1275 //\r
1276 Status = PARSE_HANDLE_DATABASE_MANAGED_CHILDREN (\r
1277 DriverHandle,\r
1278 Buffer[HandleIndex],\r
1279 &Count2,\r
1280 &Buffer2\r
1281 );\r
1282 if (EFI_ERROR(Status)) {\r
1283 break;\r
1284 }\r
1285 //\r
1286 // save out required and optional data elements\r
1287 //\r
1288 *MatchingHandleCount += Count2;\r
1289 if (MatchingHandleBuffer != NULL) {\r
1290 *MatchingHandleBuffer = BuffernCatGrow((VOID**)MatchingHandleBuffer, &HandleBufferSize, Buffer2, Count2 * sizeof(Buffer2[0]));\r
1291 }\r
1292\r
1293 //\r
1294 // free the memory\r
1295 //\r
1296 if (Buffer2 != NULL) {\r
1297 FreePool(Buffer2);\r
1298 }\r
1299 }\r
1300 }\r
1301\r
1302 if (Buffer != NULL) {\r
1303 FreePool(Buffer);\r
1304 }\r
1305 return (Status);\r
1306}\r
1307\r
1308/**\r
1309 Function to get all handles that support a given protocol or all handles.\r
1310\r
1311 @param[in] ProtocolGuid The guid of the protocol to get handles for. If NULL\r
1312 then the function will return all handles.\r
1313\r
1314 @retval NULL A memory allocation failed.\r
1315 @return A NULL terminated list of handles.\r
1316**/\r
1317EFI_HANDLE*\r
1318EFIAPI\r
40d7a9cf 1319GetHandleListByProtocol (\r
a405b86d 1320 IN CONST EFI_GUID *ProtocolGuid OPTIONAL\r
1321 )\r
1322{\r
1323 EFI_HANDLE *HandleList;\r
1324 UINTN Size;\r
1325 EFI_STATUS Status;\r
1326\r
1327 Size = 0;\r
1328 HandleList = NULL;\r
1329\r
1330 //\r
1331 // We cannot use LocateHandleBuffer since we need that NULL item on the ends of the list!\r
1332 //\r
1333 if (ProtocolGuid == NULL) {\r
1334 Status = gBS->LocateHandle(AllHandles, NULL, NULL, &Size, HandleList);\r
1335 if (Status == EFI_BUFFER_TOO_SMALL) {\r
78ed876b 1336 HandleList = AllocateZeroPool(Size + sizeof(EFI_HANDLE));\r
40d7a9cf 1337 if (HandleList == NULL) {\r
1338 return (NULL);\r
1339 }\r
a405b86d 1340 Status = gBS->LocateHandle(AllHandles, NULL, NULL, &Size, HandleList);\r
1341 HandleList[Size/sizeof(EFI_HANDLE)] = NULL;\r
1342 }\r
1343 } else {\r
1344 Status = gBS->LocateHandle(ByProtocol, (EFI_GUID*)ProtocolGuid, NULL, &Size, HandleList);\r
1345 if (Status == EFI_BUFFER_TOO_SMALL) {\r
78ed876b 1346 HandleList = AllocateZeroPool(Size + sizeof(EFI_HANDLE));\r
40d7a9cf 1347 if (HandleList == NULL) {\r
1348 return (NULL);\r
1349 }\r
a405b86d 1350 Status = gBS->LocateHandle(ByProtocol, (EFI_GUID*)ProtocolGuid, NULL, &Size, HandleList);\r
1351 HandleList[Size/sizeof(EFI_HANDLE)] = NULL;\r
1352 }\r
1353 }\r
1354 if (EFI_ERROR(Status)) {\r
1355 if (HandleList != NULL) {\r
1356 FreePool(HandleList);\r
1357 }\r
1358 return (NULL);\r
1359 }\r
1360 return (HandleList);\r
1361}\r
1362\r
1363/**\r
1364 Function to get all handles that support some protocols.\r
1365\r
1366 @param[in] ProtocolGuids A NULL terminated list of protocol GUIDs.\r
1367\r
1368 @retval NULL A memory allocation failed.\r
ff51746b 1369 @retval NULL ProtocolGuids was NULL.\r
1370 @return A NULL terminated list of EFI_HANDLEs.\r
a405b86d 1371**/\r
1372EFI_HANDLE*\r
1373EFIAPI\r
40d7a9cf 1374GetHandleListByProtocolList (\r
a405b86d 1375 IN CONST EFI_GUID **ProtocolGuids\r
1376 )\r
1377{\r
1378 EFI_HANDLE *HandleList;\r
1379 UINTN Size;\r
1380 UINTN TotalSize;\r
40d7a9cf 1381 UINTN TempSize;\r
a405b86d 1382 EFI_STATUS Status;\r
1383 CONST EFI_GUID **GuidWalker;\r
1384 EFI_HANDLE *HandleWalker1;\r
1385 EFI_HANDLE *HandleWalker2;\r
1386\r
1387 Size = 0;\r
1388 HandleList = NULL;\r
1389 TotalSize = sizeof(EFI_HANDLE);\r
1390\r
1391 for (GuidWalker = ProtocolGuids ; GuidWalker != NULL && *GuidWalker != NULL ; GuidWalker++,Size = 0){\r
1392 Status = gBS->LocateHandle(ByProtocol, (EFI_GUID*)(*GuidWalker), NULL, &Size, NULL);\r
1393 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1394 TotalSize += Size;\r
1395 }\r
1396 }\r
40d7a9cf 1397\r
1398 //\r
1399 // No handles were found... \r
1400 //\r
1401 if (TotalSize == sizeof(EFI_HANDLE)) {\r
1402 return (NULL);\r
1403 }\r
1404\r
1405 HandleList = AllocateZeroPool(TotalSize);\r
a405b86d 1406 if (HandleList == NULL) {\r
1407 return (NULL);\r
1408 }\r
1409\r
1410 Size = 0;\r
1411 for (GuidWalker = ProtocolGuids ; GuidWalker != NULL && *GuidWalker != NULL ; GuidWalker++){\r
40d7a9cf 1412 TempSize = TotalSize - Size;\r
ff51746b 1413 Status = gBS->LocateHandle(ByProtocol, (EFI_GUID*)(*GuidWalker), NULL, &TempSize, HandleList+(Size/sizeof(EFI_HANDLE)));\r
40d7a9cf 1414\r
1415 //\r
1416 // Allow for missing protocols... Only update the 'used' size upon success.\r
1417 //\r
1418 if (!EFI_ERROR(Status)) {\r
ff51746b 1419 Size += TempSize;\r
40d7a9cf 1420 }\r
a405b86d 1421 }\r
ff51746b 1422 ASSERT(HandleList[(TotalSize/sizeof(EFI_HANDLE))-1] == NULL);\r
a405b86d 1423\r
1424 for (HandleWalker1 = HandleList ; HandleWalker1 != NULL && *HandleWalker1 != NULL ; HandleWalker1++) {\r
1425 for (HandleWalker2 = HandleWalker1 + 1; HandleWalker2 != NULL && *HandleWalker2 != NULL ; HandleWalker2++) {\r
1426 if (*HandleWalker1 == *HandleWalker2) {\r
1427 //\r
1428 // copy memory back 1 handle width.\r
1429 //\r
1430 CopyMem(HandleWalker2, HandleWalker2 + 1, TotalSize - ((HandleWalker2-HandleList+1)*sizeof(EFI_HANDLE)));\r
1431 }\r
1432 }\r
1433 }\r
1434\r
1435 return (HandleList);\r
1436}\r
1437\r
1438\r
1439\r
1440\r
1441\r
1442\r
1443\r
1444\r
1445\r
1446\r