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