2 * passthrough TPM driver
4 * Copyright (c) 2010 - 2013 IBM Corporation
6 * Stefan Berger <stefanb@us.ibm.com>
8 * Copyright (C) 2011 IAIK, Graz University of Technology
9 * Author: Andreas Niederl
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, see <http://www.gnu.org/licenses/>
25 #include "qemu-common.h"
26 #include "qapi/error.h"
27 #include "qemu/error-report.h"
28 #include "qemu/sockets.h"
29 #include "sysemu/tpm_backend.h"
32 #include "hw/i386/pc.h"
33 #include "sysemu/tpm_backend_int.h"
39 #define DPRINTF(fmt, ...) do { \
41 fprintf(stderr, fmt, ## __VA_ARGS__); \
45 #define TYPE_TPM_PASSTHROUGH "tpm-passthrough"
46 #define TPM_PASSTHROUGH(obj) \
47 OBJECT_CHECK(TPMPassthruState, (obj), TYPE_TPM_PASSTHROUGH)
49 static const TPMDriverOps tpm_passthrough_driver
;
52 typedef struct TPMPassthruThreadParams
{
55 TPMRecvDataCB
*recv_data_callback
;
57 } TPMPassthruThreadParams
;
59 struct TPMPassthruState
{
64 TPMPassthruThreadParams tpm_thread_params
;
71 bool had_startup_error
;
73 TPMVersion tpm_version
;
76 typedef struct TPMPassthruState TPMPassthruState
;
78 #define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
82 static void tpm_passthrough_cancel_cmd(TPMBackend
*tb
);
84 static int tpm_passthrough_unix_write(int fd
, const uint8_t *buf
, uint32_t len
)
86 return send_all(fd
, buf
, len
);
89 static int tpm_passthrough_unix_read(int fd
, uint8_t *buf
, uint32_t len
)
91 return recv_all(fd
, buf
, len
, true);
94 static uint32_t tpm_passthrough_get_size_from_buffer(const uint8_t *buf
)
96 struct tpm_resp_hdr
*resp
= (struct tpm_resp_hdr
*)buf
;
98 return be32_to_cpu(resp
->len
);
102 * Write an error message in the given output buffer.
104 static void tpm_write_fatal_error_response(uint8_t *out
, uint32_t out_len
)
106 if (out_len
>= sizeof(struct tpm_resp_hdr
)) {
107 struct tpm_resp_hdr
*resp
= (struct tpm_resp_hdr
*)out
;
109 resp
->tag
= cpu_to_be16(TPM_TAG_RSP_COMMAND
);
110 resp
->len
= cpu_to_be32(sizeof(struct tpm_resp_hdr
));
111 resp
->errcode
= cpu_to_be32(TPM_FAIL
);
115 static bool tpm_passthrough_is_selftest(const uint8_t *in
, uint32_t in_len
)
117 struct tpm_req_hdr
*hdr
= (struct tpm_req_hdr
*)in
;
119 if (in_len
>= sizeof(*hdr
)) {
120 return (be32_to_cpu(hdr
->ordinal
) == TPM_ORD_ContinueSelfTest
);
126 static int tpm_passthrough_unix_tx_bufs(TPMPassthruState
*tpm_pt
,
127 const uint8_t *in
, uint32_t in_len
,
128 uint8_t *out
, uint32_t out_len
,
133 const struct tpm_resp_hdr
*hdr
;
135 tpm_pt
->tpm_op_canceled
= false;
136 tpm_pt
->tpm_executing
= true;
137 *selftest_done
= false;
139 is_selftest
= tpm_passthrough_is_selftest(in
, in_len
);
141 ret
= tpm_passthrough_unix_write(tpm_pt
->tpm_fd
, in
, in_len
);
143 if (!tpm_pt
->tpm_op_canceled
||
144 (tpm_pt
->tpm_op_canceled
&& errno
!= ECANCELED
)) {
145 error_report("tpm_passthrough: error while transmitting data "
147 strerror(errno
), errno
);
152 tpm_pt
->tpm_executing
= false;
154 ret
= tpm_passthrough_unix_read(tpm_pt
->tpm_fd
, out
, out_len
);
156 if (!tpm_pt
->tpm_op_canceled
||
157 (tpm_pt
->tpm_op_canceled
&& errno
!= ECANCELED
)) {
158 error_report("tpm_passthrough: error while reading data from "
160 strerror(errno
), errno
);
162 } else if (ret
< sizeof(struct tpm_resp_hdr
) ||
163 tpm_passthrough_get_size_from_buffer(out
) != ret
) {
165 error_report("tpm_passthrough: received invalid response "
169 if (is_selftest
&& (ret
>= sizeof(struct tpm_resp_hdr
))) {
170 hdr
= (struct tpm_resp_hdr
*)out
;
171 *selftest_done
= (be32_to_cpu(hdr
->errcode
) == 0);
176 tpm_write_fatal_error_response(out
, out_len
);
179 tpm_pt
->tpm_executing
= false;
184 static int tpm_passthrough_unix_transfer(TPMPassthruState
*tpm_pt
,
185 const TPMLocality
*locty_data
,
188 return tpm_passthrough_unix_tx_bufs(tpm_pt
,
189 locty_data
->w_buffer
.buffer
,
190 locty_data
->w_offset
,
191 locty_data
->r_buffer
.buffer
,
192 locty_data
->r_buffer
.size
,
196 static void tpm_passthrough_worker_thread(gpointer data
,
199 TPMPassthruThreadParams
*thr_parms
= user_data
;
200 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(thr_parms
->tb
);
201 TPMBackendCmd cmd
= (TPMBackendCmd
)data
;
202 bool selftest_done
= false;
204 DPRINTF("tpm_passthrough: processing command type %d\n", cmd
);
207 case TPM_BACKEND_CMD_PROCESS_CMD
:
208 tpm_passthrough_unix_transfer(tpm_pt
,
209 thr_parms
->tpm_state
->locty_data
,
212 thr_parms
->recv_data_callback(thr_parms
->tpm_state
,
213 thr_parms
->tpm_state
->locty_number
,
216 case TPM_BACKEND_CMD_INIT
:
217 case TPM_BACKEND_CMD_END
:
218 case TPM_BACKEND_CMD_TPM_RESET
:
225 * Start the TPM (thread). If it had been started before, then terminate
226 * and start it again.
228 static int tpm_passthrough_startup_tpm(TPMBackend
*tb
)
230 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
232 /* terminate a running TPM */
233 tpm_backend_thread_end(&tpm_pt
->tbt
);
235 tpm_backend_thread_create(&tpm_pt
->tbt
,
236 tpm_passthrough_worker_thread
,
237 &tpm_pt
->tpm_thread_params
);
242 static void tpm_passthrough_reset(TPMBackend
*tb
)
244 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
246 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
248 tpm_passthrough_cancel_cmd(tb
);
250 tpm_backend_thread_end(&tpm_pt
->tbt
);
252 tpm_pt
->had_startup_error
= false;
255 static int tpm_passthrough_init(TPMBackend
*tb
, TPMState
*s
,
256 TPMRecvDataCB
*recv_data_cb
)
258 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
260 tpm_pt
->tpm_thread_params
.tpm_state
= s
;
261 tpm_pt
->tpm_thread_params
.recv_data_callback
= recv_data_cb
;
262 tpm_pt
->tpm_thread_params
.tb
= tb
;
267 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend
*tb
)
272 static int tpm_passthrough_reset_tpm_established_flag(TPMBackend
*tb
,
275 /* only a TPM 2.0 will support this */
279 static bool tpm_passthrough_get_startup_error(TPMBackend
*tb
)
281 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
283 return tpm_pt
->had_startup_error
;
286 static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer
*sb
)
288 size_t wanted_size
= 4096; /* Linux tpm.c buffer size */
290 if (sb
->size
!= wanted_size
) {
291 sb
->buffer
= g_realloc(sb
->buffer
, wanted_size
);
292 sb
->size
= wanted_size
;
297 static void tpm_passthrough_deliver_request(TPMBackend
*tb
)
299 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
301 tpm_backend_thread_deliver_request(&tpm_pt
->tbt
);
304 static void tpm_passthrough_cancel_cmd(TPMBackend
*tb
)
306 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
310 * As of Linux 3.7 the tpm_tis driver does not properly cancel
311 * commands on all TPM manufacturers' TPMs.
312 * Only cancel if we're busy so we don't cancel someone else's
313 * command, e.g., a command executed on the host.
315 if (tpm_pt
->tpm_executing
) {
316 if (tpm_pt
->cancel_fd
>= 0) {
317 n
= write(tpm_pt
->cancel_fd
, "-", 1);
319 error_report("Canceling TPM command failed: %s",
322 tpm_pt
->tpm_op_canceled
= true;
325 error_report("Cannot cancel TPM command due to missing "
326 "TPM sysfs cancel entry");
331 static const char *tpm_passthrough_create_desc(void)
333 return "Passthrough TPM backend driver";
336 static TPMVersion
tpm_passthrough_get_tpm_version(TPMBackend
*tb
)
338 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
340 return tpm_pt
->tpm_version
;
344 * Unless path or file descriptor set has been provided by user,
345 * determine the sysfs cancel file following kernel documentation
346 * in Documentation/ABI/stable/sysfs-class-tpm.
347 * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
349 static int tpm_passthrough_open_sysfs_cancel(TPMBackend
*tb
)
351 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
356 if (tb
->cancel_path
) {
357 fd
= qemu_open(tb
->cancel_path
, O_WRONLY
);
359 error_report("Could not open TPM cancel path : %s",
365 dev
= strrchr(tpm_pt
->tpm_dev
, '/');
368 if (snprintf(path
, sizeof(path
), "/sys/class/misc/%s/device/cancel",
369 dev
) < sizeof(path
)) {
370 fd
= qemu_open(path
, O_WRONLY
);
372 tb
->cancel_path
= g_strdup(path
);
374 error_report("tpm_passthrough: Could not open TPM cancel "
375 "path %s : %s", path
, strerror(errno
));
379 error_report("tpm_passthrough: Bad TPM device path %s",
386 static int tpm_passthrough_handle_device_opts(QemuOpts
*opts
, TPMBackend
*tb
)
388 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
391 value
= qemu_opt_get(opts
, "cancel-path");
392 tb
->cancel_path
= g_strdup(value
);
394 value
= qemu_opt_get(opts
, "path");
396 value
= TPM_PASSTHROUGH_DEFAULT_DEVICE
;
399 tpm_pt
->tpm_dev
= g_strdup(value
);
401 tb
->path
= g_strdup(tpm_pt
->tpm_dev
);
403 tpm_pt
->tpm_fd
= qemu_open(tpm_pt
->tpm_dev
, O_RDWR
);
404 if (tpm_pt
->tpm_fd
< 0) {
405 error_report("Cannot access TPM device using '%s': %s",
406 tpm_pt
->tpm_dev
, strerror(errno
));
407 goto err_free_parameters
;
410 if (tpm_util_test_tpmdev(tpm_pt
->tpm_fd
, &tpm_pt
->tpm_version
)) {
411 error_report("'%s' is not a TPM device.",
413 goto err_close_tpmdev
;
419 qemu_close(tpm_pt
->tpm_fd
);
426 g_free(tpm_pt
->tpm_dev
);
427 tpm_pt
->tpm_dev
= NULL
;
432 static TPMBackend
*tpm_passthrough_create(QemuOpts
*opts
, const char *id
)
434 Object
*obj
= object_new(TYPE_TPM_PASSTHROUGH
);
435 TPMBackend
*tb
= TPM_BACKEND(obj
);
436 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
438 tb
->id
= g_strdup(id
);
439 /* let frontend set the fe_model to proper value */
442 tb
->ops
= &tpm_passthrough_driver
;
444 if (tpm_passthrough_handle_device_opts(opts
, tb
)) {
448 tpm_pt
->cancel_fd
= tpm_passthrough_open_sysfs_cancel(tb
);
449 if (tpm_pt
->cancel_fd
< 0) {
461 static void tpm_passthrough_destroy(TPMBackend
*tb
)
463 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
465 tpm_passthrough_cancel_cmd(tb
);
467 tpm_backend_thread_end(&tpm_pt
->tbt
);
469 qemu_close(tpm_pt
->tpm_fd
);
470 qemu_close(tpm_pt
->cancel_fd
);
474 g_free(tb
->cancel_path
);
475 g_free(tpm_pt
->tpm_dev
);
478 static const QemuOptDesc tpm_passthrough_cmdline_opts
[] = {
479 TPM_STANDARD_CMDLINE_OPTS
,
481 .name
= "cancel-path",
482 .type
= QEMU_OPT_STRING
,
483 .help
= "Sysfs file entry for canceling TPM commands",
487 .type
= QEMU_OPT_STRING
,
488 .help
= "Path to TPM device on the host",
490 { /* end of list */ },
493 static const TPMDriverOps tpm_passthrough_driver
= {
494 .type
= TPM_TYPE_PASSTHROUGH
,
495 .opts
= tpm_passthrough_cmdline_opts
,
496 .desc
= tpm_passthrough_create_desc
,
497 .create
= tpm_passthrough_create
,
498 .destroy
= tpm_passthrough_destroy
,
499 .init
= tpm_passthrough_init
,
500 .startup_tpm
= tpm_passthrough_startup_tpm
,
501 .realloc_buffer
= tpm_passthrough_realloc_buffer
,
502 .reset
= tpm_passthrough_reset
,
503 .had_startup_error
= tpm_passthrough_get_startup_error
,
504 .deliver_request
= tpm_passthrough_deliver_request
,
505 .cancel_cmd
= tpm_passthrough_cancel_cmd
,
506 .get_tpm_established_flag
= tpm_passthrough_get_tpm_established_flag
,
507 .reset_tpm_established_flag
= tpm_passthrough_reset_tpm_established_flag
,
508 .get_tpm_version
= tpm_passthrough_get_tpm_version
,
511 static void tpm_passthrough_inst_init(Object
*obj
)
515 static void tpm_passthrough_inst_finalize(Object
*obj
)
519 static void tpm_passthrough_class_init(ObjectClass
*klass
, void *data
)
521 TPMBackendClass
*tbc
= TPM_BACKEND_CLASS(klass
);
523 tbc
->ops
= &tpm_passthrough_driver
;
526 static const TypeInfo tpm_passthrough_info
= {
527 .name
= TYPE_TPM_PASSTHROUGH
,
528 .parent
= TYPE_TPM_BACKEND
,
529 .instance_size
= sizeof(TPMPassthruState
),
530 .class_init
= tpm_passthrough_class_init
,
531 .instance_init
= tpm_passthrough_inst_init
,
532 .instance_finalize
= tpm_passthrough_inst_finalize
,
535 static void tpm_passthrough_register(void)
537 type_register_static(&tpm_passthrough_info
);
538 tpm_register_driver(&tpm_passthrough_driver
);
541 type_init(tpm_passthrough_register
)