blob: be160c19b3cd2d5a78c355fd2040dd942d122e10 [file] [log] [blame]
Stefan Berger4549a8b2013-02-27 12:47:53 -05001/*
2 * passthrough TPM driver
3 *
4 * Copyright (c) 2010 - 2013 IBM Corporation
5 * Authors:
6 * Stefan Berger <stefanb@us.ibm.com>
7 *
8 * Copyright (C) 2011 IAIK, Graz University of Technology
9 * Author: Andreas Niederl
10 *
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.
15 *
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.
20 *
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/>
23 */
24
25#include "qemu-common.h"
26#include "qapi/error.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010027#include "qemu/error-report.h"
Stefan Berger4549a8b2013-02-27 12:47:53 -050028#include "qemu/sockets.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020029#include "sysemu/tpm_backend.h"
Stefan Berger4549a8b2013-02-27 12:47:53 -050030#include "tpm_int.h"
31#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010032#include "hw/i386/pc.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020033#include "sysemu/tpm_backend_int.h"
Stefan Berger4549a8b2013-02-27 12:47:53 -050034#include "tpm_tis.h"
Stefan Berger56a3c242015-05-26 16:51:06 -040035#include "tpm_util.h"
Stefan Berger4549a8b2013-02-27 12:47:53 -050036
Stefan Berger4d1ba9c2015-03-31 14:49:10 -040037#define DEBUG_TPM 0
Stefan Berger4549a8b2013-02-27 12:47:53 -050038
Stefan Berger4d1ba9c2015-03-31 14:49:10 -040039#define DPRINTF(fmt, ...) do { \
40 if (DEBUG_TPM) { \
41 fprintf(stderr, fmt, ## __VA_ARGS__); \
42 } \
43} while (0);
Stefan Berger4549a8b2013-02-27 12:47:53 -050044
Stefan Berger8f0605c2013-03-28 07:26:21 -040045#define TYPE_TPM_PASSTHROUGH "tpm-passthrough"
46#define TPM_PASSTHROUGH(obj) \
47 OBJECT_CHECK(TPMPassthruState, (obj), TYPE_TPM_PASSTHROUGH)
Stefan Berger4549a8b2013-02-27 12:47:53 -050048
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020049static const TPMDriverOps tpm_passthrough_driver;
50
Stefan Berger8f0605c2013-03-28 07:26:21 -040051/* data structures */
Stefan Berger4549a8b2013-02-27 12:47:53 -050052typedef struct TPMPassthruThreadParams {
53 TPMState *tpm_state;
54
55 TPMRecvDataCB *recv_data_callback;
56 TPMBackend *tb;
57} TPMPassthruThreadParams;
58
59struct TPMPassthruState {
Stefan Berger8f0605c2013-03-28 07:26:21 -040060 TPMBackend parent;
61
Stefan Berger4549a8b2013-02-27 12:47:53 -050062 TPMBackendThread tbt;
63
64 TPMPassthruThreadParams tpm_thread_params;
65
66 char *tpm_dev;
67 int tpm_fd;
Stefan Berger92dcc232013-02-27 12:47:54 -050068 bool tpm_executing;
69 bool tpm_op_canceled;
70 int cancel_fd;
Stefan Berger4549a8b2013-02-27 12:47:53 -050071 bool had_startup_error;
Stefan Berger56a3c242015-05-26 16:51:06 -040072
73 TPMVersion tpm_version;
Stefan Berger4549a8b2013-02-27 12:47:53 -050074};
75
Stefan Berger8f0605c2013-03-28 07:26:21 -040076typedef struct TPMPassthruState TPMPassthruState;
77
Stefan Berger4549a8b2013-02-27 12:47:53 -050078#define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
79
Stefan Berger92dcc232013-02-27 12:47:54 -050080/* functions */
81
82static void tpm_passthrough_cancel_cmd(TPMBackend *tb);
83
Stefan Berger4549a8b2013-02-27 12:47:53 -050084static int tpm_passthrough_unix_write(int fd, const uint8_t *buf, uint32_t len)
85{
86 return send_all(fd, buf, len);
87}
88
89static int tpm_passthrough_unix_read(int fd, uint8_t *buf, uint32_t len)
90{
91 return recv_all(fd, buf, len, true);
92}
93
94static uint32_t tpm_passthrough_get_size_from_buffer(const uint8_t *buf)
95{
96 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)buf;
97
98 return be32_to_cpu(resp->len);
99}
100
Paolo Bonzinibdee56f2013-04-02 18:28:41 +0200101/*
102 * Write an error message in the given output buffer.
103 */
104static void tpm_write_fatal_error_response(uint8_t *out, uint32_t out_len)
105{
106 if (out_len >= sizeof(struct tpm_resp_hdr)) {
107 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)out;
108
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);
112 }
113}
114
Stefan Bergerfd859082015-02-23 09:27:19 -0500115static bool tpm_passthrough_is_selftest(const uint8_t *in, uint32_t in_len)
116{
117 struct tpm_req_hdr *hdr = (struct tpm_req_hdr *)in;
118
119 if (in_len >= sizeof(*hdr)) {
120 return (be32_to_cpu(hdr->ordinal) == TPM_ORD_ContinueSelfTest);
121 }
122
123 return false;
124}
125
Stefan Berger92dcc232013-02-27 12:47:54 -0500126static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt,
Stefan Berger4549a8b2013-02-27 12:47:53 -0500127 const uint8_t *in, uint32_t in_len,
Stefan Bergerfd859082015-02-23 09:27:19 -0500128 uint8_t *out, uint32_t out_len,
129 bool *selftest_done)
Stefan Berger4549a8b2013-02-27 12:47:53 -0500130{
131 int ret;
Stefan Bergerfd859082015-02-23 09:27:19 -0500132 bool is_selftest;
133 const struct tpm_resp_hdr *hdr;
Stefan Berger4549a8b2013-02-27 12:47:53 -0500134
Stefan Berger92dcc232013-02-27 12:47:54 -0500135 tpm_pt->tpm_op_canceled = false;
136 tpm_pt->tpm_executing = true;
Stefan Bergerfd859082015-02-23 09:27:19 -0500137 *selftest_done = false;
138
139 is_selftest = tpm_passthrough_is_selftest(in, in_len);
Stefan Berger92dcc232013-02-27 12:47:54 -0500140
141 ret = tpm_passthrough_unix_write(tpm_pt->tpm_fd, in, in_len);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500142 if (ret != in_len) {
Stefan Berger92dcc232013-02-27 12:47:54 -0500143 if (!tpm_pt->tpm_op_canceled ||
144 (tpm_pt->tpm_op_canceled && errno != ECANCELED)) {
145 error_report("tpm_passthrough: error while transmitting data "
Gonglei27215a22015-02-25 12:22:35 +0800146 "to TPM: %s (%i)",
Stefan Berger92dcc232013-02-27 12:47:54 -0500147 strerror(errno), errno);
148 }
Stefan Berger4549a8b2013-02-27 12:47:53 -0500149 goto err_exit;
150 }
151
Stefan Berger92dcc232013-02-27 12:47:54 -0500152 tpm_pt->tpm_executing = false;
153
154 ret = tpm_passthrough_unix_read(tpm_pt->tpm_fd, out, out_len);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500155 if (ret < 0) {
Stefan Berger92dcc232013-02-27 12:47:54 -0500156 if (!tpm_pt->tpm_op_canceled ||
157 (tpm_pt->tpm_op_canceled && errno != ECANCELED)) {
158 error_report("tpm_passthrough: error while reading data from "
Gonglei27215a22015-02-25 12:22:35 +0800159 "TPM: %s (%i)",
Stefan Berger92dcc232013-02-27 12:47:54 -0500160 strerror(errno), errno);
161 }
Stefan Berger4549a8b2013-02-27 12:47:53 -0500162 } else if (ret < sizeof(struct tpm_resp_hdr) ||
163 tpm_passthrough_get_size_from_buffer(out) != ret) {
164 ret = -1;
165 error_report("tpm_passthrough: received invalid response "
Gonglei27215a22015-02-25 12:22:35 +0800166 "packet from TPM");
Stefan Berger4549a8b2013-02-27 12:47:53 -0500167 }
168
Stefan Bergerfd859082015-02-23 09:27:19 -0500169 if (is_selftest && (ret >= sizeof(struct tpm_resp_hdr))) {
170 hdr = (struct tpm_resp_hdr *)out;
171 *selftest_done = (be32_to_cpu(hdr->errcode) == 0);
172 }
173
Stefan Berger4549a8b2013-02-27 12:47:53 -0500174err_exit:
175 if (ret < 0) {
176 tpm_write_fatal_error_response(out, out_len);
177 }
178
Stefan Berger92dcc232013-02-27 12:47:54 -0500179 tpm_pt->tpm_executing = false;
180
Stefan Berger4549a8b2013-02-27 12:47:53 -0500181 return ret;
182}
183
Stefan Berger92dcc232013-02-27 12:47:54 -0500184static int tpm_passthrough_unix_transfer(TPMPassthruState *tpm_pt,
Stefan Bergerfd859082015-02-23 09:27:19 -0500185 const TPMLocality *locty_data,
186 bool *selftest_done)
Stefan Berger4549a8b2013-02-27 12:47:53 -0500187{
Stefan Berger92dcc232013-02-27 12:47:54 -0500188 return tpm_passthrough_unix_tx_bufs(tpm_pt,
Stefan Berger4549a8b2013-02-27 12:47:53 -0500189 locty_data->w_buffer.buffer,
190 locty_data->w_offset,
191 locty_data->r_buffer.buffer,
Stefan Bergerfd859082015-02-23 09:27:19 -0500192 locty_data->r_buffer.size,
193 selftest_done);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500194}
195
196static void tpm_passthrough_worker_thread(gpointer data,
197 gpointer user_data)
198{
199 TPMPassthruThreadParams *thr_parms = user_data;
Stefan Berger8f0605c2013-03-28 07:26:21 -0400200 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(thr_parms->tb);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500201 TPMBackendCmd cmd = (TPMBackendCmd)data;
Stefan Bergerfd859082015-02-23 09:27:19 -0500202 bool selftest_done = false;
Stefan Berger4549a8b2013-02-27 12:47:53 -0500203
204 DPRINTF("tpm_passthrough: processing command type %d\n", cmd);
205
206 switch (cmd) {
207 case TPM_BACKEND_CMD_PROCESS_CMD:
Stefan Berger92dcc232013-02-27 12:47:54 -0500208 tpm_passthrough_unix_transfer(tpm_pt,
Stefan Bergerfd859082015-02-23 09:27:19 -0500209 thr_parms->tpm_state->locty_data,
210 &selftest_done);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500211
212 thr_parms->recv_data_callback(thr_parms->tpm_state,
Stefan Bergerfd859082015-02-23 09:27:19 -0500213 thr_parms->tpm_state->locty_number,
214 selftest_done);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500215 break;
216 case TPM_BACKEND_CMD_INIT:
217 case TPM_BACKEND_CMD_END:
218 case TPM_BACKEND_CMD_TPM_RESET:
219 /* nothing to do */
220 break;
221 }
222}
223
224/*
225 * Start the TPM (thread). If it had been started before, then terminate
226 * and start it again.
227 */
228static int tpm_passthrough_startup_tpm(TPMBackend *tb)
229{
Stefan Berger8f0605c2013-03-28 07:26:21 -0400230 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500231
232 /* terminate a running TPM */
233 tpm_backend_thread_end(&tpm_pt->tbt);
234
235 tpm_backend_thread_create(&tpm_pt->tbt,
236 tpm_passthrough_worker_thread,
Stefan Berger8f0605c2013-03-28 07:26:21 -0400237 &tpm_pt->tpm_thread_params);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500238
239 return 0;
240}
241
242static void tpm_passthrough_reset(TPMBackend *tb)
243{
Stefan Berger8f0605c2013-03-28 07:26:21 -0400244 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500245
246 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
247
Stefan Berger92dcc232013-02-27 12:47:54 -0500248 tpm_passthrough_cancel_cmd(tb);
249
Stefan Berger4549a8b2013-02-27 12:47:53 -0500250 tpm_backend_thread_end(&tpm_pt->tbt);
251
252 tpm_pt->had_startup_error = false;
253}
254
255static int tpm_passthrough_init(TPMBackend *tb, TPMState *s,
256 TPMRecvDataCB *recv_data_cb)
257{
Stefan Berger8f0605c2013-03-28 07:26:21 -0400258 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500259
260 tpm_pt->tpm_thread_params.tpm_state = s;
261 tpm_pt->tpm_thread_params.recv_data_callback = recv_data_cb;
262 tpm_pt->tpm_thread_params.tb = tb;
263
264 return 0;
265}
266
267static bool tpm_passthrough_get_tpm_established_flag(TPMBackend *tb)
268{
269 return false;
270}
271
Stefan Berger116694c2015-05-26 16:51:05 -0400272static int tpm_passthrough_reset_tpm_established_flag(TPMBackend *tb,
273 uint8_t locty)
274{
275 /* only a TPM 2.0 will support this */
276 return 0;
277}
278
Stefan Berger4549a8b2013-02-27 12:47:53 -0500279static bool tpm_passthrough_get_startup_error(TPMBackend *tb)
280{
Stefan Berger8f0605c2013-03-28 07:26:21 -0400281 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500282
283 return tpm_pt->had_startup_error;
284}
285
286static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer *sb)
287{
288 size_t wanted_size = 4096; /* Linux tpm.c buffer size */
289
290 if (sb->size != wanted_size) {
291 sb->buffer = g_realloc(sb->buffer, wanted_size);
292 sb->size = wanted_size;
293 }
294 return sb->size;
295}
296
297static void tpm_passthrough_deliver_request(TPMBackend *tb)
298{
Stefan Berger8f0605c2013-03-28 07:26:21 -0400299 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500300
301 tpm_backend_thread_deliver_request(&tpm_pt->tbt);
302}
303
304static void tpm_passthrough_cancel_cmd(TPMBackend *tb)
305{
Stefan Berger8f0605c2013-03-28 07:26:21 -0400306 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
Stefan Berger92dcc232013-02-27 12:47:54 -0500307 int n;
308
309 /*
310 * As of Linux 3.7 the tpm_tis driver does not properly cancel
311 * commands on all TPM manufacturers' TPMs.
312 * Only cancel if we're busy so we don't cancel someone else's
313 * command, e.g., a command executed on the host.
314 */
315 if (tpm_pt->tpm_executing) {
316 if (tpm_pt->cancel_fd >= 0) {
317 n = write(tpm_pt->cancel_fd, "-", 1);
318 if (n != 1) {
Gonglei27215a22015-02-25 12:22:35 +0800319 error_report("Canceling TPM command failed: %s",
Stefan Berger92dcc232013-02-27 12:47:54 -0500320 strerror(errno));
321 } else {
322 tpm_pt->tpm_op_canceled = true;
323 }
324 } else {
325 error_report("Cannot cancel TPM command due to missing "
326 "TPM sysfs cancel entry");
327 }
328 }
Stefan Berger4549a8b2013-02-27 12:47:53 -0500329}
330
331static const char *tpm_passthrough_create_desc(void)
332{
333 return "Passthrough TPM backend driver";
334}
335
Stefan Berger116694c2015-05-26 16:51:05 -0400336static TPMVersion tpm_passthrough_get_tpm_version(TPMBackend *tb)
337{
Stefan Berger56a3c242015-05-26 16:51:06 -0400338 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
Stefan Berger116694c2015-05-26 16:51:05 -0400339
Stefan Berger56a3c242015-05-26 16:51:06 -0400340 return tpm_pt->tpm_version;
Stefan Berger4549a8b2013-02-27 12:47:53 -0500341}
342
Stefan Berger92dcc232013-02-27 12:47:54 -0500343/*
Stefan Berger92dcc232013-02-27 12:47:54 -0500344 * Unless path or file descriptor set has been provided by user,
345 * determine the sysfs cancel file following kernel documentation
346 * in Documentation/ABI/stable/sysfs-class-tpm.
Stefan Berger8e36d6c2013-04-16 17:08:36 -0400347 * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
Stefan Berger92dcc232013-02-27 12:47:54 -0500348 */
349static int tpm_passthrough_open_sysfs_cancel(TPMBackend *tb)
350{
Stefan Berger8e36d6c2013-04-16 17:08:36 -0400351 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
Stefan Berger92dcc232013-02-27 12:47:54 -0500352 int fd = -1;
Stefan Berger8e36d6c2013-04-16 17:08:36 -0400353 char *dev;
Stefan Berger92dcc232013-02-27 12:47:54 -0500354 char path[PATH_MAX];
Stefan Berger92dcc232013-02-27 12:47:54 -0500355
356 if (tb->cancel_path) {
357 fd = qemu_open(tb->cancel_path, O_WRONLY);
358 if (fd < 0) {
359 error_report("Could not open TPM cancel path : %s",
360 strerror(errno));
361 }
362 return fd;
363 }
364
Stefan Berger8e36d6c2013-04-16 17:08:36 -0400365 dev = strrchr(tpm_pt->tpm_dev, '/');
366 if (dev) {
367 dev++;
368 if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel",
369 dev) < sizeof(path)) {
Stefan Berger92dcc232013-02-27 12:47:54 -0500370 fd = qemu_open(path, O_WRONLY);
Stefan Berger8e36d6c2013-04-16 17:08:36 -0400371 if (fd >= 0) {
372 tb->cancel_path = g_strdup(path);
373 } else {
374 error_report("tpm_passthrough: Could not open TPM cancel "
375 "path %s : %s", path, strerror(errno));
376 }
Stefan Berger92dcc232013-02-27 12:47:54 -0500377 }
Stefan Berger8e36d6c2013-04-16 17:08:36 -0400378 } else {
379 error_report("tpm_passthrough: Bad TPM device path %s",
380 tpm_pt->tpm_dev);
Stefan Berger92dcc232013-02-27 12:47:54 -0500381 }
382
383 return fd;
384}
385
Stefan Berger4549a8b2013-02-27 12:47:53 -0500386static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb)
387{
Stefan Berger8f0605c2013-03-28 07:26:21 -0400388 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500389 const char *value;
390
Stefan Berger92dcc232013-02-27 12:47:54 -0500391 value = qemu_opt_get(opts, "cancel-path");
Markus Armbruster24588102014-12-04 10:26:55 +0100392 tb->cancel_path = g_strdup(value);
Stefan Berger92dcc232013-02-27 12:47:54 -0500393
Stefan Berger4549a8b2013-02-27 12:47:53 -0500394 value = qemu_opt_get(opts, "path");
395 if (!value) {
396 value = TPM_PASSTHROUGH_DEFAULT_DEVICE;
397 }
398
Stefan Berger8f0605c2013-03-28 07:26:21 -0400399 tpm_pt->tpm_dev = g_strdup(value);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500400
Stefan Berger8f0605c2013-03-28 07:26:21 -0400401 tb->path = g_strdup(tpm_pt->tpm_dev);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500402
Stefan Berger8f0605c2013-03-28 07:26:21 -0400403 tpm_pt->tpm_fd = qemu_open(tpm_pt->tpm_dev, O_RDWR);
404 if (tpm_pt->tpm_fd < 0) {
Gonglei27215a22015-02-25 12:22:35 +0800405 error_report("Cannot access TPM device using '%s': %s",
Stefan Berger8f0605c2013-03-28 07:26:21 -0400406 tpm_pt->tpm_dev, strerror(errno));
Stefan Berger4549a8b2013-02-27 12:47:53 -0500407 goto err_free_parameters;
408 }
409
Stefan Berger56a3c242015-05-26 16:51:06 -0400410 if (tpm_util_test_tpmdev(tpm_pt->tpm_fd, &tpm_pt->tpm_version)) {
Gonglei27215a22015-02-25 12:22:35 +0800411 error_report("'%s' is not a TPM device.",
Stefan Berger8f0605c2013-03-28 07:26:21 -0400412 tpm_pt->tpm_dev);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500413 goto err_close_tpmdev;
414 }
415
416 return 0;
417
418 err_close_tpmdev:
Stefan Berger8f0605c2013-03-28 07:26:21 -0400419 qemu_close(tpm_pt->tpm_fd);
420 tpm_pt->tpm_fd = -1;
Stefan Berger4549a8b2013-02-27 12:47:53 -0500421
422 err_free_parameters:
423 g_free(tb->path);
424 tb->path = NULL;
425
Stefan Berger8f0605c2013-03-28 07:26:21 -0400426 g_free(tpm_pt->tpm_dev);
427 tpm_pt->tpm_dev = NULL;
Stefan Berger4549a8b2013-02-27 12:47:53 -0500428
429 return 1;
430}
431
432static TPMBackend *tpm_passthrough_create(QemuOpts *opts, const char *id)
433{
Stefan Berger8f0605c2013-03-28 07:26:21 -0400434 Object *obj = object_new(TYPE_TPM_PASSTHROUGH);
435 TPMBackend *tb = TPM_BACKEND(obj);
436 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500437
Stefan Berger4549a8b2013-02-27 12:47:53 -0500438 tb->id = g_strdup(id);
439 /* let frontend set the fe_model to proper value */
440 tb->fe_model = -1;
441
442 tb->ops = &tpm_passthrough_driver;
443
444 if (tpm_passthrough_handle_device_opts(opts, tb)) {
445 goto err_exit;
446 }
447
Stefan Berger8f0605c2013-03-28 07:26:21 -0400448 tpm_pt->cancel_fd = tpm_passthrough_open_sysfs_cancel(tb);
449 if (tpm_pt->cancel_fd < 0) {
Stefan Berger92dcc232013-02-27 12:47:54 -0500450 goto err_exit;
451 }
452
Stefan Berger4549a8b2013-02-27 12:47:53 -0500453 return tb;
454
455err_exit:
456 g_free(tb->id);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500457
458 return NULL;
459}
460
461static void tpm_passthrough_destroy(TPMBackend *tb)
462{
Stefan Berger8f0605c2013-03-28 07:26:21 -0400463 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500464
Stefan Berger92dcc232013-02-27 12:47:54 -0500465 tpm_passthrough_cancel_cmd(tb);
466
Stefan Berger4549a8b2013-02-27 12:47:53 -0500467 tpm_backend_thread_end(&tpm_pt->tbt);
468
469 qemu_close(tpm_pt->tpm_fd);
Stefan Berger8f0605c2013-03-28 07:26:21 -0400470 qemu_close(tpm_pt->cancel_fd);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500471
472 g_free(tb->id);
473 g_free(tb->path);
Stefan Berger92dcc232013-02-27 12:47:54 -0500474 g_free(tb->cancel_path);
Stefan Berger8f0605c2013-03-28 07:26:21 -0400475 g_free(tpm_pt->tpm_dev);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500476}
477
Stefan Bergerbb716232013-04-22 10:41:39 -0400478static const QemuOptDesc tpm_passthrough_cmdline_opts[] = {
479 TPM_STANDARD_CMDLINE_OPTS,
480 {
481 .name = "cancel-path",
482 .type = QEMU_OPT_STRING,
483 .help = "Sysfs file entry for canceling TPM commands",
484 },
485 {
486 .name = "path",
487 .type = QEMU_OPT_STRING,
488 .help = "Path to TPM device on the host",
489 },
490 { /* end of list */ },
491};
492
Paolo Bonzinibdee56f2013-04-02 18:28:41 +0200493static const TPMDriverOps tpm_passthrough_driver = {
Stefan Berger4549a8b2013-02-27 12:47:53 -0500494 .type = TPM_TYPE_PASSTHROUGH,
Stefan Bergerbb716232013-04-22 10:41:39 -0400495 .opts = tpm_passthrough_cmdline_opts,
Stefan Berger4549a8b2013-02-27 12:47:53 -0500496 .desc = tpm_passthrough_create_desc,
497 .create = tpm_passthrough_create,
498 .destroy = tpm_passthrough_destroy,
499 .init = tpm_passthrough_init,
500 .startup_tpm = tpm_passthrough_startup_tpm,
501 .realloc_buffer = tpm_passthrough_realloc_buffer,
502 .reset = tpm_passthrough_reset,
503 .had_startup_error = tpm_passthrough_get_startup_error,
504 .deliver_request = tpm_passthrough_deliver_request,
505 .cancel_cmd = tpm_passthrough_cancel_cmd,
506 .get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag,
Stefan Berger116694c2015-05-26 16:51:05 -0400507 .reset_tpm_established_flag = tpm_passthrough_reset_tpm_established_flag,
508 .get_tpm_version = tpm_passthrough_get_tpm_version,
Stefan Berger4549a8b2013-02-27 12:47:53 -0500509};
510
Stefan Berger8f0605c2013-03-28 07:26:21 -0400511static void tpm_passthrough_inst_init(Object *obj)
512{
513}
514
515static void tpm_passthrough_inst_finalize(Object *obj)
516{
517}
518
519static void tpm_passthrough_class_init(ObjectClass *klass, void *data)
520{
521 TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
522
523 tbc->ops = &tpm_passthrough_driver;
524}
525
526static const TypeInfo tpm_passthrough_info = {
527 .name = TYPE_TPM_PASSTHROUGH,
528 .parent = TYPE_TPM_BACKEND,
529 .instance_size = sizeof(TPMPassthruState),
530 .class_init = tpm_passthrough_class_init,
531 .instance_init = tpm_passthrough_inst_init,
532 .instance_finalize = tpm_passthrough_inst_finalize,
533};
534
Stefan Berger4549a8b2013-02-27 12:47:53 -0500535static void tpm_passthrough_register(void)
536{
Stefan Berger8f0605c2013-03-28 07:26:21 -0400537 type_register_static(&tpm_passthrough_info);
Stefan Berger4549a8b2013-02-27 12:47:53 -0500538 tpm_register_driver(&tpm_passthrough_driver);
539}
540
541type_init(tpm_passthrough_register)