2 This is the code for Boot Script Executer module.
4 This driver is dispatched by Dxe core and the driver will reload itself to ACPI reserved memory
5 in the entry point. The functionality is to interpret and restore the S3 boot script
7 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
8 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
10 This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include "ScriptExecute.h"
22 EFI_GUID mBootScriptExecutorImageGuid
= {
23 0x9a8d3433, 0x9fe8, 0x42b6, { 0x87, 0xb, 0x1e, 0x31, 0xc8, 0x4e, 0xbe, 0x3b }
26 BOOLEAN mPage1GSupport
= FALSE
;
27 UINT64 mAddressEncMask
= 0;
30 Entry function of Boot script exector. This function will be executed in
32 This function should not return, because it is invoked by switch stack.
34 @param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT
35 @param PeiS3ResumeState a pointer to a structure of PEI_S3_RESUME_STATE
37 @retval EFI_INVALID_PARAMETER - OS waking vector not found
38 @retval EFI_UNSUPPORTED - something wrong when we resume to OS
42 S3BootScriptExecutorEntryFunction (
43 IN ACPI_S3_CONTEXT
*AcpiS3Context
,
44 IN PEI_S3_RESUME_STATE
*PeiS3ResumeState
47 EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE
*Facs
;
50 UINTN TempStack
[0x10];
51 UINTN AsmTransferControl16Address
;
52 IA32_DESCRIPTOR IdtDescriptor
;
55 // Disable interrupt of Debug timer, since new IDT table cannot handle it.
57 SaveAndSetDebugTimerInterrupt (FALSE
);
59 AsmReadIdtr (&IdtDescriptor
);
61 // Restore IDT for debug
63 SetIdtEntry (AcpiS3Context
);
66 // Initialize Debug Agent to support source level debug in S3 path, it will disable interrupt and Debug Timer.
68 InitializeDebugAgent (DEBUG_AGENT_INIT_S3
, (VOID
*)&IdtDescriptor
, NULL
);
71 // Because not install BootScriptExecute PPI(used just in this module), So just pass NULL
72 // for that parameter.
74 Status
= S3BootScriptExecute ();
77 // If invalid script table or opcode in S3 boot script table.
79 ASSERT_EFI_ERROR (Status
);
81 if (EFI_ERROR (Status
)) {
89 // Get ACPI Table Address
91 Facs
= (EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE
*) ((UINTN
) (AcpiS3Context
->AcpiFacsTable
));
94 // We need turn back to S3Resume - install boot script done ppi and report status code on S3resume.
96 if (PeiS3ResumeState
!= 0) {
98 // Need report status back to S3ResumePeim.
99 // If boot script execution is failed, S3ResumePeim wil report the error status code.
101 PeiS3ResumeState
->ReturnStatus
= (UINT64
)(UINTN
)Status
;
102 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode
)) {
106 DEBUG ((EFI_D_ERROR
, "Call AsmDisablePaging64() to return to S3 Resume in PEI Phase\n"));
107 PeiS3ResumeState
->AsmTransferControl
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AsmTransferControl32
;
109 if ((Facs
!= NULL
) &&
110 (Facs
->Signature
== EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE
) &&
111 (Facs
->FirmwareWakingVector
!= 0) ) {
113 // more step needed - because relative address is handled differently between X64 and IA32.
115 AsmTransferControl16Address
= (UINTN
)AsmTransferControl16
;
116 AsmFixAddress16
= (UINT32
)AsmTransferControl16Address
;
117 AsmJmpAddr32
= (UINT32
)((Facs
->FirmwareWakingVector
& 0xF) | ((Facs
->FirmwareWakingVector
& 0xFFFF0) << 12));
121 PeiS3ResumeState
->ReturnCs
,
122 (UINT32
)PeiS3ResumeState
->ReturnEntryPoint
,
123 (UINT32
)(UINTN
)AcpiS3Context
,
124 (UINT32
)(UINTN
)PeiS3ResumeState
,
125 (UINT32
)PeiS3ResumeState
->ReturnStackPointer
131 DEBUG ((EFI_D_ERROR
, "Call SwitchStack() to return to S3 Resume in PEI Phase\n"));
132 PeiS3ResumeState
->AsmTransferControl
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AsmTransferControl
;
135 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)PeiS3ResumeState
->ReturnEntryPoint
,
136 (VOID
*)(UINTN
)AcpiS3Context
,
137 (VOID
*)(UINTN
)PeiS3ResumeState
,
138 (VOID
*)(UINTN
)PeiS3ResumeState
->ReturnStackPointer
146 return EFI_UNSUPPORTED
;
150 // S3ResumePeim does not provide a way to jump back to itself, so resume to OS here directly
152 if (Facs
->XFirmwareWakingVector
!= 0) {
154 // Switch to native waking vector
156 TempStackTop
= (UINTN
)&TempStack
+ sizeof(TempStack
);
157 if ((Facs
->Version
== EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_VERSION
) &&
158 ((Facs
->Flags
& EFI_ACPI_4_0_64BIT_WAKE_SUPPORTED_F
) != 0) &&
159 ((Facs
->Flags
& EFI_ACPI_4_0_OSPM_64BIT_WAKE__F
) != 0)) {
161 // X64 long mode waking vector
163 DEBUG (( EFI_D_ERROR
, "Transfer to 64bit OS waking vector - %x\r\n", (UINTN
)Facs
->XFirmwareWakingVector
));
164 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode
)) {
166 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)Facs
->XFirmwareWakingVector
,
169 (VOID
*)(UINTN
)TempStackTop
172 // Unsupported for 32bit DXE, 64bit OS vector
173 DEBUG (( EFI_D_ERROR
, "Unsupported for 32bit DXE transfer to 64bit OS waking vector!\r\n"));
178 // IA32 protected mode waking vector (Page disabled)
180 DEBUG (( EFI_D_ERROR
, "Transfer to 32bit OS waking vector - %x\r\n", (UINTN
)Facs
->XFirmwareWakingVector
));
181 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode
)) {
184 (UINT32
)Facs
->XFirmwareWakingVector
,
191 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)Facs
->XFirmwareWakingVector
,
194 (VOID
*)(UINTN
)TempStackTop
200 // 16bit Realmode waking vector
202 DEBUG (( EFI_D_ERROR
, "Transfer to 16bit OS waking vector - %x\r\n", (UINTN
)Facs
->FirmwareWakingVector
));
203 AsmTransferControl (Facs
->FirmwareWakingVector
, 0x0);
210 return EFI_UNSUPPORTED
;
214 Register image to memory profile.
216 @param FileName File name of the image.
217 @param ImageBase Image base address.
218 @param ImageSize Image size.
219 @param FileType File type of the image.
223 RegisterMemoryProfileImage (
224 IN EFI_GUID
*FileName
,
225 IN PHYSICAL_ADDRESS ImageBase
,
227 IN EFI_FV_FILETYPE FileType
231 EDKII_MEMORY_PROFILE_PROTOCOL
*ProfileProtocol
;
232 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*FilePath
;
233 UINT8 TempBuffer
[sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
) + sizeof (EFI_DEVICE_PATH_PROTOCOL
)];
235 if ((PcdGet8 (PcdMemoryProfilePropertyMask
) & BIT0
) != 0) {
237 FilePath
= (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*)TempBuffer
;
238 Status
= gBS
->LocateProtocol (&gEdkiiMemoryProfileGuid
, NULL
, (VOID
**) &ProfileProtocol
);
239 if (!EFI_ERROR (Status
)) {
240 EfiInitializeFwVolDevicepathNode (FilePath
, FileName
);
241 SetDevicePathEndNode (FilePath
+ 1);
243 Status
= ProfileProtocol
->RegisterImage (
245 (EFI_DEVICE_PATH_PROTOCOL
*) FilePath
,
255 This is the Event notification function to reload BootScriptExecutor image
256 to RESERVED mem and save it to LockBox.
258 @param Event Pointer to this event
259 @param Context Event handler private data
263 ReadyToLockEventNotify (
272 EFI_HANDLE NewImageHandle
;
274 EFI_PHYSICAL_ADDRESS FfsBuffer
;
275 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
277 Status
= gBS
->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid
, NULL
, &Interface
);
278 if (EFI_ERROR (Status
)) {
283 // A workaround: Here we install a dummy handle
285 NewImageHandle
= NULL
;
286 Status
= gBS
->InstallProtocolInterface (
289 EFI_NATIVE_INTERFACE
,
292 ASSERT_EFI_ERROR (Status
);
295 // Reload BootScriptExecutor image itself to RESERVED mem
297 Status
= GetSectionFromAnyFv (
304 ASSERT_EFI_ERROR (Status
);
305 ImageContext
.Handle
= Buffer
;
306 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
308 // Get information about the image being loaded
310 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
311 ASSERT_EFI_ERROR (Status
);
312 if (ImageContext
.SectionAlignment
> EFI_PAGE_SIZE
) {
313 Pages
= EFI_SIZE_TO_PAGES ((UINTN
) (ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
));
315 Pages
= EFI_SIZE_TO_PAGES ((UINTN
) ImageContext
.ImageSize
);
317 FfsBuffer
= 0xFFFFFFFF;
318 Status
= gBS
->AllocatePages (
320 EfiReservedMemoryType
,
324 ASSERT_EFI_ERROR (Status
);
325 ImageContext
.ImageAddress
= (PHYSICAL_ADDRESS
)(UINTN
)FfsBuffer
;
327 // Align buffer on section boundary
329 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
330 ImageContext
.ImageAddress
&= ~((EFI_PHYSICAL_ADDRESS
)(ImageContext
.SectionAlignment
- 1));
332 // Load the image to our new buffer
334 Status
= PeCoffLoaderLoadImage (&ImageContext
);
335 ASSERT_EFI_ERROR (Status
);
338 // Relocate the image in our new buffer
340 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
341 ASSERT_EFI_ERROR (Status
);
344 // Free the buffer allocated by ReadSection since the image has been relocated in the new buffer
346 gBS
->FreePool (Buffer
);
349 // Flush the instruction cache so the image data is written before we execute it
351 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
353 RegisterMemoryProfileImage (
355 ImageContext
.ImageAddress
,
356 ImageContext
.ImageSize
,
357 EFI_FV_FILETYPE_DRIVER
360 Status
= ((EFI_IMAGE_ENTRY_POINT
)(UINTN
)(ImageContext
.EntryPoint
)) (NewImageHandle
, gST
);
361 ASSERT_EFI_ERROR (Status
);
364 // Additional step for BootScript integrity
365 // Save BootScriptExecutor image
367 Status
= SaveLockBox (
368 &mBootScriptExecutorImageGuid
,
369 (VOID
*)(UINTN
)ImageContext
.ImageAddress
,
370 (UINTN
)ImageContext
.ImageSize
372 ASSERT_EFI_ERROR (Status
);
374 Status
= SetLockBoxAttributes (&mBootScriptExecutorImageGuid
, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE
);
375 ASSERT_EFI_ERROR (Status
);
377 gBS
->CloseEvent (Event
);
381 Entrypoint of Boot script exector driver, this function will be executed in
382 normal boot phase and invoked by DXE dispatch.
384 @param[in] ImageHandle The firmware allocated handle for the EFI image.
385 @param[in] SystemTable A pointer to the EFI System Table.
387 @retval EFI_SUCCESS The entry point is executed successfully.
388 @retval other Some error occurs when executing this entry point.
392 BootScriptExecutorEntryPoint (
393 IN EFI_HANDLE ImageHandle
,
394 IN EFI_SYSTEM_TABLE
*SystemTable
399 BOOT_SCRIPT_EXECUTOR_VARIABLE
*EfiBootScriptExecutorVariable
;
400 EFI_PHYSICAL_ADDRESS BootScriptExecutorBuffer
;
403 EFI_EVENT ReadyToLockEvent
;
408 if (!PcdGetBool (PcdAcpiS3Enable
)) {
409 return EFI_UNSUPPORTED
;
413 // Make sure AddressEncMask is contained to smallest supported address field.
415 mAddressEncMask
= PcdGet64 (PcdPteMemoryEncryptionAddressOrMask
) & PAGING_1G_ADDRESS_MASK_64
;
418 // Test if the gEfiCallerIdGuid of this image is already installed. if not, the entry
419 // point is loaded by DXE code which is the first time loaded. or else, it is already
420 // be reloaded be itself.This is a work-around
422 Status
= gBS
->LocateProtocol (&gEfiCallerIdGuid
, NULL
, &DevicePath
);
423 if (EFI_ERROR (Status
)) {
425 // Create ReadyToLock event to reload BootScriptExecutor image
426 // to RESERVED mem and save it to LockBox.
428 ReadyToLockEvent
= EfiCreateProtocolNotifyEvent (
429 &gEfiDxeSmmReadyToLockProtocolGuid
,
431 ReadyToLockEventNotify
,
435 ASSERT (ReadyToLockEvent
!= NULL
);
438 // the entry point is invoked after reloading. following code only run in RESERVED mem
440 if (PcdGetBool(PcdUse1GPageTable
)) {
441 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
442 if (RegEax
>= 0x80000001) {
443 AsmCpuid (0x80000001, NULL
, NULL
, NULL
, &RegEdx
);
444 if ((RegEdx
& BIT26
) != 0) {
445 mPage1GSupport
= TRUE
;
450 BufferSize
= sizeof (BOOT_SCRIPT_EXECUTOR_VARIABLE
);
452 BootScriptExecutorBuffer
= 0xFFFFFFFF;
453 Pages
= EFI_SIZE_TO_PAGES(BufferSize
);
454 Status
= gBS
->AllocatePages (
456 EfiReservedMemoryType
,
458 &BootScriptExecutorBuffer
460 ASSERT_EFI_ERROR (Status
);
462 EfiBootScriptExecutorVariable
= (BOOT_SCRIPT_EXECUTOR_VARIABLE
*)(UINTN
)BootScriptExecutorBuffer
;
463 EfiBootScriptExecutorVariable
->BootScriptExecutorEntrypoint
= (UINTN
) S3BootScriptExecutorEntryFunction
;
465 Status
= SaveLockBox (
466 &gEfiBootScriptExecutorVariableGuid
,
467 &BootScriptExecutorBuffer
,
468 sizeof(BootScriptExecutorBuffer
)
470 ASSERT_EFI_ERROR (Status
);
473 // Additional step for BootScript integrity
474 // Save BootScriptExecutor context
476 Status
= SaveLockBox (
477 &gEfiBootScriptExecutorContextGuid
,
478 EfiBootScriptExecutorVariable
,
479 sizeof(*EfiBootScriptExecutorVariable
)
481 ASSERT_EFI_ERROR (Status
);
483 Status
= SetLockBoxAttributes (&gEfiBootScriptExecutorContextGuid
, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE
);
484 ASSERT_EFI_ERROR (Status
);