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#ifdef CONFIG_TPM 22 23#define TYPE_TPM_BACKEND "tpm-backend" 24OBJECT_DECLARE_TYPE(TPMBackend, TPMBackendClass, 25 TPM_BACKEND) 26 27 28typedef 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; 36 37struct TPMBackend { 38 Object parent; 39 40 /*< protected >*/ 41 TPMIf *tpmif; 42 bool opened; 43 bool had_startup_error; 44 TPMBackendCmd *cmd; 45 46 /* <public> */ 47 char *id; 48 49 QLIST_ENTRY(TPMBackend) list; 50}; 51 52struct TPMBackendClass { 53 ObjectClass parent_class; 54 55 enum TpmType type; 56 const QemuOptDesc *opts; 57 /* get a descriptive text of the backend to display to the user */ 58 const char *desc; 59 60 TPMBackend *(*create)(QemuOpts *opts); 61 62 /* start up the TPM on the backend - optional */ 63 int (*startup_tpm)(TPMBackend *t, size_t buffersize); 64 65 /* optional */ 66 void (*reset)(TPMBackend *t); 67 68 void (*cancel_cmd)(TPMBackend *t); 69 70 /* optional */ 71 bool (*get_tpm_established_flag)(TPMBackend *t); 72 73 /* optional */ 74 int (*reset_tpm_established_flag)(TPMBackend *t, uint8_t locty); 75 76 TPMVersion (*get_tpm_version)(TPMBackend *t); 77 78 size_t (*get_buffer_size)(TPMBackend *t); 79 80 TpmTypeOptions *(*get_tpm_options)(TPMBackend *t); 81 82 void (*handle_request)(TPMBackend *s, TPMBackendCmd *cmd, Error **errp); 83}; 84 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/** 94 * tpm_backend_init: 95 * @s: the backend to initialized 96 * @tpmif: TPM interface 97 * @datacb: callback for sending data to frontend 98 * @errp: a pointer to return the #Error object if an error occurs. 99 * 100 * Initialize the backend with the given variables. 101 * 102 * Returns 0 on success. 103 */ 104int tpm_backend_init(TPMBackend *s, TPMIf *tpmif, Error **errp); 105 106/** 107 * tpm_backend_startup_tpm: 108 * @s: the backend whose TPM support is to be started 109 * @buffersize: the buffer size the TPM is supposed to use, 110 * 0 to leave it as-is 111 * 112 * Returns 0 on success. 113 */ 114int tpm_backend_startup_tpm(TPMBackend *s, size_t buffersize); 115 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/** 127 * tpm_backend_deliver_request: 128 * @s: the backend to send the request to 129 * @cmd: the command to deliver 130 * 131 * Send a request to the backend. The backend will then send the request 132 * to the TPM implementation. 133 */ 134void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd); 135 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/** 165 * 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/** 174 * 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 183/** 184 * 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/** 194 * 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/** 203 * 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 212TPMBackend *qemu_find_tpm_be(const char *id); 213 214#endif /* CONFIG_TPM */ 215 216#endif /* TPM_BACKEND_H */ 217