xref: /openbmc/qemu/include/sysemu/tpm_backend.h (revision 68999059)
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     enum TpmModel fe_model;
55 
56     QLIST_ENTRY(TPMBackend) list;
57 };
58 
59 struct TPMBackendClass {
60     ObjectClass parent_class;
61 
62     enum TpmType type;
63     const QemuOptDesc *opts;
64     /* get a descriptive text of the backend to display to the user */
65     const char *desc;
66 
67     TPMBackend *(*create)(QemuOpts *opts, const char *id);
68 
69     /* start up the TPM on the backend */
70     int (*startup_tpm)(TPMBackend *t);
71 
72     void (*reset)(TPMBackend *t);
73 
74     void (*cancel_cmd)(TPMBackend *t);
75 
76     bool (*get_tpm_established_flag)(TPMBackend *t);
77 
78     int (*reset_tpm_established_flag)(TPMBackend *t, uint8_t locty);
79 
80     TPMVersion (*get_tpm_version)(TPMBackend *t);
81 
82     TpmTypeOptions *(*get_tpm_options)(TPMBackend *t);
83 
84     void (*opened)(TPMBackend *s, Error **errp);
85 
86     void (*handle_request)(TPMBackend *s, TPMBackendCmd *cmd);
87 };
88 
89 /**
90  * tpm_backend_get_type:
91  * @s: the backend
92  *
93  * Returns the TpmType of the backend.
94  */
95 enum TpmType tpm_backend_get_type(TPMBackend *s);
96 
97 /**
98  * tpm_backend_init:
99  * @s: the backend to initialized
100  * @tpmif: TPM interface
101  * @datacb: callback for sending data to frontend
102  *
103  * Initialize the backend with the given variables.
104  *
105  * Returns 0 on success.
106  */
107 int tpm_backend_init(TPMBackend *s, TPMIf *tpmif);
108 
109 /**
110  * tpm_backend_startup_tpm:
111  * @s: the backend whose TPM support is to be started
112  *
113  * Returns 0 on success.
114  */
115 int tpm_backend_startup_tpm(TPMBackend *s);
116 
117 /**
118  * tpm_backend_had_startup_error:
119  * @s: the backend to query for a statup error
120  *
121  * Check whether the backend had an error during startup. Returns
122  * false if no error occurred and the backend can be used, true
123  * otherwise.
124  */
125 bool tpm_backend_had_startup_error(TPMBackend *s);
126 
127 /**
128  * tpm_backend_deliver_request:
129  * @s: the backend to send the request to
130  * @cmd: the command to deliver
131  *
132  * Send a request to the backend. The backend will then send the request
133  * to the TPM implementation.
134  */
135 void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd);
136 
137 /**
138  * tpm_backend_reset:
139  * @s: the backend to reset
140  *
141  * Reset the backend into a well defined state with all previous errors
142  * reset.
143  */
144 void tpm_backend_reset(TPMBackend *s);
145 
146 /**
147  * tpm_backend_cancel_cmd:
148  * @s: the backend
149  *
150  * Cancel any ongoing command being processed by the TPM implementation
151  * on behalf of the QEMU guest.
152  */
153 void tpm_backend_cancel_cmd(TPMBackend *s);
154 
155 /**
156  * tpm_backend_get_tpm_established_flag:
157  * @s: the backend
158  *
159  * Get the TPM establishment flag. This function may be called very
160  * frequently by the frontend since for example in the TIS implementation
161  * this flag is part of a register.
162  */
163 bool tpm_backend_get_tpm_established_flag(TPMBackend *s);
164 
165 /**
166  * tpm_backend_reset_tpm_established_flag:
167  * @s: the backend
168  * @locty: the locality number
169  *
170  * Reset the TPM establishment flag.
171  */
172 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty);
173 
174 /**
175  * tpm_backend_open:
176  * @s: the backend to open
177  * @errp: a pointer to return the #Error object if an error occurs.
178  *
179  * This function will open the backend if it is not already open.  Calling this
180  * function on an already opened backend will not result in an error.
181  */
182 void tpm_backend_open(TPMBackend *s, Error **errp);
183 
184 /**
185  * tpm_backend_get_tpm_version:
186  * @s: the backend to call into
187  *
188  * Get the TPM Version that is emulated at the backend.
189  *
190  * Returns TPMVersion.
191  */
192 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s);
193 
194 /**
195  * tpm_backend_query_tpm:
196  * @s: the backend
197  *
198  * Query backend tpm info
199  *
200  * Returns newly allocated TPMInfo
201  */
202 TPMInfo *tpm_backend_query_tpm(TPMBackend *s);
203 
204 TPMBackend *qemu_find_tpm(const char *id);
205 
206 void tpm_register_model(enum TpmModel model);
207 
208 #endif
209