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