]> git.proxmox.com Git - mirror_qemu.git/blob - backends/tpm.c
tpm-backend: Initialize and free data members in it's own methods
[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 "qemu/thread.h"
21
22 static 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
31 static 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 }
39
40 enum TpmType tpm_backend_get_type(TPMBackend *s)
41 {
42 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
43
44 return k->ops->type;
45 }
46
47 const char *tpm_backend_get_desc(TPMBackend *s)
48 {
49 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
50
51 return k->ops->desc();
52 }
53
54 int tpm_backend_init(TPMBackend *s, TPMState *state,
55 TPMRecvDataCB *datacb)
56 {
57 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
58
59 s->tpm_state = state;
60 s->recv_data_callback = datacb;
61
62 return k->ops->init(s);
63 }
64
65 int tpm_backend_startup_tpm(TPMBackend *s)
66 {
67 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
68
69 /* terminate a running TPM */
70 tpm_backend_thread_end(s);
71
72 s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE,
73 NULL);
74 g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL);
75
76 return k->ops->startup_tpm(s);
77 }
78
79 bool tpm_backend_had_startup_error(TPMBackend *s)
80 {
81 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
82
83 return k->ops->had_startup_error(s);
84 }
85
86 size_t tpm_backend_realloc_buffer(TPMBackend *s, TPMSizedBuffer *sb)
87 {
88 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
89
90 return k->ops->realloc_buffer(sb);
91 }
92
93 void tpm_backend_deliver_request(TPMBackend *s)
94 {
95 g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD,
96 NULL);
97 }
98
99 void tpm_backend_reset(TPMBackend *s)
100 {
101 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
102
103 k->ops->reset(s);
104
105 tpm_backend_thread_end(s);
106 }
107
108 void tpm_backend_cancel_cmd(TPMBackend *s)
109 {
110 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
111
112 k->ops->cancel_cmd(s);
113 }
114
115 bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
116 {
117 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
118
119 return k->ops->get_tpm_established_flag(s);
120 }
121
122 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
123 {
124 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
125
126 return k->ops->reset_tpm_established_flag(s, locty);
127 }
128
129 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
130 {
131 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
132
133 return k->ops->get_tpm_version(s);
134 }
135
136 static bool tpm_backend_prop_get_opened(Object *obj, Error **errp)
137 {
138 TPMBackend *s = TPM_BACKEND(obj);
139
140 return s->opened;
141 }
142
143 void tpm_backend_open(TPMBackend *s, Error **errp)
144 {
145 object_property_set_bool(OBJECT(s), true, "opened", errp);
146 }
147
148 static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp)
149 {
150 TPMBackend *s = TPM_BACKEND(obj);
151 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
152 Error *local_err = NULL;
153
154 if (value == s->opened) {
155 return;
156 }
157
158 if (!value && s->opened) {
159 error_setg(errp, QERR_PERMISSION_DENIED);
160 return;
161 }
162
163 if (k->opened) {
164 k->opened(s, &local_err);
165 if (local_err) {
166 error_propagate(errp, local_err);
167 return;
168 }
169 }
170
171 s->opened = true;
172 }
173
174 static void tpm_backend_instance_init(Object *obj)
175 {
176 TPMBackend *s = TPM_BACKEND(obj);
177
178 object_property_add_bool(obj, "opened",
179 tpm_backend_prop_get_opened,
180 tpm_backend_prop_set_opened,
181 NULL);
182 s->fe_model = -1;
183 }
184
185 static void tpm_backend_instance_finalize(Object *obj)
186 {
187 TPMBackend *s = TPM_BACKEND(obj);
188
189 g_free(s->id);
190 g_free(s->path);
191 g_free(s->cancel_path);
192 tpm_backend_thread_end(s);
193 }
194
195 static const TypeInfo tpm_backend_info = {
196 .name = TYPE_TPM_BACKEND,
197 .parent = TYPE_OBJECT,
198 .instance_size = sizeof(TPMBackend),
199 .instance_init = tpm_backend_instance_init,
200 .instance_finalize = tpm_backend_instance_finalize,
201 .class_size = sizeof(TPMBackendClass),
202 .abstract = true,
203 };
204
205 static void register_types(void)
206 {
207 type_register_static(&tpm_backend_info);
208 }
209
210 type_init(register_types);