2 * Main file supporting the SEC Phase for Versatile Express
4 * Copyright (c) 2011-2014, ARM Limited. All rights reserved.
6 * SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/BaseLib.h>
12 #include <Library/BaseMemoryLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/DebugAgentLib.h>
15 #include <Library/PcdLib.h>
16 #include <Library/PeCoffExtraActionLib.h>
17 #include <Library/PeCoffLib.h>
19 #include <Pi/PiFirmwareFile.h>
20 #include <Pi/PiFirmwareVolume.h>
22 #define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
23 (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))
26 // Vector Table for Sec Phase
28 DebugAgentVectorTable (
33 Returns the highest bit set of the State field
35 @param ErasePolarity Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY
36 in the Attributes field.
37 @param FfsHeader Pointer to FFS File Header
40 @retval the highest bit in the State field
46 IN UINT8 ErasePolarity
,
47 IN EFI_FFS_FILE_HEADER
*FfsHeader
50 EFI_FFS_FILE_STATE FileState
;
51 EFI_FFS_FILE_STATE HighestBit
;
53 FileState
= FfsHeader
->State
;
55 if (ErasePolarity
!= 0) {
56 FileState
= (EFI_FFS_FILE_STATE
)~FileState
;
60 while (HighestBit
!= 0 && (HighestBit
& FileState
) == 0) {
68 Calculates the checksum of the header of a file.
69 The header is a zero byte checksum, so zero means header is good
71 @param FfsHeader Pointer to FFS File Header
73 @retval Checksum of the header
78 CalculateHeaderChecksum (
79 IN EFI_FFS_FILE_HEADER
*FileHeader
84 // Calculate the sum of the header
85 Sum
= CalculateSum8 ((CONST VOID
*)FileHeader
,sizeof(EFI_FFS_FILE_HEADER
));
87 // State field (since this indicates the different state of file).
88 Sum
= (UINT8
)(Sum
- FileHeader
->State
);
90 // Checksum field of the file is not part of the header checksum.
91 Sum
= (UINT8
)(Sum
- FileHeader
->IntegrityCheck
.Checksum
.File
);
98 IN EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
,
99 IN EFI_FV_FILETYPE FileType
,
100 OUT EFI_FFS_FILE_HEADER
**FileHeader
105 EFI_FFS_FILE_HEADER
*FfsFileHeader
;
109 UINT32 FileOccupiedSize
;
111 ASSERT (FwVolHeader
->Signature
== EFI_FVH_SIGNATURE
);
113 FvLength
= FwVolHeader
->FvLength
;
114 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FwVolHeader
+ FwVolHeader
->HeaderLength
);
115 FileOffset
= FwVolHeader
->HeaderLength
;
117 if (FwVolHeader
->Attributes
& EFI_FVB2_ERASE_POLARITY
) {
123 while (FileOffset
< (FvLength
- sizeof (EFI_FFS_FILE_HEADER
))) {
124 // Get FileState which is the highest bit of the State
125 FileState
= GetFileState (ErasePolarity
, FfsFileHeader
);
129 case EFI_FILE_HEADER_INVALID
:
130 FileOffset
+= sizeof(EFI_FFS_FILE_HEADER
);
131 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsFileHeader
+ sizeof(EFI_FFS_FILE_HEADER
));
134 case EFI_FILE_DATA_VALID
:
135 case EFI_FILE_MARKED_FOR_UPDATE
:
136 if (CalculateHeaderChecksum (FfsFileHeader
) != 0) {
138 return EFI_NOT_FOUND
;
141 if (FfsFileHeader
->Type
== FileType
) {
142 *FileHeader
= FfsFileHeader
;
146 FileLength
= *(UINT32
*)(FfsFileHeader
->Size
) & 0x00FFFFFF;
147 FileOccupiedSize
= GET_OCCUPIED_SIZE(FileLength
, 8);
149 FileOffset
+= FileOccupiedSize
;
150 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsFileHeader
+ FileOccupiedSize
);
153 case EFI_FILE_DELETED
:
154 FileLength
= *(UINT32
*)(FfsFileHeader
->Size
) & 0x00FFFFFF;
155 FileOccupiedSize
= GET_OCCUPIED_SIZE(FileLength
, 8);
156 FileOffset
+= FileOccupiedSize
;
157 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsFileHeader
+ FileOccupiedSize
);
161 return EFI_NOT_FOUND
;
164 return EFI_NOT_FOUND
;
169 IN EFI_FFS_FILE_HEADER
*FfsHeader
,
170 OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
177 EFI_COMMON_SECTION_HEADER
*Section
;
180 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
181 VOID
*CodeViewEntryPointer
;
183 Section
= (EFI_COMMON_SECTION_HEADER
*)(FfsHeader
+ 1);
184 SectionSize
= *(UINT32
*)(FfsHeader
->Size
) & 0x00FFFFFF;
185 SectionSize
-= sizeof (EFI_FFS_FILE_HEADER
);
189 while (ParsedLength
< SectionSize
) {
190 if ((Section
->Type
== EFI_SECTION_PE32
) || (Section
->Type
== EFI_SECTION_TE
)) {
191 EfiImage
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(Section
+ 1);
196 // Size is 24 bits wide so mask upper 8 bits.
197 // SectionLength is adjusted it is 4 byte aligned.
198 // Go to the next section
200 SectionLength
= *(UINT32
*)Section
->Size
& 0x00FFFFFF;
201 SectionLength
= GET_OCCUPIED_SIZE (SectionLength
, 4);
202 ASSERT (SectionLength
!= 0);
203 ParsedLength
+= SectionLength
;
204 Section
= (EFI_COMMON_SECTION_HEADER
*)((UINT8
*)Section
+ SectionLength
);
207 if (EfiImage
== NULL
) {
208 return EFI_NOT_FOUND
;
211 // Initialize the Image Context
212 ZeroMem (ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
213 ImageContext
->Handle
= EfiImage
;
214 ImageContext
->ImageRead
= PeCoffLoaderImageReadFromMemory
;
216 Status
= PeCoffLoaderGetImageInfo (ImageContext
);
217 if (!EFI_ERROR(Status
) && ((VOID
*)(UINTN
)ImageContext
->DebugDirectoryEntryRva
!= NULL
)) {
218 ImageAddress
= ImageContext
->ImageAddress
;
219 if (ImageContext
->IsTeImage
) {
220 ImageAddress
+= sizeof (EFI_TE_IMAGE_HEADER
) - ((EFI_TE_IMAGE_HEADER
*)EfiImage
)->StrippedSize
;
223 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(ImageAddress
+ ImageContext
->DebugDirectoryEntryRva
);
224 if (DebugEntry
->Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
225 CodeViewEntryPointer
= (VOID
*) (ImageAddress
+ (UINTN
) DebugEntry
->RVA
);
226 switch (* (UINT32
*) CodeViewEntryPointer
) {
227 case CODEVIEW_SIGNATURE_NB10
:
228 ImageContext
->PdbPointer
= (CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
230 case CODEVIEW_SIGNATURE_RSDS
:
231 ImageContext
->PdbPointer
= (CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
233 case CODEVIEW_SIGNATURE_MTOC
:
234 ImageContext
->PdbPointer
= (CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY
);
246 Initialize debug agent.
248 This function is used to set up debug environment to support source level debugging.
249 If certain Debug Agent Library instance has to save some private data in the stack,
250 this function must work on the mode that doesn't return to the caller, then
251 the caller needs to wrap up all rest of logic after InitializeDebugAgent() into one
252 function and pass it into InitializeDebugAgent(). InitializeDebugAgent() is
253 responsible to invoke the passing-in function at the end of InitializeDebugAgent().
255 If the parameter Function is not NULL, Debug Agent Library instance will invoke it by
256 passing in the Context to be its parameter.
258 If Function() is NULL, Debug Agent Library instance will return after setup debug
261 @param[in] InitFlag Init flag is used to decide the initialize process.
262 @param[in] Context Context needed according to InitFlag; it was optional.
263 @param[in] Function Continue function called by debug agent library; it was
269 InitializeDebugAgent (
271 IN VOID
*Context
, OPTIONAL
272 IN DEBUG_AGENT_CONTINUE Function OPTIONAL
276 EFI_FFS_FILE_HEADER
*FfsHeader
;
277 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
279 // We use InitFlag to know if DebugAgent has been initialized from
280 // Sec (DEBUG_AGENT_INIT_PREMEM_SEC) or PrePi (DEBUG_AGENT_INIT_POSTMEM_SEC)
282 if (InitFlag
== DEBUG_AGENT_INIT_PREMEM_SEC
) {
284 // Get the Sec or PrePeiCore module (defined as SEC type module)
286 Status
= GetFfsFile ((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)PcdGet64 (PcdSecureFvBaseAddress
), EFI_FV_FILETYPE_SECURITY_CORE
, &FfsHeader
);
287 if (!EFI_ERROR(Status
)) {
288 Status
= GetImageContext (FfsHeader
,&ImageContext
);
289 if (!EFI_ERROR(Status
)) {
290 PeCoffLoaderRelocateImageExtraAction (&ImageContext
);
293 } else if (InitFlag
== DEBUG_AGENT_INIT_POSTMEM_SEC
) {
295 // Get the PrePi or PrePeiCore module (defined as SEC type module)
297 Status
= GetFfsFile ((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)PcdGet64 (PcdFvBaseAddress
), EFI_FV_FILETYPE_SECURITY_CORE
, &FfsHeader
);
298 if (!EFI_ERROR(Status
)) {
299 Status
= GetImageContext (FfsHeader
,&ImageContext
);
300 if (!EFI_ERROR(Status
)) {
301 PeCoffLoaderRelocateImageExtraAction (&ImageContext
);
306 // Get the PeiCore module (defined as PEI_CORE type module)
308 Status
= GetFfsFile ((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)PcdGet64 (PcdFvBaseAddress
), EFI_FV_FILETYPE_PEI_CORE
, &FfsHeader
);
309 if (!EFI_ERROR(Status
)) {
310 Status
= GetImageContext (FfsHeader
,&ImageContext
);
311 if (!EFI_ERROR(Status
)) {
312 PeCoffLoaderRelocateImageExtraAction (&ImageContext
);
319 Enable/Disable the interrupt of debug timer and return the interrupt state
320 prior to the operation.
322 If EnableStatus is TRUE, enable the interrupt of debug timer.
323 If EnableStatus is FALSE, disable the interrupt of debug timer.
325 @param[in] EnableStatus Enable/Disable.
327 @return FALSE always.
332 SaveAndSetDebugTimerInterrupt (
333 IN BOOLEAN EnableStatus