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 "sysemu/tpm_backend_int.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)
49 static const TPMDriverOps tpm_passthrough_driver;
52 typedef struct TPMPassthruThreadParams {
55 TPMRecvDataCB *recv_data_callback;
57 } TPMPassthruThreadParams;
59 struct TPMPassthruState {
64 TPMPassthruThreadParams tpm_thread_params;
71 bool had_startup_error;
73 TPMVersion tpm_version;
76 typedef struct TPMPassthruState TPMPassthruState;
78 #define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
82 static void tpm_passthrough_cancel_cmd(TPMBackend *tb);
84 static int tpm_passthrough_unix_write(int fd, const uint8_t *buf, uint32_t len)
90 ret = write(fd, buf, remain);
92 if (errno != EINTR && errno != EAGAIN) {
95 } else if (ret == 0) {
105 static int tpm_passthrough_unix_read(int fd, uint8_t *buf, uint32_t len)
109 ret = read(fd, buf, len);
111 if (errno != EINTR && errno != EAGAIN) {
119 static uint32_t tpm_passthrough_get_size_from_buffer(const uint8_t *buf)
121 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)buf;
123 return be32_to_cpu(resp->len);
127 * Write an error message in the given output buffer.
129 static void tpm_write_fatal_error_response(uint8_t *out, uint32_t out_len)
131 if (out_len >= sizeof(struct tpm_resp_hdr)) {
132 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)out;
134 resp->tag = cpu_to_be16(TPM_TAG_RSP_COMMAND);
135 resp->len = cpu_to_be32(sizeof(struct tpm_resp_hdr));
136 resp->errcode = cpu_to_be32(TPM_FAIL);
140 static bool tpm_passthrough_is_selftest(const uint8_t *in, uint32_t in_len)
142 struct tpm_req_hdr *hdr = (struct tpm_req_hdr *)in;
144 if (in_len >= sizeof(*hdr)) {
145 return (be32_to_cpu(hdr->ordinal) == TPM_ORD_ContinueSelfTest);
151 static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt,
152 const uint8_t *in, uint32_t in_len,
153 uint8_t *out, uint32_t out_len,
158 const struct tpm_resp_hdr *hdr;
160 tpm_pt->tpm_op_canceled = false;
161 tpm_pt->tpm_executing = true;
162 *selftest_done = false;
164 is_selftest = tpm_passthrough_is_selftest(in, in_len);
166 ret = tpm_passthrough_unix_write(tpm_pt->tpm_fd, in, in_len);
168 if (!tpm_pt->tpm_op_canceled ||
169 (tpm_pt->tpm_op_canceled && errno != ECANCELED)) {
170 error_report("tpm_passthrough: error while transmitting data "
172 strerror(errno), errno);
177 tpm_pt->tpm_executing = false;
179 ret = tpm_passthrough_unix_read(tpm_pt->tpm_fd, out, out_len);
181 if (!tpm_pt->tpm_op_canceled ||
182 (tpm_pt->tpm_op_canceled && errno != ECANCELED)) {
183 error_report("tpm_passthrough: error while reading data from "
185 strerror(errno), errno);
187 } else if (ret < sizeof(struct tpm_resp_hdr) ||
188 tpm_passthrough_get_size_from_buffer(out) != ret) {
190 error_report("tpm_passthrough: received invalid response "
194 if (is_selftest && (ret >= sizeof(struct tpm_resp_hdr))) {
195 hdr = (struct tpm_resp_hdr *)out;
196 *selftest_done = (be32_to_cpu(hdr->errcode) == 0);
201 tpm_write_fatal_error_response(out, out_len);
204 tpm_pt->tpm_executing = false;
209 static int tpm_passthrough_unix_transfer(TPMPassthruState *tpm_pt,
210 const TPMLocality *locty_data,
213 return tpm_passthrough_unix_tx_bufs(tpm_pt,
214 locty_data->w_buffer.buffer,
215 locty_data->w_offset,
216 locty_data->r_buffer.buffer,
217 locty_data->r_buffer.size,
221 static void tpm_passthrough_worker_thread(gpointer data,
224 TPMPassthruThreadParams *thr_parms = user_data;
225 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(thr_parms->tb);
226 TPMBackendCmd cmd = (TPMBackendCmd)data;
227 bool selftest_done = false;
229 DPRINTF("tpm_passthrough: processing command type %d\n", cmd);
232 case TPM_BACKEND_CMD_PROCESS_CMD:
233 tpm_passthrough_unix_transfer(tpm_pt,
234 thr_parms->tpm_state->locty_data,
237 thr_parms->recv_data_callback(thr_parms->tpm_state,
238 thr_parms->tpm_state->locty_number,
241 case TPM_BACKEND_CMD_INIT:
242 case TPM_BACKEND_CMD_END:
243 case TPM_BACKEND_CMD_TPM_RESET:
250 * Start the TPM (thread). If it had been started before, then terminate
251 * and start it again.
253 static int tpm_passthrough_startup_tpm(TPMBackend *tb)
255 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
257 /* terminate a running TPM */
258 tpm_backend_thread_end(&tpm_pt->tbt);
260 tpm_backend_thread_create(&tpm_pt->tbt,
261 tpm_passthrough_worker_thread,
262 &tpm_pt->tpm_thread_params);
267 static void tpm_passthrough_reset(TPMBackend *tb)
269 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
271 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
273 tpm_passthrough_cancel_cmd(tb);
275 tpm_backend_thread_end(&tpm_pt->tbt);
277 tpm_pt->had_startup_error = false;
280 static int tpm_passthrough_init(TPMBackend *tb, TPMState *s,
281 TPMRecvDataCB *recv_data_cb)
283 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
285 tpm_pt->tpm_thread_params.tpm_state = s;
286 tpm_pt->tpm_thread_params.recv_data_callback = recv_data_cb;
287 tpm_pt->tpm_thread_params.tb = tb;
292 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend *tb)
297 static int tpm_passthrough_reset_tpm_established_flag(TPMBackend *tb,
300 /* only a TPM 2.0 will support this */
304 static bool tpm_passthrough_get_startup_error(TPMBackend *tb)
306 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
308 return tpm_pt->had_startup_error;
311 static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer *sb)
313 size_t wanted_size = 4096; /* Linux tpm.c buffer size */
315 if (sb->size != wanted_size) {
316 sb->buffer = g_realloc(sb->buffer, wanted_size);
317 sb->size = wanted_size;
322 static void tpm_passthrough_deliver_request(TPMBackend *tb)
324 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
326 tpm_backend_thread_deliver_request(&tpm_pt->tbt);
329 static void tpm_passthrough_cancel_cmd(TPMBackend *tb)
331 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
335 * As of Linux 3.7 the tpm_tis driver does not properly cancel
336 * commands on all TPM manufacturers' TPMs.
337 * Only cancel if we're busy so we don't cancel someone else's
338 * command, e.g., a command executed on the host.
340 if (tpm_pt->tpm_executing) {
341 if (tpm_pt->cancel_fd >= 0) {
342 n = write(tpm_pt->cancel_fd, "-", 1);
344 error_report("Canceling TPM command failed: %s",
347 tpm_pt->tpm_op_canceled = true;
350 error_report("Cannot cancel TPM command due to missing "
351 "TPM sysfs cancel entry");
356 static const char *tpm_passthrough_create_desc(void)
358 return "Passthrough TPM backend driver";
361 static TPMVersion tpm_passthrough_get_tpm_version(TPMBackend *tb)
363 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
365 return tpm_pt->tpm_version;
369 * Unless path or file descriptor set has been provided by user,
370 * determine the sysfs cancel file following kernel documentation
371 * in Documentation/ABI/stable/sysfs-class-tpm.
372 * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
374 static int tpm_passthrough_open_sysfs_cancel(TPMBackend *tb)
376 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
381 if (tb->cancel_path) {
382 fd = qemu_open(tb->cancel_path, O_WRONLY);
384 error_report("Could not open TPM cancel path : %s",
390 dev = strrchr(tpm_pt->tpm_dev, '/');
393 if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel",
394 dev) < sizeof(path)) {
395 fd = qemu_open(path, O_WRONLY);
397 tb->cancel_path = g_strdup(path);
399 error_report("tpm_passthrough: Could not open TPM cancel "
400 "path %s : %s", path, strerror(errno));
404 error_report("tpm_passthrough: Bad TPM device path %s",
411 static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb)
413 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
416 value = qemu_opt_get(opts, "cancel-path");
417 tb->cancel_path = g_strdup(value);
419 value = qemu_opt_get(opts, "path");
421 value = TPM_PASSTHROUGH_DEFAULT_DEVICE;
424 tpm_pt->tpm_dev = g_strdup(value);
426 tb->path = g_strdup(tpm_pt->tpm_dev);
428 tpm_pt->tpm_fd = qemu_open(tpm_pt->tpm_dev, O_RDWR);
429 if (tpm_pt->tpm_fd < 0) {
430 error_report("Cannot access TPM device using '%s': %s",
431 tpm_pt->tpm_dev, strerror(errno));
432 goto err_free_parameters;
435 if (tpm_util_test_tpmdev(tpm_pt->tpm_fd, &tpm_pt->tpm_version)) {
436 error_report("'%s' is not a TPM device.",
438 goto err_close_tpmdev;
444 qemu_close(tpm_pt->tpm_fd);
451 g_free(tpm_pt->tpm_dev);
452 tpm_pt->tpm_dev = NULL;
457 static TPMBackend *tpm_passthrough_create(QemuOpts *opts, const char *id)
459 Object *obj = object_new(TYPE_TPM_PASSTHROUGH);
460 TPMBackend *tb = TPM_BACKEND(obj);
461 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
463 tb->id = g_strdup(id);
464 /* let frontend set the fe_model to proper value */
467 tb->ops = &tpm_passthrough_driver;
469 if (tpm_passthrough_handle_device_opts(opts, tb)) {
473 tpm_pt->cancel_fd = tpm_passthrough_open_sysfs_cancel(tb);
474 if (tpm_pt->cancel_fd < 0) {
486 static void tpm_passthrough_destroy(TPMBackend *tb)
488 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
490 tpm_passthrough_cancel_cmd(tb);
492 tpm_backend_thread_end(&tpm_pt->tbt);
494 qemu_close(tpm_pt->tpm_fd);
495 qemu_close(tpm_pt->cancel_fd);
499 g_free(tb->cancel_path);
500 g_free(tpm_pt->tpm_dev);
503 static const QemuOptDesc tpm_passthrough_cmdline_opts[] = {
504 TPM_STANDARD_CMDLINE_OPTS,
506 .name = "cancel-path",
507 .type = QEMU_OPT_STRING,
508 .help = "Sysfs file entry for canceling TPM commands",
512 .type = QEMU_OPT_STRING,
513 .help = "Path to TPM device on the host",
515 { /* end of list */ },
518 static const TPMDriverOps tpm_passthrough_driver = {
519 .type = TPM_TYPE_PASSTHROUGH,
520 .opts = tpm_passthrough_cmdline_opts,
521 .desc = tpm_passthrough_create_desc,
522 .create = tpm_passthrough_create,
523 .destroy = tpm_passthrough_destroy,
524 .init = tpm_passthrough_init,
525 .startup_tpm = tpm_passthrough_startup_tpm,
526 .realloc_buffer = tpm_passthrough_realloc_buffer,
527 .reset = tpm_passthrough_reset,
528 .had_startup_error = tpm_passthrough_get_startup_error,
529 .deliver_request = tpm_passthrough_deliver_request,
530 .cancel_cmd = tpm_passthrough_cancel_cmd,
531 .get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag,
532 .reset_tpm_established_flag = tpm_passthrough_reset_tpm_established_flag,
533 .get_tpm_version = tpm_passthrough_get_tpm_version,
536 static void tpm_passthrough_inst_init(Object *obj)
540 static void tpm_passthrough_inst_finalize(Object *obj)
544 static void tpm_passthrough_class_init(ObjectClass *klass, void *data)
546 TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
548 tbc->ops = &tpm_passthrough_driver;
551 static const TypeInfo tpm_passthrough_info = {
552 .name = TYPE_TPM_PASSTHROUGH,
553 .parent = TYPE_TPM_BACKEND,
554 .instance_size = sizeof(TPMPassthruState),
555 .class_init = tpm_passthrough_class_init,
556 .instance_init = tpm_passthrough_inst_init,
557 .instance_finalize = tpm_passthrough_inst_finalize,
560 static void tpm_passthrough_register(void)
562 type_register_static(&tpm_passthrough_info);
563 tpm_register_driver(&tpm_passthrough_driver);
566 type_init(tpm_passthrough_register)