2013-03-28 15:26:21 +04:00
|
|
|
/*
|
|
|
|
* QEMU TPM Backend
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2013
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Stefan Berger <stefanb@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
* Based on backends/rng.c by Anthony Liguori
|
|
|
|
*/
|
|
|
|
|
2016-01-29 20:49:54 +03:00
|
|
|
#include "qemu/osdep.h"
|
2013-04-08 18:55:25 +04:00
|
|
|
#include "sysemu/tpm_backend.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2013-03-28 15:26:21 +04:00
|
|
|
#include "qapi/qmp/qerror.h"
|
2013-04-02 20:28:41 +04:00
|
|
|
#include "sysemu/tpm.h"
|
2017-10-10 01:56:01 +03:00
|
|
|
#include "hw/tpm/tpm_int.h"
|
2013-04-02 20:28:41 +04:00
|
|
|
#include "qemu/thread.h"
|
2017-09-29 14:10:14 +03:00
|
|
|
|
|
|
|
static void tpm_backend_worker_thread(gpointer data, gpointer user_data)
|
|
|
|
{
|
|
|
|
TPMBackend *s = TPM_BACKEND(user_data);
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
|
|
|
assert(k->handle_request != NULL);
|
2017-10-10 01:55:55 +03:00
|
|
|
k->handle_request(s, (TPMBackendCmd *)data);
|
2017-09-29 14:10:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tpm_backend_thread_end(TPMBackend *s)
|
|
|
|
{
|
|
|
|
if (s->thread_pool) {
|
|
|
|
g_thread_pool_free(s->thread_pool, FALSE, TRUE);
|
|
|
|
s->thread_pool = NULL;
|
|
|
|
}
|
|
|
|
}
|
2013-03-28 15:26:21 +04:00
|
|
|
|
|
|
|
enum TpmType tpm_backend_get_type(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 01:55:49 +03:00
|
|
|
return k->type;
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
2017-10-10 01:56:02 +03:00
|
|
|
int tpm_backend_init(TPMBackend *s, TPMState *state)
|
2013-03-28 15:26:21 +04:00
|
|
|
{
|
2017-09-29 14:10:14 +03:00
|
|
|
s->tpm_state = state;
|
2017-09-29 14:10:16 +03:00
|
|
|
s->had_startup_error = false;
|
2017-09-29 14:10:14 +03:00
|
|
|
|
2017-10-10 01:55:50 +03:00
|
|
|
return 0;
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int tpm_backend_startup_tpm(TPMBackend *s)
|
|
|
|
{
|
2017-09-29 14:10:16 +03:00
|
|
|
int res = 0;
|
2013-03-28 15:26:21 +04:00
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-09-29 14:10:14 +03:00
|
|
|
/* terminate a running TPM */
|
|
|
|
tpm_backend_thread_end(s);
|
|
|
|
|
|
|
|
s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE,
|
|
|
|
NULL);
|
|
|
|
|
2017-10-10 01:55:49 +03:00
|
|
|
res = k->startup_tpm ? k->startup_tpm(s) : 0;
|
2017-09-29 14:10:16 +03:00
|
|
|
|
|
|
|
s->had_startup_error = (res != 0);
|
|
|
|
|
|
|
|
return res;
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tpm_backend_had_startup_error(TPMBackend *s)
|
|
|
|
{
|
2017-09-29 14:10:16 +03:00
|
|
|
return s->had_startup_error;
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
2017-10-10 01:55:55 +03:00
|
|
|
void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd)
|
2013-03-28 15:26:21 +04:00
|
|
|
{
|
2017-10-10 01:55:55 +03:00
|
|
|
g_thread_pool_push(s->thread_pool, cmd, NULL);
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void tpm_backend_reset(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 01:55:49 +03:00
|
|
|
if (k->reset) {
|
|
|
|
k->reset(s);
|
2017-09-29 14:10:16 +03:00
|
|
|
}
|
2017-09-29 14:10:14 +03:00
|
|
|
|
|
|
|
tpm_backend_thread_end(s);
|
2017-09-29 14:10:16 +03:00
|
|
|
|
|
|
|
s->had_startup_error = false;
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void tpm_backend_cancel_cmd(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 01:55:49 +03:00
|
|
|
assert(k->cancel_cmd);
|
2017-09-29 14:10:16 +03:00
|
|
|
|
2017-10-10 01:55:49 +03:00
|
|
|
k->cancel_cmd(s);
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 01:55:49 +03:00
|
|
|
return k->get_tpm_established_flag ?
|
|
|
|
k->get_tpm_established_flag(s) : false;
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
2015-05-26 23:51:05 +03:00
|
|
|
int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 01:55:49 +03:00
|
|
|
return k->reset_tpm_established_flag ?
|
|
|
|
k->reset_tpm_established_flag(s, locty) : 0;
|
2015-05-26 23:51:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-10 01:55:49 +03:00
|
|
|
assert(k->get_tpm_version);
|
2017-09-29 14:10:16 +03:00
|
|
|
|
2017-10-10 01:55:49 +03:00
|
|
|
return k->get_tpm_version(s);
|
2015-05-26 23:51:05 +03:00
|
|
|
}
|
|
|
|
|
2017-09-29 14:10:17 +03:00
|
|
|
TPMInfo *tpm_backend_query_tpm(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMInfo *info = g_new0(TPMInfo, 1);
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
|
|
|
info->id = g_strdup(s->id);
|
|
|
|
info->model = s->fe_model;
|
2017-10-10 01:55:49 +03:00
|
|
|
if (k->get_tpm_options) {
|
|
|
|
info->options = k->get_tpm_options(s);
|
|
|
|
}
|
2017-09-29 14:10:17 +03:00
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2013-03-28 15:26:21 +04:00
|
|
|
static bool tpm_backend_prop_get_opened(Object *obj, Error **errp)
|
|
|
|
{
|
|
|
|
TPMBackend *s = TPM_BACKEND(obj);
|
|
|
|
|
|
|
|
return s->opened;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tpm_backend_open(TPMBackend *s, Error **errp)
|
|
|
|
{
|
|
|
|
object_property_set_bool(OBJECT(s), true, "opened", errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp)
|
|
|
|
{
|
|
|
|
TPMBackend *s = TPM_BACKEND(obj);
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
2014-04-25 14:44:22 +04:00
|
|
|
Error *local_err = NULL;
|
2013-03-28 15:26:21 +04:00
|
|
|
|
|
|
|
if (value == s->opened) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!value && s->opened) {
|
2015-03-17 13:54:50 +03:00
|
|
|
error_setg(errp, QERR_PERMISSION_DENIED);
|
2013-03-28 15:26:21 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k->opened) {
|
2014-04-25 14:44:22 +04:00
|
|
|
k->opened(s, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
2014-04-25 14:44:22 +04:00
|
|
|
s->opened = true;
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tpm_backend_instance_init(Object *obj)
|
|
|
|
{
|
2017-09-29 14:10:15 +03:00
|
|
|
TPMBackend *s = TPM_BACKEND(obj);
|
|
|
|
|
2013-03-28 15:26:21 +04:00
|
|
|
object_property_add_bool(obj, "opened",
|
|
|
|
tpm_backend_prop_get_opened,
|
|
|
|
tpm_backend_prop_set_opened,
|
|
|
|
NULL);
|
2017-09-29 14:10:15 +03:00
|
|
|
s->fe_model = -1;
|
2013-04-02 20:28:41 +04:00
|
|
|
}
|
|
|
|
|
2017-09-29 14:10:14 +03:00
|
|
|
static void tpm_backend_instance_finalize(Object *obj)
|
2013-04-02 20:28:41 +04:00
|
|
|
{
|
2017-09-29 14:10:14 +03:00
|
|
|
TPMBackend *s = TPM_BACKEND(obj);
|
2013-04-02 20:28:41 +04:00
|
|
|
|
2017-09-29 14:10:15 +03:00
|
|
|
g_free(s->id);
|
2017-09-29 14:10:14 +03:00
|
|
|
tpm_backend_thread_end(s);
|
2013-04-02 20:28:41 +04:00
|
|
|
}
|
|
|
|
|
2013-03-28 15:26:21 +04:00
|
|
|
static const TypeInfo tpm_backend_info = {
|
|
|
|
.name = TYPE_TPM_BACKEND,
|
|
|
|
.parent = TYPE_OBJECT,
|
|
|
|
.instance_size = sizeof(TPMBackend),
|
|
|
|
.instance_init = tpm_backend_instance_init,
|
2017-09-29 14:10:14 +03:00
|
|
|
.instance_finalize = tpm_backend_instance_finalize,
|
2013-03-28 15:26:21 +04:00
|
|
|
.class_size = sizeof(TPMBackendClass),
|
|
|
|
.abstract = true,
|
|
|
|
};
|
|
|
|
|
2017-10-10 01:56:01 +03:00
|
|
|
static const TypeInfo tpm_if_info = {
|
|
|
|
.name = TYPE_TPM_IF,
|
|
|
|
.parent = TYPE_INTERFACE,
|
|
|
|
.class_size = sizeof(TPMIfClass),
|
|
|
|
};
|
|
|
|
|
2013-03-28 15:26:21 +04:00
|
|
|
static void register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&tpm_backend_info);
|
2017-10-10 01:56:01 +03:00
|
|
|
type_register_static(&tpm_if_info);
|
2013-03-28 15:26:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type_init(register_types);
|