xref: /openbmc/qemu/tests/qtest/i440fx-test.c (revision 1e8a1fae7464ef79c9e50aa0f807d2c511be3c8e)
1*1e8a1faeSThomas Huth /*
2*1e8a1faeSThomas Huth  * qtest I440FX test case
3*1e8a1faeSThomas Huth  *
4*1e8a1faeSThomas Huth  * Copyright IBM, Corp. 2012-2013
5*1e8a1faeSThomas Huth  * Copyright Red Hat, Inc. 2013
6*1e8a1faeSThomas Huth  *
7*1e8a1faeSThomas Huth  * Authors:
8*1e8a1faeSThomas Huth  *  Anthony Liguori   <aliguori@us.ibm.com>
9*1e8a1faeSThomas Huth  *  Laszlo Ersek      <lersek@redhat.com>
10*1e8a1faeSThomas Huth  *
11*1e8a1faeSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
12*1e8a1faeSThomas Huth  * See the COPYING file in the top-level directory.
13*1e8a1faeSThomas Huth  */
14*1e8a1faeSThomas Huth 
15*1e8a1faeSThomas Huth #include "qemu/osdep.h"
16*1e8a1faeSThomas Huth 
17*1e8a1faeSThomas Huth #include "libqtest-single.h"
18*1e8a1faeSThomas Huth #include "libqos/pci.h"
19*1e8a1faeSThomas Huth #include "libqos/pci-pc.h"
20*1e8a1faeSThomas Huth #include "hw/pci/pci_regs.h"
21*1e8a1faeSThomas Huth 
22*1e8a1faeSThomas Huth #define BROKEN 1
23*1e8a1faeSThomas Huth 
24*1e8a1faeSThomas Huth typedef struct TestData
25*1e8a1faeSThomas Huth {
26*1e8a1faeSThomas Huth     int num_cpus;
27*1e8a1faeSThomas Huth } TestData;
28*1e8a1faeSThomas Huth 
29*1e8a1faeSThomas Huth typedef struct FirmwareTestFixture {
30*1e8a1faeSThomas Huth     /* decides whether we're testing -bios or -pflash */
31*1e8a1faeSThomas Huth     bool is_bios;
32*1e8a1faeSThomas Huth } FirmwareTestFixture;
33*1e8a1faeSThomas Huth 
34*1e8a1faeSThomas Huth static QPCIBus *test_start_get_bus(const TestData *s)
35*1e8a1faeSThomas Huth {
36*1e8a1faeSThomas Huth     char *cmdline;
37*1e8a1faeSThomas Huth 
38*1e8a1faeSThomas Huth     cmdline = g_strdup_printf("-smp %d", s->num_cpus);
39*1e8a1faeSThomas Huth     qtest_start(cmdline);
40*1e8a1faeSThomas Huth     g_free(cmdline);
41*1e8a1faeSThomas Huth     return qpci_new_pc(global_qtest, NULL);
42*1e8a1faeSThomas Huth }
43*1e8a1faeSThomas Huth 
44*1e8a1faeSThomas Huth static void test_i440fx_defaults(gconstpointer opaque)
45*1e8a1faeSThomas Huth {
46*1e8a1faeSThomas Huth     const TestData *s = opaque;
47*1e8a1faeSThomas Huth     QPCIBus *bus;
48*1e8a1faeSThomas Huth     QPCIDevice *dev;
49*1e8a1faeSThomas Huth     uint32_t value;
50*1e8a1faeSThomas Huth 
51*1e8a1faeSThomas Huth     bus = test_start_get_bus(s);
52*1e8a1faeSThomas Huth     dev = qpci_device_find(bus, QPCI_DEVFN(0, 0));
53*1e8a1faeSThomas Huth     g_assert(dev != NULL);
54*1e8a1faeSThomas Huth 
55*1e8a1faeSThomas Huth     /* 3.2.2 */
56*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readw(dev, PCI_VENDOR_ID), ==, 0x8086);
57*1e8a1faeSThomas Huth     /* 3.2.3 */
58*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readw(dev, PCI_DEVICE_ID), ==, 0x1237);
59*1e8a1faeSThomas Huth #ifndef BROKEN
60*1e8a1faeSThomas Huth     /* 3.2.4 */
61*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readw(dev, PCI_COMMAND), ==, 0x0006);
62*1e8a1faeSThomas Huth     /* 3.2.5 */
63*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readw(dev, PCI_STATUS), ==, 0x0280);
64*1e8a1faeSThomas Huth #endif
65*1e8a1faeSThomas Huth     /* 3.2.7 */
66*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, PCI_CLASS_PROG), ==, 0x00);
67*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readw(dev, PCI_CLASS_DEVICE), ==, 0x0600);
68*1e8a1faeSThomas Huth     /* 3.2.8 */
69*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, PCI_LATENCY_TIMER), ==, 0x00);
70*1e8a1faeSThomas Huth     /* 3.2.9 */
71*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, PCI_HEADER_TYPE), ==, 0x00);
72*1e8a1faeSThomas Huth     /* 3.2.10 */
73*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, PCI_BIST), ==, 0x00);
74*1e8a1faeSThomas Huth 
75*1e8a1faeSThomas Huth     /* 3.2.11 */
76*1e8a1faeSThomas Huth     value = qpci_config_readw(dev, 0x50); /* PMCCFG */
77*1e8a1faeSThomas Huth     if (s->num_cpus == 1) { /* WPE */
78*1e8a1faeSThomas Huth         g_assert(!(value & (1 << 15)));
79*1e8a1faeSThomas Huth     } else {
80*1e8a1faeSThomas Huth         g_assert((value & (1 << 15)));
81*1e8a1faeSThomas Huth     }
82*1e8a1faeSThomas Huth 
83*1e8a1faeSThomas Huth     g_assert(!(value & (1 << 6))); /* EPTE */
84*1e8a1faeSThomas Huth 
85*1e8a1faeSThomas Huth     /* 3.2.12 */
86*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x52), ==, 0x00); /* DETURBO */
87*1e8a1faeSThomas Huth     /* 3.2.13 */
88*1e8a1faeSThomas Huth #ifndef BROKEN
89*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x53), ==, 0x80); /* DBC */
90*1e8a1faeSThomas Huth #endif
91*1e8a1faeSThomas Huth     /* 3.2.14 */
92*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x54), ==, 0x00); /* AXC */
93*1e8a1faeSThomas Huth     /* 3.2.15 */
94*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readw(dev, 0x55), ==, 0x0000); /* DRT */
95*1e8a1faeSThomas Huth #ifndef BROKEN
96*1e8a1faeSThomas Huth     /* 3.2.16 */
97*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x57), ==, 0x01); /* DRAMC */
98*1e8a1faeSThomas Huth     /* 3.2.17 */
99*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x58), ==, 0x10); /* DRAMT */
100*1e8a1faeSThomas Huth #endif
101*1e8a1faeSThomas Huth     /* 3.2.18 */
102*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x59), ==, 0x00); /* PAM0 */
103*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x5A), ==, 0x00); /* PAM1 */
104*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x5B), ==, 0x00); /* PAM2 */
105*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x5C), ==, 0x00); /* PAM3 */
106*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x5D), ==, 0x00); /* PAM4 */
107*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x5E), ==, 0x00); /* PAM5 */
108*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x5F), ==, 0x00); /* PAM6 */
109*1e8a1faeSThomas Huth #ifndef BROKEN
110*1e8a1faeSThomas Huth     /* 3.2.19 */
111*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x60), ==, 0x01); /* DRB0 */
112*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x61), ==, 0x01); /* DRB1 */
113*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x62), ==, 0x01); /* DRB2 */
114*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x63), ==, 0x01); /* DRB3 */
115*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x64), ==, 0x01); /* DRB4 */
116*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x65), ==, 0x01); /* DRB5 */
117*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x66), ==, 0x01); /* DRB6 */
118*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x67), ==, 0x01); /* DRB7 */
119*1e8a1faeSThomas Huth #endif
120*1e8a1faeSThomas Huth     /* 3.2.20 */
121*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x68), ==, 0x00); /* FDHC */
122*1e8a1faeSThomas Huth     /* 3.2.21 */
123*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x70), ==, 0x00); /* MTT */
124*1e8a1faeSThomas Huth #ifndef BROKEN
125*1e8a1faeSThomas Huth     /* 3.2.22 */
126*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x71), ==, 0x10); /* CLT */
127*1e8a1faeSThomas Huth #endif
128*1e8a1faeSThomas Huth     /* 3.2.23 */
129*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x72), ==, 0x02); /* SMRAM */
130*1e8a1faeSThomas Huth     /* 3.2.24 */
131*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x90), ==, 0x00); /* ERRCMD */
132*1e8a1faeSThomas Huth     /* 3.2.25 */
133*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x91), ==, 0x00); /* ERRSTS */
134*1e8a1faeSThomas Huth     /* 3.2.26 */
135*1e8a1faeSThomas Huth     g_assert_cmpint(qpci_config_readb(dev, 0x93), ==, 0x00); /* TRC */
136*1e8a1faeSThomas Huth 
137*1e8a1faeSThomas Huth     g_free(dev);
138*1e8a1faeSThomas Huth     qpci_free_pc(bus);
139*1e8a1faeSThomas Huth     qtest_end();
140*1e8a1faeSThomas Huth }
141*1e8a1faeSThomas Huth 
142*1e8a1faeSThomas Huth #define PAM_RE 1
143*1e8a1faeSThomas Huth #define PAM_WE 2
144*1e8a1faeSThomas Huth 
145*1e8a1faeSThomas Huth static void pam_set(QPCIDevice *dev, int index, int flags)
146*1e8a1faeSThomas Huth {
147*1e8a1faeSThomas Huth     int regno = 0x59 + (index / 2);
148*1e8a1faeSThomas Huth     uint8_t reg;
149*1e8a1faeSThomas Huth 
150*1e8a1faeSThomas Huth     reg = qpci_config_readb(dev, regno);
151*1e8a1faeSThomas Huth     if (index & 1) {
152*1e8a1faeSThomas Huth         reg = (reg & 0x0F) | (flags << 4);
153*1e8a1faeSThomas Huth     } else {
154*1e8a1faeSThomas Huth         reg = (reg & 0xF0) | flags;
155*1e8a1faeSThomas Huth     }
156*1e8a1faeSThomas Huth     qpci_config_writeb(dev, regno, reg);
157*1e8a1faeSThomas Huth }
158*1e8a1faeSThomas Huth 
159*1e8a1faeSThomas Huth static gboolean verify_area(uint32_t start, uint32_t end, uint8_t value)
160*1e8a1faeSThomas Huth {
161*1e8a1faeSThomas Huth     uint32_t size = end - start + 1;
162*1e8a1faeSThomas Huth     gboolean ret = TRUE;
163*1e8a1faeSThomas Huth     uint8_t *data;
164*1e8a1faeSThomas Huth     int i;
165*1e8a1faeSThomas Huth 
166*1e8a1faeSThomas Huth     data = g_malloc0(size);
167*1e8a1faeSThomas Huth     memread(start, data, size);
168*1e8a1faeSThomas Huth 
169*1e8a1faeSThomas Huth     g_test_message("verify_area: data[0] = 0x%x", data[0]);
170*1e8a1faeSThomas Huth 
171*1e8a1faeSThomas Huth     for (i = 0; i < size; i++) {
172*1e8a1faeSThomas Huth         if (data[i] != value) {
173*1e8a1faeSThomas Huth             ret = FALSE;
174*1e8a1faeSThomas Huth             break;
175*1e8a1faeSThomas Huth         }
176*1e8a1faeSThomas Huth     }
177*1e8a1faeSThomas Huth 
178*1e8a1faeSThomas Huth     g_free(data);
179*1e8a1faeSThomas Huth 
180*1e8a1faeSThomas Huth     return ret;
181*1e8a1faeSThomas Huth }
182*1e8a1faeSThomas Huth 
183*1e8a1faeSThomas Huth static void write_area(uint32_t start, uint32_t end, uint8_t value)
184*1e8a1faeSThomas Huth {
185*1e8a1faeSThomas Huth     uint32_t size = end - start + 1;
186*1e8a1faeSThomas Huth     uint8_t *data;
187*1e8a1faeSThomas Huth 
188*1e8a1faeSThomas Huth     data = g_malloc(size);
189*1e8a1faeSThomas Huth     memset(data, value, size);
190*1e8a1faeSThomas Huth     memwrite(start, data, size);
191*1e8a1faeSThomas Huth 
192*1e8a1faeSThomas Huth     g_free(data);
193*1e8a1faeSThomas Huth }
194*1e8a1faeSThomas Huth 
195*1e8a1faeSThomas Huth static void test_i440fx_pam(gconstpointer opaque)
196*1e8a1faeSThomas Huth {
197*1e8a1faeSThomas Huth     const TestData *s = opaque;
198*1e8a1faeSThomas Huth     QPCIBus *bus;
199*1e8a1faeSThomas Huth     QPCIDevice *dev;
200*1e8a1faeSThomas Huth     int i;
201*1e8a1faeSThomas Huth     static struct {
202*1e8a1faeSThomas Huth         uint32_t start;
203*1e8a1faeSThomas Huth         uint32_t end;
204*1e8a1faeSThomas Huth     } pam_area[] = {
205*1e8a1faeSThomas Huth         { 0, 0 },             /* Reserved */
206*1e8a1faeSThomas Huth         { 0xF0000, 0xFFFFF }, /* BIOS Area */
207*1e8a1faeSThomas Huth         { 0xC0000, 0xC3FFF }, /* Option ROM */
208*1e8a1faeSThomas Huth         { 0xC4000, 0xC7FFF }, /* Option ROM */
209*1e8a1faeSThomas Huth         { 0xC8000, 0xCBFFF }, /* Option ROM */
210*1e8a1faeSThomas Huth         { 0xCC000, 0xCFFFF }, /* Option ROM */
211*1e8a1faeSThomas Huth         { 0xD0000, 0xD3FFF }, /* Option ROM */
212*1e8a1faeSThomas Huth         { 0xD4000, 0xD7FFF }, /* Option ROM */
213*1e8a1faeSThomas Huth         { 0xD8000, 0xDBFFF }, /* Option ROM */
214*1e8a1faeSThomas Huth         { 0xDC000, 0xDFFFF }, /* Option ROM */
215*1e8a1faeSThomas Huth         { 0xE0000, 0xE3FFF }, /* BIOS Extension */
216*1e8a1faeSThomas Huth         { 0xE4000, 0xE7FFF }, /* BIOS Extension */
217*1e8a1faeSThomas Huth         { 0xE8000, 0xEBFFF }, /* BIOS Extension */
218*1e8a1faeSThomas Huth         { 0xEC000, 0xEFFFF }, /* BIOS Extension */
219*1e8a1faeSThomas Huth     };
220*1e8a1faeSThomas Huth 
221*1e8a1faeSThomas Huth     bus = test_start_get_bus(s);
222*1e8a1faeSThomas Huth     dev = qpci_device_find(bus, QPCI_DEVFN(0, 0));
223*1e8a1faeSThomas Huth     g_assert(dev != NULL);
224*1e8a1faeSThomas Huth 
225*1e8a1faeSThomas Huth     for (i = 0; i < ARRAY_SIZE(pam_area); i++) {
226*1e8a1faeSThomas Huth         if (pam_area[i].start == pam_area[i].end) {
227*1e8a1faeSThomas Huth             continue;
228*1e8a1faeSThomas Huth         }
229*1e8a1faeSThomas Huth 
230*1e8a1faeSThomas Huth         g_test_message("Checking area 0x%05x..0x%05x",
231*1e8a1faeSThomas Huth                        pam_area[i].start, pam_area[i].end);
232*1e8a1faeSThomas Huth         /* Switch to RE for the area */
233*1e8a1faeSThomas Huth         pam_set(dev, i, PAM_RE);
234*1e8a1faeSThomas Huth         /* Verify the RAM is all zeros */
235*1e8a1faeSThomas Huth         g_assert(verify_area(pam_area[i].start, pam_area[i].end, 0));
236*1e8a1faeSThomas Huth 
237*1e8a1faeSThomas Huth         /* Switch to WE for the area */
238*1e8a1faeSThomas Huth         pam_set(dev, i, PAM_RE | PAM_WE);
239*1e8a1faeSThomas Huth         /* Write out a non-zero mask to the full area */
240*1e8a1faeSThomas Huth         write_area(pam_area[i].start, pam_area[i].end, 0x42);
241*1e8a1faeSThomas Huth 
242*1e8a1faeSThomas Huth #ifndef BROKEN
243*1e8a1faeSThomas Huth         /* QEMU only supports a limited form of PAM */
244*1e8a1faeSThomas Huth 
245*1e8a1faeSThomas Huth         /* Switch to !RE for the area */
246*1e8a1faeSThomas Huth         pam_set(dev, i, PAM_WE);
247*1e8a1faeSThomas Huth         /* Verify the area is not our mask */
248*1e8a1faeSThomas Huth         g_assert(!verify_area(pam_area[i].start, pam_area[i].end, 0x42));
249*1e8a1faeSThomas Huth #endif
250*1e8a1faeSThomas Huth 
251*1e8a1faeSThomas Huth         /* Verify the area is our new mask */
252*1e8a1faeSThomas Huth         g_assert(verify_area(pam_area[i].start, pam_area[i].end, 0x42));
253*1e8a1faeSThomas Huth 
254*1e8a1faeSThomas Huth         /* Write out a new mask */
255*1e8a1faeSThomas Huth         write_area(pam_area[i].start, pam_area[i].end, 0x82);
256*1e8a1faeSThomas Huth 
257*1e8a1faeSThomas Huth #ifndef BROKEN
258*1e8a1faeSThomas Huth         /* QEMU only supports a limited form of PAM */
259*1e8a1faeSThomas Huth 
260*1e8a1faeSThomas Huth         /* Verify the area is not our mask */
261*1e8a1faeSThomas Huth         g_assert(!verify_area(pam_area[i].start, pam_area[i].end, 0x82));
262*1e8a1faeSThomas Huth 
263*1e8a1faeSThomas Huth         /* Switch to RE for the area */
264*1e8a1faeSThomas Huth         pam_set(dev, i, PAM_RE | PAM_WE);
265*1e8a1faeSThomas Huth #endif
266*1e8a1faeSThomas Huth         /* Verify the area is our new mask */
267*1e8a1faeSThomas Huth         g_assert(verify_area(pam_area[i].start, pam_area[i].end, 0x82));
268*1e8a1faeSThomas Huth 
269*1e8a1faeSThomas Huth         /* Reset area */
270*1e8a1faeSThomas Huth         pam_set(dev, i, 0);
271*1e8a1faeSThomas Huth 
272*1e8a1faeSThomas Huth         /* Verify the area is not our new mask */
273*1e8a1faeSThomas Huth         g_assert(!verify_area(pam_area[i].start, pam_area[i].end, 0x82));
274*1e8a1faeSThomas Huth     }
275*1e8a1faeSThomas Huth 
276*1e8a1faeSThomas Huth     g_free(dev);
277*1e8a1faeSThomas Huth     qpci_free_pc(bus);
278*1e8a1faeSThomas Huth     qtest_end();
279*1e8a1faeSThomas Huth }
280*1e8a1faeSThomas Huth 
281*1e8a1faeSThomas Huth #define BLOB_SIZE ((size_t)65536)
282*1e8a1faeSThomas Huth #define ISA_BIOS_MAXSZ ((size_t)(128 * 1024))
283*1e8a1faeSThomas Huth 
284*1e8a1faeSThomas Huth /* Create a blob file, and return its absolute pathname as a dynamically
285*1e8a1faeSThomas Huth  * allocated string.
286*1e8a1faeSThomas Huth  * The file is closed before the function returns.
287*1e8a1faeSThomas Huth  * In case of error, NULL is returned. The function prints the error message.
288*1e8a1faeSThomas Huth  */
289*1e8a1faeSThomas Huth static char *create_blob_file(void)
290*1e8a1faeSThomas Huth {
291*1e8a1faeSThomas Huth     int ret, fd;
292*1e8a1faeSThomas Huth     char *pathname;
293*1e8a1faeSThomas Huth     GError *error = NULL;
294*1e8a1faeSThomas Huth 
295*1e8a1faeSThomas Huth     ret = -1;
296*1e8a1faeSThomas Huth     fd = g_file_open_tmp("blob_XXXXXX", &pathname, &error);
297*1e8a1faeSThomas Huth     if (fd == -1) {
298*1e8a1faeSThomas Huth         fprintf(stderr, "unable to create blob file: %s\n", error->message);
299*1e8a1faeSThomas Huth         g_error_free(error);
300*1e8a1faeSThomas Huth     } else {
301*1e8a1faeSThomas Huth         if (ftruncate(fd, BLOB_SIZE) == -1) {
302*1e8a1faeSThomas Huth             fprintf(stderr, "ftruncate(\"%s\", %zu): %s\n", pathname,
303*1e8a1faeSThomas Huth                     BLOB_SIZE, strerror(errno));
304*1e8a1faeSThomas Huth         } else {
305*1e8a1faeSThomas Huth             void *buf;
306*1e8a1faeSThomas Huth 
307*1e8a1faeSThomas Huth             buf = mmap(NULL, BLOB_SIZE, PROT_WRITE, MAP_SHARED, fd, 0);
308*1e8a1faeSThomas Huth             if (buf == MAP_FAILED) {
309*1e8a1faeSThomas Huth                 fprintf(stderr, "mmap(\"%s\", %zu): %s\n", pathname, BLOB_SIZE,
310*1e8a1faeSThomas Huth                         strerror(errno));
311*1e8a1faeSThomas Huth             } else {
312*1e8a1faeSThomas Huth                 size_t i;
313*1e8a1faeSThomas Huth 
314*1e8a1faeSThomas Huth                 for (i = 0; i < BLOB_SIZE; ++i) {
315*1e8a1faeSThomas Huth                     ((uint8_t *)buf)[i] = i;
316*1e8a1faeSThomas Huth                 }
317*1e8a1faeSThomas Huth                 munmap(buf, BLOB_SIZE);
318*1e8a1faeSThomas Huth                 ret = 0;
319*1e8a1faeSThomas Huth             }
320*1e8a1faeSThomas Huth         }
321*1e8a1faeSThomas Huth         close(fd);
322*1e8a1faeSThomas Huth         if (ret == -1) {
323*1e8a1faeSThomas Huth             unlink(pathname);
324*1e8a1faeSThomas Huth             g_free(pathname);
325*1e8a1faeSThomas Huth         }
326*1e8a1faeSThomas Huth     }
327*1e8a1faeSThomas Huth 
328*1e8a1faeSThomas Huth     return ret == -1 ? NULL : pathname;
329*1e8a1faeSThomas Huth }
330*1e8a1faeSThomas Huth 
331*1e8a1faeSThomas Huth static void test_i440fx_firmware(FirmwareTestFixture *fixture,
332*1e8a1faeSThomas Huth                                  gconstpointer user_data)
333*1e8a1faeSThomas Huth {
334*1e8a1faeSThomas Huth     char *fw_pathname, *cmdline;
335*1e8a1faeSThomas Huth     uint8_t *buf;
336*1e8a1faeSThomas Huth     size_t i, isa_bios_size;
337*1e8a1faeSThomas Huth 
338*1e8a1faeSThomas Huth     fw_pathname = create_blob_file();
339*1e8a1faeSThomas Huth     g_assert(fw_pathname != NULL);
340*1e8a1faeSThomas Huth 
341*1e8a1faeSThomas Huth     /* Better hope the user didn't put metacharacters in TMPDIR and co. */
342*1e8a1faeSThomas Huth     cmdline = g_strdup_printf("-S %s%s", fixture->is_bios
343*1e8a1faeSThomas Huth                                          ? "-bios "
344*1e8a1faeSThomas Huth                                          : "-drive if=pflash,format=raw,file=",
345*1e8a1faeSThomas Huth                               fw_pathname);
346*1e8a1faeSThomas Huth     g_test_message("qemu cmdline: %s", cmdline);
347*1e8a1faeSThomas Huth     qtest_start(cmdline);
348*1e8a1faeSThomas Huth     g_free(cmdline);
349*1e8a1faeSThomas Huth 
350*1e8a1faeSThomas Huth     /* QEMU has loaded the firmware (because qtest_start() only returns after
351*1e8a1faeSThomas Huth      * the QMP handshake completes). We must unlink the firmware blob right
352*1e8a1faeSThomas Huth      * here, because any assertion firing below would leak it in the
353*1e8a1faeSThomas Huth      * filesystem. This is also the reason why we recreate the blob every time
354*1e8a1faeSThomas Huth      * this function is invoked.
355*1e8a1faeSThomas Huth      */
356*1e8a1faeSThomas Huth     unlink(fw_pathname);
357*1e8a1faeSThomas Huth     g_free(fw_pathname);
358*1e8a1faeSThomas Huth 
359*1e8a1faeSThomas Huth     /* check below 4G */
360*1e8a1faeSThomas Huth     buf = g_malloc0(BLOB_SIZE);
361*1e8a1faeSThomas Huth     memread(0x100000000ULL - BLOB_SIZE, buf, BLOB_SIZE);
362*1e8a1faeSThomas Huth     for (i = 0; i < BLOB_SIZE; ++i) {
363*1e8a1faeSThomas Huth         g_assert_cmphex(buf[i], ==, (uint8_t)i);
364*1e8a1faeSThomas Huth     }
365*1e8a1faeSThomas Huth 
366*1e8a1faeSThomas Huth     /* check in ISA space too */
367*1e8a1faeSThomas Huth     memset(buf, 0, BLOB_SIZE);
368*1e8a1faeSThomas Huth     isa_bios_size = ISA_BIOS_MAXSZ < BLOB_SIZE ? ISA_BIOS_MAXSZ : BLOB_SIZE;
369*1e8a1faeSThomas Huth     memread(0x100000 - isa_bios_size, buf, isa_bios_size);
370*1e8a1faeSThomas Huth     for (i = 0; i < isa_bios_size; ++i) {
371*1e8a1faeSThomas Huth         g_assert_cmphex(buf[i], ==,
372*1e8a1faeSThomas Huth                         (uint8_t)((BLOB_SIZE - isa_bios_size) + i));
373*1e8a1faeSThomas Huth     }
374*1e8a1faeSThomas Huth 
375*1e8a1faeSThomas Huth     g_free(buf);
376*1e8a1faeSThomas Huth     qtest_end();
377*1e8a1faeSThomas Huth }
378*1e8a1faeSThomas Huth 
379*1e8a1faeSThomas Huth static void add_firmware_test(const char *testpath,
380*1e8a1faeSThomas Huth                               void (*setup_fixture)(FirmwareTestFixture *f,
381*1e8a1faeSThomas Huth                                                     gconstpointer test_data))
382*1e8a1faeSThomas Huth {
383*1e8a1faeSThomas Huth     qtest_add(testpath, FirmwareTestFixture, NULL, setup_fixture,
384*1e8a1faeSThomas Huth               test_i440fx_firmware, NULL);
385*1e8a1faeSThomas Huth }
386*1e8a1faeSThomas Huth 
387*1e8a1faeSThomas Huth static void request_bios(FirmwareTestFixture *fixture,
388*1e8a1faeSThomas Huth                          gconstpointer user_data)
389*1e8a1faeSThomas Huth {
390*1e8a1faeSThomas Huth     fixture->is_bios = true;
391*1e8a1faeSThomas Huth }
392*1e8a1faeSThomas Huth 
393*1e8a1faeSThomas Huth static void request_pflash(FirmwareTestFixture *fixture,
394*1e8a1faeSThomas Huth                            gconstpointer user_data)
395*1e8a1faeSThomas Huth {
396*1e8a1faeSThomas Huth     fixture->is_bios = false;
397*1e8a1faeSThomas Huth }
398*1e8a1faeSThomas Huth 
399*1e8a1faeSThomas Huth int main(int argc, char **argv)
400*1e8a1faeSThomas Huth {
401*1e8a1faeSThomas Huth     TestData data;
402*1e8a1faeSThomas Huth 
403*1e8a1faeSThomas Huth     g_test_init(&argc, &argv, NULL);
404*1e8a1faeSThomas Huth 
405*1e8a1faeSThomas Huth     data.num_cpus = 1;
406*1e8a1faeSThomas Huth 
407*1e8a1faeSThomas Huth     qtest_add_data_func("i440fx/defaults", &data, test_i440fx_defaults);
408*1e8a1faeSThomas Huth     qtest_add_data_func("i440fx/pam", &data, test_i440fx_pam);
409*1e8a1faeSThomas Huth     add_firmware_test("i440fx/firmware/bios", request_bios);
410*1e8a1faeSThomas Huth     add_firmware_test("i440fx/firmware/pflash", request_pflash);
411*1e8a1faeSThomas Huth 
412*1e8a1faeSThomas Huth     return g_test_run();
413*1e8a1faeSThomas Huth }
414