1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/*
* libqos fw_cfg support
*
* Copyright IBM, Corp. 2012-2013
* Copyright (C) 2013 Red Hat Inc.
*
* Authors:
* Anthony Liguori <aliguori@us.ibm.com>
* Markus Armbruster <armbru@redhat.com>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "fw_cfg.h"
#include "malloc-pc.h"
#include "libqos-malloc.h"
#include "../libqtest.h"
#include "qemu/bswap.h"
#include "hw/nvram/fw_cfg.h"
void qfw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
{
fw_cfg->select(fw_cfg, key);
}
void qfw_cfg_read_data(QFWCFG *fw_cfg, void *data, size_t len)
{
fw_cfg->read(fw_cfg, data, len);
}
void qfw_cfg_get(QFWCFG *fw_cfg, uint16_t key, void *data, size_t len)
{
qfw_cfg_select(fw_cfg, key);
qfw_cfg_read_data(fw_cfg, data, len);
}
uint16_t qfw_cfg_get_u16(QFWCFG *fw_cfg, uint16_t key)
{
uint16_t value;
qfw_cfg_get(fw_cfg, key, &value, sizeof(value));
return le16_to_cpu(value);
}
uint32_t qfw_cfg_get_u32(QFWCFG *fw_cfg, uint16_t key)
{
uint32_t value;
qfw_cfg_get(fw_cfg, key, &value, sizeof(value));
return le32_to_cpu(value);
}
uint64_t qfw_cfg_get_u64(QFWCFG *fw_cfg, uint16_t key)
{
uint64_t value;
qfw_cfg_get(fw_cfg, key, &value, sizeof(value));
return le64_to_cpu(value);
}
static void mm_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
{
qtest_writew(fw_cfg->qts, fw_cfg->base, key);
}
static void qfw_cfg_dma_transfer(QFWCFG *fw_cfg, QOSState *qs, void *address,
uint32_t length, uint32_t control)
{
FWCfgDmaAccess access;
uint32_t addr;
uint64_t guest_access_addr;
uint64_t gaddr;
/* create a data buffer in guest memory */
gaddr = guest_alloc(&qs->alloc, length);
if (control & FW_CFG_DMA_CTL_WRITE) {
qtest_bufwrite(fw_cfg->qts, gaddr, address, length);
}
access.address = cpu_to_be64(gaddr);
access.length = cpu_to_be32(length);
access.control = cpu_to_be32(control);
/* now create a separate buffer in guest memory for 'access' */
guest_access_addr = guest_alloc(&qs->alloc, sizeof(access));
qtest_bufwrite(fw_cfg->qts, guest_access_addr, &access, sizeof(access));
/* write lower 32 bits of address */
addr = cpu_to_be32((uint32_t)(uintptr_t)guest_access_addr);
qtest_outl(fw_cfg->qts, fw_cfg->base + 8, addr);
/* write upper 32 bits of address */
addr = cpu_to_be32((uint32_t)(uintptr_t)(guest_access_addr >> 32));
qtest_outl(fw_cfg->qts, fw_cfg->base + 4, addr);
g_assert(!(be32_to_cpu(access.control) & FW_CFG_DMA_CTL_ERROR));
if (control & FW_CFG_DMA_CTL_READ) {
qtest_bufread(fw_cfg->qts, gaddr, address, length);
}
guest_free(&qs->alloc, guest_access_addr);
guest_free(&qs->alloc, gaddr);
}
static void qfw_cfg_write_entry(QFWCFG *fw_cfg, QOSState *qs, uint16_t key,
void *buf, uint32_t len)
{
qfw_cfg_select(fw_cfg, key);
qfw_cfg_dma_transfer(fw_cfg, qs, buf, len, FW_CFG_DMA_CTL_WRITE);
}
static void qfw_cfg_read_entry(QFWCFG *fw_cfg, QOSState *qs, uint16_t key,
void *buf, uint32_t len)
{
qfw_cfg_select(fw_cfg, key);
qfw_cfg_dma_transfer(fw_cfg, qs, buf, len, FW_CFG_DMA_CTL_READ);
}
static bool find_pdir_entry(QFWCFG *fw_cfg, const char *filename,
uint16_t *sel, uint32_t *size)
{
g_autofree unsigned char *filesbuf = NULL;
uint32_t count;
size_t dsize;
FWCfgFile *pdir_entry;
uint32_t i;
bool found = false;
*size = 0;
*sel = 0;
qfw_cfg_get(fw_cfg, FW_CFG_FILE_DIR, &count, sizeof(count));
count = be32_to_cpu(count);
dsize = sizeof(uint32_t) + count * sizeof(struct fw_cfg_file);
filesbuf = g_malloc(dsize);
qfw_cfg_get(fw_cfg, FW_CFG_FILE_DIR, filesbuf, dsize);
pdir_entry = (FWCfgFile *)(filesbuf + sizeof(uint32_t));
for (i = 0; i < count; ++i, ++pdir_entry) {
if (!strcmp(pdir_entry->name, filename)) {
*size = be32_to_cpu(pdir_entry->size);
*sel = be16_to_cpu(pdir_entry->select);
found = true;
break;
}
}
return found;
}
/*
* The caller need check the return value. When the return value is
* nonzero, it means that some bytes have been transferred.
*
* If the fw_cfg file in question is smaller than the allocated & passed-in
* buffer, then the buffer has been populated only in part.
*
* If the fw_cfg file in question is larger than the passed-in
* buffer, then the return value explains how much room would have been
* necessary in total. And, while the caller's buffer has been fully
* populated, it has received only a starting slice of the fw_cfg file.
*/
size_t qfw_cfg_get_file(QFWCFG *fw_cfg, const char *filename,
void *data, size_t buflen)
{
size_t filesize = 0;
uint32_t len;
uint16_t sel;
if (find_pdir_entry(fw_cfg, filename, &sel, &len)) {
filesize = len;
if (len > buflen) {
len = buflen;
}
qfw_cfg_get(fw_cfg, sel, data, len);
}
return filesize;
}
/*
* The caller need check the return value. When the return value is
* nonzero, it means that some bytes have been transferred.
*
* If the fw_cfg file in question is smaller than the allocated & passed-in
* buffer, then the first len bytes were read.
*
* If the fw_cfg file in question is larger than the passed-in
* buffer, then the return value explains how much was actually read.
*
* It is illegal to call this function if fw_cfg does not support DMA
* interface. The caller should ensure that DMA is supported before
* calling this function.
*
* Passed QOSState pointer qs must be initialized. qs->alloc must also be
* properly initialized.
*/
size_t qfw_cfg_read_file(QFWCFG *fw_cfg, QOSState *qs, const char *filename,
void *data, size_t buflen)
{
uint32_t len = 0;
uint16_t sel;
uint32_t id;
g_assert(qs);
g_assert(filename);
g_assert(data);
g_assert(buflen);
/* check if DMA is supported since we use DMA for read */
id = qfw_cfg_get_u32(fw_cfg, FW_CFG_ID);
g_assert(id & FW_CFG_VERSION_DMA);
if (find_pdir_entry(fw_cfg, filename, &sel, &len)) {
if (len > buflen) {
len = buflen;
}
qfw_cfg_read_entry(fw_cfg, qs, sel, data, len);
}
return len;
}
/*
* The caller need check the return value. When the return value is
* nonzero, it means that some bytes have been transferred.
*
* If the fw_cfg file in question is smaller than the allocated & passed-in
* buffer, then the buffer has been partially written.
*
* If the fw_cfg file in question is larger than the passed-in
* buffer, then the return value explains how much was actually written.
*
* It is illegal to call this function if fw_cfg does not support DMA
* interface. The caller should ensure that DMA is supported before
* calling this function.
*
* Passed QOSState pointer qs must be initialized. qs->alloc must also be
* properly initialized.
*/
size_t qfw_cfg_write_file(QFWCFG *fw_cfg, QOSState *qs, const char *filename,
void *data, size_t buflen)
{
uint32_t len = 0;
uint16_t sel;
uint32_t id;
g_assert(qs);
g_assert(filename);
g_assert(data);
g_assert(buflen);
/* write operation is only valid if DMA is supported */
id = qfw_cfg_get_u32(fw_cfg, FW_CFG_ID);
g_assert(id & FW_CFG_VERSION_DMA);
if (find_pdir_entry(fw_cfg, filename, &sel, &len)) {
if (len > buflen) {
len = buflen;
}
qfw_cfg_write_entry(fw_cfg, qs, sel, data, len);
}
return len;
}
static void mm_fw_cfg_read(QFWCFG *fw_cfg, void *data, size_t len)
{
uint8_t *ptr = data;
int i;
for (i = 0; i < len; i++) {
ptr[i] = qtest_readb(fw_cfg->qts, fw_cfg->base + 2);
}
}
QFWCFG *mm_fw_cfg_init(QTestState *qts, uint64_t base)
{
QFWCFG *fw_cfg = g_malloc0(sizeof(*fw_cfg));
fw_cfg->base = base;
fw_cfg->qts = qts;
fw_cfg->select = mm_fw_cfg_select;
fw_cfg->read = mm_fw_cfg_read;
return fw_cfg;
}
void mm_fw_cfg_uninit(QFWCFG *fw_cfg)
{
g_free(fw_cfg);
}
static void io_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
{
qtest_outw(fw_cfg->qts, fw_cfg->base, key);
}
static void io_fw_cfg_read(QFWCFG *fw_cfg, void *data, size_t len)
{
uint8_t *ptr = data;
int i;
for (i = 0; i < len; i++) {
ptr[i] = qtest_inb(fw_cfg->qts, fw_cfg->base + 1);
}
}
QFWCFG *io_fw_cfg_init(QTestState *qts, uint16_t base)
{
QFWCFG *fw_cfg = g_malloc0(sizeof(*fw_cfg));
fw_cfg->base = base;
fw_cfg->qts = qts;
fw_cfg->select = io_fw_cfg_select;
fw_cfg->read = io_fw_cfg_read;
return fw_cfg;
}
void io_fw_cfg_uninit(QFWCFG *fw_cfg)
{
g_free(fw_cfg);
}
|