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_read(int fd
, uint8_t *buf
, uint32_t len
)
75 ret
= read(fd
, buf
, len
);
77 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
84 static int tpm_passthrough_unix_tx_bufs(TPMPassthruState
*tpm_pt
,
85 const uint8_t *in
, uint32_t in_len
,
86 uint8_t *out
, uint32_t out_len
,
91 const struct tpm_resp_hdr
*hdr
;
93 tpm_pt
->tpm_op_canceled
= false;
94 tpm_pt
->tpm_executing
= true;
95 *selftest_done
= false;
97 is_selftest
= tpm_util_is_selftest(in
, in_len
);
99 ret
= qemu_write_full(tpm_pt
->tpm_fd
, (const void *)in
, (size_t)in_len
);
101 if (!tpm_pt
->tpm_op_canceled
|| errno
!= ECANCELED
) {
102 error_report("tpm_passthrough: error while transmitting data "
104 strerror(errno
), errno
);
109 tpm_pt
->tpm_executing
= false;
111 ret
= tpm_passthrough_unix_read(tpm_pt
->tpm_fd
, out
, out_len
);
113 if (!tpm_pt
->tpm_op_canceled
|| errno
!= ECANCELED
) {
114 error_report("tpm_passthrough: error while reading data from "
116 strerror(errno
), errno
);
118 } else if (ret
< sizeof(struct tpm_resp_hdr
) ||
119 be32_to_cpu(((struct tpm_resp_hdr
*)out
)->len
) != ret
) {
121 error_report("tpm_passthrough: received invalid response "
125 if (is_selftest
&& (ret
>= sizeof(struct tpm_resp_hdr
))) {
126 hdr
= (struct tpm_resp_hdr
*)out
;
127 *selftest_done
= (be32_to_cpu(hdr
->errcode
) == 0);
132 tpm_util_write_fatal_error_response(out
, out_len
);
135 tpm_pt
->tpm_executing
= false;
140 static int tpm_passthrough_unix_transfer(TPMPassthruState
*tpm_pt
,
141 const TPMLocality
*locty_data
,
144 return tpm_passthrough_unix_tx_bufs(tpm_pt
,
145 locty_data
->w_buffer
.buffer
,
146 locty_data
->w_offset
,
147 locty_data
->r_buffer
.buffer
,
148 locty_data
->r_buffer
.size
,
152 static void tpm_passthrough_handle_request(TPMBackend
*tb
, TPMBackendCmd cmd
)
154 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
155 bool selftest_done
= false;
157 DPRINTF("tpm_passthrough: processing command type %d\n", cmd
);
160 case TPM_BACKEND_CMD_PROCESS_CMD
:
161 tpm_passthrough_unix_transfer(tpm_pt
,
162 tb
->tpm_state
->locty_data
,
165 tb
->recv_data_callback(tb
->tpm_state
,
166 tb
->tpm_state
->locty_number
,
169 case TPM_BACKEND_CMD_INIT
:
170 case TPM_BACKEND_CMD_END
:
171 case TPM_BACKEND_CMD_TPM_RESET
:
177 static void tpm_passthrough_reset(TPMBackend
*tb
)
179 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
181 tpm_passthrough_cancel_cmd(tb
);
184 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend
*tb
)
189 static int tpm_passthrough_reset_tpm_established_flag(TPMBackend
*tb
,
192 /* only a TPM 2.0 will support this */
196 static void tpm_passthrough_cancel_cmd(TPMBackend
*tb
)
198 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
202 * As of Linux 3.7 the tpm_tis driver does not properly cancel
203 * commands on all TPM manufacturers' TPMs.
204 * Only cancel if we're busy so we don't cancel someone else's
205 * command, e.g., a command executed on the host.
207 if (tpm_pt
->tpm_executing
) {
208 if (tpm_pt
->cancel_fd
>= 0) {
209 n
= write(tpm_pt
->cancel_fd
, "-", 1);
211 error_report("Canceling TPM command failed: %s",
214 tpm_pt
->tpm_op_canceled
= true;
217 error_report("Cannot cancel TPM command due to missing "
218 "TPM sysfs cancel entry");
223 static TPMVersion
tpm_passthrough_get_tpm_version(TPMBackend
*tb
)
225 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
227 return tpm_pt
->tpm_version
;
231 * Unless path or file descriptor set has been provided by user,
232 * determine the sysfs cancel file following kernel documentation
233 * in Documentation/ABI/stable/sysfs-class-tpm.
234 * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
236 static int tpm_passthrough_open_sysfs_cancel(TPMPassthruState
*tpm_pt
)
242 if (tpm_pt
->options
->cancel_path
) {
243 fd
= qemu_open(tpm_pt
->options
->cancel_path
, O_WRONLY
);
245 error_report("Could not open TPM cancel path : %s",
251 dev
= strrchr(tpm_pt
->tpm_dev
, '/');
254 if (snprintf(path
, sizeof(path
), "/sys/class/misc/%s/device/cancel",
255 dev
) < sizeof(path
)) {
256 fd
= qemu_open(path
, O_WRONLY
);
258 tpm_pt
->options
->cancel_path
= g_strdup(path
);
260 error_report("tpm_passthrough: Could not open TPM cancel "
261 "path %s : %s", path
, strerror(errno
));
265 error_report("tpm_passthrough: Bad TPM device path %s",
272 static int tpm_passthrough_handle_device_opts(QemuOpts
*opts
, TPMBackend
*tb
)
274 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
277 value
= qemu_opt_get(opts
, "cancel-path");
279 tpm_pt
->options
->cancel_path
= g_strdup(value
);
280 tpm_pt
->options
->has_cancel_path
= true;
283 value
= qemu_opt_get(opts
, "path");
285 tpm_pt
->options
->has_path
= true;
286 tpm_pt
->options
->path
= g_strdup(value
);
289 tpm_pt
->tpm_dev
= value
? value
: TPM_PASSTHROUGH_DEFAULT_DEVICE
;
290 tpm_pt
->tpm_fd
= qemu_open(tpm_pt
->tpm_dev
, O_RDWR
);
291 if (tpm_pt
->tpm_fd
< 0) {
292 error_report("Cannot access TPM device using '%s': %s",
293 tpm_pt
->tpm_dev
, strerror(errno
));
294 goto err_free_parameters
;
297 if (tpm_util_test_tpmdev(tpm_pt
->tpm_fd
, &tpm_pt
->tpm_version
)) {
298 error_report("'%s' is not a TPM device.",
300 goto err_close_tpmdev
;
306 qemu_close(tpm_pt
->tpm_fd
);
310 qapi_free_TPMPassthroughOptions(tpm_pt
->options
);
311 tpm_pt
->options
= NULL
;
312 tpm_pt
->tpm_dev
= NULL
;
317 static TPMBackend
*tpm_passthrough_create(QemuOpts
*opts
, const char *id
)
319 Object
*obj
= object_new(TYPE_TPM_PASSTHROUGH
);
320 TPMBackend
*tb
= TPM_BACKEND(obj
);
321 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
323 tb
->id
= g_strdup(id
);
325 if (tpm_passthrough_handle_device_opts(opts
, tb
)) {
329 tpm_pt
->cancel_fd
= tpm_passthrough_open_sysfs_cancel(tpm_pt
);
330 if (tpm_pt
->cancel_fd
< 0) {
342 static TpmTypeOptions
*tpm_passthrough_get_tpm_options(TPMBackend
*tb
)
344 TpmTypeOptions
*options
= g_new0(TpmTypeOptions
, 1);
346 options
->type
= TPM_TYPE_OPTIONS_KIND_PASSTHROUGH
;
347 options
->u
.passthrough
.data
= QAPI_CLONE(TPMPassthroughOptions
,
348 TPM_PASSTHROUGH(tb
)->options
);
353 static const QemuOptDesc tpm_passthrough_cmdline_opts
[] = {
354 TPM_STANDARD_CMDLINE_OPTS
,
356 .name
= "cancel-path",
357 .type
= QEMU_OPT_STRING
,
358 .help
= "Sysfs file entry for canceling TPM commands",
362 .type
= QEMU_OPT_STRING
,
363 .help
= "Path to TPM device on the host",
365 { /* end of list */ },
368 static void tpm_passthrough_inst_init(Object
*obj
)
370 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(obj
);
372 tpm_pt
->options
= g_new0(TPMPassthroughOptions
, 1);
374 tpm_pt
->cancel_fd
= -1;
377 static void tpm_passthrough_inst_finalize(Object
*obj
)
379 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(obj
);
381 tpm_passthrough_cancel_cmd(TPM_BACKEND(obj
));
383 qemu_close(tpm_pt
->tpm_fd
);
384 qemu_close(tpm_pt
->cancel_fd
);
385 qapi_free_TPMPassthroughOptions(tpm_pt
->options
);
388 static void tpm_passthrough_class_init(ObjectClass
*klass
, void *data
)
390 TPMBackendClass
*tbc
= TPM_BACKEND_CLASS(klass
);
392 tbc
->type
= TPM_TYPE_PASSTHROUGH
;
393 tbc
->opts
= tpm_passthrough_cmdline_opts
;
394 tbc
->desc
= "Passthrough TPM backend driver";
395 tbc
->create
= tpm_passthrough_create
;
396 tbc
->reset
= tpm_passthrough_reset
;
397 tbc
->cancel_cmd
= tpm_passthrough_cancel_cmd
;
398 tbc
->get_tpm_established_flag
= tpm_passthrough_get_tpm_established_flag
;
399 tbc
->reset_tpm_established_flag
=
400 tpm_passthrough_reset_tpm_established_flag
;
401 tbc
->get_tpm_version
= tpm_passthrough_get_tpm_version
;
402 tbc
->get_tpm_options
= tpm_passthrough_get_tpm_options
;
403 tbc
->handle_request
= tpm_passthrough_handle_request
;
406 static const TypeInfo tpm_passthrough_info
= {
407 .name
= TYPE_TPM_PASSTHROUGH
,
408 .parent
= TYPE_TPM_BACKEND
,
409 .instance_size
= sizeof(TPMPassthruState
),
410 .class_init
= tpm_passthrough_class_init
,
411 .instance_init
= tpm_passthrough_inst_init
,
412 .instance_finalize
= tpm_passthrough_inst_finalize
,
415 static void tpm_passthrough_register(void)
417 type_register_static(&tpm_passthrough_info
);
420 type_init(tpm_passthrough_register
)