SmmAccessDriver.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /** @file
  2. This is the driver that publishes the SMM Access Protocol
  3. instance for System Agent.
  4. Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "SmmAccessDriver.h"
  8. static SMM_ACCESS_PRIVATE_DATA mSmmAccess;
  9. /**
  10. <b>SMM Access Driver Entry Point</b>
  11. This driver installs an SMM Access Protocol
  12. - <b>Introduction</b> \n
  13. This module publishes the SMM access protocol. The protocol is used by the SMM Base driver to access the SMRAM region when the processor is not in SMM.
  14. The SMM Base driver uses the services provided by the SMM access protocol to open SMRAM during POST and copy the SMM handler.
  15. SMM access protocol is also used to close the SMRAM region once the copying is done.
  16. Finally, the SMM access protocol provides services to "Lock" the SMRAM region.
  17. Please refer the SMM Protocols section in the attached SMM CIS Specification version 0.9 for further details.
  18. This driver is required if SMM is supported. Proper configuration of SMM registers is recommended even if SMM is not supported.
  19. - <b>Porting Recommendations</b> \n
  20. No modification of this module is recommended. Any modification should be done in compliance with the _EFI_SMM_ACCESS_PROTOCOL protocol definition.
  21. @param[in] ImageHandle - Handle for the image of this driver
  22. @param[in] SystemTable - Pointer to the EFI System Table
  23. @retval EFI_SUCCESS - Protocol was installed successfully
  24. @retval EFI_UNSUPPORTED - Protocol was not installed
  25. @retval EFI_NOT_FOUND - Protocol can't be found.
  26. @retval EFI_OUT_OF_RESOURCES - Protocol does not have enough resources to initialize the driver.
  27. **/
  28. EFI_STATUS
  29. EFIAPI
  30. SmmAccessDriverEntryPoint (
  31. IN EFI_HANDLE ImageHandle,
  32. IN EFI_SYSTEM_TABLE *SystemTable
  33. )
  34. {
  35. EFI_STATUS Status;
  36. UINTN Index;
  37. EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *DescriptorBlock;
  38. EFI_PEI_HOB_POINTERS *Hob;
  39. //
  40. // Initialize Global variables
  41. //
  42. ZeroMem (&mSmmAccess, sizeof (mSmmAccess));
  43. mSmmAccess.Signature = SMM_ACCESS_PRIVATE_DATA_SIGNATURE;
  44. mSmmAccess.Handle = NULL;
  45. //
  46. // Get Hob list
  47. //
  48. Hob = GetFirstGuidHob (&gEfiSmmSmramMemoryGuid);
  49. if (Hob == NULL) {
  50. DEBUG ((DEBUG_WARN, "SmramMemoryReserve HOB not found\n"));
  51. return EFI_NOT_FOUND;
  52. }
  53. DescriptorBlock = (VOID *) ((UINT8 *) Hob + sizeof (EFI_HOB_GUID_TYPE));
  54. //
  55. // Alloc space for mSmmAccess.SmramDesc
  56. //
  57. mSmmAccess.SmramDesc = AllocateZeroPool ((DescriptorBlock->NumberOfSmmReservedRegions) * sizeof (EFI_SMRAM_DESCRIPTOR));
  58. if (mSmmAccess.SmramDesc == NULL) {
  59. DEBUG ((DEBUG_WARN, "Alloc mSmmAccess.SmramDesc fail.\n"));
  60. return EFI_OUT_OF_RESOURCES;
  61. }
  62. DEBUG ((DEBUG_INFO, "Alloc mSmmAccess.SmramDesc success.\n"));
  63. //
  64. // Use the HOB to publish SMRAM capabilities
  65. //
  66. for (Index = 0; Index < DescriptorBlock->NumberOfSmmReservedRegions; Index++) {
  67. mSmmAccess.SmramDesc[Index].PhysicalStart = DescriptorBlock->Descriptor[Index].PhysicalStart;
  68. mSmmAccess.SmramDesc[Index].CpuStart = DescriptorBlock->Descriptor[Index].CpuStart;
  69. mSmmAccess.SmramDesc[Index].PhysicalSize = DescriptorBlock->Descriptor[Index].PhysicalSize;
  70. mSmmAccess.SmramDesc[Index].RegionState = DescriptorBlock->Descriptor[Index].RegionState;
  71. }
  72. mSmmAccess.NumberRegions = Index;
  73. mSmmAccess.SmmAccess.Open = Open;
  74. mSmmAccess.SmmAccess.Close = Close;
  75. mSmmAccess.SmmAccess.Lock = Lock;
  76. mSmmAccess.SmmAccess.GetCapabilities = GetCapabilities;
  77. mSmmAccess.SmmAccess.LockState = FALSE;
  78. mSmmAccess.SmmAccess.OpenState = FALSE;
  79. //
  80. // Install our protocol interfaces on the device's handle
  81. //
  82. Status = gBS->InstallMultipleProtocolInterfaces (
  83. &mSmmAccess.Handle,
  84. &gEfiSmmAccess2ProtocolGuid,
  85. &mSmmAccess.SmmAccess,
  86. NULL
  87. );
  88. if (EFI_ERROR (Status)) {
  89. DEBUG ((DEBUG_WARN, "InstallMultipleProtocolInterfaces returned %r\n", Status));
  90. return EFI_UNSUPPORTED;
  91. }
  92. return EFI_SUCCESS;
  93. }
  94. /**
  95. This routine accepts a request to "open" a region of SMRAM. The
  96. region could be legacy ABSEG, HSEG, or TSEG near top of physical memory.
  97. The use of "open" means that the memory is visible from all boot-service
  98. and SMM agents.
  99. @param[in] This - Pointer to the SMM Access Interface.
  100. @retval EFI_SUCCESS - The region was successfully opened.
  101. @retval EFI_DEVICE_ERROR - The region could not be opened because locked by chipset.
  102. @retval EFI_INVALID_PARAMETER - The descriptor index was out of bounds.
  103. **/
  104. EFI_STATUS
  105. EFIAPI
  106. Open (
  107. IN EFI_SMM_ACCESS2_PROTOCOL *This
  108. )
  109. {
  110. SMM_ACCESS_PRIVATE_DATA *SmmAccess;
  111. UINTN DescriptorIndex;
  112. SmmAccess = SMM_ACCESS_PRIVATE_DATA_FROM_THIS (This);
  113. for (DescriptorIndex = 0; DescriptorIndex < SmmAccess->NumberRegions; DescriptorIndex++) {
  114. if (SmmAccess->SmramDesc[DescriptorIndex].RegionState & EFI_SMRAM_LOCKED) {
  115. DEBUG ((DEBUG_WARN, "Cannot open a locked SMRAM region\n"));
  116. return EFI_DEVICE_ERROR;
  117. }
  118. }
  119. for (DescriptorIndex = 0; DescriptorIndex < SmmAccess->NumberRegions; DescriptorIndex++) {
  120. SmmAccess->SmramDesc[DescriptorIndex].RegionState &= (UINT64) ~(EFI_SMRAM_CLOSED | EFI_ALLOCATED);
  121. SmmAccess->SmramDesc[DescriptorIndex].RegionState |= (UINT64) EFI_SMRAM_OPEN;
  122. }
  123. SmmAccess->SmmAccess.OpenState = TRUE;
  124. return EFI_SUCCESS;
  125. }
  126. /**
  127. This routine accepts a request to "close" a region of SMRAM. The
  128. region could be legacy AB or TSEG near top of physical memory.
  129. The use of "close" means that the memory is only visible from SMM agents,
  130. not from BS or RT code.
  131. @param[in] This - Pointer to the SMM Access Interface.
  132. @retval EFI_SUCCESS - The region was successfully closed.
  133. @retval EFI_DEVICE_ERROR - The region could not be closed because locked by chipset.
  134. @retval EFI_INVALID_PARAMETER - The descriptor index was out of bounds.
  135. **/
  136. EFI_STATUS
  137. EFIAPI
  138. Close (
  139. IN EFI_SMM_ACCESS2_PROTOCOL *This
  140. )
  141. {
  142. SMM_ACCESS_PRIVATE_DATA *SmmAccess;
  143. BOOLEAN OpenState;
  144. UINT8 Index;
  145. UINTN DescriptorIndex;
  146. SmmAccess = SMM_ACCESS_PRIVATE_DATA_FROM_THIS (This);
  147. for (DescriptorIndex = 0; DescriptorIndex < SmmAccess->NumberRegions; DescriptorIndex++) {
  148. if (SmmAccess->SmramDesc[DescriptorIndex].RegionState & EFI_SMRAM_LOCKED) {
  149. DEBUG ((DEBUG_WARN, "Cannot close a locked SMRAM region\n"));
  150. continue;
  151. }
  152. SmmAccess->SmramDesc[DescriptorIndex].RegionState &= (UINT64) ~EFI_SMRAM_OPEN;
  153. SmmAccess->SmramDesc[DescriptorIndex].RegionState |= (UINT64) (EFI_SMRAM_CLOSED | EFI_ALLOCATED);
  154. }
  155. //
  156. // Find out if any regions are still open
  157. //
  158. OpenState = FALSE;
  159. for (Index = 0; Index < mSmmAccess.NumberRegions; Index++) {
  160. if ((SmmAccess->SmramDesc[Index].RegionState & EFI_SMRAM_OPEN) == EFI_SMRAM_OPEN) {
  161. OpenState = TRUE;
  162. }
  163. }
  164. SmmAccess->SmmAccess.OpenState = OpenState;
  165. return EFI_SUCCESS;
  166. }
  167. /**
  168. This routine accepts a request to "lock" SMRAM. The
  169. region could be legacy AB or TSEG near top of physical memory.
  170. The use of "lock" means that the memory can no longer be opened
  171. to BS state..
  172. @param[in] This - Pointer to the SMM Access Interface.
  173. @retval EFI_SUCCESS - The region was successfully locked.
  174. @retval EFI_DEVICE_ERROR - The region could not be locked because at least
  175. one range is still open.
  176. @retval EFI_INVALID_PARAMETER - The descriptor index was out of bounds.
  177. **/
  178. EFI_STATUS
  179. EFIAPI
  180. Lock (
  181. IN EFI_SMM_ACCESS2_PROTOCOL *This
  182. )
  183. {
  184. SMM_ACCESS_PRIVATE_DATA *SmmAccess;
  185. UINTN DescriptorIndex;
  186. SmmAccess = SMM_ACCESS_PRIVATE_DATA_FROM_THIS (This);
  187. if (SmmAccess->SmmAccess.OpenState) {
  188. DEBUG ((DEBUG_WARN, "Cannot lock SMRAM when SMRAM regions are still open\n"));
  189. return EFI_DEVICE_ERROR;
  190. }
  191. for (DescriptorIndex = 0; DescriptorIndex < SmmAccess->NumberRegions; DescriptorIndex++) {
  192. SmmAccess->SmramDesc[DescriptorIndex].RegionState |= EFI_SMRAM_LOCKED;
  193. }
  194. SmmAccess->SmmAccess.LockState = TRUE;
  195. return EFI_SUCCESS;
  196. }
  197. /**
  198. This routine services a user request to discover the SMRAM
  199. capabilities of this platform. This will report the possible
  200. ranges that are possible for SMRAM access, based upon the
  201. memory controller capabilities.
  202. @param[in] This - Pointer to the SMRAM Access Interface.
  203. @param[in, out] SmramMapSize - Pointer to the variable containing size of the
  204. buffer to contain the description information.
  205. @param[in, out] SmramMap - Buffer containing the data describing the Smram
  206. region descriptors.
  207. @retval EFI_BUFFER_TOO_SMALL - The user did not provide a sufficient buffer.
  208. @retval EFI_SUCCESS - The user provided a sufficiently-sized buffer.
  209. **/
  210. EFI_STATUS
  211. EFIAPI
  212. GetCapabilities (
  213. IN CONST EFI_SMM_ACCESS2_PROTOCOL *This,
  214. IN OUT UINTN *SmramMapSize,
  215. IN OUT EFI_SMRAM_DESCRIPTOR *SmramMap
  216. )
  217. {
  218. EFI_STATUS Status;
  219. SMM_ACCESS_PRIVATE_DATA *SmmAccess;
  220. UINTN NecessaryBufferSize;
  221. SmmAccess = SMM_ACCESS_PRIVATE_DATA_FROM_THIS (This);
  222. NecessaryBufferSize = SmmAccess->NumberRegions * sizeof (EFI_SMRAM_DESCRIPTOR);
  223. if (*SmramMapSize < NecessaryBufferSize) {
  224. DEBUG ((DEBUG_WARN, "SMRAM Map Buffer too small\n"));
  225. Status = EFI_BUFFER_TOO_SMALL;
  226. } else {
  227. CopyMem (SmramMap, SmmAccess->SmramDesc, NecessaryBufferSize);
  228. Status = EFI_SUCCESS;
  229. }
  230. *SmramMapSize = NecessaryBufferSize;
  231. return Status;
  232. }