tpm: Set the flags of the CMD_INIT command to 0
[qemu/ar7.git] / hw / tpm / tpm_emulator.c
blob532d3e3c52a07a35f18b64e2c8795ee1e4985281
1 /*
2 * Emulator TPM driver
4 * Copyright (c) 2017 Intel Corporation
5 * Author: Amarnath Valluri <amarnath.valluri@intel.com>
7 * Copyright (c) 2010 - 2013 IBM Corporation
8 * Authors:
9 * Stefan Berger <stefanb@us.ibm.com>
11 * Copyright (C) 2011 IAIK, Graz University of Technology
12 * Author: Andreas Niederl
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, see <http://www.gnu.org/licenses/>
29 #include "qemu/osdep.h"
30 #include "qemu/error-report.h"
31 #include "qemu/sockets.h"
32 #include "io/channel-socket.h"
33 #include "sysemu/tpm_backend.h"
34 #include "tpm_int.h"
35 #include "hw/hw.h"
36 #include "tpm_util.h"
37 #include "tpm_ioctl.h"
38 #include "migration/blocker.h"
39 #include "qapi/error.h"
40 #include "qapi/clone-visitor.h"
41 #include "chardev/char-fe.h"
43 #include <fcntl.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <stdio.h>
48 #define DEBUG_TPM 0
50 #define DPRINTF(fmt, ...) do { \
51 if (DEBUG_TPM) { \
52 fprintf(stderr, "tpm-emulator:"fmt"\n", ## __VA_ARGS__); \
53 } \
54 } while (0)
56 #define TYPE_TPM_EMULATOR "tpm-emulator"
57 #define TPM_EMULATOR(obj) \
58 OBJECT_CHECK(TPMEmulator, (obj), TYPE_TPM_EMULATOR)
60 #define TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(S, cap) (((S)->caps & (cap)) == (cap))
62 /* data structures */
63 typedef struct TPMEmulator {
64 TPMBackend parent;
66 TPMEmulatorOptions *options;
67 CharBackend ctrl_chr;
68 QIOChannel *data_ioc;
69 TPMVersion tpm_version;
70 ptm_cap caps; /* capabilities of the TPM */
71 uint8_t cur_locty_number; /* last set locality */
72 Error *migration_blocker;
74 QemuMutex mutex;
76 unsigned int established_flag:1;
77 unsigned int established_flag_cached:1;
78 } TPMEmulator;
81 static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
82 size_t msg_len_in, size_t msg_len_out)
84 CharBackend *dev = &tpm->ctrl_chr;
85 uint32_t cmd_no = cpu_to_be32(cmd);
86 ssize_t n = sizeof(uint32_t) + msg_len_in;
87 uint8_t *buf = NULL;
88 int ret = -1;
90 qemu_mutex_lock(&tpm->mutex);
92 buf = g_alloca(n);
93 memcpy(buf, &cmd_no, sizeof(cmd_no));
94 memcpy(buf + sizeof(cmd_no), msg, msg_len_in);
96 n = qemu_chr_fe_write_all(dev, buf, n);
97 if (n <= 0) {
98 goto end;
101 if (msg_len_out != 0) {
102 n = qemu_chr_fe_read_all(dev, msg, msg_len_out);
103 if (n <= 0) {
104 goto end;
108 ret = 0;
110 end:
111 qemu_mutex_unlock(&tpm->mutex);
112 return ret;
115 static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,
116 const uint8_t *in, uint32_t in_len,
117 uint8_t *out, uint32_t out_len,
118 bool *selftest_done,
119 Error **err)
121 ssize_t ret;
122 bool is_selftest = false;
123 const struct tpm_resp_hdr *hdr = NULL;
125 if (selftest_done) {
126 *selftest_done = false;
127 is_selftest = tpm_util_is_selftest(in, in_len);
130 ret = qio_channel_write_all(tpm_emu->data_ioc, (char *)in, in_len, err);
131 if (ret != 0) {
132 return -1;
135 ret = qio_channel_read_all(tpm_emu->data_ioc, (char *)out, sizeof(*hdr),
136 err);
137 if (ret != 0) {
138 return -1;
141 hdr = (struct tpm_resp_hdr *)out;
142 out += sizeof(*hdr);
143 ret = qio_channel_read_all(tpm_emu->data_ioc, (char *)out,
144 be32_to_cpu(hdr->len) - sizeof(*hdr) , err);
145 if (ret != 0) {
146 return -1;
149 if (is_selftest) {
150 *selftest_done = (be32_to_cpu(hdr->errcode) == 0);
153 return 0;
156 static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number,
157 Error **errp)
159 ptm_loc loc;
161 DPRINTF("%s : locality: 0x%x", __func__, locty_number);
163 if (tpm_emu->cur_locty_number == locty_number) {
164 return 0;
167 DPRINTF("setting locality : 0x%x", locty_number);
168 loc.u.req.loc = locty_number;
169 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_LOCALITY, &loc,
170 sizeof(loc), sizeof(loc)) < 0) {
171 error_setg(errp, "tpm-emulator: could not set locality : %s",
172 strerror(errno));
173 return -1;
176 loc.u.resp.tpm_result = be32_to_cpu(loc.u.resp.tpm_result);
177 if (loc.u.resp.tpm_result != 0) {
178 error_setg(errp, "tpm-emulator: TPM result for set locality : 0x%x",
179 loc.u.resp.tpm_result);
180 return -1;
183 tpm_emu->cur_locty_number = locty_number;
185 return 0;
188 static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd *cmd)
190 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
191 Error *err = NULL;
193 DPRINTF("processing TPM command");
195 if (tpm_emulator_set_locality(tpm_emu, cmd->locty, &err) < 0) {
196 goto error;
199 if (tpm_emulator_unix_tx_bufs(tpm_emu, cmd->in, cmd->in_len,
200 cmd->out, cmd->out_len,
201 &cmd->selftest_done, &err) < 0) {
202 goto error;
205 return;
207 error:
208 tpm_util_write_fatal_error_response(cmd->out, cmd->out_len);
209 error_report_err(err);
212 static int tpm_emulator_probe_caps(TPMEmulator *tpm_emu)
214 DPRINTF("%s", __func__);
215 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_CAPABILITY,
216 &tpm_emu->caps, 0, sizeof(tpm_emu->caps)) < 0) {
217 error_report("tpm-emulator: probing failed : %s", strerror(errno));
218 return -1;
221 tpm_emu->caps = be64_to_cpu(tpm_emu->caps);
223 DPRINTF("capabilities : 0x%"PRIx64, tpm_emu->caps);
225 return 0;
228 static int tpm_emulator_check_caps(TPMEmulator *tpm_emu)
230 ptm_cap caps = 0;
231 const char *tpm = NULL;
233 /* check for min. required capabilities */
234 switch (tpm_emu->tpm_version) {
235 case TPM_VERSION_1_2:
236 caps = PTM_CAP_INIT | PTM_CAP_SHUTDOWN | PTM_CAP_GET_TPMESTABLISHED |
237 PTM_CAP_SET_LOCALITY | PTM_CAP_SET_DATAFD | PTM_CAP_STOP |
238 PTM_CAP_SET_BUFFERSIZE;
239 tpm = "1.2";
240 break;
241 case TPM_VERSION_2_0:
242 caps = PTM_CAP_INIT | PTM_CAP_SHUTDOWN | PTM_CAP_GET_TPMESTABLISHED |
243 PTM_CAP_SET_LOCALITY | PTM_CAP_RESET_TPMESTABLISHED |
244 PTM_CAP_SET_DATAFD | PTM_CAP_STOP | PTM_CAP_SET_BUFFERSIZE;
245 tpm = "2";
246 break;
247 case TPM_VERSION_UNSPEC:
248 error_report("tpm-emulator: TPM version has not been set");
249 return -1;
252 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu, caps)) {
253 error_report("tpm-emulator: TPM does not implement minimum set of "
254 "required capabilities for TPM %s (0x%x)", tpm, (int)caps);
255 return -1;
258 return 0;
261 static int tpm_emulator_stop_tpm(TPMBackend *tb)
263 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
264 ptm_res res;
266 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_STOP, &res, 0, sizeof(res)) < 0) {
267 error_report("tpm-emulator: Could not stop TPM: %s",
268 strerror(errno));
269 return -1;
272 res = be32_to_cpu(res);
273 if (res) {
274 error_report("tpm-emulator: TPM result for CMD_STOP: 0x%x", res);
275 return -1;
278 return 0;
281 static int tpm_emulator_set_buffer_size(TPMBackend *tb,
282 size_t wanted_size,
283 size_t *actual_size)
285 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
286 ptm_setbuffersize psbs;
288 if (tpm_emulator_stop_tpm(tb) < 0) {
289 return -1;
292 psbs.u.req.buffersize = cpu_to_be32(wanted_size);
294 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_BUFFERSIZE, &psbs,
295 sizeof(psbs.u.req), sizeof(psbs.u.resp)) < 0) {
296 error_report("tpm-emulator: Could not set buffer size: %s",
297 strerror(errno));
298 return -1;
301 psbs.u.resp.tpm_result = be32_to_cpu(psbs.u.resp.tpm_result);
302 if (psbs.u.resp.tpm_result != 0) {
303 error_report("tpm-emulator: TPM result for set buffer size : 0x%x",
304 psbs.u.resp.tpm_result);
305 return -1;
308 if (actual_size) {
309 *actual_size = be32_to_cpu(psbs.u.resp.buffersize);
312 DPRINTF("buffer size: %u, min: %u, max: %u\n",
313 be32_to_cpu(psbs.u.resp.buffersize),
314 be32_to_cpu(psbs.u.resp.minsize),
315 be32_to_cpu(psbs.u.resp.maxsize));
317 return 0;
320 static int tpm_emulator_startup_tpm(TPMBackend *tb, size_t buffersize)
322 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
323 ptm_init init = {
324 .u.req.init_flags = 0,
326 ptm_res res;
328 if (buffersize != 0 &&
329 tpm_emulator_set_buffer_size(tb, buffersize, NULL) < 0) {
330 goto err_exit;
333 DPRINTF("%s", __func__);
334 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_INIT, &init, sizeof(init),
335 sizeof(init)) < 0) {
336 error_report("tpm-emulator: could not send INIT: %s",
337 strerror(errno));
338 goto err_exit;
341 res = be32_to_cpu(init.u.resp.tpm_result);
342 if (res) {
343 error_report("tpm-emulator: TPM result for CMD_INIT: 0x%x", res);
344 goto err_exit;
346 return 0;
348 err_exit:
349 return -1;
352 static bool tpm_emulator_get_tpm_established_flag(TPMBackend *tb)
354 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
355 ptm_est est;
357 if (tpm_emu->established_flag_cached) {
358 return tpm_emu->established_flag;
361 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_TPMESTABLISHED, &est,
362 0, sizeof(est)) < 0) {
363 error_report("tpm-emulator: Could not get the TPM established flag: %s",
364 strerror(errno));
365 return false;
367 DPRINTF("got established flag: %0x", est.u.resp.bit);
369 tpm_emu->established_flag_cached = 1;
370 tpm_emu->established_flag = (est.u.resp.bit != 0);
372 return tpm_emu->established_flag;
375 static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb,
376 uint8_t locty)
378 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
379 ptm_reset_est reset_est;
380 ptm_res res;
382 /* only a TPM 2.0 will support this */
383 if (tpm_emu->tpm_version != TPM_VERSION_2_0) {
384 return 0;
387 reset_est.u.req.loc = tpm_emu->cur_locty_number;
388 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_RESET_TPMESTABLISHED,
389 &reset_est, sizeof(reset_est),
390 sizeof(reset_est)) < 0) {
391 error_report("tpm-emulator: Could not reset the establishment bit: %s",
392 strerror(errno));
393 return -1;
396 res = be32_to_cpu(reset_est.u.resp.tpm_result);
397 if (res) {
398 error_report("tpm-emulator: TPM result for rest establixhed flag: 0x%x",
399 res);
400 return -1;
403 tpm_emu->established_flag_cached = 0;
405 return 0;
408 static void tpm_emulator_cancel_cmd(TPMBackend *tb)
410 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
411 ptm_res res;
413 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu, PTM_CAP_CANCEL_TPM_CMD)) {
414 DPRINTF("Backend does not support CANCEL_TPM_CMD");
415 return;
418 /* FIXME: make the function non-blocking, or it may block a VCPU */
419 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_CANCEL_TPM_CMD, &res, 0,
420 sizeof(res)) < 0) {
421 error_report("tpm-emulator: Could not cancel command: %s",
422 strerror(errno));
423 } else if (res != 0) {
424 error_report("tpm-emulator: Failed to cancel TPM: 0x%x",
425 be32_to_cpu(res));
429 static TPMVersion tpm_emulator_get_tpm_version(TPMBackend *tb)
431 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
433 return tpm_emu->tpm_version;
436 static size_t tpm_emulator_get_buffer_size(TPMBackend *tb)
438 size_t actual_size;
440 if (tpm_emulator_set_buffer_size(tb, 0, &actual_size) < 0) {
441 return 4096;
444 return actual_size;
447 static int tpm_emulator_block_migration(TPMEmulator *tpm_emu)
449 Error *err = NULL;
451 error_setg(&tpm_emu->migration_blocker,
452 "Migration disabled: TPM emulator not yet migratable");
453 migrate_add_blocker(tpm_emu->migration_blocker, &err);
454 if (err) {
455 error_report_err(err);
456 error_free(tpm_emu->migration_blocker);
457 tpm_emu->migration_blocker = NULL;
459 return -1;
462 return 0;
465 static int tpm_emulator_prepare_data_fd(TPMEmulator *tpm_emu)
467 ptm_res res;
468 Error *err = NULL;
469 int fds[2] = { -1, -1 };
471 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
472 error_report("tpm-emulator: Failed to create socketpair");
473 return -1;
476 qemu_chr_fe_set_msgfds(&tpm_emu->ctrl_chr, fds + 1, 1);
478 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_DATAFD, &res, 0,
479 sizeof(res)) < 0 || res != 0) {
480 error_report("tpm-emulator: Failed to send CMD_SET_DATAFD: %s",
481 strerror(errno));
482 goto err_exit;
485 tpm_emu->data_ioc = QIO_CHANNEL(qio_channel_socket_new_fd(fds[0], &err));
486 if (err) {
487 error_prepend(&err, "tpm-emulator: Failed to create io channel: ");
488 error_report_err(err);
489 goto err_exit;
492 closesocket(fds[1]);
494 return 0;
496 err_exit:
497 closesocket(fds[0]);
498 closesocket(fds[1]);
499 return -1;
502 static int tpm_emulator_handle_device_opts(TPMEmulator *tpm_emu, QemuOpts *opts)
504 const char *value;
506 value = qemu_opt_get(opts, "chardev");
507 if (value) {
508 Error *err = NULL;
509 Chardev *dev = qemu_chr_find(value);
511 if (!dev) {
512 error_report("tpm-emulator: tpm chardev '%s' not found.", value);
513 goto err;
516 if (!qemu_chr_fe_init(&tpm_emu->ctrl_chr, dev, &err)) {
517 error_prepend(&err, "tpm-emulator: No valid chardev found at '%s':",
518 value);
519 error_report_err(err);
520 goto err;
523 tpm_emu->options->chardev = g_strdup(value);
526 if (tpm_emulator_prepare_data_fd(tpm_emu) < 0) {
527 goto err;
530 /* FIXME: tpm_util_test_tpmdev() accepts only on socket fd, as it also used
531 * by passthrough driver, which not yet using GIOChannel.
533 if (tpm_util_test_tpmdev(QIO_CHANNEL_SOCKET(tpm_emu->data_ioc)->fd,
534 &tpm_emu->tpm_version)) {
535 error_report("'%s' is not emulating TPM device. Error: %s",
536 tpm_emu->options->chardev, strerror(errno));
537 goto err;
540 DPRINTF("TPM Version %s", tpm_emu->tpm_version == TPM_VERSION_1_2 ? "1.2" :
541 (tpm_emu->tpm_version == TPM_VERSION_2_0 ? "2.0" : "Unspecified"));
543 if (tpm_emulator_probe_caps(tpm_emu) ||
544 tpm_emulator_check_caps(tpm_emu)) {
545 goto err;
548 return tpm_emulator_block_migration(tpm_emu);
550 err:
551 DPRINTF("Startup error");
552 return -1;
555 static TPMBackend *tpm_emulator_create(QemuOpts *opts)
557 TPMBackend *tb = TPM_BACKEND(object_new(TYPE_TPM_EMULATOR));
559 if (tpm_emulator_handle_device_opts(TPM_EMULATOR(tb), opts)) {
560 object_unref(OBJECT(tb));
561 return NULL;
564 return tb;
567 static TpmTypeOptions *tpm_emulator_get_tpm_options(TPMBackend *tb)
569 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
570 TpmTypeOptions *options = g_new0(TpmTypeOptions, 1);
572 options->type = TPM_TYPE_OPTIONS_KIND_EMULATOR;
573 options->u.emulator.data = QAPI_CLONE(TPMEmulatorOptions, tpm_emu->options);
575 return options;
578 static const QemuOptDesc tpm_emulator_cmdline_opts[] = {
579 TPM_STANDARD_CMDLINE_OPTS,
581 .name = "chardev",
582 .type = QEMU_OPT_STRING,
583 .help = "Character device to use for out-of-band control messages",
585 { /* end of list */ },
588 static void tpm_emulator_inst_init(Object *obj)
590 TPMEmulator *tpm_emu = TPM_EMULATOR(obj);
592 DPRINTF("%s", __func__);
593 tpm_emu->options = g_new0(TPMEmulatorOptions, 1);
594 tpm_emu->cur_locty_number = ~0;
595 qemu_mutex_init(&tpm_emu->mutex);
599 * Gracefully shut down the external TPM
601 static void tpm_emulator_shutdown(TPMEmulator *tpm_emu)
603 ptm_res res;
605 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SHUTDOWN, &res, 0, sizeof(res)) < 0) {
606 error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s",
607 strerror(errno));
608 } else if (res != 0) {
609 error_report("tpm-emulator: TPM result for sutdown: 0x%x",
610 be32_to_cpu(res));
614 static void tpm_emulator_inst_finalize(Object *obj)
616 TPMEmulator *tpm_emu = TPM_EMULATOR(obj);
618 tpm_emulator_shutdown(tpm_emu);
620 object_unref(OBJECT(tpm_emu->data_ioc));
622 qemu_chr_fe_deinit(&tpm_emu->ctrl_chr, false);
624 qapi_free_TPMEmulatorOptions(tpm_emu->options);
626 if (tpm_emu->migration_blocker) {
627 migrate_del_blocker(tpm_emu->migration_blocker);
628 error_free(tpm_emu->migration_blocker);
631 qemu_mutex_destroy(&tpm_emu->mutex);
634 static void tpm_emulator_class_init(ObjectClass *klass, void *data)
636 TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
638 tbc->type = TPM_TYPE_EMULATOR;
639 tbc->opts = tpm_emulator_cmdline_opts;
640 tbc->desc = "TPM emulator backend driver";
641 tbc->create = tpm_emulator_create;
642 tbc->startup_tpm = tpm_emulator_startup_tpm;
643 tbc->cancel_cmd = tpm_emulator_cancel_cmd;
644 tbc->get_tpm_established_flag = tpm_emulator_get_tpm_established_flag;
645 tbc->reset_tpm_established_flag = tpm_emulator_reset_tpm_established_flag;
646 tbc->get_tpm_version = tpm_emulator_get_tpm_version;
647 tbc->get_buffer_size = tpm_emulator_get_buffer_size;
648 tbc->get_tpm_options = tpm_emulator_get_tpm_options;
650 tbc->handle_request = tpm_emulator_handle_request;
653 static const TypeInfo tpm_emulator_info = {
654 .name = TYPE_TPM_EMULATOR,
655 .parent = TYPE_TPM_BACKEND,
656 .instance_size = sizeof(TPMEmulator),
657 .class_init = tpm_emulator_class_init,
658 .instance_init = tpm_emulator_inst_init,
659 .instance_finalize = tpm_emulator_inst_finalize,
662 static void tpm_emulator_register(void)
664 type_register_static(&tpm_emulator_info);
667 type_init(tpm_emulator_register)