--- /dev/null
+/** @file\r
+ Append an ACPI S3 Boot Script fragment from the QEMU_LOADER_WRITE_POINTER\r
+ commands of QEMU's fully processed table linker/loader script.\r
+\r
+ Copyright (C) 2017-2021, Red Hat, Inc.\r
+\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#include <Library/BaseLib.h> // CpuDeadLoop()\r
+#include <Library/DebugLib.h> // DEBUG()\r
+#include <Library/MemoryAllocationLib.h> // AllocatePool()\r
+#include <Library/QemuFwCfgS3Lib.h> // QemuFwCfgS3ScriptSkipBytes()\r
+\r
+#include "AcpiPlatform.h"\r
+\r
+\r
+//\r
+// Condensed structure for capturing the fw_cfg operations -- select, skip,\r
+// write -- inherent in executing a QEMU_LOADER_WRITE_POINTER command.\r
+//\r
+typedef struct {\r
+ UINT16 PointerItem; // resolved from QEMU_LOADER_WRITE_POINTER.PointerFile\r
+ UINT8 PointerSize; // copied as-is from QEMU_LOADER_WRITE_POINTER\r
+ UINT32 PointerOffset; // copied as-is from QEMU_LOADER_WRITE_POINTER\r
+ UINT64 PointerValue; // resolved from QEMU_LOADER_WRITE_POINTER.PointeeFile\r
+ // and QEMU_LOADER_WRITE_POINTER.PointeeOffset\r
+} CONDENSED_WRITE_POINTER;\r
+\r
+\r
+//\r
+// Context structure to accumulate CONDENSED_WRITE_POINTER objects from\r
+// QEMU_LOADER_WRITE_POINTER commands.\r
+//\r
+// Any pointers in this structure own the pointed-to objects; that is, when the\r
+// context structure is released, all pointed-to objects must be released too.\r
+//\r
+struct S3_CONTEXT {\r
+ CONDENSED_WRITE_POINTER *WritePointers; // one array element per processed\r
+ // QEMU_LOADER_WRITE_POINTER\r
+ // command\r
+ UINTN Allocated; // number of elements allocated for\r
+ // WritePointers\r
+ UINTN Used; // number of elements populated in\r
+ // WritePointers\r
+};\r
+\r
+\r
+//\r
+// Scratch buffer, allocated in EfiReservedMemoryType type memory, for the ACPI\r
+// S3 Boot Script opcodes to work on.\r
+//\r
+#pragma pack (1)\r
+typedef union {\r
+ UINT64 PointerValue; // filled in from CONDENSED_WRITE_POINTER.PointerValue\r
+} SCRATCH_BUFFER;\r
+#pragma pack ()\r
+\r
+\r
+/**\r
+ Allocate an S3_CONTEXT object.\r
+\r
+ @param[out] S3Context The allocated S3_CONTEXT object is returned\r
+ through this parameter.\r
+\r
+ @param[in] WritePointerCount Number of CONDENSED_WRITE_POINTER elements to\r
+ allocate room for. WritePointerCount must be\r
+ positive.\r
+\r
+ @retval EFI_SUCCESS Allocation successful.\r
+\r
+ @retval EFI_OUT_OF_RESOURCES Out of memory.\r
+\r
+ @retval EFI_INVALID_PARAMETER WritePointerCount is zero.\r
+**/\r
+EFI_STATUS\r
+AllocateS3Context (\r
+ OUT S3_CONTEXT **S3Context,\r
+ IN UINTN WritePointerCount\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ S3_CONTEXT *Context;\r
+\r
+ if (WritePointerCount == 0) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ Context = AllocateZeroPool (sizeof *Context);\r
+ if (Context == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ Context->WritePointers = AllocatePool (WritePointerCount *\r
+ sizeof *Context->WritePointers);\r
+ if (Context->WritePointers == NULL) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto FreeContext;\r
+ }\r
+\r
+ Context->Allocated = WritePointerCount;\r
+ *S3Context = Context;\r
+ return EFI_SUCCESS;\r
+\r
+FreeContext:\r
+ FreePool (Context);\r
+\r
+ return Status;\r
+}\r
+\r
+\r
+/**\r
+ Release an S3_CONTEXT object.\r
+\r
+ @param[in] S3Context The object to release.\r
+**/\r
+VOID\r
+ReleaseS3Context (\r
+ IN S3_CONTEXT *S3Context\r
+ )\r
+{\r
+ FreePool (S3Context->WritePointers);\r
+ FreePool (S3Context);\r
+}\r
+\r
+\r
+/**\r
+ Save the information necessary to replicate a QEMU_LOADER_WRITE_POINTER\r
+ command during S3 resume, in condensed format.\r
+\r
+ This function is to be called from ProcessCmdWritePointer(), after all the\r
+ sanity checks have passed, and before the fw_cfg operations are performed.\r
+\r
+ @param[in,out] S3Context The S3_CONTEXT object into which the caller wants\r
+ to save the information that was derived from\r
+ QEMU_LOADER_WRITE_POINTER.\r
+\r
+ @param[in] PointerItem The FIRMWARE_CONFIG_ITEM that\r
+ QEMU_LOADER_WRITE_POINTER.PointerFile was resolved\r
+ to, expressed as a UINT16 value.\r
+\r
+ @param[in] PointerSize Copied directly from\r
+ QEMU_LOADER_WRITE_POINTER.PointerSize.\r
+\r
+ @param[in] PointerOffset Copied directly from\r
+ QEMU_LOADER_WRITE_POINTER.PointerOffset.\r
+\r
+ @param[in] PointerValue The base address of the allocated / downloaded\r
+ fw_cfg blob that is identified by\r
+ QEMU_LOADER_WRITE_POINTER.PointeeFile, plus\r
+ QEMU_LOADER_WRITE_POINTER.PointeeOffset.\r
+\r
+ @retval EFI_SUCCESS The information derived from\r
+ QEMU_LOADER_WRITE_POINTER has been successfully\r
+ absorbed into S3Context.\r
+\r
+ @retval EFI_OUT_OF_RESOURCES No room available in S3Context.\r
+**/\r
+EFI_STATUS\r
+SaveCondensedWritePointerToS3Context (\r
+ IN OUT S3_CONTEXT *S3Context,\r
+ IN UINT16 PointerItem,\r
+ IN UINT8 PointerSize,\r
+ IN UINT32 PointerOffset,\r
+ IN UINT64 PointerValue\r
+ )\r
+{\r
+ CONDENSED_WRITE_POINTER *Condensed;\r
+\r
+ if (S3Context->Used == S3Context->Allocated) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ Condensed = S3Context->WritePointers + S3Context->Used;\r
+ Condensed->PointerItem = PointerItem;\r
+ Condensed->PointerSize = PointerSize;\r
+ Condensed->PointerOffset = PointerOffset;\r
+ Condensed->PointerValue = PointerValue;\r
+ DEBUG ((DEBUG_VERBOSE, "%a: 0x%04x/[0x%08x+%d] := 0x%Lx (%Lu)\n",\r
+ __FUNCTION__, PointerItem, PointerOffset, PointerSize, PointerValue,\r
+ (UINT64)S3Context->Used));\r
+ ++S3Context->Used;\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+ FW_CFG_BOOT_SCRIPT_CALLBACK_FUNCTION provided to QemuFwCfgS3Lib.\r
+**/\r
+STATIC\r
+VOID\r
+EFIAPI\r
+AppendFwCfgBootScript (\r
+ IN OUT VOID *Context, OPTIONAL\r
+ IN OUT VOID *ExternalScratchBuffer\r
+ )\r
+{\r
+ S3_CONTEXT *S3Context;\r
+ SCRATCH_BUFFER *ScratchBuffer;\r
+ UINTN Index;\r
+\r
+ S3Context = Context;\r
+ ScratchBuffer = ExternalScratchBuffer;\r
+\r
+ for (Index = 0; Index < S3Context->Used; ++Index) {\r
+ CONST CONDENSED_WRITE_POINTER *Condensed;\r
+ RETURN_STATUS Status;\r
+\r
+ Condensed = &S3Context->WritePointers[Index];\r
+\r
+ Status = QemuFwCfgS3ScriptSkipBytes (Condensed->PointerItem,\r
+ Condensed->PointerOffset);\r
+ if (RETURN_ERROR (Status)) {\r
+ goto FatalError;\r
+ }\r
+\r
+ ScratchBuffer->PointerValue = Condensed->PointerValue;\r
+ Status = QemuFwCfgS3ScriptWriteBytes (-1, Condensed->PointerSize);\r
+ if (RETURN_ERROR (Status)) {\r
+ goto FatalError;\r
+ }\r
+ }\r
+\r
+ DEBUG ((DEBUG_VERBOSE, "%a: boot script fragment saved\n", __FUNCTION__));\r
+\r
+ ReleaseS3Context (S3Context);\r
+ return;\r
+\r
+FatalError:\r
+ ASSERT (FALSE);\r
+ CpuDeadLoop ();\r
+}\r
+\r
+\r
+/**\r
+ Translate and append the information from an S3_CONTEXT object to the ACPI S3\r
+ Boot Script.\r
+\r
+ The effects of a successful call to this function cannot be undone.\r
+\r
+ @param[in] S3Context The S3_CONTEXT object to translate to ACPI S3 Boot\r
+ Script opcodes. If the function returns successfully,\r
+ the caller must set the S3Context pointer -- originally\r
+ returned by AllocateS3Context() -- immediately to NULL,\r
+ because the ownership of S3Context has been transferred.\r
+\r
+ @retval EFI_SUCCESS The translation of S3Context to ACPI S3 Boot Script\r
+ opcodes has been successfully executed or queued. (This\r
+ includes the case when S3Context was empty on input and\r
+ no ACPI S3 Boot Script opcodes have been necessary to\r
+ produce.)\r
+\r
+ @return Error codes from underlying functions.\r
+**/\r
+EFI_STATUS\r
+TransferS3ContextToBootScript (\r
+ IN S3_CONTEXT *S3Context\r
+ )\r
+{\r
+ RETURN_STATUS Status;\r
+\r
+ if (S3Context->Used == 0) {\r
+ ReleaseS3Context (S3Context);\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ Status = QemuFwCfgS3CallWhenBootScriptReady (AppendFwCfgBootScript,\r
+ S3Context, sizeof (SCRATCH_BUFFER));\r
+ return (EFI_STATUS)Status;\r
+}\r