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