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 |
|
---|
5 | Copyright (C) 2017, Red Hat, Inc.
|
---|
6 |
|
---|
7 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
8 | **/
|
---|
9 |
|
---|
10 | #include <Library/MemoryAllocationLib.h>
|
---|
11 | #include <Library/QemuFwCfgLib.h>
|
---|
12 | #include <Library/QemuFwCfgS3Lib.h>
|
---|
13 |
|
---|
14 | #include "AcpiPlatform.h"
|
---|
15 |
|
---|
16 |
|
---|
17 | //
|
---|
18 | // Condensed structure for capturing the fw_cfg operations -- select, skip,
|
---|
19 | // write -- inherent in executing a QEMU_LOADER_WRITE_POINTER command.
|
---|
20 | //
|
---|
21 | typedef struct {
|
---|
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;
|
---|
28 |
|
---|
29 |
|
---|
30 | //
|
---|
31 | // Context structure to accumulate CONDENSED_WRITE_POINTER objects from
|
---|
32 | // QEMU_LOADER_WRITE_POINTER commands.
|
---|
33 | //
|
---|
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.
|
---|
36 | //
|
---|
37 | struct S3_CONTEXT {
|
---|
38 | CONDENSED_WRITE_POINTER *WritePointers; // one array element per processed
|
---|
39 | // QEMU_LOADER_WRITE_POINTER
|
---|
40 | // command
|
---|
41 | UINTN Allocated; // number of elements allocated for
|
---|
42 | // WritePointers
|
---|
43 | UINTN Used; // number of elements populated in
|
---|
44 | // WritePointers
|
---|
45 | };
|
---|
46 |
|
---|
47 |
|
---|
48 | //
|
---|
49 | // Scratch buffer, allocated in EfiReservedMemoryType type memory, for the ACPI
|
---|
50 | // S3 Boot Script opcodes to work on.
|
---|
51 | //
|
---|
52 | #pragma pack (1)
|
---|
53 | typedef union {
|
---|
54 | UINT64 PointerValue; // filled in from CONDENSED_WRITE_POINTER.PointerValue
|
---|
55 | } SCRATCH_BUFFER;
|
---|
56 | #pragma pack ()
|
---|
57 |
|
---|
58 |
|
---|
59 | /**
|
---|
60 | Allocate an S3_CONTEXT object.
|
---|
61 |
|
---|
62 | @param[out] S3Context The allocated S3_CONTEXT object is returned
|
---|
63 | through this parameter.
|
---|
64 |
|
---|
65 | @param[in] WritePointerCount Number of CONDENSED_WRITE_POINTER elements to
|
---|
66 | allocate room for. WritePointerCount must be
|
---|
67 | positive.
|
---|
68 |
|
---|
69 | @retval EFI_SUCCESS Allocation successful.
|
---|
70 |
|
---|
71 | @retval EFI_OUT_OF_RESOURCES Out of memory.
|
---|
72 |
|
---|
73 | @retval EFI_INVALID_PARAMETER WritePointerCount is zero.
|
---|
74 | **/
|
---|
75 | EFI_STATUS
|
---|
76 | AllocateS3Context (
|
---|
77 | OUT S3_CONTEXT **S3Context,
|
---|
78 | IN UINTN WritePointerCount
|
---|
79 | )
|
---|
80 | {
|
---|
81 | EFI_STATUS Status;
|
---|
82 | S3_CONTEXT *Context;
|
---|
83 |
|
---|
84 | if (WritePointerCount == 0) {
|
---|
85 | return EFI_INVALID_PARAMETER;
|
---|
86 | }
|
---|
87 |
|
---|
88 | Context = AllocateZeroPool (sizeof *Context);
|
---|
89 | if (Context == NULL) {
|
---|
90 | return EFI_OUT_OF_RESOURCES;
|
---|
91 | }
|
---|
92 |
|
---|
93 | Context->WritePointers = AllocatePool (WritePointerCount *
|
---|
94 | sizeof *Context->WritePointers);
|
---|
95 | if (Context->WritePointers == NULL) {
|
---|
96 | Status = EFI_OUT_OF_RESOURCES;
|
---|
97 | goto FreeContext;
|
---|
98 | }
|
---|
99 |
|
---|
100 | Context->Allocated = WritePointerCount;
|
---|
101 | *S3Context = Context;
|
---|
102 | return EFI_SUCCESS;
|
---|
103 |
|
---|
104 | FreeContext:
|
---|
105 | FreePool (Context);
|
---|
106 |
|
---|
107 | return Status;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | /**
|
---|
112 | Release an S3_CONTEXT object.
|
---|
113 |
|
---|
114 | @param[in] S3Context The object to release.
|
---|
115 | **/
|
---|
116 | VOID
|
---|
117 | ReleaseS3Context (
|
---|
118 | IN S3_CONTEXT *S3Context
|
---|
119 | )
|
---|
120 | {
|
---|
121 | FreePool (S3Context->WritePointers);
|
---|
122 | FreePool (S3Context);
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | /**
|
---|
127 | Save the information necessary to replicate a QEMU_LOADER_WRITE_POINTER
|
---|
128 | command during S3 resume, in condensed format.
|
---|
129 |
|
---|
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.
|
---|
132 |
|
---|
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.
|
---|
136 |
|
---|
137 | @param[in] PointerItem The FIRMWARE_CONFIG_ITEM that
|
---|
138 | QEMU_LOADER_WRITE_POINTER.PointerFile was resolved
|
---|
139 | to, expressed as a UINT16 value.
|
---|
140 |
|
---|
141 | @param[in] PointerSize Copied directly from
|
---|
142 | QEMU_LOADER_WRITE_POINTER.PointerSize.
|
---|
143 |
|
---|
144 | @param[in] PointerOffset Copied directly from
|
---|
145 | QEMU_LOADER_WRITE_POINTER.PointerOffset.
|
---|
146 |
|
---|
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.
|
---|
151 |
|
---|
152 | @retval EFI_SUCCESS The information derived from
|
---|
153 | QEMU_LOADER_WRITE_POINTER has been successfully
|
---|
154 | absorbed into S3Context.
|
---|
155 |
|
---|
156 | @retval EFI_OUT_OF_RESOURCES No room available in S3Context.
|
---|
157 | **/
|
---|
158 | EFI_STATUS
|
---|
159 | SaveCondensedWritePointerToS3Context (
|
---|
160 | IN OUT S3_CONTEXT *S3Context,
|
---|
161 | IN UINT16 PointerItem,
|
---|
162 | IN UINT8 PointerSize,
|
---|
163 | IN UINT32 PointerOffset,
|
---|
164 | IN UINT64 PointerValue
|
---|
165 | )
|
---|
166 | {
|
---|
167 | CONDENSED_WRITE_POINTER *Condensed;
|
---|
168 |
|
---|
169 | if (S3Context->Used == S3Context->Allocated) {
|
---|
170 | return EFI_OUT_OF_RESOURCES;
|
---|
171 | }
|
---|
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));
|
---|
180 | ++S3Context->Used;
|
---|
181 | return EFI_SUCCESS;
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | /**
|
---|
186 | FW_CFG_BOOT_SCRIPT_CALLBACK_FUNCTION provided to QemuFwCfgS3Lib.
|
---|
187 | **/
|
---|
188 | STATIC
|
---|
189 | VOID
|
---|
190 | EFIAPI
|
---|
191 | AppendFwCfgBootScript (
|
---|
192 | IN OUT VOID *Context, OPTIONAL
|
---|
193 | IN OUT VOID *ExternalScratchBuffer
|
---|
194 | )
|
---|
195 | {
|
---|
196 | S3_CONTEXT *S3Context;
|
---|
197 | SCRATCH_BUFFER *ScratchBuffer;
|
---|
198 | UINTN Index;
|
---|
199 |
|
---|
200 | S3Context = Context;
|
---|
201 | ScratchBuffer = ExternalScratchBuffer;
|
---|
202 |
|
---|
203 | for (Index = 0; Index < S3Context->Used; ++Index) {
|
---|
204 | CONST CONDENSED_WRITE_POINTER *Condensed;
|
---|
205 | RETURN_STATUS Status;
|
---|
206 |
|
---|
207 | Condensed = &S3Context->WritePointers[Index];
|
---|
208 |
|
---|
209 | Status = QemuFwCfgS3ScriptSkipBytes (Condensed->PointerItem,
|
---|
210 | Condensed->PointerOffset);
|
---|
211 | if (RETURN_ERROR (Status)) {
|
---|
212 | goto FatalError;
|
---|
213 | }
|
---|
214 |
|
---|
215 | ScratchBuffer->PointerValue = Condensed->PointerValue;
|
---|
216 | Status = QemuFwCfgS3ScriptWriteBytes (-1, Condensed->PointerSize);
|
---|
217 | if (RETURN_ERROR (Status)) {
|
---|
218 | goto FatalError;
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | DEBUG ((DEBUG_VERBOSE, "%a: boot script fragment saved\n", __FUNCTION__));
|
---|
223 |
|
---|
224 | ReleaseS3Context (S3Context);
|
---|
225 | return;
|
---|
226 |
|
---|
227 | FatalError:
|
---|
228 | ASSERT (FALSE);
|
---|
229 | CpuDeadLoop ();
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | /**
|
---|
234 | Translate and append the information from an S3_CONTEXT object to the ACPI S3
|
---|
235 | Boot Script.
|
---|
236 |
|
---|
237 | The effects of a successful call to this function cannot be undone.
|
---|
238 |
|
---|
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 transferred.
|
---|
244 |
|
---|
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
|
---|
249 | produce.)
|
---|
250 |
|
---|
251 | @return Error codes from underlying functions.
|
---|
252 | **/
|
---|
253 | EFI_STATUS
|
---|
254 | TransferS3ContextToBootScript (
|
---|
255 | IN S3_CONTEXT *S3Context
|
---|
256 | )
|
---|
257 | {
|
---|
258 | RETURN_STATUS Status;
|
---|
259 |
|
---|
260 | if (S3Context->Used == 0) {
|
---|
261 | ReleaseS3Context (S3Context);
|
---|
262 | return EFI_SUCCESS;
|
---|
263 | }
|
---|
264 |
|
---|
265 | Status = QemuFwCfgS3CallWhenBootScriptReady (AppendFwCfgBootScript,
|
---|
266 | S3Context, sizeof (SCRATCH_BUFFER));
|
---|
267 | return (EFI_STATUS)Status;
|
---|
268 | }
|
---|