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 TPMBackendCmd { 34 uint8_t locty; 35 const uint8_t *in; 36 uint32_t in_len; 37 uint8_t *out; 38 uint32_t out_len; 39 bool selftest_done; 40 } TPMBackendCmd; 41 42 struct TPMBackend { 43 Object parent; 44 45 /*< protected >*/ 46 bool opened; 47 TPMState *tpm_state; 48 GThreadPool *thread_pool; 49 bool had_startup_error; 50 51 /* <public> */ 52 char *id; 53 enum TpmModel fe_model; 54 55 QLIST_ENTRY(TPMBackend) list; 56 }; 57 58 struct TPMBackendClass { 59 ObjectClass parent_class; 60 61 enum TpmType type; 62 const QemuOptDesc *opts; 63 /* get a descriptive text of the backend to display to the user */ 64 const char *desc; 65 66 TPMBackend *(*create)(QemuOpts *opts, const char *id); 67 68 /* start up the TPM on the backend */ 69 int (*startup_tpm)(TPMBackend *t); 70 71 void (*reset)(TPMBackend *t); 72 73 void (*cancel_cmd)(TPMBackend *t); 74 75 bool (*get_tpm_established_flag)(TPMBackend *t); 76 77 int (*reset_tpm_established_flag)(TPMBackend *t, uint8_t locty); 78 79 TPMVersion (*get_tpm_version)(TPMBackend *t); 80 81 TpmTypeOptions *(*get_tpm_options)(TPMBackend *t); 82 83 void (*opened)(TPMBackend *s, Error **errp); 84 85 void (*handle_request)(TPMBackend *s, TPMBackendCmd *cmd); 86 }; 87 88 /** 89 * tpm_backend_get_type: 90 * @s: the backend 91 * 92 * Returns the TpmType of the backend. 93 */ 94 enum TpmType tpm_backend_get_type(TPMBackend *s); 95 96 /** 97 * tpm_backend_init: 98 * @s: the backend to initialized 99 * @state: TPMState 100 * @datacb: callback for sending data to frontend 101 * 102 * Initialize the backend with the given variables. 103 * 104 * Returns 0 on success. 105 */ 106 int tpm_backend_init(TPMBackend *s, TPMState *state); 107 108 /** 109 * tpm_backend_startup_tpm: 110 * @s: the backend whose TPM support is to be started 111 * 112 * Returns 0 on success. 113 */ 114 int tpm_backend_startup_tpm(TPMBackend *s); 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 */ 124 bool 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 */ 134 void 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 */ 143 void 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 */ 152 void 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 */ 162 bool 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 */ 171 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty); 172 173 /** 174 * tpm_backend_open: 175 * @s: the backend to open 176 * @errp: a pointer to return the #Error object if an error occurs. 177 * 178 * This function will open the backend if it is not already open. Calling this 179 * function on an already opened backend will not result in an error. 180 */ 181 void tpm_backend_open(TPMBackend *s, Error **errp); 182 183 /** 184 * tpm_backend_get_tpm_version: 185 * @s: the backend to call into 186 * 187 * Get the TPM Version that is emulated at the backend. 188 * 189 * Returns TPMVersion. 190 */ 191 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s); 192 193 /** 194 * tpm_backend_query_tpm: 195 * @s: the backend 196 * 197 * Query backend tpm info 198 * 199 * Returns newly allocated TPMInfo 200 */ 201 TPMInfo *tpm_backend_query_tpm(TPMBackend *s); 202 203 TPMBackend *qemu_find_tpm(const char *id); 204 205 void tpm_register_model(enum TpmModel model); 206 207 #endif 208