target/arm: Move CPU state dumping routines to cpu.c
[qemu/ar7.git] / hw / tpm / tpm_emulator.c
blob1288cbcb8da0702de9cd283f1af17d84b5fc6b78
1 /*
2 * Emulator TPM driver
4 * Copyright (c) 2017 Intel Corporation
5 * Author: Amarnath Valluri <amarnath.valluri@intel.com>
7 * Copyright (c) 2010 - 2013, 2018 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/module.h"
32 #include "qemu/sockets.h"
33 #include "io/channel-socket.h"
34 #include "sysemu/tpm_backend.h"
35 #include "tpm_int.h"
36 #include "hw/hw.h"
37 #include "tpm_util.h"
38 #include "tpm_ioctl.h"
39 #include "migration/blocker.h"
40 #include "qapi/error.h"
41 #include "qapi/clone-visitor.h"
42 #include "qapi/qapi-visit-tpm.h"
43 #include "chardev/char-fe.h"
44 #include "trace.h"
46 #define TYPE_TPM_EMULATOR "tpm-emulator"
47 #define TPM_EMULATOR(obj) \
48 OBJECT_CHECK(TPMEmulator, (obj), TYPE_TPM_EMULATOR)
50 #define TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(S, cap) (((S)->caps & (cap)) == (cap))
52 /* data structures */
54 /* blobs from the TPM; part of VM state when migrating */
55 typedef struct TPMBlobBuffers {
56 uint32_t permanent_flags;
57 TPMSizedBuffer permanent;
59 uint32_t volatil_flags;
60 TPMSizedBuffer volatil;
62 uint32_t savestate_flags;
63 TPMSizedBuffer savestate;
64 } TPMBlobBuffers;
66 typedef struct TPMEmulator {
67 TPMBackend parent;
69 TPMEmulatorOptions *options;
70 CharBackend ctrl_chr;
71 QIOChannel *data_ioc;
72 TPMVersion tpm_version;
73 ptm_cap caps; /* capabilities of the TPM */
74 uint8_t cur_locty_number; /* last set locality */
75 Error *migration_blocker;
77 QemuMutex mutex;
79 unsigned int established_flag:1;
80 unsigned int established_flag_cached:1;
82 TPMBlobBuffers state_blobs;
83 } TPMEmulator;
86 static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
87 size_t msg_len_in, size_t msg_len_out)
89 CharBackend *dev = &tpm->ctrl_chr;
90 uint32_t cmd_no = cpu_to_be32(cmd);
91 ssize_t n = sizeof(uint32_t) + msg_len_in;
92 uint8_t *buf = NULL;
93 int ret = -1;
95 qemu_mutex_lock(&tpm->mutex);
97 buf = g_alloca(n);
98 memcpy(buf, &cmd_no, sizeof(cmd_no));
99 memcpy(buf + sizeof(cmd_no), msg, msg_len_in);
101 n = qemu_chr_fe_write_all(dev, buf, n);
102 if (n <= 0) {
103 goto end;
106 if (msg_len_out != 0) {
107 n = qemu_chr_fe_read_all(dev, msg, msg_len_out);
108 if (n <= 0) {
109 goto end;
113 ret = 0;
115 end:
116 qemu_mutex_unlock(&tpm->mutex);
117 return ret;
120 static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,
121 const uint8_t *in, uint32_t in_len,
122 uint8_t *out, uint32_t out_len,
123 bool *selftest_done,
124 Error **err)
126 ssize_t ret;
127 bool is_selftest = false;
129 if (selftest_done) {
130 *selftest_done = false;
131 is_selftest = tpm_util_is_selftest(in, in_len);
134 ret = qio_channel_write_all(tpm_emu->data_ioc, (char *)in, in_len, err);
135 if (ret != 0) {
136 return -1;
139 ret = qio_channel_read_all(tpm_emu->data_ioc, (char *)out,
140 sizeof(struct tpm_resp_hdr), err);
141 if (ret != 0) {
142 return -1;
145 ret = qio_channel_read_all(tpm_emu->data_ioc,
146 (char *)out + sizeof(struct tpm_resp_hdr),
147 tpm_cmd_get_size(out) - sizeof(struct tpm_resp_hdr), err);
148 if (ret != 0) {
149 return -1;
152 if (is_selftest) {
153 *selftest_done = tpm_cmd_get_errcode(out) == 0;
156 return 0;
159 static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number,
160 Error **errp)
162 ptm_loc loc;
164 if (tpm_emu->cur_locty_number == locty_number) {
165 return 0;
168 trace_tpm_emulator_set_locality(locty_number);
170 memset(&loc, 0, sizeof(loc));
171 loc.u.req.loc = locty_number;
172 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_LOCALITY, &loc,
173 sizeof(loc), sizeof(loc)) < 0) {
174 error_setg(errp, "tpm-emulator: could not set locality : %s",
175 strerror(errno));
176 return -1;
179 loc.u.resp.tpm_result = be32_to_cpu(loc.u.resp.tpm_result);
180 if (loc.u.resp.tpm_result != 0) {
181 error_setg(errp, "tpm-emulator: TPM result for set locality : 0x%x",
182 loc.u.resp.tpm_result);
183 return -1;
186 tpm_emu->cur_locty_number = locty_number;
188 return 0;
191 static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd *cmd,
192 Error **errp)
194 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
196 trace_tpm_emulator_handle_request();
198 if (tpm_emulator_set_locality(tpm_emu, cmd->locty, errp) < 0 ||
199 tpm_emulator_unix_tx_bufs(tpm_emu, cmd->in, cmd->in_len,
200 cmd->out, cmd->out_len,
201 &cmd->selftest_done, errp) < 0) {
202 tpm_util_write_fatal_error_response(cmd->out, cmd->out_len);
206 static int tpm_emulator_probe_caps(TPMEmulator *tpm_emu)
208 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_CAPABILITY,
209 &tpm_emu->caps, 0, sizeof(tpm_emu->caps)) < 0) {
210 error_report("tpm-emulator: probing failed : %s", strerror(errno));
211 return -1;
214 tpm_emu->caps = be64_to_cpu(tpm_emu->caps);
216 trace_tpm_emulator_probe_caps(tpm_emu->caps);
218 return 0;
221 static int tpm_emulator_check_caps(TPMEmulator *tpm_emu)
223 ptm_cap caps = 0;
224 const char *tpm = NULL;
226 /* check for min. required capabilities */
227 switch (tpm_emu->tpm_version) {
228 case TPM_VERSION_1_2:
229 caps = PTM_CAP_INIT | PTM_CAP_SHUTDOWN | PTM_CAP_GET_TPMESTABLISHED |
230 PTM_CAP_SET_LOCALITY | PTM_CAP_SET_DATAFD | PTM_CAP_STOP |
231 PTM_CAP_SET_BUFFERSIZE;
232 tpm = "1.2";
233 break;
234 case TPM_VERSION_2_0:
235 caps = PTM_CAP_INIT | PTM_CAP_SHUTDOWN | PTM_CAP_GET_TPMESTABLISHED |
236 PTM_CAP_SET_LOCALITY | PTM_CAP_RESET_TPMESTABLISHED |
237 PTM_CAP_SET_DATAFD | PTM_CAP_STOP | PTM_CAP_SET_BUFFERSIZE;
238 tpm = "2";
239 break;
240 case TPM_VERSION_UNSPEC:
241 error_report("tpm-emulator: TPM version has not been set");
242 return -1;
245 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu, caps)) {
246 error_report("tpm-emulator: TPM does not implement minimum set of "
247 "required capabilities for TPM %s (0x%x)", tpm, (int)caps);
248 return -1;
251 return 0;
254 static int tpm_emulator_stop_tpm(TPMBackend *tb)
256 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
257 ptm_res res;
259 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_STOP, &res, 0, sizeof(res)) < 0) {
260 error_report("tpm-emulator: Could not stop TPM: %s",
261 strerror(errno));
262 return -1;
265 res = be32_to_cpu(res);
266 if (res) {
267 error_report("tpm-emulator: TPM result for CMD_STOP: 0x%x", res);
268 return -1;
271 return 0;
274 static int tpm_emulator_set_buffer_size(TPMBackend *tb,
275 size_t wanted_size,
276 size_t *actual_size)
278 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
279 ptm_setbuffersize psbs;
281 if (tpm_emulator_stop_tpm(tb) < 0) {
282 return -1;
285 psbs.u.req.buffersize = cpu_to_be32(wanted_size);
287 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_BUFFERSIZE, &psbs,
288 sizeof(psbs.u.req), sizeof(psbs.u.resp)) < 0) {
289 error_report("tpm-emulator: Could not set buffer size: %s",
290 strerror(errno));
291 return -1;
294 psbs.u.resp.tpm_result = be32_to_cpu(psbs.u.resp.tpm_result);
295 if (psbs.u.resp.tpm_result != 0) {
296 error_report("tpm-emulator: TPM result for set buffer size : 0x%x",
297 psbs.u.resp.tpm_result);
298 return -1;
301 if (actual_size) {
302 *actual_size = be32_to_cpu(psbs.u.resp.buffersize);
305 trace_tpm_emulator_set_buffer_size(
306 be32_to_cpu(psbs.u.resp.buffersize),
307 be32_to_cpu(psbs.u.resp.minsize),
308 be32_to_cpu(psbs.u.resp.maxsize));
310 return 0;
313 static int tpm_emulator_startup_tpm_resume(TPMBackend *tb, size_t buffersize,
314 bool is_resume)
316 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
317 ptm_init init = {
318 .u.req.init_flags = 0,
320 ptm_res res;
322 trace_tpm_emulator_startup_tpm_resume(is_resume, buffersize);
324 if (buffersize != 0 &&
325 tpm_emulator_set_buffer_size(tb, buffersize, NULL) < 0) {
326 goto err_exit;
329 if (is_resume) {
330 init.u.req.init_flags |= cpu_to_be32(PTM_INIT_FLAG_DELETE_VOLATILE);
333 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_INIT, &init, sizeof(init),
334 sizeof(init)) < 0) {
335 error_report("tpm-emulator: could not send INIT: %s",
336 strerror(errno));
337 goto err_exit;
340 res = be32_to_cpu(init.u.resp.tpm_result);
341 if (res) {
342 error_report("tpm-emulator: TPM result for CMD_INIT: 0x%x", res);
343 goto err_exit;
345 return 0;
347 err_exit:
348 return -1;
351 static int tpm_emulator_startup_tpm(TPMBackend *tb, size_t buffersize)
353 return tpm_emulator_startup_tpm_resume(tb, buffersize, false);
356 static bool tpm_emulator_get_tpm_established_flag(TPMBackend *tb)
358 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
359 ptm_est est;
361 if (tpm_emu->established_flag_cached) {
362 return tpm_emu->established_flag;
365 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_TPMESTABLISHED, &est,
366 0, sizeof(est)) < 0) {
367 error_report("tpm-emulator: Could not get the TPM established flag: %s",
368 strerror(errno));
369 return false;
371 trace_tpm_emulator_get_tpm_established_flag(est.u.resp.bit);
373 tpm_emu->established_flag_cached = 1;
374 tpm_emu->established_flag = (est.u.resp.bit != 0);
376 return tpm_emu->established_flag;
379 static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb,
380 uint8_t locty)
382 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
383 ptm_reset_est reset_est;
384 ptm_res res;
386 /* only a TPM 2.0 will support this */
387 if (tpm_emu->tpm_version != TPM_VERSION_2_0) {
388 return 0;
391 reset_est.u.req.loc = tpm_emu->cur_locty_number;
392 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_RESET_TPMESTABLISHED,
393 &reset_est, sizeof(reset_est),
394 sizeof(reset_est)) < 0) {
395 error_report("tpm-emulator: Could not reset the establishment bit: %s",
396 strerror(errno));
397 return -1;
400 res = be32_to_cpu(reset_est.u.resp.tpm_result);
401 if (res) {
402 error_report("tpm-emulator: TPM result for rest establixhed flag: 0x%x",
403 res);
404 return -1;
407 tpm_emu->established_flag_cached = 0;
409 return 0;
412 static void tpm_emulator_cancel_cmd(TPMBackend *tb)
414 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
415 ptm_res res;
417 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu, PTM_CAP_CANCEL_TPM_CMD)) {
418 trace_tpm_emulator_cancel_cmd_not_supt();
419 return;
422 /* FIXME: make the function non-blocking, or it may block a VCPU */
423 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_CANCEL_TPM_CMD, &res, 0,
424 sizeof(res)) < 0) {
425 error_report("tpm-emulator: Could not cancel command: %s",
426 strerror(errno));
427 } else if (res != 0) {
428 error_report("tpm-emulator: Failed to cancel TPM: 0x%x",
429 be32_to_cpu(res));
433 static TPMVersion tpm_emulator_get_tpm_version(TPMBackend *tb)
435 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
437 return tpm_emu->tpm_version;
440 static size_t tpm_emulator_get_buffer_size(TPMBackend *tb)
442 size_t actual_size;
444 if (tpm_emulator_set_buffer_size(tb, 0, &actual_size) < 0) {
445 return 4096;
448 return actual_size;
451 static int tpm_emulator_block_migration(TPMEmulator *tpm_emu)
453 Error *err = NULL;
454 ptm_cap caps = PTM_CAP_GET_STATEBLOB | PTM_CAP_SET_STATEBLOB |
455 PTM_CAP_STOP;
457 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu, caps)) {
458 error_setg(&tpm_emu->migration_blocker,
459 "Migration disabled: TPM emulator does not support "
460 "migration");
461 migrate_add_blocker(tpm_emu->migration_blocker, &err);
462 if (err) {
463 error_report_err(err);
464 error_free(tpm_emu->migration_blocker);
465 tpm_emu->migration_blocker = NULL;
467 return -1;
471 return 0;
474 static int tpm_emulator_prepare_data_fd(TPMEmulator *tpm_emu)
476 ptm_res res;
477 Error *err = NULL;
478 int fds[2] = { -1, -1 };
480 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
481 error_report("tpm-emulator: Failed to create socketpair");
482 return -1;
485 qemu_chr_fe_set_msgfds(&tpm_emu->ctrl_chr, fds + 1, 1);
487 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_DATAFD, &res, 0,
488 sizeof(res)) < 0 || res != 0) {
489 error_report("tpm-emulator: Failed to send CMD_SET_DATAFD: %s",
490 strerror(errno));
491 goto err_exit;
494 tpm_emu->data_ioc = QIO_CHANNEL(qio_channel_socket_new_fd(fds[0], &err));
495 if (err) {
496 error_prepend(&err, "tpm-emulator: Failed to create io channel: ");
497 error_report_err(err);
498 goto err_exit;
501 closesocket(fds[1]);
503 return 0;
505 err_exit:
506 closesocket(fds[0]);
507 closesocket(fds[1]);
508 return -1;
511 static int tpm_emulator_handle_device_opts(TPMEmulator *tpm_emu, QemuOpts *opts)
513 const char *value;
515 value = qemu_opt_get(opts, "chardev");
516 if (value) {
517 Error *err = NULL;
518 Chardev *dev = qemu_chr_find(value);
520 if (!dev) {
521 error_report("tpm-emulator: tpm chardev '%s' not found.", value);
522 goto err;
525 if (!qemu_chr_fe_init(&tpm_emu->ctrl_chr, dev, &err)) {
526 error_prepend(&err, "tpm-emulator: No valid chardev found at '%s':",
527 value);
528 error_report_err(err);
529 goto err;
532 tpm_emu->options->chardev = g_strdup(value);
535 if (tpm_emulator_prepare_data_fd(tpm_emu) < 0) {
536 goto err;
539 /* FIXME: tpm_util_test_tpmdev() accepts only on socket fd, as it also used
540 * by passthrough driver, which not yet using GIOChannel.
542 if (tpm_util_test_tpmdev(QIO_CHANNEL_SOCKET(tpm_emu->data_ioc)->fd,
543 &tpm_emu->tpm_version)) {
544 error_report("'%s' is not emulating TPM device. Error: %s",
545 tpm_emu->options->chardev, strerror(errno));
546 goto err;
549 switch (tpm_emu->tpm_version) {
550 case TPM_VERSION_1_2:
551 trace_tpm_emulator_handle_device_opts_tpm12();
552 break;
553 case TPM_VERSION_2_0:
554 trace_tpm_emulator_handle_device_opts_tpm2();
555 break;
556 default:
557 trace_tpm_emulator_handle_device_opts_unspec();
560 if (tpm_emulator_probe_caps(tpm_emu) ||
561 tpm_emulator_check_caps(tpm_emu)) {
562 goto err;
565 return tpm_emulator_block_migration(tpm_emu);
567 err:
568 trace_tpm_emulator_handle_device_opts_startup_error();
570 return -1;
573 static TPMBackend *tpm_emulator_create(QemuOpts *opts)
575 TPMBackend *tb = TPM_BACKEND(object_new(TYPE_TPM_EMULATOR));
577 if (tpm_emulator_handle_device_opts(TPM_EMULATOR(tb), opts)) {
578 object_unref(OBJECT(tb));
579 return NULL;
582 return tb;
585 static TpmTypeOptions *tpm_emulator_get_tpm_options(TPMBackend *tb)
587 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
588 TpmTypeOptions *options = g_new0(TpmTypeOptions, 1);
590 options->type = TPM_TYPE_OPTIONS_KIND_EMULATOR;
591 options->u.emulator.data = QAPI_CLONE(TPMEmulatorOptions, tpm_emu->options);
593 return options;
596 static const QemuOptDesc tpm_emulator_cmdline_opts[] = {
597 TPM_STANDARD_CMDLINE_OPTS,
599 .name = "chardev",
600 .type = QEMU_OPT_STRING,
601 .help = "Character device to use for out-of-band control messages",
603 { /* end of list */ },
607 * Transfer a TPM state blob from the TPM into a provided buffer.
609 * @tpm_emu: TPMEmulator
610 * @type: the type of blob to transfer
611 * @tsb: the TPMSizeBuffer to fill with the blob
612 * @flags: the flags to return to the caller
614 static int tpm_emulator_get_state_blob(TPMEmulator *tpm_emu,
615 uint8_t type,
616 TPMSizedBuffer *tsb,
617 uint32_t *flags)
619 ptm_getstate pgs;
620 ptm_res res;
621 ssize_t n;
622 uint32_t totlength, length;
624 tpm_sized_buffer_reset(tsb);
626 pgs.u.req.state_flags = cpu_to_be32(PTM_STATE_FLAG_DECRYPTED);
627 pgs.u.req.type = cpu_to_be32(type);
628 pgs.u.req.offset = 0;
630 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_STATEBLOB,
631 &pgs, sizeof(pgs.u.req),
632 offsetof(ptm_getstate, u.resp.data)) < 0) {
633 error_report("tpm-emulator: could not get state blob type %d : %s",
634 type, strerror(errno));
635 return -1;
638 res = be32_to_cpu(pgs.u.resp.tpm_result);
639 if (res != 0 && (res & 0x800) == 0) {
640 error_report("tpm-emulator: Getting the stateblob (type %d) failed "
641 "with a TPM error 0x%x", type, res);
642 return -1;
645 totlength = be32_to_cpu(pgs.u.resp.totlength);
646 length = be32_to_cpu(pgs.u.resp.length);
647 if (totlength != length) {
648 error_report("tpm-emulator: Expecting to read %u bytes "
649 "but would get %u", totlength, length);
650 return -1;
653 *flags = be32_to_cpu(pgs.u.resp.state_flags);
655 if (totlength > 0) {
656 tsb->buffer = g_try_malloc(totlength);
657 if (!tsb->buffer) {
658 error_report("tpm-emulator: Out of memory allocating %u bytes",
659 totlength);
660 return -1;
663 n = qemu_chr_fe_read_all(&tpm_emu->ctrl_chr, tsb->buffer, totlength);
664 if (n != totlength) {
665 error_report("tpm-emulator: Could not read stateblob (type %d); "
666 "expected %u bytes, got %zd",
667 type, totlength, n);
668 return -1;
671 tsb->size = totlength;
673 trace_tpm_emulator_get_state_blob(type, tsb->size, *flags);
675 return 0;
678 static int tpm_emulator_get_state_blobs(TPMEmulator *tpm_emu)
680 TPMBlobBuffers *state_blobs = &tpm_emu->state_blobs;
682 if (tpm_emulator_get_state_blob(tpm_emu, PTM_BLOB_TYPE_PERMANENT,
683 &state_blobs->permanent,
684 &state_blobs->permanent_flags) < 0 ||
685 tpm_emulator_get_state_blob(tpm_emu, PTM_BLOB_TYPE_VOLATILE,
686 &state_blobs->volatil,
687 &state_blobs->volatil_flags) < 0 ||
688 tpm_emulator_get_state_blob(tpm_emu, PTM_BLOB_TYPE_SAVESTATE,
689 &state_blobs->savestate,
690 &state_blobs->savestate_flags) < 0) {
691 goto err_exit;
694 return 0;
696 err_exit:
697 tpm_sized_buffer_reset(&state_blobs->volatil);
698 tpm_sized_buffer_reset(&state_blobs->permanent);
699 tpm_sized_buffer_reset(&state_blobs->savestate);
701 return -1;
705 * Transfer a TPM state blob to the TPM emulator.
707 * @tpm_emu: TPMEmulator
708 * @type: the type of TPM state blob to transfer
709 * @tsb: TPMSizedBuffer containing the TPM state blob
710 * @flags: Flags describing the (encryption) state of the TPM state blob
712 static int tpm_emulator_set_state_blob(TPMEmulator *tpm_emu,
713 uint32_t type,
714 TPMSizedBuffer *tsb,
715 uint32_t flags)
717 ssize_t n;
718 ptm_setstate pss;
719 ptm_res tpm_result;
721 if (tsb->size == 0) {
722 return 0;
725 pss = (ptm_setstate) {
726 .u.req.state_flags = cpu_to_be32(flags),
727 .u.req.type = cpu_to_be32(type),
728 .u.req.length = cpu_to_be32(tsb->size),
731 /* write the header only */
732 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_STATEBLOB, &pss,
733 offsetof(ptm_setstate, u.req.data), 0) < 0) {
734 error_report("tpm-emulator: could not set state blob type %d : %s",
735 type, strerror(errno));
736 return -1;
739 /* now the body */
740 n = qemu_chr_fe_write_all(&tpm_emu->ctrl_chr, tsb->buffer, tsb->size);
741 if (n != tsb->size) {
742 error_report("tpm-emulator: Writing the stateblob (type %d) "
743 "failed; could not write %u bytes, but only %zd",
744 type, tsb->size, n);
745 return -1;
748 /* now get the result */
749 n = qemu_chr_fe_read_all(&tpm_emu->ctrl_chr,
750 (uint8_t *)&pss, sizeof(pss.u.resp));
751 if (n != sizeof(pss.u.resp)) {
752 error_report("tpm-emulator: Reading response from writing stateblob "
753 "(type %d) failed; expected %zu bytes, got %zd", type,
754 sizeof(pss.u.resp), n);
755 return -1;
758 tpm_result = be32_to_cpu(pss.u.resp.tpm_result);
759 if (tpm_result != 0) {
760 error_report("tpm-emulator: Setting the stateblob (type %d) failed "
761 "with a TPM error 0x%x", type, tpm_result);
762 return -1;
765 trace_tpm_emulator_set_state_blob(type, tsb->size, flags);
767 return 0;
771 * Set all the TPM state blobs.
773 * Returns a negative errno code in case of error.
775 static int tpm_emulator_set_state_blobs(TPMBackend *tb)
777 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
778 TPMBlobBuffers *state_blobs = &tpm_emu->state_blobs;
780 trace_tpm_emulator_set_state_blobs();
782 if (tpm_emulator_stop_tpm(tb) < 0) {
783 trace_tpm_emulator_set_state_blobs_error("Could not stop TPM");
784 return -EIO;
787 if (tpm_emulator_set_state_blob(tpm_emu, PTM_BLOB_TYPE_PERMANENT,
788 &state_blobs->permanent,
789 state_blobs->permanent_flags) < 0 ||
790 tpm_emulator_set_state_blob(tpm_emu, PTM_BLOB_TYPE_VOLATILE,
791 &state_blobs->volatil,
792 state_blobs->volatil_flags) < 0 ||
793 tpm_emulator_set_state_blob(tpm_emu, PTM_BLOB_TYPE_SAVESTATE,
794 &state_blobs->savestate,
795 state_blobs->savestate_flags) < 0) {
796 return -EIO;
799 trace_tpm_emulator_set_state_blobs_done();
801 return 0;
804 static int tpm_emulator_pre_save(void *opaque)
806 TPMBackend *tb = opaque;
807 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
809 trace_tpm_emulator_pre_save();
811 tpm_backend_finish_sync(tb);
813 /* get the state blobs from the TPM */
814 return tpm_emulator_get_state_blobs(tpm_emu);
818 * Load the TPM state blobs into the TPM.
820 * Returns negative errno codes in case of error.
822 static int tpm_emulator_post_load(void *opaque, int version_id)
824 TPMBackend *tb = opaque;
825 int ret;
827 ret = tpm_emulator_set_state_blobs(tb);
828 if (ret < 0) {
829 return ret;
832 if (tpm_emulator_startup_tpm_resume(tb, 0, true) < 0) {
833 return -EIO;
836 return 0;
839 static const VMStateDescription vmstate_tpm_emulator = {
840 .name = "tpm-emulator",
841 .version_id = 0,
842 .pre_save = tpm_emulator_pre_save,
843 .post_load = tpm_emulator_post_load,
844 .fields = (VMStateField[]) {
845 VMSTATE_UINT32(state_blobs.permanent_flags, TPMEmulator),
846 VMSTATE_UINT32(state_blobs.permanent.size, TPMEmulator),
847 VMSTATE_VBUFFER_ALLOC_UINT32(state_blobs.permanent.buffer,
848 TPMEmulator, 0, 0,
849 state_blobs.permanent.size),
851 VMSTATE_UINT32(state_blobs.volatil_flags, TPMEmulator),
852 VMSTATE_UINT32(state_blobs.volatil.size, TPMEmulator),
853 VMSTATE_VBUFFER_ALLOC_UINT32(state_blobs.volatil.buffer,
854 TPMEmulator, 0, 0,
855 state_blobs.volatil.size),
857 VMSTATE_UINT32(state_blobs.savestate_flags, TPMEmulator),
858 VMSTATE_UINT32(state_blobs.savestate.size, TPMEmulator),
859 VMSTATE_VBUFFER_ALLOC_UINT32(state_blobs.savestate.buffer,
860 TPMEmulator, 0, 0,
861 state_blobs.savestate.size),
863 VMSTATE_END_OF_LIST()
867 static void tpm_emulator_inst_init(Object *obj)
869 TPMEmulator *tpm_emu = TPM_EMULATOR(obj);
871 trace_tpm_emulator_inst_init();
873 tpm_emu->options = g_new0(TPMEmulatorOptions, 1);
874 tpm_emu->cur_locty_number = ~0;
875 qemu_mutex_init(&tpm_emu->mutex);
877 vmstate_register(NULL, -1, &vmstate_tpm_emulator, obj);
881 * Gracefully shut down the external TPM
883 static void tpm_emulator_shutdown(TPMEmulator *tpm_emu)
885 ptm_res res;
887 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SHUTDOWN, &res, 0, sizeof(res)) < 0) {
888 error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s",
889 strerror(errno));
890 } else if (res != 0) {
891 error_report("tpm-emulator: TPM result for sutdown: 0x%x",
892 be32_to_cpu(res));
896 static void tpm_emulator_inst_finalize(Object *obj)
898 TPMEmulator *tpm_emu = TPM_EMULATOR(obj);
899 TPMBlobBuffers *state_blobs = &tpm_emu->state_blobs;
901 tpm_emulator_shutdown(tpm_emu);
903 object_unref(OBJECT(tpm_emu->data_ioc));
905 qemu_chr_fe_deinit(&tpm_emu->ctrl_chr, false);
907 qapi_free_TPMEmulatorOptions(tpm_emu->options);
909 if (tpm_emu->migration_blocker) {
910 migrate_del_blocker(tpm_emu->migration_blocker);
911 error_free(tpm_emu->migration_blocker);
914 tpm_sized_buffer_reset(&state_blobs->volatil);
915 tpm_sized_buffer_reset(&state_blobs->permanent);
916 tpm_sized_buffer_reset(&state_blobs->savestate);
918 qemu_mutex_destroy(&tpm_emu->mutex);
920 vmstate_unregister(NULL, &vmstate_tpm_emulator, obj);
923 static void tpm_emulator_class_init(ObjectClass *klass, void *data)
925 TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
927 tbc->type = TPM_TYPE_EMULATOR;
928 tbc->opts = tpm_emulator_cmdline_opts;
929 tbc->desc = "TPM emulator backend driver";
930 tbc->create = tpm_emulator_create;
931 tbc->startup_tpm = tpm_emulator_startup_tpm;
932 tbc->cancel_cmd = tpm_emulator_cancel_cmd;
933 tbc->get_tpm_established_flag = tpm_emulator_get_tpm_established_flag;
934 tbc->reset_tpm_established_flag = tpm_emulator_reset_tpm_established_flag;
935 tbc->get_tpm_version = tpm_emulator_get_tpm_version;
936 tbc->get_buffer_size = tpm_emulator_get_buffer_size;
937 tbc->get_tpm_options = tpm_emulator_get_tpm_options;
939 tbc->handle_request = tpm_emulator_handle_request;
942 static const TypeInfo tpm_emulator_info = {
943 .name = TYPE_TPM_EMULATOR,
944 .parent = TYPE_TPM_BACKEND,
945 .instance_size = sizeof(TPMEmulator),
946 .class_init = tpm_emulator_class_init,
947 .instance_init = tpm_emulator_inst_init,
948 .instance_finalize = tpm_emulator_inst_finalize,
951 static void tpm_emulator_register(void)
953 type_register_static(&tpm_emulator_info);
956 type_init(tpm_emulator_register)