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