2 * Minimal TPM emulator for TPM test cases
4 * Copyright (c) 2018 Red Hat, Inc.
7 * Marc-André Lureau <marcandre.lureau@redhat.com>
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.
13 #include "qemu/osdep.h"
14 #include <glib/gstdio.h>
16 #include "hw/tpm/tpm_ioctl.h"
17 #include "io/channel-socket.h"
18 #include "qapi/error.h"
21 void tpm_emu_test_wait_cond(TestState
*s
)
23 gint64 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
25 g_mutex_lock(&s
->data_mutex
);
27 if (!s
->data_cond_signal
&&
28 !g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
29 g_assert_not_reached();
32 s
->data_cond_signal
= false;
34 g_mutex_unlock(&s
->data_mutex
);
37 static void *tpm_emu_tpm_thread(void *data
)
40 QIOChannel
*ioc
= s
->tpm_ioc
;
42 s
->tpm_msg
= g_new(struct tpm_hdr
, 1);
44 int minhlen
= sizeof(s
->tpm_msg
->tag
) + sizeof(s
->tpm_msg
->len
);
46 if (!qio_channel_read(ioc
, (char *)s
->tpm_msg
, minhlen
, &error_abort
)) {
49 s
->tpm_msg
->tag
= be16_to_cpu(s
->tpm_msg
->tag
);
50 s
->tpm_msg
->len
= be32_to_cpu(s
->tpm_msg
->len
);
51 g_assert_cmpint(s
->tpm_msg
->len
, >=, minhlen
);
52 g_assert_cmpint(s
->tpm_msg
->tag
, ==, TPM2_ST_NO_SESSIONS
);
54 s
->tpm_msg
= g_realloc(s
->tpm_msg
, s
->tpm_msg
->len
);
55 qio_channel_read(ioc
, (char *)&s
->tpm_msg
->code
,
56 s
->tpm_msg
->len
- minhlen
, &error_abort
);
57 s
->tpm_msg
->code
= be32_to_cpu(s
->tpm_msg
->code
);
60 s
->tpm_msg
->tag
= cpu_to_be16(TPM2_ST_NO_SESSIONS
);
61 s
->tpm_msg
->len
= cpu_to_be32(sizeof(struct tpm_hdr
));
62 s
->tpm_msg
->code
= cpu_to_be32(TPM_RC_FAILURE
);
63 qio_channel_write(ioc
, (char *)s
->tpm_msg
, be32_to_cpu(s
->tpm_msg
->len
),
69 object_unref(OBJECT(s
->tpm_ioc
));
73 void *tpm_emu_ctrl_thread(void *data
)
76 QIOChannelSocket
*lioc
= qio_channel_socket_new();
79 qio_channel_socket_listen_sync(lioc
, s
->addr
, 1, &error_abort
);
81 g_mutex_lock(&s
->data_mutex
);
82 s
->data_cond_signal
= true;
83 g_mutex_unlock(&s
->data_mutex
);
84 g_cond_signal(&s
->data_cond
);
86 qio_channel_wait(QIO_CHANNEL(lioc
), G_IO_IN
);
87 ioc
= QIO_CHANNEL(qio_channel_socket_accept(lioc
, &error_abort
));
92 struct iovec iov
= { .iov_base
= &cmd
, .iov_len
= sizeof(cmd
) };
96 qio_channel_readv_full(ioc
, &iov
, 1, &pfd
, &nfd
, &error_abort
);
97 cmd
= be32_to_cpu(cmd
);
98 g_assert_cmpint(cmd
, ==, CMD_SET_DATAFD
);
99 g_assert_cmpint(nfd
, ==, 1);
100 s
->tpm_ioc
= QIO_CHANNEL(qio_channel_socket_new_fd(*pfd
, &error_abort
));
104 qio_channel_write(ioc
, (char *)&cmd
, sizeof(cmd
), &error_abort
);
106 s
->emu_tpm_thread
= g_thread_new(NULL
, tpm_emu_tpm_thread
, s
);
113 ret
= qio_channel_read(ioc
, (char *)&cmd
, sizeof(cmd
), NULL
);
118 cmd
= be32_to_cpu(cmd
);
120 case CMD_GET_CAPABILITY
: {
121 ptm_cap cap
= cpu_to_be64(0x3fff);
122 qio_channel_write(ioc
, (char *)&cap
, sizeof(cap
), &error_abort
);
127 qio_channel_read(ioc
, (char *)&init
.u
.req
, sizeof(init
.u
.req
),
129 init
.u
.resp
.tpm_result
= 0;
130 qio_channel_write(ioc
, (char *)&init
.u
.resp
, sizeof(init
.u
.resp
),
136 qio_channel_write(ioc
, (char *)&res
, sizeof(res
), &error_abort
);
137 /* the tpm data thread is expected to finish now */
138 g_thread_join(s
->emu_tpm_thread
);
143 qio_channel_write(ioc
, (char *)&res
, sizeof(res
), &error_abort
);
146 case CMD_SET_BUFFERSIZE
: {
147 ptm_setbuffersize sbs
;
148 qio_channel_read(ioc
, (char *)&sbs
.u
.req
, sizeof(sbs
.u
.req
),
150 sbs
.u
.resp
.buffersize
= sbs
.u
.req
.buffersize
?: cpu_to_be32(4096);
151 sbs
.u
.resp
.tpm_result
= 0;
152 sbs
.u
.resp
.minsize
= cpu_to_be32(128);
153 sbs
.u
.resp
.maxsize
= cpu_to_be32(4096);
154 qio_channel_write(ioc
, (char *)&sbs
.u
.resp
, sizeof(sbs
.u
.resp
),
158 case CMD_SET_LOCALITY
: {
160 /* Note: this time it's not u.req / u.resp... */
161 qio_channel_read(ioc
, (char *)&loc
, sizeof(loc
), &error_abort
);
162 g_assert_cmpint(loc
.u
.req
.loc
, ==, 0);
163 loc
.u
.resp
.tpm_result
= 0;
164 qio_channel_write(ioc
, (char *)&loc
, sizeof(loc
), &error_abort
);
167 case CMD_GET_TPMESTABLISHED
: {
171 qio_channel_write(ioc
, (char *)&est
, sizeof(est
), &error_abort
);
175 g_debug("unimplemented %u", cmd
);
176 g_assert_not_reached();
180 object_unref(OBJECT(ioc
));
181 object_unref(OBJECT(lioc
));