2 EBL commands for EFI and PI Devices
4 Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 EFI_DXE_SERVICES
*gDS
= NULL
;
24 Print information about the File System device.
26 @param File Open File for the device
31 IN EFI_OPEN_FILE
*File
40 AsciiPrint (" %a: ", File
->DeviceName
);
41 if (File
->FsInfo
!= NULL
) {
42 for (Str
= File
->FsInfo
->VolumeLabel
; *Str
!= '\0'; Str
++) {
44 // UI makes you enter _ for space, so make the printout match that
47 AsciiPrint ("%c", *Str
);
50 if (File
->FsInfo
->ReadOnly
) {
51 AsciiPrint ("ReadOnly");
61 Print information about the FV devices.
63 @param File Open File for the device
68 IN EFI_OPEN_FILE
*File
75 AsciiPrint (" %a: 0x%08lx - 0x%08lx : 0x%08x\n", File
->DeviceName
, File
->FvStart
, File
->FvStart
+ File
->FvSize
- 1, File
->FvSize
);
81 Print information about the Blk IO devices.
82 If the device supports PXE dump out extra information
84 @param File Open File for the device
89 IN EFI_OPEN_FILE
*File
95 EFI_OPEN_FILE
*FsFile
;
101 AsciiPrint (" %a: ", File
->DeviceName
);
103 // print out name of file system, if any, on this block device
104 Max
= EfiGetDeviceCounts (EfiOpenFileSystem
);
106 for (Index
= 0; Index
< Max
; Index
++) {
107 FsFile
= EfiDeviceOpenByType (EfiOpenFileSystem
, Index
);
108 if (FsFile
!= NULL
) {
109 if (FsFile
->EfiHandle
== File
->EfiHandle
) {
110 AsciiPrint ("fs%d: ", Index
);
119 // Print out useful Block IO media properties
120 if (File
->FsBlockIoMedia
->RemovableMedia
) {
121 AsciiPrint ("Removable ");
123 if (!File
->FsBlockIoMedia
->MediaPresent
) {
124 AsciiPrint ("No Media\n");
126 if (File
->FsBlockIoMedia
->LogicalPartition
) {
127 AsciiPrint ("Partition ");
129 DeviceSize
= MultU64x32 (File
->FsBlockIoMedia
->LastBlock
+ 1, File
->FsBlockIoMedia
->BlockSize
);
130 AsciiPrint ("Size = 0x%lX\n", DeviceSize
);
136 Print information about the Load File devices.
137 If the device supports PXE dump out extra information
139 @param File Open File for the device
143 EblPrintLoadFileInfo (
144 IN EFI_OPEN_FILE
*File
147 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
148 MAC_ADDR_DEVICE_PATH
*MacAddr
;
156 AsciiPrint (" %a: %a ", File
->DeviceName
, EblLoadFileBootTypeString (File
->EfiHandle
));
158 if (File
->DevicePath
!= NULL
) {
159 // Try to print out the MAC address
160 for (DevicePathNode
= File
->DevicePath
;
161 !IsDevicePathEnd (DevicePathNode
);
162 DevicePathNode
= NextDevicePathNode (DevicePathNode
)) {
164 if ((DevicePathType (DevicePathNode
) == MESSAGING_DEVICE_PATH
) && (DevicePathSubType (DevicePathNode
) == MSG_MAC_ADDR_DP
)) {
165 MacAddr
= (MAC_ADDR_DEVICE_PATH
*)DevicePathNode
;
167 HwAddressSize
= sizeof (EFI_MAC_ADDRESS
);
168 if (MacAddr
->IfType
== 0x01 || MacAddr
->IfType
== 0x00) {
173 for (Index
= 0; Index
< HwAddressSize
; Index
++) {
174 AsciiPrint ("%02x", MacAddr
->MacAddress
.Addr
[Index
] & 0xff);
188 Dump information about devices in the system.
190 fv: PI Firmware Volume
191 fs: EFI Simple File System
193 LoadFile: EFI Load File Protocol (commonly PXE network boot)
197 @param Argc Number of command arguments in Argv
198 @param Argv Array of strings that represent the parsed command line.
199 Argv[0] is the comamnd name
216 // Need to call here to make sure Device Counts are valid
217 EblUpdateDeviceLists ();
219 // Now we can print out the info...
220 Max
= EfiGetDeviceCounts (EfiOpenFirmwareVolume
);
222 AsciiPrint ("Firmware Volume Devices:\n");
223 for (Index
= 0; Index
< Max
; Index
++) {
224 EblPrintFvbInfo (EfiDeviceOpenByType (EfiOpenFirmwareVolume
, Index
));
225 if (EblAnyKeyToContinueQtoQuit (&CurrentRow
, TRUE
)) {
231 Max
= EfiGetDeviceCounts (EfiOpenFileSystem
);
233 AsciiPrint ("File System Devices:\n");
234 for (Index
= 0; Index
< Max
; Index
++) {
235 EblPrintFsInfo (EfiDeviceOpenByType (EfiOpenFileSystem
, Index
));
236 if (EblAnyKeyToContinueQtoQuit (&CurrentRow
, TRUE
)) {
242 Max
= EfiGetDeviceCounts (EfiOpenBlockIo
);
244 AsciiPrint ("Block IO Devices:\n");
245 for (Index
= 0; Index
< Max
; Index
++) {
246 EblPrintBlkIoInfo (EfiDeviceOpenByType (EfiOpenBlockIo
, Index
));
247 if (EblAnyKeyToContinueQtoQuit (&CurrentRow
, TRUE
)) {
253 Max
= EfiGetDeviceCounts (EfiOpenLoadFile
);
255 AsciiPrint ("LoadFile Devices: (usually network)\n");
256 for (Index
= 0; Index
< Max
; Index
++) {
257 EblPrintLoadFileInfo (EfiDeviceOpenByType (EfiOpenLoadFile
, Index
));
258 if (EblAnyKeyToContinueQtoQuit (&CurrentRow
, TRUE
)) {
269 Start an EFI image (PE32+ with EFI defined entry point).
272 Argv[1] - device name and path
273 Argv[2] - "" string to pass into image being started
275 start fs1:\Temp\Fv.Fv "arg to pass" ; load an FV from the disk and pass the
276 ; ascii string arg to pass to the image
277 start fv0:\FV ; load an FV from an FV (not common)
278 start LoadFile0: ; load an FV via a PXE boot
280 @param Argc Number of command arguments in Argv
281 @param Argv Array of strings that represent the parsed command line.
282 Argv[0] is the comamnd name
295 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
296 EFI_HANDLE ImageHandle
;
301 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
306 return EFI_INVALID_PARAMETER
;
309 File
= EfiOpen (Argv
[1], EFI_FILE_MODE_READ
, 0);
311 return EFI_INVALID_PARAMETER
;
314 DevicePath
= File
->DevicePath
;
315 if (DevicePath
!= NULL
) {
316 // check for device path form: blk, fv, fs, and loadfile
317 Status
= gBS
->LoadImage (FALSE
, gImageHandle
, DevicePath
, NULL
, 0, &ImageHandle
);
319 // Check for buffer form: A0x12345678:0x1234 syntax.
320 // Means load using buffer starting at 0x12345678 of size 0x1234.
322 Status
= EfiReadAllocatePool (File
, &Buffer
, &BufferSize
);
323 if (EFI_ERROR (Status
)) {
327 Status
= gBS
->LoadImage (FALSE
, gImageHandle
, DevicePath
, Buffer
, BufferSize
, &ImageHandle
);
334 if (!EFI_ERROR (Status
)) {
336 // Argv[2] is a "" string that we pass directly to the EFI application without the ""
337 // We don't pass Argv[0] to the EFI Application (it's name) just the args
338 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&ImageInfo
);
339 ASSERT_EFI_ERROR (Status
);
341 ImageInfo
->LoadOptionsSize
= (UINT32
)AsciiStrSize (Argv
[2]);
342 ImageInfo
->LoadOptions
= AllocatePool (ImageInfo
->LoadOptionsSize
);
343 AsciiStrCpy (ImageInfo
->LoadOptions
, Argv
[2]);
346 // Transfer control to the EFI image we loaded with LoadImage()
347 Status
= gBS
->StartImage (ImageHandle
, &ExitDataSize
, &ExitData
);
355 Load a Firmware Volume (FV) into memory from a device. This causes drivers in
356 the FV to be dispatched if the dependancies of the drivers are met.
359 Argv[1] - device name and path
361 loadfv fs1:\Temp\Fv.Fv ; load an FV from the disk
362 loadfv fv0:\FV ; load an FV from an FV (not common)
363 loadfv LoadFile0: ; load an FV via a PXE boot
365 @param Argc Number of command arguments in Argv
366 @param Argv Array of strings that represent the parsed command line.
367 Argv[0] is the comamnd name
386 return EFI_INVALID_PARAMETER
;
389 File
= EfiOpen (Argv
[1], EFI_FILE_MODE_READ
, 0);
391 return EFI_INVALID_PARAMETER
;
394 if (File
->Type
== EfiOpenMemoryBuffer
) {
395 // If it is a address just use it.
396 Status
= gDS
->ProcessFirmwareVolume (File
->Buffer
, File
->Size
, &FvHandle
);
398 // If it is a file read it into memory and use it
399 Status
= EfiReadAllocatePool (File
, &FvStart
, &FvSize
);
401 if (EFI_ERROR (Status
)) {
405 Status
= gDS
->ProcessFirmwareVolume (FvStart
, FvSize
, &FvHandle
);
413 Perform an EFI connect to connect devices that follow the EFI driver model.
414 If it is a PI system also call the dispatcher in case a new FV was made
415 availible by one of the connect EFI drivers (this is not a common case).
419 @param Argc Number of command arguments in Argv
420 @param Argv Array of strings that represent the parsed command line.
421 Argv[0] is the comamnd name
434 EFI_HANDLE
*HandleBuffer
;
441 if ((*Argv
[1] == 'd') || (*Argv
[1] == 'D')) {
442 Status
= gBS
->LocateHandleBuffer (
449 if (EFI_ERROR (Status
)) {
453 for (Index
= 0; Index
< HandleCount
; Index
++) {
454 gBS
->DisconnectController (HandleBuffer
[Index
], NULL
, NULL
);
458 // Given we disconnect our console we should go and do a connect now
461 File
= EfiOpen (Argv
[1], EFI_FILE_MODE_READ
, 0);
463 AsciiPrint ("Connecting %a\n", Argv
[1]);
464 gBS
->ConnectController (File
->EfiHandle
, NULL
, NULL
, TRUE
);
473 Status
= gBS
->LocateHandleBuffer (
480 if (EFI_ERROR (Status
)) {
484 for (Index
= 0; Index
< HandleCount
; Index
++) {
485 gBS
->ConnectController (HandleBuffer
[Index
], NULL
, NULL
, TRUE
);
488 FreePool (HandleBuffer
);
491 // Check to see if it's possible to dispatch an more DXE drivers.
492 // The BdsLibConnectAllEfi () may have made new DXE drivers show up.
493 // If anything is Dispatched Status == EFI_SUCCESS and we will try
494 // the connect again.
497 Status
= EFI_NOT_FOUND
;
499 Status
= gDS
->Dispatch ();
500 if (!EFI_ERROR (Status
)) {
505 } while (!EFI_ERROR (Status
));
508 AsciiPrint ("Connected and dispatched\n");
510 AsciiPrint ("Connect\n");
518 CHAR8
*gMemMapType
[] = {
537 Dump out the EFI memory map
541 @param Argc Number of command arguments in Argv
542 @param Argv Array of strings that represent the parsed command line.
543 Argv[0] is the comamnd name
555 EFI_MEMORY_DESCRIPTOR
*MemMap
;
556 EFI_MEMORY_DESCRIPTOR
*OrigMemMap
;
559 UINTN DescriptorSize
;
560 UINT32 DescriptorVersion
;
561 UINT64 PageCount
[EfiMaxMemoryType
];
567 ZeroMem (PageCount
, sizeof (PageCount
));
569 AsciiPrint ("EFI Memory Map\n");
571 // First call is to figure out how big the buffer needs to be
574 Status
= gBS
->GetMemoryMap (&MemMapSize
, MemMap
, &MapKey
, &DescriptorSize
, &DescriptorVersion
);
575 if (Status
== EFI_BUFFER_TOO_SMALL
) {
576 // In case the AllocatPool changes the memory map we added in some extra descriptors
577 MemMapSize
+= (DescriptorSize
* 0x100);
578 OrigMemMap
= MemMap
= AllocatePool (MemMapSize
);
579 if (OrigMemMap
!= NULL
) {
580 // 2nd time we get the data
581 Status
= gBS
->GetMemoryMap (&MemMapSize
, MemMap
, &MapKey
, &DescriptorSize
, &DescriptorVersion
);
582 if (!EFI_ERROR (Status
)) {
583 for (Index
= 0, CurrentRow
= 0; Index
< MemMapSize
/DescriptorSize
; Index
++) {
584 EntrySize
= LShiftU64 (MemMap
->NumberOfPages
, 12);
585 AsciiPrint ("\n%a %016lx - %016lx: # %08lx %016lx", gMemMapType
[MemMap
->Type
% EfiMaxMemoryType
], MemMap
->PhysicalStart
, MemMap
->PhysicalStart
+ EntrySize
-1, MemMap
->NumberOfPages
, MemMap
->Attribute
);
586 if (EblAnyKeyToContinueQtoQuit (&CurrentRow
, TRUE
)) {
590 PageCount
[MemMap
->Type
% EfiMaxMemoryType
] += MemMap
->NumberOfPages
;
591 MemMap
= NEXT_MEMORY_DESCRIPTOR (MemMap
, DescriptorSize
);
595 for (Index
= 0, TotalMemory
= 0; Index
< EfiMaxMemoryType
; Index
++) {
596 if (PageCount
[Index
] != 0) {
597 AsciiPrint ("\n %a %,7ld Pages (%,14ld)", gMemMapType
[Index
], PageCount
[Index
], LShiftU64 (PageCount
[Index
], 12));
598 if (Index
== EfiLoaderCode
||
599 Index
== EfiLoaderData
||
600 Index
== EfiBootServicesCode
||
601 Index
== EfiBootServicesData
||
602 Index
== EfiRuntimeServicesCode
||
603 Index
== EfiRuntimeServicesData
||
604 Index
== EfiConventionalMemory
||
605 Index
== EfiACPIReclaimMemory
||
606 Index
== EfiACPIMemoryNVS
||
609 // Count total memory
610 TotalMemory
+= PageCount
[Index
];
615 AsciiPrint ("\nTotal Memory: %,ld MB (%,ld bytes)\n", RShiftU64 (TotalMemory
, 8), LShiftU64 (TotalMemory
, 12));
617 FreePool (OrigMemMap
);
629 Load a file into memory and optionally jump to it. A load addres can be
630 specified or automatically allocated. A quoted command line can optionally
631 be passed into the image.
634 Argv[1] - Device Name:path for the file to load
635 Argv[2] - Address to load to or '*' if the load address will be allocated
636 Argv[3] - Optional Entry point to the image. Image will be called if present
637 Argv[4] - "" string that will be passed as Argc & Argv to EntryPoint. Needs
638 to include the command name
640 go fv1:\EblCmdX 0x10000 0x10010 "EblCmdX Arg2 Arg3 Arg4"; - load EblCmdX
641 from FV1 to location 0x10000 and call the entry point at 0x10010 passing
642 in "EblCmdX Arg2 Arg3 Arg4" as the arguments.
644 go fv0:\EblCmdX * 0x10 "EblCmdX Arg2 Arg3 Arg4"; - load EblCmdX from FS0
645 to location allocated by this comamnd and call the entry point at offset 0x10
646 passing in "EblCmdX Arg2 Arg3 Arg4" as the arguments.
648 go fv1:\EblCmdX 0x10000; Load EblCmdX to address 0x10000 and return
650 @param Argc Number of command arguments in Argv
651 @param Argv Array of strings that represent the parsed command line.
652 Argv[0] is the comamnd name
667 EBL_COMMMAND EntryPoint
;
668 UINTN EntryPointArgc
;
669 CHAR8
*EntryPointArgv
[MAX_ARGS
];
673 // device name and laod address are required
677 File
= EfiOpen (Argv
[1], EFI_FILE_MODE_READ
, 0);
679 AsciiPrint (" %a is not a valid path\n", Argv
[1]);
683 EntryPoint
= (EBL_COMMMAND
)((Argc
> 3) ? (UINTN
)AsciiStrHexToUintn (Argv
[3]) : (UINTN
)NULL
);
684 if (Argv
[2][0] == '*') {
685 // * Means allocate the buffer
686 Status
= EfiReadAllocatePool (File
, &Address
, &Size
);
688 // EntryPoint is relatvie to the start of the image
689 EntryPoint
= (EBL_COMMMAND
)((UINTN
)EntryPoint
+ (UINTN
)Address
);
692 Address
= (VOID
*)AsciiStrHexToUintn (Argv
[2]);
695 // File->Size for LoadFile is lazy so we need to use the tell to figure it out
696 EfiTell (File
, NULL
);
697 Status
= EfiRead (File
, Address
, &Size
);
700 if (!EFI_ERROR (Status
)) {
701 AsciiPrint ("Loaded %,d bytes to 0x%08x\n", Size
, Address
);
705 ParseArguments (Argv
[4], &EntryPointArgc
, EntryPointArgv
);
708 EntryPointArgv
[0] = File
->FileName
;
711 Status
= EntryPoint (EntryPointArgc
, EntryPointArgv
);
719 #define FILE_COPY_CHUNK 0x20000
727 EFI_OPEN_FILE
*Source
= NULL
;
728 EFI_OPEN_FILE
*Destination
= NULL
;
729 EFI_STATUS Status
= EFI_SUCCESS
;
733 UINTN Chunk
= FILE_COPY_CHUNK
;
736 return EFI_INVALID_PARAMETER
;
739 Source
= EfiOpen(Argv
[1], EFI_FILE_MODE_READ
, 0);
740 if (Source
== NULL
) {
741 AsciiPrint("Source file open error.\n");
742 return EFI_NOT_FOUND
;
745 Destination
= EfiOpen(Argv
[2], EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
, 0);
746 if (Destination
== NULL
) {
747 AsciiPrint("Destination file open error.\n");
748 return EFI_NOT_FOUND
;
751 Buffer
= AllocatePool(FILE_COPY_CHUNK
);
752 if (Buffer
== NULL
) {
756 Size
= EfiTell(Source
, NULL
);
758 for (Offset
= 0; Offset
+ FILE_COPY_CHUNK
<= Size
; Offset
+= Chunk
) {
759 Chunk
= FILE_COPY_CHUNK
;
761 Status
= EfiRead(Source
, Buffer
, &Chunk
);
762 if (EFI_ERROR(Status
)) {
763 AsciiPrint("Read file error %r\n", Status
);
767 Status
= EfiWrite(Destination
, Buffer
, &Chunk
);
768 if (EFI_ERROR(Status
)) {
769 AsciiPrint("Write file error %r\n", Status
);
776 Chunk
= Size
- Offset
;
778 Status
= EfiRead(Source
, Buffer
, &Chunk
);
779 if (EFI_ERROR(Status
)) {
780 AsciiPrint("Read file error %r\n", Status
);
784 Status
= EfiWrite(Destination
, Buffer
, &Chunk
);
785 if (EFI_ERROR(Status
)) {
786 AsciiPrint("Write file error %r\n", Status
);
793 if (Source
!= NULL
) {
794 Status
= EfiClose(Source
);
795 if (EFI_ERROR(Status
)) {
796 AsciiPrint("Source close error %r\n", Status
);
799 if (Destination
!= NULL
) {
800 Status
= EfiClose(Destination
);
801 if (EFI_ERROR(Status
)) {
802 AsciiPrint("Destination close error %r\n", Status
);
806 if (Buffer
!= NULL
) {
819 EFI_OPEN_FILE
*File1
= NULL
;
820 EFI_OPEN_FILE
*File2
= NULL
;
821 EFI_STATUS Status
= EFI_SUCCESS
;
822 VOID
*Buffer1
= NULL
;
823 VOID
*Buffer2
= NULL
;
827 UINTN Chunk
= FILE_COPY_CHUNK
;
830 return EFI_INVALID_PARAMETER
;
833 File1
= EfiOpen(Argv
[1], EFI_FILE_MODE_READ
, 0);
835 AsciiPrint("File 1 open error.\n");
836 return EFI_NOT_FOUND
;
839 File2
= EfiOpen(Argv
[2], EFI_FILE_MODE_READ
, 0);
841 AsciiPrint("File 2 open error.\n");
842 return EFI_NOT_FOUND
;
845 Size1
= EfiTell(File1
, NULL
);
846 Size2
= EfiTell(File2
, NULL
);
848 if (Size1
!= Size2
) {
849 AsciiPrint("Files differ.\n");
853 Buffer1
= AllocatePool(FILE_COPY_CHUNK
);
854 if (Buffer1
== NULL
) {
858 Buffer2
= AllocatePool(FILE_COPY_CHUNK
);
859 if (Buffer2
== NULL
) {
863 for (Offset
= 0; Offset
+ FILE_COPY_CHUNK
<= Size1
; Offset
+= Chunk
) {
864 Chunk
= FILE_COPY_CHUNK
;
866 Status
= EfiRead(File1
, Buffer1
, &Chunk
);
867 if (EFI_ERROR(Status
)) {
868 AsciiPrint("File 1 read error\n");
872 Status
= EfiRead(File2
, Buffer2
, &Chunk
);
873 if (EFI_ERROR(Status
)) {
874 AsciiPrint("File 2 read error\n");
878 if (CompareMem(Buffer1
, Buffer2
, Chunk
) != 0) {
879 AsciiPrint("Files differ.\n");
885 if (Offset
< Size1
) {
886 Chunk
= Size1
- Offset
;
888 Status
= EfiRead(File1
, Buffer1
, &Chunk
);
889 if (EFI_ERROR(Status
)) {
890 AsciiPrint("File 1 read error\n");
894 Status
= EfiRead(File2
, Buffer2
, &Chunk
);
895 if (EFI_ERROR(Status
)) {
896 AsciiPrint("File 2 read error\n");
901 if (CompareMem(Buffer1
, Buffer2
, Chunk
) != 0) {
902 AsciiPrint("Files differ.\n");
904 AsciiPrint("Files are identical.\n");
909 Status
= EfiClose(File1
);
910 if (EFI_ERROR(Status
)) {
911 AsciiPrint("File 1 close error %r\n", Status
);
916 Status
= EfiClose(File2
);
917 if (EFI_ERROR(Status
)) {
918 AsciiPrint("File 2 close error %r\n", Status
);
922 if (Buffer1
!= NULL
) {
926 if (Buffer2
!= NULL
) {
933 GLOBAL_REMOVE_IF_UNREFERENCED
const EBL_COMMAND_TABLE mCmdDeviceTemplate
[] =
937 "[d]; Connect all EFI devices. d means disconnect",
943 "; Show information about boot devices",
949 " dev:path loadaddress entrypoint args; load to given address and jump in",
955 " devname; Load PI FV from device",
961 " path; EFI Boot Device:filepath. fs1:\\EFI\\BOOT.EFI",
967 "; dump EFI memory map",
973 " file1 file2; copy file",
979 " file1 file2; compare files",
987 Initialize the commands in this in this file
991 EblInitializeDeviceCmd (
995 EfiGetSystemConfigurationTable (&gEfiDxeServicesTableGuid
, (VOID
**) &gDS
);
996 EblAddCommands (mCmdDeviceTemplate
, sizeof (mCmdDeviceTemplate
)/sizeof (EBL_COMMAND_TABLE
));