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
;
227 TicksPerSecond
= GetPerformanceCounterProperties (NULL
, NULL
);
231 Key
= GetPerformanceMeasurement (Key
, (CONST VOID
**)&Handle
, &Token
, &Module
, &Start
, &Stop
);
233 if (AsciiStriCmp ("StartImage:", Token
) == 0) {
235 // The entry for EBL is still running so the stop time will be zero. Skip it
236 AsciiPrint (" running %a\n", ImageHandleToPdbFileName ((EFI_HANDLE
)Handle
));
238 Delta
= Start
- Stop
;
239 Microseconds
= DivU64x64Remainder (MultU64x32 (Delta
, 1000000), TicksPerSecond
, NULL
);
240 AsciiPrint ("%10ld us %a\n", Microseconds
, ImageHandleToPdbFileName ((EFI_HANDLE
)Handle
));
251 Key
= GetPerformanceMeasurement (Key
, (CONST VOID
**)&Handle
, &Token
, &Module
, &Start
, &Stop
);
253 for (Index
= 0; mTokenList
[Index
] != NULL
; Index
++) {
254 if (AsciiStriCmp (mTokenList
[Index
], Token
) == 0) {
255 Delta
= Start
- Stop
;
257 Milliseconds
= DivU64x64Remainder (MultU64x32 (Delta
, 1000), TicksPerSecond
, NULL
);
258 AsciiPrint ("%6a %6ld ms\n", Token
, Milliseconds
);
265 AsciiPrint ("Total Time = %ld ms\n\n", DivU64x64Remainder (MultU64x32 (TimeStamp
, 1000), TicksPerSecond
, NULL
));
270 #define EFI_MEMORY_PORT_IO 0x4000000000000000ULL
279 UINTN NumberOfDescriptors
;
281 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
282 EFI_GCD_IO_SPACE_DESCRIPTOR
*IoSpaceMap
;
284 Status
= gDS
->GetMemorySpaceMap(&NumberOfDescriptors
,&MemorySpaceMap
);
285 if (EFI_ERROR (Status
)) {
288 AsciiPrint (" Address Range Image Device Attributes\n");
289 AsciiPrint ("__________________________________________________________\n");
290 for (i
=0; i
< NumberOfDescriptors
; i
++) {
291 AsciiPrint ("MEM %016lx - %016lx",(UINT64
)MemorySpaceMap
[i
].BaseAddress
,MemorySpaceMap
[i
].BaseAddress
+MemorySpaceMap
[i
].Length
-1);
292 AsciiPrint (" %08x %08x",MemorySpaceMap
[i
].ImageHandle
,MemorySpaceMap
[i
].DeviceHandle
);
294 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_RUNTIME
)
295 AsciiPrint (" RUNTIME");
296 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_PORT_IO
)
297 AsciiPrint (" PORT_IO");
299 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_UC
)
300 AsciiPrint (" MEM_UC");
301 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_WC
)
302 AsciiPrint (" MEM_WC");
303 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_WT
)
304 AsciiPrint (" MEM_WT");
305 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_WB
)
306 AsciiPrint (" MEM_WB");
307 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_UCE
)
308 AsciiPrint (" MEM_UCE");
309 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_WP
)
310 AsciiPrint (" MEM_WP");
311 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_RP
)
312 AsciiPrint (" MEM_RP");
313 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_XP
)
314 AsciiPrint (" MEM_XP");
316 if (MemorySpaceMap
[i
].GcdMemoryType
& EfiGcdMemoryTypeNonExistent
)
317 AsciiPrint (" TYPE_NONEXISTENT");
318 if (MemorySpaceMap
[i
].GcdMemoryType
& EfiGcdMemoryTypeReserved
)
319 AsciiPrint (" TYPE_RESERVED");
320 if (MemorySpaceMap
[i
].GcdMemoryType
& EfiGcdMemoryTypeSystemMemory
)
321 AsciiPrint (" TYPE_SYSMEM");
322 if (MemorySpaceMap
[i
].GcdMemoryType
& EfiGcdMemoryTypeMemoryMappedIo
)
323 AsciiPrint (" TYPE_MEMMAP");
328 Status
= gDS
->GetIoSpaceMap(&NumberOfDescriptors
,&IoSpaceMap
);
329 if (EFI_ERROR (Status
)) {
332 for (i
=0; i
< NumberOfDescriptors
; i
++) {
333 AsciiPrint ("IO %08lx - %08lx",IoSpaceMap
[i
].BaseAddress
,IoSpaceMap
[i
].BaseAddress
+IoSpaceMap
[i
].Length
);
334 AsciiPrint ("\t%08x %08x",IoSpaceMap
[i
].ImageHandle
,IoSpaceMap
[i
].DeviceHandle
);
336 if (IoSpaceMap
[i
].GcdIoType
& EfiGcdMemoryTypeNonExistent
)
337 AsciiPrint (" TYPE_NONEXISTENT");
338 if (IoSpaceMap
[i
].GcdIoType
& EfiGcdMemoryTypeReserved
)
339 AsciiPrint (" TYPE_RESERVED");
340 if (IoSpaceMap
[i
].GcdIoType
& EfiGcdIoTypeIo
)
341 AsciiPrint (" TYPE_IO");
357 EFI_HANDLE
*HandleBuffer
;
360 EFI_DEVICE_PATH_PROTOCOL
* DevicePathProtocol
;
361 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
* DevicePathToTextProtocol
;
363 BdsConnectAllDrivers();
365 Status
= gBS
->LocateProtocol(&gEfiDevicePathToTextProtocolGuid
, NULL
, (VOID
**)&DevicePathToTextProtocol
);
366 if (EFI_ERROR (Status
)) {
367 AsciiPrint ("Did not find the DevicePathToTextProtocol.\n");
371 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiDevicePathProtocolGuid
, NULL
, &HandleCount
, &HandleBuffer
);
372 if (EFI_ERROR (Status
)) {
373 AsciiPrint ("No device path found\n");
377 for (Index
= 0; Index
< HandleCount
; Index
++) {
378 Status
= gBS
->HandleProtocol (HandleBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
**)&DevicePathProtocol
);
379 String
= DevicePathToTextProtocol
->ConvertDevicePathToText(DevicePathProtocol
,TRUE
,TRUE
);
380 Print (L
"[0x%X] %s\n",(UINT32
)HandleBuffer
[Index
], String
);
386 GLOBAL_REMOVE_IF_UNREFERENCED
const EBL_COMMAND_TABLE mLibCmdTemplate
[] =
389 "disasm address [count]",
390 " disassemble count instructions",
396 " Display boot performance info",
401 "symboltable [\"format string\"] [PECOFF]",
402 " show symbol table commands for debugger",
408 " dump Global Coherency Domain",
420 " list all the Device Paths",
428 EblInitializeExternalCmd (
432 EblAddCommands (mLibCmdTemplate
, sizeof (mLibCmdTemplate
)/sizeof (EBL_COMMAND_TABLE
));