]> git.proxmox.com Git - mirror_edk2.git/blob - BeagleBoardPkg/Library/EblCmdLib/EblCmdLib.c
Adding a command to show how symbols work in EFI
[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 Format = (Argc > 1) ? Argv[1] : "load /a /ni /np %a & 0x%x\n";
67 Elf = (Argc > 2) ? FALSE : TRUE;
68
69 Status = EfiGetSystemConfigurationTable (&gEfiDebugImageInfoTableGuid, (VOID **)&DebugImageTableHeader);
70 if (EFI_ERROR (Status)) {
71 return Status;
72 }
73
74 DebugTable = DebugImageTableHeader->EfiDebugImageInfoTable;
75 if (DebugTable == NULL) {
76 return EFI_SUCCESS;
77 }
78
79 for (Entry = 0; Entry < DebugImageTableHeader->TableSize; Entry++, DebugTable++) {
80 if (DebugTable->NormalImage != NULL) {
81 if ((DebugTable->NormalImage->ImageInfoType == EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL) && (DebugTable->NormalImage->LoadedImageProtocolInstance != NULL)) {
82 ImageBase = (UINT32)DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase;
83 PeCoffSizeOfHeaders = PeCoffGetSizeOfHeaders ((VOID *)(UINTN)ImageBase);
84 Pdb = PeCoffLoaderGetPdbPointer (DebugTable->NormalImage->LoadedImageProtocolInstance->ImageBase);
85 if (Elf) {
86 // ELF and Mach-O images don't include the header so the linked address does not include header
87 ImageBase += PeCoffSizeOfHeaders;
88 }
89 AsciiPrint (Format, Pdb, ImageBase);
90 }
91 }
92 }
93
94 return EFI_SUCCESS;
95 }
96
97
98 /**
99 Simple arm disassembler via a library
100
101 Argv[0] - disasm
102 Argv[1] - Address to start disassembling from
103 ARgv[2] - Number of instructions to disassembly (optional)
104
105 @param Argc Number of command arguments in Argv
106 @param Argv Array of strings that represent the parsed command line.
107 Argv[0] is the comamnd name
108
109 @return EFI_SUCCESS
110
111 **/
112 EFI_STATUS
113 EblDisassembler (
114 IN UINTN Argc,
115 IN CHAR8 **Argv
116 )
117 {
118 UINT8 *Ptr, *CurrentAddress;
119 UINT32 Address;
120 UINT32 Count;
121 CHAR8 Buffer[80];
122 UINT32 ItBlock;
123
124 if (Argc < 2) {
125 return EFI_INVALID_PARAMETER;
126 }
127
128 Address = AsciiStrHexToUintn (Argv[1]);
129 Count = (Argc > 2) ? (UINT32)AsciiStrHexToUintn (Argv[2]) : 20;
130
131 Ptr = (UINT8 *)(UINTN)Address;
132 ItBlock = 0;
133 do {
134 CurrentAddress = Ptr;
135 DisassembleInstruction (&Ptr, TRUE, TRUE, &ItBlock, Buffer, sizeof (Buffer));
136 AsciiPrint ("0x%08x: %a\n", CurrentAddress, Buffer);
137 } while (Count-- > 0);
138
139
140 return EFI_SUCCESS;
141 }
142
143
144 GLOBAL_REMOVE_IF_UNREFERENCED const EBL_COMMAND_TABLE mLibCmdTemplate[] =
145 {
146 {
147 "disasm address [count]",
148 " disassemble count instructions",
149 NULL,
150 EblDisassembler
151 },
152 {
153 "symboltable [\"format string\"] [TRUE]",
154 " show symbol table commands for debugger",
155 NULL,
156 EblSymbolTable
157 }
158 };
159
160
161 VOID
162 EblInitializeExternalCmd (
163 VOID
164 )
165 {
166 EblAddCommands (mLibCmdTemplate, sizeof (mLibCmdTemplate)/sizeof (EBL_COMMAND_TABLE));
167 return;
168 }