Add IntelFspWrapper to support boot EDKII on FSP bin.
[mirror_edk2.git] / IntelFspWrapperPkg / FspInitPei / SecMain.c
1 /** @file
2 C functions in SEC
3
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15
16 #include "SecMain.h"
17
18 EFI_PEI_PPI_DESCRIPTOR mPeiSecMainPpi[] = {
19 {
20 EFI_PEI_PPI_DESCRIPTOR_PPI,
21 &gTopOfTemporaryRamPpiGuid,
22 NULL // To be patched later.
23 },
24 {
25 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
26 &gFspInitDonePpiGuid,
27 &gFspInitDonePpi
28 },
29 };
30
31 //
32 // These are IDT entries pointing to 10:FFFFFFE4h.
33 //
34 UINT64 mIdtEntryTemplate = 0xffff8e000010ffe4ULL;
35
36 /**
37 Caller provided function to be invoked at the end of InitializeDebugAgent().
38
39 Entry point to the C language phase of SEC. After the SEC assembly
40 code has initialized some temporary memory and set up the stack,
41 the control is transferred to this function.
42
43 @param[in] Context The first input parameter of InitializeDebugAgent().
44
45 **/
46 VOID
47 EFIAPI
48 SecStartupPhase2(
49 IN VOID *Context
50 );
51
52
53 /**
54
55 Entry point to the C language phase of SEC. After the SEC assembly
56 code has initialized some temporary memory and set up the stack,
57 the control is transferred to this function.
58
59 @param[in] SizeOfRam Size of the temporary memory available for use.
60 @param[in] TempRamBase Base address of tempory ram
61 @param[in] BootFirmwareVolume Base address of the Boot Firmware Volume.
62 **/
63 VOID
64 EFIAPI
65 SecStartup (
66 IN UINT32 SizeOfRam,
67 IN UINT32 TempRamBase,
68 IN VOID *BootFirmwareVolume
69 )
70 {
71 EFI_SEC_PEI_HAND_OFF SecCoreData;
72 IA32_DESCRIPTOR IdtDescriptor;
73 SEC_IDT_TABLE IdtTableInStack;
74 UINT32 Index;
75 UINT32 PeiStackSize;
76
77 PeiStackSize = PcdGet32 (PcdPeiTemporaryRamStackSize);
78 if (PeiStackSize == 0) {
79 PeiStackSize = (SizeOfRam >> 1);
80 }
81
82 ASSERT (PeiStackSize < SizeOfRam);
83
84 //
85 // Process all libraries constructor function linked to SecCore.
86 //
87 ProcessLibraryConstructorList ();
88
89 DEBUG ((DEBUG_INFO, "FspPei - SecStartup\n"));
90
91 //
92 // Initialize floating point operating environment
93 // to be compliant with UEFI spec.
94 //
95 InitializeFloatingPointUnits ();
96
97
98 // |-------------------|---->
99 // |Idt Table |
100 // |-------------------|
101 // |PeiService Pointer | PeiStackSize
102 // |-------------------|
103 // | |
104 // | Stack |
105 // |-------------------|---->
106 // | |
107 // | |
108 // | Heap | PeiTemporayRamSize
109 // | |
110 // | |
111 // |-------------------|----> TempRamBase
112
113 IdtTableInStack.PeiService = 0;
114 for (Index = 0; Index < SEC_IDT_ENTRY_COUNT; Index ++) {
115 CopyMem ((VOID*)&IdtTableInStack.IdtTable[Index], (VOID*)&mIdtEntryTemplate, sizeof (UINT64));
116 }
117
118 IdtDescriptor.Base = (UINTN) &IdtTableInStack.IdtTable;
119 IdtDescriptor.Limit = (UINT16)(sizeof (IdtTableInStack.IdtTable) - 1);
120
121 AsmWriteIdtr (&IdtDescriptor);
122
123 //
124 // Update the base address and length of Pei temporary memory
125 //
126 SecCoreData.DataSize = (UINT16) sizeof (EFI_SEC_PEI_HAND_OFF);
127 SecCoreData.BootFirmwareVolumeBase = BootFirmwareVolume;
128 SecCoreData.BootFirmwareVolumeSize = (UINTN)(SIZE_4GB - (UINTN) BootFirmwareVolume);
129 SecCoreData.TemporaryRamBase = (VOID*)(UINTN) TempRamBase;
130 SecCoreData.TemporaryRamSize = SizeOfRam;
131 SecCoreData.PeiTemporaryRamBase = SecCoreData.TemporaryRamBase;
132 SecCoreData.PeiTemporaryRamSize = SizeOfRam - PeiStackSize;
133 SecCoreData.StackBase = (VOID*)(UINTN)(TempRamBase + SecCoreData.PeiTemporaryRamSize);
134 SecCoreData.StackSize = PeiStackSize;
135
136 DEBUG ((DEBUG_INFO, "BootFirmwareVolumeBase - 0x%x\n", SecCoreData.BootFirmwareVolumeBase));
137 DEBUG ((DEBUG_INFO, "BootFirmwareVolumeSize - 0x%x\n", SecCoreData.BootFirmwareVolumeSize));
138 DEBUG ((DEBUG_INFO, "TemporaryRamBase - 0x%x\n", SecCoreData.TemporaryRamBase));
139 DEBUG ((DEBUG_INFO, "TemporaryRamSize - 0x%x\n", SecCoreData.TemporaryRamSize));
140 DEBUG ((DEBUG_INFO, "PeiTemporaryRamBase - 0x%x\n", SecCoreData.PeiTemporaryRamBase));
141 DEBUG ((DEBUG_INFO, "PeiTemporaryRamSize - 0x%x\n", SecCoreData.PeiTemporaryRamSize));
142 DEBUG ((DEBUG_INFO, "StackBase - 0x%x\n", SecCoreData.StackBase));
143 DEBUG ((DEBUG_INFO, "StackSize - 0x%x\n", SecCoreData.StackSize));
144
145 //
146 // Initialize Debug Agent to support source level debug in SEC/PEI phases before memory ready.
147 //
148 InitializeDebugAgent (DEBUG_AGENT_INIT_PREMEM_SEC, &SecCoreData, SecStartupPhase2);
149
150 }
151
152 /**
153 This API patch the TopOfTemporaryRam value in SecPpiList.
154
155 @param[in,out] SecPpiList PPI list to be patched.
156 @param[in] TopOfTemporaryRam The top of Temporary Ram.
157
158 **/
159 VOID
160 PatchTopOfTemporaryRamPpi (
161 IN OUT EFI_PEI_PPI_DESCRIPTOR *SecPpiList,
162 IN VOID *TopOfTemporaryRam
163 )
164 {
165 SecPpiList[0].Ppi = TopOfTemporaryRam;
166 }
167
168 /**
169 Caller provided function to be invoked at the end of InitializeDebugAgent().
170
171 Entry point to the C language phase of SEC. After the SEC assembly
172 code has initialized some temporary memory and set up the stack,
173 the control is transferred to this function.
174
175 @param[in] Context The first input parameter of InitializeDebugAgent().
176
177 **/
178 VOID
179 EFIAPI
180 SecStartupPhase2(
181 IN VOID *Context
182 )
183 {
184 EFI_SEC_PEI_HAND_OFF *SecCoreData;
185 EFI_PEI_PPI_DESCRIPTOR *PpiList;
186 UINT32 Index;
187 EFI_PEI_PPI_DESCRIPTOR LocalSecPpiList[sizeof(mPeiSecMainPpi)/sizeof(mPeiSecMainPpi[0])];
188 EFI_PEI_PPI_DESCRIPTOR AllSecPpiList[FixedPcdGet32(PcdSecCoreMaxPpiSupported)];
189 EFI_PEI_CORE_ENTRY_POINT PeiCoreEntryPoint;
190
191 SecCoreData = (EFI_SEC_PEI_HAND_OFF *) Context;
192 //
193 // Find Pei Core entry point. It will report SEC and Pei Core debug information if remote debug
194 // is enabled.
195 //
196 FindAndReportEntryPoints ((EFI_FIRMWARE_VOLUME_HEADER *) SecCoreData->BootFirmwareVolumeBase, &PeiCoreEntryPoint);
197 if (PeiCoreEntryPoint == NULL)
198 {
199 CpuDeadLoop ();
200 }
201
202 CopyMem (LocalSecPpiList, mPeiSecMainPpi, sizeof(mPeiSecMainPpi));
203 PatchTopOfTemporaryRamPpi (LocalSecPpiList, (VOID *)((UINTN)SecCoreData->TemporaryRamBase + SecCoreData->TemporaryRamSize));
204
205 //
206 // Perform platform specific initialization before entering PeiCore.
207 //
208 PpiList = SecPlatformMain (SecCoreData);
209 if (PpiList != NULL) {
210 //
211 // Remove the terminal flag from the terminal Ppi
212 //
213 CopyMem (AllSecPpiList, LocalSecPpiList, sizeof (LocalSecPpiList));
214 for (Index = 0; Index < PcdGet32 (PcdSecCoreMaxPpiSupported); Index ++) {
215 if ((AllSecPpiList[Index].Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) == EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) {
216 break;
217 }
218 }
219 AllSecPpiList[Index].Flags = AllSecPpiList[Index].Flags & (~EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);
220
221 //
222 // Append the platform additional Ppi list
223 //
224 Index += 1;
225 while (Index < PcdGet32 (PcdSecCoreMaxPpiSupported) &&
226 ((PpiList->Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) != EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST)) {
227 CopyMem (&AllSecPpiList[Index], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));
228 Index++;
229 PpiList++;
230 }
231
232 //
233 // Check whether the total Ppis exceeds the max supported Ppi.
234 //
235 if (Index >= PcdGet32 (PcdSecCoreMaxPpiSupported)) {
236 //
237 // the total Ppi is larger than the supported Max
238 // PcdSecCoreMaxPpiSupported can be enlarged to solve it.
239 //
240 CpuDeadLoop ();
241 } else {
242 //
243 // Add the terminal Ppi
244 //
245 CopyMem (&AllSecPpiList[Index], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));
246 }
247
248 //
249 // Set PpiList to the total Ppi
250 //
251 PpiList = &AllSecPpiList[0];
252 } else {
253 //
254 // No addition Ppi, PpiList directly point to the common Ppi list.
255 //
256 PpiList = &LocalSecPpiList[0];
257 }
258
259 //
260 // Transfer the control to the PEI core
261 //
262 ASSERT (PeiCoreEntryPoint != NULL);
263 (*PeiCoreEntryPoint) (SecCoreData, PpiList);
264
265 //
266 // Should not come here.
267 //
268 return ;
269 }