blob: 8fd3269c1178ae0ad255b4b73b8c43226bd51fad [file] [log] [blame]
Stefan Berger8f0605c2013-03-28 07:26:21 -04001/*
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
Markus Armbruster121d0712016-06-29 10:12:57 +020013#ifndef TPM_BACKEND_H
14#define TPM_BACKEND_H
Stefan Berger8f0605c2013-03-28 07:26:21 -040015
16#include "qom/object.h"
Stefan Berger8f0605c2013-03-28 07:26:21 -040017#include "qemu/option.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020018#include "sysemu/tpm.h"
Marc-André Lureau6a8a2352018-01-29 19:33:06 +010019#include "qapi/error.h"
Stefan Berger8f0605c2013-03-28 07:26:21 -040020
Stefan Bergere542b712021-06-15 16:21:20 +020021#ifdef CONFIG_TPM
22
Stefan Berger8f0605c2013-03-28 07:26:21 -040023#define TYPE_TPM_BACKEND "tpm-backend"
Eduardo Habkostc821774a2020-08-31 17:07:37 -040024OBJECT_DECLARE_TYPE(TPMBackend, TPMBackendClass,
Eduardo Habkost30b57072020-09-16 14:25:17 -040025 TPM_BACKEND)
Stefan Berger8f0605c2013-03-28 07:26:21 -040026
Marc-André Lureaud31076b2017-10-10 00:55:49 +020027
Marc-André Lureau0e43b7e2017-10-10 00:55:55 +020028typedef struct TPMBackendCmd {
29 uint8_t locty;
30 const uint8_t *in;
31 uint32_t in_len;
32 uint8_t *out;
33 uint32_t out_len;
34 bool selftest_done;
35} TPMBackendCmd;
Stefan Berger8f0605c2013-03-28 07:26:21 -040036
Stefan Berger8f0605c2013-03-28 07:26:21 -040037struct TPMBackend {
38 Object parent;
39
40 /*< protected >*/
Marc-André Lureau8a89c9a2017-11-06 19:39:00 +010041 TPMIf *tpmif;
Stefan Berger8f0605c2013-03-28 07:26:21 -040042 bool opened;
Amarnath Valluri93330cf2017-09-29 14:10:16 +030043 bool had_startup_error;
Marc-André Lureauc4fb8562018-01-29 19:33:05 +010044 TPMBackendCmd *cmd;
Stefan Berger8f0605c2013-03-28 07:26:21 -040045
Amarnath Vallurif59864b2017-09-29 14:10:17 +030046 /* <public> */
Stefan Berger8f0605c2013-03-28 07:26:21 -040047 char *id;
Stefan Berger8f0605c2013-03-28 07:26:21 -040048
49 QLIST_ENTRY(TPMBackend) list;
50};
51
Amarnath Vallurib19a5ee2017-09-29 14:10:14 +030052struct TPMBackendClass {
53 ObjectClass parent_class;
54
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020055 enum TpmType type;
Stefan Bergerbb716232013-04-22 10:41:39 -040056 const QemuOptDesc *opts;
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020057 /* get a descriptive text of the backend to display to the user */
Amarnath Valluri93330cf2017-09-29 14:10:16 +030058 const char *desc;
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020059
Marc-André Lureau9f7c0ef2017-11-06 19:39:09 +010060 TPMBackend *(*create)(QemuOpts *opts);
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020061
Marc-André Lureauebca2df2017-11-06 19:39:07 +010062 /* start up the TPM on the backend - optional */
Stefan Berger9375c442017-11-04 19:57:15 -040063 int (*startup_tpm)(TPMBackend *t, size_t buffersize);
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020064
Marc-André Lureauebca2df2017-11-06 19:39:07 +010065 /* optional */
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020066 void (*reset)(TPMBackend *t);
67
68 void (*cancel_cmd)(TPMBackend *t);
69
Marc-André Lureauebca2df2017-11-06 19:39:07 +010070 /* optional */
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020071 bool (*get_tpm_established_flag)(TPMBackend *t);
Stefan Berger116694c2015-05-26 16:51:05 -040072
Marc-André Lureauebca2df2017-11-06 19:39:07 +010073 /* optional */
Stefan Berger116694c2015-05-26 16:51:05 -040074 int (*reset_tpm_established_flag)(TPMBackend *t, uint8_t locty);
75
76 TPMVersion (*get_tpm_version)(TPMBackend *t);
Amarnath Vallurif59864b2017-09-29 14:10:17 +030077
Stefan Bergerb21e6aa2017-11-03 18:10:01 -040078 size_t (*get_buffer_size)(TPMBackend *t);
79
Amarnath Vallurif59864b2017-09-29 14:10:17 +030080 TpmTypeOptions *(*get_tpm_options)(TPMBackend *t);
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020081
Marc-André Lureau6a8a2352018-01-29 19:33:06 +010082 void (*handle_request)(TPMBackend *s, TPMBackendCmd *cmd, Error **errp);
Marc-André Lureaud31076b2017-10-10 00:55:49 +020083};
Stefan Berger8f0605c2013-03-28 07:26:21 -040084
85/**
86 * tpm_backend_get_type:
87 * @s: the backend
88 *
89 * Returns the TpmType of the backend.
90 */
91enum TpmType tpm_backend_get_type(TPMBackend *s);
92
93/**
Stefan Berger8f0605c2013-03-28 07:26:21 -040094 * tpm_backend_init:
95 * @s: the backend to initialized
Marc-André Lureau8a89c9a2017-11-06 19:39:00 +010096 * @tpmif: TPM interface
Stefan Berger8f0605c2013-03-28 07:26:21 -040097 * @datacb: callback for sending data to frontend
Marc-André Lureau0bd6c8a2017-11-06 19:39:03 +010098 * @errp: a pointer to return the #Error object if an error occurs.
Stefan Berger8f0605c2013-03-28 07:26:21 -040099 *
100 * Initialize the backend with the given variables.
101 *
102 * Returns 0 on success.
103 */
Marc-André Lureau0bd6c8a2017-11-06 19:39:03 +0100104int tpm_backend_init(TPMBackend *s, TPMIf *tpmif, Error **errp);
Stefan Berger8f0605c2013-03-28 07:26:21 -0400105
106/**
107 * tpm_backend_startup_tpm:
108 * @s: the backend whose TPM support is to be started
Stefan Berger9375c442017-11-04 19:57:15 -0400109 * @buffersize: the buffer size the TPM is supposed to use,
110 * 0 to leave it as-is
Stefan Berger8f0605c2013-03-28 07:26:21 -0400111 *
112 * Returns 0 on success.
113 */
Stefan Berger9375c442017-11-04 19:57:15 -0400114int tpm_backend_startup_tpm(TPMBackend *s, size_t buffersize);
Stefan Berger8f0605c2013-03-28 07:26:21 -0400115
116/**
117 * tpm_backend_had_startup_error:
118 * @s: the backend to query for a statup error
119 *
120 * Check whether the backend had an error during startup. Returns
121 * false if no error occurred and the backend can be used, true
122 * otherwise.
123 */
124bool tpm_backend_had_startup_error(TPMBackend *s);
125
126/**
Stefan Berger8f0605c2013-03-28 07:26:21 -0400127 * tpm_backend_deliver_request:
128 * @s: the backend to send the request to
Marc-André Lureau0e43b7e2017-10-10 00:55:55 +0200129 * @cmd: the command to deliver
Stefan Berger8f0605c2013-03-28 07:26:21 -0400130 *
131 * Send a request to the backend. The backend will then send the request
132 * to the TPM implementation.
133 */
Marc-André Lureau0e43b7e2017-10-10 00:55:55 +0200134void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd);
Stefan Berger8f0605c2013-03-28 07:26:21 -0400135
136/**
137 * tpm_backend_reset:
138 * @s: the backend to reset
139 *
140 * Reset the backend into a well defined state with all previous errors
141 * reset.
142 */
143void tpm_backend_reset(TPMBackend *s);
144
145/**
146 * tpm_backend_cancel_cmd:
147 * @s: the backend
148 *
149 * Cancel any ongoing command being processed by the TPM implementation
150 * on behalf of the QEMU guest.
151 */
152void tpm_backend_cancel_cmd(TPMBackend *s);
153
154/**
155 * tpm_backend_get_tpm_established_flag:
156 * @s: the backend
157 *
158 * Get the TPM establishment flag. This function may be called very
159 * frequently by the frontend since for example in the TIS implementation
160 * this flag is part of a register.
161 */
162bool tpm_backend_get_tpm_established_flag(TPMBackend *s);
163
164/**
Stefan Berger116694c2015-05-26 16:51:05 -0400165 * tpm_backend_reset_tpm_established_flag:
166 * @s: the backend
167 * @locty: the locality number
168 *
169 * Reset the TPM establishment flag.
170 */
171int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty);
172
173/**
Stefan Berger116694c2015-05-26 16:51:05 -0400174 * tpm_backend_get_tpm_version:
175 * @s: the backend to call into
176 *
177 * Get the TPM Version that is emulated at the backend.
178 *
179 * Returns TPMVersion.
180 */
181TPMVersion tpm_backend_get_tpm_version(TPMBackend *s);
182
Amarnath Vallurif59864b2017-09-29 14:10:17 +0300183/**
Stefan Bergerb21e6aa2017-11-03 18:10:01 -0400184 * tpm_backend_get_buffer_size:
185 * @s: the backend to call into
186 *
187 * Get the TPM's buffer size.
188 *
189 * Returns buffer size.
190 */
191size_t tpm_backend_get_buffer_size(TPMBackend *s);
192
193/**
Marc-André Lureauc4fb8562018-01-29 19:33:05 +0100194 * tpm_backend_finish_sync:
195 * @s: the backend to call into
196 *
197 * Finish the pending command synchronously (this will call aio_poll()
198 * on qemu main AIOContext until it ends)
199 */
200void tpm_backend_finish_sync(TPMBackend *s);
201
202/**
Amarnath Vallurif59864b2017-09-29 14:10:17 +0300203 * tpm_backend_query_tpm:
204 * @s: the backend
205 *
206 * Query backend tpm info
207 *
208 * Returns newly allocated TPMInfo
209 */
210TPMInfo *tpm_backend_query_tpm(TPMBackend *s);
211
Marc-André Lureaud36e7db2017-11-06 19:39:14 +0100212TPMBackend *qemu_find_tpm_be(const char *id);
Paolo Bonzinibdee56f2013-04-02 18:28:41 +0200213
Stefan Bergere542b712021-06-15 16:21:20 +0200214#endif /* CONFIG_TPM */
215
216#endif /* TPM_BACKEND_H */