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/qmp/qerror.h"
19 #include "sysemu/tpm_backend.h"
20 #include "sysemu/tpm.h"
21 #include "qemu/config-file.h"
22 #include "qemu/error-report.h"
23 #include "qmp-commands.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)
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
);
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
)
72 QLIST_FOREACH(drv
, &tpm_backends
, list
) {
73 if (!strcmp(drv
->id
, id
)) {
82 static int tpm_init_tpmdev(void *dummy
, QemuOpts
*opts
, Error
**errp
)
86 const TPMBackendClass
*be
;
88 Error
*local_err
= NULL
;
91 if (!QLIST_EMPTY(&tpm_backends
)) {
92 error_report("Only one TPM is allowed.");
96 id
= qemu_opts_id(opts
);
98 error_report(QERR_MISSING_PARAMETER
, "id");
102 value
= qemu_opt_get(opts
, "type");
104 error_report(QERR_MISSING_PARAMETER
, "type");
105 tpm_display_backend_drivers();
109 i
= qapi_enum_parse(&TpmType_lookup
, value
, -1, NULL
);
110 be
= i
>= 0 ? tpm_be_find_by_type(i
) : NULL
;
112 error_report(QERR_INVALID_PARAMETER_VALUE
,
113 "type", "a TPM backend type");
114 tpm_display_backend_drivers();
118 /* validate backend specific opts */
119 qemu_opts_validate(opts
, be
->opts
, &local_err
);
121 error_report_err(local_err
);
125 drv
= be
->create(opts
);
130 drv
->id
= g_strdup(id
);
131 QLIST_INSERT_HEAD(&tpm_backends
, drv
, list
);
137 * Walk the list of TPM backend drivers that are in use and call their
138 * destroy function to have them cleaned up.
140 void tpm_cleanup(void)
142 TPMBackend
*drv
, *next
;
144 QLIST_FOREACH_SAFE(drv
, &tpm_backends
, list
, next
) {
145 QLIST_REMOVE(drv
, list
);
146 object_unref(OBJECT(drv
));
151 * Initialize the TPM. Process the tpmdev command line options describing the
156 if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
157 tpm_init_tpmdev
, NULL
, NULL
)) {
165 * Parse the TPM configuration options.
166 * To display all available TPM backends the user may use '-tpmdev help'
168 int tpm_config_parse(QemuOptsList
*opts_list
, const char *optarg
)
172 if (!strcmp(optarg
, "help")) {
173 tpm_display_backend_drivers();
176 opts
= qemu_opts_parse_noisily(opts_list
, optarg
, true);
184 * Walk the list of active TPM backends and collect information about them
185 * following the schema description in qapi-schema.json.
187 TPMInfoList
*qmp_query_tpm(Error
**errp
)
190 TPMInfoList
*info
, *head
= NULL
, *cur_item
= NULL
;
192 QLIST_FOREACH(drv
, &tpm_backends
, list
) {
197 info
= g_new0(TPMInfoList
, 1);
198 info
->value
= tpm_backend_query_tpm(drv
);
201 head
= cur_item
= info
;
203 cur_item
->next
= info
;
211 TpmTypeList
*qmp_query_tpm_types(Error
**errp
)
214 TpmTypeList
*head
= NULL
, *prev
= NULL
, *cur_item
;
216 for (i
= 0; i
< TPM_TYPE__MAX
; i
++) {
217 if (!tpm_be_find_by_type(i
)) {
220 cur_item
= g_new0(TpmTypeList
, 1);
224 prev
->next
= cur_item
;
234 TpmModelList
*qmp_query_tpm_models(Error
**errp
)
236 TpmModelList
*head
= NULL
, *prev
= NULL
, *cur_item
;
237 GSList
*e
, *l
= object_class_get_list(TYPE_TPM_IF
, false);
239 for (e
= l
; e
; e
= e
->next
) {
240 TPMIfClass
*c
= TPM_IF_CLASS(e
->data
);
242 cur_item
= g_new0(TpmModelList
, 1);
243 cur_item
->value
= c
->model
;
246 prev
->next
= cur_item
;