]> git.proxmox.com Git - mirror_qemu.git/blame - tpm.c
iotests: use virtio aliases for 067
[mirror_qemu.git] / tpm.c
CommitLineData
d1a0cf73
SB
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 */
d38ea87a 14#include "qemu/osdep.h"
d1a0cf73 15
d1a0cf73 16#include "qapi/qmp/qerror.h"
dccfcd0e 17#include "sysemu/tpm_backend.h"
bdee56f5 18#include "sysemu/tpm.h"
d1a0cf73 19#include "qemu/config-file.h"
d49b6836 20#include "qemu/error-report.h"
d1a0cf73
SB
21#include "qmp-commands.h"
22
23static QLIST_HEAD(, TPMBackend) tpm_backends =
24 QLIST_HEAD_INITIALIZER(tpm_backends);
25
a9a72aee 26static TPMDriverOps const *be_drivers[TPM_TYPE__MAX];
00bbf50a 27static bool tpm_models[TPM_MODEL__MAX];
d1a0cf73 28
00bbf50a 29void tpm_register_model(enum TpmModel model)
d1a0cf73 30{
00bbf50a 31 tpm_models[model] = true;
d1a0cf73 32}
4549a8b7 33
d1a0cf73
SB
34const TPMDriverOps *tpm_get_backend_driver(const char *type)
35{
f7abe0ec 36 int i = qapi_enum_parse(&TpmType_lookup, type, -1, NULL);
d1a0cf73 37
a9a72aee 38 return i >= 0 ? be_drivers[i] : NULL;
d1a0cf73
SB
39}
40
41#ifdef CONFIG_TPM
42
a9a72aee 43void tpm_register_driver(const TPMDriverOps *tdo)
d1a0cf73 44{
a9a72aee 45 assert(!be_drivers[tdo->type]);
d1a0cf73 46
a9a72aee 47 be_drivers[tdo->type] = tdo;
d1a0cf73
SB
48}
49
50/*
51 * Walk the list of available TPM backend drivers and display them on the
52 * screen.
53 */
bdee56f5 54static void tpm_display_backend_drivers(void)
d1a0cf73
SB
55{
56 int i;
57
58 fprintf(stderr, "Supported TPM types (choose only one):\n");
59
a9a72aee
MAL
60 for (i = 0; i < TPM_TYPE__MAX; i++) {
61 if (be_drivers[i] == NULL) {
62 continue;
63 }
d1a0cf73 64 fprintf(stderr, "%12s %s\n",
977c736f 65 TpmType_str(i), be_drivers[i]->desc());
d1a0cf73
SB
66 }
67 fprintf(stderr, "\n");
68}
69
70/*
71 * Find the TPM with the given Id
72 */
73TPMBackend *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
88static int configure_tpm(QemuOpts *opts)
89{
90 const char *value;
91 const char *id;
92 const TPMDriverOps *be;
93 TPMBackend *drv;
8f0605cc 94 Error *local_err = NULL;
d1a0cf73
SB
95
96 if (!QLIST_EMPTY(&tpm_backends)) {
27215a22 97 error_report("Only one TPM is allowed.");
d1a0cf73
SB
98 return 1;
99 }
100
101 id = qemu_opts_id(opts);
102 if (id == NULL) {
8b53a196 103 error_report(QERR_MISSING_PARAMETER, "id");
d1a0cf73
SB
104 return 1;
105 }
106
107 value = qemu_opt_get(opts, "type");
108 if (!value) {
8b53a196 109 error_report(QERR_MISSING_PARAMETER, "type");
d1a0cf73
SB
110 tpm_display_backend_drivers();
111 return 1;
112 }
113
114 be = tpm_get_backend_driver(value);
115 if (be == NULL) {
8b53a196
MA
116 error_report(QERR_INVALID_PARAMETER_VALUE,
117 "type", "a TPM backend type");
d1a0cf73
SB
118 tpm_display_backend_drivers();
119 return 1;
120 }
121
bb716238
SB
122 /* validate backend specific opts */
123 qemu_opts_validate(opts, be->opts, &local_err);
84d18f06 124 if (local_err) {
bc09a287 125 error_report_err(local_err);
bb716238
SB
126 return 1;
127 }
128
d1a0cf73
SB
129 drv = be->create(opts, id);
130 if (!drv) {
131 return 1;
132 }
133
8f0605cc
SB
134 tpm_backend_open(drv, &local_err);
135 if (local_err) {
bc09a287 136 error_report_err(local_err);
8f0605cc
SB
137 return 1;
138 }
139
d1a0cf73
SB
140 QLIST_INSERT_HEAD(&tpm_backends, drv, list);
141
142 return 0;
143}
144
28d0de7a 145static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
d1a0cf73
SB
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 */
154void tpm_cleanup(void)
155{
156 TPMBackend *drv, *next;
157
158 QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
159 QLIST_REMOVE(drv, list);
8f0605cc 160 tpm_backend_destroy(drv);
d1a0cf73
SB
161 }
162}
163
164/*
165 * Initialize the TPM. Process the tpmdev command line options describing the
166 * TPM backend.
167 */
168int tpm_init(void)
169{
28d0de7a
MA
170 if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
171 tpm_init_tpmdev, NULL, NULL)) {
d1a0cf73
SB
172 return -1;
173 }
174
175 atexit(tpm_cleanup);
d1a0cf73
SB
176 return 0;
177}
178
179/*
180 * Parse the TPM configuration options.
181 * To display all available TPM backends the user may use '-tpmdev help'
182 */
183int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
184{
185 QemuOpts *opts;
186
187 if (!strcmp(optarg, "help")) {
188 tpm_display_backend_drivers();
189 return -1;
190 }
70b94331 191 opts = qemu_opts_parse_noisily(opts_list, optarg, true);
d1a0cf73
SB
192 if (!opts) {
193 return -1;
194 }
195 return 0;
196}
197
198#endif /* CONFIG_TPM */
199
200static const TPMDriverOps *tpm_driver_find_by_type(enum TpmType type)
201{
a9a72aee 202 return be_drivers[type];
d1a0cf73
SB
203}
204
205static TPMInfo *qmp_query_tpm_inst(TPMBackend *drv)
206{
207 TPMInfo *res = g_new0(TPMInfo, 1);
208 TPMPassthroughOptions *tpo;
209
210 res->id = g_strdup(drv->id);
211 res->model = drv->fe_model;
88ca7bcf 212 res->options = g_new0(TpmTypeOptions, 1);
d1a0cf73 213
88ca7bcf 214 switch (drv->ops->type) {
d1a0cf73 215 case TPM_TYPE_PASSTHROUGH:
ce21131a 216 res->options->type = TPM_TYPE_OPTIONS_KIND_PASSTHROUGH;
d1a0cf73 217 tpo = g_new0(TPMPassthroughOptions, 1);
32bafa8f 218 res->options->u.passthrough.data = tpo;
d1a0cf73
SB
219 if (drv->path) {
220 tpo->path = g_strdup(drv->path);
221 tpo->has_path = true;
222 }
223 if (drv->cancel_path) {
224 tpo->cancel_path = g_strdup(drv->cancel_path);
225 tpo->has_cancel_path = true;
226 }
227 break;
7fb1cf16 228 case TPM_TYPE__MAX:
d1a0cf73
SB
229 break;
230 }
231
232 return res;
233}
234
235/*
236 * Walk the list of active TPM backends and collect information about them
237 * following the schema description in qapi-schema.json.
238 */
239TPMInfoList *qmp_query_tpm(Error **errp)
240{
241 TPMBackend *drv;
242 TPMInfoList *info, *head = NULL, *cur_item = NULL;
243
244 QLIST_FOREACH(drv, &tpm_backends, list) {
00bbf50a 245 if (!tpm_models[drv->fe_model]) {
d1a0cf73
SB
246 continue;
247 }
248 info = g_new0(TPMInfoList, 1);
249 info->value = qmp_query_tpm_inst(drv);
250
251 if (!cur_item) {
252 head = cur_item = info;
253 } else {
254 cur_item->next = info;
255 cur_item = info;
256 }
257 }
258
259 return head;
260}
261
262TpmTypeList *qmp_query_tpm_types(Error **errp)
263{
264 unsigned int i = 0;
265 TpmTypeList *head = NULL, *prev = NULL, *cur_item;
266
7fb1cf16 267 for (i = 0; i < TPM_TYPE__MAX; i++) {
d1a0cf73
SB
268 if (!tpm_driver_find_by_type(i)) {
269 continue;
270 }
271 cur_item = g_new0(TpmTypeList, 1);
272 cur_item->value = i;
273
274 if (prev) {
275 prev->next = cur_item;
276 }
277 if (!head) {
278 head = cur_item;
279 }
280 prev = cur_item;
281 }
282
283 return head;
284}
285
286TpmModelList *qmp_query_tpm_models(Error **errp)
287{
288 unsigned int i = 0;
289 TpmModelList *head = NULL, *prev = NULL, *cur_item;
290
7fb1cf16 291 for (i = 0; i < TPM_MODEL__MAX; i++) {
00bbf50a 292 if (!tpm_models[i]) {
d1a0cf73
SB
293 continue;
294 }
295 cur_item = g_new0(TpmModelList, 1);
296 cur_item->value = i;
297
298 if (prev) {
299 prev->next = cur_item;
300 }
301 if (!head) {
302 head = cur_item;
303 }
304 prev = cur_item;
305 }
306
307 return head;
308}