BootScriptSave.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  1. /** @file
  2. Save the S3 data to S3 boot script.
  3. Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "InternalBootScriptLib.h"
  7. /**
  8. Data structure usage:
  9. +------------------------------+<------- PcdS3BootScriptTablePrivateDataPtr
  10. | SCRIPT_TABLE_PRIVATE_DATA | (mS3BootScriptTablePtr, Before SmmReadyToLock)
  11. | TableBase |--- PcdS3BootScriptTablePrivateSmmDataPtr
  12. | TableLength |--|-- (mS3BootScriptTablePtr = mS3BootScriptTableSmmPtr, After SmmReadyToLock InSmm)
  13. | TableMemoryPageNumber |--|-|----
  14. | AtRuntime | | | |
  15. | InSmm | | | |
  16. | BootTimeScriptLength |--|-|---|---
  17. | SmmLocked | | | | |
  18. | BackFromS3 | | | | |
  19. +------------------------------+ | | | |
  20. | | | |
  21. +------------------------------+<-- | | |
  22. | EFI_BOOT_SCRIPT_TABLE_HEADER | | | |
  23. | TableLength |----|-- | |
  24. +------------------------------+ | | | |
  25. | ...... | | | | |
  26. +------------------------------+<---- | | |
  27. | EFI_BOOT_SCRIPT_TERMINATE | | | |
  28. +------------------------------+<------ | |
  29. | |
  30. | |
  31. mBootScriptDataBootTimeGuid LockBox: | |
  32. Used to restore data after back from S3| |
  33. to handle potential INSERT boot script | |
  34. at runtime. | |
  35. +------------------------------+ | |
  36. | Boot Time Boot Script | | |
  37. | Before SmmReadyToLock | | |
  38. | | | |
  39. | | | |
  40. +------------------------------+ | |
  41. | Boot Time Boot Script | | |
  42. | After SmmReadyToLock InSmm | | |
  43. | | | |
  44. +------------------------------+<-------|--|
  45. | |
  46. | |
  47. mBootScriptDataGuid LockBox: (IN_PLACE) | |
  48. Used to restore data at S3 resume. | |
  49. +------------------------------+ | |
  50. | Boot Time Boot Script | | |
  51. | Before SmmReadyToLock | | |
  52. | | | |
  53. | | | |
  54. +------------------------------+ | |
  55. | Boot Time Boot Script | | |
  56. | After SmmReadyToLock InSmm | | |
  57. | | | |
  58. +------------------------------+<-------|---
  59. | Runtime Boot Script | |
  60. | After SmmReadyToLock InSmm | |
  61. +------------------------------+ |
  62. | ...... | |
  63. +------------------------------+<--------
  64. mBootScriptTableBaseGuid LockBox: (IN_PLACE)
  65. +------------------------------+
  66. | mS3BootScriptTablePtr-> |
  67. | TableBase |
  68. +------------------------------+
  69. mBootScriptSmmPrivateDataGuid LockBox: (IN_PLACE)
  70. SMM private data with BackFromS3 = TRUE
  71. at runtime. S3 will help restore it to
  72. tell the Library the system is back from S3.
  73. +------------------------------+
  74. | SCRIPT_TABLE_PRIVATE_DATA |
  75. | TableBase |
  76. | TableLength |
  77. | TableMemoryPageNumber |
  78. | AtRuntime |
  79. | InSmm |
  80. | BootTimeScriptLength |
  81. | SmmLocked |
  82. | BackFromS3 = TRUE |
  83. +------------------------------+
  84. **/
  85. SCRIPT_TABLE_PRIVATE_DATA *mS3BootScriptTablePtr;
  86. //
  87. // Allocate SMM copy because we can not use mS3BootScriptTablePtr after SmmReadyToLock in InSmm.
  88. //
  89. SCRIPT_TABLE_PRIVATE_DATA *mS3BootScriptTableSmmPtr;
  90. EFI_GUID mBootScriptDataGuid = {
  91. 0xaea6b965, 0xdcf5, 0x4311, { 0xb4, 0xb8, 0xf, 0x12, 0x46, 0x44, 0x94, 0xd2 }
  92. };
  93. EFI_GUID mBootScriptDataBootTimeGuid = {
  94. 0xb5af1d7a, 0xb8cf, 0x4eb3, { 0x89, 0x25, 0xa8, 0x20, 0xe1, 0x6b, 0x68, 0x7d }
  95. };
  96. EFI_GUID mBootScriptTableBaseGuid = {
  97. 0x1810ab4a, 0x2314, 0x4df6, { 0x81, 0xeb, 0x67, 0xc6, 0xec, 0x5, 0x85, 0x91 }
  98. };
  99. EFI_GUID mBootScriptSmmPrivateDataGuid = {
  100. 0x627ee2da, 0x3bf9, 0x439b, { 0x92, 0x9f, 0x2e, 0xe, 0x6e, 0x9d, 0xba, 0x62 }
  101. };
  102. EFI_EVENT mEventDxeSmmReadyToLock = NULL;
  103. VOID *mRegistrationSmmExitBootServices = NULL;
  104. VOID *mRegistrationSmmLegacyBoot = NULL;
  105. VOID *mRegistrationSmmReadyToLock = NULL;
  106. BOOLEAN mS3BootScriptTableAllocated = FALSE;
  107. BOOLEAN mS3BootScriptTableSmmAllocated = FALSE;
  108. EFI_SMM_SYSTEM_TABLE2 *mBootScriptSmst = NULL;
  109. BOOLEAN mS3BootScriptAcpiS3Enable = TRUE;
  110. /**
  111. This is an internal function to add a terminate node the entry, recalculate the table
  112. length and fill into the table.
  113. @return the base address of the boot script table.
  114. **/
  115. UINT8 *
  116. S3BootScriptInternalCloseTable (
  117. VOID
  118. )
  119. {
  120. UINT8 *S3TableBase;
  121. EFI_BOOT_SCRIPT_TERMINATE ScriptTerminate;
  122. EFI_BOOT_SCRIPT_TABLE_HEADER *ScriptTableInfo;
  123. S3TableBase = mS3BootScriptTablePtr->TableBase;
  124. if (S3TableBase == NULL) {
  125. //
  126. // the table is not exist
  127. //
  128. return S3TableBase;
  129. }
  130. //
  131. // Append the termination entry.
  132. //
  133. ScriptTerminate.OpCode = S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE;
  134. ScriptTerminate.Length = (UINT8)sizeof (EFI_BOOT_SCRIPT_TERMINATE);
  135. CopyMem (mS3BootScriptTablePtr->TableBase + mS3BootScriptTablePtr->TableLength, &ScriptTerminate, sizeof (EFI_BOOT_SCRIPT_TERMINATE));
  136. //
  137. // fill the table length
  138. //
  139. ScriptTableInfo = (EFI_BOOT_SCRIPT_TABLE_HEADER *)(mS3BootScriptTablePtr->TableBase);
  140. ScriptTableInfo->TableLength = mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE);
  141. return S3TableBase;
  142. //
  143. // NOTE: Here we did NOT adjust the mS3BootScriptTablePtr->TableLength to
  144. // mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE).
  145. // Because maybe after SmmReadyToLock, we still need add entries into the table,
  146. // and the entry should be added start before this TERMINATE node.
  147. //
  148. }
  149. /**
  150. This function save boot script data to LockBox.
  151. **/
  152. VOID
  153. SaveBootScriptDataToLockBox (
  154. VOID
  155. )
  156. {
  157. EFI_STATUS Status;
  158. //
  159. // Save whole memory copy into LockBox.
  160. // It will be used to restore data at S3 resume.
  161. //
  162. Status = SaveLockBox (
  163. &mBootScriptDataGuid,
  164. (VOID *)mS3BootScriptTablePtr->TableBase,
  165. EFI_PAGES_TO_SIZE (mS3BootScriptTablePtr->TableMemoryPageNumber)
  166. );
  167. ASSERT_EFI_ERROR (Status);
  168. Status = SetLockBoxAttributes (&mBootScriptDataGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);
  169. ASSERT_EFI_ERROR (Status);
  170. //
  171. // Just need save TableBase.
  172. // Do not update other field because they will NOT be used in S3.
  173. //
  174. Status = SaveLockBox (
  175. &mBootScriptTableBaseGuid,
  176. (VOID *)&mS3BootScriptTablePtr->TableBase,
  177. sizeof (mS3BootScriptTablePtr->TableBase)
  178. );
  179. ASSERT_EFI_ERROR (Status);
  180. Status = SetLockBoxAttributes (&mBootScriptTableBaseGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);
  181. ASSERT_EFI_ERROR (Status);
  182. }
  183. /**
  184. This is the Event call back function to notify the Library the system is entering
  185. SmmLocked phase.
  186. @param Event Pointer to this event
  187. @param Context Event handler private data
  188. **/
  189. VOID
  190. EFIAPI
  191. S3BootScriptEventCallBack (
  192. IN EFI_EVENT Event,
  193. IN VOID *Context
  194. )
  195. {
  196. EFI_STATUS Status;
  197. VOID *Interface;
  198. //
  199. // Try to locate it because EfiCreateProtocolNotifyEvent will trigger it once when registration.
  200. // Just return if it is not found.
  201. //
  202. Status = gBS->LocateProtocol (
  203. &gEfiDxeSmmReadyToLockProtocolGuid,
  204. NULL,
  205. &Interface
  206. );
  207. if (EFI_ERROR (Status)) {
  208. return;
  209. }
  210. //
  211. // Here we should tell the library that we are entering SmmLocked phase.
  212. // and the memory page number occupied by the table should not grow anymore.
  213. //
  214. if (!mS3BootScriptTablePtr->SmmLocked) {
  215. //
  216. // Before SmmReadyToLock, we need not write the terminate node when adding a node to boot scipt table
  217. // or else, that will impact the performance. However, after SmmReadyToLock, we should append terminate
  218. // node on every add to boot script table.
  219. //
  220. S3BootScriptInternalCloseTable ();
  221. mS3BootScriptTablePtr->SmmLocked = TRUE;
  222. //
  223. // Save BootScript data to lockbox
  224. //
  225. SaveBootScriptDataToLockBox ();
  226. }
  227. }
  228. /**
  229. This is the Event call back function is triggered in SMM to notify the Library
  230. the system is entering SmmLocked phase and set InSmm flag.
  231. @param Protocol Points to the protocol's unique identifier
  232. @param Interface Points to the interface instance
  233. @param Handle The handle on which the interface was installed
  234. @retval EFI_SUCCESS SmmEventCallback runs successfully
  235. **/
  236. EFI_STATUS
  237. EFIAPI
  238. S3BootScriptSmmEventCallBack (
  239. IN CONST EFI_GUID *Protocol,
  240. IN VOID *Interface,
  241. IN EFI_HANDLE Handle
  242. )
  243. {
  244. //
  245. // Check if it is already done
  246. //
  247. if (mS3BootScriptTablePtr == mS3BootScriptTableSmmPtr) {
  248. return EFI_SUCCESS;
  249. }
  250. //
  251. // Last chance to call-out, just make sure SmmLocked is set.
  252. //
  253. S3BootScriptEventCallBack (NULL, NULL);
  254. //
  255. // Save a SMM copy. If TableBase is NOT null, it means SMM copy has been ready, skip copy mem.
  256. //
  257. if (mS3BootScriptTableSmmPtr->TableBase == NULL) {
  258. CopyMem (mS3BootScriptTableSmmPtr, mS3BootScriptTablePtr, sizeof (*mS3BootScriptTablePtr));
  259. //
  260. // Set InSmm, we allow boot script update when InSmm, but not allow boot script outside SMM.
  261. // InSmm will only be checked if SmmLocked is TRUE.
  262. //
  263. mS3BootScriptTableSmmPtr->InSmm = TRUE;
  264. }
  265. //
  266. // We should not use ACPI Reserved copy, because it is not safe.
  267. //
  268. mS3BootScriptTablePtr = mS3BootScriptTableSmmPtr;
  269. return EFI_SUCCESS;
  270. }
  271. /**
  272. This function is to save boot time boot script data to LockBox.
  273. Because there may be INSERT boot script at runtime in SMM.
  274. The boot time copy will be used to restore data after back from S3.
  275. Otherwise the data inserted may cause some boot time boot script data lost
  276. if only BootScriptData used.
  277. **/
  278. VOID
  279. SaveBootTimeDataToLockBox (
  280. VOID
  281. )
  282. {
  283. EFI_STATUS Status;
  284. //
  285. // ACPI Reserved copy is not safe, restore from BootScriptData LockBox first,
  286. // and then save the data to BootScriptDataBootTime LockBox.
  287. //
  288. Status = RestoreLockBox (
  289. &mBootScriptDataGuid,
  290. NULL,
  291. NULL
  292. );
  293. ASSERT_EFI_ERROR (Status);
  294. //
  295. // Save BootScriptDataBootTime
  296. // It will be used to restore data after back from S3.
  297. //
  298. Status = SaveLockBox (
  299. &mBootScriptDataBootTimeGuid,
  300. (VOID *)mS3BootScriptTablePtr->TableBase,
  301. mS3BootScriptTablePtr->BootTimeScriptLength
  302. );
  303. ASSERT_EFI_ERROR (Status);
  304. }
  305. /**
  306. This function save boot script SMM private data to LockBox with BackFromS3 = TRUE at runtime.
  307. S3 resume will help restore it to tell the Library the system is back from S3.
  308. **/
  309. VOID
  310. SaveSmmPriviateDataToLockBoxAtRuntime (
  311. VOID
  312. )
  313. {
  314. EFI_STATUS Status;
  315. //
  316. // Save boot script SMM private data with BackFromS3 = TRUE.
  317. //
  318. mS3BootScriptTablePtr->BackFromS3 = TRUE;
  319. Status = SaveLockBox (
  320. &mBootScriptSmmPrivateDataGuid,
  321. (VOID *)mS3BootScriptTablePtr,
  322. sizeof (SCRIPT_TABLE_PRIVATE_DATA)
  323. );
  324. ASSERT_EFI_ERROR (Status);
  325. Status = SetLockBoxAttributes (&mBootScriptSmmPrivateDataGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);
  326. ASSERT_EFI_ERROR (Status);
  327. //
  328. // Set BackFromS3 flag back to FALSE to indicate that now is not back from S3.
  329. //
  330. mS3BootScriptTablePtr->BackFromS3 = FALSE;
  331. }
  332. /**
  333. This is the Event call back function is triggered in SMM to notify the Library
  334. the system is entering runtime phase.
  335. @param[in] Protocol Points to the protocol's unique identifier
  336. @param[in] Interface Points to the interface instance
  337. @param[in] Handle The handle on which the interface was installed
  338. @retval EFI_SUCCESS SmmAtRuntimeCallBack runs successfully
  339. **/
  340. EFI_STATUS
  341. EFIAPI
  342. S3BootScriptSmmAtRuntimeCallBack (
  343. IN CONST EFI_GUID *Protocol,
  344. IN VOID *Interface,
  345. IN EFI_HANDLE Handle
  346. )
  347. {
  348. if (!mS3BootScriptTablePtr->AtRuntime) {
  349. mS3BootScriptTablePtr->BootTimeScriptLength = (UINT32)(mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE));
  350. SaveBootTimeDataToLockBox ();
  351. mS3BootScriptTablePtr->AtRuntime = TRUE;
  352. SaveSmmPriviateDataToLockBoxAtRuntime ();
  353. }
  354. return EFI_SUCCESS;
  355. }
  356. /**
  357. Library Constructor.
  358. this function just identify it is a smm driver or non-smm driver linked against
  359. with the library
  360. @param ImageHandle The firmware allocated handle for the EFI image.
  361. @param SystemTable A pointer to the EFI System Table.
  362. @retval RETURN_SUCCESS The constructor always returns RETURN_SUCCESS.
  363. **/
  364. RETURN_STATUS
  365. EFIAPI
  366. S3BootScriptLibInitialize (
  367. IN EFI_HANDLE ImageHandle,
  368. IN EFI_SYSTEM_TABLE *SystemTable
  369. )
  370. {
  371. EFI_STATUS Status;
  372. SCRIPT_TABLE_PRIVATE_DATA *S3TablePtr;
  373. SCRIPT_TABLE_PRIVATE_DATA *S3TableSmmPtr;
  374. VOID *Registration;
  375. EFI_SMM_BASE2_PROTOCOL *SmmBase2;
  376. BOOLEAN InSmm;
  377. EFI_PHYSICAL_ADDRESS Buffer;
  378. if (!PcdGetBool (PcdAcpiS3Enable)) {
  379. mS3BootScriptAcpiS3Enable = FALSE;
  380. DEBUG ((DEBUG_INFO, "%a: Skip S3BootScript because ACPI S3 disabled.\n", gEfiCallerBaseName));
  381. return RETURN_SUCCESS;
  382. }
  383. S3TablePtr = (SCRIPT_TABLE_PRIVATE_DATA *)(UINTN)PcdGet64 (PcdS3BootScriptTablePrivateDataPtr);
  384. //
  385. // The Boot script private data is not be initialized. create it
  386. //
  387. if (S3TablePtr == 0) {
  388. Buffer = SIZE_4GB - 1;
  389. Status = gBS->AllocatePages (
  390. AllocateMaxAddress,
  391. EfiReservedMemoryType,
  392. EFI_SIZE_TO_PAGES (sizeof (SCRIPT_TABLE_PRIVATE_DATA)),
  393. &Buffer
  394. );
  395. ASSERT_EFI_ERROR (Status);
  396. mS3BootScriptTableAllocated = TRUE;
  397. S3TablePtr = (VOID *)(UINTN)Buffer;
  398. Status = PcdSet64S (PcdS3BootScriptTablePrivateDataPtr, (UINT64)(UINTN)S3TablePtr);
  399. ASSERT_EFI_ERROR (Status);
  400. ZeroMem (S3TablePtr, sizeof (SCRIPT_TABLE_PRIVATE_DATA));
  401. //
  402. // Create event to notify the library system enter the SmmLocked phase.
  403. //
  404. mEventDxeSmmReadyToLock = EfiCreateProtocolNotifyEvent (
  405. &gEfiDxeSmmReadyToLockProtocolGuid,
  406. TPL_CALLBACK,
  407. S3BootScriptEventCallBack,
  408. NULL,
  409. &Registration
  410. );
  411. ASSERT (mEventDxeSmmReadyToLock != NULL);
  412. }
  413. mS3BootScriptTablePtr = S3TablePtr;
  414. //
  415. // Get InSmm, we need to register SmmReadyToLock if this library is linked to SMM driver.
  416. //
  417. Status = gBS->LocateProtocol (&gEfiSmmBase2ProtocolGuid, NULL, (VOID **)&SmmBase2);
  418. if (EFI_ERROR (Status)) {
  419. return RETURN_SUCCESS;
  420. }
  421. Status = SmmBase2->InSmm (SmmBase2, &InSmm);
  422. if (EFI_ERROR (Status)) {
  423. return RETURN_SUCCESS;
  424. }
  425. if (!InSmm) {
  426. return RETURN_SUCCESS;
  427. }
  428. //
  429. // Good, we are in SMM
  430. //
  431. Status = SmmBase2->GetSmstLocation (SmmBase2, &mBootScriptSmst);
  432. if (EFI_ERROR (Status)) {
  433. return RETURN_SUCCESS;
  434. }
  435. S3TableSmmPtr = (SCRIPT_TABLE_PRIVATE_DATA *)(UINTN)PcdGet64 (PcdS3BootScriptTablePrivateSmmDataPtr);
  436. //
  437. // The Boot script private data in SMM is not be initialized. create it
  438. //
  439. if (S3TableSmmPtr == 0) {
  440. Status = mBootScriptSmst->SmmAllocatePool (
  441. EfiRuntimeServicesData,
  442. sizeof (SCRIPT_TABLE_PRIVATE_DATA),
  443. (VOID **)&S3TableSmmPtr
  444. );
  445. ASSERT_EFI_ERROR (Status);
  446. mS3BootScriptTableSmmAllocated = TRUE;
  447. Status = PcdSet64S (PcdS3BootScriptTablePrivateSmmDataPtr, (UINT64)(UINTN)S3TableSmmPtr);
  448. ASSERT_EFI_ERROR (Status);
  449. ZeroMem (S3TableSmmPtr, sizeof (SCRIPT_TABLE_PRIVATE_DATA));
  450. //
  451. // Register SmmExitBootServices and SmmLegacyBoot notification.
  452. //
  453. Status = mBootScriptSmst->SmmRegisterProtocolNotify (
  454. &gEdkiiSmmExitBootServicesProtocolGuid,
  455. S3BootScriptSmmAtRuntimeCallBack,
  456. &mRegistrationSmmExitBootServices
  457. );
  458. ASSERT_EFI_ERROR (Status);
  459. Status = mBootScriptSmst->SmmRegisterProtocolNotify (
  460. &gEdkiiSmmLegacyBootProtocolGuid,
  461. S3BootScriptSmmAtRuntimeCallBack,
  462. &mRegistrationSmmLegacyBoot
  463. );
  464. ASSERT_EFI_ERROR (Status);
  465. }
  466. mS3BootScriptTableSmmPtr = S3TableSmmPtr;
  467. //
  468. // Register SmmReadyToLock notification.
  469. //
  470. Status = mBootScriptSmst->SmmRegisterProtocolNotify (
  471. &gEfiSmmReadyToLockProtocolGuid,
  472. S3BootScriptSmmEventCallBack,
  473. &mRegistrationSmmReadyToLock
  474. );
  475. ASSERT_EFI_ERROR (Status);
  476. return RETURN_SUCCESS;
  477. }
  478. /**
  479. Library Destructor to free the resources allocated by
  480. S3BootScriptLibInitialize() and unregister callbacks.
  481. NOTICE: The destructor doesn't support unloading as a separate action, and it
  482. only supports unloading if the containing driver's entry point function fails.
  483. @param ImageHandle The firmware allocated handle for the EFI image.
  484. @param SystemTable A pointer to the EFI System Table.
  485. @retval RETURN_SUCCESS The destructor always returns RETURN_SUCCESS.
  486. **/
  487. RETURN_STATUS
  488. EFIAPI
  489. S3BootScriptLibDeinitialize (
  490. IN EFI_HANDLE ImageHandle,
  491. IN EFI_SYSTEM_TABLE *SystemTable
  492. )
  493. {
  494. EFI_STATUS Status;
  495. if (!mS3BootScriptAcpiS3Enable) {
  496. return RETURN_SUCCESS;
  497. }
  498. DEBUG ((DEBUG_INFO, "%a() in %a module\n", __FUNCTION__, gEfiCallerBaseName));
  499. if (mEventDxeSmmReadyToLock != NULL) {
  500. //
  501. // Close the DxeSmmReadyToLock event.
  502. //
  503. Status = gBS->CloseEvent (mEventDxeSmmReadyToLock);
  504. ASSERT_EFI_ERROR (Status);
  505. }
  506. if (mBootScriptSmst != NULL) {
  507. if (mRegistrationSmmExitBootServices != NULL) {
  508. //
  509. // Unregister SmmExitBootServices notification.
  510. //
  511. Status = mBootScriptSmst->SmmRegisterProtocolNotify (
  512. &gEdkiiSmmExitBootServicesProtocolGuid,
  513. NULL,
  514. &mRegistrationSmmExitBootServices
  515. );
  516. ASSERT_EFI_ERROR (Status);
  517. }
  518. if (mRegistrationSmmLegacyBoot != NULL) {
  519. //
  520. // Unregister SmmLegacyBoot notification.
  521. //
  522. Status = mBootScriptSmst->SmmRegisterProtocolNotify (
  523. &gEdkiiSmmLegacyBootProtocolGuid,
  524. NULL,
  525. &mRegistrationSmmLegacyBoot
  526. );
  527. ASSERT_EFI_ERROR (Status);
  528. }
  529. if (mRegistrationSmmReadyToLock != NULL) {
  530. //
  531. // Unregister SmmReadyToLock notification.
  532. //
  533. Status = mBootScriptSmst->SmmRegisterProtocolNotify (
  534. &gEfiSmmReadyToLockProtocolGuid,
  535. NULL,
  536. &mRegistrationSmmReadyToLock
  537. );
  538. ASSERT_EFI_ERROR (Status);
  539. }
  540. }
  541. //
  542. // Free the resources allocated and set PCDs to 0.
  543. //
  544. if (mS3BootScriptTableAllocated) {
  545. Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)mS3BootScriptTablePtr, EFI_SIZE_TO_PAGES (sizeof (SCRIPT_TABLE_PRIVATE_DATA)));
  546. ASSERT_EFI_ERROR (Status);
  547. Status = PcdSet64S (PcdS3BootScriptTablePrivateDataPtr, 0);
  548. ASSERT_EFI_ERROR (Status);
  549. }
  550. if ((mBootScriptSmst != NULL) && mS3BootScriptTableSmmAllocated) {
  551. Status = mBootScriptSmst->SmmFreePool (mS3BootScriptTableSmmPtr);
  552. ASSERT_EFI_ERROR (Status);
  553. Status = PcdSet64S (PcdS3BootScriptTablePrivateSmmDataPtr, 0);
  554. ASSERT_EFI_ERROR (Status);
  555. }
  556. return RETURN_SUCCESS;
  557. }
  558. /**
  559. To get the start address from which a new boot time s3 boot script entry will write into.
  560. If the table is not exist, the functio will first allocate a buffer for the table
  561. If the table buffer is not enough for the new entry, in non-smm mode, the funtion will
  562. invoke reallocate to enlarge buffer.
  563. @param EntryLength the new entry length.
  564. @retval the address from which the a new s3 boot script entry will write into
  565. **/
  566. UINT8 *
  567. S3BootScriptGetBootTimeEntryAddAddress (
  568. UINT8 EntryLength
  569. )
  570. {
  571. EFI_PHYSICAL_ADDRESS S3TableBase;
  572. EFI_PHYSICAL_ADDRESS NewS3TableBase;
  573. UINT8 *NewEntryPtr;
  574. UINT32 TableLength;
  575. UINT16 PageNumber;
  576. EFI_STATUS Status;
  577. EFI_BOOT_SCRIPT_TABLE_HEADER *ScriptTableInfo;
  578. S3TableBase = (EFI_PHYSICAL_ADDRESS)(UINTN)(mS3BootScriptTablePtr->TableBase);
  579. if (S3TableBase == 0) {
  580. //
  581. // The table is not exist. This is the first to add entry.
  582. // Allocate ACPI script table space under 4G memory.
  583. //
  584. S3TableBase = 0xffffffff;
  585. Status = gBS->AllocatePages (
  586. AllocateMaxAddress,
  587. EfiReservedMemoryType,
  588. 2 + PcdGet16 (PcdS3BootScriptRuntimeTableReservePageNumber),
  589. (EFI_PHYSICAL_ADDRESS *)&S3TableBase
  590. );
  591. if (EFI_ERROR (Status)) {
  592. ASSERT_EFI_ERROR (Status);
  593. return 0;
  594. }
  595. //
  596. // Fill Table Header
  597. //
  598. ScriptTableInfo = (EFI_BOOT_SCRIPT_TABLE_HEADER *)(UINTN)S3TableBase;
  599. ScriptTableInfo->OpCode = S3_BOOT_SCRIPT_LIB_TABLE_OPCODE;
  600. ScriptTableInfo->Length = (UINT8)sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);
  601. ScriptTableInfo->Version = BOOT_SCRIPT_TABLE_VERSION;
  602. ScriptTableInfo->TableLength = 0; // will be calculate at CloseTable
  603. mS3BootScriptTablePtr->TableLength = sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);
  604. mS3BootScriptTablePtr->TableBase = (UINT8 *)(UINTN)S3TableBase;
  605. mS3BootScriptTablePtr->TableMemoryPageNumber = (UINT16)(2 + PcdGet16 (PcdS3BootScriptRuntimeTableReservePageNumber));
  606. }
  607. // Here we do not count the reserved memory for runtime script table.
  608. PageNumber = (UINT16)(mS3BootScriptTablePtr->TableMemoryPageNumber - PcdGet16 (PcdS3BootScriptRuntimeTableReservePageNumber));
  609. TableLength = mS3BootScriptTablePtr->TableLength;
  610. if (EFI_PAGES_TO_SIZE ((UINTN)PageNumber) < (TableLength + EntryLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE))) {
  611. //
  612. // The buffer is too small to hold the table, Reallocate the buffer
  613. //
  614. NewS3TableBase = 0xffffffff;
  615. Status = gBS->AllocatePages (
  616. AllocateMaxAddress,
  617. EfiReservedMemoryType,
  618. 2 + PageNumber + PcdGet16 (PcdS3BootScriptRuntimeTableReservePageNumber),
  619. (EFI_PHYSICAL_ADDRESS *)&NewS3TableBase
  620. );
  621. if (EFI_ERROR (Status)) {
  622. ASSERT_EFI_ERROR (Status);
  623. return 0;
  624. }
  625. CopyMem ((VOID *)(UINTN)NewS3TableBase, (VOID *)(UINTN)S3TableBase, TableLength);
  626. gBS->FreePages (S3TableBase, mS3BootScriptTablePtr->TableMemoryPageNumber);
  627. mS3BootScriptTablePtr->TableBase = (UINT8 *)(UINTN)NewS3TableBase;
  628. mS3BootScriptTablePtr->TableMemoryPageNumber = (UINT16)(2 + PageNumber + PcdGet16 (PcdS3BootScriptRuntimeTableReservePageNumber));
  629. }
  630. //
  631. // calculate the the start address for the new entry.
  632. //
  633. NewEntryPtr = mS3BootScriptTablePtr->TableBase + TableLength;
  634. //
  635. // update the table lenghth
  636. //
  637. mS3BootScriptTablePtr->TableLength = TableLength + EntryLength;
  638. //
  639. // In the boot time, we will not append the termination entry to the boot script
  640. // table until the callers think there is no boot time data that should be added and
  641. // it is caller's responsibility to explicit call the CloseTable.
  642. //
  643. //
  644. return NewEntryPtr;
  645. }
  646. /**
  647. To get the start address from which a new runtime(after SmmReadyToLock) s3 boot script entry will write into.
  648. In this case, it should be ensured that there is enough buffer to hold the entry.
  649. @param EntryLength the new entry length.
  650. @retval the address from which the a new s3 runtime(after SmmReadyToLock) script entry will write into
  651. **/
  652. UINT8 *
  653. S3BootScriptGetRuntimeEntryAddAddress (
  654. UINT8 EntryLength
  655. )
  656. {
  657. UINT8 *NewEntryPtr;
  658. NewEntryPtr = NULL;
  659. //
  660. // Check if the memory range reserved for S3 Boot Script table is large enough to hold the node.
  661. //
  662. if ((mS3BootScriptTablePtr->TableLength + EntryLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE)) <= EFI_PAGES_TO_SIZE ((UINTN)(mS3BootScriptTablePtr->TableMemoryPageNumber))) {
  663. NewEntryPtr = mS3BootScriptTablePtr->TableBase + mS3BootScriptTablePtr->TableLength;
  664. mS3BootScriptTablePtr->TableLength = mS3BootScriptTablePtr->TableLength + EntryLength;
  665. //
  666. // Append a terminate node on every insert
  667. //
  668. S3BootScriptInternalCloseTable ();
  669. }
  670. return (UINT8 *)NewEntryPtr;
  671. }
  672. /**
  673. This function is to restore boot time boot script data from LockBox.
  674. **/
  675. VOID
  676. RestoreBootTimeDataFromLockBox (
  677. VOID
  678. )
  679. {
  680. EFI_STATUS Status;
  681. UINTN LockBoxLength;
  682. //
  683. // Restore boot time boot script data from LockBox.
  684. //
  685. LockBoxLength = mS3BootScriptTablePtr->BootTimeScriptLength;
  686. Status = RestoreLockBox (
  687. &mBootScriptDataBootTimeGuid,
  688. (VOID *)mS3BootScriptTablePtr->TableBase,
  689. &LockBoxLength
  690. );
  691. ASSERT_EFI_ERROR (Status);
  692. //
  693. // Update the data to BootScriptData LockBox.
  694. //
  695. Status = UpdateLockBox (
  696. &mBootScriptDataGuid,
  697. 0,
  698. (VOID *)mS3BootScriptTablePtr->TableBase,
  699. LockBoxLength
  700. );
  701. ASSERT_EFI_ERROR (Status);
  702. //
  703. // Update TableLength.
  704. //
  705. mS3BootScriptTablePtr->TableLength = (UINT32)(mS3BootScriptTablePtr->BootTimeScriptLength - sizeof (EFI_BOOT_SCRIPT_TERMINATE));
  706. }
  707. /**
  708. To get the start address from which a new s3 boot script entry will write into.
  709. @param EntryLength the new entry length.
  710. @retval the address from which the a new s3 boot script entry will write into
  711. **/
  712. UINT8 *
  713. S3BootScriptGetEntryAddAddress (
  714. UINT8 EntryLength
  715. )
  716. {
  717. UINT8 *NewEntryPtr;
  718. if (!mS3BootScriptAcpiS3Enable) {
  719. return NULL;
  720. }
  721. if (mS3BootScriptTablePtr->SmmLocked) {
  722. //
  723. // We need check InSmm, because after SmmReadyToLock, only SMM driver is allowed to write boot script.
  724. //
  725. if (!mS3BootScriptTablePtr->InSmm) {
  726. //
  727. // Add DEBUG ERROR, so that we can find it after SmmReadyToLock.
  728. // Do not use ASSERT, because we may have test to invoke this interface.
  729. //
  730. DEBUG ((DEBUG_ERROR, "FATAL ERROR: Set boot script outside SMM after SmmReadyToLock!!!\n"));
  731. return NULL;
  732. }
  733. if (mS3BootScriptTablePtr->BackFromS3) {
  734. //
  735. // Back from S3, restore boot time boot script data from LockBox
  736. // and set BackFromS3 flag back to FALSE.
  737. //
  738. RestoreBootTimeDataFromLockBox ();
  739. mS3BootScriptTablePtr->BackFromS3 = FALSE;
  740. }
  741. NewEntryPtr = S3BootScriptGetRuntimeEntryAddAddress (EntryLength);
  742. } else {
  743. NewEntryPtr = S3BootScriptGetBootTimeEntryAddAddress (EntryLength);
  744. }
  745. return NewEntryPtr;
  746. }
  747. /**
  748. Sync BootScript LockBox data.
  749. @param Script The address from where the boot script has been added or updated.
  750. **/
  751. VOID
  752. SyncBootScript (
  753. IN UINT8 *Script
  754. )
  755. {
  756. EFI_STATUS Status;
  757. UINT32 ScriptOffset;
  758. UINT32 TotalScriptLength;
  759. if (!mS3BootScriptTablePtr->SmmLocked || !mS3BootScriptTablePtr->InSmm) {
  760. //
  761. // If it is not after SmmReadyToLock in SMM,
  762. // just return.
  763. //
  764. return;
  765. }
  766. ScriptOffset = (UINT32)(Script - mS3BootScriptTablePtr->TableBase);
  767. TotalScriptLength = (UINT32)(mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE));
  768. //
  769. // Update BootScriptData
  770. // So in S3 resume, the data can be restored correctly.
  771. //
  772. Status = UpdateLockBox (
  773. &mBootScriptDataGuid,
  774. ScriptOffset,
  775. (VOID *)((UINTN)mS3BootScriptTablePtr->TableBase + ScriptOffset),
  776. TotalScriptLength - ScriptOffset
  777. );
  778. ASSERT_EFI_ERROR (Status);
  779. //
  780. // Now the length field is updated, need sync to lockbox.
  781. // So at S3 resume, the data can be restored correctly.
  782. //
  783. Status = UpdateLockBox (
  784. &mBootScriptDataGuid,
  785. OFFSET_OF (EFI_BOOT_SCRIPT_TABLE_HEADER, TableLength),
  786. &TotalScriptLength,
  787. sizeof (TotalScriptLength)
  788. );
  789. ASSERT_EFI_ERROR (Status);
  790. }
  791. /**
  792. This is an function to close the S3 boot script table. The function could only be called in
  793. BOOT time phase. To comply with the Framework spec definition on
  794. EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable(), this function will fulfill following things:
  795. 1. Closes the specified boot script table
  796. 2. It allocates a new memory pool to duplicate all the boot scripts in the specified table.
  797. Once this function is called, the table maintained by the library will be destroyed
  798. after it is copied into the allocated pool.
  799. 3. Any attempts to add a script record after calling this function will cause a new table
  800. to be created by the library.
  801. 4. The base address of the allocated pool will be returned in Address. Note that after
  802. using the boot script table, the CALLER is responsible for freeing the pool that is allocated
  803. by this function.
  804. In Spec PI1.1, this EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable() is retired. To provides this API for now is
  805. for Framework Spec compatibility.
  806. If anyone does call CloseTable() on a real platform, then the caller is responsible for figuring out
  807. how to get the script to run at S3 resume because the boot script maintained by the lib will be
  808. destroyed.
  809. @return the base address of the new copy of the boot script table.
  810. @note this function could only called in boot time phase
  811. **/
  812. UINT8 *
  813. EFIAPI
  814. S3BootScriptCloseTable (
  815. VOID
  816. )
  817. {
  818. UINT8 *S3TableBase;
  819. UINT32 TableLength;
  820. UINT8 *Buffer;
  821. EFI_STATUS Status;
  822. EFI_BOOT_SCRIPT_TABLE_HEADER *ScriptTableInfo;
  823. S3TableBase = mS3BootScriptTablePtr->TableBase;
  824. if (S3TableBase == 0) {
  825. return 0;
  826. }
  827. //
  828. // Append the termination record the S3 boot script table
  829. //
  830. S3BootScriptInternalCloseTable ();
  831. TableLength = mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE);
  832. //
  833. // Allocate the buffer and copy the boot script to the buffer.
  834. //
  835. Status = gBS->AllocatePool (
  836. EfiBootServicesData,
  837. (UINTN)TableLength,
  838. (VOID **)&Buffer
  839. );
  840. if (EFI_ERROR (Status)) {
  841. return 0;
  842. }
  843. CopyMem (Buffer, S3TableBase, TableLength);
  844. //
  845. // Destroy the table maintained by the library so that the next write operation
  846. // will write the record to the first entry of the table.
  847. //
  848. // Fill the table header.
  849. ScriptTableInfo = (EFI_BOOT_SCRIPT_TABLE_HEADER *)S3TableBase;
  850. ScriptTableInfo->OpCode = S3_BOOT_SCRIPT_LIB_TABLE_OPCODE;
  851. ScriptTableInfo->Length = (UINT8)sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);
  852. ScriptTableInfo->TableLength = 0; // will be calculate at close the table
  853. mS3BootScriptTablePtr->TableLength = sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);
  854. return Buffer;
  855. }
  856. /**
  857. Save I/O write to boot script
  858. @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
  859. @param Address The base address of the I/O operations.
  860. @param Count The number of I/O operations to perform.
  861. @param Buffer The source buffer from which to write data.
  862. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  863. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  864. **/
  865. RETURN_STATUS
  866. EFIAPI
  867. S3BootScriptSaveIoWrite (
  868. IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
  869. IN UINT64 Address,
  870. IN UINTN Count,
  871. IN VOID *Buffer
  872. )
  873. {
  874. UINT8 Length;
  875. UINT8 *Script;
  876. UINT8 WidthInByte;
  877. EFI_BOOT_SCRIPT_IO_WRITE ScriptIoWrite;
  878. if (!mS3BootScriptAcpiS3Enable) {
  879. return RETURN_SUCCESS;
  880. }
  881. WidthInByte = (UINT8)(0x01 << (Width & 0x03));
  882. //
  883. // Truncation check
  884. //
  885. if ((Count > MAX_UINT8) ||
  886. (WidthInByte * Count > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_IO_WRITE)))
  887. {
  888. return RETURN_OUT_OF_RESOURCES;
  889. }
  890. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_WRITE) + (WidthInByte * Count));
  891. Script = S3BootScriptGetEntryAddAddress (Length);
  892. if (Script == NULL) {
  893. return RETURN_OUT_OF_RESOURCES;
  894. }
  895. //
  896. // save script data
  897. //
  898. ScriptIoWrite.OpCode = EFI_BOOT_SCRIPT_IO_WRITE_OPCODE;
  899. ScriptIoWrite.Length = Length;
  900. ScriptIoWrite.Width = Width;
  901. ScriptIoWrite.Address = Address;
  902. ScriptIoWrite.Count = (UINT32)Count;
  903. CopyMem ((VOID *)Script, (VOID *)&ScriptIoWrite, sizeof (EFI_BOOT_SCRIPT_IO_WRITE));
  904. CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_IO_WRITE)), Buffer, WidthInByte * Count);
  905. SyncBootScript (Script);
  906. return RETURN_SUCCESS;
  907. }
  908. /**
  909. Adds a record for an I/O modify operation into a S3 boot script table
  910. @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
  911. @param Address The base address of the I/O operations.
  912. @param Data A pointer to the data to be OR-ed.
  913. @param DataMask A pointer to the data mask to be AND-ed with the data read from the register
  914. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  915. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  916. **/
  917. RETURN_STATUS
  918. EFIAPI
  919. S3BootScriptSaveIoReadWrite (
  920. IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
  921. IN UINT64 Address,
  922. IN VOID *Data,
  923. IN VOID *DataMask
  924. )
  925. {
  926. UINT8 Length;
  927. UINT8 *Script;
  928. UINT8 WidthInByte;
  929. EFI_BOOT_SCRIPT_IO_READ_WRITE ScriptIoReadWrite;
  930. if (!mS3BootScriptAcpiS3Enable) {
  931. return RETURN_SUCCESS;
  932. }
  933. WidthInByte = (UINT8)(0x01 << (Width & 0x03));
  934. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE) + (WidthInByte * 2));
  935. Script = S3BootScriptGetEntryAddAddress (Length);
  936. if (Script == NULL) {
  937. return RETURN_OUT_OF_RESOURCES;
  938. }
  939. //
  940. // Build script data
  941. //
  942. ScriptIoReadWrite.OpCode = EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE;
  943. ScriptIoReadWrite.Length = Length;
  944. ScriptIoReadWrite.Width = Width;
  945. ScriptIoReadWrite.Address = Address;
  946. CopyMem ((VOID *)Script, (VOID *)&ScriptIoReadWrite, sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE));
  947. CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE)), Data, WidthInByte);
  948. CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE) + WidthInByte), DataMask, WidthInByte);
  949. SyncBootScript (Script);
  950. return RETURN_SUCCESS;
  951. }
  952. /**
  953. Adds a record for a memory write operation into a specified boot script table.
  954. @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
  955. @param Address The base address of the memory operations
  956. @param Count The number of memory operations to perform.
  957. @param Buffer The source buffer from which to write the data.
  958. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  959. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  960. **/
  961. RETURN_STATUS
  962. EFIAPI
  963. S3BootScriptSaveMemWrite (
  964. IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
  965. IN UINT64 Address,
  966. IN UINTN Count,
  967. IN VOID *Buffer
  968. )
  969. {
  970. UINT8 Length;
  971. UINT8 *Script;
  972. UINT8 WidthInByte;
  973. EFI_BOOT_SCRIPT_MEM_WRITE ScriptMemWrite;
  974. if (!mS3BootScriptAcpiS3Enable) {
  975. return RETURN_SUCCESS;
  976. }
  977. WidthInByte = (UINT8)(0x01 << (Width & 0x03));
  978. //
  979. // Truncation check
  980. //
  981. if ((Count > MAX_UINT8) ||
  982. (WidthInByte * Count > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_MEM_WRITE)))
  983. {
  984. return RETURN_OUT_OF_RESOURCES;
  985. }
  986. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_WRITE) + (WidthInByte * Count));
  987. Script = S3BootScriptGetEntryAddAddress (Length);
  988. if (Script == NULL) {
  989. return RETURN_OUT_OF_RESOURCES;
  990. }
  991. //
  992. // Build script data
  993. //
  994. ScriptMemWrite.OpCode = EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE;
  995. ScriptMemWrite.Length = Length;
  996. ScriptMemWrite.Width = Width;
  997. ScriptMemWrite.Address = Address;
  998. ScriptMemWrite.Count = (UINT32)Count;
  999. CopyMem ((VOID *)Script, (VOID *)&ScriptMemWrite, sizeof (EFI_BOOT_SCRIPT_MEM_WRITE));
  1000. CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_WRITE)), Buffer, WidthInByte * Count);
  1001. SyncBootScript (Script);
  1002. return RETURN_SUCCESS;
  1003. }
  1004. /**
  1005. Adds a record for a memory modify operation into a specified boot script table.
  1006. @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
  1007. @param Address The base address of the memory operations. Address needs alignment if required
  1008. @param Data A pointer to the data to be OR-ed.
  1009. @param DataMask A pointer to the data mask to be AND-ed with the data read from the register.
  1010. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1011. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1012. **/
  1013. RETURN_STATUS
  1014. EFIAPI
  1015. S3BootScriptSaveMemReadWrite (
  1016. IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
  1017. IN UINT64 Address,
  1018. IN VOID *Data,
  1019. IN VOID *DataMask
  1020. )
  1021. {
  1022. UINT8 Length;
  1023. UINT8 *Script;
  1024. UINT8 WidthInByte;
  1025. EFI_BOOT_SCRIPT_MEM_READ_WRITE ScriptMemReadWrite;
  1026. if (!mS3BootScriptAcpiS3Enable) {
  1027. return RETURN_SUCCESS;
  1028. }
  1029. WidthInByte = (UINT8)(0x01 << (Width & 0x03));
  1030. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE) + (WidthInByte * 2));
  1031. Script = S3BootScriptGetEntryAddAddress (Length);
  1032. if (Script == NULL) {
  1033. return RETURN_OUT_OF_RESOURCES;
  1034. }
  1035. //
  1036. // Build script data
  1037. //
  1038. ScriptMemReadWrite.OpCode = EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE;
  1039. ScriptMemReadWrite.Length = Length;
  1040. ScriptMemReadWrite.Width = Width;
  1041. ScriptMemReadWrite.Address = Address;
  1042. CopyMem ((VOID *)Script, (VOID *)&ScriptMemReadWrite, sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE));
  1043. CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE)), Data, WidthInByte);
  1044. CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE) + WidthInByte), DataMask, WidthInByte);
  1045. SyncBootScript (Script);
  1046. return RETURN_SUCCESS;
  1047. }
  1048. /**
  1049. Adds a record for a PCI configuration space write operation into a specified boot script table.
  1050. @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
  1051. @param Address The address within the PCI configuration space.
  1052. @param Count The number of PCI operations to perform.
  1053. @param Buffer The source buffer from which to write the data.
  1054. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1055. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1056. @note A known Limitations in the implementation which is 64bits operations are not supported.
  1057. **/
  1058. RETURN_STATUS
  1059. EFIAPI
  1060. S3BootScriptSavePciCfgWrite (
  1061. IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
  1062. IN UINT64 Address,
  1063. IN UINTN Count,
  1064. IN VOID *Buffer
  1065. )
  1066. {
  1067. UINT8 Length;
  1068. UINT8 *Script;
  1069. UINT8 WidthInByte;
  1070. EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE ScriptPciWrite;
  1071. if (!mS3BootScriptAcpiS3Enable) {
  1072. return RETURN_SUCCESS;
  1073. }
  1074. if ((Width == S3BootScriptWidthUint64) ||
  1075. (Width == S3BootScriptWidthFifoUint64) ||
  1076. (Width == S3BootScriptWidthFillUint64))
  1077. {
  1078. return EFI_INVALID_PARAMETER;
  1079. }
  1080. WidthInByte = (UINT8)(0x01 << (Width & 0x03));
  1081. //
  1082. // Truncation check
  1083. //
  1084. if ((Count > MAX_UINT8) ||
  1085. (WidthInByte * Count > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE)))
  1086. {
  1087. return RETURN_OUT_OF_RESOURCES;
  1088. }
  1089. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE) + (WidthInByte * Count));
  1090. Script = S3BootScriptGetEntryAddAddress (Length);
  1091. if (Script == NULL) {
  1092. return RETURN_OUT_OF_RESOURCES;
  1093. }
  1094. //
  1095. // Build script data
  1096. //
  1097. ScriptPciWrite.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE_OPCODE;
  1098. ScriptPciWrite.Length = Length;
  1099. ScriptPciWrite.Width = Width;
  1100. ScriptPciWrite.Address = Address;
  1101. ScriptPciWrite.Count = (UINT32)Count;
  1102. CopyMem ((VOID *)Script, (VOID *)&ScriptPciWrite, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE));
  1103. CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE)), Buffer, WidthInByte * Count);
  1104. SyncBootScript (Script);
  1105. return RETURN_SUCCESS;
  1106. }
  1107. /**
  1108. Adds a record for a PCI configuration space modify operation into a specified boot script table.
  1109. @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
  1110. @param Address The address within the PCI configuration space.
  1111. @param Data A pointer to the data to be OR-ed.The size depends on Width.
  1112. @param DataMask A pointer to the data mask to be AND-ed.
  1113. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1114. @retval RETURN__SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1115. @note A known Limitations in the implementation which is 64bits operations are not supported.
  1116. **/
  1117. RETURN_STATUS
  1118. EFIAPI
  1119. S3BootScriptSavePciCfgReadWrite (
  1120. IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
  1121. IN UINT64 Address,
  1122. IN VOID *Data,
  1123. IN VOID *DataMask
  1124. )
  1125. {
  1126. UINT8 Length;
  1127. UINT8 *Script;
  1128. UINT8 WidthInByte;
  1129. EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE ScriptPciReadWrite;
  1130. if (!mS3BootScriptAcpiS3Enable) {
  1131. return RETURN_SUCCESS;
  1132. }
  1133. if ((Width == S3BootScriptWidthUint64) ||
  1134. (Width == S3BootScriptWidthFifoUint64) ||
  1135. (Width == S3BootScriptWidthFillUint64))
  1136. {
  1137. return EFI_INVALID_PARAMETER;
  1138. }
  1139. WidthInByte = (UINT8)(0x01 << (Width & 0x03));
  1140. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE) + (WidthInByte * 2));
  1141. Script = S3BootScriptGetEntryAddAddress (Length);
  1142. if (Script == NULL) {
  1143. return RETURN_OUT_OF_RESOURCES;
  1144. }
  1145. //
  1146. // Build script data
  1147. //
  1148. ScriptPciReadWrite.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE;
  1149. ScriptPciReadWrite.Length = Length;
  1150. ScriptPciReadWrite.Width = Width;
  1151. ScriptPciReadWrite.Address = Address;
  1152. CopyMem ((VOID *)Script, (VOID *)&ScriptPciReadWrite, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE));
  1153. CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE)), Data, WidthInByte);
  1154. CopyMem (
  1155. (VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE) + WidthInByte),
  1156. DataMask,
  1157. WidthInByte
  1158. );
  1159. SyncBootScript (Script);
  1160. return RETURN_SUCCESS;
  1161. }
  1162. /**
  1163. Adds a record for a PCI configuration 2 space write operation into a specified boot script table.
  1164. @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
  1165. @param Segment The PCI segment number for Address.
  1166. @param Address The address within the PCI configuration space.
  1167. @param Count The number of PCI operations to perform.
  1168. @param Buffer The source buffer from which to write the data.
  1169. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1170. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1171. @note A known Limitations in the implementation which is 64bits operations are not supported.
  1172. **/
  1173. RETURN_STATUS
  1174. EFIAPI
  1175. S3BootScriptSavePciCfg2Write (
  1176. IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
  1177. IN UINT16 Segment,
  1178. IN UINT64 Address,
  1179. IN UINTN Count,
  1180. IN VOID *Buffer
  1181. )
  1182. {
  1183. UINT8 Length;
  1184. UINT8 *Script;
  1185. UINT8 WidthInByte;
  1186. EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE ScriptPciWrite2;
  1187. if (!mS3BootScriptAcpiS3Enable) {
  1188. return RETURN_SUCCESS;
  1189. }
  1190. if ((Width == S3BootScriptWidthUint64) ||
  1191. (Width == S3BootScriptWidthFifoUint64) ||
  1192. (Width == S3BootScriptWidthFillUint64))
  1193. {
  1194. return EFI_INVALID_PARAMETER;
  1195. }
  1196. WidthInByte = (UINT8)(0x01 << (Width & 0x03));
  1197. //
  1198. // Truncation check
  1199. //
  1200. if ((Count > MAX_UINT8) ||
  1201. (WidthInByte * Count > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE)))
  1202. {
  1203. return RETURN_OUT_OF_RESOURCES;
  1204. }
  1205. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE) + (WidthInByte * Count));
  1206. Script = S3BootScriptGetEntryAddAddress (Length);
  1207. if (Script == NULL) {
  1208. return RETURN_OUT_OF_RESOURCES;
  1209. }
  1210. //
  1211. // Build script data
  1212. //
  1213. ScriptPciWrite2.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE;
  1214. ScriptPciWrite2.Length = Length;
  1215. ScriptPciWrite2.Width = Width;
  1216. ScriptPciWrite2.Address = Address;
  1217. ScriptPciWrite2.Segment = Segment;
  1218. ScriptPciWrite2.Count = (UINT32)Count;
  1219. CopyMem ((VOID *)Script, (VOID *)&ScriptPciWrite2, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE));
  1220. CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE)), Buffer, WidthInByte * Count);
  1221. SyncBootScript (Script);
  1222. return RETURN_SUCCESS;
  1223. }
  1224. /**
  1225. Adds a record for a PCI configuration 2 space modify operation into a specified boot script table.
  1226. @param Width The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.
  1227. @param Segment The PCI segment number for Address.
  1228. @param Address The address within the PCI configuration space.
  1229. @param Data A pointer to the data to be OR-ed. The size depends on Width.
  1230. @param DataMask A pointer to the data mask to be AND-ed.
  1231. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1232. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1233. @note A known Limitations in the implementation which is 64bits operations are not supported.
  1234. **/
  1235. RETURN_STATUS
  1236. EFIAPI
  1237. S3BootScriptSavePciCfg2ReadWrite (
  1238. IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
  1239. IN UINT16 Segment,
  1240. IN UINT64 Address,
  1241. IN VOID *Data,
  1242. IN VOID *DataMask
  1243. )
  1244. {
  1245. UINT8 Length;
  1246. UINT8 *Script;
  1247. UINT8 WidthInByte;
  1248. EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE ScriptPciReadWrite2;
  1249. if (!mS3BootScriptAcpiS3Enable) {
  1250. return RETURN_SUCCESS;
  1251. }
  1252. if ((Width == S3BootScriptWidthUint64) ||
  1253. (Width == S3BootScriptWidthFifoUint64) ||
  1254. (Width == S3BootScriptWidthFillUint64))
  1255. {
  1256. return EFI_INVALID_PARAMETER;
  1257. }
  1258. WidthInByte = (UINT8)(0x01 << (Width & 0x03));
  1259. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE) + (WidthInByte * 2));
  1260. Script = S3BootScriptGetEntryAddAddress (Length);
  1261. if (Script == NULL) {
  1262. return RETURN_OUT_OF_RESOURCES;
  1263. }
  1264. //
  1265. // Build script data
  1266. //
  1267. ScriptPciReadWrite2.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE;
  1268. ScriptPciReadWrite2.Length = Length;
  1269. ScriptPciReadWrite2.Width = Width;
  1270. ScriptPciReadWrite2.Segment = Segment;
  1271. ScriptPciReadWrite2.Address = Address;
  1272. CopyMem ((VOID *)Script, (VOID *)&ScriptPciReadWrite2, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE));
  1273. CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE)), Data, WidthInByte);
  1274. CopyMem (
  1275. (VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE) + WidthInByte),
  1276. DataMask,
  1277. WidthInByte
  1278. );
  1279. SyncBootScript (Script);
  1280. return RETURN_SUCCESS;
  1281. }
  1282. /**
  1283. Checks the parameter of S3BootScriptSaveSmbusExecute().
  1284. This function checks the input parameters of SmbusExecute(). If the input parameters are valid
  1285. for certain SMBus bus protocol, it will return EFI_SUCCESS; otherwise, it will return certain
  1286. error code based on the input SMBus bus protocol.
  1287. @param SmBusAddress Address that encodes the SMBUS Slave Address, SMBUS Command, SMBUS Data Length,
  1288. and PEC.
  1289. @param Operation Signifies which particular SMBus hardware protocol instance that
  1290. it will use to execute the SMBus transactions. This SMBus
  1291. hardware protocol is defined by the SMBus Specification and is
  1292. not related to EFI.
  1293. @param Length Signifies the number of bytes that this operation will do. The
  1294. maximum number of bytes can be revision specific and operation
  1295. specific. This field will contain the actual number of bytes that
  1296. are executed for this operation. Not all operations require this
  1297. argument.
  1298. @param Buffer Contains the value of data to execute to the SMBus slave device.
  1299. Not all operations require this argument. The length of this
  1300. buffer is identified by Length.
  1301. @retval EFI_SUCCESS All the parameters are valid for the corresponding SMBus bus
  1302. protocol.
  1303. @retval EFI_INVALID_PARAMETER Operation is not defined in EFI_SMBUS_OPERATION.
  1304. @retval EFI_INVALID_PARAMETER Length/Buffer is NULL for operations except for EfiSmbusQuickRead
  1305. and EfiSmbusQuickWrite. Length is outside the range of valid
  1306. values.
  1307. @retval EFI_UNSUPPORTED The SMBus operation or PEC is not supported.
  1308. @retval EFI_BUFFER_TOO_SMALL Buffer is not sufficient for this operation.
  1309. **/
  1310. EFI_STATUS
  1311. CheckParameters (
  1312. IN UINTN SmBusAddress,
  1313. IN EFI_SMBUS_OPERATION Operation,
  1314. IN OUT UINTN *Length,
  1315. IN VOID *Buffer
  1316. )
  1317. {
  1318. EFI_STATUS Status;
  1319. UINTN RequiredLen;
  1320. EFI_SMBUS_DEVICE_COMMAND Command;
  1321. BOOLEAN PecCheck;
  1322. Command = SMBUS_LIB_COMMAND (SmBusAddress);
  1323. PecCheck = SMBUS_LIB_PEC (SmBusAddress);
  1324. //
  1325. // Set default value to be 2:
  1326. // for SmbusReadWord, SmbusWriteWord and SmbusProcessCall.
  1327. //
  1328. RequiredLen = 2;
  1329. Status = EFI_SUCCESS;
  1330. switch (Operation) {
  1331. case EfiSmbusQuickRead:
  1332. case EfiSmbusQuickWrite:
  1333. if (PecCheck || (Command != 0)) {
  1334. return EFI_UNSUPPORTED;
  1335. }
  1336. break;
  1337. case EfiSmbusReceiveByte:
  1338. case EfiSmbusSendByte:
  1339. if (Command != 0) {
  1340. return EFI_UNSUPPORTED;
  1341. }
  1342. //
  1343. // Cascade to check length parameter.
  1344. //
  1345. case EfiSmbusReadByte:
  1346. case EfiSmbusWriteByte:
  1347. RequiredLen = 1;
  1348. //
  1349. // Cascade to check length parameter.
  1350. //
  1351. case EfiSmbusReadWord:
  1352. case EfiSmbusWriteWord:
  1353. case EfiSmbusProcessCall:
  1354. if ((Buffer == NULL) || (Length == NULL)) {
  1355. return EFI_INVALID_PARAMETER;
  1356. } else if (*Length < RequiredLen) {
  1357. Status = EFI_BUFFER_TOO_SMALL;
  1358. }
  1359. *Length = RequiredLen;
  1360. break;
  1361. case EfiSmbusReadBlock:
  1362. case EfiSmbusWriteBlock:
  1363. case EfiSmbusBWBRProcessCall:
  1364. if ((Buffer == NULL) ||
  1365. (Length == NULL) ||
  1366. (*Length < MIN_SMBUS_BLOCK_LEN) ||
  1367. (*Length > MAX_SMBUS_BLOCK_LEN))
  1368. {
  1369. return EFI_INVALID_PARAMETER;
  1370. }
  1371. break;
  1372. default:
  1373. return EFI_INVALID_PARAMETER;
  1374. }
  1375. return Status;
  1376. }
  1377. /**
  1378. Adds a record for an SMBus command execution into a specified boot script table.
  1379. @param SmBusAddress Address that encodes the SMBUS Slave Address, SMBUS Command, SMBUS Data Length, and PEC.
  1380. @param Operation Indicates which particular SMBus protocol it will use to execute the SMBus
  1381. transactions.
  1382. @param Length A pointer to signify the number of bytes that this operation will do.
  1383. @param Buffer Contains the value of data to execute to the SMBUS slave device.
  1384. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1385. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1386. **/
  1387. RETURN_STATUS
  1388. EFIAPI
  1389. S3BootScriptSaveSmbusExecute (
  1390. IN UINTN SmBusAddress,
  1391. IN EFI_SMBUS_OPERATION Operation,
  1392. IN UINTN *Length,
  1393. IN VOID *Buffer
  1394. )
  1395. {
  1396. EFI_STATUS Status;
  1397. UINTN BufferLength;
  1398. UINT8 DataSize;
  1399. UINT8 *Script;
  1400. EFI_BOOT_SCRIPT_SMBUS_EXECUTE ScriptSmbusExecute;
  1401. if (!mS3BootScriptAcpiS3Enable) {
  1402. return RETURN_SUCCESS;
  1403. }
  1404. if (Length == NULL) {
  1405. BufferLength = 0;
  1406. } else {
  1407. BufferLength = *Length;
  1408. }
  1409. Status = CheckParameters (SmBusAddress, Operation, &BufferLength, Buffer);
  1410. if (EFI_ERROR (Status)) {
  1411. return Status;
  1412. }
  1413. //
  1414. // Truncation check
  1415. //
  1416. if (BufferLength > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE)) {
  1417. return RETURN_OUT_OF_RESOURCES;
  1418. }
  1419. DataSize = (UINT8)(sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE) + BufferLength);
  1420. Script = S3BootScriptGetEntryAddAddress (DataSize);
  1421. if (Script == NULL) {
  1422. return RETURN_OUT_OF_RESOURCES;
  1423. }
  1424. //
  1425. // Build script data
  1426. //
  1427. ScriptSmbusExecute.OpCode = EFI_BOOT_SCRIPT_SMBUS_EXECUTE_OPCODE;
  1428. ScriptSmbusExecute.Length = DataSize;
  1429. ScriptSmbusExecute.SmBusAddress = (UINT64)SmBusAddress;
  1430. ScriptSmbusExecute.Operation = Operation;
  1431. ScriptSmbusExecute.DataSize = (UINT32)BufferLength;
  1432. CopyMem ((VOID *)Script, (VOID *)&ScriptSmbusExecute, sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE));
  1433. CopyMem (
  1434. (VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE)),
  1435. Buffer,
  1436. BufferLength
  1437. );
  1438. SyncBootScript (Script);
  1439. return RETURN_SUCCESS;
  1440. }
  1441. /**
  1442. Adds a record for an execution stall on the processor into a specified boot script table.
  1443. @param Duration Duration in microseconds of the stall
  1444. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1445. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1446. **/
  1447. RETURN_STATUS
  1448. EFIAPI
  1449. S3BootScriptSaveStall (
  1450. IN UINTN Duration
  1451. )
  1452. {
  1453. UINT8 Length;
  1454. UINT8 *Script;
  1455. EFI_BOOT_SCRIPT_STALL ScriptStall;
  1456. if (!mS3BootScriptAcpiS3Enable) {
  1457. return RETURN_SUCCESS;
  1458. }
  1459. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_STALL));
  1460. Script = S3BootScriptGetEntryAddAddress (Length);
  1461. if (Script == NULL) {
  1462. return RETURN_OUT_OF_RESOURCES;
  1463. }
  1464. //
  1465. // Build script data
  1466. //
  1467. ScriptStall.OpCode = EFI_BOOT_SCRIPT_STALL_OPCODE;
  1468. ScriptStall.Length = Length;
  1469. ScriptStall.Duration = Duration;
  1470. CopyMem ((VOID *)Script, (VOID *)&ScriptStall, sizeof (EFI_BOOT_SCRIPT_STALL));
  1471. SyncBootScript (Script);
  1472. return RETURN_SUCCESS;
  1473. }
  1474. /**
  1475. Adds a record for dispatching specified arbitrary code into a specified boot script table.
  1476. @param EntryPoint Entry point of the code to be dispatched.
  1477. @param Context Argument to be passed into the EntryPoint of the code to be dispatched.
  1478. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1479. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1480. **/
  1481. RETURN_STATUS
  1482. EFIAPI
  1483. S3BootScriptSaveDispatch2 (
  1484. IN VOID *EntryPoint,
  1485. IN VOID *Context
  1486. )
  1487. {
  1488. UINT8 Length;
  1489. UINT8 *Script;
  1490. EFI_BOOT_SCRIPT_DISPATCH_2 ScriptDispatch2;
  1491. if (!mS3BootScriptAcpiS3Enable) {
  1492. return RETURN_SUCCESS;
  1493. }
  1494. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_DISPATCH_2));
  1495. Script = S3BootScriptGetEntryAddAddress (Length);
  1496. if (Script == NULL) {
  1497. return RETURN_OUT_OF_RESOURCES;
  1498. }
  1499. //
  1500. // Build script data
  1501. //
  1502. ScriptDispatch2.OpCode = EFI_BOOT_SCRIPT_DISPATCH_2_OPCODE;
  1503. ScriptDispatch2.Length = Length;
  1504. ScriptDispatch2.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)EntryPoint;
  1505. ScriptDispatch2.Context = (EFI_PHYSICAL_ADDRESS)(UINTN)Context;
  1506. CopyMem ((VOID *)Script, (VOID *)&ScriptDispatch2, sizeof (EFI_BOOT_SCRIPT_DISPATCH_2));
  1507. SyncBootScript (Script);
  1508. return RETURN_SUCCESS;
  1509. }
  1510. /**
  1511. Adds a record for memory reads of the memory location and continues when the exit criteria is
  1512. satisfied or after a defined duration.
  1513. Please aware, below interface is different with PI specification, Vol 5:
  1514. EFI_S3_SAVE_STATE_PROTOCOL.Write() for EFI_BOOT_SCRIPT_MEM_POLL_OPCODE.
  1515. "Duration" below is microseconds, while "Delay" in PI specification means
  1516. the number of 100ns units to poll.
  1517. @param Width The width of the memory operations.
  1518. @param Address The base address of the memory operations.
  1519. @param BitMask A pointer to the bit mask to be AND-ed with the data read from the register.
  1520. @param BitValue A pointer to the data value after to be Masked.
  1521. @param Duration Duration in microseconds of the stall.
  1522. @param LoopTimes The times of the register polling.
  1523. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1524. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1525. **/
  1526. RETURN_STATUS
  1527. EFIAPI
  1528. S3BootScriptSaveMemPoll (
  1529. IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
  1530. IN UINT64 Address,
  1531. IN VOID *BitMask,
  1532. IN VOID *BitValue,
  1533. IN UINTN Duration,
  1534. IN UINT64 LoopTimes
  1535. )
  1536. {
  1537. UINT8 Length;
  1538. UINT8 *Script;
  1539. UINT8 WidthInByte;
  1540. EFI_BOOT_SCRIPT_MEM_POLL ScriptMemPoll;
  1541. if (!mS3BootScriptAcpiS3Enable) {
  1542. return RETURN_SUCCESS;
  1543. }
  1544. WidthInByte = (UINT8)(0x01 << (Width & 0x03));
  1545. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_POLL) + (WidthInByte * 2));
  1546. Script = S3BootScriptGetEntryAddAddress (Length);
  1547. if (Script == NULL) {
  1548. return RETURN_OUT_OF_RESOURCES;
  1549. }
  1550. //
  1551. // Build script data
  1552. //
  1553. ScriptMemPoll.OpCode = EFI_BOOT_SCRIPT_MEM_POLL_OPCODE;
  1554. ScriptMemPoll.Length = Length;
  1555. ScriptMemPoll.Width = Width;
  1556. ScriptMemPoll.Address = Address;
  1557. ScriptMemPoll.Duration = Duration;
  1558. ScriptMemPoll.LoopTimes = LoopTimes;
  1559. CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_POLL)), BitValue, WidthInByte);
  1560. CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_POLL) + WidthInByte), BitMask, WidthInByte);
  1561. CopyMem ((VOID *)Script, (VOID *)&ScriptMemPoll, sizeof (EFI_BOOT_SCRIPT_MEM_POLL));
  1562. SyncBootScript (Script);
  1563. return RETURN_SUCCESS;
  1564. }
  1565. /**
  1566. Store arbitrary information in the boot script table. This opcode is a no-op on dispatch and is only
  1567. used for debugging script issues.
  1568. @param InformationLength Length of the data in bytes
  1569. @param Information Information to be logged in the boot scrpit
  1570. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1571. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1572. **/
  1573. RETURN_STATUS
  1574. EFIAPI
  1575. S3BootScriptSaveInformation (
  1576. IN UINT32 InformationLength,
  1577. IN VOID *Information
  1578. )
  1579. {
  1580. UINT8 Length;
  1581. UINT8 *Script;
  1582. EFI_BOOT_SCRIPT_INFORMATION ScriptInformation;
  1583. if (!mS3BootScriptAcpiS3Enable) {
  1584. return RETURN_SUCCESS;
  1585. }
  1586. //
  1587. // Truncation check
  1588. //
  1589. if (InformationLength > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_INFORMATION)) {
  1590. return RETURN_OUT_OF_RESOURCES;
  1591. }
  1592. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION) + InformationLength);
  1593. Script = S3BootScriptGetEntryAddAddress (Length);
  1594. if (Script == NULL) {
  1595. return RETURN_OUT_OF_RESOURCES;
  1596. }
  1597. //
  1598. // Build script data
  1599. //
  1600. ScriptInformation.OpCode = EFI_BOOT_SCRIPT_INFORMATION_OPCODE;
  1601. ScriptInformation.Length = Length;
  1602. ScriptInformation.InformationLength = InformationLength;
  1603. CopyMem ((VOID *)Script, (VOID *)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));
  1604. CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION)), (VOID *)Information, (UINTN)InformationLength);
  1605. SyncBootScript (Script);
  1606. return RETURN_SUCCESS;
  1607. }
  1608. /**
  1609. Store a string in the boot script table. This opcode is a no-op on dispatch and is only
  1610. used for debugging script issues.
  1611. @param String The string to save to boot script table
  1612. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1613. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1614. **/
  1615. RETURN_STATUS
  1616. EFIAPI
  1617. S3BootScriptSaveInformationAsciiString (
  1618. IN CONST CHAR8 *String
  1619. )
  1620. {
  1621. return S3BootScriptSaveInformation (
  1622. (UINT32)AsciiStrLen (String) + 1,
  1623. (VOID *)String
  1624. );
  1625. }
  1626. /**
  1627. Adds a record for dispatching specified arbitrary code into a specified boot script table.
  1628. @param EntryPoint Entry point of the code to be dispatched.
  1629. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1630. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1631. **/
  1632. RETURN_STATUS
  1633. EFIAPI
  1634. S3BootScriptSaveDispatch (
  1635. IN VOID *EntryPoint
  1636. )
  1637. {
  1638. UINT8 Length;
  1639. UINT8 *Script;
  1640. EFI_BOOT_SCRIPT_DISPATCH ScriptDispatch;
  1641. if (!mS3BootScriptAcpiS3Enable) {
  1642. return RETURN_SUCCESS;
  1643. }
  1644. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_DISPATCH));
  1645. Script = S3BootScriptGetEntryAddAddress (Length);
  1646. if (Script == NULL) {
  1647. return RETURN_OUT_OF_RESOURCES;
  1648. }
  1649. //
  1650. // Build script data
  1651. //
  1652. ScriptDispatch.OpCode = EFI_BOOT_SCRIPT_DISPATCH_OPCODE;
  1653. ScriptDispatch.Length = Length;
  1654. ScriptDispatch.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)EntryPoint;
  1655. CopyMem ((VOID *)Script, (VOID *)&ScriptDispatch, sizeof (EFI_BOOT_SCRIPT_DISPATCH));
  1656. SyncBootScript (Script);
  1657. return RETURN_SUCCESS;
  1658. }
  1659. /**
  1660. Adds a record for I/O reads the I/O location and continues when the exit criteria is satisfied or after a
  1661. defined duration.
  1662. @param Width The width of the I/O operations.
  1663. @param Address The base address of the I/O operations.
  1664. @param Data The comparison value used for the polling exit criteria.
  1665. @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
  1666. in Data are ignored when polling the memory address.
  1667. @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
  1668. granularity so the delay may be longer.
  1669. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1670. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1671. **/
  1672. RETURN_STATUS
  1673. EFIAPI
  1674. S3BootScriptSaveIoPoll (
  1675. IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
  1676. IN UINT64 Address,
  1677. IN VOID *Data,
  1678. IN VOID *DataMask,
  1679. IN UINT64 Delay
  1680. )
  1681. {
  1682. UINT8 WidthInByte;
  1683. UINT8 *Script;
  1684. UINT8 Length;
  1685. EFI_BOOT_SCRIPT_IO_POLL ScriptIoPoll;
  1686. if (!mS3BootScriptAcpiS3Enable) {
  1687. return RETURN_SUCCESS;
  1688. }
  1689. WidthInByte = (UINT8)(0x01 << (Width & 0x03));
  1690. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_POLL) + (WidthInByte * 2));
  1691. Script = S3BootScriptGetEntryAddAddress (Length);
  1692. if (Script == NULL) {
  1693. return RETURN_OUT_OF_RESOURCES;
  1694. }
  1695. //
  1696. // Build script data
  1697. //
  1698. ScriptIoPoll.OpCode = EFI_BOOT_SCRIPT_IO_POLL_OPCODE;
  1699. ScriptIoPoll.Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_POLL) + (WidthInByte * 2));
  1700. ScriptIoPoll.Width = Width;
  1701. ScriptIoPoll.Address = Address;
  1702. ScriptIoPoll.Delay = Delay;
  1703. CopyMem ((VOID *)Script, (VOID *)&ScriptIoPoll, sizeof (EFI_BOOT_SCRIPT_IO_POLL));
  1704. CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_IO_POLL)), Data, WidthInByte);
  1705. CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_IO_POLL) + WidthInByte), DataMask, WidthInByte);
  1706. SyncBootScript (Script);
  1707. return RETURN_SUCCESS;
  1708. }
  1709. /**
  1710. Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or
  1711. after a defined duration.
  1712. @param Width The width of the I/O operations.
  1713. @param Address The address within the PCI configuration space.
  1714. @param Data The comparison value used for the polling exit criteria.
  1715. @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
  1716. in Data are ignored when polling the memory address
  1717. @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
  1718. granularity so the delay may be longer.
  1719. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1720. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1721. @note A known Limitations in the implementation which is 64bits operations are not supported.
  1722. **/
  1723. RETURN_STATUS
  1724. EFIAPI
  1725. S3BootScriptSavePciPoll (
  1726. IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
  1727. IN UINT64 Address,
  1728. IN VOID *Data,
  1729. IN VOID *DataMask,
  1730. IN UINT64 Delay
  1731. )
  1732. {
  1733. UINT8 *Script;
  1734. UINT8 WidthInByte;
  1735. UINT8 Length;
  1736. EFI_BOOT_SCRIPT_PCI_CONFIG_POLL ScriptPciPoll;
  1737. if (!mS3BootScriptAcpiS3Enable) {
  1738. return RETURN_SUCCESS;
  1739. }
  1740. if ((Width == S3BootScriptWidthUint64) ||
  1741. (Width == S3BootScriptWidthFifoUint64) ||
  1742. (Width == S3BootScriptWidthFillUint64))
  1743. {
  1744. return EFI_INVALID_PARAMETER;
  1745. }
  1746. WidthInByte = (UINT8)(0x01 << (Width & 0x03));
  1747. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + (WidthInByte * 2));
  1748. Script = S3BootScriptGetEntryAddAddress (Length);
  1749. if (Script == NULL) {
  1750. return RETURN_OUT_OF_RESOURCES;
  1751. }
  1752. //
  1753. // Build script data
  1754. //
  1755. ScriptPciPoll.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG_POLL_OPCODE;
  1756. ScriptPciPoll.Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + (WidthInByte * 2));
  1757. ScriptPciPoll.Width = Width;
  1758. ScriptPciPoll.Address = Address;
  1759. ScriptPciPoll.Delay = Delay;
  1760. CopyMem ((VOID *)Script, (VOID *)&ScriptPciPoll, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL));
  1761. CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL)), Data, WidthInByte);
  1762. CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + WidthInByte), DataMask, WidthInByte);
  1763. SyncBootScript (Script);
  1764. return RETURN_SUCCESS;
  1765. }
  1766. /**
  1767. Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or
  1768. after a defined duration.
  1769. @param Width The width of the I/O operations.
  1770. @param Segment The PCI segment number for Address.
  1771. @param Address The address within the PCI configuration space.
  1772. @param Data The comparison value used for the polling exit criteria.
  1773. @param DataMask Mask used for the polling criteria. The bits in the bytes below Width which are zero
  1774. in Data are ignored when polling the memory address
  1775. @param Delay The number of 100ns units to poll. Note that timer available may be of poorer
  1776. granularity so the delay may be longer.
  1777. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1778. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1779. @note A known Limitations in the implementation which is 64bits operations are not supported.
  1780. **/
  1781. RETURN_STATUS
  1782. EFIAPI
  1783. S3BootScriptSavePci2Poll (
  1784. IN S3_BOOT_SCRIPT_LIB_WIDTH Width,
  1785. IN UINT16 Segment,
  1786. IN UINT64 Address,
  1787. IN VOID *Data,
  1788. IN VOID *DataMask,
  1789. IN UINT64 Delay
  1790. )
  1791. {
  1792. UINT8 WidthInByte;
  1793. UINT8 *Script;
  1794. UINT8 Length;
  1795. EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL ScriptPci2Poll;
  1796. if (!mS3BootScriptAcpiS3Enable) {
  1797. return RETURN_SUCCESS;
  1798. }
  1799. if ((Width == S3BootScriptWidthUint64) ||
  1800. (Width == S3BootScriptWidthFifoUint64) ||
  1801. (Width == S3BootScriptWidthFillUint64))
  1802. {
  1803. return EFI_INVALID_PARAMETER;
  1804. }
  1805. WidthInByte = (UINT8)(0x01 << (Width & 0x03));
  1806. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + (WidthInByte * 2));
  1807. Script = S3BootScriptGetEntryAddAddress (Length);
  1808. if (Script == NULL) {
  1809. return RETURN_OUT_OF_RESOURCES;
  1810. }
  1811. //
  1812. // Build script data
  1813. //
  1814. ScriptPci2Poll.OpCode = EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE;
  1815. ScriptPci2Poll.Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + (WidthInByte * 2));
  1816. ScriptPci2Poll.Width = Width;
  1817. ScriptPci2Poll.Segment = Segment;
  1818. ScriptPci2Poll.Address = Address;
  1819. ScriptPci2Poll.Delay = Delay;
  1820. CopyMem ((VOID *)Script, (VOID *)&ScriptPci2Poll, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL));
  1821. CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL)), Data, WidthInByte);
  1822. CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + WidthInByte), DataMask, WidthInByte);
  1823. SyncBootScript (Script);
  1824. return RETURN_SUCCESS;
  1825. }
  1826. /**
  1827. Do the calculation of start address from which a new s3 boot script entry will write into.
  1828. @param EntryLength The new entry length.
  1829. @param Position specifies the position in the boot script table where the opcode will be
  1830. inserted, either before or after, depending on BeforeOrAfter.
  1831. @param BeforeOrAfter The flag to indicate to insert the nod before or after the position.
  1832. This parameter is effective when InsertFlag is TRUE
  1833. @param Script return out the position from which the a new s3 boot script entry will write into
  1834. **/
  1835. VOID
  1836. S3BootScriptCalculateInsertAddress (
  1837. IN UINT8 EntryLength,
  1838. IN VOID *Position OPTIONAL,
  1839. IN BOOLEAN BeforeOrAfter OPTIONAL,
  1840. OUT UINT8 **Script
  1841. )
  1842. {
  1843. UINTN TableLength;
  1844. UINT8 *S3TableBase;
  1845. UINTN PositionOffset;
  1846. EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;
  1847. //
  1848. // The entry inserting to table is already added to the end of the table
  1849. //
  1850. TableLength = mS3BootScriptTablePtr->TableLength - EntryLength;
  1851. S3TableBase = mS3BootScriptTablePtr->TableBase;
  1852. //
  1853. // calculate the Position offset
  1854. //
  1855. if (Position != NULL) {
  1856. PositionOffset = (UINTN)Position - (UINTN)S3TableBase;
  1857. //
  1858. // If the BeforeOrAfter is FALSE, that means to insert the node right after the node.
  1859. //
  1860. if (!BeforeOrAfter) {
  1861. CopyMem ((VOID *)&ScriptHeader, Position, sizeof (EFI_BOOT_SCRIPT_COMMON_HEADER));
  1862. PositionOffset += (ScriptHeader.Length);
  1863. }
  1864. //
  1865. // Insert the node before the adjusted Position
  1866. //
  1867. CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset);
  1868. //
  1869. // calculate the the start address for the new entry.
  1870. //
  1871. *Script = S3TableBase + PositionOffset;
  1872. } else {
  1873. if (!BeforeOrAfter) {
  1874. //
  1875. // Insert the node to the end of the table
  1876. //
  1877. *Script = S3TableBase + TableLength;
  1878. } else {
  1879. //
  1880. // Insert the node to the beginning of the table
  1881. //
  1882. PositionOffset = (UINTN)sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);
  1883. CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset);
  1884. *Script = S3TableBase + PositionOffset;
  1885. }
  1886. }
  1887. }
  1888. /**
  1889. Move the last boot script entry to the position
  1890. @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position
  1891. in the boot script table specified by Position. If Position is NULL or points to
  1892. NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end
  1893. of the table (if FALSE).
  1894. @param Position On entry, specifies the position in the boot script table where the opcode will be
  1895. inserted, either before or after, depending on BeforeOrAfter. On exit, specifies
  1896. the position of the inserted opcode in the boot script table.
  1897. @retval RETURN_OUT_OF_RESOURCES The table is not available.
  1898. @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.
  1899. @retval RETURN_SUCCESS Opcode is inserted no action is required as ACPI S3 was disabled.
  1900. **/
  1901. RETURN_STATUS
  1902. EFIAPI
  1903. S3BootScriptMoveLastOpcode (
  1904. IN BOOLEAN BeforeOrAfter,
  1905. IN OUT VOID **Position OPTIONAL
  1906. )
  1907. {
  1908. UINT8 *Script;
  1909. VOID *TempPosition;
  1910. UINTN StartAddress;
  1911. UINT32 TableLength;
  1912. EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;
  1913. BOOLEAN ValidatePosition;
  1914. UINT8 *LastOpcode;
  1915. UINT8 TempBootScriptEntry[BOOT_SCRIPT_NODE_MAX_LENGTH];
  1916. if (!mS3BootScriptAcpiS3Enable) {
  1917. return RETURN_SUCCESS;
  1918. }
  1919. ValidatePosition = FALSE;
  1920. TempPosition = (Position == NULL) ? NULL : (*Position);
  1921. //
  1922. // Check that the script is initialized and synced without adding an entry to the script.
  1923. //
  1924. Script = S3BootScriptGetEntryAddAddress (0);
  1925. if (Script == NULL) {
  1926. return RETURN_OUT_OF_RESOURCES;
  1927. }
  1928. Script = mS3BootScriptTablePtr->TableBase;
  1929. StartAddress = (UINTN)Script;
  1930. TableLength = mS3BootScriptTablePtr->TableLength;
  1931. Script = Script + sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);
  1932. LastOpcode = Script;
  1933. //
  1934. // Find the last boot Script Entry which is not the terminate node
  1935. //
  1936. while ((UINTN)Script < (UINTN)(StartAddress + TableLength)) {
  1937. CopyMem ((VOID *)&ScriptHeader, Script, sizeof (EFI_BOOT_SCRIPT_COMMON_HEADER));
  1938. if ((TempPosition != NULL) && (TempPosition == Script)) {
  1939. //
  1940. // If the position is specified, the position must be pointed to a boot script entry start address.
  1941. //
  1942. ValidatePosition = TRUE;
  1943. }
  1944. if (ScriptHeader.OpCode != S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE) {
  1945. LastOpcode = Script;
  1946. }
  1947. Script = Script + ScriptHeader.Length;
  1948. }
  1949. //
  1950. // If the position is specified, but not the start of a boot script entry, it is a invalid input
  1951. //
  1952. if ((TempPosition != NULL) && !ValidatePosition) {
  1953. return RETURN_INVALID_PARAMETER;
  1954. }
  1955. CopyMem ((VOID *)&ScriptHeader, LastOpcode, sizeof (EFI_BOOT_SCRIPT_COMMON_HEADER));
  1956. CopyMem ((VOID *)TempBootScriptEntry, LastOpcode, ScriptHeader.Length);
  1957. //
  1958. // Find the right position to write the node in
  1959. //
  1960. S3BootScriptCalculateInsertAddress (
  1961. ScriptHeader.Length,
  1962. TempPosition,
  1963. BeforeOrAfter,
  1964. &Script
  1965. );
  1966. //
  1967. // Copy the node to Boot script table
  1968. //
  1969. CopyMem ((VOID *)Script, (VOID *)TempBootScriptEntry, ScriptHeader.Length);
  1970. SyncBootScript (Script);
  1971. //
  1972. // return out the Position
  1973. //
  1974. if (Position != NULL) {
  1975. *Position = Script;
  1976. }
  1977. return RETURN_SUCCESS;
  1978. }
  1979. /**
  1980. Create a Label node in the boot script table.
  1981. @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position
  1982. in the boot script table specified by Position. If Position is NULL or points to
  1983. NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end
  1984. of the table (if FALSE).
  1985. @param Position On entry, specifies the position in the boot script table where the opcode will be
  1986. inserted, either before or after, depending on BeforeOrAfter. On exit, specifies
  1987. the position of the inserted opcode in the boot script table.
  1988. @param InformationLength Length of the label in bytes
  1989. @param Information Label to be logged in the boot scrpit
  1990. @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.
  1991. @retval RETURN_OUT_OF_RESOURCES Not enough memory for the table do operation.
  1992. @retval RETURN_SUCCESS Opcode is added or no action is required as ACPI S3 was disabled.
  1993. **/
  1994. RETURN_STATUS
  1995. EFIAPI
  1996. S3BootScriptLabelInternal (
  1997. IN BOOLEAN BeforeOrAfter,
  1998. IN OUT VOID **Position OPTIONAL,
  1999. IN UINT32 InformationLength,
  2000. IN CONST CHAR8 *Information
  2001. )
  2002. {
  2003. UINT8 Length;
  2004. UINT8 *Script;
  2005. EFI_BOOT_SCRIPT_INFORMATION ScriptInformation;
  2006. if (!mS3BootScriptAcpiS3Enable) {
  2007. return RETURN_SUCCESS;
  2008. }
  2009. //
  2010. // Truncation check
  2011. //
  2012. if (InformationLength > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_INFORMATION)) {
  2013. return RETURN_OUT_OF_RESOURCES;
  2014. }
  2015. Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION) + InformationLength);
  2016. Script = S3BootScriptGetEntryAddAddress (Length);
  2017. if (Script == NULL) {
  2018. return RETURN_OUT_OF_RESOURCES;
  2019. }
  2020. //
  2021. // Build script data
  2022. //
  2023. ScriptInformation.OpCode = S3_BOOT_SCRIPT_LIB_LABEL_OPCODE;
  2024. ScriptInformation.Length = Length;
  2025. ScriptInformation.InformationLength = InformationLength;
  2026. CopyMem ((VOID *)Script, (VOID *)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));
  2027. CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION)), (VOID *)Information, (UINTN)InformationLength);
  2028. SyncBootScript (Script);
  2029. return S3BootScriptMoveLastOpcode (BeforeOrAfter, Position);
  2030. }
  2031. /**
  2032. Find a label within the boot script table and, if not present, optionally create it.
  2033. @param BeforeOrAfter Specifies whether the opcode is stored before (TRUE)
  2034. or after (FALSE) the position in the boot script table
  2035. specified by Position.
  2036. @param CreateIfNotFound Specifies whether the label will be created if the label
  2037. does not exists (TRUE) or not (FALSE).
  2038. @param Position On entry, specifies the position in the boot script table
  2039. where the opcode will be inserted, either before or after,
  2040. depending on BeforeOrAfter. On exit, specifies the position
  2041. of the inserted opcode in the boot script table.
  2042. @param Label Points to the label which will be inserted in the boot script table.
  2043. @retval EFI_SUCCESS The operation succeeded or no action is required.
  2044. A record was added into the specified script table if ACPI S3 was enabled.
  2045. @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.
  2046. If the opcode is unknow or not supported because of the PCD
  2047. Feature Flags.
  2048. @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
  2049. **/
  2050. RETURN_STATUS
  2051. EFIAPI
  2052. S3BootScriptLabel (
  2053. IN BOOLEAN BeforeOrAfter,
  2054. IN BOOLEAN CreateIfNotFound,
  2055. IN OUT VOID **Position OPTIONAL,
  2056. IN CONST CHAR8 *Label
  2057. )
  2058. {
  2059. UINT8 *Script;
  2060. UINTN StartAddress;
  2061. UINT32 TableLength;
  2062. EFI_BOOT_SCRIPT_COMMON_HEADER ScriptHeader;
  2063. EFI_BOOT_SCRIPT_TABLE_HEADER TableHeader;
  2064. UINT32 LabelLength;
  2065. if (!mS3BootScriptAcpiS3Enable) {
  2066. return RETURN_SUCCESS;
  2067. }
  2068. //
  2069. // Check NULL Label
  2070. //
  2071. if (Label == NULL) {
  2072. return EFI_INVALID_PARAMETER;
  2073. }
  2074. //
  2075. // Check empty Label
  2076. //
  2077. if (Label[0] == '\0') {
  2078. return EFI_INVALID_PARAMETER;
  2079. }
  2080. //
  2081. // Check that the script is initialized and synced without adding an entry to the script.
  2082. // The code must search for the label first before it knows if a new entry needs
  2083. // to be added.
  2084. //
  2085. Script = S3BootScriptGetEntryAddAddress (0);
  2086. if (Script == NULL) {
  2087. return RETURN_OUT_OF_RESOURCES;
  2088. }
  2089. //
  2090. // Check the header and search for existing label.
  2091. //
  2092. Script = mS3BootScriptTablePtr->TableBase;
  2093. CopyMem ((VOID *)&TableHeader, Script, sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER));
  2094. if (TableHeader.OpCode != S3_BOOT_SCRIPT_LIB_TABLE_OPCODE) {
  2095. return EFI_INVALID_PARAMETER;
  2096. }
  2097. StartAddress = (UINTN)Script;
  2098. TableLength = mS3BootScriptTablePtr->TableLength;
  2099. Script = Script + TableHeader.Length;
  2100. while ((UINTN)Script < (UINTN)(StartAddress + TableLength)) {
  2101. CopyMem ((VOID *)&ScriptHeader, Script, sizeof (EFI_BOOT_SCRIPT_COMMON_HEADER));
  2102. if (ScriptHeader.OpCode == S3_BOOT_SCRIPT_LIB_LABEL_OPCODE) {
  2103. if (AsciiStrCmp ((CHAR8 *)(UINTN)(Script+sizeof (EFI_BOOT_SCRIPT_INFORMATION)), Label) == 0) {
  2104. (*Position) = Script;
  2105. return EFI_SUCCESS;
  2106. }
  2107. }
  2108. Script = Script + ScriptHeader.Length;
  2109. }
  2110. if (CreateIfNotFound) {
  2111. LabelLength = (UINT32)AsciiStrSize (Label);
  2112. return S3BootScriptLabelInternal (BeforeOrAfter, Position, LabelLength, Label);
  2113. } else {
  2114. return EFI_NOT_FOUND;
  2115. }
  2116. }
  2117. /**
  2118. Compare two positions in the boot script table and return their relative position.
  2119. @param Position1 The positions in the boot script table to compare
  2120. @param Position2 The positions in the boot script table to compare
  2121. @param RelativePosition On return, points to the result of the comparison
  2122. @retval EFI_SUCCESS The operation succeeded or no action is required.
  2123. A record was added into the specified script table if ACPI S3 was enabled.
  2124. @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.
  2125. If the opcode is unknow or not supported because of the PCD
  2126. Feature Flags.
  2127. @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
  2128. **/
  2129. RETURN_STATUS
  2130. EFIAPI
  2131. S3BootScriptCompare (
  2132. IN UINT8 *Position1,
  2133. IN UINT8 *Position2,
  2134. OUT UINTN *RelativePosition
  2135. )
  2136. {
  2137. UINT8 *Script;
  2138. UINT32 TableLength;
  2139. if (!mS3BootScriptAcpiS3Enable) {
  2140. return RETURN_SUCCESS;
  2141. }
  2142. if (RelativePosition == NULL) {
  2143. return EFI_INVALID_PARAMETER;
  2144. }
  2145. //
  2146. // Check that the script is initialized and synced without adding an entry to the script.
  2147. //
  2148. Script = S3BootScriptGetEntryAddAddress (0);
  2149. if (Script == NULL) {
  2150. return RETURN_OUT_OF_RESOURCES;
  2151. }
  2152. Script = mS3BootScriptTablePtr->TableBase;
  2153. //
  2154. // mS3BootScriptTablePtr->TableLength does not include the termination node, so add it up
  2155. //
  2156. TableLength = mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE);
  2157. if ((Position1 < Script) || (Position1 > Script+TableLength)) {
  2158. return EFI_INVALID_PARAMETER;
  2159. }
  2160. if ((Position2 < Script) || (Position2 > Script+TableLength)) {
  2161. return EFI_INVALID_PARAMETER;
  2162. }
  2163. *RelativePosition = (Position1 < Position2) ? -1 : ((Position1 == Position2) ? 0 : 1);
  2164. return EFI_SUCCESS;
  2165. }