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