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"
36 #include "hw/i386/pc.h"
38 #include "tpm_ioctl.h"
39 #include "migration/blocker.h"
40 #include "qapi/error.h"
41 #include "qapi/clone-visitor.h"
42 #include "chardev/char-fe.h"
45 #include <sys/types.h>
51 #define DPRINTF(fmt, ...) do { \
53 fprintf(stderr, "tpm-emulator:"fmt"\n", ## __VA_ARGS__); \
57 #define TYPE_TPM_EMULATOR "tpm-emulator"
58 #define TPM_EMULATOR(obj) \
59 OBJECT_CHECK(TPMEmulator, (obj), TYPE_TPM_EMULATOR)
61 #define TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(S, cap) (((S)->caps & (cap)) == (cap))
64 typedef struct TPMEmulator
{
67 TPMEmulatorOptions
*options
;
70 TPMVersion tpm_version
;
71 ptm_cap caps
; /* capabilities of the TPM */
72 uint8_t cur_locty_number
; /* last set locality */
73 Error
*migration_blocker
;
79 static int tpm_emulator_ctrlcmd(TPMEmulator
*tpm
, unsigned long cmd
, void *msg
,
80 size_t msg_len_in
, size_t msg_len_out
)
82 CharBackend
*dev
= &tpm
->ctrl_chr
;
83 uint32_t cmd_no
= cpu_to_be32(cmd
);
84 ssize_t n
= sizeof(uint32_t) + msg_len_in
;
88 qemu_mutex_lock(&tpm
->mutex
);
91 memcpy(buf
, &cmd_no
, sizeof(cmd_no
));
92 memcpy(buf
+ sizeof(cmd_no
), msg
, msg_len_in
);
94 n
= qemu_chr_fe_write_all(dev
, buf
, n
);
99 if (msg_len_out
!= 0) {
100 n
= qemu_chr_fe_read_all(dev
, msg
, msg_len_out
);
109 qemu_mutex_unlock(&tpm
->mutex
);
113 static int tpm_emulator_unix_tx_bufs(TPMEmulator
*tpm_emu
,
114 const uint8_t *in
, uint32_t in_len
,
115 uint8_t *out
, uint32_t out_len
,
120 bool is_selftest
= false;
121 const struct tpm_resp_hdr
*hdr
= NULL
;
124 *selftest_done
= false;
125 is_selftest
= tpm_util_is_selftest(in
, in_len
);
128 ret
= qio_channel_write_all(tpm_emu
->data_ioc
, (char *)in
, in_len
, err
);
133 ret
= qio_channel_read_all(tpm_emu
->data_ioc
, (char *)out
, sizeof(*hdr
),
139 hdr
= (struct tpm_resp_hdr
*)out
;
141 ret
= qio_channel_read_all(tpm_emu
->data_ioc
, (char *)out
,
142 be32_to_cpu(hdr
->len
) - sizeof(*hdr
) , err
);
148 *selftest_done
= (be32_to_cpu(hdr
->errcode
) == 0);
154 static int tpm_emulator_set_locality(TPMEmulator
*tpm_emu
, uint8_t locty_number
,
159 DPRINTF("%s : locality: 0x%x", __func__
, locty_number
);
161 if (tpm_emu
->cur_locty_number
== locty_number
) {
165 DPRINTF("setting locality : 0x%x", locty_number
);
166 loc
.u
.req
.loc
= locty_number
;
167 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_SET_LOCALITY
, &loc
,
168 sizeof(loc
), sizeof(loc
)) < 0) {
169 error_setg(errp
, "tpm-emulator: could not set locality : %s",
174 loc
.u
.resp
.tpm_result
= be32_to_cpu(loc
.u
.resp
.tpm_result
);
175 if (loc
.u
.resp
.tpm_result
!= 0) {
176 error_setg(errp
, "tpm-emulator: TPM result for set locality : 0x%x",
177 loc
.u
.resp
.tpm_result
);
181 tpm_emu
->cur_locty_number
= locty_number
;
186 static void tpm_emulator_handle_request(TPMBackend
*tb
, TPMBackendCmd
*cmd
)
188 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
191 DPRINTF("processing TPM command");
193 if (tpm_emulator_set_locality(tpm_emu
, cmd
->locty
, &err
) < 0) {
197 if (tpm_emulator_unix_tx_bufs(tpm_emu
, cmd
->in
, cmd
->in_len
,
198 cmd
->out
, cmd
->out_len
,
199 &cmd
->selftest_done
, &err
) < 0) {
206 tpm_util_write_fatal_error_response(cmd
->out
, cmd
->out_len
);
207 error_report_err(err
);
210 static int tpm_emulator_probe_caps(TPMEmulator
*tpm_emu
)
212 DPRINTF("%s", __func__
);
213 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_GET_CAPABILITY
,
214 &tpm_emu
->caps
, 0, sizeof(tpm_emu
->caps
)) < 0) {
215 error_report("tpm-emulator: probing failed : %s", strerror(errno
));
219 tpm_emu
->caps
= be64_to_cpu(tpm_emu
->caps
);
221 DPRINTF("capabilities : 0x%"PRIx64
, tpm_emu
->caps
);
226 static int tpm_emulator_check_caps(TPMEmulator
*tpm_emu
)
229 const char *tpm
= NULL
;
231 /* check for min. required capabilities */
232 switch (tpm_emu
->tpm_version
) {
233 case TPM_VERSION_1_2
:
234 caps
= PTM_CAP_INIT
| PTM_CAP_SHUTDOWN
| PTM_CAP_GET_TPMESTABLISHED
|
235 PTM_CAP_SET_LOCALITY
| PTM_CAP_SET_DATAFD
| PTM_CAP_STOP
|
236 PTM_CAP_SET_BUFFERSIZE
;
239 case TPM_VERSION_2_0
:
240 caps
= PTM_CAP_INIT
| PTM_CAP_SHUTDOWN
| PTM_CAP_GET_TPMESTABLISHED
|
241 PTM_CAP_SET_LOCALITY
| PTM_CAP_RESET_TPMESTABLISHED
|
242 PTM_CAP_SET_DATAFD
| PTM_CAP_STOP
| PTM_CAP_SET_BUFFERSIZE
;
245 case TPM_VERSION_UNSPEC
:
246 error_report("tpm-emulator: TPM version has not been set");
250 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu
, caps
)) {
251 error_report("tpm-emulator: TPM does not implement minimum set of "
252 "required capabilities for TPM %s (0x%x)", tpm
, (int)caps
);
259 static int tpm_emulator_stop_tpm(TPMBackend
*tb
)
261 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
264 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_STOP
, &res
, 0, sizeof(res
)) < 0) {
265 error_report("tpm-emulator: Could not stop TPM: %s",
270 res
= be32_to_cpu(res
);
272 error_report("tpm-emulator: TPM result for CMD_STOP: 0x%x", res
);
279 static int tpm_emulator_set_buffer_size(TPMBackend
*tb
,
283 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
284 ptm_setbuffersize psbs
;
286 if (tpm_emulator_stop_tpm(tb
) < 0) {
290 psbs
.u
.req
.buffersize
= cpu_to_be32(wanted_size
);
292 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_SET_BUFFERSIZE
, &psbs
,
293 sizeof(psbs
.u
.req
), sizeof(psbs
.u
.resp
)) < 0) {
294 error_report("tpm-emulator: Could not set buffer size: %s",
299 psbs
.u
.resp
.tpm_result
= be32_to_cpu(psbs
.u
.resp
.tpm_result
);
300 if (psbs
.u
.resp
.tpm_result
!= 0) {
301 error_report("tpm-emulator: TPM result for set buffer size : 0x%x",
302 psbs
.u
.resp
.tpm_result
);
307 *actual_size
= be32_to_cpu(psbs
.u
.resp
.buffersize
);
310 DPRINTF("buffer size: %u, min: %u, max: %u\n",
311 be32_to_cpu(psbs
.u
.resp
.buffersize
),
312 be32_to_cpu(psbs
.u
.resp
.minsize
),
313 be32_to_cpu(psbs
.u
.resp
.maxsize
));
318 static int tpm_emulator_startup_tpm(TPMBackend
*tb
, size_t buffersize
)
320 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
324 if (buffersize
!= 0 &&
325 tpm_emulator_set_buffer_size(tb
, buffersize
, NULL
) < 0) {
329 DPRINTF("%s", __func__
);
330 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_INIT
, &init
, sizeof(init
),
332 error_report("tpm-emulator: could not send INIT: %s",
337 res
= be32_to_cpu(init
.u
.resp
.tpm_result
);
339 error_report("tpm-emulator: TPM result for CMD_INIT: 0x%x", res
);
348 static bool tpm_emulator_get_tpm_established_flag(TPMBackend
*tb
)
350 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
353 DPRINTF("%s", __func__
);
354 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_GET_TPMESTABLISHED
, &est
,
355 0, sizeof(est
)) < 0) {
356 error_report("tpm-emulator: Could not get the TPM established flag: %s",
360 DPRINTF("established flag: %0x", est
.u
.resp
.bit
);
362 return (est
.u
.resp
.bit
!= 0);
365 static int tpm_emulator_reset_tpm_established_flag(TPMBackend
*tb
,
368 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
369 ptm_reset_est reset_est
;
372 /* only a TPM 2.0 will support this */
373 if (tpm_emu
->tpm_version
!= TPM_VERSION_2_0
) {
377 reset_est
.u
.req
.loc
= tpm_emu
->cur_locty_number
;
378 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_RESET_TPMESTABLISHED
,
379 &reset_est
, sizeof(reset_est
),
380 sizeof(reset_est
)) < 0) {
381 error_report("tpm-emulator: Could not reset the establishment bit: %s",
386 res
= be32_to_cpu(reset_est
.u
.resp
.tpm_result
);
388 error_report("tpm-emulator: TPM result for rest establixhed flag: 0x%x",
396 static void tpm_emulator_cancel_cmd(TPMBackend
*tb
)
398 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
401 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu
, PTM_CAP_CANCEL_TPM_CMD
)) {
402 DPRINTF("Backend does not support CANCEL_TPM_CMD");
406 /* FIXME: make the function non-blocking, or it may block a VCPU */
407 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_CANCEL_TPM_CMD
, &res
, 0,
409 error_report("tpm-emulator: Could not cancel command: %s",
411 } else if (res
!= 0) {
412 error_report("tpm-emulator: Failed to cancel TPM: 0x%x",
417 static TPMVersion
tpm_emulator_get_tpm_version(TPMBackend
*tb
)
419 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
421 return tpm_emu
->tpm_version
;
424 static size_t tpm_emulator_get_buffer_size(TPMBackend
*tb
)
428 if (tpm_emulator_set_buffer_size(tb
, 0, &actual_size
) < 0) {
435 static int tpm_emulator_block_migration(TPMEmulator
*tpm_emu
)
439 error_setg(&tpm_emu
->migration_blocker
,
440 "Migration disabled: TPM emulator not yet migratable");
441 migrate_add_blocker(tpm_emu
->migration_blocker
, &err
);
443 error_report_err(err
);
444 error_free(tpm_emu
->migration_blocker
);
445 tpm_emu
->migration_blocker
= NULL
;
453 static int tpm_emulator_prepare_data_fd(TPMEmulator
*tpm_emu
)
457 int fds
[2] = { -1, -1 };
459 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
) < 0) {
460 error_report("tpm-emulator: Failed to create socketpair");
464 qemu_chr_fe_set_msgfds(&tpm_emu
->ctrl_chr
, fds
+ 1, 1);
466 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_SET_DATAFD
, &res
, 0,
467 sizeof(res
)) < 0 || res
!= 0) {
468 error_report("tpm-emulator: Failed to send CMD_SET_DATAFD: %s",
473 tpm_emu
->data_ioc
= QIO_CHANNEL(qio_channel_socket_new_fd(fds
[0], &err
));
475 error_prepend(&err
, "tpm-emulator: Failed to create io channel: ");
476 error_report_err(err
);
490 static int tpm_emulator_handle_device_opts(TPMEmulator
*tpm_emu
, QemuOpts
*opts
)
494 value
= qemu_opt_get(opts
, "chardev");
497 Chardev
*dev
= qemu_chr_find(value
);
500 error_report("tpm-emulator: tpm chardev '%s' not found.", value
);
504 if (!qemu_chr_fe_init(&tpm_emu
->ctrl_chr
, dev
, &err
)) {
505 error_prepend(&err
, "tpm-emulator: No valid chardev found at '%s':",
507 error_report_err(err
);
511 tpm_emu
->options
->chardev
= g_strdup(value
);
514 if (tpm_emulator_prepare_data_fd(tpm_emu
) < 0) {
518 /* FIXME: tpm_util_test_tpmdev() accepts only on socket fd, as it also used
519 * by passthrough driver, which not yet using GIOChannel.
521 if (tpm_util_test_tpmdev(QIO_CHANNEL_SOCKET(tpm_emu
->data_ioc
)->fd
,
522 &tpm_emu
->tpm_version
)) {
523 error_report("'%s' is not emulating TPM device. Error: %s",
524 tpm_emu
->options
->chardev
, strerror(errno
));
528 DPRINTF("TPM Version %s", tpm_emu
->tpm_version
== TPM_VERSION_1_2
? "1.2" :
529 (tpm_emu
->tpm_version
== TPM_VERSION_2_0
? "2.0" : "Unspecified"));
531 if (tpm_emulator_probe_caps(tpm_emu
) ||
532 tpm_emulator_check_caps(tpm_emu
)) {
536 return tpm_emulator_block_migration(tpm_emu
);
539 DPRINTF("Startup error");
543 static TPMBackend
*tpm_emulator_create(QemuOpts
*opts
)
545 TPMBackend
*tb
= TPM_BACKEND(object_new(TYPE_TPM_EMULATOR
));
547 if (tpm_emulator_handle_device_opts(TPM_EMULATOR(tb
), opts
)) {
548 object_unref(OBJECT(tb
));
555 static TpmTypeOptions
*tpm_emulator_get_tpm_options(TPMBackend
*tb
)
557 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
558 TpmTypeOptions
*options
= g_new0(TpmTypeOptions
, 1);
560 options
->type
= TPM_TYPE_OPTIONS_KIND_EMULATOR
;
561 options
->u
.emulator
.data
= QAPI_CLONE(TPMEmulatorOptions
, tpm_emu
->options
);
566 static const QemuOptDesc tpm_emulator_cmdline_opts
[] = {
567 TPM_STANDARD_CMDLINE_OPTS
,
570 .type
= QEMU_OPT_STRING
,
571 .help
= "Character device to use for out-of-band control messages",
573 { /* end of list */ },
576 static void tpm_emulator_inst_init(Object
*obj
)
578 TPMEmulator
*tpm_emu
= TPM_EMULATOR(obj
);
580 DPRINTF("%s", __func__
);
581 tpm_emu
->options
= g_new0(TPMEmulatorOptions
, 1);
582 tpm_emu
->cur_locty_number
= ~0;
583 qemu_mutex_init(&tpm_emu
->mutex
);
587 * Gracefully shut down the external TPM
589 static void tpm_emulator_shutdown(TPMEmulator
*tpm_emu
)
593 if (tpm_emulator_ctrlcmd(tpm_emu
, CMD_SHUTDOWN
, &res
, 0, sizeof(res
)) < 0) {
594 error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s",
596 } else if (res
!= 0) {
597 error_report("tpm-emulator: TPM result for sutdown: 0x%x",
602 static void tpm_emulator_inst_finalize(Object
*obj
)
604 TPMEmulator
*tpm_emu
= TPM_EMULATOR(obj
);
606 tpm_emulator_shutdown(tpm_emu
);
608 object_unref(OBJECT(tpm_emu
->data_ioc
));
610 qemu_chr_fe_deinit(&tpm_emu
->ctrl_chr
, false);
612 qapi_free_TPMEmulatorOptions(tpm_emu
->options
);
614 if (tpm_emu
->migration_blocker
) {
615 migrate_del_blocker(tpm_emu
->migration_blocker
);
616 error_free(tpm_emu
->migration_blocker
);
619 qemu_mutex_destroy(&tpm_emu
->mutex
);
622 static void tpm_emulator_class_init(ObjectClass
*klass
, void *data
)
624 TPMBackendClass
*tbc
= TPM_BACKEND_CLASS(klass
);
626 tbc
->type
= TPM_TYPE_EMULATOR
;
627 tbc
->opts
= tpm_emulator_cmdline_opts
;
628 tbc
->desc
= "TPM emulator backend driver";
629 tbc
->create
= tpm_emulator_create
;
630 tbc
->startup_tpm
= tpm_emulator_startup_tpm
;
631 tbc
->cancel_cmd
= tpm_emulator_cancel_cmd
;
632 tbc
->get_tpm_established_flag
= tpm_emulator_get_tpm_established_flag
;
633 tbc
->reset_tpm_established_flag
= tpm_emulator_reset_tpm_established_flag
;
634 tbc
->get_tpm_version
= tpm_emulator_get_tpm_version
;
635 tbc
->get_buffer_size
= tpm_emulator_get_buffer_size
;
636 tbc
->get_tpm_options
= tpm_emulator_get_tpm_options
;
638 tbc
->handle_request
= tpm_emulator_handle_request
;
641 static const TypeInfo tpm_emulator_info
= {
642 .name
= TYPE_TPM_EMULATOR
,
643 .parent
= TYPE_TPM_BACKEND
,
644 .instance_size
= sizeof(TPMEmulator
),
645 .class_init
= tpm_emulator_class_init
,
646 .instance_init
= tpm_emulator_inst_init
,
647 .instance_finalize
= tpm_emulator_inst_finalize
,
650 static void tpm_emulator_register(void)
652 type_register_static(&tpm_emulator_info
);
655 type_init(tpm_emulator_register
)