PiSmmIpl.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. /** @file
  2. SMM IPL that produces SMM related runtime protocols and load the SMM Core into SMRAM
  3. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiDxe.h>
  7. #include <Protocol/SmmBase2.h>
  8. #include <Protocol/SmmCommunication.h>
  9. #include <Protocol/MmCommunication2.h>
  10. #include <Protocol/SmmAccess2.h>
  11. #include <Protocol/SmmConfiguration.h>
  12. #include <Protocol/SmmControl2.h>
  13. #include <Protocol/DxeSmmReadyToLock.h>
  14. #include <Protocol/Cpu.h>
  15. #include <Guid/EventGroup.h>
  16. #include <Guid/EventLegacyBios.h>
  17. #include <Guid/LoadModuleAtFixedAddress.h>
  18. #include <Library/BaseLib.h>
  19. #include <Library/BaseMemoryLib.h>
  20. #include <Library/PeCoffLib.h>
  21. #include <Library/CacheMaintenanceLib.h>
  22. #include <Library/MemoryAllocationLib.h>
  23. #include <Library/DebugLib.h>
  24. #include <Library/UefiBootServicesTableLib.h>
  25. #include <Library/DxeServicesTableLib.h>
  26. #include <Library/DxeServicesLib.h>
  27. #include <Library/UefiLib.h>
  28. #include <Library/UefiRuntimeLib.h>
  29. #include <Library/PcdLib.h>
  30. #include <Library/ReportStatusCodeLib.h>
  31. #include "PiSmmCorePrivateData.h"
  32. #include <Library/SafeIntLib.h>
  33. #define SMRAM_CAPABILITIES (EFI_MEMORY_WB | EFI_MEMORY_UC)
  34. //
  35. // Function prototypes from produced protocols
  36. //
  37. /**
  38. Indicate whether the driver is currently executing in the SMM Initialization phase.
  39. @param This The EFI_SMM_BASE2_PROTOCOL instance.
  40. @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
  41. inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
  42. @retval EFI_INVALID_PARAMETER InSmram was NULL.
  43. @retval EFI_SUCCESS The call returned successfully.
  44. **/
  45. EFI_STATUS
  46. EFIAPI
  47. SmmBase2InSmram (
  48. IN CONST EFI_SMM_BASE2_PROTOCOL *This,
  49. OUT BOOLEAN *InSmram
  50. );
  51. /**
  52. Retrieves the location of the System Management System Table (SMST).
  53. @param This The EFI_SMM_BASE2_PROTOCOL instance.
  54. @param Smst On return, points to a pointer to the System Management Service Table (SMST).
  55. @retval EFI_INVALID_PARAMETER Smst or This was invalid.
  56. @retval EFI_SUCCESS The memory was returned to the system.
  57. @retval EFI_UNSUPPORTED Not in SMM.
  58. **/
  59. EFI_STATUS
  60. EFIAPI
  61. SmmBase2GetSmstLocation (
  62. IN CONST EFI_SMM_BASE2_PROTOCOL *This,
  63. OUT EFI_SMM_SYSTEM_TABLE2 **Smst
  64. );
  65. /**
  66. Communicates with a registered handler.
  67. This function provides a service to send and receive messages from a registered
  68. UEFI service. This function is part of the SMM Communication Protocol that may
  69. be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
  70. after SetVirtualAddressMap().
  71. @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
  72. @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
  73. @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data
  74. being returned. Zero if the handler does not wish to reply with any data.
  75. This parameter is optional and may be NULL.
  76. @retval EFI_SUCCESS The message was successfully posted.
  77. @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
  78. @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
  79. If this error is returned, the MessageLength field
  80. in the CommBuffer header or the integer pointed by
  81. CommSize, are updated to reflect the maximum payload
  82. size the implementation can accommodate.
  83. @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
  84. if not omitted, are in address range that cannot be
  85. accessed by the MM environment.
  86. **/
  87. EFI_STATUS
  88. EFIAPI
  89. SmmCommunicationCommunicate (
  90. IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,
  91. IN OUT VOID *CommBuffer,
  92. IN OUT UINTN *CommSize OPTIONAL
  93. );
  94. /**
  95. Communicates with a registered handler.
  96. This function provides a service to send and receive messages from a registered UEFI service.
  97. @param[in] This The EFI_MM_COMMUNICATION_PROTOCOL instance.
  98. @param[in] CommBufferPhysical Physical address of the MM communication buffer
  99. @param[in] CommBufferVirtual Virtual address of the MM communication buffer
  100. @param[in] CommSize The size of the data buffer being passed in. On exit, the size of data
  101. being returned. Zero if the handler does not wish to reply with any data.
  102. This parameter is optional and may be NULL.
  103. @retval EFI_SUCCESS The message was successfully posted.
  104. @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
  105. @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
  106. If this error is returned, the MessageLength field
  107. in the CommBuffer header or the integer pointed by
  108. CommSize, are updated to reflect the maximum payload
  109. size the implementation can accommodate.
  110. @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
  111. if not omitted, are in address range that cannot be
  112. accessed by the MM environment.
  113. **/
  114. EFI_STATUS
  115. EFIAPI
  116. SmmCommunicationMmCommunicate2 (
  117. IN CONST EFI_MM_COMMUNICATION2_PROTOCOL *This,
  118. IN OUT VOID *CommBufferPhysical,
  119. IN OUT VOID *CommBufferVirtual,
  120. IN OUT UINTN *CommSize OPTIONAL
  121. );
  122. /**
  123. Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
  124. @param Event The Event that is being processed, not used.
  125. @param Context Event Context, not used.
  126. **/
  127. VOID
  128. EFIAPI
  129. SmmIplSmmConfigurationEventNotify (
  130. IN EFI_EVENT Event,
  131. IN VOID *Context
  132. );
  133. /**
  134. Event notification that is fired every time a DxeSmmReadyToLock protocol is added
  135. or if gEfiEventReadyToBootGuid is signalled.
  136. @param Event The Event that is being processed, not used.
  137. @param Context Event Context, not used.
  138. **/
  139. VOID
  140. EFIAPI
  141. SmmIplReadyToLockEventNotify (
  142. IN EFI_EVENT Event,
  143. IN VOID *Context
  144. );
  145. /**
  146. Event notification that is fired when DxeDispatch Event Group is signaled.
  147. @param Event The Event that is being processed, not used.
  148. @param Context Event Context, not used.
  149. **/
  150. VOID
  151. EFIAPI
  152. SmmIplDxeDispatchEventNotify (
  153. IN EFI_EVENT Event,
  154. IN VOID *Context
  155. );
  156. /**
  157. Event notification that is fired when a GUIDed Event Group is signaled.
  158. @param Event The Event that is being processed, not used.
  159. @param Context Event Context, not used.
  160. **/
  161. VOID
  162. EFIAPI
  163. SmmIplGuidedEventNotify (
  164. IN EFI_EVENT Event,
  165. IN VOID *Context
  166. );
  167. /**
  168. Event notification that is fired when EndOfDxe Event Group is signaled.
  169. @param Event The Event that is being processed, not used.
  170. @param Context Event Context, not used.
  171. **/
  172. VOID
  173. EFIAPI
  174. SmmIplEndOfDxeEventNotify (
  175. IN EFI_EVENT Event,
  176. IN VOID *Context
  177. );
  178. /**
  179. Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
  180. This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
  181. It convers pointer to new virtual address.
  182. @param Event Event whose notification function is being invoked.
  183. @param Context Pointer to the notification function's context.
  184. **/
  185. VOID
  186. EFIAPI
  187. SmmIplSetVirtualAddressNotify (
  188. IN EFI_EVENT Event,
  189. IN VOID *Context
  190. );
  191. //
  192. // Data structure used to declare a table of protocol notifications and event
  193. // notifications required by the SMM IPL
  194. //
  195. typedef struct {
  196. BOOLEAN Protocol;
  197. BOOLEAN CloseOnLock;
  198. EFI_GUID *Guid;
  199. EFI_EVENT_NOTIFY NotifyFunction;
  200. VOID *NotifyContext;
  201. EFI_TPL NotifyTpl;
  202. EFI_EVENT Event;
  203. } SMM_IPL_EVENT_NOTIFICATION;
  204. //
  205. // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol
  206. //
  207. EFI_HANDLE mSmmIplHandle = NULL;
  208. //
  209. // SMM Base 2 Protocol instance
  210. //
  211. EFI_SMM_BASE2_PROTOCOL mSmmBase2 = {
  212. SmmBase2InSmram,
  213. SmmBase2GetSmstLocation
  214. };
  215. //
  216. // SMM Communication Protocol instance
  217. //
  218. EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication = {
  219. SmmCommunicationCommunicate
  220. };
  221. //
  222. // PI 1.7 MM Communication Protocol 2 instance
  223. //
  224. EFI_MM_COMMUNICATION2_PROTOCOL mMmCommunication2 = {
  225. SmmCommunicationMmCommunicate2
  226. };
  227. //
  228. // SMM Core Private Data structure that contains the data shared between
  229. // the SMM IPL and the SMM Core.
  230. //
  231. SMM_CORE_PRIVATE_DATA mSmmCorePrivateData = {
  232. SMM_CORE_PRIVATE_DATA_SIGNATURE, // Signature
  233. NULL, // SmmIplImageHandle
  234. 0, // SmramRangeCount
  235. NULL, // SmramRanges
  236. NULL, // SmmEntryPoint
  237. FALSE, // SmmEntryPointRegistered
  238. FALSE, // InSmm
  239. NULL, // Smst
  240. NULL, // CommunicationBuffer
  241. 0, // BufferSize
  242. EFI_SUCCESS // ReturnStatus
  243. };
  244. //
  245. // Global pointer used to access mSmmCorePrivateData from outside and inside SMM
  246. //
  247. SMM_CORE_PRIVATE_DATA *gSmmCorePrivate = &mSmmCorePrivateData;
  248. //
  249. // SMM IPL global variables
  250. //
  251. EFI_SMM_CONTROL2_PROTOCOL *mSmmControl2;
  252. EFI_SMM_ACCESS2_PROTOCOL *mSmmAccess;
  253. EFI_SMRAM_DESCRIPTOR *mCurrentSmramRange;
  254. BOOLEAN mSmmLocked = FALSE;
  255. BOOLEAN mEndOfDxe = FALSE;
  256. EFI_PHYSICAL_ADDRESS mSmramCacheBase;
  257. UINT64 mSmramCacheSize;
  258. EFI_SMM_COMMUNICATE_HEADER mCommunicateHeader;
  259. EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE *mLMFAConfigurationTable = NULL;
  260. //
  261. // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires
  262. //
  263. SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents[] = {
  264. //
  265. // Declare protocol notification on the SMM Configuration protocol. When this notification is established,
  266. // the associated event is immediately signalled, so the notification function will be executed and the
  267. // SMM Configuration Protocol will be found if it is already in the handle database.
  268. //
  269. { TRUE, FALSE, &gEfiSmmConfigurationProtocolGuid, SmmIplSmmConfigurationEventNotify, &gEfiSmmConfigurationProtocolGuid, TPL_NOTIFY, NULL },
  270. //
  271. // Declare protocol notification on DxeSmmReadyToLock protocols. When this notification is established,
  272. // the associated event is immediately signalled, so the notification function will be executed and the
  273. // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.
  274. //
  275. { TRUE, TRUE, &gEfiDxeSmmReadyToLockProtocolGuid, SmmIplReadyToLockEventNotify, &gEfiDxeSmmReadyToLockProtocolGuid, TPL_CALLBACK, NULL },
  276. //
  277. // Declare event notification on EndOfDxe event. When this notification is established,
  278. // the associated event is immediately signalled, so the notification function will be executed and the
  279. // SMM End Of Dxe Protocol will be found if it is already in the handle database.
  280. //
  281. { FALSE, TRUE, &gEfiEndOfDxeEventGroupGuid, SmmIplGuidedEventNotify, &gEfiEndOfDxeEventGroupGuid, TPL_CALLBACK, NULL },
  282. //
  283. // Declare event notification on EndOfDxe event. This is used to set EndOfDxe event signaled flag.
  284. //
  285. { FALSE, TRUE, &gEfiEndOfDxeEventGroupGuid, SmmIplEndOfDxeEventNotify, &gEfiEndOfDxeEventGroupGuid, TPL_CALLBACK, NULL },
  286. //
  287. // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core
  288. // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core
  289. // if notified, so the SMM Core can dispatch SMM drivers.
  290. //
  291. { FALSE, TRUE, &gEfiEventDxeDispatchGuid, SmmIplDxeDispatchEventNotify, &gEfiEventDxeDispatchGuid, TPL_CALLBACK, NULL },
  292. //
  293. // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is
  294. // used to make sure SMRAM is locked before any boot options are processed.
  295. //
  296. { FALSE, TRUE, &gEfiEventReadyToBootGuid, SmmIplReadyToLockEventNotify, &gEfiEventReadyToBootGuid, TPL_CALLBACK, NULL },
  297. //
  298. // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform
  299. // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core
  300. // must guarantee that it does not access any UEFI related structures outside of SMRAM.
  301. // It is also to inform the SMM Core to notify SMM driver that system enter legacy boot.
  302. //
  303. { FALSE, FALSE, &gEfiEventLegacyBootGuid, SmmIplGuidedEventNotify, &gEfiEventLegacyBootGuid, TPL_CALLBACK, NULL },
  304. //
  305. // Declare event notification on Exit Boot Services Event Group. This is used to inform the SMM Core
  306. // to notify SMM driver that system enter exit boot services.
  307. //
  308. { FALSE, FALSE, &gEfiEventExitBootServicesGuid, SmmIplGuidedEventNotify, &gEfiEventExitBootServicesGuid, TPL_CALLBACK, NULL },
  309. //
  310. // Declare event notification on Ready To Boot Event Group. This is used to inform the SMM Core
  311. // to notify SMM driver that system enter ready to boot.
  312. //
  313. { FALSE, FALSE, &gEfiEventReadyToBootGuid, SmmIplGuidedEventNotify, &gEfiEventReadyToBootGuid, TPL_CALLBACK, NULL },
  314. //
  315. // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate
  316. // and mSmmControl2 from physical addresses to virtual addresses.
  317. //
  318. { FALSE, FALSE, &gEfiEventVirtualAddressChangeGuid, SmmIplSetVirtualAddressNotify, NULL, TPL_CALLBACK, NULL },
  319. //
  320. // Terminate the table of event notifications
  321. //
  322. { FALSE, FALSE, NULL, NULL, NULL, TPL_CALLBACK, NULL }
  323. };
  324. /**
  325. Find the maximum SMRAM cache range that covers the range specified by SmramRange.
  326. This function searches and joins all adjacent ranges of SmramRange into a range to be cached.
  327. @param SmramRange The SMRAM range to search from.
  328. @param SmramCacheBase The returned cache range base.
  329. @param SmramCacheSize The returned cache range size.
  330. **/
  331. VOID
  332. GetSmramCacheRange (
  333. IN EFI_SMRAM_DESCRIPTOR *SmramRange,
  334. OUT EFI_PHYSICAL_ADDRESS *SmramCacheBase,
  335. OUT UINT64 *SmramCacheSize
  336. )
  337. {
  338. UINTN Index;
  339. EFI_PHYSICAL_ADDRESS RangeCpuStart;
  340. UINT64 RangePhysicalSize;
  341. BOOLEAN FoundAjacentRange;
  342. *SmramCacheBase = SmramRange->CpuStart;
  343. *SmramCacheSize = SmramRange->PhysicalSize;
  344. do {
  345. FoundAjacentRange = FALSE;
  346. for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index++) {
  347. RangeCpuStart = gSmmCorePrivate->SmramRanges[Index].CpuStart;
  348. RangePhysicalSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;
  349. if ((RangeCpuStart < *SmramCacheBase) && (*SmramCacheBase == (RangeCpuStart + RangePhysicalSize))) {
  350. *SmramCacheBase = RangeCpuStart;
  351. *SmramCacheSize += RangePhysicalSize;
  352. FoundAjacentRange = TRUE;
  353. } else if (((*SmramCacheBase + *SmramCacheSize) == RangeCpuStart) && (RangePhysicalSize > 0)) {
  354. *SmramCacheSize += RangePhysicalSize;
  355. FoundAjacentRange = TRUE;
  356. }
  357. }
  358. } while (FoundAjacentRange);
  359. }
  360. /**
  361. Indicate whether the driver is currently executing in the SMM Initialization phase.
  362. @param This The EFI_SMM_BASE2_PROTOCOL instance.
  363. @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
  364. inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
  365. @retval EFI_INVALID_PARAMETER InSmram was NULL.
  366. @retval EFI_SUCCESS The call returned successfully.
  367. **/
  368. EFI_STATUS
  369. EFIAPI
  370. SmmBase2InSmram (
  371. IN CONST EFI_SMM_BASE2_PROTOCOL *This,
  372. OUT BOOLEAN *InSmram
  373. )
  374. {
  375. if (InSmram == NULL) {
  376. return EFI_INVALID_PARAMETER;
  377. }
  378. *InSmram = gSmmCorePrivate->InSmm;
  379. return EFI_SUCCESS;
  380. }
  381. /**
  382. Retrieves the location of the System Management System Table (SMST).
  383. @param This The EFI_SMM_BASE2_PROTOCOL instance.
  384. @param Smst On return, points to a pointer to the System Management Service Table (SMST).
  385. @retval EFI_INVALID_PARAMETER Smst or This was invalid.
  386. @retval EFI_SUCCESS The memory was returned to the system.
  387. @retval EFI_UNSUPPORTED Not in SMM.
  388. **/
  389. EFI_STATUS
  390. EFIAPI
  391. SmmBase2GetSmstLocation (
  392. IN CONST EFI_SMM_BASE2_PROTOCOL *This,
  393. OUT EFI_SMM_SYSTEM_TABLE2 **Smst
  394. )
  395. {
  396. if ((This == NULL) || (Smst == NULL)) {
  397. return EFI_INVALID_PARAMETER;
  398. }
  399. if (!gSmmCorePrivate->InSmm) {
  400. return EFI_UNSUPPORTED;
  401. }
  402. *Smst = gSmmCorePrivate->Smst;
  403. return EFI_SUCCESS;
  404. }
  405. /**
  406. Communicates with a registered handler.
  407. This function provides a service to send and receive messages from a registered
  408. UEFI service. This function is part of the SMM Communication Protocol that may
  409. be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
  410. after SetVirtualAddressMap().
  411. @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
  412. @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
  413. @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data
  414. being returned. Zero if the handler does not wish to reply with any data.
  415. This parameter is optional and may be NULL.
  416. @retval EFI_SUCCESS The message was successfully posted.
  417. @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
  418. @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
  419. If this error is returned, the MessageLength field
  420. in the CommBuffer header or the integer pointed by
  421. CommSize, are updated to reflect the maximum payload
  422. size the implementation can accommodate.
  423. @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
  424. if not omitted, are in address range that cannot be
  425. accessed by the MM environment.
  426. **/
  427. EFI_STATUS
  428. EFIAPI
  429. SmmCommunicationCommunicate (
  430. IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,
  431. IN OUT VOID *CommBuffer,
  432. IN OUT UINTN *CommSize OPTIONAL
  433. )
  434. {
  435. EFI_STATUS Status;
  436. EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;
  437. BOOLEAN OldInSmm;
  438. UINTN TempCommSize;
  439. //
  440. // Check parameters
  441. //
  442. if (CommBuffer == NULL) {
  443. return EFI_INVALID_PARAMETER;
  444. }
  445. CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer;
  446. if (CommSize == NULL) {
  447. TempCommSize = OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + CommunicateHeader->MessageLength;
  448. } else {
  449. TempCommSize = *CommSize;
  450. //
  451. // CommSize must hold HeaderGuid and MessageLength
  452. //
  453. if (TempCommSize < OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)) {
  454. return EFI_INVALID_PARAMETER;
  455. }
  456. }
  457. //
  458. // If not already in SMM, then generate a Software SMI
  459. //
  460. if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) {
  461. //
  462. // Put arguments for Software SMI in gSmmCorePrivate
  463. //
  464. gSmmCorePrivate->CommunicationBuffer = CommBuffer;
  465. gSmmCorePrivate->BufferSize = TempCommSize;
  466. //
  467. // Generate Software SMI
  468. //
  469. Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0);
  470. if (EFI_ERROR (Status)) {
  471. return EFI_UNSUPPORTED;
  472. }
  473. //
  474. // Return status from software SMI
  475. //
  476. if (CommSize != NULL) {
  477. *CommSize = gSmmCorePrivate->BufferSize;
  478. }
  479. return gSmmCorePrivate->ReturnStatus;
  480. }
  481. //
  482. // If we are in SMM, then the execution mode must be physical, which means that
  483. // OS established virtual addresses can not be used. If SetVirtualAddressMap()
  484. // has been called, then a direct invocation of the Software SMI is not allowed,
  485. // so return EFI_INVALID_PARAMETER.
  486. //
  487. if (EfiGoneVirtual ()) {
  488. return EFI_INVALID_PARAMETER;
  489. }
  490. //
  491. // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.
  492. //
  493. if ((!gSmmCorePrivate->InSmm) && (!mSmmAccess->OpenState || mSmmAccess->LockState)) {
  494. return EFI_INVALID_PARAMETER;
  495. }
  496. //
  497. // Save current InSmm state and set InSmm state to TRUE
  498. //
  499. OldInSmm = gSmmCorePrivate->InSmm;
  500. gSmmCorePrivate->InSmm = TRUE;
  501. //
  502. // Before SetVirtualAddressMap(), we are in SMM or SMRAM is open and unlocked, call SmiManage() directly.
  503. //
  504. TempCommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
  505. Status = gSmmCorePrivate->Smst->SmiManage (
  506. &CommunicateHeader->HeaderGuid,
  507. NULL,
  508. CommunicateHeader->Data,
  509. &TempCommSize
  510. );
  511. TempCommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
  512. if (CommSize != NULL) {
  513. *CommSize = TempCommSize;
  514. }
  515. //
  516. // Restore original InSmm state
  517. //
  518. gSmmCorePrivate->InSmm = OldInSmm;
  519. return (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
  520. }
  521. /**
  522. Communicates with a registered handler.
  523. This function provides a service to send and receive messages from a registered UEFI service.
  524. @param[in] This The EFI_MM_COMMUNICATION_PROTOCOL instance.
  525. @param[in] CommBufferPhysical Physical address of the MM communication buffer
  526. @param[in] CommBufferVirtual Virtual address of the MM communication buffer
  527. @param[in] CommSize The size of the data buffer being passed in. On exit, the size of data
  528. being returned. Zero if the handler does not wish to reply with any data.
  529. This parameter is optional and may be NULL.
  530. @retval EFI_SUCCESS The message was successfully posted.
  531. @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
  532. @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
  533. If this error is returned, the MessageLength field
  534. in the CommBuffer header or the integer pointed by
  535. CommSize, are updated to reflect the maximum payload
  536. size the implementation can accommodate.
  537. @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
  538. if not omitted, are in address range that cannot be
  539. accessed by the MM environment.
  540. **/
  541. EFI_STATUS
  542. EFIAPI
  543. SmmCommunicationMmCommunicate2 (
  544. IN CONST EFI_MM_COMMUNICATION2_PROTOCOL *This,
  545. IN OUT VOID *CommBufferPhysical,
  546. IN OUT VOID *CommBufferVirtual,
  547. IN OUT UINTN *CommSize OPTIONAL
  548. )
  549. {
  550. return SmmCommunicationCommunicate (
  551. &mSmmCommunication,
  552. CommBufferPhysical,
  553. CommSize
  554. );
  555. }
  556. /**
  557. Event notification that is fired when GUIDed Event Group is signaled.
  558. @param Event The Event that is being processed, not used.
  559. @param Context Event Context, not used.
  560. **/
  561. VOID
  562. EFIAPI
  563. SmmIplGuidedEventNotify (
  564. IN EFI_EVENT Event,
  565. IN VOID *Context
  566. )
  567. {
  568. UINTN Size;
  569. //
  570. // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
  571. //
  572. CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);
  573. mCommunicateHeader.MessageLength = 1;
  574. mCommunicateHeader.Data[0] = 0;
  575. //
  576. // Generate the Software SMI and return the result
  577. //
  578. Size = sizeof (mCommunicateHeader);
  579. SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);
  580. }
  581. /**
  582. Event notification that is fired when EndOfDxe Event Group is signaled.
  583. @param Event The Event that is being processed, not used.
  584. @param Context Event Context, not used.
  585. **/
  586. VOID
  587. EFIAPI
  588. SmmIplEndOfDxeEventNotify (
  589. IN EFI_EVENT Event,
  590. IN VOID *Context
  591. )
  592. {
  593. mEndOfDxe = TRUE;
  594. }
  595. /**
  596. Event notification that is fired when DxeDispatch Event Group is signaled.
  597. @param Event The Event that is being processed, not used.
  598. @param Context Event Context, not used.
  599. **/
  600. VOID
  601. EFIAPI
  602. SmmIplDxeDispatchEventNotify (
  603. IN EFI_EVENT Event,
  604. IN VOID *Context
  605. )
  606. {
  607. UINTN Size;
  608. EFI_STATUS Status;
  609. //
  610. // Keep calling the SMM Core Dispatcher until there is no request to restart it.
  611. //
  612. while (TRUE) {
  613. //
  614. // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
  615. // Clear the buffer passed into the Software SMI. This buffer will return
  616. // the status of the SMM Core Dispatcher.
  617. //
  618. CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);
  619. mCommunicateHeader.MessageLength = 1;
  620. mCommunicateHeader.Data[0] = 0;
  621. //
  622. // Generate the Software SMI and return the result
  623. //
  624. Size = sizeof (mCommunicateHeader);
  625. SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);
  626. //
  627. // Return if there is no request to restart the SMM Core Dispatcher
  628. //
  629. if (mCommunicateHeader.Data[0] != COMM_BUFFER_SMM_DISPATCH_RESTART) {
  630. return;
  631. }
  632. //
  633. // Close all SMRAM ranges to protect SMRAM
  634. // NOTE: SMRR is enabled by CPU SMM driver by calling SmmCpuFeaturesInitializeProcessor() from SmmCpuFeaturesLib
  635. // so no need to reset the SMRAM to UC in MTRR.
  636. //
  637. Status = mSmmAccess->Close (mSmmAccess);
  638. ASSERT_EFI_ERROR (Status);
  639. //
  640. // Print debug message that the SMRAM window is now closed.
  641. //
  642. DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
  643. }
  644. }
  645. /**
  646. Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
  647. @param Event The Event that is being processed, not used.
  648. @param Context Event Context, not used.
  649. **/
  650. VOID
  651. EFIAPI
  652. SmmIplSmmConfigurationEventNotify (
  653. IN EFI_EVENT Event,
  654. IN VOID *Context
  655. )
  656. {
  657. EFI_STATUS Status;
  658. EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
  659. //
  660. // Make sure this notification is for this handler
  661. //
  662. Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);
  663. if (EFI_ERROR (Status)) {
  664. return;
  665. }
  666. //
  667. // Register the SMM Entry Point provided by the SMM Core with the SMM Configuration protocol
  668. //
  669. Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);
  670. ASSERT_EFI_ERROR (Status);
  671. //
  672. // Set flag to indicate that the SMM Entry Point has been registered which
  673. // means that SMIs are now fully operational.
  674. //
  675. gSmmCorePrivate->SmmEntryPointRegistered = TRUE;
  676. //
  677. // Print debug message showing SMM Core entry point address.
  678. //
  679. DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));
  680. }
  681. /**
  682. Event notification that is fired every time a DxeSmmReadyToLock protocol is added
  683. or if gEfiEventReadyToBootGuid is signaled.
  684. @param Event The Event that is being processed, not used.
  685. @param Context Event Context, not used.
  686. **/
  687. VOID
  688. EFIAPI
  689. SmmIplReadyToLockEventNotify (
  690. IN EFI_EVENT Event,
  691. IN VOID *Context
  692. )
  693. {
  694. EFI_STATUS Status;
  695. VOID *Interface;
  696. UINTN Index;
  697. //
  698. // See if we are already locked
  699. //
  700. if (mSmmLocked) {
  701. return;
  702. }
  703. //
  704. // Make sure this notification is for this handler
  705. //
  706. if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {
  707. Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);
  708. if (EFI_ERROR (Status)) {
  709. return;
  710. }
  711. } else {
  712. //
  713. // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
  714. // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
  715. // Print a warning on debug builds.
  716. //
  717. DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
  718. }
  719. if (!mEndOfDxe) {
  720. DEBUG ((DEBUG_ERROR, "EndOfDxe Event must be signaled before DxeSmmReadyToLock Protocol installation!\n"));
  721. REPORT_STATUS_CODE (
  722. EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED,
  723. (EFI_SOFTWARE_SMM_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)
  724. );
  725. ASSERT (FALSE);
  726. }
  727. //
  728. // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
  729. //
  730. mSmmAccess->Lock (mSmmAccess);
  731. //
  732. // Close protocol and event notification events that do not apply after the
  733. // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
  734. // event has been signalled.
  735. //
  736. for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
  737. if (mSmmIplEvents[Index].CloseOnLock) {
  738. gBS->CloseEvent (mSmmIplEvents[Index].Event);
  739. }
  740. }
  741. //
  742. // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
  743. //
  744. SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);
  745. //
  746. // Print debug message that the SMRAM window is now locked.
  747. //
  748. DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));
  749. //
  750. // Set flag so this operation will not be performed again
  751. //
  752. mSmmLocked = TRUE;
  753. }
  754. /**
  755. Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
  756. This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
  757. It convers pointer to new virtual address.
  758. @param Event Event whose notification function is being invoked.
  759. @param Context Pointer to the notification function's context.
  760. **/
  761. VOID
  762. EFIAPI
  763. SmmIplSetVirtualAddressNotify (
  764. IN EFI_EVENT Event,
  765. IN VOID *Context
  766. )
  767. {
  768. EfiConvertPointer (0x0, (VOID **)&mSmmControl2);
  769. }
  770. /**
  771. Get the fixed loading address from image header assigned by build tool. This function only be called
  772. when Loading module at Fixed address feature enabled.
  773. @param ImageContext Pointer to the image context structure that describes the PE/COFF
  774. image that needs to be examined by this function.
  775. @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
  776. @retval EFI_NOT_FOUND The image has no assigned fixed loading address.
  777. **/
  778. EFI_STATUS
  779. GetPeCoffImageFixLoadingAssignedAddress (
  780. IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
  781. )
  782. {
  783. UINTN SectionHeaderOffset;
  784. EFI_STATUS Status;
  785. EFI_IMAGE_SECTION_HEADER SectionHeader;
  786. EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
  787. EFI_PHYSICAL_ADDRESS FixLoadingAddress;
  788. UINT16 Index;
  789. UINTN Size;
  790. UINT16 NumberOfSections;
  791. EFI_PHYSICAL_ADDRESS SmramBase;
  792. UINT64 SmmCodeSize;
  793. UINT64 ValueInSectionHeader;
  794. //
  795. // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
  796. //
  797. SmmCodeSize = EFI_PAGES_TO_SIZE (PcdGet32 (PcdLoadFixAddressSmmCodePageNumber));
  798. FixLoadingAddress = 0;
  799. Status = EFI_NOT_FOUND;
  800. SmramBase = mLMFAConfigurationTable->SmramBase;
  801. //
  802. // Get PeHeader pointer
  803. //
  804. ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8 *)ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
  805. SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +
  806. sizeof (UINT32) +
  807. sizeof (EFI_IMAGE_FILE_HEADER) +
  808. ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;
  809. NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
  810. //
  811. // Get base address from the first section header that doesn't point to code section.
  812. //
  813. for (Index = 0; Index < NumberOfSections; Index++) {
  814. //
  815. // Read section header from file
  816. //
  817. Size = sizeof (EFI_IMAGE_SECTION_HEADER);
  818. Status = ImageContext->ImageRead (
  819. ImageContext->Handle,
  820. SectionHeaderOffset,
  821. &Size,
  822. &SectionHeader
  823. );
  824. if (EFI_ERROR (Status)) {
  825. return Status;
  826. }
  827. Status = EFI_NOT_FOUND;
  828. if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
  829. //
  830. // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
  831. // first section header that doesn't point to code section in image header. And there is an assumption that when the
  832. // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
  833. // fields should NOT be Zero, or else, these 2 fields should be set to Zero
  834. //
  835. ValueInSectionHeader = ReadUnaligned64 ((UINT64 *)&SectionHeader.PointerToRelocations);
  836. if (ValueInSectionHeader != 0) {
  837. //
  838. // Found first section header that doesn't point to code section in which build tool saves the
  839. // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
  840. //
  841. FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);
  842. if ((SmramBase + SmmCodeSize > FixLoadingAddress) && (SmramBase <= FixLoadingAddress)) {
  843. //
  844. // The assigned address is valid. Return the specified loading address
  845. //
  846. ImageContext->ImageAddress = FixLoadingAddress;
  847. Status = EFI_SUCCESS;
  848. }
  849. }
  850. break;
  851. }
  852. SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
  853. }
  854. DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress, Status));
  855. return Status;
  856. }
  857. /**
  858. Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
  859. @param[in, out] SmramRange Descriptor for the range of SMRAM to reload the
  860. currently executing image, the rang of SMRAM to
  861. hold SMM Core will be excluded.
  862. @param[in, out] SmramRangeSmmCore Descriptor for the range of SMRAM to hold SMM Core.
  863. @param[in] Context Context to pass into SMM Core
  864. @return EFI_STATUS
  865. **/
  866. EFI_STATUS
  867. ExecuteSmmCoreFromSmram (
  868. IN OUT EFI_SMRAM_DESCRIPTOR *SmramRange,
  869. IN OUT EFI_SMRAM_DESCRIPTOR *SmramRangeSmmCore,
  870. IN VOID *Context
  871. )
  872. {
  873. EFI_STATUS Status;
  874. VOID *SourceBuffer;
  875. UINTN SourceSize;
  876. PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
  877. UINTN PageCount;
  878. EFI_IMAGE_ENTRY_POINT EntryPoint;
  879. //
  880. // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
  881. //
  882. Status = GetSectionFromAnyFvByFileType (
  883. EFI_FV_FILETYPE_SMM_CORE,
  884. 0,
  885. EFI_SECTION_PE32,
  886. 0,
  887. &SourceBuffer,
  888. &SourceSize
  889. );
  890. if (EFI_ERROR (Status)) {
  891. return Status;
  892. }
  893. //
  894. // Initialize ImageContext
  895. //
  896. ImageContext.Handle = SourceBuffer;
  897. ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
  898. //
  899. // Get information about the image being loaded
  900. //
  901. Status = PeCoffLoaderGetImageInfo (&ImageContext);
  902. if (EFI_ERROR (Status)) {
  903. return Status;
  904. }
  905. //
  906. // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
  907. // the address assigned by build tool.
  908. //
  909. if (PcdGet64 (PcdLoadModuleAtFixAddressEnable) != 0) {
  910. //
  911. // Get the fixed loading address assigned by Build tool
  912. //
  913. Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);
  914. if (!EFI_ERROR (Status)) {
  915. //
  916. // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range
  917. //
  918. PageCount = 0;
  919. //
  920. // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.
  921. //
  922. gSmmCorePrivate->SmramRangeCount--;
  923. } else {
  924. DEBUG ((DEBUG_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
  925. //
  926. // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
  927. // specified by SmramRange
  928. //
  929. PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
  930. ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);
  931. ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));
  932. SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);
  933. SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;
  934. SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;
  935. SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;
  936. SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);
  937. //
  938. // Align buffer on section boundary
  939. //
  940. ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;
  941. }
  942. } else {
  943. //
  944. // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
  945. // specified by SmramRange
  946. //
  947. PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
  948. ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);
  949. ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));
  950. SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);
  951. SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;
  952. SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;
  953. SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;
  954. SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);
  955. //
  956. // Align buffer on section boundary
  957. //
  958. ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;
  959. }
  960. ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
  961. ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);
  962. //
  963. // Print debug message showing SMM Core load address.
  964. //
  965. DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));
  966. //
  967. // Load the image to our new buffer
  968. //
  969. Status = PeCoffLoaderLoadImage (&ImageContext);
  970. if (!EFI_ERROR (Status)) {
  971. //
  972. // Relocate the image in our new buffer
  973. //
  974. Status = PeCoffLoaderRelocateImage (&ImageContext);
  975. if (!EFI_ERROR (Status)) {
  976. //
  977. // Flush the instruction cache so the image data are written before we execute it
  978. //
  979. InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
  980. //
  981. // Print debug message showing SMM Core entry point address.
  982. //
  983. DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));
  984. gSmmCorePrivate->PiSmmCoreImageBase = ImageContext.ImageAddress;
  985. gSmmCorePrivate->PiSmmCoreImageSize = ImageContext.ImageSize;
  986. DEBUG ((DEBUG_INFO, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageBase));
  987. DEBUG ((DEBUG_INFO, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageSize));
  988. gSmmCorePrivate->PiSmmCoreEntryPoint = ImageContext.EntryPoint;
  989. //
  990. // Execute image
  991. //
  992. EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;
  993. Status = EntryPoint ((EFI_HANDLE)Context, gST);
  994. }
  995. }
  996. //
  997. // Always free memory allocated by GetFileBufferByFilePath ()
  998. //
  999. FreePool (SourceBuffer);
  1000. return Status;
  1001. }
  1002. /**
  1003. SMM split SMRAM entry.
  1004. @param[in, out] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
  1005. @param[in, out] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
  1006. @param[out] Ranges Output pointer to hold split EFI_SMRAM_DESCRIPTOR entry.
  1007. @param[in, out] RangeCount Pointer to range count.
  1008. @param[out] ReservedRanges Output pointer to hold split EFI_SMM_RESERVED_SMRAM_REGION entry.
  1009. @param[in, out] ReservedRangeCount Pointer to reserved range count.
  1010. @param[out] FinalRanges Output pointer to hold split final EFI_SMRAM_DESCRIPTOR entry
  1011. that no need to be split anymore.
  1012. @param[in, out] FinalRangeCount Pointer to final range count.
  1013. **/
  1014. VOID
  1015. SmmSplitSmramEntry (
  1016. IN OUT EFI_SMRAM_DESCRIPTOR *RangeToCompare,
  1017. IN OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare,
  1018. OUT EFI_SMRAM_DESCRIPTOR *Ranges,
  1019. IN OUT UINTN *RangeCount,
  1020. OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRanges,
  1021. IN OUT UINTN *ReservedRangeCount,
  1022. OUT EFI_SMRAM_DESCRIPTOR *FinalRanges,
  1023. IN OUT UINTN *FinalRangeCount
  1024. )
  1025. {
  1026. UINT64 RangeToCompareEnd;
  1027. UINT64 ReservedRangeToCompareEnd;
  1028. RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;
  1029. ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;
  1030. if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&
  1031. (RangeToCompare->CpuStart < ReservedRangeToCompareEnd))
  1032. {
  1033. if (RangeToCompareEnd < ReservedRangeToCompareEnd) {
  1034. //
  1035. // RangeToCompare ReservedRangeToCompare
  1036. // ---- ---- --------------------------------------
  1037. // | | | | -> 1. ReservedRangeToCompare
  1038. // ---- | | |--| --------------------------------------
  1039. // | | | | | |
  1040. // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
  1041. // | | | | | | RangeToCompare->PhysicalSize = 0
  1042. // ---- | | |--| --------------------------------------
  1043. // | | | | -> 3. ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount
  1044. // ---- ---- --------------------------------------
  1045. //
  1046. //
  1047. // 1. Update ReservedRangeToCompare.
  1048. //
  1049. ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;
  1050. //
  1051. // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
  1052. // Zero RangeToCompare->PhysicalSize.
  1053. //
  1054. FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;
  1055. FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;
  1056. FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
  1057. FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompare->PhysicalSize;
  1058. *FinalRangeCount += 1;
  1059. RangeToCompare->PhysicalSize = 0;
  1060. //
  1061. // 3. Update ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount.
  1062. //
  1063. ReservedRanges[*ReservedRangeCount].SmramReservedStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;
  1064. ReservedRanges[*ReservedRangeCount].SmramReservedSize = ReservedRangeToCompareEnd - RangeToCompareEnd;
  1065. *ReservedRangeCount += 1;
  1066. } else {
  1067. //
  1068. // RangeToCompare ReservedRangeToCompare
  1069. // ---- ---- --------------------------------------
  1070. // | | | | -> 1. ReservedRangeToCompare
  1071. // ---- | | |--| --------------------------------------
  1072. // | | | | | |
  1073. // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
  1074. // | | | | | |
  1075. // | | ---- |--| --------------------------------------
  1076. // | | | | -> 3. RangeToCompare
  1077. // ---- ---- --------------------------------------
  1078. //
  1079. //
  1080. // 1. Update ReservedRangeToCompare.
  1081. //
  1082. ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;
  1083. //
  1084. // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
  1085. //
  1086. FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;
  1087. FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;
  1088. FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
  1089. FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompareEnd - RangeToCompare->CpuStart;
  1090. *FinalRangeCount += 1;
  1091. //
  1092. // 3. Update RangeToCompare.
  1093. //
  1094. RangeToCompare->CpuStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;
  1095. RangeToCompare->PhysicalStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;
  1096. RangeToCompare->PhysicalSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;
  1097. }
  1098. } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&
  1099. (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd))
  1100. {
  1101. if (ReservedRangeToCompareEnd < RangeToCompareEnd) {
  1102. //
  1103. // RangeToCompare ReservedRangeToCompare
  1104. // ---- ---- --------------------------------------
  1105. // | | | | -> 1. RangeToCompare
  1106. // | | ---- |--| --------------------------------------
  1107. // | | | | | |
  1108. // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
  1109. // | | | | | | ReservedRangeToCompare->SmramReservedSize = 0
  1110. // | | ---- |--| --------------------------------------
  1111. // | | | | -> 3. Ranges[*RangeCount] and increment *RangeCount
  1112. // ---- ---- --------------------------------------
  1113. //
  1114. //
  1115. // 1. Update RangeToCompare.
  1116. //
  1117. RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;
  1118. //
  1119. // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
  1120. // ReservedRangeToCompare->SmramReservedSize = 0
  1121. //
  1122. FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;
  1123. FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;
  1124. FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
  1125. FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompare->SmramReservedSize;
  1126. *FinalRangeCount += 1;
  1127. ReservedRangeToCompare->SmramReservedSize = 0;
  1128. //
  1129. // 3. Update Ranges[*RangeCount] and increment *RangeCount.
  1130. //
  1131. Ranges[*RangeCount].CpuStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;
  1132. Ranges[*RangeCount].PhysicalStart = FinalRanges[*FinalRangeCount - 1].PhysicalStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;
  1133. Ranges[*RangeCount].RegionState = RangeToCompare->RegionState;
  1134. Ranges[*RangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompareEnd;
  1135. *RangeCount += 1;
  1136. } else {
  1137. //
  1138. // RangeToCompare ReservedRangeToCompare
  1139. // ---- ---- --------------------------------------
  1140. // | | | | -> 1. RangeToCompare
  1141. // | | ---- |--| --------------------------------------
  1142. // | | | | | |
  1143. // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
  1144. // | | | | | |
  1145. // ---- | | |--| --------------------------------------
  1146. // | | | | -> 3. ReservedRangeToCompare
  1147. // ---- ---- --------------------------------------
  1148. //
  1149. //
  1150. // 1. Update RangeToCompare.
  1151. //
  1152. RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;
  1153. //
  1154. // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
  1155. // ReservedRangeToCompare->SmramReservedSize = 0
  1156. //
  1157. FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;
  1158. FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;
  1159. FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
  1160. FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompare->SmramReservedStart;
  1161. *FinalRangeCount += 1;
  1162. //
  1163. // 3. Update ReservedRangeToCompare.
  1164. //
  1165. ReservedRangeToCompare->SmramReservedStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;
  1166. ReservedRangeToCompare->SmramReservedSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;
  1167. }
  1168. }
  1169. }
  1170. /**
  1171. Returns if SMRAM range and SMRAM reserved range are overlapped.
  1172. @param[in] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
  1173. @param[in] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
  1174. @retval TRUE There is overlap.
  1175. @retval TRUE Math error.
  1176. @retval FALSE There is no overlap.
  1177. **/
  1178. BOOLEAN
  1179. SmmIsSmramOverlap (
  1180. IN EFI_SMRAM_DESCRIPTOR *RangeToCompare,
  1181. IN EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare
  1182. )
  1183. {
  1184. UINT64 RangeToCompareEnd;
  1185. UINT64 ReservedRangeToCompareEnd;
  1186. BOOLEAN IsOverUnderflow1;
  1187. BOOLEAN IsOverUnderflow2;
  1188. // Check for over or underflow.
  1189. IsOverUnderflow1 = EFI_ERROR (
  1190. SafeUint64Add (
  1191. (UINT64)RangeToCompare->CpuStart,
  1192. RangeToCompare->PhysicalSize,
  1193. &RangeToCompareEnd
  1194. )
  1195. );
  1196. IsOverUnderflow2 = EFI_ERROR (
  1197. SafeUint64Add (
  1198. (UINT64)ReservedRangeToCompare->SmramReservedStart,
  1199. ReservedRangeToCompare->SmramReservedSize,
  1200. &ReservedRangeToCompareEnd
  1201. )
  1202. );
  1203. if (IsOverUnderflow1 || IsOverUnderflow2) {
  1204. return TRUE;
  1205. }
  1206. if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&
  1207. (RangeToCompare->CpuStart < ReservedRangeToCompareEnd))
  1208. {
  1209. return TRUE;
  1210. } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&
  1211. (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd))
  1212. {
  1213. return TRUE;
  1214. }
  1215. return FALSE;
  1216. }
  1217. /**
  1218. Get full SMRAM ranges.
  1219. It will get SMRAM ranges from SmmAccess protocol and SMRAM reserved ranges from
  1220. SmmConfiguration protocol, split the entries if there is overlap between them.
  1221. It will also reserve one entry for SMM core.
  1222. @param[out] FullSmramRangeCount Output pointer to full SMRAM range count.
  1223. @return Pointer to full SMRAM ranges.
  1224. **/
  1225. EFI_SMRAM_DESCRIPTOR *
  1226. GetFullSmramRanges (
  1227. OUT UINTN *FullSmramRangeCount
  1228. )
  1229. {
  1230. EFI_STATUS Status;
  1231. EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
  1232. UINTN Size;
  1233. UINTN Index;
  1234. UINTN Index2;
  1235. EFI_SMRAM_DESCRIPTOR *FullSmramRanges;
  1236. UINTN TempSmramRangeCount;
  1237. UINTN AdditionSmramRangeCount;
  1238. EFI_SMRAM_DESCRIPTOR *TempSmramRanges;
  1239. UINTN SmramRangeCount;
  1240. EFI_SMRAM_DESCRIPTOR *SmramRanges;
  1241. UINTN SmramReservedCount;
  1242. EFI_SMM_RESERVED_SMRAM_REGION *SmramReservedRanges;
  1243. UINTN MaxCount;
  1244. BOOLEAN Rescan;
  1245. //
  1246. // Get SMM Configuration Protocol if it is present.
  1247. //
  1248. SmmConfiguration = NULL;
  1249. Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **)&SmmConfiguration);
  1250. //
  1251. // Get SMRAM information.
  1252. //
  1253. Size = 0;
  1254. Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);
  1255. ASSERT (Status == EFI_BUFFER_TOO_SMALL);
  1256. SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
  1257. //
  1258. // Get SMRAM reserved region count.
  1259. //
  1260. SmramReservedCount = 0;
  1261. if (SmmConfiguration != NULL) {
  1262. while (SmmConfiguration->SmramReservedRegions[SmramReservedCount].SmramReservedSize != 0) {
  1263. SmramReservedCount++;
  1264. }
  1265. }
  1266. //
  1267. // Reserve one entry for SMM Core in the full SMRAM ranges.
  1268. //
  1269. AdditionSmramRangeCount = 1;
  1270. if (PcdGet64 (PcdLoadModuleAtFixAddressEnable) != 0) {
  1271. //
  1272. // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.
  1273. //
  1274. AdditionSmramRangeCount = 2;
  1275. }
  1276. if (SmramReservedCount == 0) {
  1277. //
  1278. // No reserved SMRAM entry from SMM Configuration Protocol.
  1279. //
  1280. *FullSmramRangeCount = SmramRangeCount + AdditionSmramRangeCount;
  1281. Size = (*FullSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR);
  1282. FullSmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocateZeroPool (Size);
  1283. ASSERT (FullSmramRanges != NULL);
  1284. Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, FullSmramRanges);
  1285. ASSERT_EFI_ERROR (Status);
  1286. return FullSmramRanges;
  1287. }
  1288. //
  1289. // Why MaxCount = X + 2 * Y?
  1290. // Take Y = 1 as example below, Y > 1 case is just the iteration of Y = 1.
  1291. //
  1292. // X = 1 Y = 1 MaxCount = 3 = 1 + 2 * 1
  1293. // ---- ----
  1294. // | | ---- |--|
  1295. // | | | | -> | |
  1296. // | | ---- |--|
  1297. // ---- ----
  1298. //
  1299. // X = 2 Y = 1 MaxCount = 4 = 2 + 2 * 1
  1300. // ---- ----
  1301. // | | | |
  1302. // | | ---- |--|
  1303. // | | | | | |
  1304. // |--| | | -> |--|
  1305. // | | | | | |
  1306. // | | ---- |--|
  1307. // | | | |
  1308. // ---- ----
  1309. //
  1310. // X = 3 Y = 1 MaxCount = 5 = 3 + 2 * 1
  1311. // ---- ----
  1312. // | | | |
  1313. // | | ---- |--|
  1314. // |--| | | |--|
  1315. // | | | | -> | |
  1316. // |--| | | |--|
  1317. // | | ---- |--|
  1318. // | | | |
  1319. // ---- ----
  1320. //
  1321. // ......
  1322. //
  1323. MaxCount = SmramRangeCount + 2 * SmramReservedCount;
  1324. Size = MaxCount * sizeof (EFI_SMM_RESERVED_SMRAM_REGION);
  1325. SmramReservedRanges = (EFI_SMM_RESERVED_SMRAM_REGION *)AllocatePool (Size);
  1326. ASSERT (SmramReservedRanges != NULL);
  1327. for (Index = 0; Index < SmramReservedCount; Index++) {
  1328. CopyMem (&SmramReservedRanges[Index], &SmmConfiguration->SmramReservedRegions[Index], sizeof (EFI_SMM_RESERVED_SMRAM_REGION));
  1329. }
  1330. Size = MaxCount * sizeof (EFI_SMRAM_DESCRIPTOR);
  1331. TempSmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);
  1332. ASSERT (TempSmramRanges != NULL);
  1333. TempSmramRangeCount = 0;
  1334. SmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);
  1335. ASSERT (SmramRanges != NULL);
  1336. Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, SmramRanges);
  1337. ASSERT_EFI_ERROR (Status);
  1338. do {
  1339. Rescan = FALSE;
  1340. for (Index = 0; (Index < SmramRangeCount) && !Rescan; Index++) {
  1341. //
  1342. // Skip zero size entry.
  1343. //
  1344. if (SmramRanges[Index].PhysicalSize != 0) {
  1345. for (Index2 = 0; (Index2 < SmramReservedCount) && !Rescan; Index2++) {
  1346. //
  1347. // Skip zero size entry.
  1348. //
  1349. if (SmramReservedRanges[Index2].SmramReservedSize != 0) {
  1350. if (SmmIsSmramOverlap (
  1351. &SmramRanges[Index],
  1352. &SmramReservedRanges[Index2]
  1353. ))
  1354. {
  1355. //
  1356. // There is overlap, need to split entry and then rescan.
  1357. //
  1358. SmmSplitSmramEntry (
  1359. &SmramRanges[Index],
  1360. &SmramReservedRanges[Index2],
  1361. SmramRanges,
  1362. &SmramRangeCount,
  1363. SmramReservedRanges,
  1364. &SmramReservedCount,
  1365. TempSmramRanges,
  1366. &TempSmramRangeCount
  1367. );
  1368. Rescan = TRUE;
  1369. }
  1370. }
  1371. }
  1372. if (!Rescan) {
  1373. //
  1374. // No any overlap, copy the entry to the temp SMRAM ranges.
  1375. // Zero SmramRanges[Index].PhysicalSize = 0;
  1376. //
  1377. CopyMem (&TempSmramRanges[TempSmramRangeCount++], &SmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));
  1378. SmramRanges[Index].PhysicalSize = 0;
  1379. }
  1380. }
  1381. }
  1382. } while (Rescan);
  1383. ASSERT (TempSmramRangeCount <= MaxCount);
  1384. //
  1385. // Sort the entries
  1386. //
  1387. FullSmramRanges = AllocateZeroPool ((TempSmramRangeCount + AdditionSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR));
  1388. ASSERT (FullSmramRanges != NULL);
  1389. *FullSmramRangeCount = 0;
  1390. do {
  1391. for (Index = 0; Index < TempSmramRangeCount; Index++) {
  1392. if (TempSmramRanges[Index].PhysicalSize != 0) {
  1393. break;
  1394. }
  1395. }
  1396. ASSERT (Index < TempSmramRangeCount);
  1397. for (Index2 = 0; Index2 < TempSmramRangeCount; Index2++) {
  1398. if ((Index2 != Index) && (TempSmramRanges[Index2].PhysicalSize != 0) && (TempSmramRanges[Index2].CpuStart < TempSmramRanges[Index].CpuStart)) {
  1399. Index = Index2;
  1400. }
  1401. }
  1402. CopyMem (&FullSmramRanges[*FullSmramRangeCount], &TempSmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));
  1403. *FullSmramRangeCount += 1;
  1404. TempSmramRanges[Index].PhysicalSize = 0;
  1405. } while (*FullSmramRangeCount < TempSmramRangeCount);
  1406. ASSERT (*FullSmramRangeCount == TempSmramRangeCount);
  1407. *FullSmramRangeCount += AdditionSmramRangeCount;
  1408. FreePool (SmramRanges);
  1409. FreePool (SmramReservedRanges);
  1410. FreePool (TempSmramRanges);
  1411. return FullSmramRanges;
  1412. }
  1413. /**
  1414. The Entry Point for SMM IPL
  1415. Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
  1416. SMM Base 2 Protocol and SMM Communication Protocol, and register for the
  1417. critical events required to coordinate between DXE and SMM environments.
  1418. @param ImageHandle The firmware allocated handle for the EFI image.
  1419. @param SystemTable A pointer to the EFI System Table.
  1420. @retval EFI_SUCCESS The entry point is executed successfully.
  1421. @retval Other Some error occurred when executing this entry point.
  1422. **/
  1423. EFI_STATUS
  1424. EFIAPI
  1425. SmmIplEntry (
  1426. IN EFI_HANDLE ImageHandle,
  1427. IN EFI_SYSTEM_TABLE *SystemTable
  1428. )
  1429. {
  1430. EFI_STATUS Status;
  1431. UINTN Index;
  1432. UINT64 MaxSize;
  1433. VOID *Registration;
  1434. UINT64 SmmCodeSize;
  1435. EFI_CPU_ARCH_PROTOCOL *CpuArch;
  1436. EFI_STATUS SetAttrStatus;
  1437. EFI_SMRAM_DESCRIPTOR *SmramRangeSmmDriver;
  1438. EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc;
  1439. //
  1440. // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
  1441. // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
  1442. // by the SMM Core
  1443. //
  1444. mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;
  1445. //
  1446. // Get SMM Access Protocol
  1447. //
  1448. Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);
  1449. ASSERT_EFI_ERROR (Status);
  1450. //
  1451. // Get SMM Control2 Protocol
  1452. //
  1453. Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);
  1454. ASSERT_EFI_ERROR (Status);
  1455. gSmmCorePrivate->SmramRanges = GetFullSmramRanges (&gSmmCorePrivate->SmramRangeCount);
  1456. //
  1457. // Open all SMRAM ranges
  1458. //
  1459. Status = mSmmAccess->Open (mSmmAccess);
  1460. ASSERT_EFI_ERROR (Status);
  1461. //
  1462. // Print debug message that the SMRAM window is now open.
  1463. //
  1464. DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));
  1465. //
  1466. // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
  1467. //
  1468. mCurrentSmramRange = NULL;
  1469. for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {
  1470. //
  1471. // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization
  1472. //
  1473. if ((gSmmCorePrivate->SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {
  1474. continue;
  1475. }
  1476. if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {
  1477. if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize - 1) <= MAX_ADDRESS) {
  1478. if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {
  1479. MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;
  1480. mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];
  1481. }
  1482. }
  1483. }
  1484. }
  1485. if (mCurrentSmramRange != NULL) {
  1486. //
  1487. // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
  1488. //
  1489. DEBUG ((
  1490. DEBUG_INFO,
  1491. "SMM IPL found SMRAM window %p - %p\n",
  1492. (VOID *)(UINTN)mCurrentSmramRange->CpuStart,
  1493. (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)
  1494. ));
  1495. GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCacheSize);
  1496. //
  1497. // Make sure we can change the desired memory attributes.
  1498. //
  1499. Status = gDS->GetMemorySpaceDescriptor (
  1500. mSmramCacheBase,
  1501. &MemDesc
  1502. );
  1503. ASSERT_EFI_ERROR (Status);
  1504. if ((MemDesc.Capabilities & SMRAM_CAPABILITIES) != SMRAM_CAPABILITIES) {
  1505. gDS->SetMemorySpaceCapabilities (
  1506. mSmramCacheBase,
  1507. mSmramCacheSize,
  1508. MemDesc.Capabilities | SMRAM_CAPABILITIES
  1509. );
  1510. }
  1511. //
  1512. // If CPU AP is present, attempt to set SMRAM cacheability to WB and clear
  1513. // all paging attributes.
  1514. // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP
  1515. // is not available here.
  1516. //
  1517. CpuArch = NULL;
  1518. Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);
  1519. if (!EFI_ERROR (Status)) {
  1520. MemDesc.Attributes &= ~(EFI_CACHE_ATTRIBUTE_MASK | EFI_MEMORY_ATTRIBUTE_MASK);
  1521. MemDesc.Attributes |= EFI_MEMORY_WB;
  1522. Status = gDS->SetMemorySpaceAttributes (
  1523. mSmramCacheBase,
  1524. mSmramCacheSize,
  1525. MemDesc.Attributes
  1526. );
  1527. if (EFI_ERROR (Status)) {
  1528. DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
  1529. }
  1530. DEBUG_CODE (
  1531. gDS->GetMemorySpaceDescriptor (
  1532. mSmramCacheBase,
  1533. &MemDesc
  1534. );
  1535. DEBUG ((DEBUG_INFO, "SMRAM attributes: %016lx\n", MemDesc.Attributes));
  1536. ASSERT ((MemDesc.Attributes & EFI_MEMORY_ATTRIBUTE_MASK) == 0);
  1537. );
  1538. }
  1539. //
  1540. // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
  1541. // Modules At Fixed Address Configuration Table.
  1542. //
  1543. if (PcdGet64 (PcdLoadModuleAtFixAddressEnable) != 0) {
  1544. //
  1545. // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
  1546. //
  1547. SmmCodeSize = LShiftU64 (PcdGet32 (PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);
  1548. //
  1549. // The SMRAM available memory is assumed to be larger than SmmCodeSize
  1550. //
  1551. ASSERT (mCurrentSmramRange->PhysicalSize > SmmCodeSize);
  1552. //
  1553. // Retrieve Load modules At fixed address configuration table and save the SMRAM base.
  1554. //
  1555. Status = EfiGetSystemConfigurationTable (
  1556. &gLoadFixedAddressConfigurationTableGuid,
  1557. (VOID **)&mLMFAConfigurationTable
  1558. );
  1559. if (!EFI_ERROR (Status) && (mLMFAConfigurationTable != NULL)) {
  1560. mLMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;
  1561. //
  1562. // Print the SMRAM base
  1563. //
  1564. DEBUG ((DEBUG_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", mLMFAConfigurationTable->SmramBase));
  1565. }
  1566. //
  1567. // Fill the Smram range for all SMM code
  1568. //
  1569. SmramRangeSmmDriver = &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 2];
  1570. SmramRangeSmmDriver->CpuStart = mCurrentSmramRange->CpuStart;
  1571. SmramRangeSmmDriver->PhysicalStart = mCurrentSmramRange->PhysicalStart;
  1572. SmramRangeSmmDriver->RegionState = mCurrentSmramRange->RegionState | EFI_ALLOCATED;
  1573. SmramRangeSmmDriver->PhysicalSize = SmmCodeSize;
  1574. mCurrentSmramRange->PhysicalSize -= SmmCodeSize;
  1575. mCurrentSmramRange->CpuStart = mCurrentSmramRange->CpuStart + SmmCodeSize;
  1576. mCurrentSmramRange->PhysicalStart = mCurrentSmramRange->PhysicalStart + SmmCodeSize;
  1577. }
  1578. //
  1579. // Load SMM Core into SMRAM and execute it from SMRAM
  1580. //
  1581. Status = ExecuteSmmCoreFromSmram (
  1582. mCurrentSmramRange,
  1583. &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 1],
  1584. gSmmCorePrivate
  1585. );
  1586. if (EFI_ERROR (Status)) {
  1587. //
  1588. // Print error message that the SMM Core failed to be loaded and executed.
  1589. //
  1590. DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
  1591. //
  1592. // Attempt to reset SMRAM cacheability to UC
  1593. //
  1594. if (CpuArch != NULL) {
  1595. SetAttrStatus = gDS->SetMemorySpaceAttributes (
  1596. mSmramCacheBase,
  1597. mSmramCacheSize,
  1598. EFI_MEMORY_UC
  1599. );
  1600. if (EFI_ERROR (SetAttrStatus)) {
  1601. DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
  1602. }
  1603. }
  1604. }
  1605. } else {
  1606. //
  1607. // Print error message that there are not enough SMRAM resources to load the SMM Core.
  1608. //
  1609. DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
  1610. }
  1611. //
  1612. // If the SMM Core could not be loaded then close SMRAM window, free allocated
  1613. // resources, and return an error so SMM IPL will be unloaded.
  1614. //
  1615. if ((mCurrentSmramRange == NULL) || EFI_ERROR (Status)) {
  1616. //
  1617. // Close all SMRAM ranges
  1618. //
  1619. Status = mSmmAccess->Close (mSmmAccess);
  1620. ASSERT_EFI_ERROR (Status);
  1621. //
  1622. // Print debug message that the SMRAM window is now closed.
  1623. //
  1624. DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
  1625. //
  1626. // Free all allocated resources
  1627. //
  1628. FreePool (gSmmCorePrivate->SmramRanges);
  1629. return EFI_UNSUPPORTED;
  1630. }
  1631. //
  1632. // Install SMM Base2 Protocol and SMM Communication Protocol
  1633. //
  1634. Status = gBS->InstallMultipleProtocolInterfaces (
  1635. &mSmmIplHandle,
  1636. &gEfiSmmBase2ProtocolGuid,
  1637. &mSmmBase2,
  1638. &gEfiSmmCommunicationProtocolGuid,
  1639. &mSmmCommunication,
  1640. &gEfiMmCommunication2ProtocolGuid,
  1641. &mMmCommunication2,
  1642. NULL
  1643. );
  1644. ASSERT_EFI_ERROR (Status);
  1645. //
  1646. // Create the set of protocol and event notifications that the SMM IPL requires
  1647. //
  1648. for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
  1649. if (mSmmIplEvents[Index].Protocol) {
  1650. mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (
  1651. mSmmIplEvents[Index].Guid,
  1652. mSmmIplEvents[Index].NotifyTpl,
  1653. mSmmIplEvents[Index].NotifyFunction,
  1654. mSmmIplEvents[Index].NotifyContext,
  1655. &Registration
  1656. );
  1657. } else {
  1658. Status = gBS->CreateEventEx (
  1659. EVT_NOTIFY_SIGNAL,
  1660. mSmmIplEvents[Index].NotifyTpl,
  1661. mSmmIplEvents[Index].NotifyFunction,
  1662. mSmmIplEvents[Index].NotifyContext,
  1663. mSmmIplEvents[Index].Guid,
  1664. &mSmmIplEvents[Index].Event
  1665. );
  1666. ASSERT_EFI_ERROR (Status);
  1667. }
  1668. }
  1669. return EFI_SUCCESS;
  1670. }