]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLib.c
ArmVirtPkg/QemuFwCfgLib: rebase lib instance to updated lib class header
[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
f1ec65ba 5\r
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "Uefi.h"\r
17#include <Library/BaseLib.h>\r
18#include <Library/BaseMemoryLib.h>\r
19#include <Library/DebugLib.h>\r
20#include <Library/IoLib.h>\r
21#include <Library/QemuFwCfgLib.h>\r
22#include <Library/MemoryAllocationLib.h>\r
23#include <Library/UefiBootServicesTableLib.h>\r
24\r
5297c0bf
LE
25#include "QemuFwCfgLibInternal.h"\r
26\r
f1ec65ba 27\r
28/**\r
29 Reads an 8-bit I/O port fifo into a block of memory.\r
30\r
31 Reads the 8-bit I/O fifo port specified by Port.\r
32\r
33 The port is read Count times, and the read data is\r
34 stored in the provided Buffer.\r
35\r
36 This function must guarantee that all I/O read and write operations are\r
37 serialized.\r
38\r
39 If 8-bit I/O port operations are not supported, then ASSERT().\r
40\r
41 @param Port The I/O port to read.\r
42 @param Count The number of times to read I/O port.\r
43 @param Buffer The buffer to store the read data into.\r
44\r
45**/\r
46VOID\r
47EFIAPI\r
48IoReadFifo8 (\r
49 IN UINTN Port,\r
50 IN UINTN Count,\r
51 OUT VOID *Buffer\r
52 );\r
53\r
29874a8c 54/**\r
55 Writes an 8-bit I/O port fifo from a block of memory.\r
56\r
57 Writes the 8-bit I/O fifo port specified by Port.\r
58\r
59 The port is written Count times, and the data are obtained\r
60 from the provided Buffer.\r
61\r
62 This function must guarantee that all I/O read and write operations are\r
63 serialized.\r
64\r
65 If 8-bit I/O port operations are not supported, then ASSERT().\r
66\r
67 @param Port The I/O port to read.\r
68 @param Count The number of times to read I/O port.\r
69 @param Buffer The buffer to store the read data into.\r
70\r
71**/\r
72VOID\r
73EFIAPI\r
74IoWriteFifo8 (\r
75 IN UINTN Port,\r
76 IN UINTN Count,\r
77 OUT VOID *Buffer\r
78 );\r
79\r
f1ec65ba 80\r
f1ec65ba 81/**\r
82 Selects a firmware configuration item for reading.\r
83 \r
84 Following this call, any data read from this item will start from\r
85 the beginning of the configuration item's data.\r
86\r
87 @param[in] QemuFwCfgItem - Firmware Configuration item to read\r
88\r
89**/\r
90VOID\r
91EFIAPI\r
92QemuFwCfgSelectItem (\r
93 IN FIRMWARE_CONFIG_ITEM QemuFwCfgItem\r
94 )\r
95{\r
96 DEBUG ((EFI_D_INFO, "Select Item: 0x%x\n", (UINT16)(UINTN) QemuFwCfgItem));\r
97 IoWrite16 (0x510, (UINT16)(UINTN) QemuFwCfgItem);\r
98}\r
99\r
100\r
101/**\r
102 Reads firmware configuration bytes into a buffer\r
103\r
104 @param[in] Size - Size in bytes to read\r
105 @param[in] Buffer - Buffer to store data into (OPTIONAL if Size is 0)\r
106\r
107**/\r
108VOID\r
109EFIAPI\r
110InternalQemuFwCfgReadBytes (\r
111 IN UINTN Size,\r
112 IN VOID *Buffer OPTIONAL\r
113 )\r
114{\r
115 IoReadFifo8 (0x511, Size, Buffer);\r
116}\r
117\r
118\r
119/**\r
120 Reads firmware configuration bytes into a buffer\r
121\r
122 If called multiple times, then the data read will\r
123 continue at the offset of the firmware configuration\r
124 item where the previous read ended.\r
125\r
126 @param[in] Size - Size in bytes to read\r
127 @param[in] Buffer - Buffer to store data into\r
128\r
129**/\r
130VOID\r
131EFIAPI\r
132QemuFwCfgReadBytes (\r
133 IN UINTN Size,\r
134 IN VOID *Buffer\r
135 )\r
136{\r
0dc231c9 137 if (InternalQemuFwCfgIsAvailable ()) {\r
f1ec65ba 138 InternalQemuFwCfgReadBytes (Size, Buffer);\r
139 } else {\r
140 ZeroMem (Buffer, Size);\r
141 }\r
142}\r
143\r
29874a8c 144/**\r
145 Write firmware configuration bytes from a buffer\r
146\r
147 If called multiple times, then the data written will\r
148 continue at the offset of the firmware configuration\r
149 item where the previous write ended.\r
150\r
151 @param[in] Size - Size in bytes to write\r
152 @param[in] Buffer - Buffer to read data from\r
153\r
154**/\r
155VOID\r
156EFIAPI\r
157QemuFwCfgWriteBytes (\r
158 IN UINTN Size,\r
159 IN VOID *Buffer\r
160 )\r
161{\r
0dc231c9 162 if (InternalQemuFwCfgIsAvailable ()) {\r
29874a8c 163 IoWriteFifo8 (0x511, Size, Buffer);\r
164 }\r
165}\r
166\r
f1ec65ba 167\r
168/**\r
169 Reads a UINT8 firmware configuration value\r
170\r
171 @return Value of Firmware Configuration item read\r
172\r
173**/\r
174UINT8\r
175EFIAPI\r
176QemuFwCfgRead8 (\r
177 VOID\r
178 )\r
179{\r
180 UINT8 Result;\r
181\r
182 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
183\r
184 return Result;\r
185}\r
186\r
187\r
188/**\r
189 Reads a UINT16 firmware configuration value\r
190\r
191 @return Value of Firmware Configuration item read\r
192\r
193**/\r
194UINT16\r
195EFIAPI\r
196QemuFwCfgRead16 (\r
197 VOID\r
198 )\r
199{\r
200 UINT16 Result;\r
201\r
202 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
203\r
204 return Result;\r
205}\r
206\r
207\r
208/**\r
209 Reads a UINT32 firmware configuration value\r
210\r
211 @return Value of Firmware Configuration item read\r
212\r
213**/\r
214UINT32\r
215EFIAPI\r
216QemuFwCfgRead32 (\r
217 VOID\r
218 )\r
219{\r
220 UINT32 Result;\r
221\r
222 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
223\r
224 return Result;\r
225}\r
226\r
227\r
228/**\r
229 Reads a UINT64 firmware configuration value\r
230\r
231 @return Value of Firmware Configuration item read\r
232\r
233**/\r
234UINT64\r
235EFIAPI\r
236QemuFwCfgRead64 (\r
237 VOID\r
238 )\r
239{\r
240 UINT64 Result;\r
241\r
242 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
243\r
244 return Result;\r
245}\r
246\r
247\r
0ac9bc9b 248/**\r
249 Find the configuration item corresponding to the firmware configuration file.\r
250\r
251 @param[in] Name - Name of file to look up.\r
252 @param[out] Item - Configuration item corresponding to the file, to be passed\r
253 to QemuFwCfgSelectItem ().\r
254 @param[out] Size - Number of bytes in the file.\r
255\r
256 @return RETURN_SUCCESS If file is found.\r
257 RETURN_NOT_FOUND If file is not found.\r
258 RETURN_UNSUPPORTED If firmware configuration is unavailable.\r
259\r
260**/\r
261RETURN_STATUS\r
262EFIAPI\r
263QemuFwCfgFindFile (\r
264 IN CONST CHAR8 *Name,\r
265 OUT FIRMWARE_CONFIG_ITEM *Item,\r
266 OUT UINTN *Size\r
267 )\r
268{\r
269 UINT32 Count;\r
270 UINT32 Idx;\r
271\r
0dc231c9 272 if (!InternalQemuFwCfgIsAvailable ()) {\r
0ac9bc9b 273 return RETURN_UNSUPPORTED;\r
274 }\r
275\r
276 QemuFwCfgSelectItem (QemuFwCfgItemFileDir);\r
277 Count = SwapBytes32 (QemuFwCfgRead32 ());\r
278\r
279 for (Idx = 0; Idx < Count; ++Idx) {\r
280 UINT32 FileSize;\r
281 UINT16 FileSelect;\r
282 UINT16 FileReserved;\r
6a904296 283 CHAR8 FName[QEMU_FW_CFG_FNAME_SIZE];\r
0ac9bc9b 284\r
285 FileSize = QemuFwCfgRead32 ();\r
286 FileSelect = QemuFwCfgRead16 ();\r
287 FileReserved = QemuFwCfgRead16 ();\r
c6910aed 288 (VOID) FileReserved; /* Force a do-nothing reference. */\r
0ac9bc9b 289 InternalQemuFwCfgReadBytes (sizeof (FName), FName);\r
290\r
291 if (AsciiStrCmp (Name, FName) == 0) {\r
292 *Item = SwapBytes16 (FileSelect);\r
293 *Size = SwapBytes32 (FileSize);\r
294 return RETURN_SUCCESS;\r
295 }\r
296 }\r
297\r
298 return RETURN_NOT_FOUND;\r
299}\r
14eb7a5b
LE
300\r
301\r
302/**\r
303 Determine if S3 support is explicitly enabled.\r
304\r
305 @retval TRUE if S3 support is explicitly enabled.\r
306 FALSE otherwise. This includes unavailability of the firmware\r
307 configuration interface.\r
308**/\r
309BOOLEAN\r
310EFIAPI\r
311QemuFwCfgS3Enabled (\r
312 VOID\r
313 )\r
314{\r
315 RETURN_STATUS Status;\r
316 FIRMWARE_CONFIG_ITEM FwCfgItem;\r
317 UINTN FwCfgSize;\r
318 UINT8 SystemStates[6];\r
319\r
320 Status = QemuFwCfgFindFile ("etc/system-states", &FwCfgItem, &FwCfgSize);\r
321 if (Status != RETURN_SUCCESS || FwCfgSize != sizeof SystemStates) {\r
322 return FALSE;\r
323 }\r
324 QemuFwCfgSelectItem (FwCfgItem);\r
325 QemuFwCfgReadBytes (sizeof SystemStates, SystemStates);\r
326 return (BOOLEAN) (SystemStates[3] & BIT7);\r
327}\r