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 "backends/tpm.h"
33 #include "hw/i386/pc.h"
35 #include "tpm_backend.h"
37 /* #define DEBUG_TPM */
40 #define DPRINTF(fmt, ...) \
41 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
43 #define DPRINTF(fmt, ...) \
47 #define TYPE_TPM_PASSTHROUGH "tpm-passthrough"
48 #define TPM_PASSTHROUGH(obj) \
49 OBJECT_CHECK(TPMPassthruState, (obj), TYPE_TPM_PASSTHROUGH)
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
);
99 static int tpm_passthrough_unix_tx_bufs(TPMPassthruState
*tpm_pt
,
100 const uint8_t *in
, uint32_t in_len
,
101 uint8_t *out
, uint32_t out_len
)
105 tpm_pt
->tpm_op_canceled
= false;
106 tpm_pt
->tpm_executing
= true;
108 ret
= tpm_passthrough_unix_write(tpm_pt
->tpm_fd
, in
, in_len
);
110 if (!tpm_pt
->tpm_op_canceled
||
111 (tpm_pt
->tpm_op_canceled
&& errno
!= ECANCELED
)) {
112 error_report("tpm_passthrough: error while transmitting data "
114 strerror(errno
), errno
);
119 tpm_pt
->tpm_executing
= false;
121 ret
= tpm_passthrough_unix_read(tpm_pt
->tpm_fd
, out
, out_len
);
123 if (!tpm_pt
->tpm_op_canceled
||
124 (tpm_pt
->tpm_op_canceled
&& errno
!= ECANCELED
)) {
125 error_report("tpm_passthrough: error while reading data from "
127 strerror(errno
), errno
);
129 } else if (ret
< sizeof(struct tpm_resp_hdr
) ||
130 tpm_passthrough_get_size_from_buffer(out
) != ret
) {
132 error_report("tpm_passthrough: received invalid response "
133 "packet from TPM\n");
138 tpm_write_fatal_error_response(out
, out_len
);
141 tpm_pt
->tpm_executing
= false;
146 static int tpm_passthrough_unix_transfer(TPMPassthruState
*tpm_pt
,
147 const TPMLocality
*locty_data
)
149 return tpm_passthrough_unix_tx_bufs(tpm_pt
,
150 locty_data
->w_buffer
.buffer
,
151 locty_data
->w_offset
,
152 locty_data
->r_buffer
.buffer
,
153 locty_data
->r_buffer
.size
);
156 static void tpm_passthrough_worker_thread(gpointer data
,
159 TPMPassthruThreadParams
*thr_parms
= user_data
;
160 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(thr_parms
->tb
);
161 TPMBackendCmd cmd
= (TPMBackendCmd
)data
;
163 DPRINTF("tpm_passthrough: processing command type %d\n", cmd
);
166 case TPM_BACKEND_CMD_PROCESS_CMD
:
167 tpm_passthrough_unix_transfer(tpm_pt
,
168 thr_parms
->tpm_state
->locty_data
);
170 thr_parms
->recv_data_callback(thr_parms
->tpm_state
,
171 thr_parms
->tpm_state
->locty_number
);
173 case TPM_BACKEND_CMD_INIT
:
174 case TPM_BACKEND_CMD_END
:
175 case TPM_BACKEND_CMD_TPM_RESET
:
182 * Start the TPM (thread). If it had been started before, then terminate
183 * and start it again.
185 static int tpm_passthrough_startup_tpm(TPMBackend
*tb
)
187 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
189 /* terminate a running TPM */
190 tpm_backend_thread_end(&tpm_pt
->tbt
);
192 tpm_backend_thread_create(&tpm_pt
->tbt
,
193 tpm_passthrough_worker_thread
,
194 &tpm_pt
->tpm_thread_params
);
199 static void tpm_passthrough_reset(TPMBackend
*tb
)
201 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
203 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
205 tpm_passthrough_cancel_cmd(tb
);
207 tpm_backend_thread_end(&tpm_pt
->tbt
);
209 tpm_pt
->had_startup_error
= false;
212 static int tpm_passthrough_init(TPMBackend
*tb
, TPMState
*s
,
213 TPMRecvDataCB
*recv_data_cb
)
215 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
217 tpm_pt
->tpm_thread_params
.tpm_state
= s
;
218 tpm_pt
->tpm_thread_params
.recv_data_callback
= recv_data_cb
;
219 tpm_pt
->tpm_thread_params
.tb
= tb
;
224 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend
*tb
)
229 static bool tpm_passthrough_get_startup_error(TPMBackend
*tb
)
231 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
233 return tpm_pt
->had_startup_error
;
236 static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer
*sb
)
238 size_t wanted_size
= 4096; /* Linux tpm.c buffer size */
240 if (sb
->size
!= wanted_size
) {
241 sb
->buffer
= g_realloc(sb
->buffer
, wanted_size
);
242 sb
->size
= wanted_size
;
247 static void tpm_passthrough_deliver_request(TPMBackend
*tb
)
249 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
251 tpm_backend_thread_deliver_request(&tpm_pt
->tbt
);
254 static void tpm_passthrough_cancel_cmd(TPMBackend
*tb
)
256 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
260 * As of Linux 3.7 the tpm_tis driver does not properly cancel
261 * commands on all TPM manufacturers' TPMs.
262 * Only cancel if we're busy so we don't cancel someone else's
263 * command, e.g., a command executed on the host.
265 if (tpm_pt
->tpm_executing
) {
266 if (tpm_pt
->cancel_fd
>= 0) {
267 n
= write(tpm_pt
->cancel_fd
, "-", 1);
269 error_report("Canceling TPM command failed: %s\n",
272 tpm_pt
->tpm_op_canceled
= true;
275 error_report("Cannot cancel TPM command due to missing "
276 "TPM sysfs cancel entry");
281 static const char *tpm_passthrough_create_desc(void)
283 return "Passthrough TPM backend driver";
287 * A basic test of a TPM device. We expect a well formatted response header
288 * (error response is fine) within one second.
290 static int tpm_passthrough_test_tpmdev(int fd
)
292 struct tpm_req_hdr req
= {
293 .tag
= cpu_to_be16(TPM_TAG_RQU_COMMAND
),
294 .len
= cpu_to_be32(sizeof(req
)),
295 .ordinal
= cpu_to_be32(TPM_ORD_GetTicks
),
297 struct tpm_resp_hdr
*resp
;
300 struct timeval tv
= {
304 unsigned char buf
[1024];
306 n
= write(fd
, &req
, sizeof(req
));
310 if (n
!= sizeof(req
)) {
315 FD_SET(fd
, &readfds
);
317 /* wait for a second */
318 n
= select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
323 n
= read(fd
, &buf
, sizeof(buf
));
324 if (n
< sizeof(struct tpm_resp_hdr
)) {
328 resp
= (struct tpm_resp_hdr
*)buf
;
329 /* check the header */
330 if (be16_to_cpu(resp
->tag
) != TPM_TAG_RSP_COMMAND
||
331 be32_to_cpu(resp
->len
) != n
) {
339 * Check whether the given base path, e.g., /sys/class/misc/tpm0/device,
340 * is the sysfs directory of a TPM. A TPM sysfs directory should be uniquely
341 * recognizable by the file entries 'pcrs' and 'cancel'.
342 * Upon success 'true' is returned and the basebath buffer has '/cancel'
345 static bool tpm_passthrough_check_sysfs_cancel(char *basepath
, size_t bufsz
)
350 snprintf(path
, sizeof(path
), "%s/pcrs", basepath
);
351 if (stat(path
, &statbuf
) == -1 || !S_ISREG(statbuf
.st_mode
)) {
355 snprintf(path
, sizeof(path
), "%s/cancel", basepath
);
356 if (stat(path
, &statbuf
) == -1 || !S_ISREG(statbuf
.st_mode
)) {
360 strncpy(basepath
, path
, bufsz
);
366 * Unless path or file descriptor set has been provided by user,
367 * determine the sysfs cancel file following kernel documentation
368 * in Documentation/ABI/stable/sysfs-class-tpm.
370 static int tpm_passthrough_open_sysfs_cancel(TPMBackend
*tb
)
376 struct dirent entry
, *result
;
379 if (tb
->cancel_path
) {
380 fd
= qemu_open(tb
->cancel_path
, O_WRONLY
);
382 error_report("Could not open TPM cancel path : %s",
388 snprintf(path
, sizeof(path
), "/sys/class/misc");
389 pnp_dir
= opendir(path
);
390 if (pnp_dir
!= NULL
) {
391 while (readdir_r(pnp_dir
, &entry
, &result
) == 0 &&
394 * only allow /sys/class/misc/tpm%u type of paths
396 if (sscanf(entry
.d_name
, "tpm%u%n", &idx
, &len
) < 1 ||
397 len
<= strlen("tpm") ||
398 len
!= strlen(entry
.d_name
)) {
402 snprintf(path
, sizeof(path
), "/sys/class/misc/%s/device",
404 if (!tpm_passthrough_check_sysfs_cancel(path
, sizeof(path
))) {
408 fd
= qemu_open(path
, O_WRONLY
);
415 tb
->cancel_path
= g_strdup(path
);
421 static int tpm_passthrough_handle_device_opts(QemuOpts
*opts
, TPMBackend
*tb
)
423 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
426 value
= qemu_opt_get(opts
, "cancel-path");
428 tb
->cancel_path
= g_strdup(value
);
431 value
= qemu_opt_get(opts
, "path");
433 value
= TPM_PASSTHROUGH_DEFAULT_DEVICE
;
436 tpm_pt
->tpm_dev
= g_strdup(value
);
438 tb
->path
= g_strdup(tpm_pt
->tpm_dev
);
440 tpm_pt
->tpm_fd
= qemu_open(tpm_pt
->tpm_dev
, O_RDWR
);
441 if (tpm_pt
->tpm_fd
< 0) {
442 error_report("Cannot access TPM device using '%s': %s\n",
443 tpm_pt
->tpm_dev
, strerror(errno
));
444 goto err_free_parameters
;
447 if (tpm_passthrough_test_tpmdev(tpm_pt
->tpm_fd
)) {
448 error_report("'%s' is not a TPM device.\n",
450 goto err_close_tpmdev
;
456 qemu_close(tpm_pt
->tpm_fd
);
463 g_free(tpm_pt
->tpm_dev
);
464 tpm_pt
->tpm_dev
= NULL
;
469 static TPMBackend
*tpm_passthrough_create(QemuOpts
*opts
, const char *id
)
471 Object
*obj
= object_new(TYPE_TPM_PASSTHROUGH
);
472 TPMBackend
*tb
= TPM_BACKEND(obj
);
473 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
475 tb
->id
= g_strdup(id
);
476 /* let frontend set the fe_model to proper value */
479 tb
->ops
= &tpm_passthrough_driver
;
481 if (tpm_passthrough_handle_device_opts(opts
, tb
)) {
485 tpm_pt
->cancel_fd
= tpm_passthrough_open_sysfs_cancel(tb
);
486 if (tpm_pt
->cancel_fd
< 0) {
498 static void tpm_passthrough_destroy(TPMBackend
*tb
)
500 TPMPassthruState
*tpm_pt
= TPM_PASSTHROUGH(tb
);
502 tpm_passthrough_cancel_cmd(tb
);
504 tpm_backend_thread_end(&tpm_pt
->tbt
);
506 qemu_close(tpm_pt
->tpm_fd
);
507 qemu_close(tpm_pt
->cancel_fd
);
511 g_free(tb
->cancel_path
);
512 g_free(tpm_pt
->tpm_dev
);
515 const TPMDriverOps tpm_passthrough_driver
= {
516 .type
= TPM_TYPE_PASSTHROUGH
,
517 .desc
= tpm_passthrough_create_desc
,
518 .create
= tpm_passthrough_create
,
519 .destroy
= tpm_passthrough_destroy
,
520 .init
= tpm_passthrough_init
,
521 .startup_tpm
= tpm_passthrough_startup_tpm
,
522 .realloc_buffer
= tpm_passthrough_realloc_buffer
,
523 .reset
= tpm_passthrough_reset
,
524 .had_startup_error
= tpm_passthrough_get_startup_error
,
525 .deliver_request
= tpm_passthrough_deliver_request
,
526 .cancel_cmd
= tpm_passthrough_cancel_cmd
,
527 .get_tpm_established_flag
= tpm_passthrough_get_tpm_established_flag
,
530 static void tpm_passthrough_inst_init(Object
*obj
)
534 static void tpm_passthrough_inst_finalize(Object
*obj
)
538 static void tpm_passthrough_class_init(ObjectClass
*klass
, void *data
)
540 TPMBackendClass
*tbc
= TPM_BACKEND_CLASS(klass
);
542 tbc
->ops
= &tpm_passthrough_driver
;
545 static const TypeInfo tpm_passthrough_info
= {
546 .name
= TYPE_TPM_PASSTHROUGH
,
547 .parent
= TYPE_TPM_BACKEND
,
548 .instance_size
= sizeof(TPMPassthruState
),
549 .class_init
= tpm_passthrough_class_init
,
550 .instance_init
= tpm_passthrough_inst_init
,
551 .instance_finalize
= tpm_passthrough_inst_finalize
,
554 static void tpm_passthrough_register(void)
556 type_register_static(&tpm_passthrough_info
);
557 tpm_register_driver(&tpm_passthrough_driver
);
560 type_init(tpm_passthrough_register
)