Include qapi/qmp/qerror.h exactly where needed
[qemu.git] / tpm.c
blob5ca3eb5ed00523c3f72afc2390e8dcbd33765f47
1 /*
2 * TPM configuration
4 * Copyright (C) 2011-2013 IBM Corporation
6 * Authors:
7 * Stefan Berger <stefanb@us.ibm.com>
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.
12 * Based on net.c
14 #include "config-host.h"
16 #include "monitor/monitor.h"
17 #include "qapi/qmp/qerror.h"
18 #include "sysemu/tpm_backend.h"
19 #include "sysemu/tpm.h"
20 #include "qemu/config-file.h"
21 #include "qemu/error-report.h"
22 #include "qmp-commands.h"
24 static QLIST_HEAD(, TPMBackend) tpm_backends =
25 QLIST_HEAD_INITIALIZER(tpm_backends);
28 #define TPM_MAX_MODELS 1
29 #define TPM_MAX_DRIVERS 1
31 static TPMDriverOps const *be_drivers[TPM_MAX_DRIVERS] = {
32 NULL,
35 static enum TpmModel tpm_models[TPM_MAX_MODELS] = {
36 TPM_MODEL_MAX,
39 int tpm_register_model(enum TpmModel model)
41 int i;
43 for (i = 0; i < TPM_MAX_MODELS; i++) {
44 if (tpm_models[i] == TPM_MODEL_MAX) {
45 tpm_models[i] = model;
46 return 0;
49 error_report("Could not register TPM model");
50 return 1;
53 static bool tpm_model_is_registered(enum TpmModel model)
55 int i;
57 for (i = 0; i < TPM_MAX_MODELS; i++) {
58 if (tpm_models[i] == model) {
59 return true;
62 return false;
65 const TPMDriverOps *tpm_get_backend_driver(const char *type)
67 int i;
69 for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
70 if (!strcmp(TpmType_lookup[be_drivers[i]->type], type)) {
71 return be_drivers[i];
75 return NULL;
78 #ifdef CONFIG_TPM
80 int tpm_register_driver(const TPMDriverOps *tdo)
82 int i;
84 for (i = 0; i < TPM_MAX_DRIVERS; i++) {
85 if (!be_drivers[i]) {
86 be_drivers[i] = tdo;
87 return 0;
90 error_report("Could not register TPM driver");
91 return 1;
95 * Walk the list of available TPM backend drivers and display them on the
96 * screen.
98 static void tpm_display_backend_drivers(void)
100 int i;
102 fprintf(stderr, "Supported TPM types (choose only one):\n");
104 for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
105 fprintf(stderr, "%12s %s\n",
106 TpmType_lookup[be_drivers[i]->type], be_drivers[i]->desc());
108 fprintf(stderr, "\n");
112 * Find the TPM with the given Id
114 TPMBackend *qemu_find_tpm(const char *id)
116 TPMBackend *drv;
118 if (id) {
119 QLIST_FOREACH(drv, &tpm_backends, list) {
120 if (!strcmp(drv->id, id)) {
121 return drv;
126 return NULL;
129 static int configure_tpm(QemuOpts *opts)
131 const char *value;
132 const char *id;
133 const TPMDriverOps *be;
134 TPMBackend *drv;
135 Error *local_err = NULL;
137 if (!QLIST_EMPTY(&tpm_backends)) {
138 error_report("Only one TPM is allowed.");
139 return 1;
142 id = qemu_opts_id(opts);
143 if (id == NULL) {
144 error_report(QERR_MISSING_PARAMETER, "id");
145 return 1;
148 value = qemu_opt_get(opts, "type");
149 if (!value) {
150 error_report(QERR_MISSING_PARAMETER, "type");
151 tpm_display_backend_drivers();
152 return 1;
155 be = tpm_get_backend_driver(value);
156 if (be == NULL) {
157 error_report(QERR_INVALID_PARAMETER_VALUE,
158 "type", "a TPM backend type");
159 tpm_display_backend_drivers();
160 return 1;
163 /* validate backend specific opts */
164 qemu_opts_validate(opts, be->opts, &local_err);
165 if (local_err) {
166 error_report_err(local_err);
167 return 1;
170 drv = be->create(opts, id);
171 if (!drv) {
172 return 1;
175 tpm_backend_open(drv, &local_err);
176 if (local_err) {
177 error_report_err(local_err);
178 return 1;
181 QLIST_INSERT_HEAD(&tpm_backends, drv, list);
183 return 0;
186 static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
188 return configure_tpm(opts);
192 * Walk the list of TPM backend drivers that are in use and call their
193 * destroy function to have them cleaned up.
195 void tpm_cleanup(void)
197 TPMBackend *drv, *next;
199 QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
200 QLIST_REMOVE(drv, list);
201 tpm_backend_destroy(drv);
206 * Initialize the TPM. Process the tpmdev command line options describing the
207 * TPM backend.
209 int tpm_init(void)
211 if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
212 tpm_init_tpmdev, NULL, NULL)) {
213 return -1;
216 atexit(tpm_cleanup);
217 return 0;
221 * Parse the TPM configuration options.
222 * To display all available TPM backends the user may use '-tpmdev help'
224 int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
226 QemuOpts *opts;
228 if (!strcmp(optarg, "help")) {
229 tpm_display_backend_drivers();
230 return -1;
232 opts = qemu_opts_parse_noisily(opts_list, optarg, true);
233 if (!opts) {
234 return -1;
236 return 0;
239 #endif /* CONFIG_TPM */
241 static const TPMDriverOps *tpm_driver_find_by_type(enum TpmType type)
243 int i;
245 for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
246 if (be_drivers[i]->type == type) {
247 return be_drivers[i];
250 return NULL;
253 static TPMInfo *qmp_query_tpm_inst(TPMBackend *drv)
255 TPMInfo *res = g_new0(TPMInfo, 1);
256 TPMPassthroughOptions *tpo;
258 res->id = g_strdup(drv->id);
259 res->model = drv->fe_model;
260 res->options = g_new0(TpmTypeOptions, 1);
262 switch (drv->ops->type) {
263 case TPM_TYPE_PASSTHROUGH:
264 res->options->kind = TPM_TYPE_OPTIONS_KIND_PASSTHROUGH;
265 tpo = g_new0(TPMPassthroughOptions, 1);
266 res->options->passthrough = tpo;
267 if (drv->path) {
268 tpo->path = g_strdup(drv->path);
269 tpo->has_path = true;
271 if (drv->cancel_path) {
272 tpo->cancel_path = g_strdup(drv->cancel_path);
273 tpo->has_cancel_path = true;
275 break;
276 case TPM_TYPE_MAX:
277 break;
280 return res;
284 * Walk the list of active TPM backends and collect information about them
285 * following the schema description in qapi-schema.json.
287 TPMInfoList *qmp_query_tpm(Error **errp)
289 TPMBackend *drv;
290 TPMInfoList *info, *head = NULL, *cur_item = NULL;
292 QLIST_FOREACH(drv, &tpm_backends, list) {
293 if (!tpm_model_is_registered(drv->fe_model)) {
294 continue;
296 info = g_new0(TPMInfoList, 1);
297 info->value = qmp_query_tpm_inst(drv);
299 if (!cur_item) {
300 head = cur_item = info;
301 } else {
302 cur_item->next = info;
303 cur_item = info;
307 return head;
310 TpmTypeList *qmp_query_tpm_types(Error **errp)
312 unsigned int i = 0;
313 TpmTypeList *head = NULL, *prev = NULL, *cur_item;
315 for (i = 0; i < TPM_TYPE_MAX; i++) {
316 if (!tpm_driver_find_by_type(i)) {
317 continue;
319 cur_item = g_new0(TpmTypeList, 1);
320 cur_item->value = i;
322 if (prev) {
323 prev->next = cur_item;
325 if (!head) {
326 head = cur_item;
328 prev = cur_item;
331 return head;
334 TpmModelList *qmp_query_tpm_models(Error **errp)
336 unsigned int i = 0;
337 TpmModelList *head = NULL, *prev = NULL, *cur_item;
339 for (i = 0; i < TPM_MODEL_MAX; i++) {
340 if (!tpm_model_is_registered(i)) {
341 continue;
343 cur_item = g_new0(TpmModelList, 1);
344 cur_item->value = i;
346 if (prev) {
347 prev->next = cur_item;
349 if (!head) {
350 head = cur_item;
352 prev = cur_item;
355 return head;