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 "hw/tpm/tpm_int.h"
21 #include "qemu/thread.h"
23 static void tpm_backend_worker_thread(gpointer data
, gpointer user_data
)
25 TPMBackend
*s
= TPM_BACKEND(user_data
);
26 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
28 assert(k
->handle_request
!= NULL
);
29 k
->handle_request(s
, (TPMBackendCmd
*)data
);
32 static void tpm_backend_thread_end(TPMBackend
*s
)
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 int tpm_backend_init(TPMBackend
*s
, TPMState
*state
)
50 s
->had_startup_error
= false;
55 int tpm_backend_startup_tpm(TPMBackend
*s
)
58 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
60 /* terminate a running TPM */
61 tpm_backend_thread_end(s
);
63 s
->thread_pool
= g_thread_pool_new(tpm_backend_worker_thread
, s
, 1, TRUE
,
66 res
= k
->startup_tpm
? k
->startup_tpm(s
) : 0;
68 s
->had_startup_error
= (res
!= 0);
73 bool tpm_backend_had_startup_error(TPMBackend
*s
)
75 return s
->had_startup_error
;
78 void tpm_backend_deliver_request(TPMBackend
*s
, TPMBackendCmd
*cmd
)
80 g_thread_pool_push(s
->thread_pool
, cmd
, NULL
);
83 void tpm_backend_reset(TPMBackend
*s
)
85 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
91 tpm_backend_thread_end(s
);
93 s
->had_startup_error
= false;
96 void tpm_backend_cancel_cmd(TPMBackend
*s
)
98 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
100 assert(k
->cancel_cmd
);
105 bool tpm_backend_get_tpm_established_flag(TPMBackend
*s
)
107 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
109 return k
->get_tpm_established_flag
?
110 k
->get_tpm_established_flag(s
) : false;
113 int tpm_backend_reset_tpm_established_flag(TPMBackend
*s
, uint8_t locty
)
115 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
117 return k
->reset_tpm_established_flag
?
118 k
->reset_tpm_established_flag(s
, locty
) : 0;
121 TPMVersion
tpm_backend_get_tpm_version(TPMBackend
*s
)
123 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
125 assert(k
->get_tpm_version
);
127 return k
->get_tpm_version(s
);
130 TPMInfo
*tpm_backend_query_tpm(TPMBackend
*s
)
132 TPMInfo
*info
= g_new0(TPMInfo
, 1);
133 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
135 info
->id
= g_strdup(s
->id
);
136 info
->model
= s
->fe_model
;
137 if (k
->get_tpm_options
) {
138 info
->options
= k
->get_tpm_options(s
);
144 static bool tpm_backend_prop_get_opened(Object
*obj
, Error
**errp
)
146 TPMBackend
*s
= TPM_BACKEND(obj
);
151 void tpm_backend_open(TPMBackend
*s
, Error
**errp
)
153 object_property_set_bool(OBJECT(s
), true, "opened", errp
);
156 static void tpm_backend_prop_set_opened(Object
*obj
, bool value
, Error
**errp
)
158 TPMBackend
*s
= TPM_BACKEND(obj
);
159 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
160 Error
*local_err
= NULL
;
162 if (value
== s
->opened
) {
166 if (!value
&& s
->opened
) {
167 error_setg(errp
, QERR_PERMISSION_DENIED
);
172 k
->opened(s
, &local_err
);
174 error_propagate(errp
, local_err
);
182 static void tpm_backend_instance_init(Object
*obj
)
184 TPMBackend
*s
= TPM_BACKEND(obj
);
186 object_property_add_bool(obj
, "opened",
187 tpm_backend_prop_get_opened
,
188 tpm_backend_prop_set_opened
,
193 static void tpm_backend_instance_finalize(Object
*obj
)
195 TPMBackend
*s
= TPM_BACKEND(obj
);
198 tpm_backend_thread_end(s
);
201 static const TypeInfo tpm_backend_info
= {
202 .name
= TYPE_TPM_BACKEND
,
203 .parent
= TYPE_OBJECT
,
204 .instance_size
= sizeof(TPMBackend
),
205 .instance_init
= tpm_backend_instance_init
,
206 .instance_finalize
= tpm_backend_instance_finalize
,
207 .class_size
= sizeof(TPMBackendClass
),
211 static const TypeInfo tpm_if_info
= {
213 .parent
= TYPE_INTERFACE
,
214 .class_size
= sizeof(TPMIfClass
),
217 static void register_types(void)
219 type_register_static(&tpm_backend_info
);
220 type_register_static(&tpm_if_info
);
223 type_init(register_types
);