xref: /qemu/backends/tpm/tpm_backend.c (revision 93330cf542b920b6ea5fea8120a08b76bb353113)
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 int tpm_backend_init(TPMBackend *s, TPMState *state,
48                      TPMRecvDataCB *datacb)
49 {
50     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
51 
52     s->tpm_state = state;
53     s->recv_data_callback = datacb;
54     s->had_startup_error = false;
55 
56     return k->ops->init ? k->ops->init(s) : 0;
57 }
58 
59 int tpm_backend_startup_tpm(TPMBackend *s)
60 {
61     int res = 0;
62     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
63 
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 
71     res = k->ops->startup_tpm ? k->ops->startup_tpm(s) : 0;
72 
73     s->had_startup_error = (res != 0);
74 
75     return res;
76 }
77 
78 bool tpm_backend_had_startup_error(TPMBackend *s)
79 {
80     return s->had_startup_error;
81 }
82 
83 size_t tpm_backend_realloc_buffer(TPMBackend *s, TPMSizedBuffer *sb)
84 {
85     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
86 
87     assert(k->ops->realloc_buffer);
88 
89     return k->ops->realloc_buffer(sb);
90 }
91 
92 void tpm_backend_deliver_request(TPMBackend *s)
93 {
94     g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD,
95                        NULL);
96 }
97 
98 void tpm_backend_reset(TPMBackend *s)
99 {
100     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
101 
102     if (k->ops->reset) {
103         k->ops->reset(s);
104     }
105 
106     tpm_backend_thread_end(s);
107 
108     s->had_startup_error = false;
109 }
110 
111 void tpm_backend_cancel_cmd(TPMBackend *s)
112 {
113     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
114 
115     assert(k->ops->cancel_cmd);
116 
117     k->ops->cancel_cmd(s);
118 }
119 
120 bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
121 {
122     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
123 
124     return k->ops->get_tpm_established_flag ?
125            k->ops->get_tpm_established_flag(s) : false;
126 }
127 
128 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
129 {
130     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
131 
132     return k->ops->reset_tpm_established_flag ?
133            k->ops->reset_tpm_established_flag(s, locty) : 0;
134 }
135 
136 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
137 {
138     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
139 
140     assert(k->ops->get_tpm_version);
141 
142     return k->ops->get_tpm_version(s);
143 }
144 
145 static bool tpm_backend_prop_get_opened(Object *obj, Error **errp)
146 {
147     TPMBackend *s = TPM_BACKEND(obj);
148 
149     return s->opened;
150 }
151 
152 void tpm_backend_open(TPMBackend *s, Error **errp)
153 {
154     object_property_set_bool(OBJECT(s), true, "opened", errp);
155 }
156 
157 static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp)
158 {
159     TPMBackend *s = TPM_BACKEND(obj);
160     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
161     Error *local_err = NULL;
162 
163     if (value == s->opened) {
164         return;
165     }
166 
167     if (!value && s->opened) {
168         error_setg(errp, QERR_PERMISSION_DENIED);
169         return;
170     }
171 
172     if (k->opened) {
173         k->opened(s, &local_err);
174         if (local_err) {
175             error_propagate(errp, local_err);
176             return;
177         }
178     }
179 
180     s->opened = true;
181 }
182 
183 static void tpm_backend_instance_init(Object *obj)
184 {
185     TPMBackend *s = TPM_BACKEND(obj);
186 
187     object_property_add_bool(obj, "opened",
188                              tpm_backend_prop_get_opened,
189                              tpm_backend_prop_set_opened,
190                              NULL);
191     s->fe_model = -1;
192 }
193 
194 static void tpm_backend_instance_finalize(Object *obj)
195 {
196     TPMBackend *s = TPM_BACKEND(obj);
197 
198     g_free(s->id);
199     g_free(s->path);
200     g_free(s->cancel_path);
201     tpm_backend_thread_end(s);
202 }
203 
204 static const TypeInfo tpm_backend_info = {
205     .name = TYPE_TPM_BACKEND,
206     .parent = TYPE_OBJECT,
207     .instance_size = sizeof(TPMBackend),
208     .instance_init = tpm_backend_instance_init,
209     .instance_finalize = tpm_backend_instance_finalize,
210     .class_size = sizeof(TPMBackendClass),
211     .abstract = true,
212 };
213 
214 static void register_types(void)
215 {
216     type_register_static(&tpm_backend_info);
217 }
218 
219 type_init(register_types);
220