tpm-backend: Remove unneeded member variable from backend class
[qemu/armbru.git] / hw / tpm / tpm_passthrough.c
bloba0baf5f080d2dd1f7f5530395dbb94b736cc2a7c
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 "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"
30 #include "tpm_int.h"
31 #include "hw/hw.h"
32 #include "hw/i386/pc.h"
33 #include "sysemu/tpm_backend_int.h"
34 #include "tpm_tis.h"
35 #include "tpm_util.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 /* data structures */
50 typedef struct TPMPassthruThreadParams {
51 TPMState *tpm_state;
53 TPMRecvDataCB *recv_data_callback;
54 TPMBackend *tb;
55 } TPMPassthruThreadParams;
57 struct TPMPassthruState {
58 TPMBackend parent;
60 TPMBackendThread tbt;
62 TPMPassthruThreadParams tpm_thread_params;
64 char *tpm_dev;
65 int tpm_fd;
66 bool tpm_executing;
67 bool tpm_op_canceled;
68 int cancel_fd;
69 bool had_startup_error;
71 TPMVersion tpm_version;
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 int ret, remain;
86 remain = len;
87 while (remain > 0) {
88 ret = write(fd, buf, remain);
89 if (ret < 0) {
90 if (errno != EINTR && errno != EAGAIN) {
91 return -1;
93 } else if (ret == 0) {
94 break;
95 } else {
96 buf += ret;
97 remain -= ret;
100 return len - remain;
103 static int tpm_passthrough_unix_read(int fd, uint8_t *buf, uint32_t len)
105 int ret;
106 reread:
107 ret = read(fd, buf, len);
108 if (ret < 0) {
109 if (errno != EINTR && errno != EAGAIN) {
110 return -1;
112 goto reread;
114 return ret;
117 static uint32_t tpm_passthrough_get_size_from_buffer(const uint8_t *buf)
119 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)buf;
121 return be32_to_cpu(resp->len);
125 * Write an error message in the given output buffer.
127 static void tpm_write_fatal_error_response(uint8_t *out, uint32_t out_len)
129 if (out_len >= sizeof(struct tpm_resp_hdr)) {
130 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)out;
132 resp->tag = cpu_to_be16(TPM_TAG_RSP_COMMAND);
133 resp->len = cpu_to_be32(sizeof(struct tpm_resp_hdr));
134 resp->errcode = cpu_to_be32(TPM_FAIL);
138 static bool tpm_passthrough_is_selftest(const uint8_t *in, uint32_t in_len)
140 struct tpm_req_hdr *hdr = (struct tpm_req_hdr *)in;
142 if (in_len >= sizeof(*hdr)) {
143 return (be32_to_cpu(hdr->ordinal) == TPM_ORD_ContinueSelfTest);
146 return false;
149 static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt,
150 const uint8_t *in, uint32_t in_len,
151 uint8_t *out, uint32_t out_len,
152 bool *selftest_done)
154 int ret;
155 bool is_selftest;
156 const struct tpm_resp_hdr *hdr;
158 tpm_pt->tpm_op_canceled = false;
159 tpm_pt->tpm_executing = true;
160 *selftest_done = false;
162 is_selftest = tpm_passthrough_is_selftest(in, in_len);
164 ret = tpm_passthrough_unix_write(tpm_pt->tpm_fd, in, in_len);
165 if (ret != in_len) {
166 if (!tpm_pt->tpm_op_canceled || errno != ECANCELED) {
167 error_report("tpm_passthrough: error while transmitting data "
168 "to TPM: %s (%i)",
169 strerror(errno), errno);
171 goto err_exit;
174 tpm_pt->tpm_executing = false;
176 ret = tpm_passthrough_unix_read(tpm_pt->tpm_fd, out, out_len);
177 if (ret < 0) {
178 if (!tpm_pt->tpm_op_canceled || errno != ECANCELED) {
179 error_report("tpm_passthrough: error while reading data from "
180 "TPM: %s (%i)",
181 strerror(errno), errno);
183 } else if (ret < sizeof(struct tpm_resp_hdr) ||
184 tpm_passthrough_get_size_from_buffer(out) != ret) {
185 ret = -1;
186 error_report("tpm_passthrough: received invalid response "
187 "packet from TPM");
190 if (is_selftest && (ret >= sizeof(struct tpm_resp_hdr))) {
191 hdr = (struct tpm_resp_hdr *)out;
192 *selftest_done = (be32_to_cpu(hdr->errcode) == 0);
195 err_exit:
196 if (ret < 0) {
197 tpm_write_fatal_error_response(out, out_len);
200 tpm_pt->tpm_executing = false;
202 return ret;
205 static int tpm_passthrough_unix_transfer(TPMPassthruState *tpm_pt,
206 const TPMLocality *locty_data,
207 bool *selftest_done)
209 return tpm_passthrough_unix_tx_bufs(tpm_pt,
210 locty_data->w_buffer.buffer,
211 locty_data->w_offset,
212 locty_data->r_buffer.buffer,
213 locty_data->r_buffer.size,
214 selftest_done);
217 static void tpm_passthrough_worker_thread(gpointer data,
218 gpointer user_data)
220 TPMPassthruThreadParams *thr_parms = user_data;
221 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(thr_parms->tb);
222 TPMBackendCmd cmd = (TPMBackendCmd)data;
223 bool selftest_done = false;
225 DPRINTF("tpm_passthrough: processing command type %d\n", cmd);
227 switch (cmd) {
228 case TPM_BACKEND_CMD_PROCESS_CMD:
229 tpm_passthrough_unix_transfer(tpm_pt,
230 thr_parms->tpm_state->locty_data,
231 &selftest_done);
233 thr_parms->recv_data_callback(thr_parms->tpm_state,
234 thr_parms->tpm_state->locty_number,
235 selftest_done);
236 break;
237 case TPM_BACKEND_CMD_INIT:
238 case TPM_BACKEND_CMD_END:
239 case TPM_BACKEND_CMD_TPM_RESET:
240 /* nothing to do */
241 break;
246 * Start the TPM (thread). If it had been started before, then terminate
247 * and start it again.
249 static int tpm_passthrough_startup_tpm(TPMBackend *tb)
251 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
253 /* terminate a running TPM */
254 tpm_backend_thread_end(&tpm_pt->tbt);
256 tpm_backend_thread_create(&tpm_pt->tbt,
257 tpm_passthrough_worker_thread,
258 &tpm_pt->tpm_thread_params);
260 return 0;
263 static void tpm_passthrough_reset(TPMBackend *tb)
265 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
267 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
269 tpm_passthrough_cancel_cmd(tb);
271 tpm_backend_thread_end(&tpm_pt->tbt);
273 tpm_pt->had_startup_error = false;
276 static int tpm_passthrough_init(TPMBackend *tb, TPMState *s,
277 TPMRecvDataCB *recv_data_cb)
279 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
281 tpm_pt->tpm_thread_params.tpm_state = s;
282 tpm_pt->tpm_thread_params.recv_data_callback = recv_data_cb;
283 tpm_pt->tpm_thread_params.tb = tb;
285 return 0;
288 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend *tb)
290 return false;
293 static int tpm_passthrough_reset_tpm_established_flag(TPMBackend *tb,
294 uint8_t locty)
296 /* only a TPM 2.0 will support this */
297 return 0;
300 static bool tpm_passthrough_get_startup_error(TPMBackend *tb)
302 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
304 return tpm_pt->had_startup_error;
307 static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer *sb)
309 size_t wanted_size = 4096; /* Linux tpm.c buffer size */
311 if (sb->size != wanted_size) {
312 sb->buffer = g_realloc(sb->buffer, wanted_size);
313 sb->size = wanted_size;
315 return sb->size;
318 static void tpm_passthrough_deliver_request(TPMBackend *tb)
320 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
322 tpm_backend_thread_deliver_request(&tpm_pt->tbt);
325 static void tpm_passthrough_cancel_cmd(TPMBackend *tb)
327 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
328 int n;
331 * As of Linux 3.7 the tpm_tis driver does not properly cancel
332 * commands on all TPM manufacturers' TPMs.
333 * Only cancel if we're busy so we don't cancel someone else's
334 * command, e.g., a command executed on the host.
336 if (tpm_pt->tpm_executing) {
337 if (tpm_pt->cancel_fd >= 0) {
338 n = write(tpm_pt->cancel_fd, "-", 1);
339 if (n != 1) {
340 error_report("Canceling TPM command failed: %s",
341 strerror(errno));
342 } else {
343 tpm_pt->tpm_op_canceled = true;
345 } else {
346 error_report("Cannot cancel TPM command due to missing "
347 "TPM sysfs cancel entry");
352 static const char *tpm_passthrough_create_desc(void)
354 return "Passthrough TPM backend driver";
357 static TPMVersion tpm_passthrough_get_tpm_version(TPMBackend *tb)
359 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
361 return tpm_pt->tpm_version;
365 * Unless path or file descriptor set has been provided by user,
366 * determine the sysfs cancel file following kernel documentation
367 * in Documentation/ABI/stable/sysfs-class-tpm.
368 * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
370 static int tpm_passthrough_open_sysfs_cancel(TPMBackend *tb)
372 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
373 int fd = -1;
374 char *dev;
375 char path[PATH_MAX];
377 if (tb->cancel_path) {
378 fd = qemu_open(tb->cancel_path, O_WRONLY);
379 if (fd < 0) {
380 error_report("Could not open TPM cancel path : %s",
381 strerror(errno));
383 return fd;
386 dev = strrchr(tpm_pt->tpm_dev, '/');
387 if (dev) {
388 dev++;
389 if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel",
390 dev) < sizeof(path)) {
391 fd = qemu_open(path, O_WRONLY);
392 if (fd >= 0) {
393 tb->cancel_path = g_strdup(path);
394 } else {
395 error_report("tpm_passthrough: Could not open TPM cancel "
396 "path %s : %s", path, strerror(errno));
399 } else {
400 error_report("tpm_passthrough: Bad TPM device path %s",
401 tpm_pt->tpm_dev);
404 return fd;
407 static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb)
409 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
410 const char *value;
412 value = qemu_opt_get(opts, "cancel-path");
413 tb->cancel_path = g_strdup(value);
415 value = qemu_opt_get(opts, "path");
416 if (!value) {
417 value = TPM_PASSTHROUGH_DEFAULT_DEVICE;
420 tpm_pt->tpm_dev = g_strdup(value);
422 tb->path = g_strdup(tpm_pt->tpm_dev);
424 tpm_pt->tpm_fd = qemu_open(tpm_pt->tpm_dev, O_RDWR);
425 if (tpm_pt->tpm_fd < 0) {
426 error_report("Cannot access TPM device using '%s': %s",
427 tpm_pt->tpm_dev, strerror(errno));
428 goto err_free_parameters;
431 if (tpm_util_test_tpmdev(tpm_pt->tpm_fd, &tpm_pt->tpm_version)) {
432 error_report("'%s' is not a TPM device.",
433 tpm_pt->tpm_dev);
434 goto err_close_tpmdev;
437 return 0;
439 err_close_tpmdev:
440 qemu_close(tpm_pt->tpm_fd);
441 tpm_pt->tpm_fd = -1;
443 err_free_parameters:
444 g_free(tb->path);
445 tb->path = NULL;
447 g_free(tpm_pt->tpm_dev);
448 tpm_pt->tpm_dev = NULL;
450 return 1;
453 static TPMBackend *tpm_passthrough_create(QemuOpts *opts, const char *id)
455 Object *obj = object_new(TYPE_TPM_PASSTHROUGH);
456 TPMBackend *tb = TPM_BACKEND(obj);
457 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
459 tb->id = g_strdup(id);
460 /* let frontend set the fe_model to proper value */
461 tb->fe_model = -1;
463 if (tpm_passthrough_handle_device_opts(opts, tb)) {
464 goto err_exit;
467 tpm_pt->cancel_fd = tpm_passthrough_open_sysfs_cancel(tb);
468 if (tpm_pt->cancel_fd < 0) {
469 goto err_exit;
472 return tb;
474 err_exit:
475 g_free(tb->id);
477 return NULL;
480 static void tpm_passthrough_destroy(TPMBackend *tb)
482 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
484 tpm_passthrough_cancel_cmd(tb);
486 tpm_backend_thread_end(&tpm_pt->tbt);
488 qemu_close(tpm_pt->tpm_fd);
489 qemu_close(tpm_pt->cancel_fd);
491 g_free(tb->id);
492 g_free(tb->path);
493 g_free(tb->cancel_path);
494 g_free(tpm_pt->tpm_dev);
497 static const QemuOptDesc tpm_passthrough_cmdline_opts[] = {
498 TPM_STANDARD_CMDLINE_OPTS,
500 .name = "cancel-path",
501 .type = QEMU_OPT_STRING,
502 .help = "Sysfs file entry for canceling TPM commands",
505 .name = "path",
506 .type = QEMU_OPT_STRING,
507 .help = "Path to TPM device on the host",
509 { /* end of list */ },
512 static const TPMDriverOps tpm_passthrough_driver = {
513 .type = TPM_TYPE_PASSTHROUGH,
514 .opts = tpm_passthrough_cmdline_opts,
515 .desc = tpm_passthrough_create_desc,
516 .create = tpm_passthrough_create,
517 .destroy = tpm_passthrough_destroy,
518 .init = tpm_passthrough_init,
519 .startup_tpm = tpm_passthrough_startup_tpm,
520 .realloc_buffer = tpm_passthrough_realloc_buffer,
521 .reset = tpm_passthrough_reset,
522 .had_startup_error = tpm_passthrough_get_startup_error,
523 .deliver_request = tpm_passthrough_deliver_request,
524 .cancel_cmd = tpm_passthrough_cancel_cmd,
525 .get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag,
526 .reset_tpm_established_flag = tpm_passthrough_reset_tpm_established_flag,
527 .get_tpm_version = tpm_passthrough_get_tpm_version,
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)