4 * Copyright (c) 2017 Intel Corporation
5 * Author: Amarnath Valluri <amarnath.valluri@intel.com>
7 * Copyright (c) 2010 - 2013 IBM Corporation
9 * Stefan Berger <stefanb@us.ibm.com>
11 * Copyright (C) 2011 IAIK, Graz University of Technology
12 * Author: Andreas Niederl
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, see <http://www.gnu.org/licenses/>
29 #include "qemu/osdep.h"
30 #include "qemu/error-report.h"
31 #include "qemu/sockets.h"
32 #include "io/channel-socket.h"
33 #include "sysemu/tpm_backend.h"
37 #include "tpm_ioctl.h"
38 #include "migration/blocker.h"
39 #include "qapi/error.h"
40 #include "qapi/clone-visitor.h"
41 #include "chardev/char-fe.h"
44 #include <sys/types.h>
50 #define DPRINTF(fmt, ...) do { \
52 fprintf(stderr, "tpm-emulator:"fmt"\n", ## __VA_ARGS__); \
56 #define TYPE_TPM_EMULATOR "tpm-emulator"
57 #define TPM_EMULATOR(obj) \
58 OBJECT_CHECK(TPMEmulator, (obj), TYPE_TPM_EMULATOR)
60 #define TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(S, cap) (((S)->caps & (cap)) == (cap))
63 typedef struct TPMEmulator
{
66 TPMEmulatorOptions
*options
;
69 TPMVersion tpm_version
;
70 ptm_cap caps
; /* capabilities of the TPM */
71 uint8_t cur_locty_number
; /* last set locality */
72 Error
*migration_blocker
;
76 unsigned int established_flag
:1;
77 unsigned int established_flag_cached
:1;
81 static int tpm_emulator_ctrlcmd(TPMEmulator
*tpm
, unsigned long cmd
, void *msg
,
82 size_t msg_len_in
, size_t msg_len_out
)
84 CharBackend
*dev
= &tpm
->ctrl_chr
;
85 uint32_t cmd_no
= cpu_to_be32(cmd
);
86 ssize_t n
= sizeof(uint32_t) + msg_len_in
;
90 qemu_mutex_lock(&tpm
->mutex
);
93 memcpy(buf
, &cmd_no
, sizeof(cmd_no
));
94 memcpy(buf
+ sizeof(cmd_no
), msg
, msg_len_in
);
96 n
= qemu_chr_fe_write_all(dev
, buf
, n
);
101 if (msg_len_out
!= 0) {
102 n
= qemu_chr_fe_read_all(dev
, msg
, msg_len_out
);
111 qemu_mutex_unlock(&tpm
->mutex
);
115 static int tpm_emulator_unix_tx_bufs(TPMEmulator
*tpm_emu
,
116 const uint8_t *in
, uint32_t in_len
,
117 uint8_t *out
, uint32_t out_len
,
122 bool is_selftest
= false;
123 const struct tpm_resp_hdr
*hdr
= NULL
;
126 *selftest_done
= false;
127 is_selftest
= tpm_util_is_selftest(in
, in_len
);
130 ret
= qio_channel_write_all(tpm_emu
->data_ioc
, (char *)in
, in_len
, err
);
135 ret
= qio_channel_read_all(tpm_emu
->data_ioc
, (char *)out
, sizeof(*hdr
),
141 hdr
= (struct tpm_resp_hdr
*)out
;
143 ret
= qio_channel_read_all(tpm_emu
->data_ioc
, (char *)out
,
144 be32_to_cpu(hdr
->len
) - sizeof(*hdr
) , err
);
150 *selftest_done
= (be32_to_cpu(hdr
->errcode
) == 0);
156 static int tpm_emulator_set_locality(TPMEmulator
*tpm_emu
, uint8_t locty_number
,
161 DPRINTF("%s : locality: 0x%x", __func__
, locty_number
);
163 if (tpm_emu
->cur_locty_number
== locty_number
) {
167 DPRINTF("setting locality : 0x%x", locty_number
);
168 loc
.u
.req
.loc
= locty_number
;
169 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_SET_LOCALITY
, &loc
,
170 sizeof(loc
), sizeof(loc
)) < 0) {
171 error_setg(errp
, "tpm-emulator: could not set locality : %s",
176 loc
.u
.resp
.tpm_result
= be32_to_cpu(loc
.u
.resp
.tpm_result
);
177 if (loc
.u
.resp
.tpm_result
!= 0) {
178 error_setg(errp
, "tpm-emulator: TPM result for set locality : 0x%x",
179 loc
.u
.resp
.tpm_result
);
183 tpm_emu
->cur_locty_number
= locty_number
;
188 static void tpm_emulator_handle_request(TPMBackend
*tb
, TPMBackendCmd
*cmd
)
190 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
193 DPRINTF("processing TPM command");
195 if (tpm_emulator_set_locality(tpm_emu
, cmd
->locty
, &err
) < 0) {
199 if (tpm_emulator_unix_tx_bufs(tpm_emu
, cmd
->in
, cmd
->in_len
,
200 cmd
->out
, cmd
->out_len
,
201 &cmd
->selftest_done
, &err
) < 0) {
208 tpm_util_write_fatal_error_response(cmd
->out
, cmd
->out_len
);
209 error_report_err(err
);
212 static int tpm_emulator_probe_caps(TPMEmulator
*tpm_emu
)
214 DPRINTF("%s", __func__
);
215 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_GET_CAPABILITY
,
216 &tpm_emu
->caps
, 0, sizeof(tpm_emu
->caps
)) < 0) {
217 error_report("tpm-emulator: probing failed : %s", strerror(errno
));
221 tpm_emu
->caps
= be64_to_cpu(tpm_emu
->caps
);
223 DPRINTF("capabilities : 0x%"PRIx64
, tpm_emu
->caps
);
228 static int tpm_emulator_check_caps(TPMEmulator
*tpm_emu
)
231 const char *tpm
= NULL
;
233 /* check for min. required capabilities */
234 switch (tpm_emu
->tpm_version
) {
235 case TPM_VERSION_1_2
:
236 caps
= PTM_CAP_INIT
| PTM_CAP_SHUTDOWN
| PTM_CAP_GET_TPMESTABLISHED
|
237 PTM_CAP_SET_LOCALITY
| PTM_CAP_SET_DATAFD
| PTM_CAP_STOP
|
238 PTM_CAP_SET_BUFFERSIZE
;
241 case TPM_VERSION_2_0
:
242 caps
= PTM_CAP_INIT
| PTM_CAP_SHUTDOWN
| PTM_CAP_GET_TPMESTABLISHED
|
243 PTM_CAP_SET_LOCALITY
| PTM_CAP_RESET_TPMESTABLISHED
|
244 PTM_CAP_SET_DATAFD
| PTM_CAP_STOP
| PTM_CAP_SET_BUFFERSIZE
;
247 case TPM_VERSION_UNSPEC
:
248 error_report("tpm-emulator: TPM version has not been set");
252 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu
, caps
)) {
253 error_report("tpm-emulator: TPM does not implement minimum set of "
254 "required capabilities for TPM %s (0x%x)", tpm
, (int)caps
);
261 static int tpm_emulator_stop_tpm(TPMBackend
*tb
)
263 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
266 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_STOP
, &res
, 0, sizeof(res
)) < 0) {
267 error_report("tpm-emulator: Could not stop TPM: %s",
272 res
= be32_to_cpu(res
);
274 error_report("tpm-emulator: TPM result for CMD_STOP: 0x%x", res
);
281 static int tpm_emulator_set_buffer_size(TPMBackend
*tb
,
285 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
286 ptm_setbuffersize psbs
;
288 if (tpm_emulator_stop_tpm(tb
) < 0) {
292 psbs
.u
.req
.buffersize
= cpu_to_be32(wanted_size
);
294 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_SET_BUFFERSIZE
, &psbs
,
295 sizeof(psbs
.u
.req
), sizeof(psbs
.u
.resp
)) < 0) {
296 error_report("tpm-emulator: Could not set buffer size: %s",
301 psbs
.u
.resp
.tpm_result
= be32_to_cpu(psbs
.u
.resp
.tpm_result
);
302 if (psbs
.u
.resp
.tpm_result
!= 0) {
303 error_report("tpm-emulator: TPM result for set buffer size : 0x%x",
304 psbs
.u
.resp
.tpm_result
);
309 *actual_size
= be32_to_cpu(psbs
.u
.resp
.buffersize
);
312 DPRINTF("buffer size: %u, min: %u, max: %u\n",
313 be32_to_cpu(psbs
.u
.resp
.buffersize
),
314 be32_to_cpu(psbs
.u
.resp
.minsize
),
315 be32_to_cpu(psbs
.u
.resp
.maxsize
));
320 static int tpm_emulator_startup_tpm(TPMBackend
*tb
, size_t buffersize
)
322 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
326 if (buffersize
!= 0 &&
327 tpm_emulator_set_buffer_size(tb
, buffersize
, NULL
) < 0) {
331 DPRINTF("%s", __func__
);
332 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_INIT
, &init
, sizeof(init
),
334 error_report("tpm-emulator: could not send INIT: %s",
339 res
= be32_to_cpu(init
.u
.resp
.tpm_result
);
341 error_report("tpm-emulator: TPM result for CMD_INIT: 0x%x", res
);
350 static bool tpm_emulator_get_tpm_established_flag(TPMBackend
*tb
)
352 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
355 if (tpm_emu
->established_flag_cached
) {
356 return tpm_emu
->established_flag
;
359 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_GET_TPMESTABLISHED
, &est
,
360 0, sizeof(est
)) < 0) {
361 error_report("tpm-emulator: Could not get the TPM established flag: %s",
365 DPRINTF("got established flag: %0x", est
.u
.resp
.bit
);
367 tpm_emu
->established_flag_cached
= 1;
368 tpm_emu
->established_flag
= (est
.u
.resp
.bit
!= 0);
370 return tpm_emu
->established_flag
;
373 static int tpm_emulator_reset_tpm_established_flag(TPMBackend
*tb
,
376 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
377 ptm_reset_est reset_est
;
380 /* only a TPM 2.0 will support this */
381 if (tpm_emu
->tpm_version
!= TPM_VERSION_2_0
) {
385 reset_est
.u
.req
.loc
= tpm_emu
->cur_locty_number
;
386 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_RESET_TPMESTABLISHED
,
387 &reset_est
, sizeof(reset_est
),
388 sizeof(reset_est
)) < 0) {
389 error_report("tpm-emulator: Could not reset the establishment bit: %s",
394 res
= be32_to_cpu(reset_est
.u
.resp
.tpm_result
);
396 error_report("tpm-emulator: TPM result for rest establixhed flag: 0x%x",
401 tpm_emu
->established_flag_cached
= 0;
406 static void tpm_emulator_cancel_cmd(TPMBackend
*tb
)
408 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
411 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu
, PTM_CAP_CANCEL_TPM_CMD
)) {
412 DPRINTF("Backend does not support CANCEL_TPM_CMD");
416 /* FIXME: make the function non-blocking, or it may block a VCPU */
417 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_CANCEL_TPM_CMD
, &res
, 0,
419 error_report("tpm-emulator: Could not cancel command: %s",
421 } else if (res
!= 0) {
422 error_report("tpm-emulator: Failed to cancel TPM: 0x%x",
427 static TPMVersion
tpm_emulator_get_tpm_version(TPMBackend
*tb
)
429 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
431 return tpm_emu
->tpm_version
;
434 static size_t tpm_emulator_get_buffer_size(TPMBackend
*tb
)
438 if (tpm_emulator_set_buffer_size(tb
, 0, &actual_size
) < 0) {
445 static int tpm_emulator_block_migration(TPMEmulator
*tpm_emu
)
449 error_setg(&tpm_emu
->migration_blocker
,
450 "Migration disabled: TPM emulator not yet migratable");
451 migrate_add_blocker(tpm_emu
->migration_blocker
, &err
);
453 error_report_err(err
);
454 error_free(tpm_emu
->migration_blocker
);
455 tpm_emu
->migration_blocker
= NULL
;
463 static int tpm_emulator_prepare_data_fd(TPMEmulator
*tpm_emu
)
467 int fds
[2] = { -1, -1 };
469 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
) < 0) {
470 error_report("tpm-emulator: Failed to create socketpair");
474 qemu_chr_fe_set_msgfds(&tpm_emu
->ctrl_chr
, fds
+ 1, 1);
476 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_SET_DATAFD
, &res
, 0,
477 sizeof(res
)) < 0 || res
!= 0) {
478 error_report("tpm-emulator: Failed to send CMD_SET_DATAFD: %s",
483 tpm_emu
->data_ioc
= QIO_CHANNEL(qio_channel_socket_new_fd(fds
[0], &err
));
485 error_prepend(&err
, "tpm-emulator: Failed to create io channel: ");
486 error_report_err(err
);
500 static int tpm_emulator_handle_device_opts(TPMEmulator
*tpm_emu
, QemuOpts
*opts
)
504 value
= qemu_opt_get(opts
, "chardev");
507 Chardev
*dev
= qemu_chr_find(value
);
510 error_report("tpm-emulator: tpm chardev '%s' not found.", value
);
514 if (!qemu_chr_fe_init(&tpm_emu
->ctrl_chr
, dev
, &err
)) {
515 error_prepend(&err
, "tpm-emulator: No valid chardev found at '%s':",
517 error_report_err(err
);
521 tpm_emu
->options
->chardev
= g_strdup(value
);
524 if (tpm_emulator_prepare_data_fd(tpm_emu
) < 0) {
528 /* FIXME: tpm_util_test_tpmdev() accepts only on socket fd, as it also used
529 * by passthrough driver, which not yet using GIOChannel.
531 if (tpm_util_test_tpmdev(QIO_CHANNEL_SOCKET(tpm_emu
->data_ioc
)->fd
,
532 &tpm_emu
->tpm_version
)) {
533 error_report("'%s' is not emulating TPM device. Error: %s",
534 tpm_emu
->options
->chardev
, strerror(errno
));
538 DPRINTF("TPM Version %s", tpm_emu
->tpm_version
== TPM_VERSION_1_2
? "1.2" :
539 (tpm_emu
->tpm_version
== TPM_VERSION_2_0
? "2.0" : "Unspecified"));
541 if (tpm_emulator_probe_caps(tpm_emu
) ||
542 tpm_emulator_check_caps(tpm_emu
)) {
546 return tpm_emulator_block_migration(tpm_emu
);
549 DPRINTF("Startup error");
553 static TPMBackend
*tpm_emulator_create(QemuOpts
*opts
)
555 TPMBackend
*tb
= TPM_BACKEND(object_new(TYPE_TPM_EMULATOR
));
557 if (tpm_emulator_handle_device_opts(TPM_EMULATOR(tb
), opts
)) {
558 object_unref(OBJECT(tb
));
565 static TpmTypeOptions
*tpm_emulator_get_tpm_options(TPMBackend
*tb
)
567 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
568 TpmTypeOptions
*options
= g_new0(TpmTypeOptions
, 1);
570 options
->type
= TPM_TYPE_OPTIONS_KIND_EMULATOR
;
571 options
->u
.emulator
.data
= QAPI_CLONE(TPMEmulatorOptions
, tpm_emu
->options
);
576 static const QemuOptDesc tpm_emulator_cmdline_opts
[] = {
577 TPM_STANDARD_CMDLINE_OPTS
,
580 .type
= QEMU_OPT_STRING
,
581 .help
= "Character device to use for out-of-band control messages",
583 { /* end of list */ },
586 static void tpm_emulator_inst_init(Object
*obj
)
588 TPMEmulator
*tpm_emu
= TPM_EMULATOR(obj
);
590 DPRINTF("%s", __func__
);
591 tpm_emu
->options
= g_new0(TPMEmulatorOptions
, 1);
592 tpm_emu
->cur_locty_number
= ~0;
593 qemu_mutex_init(&tpm_emu
->mutex
);
597 * Gracefully shut down the external TPM
599 static void tpm_emulator_shutdown(TPMEmulator
*tpm_emu
)
603 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_SHUTDOWN
, &res
, 0, sizeof(res
)) < 0) {
604 error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s",
606 } else if (res
!= 0) {
607 error_report("tpm-emulator: TPM result for sutdown: 0x%x",
612 static void tpm_emulator_inst_finalize(Object
*obj
)
614 TPMEmulator
*tpm_emu
= TPM_EMULATOR(obj
);
616 tpm_emulator_shutdown(tpm_emu
);
618 object_unref(OBJECT(tpm_emu
->data_ioc
));
620 qemu_chr_fe_deinit(&tpm_emu
->ctrl_chr
, false);
622 qapi_free_TPMEmulatorOptions(tpm_emu
->options
);
624 if (tpm_emu
->migration_blocker
) {
625 migrate_del_blocker(tpm_emu
->migration_blocker
);
626 error_free(tpm_emu
->migration_blocker
);
629 qemu_mutex_destroy(&tpm_emu
->mutex
);
632 static void tpm_emulator_class_init(ObjectClass
*klass
, void *data
)
634 TPMBackendClass
*tbc
= TPM_BACKEND_CLASS(klass
);
636 tbc
->type
= TPM_TYPE_EMULATOR
;
637 tbc
->opts
= tpm_emulator_cmdline_opts
;
638 tbc
->desc
= "TPM emulator backend driver";
639 tbc
->create
= tpm_emulator_create
;
640 tbc
->startup_tpm
= tpm_emulator_startup_tpm
;
641 tbc
->cancel_cmd
= tpm_emulator_cancel_cmd
;
642 tbc
->get_tpm_established_flag
= tpm_emulator_get_tpm_established_flag
;
643 tbc
->reset_tpm_established_flag
= tpm_emulator_reset_tpm_established_flag
;
644 tbc
->get_tpm_version
= tpm_emulator_get_tpm_version
;
645 tbc
->get_buffer_size
= tpm_emulator_get_buffer_size
;
646 tbc
->get_tpm_options
= tpm_emulator_get_tpm_options
;
648 tbc
->handle_request
= tpm_emulator_handle_request
;
651 static const TypeInfo tpm_emulator_info
= {
652 .name
= TYPE_TPM_EMULATOR
,
653 .parent
= TYPE_TPM_BACKEND
,
654 .instance_size
= sizeof(TPMEmulator
),
655 .class_init
= tpm_emulator_class_init
,
656 .instance_init
= tpm_emulator_inst_init
,
657 .instance_finalize
= tpm_emulator_inst_finalize
,
660 static void tpm_emulator_register(void)
662 type_register_static(&tpm_emulator_info
);
665 type_init(tpm_emulator_register
)