]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFsp2Pkg/FspSecCore/SecMain.c
70460a3c8befc298546949be0ddab4d05349cd0b
[mirror_edk2.git] / IntelFsp2Pkg / FspSecCore / SecMain.c
1 /** @file
2
3 Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php.
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 **/
13
14 #include "SecMain.h"
15 #include "SecFsp.h"
16
17 EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI gSecTemporaryRamSupportPpi = {
18 SecTemporaryRamSupport
19 };
20
21 EFI_PEI_PPI_DESCRIPTOR mPeiSecPlatformInformationPpi[] = {
22 {
23 EFI_PEI_PPI_DESCRIPTOR_PPI,
24 &gFspInApiModePpiGuid,
25 NULL
26 },
27 {
28 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
29 &gEfiTemporaryRamSupportPpiGuid,
30 &gSecTemporaryRamSupportPpi
31 }
32 };
33
34 //
35 // These are IDT entries pointing to 08:FFFFFFE4h.
36 //
37 UINT64 mIdtEntryTemplate = 0xffff8e000008ffe4ULL;
38
39 /**
40
41 Entry point to the C language phase of SEC. After the SEC assembly
42 code has initialized some temporary memory and set up the stack,
43 the control is transferred to this function.
44
45
46 @param[in] SizeOfRam Size of the temporary memory available for use.
47 @param[in] TempRamBase Base address of temporary ram
48 @param[in] BootFirmwareVolume Base address of the Boot Firmware Volume.
49 @param[in] PeiCore PeiCore entry point.
50 @param[in] BootLoaderStack BootLoader stack.
51 @param[in] ApiIdx the index of API.
52
53 @return This function never returns.
54
55 **/
56 VOID
57 EFIAPI
58 SecStartup (
59 IN UINT32 SizeOfRam,
60 IN UINT32 TempRamBase,
61 IN VOID *BootFirmwareVolume,
62 IN PEI_CORE_ENTRY PeiCore,
63 IN UINT32 BootLoaderStack,
64 IN UINT32 ApiIdx
65 )
66 {
67 EFI_SEC_PEI_HAND_OFF SecCoreData;
68 IA32_DESCRIPTOR IdtDescriptor;
69 SEC_IDT_TABLE IdtTableInStack;
70 UINT32 Index;
71 FSP_GLOBAL_DATA PeiFspData;
72 UINT64 ExceptionHandler;
73 UINTN IdtSize;
74
75 //
76 // Process all libraries constructor function linked to SecCore.
77 //
78 ProcessLibraryConstructorList ();
79
80 //
81 // Initialize floating point operating environment
82 // to be compliant with UEFI spec.
83 //
84 InitializeFloatingPointUnits ();
85
86
87 // |-------------------|---->
88 // |Idt Table |
89 // |-------------------|
90 // |PeiService Pointer | PeiStackSize
91 // |-------------------|
92 // | |
93 // | Stack |
94 // |-------------------|---->
95 // | |
96 // | |
97 // | Heap | PeiTemporayRamSize
98 // | |
99 // | |
100 // |-------------------|----> TempRamBase
101 IdtTableInStack.PeiService = NULL;
102 AsmReadIdtr (&IdtDescriptor);
103 if (IdtDescriptor.Base == 0) {
104 ExceptionHandler = FspGetExceptionHandler(mIdtEntryTemplate);
105 for (Index = 0; Index < FixedPcdGet8(PcdFspMaxInterruptSupported); Index ++) {
106 CopyMem ((VOID*)&IdtTableInStack.IdtTable[Index], (VOID*)&ExceptionHandler, sizeof (UINT64));
107 }
108 IdtSize = sizeof (IdtTableInStack.IdtTable);
109 } else {
110 IdtSize = IdtDescriptor.Limit + 1;
111 if (IdtSize > sizeof (IdtTableInStack.IdtTable)) {
112 //
113 // ERROR: IDT table size from boot loader is larger than FSP can support, DeadLoop here!
114 //
115 CpuDeadLoop();
116 } else {
117 CopyMem ((VOID *) (UINTN) &IdtTableInStack.IdtTable, (VOID *) IdtDescriptor.Base, IdtSize);
118 }
119 }
120 IdtDescriptor.Base = (UINTN) &IdtTableInStack.IdtTable;
121 IdtDescriptor.Limit = (UINT16)(IdtSize - 1);
122
123 AsmWriteIdtr (&IdtDescriptor);
124
125 //
126 // Initialize the global FSP data region
127 //
128 FspGlobalDataInit (&PeiFspData, BootLoaderStack, (UINT8)ApiIdx);
129
130 //
131 // Update the base address and length of Pei temporary memory
132 //
133 SecCoreData.DataSize = sizeof (EFI_SEC_PEI_HAND_OFF);
134 SecCoreData.BootFirmwareVolumeBase = BootFirmwareVolume;
135 SecCoreData.BootFirmwareVolumeSize = (UINT32)((EFI_FIRMWARE_VOLUME_HEADER *)BootFirmwareVolume)->FvLength;
136
137 SecCoreData.TemporaryRamBase = (VOID*)(UINTN) TempRamBase;
138 SecCoreData.TemporaryRamSize = SizeOfRam;
139 SecCoreData.PeiTemporaryRamBase = SecCoreData.TemporaryRamBase;
140 SecCoreData.PeiTemporaryRamSize = SecCoreData.TemporaryRamSize * PcdGet8 (PcdFspHeapSizePercentage) / 100;
141 SecCoreData.StackBase = (VOID*)(UINTN)((UINTN)SecCoreData.TemporaryRamBase + SecCoreData.PeiTemporaryRamSize);
142 SecCoreData.StackSize = SecCoreData.TemporaryRamSize - SecCoreData.PeiTemporaryRamSize;
143
144 DEBUG ((DEBUG_INFO, "Fsp BootFirmwareVolumeBase - 0x%x\n", SecCoreData.BootFirmwareVolumeBase));
145 DEBUG ((DEBUG_INFO, "Fsp BootFirmwareVolumeSize - 0x%x\n", SecCoreData.BootFirmwareVolumeSize));
146 DEBUG ((DEBUG_INFO, "Fsp TemporaryRamBase - 0x%x\n", SecCoreData.TemporaryRamBase));
147 DEBUG ((DEBUG_INFO, "Fsp TemporaryRamSize - 0x%x\n", SecCoreData.TemporaryRamSize));
148 DEBUG ((DEBUG_INFO, "Fsp PeiTemporaryRamBase - 0x%x\n", SecCoreData.PeiTemporaryRamBase));
149 DEBUG ((DEBUG_INFO, "Fsp PeiTemporaryRamSize - 0x%x\n", SecCoreData.PeiTemporaryRamSize));
150 DEBUG ((DEBUG_INFO, "Fsp StackBase - 0x%x\n", SecCoreData.StackBase));
151 DEBUG ((DEBUG_INFO, "Fsp StackSize - 0x%x\n", SecCoreData.StackSize));
152
153 //
154 // Call PeiCore Entry
155 //
156 PeiCore (&SecCoreData, mPeiSecPlatformInformationPpi);
157
158 //
159 // Should never be here
160 //
161 CpuDeadLoop ();
162 }
163
164 /**
165 This service of the TEMPORARY_RAM_SUPPORT_PPI that migrates temporary RAM into
166 permanent memory.
167
168 @param[in] PeiServices Pointer to the PEI Services Table.
169 @param[in] TemporaryMemoryBase Source Address in temporary memory from which the SEC or PEIM will copy the
170 Temporary RAM contents.
171 @param[in] PermanentMemoryBase Destination Address in permanent memory into which the SEC or PEIM will copy the
172 Temporary RAM contents.
173 @param[in] CopySize Amount of memory to migrate from temporary to permanent memory.
174
175 @retval EFI_SUCCESS The data was successfully returned.
176 @retval EFI_INVALID_PARAMETER PermanentMemoryBase + CopySize > TemporaryMemoryBase when
177 TemporaryMemoryBase > PermanentMemoryBase.
178
179 **/
180 EFI_STATUS
181 EFIAPI
182 SecTemporaryRamSupport (
183 IN CONST EFI_PEI_SERVICES **PeiServices,
184 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
185 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
186 IN UINTN CopySize
187 )
188 {
189 IA32_DESCRIPTOR IdtDescriptor;
190 VOID* OldHeap;
191 VOID* NewHeap;
192 VOID* OldStack;
193 VOID* NewStack;
194 UINTN HeapSize;
195 UINTN StackSize;
196
197 HeapSize = CopySize * PcdGet8 (PcdFspHeapSizePercentage) / 100 ;
198 StackSize = CopySize - HeapSize;
199
200 OldHeap = (VOID*)(UINTN)TemporaryMemoryBase;
201 NewHeap = (VOID*)((UINTN)PermanentMemoryBase + StackSize);
202
203 OldStack = (VOID*)((UINTN)TemporaryMemoryBase + HeapSize);
204 NewStack = (VOID*)(UINTN)PermanentMemoryBase;
205
206 //
207 // Migrate Heap
208 //
209 CopyMem (NewHeap, OldHeap, HeapSize);
210
211 //
212 // Migrate Stack
213 //
214 CopyMem (NewStack, OldStack, StackSize);
215
216
217 //
218 // We need *not* fix the return address because currently,
219 // The PeiCore is executed in flash.
220 //
221
222 //
223 // Rebase IDT table in permanent memory
224 //
225 AsmReadIdtr (&IdtDescriptor);
226 IdtDescriptor.Base = IdtDescriptor.Base - (UINTN)OldStack + (UINTN)NewStack;
227
228 AsmWriteIdtr (&IdtDescriptor);
229
230 //
231 // Fixed the FSP data pointer
232 //
233 FspDataPointerFixUp ((UINTN)NewStack - (UINTN)OldStack);
234
235 //
236 // SecSwitchStack function must be invoked after the memory migration
237 // immediately, also we need fixup the stack change caused by new call into
238 // permanent memory.
239 //
240 SecSwitchStack (
241 (UINT32) (UINTN) OldStack,
242 (UINT32) (UINTN) NewStack
243 );
244
245 return EFI_SUCCESS;
246 }