]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Sec/SecMain.c
ArmVirtPkg: ArmVirtPlatformLib: find the lowest memory node
[mirror_edk2.git] / OvmfPkg / Sec / SecMain.c
CommitLineData
bb4aa855
JJ
1/** @file\r
2 Main SEC phase code. Transitions to PEI.\r
3\r
f3e34b9d 4 Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.<BR>\r
bb4aa855
JJ
5\r
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <PiPei.h>\r
17\r
18#include <Library/PeimEntryPoint.h>\r
19#include <Library/BaseLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/BaseMemoryLib.h>\r
22#include <Library/PeiServicesLib.h>\r
23#include <Library/PcdLib.h>\r
24#include <Library/UefiCpuLib.h>\r
25#include <Library/DebugAgentLib.h>\r
26#include <Library/IoLib.h>\r
27#include <Library/PeCoffLib.h>\r
28#include <Library/PeCoffGetEntryPointLib.h>\r
29#include <Library/PeCoffExtraActionLib.h>\r
30#include <Library/ExtractGuidedSectionLib.h>\r
f3e34b9d 31#include <Library/LocalApicLib.h>\r
bb4aa855
JJ
32\r
33#include <Ppi/TemporaryRamSupport.h>\r
34\r
35#define SEC_IDT_ENTRY_COUNT 34\r
36\r
37typedef struct _SEC_IDT_TABLE {\r
38 EFI_PEI_SERVICES *PeiService;\r
39 IA32_IDT_GATE_DESCRIPTOR IdtTable[SEC_IDT_ENTRY_COUNT];\r
40} SEC_IDT_TABLE;\r
41\r
42VOID\r
43EFIAPI\r
44SecStartupPhase2 (\r
45 IN VOID *Context\r
46 );\r
47\r
48EFI_STATUS\r
49EFIAPI\r
50TemporaryRamMigration (\r
51 IN CONST EFI_PEI_SERVICES **PeiServices,\r
52 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,\r
53 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,\r
54 IN UINTN CopySize\r
55 );\r
56\r
57//\r
58//\r
59// \r
60EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI mTemporaryRamSupportPpi = {\r
61 TemporaryRamMigration\r
62};\r
63\r
64EFI_PEI_PPI_DESCRIPTOR mPrivateDispatchTable[] = {\r
65 {\r
66 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
67 &gEfiTemporaryRamSupportPpiGuid,\r
68 &mTemporaryRamSupportPpi\r
69 },\r
70};\r
71\r
72//\r
73// Template of an IDT entry pointing to 10:FFFFFFE4h.\r
74//\r
75IA32_IDT_GATE_DESCRIPTOR mIdtEntryTemplate = {\r
76 { // Bits\r
77 0xffe4, // OffsetLow\r
78 0x10, // Selector\r
79 0x0, // Reserved_0\r
80 IA32_IDT_GATE_TYPE_INTERRUPT_32, // GateType\r
81 0xffff // OffsetHigh\r
82 } \r
83};\r
84\r
85/**\r
86 Locates the main boot firmware volume.\r
87\r
88 @param[in,out] BootFv On input, the base of the BootFv\r
89 On output, the decompressed main firmware volume\r
90\r
91 @retval EFI_SUCCESS The main firmware volume was located and decompressed\r
92 @retval EFI_NOT_FOUND The main firmware volume was not found\r
93\r
94**/\r
95EFI_STATUS\r
96FindMainFv (\r
97 IN OUT EFI_FIRMWARE_VOLUME_HEADER **BootFv\r
98 )\r
99{\r
100 EFI_FIRMWARE_VOLUME_HEADER *Fv;\r
101 UINTN Distance;\r
102\r
103 ASSERT (((UINTN) *BootFv & EFI_PAGE_MASK) == 0);\r
104\r
105 Fv = *BootFv;\r
106 Distance = (UINTN) (*BootFv)->FvLength;\r
107 do {\r
108 Fv = (EFI_FIRMWARE_VOLUME_HEADER*) ((UINT8*) Fv - EFI_PAGE_SIZE);\r
109 Distance += EFI_PAGE_SIZE;\r
110 if (Distance > SIZE_32MB) {\r
111 return EFI_NOT_FOUND;\r
112 }\r
113\r
114 if (Fv->Signature != EFI_FVH_SIGNATURE) {\r
115 continue;\r
116 }\r
117\r
118 if ((UINTN) Fv->FvLength > Distance) {\r
119 continue;\r
120 }\r
121\r
122 *BootFv = Fv;\r
123 return EFI_SUCCESS;\r
124\r
125 } while (TRUE);\r
126}\r
127\r
128/**\r
129 Locates a section within a series of sections\r
130 with the specified section type.\r
131\r
4b4b783d
JJ
132 The Instance parameter indicates which instance of the section\r
133 type to return. (0 is first instance, 1 is second...)\r
134\r
bb4aa855
JJ
135 @param[in] Sections The sections to search\r
136 @param[in] SizeOfSections Total size of all sections\r
137 @param[in] SectionType The section type to locate\r
4b4b783d 138 @param[in] Instance The section instance number\r
bb4aa855
JJ
139 @param[out] FoundSection The FFS section if found\r
140\r
141 @retval EFI_SUCCESS The file and section was found\r
142 @retval EFI_NOT_FOUND The file and section was not found\r
143 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted\r
144\r
145**/\r
146EFI_STATUS\r
4b4b783d 147FindFfsSectionInstance (\r
bb4aa855
JJ
148 IN VOID *Sections,\r
149 IN UINTN SizeOfSections,\r
150 IN EFI_SECTION_TYPE SectionType,\r
4b4b783d 151 IN UINTN Instance,\r
bb4aa855
JJ
152 OUT EFI_COMMON_SECTION_HEADER **FoundSection\r
153 )\r
154{\r
155 EFI_PHYSICAL_ADDRESS CurrentAddress;\r
156 UINT32 Size;\r
157 EFI_PHYSICAL_ADDRESS EndOfSections;\r
158 EFI_COMMON_SECTION_HEADER *Section;\r
159 EFI_PHYSICAL_ADDRESS EndOfSection;\r
160\r
161 //\r
162 // Loop through the FFS file sections within the PEI Core FFS file\r
163 //\r
164 EndOfSection = (EFI_PHYSICAL_ADDRESS)(UINTN) Sections;\r
165 EndOfSections = EndOfSection + SizeOfSections;\r
166 for (;;) {\r
167 if (EndOfSection == EndOfSections) {\r
168 break;\r
169 }\r
170 CurrentAddress = (EndOfSection + 3) & ~(3ULL);\r
171 if (CurrentAddress >= EndOfSections) {\r
172 return EFI_VOLUME_CORRUPTED;\r
173 }\r
174\r
175 Section = (EFI_COMMON_SECTION_HEADER*)(UINTN) CurrentAddress;\r
bb4aa855
JJ
176\r
177 Size = SECTION_SIZE (Section);\r
178 if (Size < sizeof (*Section)) {\r
179 return EFI_VOLUME_CORRUPTED;\r
180 }\r
181\r
182 EndOfSection = CurrentAddress + Size;\r
183 if (EndOfSection > EndOfSections) {\r
184 return EFI_VOLUME_CORRUPTED;\r
185 }\r
186\r
187 //\r
188 // Look for the requested section type\r
189 //\r
190 if (Section->Type == SectionType) {\r
4b4b783d
JJ
191 if (Instance == 0) {\r
192 *FoundSection = Section;\r
193 return EFI_SUCCESS;\r
194 } else {\r
195 Instance--;\r
196 }\r
bb4aa855 197 }\r
bb4aa855
JJ
198 }\r
199\r
200 return EFI_NOT_FOUND;\r
201}\r
202\r
4b4b783d
JJ
203/**\r
204 Locates a section within a series of sections\r
205 with the specified section type.\r
206\r
207 @param[in] Sections The sections to search\r
208 @param[in] SizeOfSections Total size of all sections\r
209 @param[in] SectionType The section type to locate\r
210 @param[out] FoundSection The FFS section if found\r
211\r
212 @retval EFI_SUCCESS The file and section was found\r
213 @retval EFI_NOT_FOUND The file and section was not found\r
214 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted\r
215\r
216**/\r
217EFI_STATUS\r
218FindFfsSectionInSections (\r
219 IN VOID *Sections,\r
220 IN UINTN SizeOfSections,\r
221 IN EFI_SECTION_TYPE SectionType,\r
222 OUT EFI_COMMON_SECTION_HEADER **FoundSection\r
223 )\r
224{\r
225 return FindFfsSectionInstance (\r
226 Sections,\r
227 SizeOfSections,\r
228 SectionType,\r
229 0,\r
230 FoundSection\r
231 );\r
232}\r
233\r
bb4aa855
JJ
234/**\r
235 Locates a FFS file with the specified file type and a section\r
236 within that file with the specified section type.\r
237\r
238 @param[in] Fv The firmware volume to search\r
239 @param[in] FileType The file type to locate\r
240 @param[in] SectionType The section type to locate\r
241 @param[out] FoundSection The FFS section if found\r
242\r
243 @retval EFI_SUCCESS The file and section was found\r
244 @retval EFI_NOT_FOUND The file and section was not found\r
245 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted\r
246\r
247**/\r
248EFI_STATUS\r
bb4aa855
JJ
249FindFfsFileAndSection (\r
250 IN EFI_FIRMWARE_VOLUME_HEADER *Fv,\r
251 IN EFI_FV_FILETYPE FileType,\r
252 IN EFI_SECTION_TYPE SectionType,\r
253 OUT EFI_COMMON_SECTION_HEADER **FoundSection\r
254 )\r
255{\r
256 EFI_STATUS Status;\r
257 EFI_PHYSICAL_ADDRESS CurrentAddress;\r
258 EFI_PHYSICAL_ADDRESS EndOfFirmwareVolume;\r
259 EFI_FFS_FILE_HEADER *File;\r
260 UINT32 Size;\r
261 EFI_PHYSICAL_ADDRESS EndOfFile;\r
262\r
263 if (Fv->Signature != EFI_FVH_SIGNATURE) {\r
c67178b7 264 DEBUG ((EFI_D_ERROR, "FV at %p does not have FV header signature\n", Fv));\r
bb4aa855
JJ
265 return EFI_VOLUME_CORRUPTED;\r
266 }\r
267\r
268 CurrentAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Fv;\r
269 EndOfFirmwareVolume = CurrentAddress + Fv->FvLength;\r
270\r
271 //\r
272 // Loop through the FFS files in the Boot Firmware Volume\r
273 //\r
274 for (EndOfFile = CurrentAddress + Fv->HeaderLength; ; ) {\r
275\r
276 CurrentAddress = (EndOfFile + 7) & ~(7ULL);\r
277 if (CurrentAddress > EndOfFirmwareVolume) {\r
278 return EFI_VOLUME_CORRUPTED;\r
279 }\r
280\r
281 File = (EFI_FFS_FILE_HEADER*)(UINTN) CurrentAddress;\r
282 Size = *(UINT32*) File->Size & 0xffffff;\r
283 if (Size < (sizeof (*File) + sizeof (EFI_COMMON_SECTION_HEADER))) {\r
284 return EFI_VOLUME_CORRUPTED;\r
285 }\r
bb4aa855
JJ
286\r
287 EndOfFile = CurrentAddress + Size;\r
288 if (EndOfFile > EndOfFirmwareVolume) {\r
289 return EFI_VOLUME_CORRUPTED;\r
290 }\r
291\r
292 //\r
293 // Look for the request file type\r
294 //\r
295 if (File->Type != FileType) {\r
bb4aa855
JJ
296 continue;\r
297 }\r
298\r
299 Status = FindFfsSectionInSections (\r
300 (VOID*) (File + 1),\r
301 (UINTN) EndOfFile - (UINTN) (File + 1),\r
302 SectionType,\r
303 FoundSection\r
304 );\r
305 if (!EFI_ERROR (Status) || (Status == EFI_VOLUME_CORRUPTED)) {\r
306 return Status;\r
307 }\r
308 }\r
309}\r
310\r
311/**\r
312 Locates the compressed main firmware volume and decompresses it.\r
313\r
314 @param[in,out] Fv On input, the firmware volume to search\r
b36f701d 315 On output, the decompressed BOOT/PEI FV\r
bb4aa855
JJ
316\r
317 @retval EFI_SUCCESS The file and section was found\r
318 @retval EFI_NOT_FOUND The file and section was not found\r
319 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted\r
320\r
321**/\r
322EFI_STATUS\r
b36f701d 323DecompressMemFvs (\r
bb4aa855
JJ
324 IN OUT EFI_FIRMWARE_VOLUME_HEADER **Fv\r
325 )\r
326{\r
327 EFI_STATUS Status;\r
328 EFI_GUID_DEFINED_SECTION *Section;\r
329 UINT32 OutputBufferSize;\r
330 UINT32 ScratchBufferSize;\r
331 UINT16 SectionAttribute;\r
332 UINT32 AuthenticationStatus;\r
333 VOID *OutputBuffer;\r
334 VOID *ScratchBuffer;\r
b36f701d
JJ
335 EFI_FIRMWARE_VOLUME_IMAGE_SECTION *FvSection;\r
336 EFI_FIRMWARE_VOLUME_HEADER *PeiMemFv;\r
337 EFI_FIRMWARE_VOLUME_HEADER *DxeMemFv;\r
bb4aa855 338\r
b36f701d 339 FvSection = (EFI_FIRMWARE_VOLUME_IMAGE_SECTION*) NULL;\r
bb4aa855
JJ
340\r
341 Status = FindFfsFileAndSection (\r
342 *Fv,\r
343 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,\r
344 EFI_SECTION_GUID_DEFINED,\r
345 (EFI_COMMON_SECTION_HEADER**) &Section\r
346 );\r
347 if (EFI_ERROR (Status)) {\r
348 DEBUG ((EFI_D_ERROR, "Unable to find GUID defined section\n"));\r
349 return Status;\r
350 }\r
351\r
352 Status = ExtractGuidedSectionGetInfo (\r
353 Section,\r
354 &OutputBufferSize,\r
355 &ScratchBufferSize,\r
356 &SectionAttribute\r
357 );\r
358 if (EFI_ERROR (Status)) {\r
359 DEBUG ((EFI_D_ERROR, "Unable to GetInfo for GUIDed section\n"));\r
360 return Status;\r
361 }\r
362\r
b36f701d 363 OutputBuffer = (VOID*) ((UINT8*)(UINTN) PcdGet32 (PcdOvmfDxeMemFvBase) + SIZE_1MB);\r
bb4aa855 364 ScratchBuffer = ALIGN_POINTER ((UINT8*) OutputBuffer + OutputBufferSize, SIZE_1MB);\r
9beac0d8
LE
365\r
366 DEBUG ((EFI_D_VERBOSE, "%a: OutputBuffer@%p+0x%x ScratchBuffer@%p+0x%x "\r
367 "PcdOvmfDecompressionScratchEnd=0x%x\n", __FUNCTION__, OutputBuffer,\r
368 OutputBufferSize, ScratchBuffer, ScratchBufferSize,\r
369 PcdGet32 (PcdOvmfDecompressionScratchEnd)));\r
370 ASSERT ((UINTN)ScratchBuffer + ScratchBufferSize ==\r
371 PcdGet32 (PcdOvmfDecompressionScratchEnd));\r
372\r
bb4aa855
JJ
373 Status = ExtractGuidedSectionDecode (\r
374 Section,\r
375 &OutputBuffer,\r
376 ScratchBuffer,\r
377 &AuthenticationStatus\r
378 );\r
379 if (EFI_ERROR (Status)) {\r
380 DEBUG ((EFI_D_ERROR, "Error during GUID section decode\n"));\r
381 return Status;\r
382 }\r
383\r
b36f701d 384 Status = FindFfsSectionInstance (\r
bb4aa855
JJ
385 OutputBuffer,\r
386 OutputBufferSize,\r
387 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
b36f701d
JJ
388 0,\r
389 (EFI_COMMON_SECTION_HEADER**) &FvSection\r
bb4aa855
JJ
390 );\r
391 if (EFI_ERROR (Status)) {\r
b36f701d 392 DEBUG ((EFI_D_ERROR, "Unable to find PEI FV section\n"));\r
bb4aa855
JJ
393 return Status;\r
394 }\r
395\r
b36f701d
JJ
396 ASSERT (SECTION_SIZE (FvSection) ==\r
397 (PcdGet32 (PcdOvmfPeiMemFvSize) + sizeof (*FvSection)));\r
398 ASSERT (FvSection->Type == EFI_SECTION_FIRMWARE_VOLUME_IMAGE);\r
bb4aa855 399\r
b36f701d
JJ
400 PeiMemFv = (EFI_FIRMWARE_VOLUME_HEADER*)(UINTN) PcdGet32 (PcdOvmfPeiMemFvBase);\r
401 CopyMem (PeiMemFv, (VOID*) (FvSection + 1), PcdGet32 (PcdOvmfPeiMemFvSize));\r
402\r
403 if (PeiMemFv->Signature != EFI_FVH_SIGNATURE) {\r
404 DEBUG ((EFI_D_ERROR, "Extracted FV at %p does not have FV header signature\n", PeiMemFv));\r
405 CpuDeadLoop ();\r
406 return EFI_VOLUME_CORRUPTED;\r
407 }\r
408\r
409 Status = FindFfsSectionInstance (\r
410 OutputBuffer,\r
411 OutputBufferSize,\r
412 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
413 1,\r
414 (EFI_COMMON_SECTION_HEADER**) &FvSection\r
415 );\r
416 if (EFI_ERROR (Status)) {\r
417 DEBUG ((EFI_D_ERROR, "Unable to find DXE FV section\n"));\r
418 return Status;\r
419 }\r
420\r
421 ASSERT (FvSection->Type == EFI_SECTION_FIRMWARE_VOLUME_IMAGE);\r
422 ASSERT (SECTION_SIZE (FvSection) ==\r
423 (PcdGet32 (PcdOvmfDxeMemFvSize) + sizeof (*FvSection)));\r
424\r
425 DxeMemFv = (EFI_FIRMWARE_VOLUME_HEADER*)(UINTN) PcdGet32 (PcdOvmfDxeMemFvBase);\r
426 CopyMem (DxeMemFv, (VOID*) (FvSection + 1), PcdGet32 (PcdOvmfDxeMemFvSize));\r
427\r
428 if (DxeMemFv->Signature != EFI_FVH_SIGNATURE) {\r
429 DEBUG ((EFI_D_ERROR, "Extracted FV at %p does not have FV header signature\n", DxeMemFv));\r
bb4aa855
JJ
430 CpuDeadLoop ();\r
431 return EFI_VOLUME_CORRUPTED;\r
432 }\r
433\r
b36f701d 434 *Fv = PeiMemFv;\r
bb4aa855
JJ
435 return EFI_SUCCESS;\r
436}\r
437\r
438/**\r
439 Locates the PEI Core entry point address\r
440\r
441 @param[in] Fv The firmware volume to search\r
442 @param[out] PeiCoreEntryPoint The entry point of the PEI Core image\r
443\r
444 @retval EFI_SUCCESS The file and section was found\r
445 @retval EFI_NOT_FOUND The file and section was not found\r
446 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted\r
447\r
448**/\r
449EFI_STATUS\r
bb4aa855
JJ
450FindPeiCoreImageBaseInFv (\r
451 IN EFI_FIRMWARE_VOLUME_HEADER *Fv,\r
452 OUT EFI_PHYSICAL_ADDRESS *PeiCoreImageBase\r
453 )\r
454{\r
455 EFI_STATUS Status;\r
456 EFI_COMMON_SECTION_HEADER *Section;\r
457\r
458 Status = FindFfsFileAndSection (\r
459 Fv,\r
460 EFI_FV_FILETYPE_PEI_CORE,\r
461 EFI_SECTION_PE32,\r
462 &Section\r
463 );\r
464 if (EFI_ERROR (Status)) {\r
465 Status = FindFfsFileAndSection (\r
466 Fv,\r
467 EFI_FV_FILETYPE_PEI_CORE,\r
468 EFI_SECTION_TE,\r
469 &Section\r
470 );\r
471 if (EFI_ERROR (Status)) {\r
472 DEBUG ((EFI_D_ERROR, "Unable to find PEI Core image\n"));\r
473 return Status;\r
474 }\r
475 }\r
476\r
477 *PeiCoreImageBase = (EFI_PHYSICAL_ADDRESS)(UINTN)(Section + 1);\r
478 return EFI_SUCCESS;\r
479}\r
480\r
a781f709
JJ
481\r
482/**\r
483 Reads 8-bits of CMOS data.\r
484\r
485 Reads the 8-bits of CMOS data at the location specified by Index.\r
486 The 8-bit read value is returned.\r
487\r
488 @param Index The CMOS location to read.\r
489\r
490 @return The value read.\r
491\r
492**/\r
493STATIC\r
494UINT8\r
495CmosRead8 (\r
496 IN UINTN Index\r
497 )\r
498{\r
499 IoWrite8 (0x70, (UINT8) Index);\r
500 return IoRead8 (0x71);\r
501}\r
502\r
503\r
504STATIC\r
505BOOLEAN\r
506IsS3Resume (\r
507 VOID\r
508 )\r
509{\r
510 return (CmosRead8 (0xF) == 0xFE);\r
511}\r
512\r
513\r
514STATIC\r
515EFI_STATUS\r
516GetS3ResumePeiFv (\r
517 IN OUT EFI_FIRMWARE_VOLUME_HEADER **PeiFv\r
518 )\r
519{\r
520 *PeiFv = (EFI_FIRMWARE_VOLUME_HEADER*)(UINTN) PcdGet32 (PcdOvmfPeiMemFvBase);\r
521 return EFI_SUCCESS;\r
522}\r
523\r
524\r
bb4aa855
JJ
525/**\r
526 Locates the PEI Core entry point address\r
527\r
528 @param[in,out] Fv The firmware volume to search\r
529 @param[out] PeiCoreEntryPoint The entry point of the PEI Core image\r
530\r
531 @retval EFI_SUCCESS The file and section was found\r
532 @retval EFI_NOT_FOUND The file and section was not found\r
533 @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted\r
534\r
535**/\r
536VOID\r
bb4aa855
JJ
537FindPeiCoreImageBase (\r
538 IN OUT EFI_FIRMWARE_VOLUME_HEADER **BootFv,\r
539 OUT EFI_PHYSICAL_ADDRESS *PeiCoreImageBase\r
540 )\r
541{\r
efb0f16e
LE
542 BOOLEAN S3Resume;\r
543\r
bb4aa855
JJ
544 *PeiCoreImageBase = 0;\r
545\r
efb0f16e
LE
546 S3Resume = IsS3Resume ();\r
547 if (S3Resume && !FeaturePcdGet (PcdSmmSmramRequire)) {\r
548 //\r
549 // A malicious runtime OS may have injected something into our previously\r
550 // decoded PEI FV, but we don't care about that unless SMM/SMRAM is required.\r
551 //\r
a781f709
JJ
552 DEBUG ((EFI_D_VERBOSE, "SEC: S3 resume\n"));\r
553 GetS3ResumePeiFv (BootFv);\r
554 } else {\r
efb0f16e
LE
555 //\r
556 // We're either not resuming, or resuming "securely" -- we'll decompress\r
557 // both PEI FV and DXE FV from pristine flash.\r
558 //\r
559 DEBUG ((EFI_D_VERBOSE, "SEC: %a\n",\r
560 S3Resume ? "S3 resume (with PEI decompression)" : "Normal boot"));\r
a781f709 561 FindMainFv (BootFv);\r
bb4aa855 562\r
a781f709
JJ
563 DecompressMemFvs (BootFv);\r
564 }\r
bb4aa855
JJ
565\r
566 FindPeiCoreImageBaseInFv (*BootFv, PeiCoreImageBase);\r
567}\r
568\r
569/**\r
570 Find core image base.\r
571\r
572**/\r
573EFI_STATUS\r
bb4aa855
JJ
574FindImageBase (\r
575 IN EFI_FIRMWARE_VOLUME_HEADER *BootFirmwareVolumePtr,\r
576 OUT EFI_PHYSICAL_ADDRESS *SecCoreImageBase\r
577 )\r
578{\r
579 EFI_PHYSICAL_ADDRESS CurrentAddress;\r
580 EFI_PHYSICAL_ADDRESS EndOfFirmwareVolume;\r
581 EFI_FFS_FILE_HEADER *File;\r
582 UINT32 Size;\r
583 EFI_PHYSICAL_ADDRESS EndOfFile;\r
584 EFI_COMMON_SECTION_HEADER *Section;\r
585 EFI_PHYSICAL_ADDRESS EndOfSection;\r
586\r
587 *SecCoreImageBase = 0;\r
588\r
589 CurrentAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) BootFirmwareVolumePtr;\r
590 EndOfFirmwareVolume = CurrentAddress + BootFirmwareVolumePtr->FvLength;\r
591\r
592 //\r
593 // Loop through the FFS files in the Boot Firmware Volume\r
594 //\r
595 for (EndOfFile = CurrentAddress + BootFirmwareVolumePtr->HeaderLength; ; ) {\r
596\r
597 CurrentAddress = (EndOfFile + 7) & 0xfffffffffffffff8ULL;\r
598 if (CurrentAddress > EndOfFirmwareVolume) {\r
599 return EFI_NOT_FOUND;\r
600 }\r
601\r
602 File = (EFI_FFS_FILE_HEADER*)(UINTN) CurrentAddress;\r
603 Size = *(UINT32*) File->Size & 0xffffff;\r
604 if (Size < sizeof (*File)) {\r
605 return EFI_NOT_FOUND;\r
606 }\r
607\r
608 EndOfFile = CurrentAddress + Size;\r
609 if (EndOfFile > EndOfFirmwareVolume) {\r
610 return EFI_NOT_FOUND;\r
611 }\r
612\r
613 //\r
614 // Look for SEC Core\r
615 //\r
616 if (File->Type != EFI_FV_FILETYPE_SECURITY_CORE) {\r
617 continue;\r
618 }\r
619\r
620 //\r
621 // Loop through the FFS file sections within the FFS file\r
622 //\r
623 EndOfSection = (EFI_PHYSICAL_ADDRESS)(UINTN) (File + 1);\r
624 for (;;) {\r
625 CurrentAddress = (EndOfSection + 3) & 0xfffffffffffffffcULL;\r
626 Section = (EFI_COMMON_SECTION_HEADER*)(UINTN) CurrentAddress;\r
627\r
628 Size = *(UINT32*) Section->Size & 0xffffff;\r
629 if (Size < sizeof (*Section)) {\r
630 return EFI_NOT_FOUND;\r
631 }\r
632\r
633 EndOfSection = CurrentAddress + Size;\r
634 if (EndOfSection > EndOfFile) {\r
635 return EFI_NOT_FOUND;\r
636 }\r
637\r
638 //\r
639 // Look for executable sections\r
640 //\r
641 if (Section->Type == EFI_SECTION_PE32 || Section->Type == EFI_SECTION_TE) {\r
642 if (File->Type == EFI_FV_FILETYPE_SECURITY_CORE) {\r
643 *SecCoreImageBase = (PHYSICAL_ADDRESS) (UINTN) (Section + 1);\r
644 }\r
645 break;\r
646 }\r
647 }\r
648\r
649 //\r
650 // SEC Core image found\r
651 //\r
652 if (*SecCoreImageBase != 0) {\r
653 return EFI_SUCCESS;\r
654 }\r
655 }\r
656}\r
657\r
658/*\r
659 Find and return Pei Core entry point.\r
660\r
661 It also find SEC and PEI Core file debug inforamtion. It will report them if\r
662 remote debug is enabled.\r
663\r
664**/\r
665VOID\r
bb4aa855
JJ
666FindAndReportEntryPoints (\r
667 IN EFI_FIRMWARE_VOLUME_HEADER **BootFirmwareVolumePtr,\r
668 OUT EFI_PEI_CORE_ENTRY_POINT *PeiCoreEntryPoint\r
669 )\r
670{\r
671 EFI_STATUS Status;\r
672 EFI_PHYSICAL_ADDRESS SecCoreImageBase;\r
673 EFI_PHYSICAL_ADDRESS PeiCoreImageBase;\r
674 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
675\r
676 //\r
677 // Find SEC Core and PEI Core image base\r
678 //\r
679 Status = FindImageBase (*BootFirmwareVolumePtr, &SecCoreImageBase);\r
680 ASSERT_EFI_ERROR (Status);\r
681\r
682 FindPeiCoreImageBase (BootFirmwareVolumePtr, &PeiCoreImageBase);\r
683 \r
684 ZeroMem ((VOID *) &ImageContext, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT));\r
685 //\r
686 // Report SEC Core debug information when remote debug is enabled\r
687 //\r
688 ImageContext.ImageAddress = SecCoreImageBase;\r
689 ImageContext.PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageContext.ImageAddress);\r
690 PeCoffLoaderRelocateImageExtraAction (&ImageContext);\r
691\r
692 //\r
693 // Report PEI Core debug information when remote debug is enabled\r
694 //\r
695 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)PeiCoreImageBase;\r
696 ImageContext.PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageContext.ImageAddress);\r
697 PeCoffLoaderRelocateImageExtraAction (&ImageContext);\r
698\r
699 //\r
700 // Find PEI Core entry point\r
701 //\r
702 Status = PeCoffLoaderGetEntryPoint ((VOID *) (UINTN) PeiCoreImageBase, (VOID**) PeiCoreEntryPoint);\r
703 if (EFI_ERROR (Status)) {\r
704 *PeiCoreEntryPoint = 0;\r
705 }\r
706\r
707 return;\r
708}\r
709\r
710VOID\r
711EFIAPI\r
712SecCoreStartupWithStack (\r
713 IN EFI_FIRMWARE_VOLUME_HEADER *BootFv,\r
714 IN VOID *TopOfCurrentStack\r
715 )\r
716{\r
717 EFI_SEC_PEI_HAND_OFF SecCoreData;\r
718 SEC_IDT_TABLE IdtTableInStack;\r
719 IA32_DESCRIPTOR IdtDescriptor;\r
720 UINT32 Index;\r
320b4f08
LE
721 volatile UINT8 *Table;\r
722\r
723 //\r
724 // To ensure SMM can't be compromised on S3 resume, we must force re-init of\r
725 // the BaseExtractGuidedSectionLib. Since this is before library contructors\r
726 // are called, we must use a loop rather than SetMem.\r
727 //\r
728 Table = (UINT8*)(UINTN)FixedPcdGet64 (PcdGuidedExtractHandlerTableAddress);\r
729 for (Index = 0;\r
730 Index < FixedPcdGet32 (PcdGuidedExtractHandlerTableSize);\r
731 ++Index) {\r
732 Table[Index] = 0;\r
733 }\r
bb4aa855
JJ
734\r
735 ProcessLibraryConstructorList (NULL, NULL);\r
736\r
c67178b7 737 DEBUG ((EFI_D_INFO,\r
bb4aa855
JJ
738 "SecCoreStartupWithStack(0x%x, 0x%x)\n",\r
739 (UINT32)(UINTN)BootFv,\r
740 (UINT32)(UINTN)TopOfCurrentStack\r
741 ));\r
742\r
743 //\r
744 // Initialize floating point operating environment\r
745 // to be compliant with UEFI spec.\r
746 //\r
747 InitializeFloatingPointUnits ();\r
748\r
749 //\r
750 // Initialize IDT\r
751 // \r
752 IdtTableInStack.PeiService = NULL;\r
753 for (Index = 0; Index < SEC_IDT_ENTRY_COUNT; Index ++) {\r
754 CopyMem (&IdtTableInStack.IdtTable[Index], &mIdtEntryTemplate, sizeof (mIdtEntryTemplate));\r
755 }\r
756\r
757 IdtDescriptor.Base = (UINTN)&IdtTableInStack.IdtTable;\r
758 IdtDescriptor.Limit = (UINT16)(sizeof (IdtTableInStack.IdtTable) - 1);\r
759\r
760 AsmWriteIdtr (&IdtDescriptor);\r
761\r
b382ede3
JJ
762#if defined (MDE_CPU_X64)\r
763 //\r
764 // ASSERT that the Page Tables were set by the reset vector code to\r
765 // the address we expect.\r
766 //\r
767 ASSERT (AsmReadCr3 () == (UINTN) PcdGet32 (PcdOvmfSecPageTablesBase));\r
768#endif\r
769\r
bb4aa855
JJ
770 //\r
771 // |-------------| <-- TopOfCurrentStack\r
772 // | Stack | 32k\r
773 // |-------------|\r
774 // | Heap | 32k\r
775 // |-------------| <-- SecCoreData.TemporaryRamBase\r
776 //\r
777\r
7cb6b0e0
JJ
778 ASSERT ((UINTN) (PcdGet32 (PcdOvmfSecPeiTempRamBase) +\r
779 PcdGet32 (PcdOvmfSecPeiTempRamSize)) ==\r
780 (UINTN) TopOfCurrentStack);\r
781\r
bb4aa855
JJ
782 //\r
783 // Initialize SEC hand-off state\r
784 //\r
785 SecCoreData.DataSize = sizeof(EFI_SEC_PEI_HAND_OFF);\r
786\r
7cb6b0e0 787 SecCoreData.TemporaryRamSize = (UINTN) PcdGet32 (PcdOvmfSecPeiTempRamSize);\r
bb4aa855
JJ
788 SecCoreData.TemporaryRamBase = (VOID*)((UINT8 *)TopOfCurrentStack - SecCoreData.TemporaryRamSize);\r
789\r
790 SecCoreData.PeiTemporaryRamBase = SecCoreData.TemporaryRamBase;\r
791 SecCoreData.PeiTemporaryRamSize = SecCoreData.TemporaryRamSize >> 1;\r
792\r
793 SecCoreData.StackBase = (UINT8 *)SecCoreData.TemporaryRamBase + SecCoreData.PeiTemporaryRamSize;\r
794 SecCoreData.StackSize = SecCoreData.TemporaryRamSize >> 1;\r
795\r
796 SecCoreData.BootFirmwareVolumeBase = BootFv;\r
797 SecCoreData.BootFirmwareVolumeSize = (UINTN) BootFv->FvLength;\r
798\r
799 //\r
800 // Make sure the 8259 is masked before initializing the Debug Agent and the debug timer is enabled\r
801 //\r
802 IoWrite8 (0x21, 0xff);\r
803 IoWrite8 (0xA1, 0xff);\r
f3e34b9d
MK
804\r
805 //\r
806 // Initialize Local APIC Timer hardware and disable Local APIC Timer\r
807 // interrupts before initializing the Debug Agent and the debug timer is\r
808 // enabled.\r
809 //\r
810 InitializeApicTimer (0, MAX_UINT32, TRUE, 5);\r
811 DisableApicTimerInterrupt ();\r
bb4aa855
JJ
812 \r
813 //\r
814 // Initialize Debug Agent to support source level debug in SEC/PEI phases before memory ready.\r
815 //\r
816 InitializeDebugAgent (DEBUG_AGENT_INIT_PREMEM_SEC, &SecCoreData, SecStartupPhase2);\r
817}\r
818 \r
819/**\r
820 Caller provided function to be invoked at the end of InitializeDebugAgent().\r
821\r
822 Entry point to the C language phase of SEC. After the SEC assembly\r
823 code has initialized some temporary memory and set up the stack,\r
824 the control is transferred to this function.\r
825\r
826 @param[in] Context The first input parameter of InitializeDebugAgent().\r
827\r
828**/\r
829VOID\r
830EFIAPI\r
831SecStartupPhase2(\r
832 IN VOID *Context\r
833 )\r
834{\r
835 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
836 EFI_FIRMWARE_VOLUME_HEADER *BootFv;\r
837 EFI_PEI_CORE_ENTRY_POINT PeiCoreEntryPoint;\r
838 \r
839 SecCoreData = (EFI_SEC_PEI_HAND_OFF *) Context;\r
840 \r
841 //\r
842 // Find PEI Core entry point. It will report SEC and Pei Core debug information if remote debug\r
843 // is enabled.\r
844 //\r
845 BootFv = (EFI_FIRMWARE_VOLUME_HEADER *)SecCoreData->BootFirmwareVolumeBase;\r
846 FindAndReportEntryPoints (&BootFv, &PeiCoreEntryPoint);\r
847 SecCoreData->BootFirmwareVolumeBase = BootFv;\r
848 SecCoreData->BootFirmwareVolumeSize = (UINTN) BootFv->FvLength;\r
849\r
850 //\r
851 // Transfer the control to the PEI core\r
852 //\r
853 (*PeiCoreEntryPoint) (SecCoreData, (EFI_PEI_PPI_DESCRIPTOR *)&mPrivateDispatchTable);\r
854 \r
855 //\r
856 // If we get here then the PEI Core returned, which is not recoverable.\r
857 //\r
858 ASSERT (FALSE);\r
859 CpuDeadLoop ();\r
860}\r
861\r
862EFI_STATUS\r
863EFIAPI\r
864TemporaryRamMigration (\r
865 IN CONST EFI_PEI_SERVICES **PeiServices,\r
866 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,\r
867 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,\r
868 IN UINTN CopySize\r
869 )\r
870{\r
871 IA32_DESCRIPTOR IdtDescriptor;\r
872 VOID *OldHeap;\r
873 VOID *NewHeap;\r
874 VOID *OldStack;\r
875 VOID *NewStack;\r
876 DEBUG_AGENT_CONTEXT_POSTMEM_SEC DebugAgentContext;\r
877 BOOLEAN OldStatus;\r
878 BASE_LIBRARY_JUMP_BUFFER JumpBuffer;\r
879 \r
c67178b7 880 DEBUG ((EFI_D_INFO,\r
6394c35a
LE
881 "TemporaryRamMigration(0x%Lx, 0x%Lx, 0x%Lx)\n",\r
882 TemporaryMemoryBase,\r
883 PermanentMemoryBase,\r
884 (UINT64)CopySize\r
c67178b7 885 ));\r
bb4aa855
JJ
886 \r
887 OldHeap = (VOID*)(UINTN)TemporaryMemoryBase;\r
888 NewHeap = (VOID*)((UINTN)PermanentMemoryBase + (CopySize >> 1));\r
889 \r
890 OldStack = (VOID*)((UINTN)TemporaryMemoryBase + (CopySize >> 1));\r
891 NewStack = (VOID*)(UINTN)PermanentMemoryBase;\r
892\r
893 DebugAgentContext.HeapMigrateOffset = (UINTN)NewHeap - (UINTN)OldHeap;\r
894 DebugAgentContext.StackMigrateOffset = (UINTN)NewStack - (UINTN)OldStack;\r
895 \r
896 OldStatus = SaveAndSetDebugTimerInterrupt (FALSE);\r
897 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC, (VOID *) &DebugAgentContext, NULL);\r
898\r
899 //\r
900 // Migrate Heap\r
901 //\r
902 CopyMem (NewHeap, OldHeap, CopySize >> 1);\r
903\r
904 //\r
905 // Migrate Stack\r
906 //\r
907 CopyMem (NewStack, OldStack, CopySize >> 1);\r
908 \r
909 //\r
910 // Rebase IDT table in permanent memory\r
911 //\r
912 AsmReadIdtr (&IdtDescriptor);\r
913 IdtDescriptor.Base = IdtDescriptor.Base - (UINTN)OldStack + (UINTN)NewStack;\r
914\r
915 AsmWriteIdtr (&IdtDescriptor);\r
916\r
917 //\r
918 // Use SetJump()/LongJump() to switch to a new stack.\r
919 // \r
920 if (SetJump (&JumpBuffer) == 0) {\r
921#if defined (MDE_CPU_IA32)\r
922 JumpBuffer.Esp = JumpBuffer.Esp + DebugAgentContext.StackMigrateOffset;\r
923#endif \r
924#if defined (MDE_CPU_X64)\r
925 JumpBuffer.Rsp = JumpBuffer.Rsp + DebugAgentContext.StackMigrateOffset;\r
926#endif \r
927 LongJump (&JumpBuffer, (UINTN)-1);\r
928 }\r
929\r
930 SaveAndSetDebugTimerInterrupt (OldStatus);\r
931\r
932 return EFI_SUCCESS;\r
933}\r
934\r