]>
Commit | Line | Data |
---|---|---|
4ab6cb4c MAL |
1 | /* |
2 | * QTest testcase for TPM CRB | |
3 | * | |
4 | * Copyright (c) 2018 Red Hat, Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Marc-André Lureau <marcandre.lureau@redhat.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
10 | * See the COPYING file in the top-level directory. | |
11 | */ | |
12 | ||
13 | #include "qemu/osdep.h" | |
14 | #include <glib/gstdio.h> | |
15 | ||
16 | #include "hw/acpi/tpm.h" | |
4ab6cb4c MAL |
17 | #include "io/channel-socket.h" |
18 | #include "libqtest.h" | |
0e6ca954 | 19 | #include "tpm-emu.h" |
4ab6cb4c MAL |
20 | |
21 | #define TPM_CMD "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00" | |
22 | ||
23 | static void tpm_crb_test(const void *data) | |
24 | { | |
25 | const TestState *s = data; | |
26 | uint32_t intfid = readl(TPM_CRB_ADDR_BASE + A_CRB_INTF_ID); | |
27 | uint32_t csize = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_SIZE); | |
28 | uint64_t caddr = readq(TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_LADDR); | |
29 | uint32_t rsize = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_SIZE); | |
30 | uint64_t raddr = readq(TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_ADDR); | |
31 | ||
32 | g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, InterfaceType), ==, 1); | |
33 | g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, InterfaceVersion), ==, 1); | |
34 | g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, CapLocality), ==, 0); | |
35 | g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, CapCRBIdleBypass), ==, 0); | |
36 | g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, CapDataXferSizeSupport), | |
37 | ==, 3); | |
38 | g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, CapFIFO), ==, 0); | |
39 | g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, CapCRB), ==, 1); | |
40 | g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, InterfaceSelector), ==, 1); | |
41 | g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, RID), ==, 0); | |
42 | ||
43 | g_assert_cmpint(csize, >=, 128); | |
44 | g_assert_cmpint(rsize, >=, 128); | |
45 | g_assert_cmpint(caddr, >, TPM_CRB_ADDR_BASE); | |
46 | g_assert_cmpint(raddr, >, TPM_CRB_ADDR_BASE); | |
47 | ||
48 | memwrite(caddr, TPM_CMD, sizeof(TPM_CMD)); | |
49 | ||
50 | uint32_t sts, start = 1; | |
51 | uint64_t end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; | |
52 | writel(TPM_CRB_ADDR_BASE + A_CRB_CTRL_START, start); | |
53 | do { | |
54 | start = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_START); | |
55 | if ((start & 1) == 0) { | |
56 | break; | |
57 | } | |
58 | } while (g_get_monotonic_time() < end_time); | |
59 | start = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_START); | |
60 | g_assert_cmpint(start & 1, ==, 0); | |
61 | sts = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS); | |
62 | g_assert_cmpint(sts & 1, ==, 0); | |
63 | ||
64 | struct tpm_hdr tpm_msg; | |
65 | memread(raddr, &tpm_msg, sizeof(tpm_msg)); | |
66 | g_assert_cmpmem(&tpm_msg, sizeof(tpm_msg), s->tpm_msg, sizeof(*s->tpm_msg)); | |
67 | } | |
68 | ||
69 | int main(int argc, char **argv) | |
70 | { | |
71 | int ret; | |
72 | char *args, *tmp_path = g_dir_make_tmp("qemu-tpm-crb-test.XXXXXX", NULL); | |
73 | GThread *thread; | |
74 | TestState test; | |
75 | ||
76 | module_call_init(MODULE_INIT_QOM); | |
77 | g_test_init(&argc, &argv, NULL); | |
78 | ||
79 | test.addr = g_new0(SocketAddress, 1); | |
80 | test.addr->type = SOCKET_ADDRESS_TYPE_UNIX; | |
81 | test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL); | |
82 | g_mutex_init(&test.data_mutex); | |
83 | g_cond_init(&test.data_cond); | |
84 | ||
0e6ca954 SB |
85 | thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test); |
86 | tpm_emu_test_wait_cond(&test); | |
4ab6cb4c MAL |
87 | |
88 | args = g_strdup_printf( | |
89 | "-chardev socket,id=chr,path=%s " | |
90 | "-tpmdev emulator,id=dev,chardev=chr " | |
91 | "-device tpm-crb,tpmdev=dev", | |
92 | test.addr->u.q_unix.path); | |
93 | qtest_start(args); | |
94 | ||
95 | qtest_add_data_func("/tpm-crb/test", &test, tpm_crb_test); | |
96 | ret = g_test_run(); | |
97 | ||
98 | qtest_end(); | |
99 | ||
100 | g_thread_join(thread); | |
101 | g_unlink(test.addr->u.q_unix.path); | |
102 | qapi_free_SocketAddress(test.addr); | |
103 | g_rmdir(tmp_path); | |
104 | g_free(tmp_path); | |
105 | g_free(args); | |
106 | return ret; | |
107 | } |