TianoCore EDK2 master
Loading...
Searching...
No Matches
PrePi.c
Go to the documentation of this file.
1
9#include <PiPei.h>
10#include <Pi/PiBootMode.h>
11
12#include <Library/PeCoffLib.h>
13#include <Library/PrePiLib.h>
14#include <Library/PrintLib.h>
16#include <Library/TimerLib.h>
19
21#include <Ppi/ArmMpCoreInfo.h>
22
23#include "PrePi.h"
24
25VOID
26PrePiMain (
27 IN UINTN UefiMemoryBase,
28 IN UINTN StacksBase,
29 IN UINT64 StartTimeStamp
30 )
31{
33 EFI_STATUS Status;
34 CHAR8 Buffer[100];
35 UINTN CharCount;
36 UINTN StacksSize;
37
38 // Initialize the architecture specific bits
40
41 // Declare the PI/UEFI memory region
42 HobList = HobConstructor (
43 (VOID *)UefiMemoryBase,
44 FixedPcdGet32 (PcdSystemMemoryUefiRegionSize),
45 (VOID *)UefiMemoryBase,
46 (VOID *)StacksBase // The top of the UEFI Memory is reserved for the stacks
47 );
48 PrePeiSetHobList (HobList);
49
50 //
51 // Ensure that the loaded image is invalidated in the caches, so that any
52 // modifications we made with the caches and MMU off (such as the applied
53 // relocations) don't become invisible once we turn them on.
54 //
55 InvalidateDataCacheRange ((VOID *)(UINTN)PcdGet64 (PcdFdBaseAddress), PcdGet32 (PcdFdSize));
56
57 // SEC phase needs to run library constructors by hand.
58 ProcessLibraryConstructorList ();
59
60 // Initialize MMU and Memory HOBs (Resource Descriptor HOBs)
61 Status = MemoryPeim (UefiMemoryBase, FixedPcdGet32 (PcdSystemMemoryUefiRegionSize));
62 ASSERT_EFI_ERROR (Status);
63
64 // Initialize the Serial Port
66 CharCount = AsciiSPrint (
67 Buffer,
68 sizeof (Buffer),
69 "UEFI firmware (version %s built at %a on %a)\n\r",
70 (CHAR16 *)PcdGetPtr (PcdFirmwareVersionString),
71 __TIME__,
72 __DATE__
73 );
74 SerialPortWrite ((UINT8 *)Buffer, CharCount);
75
76 // Create the Stacks HOB (reserve the memory for all stacks)
77 StacksSize = PcdGet32 (PcdCPUCorePrimaryStackSize);
78 BuildStackHob (StacksBase, StacksSize);
79
80 // TODO: Call CpuPei as a library
81 BuildCpuHob (ArmGetPhysicalAddressBits (), PcdGet8 (PcdPrePiCpuIoSize));
82
83 // Set the Boot Mode
84 SetBootMode (BOOT_WITH_FULL_CONFIGURATION);
85
86 // Initialize Platform HOBs (CpuHob and FvHob)
87 Status = PlatformPeim ();
88 ASSERT_EFI_ERROR (Status);
89
90 // Now, the HOB List has been initialized, we can register performance information
91 PERF_START (NULL, "PEI", NULL, StartTimeStamp);
92
93 // Assume the FV that contains the SEC (our code) also contains a compressed FV.
94 Status = DecompressFirstFv ();
95 ASSERT_EFI_ERROR (Status);
96
97 // Load the DXE Core and transfer control to it
98 Status = LoadDxeCoreFromFv (NULL, SIZE_128KB);
99 ASSERT_EFI_ERROR (Status);
100}
101
102VOID
104 IN UINTN MpId,
105 IN UINTN UefiMemoryBase,
106 IN UINTN StacksBase
107 )
108{
109 UINT64 StartTimeStamp;
110
112 // We cannot call yet the PerformanceLib because the HOB List has not been initialized
113 StartTimeStamp = GetPerformanceCounter ();
114 } else {
115 StartTimeStamp = 0;
116 }
117
118 // Data Cache enabled on Primary core when MMU is enabled.
119 ArmDisableDataCache ();
120 // Invalidate instruction cache
121 ArmInvalidateInstructionCache ();
122 // Enable Instruction Caches on all cores.
123 ArmEnableInstructionCache ();
124
125 PrePiMain (UefiMemoryBase, StacksBase, StartTimeStamp);
126
127 // DXE Core should always load and never return
128 ASSERT (FALSE);
129}
130
131VOID
132RelocatePeCoffImage (
133 IN EFI_PEI_FV_HANDLE FwVolHeader,
135 )
136{
137 EFI_PEI_FILE_HANDLE FileHandle;
138 VOID *SectionData;
139 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
140 EFI_STATUS Status;
141
142 FileHandle = NULL;
143 Status = FfsFindNextFile (
144 EFI_FV_FILETYPE_SECURITY_CORE,
145 FwVolHeader,
146 &FileHandle
147 );
148 ASSERT_EFI_ERROR (Status);
149
150 Status = FfsFindSectionData (EFI_SECTION_PE32, FileHandle, &SectionData);
151 if (EFI_ERROR (Status)) {
152 Status = FfsFindSectionData (EFI_SECTION_TE, FileHandle, &SectionData);
153 }
154
155 ASSERT_EFI_ERROR (Status);
156
157 ZeroMem (&ImageContext, sizeof ImageContext);
158
159 ImageContext.Handle = (EFI_HANDLE)SectionData;
160 ImageContext.ImageRead = ImageRead;
161 PeCoffLoaderGetImageInfo (&ImageContext);
162
163 if (ImageContext.ImageAddress != (UINTN)SectionData) {
164 ImageContext.ImageAddress = (UINTN)SectionData;
165 PeCoffLoaderRelocateImage (&ImageContext);
166 }
167}
VOID ArchInitialize(VOID)
UINT64 UINTN
UINT64 EFIAPI GetPerformanceCounter(VOID)
VOID *EFIAPI InvalidateDataCacheRange(IN VOID *Address, IN UINTN Length)
RETURN_STATUS EFIAPI SerialPortInitialize(VOID)
Definition: SerialPortLib.c:25
UINTN EFIAPI SerialPortWrite(IN UINT8 *Buffer, IN UINTN NumberOfBytes)
Definition: SerialPortLib.c:52
EFI_STATUS EFIAPI PrePeiSetHobList(IN VOID *HobList)
VOID EFIAPI BuildCpuHob(IN UINT8 SizeOfMemorySpace, IN UINT8 SizeOfIoSpace)
Definition: HobLib.c:520
VOID EFIAPI BuildStackHob(IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length)
Definition: HobLib.c:546
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
BOOLEAN EFIAPI PerformanceMeasurementEnabled(VOID)
UINTN EFIAPI AsciiSPrint(OUT CHAR8 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR8 *FormatString,...)
Definition: PrintLib.c:813
#define NULL
Definition: Base.h:319
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
EFI_STATUS EFIAPI MemoryPeim(IN EFI_PHYSICAL_ADDRESS UefiMemoryBase, IN UINT64 UefiMemorySize)
#define PcdGet64(TokenName)
Definition: PcdLib.h:375
#define FixedPcdGet32(TokenName)
Definition: PcdLib.h:92
#define PcdGet8(TokenName)
Definition: PcdLib.h:336
#define PcdGet32(TokenName)
Definition: PcdLib.h:362
#define PcdGetPtr(TokenName)
Definition: PcdLib.h:388
RETURN_STATUS EFIAPI PeCoffLoaderRelocateImage(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
Definition: BasePeCoff.c:956
RETURN_STATUS(EFIAPI * PE_COFF_LOADER_READ_FILE)(IN VOID *FileHandle, IN UINTN FileOffset, IN OUT UINTN *ReadSize, OUT VOID *Buffer)
Definition: PeCoffLib.h:65
RETURN_STATUS EFIAPI PeCoffLoaderGetImageInfo(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
Definition: BasePeCoff.c:577
VOID CEntryPoint(IN UINTN UefiMemoryBase, IN UINTN StackBase)
Definition: PeilessSec.c:172
EFI_STATUS EFIAPI PlatformPeim(VOID)
#define PERF_START(Handle, Token, Module, TimeStamp)
#define EFI_SECTION_PE32
VOID * EFI_PEI_FILE_HANDLE
Definition: PiPeiCis.h:26
VOID * EFI_PEI_FV_HANDLE
Definition: PiPeiCis.h:21
EFI_STATUS EFIAPI SetBootMode(IN EFI_BOOT_MODE BootMode)
Definition: Hob.c:364
EFI_STATUS EFIAPI FfsFindSectionData(IN EFI_SECTION_TYPE SectionType, IN EFI_PEI_FILE_HANDLE FileHandle, OUT VOID **SectionData)
Definition: FwVol.c:521
EFI_STATUS EFIAPI FfsFindNextFile(IN EFI_FV_FILETYPE SearchType, IN EFI_PEI_FV_HANDLE VolumeHandle, IN OUT EFI_PEI_FILE_HANDLE *FileHandle)
Definition: FwVol.c:545
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
PE_COFF_LOADER_READ_FILE ImageRead
Definition: PeCoffLib.h:100
PHYSICAL_ADDRESS ImageAddress
Definition: PeCoffLib.h:79