xref: /openbmc/qemu/backends/tpm/tpm_backend.c (revision f8ed3648)
1 /*
2  * QEMU TPM Backend
3  *
4  * Copyright IBM, Corp. 2013
5  *
6  * Authors:
7  *  Stefan Berger   <stefanb@us.ibm.com>
8  *
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.
11  *
12  * Based on backends/rng.c by Anthony Liguori
13  */
14 
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"
24 
25 static void tpm_backend_request_completed(void *opaque, int ret)
26 {
27     TPMBackend *s = TPM_BACKEND(opaque);
28     TPMIfClass *tic = TPM_IF_GET_CLASS(s->tpmif);
29 
30     tic->request_completed(s->tpmif, ret);
31 
32     /* no need for atomic, as long the BQL is taken */
33     s->cmd = NULL;
34     object_unref(OBJECT(s));
35 }
36 
37 static int tpm_backend_worker_thread(gpointer data)
38 {
39     TPMBackend *s = TPM_BACKEND(data);
40     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
41     Error *err = NULL;
42 
43     k->handle_request(s, s->cmd, &err);
44     if (err) {
45         error_report_err(err);
46         return -1;
47     }
48 
49     return 0;
50 }
51 
52 void tpm_backend_finish_sync(TPMBackend *s)
53 {
54     while (s->cmd) {
55         aio_poll(qemu_get_aio_context(), true);
56     }
57 }
58 
59 enum TpmType tpm_backend_get_type(TPMBackend *s)
60 {
61     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
62 
63     return k->type;
64 }
65 
66 int tpm_backend_init(TPMBackend *s, TPMIf *tpmif, Error **errp)
67 {
68     if (s->tpmif) {
69         error_setg(errp, "TPM backend '%s' is already initialized", s->id);
70         return -1;
71     }
72 
73     s->tpmif = tpmif;
74     object_ref(OBJECT(tpmif));
75 
76     s->had_startup_error = false;
77 
78     return 0;
79 }
80 
81 int tpm_backend_startup_tpm(TPMBackend *s, size_t buffersize)
82 {
83     int res = 0;
84     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
85 
86     /* terminate a running TPM */
87     tpm_backend_finish_sync(s);
88 
89     res = k->startup_tpm ? k->startup_tpm(s, buffersize) : 0;
90 
91     s->had_startup_error = (res != 0);
92 
93     return res;
94 }
95 
96 bool tpm_backend_had_startup_error(TPMBackend *s)
97 {
98     return s->had_startup_error;
99 }
100 
101 void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd)
102 {
103     if (s->cmd != NULL) {
104         error_report("There is a TPM request pending");
105         return;
106     }
107 
108     s->cmd = cmd;
109     object_ref(OBJECT(s));
110     thread_pool_submit_aio(tpm_backend_worker_thread, s,
111                            tpm_backend_request_completed, s);
112 }
113 
114 void tpm_backend_reset(TPMBackend *s)
115 {
116     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
117 
118     if (k->reset) {
119         k->reset(s);
120     }
121 
122     tpm_backend_finish_sync(s);
123 
124     s->had_startup_error = false;
125 }
126 
127 void tpm_backend_cancel_cmd(TPMBackend *s)
128 {
129     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
130 
131     k->cancel_cmd(s);
132 }
133 
134 bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
135 {
136     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
137 
138     return k->get_tpm_established_flag ?
139            k->get_tpm_established_flag(s) : false;
140 }
141 
142 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
143 {
144     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
145 
146     return k->reset_tpm_established_flag ?
147            k->reset_tpm_established_flag(s, locty) : 0;
148 }
149 
150 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
151 {
152     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
153 
154     return k->get_tpm_version(s);
155 }
156 
157 size_t tpm_backend_get_buffer_size(TPMBackend *s)
158 {
159     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
160 
161     return k->get_buffer_size(s);
162 }
163 
164 TPMInfo *tpm_backend_query_tpm(TPMBackend *s)
165 {
166     TPMInfo *info = g_new0(TPMInfo, 1);
167     TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
168     TPMIfClass *tic = TPM_IF_GET_CLASS(s->tpmif);
169 
170     info->id = g_strdup(s->id);
171     info->model = tic->model;
172     info->options = k->get_tpm_options(s);
173 
174     return info;
175 }
176 
177 static void tpm_backend_instance_finalize(Object *obj)
178 {
179     TPMBackend *s = TPM_BACKEND(obj);
180 
181     object_unref(OBJECT(s->tpmif));
182     g_free(s->id);
183 }
184 
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),
191     .abstract = true,
192 };
193 
194 static const TypeInfo tpm_if_info = {
195     .name = TYPE_TPM_IF,
196     .parent = TYPE_INTERFACE,
197     .class_size = sizeof(TPMIfClass),
198 };
199 
200 static void register_types(void)
201 {
202     type_register_static(&tpm_backend_info);
203     type_register_static(&tpm_if_info);
204 }
205 
206 type_init(register_types);
207