]> git.proxmox.com Git - mirror_qemu.git/blob - backends/tpm.c
tpm: add a QOM TPM interface
[mirror_qemu.git] / backends / tpm.c
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
15 #include "qemu/osdep.h"
16 #include "sysemu/tpm_backend.h"
17 #include "qapi/error.h"
18 #include "qapi/qmp/qerror.h"
19 #include "sysemu/tpm.h"
20 #include "hw/tpm/tpm_int.h"
21 #include "qemu/thread.h"
22
23 static void tpm_backend_worker_thread(gpointer data, gpointer user_data)
24 {
25 TPMBackend *s = TPM_BACKEND(user_data);
26 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
27
28 assert(k->handle_request != NULL);
29 k->handle_request(s, (TPMBackendCmd *)data);
30 }
31
32 static void tpm_backend_thread_end(TPMBackend *s)
33 {
34 if (s->thread_pool) {
35 g_thread_pool_free(s->thread_pool, FALSE, TRUE);
36 s->thread_pool = NULL;
37 }
38 }
39
40 enum TpmType tpm_backend_get_type(TPMBackend *s)
41 {
42 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
43
44 return k->type;
45 }
46
47 int tpm_backend_init(TPMBackend *s, TPMState *state,
48 TPMRecvDataCB *datacb)
49 {
50 s->tpm_state = state;
51 s->recv_data_callback = datacb;
52 s->had_startup_error = false;
53
54 return 0;
55 }
56
57 int tpm_backend_startup_tpm(TPMBackend *s)
58 {
59 int res = 0;
60 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
61
62 /* terminate a running TPM */
63 tpm_backend_thread_end(s);
64
65 s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE,
66 NULL);
67
68 res = k->startup_tpm ? k->startup_tpm(s) : 0;
69
70 s->had_startup_error = (res != 0);
71
72 return res;
73 }
74
75 bool tpm_backend_had_startup_error(TPMBackend *s)
76 {
77 return s->had_startup_error;
78 }
79
80 void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd)
81 {
82 g_thread_pool_push(s->thread_pool, cmd, NULL);
83 }
84
85 void tpm_backend_reset(TPMBackend *s)
86 {
87 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
88
89 if (k->reset) {
90 k->reset(s);
91 }
92
93 tpm_backend_thread_end(s);
94
95 s->had_startup_error = false;
96 }
97
98 void tpm_backend_cancel_cmd(TPMBackend *s)
99 {
100 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
101
102 assert(k->cancel_cmd);
103
104 k->cancel_cmd(s);
105 }
106
107 bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
108 {
109 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
110
111 return k->get_tpm_established_flag ?
112 k->get_tpm_established_flag(s) : false;
113 }
114
115 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
116 {
117 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
118
119 return k->reset_tpm_established_flag ?
120 k->reset_tpm_established_flag(s, locty) : 0;
121 }
122
123 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
124 {
125 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
126
127 assert(k->get_tpm_version);
128
129 return k->get_tpm_version(s);
130 }
131
132 TPMInfo *tpm_backend_query_tpm(TPMBackend *s)
133 {
134 TPMInfo *info = g_new0(TPMInfo, 1);
135 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
136
137 info->id = g_strdup(s->id);
138 info->model = s->fe_model;
139 if (k->get_tpm_options) {
140 info->options = k->get_tpm_options(s);
141 }
142
143 return info;
144 }
145
146 static bool tpm_backend_prop_get_opened(Object *obj, Error **errp)
147 {
148 TPMBackend *s = TPM_BACKEND(obj);
149
150 return s->opened;
151 }
152
153 void tpm_backend_open(TPMBackend *s, Error **errp)
154 {
155 object_property_set_bool(OBJECT(s), true, "opened", errp);
156 }
157
158 static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp)
159 {
160 TPMBackend *s = TPM_BACKEND(obj);
161 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
162 Error *local_err = NULL;
163
164 if (value == s->opened) {
165 return;
166 }
167
168 if (!value && s->opened) {
169 error_setg(errp, QERR_PERMISSION_DENIED);
170 return;
171 }
172
173 if (k->opened) {
174 k->opened(s, &local_err);
175 if (local_err) {
176 error_propagate(errp, local_err);
177 return;
178 }
179 }
180
181 s->opened = true;
182 }
183
184 static void tpm_backend_instance_init(Object *obj)
185 {
186 TPMBackend *s = TPM_BACKEND(obj);
187
188 object_property_add_bool(obj, "opened",
189 tpm_backend_prop_get_opened,
190 tpm_backend_prop_set_opened,
191 NULL);
192 s->fe_model = -1;
193 }
194
195 static void tpm_backend_instance_finalize(Object *obj)
196 {
197 TPMBackend *s = TPM_BACKEND(obj);
198
199 g_free(s->id);
200 tpm_backend_thread_end(s);
201 }
202
203 static const TypeInfo tpm_backend_info = {
204 .name = TYPE_TPM_BACKEND,
205 .parent = TYPE_OBJECT,
206 .instance_size = sizeof(TPMBackend),
207 .instance_init = tpm_backend_instance_init,
208 .instance_finalize = tpm_backend_instance_finalize,
209 .class_size = sizeof(TPMBackendClass),
210 .abstract = true,
211 };
212
213 static const TypeInfo tpm_if_info = {
214 .name = TYPE_TPM_IF,
215 .parent = TYPE_INTERFACE,
216 .class_size = sizeof(TPMIfClass),
217 };
218
219 static void register_types(void)
220 {
221 type_register_static(&tpm_backend_info);
222 type_register_static(&tpm_if_info);
223 }
224
225 type_init(register_types);