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"
22 static void tpm_backend_worker_thread(gpointer data
, gpointer user_data
)
24 TPMBackend
*s
= TPM_BACKEND(user_data
);
25 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
27 assert(k
->handle_request
!= NULL
);
28 k
->handle_request(s
, (TPMBackendCmd
)data
);
31 static void tpm_backend_thread_end(TPMBackend
*s
)
34 g_thread_pool_push(s
->thread_pool
, (gpointer
)TPM_BACKEND_CMD_END
, NULL
);
35 g_thread_pool_free(s
->thread_pool
, FALSE
, TRUE
);
36 s
->thread_pool
= NULL
;
40 enum TpmType
tpm_backend_get_type(TPMBackend
*s
)
42 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
47 const char *tpm_backend_get_desc(TPMBackend
*s
)
49 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
51 return k
->ops
->desc();
54 int tpm_backend_init(TPMBackend
*s
, TPMState
*state
,
55 TPMRecvDataCB
*datacb
)
57 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
60 s
->recv_data_callback
= datacb
;
62 return k
->ops
->init(s
);
65 int tpm_backend_startup_tpm(TPMBackend
*s
)
67 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
69 /* terminate a running TPM */
70 tpm_backend_thread_end(s
);
72 s
->thread_pool
= g_thread_pool_new(tpm_backend_worker_thread
, s
, 1, TRUE
,
74 g_thread_pool_push(s
->thread_pool
, (gpointer
)TPM_BACKEND_CMD_INIT
, NULL
);
76 return k
->ops
->startup_tpm(s
);
79 bool tpm_backend_had_startup_error(TPMBackend
*s
)
81 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
83 return k
->ops
->had_startup_error(s
);
86 size_t tpm_backend_realloc_buffer(TPMBackend
*s
, TPMSizedBuffer
*sb
)
88 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
90 return k
->ops
->realloc_buffer(sb
);
93 void tpm_backend_deliver_request(TPMBackend
*s
)
95 g_thread_pool_push(s
->thread_pool
, (gpointer
)TPM_BACKEND_CMD_PROCESS_CMD
,
99 void tpm_backend_reset(TPMBackend
*s
)
101 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
105 tpm_backend_thread_end(s
);
108 void tpm_backend_cancel_cmd(TPMBackend
*s
)
110 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
112 k
->ops
->cancel_cmd(s
);
115 bool tpm_backend_get_tpm_established_flag(TPMBackend
*s
)
117 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
119 return k
->ops
->get_tpm_established_flag(s
);
122 int tpm_backend_reset_tpm_established_flag(TPMBackend
*s
, uint8_t locty
)
124 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
126 return k
->ops
->reset_tpm_established_flag(s
, locty
);
129 TPMVersion
tpm_backend_get_tpm_version(TPMBackend
*s
)
131 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
133 return k
->ops
->get_tpm_version(s
);
136 static bool tpm_backend_prop_get_opened(Object
*obj
, Error
**errp
)
138 TPMBackend
*s
= TPM_BACKEND(obj
);
143 void tpm_backend_open(TPMBackend
*s
, Error
**errp
)
145 object_property_set_bool(OBJECT(s
), true, "opened", errp
);
148 static void tpm_backend_prop_set_opened(Object
*obj
, bool value
, Error
**errp
)
150 TPMBackend
*s
= TPM_BACKEND(obj
);
151 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
152 Error
*local_err
= NULL
;
154 if (value
== s
->opened
) {
158 if (!value
&& s
->opened
) {
159 error_setg(errp
, QERR_PERMISSION_DENIED
);
164 k
->opened(s
, &local_err
);
166 error_propagate(errp
, local_err
);
174 static void tpm_backend_instance_init(Object
*obj
)
176 TPMBackend
*s
= TPM_BACKEND(obj
);
178 object_property_add_bool(obj
, "opened",
179 tpm_backend_prop_get_opened
,
180 tpm_backend_prop_set_opened
,
185 static void tpm_backend_instance_finalize(Object
*obj
)
187 TPMBackend
*s
= TPM_BACKEND(obj
);
191 g_free(s
->cancel_path
);
192 tpm_backend_thread_end(s
);
195 static const TypeInfo tpm_backend_info
= {
196 .name
= TYPE_TPM_BACKEND
,
197 .parent
= TYPE_OBJECT
,
198 .instance_size
= sizeof(TPMBackend
),
199 .instance_init
= tpm_backend_instance_init
,
200 .instance_finalize
= tpm_backend_instance_finalize
,
201 .class_size
= sizeof(TPMBackendClass
),
205 static void register_types(void)
207 type_register_static(&tpm_backend_info
);
210 type_init(register_types
);