tpm: remove TPMDriverOps
[qemu.git] / tpm.c
blob4882501bd7fa2d6ca5d4ce515e8d2533d1385917
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 "qemu/osdep.h"
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"
23 static QLIST_HEAD(, TPMBackend) tpm_backends =
24 QLIST_HEAD_INITIALIZER(tpm_backends);
26 static bool tpm_models[TPM_MODEL__MAX];
28 void tpm_register_model(enum TpmModel model)
30 tpm_models[model] = true;
33 #ifdef CONFIG_TPM
35 static const TPMBackendClass *
36 tpm_be_find_by_type(enum TpmType type)
38 ObjectClass *oc;
39 char *typename = g_strdup_printf("tpm-%s", TpmType_str(type));
41 oc = object_class_by_name(typename);
42 g_free(typename);
44 if (!object_class_dynamic_cast(oc, TYPE_TPM_BACKEND)) {
45 return NULL;
48 return TPM_BACKEND_CLASS(oc);
52 * Walk the list of available TPM backend drivers and display them on the
53 * screen.
55 static void tpm_display_backend_drivers(void)
57 int i;
59 fprintf(stderr, "Supported TPM types (choose only one):\n");
61 for (i = 0; i < TPM_TYPE__MAX; i++) {
62 const TPMBackendClass *bc = tpm_be_find_by_type(i);
63 if (!bc) {
64 continue;
66 fprintf(stderr, "%12s %s\n", TpmType_str(i), bc->desc);
68 fprintf(stderr, "\n");
72 * Find the TPM with the given Id
74 TPMBackend *qemu_find_tpm(const char *id)
76 TPMBackend *drv;
78 if (id) {
79 QLIST_FOREACH(drv, &tpm_backends, list) {
80 if (!strcmp(drv->id, id)) {
81 return drv;
86 return NULL;
89 static int configure_tpm(QemuOpts *opts)
91 const char *value;
92 const char *id;
93 const TPMBackendClass *be;
94 TPMBackend *drv;
95 Error *local_err = NULL;
96 int i;
98 if (!QLIST_EMPTY(&tpm_backends)) {
99 error_report("Only one TPM is allowed.");
100 return 1;
103 id = qemu_opts_id(opts);
104 if (id == NULL) {
105 error_report(QERR_MISSING_PARAMETER, "id");
106 return 1;
109 value = qemu_opt_get(opts, "type");
110 if (!value) {
111 error_report(QERR_MISSING_PARAMETER, "type");
112 tpm_display_backend_drivers();
113 return 1;
116 i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL);
117 be = i >= 0 ? tpm_be_find_by_type(i) : NULL;
118 if (be == NULL) {
119 error_report(QERR_INVALID_PARAMETER_VALUE,
120 "type", "a TPM backend type");
121 tpm_display_backend_drivers();
122 return 1;
125 /* validate backend specific opts */
126 qemu_opts_validate(opts, be->opts, &local_err);
127 if (local_err) {
128 error_report_err(local_err);
129 return 1;
132 drv = be->create(opts, id);
133 if (!drv) {
134 return 1;
137 tpm_backend_open(drv, &local_err);
138 if (local_err) {
139 error_report_err(local_err);
140 return 1;
143 QLIST_INSERT_HEAD(&tpm_backends, drv, list);
145 return 0;
148 static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
150 return configure_tpm(opts);
154 * Walk the list of TPM backend drivers that are in use and call their
155 * destroy function to have them cleaned up.
157 void tpm_cleanup(void)
159 TPMBackend *drv, *next;
161 QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
162 QLIST_REMOVE(drv, list);
163 object_unref(OBJECT(drv));
168 * Initialize the TPM. Process the tpmdev command line options describing the
169 * TPM backend.
171 int tpm_init(void)
173 if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
174 tpm_init_tpmdev, NULL, NULL)) {
175 return -1;
178 return 0;
182 * Parse the TPM configuration options.
183 * To display all available TPM backends the user may use '-tpmdev help'
185 int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
187 QemuOpts *opts;
189 if (!strcmp(optarg, "help")) {
190 tpm_display_backend_drivers();
191 return -1;
193 opts = qemu_opts_parse_noisily(opts_list, optarg, true);
194 if (!opts) {
195 return -1;
197 return 0;
200 #endif /* CONFIG_TPM */
203 * Walk the list of active TPM backends and collect information about them
204 * following the schema description in qapi-schema.json.
206 TPMInfoList *qmp_query_tpm(Error **errp)
208 TPMBackend *drv;
209 TPMInfoList *info, *head = NULL, *cur_item = NULL;
211 QLIST_FOREACH(drv, &tpm_backends, list) {
212 if (!tpm_models[drv->fe_model]) {
213 continue;
215 info = g_new0(TPMInfoList, 1);
216 info->value = tpm_backend_query_tpm(drv);
218 if (!cur_item) {
219 head = cur_item = info;
220 } else {
221 cur_item->next = info;
222 cur_item = info;
226 return head;
229 TpmTypeList *qmp_query_tpm_types(Error **errp)
231 unsigned int i = 0;
232 TpmTypeList *head = NULL, *prev = NULL, *cur_item;
234 for (i = 0; i < TPM_TYPE__MAX; i++) {
235 if (!tpm_be_find_by_type(i)) {
236 continue;
238 cur_item = g_new0(TpmTypeList, 1);
239 cur_item->value = i;
241 if (prev) {
242 prev->next = cur_item;
244 if (!head) {
245 head = cur_item;
247 prev = cur_item;
250 return head;
253 TpmModelList *qmp_query_tpm_models(Error **errp)
255 unsigned int i = 0;
256 TpmModelList *head = NULL, *prev = NULL, *cur_item;
258 for (i = 0; i < TPM_MODEL__MAX; i++) {
259 if (!tpm_models[i]) {
260 continue;
262 cur_item = g_new0(TpmModelList, 1);
263 cur_item->value = i;
265 if (prev) {
266 prev->next = cur_item;
268 if (!head) {
269 head = cur_item;
271 prev = cur_item;
274 return head;