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