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