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 "system/tpm_backend.h"
21 #include "system/tpm.h"
22 #include "qemu/config-file.h"
23 #include "qemu/error-report.h"
24 #include "qemu/help_option.h"
25
26 static QLIST_HEAD(, TPMBackend) tpm_backends =
27 QLIST_HEAD_INITIALIZER(tpm_backends);
28
29 static const TPMBackendClass *
tpm_be_find_by_type(enum TpmType type)30 tpm_be_find_by_type(enum TpmType type)
31 {
32 ObjectClass *oc;
33 char *typename = g_strdup_printf("tpm-%s", TpmType_str(type));
34
35 oc = object_class_by_name(typename);
36 g_free(typename);
37
38 if (!object_class_dynamic_cast(oc, TYPE_TPM_BACKEND)) {
39 return NULL;
40 }
41
42 return TPM_BACKEND_CLASS(oc);
43 }
44
45 /*
46 * Walk the list of available TPM backend drivers and display them on the
47 * screen.
48 */
tpm_display_backend_drivers(void)49 static void tpm_display_backend_drivers(void)
50 {
51 bool got_one = false;
52 int i;
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 if (!got_one) {
60 error_printf("Supported TPM types (choose only one):\n");
61 got_one = true;
62 }
63 error_printf("%12s %s\n", TpmType_str(i), bc->desc);
64 }
65 if (!got_one) {
66 error_printf("No TPM backend types are available\n");
67 }
68 }
69
70 /*
71 * Find the TPM with the given Id
72 */
qemu_find_tpm_be(const char * id)73 TPMBackend *qemu_find_tpm_be(const char *id)
74 {
75 TPMBackend *drv;
76
77 if (id) {
78 QLIST_FOREACH(drv, &tpm_backends, list) {
79 if (!strcmp(drv->id, id)) {
80 return drv;
81 }
82 }
83 }
84
85 return NULL;
86 }
87
tpm_init_tpmdev(void * dummy,QemuOpts * opts,Error ** errp)88 static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
89 {
90 /*
91 * Use of error_report() in a function with an Error ** parameter
92 * is suspicious. It is okay here. The parameter only exists to
93 * make the function usable with qemu_opts_foreach(). It is not
94 * actually used.
95 */
96 const char *value;
97 const char *id;
98 const TPMBackendClass *be;
99 TPMBackend *drv;
100 Error *local_err = NULL;
101 int i;
102
103 if (!QLIST_EMPTY(&tpm_backends)) {
104 error_report("Only one TPM is allowed.");
105 return 1;
106 }
107
108 id = qemu_opts_id(opts);
109 if (id == NULL) {
110 error_report(QERR_MISSING_PARAMETER, "id");
111 return 1;
112 }
113
114 value = qemu_opt_get(opts, "type");
115 if (!value) {
116 error_report(QERR_MISSING_PARAMETER, "type");
117 tpm_display_backend_drivers();
118 return 1;
119 }
120
121 i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL);
122 be = i >= 0 ? tpm_be_find_by_type(i) : NULL;
123 if (be == NULL) {
124 error_report(QERR_INVALID_PARAMETER_VALUE,
125 "type", "a TPM backend type");
126 tpm_display_backend_drivers();
127 return 1;
128 }
129
130 /* validate backend specific opts */
131 if (!qemu_opts_validate(opts, be->opts, &local_err)) {
132 error_report_err(local_err);
133 return 1;
134 }
135
136 drv = be->create(opts);
137 if (!drv) {
138 return 1;
139 }
140
141 drv->id = g_strdup(id);
142 QLIST_INSERT_HEAD(&tpm_backends, drv, list);
143
144 return 0;
145 }
146
147 /*
148 * Walk the list of TPM backend drivers that are in use and call their
149 * destroy function to have them cleaned up.
150 */
tpm_cleanup(void)151 void tpm_cleanup(void)
152 {
153 TPMBackend *drv, *next;
154
155 QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
156 QLIST_REMOVE(drv, list);
157 object_unref(OBJECT(drv));
158 }
159 }
160
161 /*
162 * Initialize the TPM. Process the tpmdev command line options describing the
163 * TPM backend.
164 */
tpm_init(void)165 int tpm_init(void)
166 {
167 if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
168 tpm_init_tpmdev, NULL, NULL)) {
169 return -1;
170 }
171
172 return 0;
173 }
174
175 /*
176 * Parse the TPM configuration options.
177 * To display all available TPM backends the user may use '-tpmdev help'
178 */
tpm_config_parse(QemuOptsList * opts_list,const char * optstr)179 int tpm_config_parse(QemuOptsList *opts_list, const char *optstr)
180 {
181 QemuOpts *opts;
182
183 if (is_help_option(optstr)) {
184 tpm_display_backend_drivers();
185 exit(EXIT_SUCCESS);
186 }
187 opts = qemu_opts_parse_noisily(opts_list, optstr, true);
188 if (!opts) {
189 return -1;
190 }
191 return 0;
192 }
193
194 /*
195 * Walk the list of active TPM backends and collect information about them.
196 */
qmp_query_tpm(Error ** errp)197 TPMInfoList *qmp_query_tpm(Error **errp)
198 {
199 TPMBackend *drv;
200 TPMInfoList *head = NULL, **tail = &head;
201
202 QLIST_FOREACH(drv, &tpm_backends, list) {
203 if (!drv->tpmif) {
204 continue;
205 }
206
207 QAPI_LIST_APPEND(tail, tpm_backend_query_tpm(drv));
208 }
209
210 return head;
211 }
212
qmp_query_tpm_types(Error ** errp)213 TpmTypeList *qmp_query_tpm_types(Error **errp)
214 {
215 unsigned int i = 0;
216 TpmTypeList *head = NULL, **tail = &head;
217
218 for (i = 0; i < TPM_TYPE__MAX; i++) {
219 if (!tpm_be_find_by_type(i)) {
220 continue;
221 }
222 QAPI_LIST_APPEND(tail, i);
223 }
224
225 return head;
226 }
qmp_query_tpm_models(Error ** errp)227 TpmModelList *qmp_query_tpm_models(Error **errp)
228 {
229 TpmModelList *head = NULL, **tail = &head;
230 GSList *e, *l = object_class_get_list(TYPE_TPM_IF, false);
231
232 for (e = l; e; e = e->next) {
233 TPMIfClass *c = TPM_IF_CLASS(e->data);
234
235 QAPI_LIST_APPEND(tail, c->model);
236 }
237 g_slist_free(l);
238
239 return head;
240 }
241