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 "config-host.h"
16 #include "monitor/monitor.h"
17 #include "qapi/qmp/qerror.h"
18 #include "sysemu/tpm_backend.h"
19 #include "sysemu/tpm.h"
20 #include "qemu/config-file.h"
21 #include "qmp-commands.h"
23 static QLIST_HEAD(, TPMBackend
) tpm_backends
=
24 QLIST_HEAD_INITIALIZER(tpm_backends
);
27 #define TPM_MAX_MODELS 1
28 #define TPM_MAX_DRIVERS 1
30 static TPMDriverOps
const *be_drivers
[TPM_MAX_DRIVERS
] = {
34 static enum TpmModel tpm_models
[TPM_MAX_MODELS
] = {
38 int tpm_register_model(enum TpmModel model
)
42 for (i
= 0; i
< TPM_MAX_MODELS
; i
++) {
43 if (tpm_models
[i
] == -1) {
44 tpm_models
[i
] = model
;
48 error_report("Could not register TPM model");
52 static bool tpm_model_is_registered(enum TpmModel model
)
56 for (i
= 0; i
< TPM_MAX_MODELS
; i
++) {
57 if (tpm_models
[i
] == model
) {
64 const TPMDriverOps
*tpm_get_backend_driver(const char *type
)
68 for (i
= 0; i
< TPM_MAX_DRIVERS
&& be_drivers
[i
] != NULL
; i
++) {
69 if (!strcmp(TpmType_lookup
[be_drivers
[i
]->type
], type
)) {
79 int tpm_register_driver(const TPMDriverOps
*tdo
)
83 for (i
= 0; i
< TPM_MAX_DRIVERS
; i
++) {
89 error_report("Could not register TPM driver");
94 * Walk the list of available TPM backend drivers and display them on the
97 static void tpm_display_backend_drivers(void)
101 fprintf(stderr
, "Supported TPM types (choose only one):\n");
103 for (i
= 0; i
< TPM_MAX_DRIVERS
&& be_drivers
[i
] != NULL
; i
++) {
104 fprintf(stderr
, "%12s %s\n",
105 TpmType_lookup
[be_drivers
[i
]->type
], be_drivers
[i
]->desc());
107 fprintf(stderr
, "\n");
111 * Find the TPM with the given Id
113 TPMBackend
*qemu_find_tpm(const char *id
)
118 QLIST_FOREACH(drv
, &tpm_backends
, list
) {
119 if (!strcmp(drv
->id
, id
)) {
128 static int configure_tpm(QemuOpts
*opts
)
132 const TPMDriverOps
*be
;
134 Error
*local_err
= NULL
;
136 if (!QLIST_EMPTY(&tpm_backends
)) {
137 error_report("Only one TPM is allowed.\n");
141 id
= qemu_opts_id(opts
);
143 qerror_report(QERR_MISSING_PARAMETER
, "id");
147 value
= qemu_opt_get(opts
, "type");
149 qerror_report(QERR_MISSING_PARAMETER
, "type");
150 tpm_display_backend_drivers();
154 be
= tpm_get_backend_driver(value
);
156 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "type",
157 "a TPM backend type");
158 tpm_display_backend_drivers();
162 drv
= be
->create(opts
, id
);
167 tpm_backend_open(drv
, &local_err
);
169 qerror_report_err(local_err
);
170 error_free(local_err
);
174 QLIST_INSERT_HEAD(&tpm_backends
, drv
, list
);
179 static int tpm_init_tpmdev(QemuOpts
*opts
, void *dummy
)
181 return configure_tpm(opts
);
185 * Walk the list of TPM backend drivers that are in use and call their
186 * destroy function to have them cleaned up.
188 void tpm_cleanup(void)
190 TPMBackend
*drv
, *next
;
192 QLIST_FOREACH_SAFE(drv
, &tpm_backends
, list
, next
) {
193 QLIST_REMOVE(drv
, list
);
194 tpm_backend_destroy(drv
);
199 * Initialize the TPM. Process the tpmdev command line options describing the
204 if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
205 tpm_init_tpmdev
, NULL
, 1) != 0) {
215 * Parse the TPM configuration options.
216 * To display all available TPM backends the user may use '-tpmdev help'
218 int tpm_config_parse(QemuOptsList
*opts_list
, const char *optarg
)
222 if (!strcmp(optarg
, "help")) {
223 tpm_display_backend_drivers();
226 opts
= qemu_opts_parse(opts_list
, optarg
, 1);
233 #endif /* CONFIG_TPM */
235 static const TPMDriverOps
*tpm_driver_find_by_type(enum TpmType type
)
239 for (i
= 0; i
< TPM_MAX_DRIVERS
&& be_drivers
[i
] != NULL
; i
++) {
240 if (be_drivers
[i
]->type
== type
) {
241 return be_drivers
[i
];
247 static TPMInfo
*qmp_query_tpm_inst(TPMBackend
*drv
)
249 TPMInfo
*res
= g_new0(TPMInfo
, 1);
250 TPMPassthroughOptions
*tpo
;
252 res
->id
= g_strdup(drv
->id
);
253 res
->model
= drv
->fe_model
;
254 res
->options
= g_new0(TpmTypeOptions
, 1);
256 switch (drv
->ops
->type
) {
257 case TPM_TYPE_PASSTHROUGH
:
258 res
->options
->kind
= TPM_TYPE_OPTIONS_KIND_PASSTHROUGH
;
259 tpo
= g_new0(TPMPassthroughOptions
, 1);
260 res
->options
->passthrough
= tpo
;
262 tpo
->path
= g_strdup(drv
->path
);
263 tpo
->has_path
= true;
265 if (drv
->cancel_path
) {
266 tpo
->cancel_path
= g_strdup(drv
->cancel_path
);
267 tpo
->has_cancel_path
= true;
278 * Walk the list of active TPM backends and collect information about them
279 * following the schema description in qapi-schema.json.
281 TPMInfoList
*qmp_query_tpm(Error
**errp
)
284 TPMInfoList
*info
, *head
= NULL
, *cur_item
= NULL
;
286 QLIST_FOREACH(drv
, &tpm_backends
, list
) {
287 if (!tpm_model_is_registered(drv
->fe_model
)) {
290 info
= g_new0(TPMInfoList
, 1);
291 info
->value
= qmp_query_tpm_inst(drv
);
294 head
= cur_item
= info
;
296 cur_item
->next
= info
;
304 TpmTypeList
*qmp_query_tpm_types(Error
**errp
)
307 TpmTypeList
*head
= NULL
, *prev
= NULL
, *cur_item
;
309 for (i
= 0; i
< TPM_TYPE_MAX
; i
++) {
310 if (!tpm_driver_find_by_type(i
)) {
313 cur_item
= g_new0(TpmTypeList
, 1);
317 prev
->next
= cur_item
;
328 TpmModelList
*qmp_query_tpm_models(Error
**errp
)
331 TpmModelList
*head
= NULL
, *prev
= NULL
, *cur_item
;
333 for (i
= 0; i
< TPM_MODEL_MAX
; i
++) {
334 if (!tpm_model_is_registered(i
)) {
337 cur_item
= g_new0(TpmModelList
, 1);
341 prev
->next
= cur_item
;