]> git.proxmox.com Git - mirror_qemu.git/blob - tpm.c
tpm-backend: move set 'id' to common code
[mirror_qemu.git] / tpm.c
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 #include "qemu/osdep.h"
15
16 #include "qapi/qmp/qerror.h"
17 #include "sysemu/tpm_backend.h"
18 #include "sysemu/tpm.h"
19 #include "qemu/config-file.h"
20 #include "qemu/error-report.h"
21 #include "qmp-commands.h"
22
23 static QLIST_HEAD(, TPMBackend) tpm_backends =
24 QLIST_HEAD_INITIALIZER(tpm_backends);
25
26 static bool tpm_models[TPM_MODEL__MAX];
27
28 void tpm_register_model(enum TpmModel model)
29 {
30 tpm_models[model] = true;
31 }
32
33 static const TPMBackendClass *
34 tpm_be_find_by_type(enum TpmType type)
35 {
36 ObjectClass *oc;
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
46 return TPM_BACKEND_CLASS(oc);
47 }
48
49 /*
50 * Walk the list of available TPM backend drivers and display them on the
51 * screen.
52 */
53 static void tpm_display_backend_drivers(void)
54 {
55 int i;
56
57 fprintf(stderr, "Supported TPM types (choose only one):\n");
58
59 for (i = 0; i < TPM_TYPE__MAX; i++) {
60 const TPMBackendClass *bc = tpm_be_find_by_type(i);
61 if (!bc) {
62 continue;
63 }
64 fprintf(stderr, "%12s %s\n", TpmType_str(i), bc->desc);
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
87 static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
88 {
89 const char *value;
90 const char *id;
91 const TPMBackendClass *be;
92 TPMBackend *drv;
93 Error *local_err = NULL;
94 int i;
95
96 if (!QLIST_EMPTY(&tpm_backends)) {
97 error_report("Only one TPM is allowed.");
98 return 1;
99 }
100
101 id = qemu_opts_id(opts);
102 if (id == NULL) {
103 error_report(QERR_MISSING_PARAMETER, "id");
104 return 1;
105 }
106
107 value = qemu_opt_get(opts, "type");
108 if (!value) {
109 error_report(QERR_MISSING_PARAMETER, "type");
110 tpm_display_backend_drivers();
111 return 1;
112 }
113
114 i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL);
115 be = i >= 0 ? tpm_be_find_by_type(i) : NULL;
116 if (be == NULL) {
117 error_report(QERR_INVALID_PARAMETER_VALUE,
118 "type", "a TPM backend type");
119 tpm_display_backend_drivers();
120 return 1;
121 }
122
123 /* validate backend specific opts */
124 qemu_opts_validate(opts, be->opts, &local_err);
125 if (local_err) {
126 error_report_err(local_err);
127 return 1;
128 }
129
130 drv = be->create(opts);
131 if (!drv) {
132 return 1;
133 }
134
135 drv->id = g_strdup(id);
136 QLIST_INSERT_HEAD(&tpm_backends, drv, list);
137
138 return 0;
139 }
140
141 /*
142 * Walk the list of TPM backend drivers that are in use and call their
143 * destroy function to have them cleaned up.
144 */
145 void tpm_cleanup(void)
146 {
147 TPMBackend *drv, *next;
148
149 QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
150 QLIST_REMOVE(drv, list);
151 object_unref(OBJECT(drv));
152 }
153 }
154
155 /*
156 * Initialize the TPM. Process the tpmdev command line options describing the
157 * TPM backend.
158 */
159 int tpm_init(void)
160 {
161 if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
162 tpm_init_tpmdev, NULL, NULL)) {
163 return -1;
164 }
165
166 return 0;
167 }
168
169 /*
170 * Parse the TPM configuration options.
171 * To display all available TPM backends the user may use '-tpmdev help'
172 */
173 int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
174 {
175 QemuOpts *opts;
176
177 if (!strcmp(optarg, "help")) {
178 tpm_display_backend_drivers();
179 return -1;
180 }
181 opts = qemu_opts_parse_noisily(opts_list, optarg, true);
182 if (!opts) {
183 return -1;
184 }
185 return 0;
186 }
187
188 /*
189 * Walk the list of active TPM backends and collect information about them
190 * following the schema description in qapi-schema.json.
191 */
192 TPMInfoList *qmp_query_tpm(Error **errp)
193 {
194 TPMBackend *drv;
195 TPMInfoList *info, *head = NULL, *cur_item = NULL;
196
197 QLIST_FOREACH(drv, &tpm_backends, list) {
198 if (!drv->tpmif) {
199 continue;
200 }
201
202 info = g_new0(TPMInfoList, 1);
203 info->value = tpm_backend_query_tpm(drv);
204
205 if (!cur_item) {
206 head = cur_item = info;
207 } else {
208 cur_item->next = info;
209 cur_item = info;
210 }
211 }
212
213 return head;
214 }
215
216 TpmTypeList *qmp_query_tpm_types(Error **errp)
217 {
218 unsigned int i = 0;
219 TpmTypeList *head = NULL, *prev = NULL, *cur_item;
220
221 for (i = 0; i < TPM_TYPE__MAX; i++) {
222 if (!tpm_be_find_by_type(i)) {
223 continue;
224 }
225 cur_item = g_new0(TpmTypeList, 1);
226 cur_item->value = i;
227
228 if (prev) {
229 prev->next = cur_item;
230 }
231 if (!head) {
232 head = cur_item;
233 }
234 prev = cur_item;
235 }
236
237 return head;
238 }
239
240 TpmModelList *qmp_query_tpm_models(Error **errp)
241 {
242 unsigned int i = 0;
243 TpmModelList *head = NULL, *prev = NULL, *cur_item;
244
245 for (i = 0; i < TPM_MODEL__MAX; i++) {
246 if (!tpm_models[i]) {
247 continue;
248 }
249 cur_item = g_new0(TpmModelList, 1);
250 cur_item->value = i;
251
252 if (prev) {
253 prev->next = cur_item;
254 }
255 if (!head) {
256 head = cur_item;
257 }
258 prev = cur_item;
259 }
260
261 return head;
262 }