4 * Copyright IBM, Corp. 2013
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.
12 * Based on backends/rng.c by Anthony Liguori
15 #include "qemu/osdep.h"
16 #include "sysemu/tpm_backend.h"
17 #include "qapi/error.h"
18 #include "qapi/qmp/qerror.h"
19 #include "sysemu/tpm.h"
20 #include "qemu/thread.h"
21 #include "sysemu/tpm_backend_int.h"
23 enum TpmType
tpm_backend_get_type(TPMBackend
*s
)
25 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
30 const char *tpm_backend_get_desc(TPMBackend
*s
)
32 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
34 return k
->ops
->desc();
37 void tpm_backend_destroy(TPMBackend
*s
)
39 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
44 int tpm_backend_init(TPMBackend
*s
, TPMState
*state
,
45 TPMRecvDataCB
*datacb
)
47 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
49 return k
->ops
->init(s
, state
, datacb
);
52 int tpm_backend_startup_tpm(TPMBackend
*s
)
54 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
56 return k
->ops
->startup_tpm(s
);
59 bool tpm_backend_had_startup_error(TPMBackend
*s
)
61 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
63 return k
->ops
->had_startup_error(s
);
66 size_t tpm_backend_realloc_buffer(TPMBackend
*s
, TPMSizedBuffer
*sb
)
68 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
70 return k
->ops
->realloc_buffer(sb
);
73 void tpm_backend_deliver_request(TPMBackend
*s
)
75 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
77 k
->ops
->deliver_request(s
);
80 void tpm_backend_reset(TPMBackend
*s
)
82 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
87 void tpm_backend_cancel_cmd(TPMBackend
*s
)
89 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
91 k
->ops
->cancel_cmd(s
);
94 bool tpm_backend_get_tpm_established_flag(TPMBackend
*s
)
96 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
98 return k
->ops
->get_tpm_established_flag(s
);
101 int tpm_backend_reset_tpm_established_flag(TPMBackend
*s
, uint8_t locty
)
103 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
105 return k
->ops
->reset_tpm_established_flag(s
, locty
);
108 TPMVersion
tpm_backend_get_tpm_version(TPMBackend
*s
)
110 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
112 return k
->ops
->get_tpm_version(s
);
115 static bool tpm_backend_prop_get_opened(Object
*obj
, Error
**errp
)
117 TPMBackend
*s
= TPM_BACKEND(obj
);
122 void tpm_backend_open(TPMBackend
*s
, Error
**errp
)
124 object_property_set_bool(OBJECT(s
), true, "opened", errp
);
127 static void tpm_backend_prop_set_opened(Object
*obj
, bool value
, Error
**errp
)
129 TPMBackend
*s
= TPM_BACKEND(obj
);
130 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
131 Error
*local_err
= NULL
;
133 if (value
== s
->opened
) {
137 if (!value
&& s
->opened
) {
138 error_setg(errp
, QERR_PERMISSION_DENIED
);
143 k
->opened(s
, &local_err
);
145 error_propagate(errp
, local_err
);
153 static void tpm_backend_instance_init(Object
*obj
)
155 object_property_add_bool(obj
, "opened",
156 tpm_backend_prop_get_opened
,
157 tpm_backend_prop_set_opened
,
161 void tpm_backend_thread_deliver_request(TPMBackendThread
*tbt
)
163 g_thread_pool_push(tbt
->pool
, (gpointer
)TPM_BACKEND_CMD_PROCESS_CMD
, NULL
);
166 void tpm_backend_thread_create(TPMBackendThread
*tbt
,
167 GFunc func
, gpointer user_data
)
170 tbt
->pool
= g_thread_pool_new(func
, user_data
, 1, TRUE
, NULL
);
171 g_thread_pool_push(tbt
->pool
, (gpointer
)TPM_BACKEND_CMD_INIT
, NULL
);
175 void tpm_backend_thread_end(TPMBackendThread
*tbt
)
178 g_thread_pool_push(tbt
->pool
, (gpointer
)TPM_BACKEND_CMD_END
, NULL
);
179 g_thread_pool_free(tbt
->pool
, FALSE
, TRUE
);
184 static const TypeInfo tpm_backend_info
= {
185 .name
= TYPE_TPM_BACKEND
,
186 .parent
= TYPE_OBJECT
,
187 .instance_size
= sizeof(TPMBackend
),
188 .instance_init
= tpm_backend_instance_init
,
189 .class_size
= sizeof(TPMBackendClass
),
193 static void register_types(void)
195 type_register_static(&tpm_backend_info
);
198 type_init(register_types
);