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