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