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.
5 Copyright (C) 2017, Red Hat, Inc.
7 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/MemoryAllocationLib.h>
11 #include <Library/QemuFwCfgLib.h>
12 #include <Library/QemuFwCfgS3Lib.h>
14 #include "AcpiPlatform.h"
18 // Condensed structure for capturing the fw_cfg operations -- select, skip,
19 // write -- inherent in executing a QEMU_LOADER_WRITE_POINTER command.
22 UINT16 PointerItem
; // resolved from QEMU_LOADER_WRITE_POINTER.PointerFile
23 UINT8 PointerSize
; // copied as-is from QEMU_LOADER_WRITE_POINTER
24 UINT32 PointerOffset
; // copied as-is from QEMU_LOADER_WRITE_POINTER
25 UINT64 PointerValue
; // resolved from QEMU_LOADER_WRITE_POINTER.PointeeFile
26 // and QEMU_LOADER_WRITE_POINTER.PointeeOffset
27 } CONDENSED_WRITE_POINTER
;
31 // Context structure to accumulate CONDENSED_WRITE_POINTER objects from
32 // QEMU_LOADER_WRITE_POINTER commands.
34 // Any pointers in this structure own the pointed-to objects; that is, when the
35 // context structure is released, all pointed-to objects must be released too.
38 CONDENSED_WRITE_POINTER
*WritePointers
; // one array element per processed
39 // QEMU_LOADER_WRITE_POINTER
41 UINTN Allocated
; // number of elements allocated for
43 UINTN Used
; // number of elements populated in
49 // Scratch buffer, allocated in EfiReservedMemoryType type memory, for the ACPI
50 // S3 Boot Script opcodes to work on.
54 UINT64 PointerValue
; // filled in from CONDENSED_WRITE_POINTER.PointerValue
60 Allocate an S3_CONTEXT object.
62 @param[out] S3Context The allocated S3_CONTEXT object is returned
63 through this parameter.
65 @param[in] WritePointerCount Number of CONDENSED_WRITE_POINTER elements to
66 allocate room for. WritePointerCount must be
69 @retval EFI_SUCCESS Allocation successful.
71 @retval EFI_OUT_OF_RESOURCES Out of memory.
73 @retval EFI_INVALID_PARAMETER WritePointerCount is zero.
77 OUT S3_CONTEXT
**S3Context
,
78 IN UINTN WritePointerCount
84 if (WritePointerCount
== 0) {
85 return EFI_INVALID_PARAMETER
;
88 Context
= AllocateZeroPool (sizeof *Context
);
89 if (Context
== NULL
) {
90 return EFI_OUT_OF_RESOURCES
;
93 Context
->WritePointers
= AllocatePool (WritePointerCount
*
94 sizeof *Context
->WritePointers
);
95 if (Context
->WritePointers
== NULL
) {
96 Status
= EFI_OUT_OF_RESOURCES
;
100 Context
->Allocated
= WritePointerCount
;
101 *S3Context
= Context
;
112 Release an S3_CONTEXT object.
114 @param[in] S3Context The object to release.
118 IN S3_CONTEXT
*S3Context
121 FreePool (S3Context
->WritePointers
);
122 FreePool (S3Context
);
127 Save the information necessary to replicate a QEMU_LOADER_WRITE_POINTER
128 command during S3 resume, in condensed format.
130 This function is to be called from ProcessCmdWritePointer(), after all the
131 sanity checks have passed, and before the fw_cfg operations are performed.
133 @param[in,out] S3Context The S3_CONTEXT object into which the caller wants
134 to save the information that was derived from
135 QEMU_LOADER_WRITE_POINTER.
137 @param[in] PointerItem The FIRMWARE_CONFIG_ITEM that
138 QEMU_LOADER_WRITE_POINTER.PointerFile was resolved
139 to, expressed as a UINT16 value.
141 @param[in] PointerSize Copied directly from
142 QEMU_LOADER_WRITE_POINTER.PointerSize.
144 @param[in] PointerOffset Copied directly from
145 QEMU_LOADER_WRITE_POINTER.PointerOffset.
147 @param[in] PointerValue The base address of the allocated / downloaded
148 fw_cfg blob that is identified by
149 QEMU_LOADER_WRITE_POINTER.PointeeFile, plus
150 QEMU_LOADER_WRITE_POINTER.PointeeOffset.
152 @retval EFI_SUCCESS The information derived from
153 QEMU_LOADER_WRITE_POINTER has been successfully
154 absorbed into S3Context.
156 @retval EFI_OUT_OF_RESOURCES No room available in S3Context.
159 SaveCondensedWritePointerToS3Context (
160 IN OUT S3_CONTEXT
*S3Context
,
161 IN UINT16 PointerItem
,
162 IN UINT8 PointerSize
,
163 IN UINT32 PointerOffset
,
164 IN UINT64 PointerValue
167 CONDENSED_WRITE_POINTER
*Condensed
;
169 if (S3Context
->Used
== S3Context
->Allocated
) {
170 return EFI_OUT_OF_RESOURCES
;
172 Condensed
= S3Context
->WritePointers
+ S3Context
->Used
;
173 Condensed
->PointerItem
= PointerItem
;
174 Condensed
->PointerSize
= PointerSize
;
175 Condensed
->PointerOffset
= PointerOffset
;
176 Condensed
->PointerValue
= PointerValue
;
177 DEBUG ((DEBUG_VERBOSE
, "%a: 0x%04x/[0x%08x+%d] := 0x%Lx (%Lu)\n",
178 __FUNCTION__
, PointerItem
, PointerOffset
, PointerSize
, PointerValue
,
179 (UINT64
)S3Context
->Used
));
186 FW_CFG_BOOT_SCRIPT_CALLBACK_FUNCTION provided to QemuFwCfgS3Lib.
191 AppendFwCfgBootScript (
192 IN OUT VOID
*Context
, OPTIONAL
193 IN OUT VOID
*ExternalScratchBuffer
196 S3_CONTEXT
*S3Context
;
197 SCRATCH_BUFFER
*ScratchBuffer
;
201 ScratchBuffer
= ExternalScratchBuffer
;
203 for (Index
= 0; Index
< S3Context
->Used
; ++Index
) {
204 CONST CONDENSED_WRITE_POINTER
*Condensed
;
205 RETURN_STATUS Status
;
207 Condensed
= &S3Context
->WritePointers
[Index
];
209 Status
= QemuFwCfgS3ScriptSkipBytes (Condensed
->PointerItem
,
210 Condensed
->PointerOffset
);
211 if (RETURN_ERROR (Status
)) {
215 ScratchBuffer
->PointerValue
= Condensed
->PointerValue
;
216 Status
= QemuFwCfgS3ScriptWriteBytes (-1, Condensed
->PointerSize
);
217 if (RETURN_ERROR (Status
)) {
222 DEBUG ((DEBUG_VERBOSE
, "%a: boot script fragment saved\n", __FUNCTION__
));
224 ReleaseS3Context (S3Context
);
234 Translate and append the information from an S3_CONTEXT object to the ACPI S3
237 The effects of a successful call to this function cannot be undone.
239 @param[in] S3Context The S3_CONTEXT object to translate to ACPI S3 Boot
240 Script opcodes. If the function returns successfully,
241 the caller must set the S3Context pointer -- originally
242 returned by AllocateS3Context() -- immediately to NULL,
243 because the ownership of S3Context has been transfered.
245 @retval EFI_SUCCESS The translation of S3Context to ACPI S3 Boot Script
246 opcodes has been successfully executed or queued. (This
247 includes the case when S3Context was empty on input and
248 no ACPI S3 Boot Script opcodes have been necessary to
251 @return Error codes from underlying functions.
254 TransferS3ContextToBootScript (
255 IN S3_CONTEXT
*S3Context
258 RETURN_STATUS Status
;
260 if (S3Context
->Used
== 0) {
261 ReleaseS3Context (S3Context
);
265 Status
= QemuFwCfgS3CallWhenBootScriptReady (AppendFwCfgBootScript
,
266 S3Context
, sizeof (SCRATCH_BUFFER
));
267 return (EFI_STATUS
)Status
;