]> git.proxmox.com Git - mirror_edk2.git/blame - BeagleBoardPkg/Library/EblCmdLib/EblCmdLib.c
Update the copyright notice format
[mirror_edk2.git] / BeagleBoardPkg / Library / EblCmdLib / EblCmdLib.c
CommitLineData
2ef2b01e
A
1/** @file\r
2 Add custom commands for BeagleBoard development.\r
3\r
1ebd6c11 4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
2ef2b01e 5 \r
1ebd6c11 6 This program and the accompanying materials\r
2ef2b01e
A
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <PiDxe.h>\r
17#include <Library/ArmLib.h>\r
18#include <Library/CacheMaintenanceLib.h>\r
19#include <Library/EblCmdLib.h>\r
20#include <Library/BaseLib.h>\r
21#include <Library/DebugLib.h>\r
22#include <Library/UefiBootServicesTableLib.h>\r
23#include <Library/UefiRuntimeServicesTableLib.h>\r
24#include <Library/MemoryAllocationLib.h>\r
25#include <Library/UefiLib.h>\r
26#include <Library/PcdLib.h>\r
27#include <Library/EfiFileLib.h>\r
f9f937d2 28#include <Library/ArmDisassemblerLib.h>\r
b2a73e21 29#include <Library/PeCoffGetEntryPointLib.h>\r
d02b28d7 30#include <Library/PerformanceLib.h>\r
31#include <Library/TimerLib.h>\r
2ef2b01e 32\r
b2a73e21 33#include <Guid/DebugImageInfoTable.h>\r
d02b28d7 34\r
b2a73e21 35#include <Protocol/DebugSupport.h>\r
36#include <Protocol/LoadedImage.h>\r
6f72e28d 37\r
38/**\r
b2a73e21 39 Simple arm disassembler via a library\r
6f72e28d 40\r
b2a73e21 41 Argv[0] - symboltable\r
42 Argv[1] - Optional qoted format string \r
43 Argv[2] - Optional flag\r
44\r
45 @param Argc Number of command arguments in Argv\r
46 @param Argv Array of strings that represent the parsed command line. \r
47 Argv[0] is the comamnd name\r
48\r
49 @return EFI_SUCCESS\r
50\r
51**/\r
52EFI_STATUS\r
53EblSymbolTable (\r
54 IN UINTN Argc,\r
55 IN CHAR8 **Argv\r
56 )\r
57{\r
58 EFI_STATUS Status;\r
59 EFI_DEBUG_IMAGE_INFO_TABLE_HEADER *DebugImageTableHeader = NULL;\r
60 EFI_DEBUG_IMAGE_INFO *DebugTable;\r
61 UINTN Entry;\r
62 CHAR8 *Format;\r
63 CHAR8 *Pdb;\r
64 UINT32 PeCoffSizeOfHeaders;\r
65 UINT32 ImageBase;\r
66 BOOLEAN Elf;\r
67 \r
68 // Need to add lots of error checking on the passed in string\r
b8758b6e 69 // Default string is for RealView debugger\r
70 Format = (Argc > 1) ? Argv[1] : "load /a /ni /np %a &0x%x";\r
b2a73e21 71 Elf = (Argc > 2) ? FALSE : TRUE;\r
72 \r
73 Status = EfiGetSystemConfigurationTable (&gEfiDebugImageInfoTableGuid, (VOID **)&DebugImageTableHeader);\r
74 if (EFI_ERROR (Status)) {\r
75 return Status;\r
76 }\r
77 \r
78 DebugTable = DebugImageTableHeader->EfiDebugImageInfoTable;\r
79 if (DebugTable == NULL) {\r
80 return EFI_SUCCESS;\r
81 }\r
82\r
83 for (Entry = 0; Entry < DebugImageTableHeader->TableSize; Entry++, DebugTable++) {\r
84 if (DebugTable->NormalImage != NULL) {\r
85 if ((DebugTable->NormalImage->ImageInfoType == EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL) && (DebugTable->NormalImage->LoadedImageProtocolInstance != NULL)) {\r
86 ImageBase = (UINT32)DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase;\r
87 PeCoffSizeOfHeaders = PeCoffGetSizeOfHeaders ((VOID *)(UINTN)ImageBase);\r
88 Pdb = PeCoffLoaderGetPdbPointer (DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase);\r
b8758b6e 89 if (Pdb != NULL) {\r
90 if (Elf) {\r
91 // ELF and Mach-O images don't include the header so the linked address does not include header\r
92 ImageBase += PeCoffSizeOfHeaders;\r
93 } \r
94 AsciiPrint (Format, Pdb, ImageBase);\r
95 AsciiPrint ("\n");\r
96 } else {\r
97 }\r
b2a73e21 98 }\r
99 } \r
100 }\r
101\r
102 return EFI_SUCCESS;\r
103}\r
104\r
105\r
106/**\r
107 Simple arm disassembler via a library\r
108\r
109 Argv[0] - disasm\r
110 Argv[1] - Address to start disassembling from\r
111 ARgv[2] - Number of instructions to disassembly (optional)\r
6f72e28d 112\r
113 @param Argc Number of command arguments in Argv\r
114 @param Argv Array of strings that represent the parsed command line. \r
115 Argv[0] is the comamnd name\r
116\r
117 @return EFI_SUCCESS\r
118\r
119**/\r
120EFI_STATUS\r
f9f937d2 121EblDisassembler (\r
6f72e28d 122 IN UINTN Argc,\r
123 IN CHAR8 **Argv\r
124 )\r
125{\r
77844905 126 UINT8 *Ptr, *CurrentAddress;\r
f9f937d2 127 UINT32 Address;\r
128 UINT32 Count;\r
129 CHAR8 Buffer[80];\r
f3198cba 130 UINT32 ItBlock;\r
f9f937d2 131 \r
132 if (Argc < 2) {\r
133 return EFI_INVALID_PARAMETER;\r
134 }\r
135 \r
136 Address = AsciiStrHexToUintn (Argv[1]);\r
77844905 137 Count = (Argc > 2) ? (UINT32)AsciiStrHexToUintn (Argv[2]) : 20;\r
f9f937d2 138\r
139 Ptr = (UINT8 *)(UINTN)Address; \r
f3198cba 140 ItBlock = 0;\r
77844905 141 do {\r
142 CurrentAddress = Ptr;\r
f3198cba 143 DisassembleInstruction (&Ptr, TRUE, TRUE, &ItBlock, Buffer, sizeof (Buffer));\r
77844905 144 AsciiPrint ("0x%08x: %a\n", CurrentAddress, Buffer);\r
145 } while (Count-- > 0);\r
146 \r
f9f937d2 147\r
6f72e28d 148 return EFI_SUCCESS;\r
149}\r
150\r
151\r
d02b28d7 152CHAR8 *\r
153ImageHandleToPdbFileName (\r
154 IN EFI_HANDLE Handle\r
155 )\r
156{\r
157 EFI_STATUS Status;\r
158 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
d39eb83c 159 CHAR8 *Pdb;\r
160 CHAR8 *StripLeading;\r
d02b28d7 161\r
162 Status = gBS->HandleProtocol (Handle, &gEfiLoadedImageProtocolGuid, (VOID **)&LoadedImage);\r
163 if (EFI_ERROR (Status)) {\r
164 return "";\r
165 }\r
166\r
d39eb83c 167 Pdb = PeCoffLoaderGetPdbPointer (LoadedImage->ImageBase);\r
168 StripLeading = AsciiStrStr (Pdb, "\\ARM\\");\r
169 if (StripLeading == NULL) {\r
170 StripLeading = AsciiStrStr (Pdb, "/ARM/");\r
171 if (StripLeading == NULL) {\r
172 return Pdb;\r
173 }\r
174 }\r
175 // Hopefully we hacked off the unneeded part\r
176 return (StripLeading + 5);\r
d02b28d7 177}\r
178\r
d39eb83c 179\r
d02b28d7 180CHAR8 *mTokenList[] = {\r
181 "SEC",\r
182 "PEI",\r
183 "DXE",\r
184 "BDS",\r
185 NULL\r
186};\r
187\r
188/**\r
189 Simple arm disassembler via a library\r
190\r
191 Argv[0] - disasm\r
192 Argv[1] - Address to start disassembling from\r
193 ARgv[2] - Number of instructions to disassembly (optional)\r
194\r
195 @param Argc Number of command arguments in Argv\r
196 @param Argv Array of strings that represent the parsed command line. \r
197 Argv[0] is the comamnd name\r
198\r
199 @return EFI_SUCCESS\r
200\r
201**/\r
202EFI_STATUS\r
203EblPerformance (\r
204 IN UINTN Argc,\r
205 IN CHAR8 **Argv\r
206 )\r
207{\r
208 UINTN Key;\r
209 CONST VOID *Handle;\r
210 CONST CHAR8 *Token, *Module;\r
211 UINT64 Start, Stop, TimeStamp;\r
212 UINT64 Delta, TicksPerSecond, Milliseconds, Microseconds;\r
213 UINTN Index;\r
214\r
215 TicksPerSecond = GetPerformanceCounterProperties (NULL, NULL);\r
216\r
217 Key = 0;\r
218 do {\r
219 Key = GetPerformanceMeasurement (Key, (CONST VOID **)&Handle, &Token, &Module, &Start, &Stop);\r
220 if (Key != 0) {\r
221 if (AsciiStriCmp ("StartImage:", Token) == 0) {\r
222 if (Stop == 0) {\r
223 // The entry for EBL is still running so the stop time will be zero. Skip it\r
224 AsciiPrint (" running %a\n", ImageHandleToPdbFileName ((EFI_HANDLE)Handle));\r
225 } else {\r
226 Delta = Stop - Start;\r
227 Microseconds = DivU64x64Remainder (MultU64x32 (Delta, 1000000), TicksPerSecond, NULL);\r
228 AsciiPrint ("%10ld us %a\n", Microseconds, ImageHandleToPdbFileName ((EFI_HANDLE)Handle));\r
229 }\r
230 }\r
231 }\r
232 } while (Key != 0);\r
233\r
234 AsciiPrint ("\n");\r
235\r
236 TimeStamp = 0;\r
237 Key = 0;\r
238 do {\r
239 Key = GetPerformanceMeasurement (Key, (CONST VOID **)&Handle, &Token, &Module, &Start, &Stop);\r
240 if (Key != 0) {\r
241 for (Index = 0; mTokenList[Index] != NULL; Index++) {\r
242 if (AsciiStriCmp (mTokenList[Index], Token) == 0) {\r
243 Delta = Stop - Start;\r
244 TimeStamp += Delta;\r
245 Milliseconds = DivU64x64Remainder (MultU64x32 (Delta, 1000), TicksPerSecond, NULL);\r
246 AsciiPrint ("%6a %6ld ms\n", Token, Milliseconds);\r
247 break;\r
248 }\r
249 } \r
250 }\r
251 } while (Key != 0);\r
252\r
253 AsciiPrint ("Total Time = %ld ms\n\n", DivU64x64Remainder (MultU64x32 (TimeStamp, 1000), TicksPerSecond, NULL));\r
254\r
255 return EFI_SUCCESS;\r
256}\r
257\r
258\r
2ef2b01e
A
259GLOBAL_REMOVE_IF_UNREFERENCED const EBL_COMMAND_TABLE mLibCmdTemplate[] =\r
260{\r
6f72e28d 261 {\r
f9f937d2 262 "disasm address [count]",\r
263 " disassemble count instructions",\r
6f72e28d 264 NULL,\r
f9f937d2 265 EblDisassembler\r
b2a73e21 266 },\r
d02b28d7 267 {\r
268 "performance",\r
269 " Display boot performance info",\r
270 NULL,\r
271 EblPerformance\r
272 },\r
b2a73e21 273 {\r
b8758b6e 274 "symboltable [\"format string\"] [PECOFF]",\r
b2a73e21 275 " show symbol table commands for debugger",\r
276 NULL,\r
277 EblSymbolTable\r
6f72e28d 278 }\r
2ef2b01e
A
279};\r
280\r
281\r
282VOID\r
283EblInitializeExternalCmd (\r
284 VOID\r
285 )\r
286{\r
287 EblAddCommands (mLibCmdTemplate, sizeof (mLibCmdTemplate)/sizeof (EBL_COMMAND_TABLE));\r
288 return;\r
289}\r