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"
34 #include "tpm_backend.h"
36 /* #define DEBUG_TPM */
39 #define DPRINTF(fmt, ...) \
40 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
42 #define DPRINTF(fmt, ...) \
48 typedef struct TPMPassthruThreadParams
{
51 TPMRecvDataCB
*recv_data_callback
;
53 } TPMPassthruThreadParams
;
55 struct TPMPassthruState
{
58 TPMPassthruThreadParams tpm_thread_params
;
65 bool had_startup_error
;
68 #define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
72 static void tpm_passthrough_cancel_cmd(TPMBackend
*tb
);
74 static int tpm_passthrough_unix_write(int fd
, const uint8_t *buf
, uint32_t len
)
76 return send_all(fd
, buf
, len
);
79 static int tpm_passthrough_unix_read(int fd
, uint8_t *buf
, uint32_t len
)
81 return recv_all(fd
, buf
, len
, true);
84 static uint32_t tpm_passthrough_get_size_from_buffer(const uint8_t *buf
)
86 struct tpm_resp_hdr
*resp
= (struct tpm_resp_hdr
*)buf
;
88 return be32_to_cpu(resp
->len
);
91 static int tpm_passthrough_unix_tx_bufs(TPMPassthruState
*tpm_pt
,
92 const uint8_t *in
, uint32_t in_len
,
93 uint8_t *out
, uint32_t out_len
)
97 tpm_pt
->tpm_op_canceled
= false;
98 tpm_pt
->tpm_executing
= true;
100 ret
= tpm_passthrough_unix_write(tpm_pt
->tpm_fd
, in
, in_len
);
102 if (!tpm_pt
->tpm_op_canceled
||
103 (tpm_pt
->tpm_op_canceled
&& errno
!= ECANCELED
)) {
104 error_report("tpm_passthrough: error while transmitting data "
106 strerror(errno
), errno
);
111 tpm_pt
->tpm_executing
= false;
113 ret
= tpm_passthrough_unix_read(tpm_pt
->tpm_fd
, out
, out_len
);
115 if (!tpm_pt
->tpm_op_canceled
||
116 (tpm_pt
->tpm_op_canceled
&& errno
!= ECANCELED
)) {
117 error_report("tpm_passthrough: error while reading data from "
119 strerror(errno
), errno
);
121 } else if (ret
< sizeof(struct tpm_resp_hdr
) ||
122 tpm_passthrough_get_size_from_buffer(out
) != ret
) {
124 error_report("tpm_passthrough: received invalid response "
125 "packet from TPM\n");
130 tpm_write_fatal_error_response(out
, out_len
);
133 tpm_pt
->tpm_executing
= false;
138 static int tpm_passthrough_unix_transfer(TPMPassthruState
*tpm_pt
,
139 const TPMLocality
*locty_data
)
141 return tpm_passthrough_unix_tx_bufs(tpm_pt
,
142 locty_data
->w_buffer
.buffer
,
143 locty_data
->w_offset
,
144 locty_data
->r_buffer
.buffer
,
145 locty_data
->r_buffer
.size
);
148 static void tpm_passthrough_worker_thread(gpointer data
,
151 TPMPassthruThreadParams
*thr_parms
= user_data
;
152 TPMPassthruState
*tpm_pt
= thr_parms
->tb
->s
.tpm_pt
;
153 TPMBackendCmd cmd
= (TPMBackendCmd
)data
;
155 DPRINTF("tpm_passthrough: processing command type %d\n", cmd
);
158 case TPM_BACKEND_CMD_PROCESS_CMD
:
159 tpm_passthrough_unix_transfer(tpm_pt
,
160 thr_parms
->tpm_state
->locty_data
);
162 thr_parms
->recv_data_callback(thr_parms
->tpm_state
,
163 thr_parms
->tpm_state
->locty_number
);
165 case TPM_BACKEND_CMD_INIT
:
166 case TPM_BACKEND_CMD_END
:
167 case TPM_BACKEND_CMD_TPM_RESET
:
174 * Start the TPM (thread). If it had been started before, then terminate
175 * and start it again.
177 static int tpm_passthrough_startup_tpm(TPMBackend
*tb
)
179 TPMPassthruState
*tpm_pt
= tb
->s
.tpm_pt
;
181 /* terminate a running TPM */
182 tpm_backend_thread_end(&tpm_pt
->tbt
);
184 tpm_backend_thread_create(&tpm_pt
->tbt
,
185 tpm_passthrough_worker_thread
,
186 &tb
->s
.tpm_pt
->tpm_thread_params
);
191 static void tpm_passthrough_reset(TPMBackend
*tb
)
193 TPMPassthruState
*tpm_pt
= tb
->s
.tpm_pt
;
195 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
197 tpm_passthrough_cancel_cmd(tb
);
199 tpm_backend_thread_end(&tpm_pt
->tbt
);
201 tpm_pt
->had_startup_error
= false;
204 static int tpm_passthrough_init(TPMBackend
*tb
, TPMState
*s
,
205 TPMRecvDataCB
*recv_data_cb
)
207 TPMPassthruState
*tpm_pt
= tb
->s
.tpm_pt
;
209 tpm_pt
->tpm_thread_params
.tpm_state
= s
;
210 tpm_pt
->tpm_thread_params
.recv_data_callback
= recv_data_cb
;
211 tpm_pt
->tpm_thread_params
.tb
= tb
;
216 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend
*tb
)
221 static bool tpm_passthrough_get_startup_error(TPMBackend
*tb
)
223 TPMPassthruState
*tpm_pt
= tb
->s
.tpm_pt
;
225 return tpm_pt
->had_startup_error
;
228 static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer
*sb
)
230 size_t wanted_size
= 4096; /* Linux tpm.c buffer size */
232 if (sb
->size
!= wanted_size
) {
233 sb
->buffer
= g_realloc(sb
->buffer
, wanted_size
);
234 sb
->size
= wanted_size
;
239 static void tpm_passthrough_deliver_request(TPMBackend
*tb
)
241 TPMPassthruState
*tpm_pt
= tb
->s
.tpm_pt
;
243 tpm_backend_thread_deliver_request(&tpm_pt
->tbt
);
246 static void tpm_passthrough_cancel_cmd(TPMBackend
*tb
)
248 TPMPassthruState
*tpm_pt
= tb
->s
.tpm_pt
;
252 * As of Linux 3.7 the tpm_tis driver does not properly cancel
253 * commands on all TPM manufacturers' TPMs.
254 * Only cancel if we're busy so we don't cancel someone else's
255 * command, e.g., a command executed on the host.
257 if (tpm_pt
->tpm_executing
) {
258 if (tpm_pt
->cancel_fd
>= 0) {
259 n
= write(tpm_pt
->cancel_fd
, "-", 1);
261 error_report("Canceling TPM command failed: %s\n",
264 tpm_pt
->tpm_op_canceled
= true;
267 error_report("Cannot cancel TPM command due to missing "
268 "TPM sysfs cancel entry");
273 static const char *tpm_passthrough_create_desc(void)
275 return "Passthrough TPM backend driver";
279 * A basic test of a TPM device. We expect a well formatted response header
280 * (error response is fine) within one second.
282 static int tpm_passthrough_test_tpmdev(int fd
)
284 struct tpm_req_hdr req
= {
285 .tag
= cpu_to_be16(TPM_TAG_RQU_COMMAND
),
286 .len
= cpu_to_be32(sizeof(req
)),
287 .ordinal
= cpu_to_be32(TPM_ORD_GetTicks
),
289 struct tpm_resp_hdr
*resp
;
292 struct timeval tv
= {
296 unsigned char buf
[1024];
298 n
= write(fd
, &req
, sizeof(req
));
302 if (n
!= sizeof(req
)) {
307 FD_SET(fd
, &readfds
);
309 /* wait for a second */
310 n
= select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
315 n
= read(fd
, &buf
, sizeof(buf
));
316 if (n
< sizeof(struct tpm_resp_hdr
)) {
320 resp
= (struct tpm_resp_hdr
*)buf
;
321 /* check the header */
322 if (be16_to_cpu(resp
->tag
) != TPM_TAG_RSP_COMMAND
||
323 be32_to_cpu(resp
->len
) != n
) {
331 * Check whether the given base path, e.g., /sys/class/misc/tpm0/device,
332 * is the sysfs directory of a TPM. A TPM sysfs directory should be uniquely
333 * recognizable by the file entries 'pcrs' and 'cancel'.
334 * Upon success 'true' is returned and the basebath buffer has '/cancel'
337 static bool tpm_passthrough_check_sysfs_cancel(char *basepath
, size_t bufsz
)
342 snprintf(path
, sizeof(path
), "%s/pcrs", basepath
);
343 if (stat(path
, &statbuf
) == -1 || !S_ISREG(statbuf
.st_mode
)) {
347 snprintf(path
, sizeof(path
), "%s/cancel", basepath
);
348 if (stat(path
, &statbuf
) == -1 || !S_ISREG(statbuf
.st_mode
)) {
352 strncpy(basepath
, path
, bufsz
);
358 * Unless path or file descriptor set has been provided by user,
359 * determine the sysfs cancel file following kernel documentation
360 * in Documentation/ABI/stable/sysfs-class-tpm.
362 static int tpm_passthrough_open_sysfs_cancel(TPMBackend
*tb
)
368 struct dirent entry
, *result
;
371 if (tb
->cancel_path
) {
372 fd
= qemu_open(tb
->cancel_path
, O_WRONLY
);
374 error_report("Could not open TPM cancel path : %s",
380 snprintf(path
, sizeof(path
), "/sys/class/misc");
381 pnp_dir
= opendir(path
);
382 if (pnp_dir
!= NULL
) {
383 while (readdir_r(pnp_dir
, &entry
, &result
) == 0 &&
386 * only allow /sys/class/misc/tpm%u type of paths
388 if (sscanf(entry
.d_name
, "tpm%u%n", &idx
, &len
) < 1 ||
389 len
<= strlen("tpm") ||
390 len
!= strlen(entry
.d_name
)) {
394 snprintf(path
, sizeof(path
), "/sys/class/misc/%s/device",
396 if (!tpm_passthrough_check_sysfs_cancel(path
, sizeof(path
))) {
400 fd
= qemu_open(path
, O_WRONLY
);
407 tb
->cancel_path
= g_strdup(path
);
413 static int tpm_passthrough_handle_device_opts(QemuOpts
*opts
, TPMBackend
*tb
)
417 value
= qemu_opt_get(opts
, "cancel-path");
419 tb
->cancel_path
= g_strdup(value
);
422 value
= qemu_opt_get(opts
, "path");
424 value
= TPM_PASSTHROUGH_DEFAULT_DEVICE
;
427 tb
->s
.tpm_pt
->tpm_dev
= g_strdup(value
);
429 tb
->path
= g_strdup(tb
->s
.tpm_pt
->tpm_dev
);
431 tb
->s
.tpm_pt
->tpm_fd
= qemu_open(tb
->s
.tpm_pt
->tpm_dev
, O_RDWR
);
432 if (tb
->s
.tpm_pt
->tpm_fd
< 0) {
433 error_report("Cannot access TPM device using '%s': %s\n",
434 tb
->s
.tpm_pt
->tpm_dev
, strerror(errno
));
435 goto err_free_parameters
;
438 if (tpm_passthrough_test_tpmdev(tb
->s
.tpm_pt
->tpm_fd
)) {
439 error_report("'%s' is not a TPM device.\n",
440 tb
->s
.tpm_pt
->tpm_dev
);
441 goto err_close_tpmdev
;
447 qemu_close(tb
->s
.tpm_pt
->tpm_fd
);
448 tb
->s
.tpm_pt
->tpm_fd
= -1;
454 g_free(tb
->s
.tpm_pt
->tpm_dev
);
455 tb
->s
.tpm_pt
->tpm_dev
= NULL
;
460 static TPMBackend
*tpm_passthrough_create(QemuOpts
*opts
, const char *id
)
464 tb
= g_new0(TPMBackend
, 1);
465 tb
->s
.tpm_pt
= g_new0(TPMPassthruState
, 1);
466 tb
->id
= g_strdup(id
);
467 /* let frontend set the fe_model to proper value */
470 tb
->ops
= &tpm_passthrough_driver
;
472 if (tpm_passthrough_handle_device_opts(opts
, tb
)) {
476 tb
->s
.tpm_pt
->cancel_fd
= tpm_passthrough_open_sysfs_cancel(tb
);
477 if (tb
->s
.tpm_pt
->cancel_fd
< 0) {
485 g_free(tb
->s
.tpm_pt
);
491 static void tpm_passthrough_destroy(TPMBackend
*tb
)
493 TPMPassthruState
*tpm_pt
= tb
->s
.tpm_pt
;
495 tpm_passthrough_cancel_cmd(tb
);
497 tpm_backend_thread_end(&tpm_pt
->tbt
);
499 qemu_close(tpm_pt
->tpm_fd
);
500 qemu_close(tb
->s
.tpm_pt
->cancel_fd
);
504 g_free(tb
->cancel_path
);
505 g_free(tb
->s
.tpm_pt
->tpm_dev
);
506 g_free(tb
->s
.tpm_pt
);
510 const TPMDriverOps tpm_passthrough_driver
= {
511 .type
= TPM_TYPE_PASSTHROUGH
,
512 .desc
= tpm_passthrough_create_desc
,
513 .create
= tpm_passthrough_create
,
514 .destroy
= tpm_passthrough_destroy
,
515 .init
= tpm_passthrough_init
,
516 .startup_tpm
= tpm_passthrough_startup_tpm
,
517 .realloc_buffer
= tpm_passthrough_realloc_buffer
,
518 .reset
= tpm_passthrough_reset
,
519 .had_startup_error
= tpm_passthrough_get_startup_error
,
520 .deliver_request
= tpm_passthrough_deliver_request
,
521 .cancel_cmd
= tpm_passthrough_cancel_cmd
,
522 .get_tpm_established_flag
= tpm_passthrough_get_tpm_established_flag
,
525 static void tpm_passthrough_register(void)
527 tpm_register_driver(&tpm_passthrough_driver
);
530 type_init(tpm_passthrough_register
)