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