]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Sec/SecMain.c
OVMF: Use optimized BaseMemoryLib libraries
[mirror_edk2.git] / OvmfPkg / Sec / SecMain.c
CommitLineData
49ba9447 1/** @file
2 Main SEC phase code. Transitions to PEI.
3
4 Copyright (c) 2008 - 2009, Intel Corporation
5
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14**/
15
16#include <PiPei.h>
17#include <Library/BaseLib.h>
18#include <Library/DebugLib.h>
19#include <Library/BaseMemoryLib.h>
20#include <Library/PeiServicesLib.h>
21#include <Ppi/TemporaryRamSupport.h>
22#include <Library/PcdLib.h>
284af948 23#include <Library/UefiCpuLib.h>
49ba9447 24
25#include "SecMain.h"
26
27EFI_STATUS
28EFIAPI
29TemporaryRamMigration (
30 IN CONST EFI_PEI_SERVICES **PeiServices,
31 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
32 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
33 IN UINTN CopySize
34 );
35
36STATIC TEMPORARY_RAM_SUPPORT_PPI mTempRamSupportPpi = {
37 (TEMPORARY_RAM_MIGRATION) TemporaryRamMigration
38};
39
40STATIC EFI_PEI_PPI_DESCRIPTOR mPrivateDispatchTable[] = {
41 {
42 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
43 &gEfiTemporaryRamSupportPpiGuid,
44 &mTempRamSupportPpi
45 },
46};
47
48
49VOID
50InitializeIdtPtr (
51 IN VOID* IdtPtr
52 )
53{
54 IA32_DESCRIPTOR IdtDescriptor;
55
56 IdtDescriptor.Base = (UINTN)IdtPtr;
57 IdtDescriptor.Limit = (UINT16) 0;
58 AsmWriteIdtr (&IdtDescriptor);
59}
60
61VOID
62EFIAPI
63SecCoreStartupWithStack (
0913fadc 64 IN EFI_FIRMWARE_VOLUME_HEADER *BootFirmwareVolumePtr,
65 IN VOID *TopOfCurrentStack
49ba9447 66 )
67{
68 EFI_SEC_PEI_HAND_OFF *SecCoreData;
69 UINT8 *BottomOfTempRam;
70 UINT8 *TopOfTempRam;
71 UINTN SizeOfTempRam;
72 VOID *IdtPtr;
0913fadc 73 VOID *PeiCoreEntryPoint;
49ba9447 74
284af948 75 //
76 // Initialize floating point operating environment
77 // to be compliant with UEFI spec.
78 //
79 InitializeFloatingPointUnits ();
80
0913fadc 81 DEBUG ((EFI_D_INFO,
82 "SecCoreStartupWithStack(0x%x, 0x%x)\n",
49ba9447 83 (UINT32)(UINTN)BootFirmwareVolumePtr,
0913fadc 84 (UINT32)(UINTN)TopOfCurrentStack
85 ));
49ba9447 86
49ba9447 87 BottomOfTempRam = (UINT8*)(UINTN) INITIAL_TOP_OF_STACK;
88 SizeOfTempRam = (UINTN) SIZE_64KB;
89 TopOfTempRam = BottomOfTempRam + SizeOfTempRam;
90
91 //
92 // |-------------|
93 // | SecCoreData | 4k
94 // |-------------|
95 // | Heap | 28k
96 // |-------------|
97 // | Stack | 32k
98 // |-------------| <---- INITIAL_TOP_OF_STACK
99 //
100
101 //
102 // Bind this information into the SEC hand-off state
103 //
104 SecCoreData = (EFI_SEC_PEI_HAND_OFF*)((UINTN) TopOfTempRam - SIZE_4KB);
105 SecCoreData->DataSize = sizeof(EFI_SEC_PEI_HAND_OFF);
106
107 SecCoreData->BootFirmwareVolumeBase = (VOID*)(UINTN) PcdGet32 (PcdOvmfFlashFvRecoveryBase);
108 SecCoreData->BootFirmwareVolumeSize = PcdGet32 (PcdOvmfFlashFvRecoverySize);
109
110 SecCoreData->TemporaryRamBase = (VOID*) BottomOfTempRam;
111 SecCoreData->TemporaryRamSize = SizeOfTempRam;
112
113 SecCoreData->PeiTemporaryRamSize = 28 * SIZE_1KB;
114 SecCoreData->PeiTemporaryRamBase = (VOID*)((UINTN)SecCoreData - SecCoreData->PeiTemporaryRamSize);
115
116 SecCoreData->StackBase = SecCoreData->TemporaryRamBase;
117 SecCoreData->StackSize = (UINTN)SecCoreData->PeiTemporaryRamBase - (UINTN)SecCoreData->TemporaryRamBase;
118
119 //
120 // Initialize the IDT Pointer, since IA32 & X64 architectures
121 // use it to store the PEI Services pointer.
122 //
123 IdtPtr = (VOID*)((UINT8*)SecCoreData + sizeof (*SecCoreData) + sizeof (UINTN));
124 IdtPtr = ALIGN_POINTER(IdtPtr, 16);
125 InitializeIdtPtr (IdtPtr);
126
0913fadc 127 FindPeiCoreEntryPoint (BootFirmwareVolumePtr, &PeiCoreEntryPoint);
128
129 if (PeiCoreEntryPoint != NULL) {
130 DEBUG ((EFI_D_INFO,
131 "Calling PEI Core entry point at 0x%x\n",
132 PeiCoreEntryPoint
133 ));
134 //
135 // Transfer control to the PEI Core
136 //
137 PeiSwitchStacks (
138 (SWITCH_STACK_ENTRY_POINT) (UINTN) PeiCoreEntryPoint,
139 SecCoreData,
140 (VOID *) (UINTN) ((EFI_PEI_PPI_DESCRIPTOR *) &mPrivateDispatchTable),
141 NULL,
142 TopOfCurrentStack,
143 (VOID *)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)
144 );
145 }
49ba9447 146
147 //
0913fadc 148 // If we get here, then either we couldn't locate the PEI Core, or
149 // the PEI Core returned.
150 //
151 // Both of these errors are unrecoverable.
49ba9447 152 //
153 ASSERT (FALSE);
154 CpuDeadLoop ();
155}
156
157EFI_STATUS
158EFIAPI
159TemporaryRamMigration (
160 IN CONST EFI_PEI_SERVICES **PeiServices,
161 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
162 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
163 IN UINTN CopySize
164 )
165{
166 DEBUG ((EFI_D_ERROR, "TemporaryRamMigration(0x%x, 0x%x, 0x%x)\n", (UINTN)TemporaryMemoryBase, (UINTN)PermanentMemoryBase, CopySize));
167
168 //
169 // Migrate the whole temporary memory to permenent memory.
170 //
171 CopyMem((VOID*)(UINTN)PermanentMemoryBase, (VOID*)(UINTN)TemporaryMemoryBase, CopySize);
172
173 //
174 // SecSwitchStack function must be invoked after the memory migration
175 // immediatly, also we need fixup the stack change caused by new call into
176 // permenent memory.
177 //
178 SecSwitchStack (
179 (UINTN) TemporaryMemoryBase,
180 (UINTN) PermanentMemoryBase,
181 CopySize
182 );
183
184 return EFI_SUCCESS;
185}
186