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