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