debuggers.hg

view tools/vtpm/vtpm-0.5.1.patch @ 22848:6341fe0f4e5a

Added tag 4.1.0-rc2 for changeset 9dca60d88c63
author Keir Fraser <keir@xen.org>
date Tue Jan 25 14:06:55 2011 +0000 (2011-01-25)
parents 4e67ba3c321a
children
line source
1 diff -Naurp tpm_emulator-0.5.1/Makefile tpm5-test/Makefile
2 --- tpm_emulator-0.5.1/Makefile 2008-02-14 03:22:48.000000000 -0500
3 +++ tpm5-test/Makefile 2009-07-15 09:45:28.000000000 -0400
4 @@ -10,7 +10,7 @@ VERSION_MINOR := 5
5 VERSION_BUILD := $(shell date +"%s")
6 VERSION_SUFFIX := .1
8 -SUBDIRS := tpmd tpmd_dev tddl
9 +SUBDIRS := tpmd
11 all: version all-recursive
13 @@ -48,12 +48,12 @@ user_install: user
14 modules_install: modules
15 @$(MAKE) -C tpmd_dev install || exit -1
17 -DIRS := . tpm crypto tpmd tpmd_dev tddl tpmd_dev_openbsd
18 +DIRS := . tpm crypto tpmd
19 DISTSRC := $(foreach dir, $(DIRS), $(wildcard $(dir)/*.c))
20 DISTSRC += $(foreach dir, $(DIRS), $(wildcard $(dir)/*.h))
21 -DIRS := . tpmd tpmd_dev tddl tpmd_dev_openbsd
22 +DIRS := . tpmd
23 DISTSRC += $(foreach dir, $(DIRS), $(dir)/Makefile)
24 -DISTSRC += ./README ./AUTHORS ./ChangeLog tpmd_dev/tpmd_dev.rules.in
25 +DISTSRC += ./README ./AUTHORS ./ChangeLog
26 DISTDIR := tpm_emulator-$(VERSION_MAJOR).$(VERSION_MINOR)$(VERSION_SUFFIX)
28 dist: $(DISTSRC)
29 diff -Naurp tpm_emulator-0.5.1/tpm/tpm_capability.c tpm5-test/tpm/tpm_capability.c
30 --- tpm_emulator-0.5.1/tpm/tpm_capability.c 2008-02-14 03:22:48.000000000 -0500
31 +++ tpm5-test/tpm/tpm_capability.c 2009-07-16 12:04:20.000000000 -0400
32 @@ -136,8 +136,19 @@ static TPM_RESULT cap_property(UINT32 su
34 case TPM_CAP_PROP_TIS_TIMEOUT:
35 debug("[TPM_CAP_PROP_TIS_TIMEOUT]");
36 - /* TODO: TPM_CAP_PROP_TIS_TIMEOUT */
37 - return TPM_FAIL;
38 + /* TODO: TPM_CAP_PROP_TIS_TIMEOUT: Measure these values and determine correct ones */
39 + UINT32 len = *respSize = 16;
40 + BYTE *ptr = *resp = tpm_malloc(*respSize);
41 + if (ptr == NULL ||
42 + tpm_marshal_UINT32(&ptr, &len, 200000) ||
43 + tpm_marshal_UINT32(&ptr, &len, 200000) ||
44 + tpm_marshal_UINT32(&ptr, &len, 200000) ||
45 + tpm_marshal_UINT32(&ptr, &len, 200000)) {
46 + tpm_free(*resp);
47 + return TPM_FAIL;
48 + }
49 + return TPM_SUCCESS;
50 +
52 case TPM_CAP_PROP_STARTUP_EFFECT:
53 debug("[TPM_CAP_PROP_STARTUP_EFFECT]");
54 @@ -189,8 +200,12 @@ static TPM_RESULT cap_property(UINT32 su
56 case TPM_CAP_PROP_DURATION:
57 debug("[TPM_CAP_PROP_DURATION]");
58 - /* TODO: TPM_CAP_PROP_DURATION */
59 - return TPM_FAIL;
60 + /* TODO: TPM_CAP_PROP_DURATION: Measure these values and return accurate ones */
61 + BYTE dur[]= {0x0,0x0,0x0,0xc,0x0,0x7,0xa1,0x20,0x0,0x1e,0x84,0x80,0x11,0xe1,0xa3,0x0};
62 + *respSize = 16;
63 + *resp = tpm_malloc(*respSize);
64 + memcpy(*resp,dur,16);
65 +
67 case TPM_CAP_PROP_ACTIVE_COUNTER:
68 debug("[TPM_CAP_PROP_ACTIVE_COUNTER]");
69 diff -Naurp tpm_emulator-0.5.1/tpmd/Makefile tpm5-test/tpmd/Makefile
70 --- tpm_emulator-0.5.1/tpmd/Makefile 2008-02-14 03:22:48.000000000 -0500
71 +++ tpm5-test/tpmd/Makefile 2009-07-16 12:08:26.000000000 -0400
72 @@ -8,9 +8,10 @@ WFLAGS := -Wall -Wno-unused -Wpointer-a
73 -Wwrite-strings -Wsign-compare -Wno-multichar
74 #WFLAGS += -Wextra -Wcast-qual -Wmissing-prototypes -Wmissing-declarations -Wstrict-aliasing
75 CFLAGS += $(WFLAGS) -g -I.. -I. -O2 -fno-strict-aliasing
76 +CFLAGS += -I../../../../tools/vtpm_manager/manager
77 LDFLAGS += -lgmp
79 -BINDIR := /usr/sbin/
80 +BINDIR := /usr/bin/
82 TPMD := tpmd
83 DIRS := ../tpm ../crypto
84 @@ -18,6 +19,8 @@ SRCS := $(foreach dir, $(DIRS), $(wil
85 OBJS := $(patsubst %.c, %.o, $(SRCS))
86 OBJS := $(foreach dir, $(DIRS), $(patsubst $(dir)/%.o, %.o, $(filter $(dir)/%.o, $(OBJS))))
88 +VTPM_BIN := vtpmd
89 +
90 vpath %.c $(strip $(DIRS))
92 all: $(TPMD)
93 @@ -32,10 +35,8 @@ TPMD_GROUP ?= tss
94 INSTALL ?= install
96 install: $(TPMD)
97 - $(INSTALL) -m 755 -o $(TPMD_USER) -g $(TPMD_GROUP) -d $(DESTDIR)/var/lib/tpm
98 - $(INSTALL) -m 755 -o $(TPMD_USER) -g $(TPMD_GROUP) -d $(DESTDIR)/var/run/tpm
99 $(INSTALL) -D -d $(DESTDIR)/$(BINDIR)
100 - $(INSTALL) -m 755 $(TPMD) $(DESTDIR)/$(BINDIR)
101 + $(INSTALL) -m 755 $(TPMD) $(DESTDIR)/$(BINDIR)/$(VTPM_BIN)
103 .PHONY: all clean install
105 diff -Naurp tpm_emulator-0.5.1/tpmd/tpmd.c tpm5-test/tpmd/tpmd.c
106 --- tpm_emulator-0.5.1/tpmd/tpmd.c 2008-02-14 03:22:48.000000000 -0500
107 +++ tpm5-test/tpmd/tpmd.c 2009-07-16 11:19:05.000000000 -0400
108 @@ -32,6 +32,9 @@
109 #include <grp.h>
110 #include "tpm_emulator_config.h"
111 #include "tpm/tpm_emulator.h"
112 +#include "tpm/tpm_structures.h"
113 +#include "tpm/tpm_marshalling.h"
114 +#include "vtpm_manager.h"
116 #define TPM_DAEMON_NAME "tpmd"
117 #define TPM_CMD_BUF_SIZE 4096
118 @@ -39,6 +42,24 @@
119 #define TPM_RANDOM_DEVICE "/dev/urandom"
120 #undef TPM_MKDIRS
122 +#ifdef VTPM_MULTI_VM
123 + #define DEV_BE "/dev/vtpm"
124 + #define DEV_FE "/dev/tpm"
125 +#else
126 + #define PVM_RX_FIFO_D "/var/vtpm/fifos/tpm_cmd_to_%d.fifo"
127 + #define PVM_TX_FIFO "/var/vtpm/fifos/tpm_rsp_from_all.fifo"
128 + #define HVM_RX_FIFO_D "/var/vtpm/socks/%d.socket"
129 +
130 + #define VTPM_RX_FIFO_D "/var/vtpm/fifos/vtpm_rsp_to_%d.fifo"
131 + #define VTPM_TX_FIFO "/var/vtpm/fifos/vtpm_cmd_from_all.fifo"
132 +
133 + static char *vtpm_rx_name=NULL;
134 +#endif
135 +
136 + static int vtpm_tx_fh=-1, vtpm_rx_fh=-1;
137 +
138 +#define BUFFER_SIZE 2048
139 +
140 static volatile int stopflag = 0;
141 static int is_daemon = 0;
142 static int opt_debug = 0;
143 @@ -49,6 +70,8 @@ static const char *opt_storage_file = "/
144 static uid_t opt_uid = 0;
145 static gid_t opt_gid = 0;
146 static int tpm_startup = 2;
147 +static int vtpm_type = VTPM_TYPE_PVM;
148 +int dmi_id = 0;
149 static int rand_fh;
151 void tpm_log(int priority, const char *fmt, ...)
152 @@ -90,56 +113,241 @@ uint64_t tpm_get_ticks(void)
154 int tpm_write_to_file(uint8_t *data, size_t data_length)
155 {
156 - int fh;
157 - ssize_t res;
158 - fh = open(opt_storage_file, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
159 - if (fh < 0) return -1;
160 - while (data_length > 0) {
161 - res = write(fh, data, data_length);
162 - if (res < 0) {
163 - close(fh);
164 - return -1;
165 - }
166 - data_length -= res;
167 - data += res;
168 + int res, out_data_size, in_header_size;
169 + BYTE *ptr, *out_data, *in_header;
170 + UINT32 result, len, in_rsp_size;
171 + UINT16 tag = VTPM_TAG_REQ;
172 +
173 + printf("Saving NVM\n");
174 + if (vtpm_tx_fh < 0) {
175 +#ifdef VTPM_MUTLI_VM
176 + vtpm_tx_fh = open(DEV_FE, O_RDWR);
177 +#else
178 + vtpm_tx_fh = open(VTPM_TX_FIFO, O_WRONLY);
179 +#endif
180 + }
181 +
182 + if (vtpm_tx_fh < 0) {
183 + return -1;
184 + }
185 +
186 + // Send request to VTPM Manager to encrypt data
187 +#ifdef VTPM_MUTLI_VM
188 + out_data_size = len = VTPM_COMMAND_HEADER_SIZE_CLT + data_length;
189 +#else
190 + out_data_size = len = VTPM_COMMAND_HEADER_SIZE_SRV + data_length;
191 +#endif
192 +
193 + out_data = ptr = (BYTE *) malloc(len);
194 +
195 + if (ptr == NULL
196 +#ifndef VTPM_MUTLI_VM
197 + || tpm_marshal_UINT32(&ptr, &len, dmi_id)
198 +#endif
199 + || tpm_marshal_UINT16(&ptr, &len, tag)
200 +#ifdef VTPM_MUTLI_VM
201 + || tpm_marshal_UINT32(&ptr, &len, out_data_size)
202 +#else
203 + || tpm_marshal_UINT32(&ptr, &len, out_data_size - sizeof(uint32_t))
204 +#endif
205 + || tpm_marshal_UINT32(&ptr, &len, VTPM_ORD_SAVENVM)
206 + || tpm_marshal_BYTE_ARRAY(&ptr, &len, data, data_length)) {
207 + free(out_data);
208 + return -1;
209 + }
210 +
211 + printf("\tSending SaveNVM Command.\n");
212 + res = write(vtpm_tx_fh, out_data, out_data_size);
213 + free(out_data);
214 + if (res != out_data_size) return -1;
215 +
216 + if (vtpm_rx_fh < 0) {
217 +#ifdef VTPM_MUTLI_VM
218 + vtpm_rx_fh = vtpm_tx_fh
219 +#else
220 + if (vtpm_rx_name == NULL) {
221 + vtpm_rx_name = malloc(10 + strlen(VTPM_RX_FIFO_D));
222 + sprintf(vtpm_rx_name, VTPM_RX_FIFO_D, (uint32_t) dmi_id);
223 }
224 - close(fh);
225 - return 0;
226 + vtpm_rx_fh = open(vtpm_rx_name, O_RDONLY);
227 +#endif
228 + }
229 +
230 + if (vtpm_rx_fh < 0) {
231 + return -1;
232 + }
233 +
234 + // Read Header of response so we can get the size & status
235 +#ifdef VTPM_MUTLI_VM
236 + in_header_size = len = VTPM_COMMAND_HEADER_SIZE_CLT;
237 +#else
238 + in_header_size = len = VTPM_COMMAND_HEADER_SIZE_SRV;
239 +#endif
240 + in_header = ptr = malloc(in_header_size);
241 +
242 + printf("\tReading SaveNVM header.\n");
243 + res = read(vtpm_rx_fh, in_header, in_header_size);
244 +
245 + if ( (res != in_header_size)
246 +#ifndef VTPM_MUTLI_VM
247 + || tpm_unmarshal_UINT32(&ptr, &len, (UINT32*)&dmi_id)
248 +#endif
249 + || tpm_unmarshal_UINT16(&ptr, &len, &tag)
250 + || tpm_unmarshal_UINT32(&ptr, &len, &in_rsp_size)
251 + || tpm_unmarshal_UINT32(&ptr, &len, &result) ) {
252 + free(in_header);
253 + return -1;
254 + }
255 + free(in_header);
256 +
257 + if (result != VTPM_SUCCESS) {
258 + return -1;
259 + }
260 +
261 +#ifdef VTPM_MUTLI_VM
262 + close(vtpm_tx_fh); close(vtpm_rx_fh);
263 +#endif
264 +
265 + printf("\tFinishing up SaveNVM\n");
266 + return (0);
267 }
269 int tpm_read_from_file(uint8_t **data, size_t *data_length)
270 {
271 - int fh;
272 - ssize_t res;
273 - size_t total_length;
274 - fh = open(opt_storage_file, O_RDONLY);
275 - if (fh < 0) return -1;
276 - total_length = lseek(fh, 0, SEEK_END);
277 - lseek(fh, 0, SEEK_SET);
278 - *data = tpm_malloc(total_length);
279 - if (*data == NULL) {
280 - close(fh);
281 - return -1;
282 - }
283 - *data_length = 0;
284 - while (total_length > 0) {
285 - res = read(fh, &(*data)[*data_length], total_length);
286 - if (res < 0) {
287 - close(fh);
288 - tpm_free(*data);
289 - return -1;
290 - }
291 - *data_length += res;
292 - total_length -= res;
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 + }
309 +
310 + if (vtpm_tx_fh < 0) {
311 + printf("Error in read_from_file:301\n");
312 + return -1;
313 + }
314 +
315 + // Send request to VTPM Manager to encrypt data
316 +#ifdef VTPM_MUTLI_VM
317 + out_data_size = len = VTPM_COMMAND_HEADER_SIZE_CLT;
318 +#else
319 + out_data_size = len = VTPM_COMMAND_HEADER_SIZE_SRV;
320 +#endif
321 + out_data = ptr = (BYTE *) malloc(len);
322 +
323 + if (ptr == NULL
324 +#ifndef VTPM_MUTLI_VM
325 + || tpm_marshal_UINT32(&ptr, &len, dmi_id)
326 +#endif
327 + || tpm_marshal_UINT16(&ptr, &len, tag)
328 +#ifdef VTPM_MUTLI_VM
329 + || tpm_marshal_UINT32(&ptr, &len, out_data_size)
330 +#else
331 + || tpm_marshal_UINT32(&ptr, &len, out_data_size - sizeof(uint32_t))
332 +#endif
333 + || tpm_marshal_UINT32(&ptr, &len, VTPM_ORD_LOADNVM)) {
334 + free(out_data);
335 + printf("Error in read_from_file:325\n");
336 +
337 + return -1;
338 + }
339 +
340 + printf("\tSending LoadNVM command\n");
341 + res = write(vtpm_tx_fh, out_data, out_data_size);
342 + free(out_data);
343 + if (res != out_data_size)
344 + {
345 + printf("Error in read_from_file:335\n");
346 + return -1;
347 + }
348 +
349 + if (vtpm_rx_fh < 0) {
350 +#ifdef VTPM_MUTLI_VM
351 + vtpm_rx_fh = vtpm_tx_fh;
352 +#else
353 + if (vtpm_rx_name == NULL) {
354 + vtpm_rx_name = malloc(10 + strlen(VTPM_RX_FIFO_D));
355 + sprintf(vtpm_rx_name, VTPM_RX_FIFO_D, (uint32_t) dmi_id);
356 }
357 - close(fh);
358 - return 0;
359 + vtpm_rx_fh = open(vtpm_rx_name, O_RDONLY);
360 +#endif
361 + }
362 +
363 + if (vtpm_rx_fh < 0) {
364 + printf("Error in read_from_file:352\n");
365 + return -1;
366 + }
367 +
368 + // Read Header of response so we can get the size & status
369 +#ifdef VTPM_MUTLI_VM
370 + in_header_size = len = VTPM_COMMAND_HEADER_SIZE_CLT;
371 +#else
372 + in_header_size = len = VTPM_COMMAND_HEADER_SIZE_SRV;
373 +#endif
374 + in_header = ptr = malloc(in_header_size);
375 +
376 + printf("\tReading LoadNVM header\n");
377 + res = read(vtpm_rx_fh, in_header, in_header_size);
378 +
379 + if ( (res != in_header_size)
380 +#ifndef VTPM_MUTLI_VM
381 + || tpm_unmarshal_UINT32(&ptr, &len, (UINT32*)&dmi_id)
382 +#endif
383 + || tpm_unmarshal_UINT16(&ptr, &len, &tag)
384 + || tpm_unmarshal_UINT32(&ptr, &len, &in_rsp_size)
385 + || tpm_unmarshal_UINT32(&ptr, &len, &result) ) {
386 + free(in_header);
387 + printf("Error in read_from_file:375\n");
388 + return -1;
389 + }
390 + free(in_header);
391 +
392 + if (result != VTPM_SUCCESS) {
393 + printf("Error in read_from_file:381\n");
394 + return -1;
395 + }
396 +
397 + // Read Encrypted data from VTPM Manager
398 + *data_length = in_rsp_size - VTPM_COMMAND_HEADER_SIZE_CLT;
399 + *data = (uint8_t *) malloc(*data_length);
400 +
401 + printf("\tReading clear data from LoadNVM.\n");
402 + res = read(vtpm_rx_fh, *data, *data_length);
403 +#ifdef VTPM_MUTLI_VM
404 + close(vtpm_rx_fh);close(vtpm_tx_fh);
405 +#endif
406 +
407 + printf("\tReturing from loading NVM\n");
408 + if (res != (int)*data_length) {
409 + free(*data);
410 + printf("Error in read_from_file:398\n");
411 + return -1;
412 + } else {
413 + return 0;
414 + }
415 +
416 +
417 +
418 }
420 static void print_usage(char *name)
421 {
422 printf("usage: %s [-d] [-f] [-s storage file] [-u unix socket name] "
423 - "[-o user name] [-g group name] [-h] [startup mode]\n", name);
424 + "[-o user name] [-g group name] [-h]"
425 +#ifdef VTPM_MULTI_VM
426 + "clear|save|deactivated\n", name);
427 +#else
428 + "clear|save|deactivated pvm|hvm vtpmid\n", name);
429 +#endif
430 printf(" d : enable debug mode\n");
431 printf(" f : forces the application to run in the foreground\n");
432 printf(" s : storage file to use (default: %s)\n", opt_storage_file);
433 @@ -205,7 +413,13 @@ static void parse_options(int argc, char
434 exit(EXIT_SUCCESS);
435 }
436 }
437 - if (optind < argc) {
438 + /*Make sure we have all required options*/
439 +#ifdef VTPM_MULTI_VM
440 +#define EXTRA_OPTS 0
441 +#else
442 +#define EXTRA_OPTS 2
443 +#endif
444 + if (optind < argc - EXTRA_OPTS ) {
445 debug("startup mode = '%s'", argv[optind]);
446 if (!strcmp(argv[optind], "clear")) {
447 tpm_startup = 1;
448 @@ -219,6 +433,25 @@ static void parse_options(int argc, char
449 print_usage(argv[0]);
450 exit(EXIT_SUCCESS);
451 }
452 +#ifndef VTPM_MULTI_VM
453 + ++optind;
454 + if(!strcmp(argv[optind], "pvm")) {
455 + vtpm_type = VTPM_TYPE_PVM; // Get commands from vTPM Manager through fifo
456 + } else if (!strcmp(argv[optind], "hvm")) {
457 + vtpm_type = VTPM_TYPE_HVM; // Get commands from qemu via socket
458 + } else {
459 + error("Invalid vm mode '%s'; must be 'pvm', "
460 + "or 'hvm' ", argv[optind]);
461 + print_usage(argv[0]);
462 + exit(EXIT_SUCCESS);
463 + }
464 + ++optind;
465 + dmi_id = atoi(argv[optind]);
466 +#endif
467 + } else {
468 + error("Invalid number of arguments");
469 + print_usage(argv[0]);
470 + exit(EXIT_SUCCESS);
471 }
472 }
474 @@ -348,93 +581,180 @@ static int init_socket(const char *name)
476 static void main_loop(void)
477 {
478 - int sock, fh, res;
479 - int32_t in_len;
480 + int32_t in_len, written;
481 uint32_t out_len;
482 - uint8_t in[TPM_CMD_BUF_SIZE], *out;
483 + uint8_t in[TPM_CMD_BUF_SIZE], *out, *addressed_out;
484 + int guest_id=-1;
485 + int i;
486 + char *vtpm_rx_file=NULL;
487 + int res;
488 +
489 +#ifndef VTPM_MULTI_VM
490 + int sockfd = -1;
491 struct sockaddr_un addr;
492 - socklen_t addr_len;
493 - fd_set rfds;
494 - struct timeval tv;
495 + struct sockaddr_un client_addr;
496 + unsigned int client_length;
497 +#endif
498 +
499 + int vtpm_tx_fh=-1, vtpm_rx_fh=-1;
500 +
501 +#ifndef VTPM_MULTI_VM
502 + if (vtpm_type == VTPM_TYPE_PVM) {
503 + vtpm_rx_file = malloc(10 + strlen(PVM_RX_FIFO_D));
504 + sprintf(vtpm_rx_file, PVM_RX_FIFO_D, (uint32_t) dmi_id);
505 + } else {
506 + vtpm_rx_file = malloc(10 + strlen(HVM_RX_FIFO_D));
507 + sprintf(vtpm_rx_file, HVM_RX_FIFO_D, (uint32_t) dmi_id);
508 +
509 + if ( (sockfd = socket(PF_UNIX,SOCK_STREAM,0)) < 0) {
510 + error("Unable to create socket. errno = %d\n", errno);
511 + exit (-1);
512 + }
513 +
514 + memset(&addr, 0, sizeof(addr));
515 + addr.sun_family = AF_UNIX;
516 + strcpy(addr.sun_path,vtpm_rx_file );
517 + unlink(addr.sun_path);
518 + }
519 +#endif
521 info("staring main loop");
522 - /* open UNIX socket */
523 - sock = init_socket(opt_socket_name);
524 - if (sock < 0) exit(EXIT_FAILURE);
525 /* init tpm emulator */
526 - debug("initializing TPM emulator: %d", tpm_startup);
527 +#ifdef VTPM_MULTI_VM
528 + debug("initializing TPM emulator: state=%d", tpm_startup);
529 +#else
530 + debug("initializing TPM emulator: state=%d, type=%d, id=%d", tpm_startup, vtpm_type, dmi_id);
531 +#endif
532 tpm_emulator_init(tpm_startup);
533 /* start command processing */
534 while (!stopflag) {
535 /* wait for incomming connections */
536 debug("waiting for connections...");
537 - FD_ZERO(&rfds);
538 - FD_SET(sock, &rfds);
539 - tv.tv_sec = 10;
540 - tv.tv_usec = 0;
541 - res = select(sock + 1, &rfds, NULL, NULL, &tv);
542 - if (res < 0) {
543 - error("select(sock) failed: %s", strerror(errno));
544 - break;
545 - } else if (res == 0) {
546 - continue;
547 - }
548 - addr_len = sizeof(addr);
549 - fh = accept(sock, (struct sockaddr*)&addr, &addr_len);
550 - if (fh < 0) {
551 - error("accept() failed: %s", strerror(errno));
552 - continue;
553 - }
554 + if (vtpm_rx_fh < 0) {
555 +#ifdef VTPM_MUTLI_VM
556 + vtpm_rx_fh = open(DEV_BE, O_RDWR);
557 +#else
558 + if (vtpm_type == VTPM_TYPE_PVM)
559 + {
560 + vtpm_rx_fh = open(vtpm_rx_file, O_RDONLY);
561 + } else {
562 + if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
563 + error("Unable to bind(). errno = %d\n", errno);
564 + exit (-1);
565 + }
566 +
567 + if (listen(sockfd, 10) <0) {
568 + error("Unable to listen(). errno = %d\n", errno);
569 + exit (-1);
570 + }
571 +
572 + memset(&client_addr, 0, sizeof(client_addr));
573 + client_length = sizeof(client_addr);
574 +
575 + vtpm_rx_fh = vtpm_tx_fh = accept(sockfd, (struct sockaddr *)&client_addr, &client_length);
576 + }
577 +#endif
578 + }
579 +
580 + /*Error Checking*/
581 + if (vtpm_rx_fh < 0) {
582 + error("Failed to open devices to listen to guest.\n");
583 + exit(-1);
584 + }
585 +
586 /* receive and handle commands */
587 in_len = 0;
588 do {
589 debug("waiting for commands...");
590 - FD_ZERO(&rfds);
591 - FD_SET(fh, &rfds);
592 - tv.tv_sec = TPM_COMMAND_TIMEOUT;
593 - tv.tv_usec = 0;
594 - res = select(fh + 1, &rfds, NULL, NULL, &tv);
595 - if (res < 0) {
596 - error("select(fh) failed: %s", strerror(errno));
597 - close(fh);
598 - break;
599 - } else if (res == 0) {
600 -#ifdef TPMD_DISCONNECT_IDLE_CLIENTS
601 - info("connection closed due to inactivity");
602 - close(fh);
603 - break;
604 -#else
605 - continue;
606 -#endif
607 - }
608 - in_len = read(fh, in, sizeof(in));
609 - if (in_len > 0) {
610 +
611 + in_len = read(vtpm_rx_fh, in, sizeof(in));
612 + /*Magic size of minimum TPM command is 6*/
613 + //FIXME Magic size check may not be required anymore
614 + if (in_len < 6) {
615 + info("Recv incomplete command of %d bytes.", in_len);
616 + if (in_len <= 0) {
617 + close(vtpm_rx_fh);
618 + vtpm_rx_fh = -1;
619 + continue;
620 + }
621 + } else {
622 + /*Debug Printouts*/
623 debug("received %d bytes", in_len);
624 + debug_nostop("Recv[%d]: 0x", in_len);
625 + for (i=0; i< in_len; i++)
626 + debug_more("%x ", in[i]);
627 + debug_more("\n");
628 + /*Multiple Guest check*/
629 + if (guest_id == -1) {
630 + guest_id = *((int32_t *) in);
631 + } else {
632 + if (guest_id != *((int32_t *) in) ) {
633 + error("WARNING: More than one guest attached\n");
634 + }
635 + }
636 +
637 + /*Open tx handle now*/
638 + if (vtpm_tx_fh < 0) {
639 +#ifdef VTPM_MUTLI_VM
640 + vtpm_tx_fh = open(DEV_BE, O_RDWR);
641 + vtpm_rx_fh = vtpm_tx_fh;
642 +#else
643 + if (vtpm_type == VTPM_TYPE_PVM) {
644 + vtpm_tx_fh = open(PVM_TX_FIFO, O_WRONLY);
645 + } // No need to open the other direction for HVM
646 +#endif
647 + }
648 + if (vtpm_tx_fh < 0) {
649 + error("Failed to open devices to respond to guest.\n");
650 + exit(-1);
651 + }
652 +
653 + /*Handle the TPM command now*/
654 out = NULL;
655 - res = tpm_handle_command(in, in_len, &out, &out_len);
656 + res = tpm_handle_command(in + sizeof(uint32_t), in_len - sizeof(uint32_t), &out, &out_len);
657 if (res < 0) {
658 error("tpm_handle_command() failed");
659 } else {
660 debug("sending %d bytes", out_len);
661 + //FIXME this prepending may or may not be needed
662 + /*Prepend the first 4 bytes of the in buffer.. why?*/
663 + addressed_out = (uint8_t *) tpm_malloc(sizeof(uint32_t) + out_len);
664 + *(uint32_t *) addressed_out = *(uint32_t *) in;
665 + memcpy(addressed_out + sizeof(uint32_t), out, out_len);
666 + out_len += sizeof(uint32_t);
667 + /*End Prepend*/
668 +
669 + /*Perform write operation now*/
670 while (out_len > 0) {
671 - res = write(fh, out, out_len);
672 + res = write(vtpm_tx_fh, addressed_out, out_len);
673 +
674 if (res < 0) {
675 error("write(%d) failed: %s", out_len, strerror(errno));
676 break;
677 - }
678 + } else {
679 + debug_nostop("Sent[%Zu]: ", out_len);
680 + for (i=0; (unsigned int)i< out_len; i++)
681 + debug_more("%x ", addressed_out[i]);
682 + debug_more("\n");
683 + }
684 out_len -= res;
685 }
686 tpm_free(out);
687 + tpm_free(addressed_out);
688 }
689 }
690 } while (in_len > 0);
691 - close(fh);
692 + //close(fh);
693 }
694 +
695 /* shutdown tpm emulator */
696 tpm_emulator_shutdown();
697 - /* close socket */
698 - close(sock);
699 - unlink(opt_socket_name);
700 + /* Close handles */
701 + close(vtpm_tx_fh);
702 +#ifndef VTPM_MULTI_VM
703 + close(vtpm_rx_fh);
704 + free(vtpm_rx_file);
705 +#endif
706 info("main loop stopped");
707 }
709 @@ -450,12 +770,13 @@ int main(int argc, char **argv)
710 /* open random device */
711 init_random();
712 /* init signal handlers */
713 - init_signal_handler();
714 + //init_signal_handler();
715 /* unless requested otherwiese, fork and daemonize process */
716 - if (!opt_foreground) daemonize();
717 + //if (!opt_foreground) daemonize();
718 /* start main processing loop */
719 main_loop();
720 info("stopping TPM Emulator daemon");
721 closelog();
722 return 0;
723 }
724 +
725 diff -Naurp tpm_emulator-0.5.1/tpmd/tpm_emulator_config.h tpm5-test/tpmd/tpm_emulator_config.h
726 --- tpm_emulator-0.5.1/tpmd/tpm_emulator_config.h 2008-02-14 03:22:48.000000000 -0500
727 +++ tpm5-test/tpmd/tpm_emulator_config.h 2009-07-16 11:25:26.000000000 -0400
728 @@ -29,23 +29,28 @@
730 /* TPM emulator configuration */
732 -#undef TPM_STRONG_PERSISTENCE
733 -#undef TPM_GENERATE_EK
734 +#define TPM_STRONG_PERSISTENCE
735 +#define TPM_GENERATE_EK
736 #undef TPM_GENERATE_SEED_DAA
737 #undef TPM_MEMORY_ALIGNMENT_MANDATORY
739 +extern int dmi_id;
740 +
741 /* log macros */
743 void tpm_log(int priority, const char *fmt, ...);
745 -#define debug(fmt, ...) tpm_log(LOG_DEBUG, "%s:%d: Debug: " fmt "\n", \
746 - __FILE__, __LINE__, ## __VA_ARGS__)
747 -#define info(fmt, ...) tpm_log(LOG_INFO, "%s:%d: Info: " fmt "\n", \
748 - __FILE__, __LINE__, ## __VA_ARGS__)
749 -#define error(fmt, ...) tpm_log(LOG_ERR, "%s:%d: Error: " fmt "\n", \
750 - __FILE__, __LINE__, ## __VA_ARGS__)
751 -#define alert(fmt, ...) tpm_log(LOG_ALERT, "%s:%d: Alert: " fmt "\n", \
752 - __FILE__, __LINE__, ## __VA_ARGS__)
753 +#define debug(fmt, ...) tpm_log(LOG_DEBUG, "VTPMD[%d]: %s:%d: Debug: " fmt "\n", \
754 + dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
755 +#define info(fmt, ...) tpm_log(LOG_INFO, "VTPMD[%d]: %s:%d: Info: " fmt "\n", \
756 + dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
757 +#define error(fmt, ...) tpm_log(LOG_ERR, "VTPMD[%d]: %s:%d: Error: " fmt "\n", \
758 + dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
759 +#define alert(fmt, ...) tpm_log(LOG_ALERT, "VTPMD[%d]: %s:%d: Alert: " fmt "\n", \
760 + dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
761 +#define debug_nostop(fmt, ...) tpm_log(LOG_DEBUG, "VTPMD[%d]: %s:%d: Debug: " fmt, \
762 + dmi_id, __FILE__, __LINE__, ## __VA_ARGS__)
763 +#define debug_more(fmt, ...) tpm_log(LOG_DEBUG, fmt, ## __VA_ARGS__)
765 /* min/max macros that also do strict type-checking */