]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/ScriptExecute.c
1. Add debug init flag DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64 in Debug Agent Library.
[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 if (EFI_ERROR (Status)) {
72 return Status;
73 }
74
75 AsmWbinvd ();
76
77 //
78 // Get ACPI Table Address
79 //
80 Facs = (EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *) ((UINTN) (AcpiS3Context->AcpiFacsTable));
81
82 if ((Facs == NULL) ||
83 (Facs->Signature != EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE) ||
84 ((Facs->FirmwareWakingVector == 0) && (Facs->XFirmwareWakingVector == 0)) ) {
85 CpuDeadLoop();
86 return EFI_INVALID_PARAMETER;
87 }
88
89 //
90 // We need turn back to S3Resume - install boot script done ppi and report status code on S3resume.
91 //
92 if (PeiS3ResumeState != 0) {
93 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {
94 //
95 // X64 S3 Resume
96 //
97 DEBUG ((EFI_D_ERROR, "Call AsmDisablePaging64() to return to S3 Resume in PEI Phase\n"));
98 PeiS3ResumeState->AsmTransferControl = (EFI_PHYSICAL_ADDRESS)(UINTN)AsmTransferControl32;
99
100 //
101 // more step needed - because relative address is handled differently between X64 and IA32.
102 //
103 AsmTransferControl16Address = (UINTN)AsmTransferControl16;
104 AsmFixAddress16 = (UINT32)AsmTransferControl16Address;
105 AsmJmpAddr32 = (UINT32)((Facs->FirmwareWakingVector & 0xF) | ((Facs->FirmwareWakingVector & 0xFFFF0) << 12));
106
107 AsmDisablePaging64 (
108 PeiS3ResumeState->ReturnCs,
109 (UINT32)PeiS3ResumeState->ReturnEntryPoint,
110 (UINT32)(UINTN)AcpiS3Context,
111 (UINT32)(UINTN)PeiS3ResumeState,
112 (UINT32)PeiS3ResumeState->ReturnStackPointer
113 );
114 } else {
115 //
116 // IA32 S3 Resume
117 //
118 DEBUG ((EFI_D_ERROR, "Call SwitchStack() to return to S3 Resume in PEI Phase\n"));
119 PeiS3ResumeState->AsmTransferControl = (EFI_PHYSICAL_ADDRESS)(UINTN)AsmTransferControl;
120
121 SwitchStack (
122 (SWITCH_STACK_ENTRY_POINT)(UINTN)PeiS3ResumeState->ReturnEntryPoint,
123 (VOID *)(UINTN)AcpiS3Context,
124 (VOID *)(UINTN)PeiS3ResumeState,
125 (VOID *)(UINTN)PeiS3ResumeState->ReturnStackPointer
126 );
127 }
128
129 //
130 // Never run to here
131 //
132 CpuDeadLoop();
133 return EFI_UNSUPPORTED;
134 }
135
136 if (Facs->XFirmwareWakingVector != 0) {
137 //
138 // Switch to native waking vector
139 //
140 TempStackTop = (UINTN)&TempStack + sizeof(TempStack);
141 if ((Facs->Version == EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_VERSION) &&
142 ((Facs->Flags & EFI_ACPI_4_0_64BIT_WAKE_SUPPORTED_F) != 0) &&
143 ((Facs->Flags & EFI_ACPI_4_0_OSPM_64BIT_WAKE__F) != 0)) {
144 //
145 // X64 long mode waking vector
146 //
147 DEBUG (( EFI_D_ERROR, "Transfer to 64bit OS waking vector - %x\r\n", (UINTN)Facs->XFirmwareWakingVector));
148 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {
149 SwitchStack (
150 (SWITCH_STACK_ENTRY_POINT)(UINTN)Facs->XFirmwareWakingVector,
151 NULL,
152 NULL,
153 (VOID *)(UINTN)TempStackTop
154 );
155 } else {
156 // Unsupported for 32bit DXE, 64bit OS vector
157 DEBUG (( EFI_D_ERROR, "Unsupported for 32bit DXE transfer to 64bit OS waking vector!\r\n"));
158 ASSERT (FALSE);
159 }
160 } else {
161 //
162 // IA32 protected mode waking vector (Page disabled)
163 //
164 DEBUG (( EFI_D_ERROR, "Transfer to 32bit OS waking vector - %x\r\n", (UINTN)Facs->XFirmwareWakingVector));
165 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {
166 AsmDisablePaging64 (
167 0x10,
168 (UINT32)Facs->XFirmwareWakingVector,
169 0,
170 0,
171 (UINT32)TempStackTop
172 );
173 } else {
174 SwitchStack (
175 (SWITCH_STACK_ENTRY_POINT)(UINTN)Facs->XFirmwareWakingVector,
176 NULL,
177 NULL,
178 (VOID *)(UINTN)TempStackTop
179 );
180 }
181 }
182 } else {
183 //
184 // 16bit Realmode waking vector
185 //
186 DEBUG (( EFI_D_ERROR, "Transfer to 16bit OS waking vector - %x\r\n", (UINTN)Facs->FirmwareWakingVector));
187 AsmTransferControl (Facs->FirmwareWakingVector, 0x0);
188 }
189
190 //
191 // Never run to here
192 //
193 CpuDeadLoop();
194 return EFI_UNSUPPORTED;
195 }
196 /**
197 Entrypoint of Boot script exector driver, this function will be executed in
198 normal boot phase and invoked by DXE dispatch.
199
200 @param[in] ImageHandle The firmware allocated handle for the EFI image.
201 @param[in] SystemTable A pointer to the EFI System Table.
202
203 @retval EFI_SUCCESS The entry point is executed successfully.
204 @retval other Some error occurs when executing this entry point.
205 **/
206 EFI_STATUS
207 EFIAPI
208 BootScriptExecutorEntryPoint (
209 IN EFI_HANDLE ImageHandle,
210 IN EFI_SYSTEM_TABLE *SystemTable
211 )
212 {
213 UINT8 *Buffer;
214 UINTN BufferSize;
215 UINTN Pages;
216 EFI_PHYSICAL_ADDRESS FfsBuffer;
217 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
218 BOOT_SCRIPT_EXECUTOR_VARIABLE *EfiBootScriptExecutorVariable;
219 EFI_PHYSICAL_ADDRESS BootScriptExecutorBuffer;
220 EFI_STATUS Status;
221 VOID *DevicePath;
222 EFI_HANDLE NewImageHandle;
223
224 //
225 // Test if the gEfiCallerIdGuid of this image is already installed. if not, the entry
226 // point is loaded by DXE code which is the first time loaded. or else, it is already
227 // be reloaded be itself.This is a work-around
228 //
229 Status = gBS->LocateProtocol (&gEfiCallerIdGuid, NULL, &DevicePath);
230 if (EFI_ERROR (Status)) {
231
232 //
233 // This is the first-time loaded by DXE core. reload itself to NVS mem
234 //
235 //
236 // A workarouond: Here we install a dummy handle
237 //
238 NewImageHandle = NULL;
239 Status = gBS->InstallProtocolInterface (
240 &NewImageHandle,
241 &gEfiCallerIdGuid,
242 EFI_NATIVE_INTERFACE,
243 NULL
244 );
245
246 Status = GetSectionFromAnyFv (
247 &gEfiCallerIdGuid,
248 EFI_SECTION_PE32,
249 0,
250 (VOID **) &Buffer,
251 &BufferSize
252 );
253 ImageContext.Handle = Buffer;
254 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
255 //
256 // Get information about the image being loaded
257 //
258 Status = PeCoffLoaderGetImageInfo (&ImageContext);
259 if (EFI_ERROR (Status)) {
260 return Status;
261 }
262 Pages = EFI_SIZE_TO_PAGES(BufferSize + ImageContext.SectionAlignment);
263 FfsBuffer = 0xFFFFFFFF;
264 Status = gBS->AllocatePages (
265 AllocateMaxAddress,
266 EfiACPIMemoryNVS,
267 Pages,
268 &FfsBuffer
269 );
270 if (EFI_ERROR (Status)) {
271 return EFI_OUT_OF_RESOURCES;
272 }
273 ImageContext.ImageAddress = (PHYSICAL_ADDRESS)(UINTN)FfsBuffer;
274 //
275 // Align buffer on section boundry
276 //
277 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
278 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);
279 //
280 // Load the image to our new buffer
281 //
282 Status = PeCoffLoaderLoadImage (&ImageContext);
283 if (EFI_ERROR (Status)) {
284 gBS->FreePages (FfsBuffer, Pages);
285 return Status;
286 }
287
288 //
289 // Relocate the image in our new buffer
290 //
291 Status = PeCoffLoaderRelocateImage (&ImageContext);
292
293 if (EFI_ERROR (Status)) {
294 PeCoffLoaderUnloadImage (&ImageContext);
295 gBS->FreePages (FfsBuffer, Pages);
296 return Status;
297 }
298 //
299 // Flush the instruction cache so the image data is written before we execute it
300 //
301 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
302 Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)(ImageContext.EntryPoint)) (NewImageHandle, SystemTable);
303 if (EFI_ERROR (Status)) {
304 gBS->FreePages (FfsBuffer, Pages);
305 return Status;
306 }
307 //
308 // Additional step for BootScript integrity
309 // Save BootScriptExecutor image
310 //
311 Status = SaveLockBox (
312 &mBootScriptExecutorImageGuid,
313 (VOID *)(UINTN)ImageContext.ImageAddress,
314 (UINTN)ImageContext.ImageSize
315 );
316 ASSERT_EFI_ERROR (Status);
317
318 Status = SetLockBoxAttributes (&mBootScriptExecutorImageGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);
319 ASSERT_EFI_ERROR (Status);
320
321 } else {
322 //
323 // the entry point is invoked after reloading. following code only run in ACPI NVS
324 //
325 BufferSize = sizeof (BOOT_SCRIPT_EXECUTOR_VARIABLE);
326
327 BootScriptExecutorBuffer = 0xFFFFFFFF;
328 Pages = EFI_SIZE_TO_PAGES(BufferSize);
329 Status = gBS->AllocatePages (
330 AllocateMaxAddress,
331 EfiACPIMemoryNVS,
332 Pages,
333 &BootScriptExecutorBuffer
334 );
335 if (EFI_ERROR (Status)) {
336 return EFI_OUT_OF_RESOURCES;
337 }
338
339 EfiBootScriptExecutorVariable = (BOOT_SCRIPT_EXECUTOR_VARIABLE *)(UINTN)BootScriptExecutorBuffer;
340 EfiBootScriptExecutorVariable->BootScriptExecutorEntrypoint = (UINTN) S3BootScriptExecutorEntryFunction ;
341
342 Status = SaveLockBox (
343 &gEfiBootScriptExecutorVariableGuid,
344 &BootScriptExecutorBuffer,
345 sizeof(BootScriptExecutorBuffer)
346 );
347 ASSERT_EFI_ERROR (Status);
348
349 //
350 // Additional step for BootScript integrity
351 // Save BootScriptExecutor context
352 //
353 Status = SaveLockBox (
354 &gEfiBootScriptExecutorContextGuid,
355 EfiBootScriptExecutorVariable,
356 sizeof(*EfiBootScriptExecutorVariable)
357 );
358 ASSERT_EFI_ERROR (Status);
359
360 Status = SetLockBoxAttributes (&gEfiBootScriptExecutorContextGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);
361 ASSERT_EFI_ERROR (Status);
362
363 }
364
365 return EFI_SUCCESS;
366 }
367
368
369