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