]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/DebugAgentSymbolsBaseLib/DebugAgentSymbolsBaseLib.c
f47f4250d1e8d664053646f5eec4cb2ef6ee13ae
[mirror_edk2.git] / ArmPkg / Library / DebugAgentSymbolsBaseLib / DebugAgentSymbolsBaseLib.c
1 /** @file
2 * Main file supporting the SEC Phase for Versatile Express
3 *
4 * Copyright (c) 2011-2014, ARM Limited. All rights reserved.
5 *
6 * 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 <Uefi.h>
17 #include <Library/BaseLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/DebugAgentLib.h>
21 #include <Library/PcdLib.h>
22 #include <Library/PeCoffExtraActionLib.h>
23 #include <Library/PeCoffLib.h>
24
25 #include <Pi/PiFirmwareFile.h>
26 #include <Pi/PiFirmwareVolume.h>
27
28 #define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
29 (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))
30
31
32 // Vector Table for Sec Phase
33 VOID
34 DebugAgentVectorTable (
35 VOID
36 );
37
38 /**
39 Returns the highest bit set of the State field
40
41 @param ErasePolarity Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY
42 in the Attributes field.
43 @param FfsHeader Pointer to FFS File Header
44
45
46 @retval the highest bit in the State field
47
48 **/
49 STATIC
50 EFI_FFS_FILE_STATE
51 GetFileState (
52 IN UINT8 ErasePolarity,
53 IN EFI_FFS_FILE_HEADER *FfsHeader
54 )
55 {
56 EFI_FFS_FILE_STATE FileState;
57 EFI_FFS_FILE_STATE HighestBit;
58
59 FileState = FfsHeader->State;
60
61 if (ErasePolarity != 0) {
62 FileState = (EFI_FFS_FILE_STATE)~FileState;
63 }
64
65 HighestBit = 0x80;
66 while (HighestBit != 0 && (HighestBit & FileState) == 0) {
67 HighestBit >>= 1;
68 }
69
70 return HighestBit;
71 }
72
73 /**
74 Calculates the checksum of the header of a file.
75 The header is a zero byte checksum, so zero means header is good
76
77 @param FfsHeader Pointer to FFS File Header
78
79 @retval Checksum of the header
80
81 **/
82 STATIC
83 UINT8
84 CalculateHeaderChecksum (
85 IN EFI_FFS_FILE_HEADER *FileHeader
86 )
87 {
88 UINT8 Sum;
89
90 // Calculate the sum of the header
91 Sum = CalculateSum8 ((CONST VOID*)FileHeader,sizeof(EFI_FFS_FILE_HEADER));
92
93 // State field (since this indicates the different state of file).
94 Sum = (UINT8)(Sum - FileHeader->State);
95
96 // Checksum field of the file is not part of the header checksum.
97 Sum = (UINT8)(Sum - FileHeader->IntegrityCheck.Checksum.File);
98
99 return Sum;
100 }
101
102 EFI_STATUS
103 GetFfsFile (
104 IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader,
105 IN EFI_FV_FILETYPE FileType,
106 OUT EFI_FFS_FILE_HEADER **FileHeader
107 )
108 {
109 UINT64 FvLength;
110 UINTN FileOffset;
111 EFI_FFS_FILE_HEADER *FfsFileHeader;
112 UINT8 ErasePolarity;
113 UINT8 FileState;
114 UINT32 FileLength;
115 UINT32 FileOccupiedSize;
116
117 ASSERT (FwVolHeader->Signature == EFI_FVH_SIGNATURE);
118
119 FvLength = FwVolHeader->FvLength;
120 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FwVolHeader + FwVolHeader->HeaderLength);
121 FileOffset = FwVolHeader->HeaderLength;
122
123 if (FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {
124 ErasePolarity = 1;
125 } else {
126 ErasePolarity = 0;
127 }
128
129 while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {
130 // Get FileState which is the highest bit of the State
131 FileState = GetFileState (ErasePolarity, FfsFileHeader);
132
133 switch (FileState) {
134
135 case EFI_FILE_HEADER_INVALID:
136 FileOffset += sizeof(EFI_FFS_FILE_HEADER);
137 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof(EFI_FFS_FILE_HEADER));
138 break;
139
140 case EFI_FILE_DATA_VALID:
141 case EFI_FILE_MARKED_FOR_UPDATE:
142 if (CalculateHeaderChecksum (FfsFileHeader) != 0) {
143 ASSERT (FALSE);
144 return EFI_NOT_FOUND;
145 }
146
147 if (FfsFileHeader->Type == FileType) {
148 *FileHeader = FfsFileHeader;
149 return EFI_SUCCESS;
150 }
151
152 FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
153 FileOccupiedSize = GET_OCCUPIED_SIZE(FileLength, 8);
154
155 FileOffset += FileOccupiedSize;
156 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
157 break;
158
159 case EFI_FILE_DELETED:
160 FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
161 FileOccupiedSize = GET_OCCUPIED_SIZE(FileLength, 8);
162 FileOffset += FileOccupiedSize;
163 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
164 break;
165
166 default:
167 return EFI_NOT_FOUND;
168 }
169 }
170 return EFI_NOT_FOUND;
171 }
172
173 EFI_STATUS
174 GetImageContext (
175 IN EFI_FFS_FILE_HEADER *FfsHeader,
176 OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
177 )
178 {
179 EFI_STATUS Status;
180 UINTN ParsedLength;
181 UINTN SectionSize;
182 UINTN SectionLength;
183 EFI_COMMON_SECTION_HEADER *Section;
184 VOID *EfiImage;
185 UINTN ImageAddress;
186 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry;
187 VOID *CodeViewEntryPointer;
188
189 Section = (EFI_COMMON_SECTION_HEADER *)(FfsHeader + 1);
190 SectionSize = *(UINT32 *)(FfsHeader->Size) & 0x00FFFFFF;
191 SectionSize -= sizeof (EFI_FFS_FILE_HEADER);
192 ParsedLength = 0;
193 EfiImage = NULL;
194
195 while (ParsedLength < SectionSize) {
196 if ((Section->Type == EFI_SECTION_PE32) || (Section->Type == EFI_SECTION_TE)) {
197 EfiImage = (EFI_IMAGE_OPTIONAL_HEADER_UNION*)(Section + 1);
198 break;
199 }
200
201 //
202 // Size is 24 bits wide so mask upper 8 bits.
203 // SectionLength is adjusted it is 4 byte aligned.
204 // Go to the next section
205 //
206 SectionLength = *(UINT32 *)Section->Size & 0x00FFFFFF;
207 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
208 ASSERT (SectionLength != 0);
209 ParsedLength += SectionLength;
210 Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);
211 }
212
213 if (EfiImage == NULL) {
214 return EFI_NOT_FOUND;
215 }
216
217 // Initialize the Image Context
218 ZeroMem (ImageContext, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT));
219 ImageContext->Handle = EfiImage;
220 ImageContext->ImageRead = PeCoffLoaderImageReadFromMemory;
221
222 Status = PeCoffLoaderGetImageInfo (ImageContext);
223 if (!EFI_ERROR(Status) && ((VOID*)(UINTN)ImageContext->DebugDirectoryEntryRva != NULL)) {
224 ImageAddress = ImageContext->ImageAddress;
225 if (ImageContext->IsTeImage) {
226 ImageAddress += sizeof (EFI_TE_IMAGE_HEADER) - ((EFI_TE_IMAGE_HEADER*)EfiImage)->StrippedSize;
227 }
228
229 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(ImageAddress + ImageContext->DebugDirectoryEntryRva);
230 if (DebugEntry->Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {
231 CodeViewEntryPointer = (VOID *) (ImageAddress + (UINTN) DebugEntry->RVA);
232 switch (* (UINT32 *) CodeViewEntryPointer) {
233 case CODEVIEW_SIGNATURE_NB10:
234 ImageContext->PdbPointer = (CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY);
235 break;
236 case CODEVIEW_SIGNATURE_RSDS:
237 ImageContext->PdbPointer = (CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY);
238 break;
239 case CODEVIEW_SIGNATURE_MTOC:
240 ImageContext->PdbPointer = (CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY);
241 break;
242 default:
243 break;
244 }
245 }
246 }
247
248 return Status;
249 }
250
251 /**
252 Initialize debug agent.
253
254 This function is used to set up debug environment to support source level debugging.
255 If certain Debug Agent Library instance has to save some private data in the stack,
256 this function must work on the mode that doesn't return to the caller, then
257 the caller needs to wrap up all rest of logic after InitializeDebugAgent() into one
258 function and pass it into InitializeDebugAgent(). InitializeDebugAgent() is
259 responsible to invoke the passing-in function at the end of InitializeDebugAgent().
260
261 If the parameter Function is not NULL, Debug Agent Library instance will invoke it by
262 passing in the Context to be its parameter.
263
264 If Function() is NULL, Debug Agent Library instance will return after setup debug
265 environment.
266
267 @param[in] InitFlag Init flag is used to decide the initialize process.
268 @param[in] Context Context needed according to InitFlag; it was optional.
269 @param[in] Function Continue function called by debug agent library; it was
270 optional.
271
272 **/
273 VOID
274 EFIAPI
275 InitializeDebugAgent (
276 IN UINT32 InitFlag,
277 IN VOID *Context, OPTIONAL
278 IN DEBUG_AGENT_CONTINUE Function OPTIONAL
279 )
280 {
281 EFI_STATUS Status;
282 EFI_FFS_FILE_HEADER *FfsHeader;
283 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
284
285 // We use InitFlag to know if DebugAgent has been initialized from
286 // Sec (DEBUG_AGENT_INIT_PREMEM_SEC) or PrePi (DEBUG_AGENT_INIT_POSTMEM_SEC)
287 // modules
288 if (InitFlag == DEBUG_AGENT_INIT_PREMEM_SEC) {
289 //
290 // Get the Sec or PrePeiCore module (defined as SEC type module)
291 //
292 Status = GetFfsFile ((EFI_FIRMWARE_VOLUME_HEADER*)(UINTN)PcdGet64 (PcdSecureFvBaseAddress), EFI_FV_FILETYPE_SECURITY_CORE, &FfsHeader);
293 if (!EFI_ERROR(Status)) {
294 Status = GetImageContext (FfsHeader,&ImageContext);
295 if (!EFI_ERROR(Status)) {
296 PeCoffLoaderRelocateImageExtraAction (&ImageContext);
297 }
298 }
299 } else if (InitFlag == DEBUG_AGENT_INIT_POSTMEM_SEC) {
300 //
301 // Get the PrePi or PrePeiCore module (defined as SEC type module)
302 //
303 Status = GetFfsFile ((EFI_FIRMWARE_VOLUME_HEADER*)(UINTN)PcdGet64 (PcdFvBaseAddress), EFI_FV_FILETYPE_SECURITY_CORE, &FfsHeader);
304 if (!EFI_ERROR(Status)) {
305 Status = GetImageContext (FfsHeader,&ImageContext);
306 if (!EFI_ERROR(Status)) {
307 PeCoffLoaderRelocateImageExtraAction (&ImageContext);
308 }
309 }
310
311 //
312 // Get the PeiCore module (defined as PEI_CORE type module)
313 //
314 Status = GetFfsFile ((EFI_FIRMWARE_VOLUME_HEADER*)(UINTN)PcdGet64 (PcdFvBaseAddress), EFI_FV_FILETYPE_PEI_CORE, &FfsHeader);
315 if (!EFI_ERROR(Status)) {
316 Status = GetImageContext (FfsHeader,&ImageContext);
317 if (!EFI_ERROR(Status)) {
318 PeCoffLoaderRelocateImageExtraAction (&ImageContext);
319 }
320 }
321 }
322 }
323
324 /**
325 Enable/Disable the interrupt of debug timer and return the interrupt state
326 prior to the operation.
327
328 If EnableStatus is TRUE, enable the interrupt of debug timer.
329 If EnableStatus is FALSE, disable the interrupt of debug timer.
330
331 @param[in] EnableStatus Enable/Disable.
332
333 @return FALSE always.
334
335 **/
336 BOOLEAN
337 EFIAPI
338 SaveAndSetDebugTimerInterrupt (
339 IN BOOLEAN EnableStatus
340 )
341 {
342 return FALSE;
343 }
344