Revert "tpm: Clean up error reporting in tpm_init_tpmdev()"
[qemu/ar7.git] / tpm.c
blobf6045bb6da8c6e0a1a1a10d1077085146730c9c5
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
15 #include "qemu/osdep.h"
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"
25 static QLIST_HEAD(, TPMBackend) tpm_backends =
26 QLIST_HEAD_INITIALIZER(tpm_backends);
28 static const TPMBackendClass *
29 tpm_be_find_by_type(enum TpmType type)
31 ObjectClass *oc;
32 char *typename = g_strdup_printf("tpm-%s", TpmType_str(type));
34 oc = object_class_by_name(typename);
35 g_free(typename);
37 if (!object_class_dynamic_cast(oc, TYPE_TPM_BACKEND)) {
38 return NULL;
41 return TPM_BACKEND_CLASS(oc);
45 * Walk the list of available TPM backend drivers and display them on the
46 * screen.
48 static void tpm_display_backend_drivers(void)
50 int i;
52 fprintf(stderr, "Supported TPM types (choose only one):\n");
54 for (i = 0; i < TPM_TYPE__MAX; i++) {
55 const TPMBackendClass *bc = tpm_be_find_by_type(i);
56 if (!bc) {
57 continue;
59 fprintf(stderr, "%12s %s\n", TpmType_str(i), bc->desc);
61 fprintf(stderr, "\n");
65 * Find the TPM with the given Id
67 TPMBackend *qemu_find_tpm_be(const char *id)
69 TPMBackend *drv;
71 if (id) {
72 QLIST_FOREACH(drv, &tpm_backends, list) {
73 if (!strcmp(drv->id, id)) {
74 return drv;
79 return NULL;
82 static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
85 * Use of error_report() in a function with an Error ** parameter
86 * is suspicious. It is okay here. The parameter only exists to
87 * make the function usable with qemu_opts_foreach(). It is not
88 * actually used.
90 const char *value;
91 const char *id;
92 const TPMBackendClass *be;
93 TPMBackend *drv;
94 Error *local_err = NULL;
95 int i;
97 if (!QLIST_EMPTY(&tpm_backends)) {
98 error_report("Only one TPM is allowed.");
99 return 1;
102 id = qemu_opts_id(opts);
103 if (id == NULL) {
104 error_report(QERR_MISSING_PARAMETER, "id");
105 return 1;
108 value = qemu_opt_get(opts, "type");
109 if (!value) {
110 error_report(QERR_MISSING_PARAMETER, "type");
111 tpm_display_backend_drivers();
112 return 1;
115 i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL);
116 be = i >= 0 ? tpm_be_find_by_type(i) : NULL;
117 if (be == NULL) {
118 error_report(QERR_INVALID_PARAMETER_VALUE,
119 "type", "a TPM backend type");
120 tpm_display_backend_drivers();
121 return 1;
124 /* validate backend specific opts */
125 if (!qemu_opts_validate(opts, be->opts, &local_err)) {
126 error_report_err(local_err);
127 return 1;
130 drv = be->create(opts);
131 if (!drv) {
132 return 1;
135 drv->id = g_strdup(id);
136 QLIST_INSERT_HEAD(&tpm_backends, drv, list);
138 return 0;
142 * Walk the list of TPM backend drivers that are in use and call their
143 * destroy function to have them cleaned up.
145 void tpm_cleanup(void)
147 TPMBackend *drv, *next;
149 QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
150 QLIST_REMOVE(drv, list);
151 object_unref(OBJECT(drv));
156 * Initialize the TPM. Process the tpmdev command line options describing the
157 * TPM backend.
159 int tpm_init(void)
161 if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
162 tpm_init_tpmdev, NULL, NULL)) {
163 return -1;
166 return 0;
170 * Parse the TPM configuration options.
171 * To display all available TPM backends the user may use '-tpmdev help'
173 int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
175 QemuOpts *opts;
177 if (!strcmp(optarg, "help")) {
178 tpm_display_backend_drivers();
179 return -1;
181 opts = qemu_opts_parse_noisily(opts_list, optarg, true);
182 if (!opts) {
183 return -1;
185 return 0;
189 * Walk the list of active TPM backends and collect information about them.
191 TPMInfoList *qmp_query_tpm(Error **errp)
193 TPMBackend *drv;
194 TPMInfoList *info, *head = NULL, *cur_item = NULL;
196 QLIST_FOREACH(drv, &tpm_backends, list) {
197 if (!drv->tpmif) {
198 continue;
201 info = g_new0(TPMInfoList, 1);
202 info->value = tpm_backend_query_tpm(drv);
204 if (!cur_item) {
205 head = cur_item = info;
206 } else {
207 cur_item->next = info;
208 cur_item = info;
212 return head;
215 TpmTypeList *qmp_query_tpm_types(Error **errp)
217 unsigned int i = 0;
218 TpmTypeList *head = NULL, *prev = NULL, *cur_item;
220 for (i = 0; i < TPM_TYPE__MAX; i++) {
221 if (!tpm_be_find_by_type(i)) {
222 continue;
224 cur_item = g_new0(TpmTypeList, 1);
225 cur_item->value = i;
227 if (prev) {
228 prev->next = cur_item;
230 if (!head) {
231 head = cur_item;
233 prev = cur_item;
236 return head;
238 TpmModelList *qmp_query_tpm_models(Error **errp)
240 TpmModelList *head = NULL, *prev = NULL, *cur_item;
241 GSList *e, *l = object_class_get_list(TYPE_TPM_IF, false);
243 for (e = l; e; e = e->next) {
244 TPMIfClass *c = TPM_IF_CLASS(e->data);
246 cur_item = g_new0(TpmModelList, 1);
247 cur_item->value = c->model;
249 if (prev) {
250 prev->next = cur_item;
252 if (!head) {
253 head = cur_item;
255 prev = cur_item;
257 g_slist_free(l);
259 return head;