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 * Based on backends/rng.c by Anthony Liguori 13 */ 14 15 #include "qemu/osdep.h" 16 #include "sysemu/tpm_backend.h" 17 #include "qapi/error.h" 18 #include "qapi/qmp/qerror.h" 19 #include "sysemu/tpm.h" 20 #include "qemu/thread.h" 21 22 static void tpm_backend_worker_thread(gpointer data, gpointer user_data) 23 { 24 TPMBackend *s = TPM_BACKEND(user_data); 25 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 26 27 assert(k->handle_request != NULL); 28 k->handle_request(s, (TPMBackendCmd)data); 29 } 30 31 static void tpm_backend_thread_end(TPMBackend *s) 32 { 33 if (s->thread_pool) { 34 g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_END, NULL); 35 g_thread_pool_free(s->thread_pool, FALSE, TRUE); 36 s->thread_pool = NULL; 37 } 38 } 39 40 enum TpmType tpm_backend_get_type(TPMBackend *s) 41 { 42 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 43 44 return k->type; 45 } 46 47 int tpm_backend_init(TPMBackend *s, TPMState *state, 48 TPMRecvDataCB *datacb) 49 { 50 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 51 52 s->tpm_state = state; 53 s->recv_data_callback = datacb; 54 s->had_startup_error = false; 55 56 return k->init ? k->init(s) : 0; 57 } 58 59 int tpm_backend_startup_tpm(TPMBackend *s) 60 { 61 int res = 0; 62 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 63 64 /* terminate a running TPM */ 65 tpm_backend_thread_end(s); 66 67 s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE, 68 NULL); 69 g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL); 70 71 res = k->startup_tpm ? k->startup_tpm(s) : 0; 72 73 s->had_startup_error = (res != 0); 74 75 return res; 76 } 77 78 bool tpm_backend_had_startup_error(TPMBackend *s) 79 { 80 return s->had_startup_error; 81 } 82 83 void tpm_backend_deliver_request(TPMBackend *s) 84 { 85 g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD, 86 NULL); 87 } 88 89 void tpm_backend_reset(TPMBackend *s) 90 { 91 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 92 93 if (k->reset) { 94 k->reset(s); 95 } 96 97 tpm_backend_thread_end(s); 98 99 s->had_startup_error = false; 100 } 101 102 void tpm_backend_cancel_cmd(TPMBackend *s) 103 { 104 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 105 106 assert(k->cancel_cmd); 107 108 k->cancel_cmd(s); 109 } 110 111 bool tpm_backend_get_tpm_established_flag(TPMBackend *s) 112 { 113 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 114 115 return k->get_tpm_established_flag ? 116 k->get_tpm_established_flag(s) : false; 117 } 118 119 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty) 120 { 121 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 122 123 return k->reset_tpm_established_flag ? 124 k->reset_tpm_established_flag(s, locty) : 0; 125 } 126 127 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s) 128 { 129 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 130 131 assert(k->get_tpm_version); 132 133 return k->get_tpm_version(s); 134 } 135 136 TPMInfo *tpm_backend_query_tpm(TPMBackend *s) 137 { 138 TPMInfo *info = g_new0(TPMInfo, 1); 139 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 140 141 info->id = g_strdup(s->id); 142 info->model = s->fe_model; 143 if (k->get_tpm_options) { 144 info->options = k->get_tpm_options(s); 145 } 146 147 return info; 148 } 149 150 static bool tpm_backend_prop_get_opened(Object *obj, Error **errp) 151 { 152 TPMBackend *s = TPM_BACKEND(obj); 153 154 return s->opened; 155 } 156 157 void tpm_backend_open(TPMBackend *s, Error **errp) 158 { 159 object_property_set_bool(OBJECT(s), true, "opened", errp); 160 } 161 162 static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp) 163 { 164 TPMBackend *s = TPM_BACKEND(obj); 165 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 166 Error *local_err = NULL; 167 168 if (value == s->opened) { 169 return; 170 } 171 172 if (!value && s->opened) { 173 error_setg(errp, QERR_PERMISSION_DENIED); 174 return; 175 } 176 177 if (k->opened) { 178 k->opened(s, &local_err); 179 if (local_err) { 180 error_propagate(errp, local_err); 181 return; 182 } 183 } 184 185 s->opened = true; 186 } 187 188 static void tpm_backend_instance_init(Object *obj) 189 { 190 TPMBackend *s = TPM_BACKEND(obj); 191 192 object_property_add_bool(obj, "opened", 193 tpm_backend_prop_get_opened, 194 tpm_backend_prop_set_opened, 195 NULL); 196 s->fe_model = -1; 197 } 198 199 static void tpm_backend_instance_finalize(Object *obj) 200 { 201 TPMBackend *s = TPM_BACKEND(obj); 202 203 g_free(s->id); 204 tpm_backend_thread_end(s); 205 } 206 207 static const TypeInfo tpm_backend_info = { 208 .name = TYPE_TPM_BACKEND, 209 .parent = TYPE_OBJECT, 210 .instance_size = sizeof(TPMBackend), 211 .instance_init = tpm_backend_instance_init, 212 .instance_finalize = tpm_backend_instance_finalize, 213 .class_size = sizeof(TPMBackendClass), 214 .abstract = true, 215 }; 216 217 static void register_types(void) 218 { 219 type_register_static(&tpm_backend_info); 220 } 221 222 type_init(register_types); 223