2013-03-28 11:26:21 +00: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 17:49:54 +00:00
|
|
|
#include "qemu/osdep.h"
|
2013-04-08 14:55:25 +00: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 08:01:28 +00:00
|
|
|
#include "qapi/error.h"
|
2013-03-28 11:26:21 +00:00
|
|
|
#include "qapi/qmp/qerror.h"
|
2013-04-02 16:28:41 +00:00
|
|
|
#include "sysemu/tpm.h"
|
|
|
|
#include "qemu/thread.h"
|
2017-09-29 11:10:14 +00: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);
|
|
|
|
k->handle_request(s, (TPMBackendCmd)data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tpm_backend_thread_end(TPMBackend *s)
|
|
|
|
{
|
|
|
|
if (s->thread_pool) {
|
|
|
|
g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_END, NULL);
|
|
|
|
g_thread_pool_free(s->thread_pool, FALSE, TRUE);
|
|
|
|
s->thread_pool = NULL;
|
|
|
|
}
|
|
|
|
}
|
2013-03-28 11:26:21 +00:00
|
|
|
|
|
|
|
enum TpmType tpm_backend_get_type(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-09 22:55:49 +00:00
|
|
|
return k->type;
|
2013-03-28 11:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int tpm_backend_init(TPMBackend *s, TPMState *state,
|
|
|
|
TPMRecvDataCB *datacb)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-09-29 11:10:14 +00:00
|
|
|
s->tpm_state = state;
|
|
|
|
s->recv_data_callback = datacb;
|
2017-09-29 11:10:16 +00:00
|
|
|
s->had_startup_error = false;
|
2017-09-29 11:10:14 +00:00
|
|
|
|
2017-10-09 22:55:49 +00:00
|
|
|
return k->init ? k->init(s) : 0;
|
2013-03-28 11:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int tpm_backend_startup_tpm(TPMBackend *s)
|
|
|
|
{
|
2017-09-29 11:10:16 +00:00
|
|
|
int res = 0;
|
2013-03-28 11:26:21 +00:00
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-09-29 11:10:14 +00: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);
|
|
|
|
g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL);
|
|
|
|
|
2017-10-09 22:55:49 +00:00
|
|
|
res = k->startup_tpm ? k->startup_tpm(s) : 0;
|
2017-09-29 11:10:16 +00:00
|
|
|
|
|
|
|
s->had_startup_error = (res != 0);
|
|
|
|
|
|
|
|
return res;
|
2013-03-28 11:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tpm_backend_had_startup_error(TPMBackend *s)
|
|
|
|
{
|
2017-09-29 11:10:16 +00:00
|
|
|
return s->had_startup_error;
|
2013-03-28 11:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tpm_backend_deliver_request(TPMBackend *s)
|
|
|
|
{
|
2017-09-29 11:10:14 +00:00
|
|
|
g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD,
|
|
|
|
NULL);
|
2013-03-28 11:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tpm_backend_reset(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-09 22:55:49 +00:00
|
|
|
if (k->reset) {
|
|
|
|
k->reset(s);
|
2017-09-29 11:10:16 +00:00
|
|
|
}
|
2017-09-29 11:10:14 +00:00
|
|
|
|
|
|
|
tpm_backend_thread_end(s);
|
2017-09-29 11:10:16 +00:00
|
|
|
|
|
|
|
s->had_startup_error = false;
|
2013-03-28 11:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tpm_backend_cancel_cmd(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-09 22:55:49 +00:00
|
|
|
assert(k->cancel_cmd);
|
2017-09-29 11:10:16 +00:00
|
|
|
|
2017-10-09 22:55:49 +00:00
|
|
|
k->cancel_cmd(s);
|
2013-03-28 11:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-09 22:55:49 +00:00
|
|
|
return k->get_tpm_established_flag ?
|
|
|
|
k->get_tpm_established_flag(s) : false;
|
2013-03-28 11:26:21 +00:00
|
|
|
}
|
|
|
|
|
2015-05-26 20:51:05 +00:00
|
|
|
int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-09 22:55:49 +00:00
|
|
|
return k->reset_tpm_established_flag ?
|
|
|
|
k->reset_tpm_established_flag(s, locty) : 0;
|
2015-05-26 20:51:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
|
|
|
|
{
|
|
|
|
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
|
|
|
2017-10-09 22:55:49 +00:00
|
|
|
assert(k->get_tpm_version);
|
2017-09-29 11:10:16 +00:00
|
|
|
|
2017-10-09 22:55:49 +00:00
|
|
|
return k->get_tpm_version(s);
|
2015-05-26 20:51:05 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 11:10:17 +00: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-09 22:55:49 +00:00
|
|
|
if (k->get_tpm_options) {
|
|
|
|
info->options = k->get_tpm_options(s);
|
|
|
|
}
|
2017-09-29 11:10:17 +00:00
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2013-03-28 11:26:21 +00: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 10:44:22 +00:00
|
|
|
Error *local_err = NULL;
|
2013-03-28 11:26:21 +00:00
|
|
|
|
|
|
|
if (value == s->opened) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!value && s->opened) {
|
2015-03-17 10:54:50 +00:00
|
|
|
error_setg(errp, QERR_PERMISSION_DENIED);
|
2013-03-28 11:26:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k->opened) {
|
2014-04-25 10:44:22 +00:00
|
|
|
k->opened(s, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-28 11:26:21 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 10:44:22 +00:00
|
|
|
s->opened = true;
|
2013-03-28 11:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tpm_backend_instance_init(Object *obj)
|
|
|
|
{
|
2017-09-29 11:10:15 +00:00
|
|
|
TPMBackend *s = TPM_BACKEND(obj);
|
|
|
|
|
2013-03-28 11:26:21 +00:00
|
|
|
object_property_add_bool(obj, "opened",
|
|
|
|
tpm_backend_prop_get_opened,
|
|
|
|
tpm_backend_prop_set_opened,
|
|
|
|
NULL);
|
2017-09-29 11:10:15 +00:00
|
|
|
s->fe_model = -1;
|
2013-04-02 16:28:41 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 11:10:14 +00:00
|
|
|
static void tpm_backend_instance_finalize(Object *obj)
|
2013-04-02 16:28:41 +00:00
|
|
|
{
|
2017-09-29 11:10:14 +00:00
|
|
|
TPMBackend *s = TPM_BACKEND(obj);
|
2013-04-02 16:28:41 +00:00
|
|
|
|
2017-09-29 11:10:15 +00:00
|
|
|
g_free(s->id);
|
2017-09-29 11:10:14 +00:00
|
|
|
tpm_backend_thread_end(s);
|
2013-04-02 16:28:41 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 11:26:21 +00: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 11:10:14 +00:00
|
|
|
.instance_finalize = tpm_backend_instance_finalize,
|
2013-03-28 11:26:21 +00:00
|
|
|
.class_size = sizeof(TPMBackendClass),
|
|
|
|
.abstract = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&tpm_backend_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(register_types);
|