3 Stateful and implicitly initialized fw_cfg library implementation.
5 Copyright (C) 2013 - 2014, Red Hat, Inc.
6 Copyright (c) 2011 - 2013, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials are licensed and made available
9 under the terms and conditions of the BSD License which accompanies this
10 distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
14 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/BaseLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/IoLib.h>
20 #include <Library/PcdLib.h>
21 #include <Library/QemuFwCfgLib.h>
23 STATIC UINTN mFwCfgSelectorAddress
;
24 STATIC UINTN mFwCfgDataAddress
;
28 Returns a boolean indicating if the firmware configuration interface is
29 available for library-internal purposes.
31 This function never changes fw_cfg state.
33 @retval TRUE The interface is available internally.
34 @retval FALSE The interface is not available internally.
38 InternalQemuFwCfgIsAvailable (
42 return (BOOLEAN
)(mFwCfgSelectorAddress
!= 0 && mFwCfgDataAddress
!= 0);
47 Returns a boolean indicating if the firmware configuration interface
50 This function may change fw_cfg state.
52 @retval TRUE The interface is available
53 @retval FALSE The interface is not available
58 QemuFwCfgIsAvailable (
62 return InternalQemuFwCfgIsAvailable ();
72 mFwCfgSelectorAddress
= (UINTN
)PcdGet64 (PcdFwCfgSelectorAddress
);
73 mFwCfgDataAddress
= (UINTN
)PcdGet64 (PcdFwCfgDataAddress
);
75 if (InternalQemuFwCfgIsAvailable ()) {
78 QemuFwCfgSelectItem (QemuFwCfgItemSignature
);
79 Signature
= QemuFwCfgRead32 ();
80 if (Signature
!= SIGNATURE_32 ('Q', 'E', 'M', 'U')) {
81 mFwCfgSelectorAddress
= 0;
82 mFwCfgDataAddress
= 0;
85 return RETURN_SUCCESS
;
90 Selects a firmware configuration item for reading.
92 Following this call, any data read from this item will start from the
93 beginning of the configuration item's data.
95 @param[in] QemuFwCfgItem Firmware Configuration item to read
100 QemuFwCfgSelectItem (
101 IN FIRMWARE_CONFIG_ITEM QemuFwCfgItem
104 if (InternalQemuFwCfgIsAvailable ()) {
105 MmioWrite16 (mFwCfgSelectorAddress
, SwapBytes16 ((UINT16
)QemuFwCfgItem
));
111 Reads firmware configuration bytes into a buffer
113 @param[in] Size Size in bytes to read
114 @param[in] Buffer Buffer to store data into (OPTIONAL if Size is 0)
120 InternalQemuFwCfgReadBytes (
122 IN VOID
*Buffer OPTIONAL
129 #ifdef MDE_CPU_AARCH64
139 #ifdef MDE_CPU_AARCH64
141 *(UINT64
*)Ptr
= MmioRead64 (mFwCfgDataAddress
);
145 *(UINT32
*)Ptr
= MmioRead32 (mFwCfgDataAddress
);
150 *(UINT32
*)Ptr
= MmioRead32 (mFwCfgDataAddress
);
156 *(UINT16
*)Ptr
= MmioRead16 (mFwCfgDataAddress
);
160 *Ptr
= MmioRead8 (mFwCfgDataAddress
);
166 Reads firmware configuration bytes into a buffer
168 If called multiple times, then the data read will continue at the offset of
169 the firmware configuration item where the previous read ended.
171 @param[in] Size Size in bytes to read
172 @param[in] Buffer Buffer to store data into
182 if (InternalQemuFwCfgIsAvailable ()) {
183 InternalQemuFwCfgReadBytes (Size
, Buffer
);
185 ZeroMem (Buffer
, Size
);
190 Write firmware configuration bytes from a buffer
192 If called multiple times, then the data written will continue at the offset
193 of the firmware configuration item where the previous write ended.
195 @param[in] Size Size in bytes to write
196 @param[in] Buffer Buffer to read data from
201 QemuFwCfgWriteBytes (
206 if (InternalQemuFwCfgIsAvailable ()) {
209 for (Idx
= 0; Idx
< Size
; ++Idx
) {
210 MmioWrite8 (mFwCfgDataAddress
, ((UINT8
*)Buffer
)[Idx
]);
217 Reads a UINT8 firmware configuration value
219 @return Value of Firmware Configuration item read
230 QemuFwCfgReadBytes (sizeof Result
, &Result
);
236 Reads a UINT16 firmware configuration value
238 @return Value of Firmware Configuration item read
249 QemuFwCfgReadBytes (sizeof Result
, &Result
);
255 Reads a UINT32 firmware configuration value
257 @return Value of Firmware Configuration item read
268 QemuFwCfgReadBytes (sizeof Result
, &Result
);
274 Reads a UINT64 firmware configuration value
276 @return Value of Firmware Configuration item read
287 QemuFwCfgReadBytes (sizeof Result
, &Result
);
293 Find the configuration item corresponding to the firmware configuration file.
295 @param[in] Name Name of file to look up.
296 @param[out] Item Configuration item corresponding to the file, to be passed
297 to QemuFwCfgSelectItem ().
298 @param[out] Size Number of bytes in the file.
300 @retval RETURN_SUCCESS If file is found.
301 @retval RETURN_NOT_FOUND If file is not found.
302 @retval RETURN_UNSUPPORTED If firmware configuration is unavailable.
308 IN CONST CHAR8
*Name
,
309 OUT FIRMWARE_CONFIG_ITEM
*Item
,
316 if (!InternalQemuFwCfgIsAvailable ()) {
317 return RETURN_UNSUPPORTED
;
320 QemuFwCfgSelectItem (QemuFwCfgItemFileDir
);
321 Count
= SwapBytes32 (QemuFwCfgRead32 ());
323 for (Idx
= 0; Idx
< Count
; ++Idx
) {
326 CHAR8 FName
[QEMU_FW_CFG_FNAME_SIZE
];
328 FileSize
= QemuFwCfgRead32 ();
329 FileSelect
= QemuFwCfgRead16 ();
330 QemuFwCfgRead16 (); // skip the field called "reserved"
331 InternalQemuFwCfgReadBytes (sizeof (FName
), FName
);
333 if (AsciiStrCmp (Name
, FName
) == 0) {
334 *Item
= (FIRMWARE_CONFIG_ITEM
) SwapBytes16 (FileSelect
);
335 *Size
= SwapBytes32 (FileSize
);
336 return RETURN_SUCCESS
;
340 return RETURN_NOT_FOUND
;
345 Determine if S3 support is explicitly enabled.
347 @retval TRUE if S3 support is explicitly enabled.
348 FALSE otherwise. This includes unavailability of the firmware
349 configuration interface.