xref: /openbmc/qemu/include/sysemu/tpm_backend.h (revision 8f0a3716)
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 
13 #ifndef TPM_BACKEND_H
14 #define TPM_BACKEND_H
15 
16 #include "qom/object.h"
17 #include "qemu-common.h"
18 #include "qapi-types.h"
19 #include "qemu/option.h"
20 #include "sysemu/tpm.h"
21 #include "qapi/error.h"
22 
23 #define TYPE_TPM_BACKEND "tpm-backend"
24 #define TPM_BACKEND(obj) \
25     OBJECT_CHECK(TPMBackend, (obj), TYPE_TPM_BACKEND)
26 #define TPM_BACKEND_GET_CLASS(obj) \
27     OBJECT_GET_CLASS(TPMBackendClass, (obj), TYPE_TPM_BACKEND)
28 #define TPM_BACKEND_CLASS(klass) \
29     OBJECT_CLASS_CHECK(TPMBackendClass, (klass), TYPE_TPM_BACKEND)
30 
31 typedef struct TPMBackendClass TPMBackendClass;
32 typedef struct TPMBackend TPMBackend;
33 
34 typedef struct TPMBackendCmd {
35     uint8_t locty;
36     const uint8_t *in;
37     uint32_t in_len;
38     uint8_t *out;
39     uint32_t out_len;
40     bool selftest_done;
41 } TPMBackendCmd;
42 
43 struct TPMBackend {
44     Object parent;
45 
46     /*< protected >*/
47     TPMIf *tpmif;
48     bool opened;
49     bool had_startup_error;
50     TPMBackendCmd *cmd;
51 
52     /* <public> */
53     char *id;
54 
55     QLIST_ENTRY(TPMBackend) list;
56 };
57 
58 struct TPMBackendClass {
59     ObjectClass parent_class;
60 
61     enum TpmType type;
62     const QemuOptDesc *opts;
63     /* get a descriptive text of the backend to display to the user */
64     const char *desc;
65 
66     TPMBackend *(*create)(QemuOpts *opts);
67 
68     /* start up the TPM on the backend - optional */
69     int (*startup_tpm)(TPMBackend *t, size_t buffersize);
70 
71     /* optional */
72     void (*reset)(TPMBackend *t);
73 
74     void (*cancel_cmd)(TPMBackend *t);
75 
76     /* optional */
77     bool (*get_tpm_established_flag)(TPMBackend *t);
78 
79     /* optional */
80     int (*reset_tpm_established_flag)(TPMBackend *t, uint8_t locty);
81 
82     TPMVersion (*get_tpm_version)(TPMBackend *t);
83 
84     size_t (*get_buffer_size)(TPMBackend *t);
85 
86     TpmTypeOptions *(*get_tpm_options)(TPMBackend *t);
87 
88     void (*handle_request)(TPMBackend *s, TPMBackendCmd *cmd, Error **errp);
89 };
90 
91 /**
92  * tpm_backend_get_type:
93  * @s: the backend
94  *
95  * Returns the TpmType of the backend.
96  */
97 enum TpmType tpm_backend_get_type(TPMBackend *s);
98 
99 /**
100  * tpm_backend_init:
101  * @s: the backend to initialized
102  * @tpmif: TPM interface
103  * @datacb: callback for sending data to frontend
104  * @errp: a pointer to return the #Error object if an error occurs.
105  *
106  * Initialize the backend with the given variables.
107  *
108  * Returns 0 on success.
109  */
110 int tpm_backend_init(TPMBackend *s, TPMIf *tpmif, Error **errp);
111 
112 /**
113  * tpm_backend_startup_tpm:
114  * @s: the backend whose TPM support is to be started
115  * @buffersize: the buffer size the TPM is supposed to use,
116  *              0 to leave it as-is
117  *
118  * Returns 0 on success.
119  */
120 int tpm_backend_startup_tpm(TPMBackend *s, size_t buffersize);
121 
122 /**
123  * tpm_backend_had_startup_error:
124  * @s: the backend to query for a statup error
125  *
126  * Check whether the backend had an error during startup. Returns
127  * false if no error occurred and the backend can be used, true
128  * otherwise.
129  */
130 bool tpm_backend_had_startup_error(TPMBackend *s);
131 
132 /**
133  * tpm_backend_deliver_request:
134  * @s: the backend to send the request to
135  * @cmd: the command to deliver
136  *
137  * Send a request to the backend. The backend will then send the request
138  * to the TPM implementation.
139  */
140 void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd);
141 
142 /**
143  * tpm_backend_reset:
144  * @s: the backend to reset
145  *
146  * Reset the backend into a well defined state with all previous errors
147  * reset.
148  */
149 void tpm_backend_reset(TPMBackend *s);
150 
151 /**
152  * tpm_backend_cancel_cmd:
153  * @s: the backend
154  *
155  * Cancel any ongoing command being processed by the TPM implementation
156  * on behalf of the QEMU guest.
157  */
158 void tpm_backend_cancel_cmd(TPMBackend *s);
159 
160 /**
161  * tpm_backend_get_tpm_established_flag:
162  * @s: the backend
163  *
164  * Get the TPM establishment flag. This function may be called very
165  * frequently by the frontend since for example in the TIS implementation
166  * this flag is part of a register.
167  */
168 bool tpm_backend_get_tpm_established_flag(TPMBackend *s);
169 
170 /**
171  * tpm_backend_reset_tpm_established_flag:
172  * @s: the backend
173  * @locty: the locality number
174  *
175  * Reset the TPM establishment flag.
176  */
177 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty);
178 
179 /**
180  * tpm_backend_get_tpm_version:
181  * @s: the backend to call into
182  *
183  * Get the TPM Version that is emulated at the backend.
184  *
185  * Returns TPMVersion.
186  */
187 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s);
188 
189 /**
190  * tpm_backend_get_buffer_size:
191  * @s: the backend to call into
192  *
193  * Get the TPM's buffer size.
194  *
195  * Returns buffer size.
196  */
197 size_t tpm_backend_get_buffer_size(TPMBackend *s);
198 
199 /**
200  * tpm_backend_finish_sync:
201  * @s: the backend to call into
202  *
203  * Finish the pending command synchronously (this will call aio_poll()
204  * on qemu main AIOContext until it ends)
205  */
206 void tpm_backend_finish_sync(TPMBackend *s);
207 
208 /**
209  * tpm_backend_query_tpm:
210  * @s: the backend
211  *
212  * Query backend tpm info
213  *
214  * Returns newly allocated TPMInfo
215  */
216 TPMInfo *tpm_backend_query_tpm(TPMBackend *s);
217 
218 TPMBackend *qemu_find_tpm_be(const char *id);
219 
220 #endif
221