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