]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFspPkg/FspSecCore/SecMain.c
IntelFspPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / IntelFspPkg / FspSecCore / SecMain.c
CommitLineData
c8ec22a2
JY
1/** @file\r
2\r
d5fb1edf 3 Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>\r
16a16ea6 4 SPDX-License-Identifier: BSD-2-Clause-Patent\r
c8ec22a2
JY
5\r
6**/\r
7\r
8#include "SecMain.h"\r
9#include "SecFsp.h"\r
10\r
11EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI gSecTemporaryRamSupportPpi = {\r
12 SecTemporaryRamSupport\r
13};\r
14\r
15EFI_PEI_PPI_DESCRIPTOR mPeiSecPlatformInformationPpi[] = {\r
16 {\r
17 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
18 &gEfiTemporaryRamSupportPpiGuid,\r
19 &gSecTemporaryRamSupportPpi\r
20 }\r
21};\r
22\r
23//\r
24// These are IDT entries pointing to 08:FFFFFFE4h.\r
25//\r
26UINT64 mIdtEntryTemplate = 0xffff8e000008ffe4ULL;\r
27\r
28/**\r
29\r
30 Entry point to the C language phase of SEC. After the SEC assembly\r
31 code has initialized some temporary memory and set up the stack,\r
32 the control is transferred to this function.\r
33\r
34\r
35 @param[in] SizeOfRam Size of the temporary memory available for use.\r
13ca714c 36 @param[in] TempRamBase Base address of temporary ram\r
c8ec22a2 37 @param[in] BootFirmwareVolume Base address of the Boot Firmware Volume.\r
d5fb1edf 38 @param[in] PeiCore PeiCore entry point.\r
9da59186 39 @param[in] BootLoaderStack BootLoader stack.\r
6ca9135a 40 @param[in] ApiIdx the index of API.\r
c8ec22a2
JY
41\r
42 @return This function never returns.\r
43\r
44**/\r
45VOID\r
46EFIAPI\r
47SecStartup (\r
48 IN UINT32 SizeOfRam,\r
49 IN UINT32 TempRamBase,\r
a0e0fb6d 50 IN VOID *BootFirmwareVolume,\r
d5fb1edf 51 IN PEI_CORE_ENTRY PeiCore,\r
9da59186 52 IN UINT32 BootLoaderStack,\r
d5fb1edf 53 IN UINT32 ApiIdx\r
c8ec22a2
JY
54 )\r
55{\r
56 EFI_SEC_PEI_HAND_OFF SecCoreData;\r
57 IA32_DESCRIPTOR IdtDescriptor;\r
58 SEC_IDT_TABLE IdtTableInStack;\r
59 UINT32 Index;\r
60 FSP_GLOBAL_DATA PeiFspData;\r
c8ec22a2
JY
61 UINT64 ExceptionHandler;\r
62\r
63 //\r
64 // Process all libraries constructor function linked to SecCore.\r
65 //\r
66 ProcessLibraryConstructorList ();\r
67\r
68 //\r
69 // Initialize floating point operating environment\r
70 // to be compliant with UEFI spec.\r
71 //\r
72 InitializeFloatingPointUnits ();\r
73\r
74\r
75 // |-------------------|---->\r
76 // |Idt Table |\r
77 // |-------------------|\r
78 // |PeiService Pointer | PeiStackSize\r
79 // |-------------------|\r
80 // | |\r
81 // | Stack |\r
82 // |-------------------|---->\r
83 // | |\r
84 // | |\r
85 // | Heap | PeiTemporayRamSize\r
86 // | |\r
87 // | |\r
88 // |-------------------|----> TempRamBase\r
89 IdtTableInStack.PeiService = NULL;\r
90 ExceptionHandler = FspGetExceptionHandler(mIdtEntryTemplate);\r
91 for (Index = 0; Index < SEC_IDT_ENTRY_COUNT; Index ++) {\r
92 CopyMem ((VOID*)&IdtTableInStack.IdtTable[Index], (VOID*)&ExceptionHandler, sizeof (UINT64));\r
93 }\r
94\r
95 IdtDescriptor.Base = (UINTN) &IdtTableInStack.IdtTable;\r
96 IdtDescriptor.Limit = (UINT16)(sizeof (IdtTableInStack.IdtTable) - 1);\r
97\r
98 AsmWriteIdtr (&IdtDescriptor);\r
99\r
100 //\r
baae777b 101 // Initialize the global FSP data region\r
c8ec22a2 102 //\r
9da59186 103 FspGlobalDataInit (&PeiFspData, BootLoaderStack, (UINT8)ApiIdx);\r
c8ec22a2
JY
104\r
105 //\r
106 // Update the base address and length of Pei temporary memory\r
107 //\r
108 SecCoreData.DataSize = sizeof (EFI_SEC_PEI_HAND_OFF);\r
109 SecCoreData.BootFirmwareVolumeBase = BootFirmwareVolume;\r
110 SecCoreData.BootFirmwareVolumeSize = (UINT32)((EFI_FIRMWARE_VOLUME_HEADER *)BootFirmwareVolume)->FvLength;\r
111 SecCoreData.TemporaryRamBase = (VOID*)(UINTN) TempRamBase;\r
112 SecCoreData.TemporaryRamSize = SizeOfRam;\r
113 SecCoreData.PeiTemporaryRamBase = SecCoreData.TemporaryRamBase;\r
114 SecCoreData.PeiTemporaryRamSize = SizeOfRam >> 1;\r
115 SecCoreData.StackBase = (VOID*)(UINTN)(TempRamBase + SecCoreData.PeiTemporaryRamSize);\r
116 SecCoreData.StackSize = SizeOfRam >> 1;\r
117\r
118 //\r
119 // Call PeiCore Entry\r
d5fb1edf 120 // \r
c8ec22a2
JY
121 PeiCore (&SecCoreData, mPeiSecPlatformInformationPpi);\r
122\r
123 //\r
124 // Should never be here\r
125 //\r
126 CpuDeadLoop ();\r
127}\r
128\r
129/**\r
130 This service of the TEMPORARY_RAM_SUPPORT_PPI that migrates temporary RAM into\r
131 permanent memory.\r
132\r
133 @param[in] PeiServices Pointer to the PEI Services Table.\r
134 @param[in] TemporaryMemoryBase Source Address in temporary memory from which the SEC or PEIM will copy the\r
135 Temporary RAM contents.\r
136 @param[in] PermanentMemoryBase Destination Address in permanent memory into which the SEC or PEIM will copy the\r
137 Temporary RAM contents.\r
138 @param[in] CopySize Amount of memory to migrate from temporary to permanent memory.\r
139\r
140 @retval EFI_SUCCESS The data was successfully returned.\r
141 @retval EFI_INVALID_PARAMETER PermanentMemoryBase + CopySize > TemporaryMemoryBase when\r
142 TemporaryMemoryBase > PermanentMemoryBase.\r
143\r
144**/\r
145EFI_STATUS\r
146EFIAPI\r
147SecTemporaryRamSupport (\r
148 IN CONST EFI_PEI_SERVICES **PeiServices,\r
149 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,\r
150 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,\r
151 IN UINTN CopySize\r
152 )\r
153{\r
154 IA32_DESCRIPTOR IdtDescriptor;\r
155 VOID* OldHeap;\r
156 VOID* NewHeap;\r
157 VOID* OldStack;\r
158 VOID* NewStack;\r
159\r
160 OldHeap = (VOID*)(UINTN)TemporaryMemoryBase;\r
161 NewHeap = (VOID*)((UINTN)PermanentMemoryBase + CopySize / 2);\r
162\r
163 OldStack = (VOID*)((UINTN)TemporaryMemoryBase + CopySize / 2);\r
164 NewStack = (VOID*)(UINTN)PermanentMemoryBase;\r
165\r
166 //\r
167 // Migrate Heap\r
168 //\r
169 CopyMem (NewHeap, OldHeap, CopySize / 2);\r
170\r
171 //\r
172 // Migrate Stack\r
173 //\r
174 CopyMem (NewStack, OldStack, CopySize / 2);\r
175\r
176\r
177 //\r
178 // We need *not* fix the return address because currently,\r
179 // The PeiCore is executed in flash.\r
180 //\r
181\r
182 //\r
183 // Rebase IDT table in permanent memory\r
184 //\r
185 AsmReadIdtr (&IdtDescriptor);\r
186 IdtDescriptor.Base = IdtDescriptor.Base - (UINTN)OldStack + (UINTN)NewStack;\r
187\r
188 AsmWriteIdtr (&IdtDescriptor);\r
189\r
190 //\r
191 // Fixed the FSP data pointer\r
192 //\r
193 FspDataPointerFixUp ((UINTN)NewStack - (UINTN)OldStack);\r
194\r
195 //\r
196 // SecSwitchStack function must be invoked after the memory migration\r
13ca714c
GL
197 // immediately, also we need fixup the stack change caused by new call into\r
198 // permanent memory.\r
c8ec22a2
JY
199 //\r
200 SecSwitchStack (\r
201 (UINT32) (UINTN) OldStack,\r
202 (UINT32) (UINTN) NewStack\r
203 );\r
204\r
205 return EFI_SUCCESS;\r
206}\r