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))
63 static const TPMDriverOps tpm_emulator_driver
;
66 typedef struct TPMEmulator
{
69 TPMEmulatorOptions
*options
;
72 TPMVersion tpm_version
;
73 ptm_cap caps
; /* capabilities of the TPM */
74 uint8_t cur_locty_number
; /* last set locality */
75 Error
*migration_blocker
;
79 static int tpm_emulator_ctrlcmd(CharBackend
*dev
, unsigned long cmd
, void *msg
,
80 size_t msg_len_in
, size_t msg_len_out
)
82 uint32_t cmd_no
= cpu_to_be32(cmd
);
83 ssize_t n
= sizeof(uint32_t) + msg_len_in
;
87 memcpy(buf
, &cmd_no
, sizeof(cmd_no
));
88 memcpy(buf
+ sizeof(cmd_no
), msg
, msg_len_in
);
90 n
= qemu_chr_fe_write_all(dev
, buf
, n
);
95 if (msg_len_out
!= 0) {
96 n
= qemu_chr_fe_read_all(dev
, msg
, msg_len_out
);
105 static int tpm_emulator_unix_tx_bufs(TPMEmulator
*tpm_emu
,
106 const uint8_t *in
, uint32_t in_len
,
107 uint8_t *out
, uint32_t out_len
,
112 bool is_selftest
= false;
113 const struct tpm_resp_hdr
*hdr
= NULL
;
116 *selftest_done
= false;
117 is_selftest
= tpm_util_is_selftest(in
, in_len
);
120 ret
= qio_channel_write_all(tpm_emu
->data_ioc
, (char *)in
, in_len
, err
);
125 ret
= qio_channel_read_all(tpm_emu
->data_ioc
, (char *)out
, sizeof(*hdr
),
131 hdr
= (struct tpm_resp_hdr
*)out
;
133 ret
= qio_channel_read_all(tpm_emu
->data_ioc
, (char *)out
,
134 be32_to_cpu(hdr
->len
) - sizeof(*hdr
) , err
);
140 *selftest_done
= (be32_to_cpu(hdr
->errcode
) == 0);
146 static int tpm_emulator_set_locality(TPMEmulator
*tpm_emu
, uint8_t locty_number
)
150 DPRINTF("%s : locality: 0x%x", __func__
, locty_number
);
152 if (tpm_emu
->cur_locty_number
== locty_number
) {
156 DPRINTF("setting locality : 0x%x", locty_number
);
157 loc
.u
.req
.loc
= locty_number
;
158 if (tpm_emulator_ctrlcmd(&tpm_emu
->ctrl_chr
, CMD_SET_LOCALITY
, &loc
,
159 sizeof(loc
), sizeof(loc
)) < 0) {
160 error_report("tpm-emulator: could not set locality : %s",
165 loc
.u
.resp
.tpm_result
= be32_to_cpu(loc
.u
.resp
.tpm_result
);
166 if (loc
.u
.resp
.tpm_result
!= 0) {
167 error_report("tpm-emulator: TPM result for set locality : 0x%x",
168 loc
.u
.resp
.tpm_result
);
172 tpm_emu
->cur_locty_number
= locty_number
;
177 static void tpm_emulator_handle_request(TPMBackend
*tb
, TPMBackendCmd cmd
)
179 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
180 TPMLocality
*locty
= NULL
;
181 bool selftest_done
= false;
184 DPRINTF("processing command type %d", cmd
);
187 case TPM_BACKEND_CMD_PROCESS_CMD
:
188 locty
= tb
->tpm_state
->locty_data
;
189 if (tpm_emulator_set_locality(tpm_emu
,
190 tb
->tpm_state
->locty_number
) < 0 ||
191 tpm_emulator_unix_tx_bufs(tpm_emu
, locty
->w_buffer
.buffer
,
192 locty
->w_offset
, locty
->r_buffer
.buffer
,
193 locty
->r_buffer
.size
, &selftest_done
,
195 tpm_util_write_fatal_error_response(locty
->r_buffer
.buffer
,
196 locty
->r_buffer
.size
);
197 error_report_err(err
);
200 tb
->recv_data_callback(tb
->tpm_state
, tb
->tpm_state
->locty_number
,
204 case TPM_BACKEND_CMD_INIT
:
205 case TPM_BACKEND_CMD_END
:
206 case TPM_BACKEND_CMD_TPM_RESET
:
212 static int tpm_emulator_probe_caps(TPMEmulator
*tpm_emu
)
214 DPRINTF("%s", __func__
);
215 if (tpm_emulator_ctrlcmd(&tpm_emu
->ctrl_chr
, 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
;
240 case TPM_VERSION_2_0
:
241 caps
= PTM_CAP_INIT
| PTM_CAP_SHUTDOWN
| PTM_CAP_GET_TPMESTABLISHED
|
242 PTM_CAP_SET_LOCALITY
| PTM_CAP_RESET_TPMESTABLISHED
|
246 case TPM_VERSION_UNSPEC
:
247 error_report("tpm-emulator: TPM version has not been set");
251 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu
, caps
)) {
252 error_report("tpm-emulator: TPM does not implement minimum set of "
253 "required capabilities for TPM %s (0x%x)", tpm
, (int)caps
);
260 static int tpm_emulator_startup_tpm(TPMBackend
*tb
)
262 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
266 DPRINTF("%s", __func__
);
267 if (tpm_emulator_ctrlcmd(&tpm_emu
->ctrl_chr
, CMD_INIT
, &init
, sizeof(init
),
269 error_report("tpm-emulator: could not send INIT: %s",
274 res
= be32_to_cpu(init
.u
.resp
.tpm_result
);
276 error_report("tpm-emulator: TPM result for CMD_INIT: 0x%x", res
);
285 static bool tpm_emulator_get_tpm_established_flag(TPMBackend
*tb
)
287 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
290 DPRINTF("%s", __func__
);
291 if (tpm_emulator_ctrlcmd(&tpm_emu
->ctrl_chr
, CMD_GET_TPMESTABLISHED
, &est
,
292 0, sizeof(est
)) < 0) {
293 error_report("tpm-emulator: Could not get the TPM established flag: %s",
297 DPRINTF("established flag: %0x", est
.u
.resp
.bit
);
299 return (est
.u
.resp
.bit
!= 0);
302 static int tpm_emulator_reset_tpm_established_flag(TPMBackend
*tb
,
305 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
306 ptm_reset_est reset_est
;
309 /* only a TPM 2.0 will support this */
310 if (tpm_emu
->tpm_version
!= TPM_VERSION_2_0
) {
314 reset_est
.u
.req
.loc
= tpm_emu
->cur_locty_number
;
315 if (tpm_emulator_ctrlcmd(&tpm_emu
->ctrl_chr
, CMD_RESET_TPMESTABLISHED
,
316 &reset_est
, sizeof(reset_est
),
317 sizeof(reset_est
)) < 0) {
318 error_report("tpm-emulator: Could not reset the establishment bit: %s",
323 res
= be32_to_cpu(reset_est
.u
.resp
.tpm_result
);
325 error_report("tpm-emulator: TPM result for rest establixhed flag: 0x%x",
333 static void tpm_emulator_cancel_cmd(TPMBackend
*tb
)
335 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
338 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu
, PTM_CAP_CANCEL_TPM_CMD
)) {
339 DPRINTF("Backend does not support CANCEL_TPM_CMD");
343 if (tpm_emulator_ctrlcmd(&tpm_emu
->ctrl_chr
, CMD_CANCEL_TPM_CMD
, &res
, 0,
345 error_report("tpm-emulator: Could not cancel command: %s",
347 } else if (res
!= 0) {
348 error_report("tpm-emulator: Failed to cancel TPM: 0x%x",
353 static TPMVersion
tpm_emulator_get_tpm_version(TPMBackend
*tb
)
355 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
357 return tpm_emu
->tpm_version
;
360 static int tpm_emulator_block_migration(TPMEmulator
*tpm_emu
)
364 error_setg(&tpm_emu
->migration_blocker
,
365 "Migration disabled: TPM emulator not yet migratable");
366 migrate_add_blocker(tpm_emu
->migration_blocker
, &err
);
368 error_report_err(err
);
369 error_free(tpm_emu
->migration_blocker
);
370 tpm_emu
->migration_blocker
= NULL
;
378 static int tpm_emulator_prepare_data_fd(TPMEmulator
*tpm_emu
)
382 int fds
[2] = { -1, -1 };
384 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
) < 0) {
385 error_report("tpm-emulator: Failed to create socketpair");
389 qemu_chr_fe_set_msgfds(&tpm_emu
->ctrl_chr
, fds
+ 1, 1);
391 if (tpm_emulator_ctrlcmd(&tpm_emu
->ctrl_chr
, CMD_SET_DATAFD
, &res
, 0,
392 sizeof(res
)) || res
!= 0) {
393 error_report("tpm-emulator: Failed to send CMD_SET_DATAFD: %s",
398 tpm_emu
->data_ioc
= QIO_CHANNEL(qio_channel_socket_new_fd(fds
[0], &err
));
400 error_prepend(&err
, "tpm-emulator: Failed to create io channel: ");
401 error_report_err(err
);
415 static int tpm_emulator_handle_device_opts(TPMEmulator
*tpm_emu
, QemuOpts
*opts
)
419 value
= qemu_opt_get(opts
, "chardev");
422 Chardev
*dev
= qemu_chr_find(value
);
425 error_report("tpm-emulator: tpm chardev '%s' not found.", value
);
429 if (!qemu_chr_fe_init(&tpm_emu
->ctrl_chr
, dev
, &err
)) {
430 error_prepend(&err
, "tpm-emulator: No valid chardev found at '%s':",
432 error_report_err(err
);
436 tpm_emu
->options
->chardev
= g_strdup(value
);
439 if (tpm_emulator_prepare_data_fd(tpm_emu
) < 0) {
443 /* FIXME: tpm_util_test_tpmdev() accepts only on socket fd, as it also used
444 * by passthrough driver, which not yet using GIOChannel.
446 if (tpm_util_test_tpmdev(QIO_CHANNEL_SOCKET(tpm_emu
->data_ioc
)->fd
,
447 &tpm_emu
->tpm_version
)) {
448 error_report("'%s' is not emulating TPM device. Error: %s",
449 tpm_emu
->options
->chardev
, strerror(errno
));
453 DPRINTF("TPM Version %s", tpm_emu
->tpm_version
== TPM_VERSION_1_2
? "1.2" :
454 (tpm_emu
->tpm_version
== TPM_VERSION_2_0
? "2.0" : "Unspecified"));
456 if (tpm_emulator_probe_caps(tpm_emu
) ||
457 tpm_emulator_check_caps(tpm_emu
)) {
461 return tpm_emulator_block_migration(tpm_emu
);
464 DPRINTF("Startup error");
468 static TPMBackend
*tpm_emulator_create(QemuOpts
*opts
, const char *id
)
470 TPMBackend
*tb
= TPM_BACKEND(object_new(TYPE_TPM_EMULATOR
));
472 tb
->id
= g_strdup(id
);
474 if (tpm_emulator_handle_device_opts(TPM_EMULATOR(tb
), opts
)) {
481 object_unref(OBJECT(tb
));
486 static TpmTypeOptions
*tpm_emulator_get_tpm_options(TPMBackend
*tb
)
488 TPMEmulator
*tpm_emu
= TPM_EMULATOR(tb
);
489 TpmTypeOptions
*options
= g_new0(TpmTypeOptions
, 1);
491 options
->type
= TPM_TYPE_OPTIONS_KIND_EMULATOR
;
492 options
->u
.emulator
.data
= QAPI_CLONE(TPMEmulatorOptions
, tpm_emu
->options
);
497 static const QemuOptDesc tpm_emulator_cmdline_opts
[] = {
498 TPM_STANDARD_CMDLINE_OPTS
,
501 .type
= QEMU_OPT_STRING
,
502 .help
= "Character device to use for out-of-band control messages",
504 { /* end of list */ },
507 static const TPMDriverOps tpm_emulator_driver
= {
508 .type
= TPM_TYPE_EMULATOR
,
509 .opts
= tpm_emulator_cmdline_opts
,
510 .desc
= "TPM emulator backend driver",
512 .create
= tpm_emulator_create
,
513 .startup_tpm
= tpm_emulator_startup_tpm
,
514 .cancel_cmd
= tpm_emulator_cancel_cmd
,
515 .get_tpm_established_flag
= tpm_emulator_get_tpm_established_flag
,
516 .reset_tpm_established_flag
= tpm_emulator_reset_tpm_established_flag
,
517 .get_tpm_version
= tpm_emulator_get_tpm_version
,
518 .get_tpm_options
= tpm_emulator_get_tpm_options
,
521 static void tpm_emulator_inst_init(Object
*obj
)
523 TPMEmulator
*tpm_emu
= TPM_EMULATOR(obj
);
525 DPRINTF("%s", __func__
);
526 tpm_emu
->options
= g_new0(TPMEmulatorOptions
, 1);
527 tpm_emu
->cur_locty_number
= ~0;
531 * Gracefully shut down the external TPM
533 static void tpm_emulator_shutdown(TPMEmulator
*tpm_emu
)
537 if (tpm_emulator_ctrlcmd(&tpm_emu
->ctrl_chr
, CMD_SHUTDOWN
, &res
, 0,
539 error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s",
541 } else if (res
!= 0) {
542 error_report("tpm-emulator: TPM result for sutdown: 0x%x",
547 static void tpm_emulator_inst_finalize(Object
*obj
)
549 TPMEmulator
*tpm_emu
= TPM_EMULATOR(obj
);
551 tpm_emulator_shutdown(tpm_emu
);
553 object_unref(OBJECT(tpm_emu
->data_ioc
));
555 qemu_chr_fe_deinit(&tpm_emu
->ctrl_chr
, false);
557 qapi_free_TPMEmulatorOptions(tpm_emu
->options
);
559 if (tpm_emu
->migration_blocker
) {
560 migrate_del_blocker(tpm_emu
->migration_blocker
);
561 error_free(tpm_emu
->migration_blocker
);
565 static void tpm_emulator_class_init(ObjectClass
*klass
, void *data
)
567 TPMBackendClass
*tbc
= TPM_BACKEND_CLASS(klass
);
568 tbc
->ops
= &tpm_emulator_driver
;
569 tbc
->handle_request
= tpm_emulator_handle_request
;
572 static const TypeInfo tpm_emulator_info
= {
573 .name
= TYPE_TPM_EMULATOR
,
574 .parent
= TYPE_TPM_BACKEND
,
575 .instance_size
= sizeof(TPMEmulator
),
576 .class_init
= tpm_emulator_class_init
,
577 .instance_init
= tpm_emulator_inst_init
,
578 .instance_finalize
= tpm_emulator_inst_finalize
,
581 static void tpm_emulator_register(void)
583 type_register_static(&tpm_emulator_info
);
584 tpm_register_driver(&tpm_emulator_driver
);
587 type_init(tpm_emulator_register
)