xref: /qemu/tests/qtest/prom-env-test.c (revision fcbf4a3c0c576eec1321f9cff4fa0dd8e0b1a82f)
1*fcbf4a3cSThomas Huth /*
2*fcbf4a3cSThomas Huth  * Test OpenBIOS-based machines.
3*fcbf4a3cSThomas Huth  *
4*fcbf4a3cSThomas Huth  * Copyright (c) 2016 Red Hat Inc.
5*fcbf4a3cSThomas Huth  *
6*fcbf4a3cSThomas Huth  * Author:
7*fcbf4a3cSThomas Huth  *    Thomas Huth <thuth@redhat.com>
8*fcbf4a3cSThomas Huth  *
9*fcbf4a3cSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2
10*fcbf4a3cSThomas Huth  * or later. See the COPYING file in the top-level directory.
11*fcbf4a3cSThomas Huth  *
12*fcbf4a3cSThomas Huth  * This test is used to check that some OpenBIOS machines can be started
13*fcbf4a3cSThomas Huth  * successfully in TCG mode. To do this, we first put some Forth code into
14*fcbf4a3cSThomas Huth  * the "boot-command" Open Firmware environment variable. This Forth code
15*fcbf4a3cSThomas Huth  * writes a well-known magic value to a known location in memory. Then we
16*fcbf4a3cSThomas Huth  * start the guest so that OpenBIOS can boot and finally run the Forth code.
17*fcbf4a3cSThomas Huth  * The testing code here then can finally check whether the value has been
18*fcbf4a3cSThomas Huth  * successfully written into the guest memory.
19*fcbf4a3cSThomas Huth  */
20*fcbf4a3cSThomas Huth 
21*fcbf4a3cSThomas Huth #include "qemu/osdep.h"
22*fcbf4a3cSThomas Huth #include "libqtest.h"
23*fcbf4a3cSThomas Huth 
24*fcbf4a3cSThomas Huth #define MAGIC   0xcafec0de
25*fcbf4a3cSThomas Huth #define ADDRESS 0x4000
26*fcbf4a3cSThomas Huth 
27*fcbf4a3cSThomas Huth static void check_guest_memory(void)
28*fcbf4a3cSThomas Huth {
29*fcbf4a3cSThomas Huth     uint32_t signature;
30*fcbf4a3cSThomas Huth     int i;
31*fcbf4a3cSThomas Huth 
32*fcbf4a3cSThomas Huth     /* Poll until code has run and modified memory. Wait at most 30 seconds */
33*fcbf4a3cSThomas Huth     for (i = 0; i < 3000; ++i) {
34*fcbf4a3cSThomas Huth         signature = readl(ADDRESS);
35*fcbf4a3cSThomas Huth         if (signature == MAGIC) {
36*fcbf4a3cSThomas Huth             break;
37*fcbf4a3cSThomas Huth         }
38*fcbf4a3cSThomas Huth         g_usleep(10000);
39*fcbf4a3cSThomas Huth     }
40*fcbf4a3cSThomas Huth 
41*fcbf4a3cSThomas Huth     g_assert_cmphex(signature, ==, MAGIC);
42*fcbf4a3cSThomas Huth }
43*fcbf4a3cSThomas Huth 
44*fcbf4a3cSThomas Huth static void test_machine(const void *machine)
45*fcbf4a3cSThomas Huth {
46*fcbf4a3cSThomas Huth     char *args;
47*fcbf4a3cSThomas Huth 
48*fcbf4a3cSThomas Huth     args = g_strdup_printf("-M %s,accel=tcg -prom-env 'boot-command=%x %x l!'",
49*fcbf4a3cSThomas Huth                            (const char *)machine, MAGIC, ADDRESS);
50*fcbf4a3cSThomas Huth 
51*fcbf4a3cSThomas Huth     qtest_start(args);
52*fcbf4a3cSThomas Huth     check_guest_memory();
53*fcbf4a3cSThomas Huth     qtest_quit(global_qtest);
54*fcbf4a3cSThomas Huth 
55*fcbf4a3cSThomas Huth     g_free(args);
56*fcbf4a3cSThomas Huth }
57*fcbf4a3cSThomas Huth 
58*fcbf4a3cSThomas Huth static void add_tests(const char *machines[])
59*fcbf4a3cSThomas Huth {
60*fcbf4a3cSThomas Huth     int i;
61*fcbf4a3cSThomas Huth     char *name;
62*fcbf4a3cSThomas Huth 
63*fcbf4a3cSThomas Huth     for (i = 0; machines[i] != NULL; i++) {
64*fcbf4a3cSThomas Huth         name = g_strdup_printf("prom-env/%s", machines[i]);
65*fcbf4a3cSThomas Huth         qtest_add_data_func(name, machines[i], test_machine);
66*fcbf4a3cSThomas Huth         g_free(name);
67*fcbf4a3cSThomas Huth     }
68*fcbf4a3cSThomas Huth }
69*fcbf4a3cSThomas Huth 
70*fcbf4a3cSThomas Huth int main(int argc, char *argv[])
71*fcbf4a3cSThomas Huth {
72*fcbf4a3cSThomas Huth     const char *sparc_machines[] = { "SPARCbook", "Voyager", "SS-20", NULL };
73*fcbf4a3cSThomas Huth     const char *sparc64_machines[] = { "sun4u", "sun4v", NULL };
74*fcbf4a3cSThomas Huth     const char *mac_machines[] = { "mac99", "g3beige", NULL };
75*fcbf4a3cSThomas Huth     const char *arch = qtest_get_arch();
76*fcbf4a3cSThomas Huth 
77*fcbf4a3cSThomas Huth     g_test_init(&argc, &argv, NULL);
78*fcbf4a3cSThomas Huth 
79*fcbf4a3cSThomas Huth     if (!strcmp(arch, "ppc") || !strcmp(arch, "ppc64")) {
80*fcbf4a3cSThomas Huth         add_tests(mac_machines);
81*fcbf4a3cSThomas Huth     } else if (!strcmp(arch, "sparc")) {
82*fcbf4a3cSThomas Huth         add_tests(sparc_machines);
83*fcbf4a3cSThomas Huth     } else if (!strcmp(arch, "sparc64")) {
84*fcbf4a3cSThomas Huth         add_tests(sparc64_machines);
85*fcbf4a3cSThomas Huth     } else {
86*fcbf4a3cSThomas Huth         g_assert_not_reached();
87*fcbf4a3cSThomas Huth     }
88*fcbf4a3cSThomas Huth 
89*fcbf4a3cSThomas Huth     return g_test_run();
90*fcbf4a3cSThomas Huth }
91