xref: /openbmc/qemu/include/sysemu/tpm_backend.h (revision b21e6aaf)
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 
22 #define TYPE_TPM_BACKEND "tpm-backend"
23 #define TPM_BACKEND(obj) \
24     OBJECT_CHECK(TPMBackend, (obj), TYPE_TPM_BACKEND)
25 #define TPM_BACKEND_GET_CLASS(obj) \
26     OBJECT_GET_CLASS(TPMBackendClass, (obj), TYPE_TPM_BACKEND)
27 #define TPM_BACKEND_CLASS(klass) \
28     OBJECT_CLASS_CHECK(TPMBackendClass, (klass), TYPE_TPM_BACKEND)
29 
30 typedef struct TPMBackendClass TPMBackendClass;
31 typedef struct TPMBackend TPMBackend;
32 
33 typedef struct TPMBackendCmd {
34     uint8_t locty;
35     const uint8_t *in;
36     uint32_t in_len;
37     uint8_t *out;
38     uint32_t out_len;
39     bool selftest_done;
40 } TPMBackendCmd;
41 
42 struct TPMBackend {
43     Object parent;
44 
45     /*< protected >*/
46     TPMIf *tpmif;
47     bool opened;
48     GThreadPool *thread_pool;
49     bool had_startup_error;
50     QEMUBH *bh;
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);
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);
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  *
116  * Returns 0 on success.
117  */
118 int tpm_backend_startup_tpm(TPMBackend *s);
119 
120 /**
121  * tpm_backend_had_startup_error:
122  * @s: the backend to query for a statup error
123  *
124  * Check whether the backend had an error during startup. Returns
125  * false if no error occurred and the backend can be used, true
126  * otherwise.
127  */
128 bool tpm_backend_had_startup_error(TPMBackend *s);
129 
130 /**
131  * tpm_backend_deliver_request:
132  * @s: the backend to send the request to
133  * @cmd: the command to deliver
134  *
135  * Send a request to the backend. The backend will then send the request
136  * to the TPM implementation.
137  */
138 void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd);
139 
140 /**
141  * tpm_backend_reset:
142  * @s: the backend to reset
143  *
144  * Reset the backend into a well defined state with all previous errors
145  * reset.
146  */
147 void tpm_backend_reset(TPMBackend *s);
148 
149 /**
150  * tpm_backend_cancel_cmd:
151  * @s: the backend
152  *
153  * Cancel any ongoing command being processed by the TPM implementation
154  * on behalf of the QEMU guest.
155  */
156 void tpm_backend_cancel_cmd(TPMBackend *s);
157 
158 /**
159  * tpm_backend_get_tpm_established_flag:
160  * @s: the backend
161  *
162  * Get the TPM establishment flag. This function may be called very
163  * frequently by the frontend since for example in the TIS implementation
164  * this flag is part of a register.
165  */
166 bool tpm_backend_get_tpm_established_flag(TPMBackend *s);
167 
168 /**
169  * tpm_backend_reset_tpm_established_flag:
170  * @s: the backend
171  * @locty: the locality number
172  *
173  * Reset the TPM establishment flag.
174  */
175 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty);
176 
177 /**
178  * tpm_backend_get_tpm_version:
179  * @s: the backend to call into
180  *
181  * Get the TPM Version that is emulated at the backend.
182  *
183  * Returns TPMVersion.
184  */
185 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s);
186 
187 /**
188  * tpm_backend_get_buffer_size:
189  * @s: the backend to call into
190  *
191  * Get the TPM's buffer size.
192  *
193  * Returns buffer size.
194  */
195 size_t tpm_backend_get_buffer_size(TPMBackend *s);
196 
197 /**
198  * tpm_backend_query_tpm:
199  * @s: the backend
200  *
201  * Query backend tpm info
202  *
203  * Returns newly allocated TPMInfo
204  */
205 TPMInfo *tpm_backend_query_tpm(TPMBackend *s);
206 
207 TPMBackend *qemu_find_tpm_be(const char *id);
208 
209 #endif
210