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