ia64/xen-unstable

view tools/vtpm/vtpm.patch @ 14445:cb1693873a7e

libxc: Explicitly reserve space for special pages in HVM memmap.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Fri Mar 16 18:59:28 2007 +0000 (2007-03-16)
parents a02622437e07
children 4c2124aac598
line source
1 diff -uprN tpm_emulator/AUTHORS vtpm/AUTHORS
2 --- tpm_emulator/AUTHORS 2006-12-08 12:51:29.000000000 -0800
3 +++ vtpm/AUTHORS 2006-12-13 16:38:52.000000000 -0800
4 @@ -1,3 +1,3 @@
5 Mario Strasser <mast@gmx.net>
6 Heiko Stamer <stamer@gaos.org> [DAA]
7 -INTEL Corp <> [Dropped to Ring3]
8 +INTEL Corp <> [VTPM Extensions]
9 diff -uprN tpm_emulator/ChangeLog vtpm/ChangeLog
10 --- tpm_emulator/ChangeLog 2006-12-08 12:51:29.000000000 -0800
11 +++ vtpm/ChangeLog 2006-12-13 16:38:52.000000000 -0800
12 @@ -1,5 +1,6 @@
13 ????-??-?? Intel Corp
14 * Moved module out of kernel to run as a ring 3 app
15 + * Modified save_to_file and load_from_file to call xen VTPM manager
17 2006-06-23 Mario Strasser <mast@gmx.net>
18 * tpm_startup.c: behaviour of ST_CLEAR and storage of
19 diff -uprN tpm_emulator/linux_module.h vtpm/linux_module.h
20 --- tpm_emulator/linux_module.h 2006-12-08 12:51:29.000000000 -0800
21 +++ vtpm/linux_module.h 2007-01-09 14:49:06.000000000 -0800
22 @@ -44,18 +44,26 @@
23 #define TPM_DEVICE_NAME "tpm"
24 #define TPM_MODULE_NAME "tpm_emulator"
26 +/* debug and log output functions */
27 +extern int dmi_id;
28 +
29 #ifdef DEBUG
30 -#define debug(fmt, ...) printf("TPMD: %s:%d: Debug: " fmt "\n", \
31 - __FILE__, __LINE__, ## __VA_ARGS__)
32 +#define debug(fmt, ...) printf("TPMD[%d]: %s:%d: Debug: " fmt "\n", \
33 + dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
34 +#define debug_nostop(fmt, ...) printf("TPMD[%d]: %s:%d: Debug: " fmt, \
35 + dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
36 +#define debug_more(fmt, ...) printf( fmt, ## __VA_ARGS__ )
37 #else
38 #define debug(fmt, ...)
39 +#define debug_nostop(fmt, ...)
40 +#define debug_more(fmt, ...)
41 #endif
42 -#define info(fmt, ...) printf("TPMD: %s:%d: Info: " fmt "\n", \
43 - __FILE__, __LINE__, ## __VA_ARGS__)
44 -#define error(fmt, ...) printf("TPMD: %s:%d: Error: " fmt "\n", \
45 - __FILE__, __LINE__, ## __VA_ARGS__)
46 -#define alert(fmt, ...) printf("TPMD: %s:%d: Alert: " fmt "\n", \
47 - __FILE__, __LINE__, ## __VA_ARGS__)
48 +#define info(fmt, ...) printf("TPMD[%d]: %s:%d: Info: " fmt "\n", \
49 + dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
50 +#define error(fmt, ...) printf("TPMD[%d]: %s:%d: Error: " fmt "\n", \
51 + dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
52 +#define alert(fmt, ...) printf("TPMD[%d]: %s:%d: Alert: " fmt "\n", \
53 + dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
55 /* memory allocation */
57 diff -uprN tpm_emulator/Makefile vtpm/Makefile
58 --- tpm_emulator/Makefile 2006-12-08 12:51:29.000000000 -0800
59 +++ vtpm/Makefile 2006-12-13 16:38:52.000000000 -0800
60 @@ -7,7 +7,7 @@
61 COMPILE_ARCH ?= $(shell uname -m | sed -e s/i.86/x86_32/)
63 # module settings
64 -BIN := tpm_emulator
65 +BIN := vtpmd
66 VERSION_MAJOR := 0
67 VERSION_MINOR := 4
68 VERSION_BUILD := $(shell date +"%s")
69 @@ -22,7 +22,7 @@ TOOLS_INSTALL_DIR = $(DESTDIR)/usr/bin
71 CC := gcc
72 CFLAGS += -g -Wall $(INCLUDE) -DDEBUG
73 -CFLAGS += -I. -Itpm
74 +CFLAGS += -I. -Itpm -I../../vtpm_manager/manager
76 # Is the simulator running in it's own vm?
77 #CFLAGS += -DVTPM_MULTI_VM
78 @@ -62,7 +62,6 @@ $(BIN): $(src)/crypto/gmp.h $(src)/crypt
80 install: $(BIN)
81 $(INSTALL_PROG) $(BIN) $(TOOLS_INSTALL_DIR)
82 - @if [ ! -d "/var/tpm" ]; then mkdir /var/tpm; fi
84 clean:
85 rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a $(OBJS)
86 @@ -98,3 +97,4 @@ version:
87 @echo "#endif /* _TPM_VERSION_H_ */" >> $(src)/tpm_version.h
89 .PHONY: all install clean dist gmp version
90 +
91 diff -uprN tpm_emulator/tpm/tpm_capability.c vtpm/tpm/tpm_capability.c
92 --- tpm_emulator/tpm/tpm_capability.c 2006-06-23 03:37:07.000000000 -0700
93 +++ vtpm/tpm/tpm_capability.c 2007-01-10 10:00:49.000000000 -0800
94 @@ -136,8 +136,18 @@ static TPM_RESULT cap_property(UINT32 su
96 case TPM_CAP_PROP_TIS_TIMEOUT:
97 debug("[TPM_CAP_PROP_TIS_TIMEOUT]");
98 - /* TODO: TPM_CAP_PROP_TIS_TIMEOUT */
99 - return TPM_FAIL;
100 + /* TODO: TPM_CAP_PROP_TIS_TIMEOUT: Measure these values and determine correct ones */
101 + UINT32 len = *respSize = 16;
102 + BYTE *ptr = *resp = tpm_malloc(*respSize);
103 + if (ptr == NULL ||
104 + tpm_marshal_UINT32(&ptr, &len, 200000) ||
105 + tpm_marshal_UINT32(&ptr, &len, 200000) ||
106 + tpm_marshal_UINT32(&ptr, &len, 200000) ||
107 + tpm_marshal_UINT32(&ptr, &len, 200000)) {
108 + tpm_free(*resp);
109 + return TPM_FAIL;
110 + }
111 + return TPM_SUCCESS;
113 case TPM_CAP_PROP_STARTUP_EFFECT:
114 debug("[TPM_CAP_PROP_STARTUP_EFFECT]");
115 @@ -190,7 +200,11 @@ static TPM_RESULT cap_property(UINT32 su
117 case TPM_CAP_PROP_DURATION:
118 debug("[TPM_CAP_PROP_DURATION]");
119 - /* TODO: TPM_CAP_PROP_DURATION */
120 + /* TODO: TPM_CAP_PROP_DURATION: Measure these values and return accurate ones */
121 + BYTE dur[]= {0x0,0x0,0x0,0xc,0x0,0x7,0xa1,0x20,0x0,0x1e,0x84,0x80,0x11,0xe1,0xa3,0x0};
122 + *respSize = 16;
123 + *resp = tpm_malloc(*respSize);
124 + memcpy(*resp,dur,16);
125 return TPM_FAIL;
127 case TPM_CAP_PROP_ACTIVE_COUNTER:
128 diff -uprN tpm_emulator/tpm/tpm_data.c vtpm/tpm/tpm_data.c
129 --- tpm_emulator/tpm/tpm_data.c 2006-12-08 12:51:29.000000000 -0800
130 +++ vtpm/tpm/tpm_data.c 2006-12-13 16:38:52.000000000 -0800
131 @@ -1,6 +1,7 @@
132 /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
133 * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
134 * Swiss Federal Institute of Technology (ETH) Zurich
135 + * Copyright (C) 2005 INTEL Corp
136 *
137 * This module is free software; you can redistribute it and/or modify
138 * it under the terms of the GNU General Public License as published
139 @@ -15,10 +16,15 @@
140 * $Id: tpm_data.c 98 2006-05-07 14:16:29Z hstamer $
141 */
143 +#include <sys/types.h>
144 +#include <sys/stat.h>
145 +#include <fcntl.h>
146 +#include <unistd.h>
147 +
148 #include "tpm_emulator.h"
149 #include "tpm_structures.h"
150 #include "tpm_marshalling.h"
151 -#include "linux_module.h"
152 +#include "vtpm_manager.h"
154 TPM_DATA tpmData;
156 @@ -158,45 +164,232 @@ void tpm_release_data(void)
157 #include <sys/types.h>
158 #include <sys/stat.h>
159 #include <fcntl.h>
160 -#include <unistd.h>
162 -#define TPM_STORAGE_FILE "/var/tpm/tpm_emulator-1.2." STR(VERSION_MAJOR) "." STR(VERSION_MINOR)
163 + static int vtpm_tx_fh=-1, vtpm_rx_fh=-1;
164 +
165 +#ifdef VTPM_MUTLI_VM
166 + #define DEV_FE "/dev/tpm"
167 +#else
168 + #define VTPM_RX_FIFO_D "/var/vtpm/fifos/vtpm_rsp_to_%d.fifo"
169 + #define VTPM_TX_FIFO "/var/vtpm/fifos/vtpm_cmd_from_all.fifo"
170 +
171 + extern int dmi_id;
172 + static char *vtpm_rx_name=NULL;
173 +#endif
175 static int write_to_file(uint8_t *data, size_t data_length)
176 {
177 - int res;
178 - int fp;
179 - fp = open(TPM_STORAGE_FILE, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
180 - res = write(fp, data, data_length);
181 - close(fp);
182 - return (res == data_length) ? 0 : -1;
183 + int res, out_data_size, in_header_size;
184 + BYTE *ptr, *out_data, *in_header;
185 + UINT32 result, len, in_rsp_size;
186 + UINT16 tag = VTPM_TAG_REQ;
187 +
188 + printf("Saving NVM\n");
189 + if (vtpm_tx_fh < 0) {
190 +#ifdef VTPM_MUTLI_VM
191 + vtpm_tx_fh = open(DEV_FE, O_RDWR);
192 +#else
193 + vtpm_tx_fh = open(VTPM_TX_FIFO, O_WRONLY);
194 +#endif
195 + }
196 +
197 + if (vtpm_tx_fh < 0) {
198 + return -1;
199 + }
200 +
201 + // Send request to VTPM Manager to encrypt data
202 +#ifdef VTPM_MUTLI_VM
203 + out_data_size = len = VTPM_COMMAND_HEADER_SIZE_CLT + data_length;
204 +#else
205 + out_data_size = len = VTPM_COMMAND_HEADER_SIZE_SRV + data_length;
206 +#endif
207 +
208 + out_data = ptr = (BYTE *) malloc(len);
209 +
210 + if (ptr == NULL
211 +#ifndef VTPM_MUTLI_VM
212 + || tpm_marshal_UINT32(&ptr, &len, dmi_id)
213 +#endif
214 + || tpm_marshal_UINT16(&ptr, &len, tag)
215 +#ifdef VTPM_MUTLI_VM
216 + || tpm_marshal_UINT32(&ptr, &len, out_data_size)
217 +#else
218 + || tpm_marshal_UINT32(&ptr, &len, out_data_size - sizeof(uint32_t))
219 +#endif
220 + || tpm_marshal_UINT32(&ptr, &len, VTPM_ORD_SAVENVM)
221 + || tpm_marshal_BYTE_ARRAY(&ptr, &len, data, data_length)) {
222 + free(out_data);
223 + return -1;
224 + }
225 +
226 + printf("\tSending SaveNVM Command.\n");
227 + res = write(vtpm_tx_fh, out_data, out_data_size);
228 + free(out_data);
229 + if (res != out_data_size) return -1;
230 +
231 + if (vtpm_rx_fh < 0) {
232 +#ifdef VTPM_MUTLI_VM
233 + vtpm_rx_fh = vtpm_tx_fh
234 +#else
235 + if (vtpm_rx_name == NULL) {
236 + vtpm_rx_name = malloc(10 + strlen(VTPM_RX_FIFO_D));
237 + sprintf(vtpm_rx_name, VTPM_RX_FIFO_D, (uint32_t) dmi_id);
238 + }
239 + vtpm_rx_fh = open(vtpm_rx_name, O_RDONLY);
240 +#endif
241 + }
242 +
243 + if (vtpm_rx_fh < 0) {
244 + return -1;
245 + }
246 +
247 + // Read Header of response so we can get the size & status
248 +#ifdef VTPM_MUTLI_VM
249 + in_header_size = len = VTPM_COMMAND_HEADER_SIZE_CLT;
250 +#else
251 + in_header_size = len = VTPM_COMMAND_HEADER_SIZE_SRV;
252 +#endif
253 + in_header = ptr = malloc(in_header_size);
254 +
255 + printf("\tReading SaveNVM header.\n");
256 + res = read(vtpm_rx_fh, in_header, in_header_size);
257 +
258 + if ( (res != in_header_size)
259 +#ifndef VTPM_MUTLI_VM
260 + || tpm_unmarshal_UINT32(&ptr, &len, (UINT32*)&dmi_id)
261 +#endif
262 + || tpm_unmarshal_UINT16(&ptr, &len, &tag)
263 + || tpm_unmarshal_UINT32(&ptr, &len, &in_rsp_size)
264 + || tpm_unmarshal_UINT32(&ptr, &len, &result) ) {
265 + free(in_header);
266 + return -1;
267 + }
268 + free(in_header);
269 +
270 + if (result != VTPM_SUCCESS) {
271 + return -1;
272 + }
273 +
274 +#ifdef VTPM_MUTLI_VM
275 + close(vtpm_tx_fh); close(vtpm_rx_fh);
276 +#endif
277 +
278 + printf("\tFinishing up SaveNVM\n");
279 + return (0);
280 }
282 static int read_from_file(uint8_t **data, size_t *data_length)
283 {
284 - int res;
285 - int fp, file_status;
286 - struct stat file_info;
287 - fp = open(TPM_STORAGE_FILE, O_RDONLY, 0);
288 - file_status = fstat(fp, &file_info);
289 - if (file_status < 0) {
290 - close(fp);
291 - return -1;
292 - }
293 + int res, out_data_size, in_header_size;
294 + uint8_t *ptr, *out_data, *in_header;
295 + UINT16 tag = VTPM_TAG_REQ;
296 + UINT32 len, in_rsp_size, result;
297 +#ifdef VTPM_MUTLI_VM
298 + int vtpm_rx_fh, vtpm_tx_fh;
299 +#endif
300 +
301 + printf("Loading NVM.\n");
302 + if (vtpm_tx_fh < 0) {
303 +#ifdef VTPM_MUTLI_VM
304 + vtpm_tx_fh = open(DEV_FE, O_RDWR);
305 +#else
306 + vtpm_tx_fh = open(VTPM_TX_FIFO, O_WRONLY);
307 +#endif
308 + }
310 - *data_length = file_info.st_size;
311 - *data = tpm_malloc(*data_length);
312 - if (*data == NULL) {
313 - close(fp);
314 + if (vtpm_tx_fh < 0) {
315 + return -1;
316 + }
317 +
318 + // Send request to VTPM Manager to encrypt data
319 +#ifdef VTPM_MUTLI_VM
320 + out_data_size = len = VTPM_COMMAND_HEADER_SIZE_CLT;
321 +#else
322 + out_data_size = len = VTPM_COMMAND_HEADER_SIZE_SRV;
323 +#endif
324 + out_data = ptr = (BYTE *) malloc(len);
325 +
326 + if (ptr == NULL
327 +#ifndef VTPM_MUTLI_VM
328 + || tpm_marshal_UINT32(&ptr, &len, dmi_id)
329 +#endif
330 + || tpm_marshal_UINT16(&ptr, &len, tag)
331 +#ifdef VTPM_MUTLI_VM
332 + || tpm_marshal_UINT32(&ptr, &len, out_data_size)
333 +#else
334 + || tpm_marshal_UINT32(&ptr, &len, out_data_size - sizeof(uint32_t))
335 +#endif
336 + || tpm_marshal_UINT32(&ptr, &len, VTPM_ORD_LOADNVM)) {
337 + free(out_data);
338 return -1;
339 }
340 - res = read(fp, *data, *data_length);
341 - close(fp);
342 +
343 + printf("\tSending LoadNVM command\n");
344 + res = write(vtpm_tx_fh, out_data, out_data_size);
345 + free(out_data);
346 + if (res != out_data_size) return -1;
347 +
348 + if (vtpm_rx_fh < 0) {
349 +#ifdef VTPM_MUTLI_VM
350 + vtpm_rx_fh = vtpm_tx_fh;
351 +#else
352 + if (vtpm_rx_name == NULL) {
353 + vtpm_rx_name = malloc(10 + strlen(VTPM_RX_FIFO_D));
354 + sprintf(vtpm_rx_name, VTPM_RX_FIFO_D, (uint32_t) dmi_id);
355 + }
356 + vtpm_rx_fh = open(vtpm_rx_name, O_RDONLY);
357 +#endif
358 + }
359 +
360 + if (vtpm_rx_fh < 0) {
361 + return -1;
362 + }
363 +
364 + // Read Header of response so we can get the size & status
365 +#ifdef VTPM_MUTLI_VM
366 + in_header_size = len = VTPM_COMMAND_HEADER_SIZE_CLT;
367 +#else
368 + in_header_size = len = VTPM_COMMAND_HEADER_SIZE_SRV;
369 +#endif
370 + in_header = ptr = malloc(in_header_size);
371 +
372 + printf("\tReading LoadNVM header\n");
373 + res = read(vtpm_rx_fh, in_header, in_header_size);
374 +
375 + if ( (res != in_header_size)
376 +#ifndef VTPM_MUTLI_VM
377 + || tpm_unmarshal_UINT32(&ptr, &len, (UINT32*)&dmi_id)
378 +#endif
379 + || tpm_unmarshal_UINT16(&ptr, &len, &tag)
380 + || tpm_unmarshal_UINT32(&ptr, &len, &in_rsp_size)
381 + || tpm_unmarshal_UINT32(&ptr, &len, &result) ) {
382 + free(in_header);
383 + return -1;
384 + }
385 + free(in_header);
386 +
387 + if (result != VTPM_SUCCESS) {
388 + return -1;
389 + }
390 +
391 + // Read Encrypted data from VTPM Manager
392 + *data_length = in_rsp_size - VTPM_COMMAND_HEADER_SIZE_CLT;
393 + *data = (uint8_t *) malloc(*data_length);
394 +
395 + printf("\tReading clear data from LoadNVM.\n");
396 + res = read(vtpm_rx_fh, *data, *data_length);
397 +#ifdef VTPM_MUTLI_VM
398 + close(vtpm_rx_fh);close(vtpm_tx_fh);
399 +#endif
400 +
401 + printf("\tReturing from loading NVM\n");
402 if (res != *data_length) {
403 - tpm_free(*data);
404 - return -1;
405 + free(*data);
406 + return -1;
407 + } else {
408 + return 0;
409 }
410 - return 0;
411 +
412 }
414 #else
415 diff -uprN tpm_emulator/tpmd.c vtpm/tpmd.c
416 --- tpm_emulator/tpmd.c 2006-12-08 12:51:29.000000000 -0800
417 +++ vtpm/tpmd.c 2007-01-09 14:48:56.000000000 -0800
418 @@ -21,12 +21,24 @@
419 #include <sys/stat.h>
420 #include <fcntl.h>
421 #include <sys/time.h>
422 +#include <sys/socket.h>
423 +#include <sys/un.h>
424 +#include <errno.h>
426 #include "tpm_emulator.h"
427 +#include "vtpm_manager.h"
429 -#define TPM_RX_FNAME "/var/tpm/tpm_in.fifo"
430 -#define TPM_TX_FNAME "/var/tpm/tpm_out.fifo"
431 +#ifdef VTPM_MULTI_VM
432 + #define DEV_BE "/dev/vtpm"
433 +#else
434 + #define PVM_RX_FIFO_D "/var/vtpm/fifos/tpm_cmd_to_%d.fifo"
435 + #define PVM_TX_FIFO "/var/vtpm/fifos/tpm_rsp_from_all.fifo"
437 + #define HVM_RX_FIFO_D "/var/vtpm/socks/%d.socket"
438 +#endif
439 +
440 + int dmi_id;
441 +
442 #define BUFFER_SIZE 2048
444 static int devurandom=0;
445 @@ -38,7 +50,7 @@ void get_random_bytes(void *buf, int nby
446 }
448 if (read(devurandom, buf, nbytes) != nbytes) {
449 - printf("Can't get random number.\n");
450 + error("Can't get random number.\n");
451 exit(-1);
452 }
453 }
454 @@ -52,105 +64,182 @@ uint64_t tpm_get_ticks(void)
456 int main(int argc, char **argv)
457 {
458 - uint8_t in[BUFFER_SIZE], *out;
459 + uint8_t type, in[BUFFER_SIZE], *out, *addressed_out;
460 + char *vtpm_rx_file=NULL;
461 uint32_t out_size;
462 int in_size, written;
463 - int i;
464 - struct stat file_info;
465 + int i, guest_id=-1;
467 - int tpm_tx_fh=-1, tpm_rx_fh=-1;
468 +#ifndef VTPM_MULTI_VM
469 + int sockfd = -1;
470 + struct sockaddr_un addr;
471 + struct sockaddr_un client_addr;
472 + unsigned int client_length;
473 +
474 +#endif
475 +
476 + int vtpm_tx_fh=-1, vtpm_rx_fh=-1;
477 +#ifdef VTPM_MULTI_VM
478 if (argc < 2) {
479 - printf("Usage: tpmd clear|save|deactivated\n" );
480 + error("Usage: tpmd clear|save|deactivated\n" );
481 +#else
482 + if (argc < 4) {
483 + error("Usage: tpmd clear|save|deactivated pvm|hvm vtpmid\n" );
484 +#endif
485 return -1;
486 }
488 +#ifndef VTPM_MULTI_VM
489 + /* setup type of vm */
490 + if (!strcmp(argv[2], "pvm")) {
491 + type = VTPM_TYPE_PVM; // Get commands from vTPM Manager through fifo
492 + } else if (!strcmp(argv[2], "hvm")) {
493 + type = VTPM_TYPE_HVM; // Get commands from qemu via socket
494 + } else {
495 + error("invalid vTPM type '%s'.\n", argv[2]);
496 + }
497 +
498 + dmi_id = atoi(argv[3]);
499 +
500 + if (type == VTPM_TYPE_PVM) {
501 + vtpm_rx_file = malloc(10 + strlen(PVM_RX_FIFO_D));
502 + sprintf(vtpm_rx_file, PVM_RX_FIFO_D, (uint32_t) dmi_id);
503 + } else {
504 + vtpm_rx_file = malloc(10 + strlen(HVM_RX_FIFO_D));
505 + sprintf(vtpm_rx_file, HVM_RX_FIFO_D, (uint32_t) dmi_id);
506 +
507 + if ( (sockfd = socket(PF_UNIX,SOCK_STREAM,0)) < 0) {
508 + error("Unable to create socket. errno = %d\n", errno);
509 + exit (-1);
510 + }
511 +
512 + memset(&addr, 0, sizeof(addr));
513 + addr.sun_family = AF_UNIX;
514 + strcpy(addr.sun_path,vtpm_rx_file );
515 + unlink(addr.sun_path);
516 + }
517 +#endif
518 +
519 +#ifdef VTPM_MULTI_VM
520 + info("Initializing tpm state: %s\n", argv[1]);
521 +#else
522 + info("Initializing tpm state: %s, type: %s, id: %d\n", argv[1], argv[2], dmi_id);
523 +#endif
524 +
525 /* initialize TPM emulator */
526 if (!strcmp(argv[1], "clear")) {
527 - printf("Initializing tpm: %s\n", argv[1]);
528 tpm_emulator_init(1);
529 - } else if (!strcmp(argv[1], "save")) {
530 - printf("Initializing tpm: %s\n", argv[1]);
531 + } else if (!strcmp(argv[1], "save")) {
532 tpm_emulator_init(2);
533 } else if (!strcmp(argv[1], "deactivated")) {
534 - printf("Initializing tpm: %s\n", argv[1]);
535 tpm_emulator_init(3);
536 } else {
537 - printf("invalid startup mode '%s'; must be 'clear', "
538 + error("invalid startup mode '%s'; must be 'clear', "
539 "'save' (default) or 'deactivated", argv[1]);
540 return -1;
541 }
542 -
543 - if ( stat(TPM_RX_FNAME, &file_info) == -1) {
544 - if ( mkfifo(TPM_RX_FNAME, S_IWUSR | S_IRUSR ) ) {
545 - printf("Failed to create fifo %s.\n", TPM_RX_FNAME);
546 - return -1;
547 - }
548 - }
549 -
550 - if ( stat(TPM_TX_FNAME, &file_info) == -1) {
551 - if ( mkfifo(TPM_TX_FNAME, S_IWUSR | S_IRUSR ) ) {
552 - printf("Failed to create fifo %s.\n", TPM_TX_FNAME);
553 - return -1;
554 - }
555 - }
556 -
557 +
558 while (1) {
559 abort_command:
560 - if (tpm_rx_fh < 0) {
561 - tpm_rx_fh = open(TPM_RX_FNAME, O_RDONLY);
562 + if (vtpm_rx_fh < 0) {
563 +#ifdef VTPM_MUTLI_VM
564 + vtpm_rx_fh = open(DEV_BE, O_RDWR);
565 +#else
566 + if (type == VTPM_TYPE_PVM) {
567 + vtpm_rx_fh = open(vtpm_rx_file, O_RDONLY);
568 + } else {
569 + if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
570 + error("Unable to bind(). errno = %d\n", errno);
571 + exit (-1);
572 + }
573 +
574 + if (listen(sockfd, 10) <0) {
575 + error("Unable to listen(). errno = %d\n", errno);
576 + exit (-1);
577 + }
578 +
579 + memset(&client_addr, 0, sizeof(client_addr));
580 + client_length = sizeof(client_addr);
581 +
582 + vtpm_rx_fh = vtpm_tx_fh = accept(sockfd, &client_addr, &client_length);
583 + }
584 +#endif
585 }
587 - if (tpm_rx_fh < 0) {
588 - printf("ERROR: failed to open devices to listen to guest.\n");
589 + if (vtpm_rx_fh < 0) {
590 + error("Failed to open devices to listen to guest.\n");
591 return -1;
592 }
594 - if (tpm_tx_fh < 0) {
595 - tpm_tx_fh = open(TPM_TX_FNAME, O_WRONLY);
596 - }
597 -
598 - if (tpm_tx_fh < 0) {
599 - printf("ERROR: failed to open devices to respond to guest.\n");
600 - return -1;
601 - }
602 -
603 - in_size = read(tpm_rx_fh, in, BUFFER_SIZE);
604 + in_size = read(vtpm_rx_fh, in, BUFFER_SIZE);
605 if (in_size < 6) { // Magic size of minium TPM command
606 - printf("Recv[%d] to small: 0x", in_size);
607 + info("Recv incomplete command of %d bytes.", in_size);
608 if (in_size <= 0) {
609 - close(tpm_rx_fh);
610 - tpm_rx_fh = -1;
611 + close(vtpm_rx_fh);
612 + vtpm_rx_fh = -1;
613 goto abort_command;
614 }
615 } else {
616 - printf("Recv[%d]: 0x", in_size);
617 + debug_nostop("Recv[%d]: 0x", in_size);
618 for (i=0; i< in_size; i++)
619 - printf("%x ", in[i]);
620 - printf("\n");
621 + debug_more("%x ", in[i]);
622 + debug_more("\n");
623 }
625 -
626 - if (tpm_handle_command(in, in_size, &out, &out_size) != 0) {
627 - printf("ERROR: Handler Failed.\n");
628 + if (guest_id == -1) {
629 + guest_id = *((uint32_t *) in);
630 + } else {
631 + if (guest_id != *((uint32_t *) in) ) {
632 + error("WARNING: More than one guest attached\n");
633 + }
634 + }
635 +
636 + if (vtpm_tx_fh < 0) {
637 +#ifdef VTPM_MUTLI_VM
638 + vtpm_tx_fh = open(DEV_BE, O_RDWR);
639 + vtpm_rx_fh = vtpm_tx_fh;
640 +#else
641 + if (type == VTPM_TYPE_PVM) {
642 + vtpm_tx_fh = open(PVM_TX_FIFO, O_WRONLY);
643 + } // No need to open the other direction for HVM
644 +#endif
645 + }
646 +
647 + if (vtpm_tx_fh < 0) {
648 + error("Failed to open devices to respond to guest.\n");
649 + return -1;
650 + }
651 +
652 + // Handle the command, but skip the domain id header
653 + if (tpm_handle_command(in + sizeof(uint32_t), in_size - sizeof(uint32_t), &out, &out_size) != 0) {
654 + error("Handler Failed.\n");
655 }
657 - written = write(tpm_tx_fh, out, out_size);
658 + addressed_out = (uint8_t *) tpm_malloc(sizeof(uint32_t) + out_size);
659 + *(uint32_t *) addressed_out = *(uint32_t *) in;
660 + memcpy(addressed_out + sizeof(uint32_t), out, out_size);
661 +
662 + written = write(vtpm_tx_fh, addressed_out, out_size + sizeof(uint32_t));
664 - if (written != out_size ) {
665 - printf("ERROR: Part of response not written %d/%d.\nAttempt: ", written, out_size);
666 + if (written != out_size + sizeof(uint32_t)) {
667 + error("Part of response not written %d/%d.\n", written, out_size);
668 } else {
669 - printf("Sent[%Zu]: ", out_size);
670 + debug_nostop("Sent[%Zu]: ", out_size + sizeof(uint32_t));
671 + for (i=0; i< out_size+ sizeof(uint32_t); i++)
672 + debug_more("%x ", addressed_out[i]);
673 + debug_more("\n");
674 }
675 - for (i=0; i< out_size; i++)
676 - printf("%x ", out[i]);
677 - printf("\n");
678 tpm_free(out);
679 + tpm_free(addressed_out);
681 } // loop
683 tpm_emulator_shutdown();
685 - close(tpm_tx_fh);
686 - close(tpm_rx_fh);
687 + close(vtpm_tx_fh);
688 +#ifndef VTPM_MUTLI_VM
689 + close(vtpm_rx_fh);
690 + free (vtpm_rx_file);
691 +#endif
693 }