3 * Copyright (c) 2011, 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 comamnd 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 Format
= (Argc
> 1) ? Argv
[1] : "load /a /ni /np %a &0x%x";
79 Elf
= (Argc
> 2) ? FALSE
: TRUE
;
81 Status
= EfiGetSystemConfigurationTable (&gEfiDebugImageInfoTableGuid
, (VOID
**)&DebugImageTableHeader
);
82 if (EFI_ERROR (Status
)) {
86 DebugTable
= DebugImageTableHeader
->EfiDebugImageInfoTable
;
87 if (DebugTable
== NULL
) {
91 for (Entry
= 0; Entry
< DebugImageTableHeader
->TableSize
; Entry
++, DebugTable
++) {
92 if (DebugTable
->NormalImage
!= NULL
) {
93 if ((DebugTable
->NormalImage
->ImageInfoType
== EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL
) && (DebugTable
->NormalImage
->LoadedImageProtocolInstance
!= NULL
)) {
94 ImageBase
= (UINT32
)DebugTable
->NormalImage
->LoadedImageProtocolInstance
->ImageBase
;
95 PeCoffSizeOfHeaders
= PeCoffGetSizeOfHeaders ((VOID
*)(UINTN
)ImageBase
);
96 Pdb
= PeCoffLoaderGetPdbPointer (DebugTable
->NormalImage
->LoadedImageProtocolInstance
->ImageBase
);
99 // ELF and Mach-O images don't include the header so the linked address does not include header
100 ImageBase
+= PeCoffSizeOfHeaders
;
102 AsciiPrint (Format
, Pdb
, ImageBase
);
115 Simple arm disassembler via a library
118 Argv[1] - Address to start disassembling from
119 ARgv[2] - Number of instructions to disassembly (optional)
121 @param Argc Number of command arguments in Argv
122 @param Argv Array of strings that represent the parsed command line.
123 Argv[0] is the command name
134 UINT8
*Ptr
, *CurrentAddress
;
141 return EFI_INVALID_PARAMETER
;
144 Address
= AsciiStrHexToUintn (Argv
[1]);
145 Count
= (Argc
> 2) ? (UINT32
)AsciiStrHexToUintn (Argv
[2]) : 20;
147 Ptr
= (UINT8
*)(UINTN
)Address
;
150 CurrentAddress
= Ptr
;
151 DisassembleInstruction (&Ptr
, TRUE
, TRUE
, &ItBlock
, Buffer
, sizeof (Buffer
));
152 AsciiPrint ("0x%08x: %a\n", CurrentAddress
, Buffer
);
153 } while (Count
-- > 0);
161 ImageHandleToPdbFileName (
166 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
170 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&LoadedImage
);
171 if (EFI_ERROR (Status
)) {
175 Pdb
= PeCoffLoaderGetPdbPointer (LoadedImage
->ImageBase
);
176 StripLeading
= AsciiStrStr (Pdb
, "\\ARM\\");
177 if (StripLeading
== NULL
) {
178 StripLeading
= AsciiStrStr (Pdb
, "/ARM/");
179 if (StripLeading
== NULL
) {
183 // Hopefully we hacked off the unneeded part
184 return (StripLeading
+ 5);
188 STATIC CHAR8
*mTokenList
[] = {
197 Simple arm disassembler via a library
200 Argv[1] - Address to start disassembling from
201 ARgv[2] - Number of instructions to disassembly (optional)
203 @param Argc Number of command arguments in Argv
204 @param Argv Array of strings that represent the parsed command line.
205 Argv[0] is the command name
218 CONST CHAR8
*Token
, *Module
;
219 UINT64 Start
, Stop
, TimeStamp
;
220 UINT64 Delta
, TicksPerSecond
, Milliseconds
, Microseconds
;
223 TicksPerSecond
= GetPerformanceCounterProperties (NULL
, NULL
);
227 Key
= GetPerformanceMeasurement (Key
, (CONST VOID
**)&Handle
, &Token
, &Module
, &Start
, &Stop
);
229 if (AsciiStriCmp ("StartImage:", Token
) == 0) {
231 // The entry for EBL is still running so the stop time will be zero. Skip it
232 AsciiPrint (" running %a\n", ImageHandleToPdbFileName ((EFI_HANDLE
)Handle
));
234 Delta
= Start
- Stop
;
235 Microseconds
= DivU64x64Remainder (MultU64x32 (Delta
, 1000000), TicksPerSecond
, NULL
);
236 AsciiPrint ("%10ld us %a\n", Microseconds
, ImageHandleToPdbFileName ((EFI_HANDLE
)Handle
));
247 Key
= GetPerformanceMeasurement (Key
, (CONST VOID
**)&Handle
, &Token
, &Module
, &Start
, &Stop
);
249 for (Index
= 0; mTokenList
[Index
] != NULL
; Index
++) {
250 if (AsciiStriCmp (mTokenList
[Index
], Token
) == 0) {
251 Delta
= Start
- Stop
;
253 Milliseconds
= DivU64x64Remainder (MultU64x32 (Delta
, 1000), TicksPerSecond
, NULL
);
254 AsciiPrint ("%6a %6ld ms\n", Token
, Milliseconds
);
261 AsciiPrint ("Total Time = %ld ms\n\n", DivU64x64Remainder (MultU64x32 (TimeStamp
, 1000), TicksPerSecond
, NULL
));
266 #define EFI_MEMORY_PORT_IO 0x4000000000000000ULL
275 UINTN NumberOfDescriptors
;
277 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
278 EFI_GCD_IO_SPACE_DESCRIPTOR
*IoSpaceMap
;
280 Status
= gDS
->GetMemorySpaceMap(&NumberOfDescriptors
,&MemorySpaceMap
);
281 if (EFI_ERROR (Status
)) {
284 AsciiPrint (" Address Range Image Device Attributes\n");
285 AsciiPrint ("__________________________________________________________\n");
286 for (i
=0; i
< NumberOfDescriptors
; i
++) {
287 AsciiPrint ("MEM %016lx - %016lx",(UINT64
)MemorySpaceMap
[i
].BaseAddress
,MemorySpaceMap
[i
].BaseAddress
+MemorySpaceMap
[i
].Length
-1);
288 AsciiPrint (" %08x %08x",MemorySpaceMap
[i
].ImageHandle
,MemorySpaceMap
[i
].DeviceHandle
);
290 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_RUNTIME
)
291 AsciiPrint (" RUNTIME");
292 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_PORT_IO
)
293 AsciiPrint (" PORT_IO");
295 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_UC
)
296 AsciiPrint (" MEM_UC");
297 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_WC
)
298 AsciiPrint (" MEM_WC");
299 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_WT
)
300 AsciiPrint (" MEM_WT");
301 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_WB
)
302 AsciiPrint (" MEM_WB");
303 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_UCE
)
304 AsciiPrint (" MEM_UCE");
305 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_WP
)
306 AsciiPrint (" MEM_WP");
307 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_RP
)
308 AsciiPrint (" MEM_RP");
309 if (MemorySpaceMap
[i
].Attributes
& EFI_MEMORY_XP
)
310 AsciiPrint (" MEM_XP");
312 if (MemorySpaceMap
[i
].GcdMemoryType
& EfiGcdMemoryTypeNonExistent
)
313 AsciiPrint (" TYPE_NONEXISTENT");
314 if (MemorySpaceMap
[i
].GcdMemoryType
& EfiGcdMemoryTypeReserved
)
315 AsciiPrint (" TYPE_RESERVED");
316 if (MemorySpaceMap
[i
].GcdMemoryType
& EfiGcdMemoryTypeSystemMemory
)
317 AsciiPrint (" TYPE_SYSMEM");
318 if (MemorySpaceMap
[i
].GcdMemoryType
& EfiGcdMemoryTypeMemoryMappedIo
)
319 AsciiPrint (" TYPE_MEMMAP");
324 Status
= gDS
->GetIoSpaceMap(&NumberOfDescriptors
,&IoSpaceMap
);
325 if (EFI_ERROR (Status
)) {
328 for (i
=0; i
< NumberOfDescriptors
; i
++) {
329 AsciiPrint ("IO %08lx - %08lx",IoSpaceMap
[i
].BaseAddress
,IoSpaceMap
[i
].BaseAddress
+IoSpaceMap
[i
].Length
);
330 AsciiPrint ("\t%08x %08x",IoSpaceMap
[i
].ImageHandle
,IoSpaceMap
[i
].DeviceHandle
);
332 if (IoSpaceMap
[i
].GcdIoType
& EfiGcdMemoryTypeNonExistent
)
333 AsciiPrint (" TYPE_NONEXISTENT");
334 if (IoSpaceMap
[i
].GcdIoType
& EfiGcdMemoryTypeReserved
)
335 AsciiPrint (" TYPE_RESERVED");
336 if (IoSpaceMap
[i
].GcdIoType
& EfiGcdIoTypeIo
)
337 AsciiPrint (" TYPE_IO");
353 EFI_HANDLE
*HandleBuffer
;
356 EFI_DEVICE_PATH_PROTOCOL
* DevicePathProtocol
;
357 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
* DevicePathToTextProtocol
;
359 BdsConnectAllDrivers();
361 Status
= gBS
->LocateProtocol(&gEfiDevicePathToTextProtocolGuid
, NULL
, (VOID
**)&DevicePathToTextProtocol
);
362 if (EFI_ERROR (Status
)) {
363 AsciiPrint ("Did not find the DevicePathToTextProtocol.\n");
367 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiDevicePathProtocolGuid
, NULL
, &HandleCount
, &HandleBuffer
);
368 if (EFI_ERROR (Status
)) {
369 AsciiPrint ("No device path found\n");
373 for (Index
= 0; Index
< HandleCount
; Index
++) {
374 Status
= gBS
->HandleProtocol (HandleBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
**)&DevicePathProtocol
);
375 String
= DevicePathToTextProtocol
->ConvertDevicePathToText(DevicePathProtocol
,TRUE
,TRUE
);
376 Print (L
"[0x%X] %s\n",(UINT32
)HandleBuffer
[Index
], String
);
382 GLOBAL_REMOVE_IF_UNREFERENCED
const EBL_COMMAND_TABLE mLibCmdTemplate
[] =
385 "disasm address [count]",
386 " disassemble count instructions",
392 " Display boot performance info",
397 "symboltable [\"format string\"] [PECOFF]",
398 " show symbol table commands for debugger",
404 " dump Global Coherency Domain",
416 " list all the Device Paths",
424 EblInitializeExternalCmd (
428 EblAddCommands (mLibCmdTemplate
, sizeof (mLibCmdTemplate
)/sizeof (EBL_COMMAND_TABLE
));