blob: 37c84b7c66b58404bae5de80b7e55c5d2acf3b35 [file] [log] [blame]
Stefan Berger8f0605c2013-03-28 07:26:21 -04001/*
2 * QEMU TPM Backend
3 *
4 * Copyright IBM, Corp. 2013
5 *
6 * Authors:
7 * Stefan Berger <stefanb@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 *
12 * Based on backends/rng.c by Anthony Liguori
13 */
14
Peter Maydell9c058332016-01-29 17:49:54 +000015#include "qemu/osdep.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020016#include "sysemu/tpm_backend.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010017#include "qapi/error.h"
Stefan Berger8f0605c2013-03-28 07:26:21 -040018#include "qapi/qmp/qerror.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020019#include "sysemu/tpm.h"
20#include "qemu/thread.h"
Amarnath Vallurib19a5ee2017-09-29 14:10:14 +030021
22static void tpm_backend_worker_thread(gpointer data, gpointer user_data)
23{
24 TPMBackend *s = TPM_BACKEND(user_data);
25 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
26
27 assert(k->handle_request != NULL);
28 k->handle_request(s, (TPMBackendCmd)data);
29}
30
31static void tpm_backend_thread_end(TPMBackend *s)
32{
33 if (s->thread_pool) {
34 g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_END, NULL);
35 g_thread_pool_free(s->thread_pool, FALSE, TRUE);
36 s->thread_pool = NULL;
37 }
38}
Stefan Berger8f0605c2013-03-28 07:26:21 -040039
40enum TpmType tpm_backend_get_type(TPMBackend *s)
41{
42 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
43
44 return k->ops->type;
45}
46
Stefan Berger8f0605c2013-03-28 07:26:21 -040047int tpm_backend_init(TPMBackend *s, TPMState *state,
48 TPMRecvDataCB *datacb)
49{
50 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
51
Amarnath Vallurib19a5ee2017-09-29 14:10:14 +030052 s->tpm_state = state;
53 s->recv_data_callback = datacb;
Amarnath Valluri93330cf2017-09-29 14:10:16 +030054 s->had_startup_error = false;
Amarnath Vallurib19a5ee2017-09-29 14:10:14 +030055
Amarnath Valluri93330cf2017-09-29 14:10:16 +030056 return k->ops->init ? k->ops->init(s) : 0;
Stefan Berger8f0605c2013-03-28 07:26:21 -040057}
58
59int tpm_backend_startup_tpm(TPMBackend *s)
60{
Amarnath Valluri93330cf2017-09-29 14:10:16 +030061 int res = 0;
Stefan Berger8f0605c2013-03-28 07:26:21 -040062 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
63
Amarnath Vallurib19a5ee2017-09-29 14:10:14 +030064 /* terminate a running TPM */
65 tpm_backend_thread_end(s);
66
67 s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE,
68 NULL);
69 g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL);
70
Amarnath Valluri93330cf2017-09-29 14:10:16 +030071 res = k->ops->startup_tpm ? k->ops->startup_tpm(s) : 0;
72
73 s->had_startup_error = (res != 0);
74
75 return res;
Stefan Berger8f0605c2013-03-28 07:26:21 -040076}
77
78bool tpm_backend_had_startup_error(TPMBackend *s)
79{
Amarnath Valluri93330cf2017-09-29 14:10:16 +030080 return s->had_startup_error;
Stefan Berger8f0605c2013-03-28 07:26:21 -040081}
82
Stefan Berger8f0605c2013-03-28 07:26:21 -040083void tpm_backend_deliver_request(TPMBackend *s)
84{
Amarnath Vallurib19a5ee2017-09-29 14:10:14 +030085 g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD,
86 NULL);
Stefan Berger8f0605c2013-03-28 07:26:21 -040087}
88
89void tpm_backend_reset(TPMBackend *s)
90{
91 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
92
Amarnath Valluri93330cf2017-09-29 14:10:16 +030093 if (k->ops->reset) {
94 k->ops->reset(s);
95 }
Amarnath Vallurib19a5ee2017-09-29 14:10:14 +030096
97 tpm_backend_thread_end(s);
Amarnath Valluri93330cf2017-09-29 14:10:16 +030098
99 s->had_startup_error = false;
Stefan Berger8f0605c2013-03-28 07:26:21 -0400100}
101
102void tpm_backend_cancel_cmd(TPMBackend *s)
103{
104 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
105
Amarnath Valluri93330cf2017-09-29 14:10:16 +0300106 assert(k->ops->cancel_cmd);
107
Stefan Berger8f0605c2013-03-28 07:26:21 -0400108 k->ops->cancel_cmd(s);
109}
110
111bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
112{
113 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
114
Amarnath Valluri93330cf2017-09-29 14:10:16 +0300115 return k->ops->get_tpm_established_flag ?
116 k->ops->get_tpm_established_flag(s) : false;
Stefan Berger8f0605c2013-03-28 07:26:21 -0400117}
118
Stefan Berger116694c2015-05-26 16:51:05 -0400119int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
120{
121 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
122
Amarnath Valluri93330cf2017-09-29 14:10:16 +0300123 return k->ops->reset_tpm_established_flag ?
124 k->ops->reset_tpm_established_flag(s, locty) : 0;
Stefan Berger116694c2015-05-26 16:51:05 -0400125}
126
127TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
128{
129 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
130
Amarnath Valluri93330cf2017-09-29 14:10:16 +0300131 assert(k->ops->get_tpm_version);
132
Stefan Berger116694c2015-05-26 16:51:05 -0400133 return k->ops->get_tpm_version(s);
134}
135
Amarnath Vallurif59864b2017-09-29 14:10:17 +0300136TPMInfo *tpm_backend_query_tpm(TPMBackend *s)
137{
138 TPMInfo *info = g_new0(TPMInfo, 1);
139 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
140
141 info->id = g_strdup(s->id);
142 info->model = s->fe_model;
143 info->options = k->ops->get_tpm_options ?
144 k->ops->get_tpm_options(s) : NULL;
145
146 return info;
147}
148
Stefan Berger8f0605c2013-03-28 07:26:21 -0400149static bool tpm_backend_prop_get_opened(Object *obj, Error **errp)
150{
151 TPMBackend *s = TPM_BACKEND(obj);
152
153 return s->opened;
154}
155
156void tpm_backend_open(TPMBackend *s, Error **errp)
157{
158 object_property_set_bool(OBJECT(s), true, "opened", errp);
159}
160
161static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp)
162{
163 TPMBackend *s = TPM_BACKEND(obj);
164 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
Markus Armbruster65cd9062014-04-25 12:44:22 +0200165 Error *local_err = NULL;
Stefan Berger8f0605c2013-03-28 07:26:21 -0400166
167 if (value == s->opened) {
168 return;
169 }
170
171 if (!value && s->opened) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100172 error_setg(errp, QERR_PERMISSION_DENIED);
Stefan Berger8f0605c2013-03-28 07:26:21 -0400173 return;
174 }
175
176 if (k->opened) {
Markus Armbruster65cd9062014-04-25 12:44:22 +0200177 k->opened(s, &local_err);
178 if (local_err) {
179 error_propagate(errp, local_err);
180 return;
181 }
Stefan Berger8f0605c2013-03-28 07:26:21 -0400182 }
183
Markus Armbruster65cd9062014-04-25 12:44:22 +0200184 s->opened = true;
Stefan Berger8f0605c2013-03-28 07:26:21 -0400185}
186
187static void tpm_backend_instance_init(Object *obj)
188{
Amarnath Vallurif35fe5c2017-09-29 14:10:15 +0300189 TPMBackend *s = TPM_BACKEND(obj);
190
Stefan Berger8f0605c2013-03-28 07:26:21 -0400191 object_property_add_bool(obj, "opened",
192 tpm_backend_prop_get_opened,
193 tpm_backend_prop_set_opened,
194 NULL);
Amarnath Vallurif35fe5c2017-09-29 14:10:15 +0300195 s->fe_model = -1;
Stefan Berger8f0605c2013-03-28 07:26:21 -0400196}
197
Amarnath Vallurib19a5ee2017-09-29 14:10:14 +0300198static void tpm_backend_instance_finalize(Object *obj)
Paolo Bonzinibdee56f2013-04-02 18:28:41 +0200199{
Amarnath Vallurib19a5ee2017-09-29 14:10:14 +0300200 TPMBackend *s = TPM_BACKEND(obj);
Paolo Bonzinibdee56f2013-04-02 18:28:41 +0200201
Amarnath Vallurif35fe5c2017-09-29 14:10:15 +0300202 g_free(s->id);
Amarnath Vallurib19a5ee2017-09-29 14:10:14 +0300203 tpm_backend_thread_end(s);
Paolo Bonzinibdee56f2013-04-02 18:28:41 +0200204}
205
Stefan Berger8f0605c2013-03-28 07:26:21 -0400206static const TypeInfo tpm_backend_info = {
207 .name = TYPE_TPM_BACKEND,
208 .parent = TYPE_OBJECT,
209 .instance_size = sizeof(TPMBackend),
210 .instance_init = tpm_backend_instance_init,
Amarnath Vallurib19a5ee2017-09-29 14:10:14 +0300211 .instance_finalize = tpm_backend_instance_finalize,
Stefan Berger8f0605c2013-03-28 07:26:21 -0400212 .class_size = sizeof(TPMBackendClass),
213 .abstract = true,
214};
215
216static void register_types(void)
217{
218 type_register_static(&tpm_backend_info);
219}
220
221type_init(register_types);