ScriptSave.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /** @file
  2. Implementation for S3 Boot Script Saver driver.
  3. Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "InternalBootScriptSave.h"
  7. EFI_HANDLE mHandle = NULL;
  8. EFI_BOOT_SCRIPT_SAVE_PROTOCOL mS3ScriptSave = {
  9. BootScriptWrite,
  10. BootScriptCloseTable
  11. };
  12. /**
  13. Internal function to add IO write opcode to the table.
  14. @param Marker The variable argument list to get the opcode
  15. and associated attributes.
  16. @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
  17. @retval EFI_SUCCESS Opcode is added.
  18. **/
  19. EFI_STATUS
  20. BootScriptIoWrite (
  21. IN VA_LIST Marker
  22. )
  23. {
  24. S3_BOOT_SCRIPT_LIB_WIDTH Width;
  25. UINT64 Address;
  26. UINTN Count;
  27. UINT8 *Buffer;
  28. Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
  29. Address = VA_ARG (Marker, UINT64);
  30. Count = VA_ARG (Marker, UINTN);
  31. Buffer = VA_ARG (Marker, UINT8 *);
  32. return S3BootScriptSaveIoWrite (Width, Address, Count, Buffer);
  33. }
  34. /**
  35. Internal function to add IO read/write opcode to the table.
  36. @param Marker The variable argument list to get the opcode
  37. and associated attributes.
  38. @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
  39. @retval EFI_SUCCESS Opcode is added.
  40. **/
  41. EFI_STATUS
  42. BootScriptIoReadWrite (
  43. IN VA_LIST Marker
  44. )
  45. {
  46. S3_BOOT_SCRIPT_LIB_WIDTH Width;
  47. UINT64 Address;
  48. UINT8 *Data;
  49. UINT8 *DataMask;
  50. Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
  51. Address = VA_ARG (Marker, UINT64);
  52. Data = VA_ARG (Marker, UINT8 *);
  53. DataMask = VA_ARG (Marker, UINT8 *);
  54. return S3BootScriptSaveIoReadWrite (Width, Address, Data, DataMask);
  55. }
  56. /**
  57. Internal function to add memory write opcode to the table.
  58. @param Marker The variable argument list to get the opcode
  59. and associated attributes.
  60. @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
  61. @retval EFI_SUCCESS Opcode is added.
  62. **/
  63. EFI_STATUS
  64. BootScriptMemWrite (
  65. IN VA_LIST Marker
  66. )
  67. {
  68. S3_BOOT_SCRIPT_LIB_WIDTH Width;
  69. UINT64 Address;
  70. UINTN Count;
  71. UINT8 *Buffer;
  72. Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
  73. Address = VA_ARG (Marker, UINT64);
  74. Count = VA_ARG (Marker, UINTN);
  75. Buffer = VA_ARG (Marker, UINT8 *);
  76. return S3BootScriptSaveMemWrite (Width, Address, Count, Buffer);
  77. }
  78. /**
  79. Internal function to add memory read/write opcode to the table.
  80. @param Marker The variable argument list to get the opcode
  81. and associated attributes.
  82. @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
  83. @retval EFI_SUCCESS Opcode is added.
  84. **/
  85. EFI_STATUS
  86. BootScriptMemReadWrite (
  87. IN VA_LIST Marker
  88. )
  89. {
  90. S3_BOOT_SCRIPT_LIB_WIDTH Width;
  91. UINT64 Address;
  92. UINT8 *Data;
  93. UINT8 *DataMask;
  94. Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
  95. Address = VA_ARG (Marker, UINT64);
  96. Data = VA_ARG (Marker, UINT8 *);
  97. DataMask = VA_ARG (Marker, UINT8 *);
  98. return S3BootScriptSaveMemReadWrite (Width, Address, Data, DataMask);
  99. }
  100. /**
  101. Internal function to add PciCfg write opcode to the table.
  102. @param Marker The variable argument list to get the opcode
  103. and associated attributes.
  104. @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
  105. @retval EFI_SUCCESS Opcode is added.
  106. **/
  107. EFI_STATUS
  108. BootScriptPciCfgWrite (
  109. IN VA_LIST Marker
  110. )
  111. {
  112. S3_BOOT_SCRIPT_LIB_WIDTH Width;
  113. UINT64 Address;
  114. UINTN Count;
  115. UINT8 *Buffer;
  116. Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
  117. Address = VA_ARG (Marker, UINT64);
  118. Count = VA_ARG (Marker, UINTN);
  119. Buffer = VA_ARG (Marker, UINT8 *);
  120. return S3BootScriptSavePciCfgWrite (Width, Address, Count, Buffer);
  121. }
  122. /**
  123. Internal function to PciCfg read/write opcode to the table.
  124. @param Marker The variable argument list to get the opcode
  125. and associated attributes.
  126. @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
  127. @retval EFI_SUCCESS Opcode is added.
  128. **/
  129. EFI_STATUS
  130. BootScriptPciCfgReadWrite (
  131. IN VA_LIST Marker
  132. )
  133. {
  134. S3_BOOT_SCRIPT_LIB_WIDTH Width;
  135. UINT64 Address;
  136. UINT8 *Data;
  137. UINT8 *DataMask;
  138. Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
  139. Address = VA_ARG (Marker, UINT64);
  140. Data = VA_ARG (Marker, UINT8 *);
  141. DataMask = VA_ARG (Marker, UINT8 *);
  142. return S3BootScriptSavePciCfgReadWrite (Width, Address, Data, DataMask);
  143. }
  144. /**
  145. Internal function to add PciCfg2 write opcode to the table.
  146. @param Marker The variable argument list to get the opcode
  147. and associated attributes.
  148. @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
  149. @retval EFI_SUCCESS Opcode is added.
  150. **/
  151. EFI_STATUS
  152. BootScriptPciCfg2Write (
  153. IN VA_LIST Marker
  154. )
  155. {
  156. S3_BOOT_SCRIPT_LIB_WIDTH Width;
  157. UINT64 Address;
  158. UINTN Count;
  159. UINT8 *Buffer;
  160. UINT16 Segment;
  161. Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
  162. Address = VA_ARG (Marker, UINT64);
  163. Count = VA_ARG (Marker, UINTN);
  164. Buffer = VA_ARG (Marker, UINT8 *);
  165. Segment = VA_ARG (Marker, UINT16);
  166. return S3BootScriptSavePciCfg2Write (Width, Segment, Address, Count, Buffer);
  167. }
  168. /**
  169. Internal function to PciCfg2 read/write opcode to the table.
  170. @param Marker The variable argument list to get the opcode
  171. and associated attributes.
  172. @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
  173. @retval EFI_SUCCESS Opcode is added.
  174. **/
  175. EFI_STATUS
  176. BootScriptPciCfg2ReadWrite (
  177. IN VA_LIST Marker
  178. )
  179. {
  180. S3_BOOT_SCRIPT_LIB_WIDTH Width;
  181. UINT16 Segment;
  182. UINT64 Address;
  183. UINT8 *Data;
  184. UINT8 *DataMask;
  185. Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
  186. Address = VA_ARG (Marker, UINT64);
  187. Segment = VA_ARG (Marker, UINT16);
  188. Data = VA_ARG (Marker, UINT8 *);
  189. DataMask = VA_ARG (Marker, UINT8 *);
  190. return S3BootScriptSavePciCfg2ReadWrite (Width, Segment, Address, Data, DataMask);
  191. }
  192. /**
  193. Internal function to add smbus execute opcode to the table.
  194. @param Marker The variable argument list to get the opcode
  195. and associated attributes.
  196. @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
  197. @retval EFI_SUCCESS Opcode is added.
  198. **/
  199. EFI_STATUS
  200. BootScriptSmbusExecute (
  201. IN VA_LIST Marker
  202. )
  203. {
  204. EFI_SMBUS_DEVICE_ADDRESS SlaveAddress;
  205. EFI_SMBUS_DEVICE_COMMAND Command;
  206. EFI_SMBUS_OPERATION Operation;
  207. BOOLEAN PecCheck;
  208. VOID *Buffer;
  209. UINTN *DataSize;
  210. UINTN SmBusAddress;
  211. SlaveAddress.SmbusDeviceAddress = VA_ARG (Marker, UINTN);
  212. Command = VA_ARG (Marker, EFI_SMBUS_DEVICE_COMMAND);
  213. Operation = VA_ARG (Marker, EFI_SMBUS_OPERATION);
  214. PecCheck = VA_ARG (Marker, BOOLEAN);
  215. SmBusAddress = SMBUS_LIB_ADDRESS (SlaveAddress.SmbusDeviceAddress,Command,0,PecCheck);
  216. DataSize = VA_ARG (Marker, UINTN *);
  217. Buffer = VA_ARG (Marker, VOID *);
  218. return S3BootScriptSaveSmbusExecute (SmBusAddress, Operation, DataSize, Buffer);
  219. }
  220. /**
  221. Internal function to add stall opcode to the table.
  222. @param Marker The variable argument list to get the opcode
  223. and associated attributes.
  224. @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
  225. @retval EFI_SUCCESS Opcode is added.
  226. **/
  227. EFI_STATUS
  228. BootScriptStall (
  229. IN VA_LIST Marker
  230. )
  231. {
  232. UINT32 Duration;
  233. Duration = VA_ARG (Marker, UINT32);
  234. return S3BootScriptSaveStall (Duration);
  235. }
  236. /**
  237. Internal function to add Save jmp address according to DISPATCH_OPCODE.
  238. We ignore "Context" parameter.
  239. @param Marker The variable argument list to get the opcode
  240. and associated attributes.
  241. @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
  242. @retval EFI_SUCCESS Opcode is added.
  243. **/
  244. EFI_STATUS
  245. BootScriptDispatch (
  246. IN VA_LIST Marker
  247. )
  248. {
  249. VOID *EntryPoint;
  250. EntryPoint = (VOID*)(UINTN)VA_ARG (Marker, EFI_PHYSICAL_ADDRESS);
  251. return S3BootScriptSaveDispatch (EntryPoint);
  252. }
  253. /**
  254. Internal function to add memory pool operation to the table.
  255. @param Marker The variable argument list to get the opcode
  256. and associated attributes.
  257. @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
  258. @retval EFI_SUCCESS Opcode is added.
  259. **/
  260. EFI_STATUS
  261. BootScriptMemPoll (
  262. IN VA_LIST Marker
  263. )
  264. {
  265. S3_BOOT_SCRIPT_LIB_WIDTH Width;
  266. UINT64 Address;
  267. UINT8 *BitMask;
  268. UINT8 *BitValue;
  269. UINTN Duration;
  270. UINT64 LoopTimes;
  271. Width = VA_ARG (Marker, S3_BOOT_SCRIPT_LIB_WIDTH);
  272. Address = VA_ARG (Marker, UINT64);
  273. BitMask = VA_ARG (Marker, UINT8 *);
  274. BitValue = VA_ARG (Marker, UINT8 *);
  275. Duration = (UINTN)VA_ARG (Marker, UINT64);
  276. LoopTimes = VA_ARG (Marker, UINT64);
  277. return S3BootScriptSaveMemPoll (Width, Address, BitMask, BitValue, Duration, LoopTimes);
  278. }
  279. /**
  280. Internal function to add Save jmp address according to DISPATCH_OPCODE2.
  281. The "Context" parameter is not ignored.
  282. @param Marker The variable argument list to get the opcode
  283. and associated attributes.
  284. @retval EFI_OUT_OF_RESOURCES Not enough resource to do operation.
  285. @retval EFI_SUCCESS Opcode is added.
  286. **/
  287. EFI_STATUS
  288. BootScriptDispatch2 (
  289. IN VA_LIST Marker
  290. )
  291. {
  292. VOID *EntryPoint;
  293. VOID *Context;
  294. EntryPoint = (VOID*)(UINTN)VA_ARG (Marker, EFI_PHYSICAL_ADDRESS);
  295. Context = (VOID*)(UINTN)VA_ARG (Marker, EFI_PHYSICAL_ADDRESS);
  296. return S3BootScriptSaveDispatch2 (EntryPoint, Context);
  297. }
  298. /**
  299. Internal function to add the opcode link node to the link list.
  300. @param Marker The variable argument list to get the opcode
  301. and associated attributes.
  302. @retval EFI_OUT_OF_RESOURCES Not enought resource to complete the operations.
  303. @retval EFI_SUCCESS The opcode entry is added to the link list
  304. successfully.
  305. **/
  306. EFI_STATUS
  307. BootScriptInformation (
  308. IN VA_LIST Marker
  309. )
  310. {
  311. UINT32 InformationLength;
  312. EFI_PHYSICAL_ADDRESS Information;
  313. InformationLength = VA_ARG (Marker, UINT32);
  314. Information = VA_ARG (Marker, EFI_PHYSICAL_ADDRESS);
  315. return S3BootScriptSaveInformation (InformationLength, (VOID*)(UINTN)Information);
  316. }
  317. /**
  318. Adds a record into a specified Framework boot script table.
  319. This function is used to store a boot script record into a given boot
  320. script table. If the table specified by TableName is nonexistent in the
  321. system, a new table will automatically be created and then the script record
  322. will be added into the new table. A boot script table can add new script records
  323. until EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable() is called. Currently, the only
  324. meaningful table name is EFI_ACPI_S3_RESUME_SCRIPT_TABLE. This function is
  325. responsible for allocating necessary memory for the script.
  326. This function has a variable parameter list. The exact parameter list depends on
  327. the OpCode that is passed into the function. If an unsupported OpCode or illegal
  328. parameter list is passed in, this function returns EFI_INVALID_PARAMETER.
  329. If there are not enough resources available for storing more scripts, this function returns
  330. EFI_OUT_OF_RESOURCES.
  331. @param This A pointer to the EFI_BOOT_SCRIPT_SAVE_PROTOCOL instance.
  332. @param TableName Name of the script table. Currently, the only meaningful value is
  333. EFI_ACPI_S3_RESUME_SCRIPT_TABLE.
  334. @param OpCode The operation code (opcode) number.
  335. @param ... Argument list that is specific to each opcode.
  336. @retval EFI_SUCCESS The operation succeeded. A record was added into the
  337. specified script table.
  338. @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.
  339. If the opcode is unknow or not supported because of the PCD
  340. Feature Flags.
  341. @retval EFI_OUT_OF_RESOURCES There is insufficient memory to store the boot script.
  342. **/
  343. EFI_STATUS
  344. EFIAPI
  345. BootScriptWrite (
  346. IN EFI_BOOT_SCRIPT_SAVE_PROTOCOL *This,
  347. IN UINT16 TableName,
  348. IN UINT16 OpCode,
  349. ...
  350. )
  351. {
  352. EFI_STATUS Status;
  353. VA_LIST Marker;
  354. if (TableName != FRAMEWORK_EFI_ACPI_S3_RESUME_SCRIPT_TABLE) {
  355. //
  356. // Only S3 boot script is supported for now.
  357. //
  358. return EFI_OUT_OF_RESOURCES;
  359. }
  360. //
  361. // Build script according to opcode.
  362. //
  363. switch (OpCode) {
  364. case EFI_BOOT_SCRIPT_IO_WRITE_OPCODE:
  365. VA_START (Marker, OpCode);
  366. Status = BootScriptIoWrite (Marker);
  367. VA_END (Marker);
  368. break;
  369. case EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE:
  370. VA_START (Marker, OpCode);
  371. Status = BootScriptIoReadWrite (Marker);
  372. VA_END (Marker);
  373. break;
  374. case EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE:
  375. VA_START (Marker, OpCode);
  376. Status = BootScriptMemWrite (Marker);
  377. VA_END (Marker);
  378. break;
  379. case EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE:
  380. VA_START (Marker, OpCode);
  381. Status = BootScriptMemReadWrite (Marker);
  382. VA_END (Marker);
  383. break;
  384. case EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE_OPCODE:
  385. VA_START (Marker, OpCode);
  386. Status = BootScriptPciCfgWrite (Marker);
  387. VA_END (Marker);
  388. break;
  389. case EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE:
  390. VA_START (Marker, OpCode);
  391. Status = BootScriptPciCfgReadWrite (Marker);
  392. VA_END (Marker);
  393. break;
  394. case EFI_BOOT_SCRIPT_SMBUS_EXECUTE_OPCODE:
  395. VA_START (Marker, OpCode);
  396. Status = BootScriptSmbusExecute (Marker);
  397. VA_END (Marker);
  398. break;
  399. case EFI_BOOT_SCRIPT_STALL_OPCODE:
  400. VA_START (Marker, OpCode);
  401. Status = BootScriptStall (Marker);
  402. VA_END (Marker);
  403. break;
  404. case EFI_BOOT_SCRIPT_DISPATCH_OPCODE:
  405. VA_START (Marker, OpCode);
  406. Status = BootScriptDispatch (Marker);
  407. VA_END (Marker);
  408. break;
  409. case EFI_BOOT_SCRIPT_DISPATCH_2_OPCODE:
  410. VA_START (Marker, OpCode);
  411. Status = BootScriptDispatch2 (Marker);
  412. VA_END (Marker);
  413. break;
  414. case EFI_BOOT_SCRIPT_INFORMATION_OPCODE:
  415. VA_START (Marker, OpCode);
  416. Status = BootScriptInformation (Marker);
  417. VA_END (Marker);
  418. break;
  419. case EFI_BOOT_SCRIPT_MEM_POLL_OPCODE:
  420. VA_START (Marker, OpCode);
  421. Status = BootScriptMemPoll (Marker);
  422. VA_END (Marker);
  423. break;
  424. case EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE:
  425. VA_START (Marker, OpCode);
  426. Status = BootScriptPciCfg2Write (Marker);
  427. VA_END (Marker);
  428. break;
  429. case EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE:
  430. VA_START (Marker, OpCode);
  431. Status = BootScriptPciCfg2ReadWrite (Marker);
  432. VA_END (Marker);
  433. break;
  434. default:
  435. Status = EFI_INVALID_PARAMETER;
  436. break;
  437. }
  438. return Status;
  439. }
  440. /**
  441. Closes the specified script table.
  442. This function closes the specified boot script table and returns the base address
  443. of the table. It allocates a new pool to duplicate all the boot scripts in the specified
  444. table. Once this function is called, the specified table will be destroyed after it is
  445. copied into the allocated pool. As a result, any attempts to add a script record into a
  446. closed table will cause a new table to be created. The base address of the allocated pool
  447. will be returned in Address. After using the boot script table, the caller is responsible
  448. for freeing the pool that is allocated by this function. If the boot script table,
  449. such as EFI_ACPI_S3_RESUME_SCRIPT_TABLE, is required to be stored in a nonperturbed
  450. memory region, the caller should copy the table into the nonperturbed memory region by itself.
  451. @param This A pointer to the EFI_BOOT_SCRIPT_SAVE_PROTOCOL instance.
  452. @param TableName Name of the script table. Currently, the only meaningful value is
  453. EFI_ACPI_S3_RESUME_SCRIPT_TABLE.
  454. @param Address A pointer to the physical address where the table begins.
  455. @retval EFI_SUCCESS The table was successfully returned.
  456. @retval EFI_NOT_FOUND The specified table was not created previously.
  457. @retval EFI_OUT_OF_RESOURCE Memory is insufficient to hold the reorganized boot script table.
  458. @retval EFI_UNSUPPORTED The table type is not EFI_ACPI_S3_RESUME_SCRIPT_TABLE.
  459. **/
  460. EFI_STATUS
  461. EFIAPI
  462. BootScriptCloseTable (
  463. IN EFI_BOOT_SCRIPT_SAVE_PROTOCOL *This,
  464. IN UINT16 TableName,
  465. OUT EFI_PHYSICAL_ADDRESS *Address
  466. )
  467. {
  468. if (TableName != FRAMEWORK_EFI_ACPI_S3_RESUME_SCRIPT_TABLE) {
  469. //
  470. // Only S3 boot script is supported for now.
  471. //
  472. return EFI_NOT_FOUND;
  473. }
  474. *Address = (EFI_PHYSICAL_ADDRESS)(UINTN)S3BootScriptCloseTable ();
  475. if (*Address == 0) {
  476. return EFI_NOT_FOUND;
  477. }
  478. return EFI_SUCCESS;
  479. }
  480. /**
  481. This routine is entry point of ScriptSave driver.
  482. @param ImageHandle Handle for this drivers loaded image protocol.
  483. @param SystemTable EFI system table.
  484. @retval EFI_OUT_OF_RESOURCES No enough resource.
  485. @retval EFI_SUCCESS Succesfully installed the ScriptSave driver.
  486. @retval other Errors occured.
  487. **/
  488. EFI_STATUS
  489. EFIAPI
  490. InitializeScriptSave (
  491. IN EFI_HANDLE ImageHandle,
  492. IN EFI_SYSTEM_TABLE *SystemTable
  493. )
  494. {
  495. return gBS->InstallProtocolInterface (
  496. &mHandle,
  497. &gEfiBootScriptSaveProtocolGuid,
  498. EFI_NATIVE_INTERFACE,
  499. &mS3ScriptSave
  500. );
  501. }