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#define TPM_BACKEND(obj) \
23 OBJECT_CHECK(TPMBackend, (obj), TYPE_TPM_BACKEND)
24#define TPM_BACKEND_GET_CLASS(obj) \
25 OBJECT_GET_CLASS(TPMBackendClass, (obj), TYPE_TPM_BACKEND)
26#define TPM_BACKEND_CLASS(klass) \
27 OBJECT_CLASS_CHECK(TPMBackendClass, (klass), TYPE_TPM_BACKEND)
28
29typedef struct TPMBackendClass TPMBackendClass;
30typedef struct TPMBackend TPMBackend;
31
32typedef struct TPMBackendCmd {
33 uint8_t locty;
34 const uint8_t *in;
35 uint32_t in_len;
36 uint8_t *out;
37 uint32_t out_len;
38 bool selftest_done;
39} TPMBackendCmd;
40
41struct TPMBackend {
42 Object parent;
43
44 /*< protected >*/
45 TPMIf *tpmif;
46 bool opened;
47 bool had_startup_error;
48 TPMBackendCmd *cmd;
49
50 /* <public> */
51 char *id;
52
53 QLIST_ENTRY(TPMBackend) list;
54};
55
56struct TPMBackendClass {
57 ObjectClass parent_class;
58
59 enum TpmType type;
60 const QemuOptDesc *opts;
61 /* get a descriptive text of the backend to display to the user */
62 const char *desc;
63
64 TPMBackend *(*create)(QemuOpts *opts);
65
66 /* start up the TPM on the backend - optional */
67 int (*startup_tpm)(TPMBackend *t, size_t buffersize);
68
69 /* optional */
70 void (*reset)(TPMBackend *t);
71
72 void (*cancel_cmd)(TPMBackend *t);
73
74 /* optional */
75 bool (*get_tpm_established_flag)(TPMBackend *t);
76
77 /* optional */
78 int (*reset_tpm_established_flag)(TPMBackend *t, uint8_t locty);
79
80 TPMVersion (*get_tpm_version)(TPMBackend *t);
81
82 size_t (*get_buffer_size)(TPMBackend *t);
83
84 TpmTypeOptions *(*get_tpm_options)(TPMBackend *t);
85
86 void (*handle_request)(TPMBackend *s, TPMBackendCmd *cmd, Error **errp);
87};
88
89/**
90 * tpm_backend_get_type:
91 * @s: the backend
92 *
93 * Returns the TpmType of the backend.
94 */
95enum 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 * @errp: a pointer to return the #Error object if an error occurs.
103 *
104 * Initialize the backend with the given variables.
105 *
106 * Returns 0 on success.
107 */
108int tpm_backend_init(TPMBackend *s, TPMIf *tpmif, Error **errp);
109
110/**
111 * tpm_backend_startup_tpm:
112 * @s: the backend whose TPM support is to be started
113 * @buffersize: the buffer size the TPM is supposed to use,
114 * 0 to leave it as-is
115 *
116 * Returns 0 on success.
117 */
118int tpm_backend_startup_tpm(TPMBackend *s, size_t buffersize);
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 */
128bool 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 */
138void 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 */
147void 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 */
156void 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 */
166bool 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 */
175int 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 */
185TPMVersion 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 */
195size_t tpm_backend_get_buffer_size(TPMBackend *s);
196
197/**
198 * tpm_backend_finish_sync:
199 * @s: the backend to call into
200 *
201 * Finish the pending command synchronously (this will call aio_poll()
202 * on qemu main AIOContext until it ends)
203 */
204void tpm_backend_finish_sync(TPMBackend *s);
205
206/**
207 * tpm_backend_query_tpm:
208 * @s: the backend
209 *
210 * Query backend tpm info
211 *
212 * Returns newly allocated TPMInfo
213 */
214TPMInfo *tpm_backend_query_tpm(TPMBackend *s);
215
216TPMBackend *qemu_find_tpm_be(const char *id);
217
218#endif
219