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