]> git.proxmox.com Git - mirror_edk2.git/blob - BeagleBoardPkg/Library/EblCmdLib/EblCmdLib.c
2141c159b500e6ef962ca3f0b15790d7c3e112f5
[mirror_edk2.git] / BeagleBoardPkg / Library / EblCmdLib / EblCmdLib.c
1 /** @file
2 Add custom commands for BeagleBoard development.
3
4 Copyright (c) 2008-2010, Apple Inc. All rights reserved.
5
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include <PiDxe.h>
17 #include <Library/ArmLib.h>
18 #include <Library/CacheMaintenanceLib.h>
19 #include <Library/EblCmdLib.h>
20 #include <Library/BaseLib.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
31 #include <Guid/DebugImageInfoTable.h>
32 #include <Protocol/DebugSupport.h>
33 #include <Protocol/LoadedImage.h>
34
35 /**
36 Simple arm disassembler via a library
37
38 Argv[0] - symboltable
39 Argv[1] - Optional qoted format string
40 Argv[2] - Optional flag
41
42 @param Argc Number of command arguments in Argv
43 @param Argv Array of strings that represent the parsed command line.
44 Argv[0] is the comamnd name
45
46 @return EFI_SUCCESS
47
48 **/
49 EFI_STATUS
50 EblSymbolTable (
51 IN UINTN Argc,
52 IN CHAR8 **Argv
53 )
54 {
55 EFI_STATUS Status;
56 EFI_DEBUG_IMAGE_INFO_TABLE_HEADER *DebugImageTableHeader = NULL;
57 EFI_DEBUG_IMAGE_INFO *DebugTable;
58 UINTN Entry;
59 CHAR8 *Format;
60 CHAR8 *Pdb;
61 UINT32 PeCoffSizeOfHeaders;
62 UINT32 ImageBase;
63 BOOLEAN Elf;
64
65 // Need to add lots of error checking on the passed in string
66 // Default string is for RealView debugger
67 Format = (Argc > 1) ? Argv[1] : "load /a /ni /np %a &0x%x";
68 Elf = (Argc > 2) ? FALSE : TRUE;
69
70 Status = EfiGetSystemConfigurationTable (&gEfiDebugImageInfoTableGuid, (VOID **)&DebugImageTableHeader);
71 if (EFI_ERROR (Status)) {
72 return Status;
73 }
74
75 DebugTable = DebugImageTableHeader->EfiDebugImageInfoTable;
76 if (DebugTable == NULL) {
77 return EFI_SUCCESS;
78 }
79
80 for (Entry = 0; Entry < DebugImageTableHeader->TableSize; Entry++, DebugTable++) {
81 if (DebugTable->NormalImage != NULL) {
82 if ((DebugTable->NormalImage->ImageInfoType == EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL) && (DebugTable->NormalImage->LoadedImageProtocolInstance != NULL)) {
83 ImageBase = (UINT32)DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase;
84 PeCoffSizeOfHeaders = PeCoffGetSizeOfHeaders ((VOID *)(UINTN)ImageBase);
85 Pdb = PeCoffLoaderGetPdbPointer (DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase);
86 if (Pdb != NULL) {
87 if (Elf) {
88 // ELF and Mach-O images don't include the header so the linked address does not include header
89 ImageBase += PeCoffSizeOfHeaders;
90 }
91 AsciiPrint (Format, Pdb, ImageBase);
92 AsciiPrint ("\n");
93 } else {
94 }
95 }
96 }
97 }
98
99 return EFI_SUCCESS;
100 }
101
102
103 /**
104 Simple arm disassembler via a library
105
106 Argv[0] - disasm
107 Argv[1] - Address to start disassembling from
108 ARgv[2] - Number of instructions to disassembly (optional)
109
110 @param Argc Number of command arguments in Argv
111 @param Argv Array of strings that represent the parsed command line.
112 Argv[0] is the comamnd name
113
114 @return EFI_SUCCESS
115
116 **/
117 EFI_STATUS
118 EblDisassembler (
119 IN UINTN Argc,
120 IN CHAR8 **Argv
121 )
122 {
123 UINT8 *Ptr, *CurrentAddress;
124 UINT32 Address;
125 UINT32 Count;
126 CHAR8 Buffer[80];
127 UINT32 ItBlock;
128
129 if (Argc < 2) {
130 return EFI_INVALID_PARAMETER;
131 }
132
133 Address = AsciiStrHexToUintn (Argv[1]);
134 Count = (Argc > 2) ? (UINT32)AsciiStrHexToUintn (Argv[2]) : 20;
135
136 Ptr = (UINT8 *)(UINTN)Address;
137 ItBlock = 0;
138 do {
139 CurrentAddress = Ptr;
140 DisassembleInstruction (&Ptr, TRUE, TRUE, &ItBlock, Buffer, sizeof (Buffer));
141 AsciiPrint ("0x%08x: %a\n", CurrentAddress, Buffer);
142 } while (Count-- > 0);
143
144
145 return EFI_SUCCESS;
146 }
147
148
149 GLOBAL_REMOVE_IF_UNREFERENCED const EBL_COMMAND_TABLE mLibCmdTemplate[] =
150 {
151 {
152 "disasm address [count]",
153 " disassemble count instructions",
154 NULL,
155 EblDisassembler
156 },
157 {
158 "symboltable [\"format string\"] [PECOFF]",
159 " show symbol table commands for debugger",
160 NULL,
161 EblSymbolTable
162 }
163 };
164
165
166 VOID
167 EblInitializeExternalCmd (
168 VOID
169 )
170 {
171 EblAddCommands (mLibCmdTemplate, sizeof (mLibCmdTemplate)/sizeof (EBL_COMMAND_TABLE));
172 return;
173 }