]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLib.c
OvmfPkg/QemuFwCfgLib: Implement SEV internal function for Dxe phase
[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
71 volatile FW_CFG_DMA_ACCESS Access;\r
72 UINT32 AccessHigh, AccessLow;\r
73 UINT32 Status;\r
74\r
d055601e
LE
75 ASSERT (Control == FW_CFG_DMA_CTL_WRITE || Control == FW_CFG_DMA_CTL_READ ||\r
76 Control == FW_CFG_DMA_CTL_SKIP);\r
77\r
2c8dcbc6
LE
78 if (Size == 0) {\r
79 return;\r
80 }\r
81\r
d055601e 82 Access.Control = SwapBytes32 (Control);\r
2c8dcbc6
LE
83 Access.Length = SwapBytes32 (Size);\r
84 Access.Address = SwapBytes64 ((UINTN)Buffer);\r
85\r
86 //\r
87 // Delimit the transfer from (a) modifications to Access, (b) in case of a\r
88 // write, from writes to Buffer by the caller.\r
89 //\r
90 MemoryFence ();\r
91\r
92 //\r
93 // Start the transfer.\r
94 //\r
95 AccessHigh = (UINT32)RShiftU64 ((UINTN)&Access, 32);\r
96 AccessLow = (UINT32)(UINTN)&Access;\r
ed1a2d42
LE
97 IoWrite32 (FW_CFG_IO_DMA_ADDRESS, SwapBytes32 (AccessHigh));\r
98 IoWrite32 (FW_CFG_IO_DMA_ADDRESS + 4, SwapBytes32 (AccessLow));\r
2c8dcbc6
LE
99\r
100 //\r
101 // Don't look at Access.Control before starting the transfer.\r
102 //\r
103 MemoryFence ();\r
104\r
105 //\r
106 // Wait for the transfer to complete.\r
107 //\r
108 do {\r
109 Status = SwapBytes32 (Access.Control);\r
110 ASSERT ((Status & FW_CFG_DMA_CTL_ERROR) == 0);\r
111 } while (Status != 0);\r
112\r
113 //\r
114 // After a read, the caller will want to use Buffer.\r
115 //\r
116 MemoryFence ();\r
117}\r
118\r
119\r
f1ec65ba 120/**\r
121 Reads firmware configuration bytes into a buffer\r
122\r
123 @param[in] Size - Size in bytes to read\r
124 @param[in] Buffer - Buffer to store data into (OPTIONAL if Size is 0)\r
125\r
126**/\r
127VOID\r
128EFIAPI\r
129InternalQemuFwCfgReadBytes (\r
130 IN UINTN Size,\r
131 IN VOID *Buffer OPTIONAL\r
132 )\r
133{\r
2c8dcbc6 134 if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
d055601e 135 InternalQemuFwCfgDmaBytes ((UINT32)Size, Buffer, FW_CFG_DMA_CTL_READ);\r
2c8dcbc6
LE
136 return;\r
137 }\r
509e6b5a 138 IoReadFifo8 (FW_CFG_IO_DATA, Size, Buffer);\r
f1ec65ba 139}\r
140\r
141\r
142/**\r
143 Reads firmware configuration bytes into a buffer\r
144\r
145 If called multiple times, then the data read will\r
146 continue at the offset of the firmware configuration\r
147 item where the previous read ended.\r
148\r
149 @param[in] Size - Size in bytes to read\r
150 @param[in] Buffer - Buffer to store data into\r
151\r
152**/\r
153VOID\r
154EFIAPI\r
155QemuFwCfgReadBytes (\r
156 IN UINTN Size,\r
157 IN VOID *Buffer\r
158 )\r
159{\r
0dc231c9 160 if (InternalQemuFwCfgIsAvailable ()) {\r
f1ec65ba 161 InternalQemuFwCfgReadBytes (Size, Buffer);\r
162 } else {\r
163 ZeroMem (Buffer, Size);\r
164 }\r
165}\r
166\r
29874a8c 167/**\r
168 Write firmware configuration bytes from a buffer\r
169\r
170 If called multiple times, then the data written will\r
171 continue at the offset of the firmware configuration\r
172 item where the previous write ended.\r
173\r
174 @param[in] Size - Size in bytes to write\r
175 @param[in] Buffer - Buffer to read data from\r
176\r
177**/\r
178VOID\r
179EFIAPI\r
180QemuFwCfgWriteBytes (\r
181 IN UINTN Size,\r
182 IN VOID *Buffer\r
183 )\r
184{\r
0dc231c9 185 if (InternalQemuFwCfgIsAvailable ()) {\r
2c8dcbc6 186 if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
d055601e 187 InternalQemuFwCfgDmaBytes ((UINT32)Size, Buffer, FW_CFG_DMA_CTL_WRITE);\r
2c8dcbc6
LE
188 return;\r
189 }\r
509e6b5a 190 IoWriteFifo8 (FW_CFG_IO_DATA, Size, Buffer);\r
29874a8c 191 }\r
192}\r
193\r
f1ec65ba 194\r
fcca9f67
LE
195/**\r
196 Skip bytes in the firmware configuration item.\r
197\r
198 Increase the offset of the firmware configuration item without transferring\r
199 bytes between the item and a caller-provided buffer. Subsequent read, write\r
200 or skip operations will commence at the increased offset.\r
201\r
202 @param[in] Size Number of bytes to skip.\r
203**/\r
204VOID\r
205EFIAPI\r
206QemuFwCfgSkipBytes (\r
207 IN UINTN Size\r
208 )\r
209{\r
210 UINTN ChunkSize;\r
211 UINT8 SkipBuffer[256];\r
212\r
213 if (!InternalQemuFwCfgIsAvailable ()) {\r
214 return;\r
215 }\r
216\r
217 if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
218 InternalQemuFwCfgDmaBytes ((UINT32)Size, NULL, FW_CFG_DMA_CTL_SKIP);\r
219 return;\r
220 }\r
221\r
222 //\r
223 // Emulate the skip by reading data in chunks, and throwing it away. The\r
224 // implementation below is suitable even for phases where RAM or dynamic\r
225 // allocation is not available or appropriate. It also doesn't affect the\r
226 // static data footprint for client modules. Large skips are not expected,\r
227 // therefore this fallback is not performance critical. The size of\r
228 // SkipBuffer is thought not to exert a large pressure on the stack in any\r
229 // phase.\r
230 //\r
231 while (Size > 0) {\r
232 ChunkSize = MIN (Size, sizeof SkipBuffer);\r
509e6b5a 233 IoReadFifo8 (FW_CFG_IO_DATA, ChunkSize, SkipBuffer);\r
fcca9f67
LE
234 Size -= ChunkSize;\r
235 }\r
236}\r
237\r
238\r
f1ec65ba 239/**\r
240 Reads a UINT8 firmware configuration value\r
241\r
242 @return Value of Firmware Configuration item read\r
243\r
244**/\r
245UINT8\r
246EFIAPI\r
247QemuFwCfgRead8 (\r
248 VOID\r
249 )\r
250{\r
251 UINT8 Result;\r
252\r
253 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
254\r
255 return Result;\r
256}\r
257\r
258\r
259/**\r
260 Reads a UINT16 firmware configuration value\r
261\r
262 @return Value of Firmware Configuration item read\r
263\r
264**/\r
265UINT16\r
266EFIAPI\r
267QemuFwCfgRead16 (\r
268 VOID\r
269 )\r
270{\r
271 UINT16 Result;\r
272\r
273 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
274\r
275 return Result;\r
276}\r
277\r
278\r
279/**\r
280 Reads a UINT32 firmware configuration value\r
281\r
282 @return Value of Firmware Configuration item read\r
283\r
284**/\r
285UINT32\r
286EFIAPI\r
287QemuFwCfgRead32 (\r
288 VOID\r
289 )\r
290{\r
291 UINT32 Result;\r
292\r
293 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
294\r
295 return Result;\r
296}\r
297\r
298\r
299/**\r
300 Reads a UINT64 firmware configuration value\r
301\r
302 @return Value of Firmware Configuration item read\r
303\r
304**/\r
305UINT64\r
306EFIAPI\r
307QemuFwCfgRead64 (\r
308 VOID\r
309 )\r
310{\r
311 UINT64 Result;\r
312\r
313 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
314\r
315 return Result;\r
316}\r
317\r
318\r
0ac9bc9b 319/**\r
320 Find the configuration item corresponding to the firmware configuration file.\r
321\r
322 @param[in] Name - Name of file to look up.\r
323 @param[out] Item - Configuration item corresponding to the file, to be passed\r
324 to QemuFwCfgSelectItem ().\r
325 @param[out] Size - Number of bytes in the file.\r
326\r
327 @return RETURN_SUCCESS If file is found.\r
328 RETURN_NOT_FOUND If file is not found.\r
329 RETURN_UNSUPPORTED If firmware configuration is unavailable.\r
330\r
331**/\r
332RETURN_STATUS\r
333EFIAPI\r
334QemuFwCfgFindFile (\r
335 IN CONST CHAR8 *Name,\r
336 OUT FIRMWARE_CONFIG_ITEM *Item,\r
337 OUT UINTN *Size\r
338 )\r
339{\r
340 UINT32 Count;\r
341 UINT32 Idx;\r
342\r
0dc231c9 343 if (!InternalQemuFwCfgIsAvailable ()) {\r
0ac9bc9b 344 return RETURN_UNSUPPORTED;\r
345 }\r
346\r
347 QemuFwCfgSelectItem (QemuFwCfgItemFileDir);\r
348 Count = SwapBytes32 (QemuFwCfgRead32 ());\r
349\r
350 for (Idx = 0; Idx < Count; ++Idx) {\r
351 UINT32 FileSize;\r
352 UINT16 FileSelect;\r
353 UINT16 FileReserved;\r
6a904296 354 CHAR8 FName[QEMU_FW_CFG_FNAME_SIZE];\r
0ac9bc9b 355\r
356 FileSize = QemuFwCfgRead32 ();\r
357 FileSelect = QemuFwCfgRead16 ();\r
358 FileReserved = QemuFwCfgRead16 ();\r
c6910aed 359 (VOID) FileReserved; /* Force a do-nothing reference. */\r
0ac9bc9b 360 InternalQemuFwCfgReadBytes (sizeof (FName), FName);\r
361\r
362 if (AsciiStrCmp (Name, FName) == 0) {\r
363 *Item = SwapBytes16 (FileSelect);\r
364 *Size = SwapBytes32 (FileSize);\r
365 return RETURN_SUCCESS;\r
366 }\r
367 }\r
368\r
369 return RETURN_NOT_FOUND;\r
370}\r