3 * Copyright (c) 2011-2012, ARM Limited. All rights reserved.
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/ArmLib.h>
17 #include <Library/CacheMaintenanceLib.h>
18 #include <Library/EblCmdLib.h>
19 #include <Library/BaseLib.h>
20 #include <Library/DxeServicesTableLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
23 #include <Library/UefiRuntimeServicesTableLib.h>
24 #include <Library/MemoryAllocationLib.h>
25 #include <Library/UefiLib.h>
26 #include <Library/PcdLib.h>
27 #include <Library/EfiFileLib.h>
28 #include <Library/ArmDisassemblerLib.h>
29 #include <Library/PeCoffGetEntryPointLib.h>
30 #include <Library/PerformanceLib.h>
31 #include <Library/TimerLib.h>
32 #include <Library/BdsLib.h>
34 #include <Guid/DebugImageInfoTable.h>
36 #include <Protocol/DebugSupport.h>
37 #include <Protocol/LoadedImage.h>
38 #include <Protocol/DevicePathToText.h>
47 Simple arm disassembler via a library
50 Argv[1] - Optional qoted format string
51 Argv[2] - Optional flag
53 @param Argc Number of command arguments in Argv
54 @param Argv Array of strings that represent the parsed command line.
55 Argv[0] is the command name
67 EFI_DEBUG_IMAGE_INFO_TABLE_HEADER
*DebugImageTableHeader
= NULL
;
68 EFI_DEBUG_IMAGE_INFO
*DebugTable
;
72 UINT32 PeCoffSizeOfHeaders
;
76 // Need to add lots of error checking on the passed in string
77 // Default string is for RealView debugger
78 #if (__ARMCC_VERSION < 500000)
79 Format
= (Argc
> 1) ? Argv
[1] : "load /a /ni /np %a &0x%x";
81 Format
= (Argc
> 1) ? Argv
[1] : "add-symbol-file %a 0x%x";
83 Elf
= (Argc
> 2) ? FALSE
: TRUE
;
85 Status
= EfiGetSystemConfigurationTable (&gEfiDebugImageInfoTableGuid
, (VOID
**)&DebugImageTableHeader
);
86 if (EFI_ERROR (Status
)) {
90 DebugTable
= DebugImageTableHeader
->EfiDebugImageInfoTable
;
91 if (DebugTable
== NULL
) {
95 for (Entry
= 0; Entry
< DebugImageTableHeader
->TableSize
; Entry
++, DebugTable
++) {
96 if (DebugTable
->NormalImage
!= NULL
) {
97 if ((DebugTable
->NormalImage
->ImageInfoType
== EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
) && (DebugTable
->NormalImage
->LoadedImageProtocolInstance
!= NULL
)) {
98 ImageBase
= (UINT32
)DebugTable
->NormalImage
->LoadedImageProtocolInstance
->ImageBase
;
99 PeCoffSizeOfHeaders
= PeCoffGetSizeOfHeaders ((VOID
*)(UINTN
)ImageBase
);
100 Pdb
= PeCoffLoaderGetPdbPointer (DebugTable
->NormalImage
->LoadedImageProtocolInstance
->ImageBase
);
103 // ELF and Mach-O images don't include the header so the linked address does not include header
104 ImageBase
+= PeCoffSizeOfHeaders
;
106 AsciiPrint (Format
, Pdb
, ImageBase
);
119 Simple arm disassembler via a library
122 Argv[1] - Address to start disassembling from
123 ARgv[2] - Number of instructions to disassembly (optional)
125 @param Argc Number of command arguments in Argv
126 @param Argv Array of strings that represent the parsed command line.
127 Argv[0] is the command name
138 UINT8
*Ptr
, *CurrentAddress
;
145 return EFI_INVALID_PARAMETER
;
148 Address
= AsciiStrHexToUintn (Argv
[1]);
149 Count
= (Argc
> 2) ? (UINT32
)AsciiStrHexToUintn (Argv
[2]) : 20;
151 Ptr
= (UINT8
*)(UINTN
)Address
;
154 CurrentAddress
= Ptr
;
155 DisassembleInstruction (&Ptr
, TRUE
, TRUE
, &ItBlock
, Buffer
, sizeof (Buffer
));
156 AsciiPrint ("0x%08x: %a\n", CurrentAddress
, Buffer
);
157 } while (Count
-- > 0);
165 ImageHandleToPdbFileName (
170 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
174 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&LoadedImage
);
175 if (EFI_ERROR (Status
)) {
179 Pdb
= PeCoffLoaderGetPdbPointer (LoadedImage
->ImageBase
);
180 StripLeading
= AsciiStrStr (Pdb
, "\\ARM\\");
181 if (StripLeading
== NULL
) {
182 StripLeading
= AsciiStrStr (Pdb
, "/ARM/");
183 if (StripLeading
== NULL
) {
187 // Hopefully we hacked off the unneeded part
188 return (StripLeading
+ 5);
192 STATIC CHAR8
*mTokenList
[] = {
201 Simple arm disassembler via a library
204 Argv[1] - Address to start disassembling from
205 ARgv[2] - Number of instructions to disassembly (optional)
207 @param Argc Number of command arguments in Argv
208 @param Argv Array of strings that represent the parsed command line.
209 Argv[0] is the command name
222 CONST CHAR8
*Token
, *Module
;
223 UINT64 Start
, Stop
, TimeStamp
;
224 UINT64 Delta
, TicksPerSecond
, Milliseconds
, Microseconds
;
228 TicksPerSecond
= GetPerformanceCounterProperties (&Start
, &Stop
);
237 Key
= GetPerformanceMeasurement (Key
, (CONST VOID
**)&Handle
, &Token
, &Module
, &Start
, &Stop
);
239 if (AsciiStriCmp ("StartImage:", Token
) == 0) {
241 // The entry for EBL is still running so the stop time will be zero. Skip it
242 AsciiPrint (" running %a\n", ImageHandleToPdbFileName ((EFI_HANDLE
)Handle
));
244 Delta
= CountUp
?(Stop
- Start
):(Start
- Stop
);
245 Microseconds
= DivU64x64Remainder (MultU64x32 (Delta
, 1000000), TicksPerSecond
, NULL
);
246 AsciiPrint ("%10ld us %a\n", Microseconds
, ImageHandleToPdbFileName ((EFI_HANDLE
)Handle
));
257 Key
= GetPerformanceMeasurement (Key
, (CONST VOID
**)&Handle
, &Token
, &Module
, &Start
, &Stop
);
259 for (Index
= 0; mTokenList
[Index
] != NULL
; Index
++) {
260 if (AsciiStriCmp (mTokenList
[Index
], Token
) == 0) {
261 Delta
= CountUp
?(Stop
- Start
):(Start
- Stop
);
263 Milliseconds
= DivU64x64Remainder (MultU64x32 (Delta
, 1000), TicksPerSecond
, NULL
);
264 AsciiPrint ("%6a %6ld ms\n", Token
, Milliseconds
);
271 AsciiPrint ("Total Time = %ld ms\n\n", DivU64x64Remainder (MultU64x32 (TimeStamp
, 1000), TicksPerSecond
, NULL
));
276 #define EFI_MEMORY_PORT_IO 0x4000000000000000ULL
285 UINTN NumberOfDescriptors
;
287 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
288 EFI_GCD_IO_SPACE_DESCRIPTOR
*IoSpaceMap
;
290 Status
= gDS
->GetMemorySpaceMap(&NumberOfDescriptors
,&MemorySpaceMap
);
291 if (EFI_ERROR (Status
)) {
294 AsciiPrint (" Address Range Image Device Attributes\n");
295 AsciiPrint ("__________________________________________________________\n");
296 for (i
=0; i
< NumberOfDescriptors
; i
++) {
297 AsciiPrint ("MEM %016lx - %016lx",(UINT64
)MemorySpaceMap
[i
].BaseAddress
,MemorySpaceMap
[i
].BaseAddress
+MemorySpaceMap
[i
].Length
-1);
298 AsciiPrint (" %08x %08x",MemorySpaceMap
[i
].ImageHandle
,MemorySpaceMap
[i
].DeviceHandle
);
300 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_RUNTIME
)
301 AsciiPrint (" RUNTIME");
302 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_PORT_IO
)
303 AsciiPrint (" PORT_IO");
305 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_UC
)
306 AsciiPrint (" MEM_UC");
307 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_WC
)
308 AsciiPrint (" MEM_WC");
309 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_WT
)
310 AsciiPrint (" MEM_WT");
311 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_WB
)
312 AsciiPrint (" MEM_WB");
313 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_UCE
)
314 AsciiPrint (" MEM_UCE");
315 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_WP
)
316 AsciiPrint (" MEM_WP");
317 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_RP
)
318 AsciiPrint (" MEM_RP");
319 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_XP
)
320 AsciiPrint (" MEM_XP");
322 if (MemorySpaceMap
[i
].GcdMemoryType
& EfiGcdMemoryTypeNonExistent
)
323 AsciiPrint (" TYPE_NONEXISTENT");
324 if (MemorySpaceMap
[i
].GcdMemoryType
& EfiGcdMemoryTypeReserved
)
325 AsciiPrint (" TYPE_RESERVED");
326 if (MemorySpaceMap
[i
].GcdMemoryType
& EfiGcdMemoryTypeSystemMemory
)
327 AsciiPrint (" TYPE_SYSMEM");
328 if (MemorySpaceMap
[i
].GcdMemoryType
& EfiGcdMemoryTypeMemoryMappedIo
)
329 AsciiPrint (" TYPE_MEMMAP");
334 Status
= gDS
->GetIoSpaceMap(&NumberOfDescriptors
,&IoSpaceMap
);
335 if (EFI_ERROR (Status
)) {
338 for (i
=0; i
< NumberOfDescriptors
; i
++) {
339 AsciiPrint ("IO %08lx - %08lx",IoSpaceMap
[i
].BaseAddress
,IoSpaceMap
[i
].BaseAddress
+IoSpaceMap
[i
].Length
);
340 AsciiPrint ("\t%08x %08x",IoSpaceMap
[i
].ImageHandle
,IoSpaceMap
[i
].DeviceHandle
);
342 if (IoSpaceMap
[i
].GcdIoType
& EfiGcdMemoryTypeNonExistent
)
343 AsciiPrint (" TYPE_NONEXISTENT");
344 if (IoSpaceMap
[i
].GcdIoType
& EfiGcdMemoryTypeReserved
)
345 AsciiPrint (" TYPE_RESERVED");
346 if (IoSpaceMap
[i
].GcdIoType
& EfiGcdIoTypeIo
)
347 AsciiPrint (" TYPE_IO");
363 EFI_HANDLE
*HandleBuffer
;
366 EFI_DEVICE_PATH_PROTOCOL
* DevicePathProtocol
;
367 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
* DevicePathToTextProtocol
;
369 BdsConnectAllDrivers();
371 Status
= gBS
->LocateProtocol(&gEfiDevicePathToTextProtocolGuid
, NULL
, (VOID
**)&DevicePathToTextProtocol
);
372 if (EFI_ERROR (Status
)) {
373 AsciiPrint ("Did not find the DevicePathToTextProtocol.\n");
377 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiDevicePathProtocolGuid
, NULL
, &HandleCount
, &HandleBuffer
);
378 if (EFI_ERROR (Status
)) {
379 AsciiPrint ("No device path found\n");
383 for (Index
= 0; Index
< HandleCount
; Index
++) {
384 Status
= gBS
->HandleProtocol (HandleBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
**)&DevicePathProtocol
);
385 String
= DevicePathToTextProtocol
->ConvertDevicePathToText(DevicePathProtocol
,TRUE
,TRUE
);
386 Print (L
"[0x%X] %s\n",(UINT32
)HandleBuffer
[Index
], String
);
392 GLOBAL_REMOVE_IF_UNREFERENCED
const EBL_COMMAND_TABLE mLibCmdTemplate
[] =
395 "disasm address [count]",
396 " disassemble count instructions",
402 " Display boot performance info",
407 "symboltable [\"format string\"] [PECOFF]",
408 " show symbol table commands for debugger",
414 " dump Global Coherency Domain",
426 " list all the Device Paths",
434 EblInitializeExternalCmd (
438 EblAddCommands (mLibCmdTemplate
, sizeof (mLibCmdTemplate
)/sizeof (EBL_COMMAND_TABLE
));