qemu-char: avoid potential double-free
[qemu.git] / backends / tpm.c
blobd53da18627f2e161c747cbabf14456c5bfb43955
1 /*
2 * QEMU TPM Backend
4 * Copyright IBM, Corp. 2013
6 * Authors:
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/qmp/qerror.h"
18 #include "sysemu/tpm.h"
19 #include "qemu/thread.h"
20 #include "sysemu/tpm_backend_int.h"
22 enum TpmType tpm_backend_get_type(TPMBackend *s)
24 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
26 return k->ops->type;
29 const char *tpm_backend_get_desc(TPMBackend *s)
31 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
33 return k->ops->desc();
36 void tpm_backend_destroy(TPMBackend *s)
38 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
40 k->ops->destroy(s);
43 int tpm_backend_init(TPMBackend *s, TPMState *state,
44 TPMRecvDataCB *datacb)
46 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
48 return k->ops->init(s, state, datacb);
51 int tpm_backend_startup_tpm(TPMBackend *s)
53 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
55 return k->ops->startup_tpm(s);
58 bool tpm_backend_had_startup_error(TPMBackend *s)
60 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
62 return k->ops->had_startup_error(s);
65 size_t tpm_backend_realloc_buffer(TPMBackend *s, TPMSizedBuffer *sb)
67 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
69 return k->ops->realloc_buffer(sb);
72 void tpm_backend_deliver_request(TPMBackend *s)
74 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
76 k->ops->deliver_request(s);
79 void tpm_backend_reset(TPMBackend *s)
81 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
83 k->ops->reset(s);
86 void tpm_backend_cancel_cmd(TPMBackend *s)
88 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
90 k->ops->cancel_cmd(s);
93 bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
95 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
97 return k->ops->get_tpm_established_flag(s);
100 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
102 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
104 return k->ops->reset_tpm_established_flag(s, locty);
107 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
109 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
111 return k->ops->get_tpm_version(s);
114 static bool tpm_backend_prop_get_opened(Object *obj, Error **errp)
116 TPMBackend *s = TPM_BACKEND(obj);
118 return s->opened;
121 void tpm_backend_open(TPMBackend *s, Error **errp)
123 object_property_set_bool(OBJECT(s), true, "opened", errp);
126 static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp)
128 TPMBackend *s = TPM_BACKEND(obj);
129 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
130 Error *local_err = NULL;
132 if (value == s->opened) {
133 return;
136 if (!value && s->opened) {
137 error_setg(errp, QERR_PERMISSION_DENIED);
138 return;
141 if (k->opened) {
142 k->opened(s, &local_err);
143 if (local_err) {
144 error_propagate(errp, local_err);
145 return;
149 s->opened = true;
152 static void tpm_backend_instance_init(Object *obj)
154 object_property_add_bool(obj, "opened",
155 tpm_backend_prop_get_opened,
156 tpm_backend_prop_set_opened,
157 NULL);
160 void tpm_backend_thread_deliver_request(TPMBackendThread *tbt)
162 g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD, NULL);
165 void tpm_backend_thread_create(TPMBackendThread *tbt,
166 GFunc func, gpointer user_data)
168 if (!tbt->pool) {
169 tbt->pool = g_thread_pool_new(func, user_data, 1, TRUE, NULL);
170 g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL);
174 void tpm_backend_thread_end(TPMBackendThread *tbt)
176 if (tbt->pool) {
177 g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_END, NULL);
178 g_thread_pool_free(tbt->pool, FALSE, TRUE);
179 tbt->pool = NULL;
183 static const TypeInfo tpm_backend_info = {
184 .name = TYPE_TPM_BACKEND,
185 .parent = TYPE_OBJECT,
186 .instance_size = sizeof(TPMBackend),
187 .instance_init = tpm_backend_instance_init,
188 .class_size = sizeof(TPMBackendClass),
189 .abstract = true,
192 static void register_types(void)
194 type_register_static(&tpm_backend_info);
197 type_init(register_types);