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 "qapi/clone-visitor.h"
37 #define DPRINTF(fmt, ...) do { \
39 fprintf(stderr, fmt, ## __VA_ARGS__); \
43 #define TYPE_TPM_PASSTHROUGH "tpm-passthrough"
44 #define TPM_PASSTHROUGH(obj) \
45 OBJECT_CHECK(TPMPassthruState, (obj), TYPE_TPM_PASSTHROUGH)
48 struct TPMPassthruState
{
51 TPMPassthroughOptions
*options
;
58 TPMVersion tpm_version
;
59 size_t tpm_buffersize
;
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
) {
84 static void 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
,
87 bool *selftest_done
, Error
**errp
)
92 /* FIXME: protect shared variables or use other sync mechanism */
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
, in
, in_len
);
101 if (!tpm_pt
->tpm_op_canceled
|| errno
!= ECANCELED
) {
102 error_setg_errno(errp
, errno
, "tpm_passthrough: error while "
103 "transmitting data to TPM");
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_setg_errno(errp
, errno
, "tpm_passthrough: error while "
114 "reading data from TPM");
116 } else if (ret
< sizeof(struct tpm_resp_hdr
) ||
117 tpm_cmd_get_size(out
) != ret
) {
119 error_setg_errno(errp
, errno
, "tpm_passthrough: received invalid "
120 "response packet from TPM");
123 if (is_selftest
&& (ret
>= sizeof(struct tpm_resp_hdr
))) {
124 *selftest_done
= tpm_cmd_get_errcode(out
) == 0;
129 tpm_util_write_fatal_error_response(out
, out_len
);
132 tpm_pt
->tpm_executing
= false;
135 static void tpm_passthrough_handle_request(TPMBackend
*tb
, TPMBackendCmd
*cmd
,
138 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
140 DPRINTF("tpm_passthrough: processing command %p\n", cmd
);
142 tpm_passthrough_unix_tx_bufs(tpm_pt
, cmd
->in
, cmd
->in_len
,
143 cmd
->out
, cmd
->out_len
, &cmd
->selftest_done
,
147 static void tpm_passthrough_reset(TPMBackend
*tb
)
149 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
151 tpm_passthrough_cancel_cmd(tb
);
154 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend
*tb
)
159 static int tpm_passthrough_reset_tpm_established_flag(TPMBackend
*tb
,
162 /* only a TPM 2.0 will support this */
166 static void tpm_passthrough_cancel_cmd(TPMBackend
*tb
)
168 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
172 * As of Linux 3.7 the tpm_tis driver does not properly cancel
173 * commands on all TPM manufacturers' TPMs.
174 * Only cancel if we're busy so we don't cancel someone else's
175 * command, e.g., a command executed on the host.
177 if (tpm_pt
->tpm_executing
) {
178 if (tpm_pt
->cancel_fd
>= 0) {
179 tpm_pt
->tpm_op_canceled
= true;
180 n
= write(tpm_pt
->cancel_fd
, "-", 1);
182 error_report("Canceling TPM command failed: %s",
186 error_report("Cannot cancel TPM command due to missing "
187 "TPM sysfs cancel entry");
192 static TPMVersion
tpm_passthrough_get_tpm_version(TPMBackend
*tb
)
194 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
196 return tpm_pt
->tpm_version
;
199 static size_t tpm_passthrough_get_buffer_size(TPMBackend
*tb
)
201 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
204 ret
= tpm_util_get_buffer_size(tpm_pt
->tpm_fd
, tpm_pt
->tpm_version
,
205 &tpm_pt
->tpm_buffersize
);
207 tpm_pt
->tpm_buffersize
= 4096;
209 return tpm_pt
->tpm_buffersize
;
213 * Unless path or file descriptor set has been provided by user,
214 * determine the sysfs cancel file following kernel documentation
215 * in Documentation/ABI/stable/sysfs-class-tpm.
216 * From /dev/tpm0 create /sys/class/tpm/tpm0/device/cancel
217 * before 4.0: /sys/class/misc/tpm0/device/cancel
219 static int tpm_passthrough_open_sysfs_cancel(TPMPassthruState
*tpm_pt
)
225 if (tpm_pt
->options
->cancel_path
) {
226 fd
= qemu_open(tpm_pt
->options
->cancel_path
, O_WRONLY
);
228 error_report("tpm_passthrough: Could not open TPM cancel path: %s",
234 dev
= strrchr(tpm_pt
->tpm_dev
, '/');
236 error_report("tpm_passthrough: Bad TPM device path %s",
242 if (snprintf(path
, sizeof(path
), "/sys/class/tpm/%s/device/cancel",
243 dev
) < sizeof(path
)) {
244 fd
= qemu_open(path
, O_WRONLY
);
246 if (snprintf(path
, sizeof(path
), "/sys/class/misc/%s/device/cancel",
247 dev
) < sizeof(path
)) {
248 fd
= qemu_open(path
, O_WRONLY
);
254 error_report("tpm_passthrough: Could not guess TPM cancel path");
256 tpm_pt
->options
->cancel_path
= g_strdup(path
);
263 tpm_passthrough_handle_device_opts(TPMPassthruState
*tpm_pt
, QemuOpts
*opts
)
267 value
= qemu_opt_get(opts
, "cancel-path");
269 tpm_pt
->options
->cancel_path
= g_strdup(value
);
270 tpm_pt
->options
->has_cancel_path
= true;
273 value
= qemu_opt_get(opts
, "path");
275 tpm_pt
->options
->has_path
= true;
276 tpm_pt
->options
->path
= g_strdup(value
);
279 tpm_pt
->tpm_dev
= value
? value
: TPM_PASSTHROUGH_DEFAULT_DEVICE
;
280 tpm_pt
->tpm_fd
= qemu_open(tpm_pt
->tpm_dev
, O_RDWR
);
281 if (tpm_pt
->tpm_fd
< 0) {
282 error_report("Cannot access TPM device using '%s': %s",
283 tpm_pt
->tpm_dev
, strerror(errno
));
287 if (tpm_util_test_tpmdev(tpm_pt
->tpm_fd
, &tpm_pt
->tpm_version
)) {
288 error_report("'%s' is not a TPM device.",
293 tpm_pt
->cancel_fd
= tpm_passthrough_open_sysfs_cancel(tpm_pt
);
294 if (tpm_pt
->cancel_fd
< 0) {
301 static TPMBackend
*tpm_passthrough_create(QemuOpts
*opts
)
303 Object
*obj
= object_new(TYPE_TPM_PASSTHROUGH
);
305 if (tpm_passthrough_handle_device_opts(TPM_PASSTHROUGH(obj
), opts
)) {
310 return TPM_BACKEND(obj
);
313 static int tpm_passthrough_startup_tpm(TPMBackend
*tb
, size_t buffersize
)
315 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
317 if (buffersize
&& buffersize
< tpm_pt
->tpm_buffersize
) {
318 error_report("Requested buffer size of %zu is smaller than host TPM's "
319 "fixed buffer size of %zu",
320 buffersize
, tpm_pt
->tpm_buffersize
);
327 static TpmTypeOptions
*tpm_passthrough_get_tpm_options(TPMBackend
*tb
)
329 TpmTypeOptions
*options
= g_new0(TpmTypeOptions
, 1);
331 options
->type
= TPM_TYPE_OPTIONS_KIND_PASSTHROUGH
;
332 options
->u
.passthrough
.data
= QAPI_CLONE(TPMPassthroughOptions
,
333 TPM_PASSTHROUGH(tb
)->options
);
338 static const QemuOptDesc tpm_passthrough_cmdline_opts
[] = {
339 TPM_STANDARD_CMDLINE_OPTS
,
341 .name
= "cancel-path",
342 .type
= QEMU_OPT_STRING
,
343 .help
= "Sysfs file entry for canceling TPM commands",
347 .type
= QEMU_OPT_STRING
,
348 .help
= "Path to TPM device on the host",
350 { /* end of list */ },
353 static void tpm_passthrough_inst_init(Object
*obj
)
355 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(obj
);
357 tpm_pt
->options
= g_new0(TPMPassthroughOptions
, 1);
359 tpm_pt
->cancel_fd
= -1;
362 static void tpm_passthrough_inst_finalize(Object
*obj
)
364 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(obj
);
366 tpm_passthrough_cancel_cmd(TPM_BACKEND(obj
));
368 if (tpm_pt
->tpm_fd
>= 0) {
369 qemu_close(tpm_pt
->tpm_fd
);
371 if (tpm_pt
->cancel_fd
>= 0) {
372 qemu_close(tpm_pt
->cancel_fd
);
374 qapi_free_TPMPassthroughOptions(tpm_pt
->options
);
377 static void tpm_passthrough_class_init(ObjectClass
*klass
, void *data
)
379 TPMBackendClass
*tbc
= TPM_BACKEND_CLASS(klass
);
381 tbc
->type
= TPM_TYPE_PASSTHROUGH
;
382 tbc
->opts
= tpm_passthrough_cmdline_opts
;
383 tbc
->desc
= "Passthrough TPM backend driver";
384 tbc
->create
= tpm_passthrough_create
;
385 tbc
->startup_tpm
= tpm_passthrough_startup_tpm
;
386 tbc
->reset
= tpm_passthrough_reset
;
387 tbc
->cancel_cmd
= tpm_passthrough_cancel_cmd
;
388 tbc
->get_tpm_established_flag
= tpm_passthrough_get_tpm_established_flag
;
389 tbc
->reset_tpm_established_flag
=
390 tpm_passthrough_reset_tpm_established_flag
;
391 tbc
->get_tpm_version
= tpm_passthrough_get_tpm_version
;
392 tbc
->get_buffer_size
= tpm_passthrough_get_buffer_size
;
393 tbc
->get_tpm_options
= tpm_passthrough_get_tpm_options
;
394 tbc
->handle_request
= tpm_passthrough_handle_request
;
397 static const TypeInfo tpm_passthrough_info
= {
398 .name
= TYPE_TPM_PASSTHROUGH
,
399 .parent
= TYPE_TPM_BACKEND
,
400 .instance_size
= sizeof(TPMPassthruState
),
401 .class_init
= tpm_passthrough_class_init
,
402 .instance_init
= tpm_passthrough_inst_init
,
403 .instance_finalize
= tpm_passthrough_inst_finalize
,
406 static void tpm_passthrough_register(void)
408 type_register_static(&tpm_passthrough_info
);
411 type_init(tpm_passthrough_register
)