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