ScriptExecute.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /** @file
  2. This is the code for Boot Script Executer module.
  3. This driver is dispatched by Dxe core and the driver will reload itself to ACPI NVS memory
  4. in the entry point. The functionality is to interpret and restore the S3 boot script
  5. Copyright (c) 2013-2016 Intel Corporation.
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include "ScriptExecute.h"
  9. #pragma pack(1)
  10. typedef union {
  11. struct {
  12. UINT32 LimitLow : 16;
  13. UINT32 BaseLow : 16;
  14. UINT32 BaseMid : 8;
  15. UINT32 Type : 4;
  16. UINT32 System : 1;
  17. UINT32 Dpl : 2;
  18. UINT32 Present : 1;
  19. UINT32 LimitHigh : 4;
  20. UINT32 Software : 1;
  21. UINT32 Reserved : 1;
  22. UINT32 DefaultSize : 1;
  23. UINT32 Granularity : 1;
  24. UINT32 BaseHigh : 8;
  25. } Bits;
  26. UINT64 Uint64;
  27. } IA32_GDT;
  28. #pragma pack()
  29. EFI_GUID mBootScriptExecutorImageGuid = {
  30. 0x9a8d3433, 0x9fe8, 0x42b6, {0x87, 0xb, 0x1e, 0x31, 0xc8, 0x4e, 0xbe, 0x3b}
  31. };
  32. //
  33. // Global Descriptor Table (GDT)
  34. //
  35. GLOBAL_REMOVE_IF_UNREFERENCED IA32_GDT mGdtEntries[] = {
  36. /* selector { Global Segment Descriptor } */
  37. /* 0x00 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  38. /* 0x08 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  39. /* 0x10 */ {{0xFFFF, 0, 0, 0xB, 1, 0, 1, 0xF, 0, 0, 1, 1, 0}},
  40. /* 0x18 */ {{0xFFFF, 0, 0, 0x3, 1, 0, 1, 0xF, 0, 0, 1, 1, 0}},
  41. /* 0x20 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  42. /* 0x28 */ {{0xFFFF, 0, 0, 0xB, 1, 0, 1, 0xF, 0, 0, 0, 1, 0}},
  43. /* 0x30 */ {{0xFFFF, 0, 0, 0x3, 1, 0, 1, 0xF, 0, 0, 0, 1, 0}},
  44. /* 0x38 */ {{0xFFFF, 0, 0, 0xB, 1, 0, 1, 0xF, 0, 1, 0, 1, 0}},
  45. /* 0x40 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  46. };
  47. //
  48. // IA32 Gdt register
  49. //
  50. GLOBAL_REMOVE_IF_UNREFERENCED CONST IA32_DESCRIPTOR mGdt = {
  51. sizeof (mGdtEntries) - 1,
  52. (UINTN) mGdtEntries
  53. };
  54. /**
  55. Entry function of Boot script exector. This function will be executed in
  56. S3 boot path.
  57. This function should not return, because it is invoked by switch stack.
  58. @param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT
  59. @param PeiS3ResumeState a pointer to a structure of PEI_S3_RESUME_STATE
  60. @retval EFI_INVALID_PARAMETER - OS waking vector not found
  61. @retval EFI_UNSUPPORTED - something wrong when we resume to OS
  62. **/
  63. EFI_STATUS
  64. EFIAPI
  65. S3BootScriptExecutorEntryFunction (
  66. IN ACPI_S3_CONTEXT *AcpiS3Context,
  67. IN PEI_S3_RESUME_STATE *PeiS3ResumeState
  68. )
  69. {
  70. EFI_STATUS Status;
  71. //
  72. // Disable interrupt of Debug timer, since new IDT table cannot handle it.
  73. //
  74. SaveAndSetDebugTimerInterrupt (FALSE);
  75. //
  76. // Restore IDT for debug
  77. //
  78. SetIdtEntry (AcpiS3Context);
  79. //
  80. // Initialize Debug Agent to support source level debug in S3 path.
  81. //
  82. InitializeDebugAgent (DEBUG_AGENT_INIT_S3, NULL, NULL);
  83. //
  84. // Because not install BootScriptExecute PPI(used just in this module), So just pass NULL
  85. // for that parameter.
  86. //
  87. Status = S3BootScriptExecute ();
  88. AsmWbinvd ();
  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. //
  99. // IA32 S3 Resume
  100. //
  101. DEBUG ((EFI_D_INFO, "Call SwitchStack() to return to S3 Resume in PEI Phase\n"));
  102. PeiS3ResumeState->AsmTransferControl = (EFI_PHYSICAL_ADDRESS)(UINTN)PlatformTransferControl16;
  103. SwitchStack (
  104. (SWITCH_STACK_ENTRY_POINT)(UINTN)PeiS3ResumeState->ReturnEntryPoint,
  105. (VOID *)(UINTN)AcpiS3Context,
  106. (VOID *)(UINTN)PeiS3ResumeState,
  107. (VOID *)(UINTN)PeiS3ResumeState->ReturnStackPointer
  108. );
  109. //
  110. // Never run to here
  111. //
  112. CpuDeadLoop();
  113. return EFI_UNSUPPORTED;
  114. }
  115. //
  116. // Never run to here
  117. //
  118. CpuDeadLoop();
  119. return EFI_UNSUPPORTED;
  120. }
  121. /**
  122. Entrypoint of Boot script exector driver, this function will be executed in
  123. normal boot phase and invoked by DXE dispatch.
  124. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  125. @param[in] SystemTable A pointer to the EFI System Table.
  126. @retval EFI_SUCCESS The entry point is executed successfully.
  127. @retval other Some error occurs when executing this entry point.
  128. **/
  129. EFI_STATUS
  130. EFIAPI
  131. BootScriptExecutorEntryPoint (
  132. IN EFI_HANDLE ImageHandle,
  133. IN EFI_SYSTEM_TABLE *SystemTable
  134. )
  135. {
  136. UINT8 *Buffer;
  137. UINTN BufferSize;
  138. UINTN Pages;
  139. EFI_PHYSICAL_ADDRESS FfsBuffer;
  140. PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
  141. BOOT_SCRIPT_EXECUTOR_VARIABLE *EfiBootScriptExecutorVariable;
  142. EFI_PHYSICAL_ADDRESS BootScriptExecutorBuffer;
  143. EFI_STATUS Status;
  144. VOID *DevicePath;
  145. EFI_HANDLE NewImageHandle;
  146. //
  147. // Test if the gEfiCallerIdGuid of this image is already installed. if not, the entry
  148. // point is loaded by DXE code which is the first time loaded. or else, it is already
  149. // be reloaded be itself.This is a work-around
  150. //
  151. Status = gBS->LocateProtocol (&gEfiCallerIdGuid, NULL, &DevicePath);
  152. if (EFI_ERROR (Status)) {
  153. //
  154. // This is the first-time loaded by DXE core. reload itself to NVS mem
  155. //
  156. //
  157. // A workarouond: Here we install a dummy handle
  158. //
  159. NewImageHandle = NULL;
  160. Status = gBS->InstallProtocolInterface (
  161. &NewImageHandle,
  162. &gEfiCallerIdGuid,
  163. EFI_NATIVE_INTERFACE,
  164. NULL
  165. );
  166. Status = GetSectionFromAnyFv (
  167. &gEfiCallerIdGuid,
  168. EFI_SECTION_PE32,
  169. 0,
  170. (VOID **) &Buffer,
  171. &BufferSize
  172. );
  173. ImageContext.Handle = Buffer;
  174. ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
  175. //
  176. // Get information about the image being loaded
  177. //
  178. Status = PeCoffLoaderGetImageInfo (&ImageContext);
  179. if (EFI_ERROR (Status)) {
  180. return Status;
  181. }
  182. Pages = EFI_SIZE_TO_PAGES(BufferSize + ImageContext.SectionAlignment);
  183. FfsBuffer = 0xFFFFFFFF;
  184. Status = gBS->AllocatePages (
  185. AllocateMaxAddress,
  186. EfiACPIMemoryNVS,
  187. Pages,
  188. &FfsBuffer
  189. );
  190. if (EFI_ERROR (Status)) {
  191. return EFI_OUT_OF_RESOURCES;
  192. }
  193. ImageContext.ImageAddress = (PHYSICAL_ADDRESS)(UINTN)FfsBuffer;
  194. //
  195. // Align buffer on section boundary
  196. //
  197. ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
  198. ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);
  199. //
  200. // Load the image to our new buffer
  201. //
  202. Status = PeCoffLoaderLoadImage (&ImageContext);
  203. if (EFI_ERROR (Status)) {
  204. gBS->FreePages (FfsBuffer, Pages);
  205. return Status;
  206. }
  207. //
  208. // Relocate the image in our new buffer
  209. //
  210. Status = PeCoffLoaderRelocateImage (&ImageContext);
  211. if (EFI_ERROR (Status)) {
  212. PeCoffLoaderUnloadImage (&ImageContext);
  213. gBS->FreePages (FfsBuffer, Pages);
  214. return Status;
  215. }
  216. //
  217. // Flush the instruction cache so the image data is written before we execute it
  218. //
  219. InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
  220. Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)(ImageContext.EntryPoint)) (NewImageHandle, SystemTable);
  221. if (EFI_ERROR (Status)) {
  222. gBS->FreePages (FfsBuffer, Pages);
  223. return Status;
  224. }
  225. //
  226. // Additional step for BootScript integrity
  227. // Save BootScriptExecutor image
  228. //
  229. Status = SaveLockBox (
  230. &mBootScriptExecutorImageGuid,
  231. (VOID *)(UINTN)ImageContext.ImageAddress,
  232. (UINTN)ImageContext.ImageSize
  233. );
  234. ASSERT_EFI_ERROR (Status);
  235. Status = SetLockBoxAttributes (&mBootScriptExecutorImageGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);
  236. ASSERT_EFI_ERROR (Status);
  237. } else {
  238. //
  239. // the entry point is invoked after reloading. following code only run in ACPI NVS
  240. //
  241. BufferSize = sizeof (BOOT_SCRIPT_EXECUTOR_VARIABLE);
  242. BootScriptExecutorBuffer = 0xFFFFFFFF;
  243. Pages = EFI_SIZE_TO_PAGES(BufferSize);
  244. Status = gBS->AllocatePages (
  245. AllocateMaxAddress,
  246. EfiACPIMemoryNVS,
  247. Pages,
  248. &BootScriptExecutorBuffer
  249. );
  250. if (EFI_ERROR (Status)) {
  251. return EFI_OUT_OF_RESOURCES;
  252. }
  253. EfiBootScriptExecutorVariable = (BOOT_SCRIPT_EXECUTOR_VARIABLE *)(UINTN)BootScriptExecutorBuffer;
  254. EfiBootScriptExecutorVariable->BootScriptExecutorEntrypoint = (UINTN) S3BootScriptExecutorEntryFunction ;
  255. Status = SaveLockBox (
  256. &gEfiBootScriptExecutorVariableGuid,
  257. &BootScriptExecutorBuffer,
  258. sizeof(BootScriptExecutorBuffer)
  259. );
  260. ASSERT_EFI_ERROR (Status);
  261. //
  262. // Additional step for BootScript integrity
  263. // Save BootScriptExecutor context
  264. //
  265. Status = SaveLockBox (
  266. &gEfiBootScriptExecutorContextGuid,
  267. EfiBootScriptExecutorVariable,
  268. sizeof(*EfiBootScriptExecutorVariable)
  269. );
  270. ASSERT_EFI_ERROR (Status);
  271. Status = SetLockBoxAttributes (&gEfiBootScriptExecutorContextGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);
  272. ASSERT_EFI_ERROR (Status);
  273. }
  274. return EFI_SUCCESS;
  275. }
  276. /**
  277. Platform specific mechanism to transfer control to 16bit OS waking vector
  278. @param[in] AcpiWakingVector The 16bit OS waking vector
  279. @param[in] AcpiLowMemoryBase A buffer under 1M which could be used during the transfer
  280. **/
  281. VOID
  282. PlatformTransferControl16 (
  283. IN UINT32 AcpiWakingVector,
  284. IN UINT32 AcpiLowMemoryBase
  285. )
  286. {
  287. UINT32 NewValue;
  288. UINT64 BaseAddress;
  289. UINT64 SmramLength;
  290. UINTN Index;
  291. DEBUG (( EFI_D_INFO, "PlatformTransferControl - Entry\r\n"));
  292. //
  293. // Need to make sure the GDT is loaded with values that support long mode and real mode.
  294. //
  295. AsmWriteGdtr (&mGdt);
  296. //
  297. // Disable eSram block (this will also clear/zero eSRAM)
  298. // We only use eSRAM in the PEI phase. Disable now that we are resuming the OS
  299. //
  300. NewValue = QNCPortRead (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, QUARK_NC_MEMORY_MANAGER_ESRAMPGCTRL_BLOCK);
  301. NewValue |= BLOCK_DISABLE_PG;
  302. QNCPortWrite (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, QUARK_NC_MEMORY_MANAGER_ESRAMPGCTRL_BLOCK, NewValue);
  303. //
  304. // Update HMBOUND to top of DDR3 memory and LOCK
  305. // We disabled eSRAM so now we move HMBOUND down to top of DDR3
  306. //
  307. QNCGetTSEGMemoryRange (&BaseAddress, &SmramLength);
  308. NewValue = (UINT32)(BaseAddress + SmramLength);
  309. DEBUG ((EFI_D_INFO,"Locking HMBOUND at: = 0x%8x\n",NewValue));
  310. QNCPortWrite (QUARK_NC_HOST_BRIDGE_SB_PORT_ID, QUARK_NC_HOST_BRIDGE_HMBOUND_REG, (NewValue | HMBOUND_LOCK));
  311. //
  312. // Lock all IMR regions now that HMBOUND is locked
  313. //
  314. for (Index = (QUARK_NC_MEMORY_MANAGER_IMR0+QUARK_NC_MEMORY_MANAGER_IMRXL); Index <= (QUARK_NC_MEMORY_MANAGER_IMR7+QUARK_NC_MEMORY_MANAGER_IMRXL); Index += 4) {
  315. NewValue = QNCPortRead (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, Index);
  316. NewValue |= IMR_LOCK;
  317. QNCPortWrite (QUARK_NC_MEMORY_MANAGER_SB_PORT_ID, Index, NewValue);
  318. }
  319. //
  320. // Call ASM routine to switch to real mode and jump to 16bit OS waking vector
  321. //
  322. AsmTransferControl(AcpiWakingVector, 0);
  323. //
  324. // Never run to here
  325. //
  326. CpuDeadLoop();
  327. }