3 Copyright (c) 2011 - 2013, Intel Corporation. All rights reserved.<BR>
4 Copyright (C) 2013, Red Hat, Inc.
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/BaseLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/QemuFwCfgLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
25 STATIC BOOLEAN mQemuFwCfgSupported
= FALSE
;
29 Reads an 8-bit I/O port fifo into a block of memory.
31 Reads the 8-bit I/O fifo port specified by Port.
33 The port is read Count times, and the read data is
34 stored in the provided Buffer.
36 This function must guarantee that all I/O read and write operations are
39 If 8-bit I/O port operations are not supported, then ASSERT().
41 @param Port The I/O port to read.
42 @param Count The number of times to read I/O port.
43 @param Buffer The buffer to store the read data into.
55 Writes an 8-bit I/O port fifo from a block of memory.
57 Writes the 8-bit I/O fifo port specified by Port.
59 The port is written Count times, and the data are obtained
60 from the provided Buffer.
62 This function must guarantee that all I/O read and write operations are
65 If 8-bit I/O port operations are not supported, then ASSERT().
67 @param Port The I/O port to read.
68 @param Count The number of times to read I/O port.
69 @param Buffer The buffer to store the read data into.
82 Returns a boolean indicating if the firmware configuration interface
85 This function may change fw_cfg state.
87 @retval TRUE The interface is available
88 @retval FALSE The interface is not available
93 QemuFwCfgIsAvailable (
97 return InternalQemuFwCfgIsAvailable ();
102 Selects a firmware configuration item for reading.
104 Following this call, any data read from this item will start from
105 the beginning of the configuration item's data.
107 @param[in] QemuFwCfgItem - Firmware Configuration item to read
112 QemuFwCfgSelectItem (
113 IN FIRMWARE_CONFIG_ITEM QemuFwCfgItem
116 DEBUG ((EFI_D_INFO
, "Select Item: 0x%x\n", (UINT16
)(UINTN
) QemuFwCfgItem
));
117 IoWrite16 (0x510, (UINT16
)(UINTN
) QemuFwCfgItem
);
122 Reads firmware configuration bytes into a buffer
124 @param[in] Size - Size in bytes to read
125 @param[in] Buffer - Buffer to store data into (OPTIONAL if Size is 0)
130 InternalQemuFwCfgReadBytes (
132 IN VOID
*Buffer OPTIONAL
135 IoReadFifo8 (0x511, Size
, Buffer
);
140 Reads firmware configuration bytes into a buffer
142 If called multiple times, then the data read will
143 continue at the offset of the firmware configuration
144 item where the previous read ended.
146 @param[in] Size - Size in bytes to read
147 @param[in] Buffer - Buffer to store data into
157 if (InternalQemuFwCfgIsAvailable ()) {
158 InternalQemuFwCfgReadBytes (Size
, Buffer
);
160 ZeroMem (Buffer
, Size
);
165 Write firmware configuration bytes from a buffer
167 If called multiple times, then the data written will
168 continue at the offset of the firmware configuration
169 item where the previous write ended.
171 @param[in] Size - Size in bytes to write
172 @param[in] Buffer - Buffer to read data from
177 QemuFwCfgWriteBytes (
182 if (InternalQemuFwCfgIsAvailable ()) {
183 IoWriteFifo8 (0x511, Size
, Buffer
);
189 Reads a UINT8 firmware configuration value
191 @return Value of Firmware Configuration item read
202 QemuFwCfgReadBytes (sizeof (Result
), &Result
);
209 Reads a UINT16 firmware configuration value
211 @return Value of Firmware Configuration item read
222 QemuFwCfgReadBytes (sizeof (Result
), &Result
);
229 Reads a UINT32 firmware configuration value
231 @return Value of Firmware Configuration item read
242 QemuFwCfgReadBytes (sizeof (Result
), &Result
);
249 Reads a UINT64 firmware configuration value
251 @return Value of Firmware Configuration item read
262 QemuFwCfgReadBytes (sizeof (Result
), &Result
);
270 QemuFwCfgInitialize (
278 // Enable the access routines while probing to see if it is supported.
280 mQemuFwCfgSupported
= TRUE
;
282 QemuFwCfgSelectItem (QemuFwCfgItemSignature
);
283 Signature
= QemuFwCfgRead32 ();
284 DEBUG ((EFI_D_INFO
, "FW CFG Signature: 0x%x\n", Signature
));
285 QemuFwCfgSelectItem (QemuFwCfgItemInterfaceVersion
);
286 Revision
= QemuFwCfgRead32 ();
287 DEBUG ((EFI_D_INFO
, "FW CFG Revision: 0x%x\n", Revision
));
288 if ((Signature
!= SIGNATURE_32 ('Q', 'E', 'M', 'U')) ||
291 DEBUG ((EFI_D_INFO
, "QemuFwCfg interface not supported.\n"));
292 mQemuFwCfgSupported
= FALSE
;
293 return RETURN_SUCCESS
;
296 DEBUG ((EFI_D_INFO
, "QemuFwCfg interface is supported.\n"));
297 return RETURN_SUCCESS
;
302 Find the configuration item corresponding to the firmware configuration file.
304 @param[in] Name - Name of file to look up.
305 @param[out] Item - Configuration item corresponding to the file, to be passed
306 to QemuFwCfgSelectItem ().
307 @param[out] Size - Number of bytes in the file.
309 @return RETURN_SUCCESS If file is found.
310 RETURN_NOT_FOUND If file is not found.
311 RETURN_UNSUPPORTED If firmware configuration is unavailable.
317 IN CONST CHAR8
*Name
,
318 OUT FIRMWARE_CONFIG_ITEM
*Item
,
325 if (!InternalQemuFwCfgIsAvailable ()) {
326 return RETURN_UNSUPPORTED
;
329 QemuFwCfgSelectItem (QemuFwCfgItemFileDir
);
330 Count
= SwapBytes32 (QemuFwCfgRead32 ());
332 for (Idx
= 0; Idx
< Count
; ++Idx
) {
338 FileSize
= QemuFwCfgRead32 ();
339 FileSelect
= QemuFwCfgRead16 ();
340 FileReserved
= QemuFwCfgRead16 ();
341 InternalQemuFwCfgReadBytes (sizeof (FName
), FName
);
343 if (AsciiStrCmp (Name
, FName
) == 0) {
344 *Item
= SwapBytes16 (FileSelect
);
345 *Size
= SwapBytes32 (FileSize
);
346 return RETURN_SUCCESS
;
350 return RETURN_NOT_FOUND
;
355 Returns a boolean indicating if the firmware configuration interface is
356 available for library-internal purposes.
358 This function never changes fw_cfg state.
360 @retval TRUE The interface is available internally.
361 @retval FALSE The interface is not available internally.
365 InternalQemuFwCfgIsAvailable (
369 return mQemuFwCfgSupported
;