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 "qapi/clone-visitor.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 struct TPMPassthruState
{
53 TPMPassthroughOptions
*options
;
60 TPMVersion tpm_version
;
63 typedef struct TPMPassthruState TPMPassthruState
;
65 #define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
69 static void tpm_passthrough_cancel_cmd(TPMBackend
*tb
);
71 static int tpm_passthrough_unix_write(int fd
, const uint8_t *buf
, uint32_t len
)
77 ret
= write(fd
, buf
, remain
);
79 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
82 } else if (ret
== 0) {
92 static int tpm_passthrough_unix_read(int fd
, uint8_t *buf
, uint32_t len
)
96 ret
= read(fd
, buf
, len
);
98 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
106 static uint32_t tpm_passthrough_get_size_from_buffer(const uint8_t *buf
)
108 struct tpm_resp_hdr
*resp
= (struct tpm_resp_hdr
*)buf
;
110 return be32_to_cpu(resp
->len
);
114 * Write an error message in the given output buffer.
116 static void tpm_write_fatal_error_response(uint8_t *out
, uint32_t out_len
)
118 if (out_len
>= sizeof(struct tpm_resp_hdr
)) {
119 struct tpm_resp_hdr
*resp
= (struct tpm_resp_hdr
*)out
;
121 resp
->tag
= cpu_to_be16(TPM_TAG_RSP_COMMAND
);
122 resp
->len
= cpu_to_be32(sizeof(struct tpm_resp_hdr
));
123 resp
->errcode
= cpu_to_be32(TPM_FAIL
);
127 static bool tpm_passthrough_is_selftest(const uint8_t *in
, uint32_t in_len
)
129 struct tpm_req_hdr
*hdr
= (struct tpm_req_hdr
*)in
;
131 if (in_len
>= sizeof(*hdr
)) {
132 return (be32_to_cpu(hdr
->ordinal
) == TPM_ORD_ContinueSelfTest
);
138 static int tpm_passthrough_unix_tx_bufs(TPMPassthruState
*tpm_pt
,
139 const uint8_t *in
, uint32_t in_len
,
140 uint8_t *out
, uint32_t out_len
,
145 const struct tpm_resp_hdr
*hdr
;
147 tpm_pt
->tpm_op_canceled
= false;
148 tpm_pt
->tpm_executing
= true;
149 *selftest_done
= false;
151 is_selftest
= tpm_passthrough_is_selftest(in
, in_len
);
153 ret
= tpm_passthrough_unix_write(tpm_pt
->tpm_fd
, in
, in_len
);
155 if (!tpm_pt
->tpm_op_canceled
|| errno
!= ECANCELED
) {
156 error_report("tpm_passthrough: error while transmitting data "
158 strerror(errno
), errno
);
163 tpm_pt
->tpm_executing
= false;
165 ret
= tpm_passthrough_unix_read(tpm_pt
->tpm_fd
, out
, out_len
);
167 if (!tpm_pt
->tpm_op_canceled
|| errno
!= ECANCELED
) {
168 error_report("tpm_passthrough: error while reading data from "
170 strerror(errno
), errno
);
172 } else if (ret
< sizeof(struct tpm_resp_hdr
) ||
173 tpm_passthrough_get_size_from_buffer(out
) != ret
) {
175 error_report("tpm_passthrough: received invalid response "
179 if (is_selftest
&& (ret
>= sizeof(struct tpm_resp_hdr
))) {
180 hdr
= (struct tpm_resp_hdr
*)out
;
181 *selftest_done
= (be32_to_cpu(hdr
->errcode
) == 0);
186 tpm_write_fatal_error_response(out
, out_len
);
189 tpm_pt
->tpm_executing
= false;
194 static int tpm_passthrough_unix_transfer(TPMPassthruState
*tpm_pt
,
195 const TPMLocality
*locty_data
,
198 return tpm_passthrough_unix_tx_bufs(tpm_pt
,
199 locty_data
->w_buffer
.buffer
,
200 locty_data
->w_offset
,
201 locty_data
->r_buffer
.buffer
,
202 locty_data
->r_buffer
.size
,
206 static void tpm_passthrough_handle_request(TPMBackend
*tb
, TPMBackendCmd cmd
)
208 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
209 bool selftest_done
= false;
211 DPRINTF("tpm_passthrough: processing command type %d\n", cmd
);
214 case TPM_BACKEND_CMD_PROCESS_CMD
:
215 tpm_passthrough_unix_transfer(tpm_pt
,
216 tb
->tpm_state
->locty_data
,
219 tb
->recv_data_callback(tb
->tpm_state
,
220 tb
->tpm_state
->locty_number
,
223 case TPM_BACKEND_CMD_INIT
:
224 case TPM_BACKEND_CMD_END
:
225 case TPM_BACKEND_CMD_TPM_RESET
:
231 static void tpm_passthrough_reset(TPMBackend
*tb
)
233 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
235 tpm_passthrough_cancel_cmd(tb
);
238 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend
*tb
)
243 static int tpm_passthrough_reset_tpm_established_flag(TPMBackend
*tb
,
246 /* only a TPM 2.0 will support this */
250 static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer
*sb
)
252 size_t wanted_size
= 4096; /* Linux tpm.c buffer size */
254 if (sb
->size
!= wanted_size
) {
255 sb
->buffer
= g_realloc(sb
->buffer
, wanted_size
);
256 sb
->size
= wanted_size
;
261 static void tpm_passthrough_cancel_cmd(TPMBackend
*tb
)
263 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
267 * As of Linux 3.7 the tpm_tis driver does not properly cancel
268 * commands on all TPM manufacturers' TPMs.
269 * Only cancel if we're busy so we don't cancel someone else's
270 * command, e.g., a command executed on the host.
272 if (tpm_pt
->tpm_executing
) {
273 if (tpm_pt
->cancel_fd
>= 0) {
274 n
= write(tpm_pt
->cancel_fd
, "-", 1);
276 error_report("Canceling TPM command failed: %s",
279 tpm_pt
->tpm_op_canceled
= true;
282 error_report("Cannot cancel TPM command due to missing "
283 "TPM sysfs cancel entry");
288 static TPMVersion
tpm_passthrough_get_tpm_version(TPMBackend
*tb
)
290 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
292 return tpm_pt
->tpm_version
;
296 * Unless path or file descriptor set has been provided by user,
297 * determine the sysfs cancel file following kernel documentation
298 * in Documentation/ABI/stable/sysfs-class-tpm.
299 * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
301 static int tpm_passthrough_open_sysfs_cancel(TPMPassthruState
*tpm_pt
)
307 if (tpm_pt
->options
->cancel_path
) {
308 fd
= qemu_open(tpm_pt
->options
->cancel_path
, O_WRONLY
);
310 error_report("Could not open TPM cancel path : %s",
316 dev
= strrchr(tpm_pt
->tpm_dev
, '/');
319 if (snprintf(path
, sizeof(path
), "/sys/class/misc/%s/device/cancel",
320 dev
) < sizeof(path
)) {
321 fd
= qemu_open(path
, O_WRONLY
);
323 tpm_pt
->options
->cancel_path
= g_strdup(path
);
325 error_report("tpm_passthrough: Could not open TPM cancel "
326 "path %s : %s", path
, strerror(errno
));
330 error_report("tpm_passthrough: Bad TPM device path %s",
337 static int tpm_passthrough_handle_device_opts(QemuOpts
*opts
, TPMBackend
*tb
)
339 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
342 value
= qemu_opt_get(opts
, "cancel-path");
344 tpm_pt
->options
->cancel_path
= g_strdup(value
);
345 tpm_pt
->options
->has_cancel_path
= true;
348 value
= qemu_opt_get(opts
, "path");
350 tpm_pt
->options
->has_path
= true;
351 tpm_pt
->options
->path
= g_strdup(value
);
354 tpm_pt
->tpm_dev
= value
? value
: TPM_PASSTHROUGH_DEFAULT_DEVICE
;
355 tpm_pt
->tpm_fd
= qemu_open(tpm_pt
->tpm_dev
, O_RDWR
);
356 if (tpm_pt
->tpm_fd
< 0) {
357 error_report("Cannot access TPM device using '%s': %s",
358 tpm_pt
->tpm_dev
, strerror(errno
));
359 goto err_free_parameters
;
362 if (tpm_util_test_tpmdev(tpm_pt
->tpm_fd
, &tpm_pt
->tpm_version
)) {
363 error_report("'%s' is not a TPM device.",
365 goto err_close_tpmdev
;
371 qemu_close(tpm_pt
->tpm_fd
);
375 qapi_free_TPMPassthroughOptions(tpm_pt
->options
);
376 tpm_pt
->options
= NULL
;
377 tpm_pt
->tpm_dev
= NULL
;
382 static TPMBackend
*tpm_passthrough_create(QemuOpts
*opts
, const char *id
)
384 Object
*obj
= object_new(TYPE_TPM_PASSTHROUGH
);
385 TPMBackend
*tb
= TPM_BACKEND(obj
);
386 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
388 tb
->id
= g_strdup(id
);
390 if (tpm_passthrough_handle_device_opts(opts
, tb
)) {
394 tpm_pt
->cancel_fd
= tpm_passthrough_open_sysfs_cancel(tpm_pt
);
395 if (tpm_pt
->cancel_fd
< 0) {
407 static TpmTypeOptions
*tpm_passthrough_get_tpm_options(TPMBackend
*tb
)
409 TpmTypeOptions
*options
= g_new0(TpmTypeOptions
, 1);
411 options
->type
= TPM_TYPE_OPTIONS_KIND_PASSTHROUGH
;
412 options
->u
.passthrough
.data
= QAPI_CLONE(TPMPassthroughOptions
,
413 TPM_PASSTHROUGH(tb
)->options
);
418 static const QemuOptDesc tpm_passthrough_cmdline_opts
[] = {
419 TPM_STANDARD_CMDLINE_OPTS
,
421 .name
= "cancel-path",
422 .type
= QEMU_OPT_STRING
,
423 .help
= "Sysfs file entry for canceling TPM commands",
427 .type
= QEMU_OPT_STRING
,
428 .help
= "Path to TPM device on the host",
430 { /* end of list */ },
433 static const TPMDriverOps tpm_passthrough_driver
= {
434 .type
= TPM_TYPE_PASSTHROUGH
,
435 .opts
= tpm_passthrough_cmdline_opts
,
436 .desc
= "Passthrough TPM backend driver",
437 .create
= tpm_passthrough_create
,
438 .realloc_buffer
= tpm_passthrough_realloc_buffer
,
439 .reset
= tpm_passthrough_reset
,
440 .cancel_cmd
= tpm_passthrough_cancel_cmd
,
441 .get_tpm_established_flag
= tpm_passthrough_get_tpm_established_flag
,
442 .reset_tpm_established_flag
= tpm_passthrough_reset_tpm_established_flag
,
443 .get_tpm_version
= tpm_passthrough_get_tpm_version
,
444 .get_tpm_options
= tpm_passthrough_get_tpm_options
,
447 static void tpm_passthrough_inst_init(Object
*obj
)
449 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(obj
);
451 tpm_pt
->options
= g_new0(TPMPassthroughOptions
, 1);
453 tpm_pt
->cancel_fd
= -1;
456 static void tpm_passthrough_inst_finalize(Object
*obj
)
458 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(obj
);
460 tpm_passthrough_cancel_cmd(TPM_BACKEND(obj
));
462 qemu_close(tpm_pt
->tpm_fd
);
463 qemu_close(tpm_pt
->cancel_fd
);
464 qapi_free_TPMPassthroughOptions(tpm_pt
->options
);
467 static void tpm_passthrough_class_init(ObjectClass
*klass
, void *data
)
469 TPMBackendClass
*tbc
= TPM_BACKEND_CLASS(klass
);
471 tbc
->ops
= &tpm_passthrough_driver
;
472 tbc
->handle_request
= tpm_passthrough_handle_request
;
475 static const TypeInfo tpm_passthrough_info
= {
476 .name
= TYPE_TPM_PASSTHROUGH
,
477 .parent
= TYPE_TPM_BACKEND
,
478 .instance_size
= sizeof(TPMPassthruState
),
479 .class_init
= tpm_passthrough_class_init
,
480 .instance_init
= tpm_passthrough_inst_init
,
481 .instance_finalize
= tpm_passthrough_inst_finalize
,
484 static void tpm_passthrough_register(void)
486 type_register_static(&tpm_passthrough_info
);
487 tpm_register_driver(&tpm_passthrough_driver
);
490 type_init(tpm_passthrough_register
)