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 "sysemu/tpm.h"
19 #include "qemu/thread.h"
20 #include "qemu/main-loop.h"
21 #include "qemu/module.h"
22 #include "block/thread-pool.h"
23 #include "qemu/error-report.h"
25 static void tpm_backend_request_completed(void *opaque
, int ret
)
27 TPMBackend
*s
= TPM_BACKEND(opaque
);
28 TPMIfClass
*tic
= TPM_IF_GET_CLASS(s
->tpmif
);
30 tic
->request_completed(s
->tpmif
, ret
);
32 /* no need for atomic, as long the BQL is taken */
34 object_unref(OBJECT(s
));
37 static int tpm_backend_worker_thread(gpointer data
)
39 TPMBackend
*s
= TPM_BACKEND(data
);
40 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
43 k
->handle_request(s
, s
->cmd
, &err
);
45 error_report_err(err
);
52 void tpm_backend_finish_sync(TPMBackend
*s
)
55 aio_poll(qemu_get_aio_context(), true);
59 enum TpmType
tpm_backend_get_type(TPMBackend
*s
)
61 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
66 int tpm_backend_init(TPMBackend
*s
, TPMIf
*tpmif
, Error
**errp
)
69 error_setg(errp
, "TPM backend '%s' is already initialized", s
->id
);
74 object_ref(OBJECT(tpmif
));
76 s
->had_startup_error
= false;
81 int tpm_backend_startup_tpm(TPMBackend
*s
, size_t buffersize
)
84 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
86 /* terminate a running TPM */
87 tpm_backend_finish_sync(s
);
89 res
= k
->startup_tpm
? k
->startup_tpm(s
, buffersize
) : 0;
91 s
->had_startup_error
= (res
!= 0);
96 bool tpm_backend_had_startup_error(TPMBackend
*s
)
98 return s
->had_startup_error
;
101 void tpm_backend_deliver_request(TPMBackend
*s
, TPMBackendCmd
*cmd
)
103 if (s
->cmd
!= NULL
) {
104 error_report("There is a TPM request pending");
109 object_ref(OBJECT(s
));
110 thread_pool_submit_aio(tpm_backend_worker_thread
, s
,
111 tpm_backend_request_completed
, s
);
114 void tpm_backend_reset(TPMBackend
*s
)
116 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
122 tpm_backend_finish_sync(s
);
124 s
->had_startup_error
= false;
127 void tpm_backend_cancel_cmd(TPMBackend
*s
)
129 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
134 bool tpm_backend_get_tpm_established_flag(TPMBackend
*s
)
136 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
138 return k
->get_tpm_established_flag
?
139 k
->get_tpm_established_flag(s
) : false;
142 int tpm_backend_reset_tpm_established_flag(TPMBackend
*s
, uint8_t locty
)
144 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
146 return k
->reset_tpm_established_flag
?
147 k
->reset_tpm_established_flag(s
, locty
) : 0;
150 TPMVersion
tpm_backend_get_tpm_version(TPMBackend
*s
)
152 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
154 return k
->get_tpm_version(s
);
157 size_t tpm_backend_get_buffer_size(TPMBackend
*s
)
159 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
161 return k
->get_buffer_size(s
);
164 TPMInfo
*tpm_backend_query_tpm(TPMBackend
*s
)
166 TPMInfo
*info
= g_new0(TPMInfo
, 1);
167 TPMBackendClass
*k
= TPM_BACKEND_GET_CLASS(s
);
168 TPMIfClass
*tic
= TPM_IF_GET_CLASS(s
->tpmif
);
170 info
->id
= g_strdup(s
->id
);
171 info
->model
= tic
->model
;
172 info
->options
= k
->get_tpm_options(s
);
177 static void tpm_backend_instance_finalize(Object
*obj
)
179 TPMBackend
*s
= TPM_BACKEND(obj
);
181 object_unref(OBJECT(s
->tpmif
));
185 static const TypeInfo tpm_backend_info
= {
186 .name
= TYPE_TPM_BACKEND
,
187 .parent
= TYPE_OBJECT
,
188 .instance_size
= sizeof(TPMBackend
),
189 .instance_finalize
= tpm_backend_instance_finalize
,
190 .class_size
= sizeof(TPMBackendClass
),
194 static const TypeInfo tpm_if_info
= {
196 .parent
= TYPE_INTERFACE
,
197 .class_size
= sizeof(TPMIfClass
),
200 static void register_types(void)
202 type_register_static(&tpm_backend_info
);
203 type_register_static(&tpm_if_info
);
206 type_init(register_types
);