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"
34 #include "sysemu/tpm_backend_int.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)
51 static const TPMDriverOps tpm_passthrough_driver;
54 typedef struct TPMPassthruThreadParams {
57 TPMRecvDataCB *recv_data_callback;
59 } TPMPassthruThreadParams;
61 struct TPMPassthruState {
66 TPMPassthruThreadParams tpm_thread_params;
73 bool had_startup_error;
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)
86 return send_all(fd, buf, len);
89 static int tpm_passthrough_unix_read(int fd, uint8_t *buf, uint32_t len)
91 return recv_all(fd, buf, len, true);
94 static uint32_t tpm_passthrough_get_size_from_buffer(const uint8_t *buf)
96 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)buf;
98 return be32_to_cpu(resp->len);
102 * Write an error message in the given output buffer.
104 static void tpm_write_fatal_error_response(uint8_t *out, uint32_t out_len)
106 if (out_len >= sizeof(struct tpm_resp_hdr)) {
107 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)out;
109 resp->tag = cpu_to_be16(TPM_TAG_RSP_COMMAND);
110 resp->len = cpu_to_be32(sizeof(struct tpm_resp_hdr));
111 resp->errcode = cpu_to_be32(TPM_FAIL);
115 static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt,
116 const uint8_t *in, uint32_t in_len,
117 uint8_t *out, uint32_t out_len)
121 tpm_pt->tpm_op_canceled = false;
122 tpm_pt->tpm_executing = true;
124 ret = tpm_passthrough_unix_write(tpm_pt->tpm_fd, in, in_len);
126 if (!tpm_pt->tpm_op_canceled ||
127 (tpm_pt->tpm_op_canceled && errno != ECANCELED)) {
128 error_report("tpm_passthrough: error while transmitting data "
130 strerror(errno), errno);
135 tpm_pt->tpm_executing = false;
137 ret = tpm_passthrough_unix_read(tpm_pt->tpm_fd, out, out_len);
139 if (!tpm_pt->tpm_op_canceled ||
140 (tpm_pt->tpm_op_canceled && errno != ECANCELED)) {
141 error_report("tpm_passthrough: error while reading data from "
143 strerror(errno), errno);
145 } else if (ret < sizeof(struct tpm_resp_hdr) ||
146 tpm_passthrough_get_size_from_buffer(out) != ret) {
148 error_report("tpm_passthrough: received invalid response "
149 "packet from TPM\n");
154 tpm_write_fatal_error_response(out, out_len);
157 tpm_pt->tpm_executing = false;
162 static int tpm_passthrough_unix_transfer(TPMPassthruState *tpm_pt,
163 const TPMLocality *locty_data)
165 return tpm_passthrough_unix_tx_bufs(tpm_pt,
166 locty_data->w_buffer.buffer,
167 locty_data->w_offset,
168 locty_data->r_buffer.buffer,
169 locty_data->r_buffer.size);
172 static void tpm_passthrough_worker_thread(gpointer data,
175 TPMPassthruThreadParams *thr_parms = user_data;
176 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(thr_parms->tb);
177 TPMBackendCmd cmd = (TPMBackendCmd)data;
179 DPRINTF("tpm_passthrough: processing command type %d\n", cmd);
182 case TPM_BACKEND_CMD_PROCESS_CMD:
183 tpm_passthrough_unix_transfer(tpm_pt,
184 thr_parms->tpm_state->locty_data);
186 thr_parms->recv_data_callback(thr_parms->tpm_state,
187 thr_parms->tpm_state->locty_number);
189 case TPM_BACKEND_CMD_INIT:
190 case TPM_BACKEND_CMD_END:
191 case TPM_BACKEND_CMD_TPM_RESET:
198 * Start the TPM (thread). If it had been started before, then terminate
199 * and start it again.
201 static int tpm_passthrough_startup_tpm(TPMBackend *tb)
203 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
205 /* terminate a running TPM */
206 tpm_backend_thread_end(&tpm_pt->tbt);
208 tpm_backend_thread_create(&tpm_pt->tbt,
209 tpm_passthrough_worker_thread,
210 &tpm_pt->tpm_thread_params);
215 static void tpm_passthrough_reset(TPMBackend *tb)
217 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
219 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
221 tpm_passthrough_cancel_cmd(tb);
223 tpm_backend_thread_end(&tpm_pt->tbt);
225 tpm_pt->had_startup_error = false;
228 static int tpm_passthrough_init(TPMBackend *tb, TPMState *s,
229 TPMRecvDataCB *recv_data_cb)
231 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
233 tpm_pt->tpm_thread_params.tpm_state = s;
234 tpm_pt->tpm_thread_params.recv_data_callback = recv_data_cb;
235 tpm_pt->tpm_thread_params.tb = tb;
240 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend *tb)
245 static bool tpm_passthrough_get_startup_error(TPMBackend *tb)
247 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
249 return tpm_pt->had_startup_error;
252 static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer *sb)
254 size_t wanted_size = 4096; /* Linux tpm.c buffer size */
256 if (sb->size != wanted_size) {
257 sb->buffer = g_realloc(sb->buffer, wanted_size);
258 sb->size = wanted_size;
263 static void tpm_passthrough_deliver_request(TPMBackend *tb)
265 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
267 tpm_backend_thread_deliver_request(&tpm_pt->tbt);
270 static void tpm_passthrough_cancel_cmd(TPMBackend *tb)
272 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
276 * As of Linux 3.7 the tpm_tis driver does not properly cancel
277 * commands on all TPM manufacturers' TPMs.
278 * Only cancel if we're busy so we don't cancel someone else's
279 * command, e.g., a command executed on the host.
281 if (tpm_pt->tpm_executing) {
282 if (tpm_pt->cancel_fd >= 0) {
283 n = write(tpm_pt->cancel_fd, "-", 1);
285 error_report("Canceling TPM command failed: %s\n",
288 tpm_pt->tpm_op_canceled = true;
291 error_report("Cannot cancel TPM command due to missing "
292 "TPM sysfs cancel entry");
297 static const char *tpm_passthrough_create_desc(void)
299 return "Passthrough TPM backend driver";
303 * A basic test of a TPM device. We expect a well formatted response header
304 * (error response is fine) within one second.
306 static int tpm_passthrough_test_tpmdev(int fd)
308 struct tpm_req_hdr req = {
309 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
310 .len = cpu_to_be32(sizeof(req)),
311 .ordinal = cpu_to_be32(TPM_ORD_GetTicks),
313 struct tpm_resp_hdr *resp;
316 struct timeval tv = {
320 unsigned char buf[1024];
322 n = write(fd, &req, sizeof(req));
326 if (n != sizeof(req)) {
331 FD_SET(fd, &readfds);
333 /* wait for a second */
334 n = select(fd + 1, &readfds, NULL, NULL, &tv);
339 n = read(fd, &buf, sizeof(buf));
340 if (n < sizeof(struct tpm_resp_hdr)) {
344 resp = (struct tpm_resp_hdr *)buf;
345 /* check the header */
346 if (be16_to_cpu(resp->tag) != TPM_TAG_RSP_COMMAND ||
347 be32_to_cpu(resp->len) != n) {
355 * Check whether the given base path, e.g., /sys/class/misc/tpm0/device,
356 * is the sysfs directory of a TPM. A TPM sysfs directory should be uniquely
357 * recognizable by the file entries 'pcrs' and 'cancel'.
358 * Upon success 'true' is returned and the basebath buffer has '/cancel'
361 static bool tpm_passthrough_check_sysfs_cancel(char *basepath, size_t bufsz)
366 snprintf(path, sizeof(path), "%s/pcrs", basepath);
367 if (stat(path, &statbuf) == -1 || !S_ISREG(statbuf.st_mode)) {
371 snprintf(path, sizeof(path), "%s/cancel", basepath);
372 if (stat(path, &statbuf) == -1 || !S_ISREG(statbuf.st_mode)) {
376 strncpy(basepath, path, bufsz);
382 * Unless path or file descriptor set has been provided by user,
383 * determine the sysfs cancel file following kernel documentation
384 * in Documentation/ABI/stable/sysfs-class-tpm.
386 static int tpm_passthrough_open_sysfs_cancel(TPMBackend *tb)
392 struct dirent entry, *result;
395 if (tb->cancel_path) {
396 fd = qemu_open(tb->cancel_path, O_WRONLY);
398 error_report("Could not open TPM cancel path : %s",
404 snprintf(path, sizeof(path), "/sys/class/misc");
405 pnp_dir = opendir(path);
406 if (pnp_dir != NULL) {
407 while (readdir_r(pnp_dir, &entry, &result) == 0 &&
410 * only allow /sys/class/misc/tpm%u type of paths
412 if (sscanf(entry.d_name, "tpm%u%n", &idx, &len) < 1 ||
413 len <= strlen("tpm") ||
414 len != strlen(entry.d_name)) {
418 snprintf(path, sizeof(path), "/sys/class/misc/%s/device",
420 if (!tpm_passthrough_check_sysfs_cancel(path, sizeof(path))) {
424 fd = qemu_open(path, O_WRONLY);
431 tb->cancel_path = g_strdup(path);
437 static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb)
439 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
442 value = qemu_opt_get(opts, "cancel-path");
444 tb->cancel_path = g_strdup(value);
447 value = qemu_opt_get(opts, "path");
449 value = TPM_PASSTHROUGH_DEFAULT_DEVICE;
452 tpm_pt->tpm_dev = g_strdup(value);
454 tb->path = g_strdup(tpm_pt->tpm_dev);
456 tpm_pt->tpm_fd = qemu_open(tpm_pt->tpm_dev, O_RDWR);
457 if (tpm_pt->tpm_fd < 0) {
458 error_report("Cannot access TPM device using '%s': %s\n",
459 tpm_pt->tpm_dev, strerror(errno));
460 goto err_free_parameters;
463 if (tpm_passthrough_test_tpmdev(tpm_pt->tpm_fd)) {
464 error_report("'%s' is not a TPM device.\n",
466 goto err_close_tpmdev;
472 qemu_close(tpm_pt->tpm_fd);
479 g_free(tpm_pt->tpm_dev);
480 tpm_pt->tpm_dev = NULL;
485 static TPMBackend *tpm_passthrough_create(QemuOpts *opts, const char *id)
487 Object *obj = object_new(TYPE_TPM_PASSTHROUGH);
488 TPMBackend *tb = TPM_BACKEND(obj);
489 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
491 tb->id = g_strdup(id);
492 /* let frontend set the fe_model to proper value */
495 tb->ops = &tpm_passthrough_driver;
497 if (tpm_passthrough_handle_device_opts(opts, tb)) {
501 tpm_pt->cancel_fd = tpm_passthrough_open_sysfs_cancel(tb);
502 if (tpm_pt->cancel_fd < 0) {
514 static void tpm_passthrough_destroy(TPMBackend *tb)
516 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
518 tpm_passthrough_cancel_cmd(tb);
520 tpm_backend_thread_end(&tpm_pt->tbt);
522 qemu_close(tpm_pt->tpm_fd);
523 qemu_close(tpm_pt->cancel_fd);
527 g_free(tb->cancel_path);
528 g_free(tpm_pt->tpm_dev);
531 static const TPMDriverOps tpm_passthrough_driver = {
532 .type = TPM_TYPE_PASSTHROUGH,
533 .desc = tpm_passthrough_create_desc,
534 .create = tpm_passthrough_create,
535 .destroy = tpm_passthrough_destroy,
536 .init = tpm_passthrough_init,
537 .startup_tpm = tpm_passthrough_startup_tpm,
538 .realloc_buffer = tpm_passthrough_realloc_buffer,
539 .reset = tpm_passthrough_reset,
540 .had_startup_error = tpm_passthrough_get_startup_error,
541 .deliver_request = tpm_passthrough_deliver_request,
542 .cancel_cmd = tpm_passthrough_cancel_cmd,
543 .get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag,
546 static void tpm_passthrough_inst_init(Object *obj)
550 static void tpm_passthrough_inst_finalize(Object *obj)
554 static void tpm_passthrough_class_init(ObjectClass *klass, void *data)
556 TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
558 tbc->ops = &tpm_passthrough_driver;
561 static const TypeInfo tpm_passthrough_info = {
562 .name = TYPE_TPM_PASSTHROUGH,
563 .parent = TYPE_TPM_BACKEND,
564 .instance_size = sizeof(TPMPassthruState),
565 .class_init = tpm_passthrough_class_init,
566 .instance_init = tpm_passthrough_inst_init,
567 .instance_finalize = tpm_passthrough_inst_finalize,
570 static void tpm_passthrough_register(void)
572 type_register_static(&tpm_passthrough_info);
573 tpm_register_driver(&tpm_passthrough_driver);
576 type_init(tpm_passthrough_register)