Merge remote-tracking branch 'remotes/kraxel/tags/ui-20171016-pull-request' into...
[qemu.git] / backends / tpm.c
CommitLineData
8f0605cc
SB
1/*
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
9c058332 15#include "qemu/osdep.h"
dccfcd0e 16#include "sysemu/tpm_backend.h"
da34e65c 17#include "qapi/error.h"
8f0605cc 18#include "qapi/qmp/qerror.h"
bdee56f5
PB
19#include "sysemu/tpm.h"
20#include "qemu/thread.h"
b19a5eea
AV
21
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}
8f0605cc
SB
39
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
8f0605cc
SB
47int tpm_backend_init(TPMBackend *s, TPMState *state,
48 TPMRecvDataCB *datacb)
49{
50 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
51
b19a5eea
AV
52 s->tpm_state = state;
53 s->recv_data_callback = datacb;
93330cf5 54 s->had_startup_error = false;
b19a5eea 55
93330cf5 56 return k->ops->init ? k->ops->init(s) : 0;
8f0605cc
SB
57}
58
59int tpm_backend_startup_tpm(TPMBackend *s)
60{
93330cf5 61 int res = 0;
8f0605cc
SB
62 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
63
b19a5eea
AV
64 /* 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
93330cf5
AV
71 res = k->ops->startup_tpm ? k->ops->startup_tpm(s) : 0;
72
73 s->had_startup_error = (res != 0);
74
75 return res;
8f0605cc
SB
76}
77
78bool tpm_backend_had_startup_error(TPMBackend *s)
79{
93330cf5 80 return s->had_startup_error;
8f0605cc
SB
81}
82
8f0605cc
SB
83void tpm_backend_deliver_request(TPMBackend *s)
84{
b19a5eea
AV
85 g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD,
86 NULL);
8f0605cc
SB
87}
88
89void tpm_backend_reset(TPMBackend *s)
90{
91 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
92
93330cf5
AV
93 if (k->ops->reset) {
94 k->ops->reset(s);
95 }
b19a5eea
AV
96
97 tpm_backend_thread_end(s);
93330cf5
AV
98
99 s->had_startup_error = false;
8f0605cc
SB
100}
101
102void tpm_backend_cancel_cmd(TPMBackend *s)
103{
104 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
105
93330cf5
AV
106 assert(k->ops->cancel_cmd);
107
8f0605cc
SB
108 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
93330cf5
AV
115 return k->ops->get_tpm_established_flag ?
116 k->ops->get_tpm_established_flag(s) : false;
8f0605cc
SB
117}
118
116694c3
SB
119int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
120{
121 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
122
93330cf5
AV
123 return k->ops->reset_tpm_established_flag ?
124 k->ops->reset_tpm_established_flag(s, locty) : 0;
116694c3
SB
125}
126
127TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
128{
129 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
130
93330cf5
AV
131 assert(k->ops->get_tpm_version);
132
116694c3
SB
133 return k->ops->get_tpm_version(s);
134}
135
f59864ba
AV
136TPMInfo *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
8f0605cc
SB
149static 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);
65cd9064 165 Error *local_err = NULL;
8f0605cc
SB
166
167 if (value == s->opened) {
168 return;
169 }
170
171 if (!value && s->opened) {
c6bd8c70 172 error_setg(errp, QERR_PERMISSION_DENIED);
8f0605cc
SB
173 return;
174 }
175
176 if (k->opened) {
65cd9064
MA
177 k->opened(s, &local_err);
178 if (local_err) {
179 error_propagate(errp, local_err);
180 return;
181 }
8f0605cc
SB
182 }
183
65cd9064 184 s->opened = true;
8f0605cc
SB
185}
186
187static void tpm_backend_instance_init(Object *obj)
188{
f35fe5cb
AV
189 TPMBackend *s = TPM_BACKEND(obj);
190
8f0605cc
SB
191 object_property_add_bool(obj, "opened",
192 tpm_backend_prop_get_opened,
193 tpm_backend_prop_set_opened,
194 NULL);
f35fe5cb 195 s->fe_model = -1;
bdee56f5
PB
196}
197
b19a5eea 198static void tpm_backend_instance_finalize(Object *obj)
bdee56f5 199{
b19a5eea 200 TPMBackend *s = TPM_BACKEND(obj);
bdee56f5 201
f35fe5cb 202 g_free(s->id);
b19a5eea 203 tpm_backend_thread_end(s);
bdee56f5
PB
204}
205
8f0605cc
SB
206static 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,
b19a5eea 211 .instance_finalize = tpm_backend_instance_finalize,
8f0605cc
SB
212 .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);
This page took 0.227255 seconds and 4 git commands to generate.