4 * Copyright (c) 2018 IBM Corporation
5 * Copyright (c) 2018 Red Hat, Inc.
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"
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
);
36 start
= qtest_readl(s
, TPM_CRB_ADDR_BASE
+ A_CRB_CTRL_START
);
37 if ((start
& 1) == 0) {
40 if (g_get_monotonic_time() >= end_time
) {
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"
76 unsigned char tpm_pcrextend_resp
[] =
77 "\x80\x02\x00\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00\x00\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"
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)
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
);
117 n
= read(mystdout
, buffer
, sizeof(buffer
) - 1);
122 if (!strstr(buffer
, "--tpm2")) {
127 for (i
= 0; swtpm_argv
[i
]; i
++) {
128 g_free(swtpm_argv
[i
]);
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",
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
,
150 succ
= tpm_util_swtpm_has_tpm2();
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
);
163 for (i
= 0; swtpm_argv
[i
]; i
++) {
164 g_free(swtpm_argv
[i
]);
170 void tpm_util_swtpm_kill(GPid pid
)
178 g_spawn_close_pid(pid
);