]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/AcpiPlatformDxe/BootScript.c
NetworkPkg: Refine type cast for pointer subtraction
[mirror_edk2.git] / OvmfPkg / AcpiPlatformDxe / BootScript.c
CommitLineData
df73df13
LE
1/** @file\r
2 Append an ACPI S3 Boot Script fragment from the QEMU_LOADER_WRITE_POINTER\r
3 commands of QEMU's fully processed table linker/loader script.\r
4\r
5 Copyright (C) 2017, Red Hat, Inc.\r
6\r
7 This program and the accompanying materials are licensed and made available\r
8 under the terms and conditions of the BSD License which accompanies this\r
9 distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
13 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14**/\r
15\r
16#include <Library/MemoryAllocationLib.h>\r
17#include <Library/QemuFwCfgLib.h>\r
18#include <Protocol/S3SaveState.h>\r
19\r
20#include "AcpiPlatform.h"\r
21\r
22\r
23//\r
24// Condensed structure for capturing the fw_cfg operations -- select, skip,\r
25// write -- inherent in executing a QEMU_LOADER_WRITE_POINTER command.\r
26//\r
27typedef struct {\r
28 UINT16 PointerItem; // resolved from QEMU_LOADER_WRITE_POINTER.PointerFile\r
29 UINT8 PointerSize; // copied as-is from QEMU_LOADER_WRITE_POINTER\r
30 UINT32 PointerOffset; // copied as-is from QEMU_LOADER_WRITE_POINTER\r
31 UINT64 PointerValue; // resolved from QEMU_LOADER_WRITE_POINTER.PointeeFile\r
6025488d 32 // and QEMU_LOADER_WRITE_POINTER.PointeeOffset\r
df73df13
LE
33} CONDENSED_WRITE_POINTER;\r
34\r
35\r
36//\r
37// Context structure to accumulate CONDENSED_WRITE_POINTER objects from\r
38// QEMU_LOADER_WRITE_POINTER commands.\r
39//\r
40// Any pointers in this structure own the pointed-to objects; that is, when the\r
41// context structure is released, all pointed-to objects must be released too.\r
42//\r
43struct S3_CONTEXT {\r
44 CONDENSED_WRITE_POINTER *WritePointers; // one array element per processed\r
45 // QEMU_LOADER_WRITE_POINTER\r
46 // command\r
47 UINTN Allocated; // number of elements allocated for\r
48 // WritePointers\r
49 UINTN Used; // number of elements populated in\r
50 // WritePointers\r
51};\r
52\r
53\r
54//\r
55// Scratch buffer, allocated in EfiReservedMemoryType type memory, for the ACPI\r
56// S3 Boot Script opcodes to work on. We use the buffer to compose and to\r
57// replay several fw_cfg select+skip and write operations, using the DMA access\r
58// method. The fw_cfg operations will implement the actions dictated by\r
59// CONDENSED_WRITE_POINTER objects.\r
60//\r
61#pragma pack (1)\r
62typedef struct {\r
63 FW_CFG_DMA_ACCESS Access; // filled in from\r
64 // CONDENSED_WRITE_POINTER.PointerItem,\r
65 // CONDENSED_WRITE_POINTER.PointerSize,\r
66 // CONDENSED_WRITE_POINTER.PointerOffset\r
67 UINT64 PointerValue; // filled in from\r
68 // CONDENSED_WRITE_POINTER.PointerValue\r
69} SCRATCH_BUFFER;\r
70#pragma pack ()\r
71\r
72\r
73/**\r
74 Allocate an S3_CONTEXT object.\r
75\r
76 @param[out] S3Context The allocated S3_CONTEXT object is returned\r
77 through this parameter.\r
78\r
79 @param[in] WritePointerCount Number of CONDENSED_WRITE_POINTER elements to\r
80 allocate room for. WritePointerCount must be\r
81 positive.\r
82\r
83 @retval EFI_SUCCESS Allocation successful.\r
84\r
85 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
86\r
87 @retval EFI_INVALID_PARAMETER WritePointerCount is zero.\r
88**/\r
89EFI_STATUS\r
90AllocateS3Context (\r
91 OUT S3_CONTEXT **S3Context,\r
92 IN UINTN WritePointerCount\r
93 )\r
94{\r
95 EFI_STATUS Status;\r
96 S3_CONTEXT *Context;\r
97\r
98 if (WritePointerCount == 0) {\r
99 return EFI_INVALID_PARAMETER;\r
100 }\r
101\r
102 Context = AllocateZeroPool (sizeof *Context);\r
103 if (Context == NULL) {\r
104 return EFI_OUT_OF_RESOURCES;\r
105 }\r
106\r
107 Context->WritePointers = AllocatePool (WritePointerCount *\r
108 sizeof *Context->WritePointers);\r
109 if (Context->WritePointers == NULL) {\r
110 Status = EFI_OUT_OF_RESOURCES;\r
111 goto FreeContext;\r
112 }\r
113\r
114 Context->Allocated = WritePointerCount;\r
115 *S3Context = Context;\r
116 return EFI_SUCCESS;\r
117\r
118FreeContext:\r
119 FreePool (Context);\r
120\r
121 return Status;\r
122}\r
123\r
124\r
125/**\r
126 Release an S3_CONTEXT object.\r
127\r
128 @param[in] S3Context The object to release.\r
129**/\r
130VOID\r
131ReleaseS3Context (\r
132 IN S3_CONTEXT *S3Context\r
133 )\r
134{\r
135 FreePool (S3Context->WritePointers);\r
136 FreePool (S3Context);\r
137}\r
138\r
139\r
140/**\r
141 Save the information necessary to replicate a QEMU_LOADER_WRITE_POINTER\r
142 command during S3 resume, in condensed format.\r
143\r
144 This function is to be called from ProcessCmdWritePointer(), after all the\r
145 sanity checks have passed, and before the fw_cfg operations are performed.\r
146\r
147 @param[in,out] S3Context The S3_CONTEXT object into which the caller wants\r
148 to save the information that was derived from\r
149 QEMU_LOADER_WRITE_POINTER.\r
150\r
151 @param[in] PointerItem The FIRMWARE_CONFIG_ITEM that\r
152 QEMU_LOADER_WRITE_POINTER.PointerFile was resolved\r
153 to, expressed as a UINT16 value.\r
154\r
155 @param[in] PointerSize Copied directly from\r
156 QEMU_LOADER_WRITE_POINTER.PointerSize.\r
157\r
158 @param[in] PointerOffset Copied directly from\r
159 QEMU_LOADER_WRITE_POINTER.PointerOffset.\r
160\r
161 @param[in] PointerValue The base address of the allocated / downloaded\r
162 fw_cfg blob that is identified by\r
6025488d
LE
163 QEMU_LOADER_WRITE_POINTER.PointeeFile, plus\r
164 QEMU_LOADER_WRITE_POINTER.PointeeOffset.\r
df73df13
LE
165\r
166 @retval EFI_SUCCESS The information derived from\r
167 QEMU_LOADER_WRITE_POINTER has been successfully\r
168 absorbed into S3Context.\r
169\r
170 @retval EFI_OUT_OF_RESOURCES No room available in S3Context.\r
171**/\r
172EFI_STATUS\r
173SaveCondensedWritePointerToS3Context (\r
174 IN OUT S3_CONTEXT *S3Context,\r
175 IN UINT16 PointerItem,\r
176 IN UINT8 PointerSize,\r
177 IN UINT32 PointerOffset,\r
178 IN UINT64 PointerValue\r
179 )\r
180{\r
181 CONDENSED_WRITE_POINTER *Condensed;\r
182\r
183 if (S3Context->Used == S3Context->Allocated) {\r
184 return EFI_OUT_OF_RESOURCES;\r
185 }\r
186 Condensed = S3Context->WritePointers + S3Context->Used;\r
187 Condensed->PointerItem = PointerItem;\r
188 Condensed->PointerSize = PointerSize;\r
189 Condensed->PointerOffset = PointerOffset;\r
190 Condensed->PointerValue = PointerValue;\r
191 DEBUG ((DEBUG_VERBOSE, "%a: 0x%04x/[0x%08x+%d] := 0x%Lx (%Lu)\n",\r
192 __FUNCTION__, PointerItem, PointerOffset, PointerSize, PointerValue,\r
193 (UINT64)S3Context->Used));\r
194 ++S3Context->Used;\r
195 return EFI_SUCCESS;\r
196}\r
197\r
198\r
199/**\r
200 Translate and append the information from an S3_CONTEXT object to the ACPI S3\r
201 Boot Script.\r
202\r
203 The effects of a successful call to this function cannot be undone.\r
204\r
205 @param[in] S3Context The S3_CONTEXT object to translate to ACPI S3 Boot\r
206 Script opcodes.\r
207\r
208 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
209\r
210 @retval EFI_SUCCESS The translation of S3Context to ACPI S3 Boot\r
211 Script opcodes has been successful.\r
212\r
213 @return Error codes from underlying functions.\r
214**/\r
215EFI_STATUS\r
216TransferS3ContextToBootScript (\r
217 IN CONST S3_CONTEXT *S3Context\r
218 )\r
219{\r
220 EFI_STATUS Status;\r
221 EFI_S3_SAVE_STATE_PROTOCOL *S3SaveState;\r
222 SCRATCH_BUFFER *ScratchBuffer;\r
223 FW_CFG_DMA_ACCESS *Access;\r
224 UINT64 BigEndianAddressOfAccess;\r
225 UINT32 ControlPollData;\r
226 UINT32 ControlPollMask;\r
227 UINTN Index;\r
228\r
229 //\r
230 // If the following protocol lookup fails, it shall not happen due to an\r
231 // unexpected DXE driver dispatch order.\r
232 //\r
233 // Namely, this function is only invoked on QEMU. Therefore it is only\r
234 // reached after Platform BDS signals gRootBridgesConnectedEventGroupGuid\r
235 // (see OnRootBridgesConnected() in "EntryPoint.c"). Hence, because\r
236 // TransferS3ContextToBootScript() is invoked in BDS, all DXE drivers,\r
237 // including S3SaveStateDxe (producing EFI_S3_SAVE_STATE_PROTOCOL), have been\r
238 // dispatched by the time we get here. (S3SaveStateDxe is not expected to\r
239 // have any stricter-than-TRUE DEPEX -- not a DEPEX that gets unblocked only\r
240 // within BDS anyway.)\r
241 //\r
242 // Reaching this function also depends on QemuFwCfgS3Enabled(). That implies\r
243 // S3SaveStateDxe has not exited immediately due to S3 being disabled. Thus\r
244 // EFI_S3_SAVE_STATE_PROTOCOL can only be missing for genuinely unforeseeable\r
245 // reasons.\r
246 //\r
247 Status = gBS->LocateProtocol (&gEfiS3SaveStateProtocolGuid,\r
248 NULL /* Registration */, (VOID **)&S3SaveState);\r
249 if (EFI_ERROR (Status)) {\r
250 DEBUG ((DEBUG_ERROR, "%a: LocateProtocol(): %r\n", __FUNCTION__, Status));\r
251 return Status;\r
252 }\r
253\r
254 ScratchBuffer = AllocateReservedPool (sizeof *ScratchBuffer);\r
255 if (ScratchBuffer == NULL) {\r
256 return EFI_OUT_OF_RESOURCES;\r
257 }\r
258\r
259 //\r
260 // Set up helper variables that we'll use identically for all\r
261 // CONDENSED_WRITE_POINTER elements.\r
262 //\r
263 Access = &ScratchBuffer->Access;\r
264 BigEndianAddressOfAccess = SwapBytes64 ((UINTN)Access);\r
265 ControlPollData = 0;\r
266 ControlPollMask = MAX_UINT32;\r
267\r
268 //\r
269 // For each CONDENSED_WRITE_POINTER, we need six ACPI S3 Boot Script opcodes:\r
270 // (1) restore an FW_CFG_DMA_ACCESS object in reserved memory that selects\r
271 // the writeable fw_cfg file PointerFile (through PointerItem), and skips\r
272 // to PointerOffset in it,\r
273 // (2) call QEMU with the FW_CFG_DMA_ACCESS object,\r
274 // (3) wait for the select+skip to finish,\r
275 // (4) restore a SCRATCH_BUFFER object in reserved memory that writes\r
6025488d
LE
276 // PointerValue (base address of the allocated / downloaded PointeeFile,\r
277 // plus PointeeOffset), of size PointerSize, into the fw_cfg file\r
278 // selected in (1), at the offset sought to in (1),\r
df73df13
LE
279 // (5) call QEMU with the FW_CFG_DMA_ACCESS object,\r
280 // (6) wait for the write to finish.\r
281 //\r
282 // EFI_S3_SAVE_STATE_PROTOCOL does not allow rolling back opcode additions,\r
283 // therefore we treat any failure here as fatal.\r
284 //\r
285 for (Index = 0; Index < S3Context->Used; ++Index) {\r
286 CONST CONDENSED_WRITE_POINTER *Condensed;\r
287\r
288 Condensed = &S3Context->WritePointers[Index];\r
289\r
290 //\r
291 // (1) restore an FW_CFG_DMA_ACCESS object in reserved memory that selects\r
292 // the writeable fw_cfg file PointerFile (through PointerItem), and\r
293 // skips to PointerOffset in it,\r
294 //\r
295 Access->Control = SwapBytes32 ((UINT32)Condensed->PointerItem << 16 |\r
296 FW_CFG_DMA_CTL_SELECT | FW_CFG_DMA_CTL_SKIP);\r
297 Access->Length = SwapBytes32 (Condensed->PointerOffset);\r
298 Access->Address = 0;\r
299 Status = S3SaveState->Write (\r
300 S3SaveState, // This\r
301 EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE, // OpCode\r
302 EfiBootScriptWidthUint8, // Width\r
303 (UINT64)(UINTN)Access, // Address\r
304 sizeof *Access, // Count\r
305 Access // Buffer\r
306 );\r
307 if (EFI_ERROR (Status)) {\r
308 DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 1: %r\n", __FUNCTION__,\r
309 (UINT64)Index, Status));\r
310 goto FatalError;\r
311 }\r
312\r
313 //\r
314 // (2) call QEMU with the FW_CFG_DMA_ACCESS object,\r
315 //\r
316 Status = S3SaveState->Write (\r
317 S3SaveState, // This\r
318 EFI_BOOT_SCRIPT_IO_WRITE_OPCODE, // OpCode\r
319 EfiBootScriptWidthUint32, // Width\r
ed1a2d42 320 (UINT64)FW_CFG_IO_DMA_ADDRESS, // Address\r
df73df13
LE
321 (UINTN)2, // Count\r
322 &BigEndianAddressOfAccess // Buffer\r
323 );\r
324 if (EFI_ERROR (Status)) {\r
325 DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 2: %r\n", __FUNCTION__,\r
326 (UINT64)Index, Status));\r
327 goto FatalError;\r
328 }\r
329\r
330 //\r
331 // (3) wait for the select+skip to finish,\r
332 //\r
333 Status = S3SaveState->Write (\r
334 S3SaveState, // This\r
335 EFI_BOOT_SCRIPT_MEM_POLL_OPCODE, // OpCode\r
336 EfiBootScriptWidthUint32, // Width\r
337 (UINT64)(UINTN)&Access->Control, // Address\r
338 &ControlPollData, // Data\r
339 &ControlPollMask, // DataMask\r
340 MAX_UINT64 // Delay\r
341 );\r
342 if (EFI_ERROR (Status)) {\r
343 DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 3: %r\n", __FUNCTION__,\r
344 (UINT64)Index, Status));\r
345 goto FatalError;\r
346 }\r
347\r
348 //\r
349 // (4) restore a SCRATCH_BUFFER object in reserved memory that writes\r
350 // PointerValue (base address of the allocated / downloaded\r
6025488d
LE
351 // PointeeFile, plus PointeeOffset), of size PointerSize, into the\r
352 // fw_cfg file selected in (1), at the offset sought to in (1),\r
df73df13
LE
353 //\r
354 Access->Control = SwapBytes32 (FW_CFG_DMA_CTL_WRITE);\r
355 Access->Length = SwapBytes32 (Condensed->PointerSize);\r
356 Access->Address = SwapBytes64 ((UINTN)&ScratchBuffer->PointerValue);\r
357 ScratchBuffer->PointerValue = Condensed->PointerValue;\r
358 Status = S3SaveState->Write (\r
359 S3SaveState, // This\r
360 EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE, // OpCode\r
361 EfiBootScriptWidthUint8, // Width\r
362 (UINT64)(UINTN)ScratchBuffer, // Address\r
363 sizeof *ScratchBuffer, // Count\r
364 ScratchBuffer // Buffer\r
365 );\r
366 if (EFI_ERROR (Status)) {\r
367 DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 4: %r\n", __FUNCTION__,\r
368 (UINT64)Index, Status));\r
369 goto FatalError;\r
370 }\r
371\r
372 //\r
373 // (5) call QEMU with the FW_CFG_DMA_ACCESS object,\r
374 //\r
375 Status = S3SaveState->Write (\r
376 S3SaveState, // This\r
377 EFI_BOOT_SCRIPT_IO_WRITE_OPCODE, // OpCode\r
378 EfiBootScriptWidthUint32, // Width\r
ed1a2d42 379 (UINT64)FW_CFG_IO_DMA_ADDRESS, // Address\r
df73df13
LE
380 (UINTN)2, // Count\r
381 &BigEndianAddressOfAccess // Buffer\r
382 );\r
383 if (EFI_ERROR (Status)) {\r
384 DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 5: %r\n", __FUNCTION__,\r
385 (UINT64)Index, Status));\r
386 goto FatalError;\r
387 }\r
388\r
389 //\r
390 // (6) wait for the write to finish.\r
391 //\r
392 Status = S3SaveState->Write (\r
393 S3SaveState, // This\r
394 EFI_BOOT_SCRIPT_MEM_POLL_OPCODE, // OpCode\r
395 EfiBootScriptWidthUint32, // Width\r
396 (UINT64)(UINTN)&Access->Control, // Address\r
397 &ControlPollData, // Data\r
398 &ControlPollMask, // DataMask\r
399 MAX_UINT64 // Delay\r
400 );\r
401 if (EFI_ERROR (Status)) {\r
402 DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 6: %r\n", __FUNCTION__,\r
403 (UINT64)Index, Status));\r
404 goto FatalError;\r
405 }\r
406 }\r
407\r
408 DEBUG ((DEBUG_VERBOSE, "%a: boot script fragment saved, ScratchBuffer=%p\n",\r
409 __FUNCTION__, (VOID *)ScratchBuffer));\r
410 return EFI_SUCCESS;\r
411\r
412FatalError:\r
413 ASSERT (FALSE);\r
414 CpuDeadLoop ();\r
415 return Status;\r
416}\r