]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/ScriptExecute.c
Moved the PeiS3ResumeState access when PeiS3ResumeState is not 0, this will fix K9...
[mirror_edk2.git] / MdeModulePkg / Universal / Acpi / BootScriptExecutorDxe / ScriptExecute.c
1 /** @file
2 This is the code for Boot Script Executer module.
3
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
6
7 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
8
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
13
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.
16
17 **/
18
19 #include "ScriptExecute.h"
20
21 EFI_GUID mBootScriptExecutorImageGuid = {
22 0x9a8d3433, 0x9fe8, 0x42b6, { 0x87, 0xb, 0x1e, 0x31, 0xc8, 0x4e, 0xbe, 0x3b }
23 };
24
25 /**
26 Entry function of Boot script exector. This function will be executed in
27 S3 boot path.
28 This function should not return, because it is invoked by switch stack.
29
30 @param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT
31 @param PeiS3ResumeState a pointer to a structure of PEI_S3_RESUME_STATE
32
33 @retval EFI_INVALID_PARAMETER - OS waking vector not found
34 @retval EFI_UNSUPPORTED - something wrong when we resume to OS
35 **/
36 EFI_STATUS
37 EFIAPI
38 S3BootScriptExecutorEntryFunction (
39 IN ACPI_S3_CONTEXT *AcpiS3Context,
40 IN PEI_S3_RESUME_STATE *PeiS3ResumeState
41 )
42 {
43 EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *Facs;
44 EFI_STATUS Status;
45 UINTN TempStackTop;
46 UINTN TempStack[0x10];
47 UINTN AsmTransferControl16Address;
48 IA32_DESCRIPTOR IdtDescriptor;
49
50 //
51 // Disable interrupt of Debug timer, since new IDT table cannot handle it.
52 //
53 SaveAndSetDebugTimerInterrupt (FALSE);
54
55 AsmReadIdtr (&IdtDescriptor);
56 //
57 // Restore IDT for debug
58 //
59 SetIdtEntry (AcpiS3Context);
60
61 //
62 // Initialize Debug Agent to support source level debug in S3 path, it will disable interrupt and Debug Timer.
63 //
64 InitializeDebugAgent (DEBUG_AGENT_INIT_S3, (VOID *)&IdtDescriptor, NULL);
65
66 //
67 // Because not install BootScriptExecute PPI(used just in this module), So just pass NULL
68 // for that parameter.
69 //
70 Status = S3BootScriptExecute ();
71
72 AsmWbinvd ();
73
74 //
75 // Get ACPI Table Address
76 //
77 Facs = (EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *) ((UINTN) (AcpiS3Context->AcpiFacsTable));
78
79 //
80 // We need turn back to S3Resume - install boot script done ppi and report status code on S3resume.
81 //
82 if (PeiS3ResumeState != 0) {
83 //
84 // Need report status back to S3ResumePeim.
85 // If boot script execution is failed, S3ResumePeim wil report the error status code.
86 //
87 PeiS3ResumeState->ReturnStatus = (UINT64)(UINTN)Status;
88 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {
89 //
90 // X64 S3 Resume
91 //
92 DEBUG ((EFI_D_ERROR, "Call AsmDisablePaging64() to return to S3 Resume in PEI Phase\n"));
93 PeiS3ResumeState->AsmTransferControl = (EFI_PHYSICAL_ADDRESS)(UINTN)AsmTransferControl32;
94
95 if ((Facs != NULL) &&
96 (Facs->Signature == EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE) &&
97 (Facs->FirmwareWakingVector != 0) ) {
98 //
99 // more step needed - because relative address is handled differently between X64 and IA32.
100 //
101 AsmTransferControl16Address = (UINTN)AsmTransferControl16;
102 AsmFixAddress16 = (UINT32)AsmTransferControl16Address;
103 AsmJmpAddr32 = (UINT32)((Facs->FirmwareWakingVector & 0xF) | ((Facs->FirmwareWakingVector & 0xFFFF0) << 12));
104 }
105
106 AsmDisablePaging64 (
107 PeiS3ResumeState->ReturnCs,
108 (UINT32)PeiS3ResumeState->ReturnEntryPoint,
109 (UINT32)(UINTN)AcpiS3Context,
110 (UINT32)(UINTN)PeiS3ResumeState,
111 (UINT32)PeiS3ResumeState->ReturnStackPointer
112 );
113 } else {
114 //
115 // IA32 S3 Resume
116 //
117 DEBUG ((EFI_D_ERROR, "Call SwitchStack() to return to S3 Resume in PEI Phase\n"));
118 PeiS3ResumeState->AsmTransferControl = (EFI_PHYSICAL_ADDRESS)(UINTN)AsmTransferControl;
119
120 SwitchStack (
121 (SWITCH_STACK_ENTRY_POINT)(UINTN)PeiS3ResumeState->ReturnEntryPoint,
122 (VOID *)(UINTN)AcpiS3Context,
123 (VOID *)(UINTN)PeiS3ResumeState,
124 (VOID *)(UINTN)PeiS3ResumeState->ReturnStackPointer
125 );
126 }
127
128 //
129 // Never run to here
130 //
131 CpuDeadLoop();
132 return EFI_UNSUPPORTED;
133 }
134
135 //
136 // S3ResumePeim does not provide a way to jump back to itself, so resume to OS here directly
137 //
138 if (Facs->XFirmwareWakingVector != 0) {
139 //
140 // Switch to native waking vector
141 //
142 TempStackTop = (UINTN)&TempStack + sizeof(TempStack);
143 if ((Facs->Version == EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_VERSION) &&
144 ((Facs->Flags & EFI_ACPI_4_0_64BIT_WAKE_SUPPORTED_F) != 0) &&
145 ((Facs->Flags & EFI_ACPI_4_0_OSPM_64BIT_WAKE__F) != 0)) {
146 //
147 // X64 long mode waking vector
148 //
149 DEBUG (( EFI_D_ERROR, "Transfer to 64bit OS waking vector - %x\r\n", (UINTN)Facs->XFirmwareWakingVector));
150 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {
151 SwitchStack (
152 (SWITCH_STACK_ENTRY_POINT)(UINTN)Facs->XFirmwareWakingVector,
153 NULL,
154 NULL,
155 (VOID *)(UINTN)TempStackTop
156 );
157 } else {
158 // Unsupported for 32bit DXE, 64bit OS vector
159 DEBUG (( EFI_D_ERROR, "Unsupported for 32bit DXE transfer to 64bit OS waking vector!\r\n"));
160 ASSERT (FALSE);
161 }
162 } else {
163 //
164 // IA32 protected mode waking vector (Page disabled)
165 //
166 DEBUG (( EFI_D_ERROR, "Transfer to 32bit OS waking vector - %x\r\n", (UINTN)Facs->XFirmwareWakingVector));
167 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {
168 AsmDisablePaging64 (
169 0x10,
170 (UINT32)Facs->XFirmwareWakingVector,
171 0,
172 0,
173 (UINT32)TempStackTop
174 );
175 } else {
176 SwitchStack (
177 (SWITCH_STACK_ENTRY_POINT)(UINTN)Facs->XFirmwareWakingVector,
178 NULL,
179 NULL,
180 (VOID *)(UINTN)TempStackTop
181 );
182 }
183 }
184 } else {
185 //
186 // 16bit Realmode waking vector
187 //
188 DEBUG (( EFI_D_ERROR, "Transfer to 16bit OS waking vector - %x\r\n", (UINTN)Facs->FirmwareWakingVector));
189 AsmTransferControl (Facs->FirmwareWakingVector, 0x0);
190 }
191
192 //
193 // Never run to here
194 //
195 CpuDeadLoop();
196 return EFI_UNSUPPORTED;
197 }
198 /**
199 Entrypoint of Boot script exector driver, this function will be executed in
200 normal boot phase and invoked by DXE dispatch.
201
202 @param[in] ImageHandle The firmware allocated handle for the EFI image.
203 @param[in] SystemTable A pointer to the EFI System Table.
204
205 @retval EFI_SUCCESS The entry point is executed successfully.
206 @retval other Some error occurs when executing this entry point.
207 **/
208 EFI_STATUS
209 EFIAPI
210 BootScriptExecutorEntryPoint (
211 IN EFI_HANDLE ImageHandle,
212 IN EFI_SYSTEM_TABLE *SystemTable
213 )
214 {
215 UINT8 *Buffer;
216 UINTN BufferSize;
217 UINTN Pages;
218 EFI_PHYSICAL_ADDRESS FfsBuffer;
219 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
220 BOOT_SCRIPT_EXECUTOR_VARIABLE *EfiBootScriptExecutorVariable;
221 EFI_PHYSICAL_ADDRESS BootScriptExecutorBuffer;
222 EFI_STATUS Status;
223 VOID *DevicePath;
224 EFI_HANDLE NewImageHandle;
225
226 //
227 // Test if the gEfiCallerIdGuid of this image is already installed. if not, the entry
228 // point is loaded by DXE code which is the first time loaded. or else, it is already
229 // be reloaded be itself.This is a work-around
230 //
231 Status = gBS->LocateProtocol (&gEfiCallerIdGuid, NULL, &DevicePath);
232 if (EFI_ERROR (Status)) {
233
234 //
235 // This is the first-time loaded by DXE core. reload itself to NVS mem
236 //
237 //
238 // A workarouond: Here we install a dummy handle
239 //
240 NewImageHandle = NULL;
241 Status = gBS->InstallProtocolInterface (
242 &NewImageHandle,
243 &gEfiCallerIdGuid,
244 EFI_NATIVE_INTERFACE,
245 NULL
246 );
247
248 Status = GetSectionFromAnyFv (
249 &gEfiCallerIdGuid,
250 EFI_SECTION_PE32,
251 0,
252 (VOID **) &Buffer,
253 &BufferSize
254 );
255 ImageContext.Handle = Buffer;
256 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
257 //
258 // Get information about the image being loaded
259 //
260 Status = PeCoffLoaderGetImageInfo (&ImageContext);
261 if (EFI_ERROR (Status)) {
262 return Status;
263 }
264 Pages = EFI_SIZE_TO_PAGES(BufferSize + ImageContext.SectionAlignment);
265 FfsBuffer = 0xFFFFFFFF;
266 Status = gBS->AllocatePages (
267 AllocateMaxAddress,
268 EfiACPIMemoryNVS,
269 Pages,
270 &FfsBuffer
271 );
272 if (EFI_ERROR (Status)) {
273 return EFI_OUT_OF_RESOURCES;
274 }
275 ImageContext.ImageAddress = (PHYSICAL_ADDRESS)(UINTN)FfsBuffer;
276 //
277 // Align buffer on section boundry
278 //
279 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
280 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);
281 //
282 // Load the image to our new buffer
283 //
284 Status = PeCoffLoaderLoadImage (&ImageContext);
285 if (EFI_ERROR (Status)) {
286 gBS->FreePages (FfsBuffer, Pages);
287 return Status;
288 }
289
290 //
291 // Relocate the image in our new buffer
292 //
293 Status = PeCoffLoaderRelocateImage (&ImageContext);
294
295 if (EFI_ERROR (Status)) {
296 PeCoffLoaderUnloadImage (&ImageContext);
297 gBS->FreePages (FfsBuffer, Pages);
298 return Status;
299 }
300 //
301 // Flush the instruction cache so the image data is written before we execute it
302 //
303 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
304 Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)(ImageContext.EntryPoint)) (NewImageHandle, SystemTable);
305 if (EFI_ERROR (Status)) {
306 gBS->FreePages (FfsBuffer, Pages);
307 return Status;
308 }
309 //
310 // Additional step for BootScript integrity
311 // Save BootScriptExecutor image
312 //
313 Status = SaveLockBox (
314 &mBootScriptExecutorImageGuid,
315 (VOID *)(UINTN)ImageContext.ImageAddress,
316 (UINTN)ImageContext.ImageSize
317 );
318 ASSERT_EFI_ERROR (Status);
319
320 Status = SetLockBoxAttributes (&mBootScriptExecutorImageGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);
321 ASSERT_EFI_ERROR (Status);
322
323 } else {
324 //
325 // the entry point is invoked after reloading. following code only run in ACPI NVS
326 //
327 BufferSize = sizeof (BOOT_SCRIPT_EXECUTOR_VARIABLE);
328
329 BootScriptExecutorBuffer = 0xFFFFFFFF;
330 Pages = EFI_SIZE_TO_PAGES(BufferSize);
331 Status = gBS->AllocatePages (
332 AllocateMaxAddress,
333 EfiACPIMemoryNVS,
334 Pages,
335 &BootScriptExecutorBuffer
336 );
337 if (EFI_ERROR (Status)) {
338 return EFI_OUT_OF_RESOURCES;
339 }
340
341 EfiBootScriptExecutorVariable = (BOOT_SCRIPT_EXECUTOR_VARIABLE *)(UINTN)BootScriptExecutorBuffer;
342 EfiBootScriptExecutorVariable->BootScriptExecutorEntrypoint = (UINTN) S3BootScriptExecutorEntryFunction ;
343
344 Status = SaveLockBox (
345 &gEfiBootScriptExecutorVariableGuid,
346 &BootScriptExecutorBuffer,
347 sizeof(BootScriptExecutorBuffer)
348 );
349 ASSERT_EFI_ERROR (Status);
350
351 //
352 // Additional step for BootScript integrity
353 // Save BootScriptExecutor context
354 //
355 Status = SaveLockBox (
356 &gEfiBootScriptExecutorContextGuid,
357 EfiBootScriptExecutorVariable,
358 sizeof(*EfiBootScriptExecutorVariable)
359 );
360 ASSERT_EFI_ERROR (Status);
361
362 Status = SetLockBoxAttributes (&gEfiBootScriptExecutorContextGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);
363 ASSERT_EFI_ERROR (Status);
364
365 }
366
367 return EFI_SUCCESS;
368 }
369
370
371