SecMain.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /** @file
  2. C functions in SEC
  3. Copyright (c) 2008 - 2019, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "SecMain.h"
  7. EFI_PEI_TEMPORARY_RAM_DONE_PPI gSecTemporaryRamDonePpi = {
  8. SecTemporaryRamDone
  9. };
  10. EFI_SEC_PLATFORM_INFORMATION_PPI mSecPlatformInformationPpi = { SecPlatformInformation };
  11. EFI_PEI_PPI_DESCRIPTOR mPeiSecPlatformInformationPpi[] = {
  12. {
  13. //
  14. // SecPerformance PPI notify descriptor.
  15. //
  16. EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,
  17. &gPeiSecPerformancePpiGuid,
  18. (VOID *)(UINTN)SecPerformancePpiCallBack
  19. },
  20. {
  21. EFI_PEI_PPI_DESCRIPTOR_PPI,
  22. &gEfiTemporaryRamDonePpiGuid,
  23. &gSecTemporaryRamDonePpi
  24. },
  25. {
  26. (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  27. &gEfiSecPlatformInformationPpiGuid,
  28. &mSecPlatformInformationPpi
  29. }
  30. };
  31. /**
  32. Migrates the Global Descriptor Table (GDT) to permanent memory.
  33. @retval EFI_SUCCESS The GDT was migrated successfully.
  34. @retval EFI_OUT_OF_RESOURCES The GDT could not be migrated due to lack of available memory.
  35. **/
  36. EFI_STATUS
  37. MigrateGdt (
  38. VOID
  39. )
  40. {
  41. EFI_STATUS Status;
  42. UINTN GdtBufferSize;
  43. IA32_DESCRIPTOR Gdtr;
  44. VOID *GdtBuffer;
  45. AsmReadGdtr ((IA32_DESCRIPTOR *)&Gdtr);
  46. GdtBufferSize = sizeof (IA32_SEGMENT_DESCRIPTOR) -1 + Gdtr.Limit + 1;
  47. Status = PeiServicesAllocatePool (
  48. GdtBufferSize,
  49. &GdtBuffer
  50. );
  51. ASSERT (GdtBuffer != NULL);
  52. if (EFI_ERROR (Status)) {
  53. return EFI_OUT_OF_RESOURCES;
  54. }
  55. GdtBuffer = ALIGN_POINTER (GdtBuffer, sizeof (IA32_SEGMENT_DESCRIPTOR));
  56. CopyMem (GdtBuffer, (VOID *)Gdtr.Base, Gdtr.Limit + 1);
  57. Gdtr.Base = (UINTN)GdtBuffer;
  58. AsmWriteGdtr (&Gdtr);
  59. return EFI_SUCCESS;
  60. }
  61. //
  62. // These are IDT entries pointing to 10:FFFFFFE4h.
  63. //
  64. UINT64 mIdtEntryTemplate = 0xffff8e000010ffe4ULL;
  65. /**
  66. Caller provided function to be invoked at the end of InitializeDebugAgent().
  67. Entry point to the C language phase of SEC. After the SEC assembly
  68. code has initialized some temporary memory and set up the stack,
  69. the control is transferred to this function.
  70. @param[in] Context The first input parameter of InitializeDebugAgent().
  71. **/
  72. VOID
  73. NORETURN
  74. EFIAPI
  75. SecStartupPhase2 (
  76. IN VOID *Context
  77. );
  78. /**
  79. Entry point of the notification callback function itself within the PEIM.
  80. It is to get SEC performance data and build HOB to convey the SEC performance
  81. data to DXE phase.
  82. @param PeiServices Indirect reference to the PEI Services Table.
  83. @param NotifyDescriptor Address of the notification descriptor data structure.
  84. @param Ppi Address of the PPI that was installed.
  85. @return Status of the notification.
  86. The status code returned from this function is ignored.
  87. **/
  88. EFI_STATUS
  89. EFIAPI
  90. SecPerformancePpiCallBack (
  91. IN EFI_PEI_SERVICES **PeiServices,
  92. IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
  93. IN VOID *Ppi
  94. )
  95. {
  96. EFI_STATUS Status;
  97. PEI_SEC_PERFORMANCE_PPI *SecPerf;
  98. FIRMWARE_SEC_PERFORMANCE Performance;
  99. SecPerf = (PEI_SEC_PERFORMANCE_PPI *)Ppi;
  100. Status = SecPerf->GetPerformance ((CONST EFI_PEI_SERVICES **)PeiServices, SecPerf, &Performance);
  101. if (!EFI_ERROR (Status)) {
  102. BuildGuidDataHob (
  103. &gEfiFirmwarePerformanceGuid,
  104. &Performance,
  105. sizeof (FIRMWARE_SEC_PERFORMANCE)
  106. );
  107. DEBUG ((DEBUG_INFO, "FPDT: SEC Performance Hob ResetEnd = %ld\n", Performance.ResetEnd));
  108. }
  109. return Status;
  110. }
  111. /**
  112. Entry point to the C language phase of SEC. After the SEC assembly
  113. code has initialized some temporary memory and set up the stack,
  114. the control is transferred to this function.
  115. @param SizeOfRam Size of the temporary memory available for use.
  116. @param TempRamBase Base address of temporary ram
  117. @param BootFirmwareVolume Base address of the Boot Firmware Volume.
  118. **/
  119. VOID
  120. NORETURN
  121. EFIAPI
  122. SecStartup (
  123. IN UINT32 SizeOfRam,
  124. IN UINT32 TempRamBase,
  125. IN VOID *BootFirmwareVolume
  126. )
  127. {
  128. EFI_SEC_PEI_HAND_OFF SecCoreData;
  129. IA32_DESCRIPTOR IdtDescriptor;
  130. SEC_IDT_TABLE IdtTableInStack;
  131. UINT32 Index;
  132. UINT32 PeiStackSize;
  133. EFI_STATUS Status;
  134. //
  135. // Report Status Code to indicate entering SEC core
  136. //
  137. REPORT_STATUS_CODE (
  138. EFI_PROGRESS_CODE,
  139. EFI_SOFTWARE_SEC | EFI_SW_SEC_PC_ENTRY_POINT
  140. );
  141. DEBUG ((
  142. DEBUG_INFO,
  143. "%a() TempRAM Base: 0x%x, TempRAM Size: 0x%x, BootFirmwareVolume 0x%x\n",
  144. __FUNCTION__,
  145. TempRamBase,
  146. SizeOfRam,
  147. BootFirmwareVolume
  148. ));
  149. PeiStackSize = PcdGet32 (PcdPeiTemporaryRamStackSize);
  150. if (PeiStackSize == 0) {
  151. PeiStackSize = (SizeOfRam >> 1);
  152. }
  153. ASSERT (PeiStackSize < SizeOfRam);
  154. //
  155. // Process all libraries constructor function linked to SecCore.
  156. //
  157. ProcessLibraryConstructorList ();
  158. //
  159. // Initialize floating point operating environment
  160. // to be compliant with UEFI spec.
  161. //
  162. InitializeFloatingPointUnits ();
  163. // |-------------------|---->
  164. // |IDT Table |
  165. // |-------------------|
  166. // |PeiService Pointer | PeiStackSize
  167. // |-------------------|
  168. // | |
  169. // | Stack |
  170. // |-------------------|---->
  171. // | |
  172. // | |
  173. // | Heap | PeiTemporayRamSize
  174. // | |
  175. // | |
  176. // |-------------------|----> TempRamBase
  177. IdtTableInStack.PeiService = 0;
  178. for (Index = 0; Index < SEC_IDT_ENTRY_COUNT; Index++) {
  179. ZeroMem ((VOID *)&IdtTableInStack.IdtTable[Index], sizeof (IA32_IDT_GATE_DESCRIPTOR));
  180. CopyMem ((VOID *)&IdtTableInStack.IdtTable[Index], (VOID *)&mIdtEntryTemplate, sizeof (UINT64));
  181. }
  182. IdtDescriptor.Base = (UINTN)&IdtTableInStack.IdtTable;
  183. IdtDescriptor.Limit = (UINT16)(sizeof (IdtTableInStack.IdtTable) - 1);
  184. AsmWriteIdtr (&IdtDescriptor);
  185. //
  186. // Setup the default exception handlers
  187. //
  188. Status = InitializeCpuExceptionHandlers (NULL);
  189. ASSERT_EFI_ERROR (Status);
  190. //
  191. // Update the base address and length of Pei temporary memory
  192. //
  193. SecCoreData.DataSize = (UINT16)sizeof (EFI_SEC_PEI_HAND_OFF);
  194. SecCoreData.BootFirmwareVolumeBase = BootFirmwareVolume;
  195. SecCoreData.BootFirmwareVolumeSize = (UINTN)((EFI_FIRMWARE_VOLUME_HEADER *)BootFirmwareVolume)->FvLength;
  196. SecCoreData.TemporaryRamBase = (VOID *)(UINTN)TempRamBase;
  197. SecCoreData.TemporaryRamSize = SizeOfRam;
  198. SecCoreData.PeiTemporaryRamBase = SecCoreData.TemporaryRamBase;
  199. SecCoreData.PeiTemporaryRamSize = SizeOfRam - PeiStackSize;
  200. SecCoreData.StackBase = (VOID *)(UINTN)(TempRamBase + SecCoreData.PeiTemporaryRamSize);
  201. SecCoreData.StackSize = PeiStackSize;
  202. DEBUG ((
  203. DEBUG_INFO,
  204. "%a() BFV Base: 0x%x, BFV Size: 0x%x, TempRAM Base: 0x%x, TempRAM Size: 0x%x, PeiTempRamBase: 0x%x, PeiTempRamSize: 0x%x, StackBase: 0x%x, StackSize: 0x%x\n",
  205. __FUNCTION__,
  206. SecCoreData.BootFirmwareVolumeBase,
  207. SecCoreData.BootFirmwareVolumeSize,
  208. SecCoreData.TemporaryRamBase,
  209. SecCoreData.TemporaryRamSize,
  210. SecCoreData.PeiTemporaryRamBase,
  211. SecCoreData.PeiTemporaryRamSize,
  212. SecCoreData.StackBase,
  213. SecCoreData.StackSize
  214. ));
  215. //
  216. // Initialize Debug Agent to support source level debug in SEC/PEI phases before memory ready.
  217. //
  218. InitializeDebugAgent (DEBUG_AGENT_INIT_PREMEM_SEC, &SecCoreData, SecStartupPhase2);
  219. //
  220. // Should not come here.
  221. //
  222. UNREACHABLE ();
  223. }
  224. /**
  225. Caller provided function to be invoked at the end of InitializeDebugAgent().
  226. Entry point to the C language phase of SEC. After the SEC assembly
  227. code has initialized some temporary memory and set up the stack,
  228. the control is transferred to this function.
  229. @param[in] Context The first input parameter of InitializeDebugAgent().
  230. **/
  231. VOID
  232. NORETURN
  233. EFIAPI
  234. SecStartupPhase2 (
  235. IN VOID *Context
  236. )
  237. {
  238. EFI_SEC_PEI_HAND_OFF *SecCoreData;
  239. EFI_PEI_PPI_DESCRIPTOR *PpiList;
  240. UINT32 Index;
  241. EFI_PEI_PPI_DESCRIPTOR *AllSecPpiList;
  242. EFI_PEI_CORE_ENTRY_POINT PeiCoreEntryPoint;
  243. PeiCoreEntryPoint = NULL;
  244. SecCoreData = (EFI_SEC_PEI_HAND_OFF *)Context;
  245. //
  246. // Perform platform specific initialization before entering PeiCore.
  247. //
  248. PpiList = SecPlatformMain (SecCoreData);
  249. //
  250. // Find Pei Core entry point. It will report SEC and Pei Core debug information if remote debug
  251. // is enabled.
  252. //
  253. if (PpiList != NULL) {
  254. Index = 0;
  255. do {
  256. if (CompareGuid (PpiList[Index].Guid, &gEfiPeiCoreFvLocationPpiGuid) &&
  257. (((EFI_PEI_CORE_FV_LOCATION_PPI *)PpiList[Index].Ppi)->PeiCoreFvLocation != 0)
  258. )
  259. {
  260. //
  261. // In this case, SecCore is in BFV but PeiCore is in another FV reported by PPI.
  262. //
  263. FindAndReportEntryPoints (
  264. (EFI_FIRMWARE_VOLUME_HEADER *)SecCoreData->BootFirmwareVolumeBase,
  265. (EFI_FIRMWARE_VOLUME_HEADER *)((EFI_PEI_CORE_FV_LOCATION_PPI *)PpiList[Index].Ppi)->PeiCoreFvLocation,
  266. &PeiCoreEntryPoint
  267. );
  268. if (PeiCoreEntryPoint != NULL) {
  269. break;
  270. } else {
  271. //
  272. // Invalid PeiCore FV provided by platform
  273. //
  274. CpuDeadLoop ();
  275. }
  276. }
  277. } while ((PpiList[Index++].Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) != EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);
  278. }
  279. //
  280. // If EFI_PEI_CORE_FV_LOCATION_PPI not found, try to locate PeiCore from BFV.
  281. //
  282. if (PeiCoreEntryPoint == NULL) {
  283. //
  284. // Both SecCore and PeiCore are in BFV.
  285. //
  286. FindAndReportEntryPoints (
  287. (EFI_FIRMWARE_VOLUME_HEADER *)SecCoreData->BootFirmwareVolumeBase,
  288. (EFI_FIRMWARE_VOLUME_HEADER *)SecCoreData->BootFirmwareVolumeBase,
  289. &PeiCoreEntryPoint
  290. );
  291. if (PeiCoreEntryPoint == NULL) {
  292. CpuDeadLoop ();
  293. }
  294. }
  295. DEBUG ((
  296. DEBUG_INFO,
  297. "%a() PeiCoreEntryPoint: 0x%x\n",
  298. __FUNCTION__,
  299. PeiCoreEntryPoint
  300. ));
  301. if (PpiList != NULL) {
  302. AllSecPpiList = (EFI_PEI_PPI_DESCRIPTOR *)SecCoreData->PeiTemporaryRamBase;
  303. //
  304. // Remove the terminal flag from the terminal PPI
  305. //
  306. CopyMem (AllSecPpiList, mPeiSecPlatformInformationPpi, sizeof (mPeiSecPlatformInformationPpi));
  307. Index = sizeof (mPeiSecPlatformInformationPpi) / sizeof (EFI_PEI_PPI_DESCRIPTOR) - 1;
  308. AllSecPpiList[Index].Flags = AllSecPpiList[Index].Flags & (~EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);
  309. //
  310. // Append the platform additional PPI list
  311. //
  312. Index += 1;
  313. while (((PpiList->Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) != EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST)) {
  314. CopyMem (&AllSecPpiList[Index], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));
  315. Index++;
  316. PpiList++;
  317. }
  318. //
  319. // Add the terminal PPI
  320. //
  321. CopyMem (&AllSecPpiList[Index++], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));
  322. //
  323. // Set PpiList to the total PPI
  324. //
  325. PpiList = AllSecPpiList;
  326. //
  327. // Adjust PEI TEMP RAM Range.
  328. //
  329. ASSERT (SecCoreData->PeiTemporaryRamSize > Index * sizeof (EFI_PEI_PPI_DESCRIPTOR));
  330. SecCoreData->PeiTemporaryRamBase = (VOID *)((UINTN)SecCoreData->PeiTemporaryRamBase + Index * sizeof (EFI_PEI_PPI_DESCRIPTOR));
  331. SecCoreData->PeiTemporaryRamSize = SecCoreData->PeiTemporaryRamSize - Index * sizeof (EFI_PEI_PPI_DESCRIPTOR);
  332. //
  333. // Adjust the Base and Size to be 8-byte aligned as HOB which has 8byte aligned requirement
  334. // will be built based on them in PEI phase.
  335. //
  336. SecCoreData->PeiTemporaryRamBase = (VOID *)(((UINTN)SecCoreData->PeiTemporaryRamBase + 7) & ~0x07);
  337. SecCoreData->PeiTemporaryRamSize &= ~(UINTN)0x07;
  338. DEBUG ((
  339. DEBUG_INFO,
  340. "%a() PeiTemporaryRamBase: 0x%x, PeiTemporaryRamSize: 0x%x\n",
  341. __FUNCTION__,
  342. SecCoreData->PeiTemporaryRamBase,
  343. SecCoreData->PeiTemporaryRamSize
  344. ));
  345. } else {
  346. //
  347. // No addition PPI, PpiList directly point to the common PPI list.
  348. //
  349. PpiList = &mPeiSecPlatformInformationPpi[0];
  350. }
  351. DEBUG ((
  352. DEBUG_INFO,
  353. "%a() Stack Base: 0x%p, Stack Size: 0x%x\n",
  354. __FUNCTION__,
  355. SecCoreData->StackBase,
  356. (UINT32)SecCoreData->StackSize
  357. ));
  358. //
  359. // Report Status Code to indicate transferring to PEI core
  360. //
  361. REPORT_STATUS_CODE (
  362. EFI_PROGRESS_CODE,
  363. EFI_SOFTWARE_SEC | EFI_SW_SEC_PC_HANDOFF_TO_NEXT
  364. );
  365. //
  366. // Transfer the control to the PEI core
  367. //
  368. ASSERT (PeiCoreEntryPoint != NULL);
  369. (*PeiCoreEntryPoint)(SecCoreData, PpiList);
  370. //
  371. // Should not come here.
  372. //
  373. UNREACHABLE ();
  374. }
  375. /**
  376. TemporaryRamDone() disables the use of Temporary RAM. If present, this service is invoked
  377. by the PEI Foundation after the EFI_PEI_PERMANANT_MEMORY_INSTALLED_PPI is installed.
  378. @retval EFI_SUCCESS Use of Temporary RAM was disabled.
  379. @retval EFI_INVALID_PARAMETER Temporary RAM could not be disabled.
  380. **/
  381. EFI_STATUS
  382. EFIAPI
  383. SecTemporaryRamDone (
  384. VOID
  385. )
  386. {
  387. EFI_STATUS Status;
  388. EFI_STATUS Status2;
  389. UINTN Index;
  390. BOOLEAN State;
  391. EFI_PEI_PPI_DESCRIPTOR *PeiPpiDescriptor;
  392. REPUBLISH_SEC_PPI_PPI *RepublishSecPpiPpi;
  393. //
  394. // Republish Sec Platform Information(2) PPI
  395. //
  396. RepublishSecPlatformInformationPpi ();
  397. //
  398. // Re-install SEC PPIs using a PEIM produced service if published
  399. //
  400. for (Index = 0, Status = EFI_SUCCESS; Status == EFI_SUCCESS; Index++) {
  401. Status = PeiServicesLocatePpi (
  402. &gRepublishSecPpiPpiGuid,
  403. Index,
  404. &PeiPpiDescriptor,
  405. (VOID **)&RepublishSecPpiPpi
  406. );
  407. if (!EFI_ERROR (Status)) {
  408. DEBUG ((DEBUG_INFO, "Calling RepublishSecPpi instance %d.\n", Index));
  409. Status2 = RepublishSecPpiPpi->RepublishSecPpis ();
  410. ASSERT_EFI_ERROR (Status2);
  411. }
  412. }
  413. //
  414. // Migrate DebugAgentContext.
  415. //
  416. InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC, NULL, NULL);
  417. //
  418. // Disable interrupts and save current interrupt state
  419. //
  420. State = SaveAndDisableInterrupts ();
  421. //
  422. // Migrate GDT before NEM near down
  423. //
  424. if (PcdGetBool (PcdMigrateTemporaryRamFirmwareVolumes)) {
  425. Status = MigrateGdt ();
  426. ASSERT_EFI_ERROR (Status);
  427. }
  428. //
  429. // Disable Temporary RAM after Stack and Heap have been migrated at this point.
  430. //
  431. SecPlatformDisableTemporaryMemory ();
  432. //
  433. // Restore original interrupt state
  434. //
  435. SetInterruptState (State);
  436. return EFI_SUCCESS;
  437. }