]> git.proxmox.com Git - mirror_qemu.git/blame - hw/tpm/tpm_passthrough.c
tpm: convert tpm_crb.c to use trace-events
[mirror_qemu.git] / hw / tpm / tpm_passthrough.c
CommitLineData
4549a8b7
SB
1/*
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
0430891c 25#include "qemu/osdep.h"
4549a8b7 26#include "qemu-common.h"
d49b6836 27#include "qemu/error-report.h"
4549a8b7 28#include "qemu/sockets.h"
dccfcd0e 29#include "sysemu/tpm_backend.h"
4549a8b7
SB
30#include "tpm_int.h"
31#include "hw/hw.h"
f59864ba 32#include "qapi/clone-visitor.h"
56a3c24f 33#include "tpm_util.h"
4549a8b7 34
4d1ba9c4
SB
35#define DEBUG_TPM 0
36
37#define DPRINTF(fmt, ...) do { \
38 if (DEBUG_TPM) { \
39 fprintf(stderr, fmt, ## __VA_ARGS__); \
40 } \
2562755e 41} while (0)
4549a8b7 42
8f0605cc
SB
43#define TYPE_TPM_PASSTHROUGH "tpm-passthrough"
44#define TPM_PASSTHROUGH(obj) \
45 OBJECT_CHECK(TPMPassthruState, (obj), TYPE_TPM_PASSTHROUGH)
4549a8b7 46
8f0605cc 47/* data structures */
4549a8b7 48struct TPMPassthruState {
8f0605cc
SB
49 TPMBackend parent;
50
f59864ba
AV
51 TPMPassthroughOptions *options;
52 const char *tpm_dev;
4549a8b7 53 int tpm_fd;
92dcc234
SB
54 bool tpm_executing;
55 bool tpm_op_canceled;
56 int cancel_fd;
56a3c24f
SB
57
58 TPMVersion tpm_version;
abc5cda0 59 size_t tpm_buffersize;
4549a8b7
SB
60};
61
8f0605cc
SB
62typedef struct TPMPassthruState TPMPassthruState;
63
4549a8b7
SB
64#define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
65
92dcc234
SB
66/* functions */
67
68static void tpm_passthrough_cancel_cmd(TPMBackend *tb);
69
4549a8b7
SB
70static int tpm_passthrough_unix_read(int fd, uint8_t *buf, uint32_t len)
71{
46f296cd
DB
72 int ret;
73 reread:
74 ret = read(fd, buf, len);
75 if (ret < 0) {
76 if (errno != EINTR && errno != EAGAIN) {
77 return -1;
78 }
79 goto reread;
80 }
81 return ret;
4549a8b7 82}
6a8a2354
MAL
83
84static void tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt,
85 const uint8_t *in, uint32_t in_len,
86 uint8_t *out, uint32_t out_len,
87 bool *selftest_done, Error **errp)
4549a8b7 88{
4a3d8098 89 ssize_t ret;
fd859081 90 bool is_selftest;
4549a8b7 91
21cb1e63 92 /* FIXME: protect shared variables or use other sync mechanism */
92dcc234
SB
93 tpm_pt->tpm_op_canceled = false;
94 tpm_pt->tpm_executing = true;
fd859081
SB
95 *selftest_done = false;
96
4a3d8098 97 is_selftest = tpm_util_is_selftest(in, in_len);
92dcc234 98
54aa36d5 99 ret = qemu_write_full(tpm_pt->tpm_fd, in, in_len);
4549a8b7 100 if (ret != in_len) {
5f333d79 101 if (!tpm_pt->tpm_op_canceled || errno != ECANCELED) {
6a8a2354
MAL
102 error_setg_errno(errp, errno, "tpm_passthrough: error while "
103 "transmitting data to TPM");
92dcc234 104 }
4549a8b7
SB
105 goto err_exit;
106 }
107
92dcc234
SB
108 tpm_pt->tpm_executing = false;
109
110 ret = tpm_passthrough_unix_read(tpm_pt->tpm_fd, out, out_len);
4549a8b7 111 if (ret < 0) {
5f333d79 112 if (!tpm_pt->tpm_op_canceled || errno != ECANCELED) {
6a8a2354
MAL
113 error_setg_errno(errp, errno, "tpm_passthrough: error while "
114 "reading data from TPM");
92dcc234 115 }
4549a8b7 116 } else if (ret < sizeof(struct tpm_resp_hdr) ||
cc1b6c55 117 tpm_cmd_get_size(out) != ret) {
4549a8b7 118 ret = -1;
6a8a2354
MAL
119 error_setg_errno(errp, errno, "tpm_passthrough: received invalid "
120 "response packet from TPM");
4549a8b7
SB
121 }
122
fd859081 123 if (is_selftest && (ret >= sizeof(struct tpm_resp_hdr))) {
cc1b6c55 124 *selftest_done = tpm_cmd_get_errcode(out) == 0;
fd859081
SB
125 }
126
4549a8b7
SB
127err_exit:
128 if (ret < 0) {
4a3d8098 129 tpm_util_write_fatal_error_response(out, out_len);
4549a8b7
SB
130 }
131
92dcc234 132 tpm_pt->tpm_executing = false;
4549a8b7
SB
133}
134
6a8a2354
MAL
135static void tpm_passthrough_handle_request(TPMBackend *tb, TPMBackendCmd *cmd,
136 Error **errp)
4549a8b7 137{
b19a5eea 138 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
4549a8b7 139
0e43b7e6 140 DPRINTF("tpm_passthrough: processing command %p\n", cmd);
905e78ba 141
0e43b7e6 142 tpm_passthrough_unix_tx_bufs(tpm_pt, cmd->in, cmd->in_len,
6a8a2354
MAL
143 cmd->out, cmd->out_len, &cmd->selftest_done,
144 errp);
4549a8b7
SB
145}
146
4549a8b7
SB
147static void tpm_passthrough_reset(TPMBackend *tb)
148{
4549a8b7
SB
149 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
150
92dcc234 151 tpm_passthrough_cancel_cmd(tb);
4549a8b7
SB
152}
153
154static bool tpm_passthrough_get_tpm_established_flag(TPMBackend *tb)
155{
156 return false;
157}
158
116694c3
SB
159static int tpm_passthrough_reset_tpm_established_flag(TPMBackend *tb,
160 uint8_t locty)
161{
162 /* only a TPM 2.0 will support this */
163 return 0;
164}
165
4549a8b7
SB
166static void tpm_passthrough_cancel_cmd(TPMBackend *tb)
167{
8f0605cc 168 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
92dcc234
SB
169 int n;
170
171 /*
172 * As of Linux 3.7 the tpm_tis driver does not properly cancel
173 * commands on all TPM manufacturers' TPMs.
174 * Only cancel if we're busy so we don't cancel someone else's
175 * command, e.g., a command executed on the host.
176 */
177 if (tpm_pt->tpm_executing) {
178 if (tpm_pt->cancel_fd >= 0) {
21cb1e63 179 tpm_pt->tpm_op_canceled = true;
92dcc234
SB
180 n = write(tpm_pt->cancel_fd, "-", 1);
181 if (n != 1) {
27215a22 182 error_report("Canceling TPM command failed: %s",
92dcc234 183 strerror(errno));
92dcc234
SB
184 }
185 } else {
186 error_report("Cannot cancel TPM command due to missing "
187 "TPM sysfs cancel entry");
188 }
189 }
4549a8b7
SB
190}
191
116694c3
SB
192static TPMVersion tpm_passthrough_get_tpm_version(TPMBackend *tb)
193{
56a3c24f 194 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
4549a8b7 195
56a3c24f 196 return tpm_pt->tpm_version;
4549a8b7
SB
197}
198
b21e6aaf
SB
199static size_t tpm_passthrough_get_buffer_size(TPMBackend *tb)
200{
abc5cda0
SB
201 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
202 int ret;
203
204 ret = tpm_util_get_buffer_size(tpm_pt->tpm_fd, tpm_pt->tpm_version,
205 &tpm_pt->tpm_buffersize);
206 if (ret < 0) {
207 tpm_pt->tpm_buffersize = 4096;
208 }
209 return tpm_pt->tpm_buffersize;
b21e6aaf
SB
210}
211
92dcc234
SB
212/*
213 * Unless path or file descriptor set has been provided by user,
214 * determine the sysfs cancel file following kernel documentation
215 * in Documentation/ABI/stable/sysfs-class-tpm.
05b71fb2
MAL
216 * From /dev/tpm0 create /sys/class/tpm/tpm0/device/cancel
217 * before 4.0: /sys/class/misc/tpm0/device/cancel
92dcc234 218 */
f59864ba 219static int tpm_passthrough_open_sysfs_cancel(TPMPassthruState *tpm_pt)
92dcc234
SB
220{
221 int fd = -1;
8e36d6ca 222 char *dev;
92dcc234 223 char path[PATH_MAX];
92dcc234 224
f59864ba
AV
225 if (tpm_pt->options->cancel_path) {
226 fd = qemu_open(tpm_pt->options->cancel_path, O_WRONLY);
92dcc234 227 if (fd < 0) {
05b71fb2 228 error_report("tpm_passthrough: Could not open TPM cancel path: %s",
92dcc234
SB
229 strerror(errno));
230 }
231 return fd;
232 }
233
8e36d6ca 234 dev = strrchr(tpm_pt->tpm_dev, '/');
05b71fb2
MAL
235 if (!dev) {
236 error_report("tpm_passthrough: Bad TPM device path %s",
237 tpm_pt->tpm_dev);
238 return -1;
239 }
240
241 dev++;
242 if (snprintf(path, sizeof(path), "/sys/class/tpm/%s/device/cancel",
243 dev) < sizeof(path)) {
244 fd = qemu_open(path, O_WRONLY);
245 if (fd < 0) {
246 if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel",
247 dev) < sizeof(path)) {
248 fd = qemu_open(path, O_WRONLY);
8e36d6ca 249 }
92dcc234 250 }
05b71fb2
MAL
251 }
252
253 if (fd < 0) {
254 error_report("tpm_passthrough: Could not guess TPM cancel path");
8e36d6ca 255 } else {
05b71fb2 256 tpm_pt->options->cancel_path = g_strdup(path);
92dcc234
SB
257 }
258
259 return fd;
260}
261
803de211
MAL
262static int
263tpm_passthrough_handle_device_opts(TPMPassthruState *tpm_pt, QemuOpts *opts)
4549a8b7
SB
264{
265 const char *value;
266
92dcc234 267 value = qemu_opt_get(opts, "cancel-path");
f59864ba
AV
268 if (value) {
269 tpm_pt->options->cancel_path = g_strdup(value);
270 tpm_pt->options->has_cancel_path = true;
271 }
92dcc234 272
4549a8b7 273 value = qemu_opt_get(opts, "path");
f59864ba
AV
274 if (value) {
275 tpm_pt->options->has_path = true;
276 tpm_pt->options->path = g_strdup(value);
4549a8b7
SB
277 }
278
f59864ba 279 tpm_pt->tpm_dev = value ? value : TPM_PASSTHROUGH_DEFAULT_DEVICE;
8f0605cc
SB
280 tpm_pt->tpm_fd = qemu_open(tpm_pt->tpm_dev, O_RDWR);
281 if (tpm_pt->tpm_fd < 0) {
27215a22 282 error_report("Cannot access TPM device using '%s': %s",
8f0605cc 283 tpm_pt->tpm_dev, strerror(errno));
bef2ed3f 284 return -1;
4549a8b7
SB
285 }
286
56a3c24f 287 if (tpm_util_test_tpmdev(tpm_pt->tpm_fd, &tpm_pt->tpm_version)) {
27215a22 288 error_report("'%s' is not a TPM device.",
8f0605cc 289 tpm_pt->tpm_dev);
bef2ed3f 290 return -1;
4549a8b7
SB
291 }
292
bef2ed3f
MAL
293 tpm_pt->cancel_fd = tpm_passthrough_open_sysfs_cancel(tpm_pt);
294 if (tpm_pt->cancel_fd < 0) {
295 return -1;
296 }
4549a8b7 297
bef2ed3f 298 return 0;
4549a8b7
SB
299}
300
9f7c0ef2 301static TPMBackend *tpm_passthrough_create(QemuOpts *opts)
4549a8b7 302{
8f0605cc 303 Object *obj = object_new(TYPE_TPM_PASSTHROUGH);
4549a8b7 304
bef2ed3f
MAL
305 if (tpm_passthrough_handle_device_opts(TPM_PASSTHROUGH(obj), opts)) {
306 object_unref(obj);
307 return NULL;
92dcc234
SB
308 }
309
9f7c0ef2 310 return TPM_BACKEND(obj);
4549a8b7
SB
311}
312
683c4b77
SB
313static int tpm_passthrough_startup_tpm(TPMBackend *tb, size_t buffersize)
314{
315 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
316
317 if (buffersize && buffersize < tpm_pt->tpm_buffersize) {
318 error_report("Requested buffer size of %zu is smaller than host TPM's "
319 "fixed buffer size of %zu",
320 buffersize, tpm_pt->tpm_buffersize);
321 return -1;
322 }
323
324 return 0;
325}
326
f59864ba
AV
327static TpmTypeOptions *tpm_passthrough_get_tpm_options(TPMBackend *tb)
328{
329 TpmTypeOptions *options = g_new0(TpmTypeOptions, 1);
330
331 options->type = TPM_TYPE_OPTIONS_KIND_PASSTHROUGH;
332 options->u.passthrough.data = QAPI_CLONE(TPMPassthroughOptions,
333 TPM_PASSTHROUGH(tb)->options);
334
335 return options;
336}
337
bb716238
SB
338static const QemuOptDesc tpm_passthrough_cmdline_opts[] = {
339 TPM_STANDARD_CMDLINE_OPTS,
340 {
341 .name = "cancel-path",
342 .type = QEMU_OPT_STRING,
343 .help = "Sysfs file entry for canceling TPM commands",
344 },
345 {
346 .name = "path",
347 .type = QEMU_OPT_STRING,
348 .help = "Path to TPM device on the host",
349 },
350 { /* end of list */ },
351};
352
8f0605cc
SB
353static void tpm_passthrough_inst_init(Object *obj)
354{
f35fe5cb
AV
355 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(obj);
356
f59864ba 357 tpm_pt->options = g_new0(TPMPassthroughOptions, 1);
f35fe5cb
AV
358 tpm_pt->tpm_fd = -1;
359 tpm_pt->cancel_fd = -1;
8f0605cc
SB
360}
361
362static void tpm_passthrough_inst_finalize(Object *obj)
363{
f35fe5cb
AV
364 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(obj);
365
366 tpm_passthrough_cancel_cmd(TPM_BACKEND(obj));
367
8df4d848
MAL
368 if (tpm_pt->tpm_fd >= 0) {
369 qemu_close(tpm_pt->tpm_fd);
370 }
371 if (tpm_pt->cancel_fd >= 0) {
372 qemu_close(tpm_pt->cancel_fd);
373 }
f59864ba 374 qapi_free_TPMPassthroughOptions(tpm_pt->options);
8f0605cc
SB
375}
376
377static void tpm_passthrough_class_init(ObjectClass *klass, void *data)
378{
379 TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
380
d31076ba
MAL
381 tbc->type = TPM_TYPE_PASSTHROUGH;
382 tbc->opts = tpm_passthrough_cmdline_opts;
383 tbc->desc = "Passthrough TPM backend driver";
384 tbc->create = tpm_passthrough_create;
683c4b77 385 tbc->startup_tpm = tpm_passthrough_startup_tpm;
d31076ba
MAL
386 tbc->reset = tpm_passthrough_reset;
387 tbc->cancel_cmd = tpm_passthrough_cancel_cmd;
388 tbc->get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag;
389 tbc->reset_tpm_established_flag =
390 tpm_passthrough_reset_tpm_established_flag;
391 tbc->get_tpm_version = tpm_passthrough_get_tpm_version;
b21e6aaf 392 tbc->get_buffer_size = tpm_passthrough_get_buffer_size;
d31076ba 393 tbc->get_tpm_options = tpm_passthrough_get_tpm_options;
b19a5eea 394 tbc->handle_request = tpm_passthrough_handle_request;
8f0605cc
SB
395}
396
397static const TypeInfo tpm_passthrough_info = {
398 .name = TYPE_TPM_PASSTHROUGH,
399 .parent = TYPE_TPM_BACKEND,
400 .instance_size = sizeof(TPMPassthruState),
401 .class_init = tpm_passthrough_class_init,
402 .instance_init = tpm_passthrough_inst_init,
403 .instance_finalize = tpm_passthrough_inst_finalize,
404};
405
4549a8b7
SB
406static void tpm_passthrough_register(void)
407{
8f0605cc 408 type_register_static(&tpm_passthrough_info);
4549a8b7
SB
409}
410
411type_init(tpm_passthrough_register)