OvmfPkg/QemuFwCfgLib: Suppress GCC49 IA32 build failure
[mirror_edk2.git] / OvmfPkg / Library / QemuFwCfgLib / QemuFwCfgLib.c
CommitLineData
f1ec65ba 1/** @file\r
2\r
29874a8c 3 Copyright (c) 2011 - 2013, Intel Corporation. All rights reserved.<BR>\r
0dc231c9 4 Copyright (C) 2013, Red Hat, Inc.\r
2b631390 5 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
f1ec65ba 6\r
7 This program and the accompanying materials\r
8 are licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this 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,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include "Uefi.h"\r
18#include <Library/BaseLib.h>\r
19#include <Library/BaseMemoryLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/IoLib.h>\r
22#include <Library/QemuFwCfgLib.h>\r
23#include <Library/MemoryAllocationLib.h>\r
24#include <Library/UefiBootServicesTableLib.h>\r
25\r
5297c0bf
LE
26#include "QemuFwCfgLibInternal.h"\r
27\r
f1ec65ba 28\r
f1ec65ba 29/**\r
30 Selects a firmware configuration item for reading.\r
31 \r
32 Following this call, any data read from this item will start from\r
33 the beginning of the configuration item's data.\r
34\r
35 @param[in] QemuFwCfgItem - Firmware Configuration item to read\r
36\r
37**/\r
38VOID\r
39EFIAPI\r
40QemuFwCfgSelectItem (\r
41 IN FIRMWARE_CONFIG_ITEM QemuFwCfgItem\r
42 )\r
43{\r
44 DEBUG ((EFI_D_INFO, "Select Item: 0x%x\n", (UINT16)(UINTN) QemuFwCfgItem));\r
21ca2f28 45 IoWrite16 (FW_CFG_IO_SELECTOR, (UINT16)(UINTN) QemuFwCfgItem);\r
f1ec65ba 46}\r
47\r
48\r
2c8dcbc6 49/**\r
d055601e
LE
50 Transfer an array of bytes, or skip a number of bytes, using the DMA\r
51 interface.\r
2c8dcbc6 52\r
d055601e
LE
53 @param[in] Size Size in bytes to transfer or skip.\r
54\r
55 @param[in,out] Buffer Buffer to read data into or write data from. Ignored,\r
56 and may be NULL, if Size is zero, or Control is\r
57 FW_CFG_DMA_CTL_SKIP.\r
58\r
59 @param[in] Control One of the following:\r
60 FW_CFG_DMA_CTL_WRITE - write to fw_cfg from Buffer.\r
61 FW_CFG_DMA_CTL_READ - read from fw_cfg into Buffer.\r
62 FW_CFG_DMA_CTL_SKIP - skip bytes in fw_cfg.\r
2c8dcbc6
LE
63**/\r
64VOID\r
65InternalQemuFwCfgDmaBytes (\r
66 IN UINT32 Size,\r
67 IN OUT VOID *Buffer OPTIONAL,\r
d055601e 68 IN UINT32 Control\r
2c8dcbc6
LE
69 )\r
70{\r
7cfe445d
BS
71 volatile FW_CFG_DMA_ACCESS LocalAccess;\r
72 volatile FW_CFG_DMA_ACCESS *Access;\r
2c8dcbc6
LE
73 UINT32 AccessHigh, AccessLow;\r
74 UINT32 Status;\r
66c548be
BS
75 UINT32 NumPages;\r
76 VOID *DmaBuffer, *BounceBuffer;\r
2c8dcbc6 77\r
d055601e
LE
78 ASSERT (Control == FW_CFG_DMA_CTL_WRITE || Control == FW_CFG_DMA_CTL_READ ||\r
79 Control == FW_CFG_DMA_CTL_SKIP);\r
80\r
2c8dcbc6
LE
81 if (Size == 0) {\r
82 return;\r
83 }\r
84\r
e508e069
BS
85 //\r
86 // set NumPages to suppress incorrect compiler/analyzer warnings\r
87 //\r
88 NumPages = 0;\r
89\r
66c548be
BS
90 //\r
91 // When SEV is enabled then allocate DMA bounce buffer\r
92 //\r
93 if (InternalQemuFwCfgSevIsEnabled ()) {\r
94 UINTN TotalSize;\r
95\r
96 TotalSize = sizeof (*Access);\r
97 //\r
98 // Skip operation does not need buffer\r
99 //\r
100 if (Control != FW_CFG_DMA_CTL_SKIP) {\r
101 TotalSize += Size;\r
102 }\r
103\r
104 //\r
105 // Allocate SEV DMA buffer\r
106 //\r
107 NumPages = (UINT32)EFI_SIZE_TO_PAGES (TotalSize);\r
108 InternalQemuFwCfgSevDmaAllocateBuffer (&BounceBuffer, NumPages);\r
109\r
110 Access = BounceBuffer;\r
111 DmaBuffer = (UINT8*)BounceBuffer + sizeof (*Access);\r
112\r
113 //\r
114 // Decrypt data from encrypted guest buffer into DMA buffer\r
115 //\r
116 if (Control == FW_CFG_DMA_CTL_WRITE) {\r
117 CopyMem (DmaBuffer, Buffer, Size);\r
118 }\r
119 } else {\r
120 Access = &LocalAccess;\r
121 DmaBuffer = Buffer;\r
122 BounceBuffer = NULL;\r
123 }\r
7cfe445d
BS
124\r
125 Access->Control = SwapBytes32 (Control);\r
126 Access->Length = SwapBytes32 (Size);\r
66c548be 127 Access->Address = SwapBytes64 ((UINTN)DmaBuffer);\r
2c8dcbc6
LE
128\r
129 //\r
130 // Delimit the transfer from (a) modifications to Access, (b) in case of a\r
131 // write, from writes to Buffer by the caller.\r
132 //\r
133 MemoryFence ();\r
134\r
135 //\r
136 // Start the transfer.\r
137 //\r
7cfe445d
BS
138 AccessHigh = (UINT32)RShiftU64 ((UINTN)Access, 32);\r
139 AccessLow = (UINT32)(UINTN)Access;\r
ed1a2d42
LE
140 IoWrite32 (FW_CFG_IO_DMA_ADDRESS, SwapBytes32 (AccessHigh));\r
141 IoWrite32 (FW_CFG_IO_DMA_ADDRESS + 4, SwapBytes32 (AccessLow));\r
2c8dcbc6
LE
142\r
143 //\r
144 // Don't look at Access.Control before starting the transfer.\r
145 //\r
146 MemoryFence ();\r
147\r
148 //\r
149 // Wait for the transfer to complete.\r
150 //\r
151 do {\r
7cfe445d 152 Status = SwapBytes32 (Access->Control);\r
2c8dcbc6
LE
153 ASSERT ((Status & FW_CFG_DMA_CTL_ERROR) == 0);\r
154 } while (Status != 0);\r
155\r
156 //\r
157 // After a read, the caller will want to use Buffer.\r
158 //\r
159 MemoryFence ();\r
66c548be
BS
160\r
161 //\r
162 // If Bounce buffer was allocated then copy the data into guest buffer and\r
163 // free the bounce buffer\r
164 //\r
165 if (BounceBuffer != NULL) {\r
166 //\r
167 // Encrypt the data from DMA buffer into guest buffer\r
168 //\r
169 if (Control == FW_CFG_DMA_CTL_READ) {\r
170 CopyMem (Buffer, DmaBuffer, Size);\r
171 }\r
172\r
173 InternalQemuFwCfgSevDmaFreeBuffer (BounceBuffer, NumPages);\r
174 }\r
2c8dcbc6
LE
175}\r
176\r
177\r
f1ec65ba 178/**\r
179 Reads firmware configuration bytes into a buffer\r
180\r
181 @param[in] Size - Size in bytes to read\r
182 @param[in] Buffer - Buffer to store data into (OPTIONAL if Size is 0)\r
183\r
184**/\r
185VOID\r
186EFIAPI\r
187InternalQemuFwCfgReadBytes (\r
188 IN UINTN Size,\r
189 IN VOID *Buffer OPTIONAL\r
190 )\r
191{\r
2c8dcbc6 192 if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
d055601e 193 InternalQemuFwCfgDmaBytes ((UINT32)Size, Buffer, FW_CFG_DMA_CTL_READ);\r
2c8dcbc6
LE
194 return;\r
195 }\r
509e6b5a 196 IoReadFifo8 (FW_CFG_IO_DATA, Size, Buffer);\r
f1ec65ba 197}\r
198\r
199\r
200/**\r
201 Reads firmware configuration bytes into a buffer\r
202\r
203 If called multiple times, then the data read will\r
204 continue at the offset of the firmware configuration\r
205 item where the previous read ended.\r
206\r
207 @param[in] Size - Size in bytes to read\r
208 @param[in] Buffer - Buffer to store data into\r
209\r
210**/\r
211VOID\r
212EFIAPI\r
213QemuFwCfgReadBytes (\r
214 IN UINTN Size,\r
215 IN VOID *Buffer\r
216 )\r
217{\r
0dc231c9 218 if (InternalQemuFwCfgIsAvailable ()) {\r
f1ec65ba 219 InternalQemuFwCfgReadBytes (Size, Buffer);\r
220 } else {\r
221 ZeroMem (Buffer, Size);\r
222 }\r
223}\r
224\r
29874a8c 225/**\r
226 Write firmware configuration bytes from a buffer\r
227\r
228 If called multiple times, then the data written will\r
229 continue at the offset of the firmware configuration\r
230 item where the previous write ended.\r
231\r
232 @param[in] Size - Size in bytes to write\r
233 @param[in] Buffer - Buffer to read data from\r
234\r
235**/\r
236VOID\r
237EFIAPI\r
238QemuFwCfgWriteBytes (\r
239 IN UINTN Size,\r
240 IN VOID *Buffer\r
241 )\r
242{\r
0dc231c9 243 if (InternalQemuFwCfgIsAvailable ()) {\r
2c8dcbc6 244 if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
d055601e 245 InternalQemuFwCfgDmaBytes ((UINT32)Size, Buffer, FW_CFG_DMA_CTL_WRITE);\r
2c8dcbc6
LE
246 return;\r
247 }\r
509e6b5a 248 IoWriteFifo8 (FW_CFG_IO_DATA, Size, Buffer);\r
29874a8c 249 }\r
250}\r
251\r
f1ec65ba 252\r
fcca9f67
LE
253/**\r
254 Skip bytes in the firmware configuration item.\r
255\r
256 Increase the offset of the firmware configuration item without transferring\r
257 bytes between the item and a caller-provided buffer. Subsequent read, write\r
258 or skip operations will commence at the increased offset.\r
259\r
260 @param[in] Size Number of bytes to skip.\r
261**/\r
262VOID\r
263EFIAPI\r
264QemuFwCfgSkipBytes (\r
265 IN UINTN Size\r
266 )\r
267{\r
268 UINTN ChunkSize;\r
269 UINT8 SkipBuffer[256];\r
270\r
271 if (!InternalQemuFwCfgIsAvailable ()) {\r
272 return;\r
273 }\r
274\r
275 if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
276 InternalQemuFwCfgDmaBytes ((UINT32)Size, NULL, FW_CFG_DMA_CTL_SKIP);\r
277 return;\r
278 }\r
279\r
280 //\r
281 // Emulate the skip by reading data in chunks, and throwing it away. The\r
282 // implementation below is suitable even for phases where RAM or dynamic\r
283 // allocation is not available or appropriate. It also doesn't affect the\r
284 // static data footprint for client modules. Large skips are not expected,\r
285 // therefore this fallback is not performance critical. The size of\r
286 // SkipBuffer is thought not to exert a large pressure on the stack in any\r
287 // phase.\r
288 //\r
289 while (Size > 0) {\r
290 ChunkSize = MIN (Size, sizeof SkipBuffer);\r
509e6b5a 291 IoReadFifo8 (FW_CFG_IO_DATA, ChunkSize, SkipBuffer);\r
fcca9f67
LE
292 Size -= ChunkSize;\r
293 }\r
294}\r
295\r
296\r
f1ec65ba 297/**\r
298 Reads a UINT8 firmware configuration value\r
299\r
300 @return Value of Firmware Configuration item read\r
301\r
302**/\r
303UINT8\r
304EFIAPI\r
305QemuFwCfgRead8 (\r
306 VOID\r
307 )\r
308{\r
309 UINT8 Result;\r
310\r
311 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
312\r
313 return Result;\r
314}\r
315\r
316\r
317/**\r
318 Reads a UINT16 firmware configuration value\r
319\r
320 @return Value of Firmware Configuration item read\r
321\r
322**/\r
323UINT16\r
324EFIAPI\r
325QemuFwCfgRead16 (\r
326 VOID\r
327 )\r
328{\r
329 UINT16 Result;\r
330\r
331 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
332\r
333 return Result;\r
334}\r
335\r
336\r
337/**\r
338 Reads a UINT32 firmware configuration value\r
339\r
340 @return Value of Firmware Configuration item read\r
341\r
342**/\r
343UINT32\r
344EFIAPI\r
345QemuFwCfgRead32 (\r
346 VOID\r
347 )\r
348{\r
349 UINT32 Result;\r
350\r
351 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
352\r
353 return Result;\r
354}\r
355\r
356\r
357/**\r
358 Reads a UINT64 firmware configuration value\r
359\r
360 @return Value of Firmware Configuration item read\r
361\r
362**/\r
363UINT64\r
364EFIAPI\r
365QemuFwCfgRead64 (\r
366 VOID\r
367 )\r
368{\r
369 UINT64 Result;\r
370\r
371 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
372\r
373 return Result;\r
374}\r
375\r
376\r
0ac9bc9b 377/**\r
378 Find the configuration item corresponding to the firmware configuration file.\r
379\r
380 @param[in] Name - Name of file to look up.\r
381 @param[out] Item - Configuration item corresponding to the file, to be passed\r
382 to QemuFwCfgSelectItem ().\r
383 @param[out] Size - Number of bytes in the file.\r
384\r
385 @return RETURN_SUCCESS If file is found.\r
386 RETURN_NOT_FOUND If file is not found.\r
387 RETURN_UNSUPPORTED If firmware configuration is unavailable.\r
388\r
389**/\r
390RETURN_STATUS\r
391EFIAPI\r
392QemuFwCfgFindFile (\r
393 IN CONST CHAR8 *Name,\r
394 OUT FIRMWARE_CONFIG_ITEM *Item,\r
395 OUT UINTN *Size\r
396 )\r
397{\r
398 UINT32 Count;\r
399 UINT32 Idx;\r
400\r
0dc231c9 401 if (!InternalQemuFwCfgIsAvailable ()) {\r
0ac9bc9b 402 return RETURN_UNSUPPORTED;\r
403 }\r
404\r
405 QemuFwCfgSelectItem (QemuFwCfgItemFileDir);\r
406 Count = SwapBytes32 (QemuFwCfgRead32 ());\r
407\r
408 for (Idx = 0; Idx < Count; ++Idx) {\r
409 UINT32 FileSize;\r
410 UINT16 FileSelect;\r
411 UINT16 FileReserved;\r
6a904296 412 CHAR8 FName[QEMU_FW_CFG_FNAME_SIZE];\r
0ac9bc9b 413\r
414 FileSize = QemuFwCfgRead32 ();\r
415 FileSelect = QemuFwCfgRead16 ();\r
416 FileReserved = QemuFwCfgRead16 ();\r
c6910aed 417 (VOID) FileReserved; /* Force a do-nothing reference. */\r
0ac9bc9b 418 InternalQemuFwCfgReadBytes (sizeof (FName), FName);\r
419\r
420 if (AsciiStrCmp (Name, FName) == 0) {\r
421 *Item = SwapBytes16 (FileSelect);\r
422 *Size = SwapBytes32 (FileSize);\r
423 return RETURN_SUCCESS;\r
424 }\r
425 }\r
426\r
427 return RETURN_NOT_FOUND;\r
428}\r