Extend TPM TIS interface to support TPM 2
[qemu.git] / hw / tpm / tpm_passthrough.c
blobf1361d2cb6e4968dc7511bb35829a51b761789dc
1 /*
2 * passthrough TPM driver
4 * Copyright (c) 2010 - 2013 IBM Corporation
5 * Authors:
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 <dirent.h>
27 #include "qemu-common.h"
28 #include "qapi/error.h"
29 #include "qemu/sockets.h"
30 #include "sysemu/tpm_backend.h"
31 #include "tpm_int.h"
32 #include "hw/hw.h"
33 #include "hw/i386/pc.h"
34 #include "sysemu/tpm_backend_int.h"
35 #include "tpm_tis.h"
37 #define DEBUG_TPM 0
39 #define DPRINTF(fmt, ...) do { \
40 if (DEBUG_TPM) { \
41 fprintf(stderr, fmt, ## __VA_ARGS__); \
42 } \
43 } while (0);
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;
51 /* data structures */
52 typedef struct TPMPassthruThreadParams {
53 TPMState *tpm_state;
55 TPMRecvDataCB *recv_data_callback;
56 TPMBackend *tb;
57 } TPMPassthruThreadParams;
59 struct TPMPassthruState {
60 TPMBackend parent;
62 TPMBackendThread tbt;
64 TPMPassthruThreadParams tpm_thread_params;
66 char *tpm_dev;
67 int tpm_fd;
68 bool tpm_executing;
69 bool tpm_op_canceled;
70 int cancel_fd;
71 bool had_startup_error;
74 typedef struct TPMPassthruState TPMPassthruState;
76 #define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
78 /* functions */
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);
100 * Write an error message in the given output buffer.
102 static void tpm_write_fatal_error_response(uint8_t *out, uint32_t out_len)
104 if (out_len >= sizeof(struct tpm_resp_hdr)) {
105 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)out;
107 resp->tag = cpu_to_be16(TPM_TAG_RSP_COMMAND);
108 resp->len = cpu_to_be32(sizeof(struct tpm_resp_hdr));
109 resp->errcode = cpu_to_be32(TPM_FAIL);
113 static bool tpm_passthrough_is_selftest(const uint8_t *in, uint32_t in_len)
115 struct tpm_req_hdr *hdr = (struct tpm_req_hdr *)in;
117 if (in_len >= sizeof(*hdr)) {
118 return (be32_to_cpu(hdr->ordinal) == TPM_ORD_ContinueSelfTest);
121 return false;
124 static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt,
125 const uint8_t *in, uint32_t in_len,
126 uint8_t *out, uint32_t out_len,
127 bool *selftest_done)
129 int ret;
130 bool is_selftest;
131 const struct tpm_resp_hdr *hdr;
133 tpm_pt->tpm_op_canceled = false;
134 tpm_pt->tpm_executing = true;
135 *selftest_done = false;
137 is_selftest = tpm_passthrough_is_selftest(in, in_len);
139 ret = tpm_passthrough_unix_write(tpm_pt->tpm_fd, in, in_len);
140 if (ret != in_len) {
141 if (!tpm_pt->tpm_op_canceled ||
142 (tpm_pt->tpm_op_canceled && errno != ECANCELED)) {
143 error_report("tpm_passthrough: error while transmitting data "
144 "to TPM: %s (%i)",
145 strerror(errno), errno);
147 goto err_exit;
150 tpm_pt->tpm_executing = false;
152 ret = tpm_passthrough_unix_read(tpm_pt->tpm_fd, out, out_len);
153 if (ret < 0) {
154 if (!tpm_pt->tpm_op_canceled ||
155 (tpm_pt->tpm_op_canceled && errno != ECANCELED)) {
156 error_report("tpm_passthrough: error while reading data from "
157 "TPM: %s (%i)",
158 strerror(errno), errno);
160 } else if (ret < sizeof(struct tpm_resp_hdr) ||
161 tpm_passthrough_get_size_from_buffer(out) != ret) {
162 ret = -1;
163 error_report("tpm_passthrough: received invalid response "
164 "packet from TPM");
167 if (is_selftest && (ret >= sizeof(struct tpm_resp_hdr))) {
168 hdr = (struct tpm_resp_hdr *)out;
169 *selftest_done = (be32_to_cpu(hdr->errcode) == 0);
172 err_exit:
173 if (ret < 0) {
174 tpm_write_fatal_error_response(out, out_len);
177 tpm_pt->tpm_executing = false;
179 return ret;
182 static int tpm_passthrough_unix_transfer(TPMPassthruState *tpm_pt,
183 const TPMLocality *locty_data,
184 bool *selftest_done)
186 return tpm_passthrough_unix_tx_bufs(tpm_pt,
187 locty_data->w_buffer.buffer,
188 locty_data->w_offset,
189 locty_data->r_buffer.buffer,
190 locty_data->r_buffer.size,
191 selftest_done);
194 static void tpm_passthrough_worker_thread(gpointer data,
195 gpointer user_data)
197 TPMPassthruThreadParams *thr_parms = user_data;
198 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(thr_parms->tb);
199 TPMBackendCmd cmd = (TPMBackendCmd)data;
200 bool selftest_done = false;
202 DPRINTF("tpm_passthrough: processing command type %d\n", cmd);
204 switch (cmd) {
205 case TPM_BACKEND_CMD_PROCESS_CMD:
206 tpm_passthrough_unix_transfer(tpm_pt,
207 thr_parms->tpm_state->locty_data,
208 &selftest_done);
210 thr_parms->recv_data_callback(thr_parms->tpm_state,
211 thr_parms->tpm_state->locty_number,
212 selftest_done);
213 break;
214 case TPM_BACKEND_CMD_INIT:
215 case TPM_BACKEND_CMD_END:
216 case TPM_BACKEND_CMD_TPM_RESET:
217 /* nothing to do */
218 break;
223 * Start the TPM (thread). If it had been started before, then terminate
224 * and start it again.
226 static int tpm_passthrough_startup_tpm(TPMBackend *tb)
228 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
230 /* terminate a running TPM */
231 tpm_backend_thread_end(&tpm_pt->tbt);
233 tpm_backend_thread_create(&tpm_pt->tbt,
234 tpm_passthrough_worker_thread,
235 &tpm_pt->tpm_thread_params);
237 return 0;
240 static void tpm_passthrough_reset(TPMBackend *tb)
242 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
244 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
246 tpm_passthrough_cancel_cmd(tb);
248 tpm_backend_thread_end(&tpm_pt->tbt);
250 tpm_pt->had_startup_error = false;
253 static int tpm_passthrough_init(TPMBackend *tb, TPMState *s,
254 TPMRecvDataCB *recv_data_cb)
256 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
258 tpm_pt->tpm_thread_params.tpm_state = s;
259 tpm_pt->tpm_thread_params.recv_data_callback = recv_data_cb;
260 tpm_pt->tpm_thread_params.tb = tb;
262 return 0;
265 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend *tb)
267 return false;
270 static int tpm_passthrough_reset_tpm_established_flag(TPMBackend *tb,
271 uint8_t locty)
273 /* only a TPM 2.0 will support this */
274 return 0;
277 static bool tpm_passthrough_get_startup_error(TPMBackend *tb)
279 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
281 return tpm_pt->had_startup_error;
284 static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer *sb)
286 size_t wanted_size = 4096; /* Linux tpm.c buffer size */
288 if (sb->size != wanted_size) {
289 sb->buffer = g_realloc(sb->buffer, wanted_size);
290 sb->size = wanted_size;
292 return sb->size;
295 static void tpm_passthrough_deliver_request(TPMBackend *tb)
297 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
299 tpm_backend_thread_deliver_request(&tpm_pt->tbt);
302 static void tpm_passthrough_cancel_cmd(TPMBackend *tb)
304 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
305 int n;
308 * As of Linux 3.7 the tpm_tis driver does not properly cancel
309 * commands on all TPM manufacturers' TPMs.
310 * Only cancel if we're busy so we don't cancel someone else's
311 * command, e.g., a command executed on the host.
313 if (tpm_pt->tpm_executing) {
314 if (tpm_pt->cancel_fd >= 0) {
315 n = write(tpm_pt->cancel_fd, "-", 1);
316 if (n != 1) {
317 error_report("Canceling TPM command failed: %s",
318 strerror(errno));
319 } else {
320 tpm_pt->tpm_op_canceled = true;
322 } else {
323 error_report("Cannot cancel TPM command due to missing "
324 "TPM sysfs cancel entry");
329 static const char *tpm_passthrough_create_desc(void)
331 return "Passthrough TPM backend driver";
334 static TPMVersion tpm_passthrough_get_tpm_version(TPMBackend *tb)
336 return TPM_VERSION_1_2;
340 * A basic test of a TPM device. We expect a well formatted response header
341 * (error response is fine) within one second.
343 static int tpm_passthrough_test_tpmdev(int fd)
345 struct tpm_req_hdr req = {
346 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
347 .len = cpu_to_be32(sizeof(req)),
348 .ordinal = cpu_to_be32(TPM_ORD_GetTicks),
350 struct tpm_resp_hdr *resp;
351 fd_set readfds;
352 int n;
353 struct timeval tv = {
354 .tv_sec = 1,
355 .tv_usec = 0,
357 unsigned char buf[1024];
359 n = write(fd, &req, sizeof(req));
360 if (n < 0) {
361 return errno;
363 if (n != sizeof(req)) {
364 return EFAULT;
367 FD_ZERO(&readfds);
368 FD_SET(fd, &readfds);
370 /* wait for a second */
371 n = select(fd + 1, &readfds, NULL, NULL, &tv);
372 if (n != 1) {
373 return errno;
376 n = read(fd, &buf, sizeof(buf));
377 if (n < sizeof(struct tpm_resp_hdr)) {
378 return EFAULT;
381 resp = (struct tpm_resp_hdr *)buf;
382 /* check the header */
383 if (be16_to_cpu(resp->tag) != TPM_TAG_RSP_COMMAND ||
384 be32_to_cpu(resp->len) != n) {
385 return EBADMSG;
388 return 0;
392 * Unless path or file descriptor set has been provided by user,
393 * determine the sysfs cancel file following kernel documentation
394 * in Documentation/ABI/stable/sysfs-class-tpm.
395 * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
397 static int tpm_passthrough_open_sysfs_cancel(TPMBackend *tb)
399 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
400 int fd = -1;
401 char *dev;
402 char path[PATH_MAX];
404 if (tb->cancel_path) {
405 fd = qemu_open(tb->cancel_path, O_WRONLY);
406 if (fd < 0) {
407 error_report("Could not open TPM cancel path : %s",
408 strerror(errno));
410 return fd;
413 dev = strrchr(tpm_pt->tpm_dev, '/');
414 if (dev) {
415 dev++;
416 if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel",
417 dev) < sizeof(path)) {
418 fd = qemu_open(path, O_WRONLY);
419 if (fd >= 0) {
420 tb->cancel_path = g_strdup(path);
421 } else {
422 error_report("tpm_passthrough: Could not open TPM cancel "
423 "path %s : %s", path, strerror(errno));
426 } else {
427 error_report("tpm_passthrough: Bad TPM device path %s",
428 tpm_pt->tpm_dev);
431 return fd;
434 static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb)
436 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
437 const char *value;
439 value = qemu_opt_get(opts, "cancel-path");
440 tb->cancel_path = g_strdup(value);
442 value = qemu_opt_get(opts, "path");
443 if (!value) {
444 value = TPM_PASSTHROUGH_DEFAULT_DEVICE;
447 tpm_pt->tpm_dev = g_strdup(value);
449 tb->path = g_strdup(tpm_pt->tpm_dev);
451 tpm_pt->tpm_fd = qemu_open(tpm_pt->tpm_dev, O_RDWR);
452 if (tpm_pt->tpm_fd < 0) {
453 error_report("Cannot access TPM device using '%s': %s",
454 tpm_pt->tpm_dev, strerror(errno));
455 goto err_free_parameters;
458 if (tpm_passthrough_test_tpmdev(tpm_pt->tpm_fd)) {
459 error_report("'%s' is not a TPM device.",
460 tpm_pt->tpm_dev);
461 goto err_close_tpmdev;
464 return 0;
466 err_close_tpmdev:
467 qemu_close(tpm_pt->tpm_fd);
468 tpm_pt->tpm_fd = -1;
470 err_free_parameters:
471 g_free(tb->path);
472 tb->path = NULL;
474 g_free(tpm_pt->tpm_dev);
475 tpm_pt->tpm_dev = NULL;
477 return 1;
480 static TPMBackend *tpm_passthrough_create(QemuOpts *opts, const char *id)
482 Object *obj = object_new(TYPE_TPM_PASSTHROUGH);
483 TPMBackend *tb = TPM_BACKEND(obj);
484 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
486 tb->id = g_strdup(id);
487 /* let frontend set the fe_model to proper value */
488 tb->fe_model = -1;
490 tb->ops = &tpm_passthrough_driver;
492 if (tpm_passthrough_handle_device_opts(opts, tb)) {
493 goto err_exit;
496 tpm_pt->cancel_fd = tpm_passthrough_open_sysfs_cancel(tb);
497 if (tpm_pt->cancel_fd < 0) {
498 goto err_exit;
501 return tb;
503 err_exit:
504 g_free(tb->id);
506 return NULL;
509 static void tpm_passthrough_destroy(TPMBackend *tb)
511 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
513 tpm_passthrough_cancel_cmd(tb);
515 tpm_backend_thread_end(&tpm_pt->tbt);
517 qemu_close(tpm_pt->tpm_fd);
518 qemu_close(tpm_pt->cancel_fd);
520 g_free(tb->id);
521 g_free(tb->path);
522 g_free(tb->cancel_path);
523 g_free(tpm_pt->tpm_dev);
526 static const QemuOptDesc tpm_passthrough_cmdline_opts[] = {
527 TPM_STANDARD_CMDLINE_OPTS,
529 .name = "cancel-path",
530 .type = QEMU_OPT_STRING,
531 .help = "Sysfs file entry for canceling TPM commands",
534 .name = "path",
535 .type = QEMU_OPT_STRING,
536 .help = "Path to TPM device on the host",
538 { /* end of list */ },
541 static const TPMDriverOps tpm_passthrough_driver = {
542 .type = TPM_TYPE_PASSTHROUGH,
543 .opts = tpm_passthrough_cmdline_opts,
544 .desc = tpm_passthrough_create_desc,
545 .create = tpm_passthrough_create,
546 .destroy = tpm_passthrough_destroy,
547 .init = tpm_passthrough_init,
548 .startup_tpm = tpm_passthrough_startup_tpm,
549 .realloc_buffer = tpm_passthrough_realloc_buffer,
550 .reset = tpm_passthrough_reset,
551 .had_startup_error = tpm_passthrough_get_startup_error,
552 .deliver_request = tpm_passthrough_deliver_request,
553 .cancel_cmd = tpm_passthrough_cancel_cmd,
554 .get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag,
555 .reset_tpm_established_flag = tpm_passthrough_reset_tpm_established_flag,
556 .get_tpm_version = tpm_passthrough_get_tpm_version,
559 static void tpm_passthrough_inst_init(Object *obj)
563 static void tpm_passthrough_inst_finalize(Object *obj)
567 static void tpm_passthrough_class_init(ObjectClass *klass, void *data)
569 TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
571 tbc->ops = &tpm_passthrough_driver;
574 static const TypeInfo tpm_passthrough_info = {
575 .name = TYPE_TPM_PASSTHROUGH,
576 .parent = TYPE_TPM_BACKEND,
577 .instance_size = sizeof(TPMPassthruState),
578 .class_init = tpm_passthrough_class_init,
579 .instance_init = tpm_passthrough_inst_init,
580 .instance_finalize = tpm_passthrough_inst_finalize,
583 static void tpm_passthrough_register(void)
585 type_register_static(&tpm_passthrough_info);
586 tpm_register_driver(&tpm_passthrough_driver);
589 type_init(tpm_passthrough_register)