4 * Copyright (C) 2011-2013 IBM Corporation
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.
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
)
32 char *typename
= g_strdup_printf("tpm-%s", TpmType_str(type
));
34 oc
= object_class_by_name(typename
);
37 if (!object_class_dynamic_cast(oc
, TYPE_TPM_BACKEND
)) {
41 return TPM_BACKEND_CLASS(oc
);
45 * Walk the list of available TPM backend drivers and display them on the
48 static void tpm_display_backend_drivers(void)
53 for (i
= 0; i
< TPM_TYPE__MAX
; i
++) {
54 const TPMBackendClass
*bc
= tpm_be_find_by_type(i
);
59 error_printf("Supported TPM types (choose only one):\n");
62 error_printf("%12s %s\n", TpmType_str(i
), bc
->desc
);
65 error_printf("No TPM backend types are available\n");
70 * Find the TPM with the given Id
72 TPMBackend
*qemu_find_tpm_be(const char *id
)
77 QLIST_FOREACH(drv
, &tpm_backends
, list
) {
78 if (!strcmp(drv
->id
, id
)) {
87 static int tpm_init_tpmdev(void *dummy
, QemuOpts
*opts
, Error
**errp
)
90 * Use of error_report() in a function with an Error ** parameter
91 * is suspicious. It is okay here. The parameter only exists to
92 * make the function usable with qemu_opts_foreach(). It is not
97 const TPMBackendClass
*be
;
99 Error
*local_err
= NULL
;
102 if (!QLIST_EMPTY(&tpm_backends
)) {
103 error_report("Only one TPM is allowed.");
107 id
= qemu_opts_id(opts
);
109 error_report(QERR_MISSING_PARAMETER
, "id");
113 value
= qemu_opt_get(opts
, "type");
115 error_report(QERR_MISSING_PARAMETER
, "type");
116 tpm_display_backend_drivers();
120 i
= qapi_enum_parse(&TpmType_lookup
, value
, -1, NULL
);
121 be
= i
>= 0 ? tpm_be_find_by_type(i
) : NULL
;
123 error_report(QERR_INVALID_PARAMETER_VALUE
,
124 "type", "a TPM backend type");
125 tpm_display_backend_drivers();
129 /* validate backend specific opts */
130 if (!qemu_opts_validate(opts
, be
->opts
, &local_err
)) {
131 error_report_err(local_err
);
135 drv
= be
->create(opts
);
140 drv
->id
= g_strdup(id
);
141 QLIST_INSERT_HEAD(&tpm_backends
, drv
, list
);
147 * Walk the list of TPM backend drivers that are in use and call their
148 * destroy function to have them cleaned up.
150 void tpm_cleanup(void)
152 TPMBackend
*drv
, *next
;
154 QLIST_FOREACH_SAFE(drv
, &tpm_backends
, list
, next
) {
155 QLIST_REMOVE(drv
, list
);
156 object_unref(OBJECT(drv
));
161 * Initialize the TPM. Process the tpmdev command line options describing the
166 if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
167 tpm_init_tpmdev
, NULL
, NULL
)) {
175 * Parse the TPM configuration options.
176 * To display all available TPM backends the user may use '-tpmdev help'
178 int tpm_config_parse(QemuOptsList
*opts_list
, const char *optarg
)
182 if (!strcmp(optarg
, "help")) {
183 tpm_display_backend_drivers();
186 opts
= qemu_opts_parse_noisily(opts_list
, optarg
, true);
194 * Walk the list of active TPM backends and collect information about them.
196 TPMInfoList
*qmp_query_tpm(Error
**errp
)
199 TPMInfoList
*info
, *head
= NULL
, *cur_item
= NULL
;
201 QLIST_FOREACH(drv
, &tpm_backends
, list
) {
206 info
= g_new0(TPMInfoList
, 1);
207 info
->value
= tpm_backend_query_tpm(drv
);
210 head
= cur_item
= info
;
212 cur_item
->next
= info
;
220 TpmTypeList
*qmp_query_tpm_types(Error
**errp
)
223 TpmTypeList
*head
= NULL
, *prev
= NULL
, *cur_item
;
225 for (i
= 0; i
< TPM_TYPE__MAX
; i
++) {
226 if (!tpm_be_find_by_type(i
)) {
229 cur_item
= g_new0(TpmTypeList
, 1);
233 prev
->next
= cur_item
;
243 TpmModelList
*qmp_query_tpm_models(Error
**errp
)
245 TpmModelList
*head
= NULL
, *prev
= NULL
, *cur_item
;
246 GSList
*e
, *l
= object_class_get_list(TYPE_TPM_IF
, false);
248 for (e
= l
; e
; e
= e
->next
) {
249 TPMIfClass
*c
= TPM_IF_CLASS(e
->data
);
251 cur_item
= g_new0(TpmModelList
, 1);
252 cur_item
->value
= c
->model
;
255 prev
->next
= cur_item
;