]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Ebl/EfiDevice.c
Fix svn check in bug that broke CWD. Update the connect disconnect support in device...
[mirror_edk2.git] / EmbeddedPkg / Ebl / EfiDevice.c
CommitLineData
2ef2b01e
A
1/** @file\r
2 EBL commands for EFI and PI Devices\r
3\r
60274cca
HT
4 Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>\r
5 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
2ef2b01e 6\r
60274cca 7 This program and the accompanying materials\r
2ef2b01e
A
8 are licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include "Ebl.h"\r
18\r
19\r
20EFI_DXE_SERVICES *gDS = NULL;\r
21\r
22\r
23/**\r
24 Print information about the File System device.\r
25\r
26 @param File Open File for the device\r
27\r
28**/\r
29VOID\r
30EblPrintFsInfo (\r
31 IN EFI_OPEN_FILE *File\r
32 )\r
33{\r
638909f1 34 CHAR16 *Str;\r
35\r
2ef2b01e
A
36 if (File == NULL) {\r
37 return;\r
38 }\r
39\r
40 AsciiPrint (" %a: ", File->DeviceName);\r
41 if (File->FsInfo != NULL) {\r
638909f1 42 for (Str = File->FsInfo->VolumeLabel; *Str != '\0'; Str++) {\r
43 if (*Str == ' ') {\r
44 // UI makes you enter _ for space, so make the printout match that\r
45 *Str = '_';\r
46 }\r
47 AsciiPrint ("%c", *Str);\r
48 }\r
49 AsciiPrint (":");\r
2ef2b01e
A
50 if (File->FsInfo->ReadOnly) {\r
51 AsciiPrint ("ReadOnly");\r
52 }\r
53 }\r
54\r
55 AsciiPrint ("\n");\r
56 EfiClose (File);\r
57}\r
58\r
59\r
60/**\r
61 Print information about the FV devices.\r
62\r
63 @param File Open File for the device\r
64\r
65**/\r
66VOID\r
67EblPrintFvbInfo (\r
68 IN EFI_OPEN_FILE *File\r
69 )\r
70{\r
71 if (File == NULL) {\r
72 return;\r
73 }\r
74\r
75 AsciiPrint (" %a: 0x%08lx - 0x%08lx : 0x%08x\n", File->DeviceName, File->FvStart, File->FvStart + File->FvSize - 1, File->FvSize);\r
76 EfiClose (File);\r
77}\r
78\r
79\r
80/**\r
81 Print information about the Blk IO devices.\r
82 If the device supports PXE dump out extra information\r
83\r
84 @param File Open File for the device\r
85\r
86**/\r
87VOID\r
88EblPrintBlkIoInfo (\r
89 IN EFI_OPEN_FILE *File\r
90 )\r
91{\r
92 UINT64 DeviceSize;\r
6f72e28d 93 UINTN Index;\r
94 UINTN Max;\r
95 EFI_OPEN_FILE *FsFile;\r
2ef2b01e
A
96\r
97 if (File == NULL) {\r
98 return;\r
99 }\r
100\r
101 AsciiPrint (" %a: ", File->DeviceName);\r
6f72e28d 102\r
103 // print out name of file system, if any, on this block device\r
104 Max = EfiGetDeviceCounts (EfiOpenFileSystem);\r
105 if (Max != 0) {\r
106 for (Index = 0; Index < Max; Index++) {\r
107 FsFile = EfiDeviceOpenByType (EfiOpenFileSystem, Index);\r
108 if (FsFile != NULL) {\r
109 if (FsFile->EfiHandle == File->EfiHandle) {\r
110 AsciiPrint ("fs%d: ", Index);\r
111 EfiClose (FsFile);\r
112 break;\r
113 }\r
0a0951ea 114 EfiClose (FsFile);\r
6f72e28d 115 }\r
6f72e28d 116 }\r
117 }\r
118\r
119 // Print out useful Block IO media properties\r
0a0951ea 120 if (File->FsBlockIoMedia->RemovableMedia) {\r
2ef2b01e
A
121 AsciiPrint ("Removable ");\r
122 }\r
0a0951ea 123 if (!File->FsBlockIoMedia->MediaPresent) {\r
124 AsciiPrint ("No Media\n");\r
125 } else {\r
126 if (File->FsBlockIoMedia->LogicalPartition) {\r
127 AsciiPrint ("Partition ");\r
128 }\r
129 DeviceSize = MultU64x32 (File->FsBlockIoMedia->LastBlock + 1, File->FsBlockIoMedia->BlockSize);\r
130 AsciiPrint ("Size = 0x%lX\n", DeviceSize);\r
2ef2b01e 131 }\r
2ef2b01e
A
132 EfiClose (File);\r
133}\r
134\r
2ef2b01e
A
135 /**\r
136 Print information about the Load File devices.\r
137 If the device supports PXE dump out extra information\r
138\r
139 @param File Open File for the device\r
140\r
141**/\r
142VOID\r
143EblPrintLoadFileInfo (\r
144 IN EFI_OPEN_FILE *File\r
145 )\r
146{\r
147 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r
148 MAC_ADDR_DEVICE_PATH *MacAddr;\r
149 UINTN HwAddressSize;\r
150 UINTN Index;\r
151\r
152 if (File == NULL) {\r
153 return;\r
154 }\r
155\r
156 AsciiPrint (" %a: %a ", File->DeviceName, EblLoadFileBootTypeString (File->EfiHandle));\r
157\r
158 if (File->DevicePath != NULL) {\r
159 // Try to print out the MAC address\r
160 for (DevicePathNode = File->DevicePath; \r
161 !IsDevicePathEnd (DevicePathNode); \r
162 DevicePathNode = NextDevicePathNode (DevicePathNode)) {\r
163 \r
164 if ((DevicePathType (DevicePathNode) == MESSAGING_DEVICE_PATH) && (DevicePathSubType (DevicePathNode) == MSG_MAC_ADDR_DP)) {\r
165 MacAddr = (MAC_ADDR_DEVICE_PATH *)DevicePathNode;\r
166 \r
167 HwAddressSize = sizeof (EFI_MAC_ADDRESS);\r
168 if (MacAddr->IfType == 0x01 || MacAddr->IfType == 0x00) {\r
169 HwAddressSize = 6;\r
170 }\r
171 \r
172 AsciiPrint ("MAC ");\r
173 for (Index = 0; Index < HwAddressSize; Index++) {\r
174 AsciiPrint ("%02x", MacAddr->MacAddress.Addr[Index] & 0xff);\r
175 }\r
176 } \r
177 }\r
178 }\r
179\r
180 AsciiPrint ("\n");\r
181 EfiClose (File);\r
182 return;\r
183}\r
184\r
185\r
186\r
187/**\r
188 Dump information about devices in the system. \r
189\r
190 fv: PI Firmware Volume\r
191 fs: EFI Simple File System\r
192 blk: EFI Block IO\r
193 LoadFile: EFI Load File Protocol (commonly PXE network boot)\r
194\r
195 Argv[0] - "device"\r
196\r
197 @param Argc Number of command arguments in Argv\r
198 @param Argv Array of strings that represent the parsed command line. \r
199 Argv[0] is the comamnd name\r
200\r
201 @return EFI_SUCCESS\r
202\r
203**/\r
204EFI_STATUS\r
205EblDeviceCmd (\r
206 IN UINTN Argc,\r
207 IN CHAR8 **Argv\r
208 )\r
209{\r
208a8330 210 EFI_STATUS Status;\r
2ef2b01e
A
211 UINTN Index;\r
212 UINTN CurrentRow;\r
213 UINTN Max;\r
f6381f4c 214 EFI_OPEN_FILE *File;\r
2ef2b01e
A
215\r
216 CurrentRow = 0;\r
208a8330 217\r
2ef2b01e
A
218 // Need to call here to make sure Device Counts are valid\r
219 EblUpdateDeviceLists ();\r
220\r
f6381f4c 221 //\r
222 // Probe for media insertion/removal in removable media devices\r
223 //\r
224 Max = EfiGetDeviceCounts (EfiOpenBlockIo);\r
225 if (Max != 0) {\r
226 for (Index = 0; Index < Max; Index++) {\r
227 File = EfiDeviceOpenByType (EfiOpenBlockIo, Index);\r
228 if (File != NULL) {\r
229 if (File->FsBlockIoMedia->RemovableMedia) {\r
230 if (File->FsBlockIoMedia->MediaPresent) {\r
208a8330 231 // Probe to see if media is present\r
232 Status = File->FsBlockIo->ReadBlocks (File->FsBlockIo, File->FsBlockIo->Media->MediaId, (EFI_LBA)0, 0, NULL);\r
233 if (Status == EFI_NO_MEDIA) {\r
234 gBS->DisconnectController (File->EfiHandle, NULL, NULL);\r
235 }\r
236 } else {\r
237 // Probe for media insertion and connect partition and filesystem drivers if needed\r
238 gBS->ConnectController (File->EfiHandle, NULL, NULL, TRUE);\r
f6381f4c 239 }\r
f6381f4c 240 }\r
241 EfiClose (File);\r
242 }\r
243 }\r
244 }\r
245\r
246 // Now we can print out the info...\r
247\r
2ef2b01e
A
248 Max = EfiGetDeviceCounts (EfiOpenFirmwareVolume);\r
249 if (Max != 0) {\r
250 AsciiPrint ("Firmware Volume Devices:\n");\r
251 for (Index = 0; Index < Max; Index++) {\r
252 EblPrintFvbInfo (EfiDeviceOpenByType (EfiOpenFirmwareVolume, Index));\r
253 if (EblAnyKeyToContinueQtoQuit (&CurrentRow, TRUE)) {\r
254 break;\r
255 }\r
256 }\r
257 }\r
258\r
259 Max = EfiGetDeviceCounts (EfiOpenFileSystem);\r
260 if (Max != 0) {\r
261 AsciiPrint ("File System Devices:\n");\r
262 for (Index = 0; Index < Max; Index++) {\r
263 EblPrintFsInfo (EfiDeviceOpenByType (EfiOpenFileSystem, Index));\r
264 if (EblAnyKeyToContinueQtoQuit (&CurrentRow, TRUE)) {\r
265 break;\r
266 }\r
267 }\r
268 }\r
269\r
270 Max = EfiGetDeviceCounts (EfiOpenBlockIo);\r
271 if (Max != 0) {\r
272 AsciiPrint ("Block IO Devices:\n");\r
273 for (Index = 0; Index < Max; Index++) {\r
274 EblPrintBlkIoInfo (EfiDeviceOpenByType (EfiOpenBlockIo, Index));\r
275 if (EblAnyKeyToContinueQtoQuit (&CurrentRow, TRUE)) {\r
276 break;\r
277 }\r
278 }\r
279 }\r
280 \r
281 Max = EfiGetDeviceCounts (EfiOpenLoadFile);\r
282 if (Max != 0) {\r
283 AsciiPrint ("LoadFile Devices: (usually network)\n");\r
284 for (Index = 0; Index < Max; Index++) {\r
285 EblPrintLoadFileInfo (EfiDeviceOpenByType (EfiOpenLoadFile, Index));\r
286 if (EblAnyKeyToContinueQtoQuit (&CurrentRow, TRUE)) {\r
287 break;\r
288 }\r
289 }\r
290 }\r
291\r
292 return EFI_SUCCESS;\r
293}\r
294\r
295\r
296/**\r
297 Start an EFI image (PE32+ with EFI defined entry point). \r
298\r
299 Argv[0] - "start"\r
300 Argv[1] - device name and path\r
301 Argv[2] - "" string to pass into image being started\r
302\r
303 start fs1:\Temp\Fv.Fv "arg to pass" ; load an FV from the disk and pass the\r
304 ; ascii string arg to pass to the image\r
305 start fv0:\FV ; load an FV from an FV (not common)\r
306 start LoadFile0: ; load an FV via a PXE boot\r
307\r
308 @param Argc Number of command arguments in Argv\r
309 @param Argv Array of strings that represent the parsed command line. \r
310 Argv[0] is the comamnd name\r
311\r
312 @return EFI_SUCCESS\r
313\r
314**/\r
315EFI_STATUS\r
316EblStartCmd (\r
317 IN UINTN Argc,\r
318 IN CHAR8 **Argv\r
319 )\r
320{\r
321 EFI_STATUS Status;\r
322 EFI_OPEN_FILE *File;\r
323 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
324 EFI_HANDLE ImageHandle;\r
325 UINTN ExitDataSize;\r
326 CHAR16 *ExitData;\r
327 VOID *Buffer;\r
328 UINTN BufferSize;\r
329 EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r
330\r
331 ImageHandle = NULL;\r
332\r
333 if (Argc < 2) {\r
334 return EFI_INVALID_PARAMETER;\r
335 }\r
336\r
337 File = EfiOpen (Argv[1], EFI_FILE_MODE_READ, 0);\r
338 if (File == NULL) {\r
339 return EFI_INVALID_PARAMETER; \r
340 }\r
341\r
342 DevicePath = File->DevicePath;\r
343 if (DevicePath != NULL) {\r
344 // check for device path form: blk, fv, fs, and loadfile\r
345 Status = gBS->LoadImage (FALSE, gImageHandle, DevicePath, NULL, 0, &ImageHandle);\r
346 } else {\r
347 // Check for buffer form: A0x12345678:0x1234 syntax.\r
348 // Means load using buffer starting at 0x12345678 of size 0x1234.\r
349\r
350 Status = EfiReadAllocatePool (File, &Buffer, &BufferSize);\r
351 if (EFI_ERROR (Status)) {\r
352 EfiClose (File);\r
353 return Status;\r
354 }\r
355 Status = gBS->LoadImage (FALSE, gImageHandle, DevicePath, Buffer, BufferSize, &ImageHandle);\r
356\r
357 FreePool (Buffer);\r
358 }\r
359 \r
360 EfiClose (File);\r
361\r
362 if (!EFI_ERROR (Status)) {\r
363 if (Argc >= 3) {\r
364 // Argv[2] is a "" string that we pass directly to the EFI application without the ""\r
365 // We don't pass Argv[0] to the EFI Application (it's name) just the args\r
366 Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&ImageInfo);\r
367 ASSERT_EFI_ERROR (Status);\r
368 \r
369 ImageInfo->LoadOptionsSize = (UINT32)AsciiStrSize (Argv[2]);\r
370 ImageInfo->LoadOptions = AllocatePool (ImageInfo->LoadOptionsSize);\r
371 AsciiStrCpy (ImageInfo->LoadOptions, Argv[2]);\r
372 }\r
373\r
374 // Transfer control to the EFI image we loaded with LoadImage()\r
375 Status = gBS->StartImage (ImageHandle, &ExitDataSize, &ExitData);\r
376 }\r
377\r
378 return Status;\r
379}\r
380\r
381\r
382/**\r
383 Load a Firmware Volume (FV) into memory from a device. This causes drivers in\r
384 the FV to be dispatched if the dependancies of the drivers are met.\r
385 \r
386 Argv[0] - "loadfv"\r
387 Argv[1] - device name and path\r
388\r
389 loadfv fs1:\Temp\Fv.Fv ; load an FV from the disk\r
390 loadfv fv0:\FV ; load an FV from an FV (not common)\r
391 loadfv LoadFile0: ; load an FV via a PXE boot\r
392\r
393 @param Argc Number of command arguments in Argv\r
394 @param Argv Array of strings that represent the parsed command line. \r
395 Argv[0] is the comamnd name\r
396\r
397 @return EFI_SUCCESS\r
398\r
399**/\r
400EFI_STATUS\r
401EblLoadFvCmd (\r
402 IN UINTN Argc,\r
403 IN CHAR8 **Argv\r
404 )\r
405{\r
406 EFI_STATUS Status;\r
407 EFI_OPEN_FILE *File;\r
408 VOID *FvStart;\r
409 UINTN FvSize;\r
410 EFI_HANDLE FvHandle;\r
411\r
412\r
413 if (Argc < 2) {\r
414 return EFI_INVALID_PARAMETER;\r
415 }\r
416\r
417 File = EfiOpen (Argv[1], EFI_FILE_MODE_READ, 0);\r
418 if (File == NULL) {\r
419 return EFI_INVALID_PARAMETER; \r
420 }\r
421\r
422 if (File->Type == EfiOpenMemoryBuffer) {\r
423 // If it is a address just use it.\r
424 Status = gDS->ProcessFirmwareVolume (File->Buffer, File->Size, &FvHandle);\r
425 } else {\r
426 // If it is a file read it into memory and use it\r
427 Status = EfiReadAllocatePool (File, &FvStart, &FvSize);\r
428 EfiClose (File);\r
429 if (EFI_ERROR (Status)) {\r
430 return Status;\r
431 }\r
432 \r
433 Status = gDS->ProcessFirmwareVolume (FvStart, FvSize, &FvHandle);\r
434 FreePool (FvStart);\r
435 }\r
436 return Status;\r
437}\r
438\r
439\r
440/**\r
441 Perform an EFI connect to connect devices that follow the EFI driver model. \r
442 If it is a PI system also call the dispatcher in case a new FV was made\r
443 availible by one of the connect EFI drivers (this is not a common case).\r
444 \r
445 Argv[0] - "connect"\r
446\r
447 @param Argc Number of command arguments in Argv\r
448 @param Argv Array of strings that represent the parsed command line. \r
449 Argv[0] is the comamnd name\r
450\r
451 @return EFI_SUCCESS\r
452\r
453**/\r
454EFI_STATUS\r
455EblConnectCmd (\r
456 IN UINTN Argc,\r
457 IN CHAR8 **Argv\r
458 )\r
459{\r
460 EFI_STATUS Status;\r
461 UINTN HandleCount;\r
462 EFI_HANDLE *HandleBuffer;\r
463 UINTN Index;\r
464 BOOLEAN Dispatch;\r
465 EFI_OPEN_FILE *File;\r
466\r
467\r
468 if (Argc > 1) {\r
469 if ((*Argv[1] == 'd') || (*Argv[1] == 'D')) {\r
470 Status = gBS->LocateHandleBuffer (\r
471 AllHandles,\r
472 NULL,\r
473 NULL,\r
474 &HandleCount,\r
475 &HandleBuffer\r
476 );\r
477 if (EFI_ERROR (Status)) {\r
478 return Status;\r
479 }\r
480 \r
481 for (Index = 0; Index < HandleCount; Index++) {\r
482 gBS->DisconnectController (HandleBuffer[Index], NULL, NULL);\r
483 }\r
484 \r
485 //\r
486 // Given we disconnect our console we should go and do a connect now\r
487 //\r
488 } else {\r
489 File = EfiOpen (Argv[1], EFI_FILE_MODE_READ, 0);\r
490 if (File != NULL) {\r
491 AsciiPrint ("Connecting %a\n", Argv[1]);\r
492 gBS->ConnectController (File->EfiHandle, NULL, NULL, TRUE);\r
493 EfiClose (File);\r
494 return EFI_SUCCESS;\r
495 }\r
496 }\r
497 }\r
498\r
499 Dispatch = FALSE;\r
500 do {\r
501 Status = gBS->LocateHandleBuffer (\r
502 AllHandles,\r
503 NULL,\r
504 NULL,\r
505 &HandleCount,\r
506 &HandleBuffer\r
507 );\r
508 if (EFI_ERROR (Status)) {\r
509 return Status;\r
510 }\r
511\r
512 for (Index = 0; Index < HandleCount; Index++) {\r
513 gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);\r
514 }\r
515\r
516 FreePool (HandleBuffer);\r
517\r
518 //\r
519 // Check to see if it's possible to dispatch an more DXE drivers.\r
520 // The BdsLibConnectAllEfi () may have made new DXE drivers show up.\r
521 // If anything is Dispatched Status == EFI_SUCCESS and we will try\r
522 // the connect again.\r
523 //\r
524 if (gDS == NULL) {\r
525 Status = EFI_NOT_FOUND;\r
526 } else {\r
527 Status = gDS->Dispatch ();\r
528 if (!EFI_ERROR (Status)) {\r
529 Dispatch = TRUE;\r
530 }\r
531 }\r
532\r
533 } while (!EFI_ERROR (Status));\r
534\r
535 if (Dispatch) {\r
536 AsciiPrint ("Connected and dispatched\n");\r
537 } else {\r
538 AsciiPrint ("Connect\n");\r
539 }\r
540\r
541 return EFI_SUCCESS;\r
542}\r
543\r
544\r
545\r
546CHAR8 *gMemMapType[] = {\r
547 "reserved ",\r
548 "LoaderCode",\r
549 "LoaderData",\r
550 "BS_code ",\r
551 "BS_data ",\r
552 "RT_code ",\r
553 "RT_data ",\r
554 "available ",\r
555 "Unusable ",\r
556 "ACPI_recl ",\r
557 "ACPI_NVS ",\r
558 "MemMapIO ",\r
559 "MemPortIO ",\r
560 "PAL_code "\r
561};\r
562\r
563\r
564/**\r
565 Dump out the EFI memory map\r
566 \r
567 Argv[0] - "memmap"\r
568\r
569 @param Argc Number of command arguments in Argv\r
570 @param Argv Array of strings that represent the parsed command line. \r
571 Argv[0] is the comamnd name\r
572\r
573 @return EFI_SUCCESS\r
574\r
575**/\r
576EFI_STATUS\r
577EblMemMapCmd (\r
578 IN UINTN Argc,\r
579 IN CHAR8 **Argv\r
580 )\r
581{\r
582 EFI_STATUS Status;\r
583 EFI_MEMORY_DESCRIPTOR *MemMap;\r
584 EFI_MEMORY_DESCRIPTOR *OrigMemMap;\r
585 UINTN MemMapSize;\r
586 UINTN MapKey;\r
587 UINTN DescriptorSize;\r
588 UINT32 DescriptorVersion;\r
589 UINT64 PageCount[EfiMaxMemoryType];\r
590 UINTN Index;\r
591 UINT64 EntrySize;\r
592 UINTN CurrentRow;\r
593 UINT64 TotalMemory;\r
594\r
595 ZeroMem (PageCount, sizeof (PageCount));\r
596\r
597 AsciiPrint ("EFI Memory Map\n");\r
598 \r
599 // First call is to figure out how big the buffer needs to be\r
600 MemMapSize = 0;\r
601 MemMap = NULL;\r
602 Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize, &DescriptorVersion);\r
603 if (Status == EFI_BUFFER_TOO_SMALL) {\r
604 // In case the AllocatPool changes the memory map we added in some extra descriptors\r
605 MemMapSize += (DescriptorSize * 0x100);\r
606 OrigMemMap = MemMap = AllocatePool (MemMapSize);\r
607 if (OrigMemMap != NULL) {\r
608 // 2nd time we get the data\r
609 Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize, &DescriptorVersion);\r
610 if (!EFI_ERROR (Status)) {\r
611 for (Index = 0, CurrentRow = 0; Index < MemMapSize/DescriptorSize; Index++) {\r
612 EntrySize = LShiftU64 (MemMap->NumberOfPages, 12);\r
613 AsciiPrint ("\n%a %016lx - %016lx: # %08lx %016lx", gMemMapType[MemMap->Type % EfiMaxMemoryType], MemMap->PhysicalStart, MemMap->PhysicalStart + EntrySize -1, MemMap->NumberOfPages, MemMap->Attribute);\r
614 if (EblAnyKeyToContinueQtoQuit (&CurrentRow, TRUE)) {\r
615 break;\r
616 }\r
617\r
618 PageCount[MemMap->Type % EfiMaxMemoryType] += MemMap->NumberOfPages;\r
619 MemMap = NEXT_MEMORY_DESCRIPTOR (MemMap, DescriptorSize);\r
620 }\r
621 }\r
622 \r
623 for (Index = 0, TotalMemory = 0; Index < EfiMaxMemoryType; Index++) {\r
624 if (PageCount[Index] != 0) {\r
625 AsciiPrint ("\n %a %,7ld Pages (%,14ld)", gMemMapType[Index], PageCount[Index], LShiftU64 (PageCount[Index], 12));\r
626 if (Index == EfiLoaderCode ||\r
627 Index == EfiLoaderData ||\r
628 Index == EfiBootServicesCode ||\r
629 Index == EfiBootServicesData ||\r
630 Index == EfiRuntimeServicesCode ||\r
631 Index == EfiRuntimeServicesData ||\r
632 Index == EfiConventionalMemory ||\r
633 Index == EfiACPIReclaimMemory ||\r
634 Index == EfiACPIMemoryNVS ||\r
635 Index == EfiPalCode\r
636 ) {\r
637 // Count total memory\r
638 TotalMemory += PageCount[Index];\r
639 }\r
640 }\r
641 }\r
642\r
643 AsciiPrint ("\nTotal Memory: %,ld MB (%,ld bytes)\n", RShiftU64 (TotalMemory, 8), LShiftU64 (TotalMemory, 12));\r
644\r
645 FreePool (OrigMemMap);\r
646\r
647 }\r
648 }\r
649\r
650 return EFI_SUCCESS;\r
651}\r
652\r
653\r
654\r
655\r
656/**\r
657 Load a file into memory and optionally jump to it. A load addres can be \r
658 specified or automatically allocated. A quoted command line can optionally\r
659 be passed into the image. \r
660\r
661 Argv[0] - "go"\r
662 Argv[1] - Device Name:path for the file to load\r
663 Argv[2] - Address to load to or '*' if the load address will be allocated\r
664 Argv[3] - Optional Entry point to the image. Image will be called if present\r
665 Argv[4] - "" string that will be passed as Argc & Argv to EntryPoint. Needs \r
666 to include the command name\r
667\r
668 go fv1:\EblCmdX 0x10000 0x10010 "EblCmdX Arg2 Arg3 Arg4"; - load EblCmdX \r
669 from FV1 to location 0x10000 and call the entry point at 0x10010 passing \r
670 in "EblCmdX Arg2 Arg3 Arg4" as the arguments.\r
671\r
672 go fv0:\EblCmdX * 0x10 "EblCmdX Arg2 Arg3 Arg4"; - load EblCmdX from FS0 \r
673 to location allocated by this comamnd and call the entry point at offset 0x10 \r
674 passing in "EblCmdX Arg2 Arg3 Arg4" as the arguments.\r
675\r
676 go fv1:\EblCmdX 0x10000; Load EblCmdX to address 0x10000 and return\r
677\r
678 @param Argc Number of command arguments in Argv\r
679 @param Argv Array of strings that represent the parsed command line. \r
680 Argv[0] is the comamnd name\r
681\r
682 @return EFI_SUCCESS\r
683\r
684**/\r
685EFI_STATUS\r
686EblGoCmd (\r
687 IN UINTN Argc,\r
688 IN CHAR8 **Argv\r
689 )\r
690{\r
691 EFI_STATUS Status;\r
692 EFI_OPEN_FILE *File;\r
693 VOID *Address;\r
694 UINTN Size;\r
695 EBL_COMMMAND EntryPoint;\r
696 UINTN EntryPointArgc;\r
697 CHAR8 *EntryPointArgv[MAX_ARGS];\r
698 \r
699\r
700 if (Argc <= 2) {\r
701 // device name and laod address are required\r
702 return EFI_SUCCESS;\r
703 }\r
704\r
705 File = EfiOpen (Argv[1], EFI_FILE_MODE_READ, 0);\r
706 if (File == NULL) {\r
707 AsciiPrint (" %a is not a valid path\n", Argv[1]);\r
708 return EFI_SUCCESS;\r
709 }\r
710\r
711 EntryPoint = (EBL_COMMMAND)((Argc > 3) ? (UINTN)AsciiStrHexToUintn (Argv[3]) : (UINTN)NULL);\r
712 if (Argv[2][0] == '*') {\r
713 // * Means allocate the buffer\r
714 Status = EfiReadAllocatePool (File, &Address, &Size);\r
715 \r
716 // EntryPoint is relatvie to the start of the image \r
717 EntryPoint = (EBL_COMMMAND)((UINTN)EntryPoint + (UINTN)Address);\r
718\r
719 } else {\r
720 Address = (VOID *)AsciiStrHexToUintn (Argv[2]);\r
721 Size = File->Size;\r
722\r
723 // File->Size for LoadFile is lazy so we need to use the tell to figure it out\r
724 EfiTell (File, NULL);\r
725 Status = EfiRead (File, Address, &Size);\r
726 }\r
727\r
728 if (!EFI_ERROR (Status)) {\r
729 AsciiPrint ("Loaded %,d bytes to 0x%08x\n", Size, Address);\r
730\r
731 if (Argc > 3) {\r
732 if (Argc > 4) {\r
733 ParseArguments (Argv[4], &EntryPointArgc, EntryPointArgv);\r
734 } else {\r
735 EntryPointArgc = 1;\r
736 EntryPointArgv[0] = File->FileName;\r
737 }\r
738 \r
739 Status = EntryPoint (EntryPointArgc, EntryPointArgv);\r
740 }\r
741 }\r
742\r
743 EfiClose (File);\r
744 return Status;\r
745}\r
746\r
747#define FILE_COPY_CHUNK 0x20000\r
748\r
749EFI_STATUS\r
750EblFileCopyCmd (\r
751 IN UINTN Argc,\r
752 IN CHAR8 **Argv\r
753 )\r
754{\r
755 EFI_OPEN_FILE *Source = NULL;\r
756 EFI_OPEN_FILE *Destination = NULL;\r
757 EFI_STATUS Status = EFI_SUCCESS;\r
758 VOID *Buffer = NULL;\r
759 UINTN Size;\r
760 UINTN Offset;\r
761 UINTN Chunk = FILE_COPY_CHUNK;\r
f6381f4c 762\r
2ef2b01e
A
763 if (Argc < 3) {\r
764 return EFI_INVALID_PARAMETER;\r
765 }\r
766 \r
767 Source = EfiOpen(Argv[1], EFI_FILE_MODE_READ, 0);\r
768 if (Source == NULL) {\r
769 AsciiPrint("Source file open error.\n");\r
770 return EFI_NOT_FOUND;\r
771 }\r
772 \r
773 Destination = EfiOpen(Argv[2], EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0);\r
774 if (Destination == NULL) {\r
775 AsciiPrint("Destination file open error.\n");\r
776 return EFI_NOT_FOUND;\r
777 }\r
778\r
779 Buffer = AllocatePool(FILE_COPY_CHUNK);\r
780 if (Buffer == NULL) {\r
781 goto Exit;\r
782 }\r
783 \r
784 Size = EfiTell(Source, NULL);\r
785\r
786 for (Offset = 0; Offset + FILE_COPY_CHUNK <= Size; Offset += Chunk) {\r
787 Chunk = FILE_COPY_CHUNK;\r
788 \r
789 Status = EfiRead(Source, Buffer, &Chunk);\r
790 if (EFI_ERROR(Status)) {\r
f6381f4c 791 AsciiPrint("Read file error %r\n", Status);\r
2ef2b01e
A
792 goto Exit;\r
793 }\r
794\r
795 Status = EfiWrite(Destination, Buffer, &Chunk);\r
796 if (EFI_ERROR(Status)) {\r
f6381f4c 797 AsciiPrint("Write file error %r\n", Status);\r
2ef2b01e
A
798 goto Exit;\r
799 } \r
800 }\r
801 \r
802 // Any left over?\r
803 if (Offset < Size) {\r
804 Chunk = Size - Offset;\r
805 \r
806 Status = EfiRead(Source, Buffer, &Chunk);\r
807 if (EFI_ERROR(Status)) {\r
f6381f4c 808 AsciiPrint("Read file error %r\n", Status);\r
2ef2b01e
A
809 goto Exit;\r
810 }\r
811\r
812 Status = EfiWrite(Destination, Buffer, &Chunk);\r
813 if (EFI_ERROR(Status)) {\r
f6381f4c 814 AsciiPrint("Write file error %r\n", Status);\r
2ef2b01e
A
815 goto Exit;\r
816 } \r
817 }\r
818\r
f6381f4c 819\r
2ef2b01e
A
820Exit:\r
821 if (Source != NULL) {\r
822 Status = EfiClose(Source);\r
823 if (EFI_ERROR(Status)) {\r
824 AsciiPrint("Source close error %r\n", Status);\r
825 }\r
826 }\r
2ef2b01e
A
827 if (Destination != NULL) {\r
828 Status = EfiClose(Destination);\r
829 if (EFI_ERROR(Status)) {\r
830 AsciiPrint("Destination close error %r\n", Status);\r
831 }\r
832 }\r
833 \r
834 if (Buffer != NULL) {\r
835 FreePool(Buffer);\r
836 }\r
837 \r
838 return Status;\r
839}\r
840\r
841EFI_STATUS\r
842EblFileDiffCmd (\r
843 IN UINTN Argc,\r
844 IN CHAR8 **Argv\r
845 )\r
846{\r
847 EFI_OPEN_FILE *File1 = NULL;\r
848 EFI_OPEN_FILE *File2 = NULL;\r
849 EFI_STATUS Status = EFI_SUCCESS;\r
850 VOID *Buffer1 = NULL;\r
851 VOID *Buffer2 = NULL;\r
852 UINTN Size1;\r
853 UINTN Size2;\r
854 UINTN Offset;\r
855 UINTN Chunk = FILE_COPY_CHUNK;\r
856 \r
857 if (Argc != 3) {\r
858 return EFI_INVALID_PARAMETER;\r
859 }\r
860 \r
861 File1 = EfiOpen(Argv[1], EFI_FILE_MODE_READ, 0);\r
862 if (File1 == NULL) {\r
863 AsciiPrint("File 1 open error.\n");\r
864 return EFI_NOT_FOUND;\r
865 }\r
866 \r
867 File2 = EfiOpen(Argv[2], EFI_FILE_MODE_READ, 0);\r
868 if (File2 == NULL) {\r
869 AsciiPrint("File 2 open error.\n");\r
870 return EFI_NOT_FOUND;\r
871 }\r
872\r
873 Size1 = EfiTell(File1, NULL);\r
874 Size2 = EfiTell(File2, NULL);\r
875\r
876 if (Size1 != Size2) {\r
877 AsciiPrint("Files differ.\n");\r
878 goto Exit;\r
879 }\r
880\r
881 Buffer1 = AllocatePool(FILE_COPY_CHUNK);\r
882 if (Buffer1 == NULL) {\r
883 goto Exit;\r
884 }\r
885 \r
886 Buffer2 = AllocatePool(FILE_COPY_CHUNK);\r
887 if (Buffer2 == NULL) {\r
888 goto Exit;\r
889 } \r
890\r
891 for (Offset = 0; Offset + FILE_COPY_CHUNK <= Size1; Offset += Chunk) {\r
892 Chunk = FILE_COPY_CHUNK;\r
893 \r
894 Status = EfiRead(File1, Buffer1, &Chunk);\r
895 if (EFI_ERROR(Status)) {\r
896 AsciiPrint("File 1 read error\n");\r
897 goto Exit;\r
898 }\r
899\r
900 Status = EfiRead(File2, Buffer2, &Chunk);\r
901 if (EFI_ERROR(Status)) {\r
902 AsciiPrint("File 2 read error\n");\r
903 goto Exit;\r
904 }\r
905 \r
906 if (CompareMem(Buffer1, Buffer2, Chunk) != 0) {\r
907 AsciiPrint("Files differ.\n");\r
908 goto Exit;\r
909 };\r
910 }\r
911 \r
912 // Any left over?\r
913 if (Offset < Size1) {\r
914 Chunk = Size1 - Offset;\r
915 \r
916 Status = EfiRead(File1, Buffer1, &Chunk);\r
917 if (EFI_ERROR(Status)) {\r
918 AsciiPrint("File 1 read error\n");\r
919 goto Exit;\r
920 }\r
921\r
922 Status = EfiRead(File2, Buffer2, &Chunk);\r
923 if (EFI_ERROR(Status)) {\r
924 AsciiPrint("File 2 read error\n");\r
925 goto Exit;\r
926 } \r
927 }\r
928 \r
929 if (CompareMem(Buffer1, Buffer2, Chunk) != 0) {\r
930 AsciiPrint("Files differ.\n");\r
931 } else {\r
932 AsciiPrint("Files are identical.\n");\r
933 }\r
934\r
935Exit:\r
936 if (File1 != NULL) {\r
937 Status = EfiClose(File1);\r
938 if (EFI_ERROR(Status)) {\r
939 AsciiPrint("File 1 close error %r\n", Status);\r
940 }\r
941 }\r
942 \r
943 if (File2 != NULL) {\r
944 Status = EfiClose(File2);\r
945 if (EFI_ERROR(Status)) {\r
946 AsciiPrint("File 2 close error %r\n", Status);\r
947 }\r
948 }\r
949 \r
950 if (Buffer1 != NULL) {\r
951 FreePool(Buffer1);\r
952 }\r
953 \r
954 if (Buffer2 != NULL) {\r
955 FreePool(Buffer2);\r
956 }\r
957 \r
958 return Status;\r
959}\r
960\r
961GLOBAL_REMOVE_IF_UNREFERENCED const EBL_COMMAND_TABLE mCmdDeviceTemplate[] =\r
962{\r
963 {\r
964 "connect",\r
965 "[d]; Connect all EFI devices. d means disconnect",\r
966 NULL,\r
967 EblConnectCmd\r
968 },\r
969 {\r
970 "device",\r
971 "; Show information about boot devices",\r
972 NULL,\r
973 EblDeviceCmd\r
974 },\r
975 {\r
976 "go",\r
977 " dev:path loadaddress entrypoint args; load to given address and jump in", \r
978 NULL,\r
979 EblGoCmd\r
980 },\r
981 {\r
982 "loadfv",\r
983 " devname; Load PI FV from device", \r
984 NULL,\r
985 EblLoadFvCmd\r
986 },\r
987 {\r
988 "start",\r
989 " path; EFI Boot Device:filepath. fs1:\\EFI\\BOOT.EFI", \r
990 NULL,\r
991 EblStartCmd\r
992 },\r
993 {\r
994 "memmap",\r
995 "; dump EFI memory map", \r
996 NULL,\r
997 EblMemMapCmd\r
998 },\r
999 {\r
1000 "cp",\r
1001 " file1 file2; copy file",\r
1002 NULL,\r
1003 EblFileCopyCmd\r
1004 },\r
1005 {\r
1006 "diff",\r
1007 " file1 file2; compare files",\r
1008 NULL,\r
1009 EblFileDiffCmd\r
1010 }\r
1011};\r
1012\r
1013\r
1014/**\r
1015 Initialize the commands in this in this file\r
1016**/\r
1017\r
1018VOID\r
1019EblInitializeDeviceCmd (\r
1020 VOID\r
1021 )\r
1022{\r
1023 EfiGetSystemConfigurationTable (&gEfiDxeServicesTableGuid, (VOID **) &gDS);\r
1024 EblAddCommands (mCmdDeviceTemplate, sizeof (mCmdDeviceTemplate)/sizeof (EBL_COMMAND_TABLE));\r
1025}\r
1026\r