]>
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 | */ | |
e688df6b | 14 | |
d38ea87a | 15 | #include "qemu/osdep.h" |
d1a0cf73 | 16 | |
e688df6b | 17 | #include "qapi/error.h" |
9af23989 | 18 | #include "qapi/qapi-commands-tpm.h" |
d1a0cf73 | 19 | #include "qapi/qmp/qerror.h" |
dccfcd0e | 20 | #include "sysemu/tpm_backend.h" |
bdee56f5 | 21 | #include "sysemu/tpm.h" |
d1a0cf73 | 22 | #include "qemu/config-file.h" |
d49b6836 | 23 | #include "qemu/error-report.h" |
d1a0cf73 SB |
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; |
d91a7a57 | 88 | int i; |
d1a0cf73 SB |
89 | |
90 | if (!QLIST_EMPTY(&tpm_backends)) { | |
d10e05f1 | 91 | error_setg(errp, "Only one TPM is allowed."); |
d1a0cf73 SB |
92 | return 1; |
93 | } | |
94 | ||
95 | id = qemu_opts_id(opts); | |
96 | if (id == NULL) { | |
d10e05f1 | 97 | error_setg(errp, QERR_MISSING_PARAMETER, "id"); |
d1a0cf73 SB |
98 | return 1; |
99 | } | |
100 | ||
101 | value = qemu_opt_get(opts, "type"); | |
102 | if (!value) { | |
d10e05f1 | 103 | error_setg(errp, QERR_MISSING_PARAMETER, "type"); |
d1a0cf73 SB |
104 | tpm_display_backend_drivers(); |
105 | return 1; | |
106 | } | |
107 | ||
d91a7a57 | 108 | i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL); |
d31076ba | 109 | be = i >= 0 ? tpm_be_find_by_type(i) : NULL; |
d1a0cf73 | 110 | if (be == NULL) { |
d10e05f1 MA |
111 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type", |
112 | "a TPM backend type"); | |
d1a0cf73 SB |
113 | tpm_display_backend_drivers(); |
114 | return 1; | |
115 | } | |
116 | ||
bb716238 | 117 | /* validate backend specific opts */ |
668f62ec | 118 | if (!qemu_opts_validate(opts, be->opts, errp)) { |
bb716238 SB |
119 | return 1; |
120 | } | |
121 | ||
9f7c0ef2 | 122 | drv = be->create(opts); |
d1a0cf73 SB |
123 | if (!drv) { |
124 | return 1; | |
125 | } | |
126 | ||
9f7c0ef2 | 127 | drv->id = g_strdup(id); |
d1a0cf73 SB |
128 | QLIST_INSERT_HEAD(&tpm_backends, drv, list); |
129 | ||
130 | return 0; | |
131 | } | |
132 | ||
d1a0cf73 SB |
133 | /* |
134 | * Walk the list of TPM backend drivers that are in use and call their | |
135 | * destroy function to have them cleaned up. | |
136 | */ | |
137 | void tpm_cleanup(void) | |
138 | { | |
139 | TPMBackend *drv, *next; | |
140 | ||
141 | QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) { | |
142 | QLIST_REMOVE(drv, list); | |
f35fe5cb | 143 | object_unref(OBJECT(drv)); |
d1a0cf73 SB |
144 | } |
145 | } | |
146 | ||
147 | /* | |
148 | * Initialize the TPM. Process the tpmdev command line options describing the | |
149 | * TPM backend. | |
150 | */ | |
d10e05f1 | 151 | void tpm_init(void) |
d1a0cf73 | 152 | { |
d10e05f1 MA |
153 | qemu_opts_foreach(qemu_find_opts("tpmdev"), |
154 | tpm_init_tpmdev, NULL, &error_fatal); | |
d1a0cf73 SB |
155 | } |
156 | ||
157 | /* | |
158 | * Parse the TPM configuration options. | |
159 | * To display all available TPM backends the user may use '-tpmdev help' | |
160 | */ | |
161 | int tpm_config_parse(QemuOptsList *opts_list, const char *optarg) | |
162 | { | |
163 | QemuOpts *opts; | |
164 | ||
165 | if (!strcmp(optarg, "help")) { | |
166 | tpm_display_backend_drivers(); | |
167 | return -1; | |
168 | } | |
70b94331 | 169 | opts = qemu_opts_parse_noisily(opts_list, optarg, true); |
d1a0cf73 SB |
170 | if (!opts) { |
171 | return -1; | |
172 | } | |
173 | return 0; | |
174 | } | |
175 | ||
d1a0cf73 | 176 | /* |
eb815e24 | 177 | * Walk the list of active TPM backends and collect information about them. |
d1a0cf73 SB |
178 | */ |
179 | TPMInfoList *qmp_query_tpm(Error **errp) | |
180 | { | |
181 | TPMBackend *drv; | |
182 | TPMInfoList *info, *head = NULL, *cur_item = NULL; | |
183 | ||
184 | QLIST_FOREACH(drv, &tpm_backends, list) { | |
191adc94 | 185 | if (!drv->tpmif) { |
d1a0cf73 SB |
186 | continue; |
187 | } | |
191adc94 | 188 | |
d1a0cf73 | 189 | info = g_new0(TPMInfoList, 1); |
f59864ba | 190 | info->value = tpm_backend_query_tpm(drv); |
d1a0cf73 SB |
191 | |
192 | if (!cur_item) { | |
193 | head = cur_item = info; | |
194 | } else { | |
195 | cur_item->next = info; | |
196 | cur_item = info; | |
197 | } | |
198 | } | |
199 | ||
200 | return head; | |
201 | } | |
202 | ||
203 | TpmTypeList *qmp_query_tpm_types(Error **errp) | |
204 | { | |
205 | unsigned int i = 0; | |
206 | TpmTypeList *head = NULL, *prev = NULL, *cur_item; | |
207 | ||
7fb1cf16 | 208 | for (i = 0; i < TPM_TYPE__MAX; i++) { |
d31076ba | 209 | if (!tpm_be_find_by_type(i)) { |
d1a0cf73 SB |
210 | continue; |
211 | } | |
212 | cur_item = g_new0(TpmTypeList, 1); | |
213 | cur_item->value = i; | |
214 | ||
215 | if (prev) { | |
216 | prev->next = cur_item; | |
217 | } | |
218 | if (!head) { | |
219 | head = cur_item; | |
220 | } | |
221 | prev = cur_item; | |
222 | } | |
223 | ||
224 | return head; | |
225 | } | |
d1a0cf73 SB |
226 | TpmModelList *qmp_query_tpm_models(Error **errp) |
227 | { | |
d1a0cf73 | 228 | TpmModelList *head = NULL, *prev = NULL, *cur_item; |
d3fd953f MAL |
229 | GSList *e, *l = object_class_get_list(TYPE_TPM_IF, false); |
230 | ||
231 | for (e = l; e; e = e->next) { | |
232 | TPMIfClass *c = TPM_IF_CLASS(e->data); | |
d1a0cf73 | 233 | |
d1a0cf73 | 234 | cur_item = g_new0(TpmModelList, 1); |
d3fd953f | 235 | cur_item->value = c->model; |
d1a0cf73 SB |
236 | |
237 | if (prev) { | |
238 | prev->next = cur_item; | |
239 | } | |
240 | if (!head) { | |
241 | head = cur_item; | |
242 | } | |
243 | prev = cur_item; | |
244 | } | |
d3fd953f | 245 | g_slist_free(l); |
d1a0cf73 SB |
246 | |
247 | return head; | |
248 | } |