]>
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 | ||
00bbf50a | 26 | static bool tpm_models[TPM_MODEL__MAX]; |
d1a0cf73 | 27 | |
00bbf50a | 28 | void 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 | ||
f3faa1df MAL |
35 | static const TPMDriverOps * |
36 | tpm_driver_find_by_type(enum TpmType type) | |
37 | { | |
38 | ObjectClass *oc; | |
39 | TPMBackendClass *bc; | |
40 | char *typename = g_strdup_printf("tpm-%s", TpmType_str(type)); | |
41 | ||
42 | oc = object_class_by_name(typename); | |
43 | g_free(typename); | |
44 | ||
45 | if (!object_class_dynamic_cast(oc, TYPE_TPM_BACKEND)) { | |
46 | return NULL; | |
47 | } | |
48 | ||
49 | bc = TPM_BACKEND_CLASS(oc); | |
50 | return bc->ops; | |
51 | } | |
52 | ||
d1a0cf73 SB |
53 | /* |
54 | * Walk the list of available TPM backend drivers and display them on the | |
55 | * screen. | |
56 | */ | |
bdee56f5 | 57 | static void tpm_display_backend_drivers(void) |
d1a0cf73 SB |
58 | { |
59 | int i; | |
60 | ||
61 | fprintf(stderr, "Supported TPM types (choose only one):\n"); | |
62 | ||
a9a72aee | 63 | for (i = 0; i < TPM_TYPE__MAX; i++) { |
f3faa1df MAL |
64 | const TPMDriverOps *ops = tpm_driver_find_by_type(i); |
65 | if (!ops) { | |
a9a72aee MAL |
66 | continue; |
67 | } | |
f3faa1df | 68 | fprintf(stderr, "%12s %s\n", TpmType_str(i), ops->desc); |
d1a0cf73 SB |
69 | } |
70 | fprintf(stderr, "\n"); | |
71 | } | |
72 | ||
73 | /* | |
74 | * Find the TPM with the given Id | |
75 | */ | |
76 | TPMBackend *qemu_find_tpm(const char *id) | |
77 | { | |
78 | TPMBackend *drv; | |
79 | ||
80 | if (id) { | |
81 | QLIST_FOREACH(drv, &tpm_backends, list) { | |
82 | if (!strcmp(drv->id, id)) { | |
83 | return drv; | |
84 | } | |
85 | } | |
86 | } | |
87 | ||
88 | return NULL; | |
89 | } | |
90 | ||
91 | static int configure_tpm(QemuOpts *opts) | |
92 | { | |
93 | const char *value; | |
94 | const char *id; | |
95 | const TPMDriverOps *be; | |
96 | TPMBackend *drv; | |
8f0605cc | 97 | Error *local_err = NULL; |
d91a7a57 | 98 | int i; |
d1a0cf73 SB |
99 | |
100 | if (!QLIST_EMPTY(&tpm_backends)) { | |
27215a22 | 101 | error_report("Only one TPM is allowed."); |
d1a0cf73 SB |
102 | return 1; |
103 | } | |
104 | ||
105 | id = qemu_opts_id(opts); | |
106 | if (id == NULL) { | |
8b53a196 | 107 | error_report(QERR_MISSING_PARAMETER, "id"); |
d1a0cf73 SB |
108 | return 1; |
109 | } | |
110 | ||
111 | value = qemu_opt_get(opts, "type"); | |
112 | if (!value) { | |
8b53a196 | 113 | error_report(QERR_MISSING_PARAMETER, "type"); |
d1a0cf73 SB |
114 | tpm_display_backend_drivers(); |
115 | return 1; | |
116 | } | |
117 | ||
d91a7a57 MAL |
118 | i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL); |
119 | be = i >= 0 ? tpm_driver_find_by_type(i) : NULL; | |
d1a0cf73 | 120 | if (be == NULL) { |
8b53a196 MA |
121 | error_report(QERR_INVALID_PARAMETER_VALUE, |
122 | "type", "a TPM backend type"); | |
d1a0cf73 SB |
123 | tpm_display_backend_drivers(); |
124 | return 1; | |
125 | } | |
126 | ||
bb716238 SB |
127 | /* validate backend specific opts */ |
128 | qemu_opts_validate(opts, be->opts, &local_err); | |
84d18f06 | 129 | if (local_err) { |
bc09a287 | 130 | error_report_err(local_err); |
bb716238 SB |
131 | return 1; |
132 | } | |
133 | ||
d1a0cf73 SB |
134 | drv = be->create(opts, id); |
135 | if (!drv) { | |
136 | return 1; | |
137 | } | |
138 | ||
8f0605cc SB |
139 | tpm_backend_open(drv, &local_err); |
140 | if (local_err) { | |
bc09a287 | 141 | error_report_err(local_err); |
8f0605cc SB |
142 | return 1; |
143 | } | |
144 | ||
d1a0cf73 SB |
145 | QLIST_INSERT_HEAD(&tpm_backends, drv, list); |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
28d0de7a | 150 | static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp) |
d1a0cf73 SB |
151 | { |
152 | return configure_tpm(opts); | |
153 | } | |
154 | ||
155 | /* | |
156 | * Walk the list of TPM backend drivers that are in use and call their | |
157 | * destroy function to have them cleaned up. | |
158 | */ | |
159 | void tpm_cleanup(void) | |
160 | { | |
161 | TPMBackend *drv, *next; | |
162 | ||
163 | QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) { | |
164 | QLIST_REMOVE(drv, list); | |
f35fe5cb | 165 | object_unref(OBJECT(drv)); |
d1a0cf73 SB |
166 | } |
167 | } | |
168 | ||
169 | /* | |
170 | * Initialize the TPM. Process the tpmdev command line options describing the | |
171 | * TPM backend. | |
172 | */ | |
173 | int tpm_init(void) | |
174 | { | |
28d0de7a MA |
175 | if (qemu_opts_foreach(qemu_find_opts("tpmdev"), |
176 | tpm_init_tpmdev, NULL, NULL)) { | |
d1a0cf73 SB |
177 | return -1; |
178 | } | |
179 | ||
d1a0cf73 SB |
180 | return 0; |
181 | } | |
182 | ||
183 | /* | |
184 | * Parse the TPM configuration options. | |
185 | * To display all available TPM backends the user may use '-tpmdev help' | |
186 | */ | |
187 | int tpm_config_parse(QemuOptsList *opts_list, const char *optarg) | |
188 | { | |
189 | QemuOpts *opts; | |
190 | ||
191 | if (!strcmp(optarg, "help")) { | |
192 | tpm_display_backend_drivers(); | |
193 | return -1; | |
194 | } | |
70b94331 | 195 | opts = qemu_opts_parse_noisily(opts_list, optarg, true); |
d1a0cf73 SB |
196 | if (!opts) { |
197 | return -1; | |
198 | } | |
199 | return 0; | |
200 | } | |
201 | ||
202 | #endif /* CONFIG_TPM */ | |
203 | ||
d1a0cf73 SB |
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) { | |
00bbf50a | 214 | if (!tpm_models[drv->fe_model]) { |
d1a0cf73 SB |
215 | continue; |
216 | } | |
217 | info = g_new0(TPMInfoList, 1); | |
f59864ba | 218 | info->value = tpm_backend_query_tpm(drv); |
d1a0cf73 SB |
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 | ||
7fb1cf16 | 236 | for (i = 0; i < TPM_TYPE__MAX; i++) { |
d1a0cf73 SB |
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 | ||
7fb1cf16 | 260 | for (i = 0; i < TPM_MODEL__MAX; i++) { |
00bbf50a | 261 | if (!tpm_models[i]) { |
d1a0cf73 SB |
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 | } |