linux-user: move generic signal definitions to generic/signal.h
[qemu/ar7.git] / tests / tpm-util.c
blobc9b3947c1c6ebd306560a250564e5cf4e269086e
1 /*
2 * QTest TPM utilities
4 * Copyright (c) 2018 IBM Corporation
5 * Copyright (c) 2018 Red Hat, Inc.
7 * Authors:
8 * Stefan Berger <stefanb@linux.vnet.ibm.com>
9 * Marc-André Lureau <marcandre.lureau@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
17 #include "hw/acpi/tpm.h"
18 #include "libqtest.h"
19 #include "tpm-util.h"
21 void tpm_util_crb_transfer(QTestState *s,
22 const unsigned char *req, size_t req_size,
23 unsigned char *rsp, size_t rsp_size)
25 uint64_t caddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_LADDR);
26 uint64_t raddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_ADDR);
28 qtest_writeb(s, TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL, 1);
30 qtest_memwrite(s, caddr, req, req_size);
32 uint32_t sts, start = 1;
33 uint64_t end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
34 qtest_writel(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START, start);
35 while (true) {
36 start = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START);
37 if ((start & 1) == 0) {
38 break;
40 if (g_get_monotonic_time() >= end_time) {
41 break;
44 start = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START);
45 g_assert_cmpint(start & 1, ==, 0);
46 sts = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS);
47 g_assert_cmpint(sts & 1, ==, 0);
49 qtest_memread(s, raddr, rsp, rsp_size);
52 void tpm_util_startup(QTestState *s, tx_func *tx)
54 unsigned char buffer[1024];
55 unsigned char tpm_startup[] =
56 "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00";
57 unsigned char tpm_startup_resp[] =
58 "\x80\x01\x00\x00\x00\x0a\x00\x00\x00\x00";
60 tx(s, tpm_startup, sizeof(tpm_startup), buffer, sizeof(buffer));
62 g_assert_cmpmem(buffer, sizeof(tpm_startup_resp),
63 tpm_startup_resp, sizeof(tpm_startup_resp));
66 void tpm_util_pcrextend(QTestState *s, tx_func *tx)
68 unsigned char buffer[1024];
69 unsigned char tpm_pcrextend[] =
70 "\x80\x02\x00\x00\x00\x41\x00\x00\x01\x82\x00\x00\x00\x0a\x00\x00"
71 "\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00"
72 "\x0b\x74\x65\x73\x74\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
73 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
74 "\x00";
76 unsigned char tpm_pcrextend_resp[] =
77 "\x80\x02\x00\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
78 "\x01\x00\x00";
80 tx(s, tpm_pcrextend, sizeof(tpm_pcrextend), buffer, sizeof(buffer));
82 g_assert_cmpmem(buffer, sizeof(tpm_pcrextend_resp),
83 tpm_pcrextend_resp, sizeof(tpm_pcrextend_resp));
86 void tpm_util_pcrread(QTestState *s, tx_func *tx,
87 const unsigned char *exp_resp, size_t exp_resp_size)
89 unsigned char buffer[1024];
90 unsigned char tpm_pcrread[] =
91 "\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b"
92 "\x03\x00\x04\x00";
94 tx(s, tpm_pcrread, sizeof(tpm_pcrread), buffer, sizeof(buffer));
96 g_assert_cmpmem(buffer, exp_resp_size, exp_resp, exp_resp_size);
99 static gboolean tpm_util_swtpm_has_tpm2(void)
101 gint mystdout;
102 gboolean succ;
103 unsigned i;
104 char buffer[10240];
105 ssize_t n;
106 gchar *swtpm_argv[] = {
107 g_strdup("swtpm"), g_strdup("socket"), g_strdup("--help"), NULL
110 succ = g_spawn_async_with_pipes(NULL, swtpm_argv, NULL,
111 G_SPAWN_SEARCH_PATH, NULL, NULL, NULL,
112 NULL, &mystdout, NULL, NULL);
113 if (!succ) {
114 goto cleanup;
117 n = read(mystdout, buffer, sizeof(buffer) - 1);
118 if (n < 0) {
119 goto cleanup;
121 buffer[n] = 0;
122 if (!strstr(buffer, "--tpm2")) {
123 succ = false;
126 cleanup:
127 for (i = 0; swtpm_argv[i]; i++) {
128 g_free(swtpm_argv[i]);
131 return succ;
134 gboolean tpm_util_swtpm_start(const char *path, GPid *pid,
135 SocketAddress **addr, GError **error)
137 char *swtpm_argv_tpmstate = g_strdup_printf("dir=%s", path);
138 char *swtpm_argv_ctrl = g_strdup_printf("type=unixio,path=%s/sock",
139 path);
140 gchar *swtpm_argv[] = {
141 g_strdup("swtpm"), g_strdup("socket"),
142 g_strdup("--tpmstate"), swtpm_argv_tpmstate,
143 g_strdup("--ctrl"), swtpm_argv_ctrl,
144 g_strdup("--tpm2"),
145 NULL
147 gboolean succ;
148 unsigned i;
150 succ = tpm_util_swtpm_has_tpm2();
151 if (!succ) {
152 goto cleanup;
155 *addr = g_new0(SocketAddress, 1);
156 (*addr)->type = SOCKET_ADDRESS_TYPE_UNIX;
157 (*addr)->u.q_unix.path = g_build_filename(path, "sock", NULL);
159 succ = g_spawn_async(NULL, swtpm_argv, NULL, G_SPAWN_SEARCH_PATH,
160 NULL, NULL, pid, error);
162 cleanup:
163 for (i = 0; swtpm_argv[i]; i++) {
164 g_free(swtpm_argv[i]);
167 return succ;
170 void tpm_util_swtpm_kill(GPid pid)
172 int n;
174 if (!pid) {
175 return;
178 g_spawn_close_pid(pid);
180 n = kill(pid, 0);
181 if (n < 0) {
182 return;
185 kill(pid, SIGKILL);