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/osdep.h"
26 #include "qemu-common.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)
50 typedef struct TPMPassthruThreadParams
{
53 TPMRecvDataCB
*recv_data_callback
;
55 } TPMPassthruThreadParams
;
57 struct TPMPassthruState
{
62 TPMPassthruThreadParams tpm_thread_params
;
69 bool had_startup_error
;
71 TPMVersion tpm_version
;
74 typedef struct TPMPassthruState TPMPassthruState
;
76 #define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
80 static void tpm_passthrough_cancel_cmd(TPMBackend
*tb
);
82 static int tpm_passthrough_unix_write(int fd
, const uint8_t *buf
, uint32_t len
)
88 ret
= write(fd
, buf
, remain
);
90 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
93 } else if (ret
== 0) {
103 static int tpm_passthrough_unix_read(int fd
, uint8_t *buf
, uint32_t len
)
107 ret
= read(fd
, buf
, len
);
109 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
117 static uint32_t tpm_passthrough_get_size_from_buffer(const uint8_t *buf
)
119 struct tpm_resp_hdr
*resp
= (struct tpm_resp_hdr
*)buf
;
121 return be32_to_cpu(resp
->len
);
125 * Write an error message in the given output buffer.
127 static void tpm_write_fatal_error_response(uint8_t *out
, uint32_t out_len
)
129 if (out_len
>= sizeof(struct tpm_resp_hdr
)) {
130 struct tpm_resp_hdr
*resp
= (struct tpm_resp_hdr
*)out
;
132 resp
->tag
= cpu_to_be16(TPM_TAG_RSP_COMMAND
);
133 resp
->len
= cpu_to_be32(sizeof(struct tpm_resp_hdr
));
134 resp
->errcode
= cpu_to_be32(TPM_FAIL
);
138 static bool tpm_passthrough_is_selftest(const uint8_t *in
, uint32_t in_len
)
140 struct tpm_req_hdr
*hdr
= (struct tpm_req_hdr
*)in
;
142 if (in_len
>= sizeof(*hdr
)) {
143 return (be32_to_cpu(hdr
->ordinal
) == TPM_ORD_ContinueSelfTest
);
149 static int tpm_passthrough_unix_tx_bufs(TPMPassthruState
*tpm_pt
,
150 const uint8_t *in
, uint32_t in_len
,
151 uint8_t *out
, uint32_t out_len
,
156 const struct tpm_resp_hdr
*hdr
;
158 tpm_pt
->tpm_op_canceled
= false;
159 tpm_pt
->tpm_executing
= true;
160 *selftest_done
= false;
162 is_selftest
= tpm_passthrough_is_selftest(in
, in_len
);
164 ret
= tpm_passthrough_unix_write(tpm_pt
->tpm_fd
, in
, in_len
);
166 if (!tpm_pt
->tpm_op_canceled
|| errno
!= ECANCELED
) {
167 error_report("tpm_passthrough: error while transmitting data "
169 strerror(errno
), errno
);
174 tpm_pt
->tpm_executing
= false;
176 ret
= tpm_passthrough_unix_read(tpm_pt
->tpm_fd
, out
, out_len
);
178 if (!tpm_pt
->tpm_op_canceled
|| errno
!= ECANCELED
) {
179 error_report("tpm_passthrough: error while reading data from "
181 strerror(errno
), errno
);
183 } else if (ret
< sizeof(struct tpm_resp_hdr
) ||
184 tpm_passthrough_get_size_from_buffer(out
) != ret
) {
186 error_report("tpm_passthrough: received invalid response "
190 if (is_selftest
&& (ret
>= sizeof(struct tpm_resp_hdr
))) {
191 hdr
= (struct tpm_resp_hdr
*)out
;
192 *selftest_done
= (be32_to_cpu(hdr
->errcode
) == 0);
197 tpm_write_fatal_error_response(out
, out_len
);
200 tpm_pt
->tpm_executing
= false;
205 static int tpm_passthrough_unix_transfer(TPMPassthruState
*tpm_pt
,
206 const TPMLocality
*locty_data
,
209 return tpm_passthrough_unix_tx_bufs(tpm_pt
,
210 locty_data
->w_buffer
.buffer
,
211 locty_data
->w_offset
,
212 locty_data
->r_buffer
.buffer
,
213 locty_data
->r_buffer
.size
,
217 static void tpm_passthrough_worker_thread(gpointer data
,
220 TPMPassthruThreadParams
*thr_parms
= user_data
;
221 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(thr_parms
->tb
);
222 TPMBackendCmd cmd
= (TPMBackendCmd
)data
;
223 bool selftest_done
= false;
225 DPRINTF("tpm_passthrough: processing command type %d\n", cmd
);
228 case TPM_BACKEND_CMD_PROCESS_CMD
:
229 tpm_passthrough_unix_transfer(tpm_pt
,
230 thr_parms
->tpm_state
->locty_data
,
233 thr_parms
->recv_data_callback(thr_parms
->tpm_state
,
234 thr_parms
->tpm_state
->locty_number
,
237 case TPM_BACKEND_CMD_INIT
:
238 case TPM_BACKEND_CMD_END
:
239 case TPM_BACKEND_CMD_TPM_RESET
:
246 * Start the TPM (thread). If it had been started before, then terminate
247 * and start it again.
249 static int tpm_passthrough_startup_tpm(TPMBackend
*tb
)
251 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
253 /* terminate a running TPM */
254 tpm_backend_thread_end(&tpm_pt
->tbt
);
256 tpm_backend_thread_create(&tpm_pt
->tbt
,
257 tpm_passthrough_worker_thread
,
258 &tpm_pt
->tpm_thread_params
);
263 static void tpm_passthrough_reset(TPMBackend
*tb
)
265 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
267 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
269 tpm_passthrough_cancel_cmd(tb
);
271 tpm_backend_thread_end(&tpm_pt
->tbt
);
273 tpm_pt
->had_startup_error
= false;
276 static int tpm_passthrough_init(TPMBackend
*tb
, TPMState
*s
,
277 TPMRecvDataCB
*recv_data_cb
)
279 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
281 tpm_pt
->tpm_thread_params
.tpm_state
= s
;
282 tpm_pt
->tpm_thread_params
.recv_data_callback
= recv_data_cb
;
283 tpm_pt
->tpm_thread_params
.tb
= tb
;
288 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend
*tb
)
293 static int tpm_passthrough_reset_tpm_established_flag(TPMBackend
*tb
,
296 /* only a TPM 2.0 will support this */
300 static bool tpm_passthrough_get_startup_error(TPMBackend
*tb
)
302 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
304 return tpm_pt
->had_startup_error
;
307 static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer
*sb
)
309 size_t wanted_size
= 4096; /* Linux tpm.c buffer size */
311 if (sb
->size
!= wanted_size
) {
312 sb
->buffer
= g_realloc(sb
->buffer
, wanted_size
);
313 sb
->size
= wanted_size
;
318 static void tpm_passthrough_deliver_request(TPMBackend
*tb
)
320 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
322 tpm_backend_thread_deliver_request(&tpm_pt
->tbt
);
325 static void tpm_passthrough_cancel_cmd(TPMBackend
*tb
)
327 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
331 * As of Linux 3.7 the tpm_tis driver does not properly cancel
332 * commands on all TPM manufacturers' TPMs.
333 * Only cancel if we're busy so we don't cancel someone else's
334 * command, e.g., a command executed on the host.
336 if (tpm_pt
->tpm_executing
) {
337 if (tpm_pt
->cancel_fd
>= 0) {
338 n
= write(tpm_pt
->cancel_fd
, "-", 1);
340 error_report("Canceling TPM command failed: %s",
343 tpm_pt
->tpm_op_canceled
= true;
346 error_report("Cannot cancel TPM command due to missing "
347 "TPM sysfs cancel entry");
352 static const char *tpm_passthrough_create_desc(void)
354 return "Passthrough TPM backend driver";
357 static TPMVersion
tpm_passthrough_get_tpm_version(TPMBackend
*tb
)
359 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
361 return tpm_pt
->tpm_version
;
365 * Unless path or file descriptor set has been provided by user,
366 * determine the sysfs cancel file following kernel documentation
367 * in Documentation/ABI/stable/sysfs-class-tpm.
368 * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
370 static int tpm_passthrough_open_sysfs_cancel(TPMBackend
*tb
)
372 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
377 if (tb
->cancel_path
) {
378 fd
= qemu_open(tb
->cancel_path
, O_WRONLY
);
380 error_report("Could not open TPM cancel path : %s",
386 dev
= strrchr(tpm_pt
->tpm_dev
, '/');
389 if (snprintf(path
, sizeof(path
), "/sys/class/misc/%s/device/cancel",
390 dev
) < sizeof(path
)) {
391 fd
= qemu_open(path
, O_WRONLY
);
393 tb
->cancel_path
= g_strdup(path
);
395 error_report("tpm_passthrough: Could not open TPM cancel "
396 "path %s : %s", path
, strerror(errno
));
400 error_report("tpm_passthrough: Bad TPM device path %s",
407 static int tpm_passthrough_handle_device_opts(QemuOpts
*opts
, TPMBackend
*tb
)
409 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
412 value
= qemu_opt_get(opts
, "cancel-path");
413 tb
->cancel_path
= g_strdup(value
);
415 value
= qemu_opt_get(opts
, "path");
417 value
= TPM_PASSTHROUGH_DEFAULT_DEVICE
;
420 tpm_pt
->tpm_dev
= g_strdup(value
);
422 tb
->path
= g_strdup(tpm_pt
->tpm_dev
);
424 tpm_pt
->tpm_fd
= qemu_open(tpm_pt
->tpm_dev
, O_RDWR
);
425 if (tpm_pt
->tpm_fd
< 0) {
426 error_report("Cannot access TPM device using '%s': %s",
427 tpm_pt
->tpm_dev
, strerror(errno
));
428 goto err_free_parameters
;
431 if (tpm_util_test_tpmdev(tpm_pt
->tpm_fd
, &tpm_pt
->tpm_version
)) {
432 error_report("'%s' is not a TPM device.",
434 goto err_close_tpmdev
;
440 qemu_close(tpm_pt
->tpm_fd
);
447 g_free(tpm_pt
->tpm_dev
);
448 tpm_pt
->tpm_dev
= NULL
;
453 static TPMBackend
*tpm_passthrough_create(QemuOpts
*opts
, const char *id
)
455 Object
*obj
= object_new(TYPE_TPM_PASSTHROUGH
);
456 TPMBackend
*tb
= TPM_BACKEND(obj
);
457 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
459 tb
->id
= g_strdup(id
);
460 /* let frontend set the fe_model to proper value */
463 if (tpm_passthrough_handle_device_opts(opts
, tb
)) {
467 tpm_pt
->cancel_fd
= tpm_passthrough_open_sysfs_cancel(tb
);
468 if (tpm_pt
->cancel_fd
< 0) {
480 static void tpm_passthrough_destroy(TPMBackend
*tb
)
482 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
484 tpm_passthrough_cancel_cmd(tb
);
486 tpm_backend_thread_end(&tpm_pt
->tbt
);
488 qemu_close(tpm_pt
->tpm_fd
);
489 qemu_close(tpm_pt
->cancel_fd
);
493 g_free(tb
->cancel_path
);
494 g_free(tpm_pt
->tpm_dev
);
497 static const QemuOptDesc tpm_passthrough_cmdline_opts
[] = {
498 TPM_STANDARD_CMDLINE_OPTS
,
500 .name
= "cancel-path",
501 .type
= QEMU_OPT_STRING
,
502 .help
= "Sysfs file entry for canceling TPM commands",
506 .type
= QEMU_OPT_STRING
,
507 .help
= "Path to TPM device on the host",
509 { /* end of list */ },
512 static const TPMDriverOps tpm_passthrough_driver
= {
513 .type
= TPM_TYPE_PASSTHROUGH
,
514 .opts
= tpm_passthrough_cmdline_opts
,
515 .desc
= tpm_passthrough_create_desc
,
516 .create
= tpm_passthrough_create
,
517 .destroy
= tpm_passthrough_destroy
,
518 .init
= tpm_passthrough_init
,
519 .startup_tpm
= tpm_passthrough_startup_tpm
,
520 .realloc_buffer
= tpm_passthrough_realloc_buffer
,
521 .reset
= tpm_passthrough_reset
,
522 .had_startup_error
= tpm_passthrough_get_startup_error
,
523 .deliver_request
= tpm_passthrough_deliver_request
,
524 .cancel_cmd
= tpm_passthrough_cancel_cmd
,
525 .get_tpm_established_flag
= tpm_passthrough_get_tpm_established_flag
,
526 .reset_tpm_established_flag
= tpm_passthrough_reset_tpm_established_flag
,
527 .get_tpm_version
= tpm_passthrough_get_tpm_version
,
530 static void tpm_passthrough_inst_init(Object
*obj
)
534 static void tpm_passthrough_inst_finalize(Object
*obj
)
538 static void tpm_passthrough_class_init(ObjectClass
*klass
, void *data
)
540 TPMBackendClass
*tbc
= TPM_BACKEND_CLASS(klass
);
542 tbc
->ops
= &tpm_passthrough_driver
;
545 static const TypeInfo tpm_passthrough_info
= {
546 .name
= TYPE_TPM_PASSTHROUGH
,
547 .parent
= TYPE_TPM_BACKEND
,
548 .instance_size
= sizeof(TPMPassthruState
),
549 .class_init
= tpm_passthrough_class_init
,
550 .instance_init
= tpm_passthrough_inst_init
,
551 .instance_finalize
= tpm_passthrough_inst_finalize
,
554 static void tpm_passthrough_register(void)
556 type_register_static(&tpm_passthrough_info
);
557 tpm_register_driver(&tpm_passthrough_driver
);
560 type_init(tpm_passthrough_register
)