2 * Main file supporting the SEC Phase for Versatile Express
4 * Copyright (c) 2011-2012, ARM Limited. All rights reserved.
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
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.
17 #include <Library/ArmLib.h>
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/DebugAgentLib.h>
22 #include <Library/PcdLib.h>
23 #include <Library/PeCoffExtraActionLib.h>
24 #include <Library/PeCoffLib.h>
26 #include <Pi/PiFirmwareFile.h>
27 #include <Pi/PiFirmwareVolume.h>
29 #define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
30 (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))
33 // Vector Table for Sec Phase
35 DebugAgentVectorTable (
40 Returns the highest bit set of the State field
42 @param ErasePolarity Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY
43 in the Attributes field.
44 @param FfsHeader Pointer to FFS File Header
47 @retval the highest bit in the State field
53 IN UINT8 ErasePolarity
,
54 IN EFI_FFS_FILE_HEADER
*FfsHeader
57 EFI_FFS_FILE_STATE FileState
;
58 EFI_FFS_FILE_STATE HighestBit
;
60 FileState
= FfsHeader
->State
;
62 if (ErasePolarity
!= 0) {
63 FileState
= (EFI_FFS_FILE_STATE
)~FileState
;
67 while (HighestBit
!= 0 && (HighestBit
& FileState
) == 0) {
75 Calculates the checksum of the header of a file.
76 The header is a zero byte checksum, so zero means header is good
78 @param FfsHeader Pointer to FFS File Header
80 @retval Checksum of the header
85 CalculateHeaderChecksum (
86 IN EFI_FFS_FILE_HEADER
*FileHeader
91 // Calculate the sum of the header
92 Sum
= CalculateSum8 ((CONST VOID
*)FileHeader
,sizeof(EFI_FFS_FILE_HEADER
));
94 // State field (since this indicates the different state of file).
95 Sum
= (UINT8
)(Sum
- FileHeader
->State
);
97 // Checksum field of the file is not part of the header checksum.
98 Sum
= (UINT8
)(Sum
- FileHeader
->IntegrityCheck
.Checksum
.File
);
105 IN EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
,
106 IN EFI_FV_FILETYPE FileType
,
107 OUT EFI_FFS_FILE_HEADER
**FileHeader
112 EFI_FFS_FILE_HEADER
*FfsFileHeader
;
116 UINT32 FileOccupiedSize
;
118 ASSERT (FwVolHeader
->Signature
== EFI_FVH_SIGNATURE
);
120 FvLength
= FwVolHeader
->FvLength
;
121 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FwVolHeader
+ FwVolHeader
->HeaderLength
);
122 FileOffset
= FwVolHeader
->HeaderLength
;
124 if (FwVolHeader
->Attributes
& EFI_FVB2_ERASE_POLARITY
) {
130 while (FileOffset
< (FvLength
- sizeof (EFI_FFS_FILE_HEADER
))) {
131 // Get FileState which is the highest bit of the State
132 FileState
= GetFileState (ErasePolarity
, FfsFileHeader
);
136 case EFI_FILE_HEADER_INVALID
:
137 FileOffset
+= sizeof(EFI_FFS_FILE_HEADER
);
138 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsFileHeader
+ sizeof(EFI_FFS_FILE_HEADER
));
141 case EFI_FILE_DATA_VALID
:
142 case EFI_FILE_MARKED_FOR_UPDATE
:
143 if (CalculateHeaderChecksum (FfsFileHeader
) != 0) {
145 return EFI_NOT_FOUND
;
148 if (FfsFileHeader
->Type
== FileType
) {
149 *FileHeader
= FfsFileHeader
;
153 FileLength
= *(UINT32
*)(FfsFileHeader
->Size
) & 0x00FFFFFF;
154 FileOccupiedSize
= GET_OCCUPIED_SIZE(FileLength
, 8);
156 FileOffset
+= FileOccupiedSize
;
157 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsFileHeader
+ FileOccupiedSize
);
160 case EFI_FILE_DELETED
:
161 FileLength
= *(UINT32
*)(FfsFileHeader
->Size
) & 0x00FFFFFF;
162 FileOccupiedSize
= GET_OCCUPIED_SIZE(FileLength
, 8);
163 FileOffset
+= FileOccupiedSize
;
164 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsFileHeader
+ FileOccupiedSize
);
168 return EFI_NOT_FOUND
;
171 return EFI_NOT_FOUND
;
176 IN EFI_FFS_FILE_HEADER
*FfsHeader
,
177 OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
184 EFI_COMMON_SECTION_HEADER
*Section
;
187 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
188 VOID
*CodeViewEntryPointer
;
190 Section
= (EFI_COMMON_SECTION_HEADER
*)(FfsHeader
+ 1);
191 SectionSize
= *(UINT32
*)(FfsHeader
->Size
) & 0x00FFFFFF;
192 SectionSize
-= sizeof (EFI_FFS_FILE_HEADER
);
196 while (ParsedLength
< SectionSize
) {
197 if ((Section
->Type
== EFI_SECTION_PE32
) || (Section
->Type
== EFI_SECTION_TE
)) {
198 EfiImage
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(Section
+ 1);
203 // Size is 24 bits wide so mask upper 8 bits.
204 // SectionLength is adjusted it is 4 byte aligned.
205 // Go to the next section
207 SectionLength
= *(UINT32
*)Section
->Size
& 0x00FFFFFF;
208 SectionLength
= GET_OCCUPIED_SIZE (SectionLength
, 4);
209 ASSERT (SectionLength
!= 0);
210 ParsedLength
+= SectionLength
;
211 Section
= (EFI_COMMON_SECTION_HEADER
*)((UINT8
*)Section
+ SectionLength
);
214 if (EfiImage
== NULL
) {
215 return EFI_NOT_FOUND
;
218 // Initialize the Image Context
219 ZeroMem (ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
220 ImageContext
->Handle
= EfiImage
;
221 ImageContext
->ImageRead
= PeCoffLoaderImageReadFromMemory
;
223 Status
= PeCoffLoaderGetImageInfo (ImageContext
);
224 if (!EFI_ERROR(Status
) && ((VOID
*)(UINTN
)ImageContext
->DebugDirectoryEntryRva
!= NULL
)) {
225 ImageAddress
= ImageContext
->ImageAddress
;
226 if (ImageContext
->IsTeImage
) {
227 ImageAddress
+= sizeof (EFI_TE_IMAGE_HEADER
) - ((EFI_TE_IMAGE_HEADER
*)EfiImage
)->StrippedSize
;
230 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(ImageAddress
+ ImageContext
->DebugDirectoryEntryRva
);
231 if (DebugEntry
->Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
232 CodeViewEntryPointer
= (VOID
*) (ImageAddress
+ (UINTN
) DebugEntry
->RVA
);
233 switch (* (UINT32
*) CodeViewEntryPointer
) {
234 case CODEVIEW_SIGNATURE_NB10
:
235 ImageContext
->PdbPointer
= (CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
237 case CODEVIEW_SIGNATURE_RSDS
:
238 ImageContext
->PdbPointer
= (CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
240 case CODEVIEW_SIGNATURE_MTOC
:
241 ImageContext
->PdbPointer
= (CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY
);
253 Initialize debug agent.
255 This function is used to set up debug environment to support source level debugging.
256 If certain Debug Agent Library instance has to save some private data in the stack,
257 this function must work on the mode that doesn't return to the caller, then
258 the caller needs to wrap up all rest of logic after InitializeDebugAgent() into one
259 function and pass it into InitializeDebugAgent(). InitializeDebugAgent() is
260 responsible to invoke the passing-in function at the end of InitializeDebugAgent().
262 If the parameter Function is not NULL, Debug Agent Libary instance will invoke it by
263 passing in the Context to be its parameter.
265 If Function() is NULL, Debug Agent Library instance will return after setup debug
268 @param[in] InitFlag Init flag is used to decide the initialize process.
269 @param[in] Context Context needed according to InitFlag; it was optional.
270 @param[in] Function Continue function called by debug agent library; it was
276 InitializeDebugAgent (
278 IN VOID
*Context
, OPTIONAL
279 IN DEBUG_AGENT_CONTINUE Function OPTIONAL
283 EFI_FFS_FILE_HEADER
*FfsHeader
;
284 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
286 // Now we've got UART, make the check:
287 // - The Vector table must be 32-byte aligned
288 //Need to fix basetools ASSERT(((UINTN)DebugAgentVectorTable & ARM_VECTOR_TABLE_ALIGNMENT) == 0);
289 ArmWriteVBar ((UINTN
)DebugAgentVectorTable
);
291 // We use InitFlag to know if DebugAgent has been intialized from
292 // Sec (DEBUG_AGENT_INIT_PREMEM_SEC) or PrePi (DEBUG_AGENT_INIT_POSTMEM_SEC)
294 if (InitFlag
== DEBUG_AGENT_INIT_PREMEM_SEC
) {
296 // Get the Sec or PrePeiCore module (defined as SEC type module)
298 Status
= GetFfsFile ((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)PcdGet32(PcdSecureFvBaseAddress
), EFI_FV_FILETYPE_SECURITY_CORE
, &FfsHeader
);
299 if (!EFI_ERROR(Status
)) {
300 Status
= GetImageContext (FfsHeader
,&ImageContext
);
301 if (!EFI_ERROR(Status
)) {
302 PeCoffLoaderRelocateImageExtraAction (&ImageContext
);
305 } else if (InitFlag
== DEBUG_AGENT_INIT_POSTMEM_SEC
) {
307 // Get the PrePi or PrePeiCore module (defined as SEC type module)
309 Status
= GetFfsFile ((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)PcdGet32(PcdFvBaseAddress
), EFI_FV_FILETYPE_SECURITY_CORE
, &FfsHeader
);
310 if (!EFI_ERROR(Status
)) {
311 Status
= GetImageContext (FfsHeader
,&ImageContext
);
312 if (!EFI_ERROR(Status
)) {
313 PeCoffLoaderRelocateImageExtraAction (&ImageContext
);
318 // Get the PeiCore module (defined as PEI_CORE type module)
320 Status
= GetFfsFile ((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)PcdGet32(PcdFvBaseAddress
), EFI_FV_FILETYPE_PEI_CORE
, &FfsHeader
);
321 if (!EFI_ERROR(Status
)) {
322 Status
= GetImageContext (FfsHeader
,&ImageContext
);
323 if (!EFI_ERROR(Status
)) {
324 PeCoffLoaderRelocateImageExtraAction (&ImageContext
);
331 Enable/Disable the interrupt of debug timer and return the interrupt state
332 prior to the operation.
334 If EnableStatus is TRUE, enable the interrupt of debug timer.
335 If EnableStatus is FALSE, disable the interrupt of debug timer.
337 @param[in] EnableStatus Enable/Disable.
339 @return FALSE always.
344 SaveAndSetDebugTimerInterrupt (
345 IN BOOLEAN EnableStatus