]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/PeilessStartupLib/PeilessStartup.c
OvmfPkg: Refactor MeasureHobList
[mirror_edk2.git] / OvmfPkg / Library / PeilessStartupLib / PeilessStartup.c
CommitLineData
4fe26784
MX
1/** @file\r
2\r
3 Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
4\r
5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7**/\r
8\r
9#include <PiPei.h>\r
10#include <Library/BaseLib.h>\r
11#include <Library/BaseMemoryLib.h>\r
12#include <Library/MemoryAllocationLib.h>\r
13#include <Library/DebugLib.h>\r
14#include <Protocol/DebugSupport.h>\r
15#include <Library/TdxLib.h>\r
16#include <IndustryStandard/Tdx.h>\r
17#include <Library/PrePiLib.h>\r
18#include <Library/PeilessStartupLib.h>\r
19#include <Library/PlatformInitLib.h>\r
f41acc65 20#include <Library/TdxHelperLib.h>\r
4fe26784
MX
21#include <ConfidentialComputingGuestAttr.h>\r
22#include <Guid/MemoryTypeInformation.h>\r
23#include <OvmfPlatforms.h>\r
24#include "PeilessStartupInternal.h"\r
25\r
26#define GET_GPAW_INIT_STATE(INFO) ((UINT8) ((INFO) & 0x3f))\r
27\r
28EFI_MEMORY_TYPE_INFORMATION mDefaultMemoryTypeInformation[] = {\r
29 { EfiACPIMemoryNVS, 0x004 },\r
30 { EfiACPIReclaimMemory, 0x008 },\r
31 { EfiReservedMemoryType, 0x004 },\r
32 { EfiRuntimeServicesData, 0x024 },\r
33 { EfiRuntimeServicesCode, 0x030 },\r
34 { EfiBootServicesCode, 0x180 },\r
35 { EfiBootServicesData, 0xF00 },\r
36 { EfiMaxMemoryType, 0x000 }\r
37};\r
38\r
39EFI_STATUS\r
40EFIAPI\r
41InitializePlatform (\r
42 EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
43 )\r
44{\r
124b7650 45 VOID *VariableStore;\r
4fe26784
MX
46\r
47 DEBUG ((DEBUG_INFO, "InitializePlatform in Pei-less boot\n"));\r
48 PlatformDebugDumpCmos ();\r
49\r
50 PlatformInfoHob->DefaultMaxCpuNumber = 64;\r
51 PlatformInfoHob->PcdPciMmio64Size = 0x800000000;\r
52\r
53 PlatformInfoHob->HostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);\r
54 DEBUG ((DEBUG_INFO, "HostBridgeDeviceId = 0x%x\n", PlatformInfoHob->HostBridgeDevId));\r
55\r
56 PlatformAddressWidthInitialization (PlatformInfoHob);\r
57 DEBUG ((\r
58 DEBUG_INFO,\r
59 "PhysMemAddressWidth=0x%x, Pci64Base=0x%llx, Pci64Size=0x%llx\n",\r
60 PlatformInfoHob->PhysMemAddressWidth,\r
61 PlatformInfoHob->PcdPciMmio64Base,\r
62 PlatformInfoHob->PcdPciMmio64Size\r
63 ));\r
64\r
65 PlatformMaxCpuCountInitialization (PlatformInfoHob);\r
66 DEBUG ((\r
67 DEBUG_INFO,\r
68 "MaxCpuCount=%d, BootCpuCount=%d\n",\r
69 PlatformInfoHob->PcdCpuMaxLogicalProcessorNumber,\r
70 PlatformInfoHob->PcdCpuBootLogicalProcessorNumber\r
71 ));\r
72\r
124b7650 73 PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r
4fe26784
MX
74 PlatformQemuUc32BaseInitialization (PlatformInfoHob);\r
75 DEBUG ((\r
76 DEBUG_INFO,\r
77 "Uc32Base = 0x%x, Uc32Size = 0x%x, LowerMemorySize = 0x%x\n",\r
78 PlatformInfoHob->Uc32Base,\r
79 PlatformInfoHob->Uc32Size,\r
124b7650 80 PlatformInfoHob->LowMemory\r
4fe26784
MX
81 ));\r
82\r
ee91d9ef
MX
83 VariableStore = PlatformReserveEmuVariableNvStore ();\r
84 PlatformInfoHob->PcdEmuVariableNvStoreReserved = (UINT64)(UINTN)VariableStore;\r
85 #ifdef SECURE_BOOT_FEATURE_ENABLED\r
86 PlatformInitEmuVariableNvStore (VariableStore);\r
87 #endif\r
88\r
4fe26784
MX
89 if (TdIsEnabled ()) {\r
90 PlatformTdxPublishRamRegions ();\r
91 } else {\r
92 PlatformQemuInitializeRam (PlatformInfoHob);\r
93 PlatformQemuInitializeRamForS3 (PlatformInfoHob);\r
94 }\r
95\r
96 //\r
97 // Create Memory Type Information HOB\r
98 //\r
99 BuildGuidDataHob (\r
100 &gEfiMemoryTypeInformationGuid,\r
101 mDefaultMemoryTypeInformation,\r
102 sizeof (mDefaultMemoryTypeInformation)\r
103 );\r
104\r
105 PlatformMemMapInitialization (PlatformInfoHob);\r
106\r
107 PlatformNoexecDxeInitialization (PlatformInfoHob);\r
108\r
109 if (TdIsEnabled ()) {\r
110 PlatformInfoHob->PcdConfidentialComputingGuestAttr = CCAttrIntelTdx;\r
111 PlatformInfoHob->PcdTdxSharedBitMask = TdSharedPageMask ();\r
112 PlatformInfoHob->PcdSetNxForStack = TRUE;\r
113 }\r
114\r
115 PlatformMiscInitialization (PlatformInfoHob);\r
116\r
117 return EFI_SUCCESS;\r
118}\r
119\r
120/**\r
121 * This function brings up the Tdx guest from SEC phase to DXE phase.\r
122 * PEI phase is skipped because most of the components in PEI phase\r
123 * is not needed for Tdx guest, for example, MP Services, TPM etc.\r
124 * In this way, the attack surfaces are reduced as much as possible.\r
125 *\r
126 * @param Context The pointer to the SecCoreData\r
127 * @return VOID This function never returns\r
128 */\r
129VOID\r
130EFIAPI\r
131PeilessStartup (\r
132 IN VOID *Context\r
133 )\r
134{\r
135 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
136 EFI_FIRMWARE_VOLUME_HEADER *BootFv;\r
137 EFI_STATUS Status;\r
138 EFI_HOB_PLATFORM_INFO PlatformInfoHob;\r
139 UINT32 DxeCodeBase;\r
140 UINT32 DxeCodeSize;\r
141 TD_RETURN_DATA TdReturnData;\r
142 VOID *VmmHobList;\r
4b0a6226 143 UINT8 *CfvBase;\r
4fe26784
MX
144\r
145 Status = EFI_SUCCESS;\r
146 BootFv = NULL;\r
147 VmmHobList = NULL;\r
148 SecCoreData = (EFI_SEC_PEI_HAND_OFF *)Context;\r
4b0a6226 149 CfvBase = (UINT8 *)(UINTN)FixedPcdGet32 (PcdCfvBase);\r
4fe26784
MX
150\r
151 ZeroMem (&PlatformInfoHob, sizeof (PlatformInfoHob));\r
152\r
153 if (TdIsEnabled ()) {\r
154 VmmHobList = (VOID *)(UINTN)FixedPcdGet32 (PcdOvmfSecGhcbBase);\r
155 Status = TdCall (TDCALL_TDINFO, 0, 0, 0, &TdReturnData);\r
156 ASSERT (Status == EFI_SUCCESS);\r
157\r
158 DEBUG ((\r
159 DEBUG_INFO,\r
160 "Tdx started with(Hob: 0x%x, Gpaw: 0x%x, Cpus: %d)\n",\r
161 (UINT32)(UINTN)VmmHobList,\r
162 GET_GPAW_INIT_STATE (TdReturnData.TdInfo.Gpaw),\r
163 TdReturnData.TdInfo.NumVcpus\r
164 ));\r
165\r
166 Status = ConstructFwHobList (VmmHobList);\r
167 } else {\r
168 DEBUG ((DEBUG_INFO, "Ovmf started\n"));\r
169 Status = ConstructSecHobList ();\r
170 }\r
171\r
172 if (EFI_ERROR (Status)) {\r
173 ASSERT (FALSE);\r
174 CpuDeadLoop ();\r
175 }\r
176\r
177 DEBUG ((DEBUG_INFO, "HobList: %p\n", GetHobList ()));\r
178\r
4b0a6226
MX
179 if (TdIsEnabled ()) {\r
180 //\r
181 // Measure HobList\r
182 //\r
f41acc65
MX
183 Status = TdxHelperMeasureTdHob ();\r
184 if (EFI_ERROR (Status)) {\r
185 ASSERT (FALSE);\r
186 CpuDeadLoop ();\r
187 }\r
188\r
189 //\r
190 // Build GuidHob for tdx measurement\r
191 //\r
192 Status = TdxHelperBuildGuidHobForTdxMeasurement ();\r
4b0a6226
MX
193 if (EFI_ERROR (Status)) {\r
194 ASSERT (FALSE);\r
195 CpuDeadLoop ();\r
196 }\r
197\r
4b0a6226
MX
198 //\r
199 // Measure Tdx CFV\r
200 //\r
201 Status = MeasureFvImage ((EFI_PHYSICAL_ADDRESS)(UINTN)CfvBase, FixedPcdGet32 (PcdCfvRawDataSize), 1);\r
202 if (EFI_ERROR (Status)) {\r
203 ASSERT (FALSE);\r
204 CpuDeadLoop ();\r
205 }\r
206 }\r
207\r
4fe26784
MX
208 //\r
209 // Initialize the Platform\r
210 //\r
211 Status = InitializePlatform (&PlatformInfoHob);\r
212 if (EFI_ERROR (Status)) {\r
213 ASSERT (FALSE);\r
214 CpuDeadLoop ();\r
215 }\r
216\r
217 BuildGuidDataHob (&gUefiOvmfPkgPlatformInfoGuid, &PlatformInfoHob, sizeof (EFI_HOB_PLATFORM_INFO));\r
218\r
219 //\r
220 // SecFV\r
221 //\r
222 BootFv = (EFI_FIRMWARE_VOLUME_HEADER *)SecCoreData->BootFirmwareVolumeBase;\r
223 BuildFvHob ((UINTN)BootFv, BootFv->FvLength);\r
224\r
225 //\r
226 // DxeFV\r
227 //\r
228 DxeCodeBase = PcdGet32 (PcdBfvBase);\r
229 DxeCodeSize = PcdGet32 (PcdBfvRawDataSize) - (UINT32)BootFv->FvLength;\r
230 BuildFvHob (DxeCodeBase, DxeCodeSize);\r
231\r
232 DEBUG ((DEBUG_INFO, "SecFv : %p, 0x%x\n", BootFv, BootFv->FvLength));\r
233 DEBUG ((DEBUG_INFO, "DxeFv : %x, 0x%x\n", DxeCodeBase, DxeCodeSize));\r
234\r
235 BuildStackHob ((UINTN)SecCoreData->StackBase, SecCoreData->StackSize <<= 1);\r
236\r
237 BuildResourceDescriptorHob (\r
238 EFI_RESOURCE_SYSTEM_MEMORY,\r
239 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
240 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
241 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
242 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
243 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
244 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |\r
245 EFI_RESOURCE_ATTRIBUTE_TESTED,\r
246 (UINT64)SecCoreData->TemporaryRamBase,\r
247 (UINT64)SecCoreData->TemporaryRamSize\r
248 );\r
249\r
250 //\r
251 // Load the DXE Core and transfer control to it.\r
252 // Only DxeFV is in the compressed section.\r
253 //\r
254 Status = DxeLoadCore (1);\r
255\r
256 //\r
257 // Never arrive here.\r
258 //\r
259 ASSERT (FALSE);\r
260 CpuDeadLoop ();\r
261}\r