3 Copyright (c) 2011 - 2013, Intel Corporation. All rights reserved.<BR>
4 Copyright (C) 2013, Red Hat, Inc.
5 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/IoLib.h>
22 #include <Library/QemuFwCfgLib.h>
23 #include <Library/MemoryAllocationLib.h>
24 #include <Library/UefiBootServicesTableLib.h>
26 #include "QemuFwCfgLibInternal.h"
30 Selects a firmware configuration item for reading.
32 Following this call, any data read from this item will start from
33 the beginning of the configuration item's data.
35 @param[in] QemuFwCfgItem - Firmware Configuration item to read
41 IN FIRMWARE_CONFIG_ITEM QemuFwCfgItem
44 DEBUG ((EFI_D_INFO
, "Select Item: 0x%x\n", (UINT16
)(UINTN
) QemuFwCfgItem
));
45 IoWrite16 (FW_CFG_IO_SELECTOR
, (UINT16
)(UINTN
) QemuFwCfgItem
);
50 Transfer an array of bytes, or skip a number of bytes, using the DMA
53 @param[in] Size Size in bytes to transfer or skip.
55 @param[in,out] Buffer Buffer to read data into or write data from. Ignored,
56 and may be NULL, if Size is zero, or Control is
59 @param[in] Control One of the following:
60 FW_CFG_DMA_CTL_WRITE - write to fw_cfg from Buffer.
61 FW_CFG_DMA_CTL_READ - read from fw_cfg into Buffer.
62 FW_CFG_DMA_CTL_SKIP - skip bytes in fw_cfg.
65 InternalQemuFwCfgDmaBytes (
67 IN OUT VOID
*Buffer OPTIONAL
,
71 volatile FW_CFG_DMA_ACCESS LocalAccess
;
72 volatile FW_CFG_DMA_ACCESS
*Access
;
73 UINT32 AccessHigh
, AccessLow
;
76 VOID
*DmaBuffer
, *BounceBuffer
;
78 ASSERT (Control
== FW_CFG_DMA_CTL_WRITE
|| Control
== FW_CFG_DMA_CTL_READ
||
79 Control
== FW_CFG_DMA_CTL_SKIP
);
86 // When SEV is enabled then allocate DMA bounce buffer
88 if (InternalQemuFwCfgSevIsEnabled ()) {
91 TotalSize
= sizeof (*Access
);
93 // Skip operation does not need buffer
95 if (Control
!= FW_CFG_DMA_CTL_SKIP
) {
100 // Allocate SEV DMA buffer
102 NumPages
= (UINT32
)EFI_SIZE_TO_PAGES (TotalSize
);
103 InternalQemuFwCfgSevDmaAllocateBuffer (&BounceBuffer
, NumPages
);
105 Access
= BounceBuffer
;
106 DmaBuffer
= (UINT8
*)BounceBuffer
+ sizeof (*Access
);
109 // Decrypt data from encrypted guest buffer into DMA buffer
111 if (Control
== FW_CFG_DMA_CTL_WRITE
) {
112 CopyMem (DmaBuffer
, Buffer
, Size
);
115 Access
= &LocalAccess
;
120 Access
->Control
= SwapBytes32 (Control
);
121 Access
->Length
= SwapBytes32 (Size
);
122 Access
->Address
= SwapBytes64 ((UINTN
)DmaBuffer
);
125 // Delimit the transfer from (a) modifications to Access, (b) in case of a
126 // write, from writes to Buffer by the caller.
131 // Start the transfer.
133 AccessHigh
= (UINT32
)RShiftU64 ((UINTN
)Access
, 32);
134 AccessLow
= (UINT32
)(UINTN
)Access
;
135 IoWrite32 (FW_CFG_IO_DMA_ADDRESS
, SwapBytes32 (AccessHigh
));
136 IoWrite32 (FW_CFG_IO_DMA_ADDRESS
+ 4, SwapBytes32 (AccessLow
));
139 // Don't look at Access.Control before starting the transfer.
144 // Wait for the transfer to complete.
147 Status
= SwapBytes32 (Access
->Control
);
148 ASSERT ((Status
& FW_CFG_DMA_CTL_ERROR
) == 0);
149 } while (Status
!= 0);
152 // After a read, the caller will want to use Buffer.
157 // If Bounce buffer was allocated then copy the data into guest buffer and
158 // free the bounce buffer
160 if (BounceBuffer
!= NULL
) {
162 // Encrypt the data from DMA buffer into guest buffer
164 if (Control
== FW_CFG_DMA_CTL_READ
) {
165 CopyMem (Buffer
, DmaBuffer
, Size
);
168 InternalQemuFwCfgSevDmaFreeBuffer (BounceBuffer
, NumPages
);
174 Reads firmware configuration bytes into a buffer
176 @param[in] Size - Size in bytes to read
177 @param[in] Buffer - Buffer to store data into (OPTIONAL if Size is 0)
182 InternalQemuFwCfgReadBytes (
184 IN VOID
*Buffer OPTIONAL
187 if (InternalQemuFwCfgDmaIsAvailable () && Size
<= MAX_UINT32
) {
188 InternalQemuFwCfgDmaBytes ((UINT32
)Size
, Buffer
, FW_CFG_DMA_CTL_READ
);
191 IoReadFifo8 (FW_CFG_IO_DATA
, Size
, Buffer
);
196 Reads firmware configuration bytes into a buffer
198 If called multiple times, then the data read will
199 continue at the offset of the firmware configuration
200 item where the previous read ended.
202 @param[in] Size - Size in bytes to read
203 @param[in] Buffer - Buffer to store data into
213 if (InternalQemuFwCfgIsAvailable ()) {
214 InternalQemuFwCfgReadBytes (Size
, Buffer
);
216 ZeroMem (Buffer
, Size
);
221 Write firmware configuration bytes from a buffer
223 If called multiple times, then the data written will
224 continue at the offset of the firmware configuration
225 item where the previous write ended.
227 @param[in] Size - Size in bytes to write
228 @param[in] Buffer - Buffer to read data from
233 QemuFwCfgWriteBytes (
238 if (InternalQemuFwCfgIsAvailable ()) {
239 if (InternalQemuFwCfgDmaIsAvailable () && Size
<= MAX_UINT32
) {
240 InternalQemuFwCfgDmaBytes ((UINT32
)Size
, Buffer
, FW_CFG_DMA_CTL_WRITE
);
243 IoWriteFifo8 (FW_CFG_IO_DATA
, Size
, Buffer
);
249 Skip bytes in the firmware configuration item.
251 Increase the offset of the firmware configuration item without transferring
252 bytes between the item and a caller-provided buffer. Subsequent read, write
253 or skip operations will commence at the increased offset.
255 @param[in] Size Number of bytes to skip.
264 UINT8 SkipBuffer
[256];
266 if (!InternalQemuFwCfgIsAvailable ()) {
270 if (InternalQemuFwCfgDmaIsAvailable () && Size
<= MAX_UINT32
) {
271 InternalQemuFwCfgDmaBytes ((UINT32
)Size
, NULL
, FW_CFG_DMA_CTL_SKIP
);
276 // Emulate the skip by reading data in chunks, and throwing it away. The
277 // implementation below is suitable even for phases where RAM or dynamic
278 // allocation is not available or appropriate. It also doesn't affect the
279 // static data footprint for client modules. Large skips are not expected,
280 // therefore this fallback is not performance critical. The size of
281 // SkipBuffer is thought not to exert a large pressure on the stack in any
285 ChunkSize
= MIN (Size
, sizeof SkipBuffer
);
286 IoReadFifo8 (FW_CFG_IO_DATA
, ChunkSize
, SkipBuffer
);
293 Reads a UINT8 firmware configuration value
295 @return Value of Firmware Configuration item read
306 QemuFwCfgReadBytes (sizeof (Result
), &Result
);
313 Reads a UINT16 firmware configuration value
315 @return Value of Firmware Configuration item read
326 QemuFwCfgReadBytes (sizeof (Result
), &Result
);
333 Reads a UINT32 firmware configuration value
335 @return Value of Firmware Configuration item read
346 QemuFwCfgReadBytes (sizeof (Result
), &Result
);
353 Reads a UINT64 firmware configuration value
355 @return Value of Firmware Configuration item read
366 QemuFwCfgReadBytes (sizeof (Result
), &Result
);
373 Find the configuration item corresponding to the firmware configuration file.
375 @param[in] Name - Name of file to look up.
376 @param[out] Item - Configuration item corresponding to the file, to be passed
377 to QemuFwCfgSelectItem ().
378 @param[out] Size - Number of bytes in the file.
380 @return RETURN_SUCCESS If file is found.
381 RETURN_NOT_FOUND If file is not found.
382 RETURN_UNSUPPORTED If firmware configuration is unavailable.
388 IN CONST CHAR8
*Name
,
389 OUT FIRMWARE_CONFIG_ITEM
*Item
,
396 if (!InternalQemuFwCfgIsAvailable ()) {
397 return RETURN_UNSUPPORTED
;
400 QemuFwCfgSelectItem (QemuFwCfgItemFileDir
);
401 Count
= SwapBytes32 (QemuFwCfgRead32 ());
403 for (Idx
= 0; Idx
< Count
; ++Idx
) {
407 CHAR8 FName
[QEMU_FW_CFG_FNAME_SIZE
];
409 FileSize
= QemuFwCfgRead32 ();
410 FileSelect
= QemuFwCfgRead16 ();
411 FileReserved
= QemuFwCfgRead16 ();
412 (VOID
) FileReserved
; /* Force a do-nothing reference. */
413 InternalQemuFwCfgReadBytes (sizeof (FName
), FName
);
415 if (AsciiStrCmp (Name
, FName
) == 0) {
416 *Item
= SwapBytes16 (FileSelect
);
417 *Size
= SwapBytes32 (FileSize
);
418 return RETURN_SUCCESS
;
422 return RETURN_NOT_FOUND
;