SecMigrationPei.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /** @file
  2. Migrates SEC structures after permanent memory is installed.
  3. Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Base.h>
  7. #include <Library/BaseLib.h>
  8. #include <Library/BaseMemoryLib.h>
  9. #include <Library/DebugLib.h>
  10. #include <Library/HobLib.h>
  11. #include <Library/MemoryAllocationLib.h>
  12. #include <Library/PeiServicesLib.h>
  13. #include <Library/PeiServicesTablePointerLib.h>
  14. #include "SecMigrationPei.h"
  15. STATIC REPUBLISH_SEC_PPI_PPI mEdkiiRepublishSecPpiPpi = {
  16. RepublishSecPpis
  17. };
  18. GLOBAL_REMOVE_IF_UNREFERENCED EFI_SEC_PLATFORM_INFORMATION_PPI mSecPlatformInformationPostMemoryPpi = {
  19. SecPlatformInformationPostMemory
  20. };
  21. GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_TEMPORARY_RAM_DONE_PPI mSecTemporaryRamDonePostMemoryPpi = {
  22. SecTemporaryRamDonePostMemory
  23. };
  24. GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPostMemoryPpi = {
  25. SecTemporaryRamSupportPostMemory
  26. };
  27. GLOBAL_REMOVE_IF_UNREFERENCED PEI_SEC_PERFORMANCE_PPI mSecPerformancePpi = {
  28. GetPerformancePostMemory
  29. };
  30. STATIC EFI_PEI_PPI_DESCRIPTOR mEdkiiRepublishSecPpiDescriptor = {
  31. (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  32. &gRepublishSecPpiPpiGuid,
  33. &mEdkiiRepublishSecPpiPpi
  34. };
  35. GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_PPI_DESCRIPTOR mSecPlatformInformationPostMemoryDescriptor = {
  36. (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  37. &gEfiSecPlatformInformationPpiGuid,
  38. &mSecPlatformInformationPostMemoryPpi
  39. };
  40. GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_PPI_DESCRIPTOR mSecTemporaryRamDonePostMemoryDescriptor = {
  41. (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  42. &gEfiTemporaryRamDonePpiGuid,
  43. &mSecTemporaryRamDonePostMemoryPpi
  44. };
  45. GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_PPI_DESCRIPTOR mSecTemporaryRamSupportPostMemoryDescriptor = {
  46. (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  47. &gEfiTemporaryRamSupportPpiGuid,
  48. &mSecTemporaryRamSupportPostMemoryPpi
  49. };
  50. GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_PPI_DESCRIPTOR mSecPerformancePpiDescriptor = {
  51. (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  52. &gPeiSecPerformancePpiGuid,
  53. &mSecPerformancePpi
  54. };
  55. /**
  56. Disables the use of Temporary RAM.
  57. If present, this service is invoked by the PEI Foundation after
  58. the EFI_PEI_PERMANANT_MEMORY_INSTALLED_PPI is installed.
  59. @retval EFI_SUCCESS Dummy function, alway return this value.
  60. **/
  61. EFI_STATUS
  62. EFIAPI
  63. SecTemporaryRamDonePostMemory (
  64. VOID
  65. )
  66. {
  67. //
  68. // Temporary RAM Done is already done in post-memory
  69. // install a stub function that is located in permanent memory
  70. //
  71. return EFI_SUCCESS;
  72. }
  73. /**
  74. This service of the EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI that migrates temporary RAM into
  75. permanent memory.
  76. @param PeiServices Pointer to the PEI Services Table.
  77. @param TemporaryMemoryBase Source Address in temporary memory from which the SEC or PEIM will copy the
  78. Temporary RAM contents.
  79. @param PermanentMemoryBase Destination Address in permanent memory into which the SEC or PEIM will copy the
  80. Temporary RAM contents.
  81. @param CopySize Amount of memory to migrate from temporary to permanent memory.
  82. @retval EFI_SUCCESS The data was successfully returned.
  83. @retval EFI_INVALID_PARAMETER PermanentMemoryBase + CopySize > TemporaryMemoryBase when
  84. TemporaryMemoryBase > PermanentMemoryBase.
  85. **/
  86. EFI_STATUS
  87. EFIAPI
  88. SecTemporaryRamSupportPostMemory (
  89. IN CONST EFI_PEI_SERVICES **PeiServices,
  90. IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
  91. IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
  92. IN UINTN CopySize
  93. )
  94. {
  95. //
  96. // Temporary RAM Support is already done in post-memory
  97. // install a stub function that is located in permanent memory
  98. //
  99. return EFI_SUCCESS;
  100. }
  101. /**
  102. This interface conveys performance information out of the Security (SEC) phase into PEI.
  103. This service is published by the SEC phase. The SEC phase handoff has an optional
  104. EFI_PEI_PPI_DESCRIPTOR list as its final argument when control is passed from SEC into the
  105. PEI Foundation. As such, if the platform supports collecting performance data in SEC,
  106. this information is encapsulated into the data structure abstracted by this service.
  107. This information is collected for the boot-strap processor (BSP) on IA-32.
  108. @param[in] PeiServices The pointer to the PEI Services Table.
  109. @param[in] This The pointer to this instance of the PEI_SEC_PERFORMANCE_PPI.
  110. @param[out] Performance The pointer to performance data collected in SEC phase.
  111. @retval EFI_SUCCESS The performance data was successfully returned.
  112. @retval EFI_INVALID_PARAMETER The This or Performance is NULL.
  113. @retval EFI_NOT_FOUND Can't found the HOB created by the SecMigrationPei component.
  114. **/
  115. EFI_STATUS
  116. EFIAPI
  117. GetPerformancePostMemory (
  118. IN CONST EFI_PEI_SERVICES **PeiServices,
  119. IN PEI_SEC_PERFORMANCE_PPI *This,
  120. OUT FIRMWARE_SEC_PERFORMANCE *Performance
  121. )
  122. {
  123. SEC_PLATFORM_INFORMATION_CONTEXT_HOB *SecPlatformInformationContexHob;
  124. if ((This == NULL) || (Performance == NULL)) {
  125. return EFI_INVALID_PARAMETER;
  126. }
  127. SecPlatformInformationContexHob = GetFirstGuidHob (&gEfiCallerIdGuid);
  128. if (SecPlatformInformationContexHob == NULL) {
  129. return EFI_NOT_FOUND;
  130. }
  131. Performance->ResetEnd = SecPlatformInformationContexHob->FirmwareSecPerformance.ResetEnd;
  132. return EFI_SUCCESS;
  133. }
  134. /**
  135. This interface conveys state information out of the Security (SEC) phase into PEI.
  136. @param[in] PeiServices Pointer to the PEI Services Table.
  137. @param[in,out] StructureSize Pointer to the variable describing size of the input buffer.
  138. @param[out] PlatformInformationRecord Pointer to the EFI_SEC_PLATFORM_INFORMATION_RECORD.
  139. @retval EFI_SUCCESS The data was successfully returned.
  140. @retval EFI_NOT_FOUND Can't found the HOB created by SecMigrationPei component.
  141. @retval EFI_BUFFER_TOO_SMALL The size of buffer pointed by StructureSize is too small and will return
  142. the minimal required size in the buffer pointed by StructureSize.
  143. @retval EFI_INVALID_PARAMETER The StructureSize is NULL or PlatformInformationRecord is NULL.
  144. **/
  145. EFI_STATUS
  146. EFIAPI
  147. SecPlatformInformationPostMemory (
  148. IN CONST EFI_PEI_SERVICES **PeiServices,
  149. IN OUT UINT64 *StructureSize,
  150. OUT EFI_SEC_PLATFORM_INFORMATION_RECORD *PlatformInformationRecord
  151. )
  152. {
  153. SEC_PLATFORM_INFORMATION_CONTEXT_HOB *SecPlatformInformationContexHob;
  154. if (StructureSize == NULL) {
  155. return EFI_INVALID_PARAMETER;
  156. }
  157. SecPlatformInformationContexHob = GetFirstGuidHob (&gEfiCallerIdGuid);
  158. if (SecPlatformInformationContexHob == NULL) {
  159. return EFI_NOT_FOUND;
  160. }
  161. if (*StructureSize < SecPlatformInformationContexHob->Context.StructureSize) {
  162. *StructureSize = SecPlatformInformationContexHob->Context.StructureSize;
  163. return EFI_BUFFER_TOO_SMALL;
  164. }
  165. if (PlatformInformationRecord == NULL) {
  166. return EFI_INVALID_PARAMETER;
  167. }
  168. *StructureSize = SecPlatformInformationContexHob->Context.StructureSize;
  169. CopyMem (
  170. (VOID *)PlatformInformationRecord,
  171. (VOID *)SecPlatformInformationContexHob->Context.PlatformInformationRecord,
  172. (UINTN)SecPlatformInformationContexHob->Context.StructureSize
  173. );
  174. return EFI_SUCCESS;
  175. }
  176. /**
  177. This interface re-installs PPIs installed in SecCore from a post-memory PEIM.
  178. This is to allow a platform that may not support relocation of SecCore to update the PPI instance to a post-memory
  179. copy from a PEIM that has been shadowed to permanent memory.
  180. @retval EFI_SUCCESS The SecCore PPIs were re-installed successfully.
  181. @retval Others An error occurred re-installing the SecCore PPIs.
  182. **/
  183. EFI_STATUS
  184. EFIAPI
  185. RepublishSecPpis (
  186. VOID
  187. )
  188. {
  189. EFI_STATUS Status;
  190. EFI_PEI_PPI_DESCRIPTOR *PeiPpiDescriptor;
  191. VOID *PeiPpi;
  192. SEC_PLATFORM_INFORMATION_CONTEXT_HOB *SecPlatformInformationContextHob;
  193. EFI_SEC_PLATFORM_INFORMATION_RECORD *SecPlatformInformationPtr;
  194. UINT64 SecStructureSize;
  195. SecPlatformInformationPtr = NULL;
  196. SecStructureSize = 0;
  197. Status = PeiServicesLocatePpi (
  198. &gEfiTemporaryRamDonePpiGuid,
  199. 0,
  200. &PeiPpiDescriptor,
  201. (VOID **)&PeiPpi
  202. );
  203. if (!EFI_ERROR (Status)) {
  204. Status = PeiServicesReInstallPpi (
  205. PeiPpiDescriptor,
  206. &mSecTemporaryRamDonePostMemoryDescriptor
  207. );
  208. ASSERT_EFI_ERROR (Status);
  209. }
  210. Status = PeiServicesLocatePpi (
  211. &gEfiTemporaryRamSupportPpiGuid,
  212. 0,
  213. &PeiPpiDescriptor,
  214. (VOID **)&PeiPpi
  215. );
  216. if (!EFI_ERROR (Status)) {
  217. Status = PeiServicesReInstallPpi (
  218. PeiPpiDescriptor,
  219. &mSecTemporaryRamSupportPostMemoryDescriptor
  220. );
  221. ASSERT_EFI_ERROR (Status);
  222. }
  223. Status = PeiServicesCreateHob (
  224. EFI_HOB_TYPE_GUID_EXTENSION,
  225. sizeof (SEC_PLATFORM_INFORMATION_CONTEXT_HOB),
  226. (VOID **)&SecPlatformInformationContextHob
  227. );
  228. ASSERT_EFI_ERROR (Status);
  229. if (EFI_ERROR (Status)) {
  230. DEBUG ((DEBUG_ERROR, "SecPlatformInformation Context HOB could not be created.\n"));
  231. return Status;
  232. }
  233. SecPlatformInformationContextHob->Header.Name = gEfiCallerIdGuid;
  234. SecPlatformInformationContextHob->Revision = 1;
  235. Status = PeiServicesLocatePpi (
  236. &gPeiSecPerformancePpiGuid,
  237. 0,
  238. &PeiPpiDescriptor,
  239. (VOID **)&PeiPpi
  240. );
  241. if (!EFI_ERROR (Status)) {
  242. Status = ((PEI_SEC_PERFORMANCE_PPI *)PeiPpi)->GetPerformance (
  243. GetPeiServicesTablePointer (),
  244. (PEI_SEC_PERFORMANCE_PPI *)PeiPpi,
  245. &SecPlatformInformationContextHob->FirmwareSecPerformance
  246. );
  247. ASSERT_EFI_ERROR (Status);
  248. if (!EFI_ERROR (Status)) {
  249. Status = PeiServicesReInstallPpi (
  250. PeiPpiDescriptor,
  251. &mSecPerformancePpiDescriptor
  252. );
  253. ASSERT_EFI_ERROR (Status);
  254. }
  255. }
  256. Status = PeiServicesLocatePpi (
  257. &gEfiSecPlatformInformationPpiGuid,
  258. 0,
  259. &PeiPpiDescriptor,
  260. (VOID **)&PeiPpi
  261. );
  262. if (!EFI_ERROR (Status)) {
  263. Status = ((EFI_SEC_PLATFORM_INFORMATION_PPI *)PeiPpi)->PlatformInformation (
  264. GetPeiServicesTablePointer (),
  265. &SecStructureSize,
  266. SecPlatformInformationPtr
  267. );
  268. ASSERT (Status == EFI_BUFFER_TOO_SMALL);
  269. if (Status != EFI_BUFFER_TOO_SMALL) {
  270. return EFI_NOT_FOUND;
  271. }
  272. ZeroMem ((VOID *)&(SecPlatformInformationContextHob->Context), sizeof (SEC_PLATFORM_INFORMATION_CONTEXT));
  273. SecPlatformInformationContextHob->Context.PlatformInformationRecord = AllocatePool ((UINTN)SecStructureSize);
  274. ASSERT (SecPlatformInformationContextHob->Context.PlatformInformationRecord != NULL);
  275. if (SecPlatformInformationContextHob->Context.PlatformInformationRecord == NULL) {
  276. return EFI_OUT_OF_RESOURCES;
  277. }
  278. SecPlatformInformationContextHob->Context.StructureSize = SecStructureSize;
  279. Status = ((EFI_SEC_PLATFORM_INFORMATION_PPI *)PeiPpi)->PlatformInformation (
  280. GetPeiServicesTablePointer (),
  281. &(SecPlatformInformationContextHob->Context.StructureSize),
  282. SecPlatformInformationContextHob->Context.PlatformInformationRecord
  283. );
  284. ASSERT_EFI_ERROR (Status);
  285. if (!EFI_ERROR (Status)) {
  286. Status = PeiServicesReInstallPpi (
  287. PeiPpiDescriptor,
  288. &mSecPlatformInformationPostMemoryDescriptor
  289. );
  290. ASSERT_EFI_ERROR (Status);
  291. }
  292. }
  293. return EFI_SUCCESS;
  294. }
  295. /**
  296. This function is the entry point which installs an instance of REPUBLISH_SEC_PPI_PPI.
  297. It install the RepublishSecPpi depent on PcdMigrateTemporaryRamFirmwareVolumes, install
  298. the PPI when the PcdMigrateTemporaryRamFirmwareVolumes enabled.
  299. @param[in] FileHandle Pointer to image file handle.
  300. @param[in] PeiServices Pointer to PEI Services Table
  301. @retval EFI_ABORTED Disable evacuate temporary memory feature by disable
  302. PcdMigrateTemporaryRamFirmwareVolumes.
  303. @retval EFI_SUCCESS An instance of REPUBLISH_SEC_PPI_PPI was installed successfully.
  304. @retval Others An error occurred installing and instance of REPUBLISH_SEC_PPI_PPI.
  305. **/
  306. EFI_STATUS
  307. EFIAPI
  308. SecMigrationPeiInitialize (
  309. IN EFI_PEI_FILE_HANDLE FileHandle,
  310. IN CONST EFI_PEI_SERVICES **PeiServices
  311. )
  312. {
  313. EFI_STATUS Status;
  314. Status = EFI_ABORTED;
  315. if (PcdGetBool (PcdMigrateTemporaryRamFirmwareVolumes)) {
  316. Status = PeiServicesInstallPpi (&mEdkiiRepublishSecPpiDescriptor);
  317. ASSERT_EFI_ERROR (Status);
  318. }
  319. return Status;
  320. }