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