BootScript.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /** @file
  2. Append an ACPI S3 Boot Script fragment from the QEMU_LOADER_WRITE_POINTER
  3. commands of QEMU's fully processed table linker/loader script.
  4. Copyright (C) 2017, Red Hat, Inc.
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <Library/BaseLib.h> // CpuDeadLoop()
  8. #include <Library/DebugLib.h> // DEBUG()
  9. #include <Library/MemoryAllocationLib.h> // AllocatePool()
  10. #include <Library/QemuFwCfgS3Lib.h> // QemuFwCfgS3ScriptSkipBytes()
  11. #include "AcpiPlatform.h"
  12. //
  13. // Condensed structure for capturing the fw_cfg operations -- select, skip,
  14. // write -- inherent in executing a QEMU_LOADER_WRITE_POINTER command.
  15. //
  16. typedef struct {
  17. UINT16 PointerItem; // resolved from QEMU_LOADER_WRITE_POINTER.PointerFile
  18. UINT8 PointerSize; // copied as-is from QEMU_LOADER_WRITE_POINTER
  19. UINT32 PointerOffset; // copied as-is from QEMU_LOADER_WRITE_POINTER
  20. UINT64 PointerValue; // resolved from QEMU_LOADER_WRITE_POINTER.PointeeFile
  21. // and QEMU_LOADER_WRITE_POINTER.PointeeOffset
  22. } CONDENSED_WRITE_POINTER;
  23. //
  24. // Context structure to accumulate CONDENSED_WRITE_POINTER objects from
  25. // QEMU_LOADER_WRITE_POINTER commands.
  26. //
  27. // Any pointers in this structure own the pointed-to objects; that is, when the
  28. // context structure is released, all pointed-to objects must be released too.
  29. //
  30. struct S3_CONTEXT {
  31. CONDENSED_WRITE_POINTER *WritePointers; // one array element per processed
  32. // QEMU_LOADER_WRITE_POINTER
  33. // command
  34. UINTN Allocated; // number of elements allocated for
  35. // WritePointers
  36. UINTN Used; // number of elements populated in
  37. // WritePointers
  38. };
  39. //
  40. // Scratch buffer, allocated in EfiReservedMemoryType type memory, for the ACPI
  41. // S3 Boot Script opcodes to work on.
  42. //
  43. #pragma pack (1)
  44. typedef union {
  45. UINT64 PointerValue; // filled in from CONDENSED_WRITE_POINTER.PointerValue
  46. } SCRATCH_BUFFER;
  47. #pragma pack ()
  48. /**
  49. Allocate an S3_CONTEXT object.
  50. @param[out] S3Context The allocated S3_CONTEXT object is returned
  51. through this parameter.
  52. @param[in] WritePointerCount Number of CONDENSED_WRITE_POINTER elements to
  53. allocate room for. WritePointerCount must be
  54. positive.
  55. @retval EFI_SUCCESS Allocation successful.
  56. @retval EFI_OUT_OF_RESOURCES Out of memory.
  57. @retval EFI_INVALID_PARAMETER WritePointerCount is zero.
  58. **/
  59. EFI_STATUS
  60. AllocateS3Context (
  61. OUT S3_CONTEXT **S3Context,
  62. IN UINTN WritePointerCount
  63. )
  64. {
  65. EFI_STATUS Status;
  66. S3_CONTEXT *Context;
  67. if (WritePointerCount == 0) {
  68. return EFI_INVALID_PARAMETER;
  69. }
  70. Context = AllocateZeroPool (sizeof *Context);
  71. if (Context == NULL) {
  72. return EFI_OUT_OF_RESOURCES;
  73. }
  74. Context->WritePointers = AllocatePool (
  75. WritePointerCount *
  76. sizeof *Context->WritePointers
  77. );
  78. if (Context->WritePointers == NULL) {
  79. Status = EFI_OUT_OF_RESOURCES;
  80. goto FreeContext;
  81. }
  82. Context->Allocated = WritePointerCount;
  83. *S3Context = Context;
  84. return EFI_SUCCESS;
  85. FreeContext:
  86. FreePool (Context);
  87. return Status;
  88. }
  89. /**
  90. Release an S3_CONTEXT object.
  91. @param[in] S3Context The object to release.
  92. **/
  93. VOID
  94. ReleaseS3Context (
  95. IN S3_CONTEXT *S3Context
  96. )
  97. {
  98. FreePool (S3Context->WritePointers);
  99. FreePool (S3Context);
  100. }
  101. /**
  102. Save the information necessary to replicate a QEMU_LOADER_WRITE_POINTER
  103. command during S3 resume, in condensed format.
  104. This function is to be called from ProcessCmdWritePointer(), after all the
  105. sanity checks have passed, and before the fw_cfg operations are performed.
  106. @param[in,out] S3Context The S3_CONTEXT object into which the caller wants
  107. to save the information that was derived from
  108. QEMU_LOADER_WRITE_POINTER.
  109. @param[in] PointerItem The FIRMWARE_CONFIG_ITEM that
  110. QEMU_LOADER_WRITE_POINTER.PointerFile was resolved
  111. to, expressed as a UINT16 value.
  112. @param[in] PointerSize Copied directly from
  113. QEMU_LOADER_WRITE_POINTER.PointerSize.
  114. @param[in] PointerOffset Copied directly from
  115. QEMU_LOADER_WRITE_POINTER.PointerOffset.
  116. @param[in] PointerValue The base address of the allocated / downloaded
  117. fw_cfg blob that is identified by
  118. QEMU_LOADER_WRITE_POINTER.PointeeFile, plus
  119. QEMU_LOADER_WRITE_POINTER.PointeeOffset.
  120. @retval EFI_SUCCESS The information derived from
  121. QEMU_LOADER_WRITE_POINTER has been successfully
  122. absorbed into S3Context.
  123. @retval EFI_OUT_OF_RESOURCES No room available in S3Context.
  124. **/
  125. EFI_STATUS
  126. SaveCondensedWritePointerToS3Context (
  127. IN OUT S3_CONTEXT *S3Context,
  128. IN UINT16 PointerItem,
  129. IN UINT8 PointerSize,
  130. IN UINT32 PointerOffset,
  131. IN UINT64 PointerValue
  132. )
  133. {
  134. CONDENSED_WRITE_POINTER *Condensed;
  135. if (S3Context->Used == S3Context->Allocated) {
  136. return EFI_OUT_OF_RESOURCES;
  137. }
  138. Condensed = S3Context->WritePointers + S3Context->Used;
  139. Condensed->PointerItem = PointerItem;
  140. Condensed->PointerSize = PointerSize;
  141. Condensed->PointerOffset = PointerOffset;
  142. Condensed->PointerValue = PointerValue;
  143. DEBUG ((
  144. DEBUG_VERBOSE,
  145. "%a: 0x%04x/[0x%08x+%d] := 0x%Lx (%Lu)\n",
  146. __FUNCTION__,
  147. PointerItem,
  148. PointerOffset,
  149. PointerSize,
  150. PointerValue,
  151. (UINT64)S3Context->Used
  152. ));
  153. ++S3Context->Used;
  154. return EFI_SUCCESS;
  155. }
  156. /**
  157. FW_CFG_BOOT_SCRIPT_CALLBACK_FUNCTION provided to QemuFwCfgS3Lib.
  158. **/
  159. STATIC
  160. VOID
  161. EFIAPI
  162. AppendFwCfgBootScript (
  163. IN OUT VOID *Context OPTIONAL,
  164. IN OUT VOID *ExternalScratchBuffer
  165. )
  166. {
  167. S3_CONTEXT *S3Context;
  168. SCRATCH_BUFFER *ScratchBuffer;
  169. UINTN Index;
  170. S3Context = Context;
  171. ScratchBuffer = ExternalScratchBuffer;
  172. for (Index = 0; Index < S3Context->Used; ++Index) {
  173. CONST CONDENSED_WRITE_POINTER *Condensed;
  174. RETURN_STATUS Status;
  175. Condensed = &S3Context->WritePointers[Index];
  176. Status = QemuFwCfgS3ScriptSkipBytes (
  177. Condensed->PointerItem,
  178. Condensed->PointerOffset
  179. );
  180. if (RETURN_ERROR (Status)) {
  181. goto FatalError;
  182. }
  183. ScratchBuffer->PointerValue = Condensed->PointerValue;
  184. Status = QemuFwCfgS3ScriptWriteBytes (-1, Condensed->PointerSize);
  185. if (RETURN_ERROR (Status)) {
  186. goto FatalError;
  187. }
  188. }
  189. DEBUG ((DEBUG_VERBOSE, "%a: boot script fragment saved\n", __FUNCTION__));
  190. ReleaseS3Context (S3Context);
  191. return;
  192. FatalError:
  193. ASSERT (FALSE);
  194. CpuDeadLoop ();
  195. }
  196. /**
  197. Translate and append the information from an S3_CONTEXT object to the ACPI S3
  198. Boot Script.
  199. The effects of a successful call to this function cannot be undone.
  200. @param[in] S3Context The S3_CONTEXT object to translate to ACPI S3 Boot
  201. Script opcodes. If the function returns successfully,
  202. the caller must set the S3Context pointer -- originally
  203. returned by AllocateS3Context() -- immediately to NULL,
  204. because the ownership of S3Context has been transferred.
  205. @retval EFI_SUCCESS The translation of S3Context to ACPI S3 Boot Script
  206. opcodes has been successfully executed or queued. (This
  207. includes the case when S3Context was empty on input and
  208. no ACPI S3 Boot Script opcodes have been necessary to
  209. produce.)
  210. @return Error codes from underlying functions.
  211. **/
  212. EFI_STATUS
  213. TransferS3ContextToBootScript (
  214. IN S3_CONTEXT *S3Context
  215. )
  216. {
  217. RETURN_STATUS Status;
  218. if (S3Context->Used == 0) {
  219. ReleaseS3Context (S3Context);
  220. return EFI_SUCCESS;
  221. }
  222. Status = QemuFwCfgS3CallWhenBootScriptReady (
  223. AppendFwCfgBootScript,
  224. S3Context,
  225. sizeof (SCRATCH_BUFFER)
  226. );
  227. return (EFI_STATUS)Status;
  228. }