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.
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 static const TPMBackendClass
*
34 tpm_be_find_by_type(enum TpmType type
)
37 char *typename
= g_strdup_printf("tpm-%s", TpmType_str(type
));
39 oc
= object_class_by_name(typename
);
42 if (!object_class_dynamic_cast(oc
, TYPE_TPM_BACKEND
)) {
46 return TPM_BACKEND_CLASS(oc
);
50 * Walk the list of available TPM backend drivers and display them on the
53 static void tpm_display_backend_drivers(void)
57 fprintf(stderr
, "Supported TPM types (choose only one):\n");
59 for (i
= 0; i
< TPM_TYPE__MAX
; i
++) {
60 const TPMBackendClass
*bc
= tpm_be_find_by_type(i
);
64 fprintf(stderr
, "%12s %s\n", TpmType_str(i
), bc
->desc
);
66 fprintf(stderr
, "\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
)
91 const TPMBackendClass
*be
;
93 Error
*local_err
= NULL
;
96 if (!QLIST_EMPTY(&tpm_backends
)) {
97 error_report("Only one TPM is allowed.");
101 id
= qemu_opts_id(opts
);
103 error_report(QERR_MISSING_PARAMETER
, "id");
107 value
= qemu_opt_get(opts
, "type");
109 error_report(QERR_MISSING_PARAMETER
, "type");
110 tpm_display_backend_drivers();
114 i
= qapi_enum_parse(&TpmType_lookup
, value
, -1, NULL
);
115 be
= i
>= 0 ? tpm_be_find_by_type(i
) : NULL
;
117 error_report(QERR_INVALID_PARAMETER_VALUE
,
118 "type", "a TPM backend type");
119 tpm_display_backend_drivers();
123 /* validate backend specific opts */
124 qemu_opts_validate(opts
, be
->opts
, &local_err
);
126 error_report_err(local_err
);
130 drv
= be
->create(opts
);
135 drv
->id
= g_strdup(id
);
136 QLIST_INSERT_HEAD(&tpm_backends
, drv
, list
);
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
161 if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
162 tpm_init_tpmdev
, NULL
, NULL
)) {
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
)
177 if (!strcmp(optarg
, "help")) {
178 tpm_display_backend_drivers();
181 opts
= qemu_opts_parse_noisily(opts_list
, optarg
, true);
189 * Walk the list of active TPM backends and collect information about them
190 * following the schema description in qapi-schema.json.
192 TPMInfoList
*qmp_query_tpm(Error
**errp
)
195 TPMInfoList
*info
, *head
= NULL
, *cur_item
= NULL
;
197 QLIST_FOREACH(drv
, &tpm_backends
, list
) {
202 info
= g_new0(TPMInfoList
, 1);
203 info
->value
= tpm_backend_query_tpm(drv
);
206 head
= cur_item
= info
;
208 cur_item
->next
= info
;
216 TpmTypeList
*qmp_query_tpm_types(Error
**errp
)
219 TpmTypeList
*head
= NULL
, *prev
= NULL
, *cur_item
;
221 for (i
= 0; i
< TPM_TYPE__MAX
; i
++) {
222 if (!tpm_be_find_by_type(i
)) {
225 cur_item
= g_new0(TpmTypeList
, 1);
229 prev
->next
= cur_item
;
240 TpmModelList
*qmp_query_tpm_models(Error
**errp
)
243 TpmModelList
*head
= NULL
, *prev
= NULL
, *cur_item
;
245 for (i
= 0; i
< TPM_MODEL__MAX
; i
++) {
246 if (!tpm_models
[i
]) {
249 cur_item
= g_new0(TpmModelList
, 1);
253 prev
->next
= cur_item
;