]> git.proxmox.com Git - mirror_qemu.git/blob - tpm.c
tpm: Move tpm_cleanup() to right place
[mirror_qemu.git] / tpm.c
1 /*
2 * TPM configuration
3 *
4 * Copyright (C) 2011-2013 IBM Corporation
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 net.c
13 */
14 #include "qemu/osdep.h"
15
16 #include "qapi/qmp/qerror.h"
17 #include "sysemu/tpm_backend.h"
18 #include "sysemu/tpm.h"
19 #include "qemu/config-file.h"
20 #include "qemu/error-report.h"
21 #include "qmp-commands.h"
22
23 static QLIST_HEAD(, TPMBackend) tpm_backends =
24 QLIST_HEAD_INITIALIZER(tpm_backends);
25
26 static TPMDriverOps const *be_drivers[TPM_TYPE__MAX];
27 static bool tpm_models[TPM_MODEL__MAX];
28
29 void tpm_register_model(enum TpmModel model)
30 {
31 tpm_models[model] = true;
32 }
33
34 const TPMDriverOps *tpm_get_backend_driver(const char *type)
35 {
36 int i = qapi_enum_parse(&TpmType_lookup, type, -1, NULL);
37
38 return i >= 0 ? be_drivers[i] : NULL;
39 }
40
41 #ifdef CONFIG_TPM
42
43 void tpm_register_driver(const TPMDriverOps *tdo)
44 {
45 assert(!be_drivers[tdo->type]);
46
47 be_drivers[tdo->type] = tdo;
48 }
49
50 /*
51 * Walk the list of available TPM backend drivers and display them on the
52 * screen.
53 */
54 static void tpm_display_backend_drivers(void)
55 {
56 int i;
57
58 fprintf(stderr, "Supported TPM types (choose only one):\n");
59
60 for (i = 0; i < TPM_TYPE__MAX; i++) {
61 if (be_drivers[i] == NULL) {
62 continue;
63 }
64 fprintf(stderr, "%12s %s\n",
65 TpmType_str(i), be_drivers[i]->desc);
66 }
67 fprintf(stderr, "\n");
68 }
69
70 /*
71 * Find the TPM with the given Id
72 */
73 TPMBackend *qemu_find_tpm(const char *id)
74 {
75 TPMBackend *drv;
76
77 if (id) {
78 QLIST_FOREACH(drv, &tpm_backends, list) {
79 if (!strcmp(drv->id, id)) {
80 return drv;
81 }
82 }
83 }
84
85 return NULL;
86 }
87
88 static int configure_tpm(QemuOpts *opts)
89 {
90 const char *value;
91 const char *id;
92 const TPMDriverOps *be;
93 TPMBackend *drv;
94 Error *local_err = NULL;
95
96 if (!QLIST_EMPTY(&tpm_backends)) {
97 error_report("Only one TPM is allowed.");
98 return 1;
99 }
100
101 id = qemu_opts_id(opts);
102 if (id == NULL) {
103 error_report(QERR_MISSING_PARAMETER, "id");
104 return 1;
105 }
106
107 value = qemu_opt_get(opts, "type");
108 if (!value) {
109 error_report(QERR_MISSING_PARAMETER, "type");
110 tpm_display_backend_drivers();
111 return 1;
112 }
113
114 be = tpm_get_backend_driver(value);
115 if (be == NULL) {
116 error_report(QERR_INVALID_PARAMETER_VALUE,
117 "type", "a TPM backend type");
118 tpm_display_backend_drivers();
119 return 1;
120 }
121
122 /* validate backend specific opts */
123 qemu_opts_validate(opts, be->opts, &local_err);
124 if (local_err) {
125 error_report_err(local_err);
126 return 1;
127 }
128
129 drv = be->create(opts, id);
130 if (!drv) {
131 return 1;
132 }
133
134 tpm_backend_open(drv, &local_err);
135 if (local_err) {
136 error_report_err(local_err);
137 return 1;
138 }
139
140 QLIST_INSERT_HEAD(&tpm_backends, drv, list);
141
142 return 0;
143 }
144
145 static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
146 {
147 return configure_tpm(opts);
148 }
149
150 /*
151 * Walk the list of TPM backend drivers that are in use and call their
152 * destroy function to have them cleaned up.
153 */
154 void tpm_cleanup(void)
155 {
156 TPMBackend *drv, *next;
157
158 QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
159 QLIST_REMOVE(drv, list);
160 object_unref(OBJECT(drv));
161 }
162 }
163
164 /*
165 * Initialize the TPM. Process the tpmdev command line options describing the
166 * TPM backend.
167 */
168 int tpm_init(void)
169 {
170 if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
171 tpm_init_tpmdev, NULL, NULL)) {
172 return -1;
173 }
174
175 return 0;
176 }
177
178 /*
179 * Parse the TPM configuration options.
180 * To display all available TPM backends the user may use '-tpmdev help'
181 */
182 int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
183 {
184 QemuOpts *opts;
185
186 if (!strcmp(optarg, "help")) {
187 tpm_display_backend_drivers();
188 return -1;
189 }
190 opts = qemu_opts_parse_noisily(opts_list, optarg, true);
191 if (!opts) {
192 return -1;
193 }
194 return 0;
195 }
196
197 #endif /* CONFIG_TPM */
198
199 static const TPMDriverOps *tpm_driver_find_by_type(enum TpmType type)
200 {
201 return be_drivers[type];
202 }
203
204 /*
205 * Walk the list of active TPM backends and collect information about them
206 * following the schema description in qapi-schema.json.
207 */
208 TPMInfoList *qmp_query_tpm(Error **errp)
209 {
210 TPMBackend *drv;
211 TPMInfoList *info, *head = NULL, *cur_item = NULL;
212
213 QLIST_FOREACH(drv, &tpm_backends, list) {
214 if (!tpm_models[drv->fe_model]) {
215 continue;
216 }
217 info = g_new0(TPMInfoList, 1);
218 info->value = tpm_backend_query_tpm(drv);
219
220 if (!cur_item) {
221 head = cur_item = info;
222 } else {
223 cur_item->next = info;
224 cur_item = info;
225 }
226 }
227
228 return head;
229 }
230
231 TpmTypeList *qmp_query_tpm_types(Error **errp)
232 {
233 unsigned int i = 0;
234 TpmTypeList *head = NULL, *prev = NULL, *cur_item;
235
236 for (i = 0; i < TPM_TYPE__MAX; i++) {
237 if (!tpm_driver_find_by_type(i)) {
238 continue;
239 }
240 cur_item = g_new0(TpmTypeList, 1);
241 cur_item->value = i;
242
243 if (prev) {
244 prev->next = cur_item;
245 }
246 if (!head) {
247 head = cur_item;
248 }
249 prev = cur_item;
250 }
251
252 return head;
253 }
254
255 TpmModelList *qmp_query_tpm_models(Error **errp)
256 {
257 unsigned int i = 0;
258 TpmModelList *head = NULL, *prev = NULL, *cur_item;
259
260 for (i = 0; i < TPM_MODEL__MAX; i++) {
261 if (!tpm_models[i]) {
262 continue;
263 }
264 cur_item = g_new0(TpmModelList, 1);
265 cur_item->value = i;
266
267 if (prev) {
268 prev->next = cur_item;
269 }
270 if (!head) {
271 head = cur_item;
272 }
273 prev = cur_item;
274 }
275
276 return head;
277 }