xref: /openbmc/qemu/include/sysemu/tpm_backend.h (revision 14a650ec)
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 _QEMU_TPM_H
14 #define _QEMU_TPM_H
15 
16 #include "qom/object.h"
17 #include "qemu-common.h"
18 #include "qapi/error.h"
19 #include "qapi-types.h"
20 #include "qemu/option.h"
21 #include "sysemu/tpm.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 TPMDriverOps TPMDriverOps;
35 
36 struct TPMBackendClass {
37     ObjectClass parent_class;
38 
39     const TPMDriverOps *ops;
40 
41     void (*opened)(TPMBackend *s, Error **errp);
42 };
43 
44 struct TPMBackend {
45     Object parent;
46 
47     /*< protected >*/
48     bool opened;
49 
50     char *id;
51     enum TpmModel fe_model;
52     char *path;
53     char *cancel_path;
54     const TPMDriverOps *ops;
55 
56     QLIST_ENTRY(TPMBackend) list;
57 };
58 
59 typedef void (TPMRecvDataCB)(TPMState *, uint8_t locty);
60 
61 typedef struct TPMSizedBuffer {
62     uint32_t size;
63     uint8_t  *buffer;
64 } TPMSizedBuffer;
65 
66 struct TPMDriverOps {
67     enum TpmType type;
68     const QemuOptDesc *opts;
69     /* get a descriptive text of the backend to display to the user */
70     const char *(*desc)(void);
71 
72     TPMBackend *(*create)(QemuOpts *opts, const char *id);
73     void (*destroy)(TPMBackend *t);
74 
75     /* initialize the backend */
76     int (*init)(TPMBackend *t, TPMState *s, TPMRecvDataCB *datacb);
77     /* start up the TPM on the backend */
78     int (*startup_tpm)(TPMBackend *t);
79     /* returns true if nothing will ever answer TPM requests */
80     bool (*had_startup_error)(TPMBackend *t);
81 
82     size_t (*realloc_buffer)(TPMSizedBuffer *sb);
83 
84     void (*deliver_request)(TPMBackend *t);
85 
86     void (*reset)(TPMBackend *t);
87 
88     void (*cancel_cmd)(TPMBackend *t);
89 
90     bool (*get_tpm_established_flag)(TPMBackend *t);
91 };
92 
93 
94 /**
95  * tpm_backend_get_type:
96  * @s: the backend
97  *
98  * Returns the TpmType of the backend.
99  */
100 enum TpmType tpm_backend_get_type(TPMBackend *s);
101 
102 /**
103  * tpm_backend_get_desc:
104  * @s: the backend
105  *
106  * Returns a human readable description of the backend.
107  */
108 const char *tpm_backend_get_desc(TPMBackend *s);
109 
110 /**
111  * tpm_backend_destroy:
112  * @s: the backend to destroy
113  */
114 void tpm_backend_destroy(TPMBackend *s);
115 
116 /**
117  * tpm_backend_init:
118  * @s: the backend to initialized
119  * @state: TPMState
120  * @datacb: callback for sending data to frontend
121  *
122  * Initialize the backend with the given variables.
123  *
124  * Returns 0 on success.
125  */
126 int tpm_backend_init(TPMBackend *s, TPMState *state,
127                      TPMRecvDataCB *datacb);
128 
129 /**
130  * tpm_backend_startup_tpm:
131  * @s: the backend whose TPM support is to be started
132  *
133  * Returns 0 on success.
134  */
135 int tpm_backend_startup_tpm(TPMBackend *s);
136 
137 /**
138  * tpm_backend_had_startup_error:
139  * @s: the backend to query for a statup error
140  *
141  * Check whether the backend had an error during startup. Returns
142  * false if no error occurred and the backend can be used, true
143  * otherwise.
144  */
145 bool tpm_backend_had_startup_error(TPMBackend *s);
146 
147 /**
148  * tpm_backend_realloc_buffer:
149  * @s: the backend
150  * @sb: the TPMSizedBuffer to re-allocated to the size suitable for the
151  *      backend.
152  *
153  * This function returns the size of the allocated buffer
154  */
155 size_t tpm_backend_realloc_buffer(TPMBackend *s, TPMSizedBuffer *sb);
156 
157 /**
158  * tpm_backend_deliver_request:
159  * @s: the backend to send the request to
160  *
161  * Send a request to the backend. The backend will then send the request
162  * to the TPM implementation.
163  */
164 void tpm_backend_deliver_request(TPMBackend *s);
165 
166 /**
167  * tpm_backend_reset:
168  * @s: the backend to reset
169  *
170  * Reset the backend into a well defined state with all previous errors
171  * reset.
172  */
173 void tpm_backend_reset(TPMBackend *s);
174 
175 /**
176  * tpm_backend_cancel_cmd:
177  * @s: the backend
178  *
179  * Cancel any ongoing command being processed by the TPM implementation
180  * on behalf of the QEMU guest.
181  */
182 void tpm_backend_cancel_cmd(TPMBackend *s);
183 
184 /**
185  * tpm_backend_get_tpm_established_flag:
186  * @s: the backend
187  *
188  * Get the TPM establishment flag. This function may be called very
189  * frequently by the frontend since for example in the TIS implementation
190  * this flag is part of a register.
191  */
192 bool tpm_backend_get_tpm_established_flag(TPMBackend *s);
193 
194 /**
195  * tpm_backend_open:
196  * @s: the backend to open
197  * @errp: a pointer to return the #Error object if an error occurs.
198  *
199  * This function will open the backend if it is not already open.  Calling this
200  * function on an already opened backend will not result in an error.
201  */
202 void tpm_backend_open(TPMBackend *s, Error **errp);
203 
204 TPMBackend *qemu_find_tpm(const char *id);
205 
206 const TPMDriverOps *tpm_get_backend_driver(const char *type);
207 int tpm_register_model(enum TpmModel model);
208 int tpm_register_driver(const TPMDriverOps *tdo);
209 
210 #endif
211