2 * passthrough TPM driver
4 * Copyright (c) 2010 - 2013 IBM Corporation
6 * Stefan Berger <stefanb@us.ibm.com>
8 * Copyright (C) 2011 IAIK, Graz University of Technology
9 * Author: Andreas Niederl
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, see <http://www.gnu.org/licenses/>
25 #include "qemu-common.h"
26 #include "qapi/error.h"
27 #include "qemu/sockets.h"
32 #include "tpm_backend.h"
34 /* #define DEBUG_TPM */
37 #define DPRINTF(fmt, ...) \
38 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
40 #define DPRINTF(fmt, ...) \
46 typedef struct TPMPassthruThreadParams
{
49 TPMRecvDataCB
*recv_data_callback
;
51 } TPMPassthruThreadParams
;
53 struct TPMPassthruState
{
56 TPMPassthruThreadParams tpm_thread_params
;
60 bool had_startup_error
;
63 #define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
65 static int tpm_passthrough_unix_write(int fd
, const uint8_t *buf
, uint32_t len
)
67 return send_all(fd
, buf
, len
);
70 static int tpm_passthrough_unix_read(int fd
, uint8_t *buf
, uint32_t len
)
72 return recv_all(fd
, buf
, len
, true);
75 static uint32_t tpm_passthrough_get_size_from_buffer(const uint8_t *buf
)
77 struct tpm_resp_hdr
*resp
= (struct tpm_resp_hdr
*)buf
;
79 return be32_to_cpu(resp
->len
);
82 static int tpm_passthrough_unix_tx_bufs(int tpm_fd
,
83 const uint8_t *in
, uint32_t in_len
,
84 uint8_t *out
, uint32_t out_len
)
88 ret
= tpm_passthrough_unix_write(tpm_fd
, in
, in_len
);
90 error_report("tpm_passthrough: error while transmitting data "
92 strerror(errno
), errno
);
96 ret
= tpm_passthrough_unix_read(tpm_fd
, out
, out_len
);
98 error_report("tpm_passthrough: error while reading data from "
100 strerror(errno
), errno
);
101 } else if (ret
< sizeof(struct tpm_resp_hdr
) ||
102 tpm_passthrough_get_size_from_buffer(out
) != ret
) {
104 error_report("tpm_passthrough: received invalid response "
105 "packet from TPM\n");
110 tpm_write_fatal_error_response(out
, out_len
);
116 static int tpm_passthrough_unix_transfer(int tpm_fd
,
117 const TPMLocality
*locty_data
)
119 return tpm_passthrough_unix_tx_bufs(tpm_fd
,
120 locty_data
->w_buffer
.buffer
,
121 locty_data
->w_offset
,
122 locty_data
->r_buffer
.buffer
,
123 locty_data
->r_buffer
.size
);
126 static void tpm_passthrough_worker_thread(gpointer data
,
129 TPMPassthruThreadParams
*thr_parms
= user_data
;
130 TPMPassthruState
*tpm_pt
= thr_parms
->tb
->s
.tpm_pt
;
131 TPMBackendCmd cmd
= (TPMBackendCmd
)data
;
133 DPRINTF("tpm_passthrough: processing command type %d\n", cmd
);
136 case TPM_BACKEND_CMD_PROCESS_CMD
:
137 tpm_passthrough_unix_transfer(tpm_pt
->tpm_fd
,
138 thr_parms
->tpm_state
->locty_data
);
140 thr_parms
->recv_data_callback(thr_parms
->tpm_state
,
141 thr_parms
->tpm_state
->locty_number
);
143 case TPM_BACKEND_CMD_INIT
:
144 case TPM_BACKEND_CMD_END
:
145 case TPM_BACKEND_CMD_TPM_RESET
:
152 * Start the TPM (thread). If it had been started before, then terminate
153 * and start it again.
155 static int tpm_passthrough_startup_tpm(TPMBackend
*tb
)
157 TPMPassthruState
*tpm_pt
= tb
->s
.tpm_pt
;
159 /* terminate a running TPM */
160 tpm_backend_thread_end(&tpm_pt
->tbt
);
162 tpm_backend_thread_create(&tpm_pt
->tbt
,
163 tpm_passthrough_worker_thread
,
164 &tb
->s
.tpm_pt
->tpm_thread_params
);
169 static void tpm_passthrough_reset(TPMBackend
*tb
)
171 TPMPassthruState
*tpm_pt
= tb
->s
.tpm_pt
;
173 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
175 tpm_backend_thread_end(&tpm_pt
->tbt
);
177 tpm_pt
->had_startup_error
= false;
180 static int tpm_passthrough_init(TPMBackend
*tb
, TPMState
*s
,
181 TPMRecvDataCB
*recv_data_cb
)
183 TPMPassthruState
*tpm_pt
= tb
->s
.tpm_pt
;
185 tpm_pt
->tpm_thread_params
.tpm_state
= s
;
186 tpm_pt
->tpm_thread_params
.recv_data_callback
= recv_data_cb
;
187 tpm_pt
->tpm_thread_params
.tb
= tb
;
192 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend
*tb
)
197 static bool tpm_passthrough_get_startup_error(TPMBackend
*tb
)
199 TPMPassthruState
*tpm_pt
= tb
->s
.tpm_pt
;
201 return tpm_pt
->had_startup_error
;
204 static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer
*sb
)
206 size_t wanted_size
= 4096; /* Linux tpm.c buffer size */
208 if (sb
->size
!= wanted_size
) {
209 sb
->buffer
= g_realloc(sb
->buffer
, wanted_size
);
210 sb
->size
= wanted_size
;
215 static void tpm_passthrough_deliver_request(TPMBackend
*tb
)
217 TPMPassthruState
*tpm_pt
= tb
->s
.tpm_pt
;
219 tpm_backend_thread_deliver_request(&tpm_pt
->tbt
);
222 static void tpm_passthrough_cancel_cmd(TPMBackend
*tb
)
224 /* cancelling an ongoing command is known not to work with some TPMs */
227 static const char *tpm_passthrough_create_desc(void)
229 return "Passthrough TPM backend driver";
233 * A basic test of a TPM device. We expect a well formatted response header
234 * (error response is fine) within one second.
236 static int tpm_passthrough_test_tpmdev(int fd
)
238 struct tpm_req_hdr req
= {
239 .tag
= cpu_to_be16(TPM_TAG_RQU_COMMAND
),
240 .len
= cpu_to_be32(sizeof(req
)),
241 .ordinal
= cpu_to_be32(TPM_ORD_GetTicks
),
243 struct tpm_resp_hdr
*resp
;
246 struct timeval tv
= {
250 unsigned char buf
[1024];
252 n
= write(fd
, &req
, sizeof(req
));
256 if (n
!= sizeof(req
)) {
261 FD_SET(fd
, &readfds
);
263 /* wait for a second */
264 n
= select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
269 n
= read(fd
, &buf
, sizeof(buf
));
270 if (n
< sizeof(struct tpm_resp_hdr
)) {
274 resp
= (struct tpm_resp_hdr
*)buf
;
275 /* check the header */
276 if (be16_to_cpu(resp
->tag
) != TPM_TAG_RSP_COMMAND
||
277 be32_to_cpu(resp
->len
) != n
) {
284 static int tpm_passthrough_handle_device_opts(QemuOpts
*opts
, TPMBackend
*tb
)
288 value
= qemu_opt_get(opts
, "path");
290 value
= TPM_PASSTHROUGH_DEFAULT_DEVICE
;
293 tb
->s
.tpm_pt
->tpm_dev
= g_strdup(value
);
295 tb
->path
= g_strdup(tb
->s
.tpm_pt
->tpm_dev
);
297 tb
->s
.tpm_pt
->tpm_fd
= qemu_open(tb
->s
.tpm_pt
->tpm_dev
, O_RDWR
);
298 if (tb
->s
.tpm_pt
->tpm_fd
< 0) {
299 error_report("Cannot access TPM device using '%s': %s\n",
300 tb
->s
.tpm_pt
->tpm_dev
, strerror(errno
));
301 goto err_free_parameters
;
304 if (tpm_passthrough_test_tpmdev(tb
->s
.tpm_pt
->tpm_fd
)) {
305 error_report("'%s' is not a TPM device.\n",
306 tb
->s
.tpm_pt
->tpm_dev
);
307 goto err_close_tpmdev
;
313 qemu_close(tb
->s
.tpm_pt
->tpm_fd
);
314 tb
->s
.tpm_pt
->tpm_fd
= -1;
320 g_free(tb
->s
.tpm_pt
->tpm_dev
);
321 tb
->s
.tpm_pt
->tpm_dev
= NULL
;
326 static TPMBackend
*tpm_passthrough_create(QemuOpts
*opts
, const char *id
)
330 tb
= g_new0(TPMBackend
, 1);
331 tb
->s
.tpm_pt
= g_new0(TPMPassthruState
, 1);
332 tb
->id
= g_strdup(id
);
333 /* let frontend set the fe_model to proper value */
336 tb
->ops
= &tpm_passthrough_driver
;
338 if (tpm_passthrough_handle_device_opts(opts
, tb
)) {
346 g_free(tb
->s
.tpm_pt
);
352 static void tpm_passthrough_destroy(TPMBackend
*tb
)
354 TPMPassthruState
*tpm_pt
= tb
->s
.tpm_pt
;
356 tpm_backend_thread_end(&tpm_pt
->tbt
);
358 qemu_close(tpm_pt
->tpm_fd
);
362 g_free(tb
->s
.tpm_pt
->tpm_dev
);
363 g_free(tb
->s
.tpm_pt
);
367 const TPMDriverOps tpm_passthrough_driver
= {
368 .type
= TPM_TYPE_PASSTHROUGH
,
369 .desc
= tpm_passthrough_create_desc
,
370 .create
= tpm_passthrough_create
,
371 .destroy
= tpm_passthrough_destroy
,
372 .init
= tpm_passthrough_init
,
373 .startup_tpm
= tpm_passthrough_startup_tpm
,
374 .realloc_buffer
= tpm_passthrough_realloc_buffer
,
375 .reset
= tpm_passthrough_reset
,
376 .had_startup_error
= tpm_passthrough_get_startup_error
,
377 .deliver_request
= tpm_passthrough_deliver_request
,
378 .cancel_cmd
= tpm_passthrough_cancel_cmd
,
379 .get_tpm_established_flag
= tpm_passthrough_get_tpm_established_flag
,
382 static void tpm_passthrough_register(void)
384 tpm_register_driver(&tpm_passthrough_driver
);
387 type_init(tpm_passthrough_register
)