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