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/>
27 #include "qemu-common.h"
28 #include "qapi/error.h"
29 #include "qemu/sockets.h"
30 #include "sysemu/tpm_backend.h"
33 #include "hw/i386/pc.h"
34 #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
;
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
)
84 return send_all(fd
, buf
, len
);
87 static int tpm_passthrough_unix_read(int fd
, uint8_t *buf
, uint32_t len
)
89 return recv_all(fd
, buf
, len
, true);
92 static uint32_t tpm_passthrough_get_size_from_buffer(const uint8_t *buf
)
94 struct tpm_resp_hdr
*resp
= (struct tpm_resp_hdr
*)buf
;
96 return be32_to_cpu(resp
->len
);
100 * Write an error message in the given output buffer.
102 static void tpm_write_fatal_error_response(uint8_t *out
, uint32_t out_len
)
104 if (out_len
>= sizeof(struct tpm_resp_hdr
)) {
105 struct tpm_resp_hdr
*resp
= (struct tpm_resp_hdr
*)out
;
107 resp
->tag
= cpu_to_be16(TPM_TAG_RSP_COMMAND
);
108 resp
->len
= cpu_to_be32(sizeof(struct tpm_resp_hdr
));
109 resp
->errcode
= cpu_to_be32(TPM_FAIL
);
113 static bool tpm_passthrough_is_selftest(const uint8_t *in
, uint32_t in_len
)
115 struct tpm_req_hdr
*hdr
= (struct tpm_req_hdr
*)in
;
117 if (in_len
>= sizeof(*hdr
)) {
118 return (be32_to_cpu(hdr
->ordinal
) == TPM_ORD_ContinueSelfTest
);
124 static int tpm_passthrough_unix_tx_bufs(TPMPassthruState
*tpm_pt
,
125 const uint8_t *in
, uint32_t in_len
,
126 uint8_t *out
, uint32_t out_len
,
131 const struct tpm_resp_hdr
*hdr
;
133 tpm_pt
->tpm_op_canceled
= false;
134 tpm_pt
->tpm_executing
= true;
135 *selftest_done
= false;
137 is_selftest
= tpm_passthrough_is_selftest(in
, in_len
);
139 ret
= tpm_passthrough_unix_write(tpm_pt
->tpm_fd
, in
, in_len
);
141 if (!tpm_pt
->tpm_op_canceled
||
142 (tpm_pt
->tpm_op_canceled
&& errno
!= ECANCELED
)) {
143 error_report("tpm_passthrough: error while transmitting data "
145 strerror(errno
), errno
);
150 tpm_pt
->tpm_executing
= false;
152 ret
= tpm_passthrough_unix_read(tpm_pt
->tpm_fd
, out
, out_len
);
154 if (!tpm_pt
->tpm_op_canceled
||
155 (tpm_pt
->tpm_op_canceled
&& errno
!= ECANCELED
)) {
156 error_report("tpm_passthrough: error while reading data from "
158 strerror(errno
), errno
);
160 } else if (ret
< sizeof(struct tpm_resp_hdr
) ||
161 tpm_passthrough_get_size_from_buffer(out
) != ret
) {
163 error_report("tpm_passthrough: received invalid response "
167 if (is_selftest
&& (ret
>= sizeof(struct tpm_resp_hdr
))) {
168 hdr
= (struct tpm_resp_hdr
*)out
;
169 *selftest_done
= (be32_to_cpu(hdr
->errcode
) == 0);
174 tpm_write_fatal_error_response(out
, out_len
);
177 tpm_pt
->tpm_executing
= false;
182 static int tpm_passthrough_unix_transfer(TPMPassthruState
*tpm_pt
,
183 const TPMLocality
*locty_data
,
186 return tpm_passthrough_unix_tx_bufs(tpm_pt
,
187 locty_data
->w_buffer
.buffer
,
188 locty_data
->w_offset
,
189 locty_data
->r_buffer
.buffer
,
190 locty_data
->r_buffer
.size
,
194 static void tpm_passthrough_worker_thread(gpointer data
,
197 TPMPassthruThreadParams
*thr_parms
= user_data
;
198 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(thr_parms
->tb
);
199 TPMBackendCmd cmd
= (TPMBackendCmd
)data
;
200 bool selftest_done
= false;
202 DPRINTF("tpm_passthrough: processing command type %d\n", cmd
);
205 case TPM_BACKEND_CMD_PROCESS_CMD
:
206 tpm_passthrough_unix_transfer(tpm_pt
,
207 thr_parms
->tpm_state
->locty_data
,
210 thr_parms
->recv_data_callback(thr_parms
->tpm_state
,
211 thr_parms
->tpm_state
->locty_number
,
214 case TPM_BACKEND_CMD_INIT
:
215 case TPM_BACKEND_CMD_END
:
216 case TPM_BACKEND_CMD_TPM_RESET
:
223 * Start the TPM (thread). If it had been started before, then terminate
224 * and start it again.
226 static int tpm_passthrough_startup_tpm(TPMBackend
*tb
)
228 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
230 /* terminate a running TPM */
231 tpm_backend_thread_end(&tpm_pt
->tbt
);
233 tpm_backend_thread_create(&tpm_pt
->tbt
,
234 tpm_passthrough_worker_thread
,
235 &tpm_pt
->tpm_thread_params
);
240 static void tpm_passthrough_reset(TPMBackend
*tb
)
242 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
244 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
246 tpm_passthrough_cancel_cmd(tb
);
248 tpm_backend_thread_end(&tpm_pt
->tbt
);
250 tpm_pt
->had_startup_error
= false;
253 static int tpm_passthrough_init(TPMBackend
*tb
, TPMState
*s
,
254 TPMRecvDataCB
*recv_data_cb
)
256 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
258 tpm_pt
->tpm_thread_params
.tpm_state
= s
;
259 tpm_pt
->tpm_thread_params
.recv_data_callback
= recv_data_cb
;
260 tpm_pt
->tpm_thread_params
.tb
= tb
;
265 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend
*tb
)
270 static bool tpm_passthrough_get_startup_error(TPMBackend
*tb
)
272 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
274 return tpm_pt
->had_startup_error
;
277 static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer
*sb
)
279 size_t wanted_size
= 4096; /* Linux tpm.c buffer size */
281 if (sb
->size
!= wanted_size
) {
282 sb
->buffer
= g_realloc(sb
->buffer
, wanted_size
);
283 sb
->size
= wanted_size
;
288 static void tpm_passthrough_deliver_request(TPMBackend
*tb
)
290 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
292 tpm_backend_thread_deliver_request(&tpm_pt
->tbt
);
295 static void tpm_passthrough_cancel_cmd(TPMBackend
*tb
)
297 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
301 * As of Linux 3.7 the tpm_tis driver does not properly cancel
302 * commands on all TPM manufacturers' TPMs.
303 * Only cancel if we're busy so we don't cancel someone else's
304 * command, e.g., a command executed on the host.
306 if (tpm_pt
->tpm_executing
) {
307 if (tpm_pt
->cancel_fd
>= 0) {
308 n
= write(tpm_pt
->cancel_fd
, "-", 1);
310 error_report("Canceling TPM command failed: %s",
313 tpm_pt
->tpm_op_canceled
= true;
316 error_report("Cannot cancel TPM command due to missing "
317 "TPM sysfs cancel entry");
322 static const char *tpm_passthrough_create_desc(void)
324 return "Passthrough TPM backend driver";
328 * A basic test of a TPM device. We expect a well formatted response header
329 * (error response is fine) within one second.
331 static int tpm_passthrough_test_tpmdev(int fd
)
333 struct tpm_req_hdr req
= {
334 .tag
= cpu_to_be16(TPM_TAG_RQU_COMMAND
),
335 .len
= cpu_to_be32(sizeof(req
)),
336 .ordinal
= cpu_to_be32(TPM_ORD_GetTicks
),
338 struct tpm_resp_hdr
*resp
;
341 struct timeval tv
= {
345 unsigned char buf
[1024];
347 n
= write(fd
, &req
, sizeof(req
));
351 if (n
!= sizeof(req
)) {
356 FD_SET(fd
, &readfds
);
358 /* wait for a second */
359 n
= select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
364 n
= read(fd
, &buf
, sizeof(buf
));
365 if (n
< sizeof(struct tpm_resp_hdr
)) {
369 resp
= (struct tpm_resp_hdr
*)buf
;
370 /* check the header */
371 if (be16_to_cpu(resp
->tag
) != TPM_TAG_RSP_COMMAND
||
372 be32_to_cpu(resp
->len
) != n
) {
380 * Unless path or file descriptor set has been provided by user,
381 * determine the sysfs cancel file following kernel documentation
382 * in Documentation/ABI/stable/sysfs-class-tpm.
383 * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
385 static int tpm_passthrough_open_sysfs_cancel(TPMBackend
*tb
)
387 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
392 if (tb
->cancel_path
) {
393 fd
= qemu_open(tb
->cancel_path
, O_WRONLY
);
395 error_report("Could not open TPM cancel path : %s",
401 dev
= strrchr(tpm_pt
->tpm_dev
, '/');
404 if (snprintf(path
, sizeof(path
), "/sys/class/misc/%s/device/cancel",
405 dev
) < sizeof(path
)) {
406 fd
= qemu_open(path
, O_WRONLY
);
408 tb
->cancel_path
= g_strdup(path
);
410 error_report("tpm_passthrough: Could not open TPM cancel "
411 "path %s : %s", path
, strerror(errno
));
415 error_report("tpm_passthrough: Bad TPM device path %s",
422 static int tpm_passthrough_handle_device_opts(QemuOpts
*opts
, TPMBackend
*tb
)
424 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
427 value
= qemu_opt_get(opts
, "cancel-path");
428 tb
->cancel_path
= g_strdup(value
);
430 value
= qemu_opt_get(opts
, "path");
432 value
= TPM_PASSTHROUGH_DEFAULT_DEVICE
;
435 tpm_pt
->tpm_dev
= g_strdup(value
);
437 tb
->path
= g_strdup(tpm_pt
->tpm_dev
);
439 tpm_pt
->tpm_fd
= qemu_open(tpm_pt
->tpm_dev
, O_RDWR
);
440 if (tpm_pt
->tpm_fd
< 0) {
441 error_report("Cannot access TPM device using '%s': %s",
442 tpm_pt
->tpm_dev
, strerror(errno
));
443 goto err_free_parameters
;
446 if (tpm_passthrough_test_tpmdev(tpm_pt
->tpm_fd
)) {
447 error_report("'%s' is not a TPM device.",
449 goto err_close_tpmdev
;
455 qemu_close(tpm_pt
->tpm_fd
);
462 g_free(tpm_pt
->tpm_dev
);
463 tpm_pt
->tpm_dev
= NULL
;
468 static TPMBackend
*tpm_passthrough_create(QemuOpts
*opts
, const char *id
)
470 Object
*obj
= object_new(TYPE_TPM_PASSTHROUGH
);
471 TPMBackend
*tb
= TPM_BACKEND(obj
);
472 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
474 tb
->id
= g_strdup(id
);
475 /* let frontend set the fe_model to proper value */
478 tb
->ops
= &tpm_passthrough_driver
;
480 if (tpm_passthrough_handle_device_opts(opts
, tb
)) {
484 tpm_pt
->cancel_fd
= tpm_passthrough_open_sysfs_cancel(tb
);
485 if (tpm_pt
->cancel_fd
< 0) {
497 static void tpm_passthrough_destroy(TPMBackend
*tb
)
499 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
501 tpm_passthrough_cancel_cmd(tb
);
503 tpm_backend_thread_end(&tpm_pt
->tbt
);
505 qemu_close(tpm_pt
->tpm_fd
);
506 qemu_close(tpm_pt
->cancel_fd
);
510 g_free(tb
->cancel_path
);
511 g_free(tpm_pt
->tpm_dev
);
514 static const QemuOptDesc tpm_passthrough_cmdline_opts
[] = {
515 TPM_STANDARD_CMDLINE_OPTS
,
517 .name
= "cancel-path",
518 .type
= QEMU_OPT_STRING
,
519 .help
= "Sysfs file entry for canceling TPM commands",
523 .type
= QEMU_OPT_STRING
,
524 .help
= "Path to TPM device on the host",
526 { /* end of list */ },
529 static const TPMDriverOps tpm_passthrough_driver
= {
530 .type
= TPM_TYPE_PASSTHROUGH
,
531 .opts
= tpm_passthrough_cmdline_opts
,
532 .desc
= tpm_passthrough_create_desc
,
533 .create
= tpm_passthrough_create
,
534 .destroy
= tpm_passthrough_destroy
,
535 .init
= tpm_passthrough_init
,
536 .startup_tpm
= tpm_passthrough_startup_tpm
,
537 .realloc_buffer
= tpm_passthrough_realloc_buffer
,
538 .reset
= tpm_passthrough_reset
,
539 .had_startup_error
= tpm_passthrough_get_startup_error
,
540 .deliver_request
= tpm_passthrough_deliver_request
,
541 .cancel_cmd
= tpm_passthrough_cancel_cmd
,
542 .get_tpm_established_flag
= tpm_passthrough_get_tpm_established_flag
,
545 static void tpm_passthrough_inst_init(Object
*obj
)
549 static void tpm_passthrough_inst_finalize(Object
*obj
)
553 static void tpm_passthrough_class_init(ObjectClass
*klass
, void *data
)
555 TPMBackendClass
*tbc
= TPM_BACKEND_CLASS(klass
);
557 tbc
->ops
= &tpm_passthrough_driver
;
560 static const TypeInfo tpm_passthrough_info
= {
561 .name
= TYPE_TPM_PASSTHROUGH
,
562 .parent
= TYPE_TPM_BACKEND
,
563 .instance_size
= sizeof(TPMPassthruState
),
564 .class_init
= tpm_passthrough_class_init
,
565 .instance_init
= tpm_passthrough_inst_init
,
566 .instance_finalize
= tpm_passthrough_inst_finalize
,
569 static void tpm_passthrough_register(void)
571 type_register_static(&tpm_passthrough_info
);
572 tpm_register_driver(&tpm_passthrough_driver
);
575 type_init(tpm_passthrough_register
)