PiSmmCommunicationSmm.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /** @file
  2. PiSmmCommunication SMM Driver.
  3. Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiSmm.h>
  7. #include <Library/UefiDriverEntryPoint.h>
  8. #include <Library/UefiBootServicesTableLib.h>
  9. #include <Library/UefiRuntimeServicesTableLib.h>
  10. #include <Library/SmmServicesTableLib.h>
  11. #include <Library/BaseLib.h>
  12. #include <Library/BaseMemoryLib.h>
  13. #include <Library/DebugLib.h>
  14. #include <Library/SmmMemLib.h>
  15. #include <Protocol/SmmSwDispatch2.h>
  16. #include <Protocol/SmmCommunication.h>
  17. #include <Ppi/SmmCommunication.h>
  18. #include "PiSmmCommunicationPrivate.h"
  19. EFI_SMM_COMMUNICATION_CONTEXT mSmmCommunicationContext = {
  20. SMM_COMMUNICATION_SIGNATURE
  21. };
  22. /**
  23. Set SMM communication context.
  24. **/
  25. VOID
  26. SetCommunicationContext (
  27. VOID
  28. )
  29. {
  30. EFI_STATUS Status;
  31. Status = gSmst->SmmInstallConfigurationTable (
  32. gSmst,
  33. &gEfiPeiSmmCommunicationPpiGuid,
  34. &mSmmCommunicationContext,
  35. sizeof (mSmmCommunicationContext)
  36. );
  37. ASSERT_EFI_ERROR (Status);
  38. }
  39. /**
  40. Dispatch function for a Software SMI handler.
  41. @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
  42. @param Context Points to an optional handler context which was specified when the
  43. handler was registered.
  44. @param CommBuffer A pointer to a collection of data in memory that will
  45. be conveyed from a non-SMM environment into an SMM environment.
  46. @param CommBufferSize The size of the CommBuffer.
  47. @retval EFI_SUCCESS Command is handled successfully.
  48. **/
  49. EFI_STATUS
  50. EFIAPI
  51. PiSmmCommunicationHandler (
  52. IN EFI_HANDLE DispatchHandle,
  53. IN CONST VOID *Context OPTIONAL,
  54. IN OUT VOID *CommBuffer OPTIONAL,
  55. IN OUT UINTN *CommBufferSize OPTIONAL
  56. )
  57. {
  58. UINTN CommSize;
  59. EFI_STATUS Status;
  60. EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;
  61. EFI_PHYSICAL_ADDRESS *BufferPtrAddress;
  62. DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler Enter\n"));
  63. BufferPtrAddress = (EFI_PHYSICAL_ADDRESS *)(UINTN)mSmmCommunicationContext.BufferPtrAddress;
  64. CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)(UINTN)*BufferPtrAddress;
  65. DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler CommunicateHeader - %x\n", CommunicateHeader));
  66. if (CommunicateHeader == NULL) {
  67. DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler is NULL, needn't to call dispatch function\n"));
  68. Status = EFI_SUCCESS;
  69. } else {
  70. if (!SmmIsBufferOutsideSmmValid ((UINTN)CommunicateHeader, OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data))) {
  71. DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler CommunicateHeader invalid - 0x%x\n", CommunicateHeader));
  72. Status = EFI_SUCCESS;
  73. goto Done;
  74. }
  75. CommSize = (UINTN)CommunicateHeader->MessageLength;
  76. if (!SmmIsBufferOutsideSmmValid ((UINTN)&CommunicateHeader->Data[0], CommSize)) {
  77. DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler CommunicateData invalid - 0x%x\n", &CommunicateHeader->Data[0]));
  78. Status = EFI_SUCCESS;
  79. goto Done;
  80. }
  81. //
  82. // Call dispatch function
  83. //
  84. DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler Data - %x\n", &CommunicateHeader->Data[0]));
  85. Status = gSmst->SmiManage (
  86. &CommunicateHeader->HeaderGuid,
  87. NULL,
  88. &CommunicateHeader->Data[0],
  89. &CommSize
  90. );
  91. }
  92. Done:
  93. DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler %r\n", Status));
  94. DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler Exit\n"));
  95. return (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_INTERRUPT_PENDING;
  96. }
  97. /**
  98. Allocate EfiACPIMemoryNVS below 4G memory address.
  99. This function allocates EfiACPIMemoryNVS below 4G memory address.
  100. @param Size Size of memory to allocate.
  101. @return Allocated address for output.
  102. **/
  103. VOID *
  104. AllocateAcpiNvsMemoryBelow4G (
  105. IN UINTN Size
  106. )
  107. {
  108. UINTN Pages;
  109. EFI_PHYSICAL_ADDRESS Address;
  110. EFI_STATUS Status;
  111. VOID *Buffer;
  112. Pages = EFI_SIZE_TO_PAGES (Size);
  113. Address = 0xffffffff;
  114. Status = gBS->AllocatePages (
  115. AllocateMaxAddress,
  116. EfiACPIMemoryNVS,
  117. Pages,
  118. &Address
  119. );
  120. ASSERT_EFI_ERROR (Status);
  121. Buffer = (VOID *)(UINTN)Address;
  122. ZeroMem (Buffer, Size);
  123. return Buffer;
  124. }
  125. /**
  126. Entry Point for PI SMM communication SMM driver.
  127. @param[in] ImageHandle Image handle of this driver.
  128. @param[in] SystemTable A Pointer to the EFI System Table.
  129. @retval EFI_SUCCESS
  130. @return Others Some error occurs.
  131. **/
  132. EFI_STATUS
  133. EFIAPI
  134. PiSmmCommunicationSmmEntryPoint (
  135. IN EFI_HANDLE ImageHandle,
  136. IN EFI_SYSTEM_TABLE *SystemTable
  137. )
  138. {
  139. EFI_STATUS Status;
  140. EFI_SMM_SW_DISPATCH2_PROTOCOL *SmmSwDispatch2;
  141. EFI_SMM_SW_REGISTER_CONTEXT SmmSwDispatchContext;
  142. EFI_HANDLE DispatchHandle;
  143. EFI_PHYSICAL_ADDRESS *BufferPtrAddress;
  144. //
  145. // Register software SMI handler
  146. //
  147. Status = gSmst->SmmLocateProtocol (
  148. &gEfiSmmSwDispatch2ProtocolGuid,
  149. NULL,
  150. (VOID **)&SmmSwDispatch2
  151. );
  152. ASSERT_EFI_ERROR (Status);
  153. SmmSwDispatchContext.SwSmiInputValue = (UINTN)-1;
  154. Status = SmmSwDispatch2->Register (
  155. SmmSwDispatch2,
  156. PiSmmCommunicationHandler,
  157. &SmmSwDispatchContext,
  158. &DispatchHandle
  159. );
  160. ASSERT_EFI_ERROR (Status);
  161. DEBUG ((DEBUG_INFO, "SmmCommunication SwSmi: %x\n", (UINTN)SmmSwDispatchContext.SwSmiInputValue));
  162. BufferPtrAddress = AllocateAcpiNvsMemoryBelow4G (sizeof (EFI_PHYSICAL_ADDRESS));
  163. ASSERT (BufferPtrAddress != NULL);
  164. DEBUG ((DEBUG_INFO, "SmmCommunication BufferPtrAddress: 0x%016lx, BufferPtr: 0x%016lx\n", (EFI_PHYSICAL_ADDRESS)(UINTN)BufferPtrAddress, *BufferPtrAddress));
  165. //
  166. // Save context
  167. //
  168. mSmmCommunicationContext.SwSmiNumber = (UINT32)SmmSwDispatchContext.SwSmiInputValue;
  169. mSmmCommunicationContext.BufferPtrAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)BufferPtrAddress;
  170. SetCommunicationContext ();
  171. return Status;
  172. }