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 NVS memory
5 in the entry point. The functionality is to interpret and restore the S3 boot script
7 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include "ScriptExecute.h"
21 EFI_PHYSICAL_ADDRESS mPerfDataMemAddress
;
22 UINT64 mS3BootScriptEntryTick
;
23 UINT64 mScriptStartTick
;
24 UINT64 mScriptEndTick
;
26 EFI_GUID mBootScriptExecutorImageGuid
= {
27 0x9a8d3433, 0x9fe8, 0x42b6, 0x87, 0xb, 0x1e, 0x31, 0xc8, 0x4e, 0xbe, 0x3b
31 The event callback is used to get the base address of boot performance data structure on
32 LegacyBoot event and ExitBootServices event.
34 @param Event The event handle.
35 @param Context The event context.
48 VarSize
= sizeof (EFI_PHYSICAL_ADDRESS
);
49 Status
= gRT
->GetVariable (
51 &gPerformanceProtocolGuid
,
56 if (EFI_ERROR (Status
)) {
57 mPerfDataMemAddress
= 0;
62 Record S3 Script execution time and adjust total S3 resume time for script running.
65 WriteToOsS3PerformanceData (
73 UINT64 ScriptExecuteTicks
;
74 PERF_HEADER
*PerfHeader
;
77 Ticker
= GetPerformanceCounter ();
79 PerfHeader
= (PERF_HEADER
*)(UINTN
)mPerfDataMemAddress
;
80 if (PerfHeader
== NULL
) {
84 Freq
= GetPerformanceCounterProperties (&StartValue
, &EndValue
);
85 Freq
= DivU64x32 (Freq
, 1000);
87 if (EndValue
>= StartValue
) {
88 ScriptExecuteTicks
= mScriptEndTick
- mScriptStartTick
;
89 PerfHeader
->S3Resume
+= Ticker
- mS3BootScriptEntryTick
;
91 ScriptExecuteTicks
= mScriptStartTick
- mScriptEndTick
;
92 PerfHeader
->S3Resume
+= mS3BootScriptEntryTick
- Ticker
;
94 if (PerfHeader
->S3EntryNum
< PERF_PEI_ENTRY_MAX_NUM
) {
95 PerfData
= &PerfHeader
->S3Entry
[PerfHeader
->S3EntryNum
];
96 PerfData
->Duration
= (UINT32
) DivU64x32 (ScriptExecuteTicks
, (UINT32
) Freq
);;
97 AsciiStrnCpy (PerfData
->Token
, "ScriptExec", PERF_TOKEN_LENGTH
);
98 PerfHeader
->S3EntryNum
++;
103 Entry function of Boot script exector. This function will be executed in
105 This function should not return, because it is invoked by switch stack.
107 @param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT
108 @param PeiS3ResumeState a pointer to a structure of PEI_S3_RESUME_STATE
110 @retval EFI_INVALID_PARAMETER - OS waking vector not found
111 @retval EFI_UNSUPPORTED - something wrong when we resume to OS
115 S3BootScriptExecutorEntryFunction (
116 IN ACPI_S3_CONTEXT
*AcpiS3Context
,
117 IN PEI_S3_RESUME_STATE
*PeiS3ResumeState
120 EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE
*Facs
;
123 UINTN TempStack
[0x10];
124 UINTN AsmTransferControl16Address
;
127 mS3BootScriptEntryTick
= GetPerformanceCounter ();
131 // Disable interrupt of Debug timer, since new IDT table cannot handle it.
133 SaveAndSetDebugTimerInterrupt (FALSE
);
136 // Restore IDT for debug
138 SetIdtEntry (AcpiS3Context
);
141 // Initialize Debug Agent to support source level debug in S3 path.
143 InitializeDebugAgent (DEBUG_AGENT_INIT_S3
, NULL
, NULL
);
146 // Because not install BootScriptExecute PPI(used just in this module), So just pass NULL
147 // for that parameter.
150 mScriptStartTick
= GetPerformanceCounter ();
152 Status
= S3BootScriptExecute ();
154 mScriptEndTick
= GetPerformanceCounter ();
156 if (EFI_ERROR (Status
)) {
163 // Get ACPI Table Address
165 Facs
= (EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE
*) ((UINTN
) (AcpiS3Context
->AcpiFacsTable
));
167 if ((Facs
== NULL
) ||
168 (Facs
->Signature
!= EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE
) ||
169 ((Facs
->FirmwareWakingVector
== 0) && (Facs
->XFirmwareWakingVector
== 0)) ) {
171 return EFI_INVALID_PARAMETER
;
175 // We need turn back to S3Resume - install boot script done ppi and report status code on S3resume.
177 if (PeiS3ResumeState
!= 0) {
178 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode
)) {
182 DEBUG ((EFI_D_ERROR
, "Call AsmDisablePaging64() to return to S3 Resume in PEI Phase\n"));
183 PeiS3ResumeState
->AsmTransferControl
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AsmTransferControl32
;
186 // more step needed - because relative address is handled differently between X64 and IA32.
188 AsmTransferControl16Address
= (UINTN
)AsmTransferControl16
;
189 AsmFixAddress16
= (UINT32
)AsmTransferControl16Address
;
190 AsmJmpAddr32
= (UINT32
)((Facs
->FirmwareWakingVector
& 0xF) | ((Facs
->FirmwareWakingVector
& 0xFFFF0) << 12));
193 PeiS3ResumeState
->ReturnCs
,
194 (UINT32
)PeiS3ResumeState
->ReturnEntryPoint
,
195 (UINT32
)(UINTN
)AcpiS3Context
,
196 (UINT32
)(UINTN
)PeiS3ResumeState
,
197 (UINT32
)PeiS3ResumeState
->ReturnStackPointer
203 DEBUG ((EFI_D_ERROR
, "Call SwitchStack() to return to S3 Resume in PEI Phase\n"));
204 PeiS3ResumeState
->AsmTransferControl
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AsmTransferControl
;
207 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)PeiS3ResumeState
->ReturnEntryPoint
,
208 (VOID
*)(UINTN
)AcpiS3Context
,
209 (VOID
*)(UINTN
)PeiS3ResumeState
,
210 (VOID
*)(UINTN
)PeiS3ResumeState
->ReturnStackPointer
218 return EFI_UNSUPPORTED
;
222 WriteToOsS3PerformanceData ();
225 if (Facs
->XFirmwareWakingVector
!= 0) {
227 // Switch to native waking vector
229 TempStackTop
= (UINTN
)&TempStack
+ sizeof(TempStack
);
230 if ((Facs
->Version
== EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_VERSION
) &&
231 ((Facs
->Flags
& EFI_ACPI_4_0_64BIT_WAKE_SUPPORTED_F
) != 0) &&
232 ((Facs
->Flags
& EFI_ACPI_4_0_OSPM_64BIT_WAKE__F
) != 0)) {
234 // X64 long mode waking vector
236 DEBUG (( EFI_D_ERROR
, "Transfer to 64bit OS waking vector - %x\r\n", (UINTN
)Facs
->XFirmwareWakingVector
));
237 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode
)) {
239 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)Facs
->XFirmwareWakingVector
,
242 (VOID
*)(UINTN
)TempStackTop
245 // Unsupported for 32bit DXE, 64bit OS vector
246 DEBUG (( EFI_D_ERROR
, "Unsupported for 32bit DXE transfer to 64bit OS waking vector!\r\n"));
251 // IA32 protected mode waking vector (Page disabled)
253 DEBUG (( EFI_D_ERROR
, "Transfer to 32bit OS waking vector - %x\r\n", (UINTN
)Facs
->XFirmwareWakingVector
));
254 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode
)) {
257 (UINT32
)Facs
->XFirmwareWakingVector
,
264 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)Facs
->XFirmwareWakingVector
,
267 (VOID
*)(UINTN
)TempStackTop
273 // 16bit Realmode waking vector
275 DEBUG (( EFI_D_ERROR
, "Transfer to 16bit OS waking vector - %x\r\n", (UINTN
)Facs
->FirmwareWakingVector
));
276 AsmTransferControl (Facs
->FirmwareWakingVector
, 0x0);
283 return EFI_UNSUPPORTED
;
286 Entrypoint of Boot script exector driver, this function will be executed in
287 normal boot phase and invoked by DXE dispatch.
289 @param[in] ImageHandle The firmware allocated handle for the EFI image.
290 @param[in] SystemTable A pointer to the EFI System Table.
292 @retval EFI_SUCCESS The entry point is executed successfully.
293 @retval other Some error occurs when executing this entry point.
297 BootScriptExecutorEntryPoint (
298 IN EFI_HANDLE ImageHandle
,
299 IN EFI_SYSTEM_TABLE
*SystemTable
305 EFI_PHYSICAL_ADDRESS FfsBuffer
;
306 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
307 BOOT_SCRIPT_EXECUTOR_VARIABLE
*EfiBootScriptExecutorVariable
;
308 EFI_PHYSICAL_ADDRESS BootScriptExecutorBuffer
;
311 EFI_HANDLE NewImageHandle
;
314 // Test if the gEfiCallerIdGuid of this image is already installed. if not, the entry
315 // point is loaded by DXE code which is the first time loaded. or else, it is already
316 // be reloaded be itself.This is a work-around
318 Status
= gBS
->LocateProtocol (&gEfiCallerIdGuid
, NULL
, &DevicePath
);
319 if (EFI_ERROR (Status
)) {
322 // This is the first-time loaded by DXE core. reload itself to NVS mem
325 // A workarouond: Here we install a dummy handle
327 NewImageHandle
= NULL
;
328 Status
= gBS
->InstallProtocolInterface (
331 EFI_NATIVE_INTERFACE
,
335 Status
= GetSectionFromAnyFv (
342 ImageContext
.Handle
= Buffer
;
343 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
345 // Get information about the image being loaded
347 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
348 if (EFI_ERROR (Status
)) {
351 Pages
= EFI_SIZE_TO_PAGES(BufferSize
+ ImageContext
.SectionAlignment
);
352 FfsBuffer
= 0xFFFFFFFF;
353 Status
= gBS
->AllocatePages (
359 if (EFI_ERROR (Status
)) {
360 return EFI_OUT_OF_RESOURCES
;
362 ImageContext
.ImageAddress
= (PHYSICAL_ADDRESS
)(UINTN
)FfsBuffer
;
364 // Align buffer on section boundry
366 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
367 ImageContext
.ImageAddress
&= ~(ImageContext
.SectionAlignment
- 1);
369 // Load the image to our new buffer
371 Status
= PeCoffLoaderLoadImage (&ImageContext
);
372 if (EFI_ERROR (Status
)) {
373 gBS
->FreePages (FfsBuffer
, Pages
);
378 // Relocate the image in our new buffer
380 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
382 if (EFI_ERROR (Status
)) {
383 PeCoffLoaderUnloadImage (&ImageContext
);
384 gBS
->FreePages (FfsBuffer
, Pages
);
388 // Flush the instruction cache so the image data is written before we execute it
390 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
391 Status
= ((EFI_IMAGE_ENTRY_POINT
)(UINTN
)(ImageContext
.EntryPoint
)) (NewImageHandle
, SystemTable
);
392 if (EFI_ERROR (Status
)) {
393 gBS
->FreePages (FfsBuffer
, Pages
);
397 // Additional step for BootScript integrity
398 // Save BootScriptExecutor image
400 Status
= SaveLockBox (
401 &mBootScriptExecutorImageGuid
,
402 (VOID
*)(UINTN
)ImageContext
.ImageAddress
,
403 (UINTN
)ImageContext
.ImageSize
405 ASSERT_EFI_ERROR (Status
);
407 Status
= SetLockBoxAttributes (&mBootScriptExecutorImageGuid
, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE
);
408 ASSERT_EFI_ERROR (Status
);
412 // the entry point is invoked after reloading. following code only run in ACPI NVS
414 BufferSize
= sizeof (BOOT_SCRIPT_EXECUTOR_VARIABLE
);
416 BootScriptExecutorBuffer
= 0xFFFFFFFF;
417 Pages
= EFI_SIZE_TO_PAGES(BufferSize
);
418 Status
= gBS
->AllocatePages (
422 &BootScriptExecutorBuffer
424 if (EFI_ERROR (Status
)) {
425 return EFI_OUT_OF_RESOURCES
;
428 EfiBootScriptExecutorVariable
= (BOOT_SCRIPT_EXECUTOR_VARIABLE
*)(UINTN
)BootScriptExecutorBuffer
;
429 EfiBootScriptExecutorVariable
->BootScriptExecutorEntrypoint
= (UINTN
) S3BootScriptExecutorEntryFunction
;
431 Status
= SaveLockBox (
432 &gEfiBootScriptExecutorVariableGuid
,
433 &BootScriptExecutorBuffer
,
434 sizeof(BootScriptExecutorBuffer
)
436 ASSERT_EFI_ERROR (Status
);
439 // Additional step for BootScript integrity
440 // Save BootScriptExecutor context
442 Status
= SaveLockBox (
443 &gEfiBootScriptExecutorContextGuid
,
444 EfiBootScriptExecutorVariable
,
445 sizeof(*EfiBootScriptExecutorVariable
)
447 ASSERT_EFI_ERROR (Status
);
449 Status
= SetLockBoxAttributes (&gEfiBootScriptExecutorContextGuid
, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE
);
450 ASSERT_EFI_ERROR (Status
);
460 &gEfiEventExitBootServicesGuid
,
464 EfiCreateEventLegacyBootEx(