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"
38 #define DPRINTF(fmt, ...) do { \
40 fprintf(stderr, fmt, ## __VA_ARGS__); \
44 #define TYPE_TPM_PASSTHROUGH "tpm-passthrough"
45 #define TPM_PASSTHROUGH(obj) \
46 OBJECT_CHECK(TPMPassthruState, (obj), TYPE_TPM_PASSTHROUGH)
49 struct TPMPassthruState
{
52 TPMPassthroughOptions
*options
;
59 TPMVersion tpm_version
;
62 typedef struct TPMPassthruState TPMPassthruState
;
64 #define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
68 static void tpm_passthrough_cancel_cmd(TPMBackend
*tb
);
70 static int tpm_passthrough_unix_read(int fd
, uint8_t *buf
, uint32_t len
)
74 ret
= read(fd
, buf
, len
);
76 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
83 static int tpm_passthrough_unix_tx_bufs(TPMPassthruState
*tpm_pt
,
84 const uint8_t *in
, uint32_t in_len
,
85 uint8_t *out
, uint32_t out_len
,
90 const struct tpm_resp_hdr
*hdr
;
92 tpm_pt
->tpm_op_canceled
= false;
93 tpm_pt
->tpm_executing
= true;
94 *selftest_done
= false;
96 is_selftest
= tpm_util_is_selftest(in
, in_len
);
98 ret
= qemu_write_full(tpm_pt
->tpm_fd
, in
, in_len
);
100 if (!tpm_pt
->tpm_op_canceled
|| errno
!= ECANCELED
) {
101 error_report("tpm_passthrough: error while transmitting data "
103 strerror(errno
), errno
);
108 tpm_pt
->tpm_executing
= false;
110 ret
= tpm_passthrough_unix_read(tpm_pt
->tpm_fd
, out
, out_len
);
112 if (!tpm_pt
->tpm_op_canceled
|| errno
!= ECANCELED
) {
113 error_report("tpm_passthrough: error while reading data from "
115 strerror(errno
), errno
);
117 } else if (ret
< sizeof(struct tpm_resp_hdr
) ||
118 be32_to_cpu(((struct tpm_resp_hdr
*)out
)->len
) != ret
) {
120 error_report("tpm_passthrough: received invalid response "
124 if (is_selftest
&& (ret
>= sizeof(struct tpm_resp_hdr
))) {
125 hdr
= (struct tpm_resp_hdr
*)out
;
126 *selftest_done
= (be32_to_cpu(hdr
->errcode
) == 0);
131 tpm_util_write_fatal_error_response(out
, out_len
);
134 tpm_pt
->tpm_executing
= false;
139 static void tpm_passthrough_handle_request(TPMBackend
*tb
, TPMBackendCmd
*cmd
)
141 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
143 DPRINTF("tpm_passthrough: processing command %p\n", cmd
);
145 tpm_passthrough_unix_tx_bufs(tpm_pt
, cmd
->in
, cmd
->in_len
,
146 cmd
->out
, cmd
->out_len
, &cmd
->selftest_done
);
149 static void tpm_passthrough_reset(TPMBackend
*tb
)
151 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
153 tpm_passthrough_cancel_cmd(tb
);
156 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend
*tb
)
161 static int tpm_passthrough_reset_tpm_established_flag(TPMBackend
*tb
,
164 /* only a TPM 2.0 will support this */
168 static void tpm_passthrough_cancel_cmd(TPMBackend
*tb
)
170 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
174 * As of Linux 3.7 the tpm_tis driver does not properly cancel
175 * commands on all TPM manufacturers' TPMs.
176 * Only cancel if we're busy so we don't cancel someone else's
177 * command, e.g., a command executed on the host.
179 if (tpm_pt
->tpm_executing
) {
180 if (tpm_pt
->cancel_fd
>= 0) {
181 n
= write(tpm_pt
->cancel_fd
, "-", 1);
183 error_report("Canceling TPM command failed: %s",
186 tpm_pt
->tpm_op_canceled
= true;
189 error_report("Cannot cancel TPM command due to missing "
190 "TPM sysfs cancel entry");
195 static TPMVersion
tpm_passthrough_get_tpm_version(TPMBackend
*tb
)
197 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
199 return tpm_pt
->tpm_version
;
203 * Unless path or file descriptor set has been provided by user,
204 * determine the sysfs cancel file following kernel documentation
205 * in Documentation/ABI/stable/sysfs-class-tpm.
206 * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
208 static int tpm_passthrough_open_sysfs_cancel(TPMPassthruState
*tpm_pt
)
214 if (tpm_pt
->options
->cancel_path
) {
215 fd
= qemu_open(tpm_pt
->options
->cancel_path
, O_WRONLY
);
217 error_report("Could not open TPM cancel path : %s",
223 dev
= strrchr(tpm_pt
->tpm_dev
, '/');
226 if (snprintf(path
, sizeof(path
), "/sys/class/misc/%s/device/cancel",
227 dev
) < sizeof(path
)) {
228 fd
= qemu_open(path
, O_WRONLY
);
230 tpm_pt
->options
->cancel_path
= g_strdup(path
);
232 error_report("tpm_passthrough: Could not open TPM cancel "
233 "path %s : %s", path
, strerror(errno
));
237 error_report("tpm_passthrough: Bad TPM device path %s",
244 static int tpm_passthrough_handle_device_opts(QemuOpts
*opts
, TPMBackend
*tb
)
246 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
249 value
= qemu_opt_get(opts
, "cancel-path");
251 tpm_pt
->options
->cancel_path
= g_strdup(value
);
252 tpm_pt
->options
->has_cancel_path
= true;
255 value
= qemu_opt_get(opts
, "path");
257 tpm_pt
->options
->has_path
= true;
258 tpm_pt
->options
->path
= g_strdup(value
);
261 tpm_pt
->tpm_dev
= value
? value
: TPM_PASSTHROUGH_DEFAULT_DEVICE
;
262 tpm_pt
->tpm_fd
= qemu_open(tpm_pt
->tpm_dev
, O_RDWR
);
263 if (tpm_pt
->tpm_fd
< 0) {
264 error_report("Cannot access TPM device using '%s': %s",
265 tpm_pt
->tpm_dev
, strerror(errno
));
266 goto err_free_parameters
;
269 if (tpm_util_test_tpmdev(tpm_pt
->tpm_fd
, &tpm_pt
->tpm_version
)) {
270 error_report("'%s' is not a TPM device.",
272 goto err_close_tpmdev
;
278 qemu_close(tpm_pt
->tpm_fd
);
282 qapi_free_TPMPassthroughOptions(tpm_pt
->options
);
283 tpm_pt
->options
= NULL
;
284 tpm_pt
->tpm_dev
= NULL
;
289 static TPMBackend
*tpm_passthrough_create(QemuOpts
*opts
, const char *id
)
291 Object
*obj
= object_new(TYPE_TPM_PASSTHROUGH
);
292 TPMBackend
*tb
= TPM_BACKEND(obj
);
293 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
295 tb
->id
= g_strdup(id
);
297 if (tpm_passthrough_handle_device_opts(opts
, tb
)) {
301 tpm_pt
->cancel_fd
= tpm_passthrough_open_sysfs_cancel(tpm_pt
);
302 if (tpm_pt
->cancel_fd
< 0) {
314 static TpmTypeOptions
*tpm_passthrough_get_tpm_options(TPMBackend
*tb
)
316 TpmTypeOptions
*options
= g_new0(TpmTypeOptions
, 1);
318 options
->type
= TPM_TYPE_OPTIONS_KIND_PASSTHROUGH
;
319 options
->u
.passthrough
.data
= QAPI_CLONE(TPMPassthroughOptions
,
320 TPM_PASSTHROUGH(tb
)->options
);
325 static const QemuOptDesc tpm_passthrough_cmdline_opts
[] = {
326 TPM_STANDARD_CMDLINE_OPTS
,
328 .name
= "cancel-path",
329 .type
= QEMU_OPT_STRING
,
330 .help
= "Sysfs file entry for canceling TPM commands",
334 .type
= QEMU_OPT_STRING
,
335 .help
= "Path to TPM device on the host",
337 { /* end of list */ },
340 static void tpm_passthrough_inst_init(Object
*obj
)
342 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(obj
);
344 tpm_pt
->options
= g_new0(TPMPassthroughOptions
, 1);
346 tpm_pt
->cancel_fd
= -1;
349 static void tpm_passthrough_inst_finalize(Object
*obj
)
351 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(obj
);
353 tpm_passthrough_cancel_cmd(TPM_BACKEND(obj
));
355 qemu_close(tpm_pt
->tpm_fd
);
356 qemu_close(tpm_pt
->cancel_fd
);
357 qapi_free_TPMPassthroughOptions(tpm_pt
->options
);
360 static void tpm_passthrough_class_init(ObjectClass
*klass
, void *data
)
362 TPMBackendClass
*tbc
= TPM_BACKEND_CLASS(klass
);
364 tbc
->type
= TPM_TYPE_PASSTHROUGH
;
365 tbc
->opts
= tpm_passthrough_cmdline_opts
;
366 tbc
->desc
= "Passthrough TPM backend driver";
367 tbc
->create
= tpm_passthrough_create
;
368 tbc
->reset
= tpm_passthrough_reset
;
369 tbc
->cancel_cmd
= tpm_passthrough_cancel_cmd
;
370 tbc
->get_tpm_established_flag
= tpm_passthrough_get_tpm_established_flag
;
371 tbc
->reset_tpm_established_flag
=
372 tpm_passthrough_reset_tpm_established_flag
;
373 tbc
->get_tpm_version
= tpm_passthrough_get_tpm_version
;
374 tbc
->get_tpm_options
= tpm_passthrough_get_tpm_options
;
375 tbc
->handle_request
= tpm_passthrough_handle_request
;
378 static const TypeInfo tpm_passthrough_info
= {
379 .name
= TYPE_TPM_PASSTHROUGH
,
380 .parent
= TYPE_TPM_BACKEND
,
381 .instance_size
= sizeof(TPMPassthruState
),
382 .class_init
= tpm_passthrough_class_init
,
383 .instance_init
= tpm_passthrough_inst_init
,
384 .instance_finalize
= tpm_passthrough_inst_finalize
,
387 static void tpm_passthrough_register(void)
389 type_register_static(&tpm_passthrough_info
);
392 type_init(tpm_passthrough_register
)