ia64/xen-unstable

changeset 6575:0db6e392c380

Remainder of Intel/IBM joint TPM Virtualization implementation for Xen.

Signed-off-by: Vinnie Scarlata <vincent.r.scarlata@intel.com>
Signed-off-by: Joseph Cihula <joseph.cihula@intel.com>
Signed-off-by: Steven Hand <steven@xensource.com>
author shand@ubuntu.eng.hq.xensource.com
date Tue Aug 30 11:53:53 2005 -0800 (2005-08-30)
parents ff536c11c178
children dd108e5ad24d
files Makefile tools/Makefile tools/vtpm/Makefile tools/vtpm/README tools/vtpm/Rules.mk tools/vtpm/tpm_emulator.patch tools/vtpm/vtpm.patch
line diff
     1.1 --- a/Makefile	Tue Aug 30 11:48:08 2005 -0800
     1.2 +++ b/Makefile	Tue Aug 30 11:53:53 2005 -0800
     1.3 @@ -35,11 +35,11 @@ ifeq ($(XEN_TARGET_X86_PAE),y)
     1.4  export pae=y
     1.5  endif
     1.6  
     1.7 -.PHONY:	all dist install xen tools kernels docs world clean mkpatches mrproper
     1.8 +.PHONY:	all dist install xen kernels tools docs world clean mkpatches mrproper
     1.9  .PHONY:	kbuild kdelete kclean
    1.10  
    1.11  # build and install everything into the standard system directories
    1.12 -install: install-xen install-tools install-kernels install-docs
    1.13 +install: install-xen install-kernels install-tools install-docs
    1.14  
    1.15  build: kernels
    1.16  	$(MAKE) -C xen build
    1.17 @@ -47,7 +47,7 @@ build: kernels
    1.18  	$(MAKE) -C docs build
    1.19  
    1.20  # build and install everything into local dist directory
    1.21 -dist: xen tools kernels docs
    1.22 +dist: xen kernels tools docs
    1.23  	$(INSTALL_DIR) $(DISTDIR)/check
    1.24  	$(INSTALL_DATA) ./COPYING $(DISTDIR)
    1.25  	$(INSTALL_DATA) ./README $(DISTDIR)
     2.1 --- a/tools/Makefile	Tue Aug 30 11:48:08 2005 -0800
     2.2 +++ b/tools/Makefile	Tue Aug 30 11:53:53 2005 -0800
     2.3 @@ -12,6 +12,8 @@ SUBDIRS += xcutils
     2.4  SUBDIRS += firmware
     2.5  SUBDIRS += security
     2.6  SUBDIRS += console
     2.7 +SUBDIRS += vtpm_manager
     2.8 +SUBDIRS += vtpm
     2.9  SUBDIRS += xenstat
    2.10  
    2.11  # These don't cross-compile
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tools/vtpm/Makefile	Tue Aug 30 11:53:53 2005 -0800
     3.3 @@ -0,0 +1,63 @@
     3.4 +XEN_ROOT = ../..
     3.5 +
     3.6 +# Base definitions and rules
     3.7 +include $(XEN_ROOT)/tools/vtpm/Rules.mk
     3.8 +
     3.9 +# Dir name for emulator (as dom0 tpm driver)
    3.10 +TPM_EMULATOR_DIR = tpm_emulator-0.2
    3.11 +# Dir name for vtpm instance
    3.12 +VTPM_DIR = vtpm
    3.13 +
    3.14 +# Emulator tarball name
    3.15 +TPM_EMULATOR_TARFILE = tpm_emulator-0.2b.tar.gz
    3.16 +
    3.17 +all: build
    3.18 +
    3.19 +build: $(TPM_EMULATOR_TARFILE) extract patch build_sub
    3.20 +
    3.21 +install: build
    3.22 +	$(MAKE) -C $(TPM_EMULATOR_DIR) $@
    3.23 +	$(MAKE) -C $(VTPM_DIR) $@
    3.24 +
    3.25 +clean:
    3.26 +	if [ -d $(TPM_EMULATOR_DIR) ]; \
    3.27 +		then $(MAKE) -C $(TPM_EMULATOR_DIR) clean; \
    3.28 +	fi
    3.29 +	if [ -d $(VTPM_DIR) ]; \
    3.30 +		then $(MAKE) -C $(VTPM_DIR) clean; \
    3.31 +	fi
    3.32 +	rm -rf $(TPM_EMULATOR_DIR)
    3.33 +	rm -rf $(VTPM_DIR)
    3.34 +
    3.35 +mrproper: clean
    3.36 +	rm -f $(TPM_EMULATOR_TARFILE)
    3.37 +
    3.38 +# Download Swiss emulator
    3.39 +$(TPM_EMULATOR_TARFILE):
    3.40 +	wget http://download.berlios.de/tpm-emulator/$(TPM_EMULATOR_TARFILE)
    3.41 +
    3.42 +# Create vtpm and TPM emulator dirs
    3.43 +extract: $(TPM_EMULATOR_DIR)/README $(VTPM_DIR)/README
    3.44 +
    3.45 +$(TPM_EMULATOR_DIR)/README:
    3.46 +	-rm -rf $(TPM_EMULATOR_DIR)
    3.47 +	tar -xzf $(TPM_EMULATOR_TARFILE)
    3.48 +
    3.49 +$(VTPM_DIR)/README:
    3.50 +	-rm -rf $(VTPM_DIR)
    3.51 +	cp -r --preserve $(TPM_EMULATOR_DIR) $(VTPM_DIR)
    3.52 +
    3.53 +# apply patches for 1) used as dom0 tpm driver 2) used as vtpm device instance
    3.54 +patch: $(TPM_EMULATOR_DIR)/Makefile $(VTPM_DIR)/Makefile
    3.55 +
    3.56 +$(TPM_EMULATOR_DIR)/Makefile: tpm_emulator.patch
    3.57 +	-cd $(TPM_EMULATOR_DIR); \
    3.58 +	patch -p1 <../tpm_emulator.patch
    3.59 +
    3.60 +$(VTPM_DIR)/Makefile: vtpm.patch
    3.61 +	-cd $(VTPM_DIR); \
    3.62 +	patch -p1 <../vtpm.patch
    3.63 +
    3.64 +build_sub:
    3.65 +	$(MAKE) -C $(TPM_EMULATOR_DIR)
    3.66 +	$(MAKE) -C $(VTPM_DIR)
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tools/vtpm/README	Tue Aug 30 11:53:53 2005 -0800
     4.3 @@ -0,0 +1,44 @@
     4.4 +
     4.5 +Directory Structure
     4.6 +===================
     4.7 +tools/vtpm/tpm_emulator-0.2b.tar.gz    -> TPM Emulator downloaded at build time that will
     4.8 +                                          be patched and used for our vtpms
     4.9 +tools/vtpm/vtpm.patch                  -> patch applied to tpm_emulator to make vtpm
    4.10 +tools/vtpm/vtpm/                       -> (created on build) tpm_emulator moved to ring 3,
    4.11 +                                          listens on a pair of fifos for TPM commands,
    4.12 +                                          persistent state is sent via named fifo to vtpm
    4.13 +                                            manager, which encrypts it and protects it.
    4.14 +tools/vtpm/tpm_emulator.patch          -> To allow for debugging and testing on non-TPM
    4.15 +                                          platforms, this patches the emulator to allow
    4.16 +                                          it to be inserted into the dom0 kernel
    4.17 +tools/vtpm/tpm_emulator-0.2            -> (created on build) directory containing patched emulator
    4.18 +
    4.19 +Compile Flags
    4.20 +===================
    4.21 +VTPM_MULTI_VM                -> Defined (not finished): VTPMs run in their own VMs
    4.22 +                                Not Defined (default): VTPMs are processes
    4.23 +
    4.24 +Requirements
    4.25 +============
    4.26 +- xen-unstable 
    4.27 +- IBM frontend/backend vtpm driver patch
    4.28 +- vtpm_managerd
    4.29 +
    4.30 +vtpmd Flow (for vtpm_manager. vtpmd never run by default)
    4.31 +============================
    4.32 +- Launch the VTPM manager (vtpm_managerd) which which begins listening to the BE with one thread
    4.33 +  and listens to a named fifo that is shared by the vtpms to commuincate with the manager.
    4.34 +- VTPM Manager listens to TPM BE.
    4.35 +- When xend launches a tpm frontend equipped VM it contacts the manager over the vtpm backend. 
    4.36 +- When the manager receives the open message from the BE, it launches a vtpm
    4.37 +- Xend allows the VM to continue booting. 
    4.38 +- When a TPM request is issued to the front end, the front end transmits the TPM request to the backend.
    4.39 +- The manager receives the TPM requests and uses a named fifo to forward the request to the vtpm.
    4.40 +- The fifo listener begins listening for the reply from vtpm for the request.
    4.41 +- Vtpm processes request and replies to manager over shared named fifo.
    4.42 +- If needed, the vtpm may send a request to the vtpm_manager at any time to save it's secrets to disk.
    4.43 +- Manager receives response from vtpm and passes it back to backend for forwarding to guest.
    4.44 +
    4.45 +tpm_emulator flow
    4.46 +==================
    4.47 +Read documentation in tpm_emulator-0.2 directory
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/tools/vtpm/Rules.mk	Tue Aug 30 11:53:53 2005 -0800
     5.3 @@ -0,0 +1,37 @@
     5.4 +# Base definitions and rules (XEN_ROOT must be defined in including Makefile)
     5.5 +include $(XEN_ROOT)/tools/Rules.mk
     5.6 +
     5.7 +#
     5.8 +# Tool definitions
     5.9 +#
    5.10 +
    5.11 +# Installation program and options
    5.12 +INSTALL         = install
    5.13 +INSTALL_PROG    = $(INSTALL) -m0755
    5.14 +INSTALL_DIR     = $(INSTALL) -d -m0755
    5.15 +
    5.16 +# Xen tools installation directory
    5.17 +TOOLS_INSTALL_DIR = $(DESTDIR)/usr/bin
    5.18 +
    5.19 +# General compiler flags
    5.20 +CFLAGS   = -Wall -Werror -g3 -I.
    5.21 +
    5.22 +# For generating dependencies
    5.23 +CFLAGS	+= -Wp,-MD,.$(@F).d
    5.24 +
    5.25 +DEP_FILES	= .*.d
    5.26 +
    5.27 +# Generic project files
    5.28 +HDRS	= $(wildcard *.h)
    5.29 +SRCS	= $(wildcard *.c)
    5.30 +OBJS	= $(patsubst %.c,%.o,$(SRCS))
    5.31 +
    5.32 +# Generic (non-header) dependencies
    5.33 +$(SRCS): Makefile $(XEN_ROOT)/tools/Rules.mk $(XEN_ROOT)/tools/vtpm/Rules.mk
    5.34 +
    5.35 +$(OBJS): $(SRCS)
    5.36 +
    5.37 +-include $(DEP_FILES)
    5.38 +
    5.39 +# Make sure these are just rules
    5.40 +.PHONY : all build install clean
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/tools/vtpm/tpm_emulator.patch	Tue Aug 30 11:53:53 2005 -0800
     6.3 @@ -0,0 +1,149 @@
     6.4 +diff -uprN orig/tpm_emulator-0.2/AUTHORS tpm_emulator-0.2/AUTHORS
     6.5 +--- orig/tpm_emulator-0.2/AUTHORS	2005-08-17 10:58:36.000000000 -0700
     6.6 ++++ tpm_emulator-0.2/AUTHORS	2005-08-17 10:55:52.000000000 -0700
     6.7 +@@ -1 +1,2 @@
     6.8 + Mario Strasser <mast@gmx.net>
     6.9 ++INTEL Corp <>
    6.10 +diff -uprN orig/tpm_emulator-0.2/ChangeLog tpm_emulator-0.2/ChangeLog
    6.11 +--- orig/tpm_emulator-0.2/ChangeLog	2005-08-17 10:58:36.000000000 -0700
    6.12 ++++ tpm_emulator-0.2/ChangeLog	2005-08-17 10:55:52.000000000 -0700
    6.13 +@@ -1,3 +1,7 @@
    6.14 ++2005-08-16: INTEL Corp
    6.15 ++	* Set default permissions to PCRs
    6.16 ++	* Changed device to /dev/tpm0
    6.17 ++
    6.18 + 2005-08-15  Mario Strasser <mast@gmx.net>
    6.19 + 	* all: some typos corrected
    6.20 + 	* tpm_integrity.c: bug in TPM_Extend fixed
    6.21 +diff -uprN orig/tpm_emulator-0.2/Makefile tpm_emulator-0.2/Makefile
    6.22 +--- orig/tpm_emulator-0.2/Makefile	2005-08-17 10:58:36.000000000 -0700
    6.23 ++++ tpm_emulator-0.2/Makefile	2005-08-17 10:55:52.000000000 -0700
    6.24 +@@ -1,15 +1,18 @@
    6.25 + # Software-Based Trusted Platform Module (TPM) Emulator for Linux
    6.26 + # Copyright (C) 2004 Mario Strasser <mast@gmx.net>
    6.27 ++# Copyright (C) 2005 INTEL Corp.
    6.28 + #
    6.29 + # $Id: Makefile 10 2005-04-26 20:59:50Z mast $
    6.30 + 
    6.31 ++XEN_ROOT       := ../../..
    6.32 ++
    6.33 + # kernel settings
    6.34 + KERNEL_RELEASE := $(shell uname -r)
    6.35 +-KERNEL_BUILD   := /lib/modules/$(KERNEL_RELEASE)/build
    6.36 ++KERNEL_BUILD   := $(XEN_ROOT)/linux-2.6.12-xen0
    6.37 + MOD_SUBDIR     := misc
    6.38 + 
    6.39 + # module settings
    6.40 +-MODULE_NAME    := tpm_emulator
    6.41 ++BIN            := tpm_emulator
    6.42 + VERSION_MAJOR  := 0
    6.43 + VERSION_MINOR  := 2
    6.44 + VERSION_BUILD  := $(shell date +"%s")
    6.45 +@@ -27,11 +30,9 @@ DIRS           := . crypto tpm 
    6.46 + SRCS           := $(foreach dir, $(DIRS), $(wildcard $(src)/$(dir)/*.c))
    6.47 + OBJS           := $(patsubst %.c, %.o, $(SRCS))
    6.48 + SRCS           += $(foreach dir, $(DIRS), $(wildcard $(src)/$(dir)/*.h))
    6.49 +-DISTSRC        := ./README ./AUTHORS ./ChangeLog ./Makefile $(SRCS)
    6.50 +-DISTDIR        := tpm_emulator-$(VERSION_MAJOR).$(VERSION_MINOR)
    6.51 + 
    6.52 +-obj-m               := $(MODULE_NAME).o
    6.53 +-$(MODULE_NAME)-objs := $(patsubst $(src)/%.o, %.o, $(OBJS)) crypto/libgmp.a
    6.54 ++obj-m               := $(BIN).o
    6.55 ++$(BIN)-objs := $(patsubst $(src)/%.o, %.o, $(OBJS)) crypto/libgmp.a
    6.56 + 
    6.57 + EXTRA_CFLAGS   += -I$(src) -I$(src)/crypto -I$(src)/tpm 
    6.58 + 
    6.59 +@@ -42,23 +43,16 @@ all:	$(src)/crypto/gmp.h $(src)/crypto/l
    6.60 + 	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) modules
    6.61 + 
    6.62 + install:
    6.63 +-	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) modules_install
    6.64 +-	test -d /var/tpm || mkdir /var/tpm
    6.65 +-	test -c /dev/tpm || mknod /dev/tpm c 10 224
    6.66 +-	chmod 666 /dev/tpm
    6.67 +-	depmod -a
    6.68 ++	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) INSTALL_MOD_PATH=$(DESTDIR) modules_install
    6.69 ++	test -d $(DESTDIR)/var/tpm || mkdir $(DESTDIR)/var/tpm
    6.70 ++	test -c /dev/tpm0 || mknod /dev/tpm0 c 10 224
    6.71 ++	chmod 666 /dev/tpm0
    6.72 + 
    6.73 + clean:
    6.74 + 	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) clean
    6.75 + 	rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a
    6.76 + 
    6.77 +-dist:	$(DISTSRC)
    6.78 +-	rm -rf $(DISTDIR)
    6.79 +-	mkdir $(DISTDIR)
    6.80 +-	cp --parents $(DISTSRC) $(DISTDIR)/
    6.81 +-	rm -f $(DISTDIR)/crypto/gmp.h 
    6.82 +-	tar -chzf $(DISTDIR).tar.gz $(DISTDIR)
    6.83 +-	rm -rf $(DISTDIR)
    6.84 ++mrproper: clean
    6.85 + 
    6.86 + $(src)/crypto/libgmp.a:
    6.87 + 	test -f $(src)/crypto/libgmp.a || ln -s $(GMP_LIB) $(src)/crypto/libgmp.a
    6.88 +diff -uprN orig/tpm_emulator-0.2/README tpm_emulator-0.2/README
    6.89 +--- orig/tpm_emulator-0.2/README	2005-08-17 10:58:36.000000000 -0700
    6.90 ++++ tpm_emulator-0.2/README	2005-08-17 10:55:52.000000000 -0700
    6.91 +@@ -13,7 +13,8 @@ $Id: README 8 2005-01-25 21:11:45Z jmoli
    6.92 + Copyright
    6.93 + --------------------------------------------------------------------------
    6.94 + Copyright (C) 2004 Mario Strasser <mast@gmx.net> and Swiss Federal 
    6.95 +-Institute of Technology (ETH) Zurich.
    6.96 ++                   Institute of Technology (ETH) Zurich.
    6.97 ++Copyright (C) 2005 
    6.98 +               
    6.99 + This program is free software; you can redistribute it and/or modify
   6.100 + it under the terms of the GNU General Public License as published by
   6.101 +diff -uprN orig/tpm_emulator-0.2/linux_module.h tpm_emulator-0.2/linux_module.h
   6.102 +--- orig/tpm_emulator-0.2/linux_module.h	2005-08-17 10:58:36.000000000 -0700
   6.103 ++++ tpm_emulator-0.2/linux_module.h	2005-08-17 10:55:52.000000000 -0700
   6.104 +@@ -1,5 +1,6 @@
   6.105 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   6.106 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   6.107 ++ * Copyright (C) 2005 INTEL Corp.
   6.108 +  *
   6.109 +  * This module is free software; you can redistribute it and/or modify
   6.110 +  * it under the terms of the GNU General Public License as published
   6.111 +@@ -33,7 +34,7 @@
   6.112 + #include "tpm_version.h"
   6.113 + 
   6.114 + #define TPM_DEVICE_MINOR	224
   6.115 +-#define TPM_DEVICE_NAME         "tpm"
   6.116 ++#define TPM_DEVICE_NAME         "tpm0"
   6.117 + #define TPM_MODULE_NAME 	"tpm_emulator"
   6.118 + 
   6.119 + /* debug and log output functions */
   6.120 +diff -uprN orig/tpm_emulator-0.2/tpm/tpm_data.c tpm_emulator-0.2/tpm/tpm_data.c
   6.121 +--- orig/tpm_emulator-0.2/tpm/tpm_data.c	2005-08-17 10:58:36.000000000 -0700
   6.122 ++++ tpm_emulator-0.2/tpm/tpm_data.c	2005-08-17 10:55:52.000000000 -0700
   6.123 +@@ -1,6 +1,7 @@
   6.124 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   6.125 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   6.126 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
   6.127 ++ * Copyright (C) 2005 INTEL Corp
   6.128 +  *
   6.129 +  * This module is free software; you can redistribute it and/or modify
   6.130 +  * it under the terms of the GNU General Public License as published
   6.131 +@@ -85,6 +86,11 @@ void tpm_init_data(void)
   6.132 +   tpmData.permanent.data.version.revMinor = VERSION_MINOR;
   6.133 +   /* setup PCR attributes */
   6.134 +   for (i = 0; i < TPM_NUM_PCR; i++) {
   6.135 ++    int j;
   6.136 ++    for (j=0; j < TPM_NUM_LOCALITY; j++) {
   6.137 ++      tpmData.permanent.data.pcrAttrib[i].pcrExtendLocal[j] = TRUE;
   6.138 ++    }
   6.139 ++
   6.140 +     tpmData.permanent.data.pcrAttrib[i].pcrReset = TRUE;
   6.141 +   }
   6.142 +   /* set tick type */
   6.143 +diff -uprN orig/tpm_emulator-0.2/tpm_version.h tpm_emulator-0.2/tpm_version.h
   6.144 +--- orig/tpm_emulator-0.2/tpm_version.h	2005-08-17 10:58:36.000000000 -0700
   6.145 ++++ tpm_emulator-0.2/tpm_version.h	2005-08-17 10:55:53.000000000 -0700
   6.146 +@@ -2,5 +2,5 @@
   6.147 + #define _TPM_VERSION_H_
   6.148 + #define VERSION_MAJOR 0
   6.149 + #define VERSION_MINOR 2
   6.150 +-#define VERSION_BUILD 1123950310
   6.151 ++#define VERSION_BUILD 1124301353
   6.152 + #endif /* _TPM_VERSION_H_ */
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/tools/vtpm/vtpm.patch	Tue Aug 30 11:53:53 2005 -0800
     7.3 @@ -0,0 +1,1645 @@
     7.4 +diff -uprN orig/tpm_emulator-0.2/AUTHORS vtpm/AUTHORS
     7.5 +--- orig/tpm_emulator-0.2/AUTHORS	2005-08-17 10:58:36.000000000 -0700
     7.6 ++++ vtpm/AUTHORS	2005-08-17 10:55:52.000000000 -0700
     7.7 +@@ -1 +1,2 @@
     7.8 + Mario Strasser <mast@gmx.net>
     7.9 ++INTEL Corp <>
    7.10 +diff -uprN orig/tpm_emulator-0.2/ChangeLog vtpm/ChangeLog
    7.11 +--- orig/tpm_emulator-0.2/ChangeLog	2005-08-17 10:58:36.000000000 -0700
    7.12 ++++ vtpm/ChangeLog	2005-08-17 10:55:52.000000000 -0700
    7.13 +@@ -1,3 +1,7 @@
    7.14 ++2005-08-16 Intel Corp
    7.15 ++	Moved module out of kernel to run as a ring 3 app
    7.16 ++	Modified save_to_file and load_from_file to call a xen backend driver to call a VTPM manager
    7.17 ++
    7.18 + 2005-08-15  Mario Strasser <mast@gmx.net>
    7.19 + 	* all: some typos corrected
    7.20 + 	* tpm_integrity.c: bug in TPM_Extend fixed
    7.21 +diff -uprN orig/tpm_emulator-0.2/Makefile vtpm/Makefile
    7.22 +--- orig/tpm_emulator-0.2/Makefile	2005-08-17 10:58:36.000000000 -0700
    7.23 ++++ vtpm/Makefile	2005-08-17 10:55:52.000000000 -0700
    7.24 +@@ -1,21 +1,29 @@
    7.25 + # Software-Based Trusted Platform Module (TPM) Emulator for Linux
    7.26 + # Copyright (C) 2004 Mario Strasser <mast@gmx.net>
    7.27 ++# Copyright (C) 2005 INTEL Corp.
    7.28 + #
    7.29 + # $Id: Makefile 10 2005-04-26 20:59:50Z mast $
    7.30 + 
    7.31 +-# kernel settings
    7.32 +-KERNEL_RELEASE := $(shell uname -r)
    7.33 +-KERNEL_BUILD   := /lib/modules/$(KERNEL_RELEASE)/build
    7.34 +-MOD_SUBDIR     := misc
    7.35 +-
    7.36 + # module settings
    7.37 +-MODULE_NAME    := tpm_emulator
    7.38 ++BIN            := vtpmd
    7.39 + VERSION_MAJOR  := 0
    7.40 + VERSION_MINOR  := 2
    7.41 + VERSION_BUILD  := $(shell date +"%s")
    7.42 + 
    7.43 +-# enable/disable DEBUG messages
    7.44 +-EXTRA_CFLAGS   += -DDEBUG -g  
    7.45 ++# Installation program and options
    7.46 ++INSTALL         = install
    7.47 ++INSTALL_PROG    = $(INSTALL) -m0755
    7.48 ++INSTALL_DIR     = $(INSTALL) -d -m0755
    7.49 ++
    7.50 ++# Xen tools installation directory
    7.51 ++TOOLS_INSTALL_DIR = $(DESTDIR)/usr/bin
    7.52 ++
    7.53 ++CC      := gcc
    7.54 ++CFLAGS  += -g -Wall $(INCLUDE) -DDEBUG
    7.55 ++CFLAGS  += -I. -Itpm
    7.56 ++
    7.57 ++# Is the simulator running in it's own vm?
    7.58 ++#CFLAGS += -DVTPM_MULTI_VM
    7.59 + 
    7.60 + # GNU MP configuration
    7.61 + GMP_LIB        := /usr/lib/libgmp.a
    7.62 +@@ -27,38 +35,31 @@ DIRS           := . crypto tpm 
    7.63 + SRCS           := $(foreach dir, $(DIRS), $(wildcard $(src)/$(dir)/*.c))
    7.64 + OBJS           := $(patsubst %.c, %.o, $(SRCS))
    7.65 + SRCS           += $(foreach dir, $(DIRS), $(wildcard $(src)/$(dir)/*.h))
    7.66 +-DISTSRC        := ./README ./AUTHORS ./ChangeLog ./Makefile $(SRCS)
    7.67 +-DISTDIR        := tpm_emulator-$(VERSION_MAJOR).$(VERSION_MINOR)
    7.68 + 
    7.69 +-obj-m               := $(MODULE_NAME).o
    7.70 +-$(MODULE_NAME)-objs := $(patsubst $(src)/%.o, %.o, $(OBJS)) crypto/libgmp.a
    7.71 ++obj-m               := $(BIN)
    7.72 ++$(BIN)-objs := $(patsubst $(src)/%.o, %.o, $(OBJS)) crypto/libgmp.a
    7.73 + 
    7.74 + EXTRA_CFLAGS   += -I$(src) -I$(src)/crypto -I$(src)/tpm 
    7.75 + 
    7.76 + # do not print "Entering directory ..."
    7.77 + MAKEFLAGS      += --no-print-directory
    7.78 + 
    7.79 +-all:	$(src)/crypto/gmp.h $(src)/crypto/libgmp.a version
    7.80 +-	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) modules
    7.81 ++all: $(BIN)
    7.82 ++
    7.83 ++$(BIN):	$(src)/crypto/gmp.h $(src)/crypto/libgmp.a version $(SRCS) $(OBJS)
    7.84 ++	$(CC) $(CFLAGS) $(OBJS) $(src)/crypto/libgmp.a -o $(BIN)
    7.85 ++
    7.86 ++%.o: %.c
    7.87 ++	$(CC) $(CFLAGS) -c $< -o $@
    7.88 + 
    7.89 + install:
    7.90 +-	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) modules_install
    7.91 +-	test -d /var/tpm || mkdir /var/tpm
    7.92 +-	test -c /dev/tpm || mknod /dev/tpm c 10 224
    7.93 +-	chmod 666 /dev/tpm
    7.94 +-	depmod -a
    7.95 ++	$(INSTALL_PROG) $(BIN) $(TOOLS_INSTALL_DIR)
    7.96 + 
    7.97 + clean:
    7.98 +-	@$(MAKE) -C $(KERNEL_BUILD) M=$(CURDIR) clean
    7.99 +-	rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a
   7.100 ++	rm -f $(src)/crypto/gmp.h $(src)/crypto/libgmp.a $(OBJS)
   7.101 + 
   7.102 +-dist:	$(DISTSRC)
   7.103 +-	rm -rf $(DISTDIR)
   7.104 +-	mkdir $(DISTDIR)
   7.105 +-	cp --parents $(DISTSRC) $(DISTDIR)/
   7.106 +-	rm -f $(DISTDIR)/crypto/gmp.h 
   7.107 +-	tar -chzf $(DISTDIR).tar.gz $(DISTDIR)
   7.108 +-	rm -rf $(DISTDIR)
   7.109 ++mrproper: clean
   7.110 ++	rm -f $(BIN)
   7.111 + 
   7.112 + $(src)/crypto/libgmp.a:
   7.113 + 	test -f $(src)/crypto/libgmp.a || ln -s $(GMP_LIB) $(src)/crypto/libgmp.a
   7.114 +diff -uprN orig/tpm_emulator-0.2/README vtpm/README
   7.115 +--- orig/tpm_emulator-0.2/README	2005-08-17 10:58:36.000000000 -0700
   7.116 ++++ vtpm/README	2005-08-17 10:55:52.000000000 -0700
   7.117 +@@ -13,7 +13,8 @@ $Id: README 8 2005-01-25 21:11:45Z jmoli
   7.118 + Copyright
   7.119 + --------------------------------------------------------------------------
   7.120 + Copyright (C) 2004 Mario Strasser <mast@gmx.net> and Swiss Federal 
   7.121 +-Institute of Technology (ETH) Zurich.
   7.122 ++                   Institute of Technology (ETH) Zurich.
   7.123 ++Copyright (C) 2005 INTEL Corp 
   7.124 +               
   7.125 + This program is free software; you can redistribute it and/or modify
   7.126 + it under the terms of the GNU General Public License as published by
   7.127 +diff -uprN orig/tpm_emulator-0.2/crypto/gmp_kernel_wrapper.c vtpm/crypto/gmp_kernel_wrapper.c
   7.128 +--- orig/tpm_emulator-0.2/crypto/gmp_kernel_wrapper.c	2005-08-17 10:58:36.000000000 -0700
   7.129 ++++ vtpm/crypto/gmp_kernel_wrapper.c	2005-08-17 10:55:52.000000000 -0700
   7.130 +@@ -1,5 +1,6 @@
   7.131 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   7.132 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   7.133 ++ * Copyright (C) 2005 INTEL Corp
   7.134 +  *
   7.135 +  * This module is free software; you can redistribute it and/or modify
   7.136 +  * it under the terms of the GNU General Public License as published
   7.137 +@@ -24,15 +25,10 @@ int __gmp_junk;
   7.138 + void __attribute__ ((regparm(0))) __gmp_assert_fail(const char *filename, 
   7.139 +   int linenum, const char *expr) 
   7.140 + {
   7.141 +-  panic(KERN_CRIT TPM_MODULE_NAME "%s:%d: GNU MP assertion failed: %s\n", 
   7.142 ++  error("%s:%d: GNU MP assertion failed: %s\n", 
   7.143 +     filename, linenum, expr);
   7.144 + }
   7.145 + 
   7.146 +-void __attribute__ ((regparm(0))) abort(void)
   7.147 +-{
   7.148 +-  panic(KERN_CRIT TPM_MODULE_NAME "GNU MP abort() was called\n");
   7.149 +-}
   7.150 +-
   7.151 + /* overwrite GNU MP random functions (used by mpz/millerrabin.c) */ 
   7.152 + 
   7.153 + void __attribute__ ((regparm(0))) gmp_randinit(gmp_randstate_t rstate, 
   7.154 +@@ -77,20 +73,19 @@ void __attribute__ ((regparm(0))) mpz_ur
   7.155 + 
   7.156 + void __attribute__ ((regparm(0))) *kernel_allocate(size_t size)
   7.157 + {
   7.158 +-  void *ret  = (void*)kmalloc(size, GFP_KERNEL);
   7.159 +-  if (!ret) panic(KERN_CRIT TPM_MODULE_NAME 
   7.160 +-    "GMP: cannot allocate memory (size=%u)\n", size);
   7.161 ++  void *ret  = (void*)malloc(size);
   7.162 ++  if (!ret) error("GMP: cannot allocate memory (size=%u)\n", size);
   7.163 +   return ret;
   7.164 + }
   7.165 + 
   7.166 + void __attribute__ ((regparm(0))) *kernel_reallocate(void *oldptr, 
   7.167 +   size_t old_size, size_t new_size)
   7.168 + {
   7.169 +-  void *ret = (void*)kmalloc(new_size, GFP_KERNEL);
   7.170 +-  if (!ret) panic(KERN_CRIT TPM_MODULE_NAME "GMP: Cannot reallocate memory "
   7.171 ++  void *ret = (void*)malloc(new_size);
   7.172 ++  if (!ret) error("GMP: Cannot reallocate memory "
   7.173 +     "(old_size=%u new_size=%u)\n", old_size, new_size);
   7.174 +   memcpy(ret, oldptr, old_size);
   7.175 +-  kfree(oldptr);
   7.176 ++  free(oldptr);
   7.177 +   return ret;
   7.178 + }
   7.179 + 
   7.180 +@@ -99,7 +94,7 @@ void __attribute__ ((regparm(0))) kernel
   7.181 +   /* overwrite used memory */
   7.182 +   if (blk_ptr != NULL) { 
   7.183 +     memset(blk_ptr, 0, blk_size);
   7.184 +-    kfree(blk_ptr);
   7.185 ++    free(blk_ptr);
   7.186 +   }
   7.187 + }
   7.188 + 
   7.189 +diff -uprN orig/tpm_emulator-0.2/crypto/rsa.c vtpm/crypto/rsa.c
   7.190 +--- orig/tpm_emulator-0.2/crypto/rsa.c	2005-08-17 10:58:36.000000000 -0700
   7.191 ++++ vtpm/crypto/rsa.c	2005-08-17 10:55:52.000000000 -0700
   7.192 +@@ -1,5 +1,6 @@
   7.193 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   7.194 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   7.195 ++ * Copyright (C) 2005 INTEL Corp
   7.196 +  *
   7.197 +  * This module is free software; you can redistribute it and/or modify
   7.198 +  * it under the terms of the GNU General Public License as published
   7.199 +@@ -363,7 +364,7 @@ static int encode_message(int type, uint
   7.200 +       msg[0] = 0x00;
   7.201 +       get_random_bytes(&msg[1], SHA1_DIGEST_LENGTH);
   7.202 +       sha1_init(&ctx);
   7.203 +-      sha1_update(&ctx, "TCPA", 4);
   7.204 ++      sha1_update(&ctx, (uint8_t *) "TCPA", 4);
   7.205 +       sha1_final(&ctx, &msg[1 + SHA1_DIGEST_LENGTH]);
   7.206 +       memset(&msg[1 + 2 * SHA1_DIGEST_LENGTH], 0x00, 
   7.207 +         msg_len - data_len - 2 * SHA1_DIGEST_LENGTH - 2);
   7.208 +@@ -411,7 +412,7 @@ static int decode_message(int type, uint
   7.209 +       mask_generation(&msg[1], SHA1_DIGEST_LENGTH,
   7.210 +         &msg[1 + SHA1_DIGEST_LENGTH], msg_len - SHA1_DIGEST_LENGTH - 1);
   7.211 +       sha1_init(&ctx);
   7.212 +-      sha1_update(&ctx, "TCPA", 4);
   7.213 ++      sha1_update(&ctx, (uint8_t *) "TCPA", 4);
   7.214 +       sha1_final(&ctx, &msg[1]);
   7.215 +       if (memcmp(&msg[1], &msg[1 + SHA1_DIGEST_LENGTH], 
   7.216 +           SHA1_DIGEST_LENGTH) != 0) return -1;
   7.217 +diff -uprN orig/tpm_emulator-0.2/linux_module.c vtpm/linux_module.c
   7.218 +--- orig/tpm_emulator-0.2/linux_module.c	2005-08-17 10:58:36.000000000 -0700
   7.219 ++++ vtpm/linux_module.c	1969-12-31 16:00:00.000000000 -0800
   7.220 +@@ -1,163 +0,0 @@
   7.221 +-/* Software-Based Trusted Platform Module (TPM) Emulator for Linux 
   7.222 +- * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   7.223 +- *
   7.224 +- * This module is free software; you can redistribute it and/or modify 
   7.225 +- * it under the terms of the GNU General Public License as published 
   7.226 +- * by the Free Software Foundation; either version 2 of the License, 
   7.227 +- * or (at your option) any later version.  
   7.228 +- *
   7.229 +- * This module is distributed in the hope that it will be useful, 
   7.230 +- * but WITHOUT ANY WARRANTY; without even the implied warranty of 
   7.231 +- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
   7.232 +- * GNU General Public License for more details.
   7.233 +- *
   7.234 +- * $Id: linux_module.c 19 2005-05-18 08:29:37Z mast $
   7.235 +- */
   7.236 +-
   7.237 +-#include <linux/module.h>
   7.238 +-#include <linux/kernel.h>
   7.239 +-#include <linux/init.h>
   7.240 +-#include <linux/miscdevice.h>
   7.241 +-#include <linux/poll.h>
   7.242 +-#include "linux_module.h"
   7.243 +-#include "tpm/tpm_emulator.h"
   7.244 +-
   7.245 +-MODULE_LICENSE("GPL");
   7.246 +-MODULE_AUTHOR("Mario Strasser <mast@gmx.net>");
   7.247 +-MODULE_DESCRIPTION("Trusted Platform Module (TPM) Emulator");
   7.248 +-MODULE_SUPPORTED_DEVICE(TPM_DEVICE_NAME);
   7.249 +-
   7.250 +-/* module startup parameters */
   7.251 +-char *startup = "save";
   7.252 +-MODULE_PARM(startup, "s");
   7.253 +-MODULE_PARM_DESC(startup, " Sets the startup mode of the TPM. "
   7.254 +-  "Possible values are 'clear', 'save' (default) and 'deactivated.");
   7.255 +-char *storage_file = "/var/tpm/tpm_emulator-1.2.0.1";
   7.256 +-MODULE_PARM(storage_file, "s");
   7.257 +-MODULE_PARM_DESC(storage_file, " Sets the persistent-data storage " 
   7.258 +-  "file of the TPM.");
   7.259 +-
   7.260 +-/* TPM lock */
   7.261 +-static struct semaphore tpm_mutex;
   7.262 +-
   7.263 +-/* TPM command response */
   7.264 +-static struct {
   7.265 +-  uint8_t *data;
   7.266 +-  uint32_t size;
   7.267 +-} tpm_response;
   7.268 +-
   7.269 +-/* module state */
   7.270 +-#define STATE_IS_OPEN 0
   7.271 +-static uint32_t module_state;
   7.272 +-
   7.273 +-static int tpm_open(struct inode *inode, struct file *file)
   7.274 +-{
   7.275 +-  debug("%s()", __FUNCTION__);
   7.276 +-  if (test_and_set_bit(STATE_IS_OPEN, (void*)&module_state)) return -EBUSY;
   7.277 +-  return 0;
   7.278 +-}
   7.279 +-
   7.280 +-static int tpm_release(struct inode *inode, struct file *file)
   7.281 +-{
   7.282 +-  debug("%s()", __FUNCTION__);
   7.283 +-  clear_bit(STATE_IS_OPEN, (void*)&module_state);
   7.284 +-  return 0;
   7.285 +-}
   7.286 +-
   7.287 +-static ssize_t tpm_read(struct file *file, char *buf, size_t count, loff_t *ppos)
   7.288 +-{
   7.289 +-  debug("%s(%d)", __FUNCTION__, count);
   7.290 +-  down(&tpm_mutex);
   7.291 +-  if (tpm_response.data != NULL) {
   7.292 +-    count = min(count, (size_t)tpm_response.size - (size_t)*ppos);
   7.293 +-    count -= copy_to_user(buf, &tpm_response.data[*ppos], count);
   7.294 +-    *ppos += count;
   7.295 +-  } else {
   7.296 +-    count = 0;
   7.297 +-  }
   7.298 +-  up(&tpm_mutex);
   7.299 +-  return count;
   7.300 +-}
   7.301 +-
   7.302 +-static ssize_t tpm_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
   7.303 +-{
   7.304 +-  debug("%s(%d)", __FUNCTION__, count);
   7.305 +-  down(&tpm_mutex);
   7.306 +-  *ppos = 0;
   7.307 +-  if (tpm_response.data != NULL) kfree(tpm_response.data);
   7.308 +-  if (tpm_handle_command(buf, count, &tpm_response.data, 
   7.309 +-                         &tpm_response.size) != 0) { 
   7.310 +-    count = -EILSEQ;
   7.311 +-    tpm_response.data = NULL;
   7.312 +-  }
   7.313 +-  up(&tpm_mutex);
   7.314 +-  return count;
   7.315 +-}
   7.316 +-
   7.317 +-static int tpm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
   7.318 +-{
   7.319 +-  debug("%s(%d, %ld)", __FUNCTION__, cmd, arg);
   7.320 +-  return -1;
   7.321 +-}
   7.322 +-
   7.323 +-struct file_operations fops = {
   7.324 +-  .owner   = THIS_MODULE,
   7.325 +-  .open    = tpm_open,
   7.326 +-  .release = tpm_release,
   7.327 +-  .read    = tpm_read,
   7.328 +-  .write   = tpm_write,
   7.329 +-  .ioctl   = tpm_ioctl,
   7.330 +-};
   7.331 +-
   7.332 +-static struct miscdevice tpm_dev = {
   7.333 +-  .minor      = TPM_DEVICE_MINOR, 
   7.334 +-  .name       = TPM_DEVICE_NAME, 
   7.335 +-  .fops       = &fops,
   7.336 +-};
   7.337 +-
   7.338 +-int __init init_tpm_module(void)
   7.339 +-{
   7.340 +-  int res = misc_register(&tpm_dev);
   7.341 +-  if (res != 0) {
   7.342 +-    error("misc_register() failed for minor %d\n", TPM_DEVICE_MINOR);
   7.343 +-    return res;
   7.344 +-  }
   7.345 +-  /* initialize variables */
   7.346 +-  sema_init(&tpm_mutex, 1);
   7.347 +-  module_state = 0;
   7.348 +-  tpm_response.data = NULL;    
   7.349 +-  /* initialize TPM emulator */
   7.350 +-  if (!strcmp(startup, "clear")) {
   7.351 +-    tpm_emulator_init(1);
   7.352 +-  } else if (!strcmp(startup, "save")) { 
   7.353 +-    tpm_emulator_init(2);
   7.354 +-  } else if (!strcmp(startup, "deactivated")) {
   7.355 +-    tpm_emulator_init(3);
   7.356 +-  } else {
   7.357 +-    error("invalid startup mode '%s'; must be 'clear', "
   7.358 +-      "'save' (default) or 'deactivated", startup);
   7.359 +-    misc_deregister(&tpm_dev);
   7.360 +-    return -EINVAL;
   7.361 +-  }
   7.362 +-  return 0;
   7.363 +-}
   7.364 +-
   7.365 +-void __exit cleanup_tpm_module(void)
   7.366 +-{
   7.367 +-  tpm_emulator_shutdown();
   7.368 +-  misc_deregister(&tpm_dev);
   7.369 +-}
   7.370 +-
   7.371 +-module_init(init_tpm_module);
   7.372 +-module_exit(cleanup_tpm_module);
   7.373 +-
   7.374 +-uint64_t tpm_get_ticks(void)
   7.375 +-{
   7.376 +-  static struct timespec old_time = {0, 0}; 
   7.377 +-  struct timespec new_time = current_kernel_time();
   7.378 +-  uint64_t ticks = (uint64_t)(old_time.tv_sec - new_time.tv_sec) * 1000000
   7.379 +-                   + (old_time.tv_nsec - new_time.tv_nsec) / 1000;
   7.380 +-  old_time = new_time;
   7.381 +-  return (ticks > 0) ? ticks : 1;
   7.382 +-}
   7.383 +-
   7.384 +diff -uprN orig/tpm_emulator-0.2/linux_module.h vtpm/linux_module.h
   7.385 +--- orig/tpm_emulator-0.2/linux_module.h	2005-08-17 10:58:36.000000000 -0700
   7.386 ++++ vtpm/linux_module.h	2005-08-17 10:55:52.000000000 -0700
   7.387 +@@ -1,5 +1,6 @@
   7.388 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   7.389 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   7.390 ++ * Copyright (C) 2005 INTEL Corp
   7.391 +  *
   7.392 +  * This module is free software; you can redistribute it and/or modify
   7.393 +  * it under the terms of the GNU General Public License as published
   7.394 +@@ -17,17 +18,22 @@
   7.395 + #ifndef _LINUX_MODULE_H_
   7.396 + #define _LINUX_MODULE_H_
   7.397 + 
   7.398 +-#include <linux/version.h>
   7.399 +-#include <linux/kernel.h>
   7.400 +-#include <linux/slab.h>
   7.401 ++#include <malloc.h>
   7.402 ++#include <stdint.h>
   7.403 ++#include <stdio.h>
   7.404 ++#include <string.h>
   7.405 + #include <linux/types.h>
   7.406 +-#include <linux/string.h>
   7.407 +-#include <linux/random.h>
   7.408 +-#include <linux/time.h>
   7.409 +-#include <asm/byteorder.h>
   7.410 + 
   7.411 +-/* module settings */
   7.412 ++#include <endian.h>
   7.413 ++#define __BYTEORDER_HAS_U64__
   7.414 ++#ifdef LITTLE_ENDIAN
   7.415 ++ #include <linux/byteorder/little_endian.h>
   7.416 ++#else
   7.417 ++ #include <linux/byteorder/big_endian.h>
   7.418 ++#endif
   7.419 + 
   7.420 ++/* module settings */
   7.421 ++#define min(A,B) ((A)<(B)?(A):(B))
   7.422 + #define STR(s) __STR__(s)
   7.423 + #define __STR__(s) #s
   7.424 + #include "tpm_version.h"
   7.425 +@@ -39,32 +45,35 @@
   7.426 + /* debug and log output functions */
   7.427 + 
   7.428 + #ifdef DEBUG
   7.429 +-#define debug(fmt, ...) printk(KERN_DEBUG "%s %s:%d: Debug: " fmt "\n", \
   7.430 +-                        TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
   7.431 ++#define debug(fmt, ...) printf("%s:%d: Debug: " fmt "\n", \
   7.432 ++                        __FILE__, __LINE__, ## __VA_ARGS__)
   7.433 + #else
   7.434 + #define debug(fmt, ...) 
   7.435 + #endif
   7.436 +-#define info(fmt, ...)  printk(KERN_INFO "%s %s:%d: Info: " fmt "\n", \
   7.437 +-                        TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
   7.438 +-#define error(fmt, ...) printk(KERN_ERR "%s %s:%d: Error: " fmt "\n", \
   7.439 +-                        TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
   7.440 +-#define alert(fmt, ...) printk(KERN_ALERT "%s %s:%d: Alert: " fmt "\n", \
   7.441 +-                        TPM_MODULE_NAME, __FILE__, __LINE__, ## __VA_ARGS__)
   7.442 ++#define info(fmt, ...)  printf("%s:%d: Info: " fmt "\n", \
   7.443 ++                        __FILE__, __LINE__, ## __VA_ARGS__)
   7.444 ++#define error(fmt, ...) printf("%s:%d: Error: " fmt "\n", \
   7.445 ++                        __FILE__, __LINE__, ## __VA_ARGS__)
   7.446 ++#define alert(fmt, ...) printf("%s:%d: Alert: " fmt "\n", \
   7.447 ++                        __FILE__, __LINE__, ## __VA_ARGS__)
   7.448 + 
   7.449 + /* memory allocation */
   7.450 + 
   7.451 + static inline void *tpm_malloc(size_t size) 
   7.452 + {
   7.453 +-  return kmalloc(size, GFP_KERNEL);  
   7.454 ++  return malloc(size);  
   7.455 + }
   7.456 + 
   7.457 + static inline void tpm_free(const void *ptr)
   7.458 + {
   7.459 +-  if (ptr != NULL) kfree(ptr);
   7.460 ++  if (ptr != NULL) free( (void *) ptr);
   7.461 + }
   7.462 + 
   7.463 + /* random numbers */
   7.464 + 
   7.465 ++//FIXME;
   7.466 ++void get_random_bytes(void *buf, int nbytes);
   7.467 ++
   7.468 + static inline void tpm_get_random_bytes(void *buf, int nbytes)
   7.469 + {
   7.470 +   get_random_bytes(buf, nbytes);
   7.471 +@@ -84,9 +93,9 @@ uint64_t tpm_get_ticks(void);
   7.472 + #define CPU_TO_LE16(x) __cpu_to_le16(x)
   7.473 + 
   7.474 + #define BE64_TO_CPU(x) __be64_to_cpu(x)
   7.475 +-#define LE64_TO_CPU(x) __be64_to_cpu(x)
   7.476 ++#define LE64_TO_CPU(x) __le64_to_cpu(x)
   7.477 + #define BE32_TO_CPU(x) __be32_to_cpu(x)
   7.478 +-#define LE32_TO_CPU(x) __be32_to_cpu(x)
   7.479 ++#define LE32_TO_CPU(x) __le32_to_cpu(x)
   7.480 + #define BE16_TO_CPU(x) __be16_to_cpu(x)
   7.481 + #define LE16_TO_CPU(x) __le16_to_cpu(x)
   7.482 + 
   7.483 +diff -uprN orig/tpm_emulator-0.2/tpm/tpm_audit.c vtpm/tpm/tpm_audit.c
   7.484 +--- orig/tpm_emulator-0.2/tpm/tpm_audit.c	2005-08-17 10:58:36.000000000 -0700
   7.485 ++++ vtpm/tpm/tpm_audit.c	2005-08-17 10:55:52.000000000 -0700
   7.486 +@@ -1,6 +1,7 @@
   7.487 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   7.488 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   7.489 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
   7.490 ++ * Copyright (C) 2005 INTEL Corp
   7.491 +  *
   7.492 +  * This module is free software; you can redistribute it and/or modify
   7.493 +  * it under the terms of the GNU General Public License as published
   7.494 +@@ -45,14 +46,14 @@ void tpm_audit_request(TPM_COMMAND_CODE 
   7.495 +       tpmData.permanent.data.auditMonotonicCounter++;
   7.496 +     }
   7.497 +     /* update audit digest */
   7.498 +-    *((UINT16*)&buf[0])  = cpu_to_be16(TPM_TAG_AUDIT_EVENT_IN);
   7.499 +-    *((UINT32*)&buf[2]) = cpu_to_be32(ordinal);
   7.500 ++    *((UINT16*)&buf[0])  = CPU_TO_BE16(TPM_TAG_AUDIT_EVENT_IN);
   7.501 ++    *((UINT32*)&buf[2]) = CPU_TO_BE32(ordinal);
   7.502 +     sha1_init(&sha1_ctx);
   7.503 +     sha1_update(&sha1_ctx, req->param, req->paramSize);
   7.504 +     sha1_final(&sha1_ctx, &buf[6]);
   7.505 +-    *((UINT16*)&buf[26])  = cpu_to_be16(TPM_TAG_COUNTER_VALUE);
   7.506 ++    *((UINT16*)&buf[26])  = CPU_TO_BE16(TPM_TAG_COUNTER_VALUE);
   7.507 +     memset(&buf[30], 0, 4);
   7.508 +-    *((UINT32*)&buf[34]) = cpu_to_be32(tpmData.permanent.data.auditMonotonicCounter);
   7.509 ++    *((UINT32*)&buf[34]) = CPU_TO_BE32(tpmData.permanent.data.auditMonotonicCounter);
   7.510 +     sha1_init(&sha1_ctx);
   7.511 +     sha1_update(&sha1_ctx, tpmData.stany.data.auditDigest.digest, 
   7.512 +       sizeof(TPM_DIGEST));
   7.513 +@@ -70,15 +71,15 @@ void tpm_audit_response(TPM_COMMAND_CODE
   7.514 +       && (AUDIT_STATUS[ord / 8] & (1 << (ord & 0x07)))) {
   7.515 +     info("tpm_audit_response()");
   7.516 +     /* update audit digest */
   7.517 +-    *((UINT16*)&buf[0])  = cpu_to_be16(TPM_TAG_AUDIT_EVENT_OUT);
   7.518 +-    *((UINT32*)&buf[2]) = cpu_to_be32(ordinal);
   7.519 ++    *((UINT16*)&buf[0])  = CPU_TO_BE16(TPM_TAG_AUDIT_EVENT_OUT);
   7.520 ++    *((UINT32*)&buf[2]) = CPU_TO_BE32(ordinal);
   7.521 +     sha1_init(&sha1_ctx);
   7.522 +     sha1_update(&sha1_ctx, rsp->param, rsp->paramSize);
   7.523 +     sha1_final(&sha1_ctx, &buf[6]);
   7.524 +-    *((UINT16*)&buf[26])  = cpu_to_be16(TPM_TAG_COUNTER_VALUE);
   7.525 ++    *((UINT16*)&buf[26])  = CPU_TO_BE16(TPM_TAG_COUNTER_VALUE);
   7.526 +     memset(&buf[30], 0, 4);
   7.527 +-    *((UINT32*)&buf[34]) = cpu_to_be32(tpmData.permanent.data.auditMonotonicCounter);
   7.528 +-    *((UINT32*)&buf[34]) = cpu_to_be32(rsp->result);
   7.529 ++    *((UINT32*)&buf[34]) = CPU_TO_BE32(tpmData.permanent.data.auditMonotonicCounter);
   7.530 ++    *((UINT32*)&buf[34]) = CPU_TO_BE32(rsp->result);
   7.531 +     sha1_init(&sha1_ctx);
   7.532 +     sha1_update(&sha1_ctx, tpmData.stany.data.auditDigest.digest, 
   7.533 +       sizeof(TPM_DIGEST));
   7.534 +@@ -158,7 +159,7 @@ TPM_RESULT TPM_GetAuditDigestSigned(TPM_
   7.535 +   }
   7.536 +   memcpy(&buf[0], "\x05\x00ADIG", 6);
   7.537 +   memcpy(&buf[6], antiReplay->nonce, 20);
   7.538 +-  *(UINT32*)&buf[26] = cpu_to_be32(buf_size - 30);
   7.539 ++  *(UINT32*)&buf[26] = CPU_TO_BE32(buf_size - 30);
   7.540 +   memcpy(&buf[30], auditDigest->digest, 20);
   7.541 +   ptr = &buf[50];
   7.542 +   len = buf_size - 50;
   7.543 +@@ -198,4 +199,3 @@ TPM_RESULT TPM_SetOrdinalAuditStatus(TPM
   7.544 +   }
   7.545 +   return TPM_SUCCESS;
   7.546 + }
   7.547 +-
   7.548 +diff -uprN orig/tpm_emulator-0.2/tpm/tpm_authorization.c vtpm/tpm/tpm_authorization.c
   7.549 +--- orig/tpm_emulator-0.2/tpm/tpm_authorization.c	2005-08-17 10:58:36.000000000 -0700
   7.550 ++++ vtpm/tpm/tpm_authorization.c	2005-08-17 10:55:52.000000000 -0700
   7.551 +@@ -1,6 +1,7 @@
   7.552 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   7.553 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   7.554 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
   7.555 ++ * Copyright (C) 2005 INTEL Corp
   7.556 +  *
   7.557 +  * This module is free software; you can redistribute it and/or modify
   7.558 +  * it under the terms of the GNU General Public License as published
   7.559 +@@ -268,7 +269,7 @@ TPM_RESULT tpm_verify_auth(TPM_AUTH *aut
   7.560 + {
   7.561 +   hmac_ctx_t ctx;
   7.562 +   TPM_SESSION_DATA *session;
   7.563 +-  UINT32 auth_handle = cpu_to_be32(auth->authHandle);
   7.564 ++  UINT32 auth_handle = CPU_TO_BE32(auth->authHandle);
   7.565 +   
   7.566 +   info("tpm_verify_auth(%08x)", auth->authHandle);
   7.567 +   /* get dedicated authorization session */
   7.568 +@@ -316,5 +317,3 @@ void tpm_decrypt_auth_secret(TPM_ENCAUTH
   7.569 +   for (i = 0; i < sizeof(TPM_SECRET); i++)
   7.570 +     plainAuth[i] ^= encAuth[i];
   7.571 + }
   7.572 +-
   7.573 +-
   7.574 +diff -uprN orig/tpm_emulator-0.2/tpm/tpm_capability.c vtpm/tpm/tpm_capability.c
   7.575 +--- orig/tpm_emulator-0.2/tpm/tpm_capability.c	2005-08-17 10:58:36.000000000 -0700
   7.576 ++++ vtpm/tpm/tpm_capability.c	2005-08-17 10:55:52.000000000 -0700
   7.577 +@@ -1,6 +1,7 @@
   7.578 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   7.579 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   7.580 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
   7.581 ++ * Copyright (C) 2005 INTEL Corp
   7.582 +  *
   7.583 +  * This module is free software; you can redistribute it and/or modify
   7.584 +  * it under the terms of the GNU General Public License as published
   7.585 +@@ -398,7 +399,7 @@ TPM_RESULT TPM_GetCapability(TPM_CAPABIL
   7.586 + 
   7.587 +     case TPM_CAP_KEY_HANDLE:
   7.588 +       debug("[TPM_CAP_KEY_HANDLE]");
   7.589 +-      subCapSize = cpu_to_be32(TPM_RT_KEY);
   7.590 ++      subCapSize = CPU_TO_BE32(TPM_RT_KEY);
   7.591 +       return cap_handle(4, (BYTE*)&subCapSize, respSize, resp);
   7.592 + 
   7.593 +     case TPM_CAP_CHECK_LOADED:
   7.594 +@@ -472,4 +473,3 @@ TPM_RESULT TPM_GetCapability(TPM_CAPABIL
   7.595 +       return TPM_BAD_MODE;
   7.596 +   }
   7.597 + }
   7.598 +-
   7.599 +diff -uprN orig/tpm_emulator-0.2/tpm/tpm_cmd_handler.c vtpm/tpm/tpm_cmd_handler.c
   7.600 +--- orig/tpm_emulator-0.2/tpm/tpm_cmd_handler.c	2005-08-17 10:58:36.000000000 -0700
   7.601 ++++ vtpm/tpm/tpm_cmd_handler.c	2005-08-17 10:55:52.000000000 -0700
   7.602 +@@ -1,6 +1,7 @@
   7.603 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   7.604 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   7.605 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
   7.606 ++ * Copyright (C) 2005 INTEL Corp
   7.607 +  *
   7.608 +  * This module is free software; you can redistribute it and/or modify
   7.609 +  * it under the terms of the GNU General Public License as published
   7.610 +@@ -26,7 +27,7 @@ static void tpm_compute_in_param_digest(
   7.611 + {
   7.612 +   sha1_ctx_t sha1;
   7.613 +   UINT32 offset;
   7.614 +-  UINT32 ord = cpu_to_be32(req->ordinal);
   7.615 ++  UINT32 ord = CPU_TO_BE32(req->ordinal);
   7.616 + 
   7.617 +   /* skip all key-handles at the beginning */
   7.618 +   switch (req->ordinal) {
   7.619 +@@ -82,8 +83,8 @@ static void tpm_compute_in_param_digest(
   7.620 + static void tpm_compute_out_param_digest(TPM_COMMAND_CODE ordinal, TPM_RESPONSE *rsp)
   7.621 + {
   7.622 +   sha1_ctx_t sha1;
   7.623 +-  UINT32 res = cpu_to_be32(rsp->result);
   7.624 +-  UINT32 ord = cpu_to_be32(ordinal);
   7.625 ++  UINT32 res = CPU_TO_BE32(rsp->result);
   7.626 ++  UINT32 ord = CPU_TO_BE32(ordinal);
   7.627 + 
   7.628 +   /* compute SHA1 hash */
   7.629 +   sha1_init(&sha1);
   7.630 +@@ -3081,7 +3082,7 @@ static void tpm_setup_rsp_auth(TPM_COMMA
   7.631 +       hmac_update(&hmac, rsp->auth2->digest, sizeof(rsp->auth2->digest));
   7.632 + #if 0
   7.633 +       if (tpm_get_auth(rsp->auth2->authHandle)->type == TPM_ST_OIAP) {
   7.634 +-        UINT32 handle = cpu_to_be32(rsp->auth2->authHandle);
   7.635 ++        UINT32 handle = CPU_TO_BE32(rsp->auth2->authHandle);
   7.636 +         hmac_update(&hmac, (BYTE*)&handle, 4);
   7.637 +       }
   7.638 + #endif
   7.639 +@@ -3096,7 +3097,7 @@ static void tpm_setup_rsp_auth(TPM_COMMA
   7.640 +       hmac_update(&hmac, rsp->auth1->digest, sizeof(rsp->auth1->digest));
   7.641 + #if 0
   7.642 +       if (tpm_get_auth(rsp->auth1->authHandle)->type == TPM_ST_OIAP) {
   7.643 +-        UINT32 handle = cpu_to_be32(rsp->auth1->authHandle);
   7.644 ++        UINT32 handle = CPU_TO_BE32(rsp->auth1->authHandle);
   7.645 +         hmac_update(&hmac, (BYTE*)&handle, 4);
   7.646 +       }
   7.647 + #endif
   7.648 +@@ -3179,7 +3180,9 @@ extern const char *tpm_error_to_string(T
   7.649 + static void tpm_execute_command(TPM_REQUEST *req, TPM_RESPONSE *rsp)
   7.650 + {
   7.651 +   TPM_RESULT res;
   7.652 +-  
   7.653 ++
   7.654 ++  req->tag = (BYTE) req->tag;  // VIN HACK!!! 
   7.655 ++
   7.656 +   /* setup authorisation as well as response tag and size */
   7.657 +   memset(rsp, 0, sizeof(*rsp));
   7.658 +   switch (req->tag) {
   7.659 +@@ -3878,4 +3881,3 @@ int tpm_handle_command(const uint8_t *in
   7.660 +   tpm_free(rsp.param);
   7.661 +   return 0;
   7.662 + }
   7.663 +-
   7.664 +diff -uprN orig/tpm_emulator-0.2/tpm/tpm_crypto.c vtpm/tpm/tpm_crypto.c
   7.665 +--- orig/tpm_emulator-0.2/tpm/tpm_crypto.c	2005-08-17 10:58:36.000000000 -0700
   7.666 ++++ vtpm/tpm/tpm_crypto.c	2005-08-17 10:55:52.000000000 -0700
   7.667 +@@ -1,6 +1,7 @@
   7.668 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   7.669 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   7.670 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
   7.671 ++ * Copyright (C) 2005 INTEL Corp
   7.672 +  *
   7.673 +  * This module is free software; you can redistribute it and/or modify
   7.674 +  * it under the terms of the GNU General Public License as published
   7.675 +@@ -106,7 +107,7 @@ TPM_RESULT tpm_sign(TPM_KEY_DATA *key, T
   7.676 +     /* setup TPM_SIGN_INFO structure */
   7.677 +     memcpy(&buf[0], "\x05\x00SIGN", 6);
   7.678 +     memcpy(&buf[6], auth->nonceOdd.nonce, 20);
   7.679 +-    *(UINT32*)&buf[26] = cpu_to_be32(areaToSignSize);
   7.680 ++    *(UINT32*)&buf[26] = CPU_TO_BE32(areaToSignSize);
   7.681 +     memcpy(&buf[30], areaToSign, areaToSignSize);
   7.682 +     if (rsa_sign(&key->key, RSA_SSA_PKCS1_SHA1, 
   7.683 +         buf, areaToSignSize + 30, *sig)) {
   7.684 +@@ -379,4 +380,3 @@ TPM_RESULT TPM_CertifyKey2(TPM_KEY_HANDL
   7.685 +   }  
   7.686 +   return TPM_SUCCESS;
   7.687 + }
   7.688 +-
   7.689 +diff -uprN orig/tpm_emulator-0.2/tpm/tpm_data.c vtpm/tpm/tpm_data.c
   7.690 +--- orig/tpm_emulator-0.2/tpm/tpm_data.c	2005-08-17 10:58:36.000000000 -0700
   7.691 ++++ vtpm/tpm/tpm_data.c	2005-08-17 10:55:52.000000000 -0700
   7.692 +@@ -1,6 +1,7 @@
   7.693 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
   7.694 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
   7.695 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
   7.696 ++ * Copyright (C) 2005 INTEL Corp
   7.697 +  *
   7.698 +  * This module is free software; you can redistribute it and/or modify
   7.699 +  * it under the terms of the GNU General Public License as published
   7.700 +@@ -15,9 +16,15 @@
   7.701 +  * $Id: tpm_data.c 9 2005-04-26 18:15:31Z mast $
   7.702 +  */
   7.703 + 
   7.704 ++#include <sys/types.h>
   7.705 ++#include <sys/stat.h>
   7.706 ++#include <fcntl.h>
   7.707 ++#include <unistd.h>
   7.708 ++
   7.709 + #include "tpm_emulator.h"
   7.710 + #include "tpm_structures.h"
   7.711 + #include "tpm_marshalling.h"
   7.712 ++#include "vtpm_manager.h"
   7.713 + 
   7.714 + TPM_DATA tpmData;
   7.715 + 
   7.716 +@@ -28,6 +35,7 @@ BOOL tpm_get_physical_presence(void)
   7.717 + 
   7.718 + void tpm_init_data(void)
   7.719 + {
   7.720 ++#ifndef TPM_GENERATE_EK
   7.721 +   /* endorsement key */
   7.722 +   uint8_t ek_n[] =  "\xa8\xdb\xa9\x42\xa8\xf3\xb8\x06\x85\x90\x76\x93\xad\xf7"
   7.723 +     "\x74\xec\x3f\xd3\x3d\x9d\xe8\x2e\xff\x15\xed\x0e\xce\x5f\x93"
   7.724 +@@ -66,6 +74,8 @@ void tpm_init_data(void)
   7.725 +     "\xd1\xc0\x8b\x5b\xa2\x2e\xa7\x15\xca\x50\x75\x10\x48\x9c\x2b"
   7.726 +     "\x18\xb9\x67\x8f\x5d\x64\xc3\x28\x9f\x2f\x16\x2f\x08\xda\x47"
   7.727 +     "\xec\x86\x43\x0c\x80\x99\x07\x34\x0f";
   7.728 ++#endif
   7.729 ++
   7.730 +   int i;
   7.731 +   /* reset all data to NULL, FALSE or 0 */
   7.732 +   memset(&tpmData, 0, sizeof(tpmData));
   7.733 +@@ -85,6 +95,10 @@ void tpm_init_data(void)
   7.734 +   tpmData.permanent.data.version.revMinor = VERSION_MINOR;
   7.735 +   /* setup PCR attributes */
   7.736 +   for (i = 0; i < TPM_NUM_PCR; i++) {
   7.737 ++    int j;
   7.738 ++    for (j=0; j < TPM_NUM_LOCALITY; j++) {
   7.739 ++      tpmData.permanent.data.pcrAttrib[i].pcrExtendLocal[j] = TRUE;
   7.740 ++    }
   7.741 +     tpmData.permanent.data.pcrAttrib[i].pcrReset = TRUE;
   7.742 +   }
   7.743 +   /* set tick type */
   7.744 +@@ -115,49 +129,235 @@ void tpm_release_data(void)
   7.745 + 
   7.746 + #ifdef TPM_STORE_TO_FILE
   7.747 + 
   7.748 +-#include <linux/fs.h>
   7.749 +-#include <linux/unistd.h>
   7.750 +-#include <asm/uaccess.h>
   7.751 ++#include <sys/types.h>
   7.752 ++#include <sys/stat.h>
   7.753 ++#include <fcntl.h>
   7.754 ++
   7.755 ++ static int vtpm_tx_fh=-1, vtpm_rx_fh=-1;
   7.756 + 
   7.757 +-#define TPM_STORAGE_FILE "/var/tpm/tpm_emulator-1.2." STR(VERSION_MAJOR) "." STR(VERSION_MINOR) 
   7.758 ++#ifdef VTPM_MUTLI_VM
   7.759 ++ #define DEV_FE "/dev/tpm"
   7.760 ++#else
   7.761 ++ #define VTPM_RX_FIFO_D  "/var/vtpm/fifos/vtpm-to-%d.fifo"
   7.762 ++ #define VTPM_TX_FIFO  "/var/vtpm/fifos/vtpm-from-all.fifo"
   7.763 ++
   7.764 ++ extern int dmi_id;
   7.765 ++ static char *vtpm_rx_name=NULL; 
   7.766 ++#endif
   7.767 + 
   7.768 + static int write_to_file(uint8_t *data, size_t data_length)
   7.769 + {
   7.770 +-  int res;
   7.771 +-  struct file *fp;
   7.772 +-  mm_segment_t old_fs = get_fs();
   7.773 +-  fp = filp_open(TPM_STORAGE_FILE, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
   7.774 +-  if (IS_ERR(fp)) return -1;
   7.775 +-  set_fs(get_ds());
   7.776 +-  res = fp->f_op->write(fp, data, data_length, &fp->f_pos);
   7.777 +-  set_fs(old_fs);
   7.778 +-  filp_close(fp, NULL);
   7.779 +-  return (res == data_length) ? 0 : -1;
   7.780 ++  int res, out_data_size, in_header_size;
   7.781 ++  BYTE *ptr, *out_data, *in_header;
   7.782 ++  UINT32 result, len, in_rsp_size;
   7.783 ++  UINT16 tag = VTPM_TAG_REQ;
   7.784 ++	
   7.785 ++  printf("Saving NVM\n");
   7.786 ++  if (vtpm_tx_fh < 0) {
   7.787 ++#ifdef VTPM_MUTLI_VM
   7.788 ++    vtpm_tx_fh = open(DEV_FE, O_RDWR);
   7.789 ++#else
   7.790 ++	vtpm_tx_fh = open(VTPM_TX_FIFO, O_WRONLY);
   7.791 ++#endif
   7.792 ++  }
   7.793 ++
   7.794 ++  if (vtpm_tx_fh < 0) {
   7.795 ++		return -1;
   7.796 ++  }
   7.797 ++ 
   7.798 ++  // Send request to VTPM Manager to encrypt data
   7.799 ++#ifdef VTPM_MUTLI_VM
   7.800 ++  out_data_size = len = VTPM_COMMAND_HEADER_SIZE_CLT + data_length;
   7.801 ++#else
   7.802 ++  out_data_size = len = VTPM_COMMAND_HEADER_SIZE_SRV + data_length;
   7.803 ++#endif
   7.804 ++  
   7.805 ++  out_data = ptr = (BYTE *) malloc(len);
   7.806 ++
   7.807 ++  if (ptr == NULL
   7.808 ++#ifndef VTPM_MUTLI_VM
   7.809 ++      || tpm_marshal_UINT32(&ptr, &len, dmi_id)
   7.810 ++#endif
   7.811 ++	  || tpm_marshal_UINT16(&ptr, &len, tag)
   7.812 ++#ifdef VTPM_MUTLI_VM
   7.813 ++	  || tpm_marshal_UINT32(&ptr, &len, out_data_size)
   7.814 ++#else
   7.815 ++	  || tpm_marshal_UINT32(&ptr, &len, out_data_size - sizeof(uint32_t))
   7.816 ++#endif  
   7.817 ++	  || tpm_marshal_UINT32(&ptr, &len, VTPM_ORD_SAVENVM)
   7.818 ++	  || tpm_marshal_BYTE_ARRAY(&ptr, &len, data, data_length)) {
   7.819 ++	free(out_data);
   7.820 ++	return -1;
   7.821 ++  }
   7.822 ++  
   7.823 ++  printf("\tSending SaveNVM Command.\n");
   7.824 ++  res = write(vtpm_tx_fh, out_data, out_data_size);
   7.825 ++  free(out_data);
   7.826 ++  if (res != out_data_size) return -1;
   7.827 ++
   7.828 ++  if (vtpm_rx_fh < 0) {
   7.829 ++#ifdef VTPM_MUTLI_VM
   7.830 ++    vtpm_rx_fh = vtpm_tx_fh
   7.831 ++#else
   7.832 ++    if (vtpm_rx_name == NULL) {
   7.833 ++      vtpm_rx_name = malloc(10 + strlen(VTPM_RX_FIFO_D));
   7.834 ++      sprintf(vtpm_rx_name, VTPM_RX_FIFO_D, (uint32_t) dmi_id);
   7.835 ++    }
   7.836 ++	vtpm_rx_fh = open(vtpm_rx_name, O_RDONLY);
   7.837 ++#endif
   7.838 ++  }
   7.839 ++
   7.840 ++  if (vtpm_rx_fh < 0) {
   7.841 ++		return -1;
   7.842 ++  }
   7.843 ++  
   7.844 ++  // Read Header of response so we can get the size & status
   7.845 ++#ifdef VTPM_MUTLI_VM
   7.846 ++  in_header_size = len = VTPM_COMMAND_HEADER_SIZE_CLT;
   7.847 ++#else
   7.848 ++  in_header_size = len = VTPM_COMMAND_HEADER_SIZE_SRV;
   7.849 ++#endif
   7.850 ++  in_header = ptr = malloc(in_header_size);
   7.851 ++  
   7.852 ++  printf("\tReading SaveNVM header.\n");
   7.853 ++  res = read(vtpm_rx_fh, in_header, in_header_size);
   7.854 ++
   7.855 ++  if ( (res != in_header_size)
   7.856 ++#ifndef VTPM_MUTLI_VM
   7.857 ++       || tpm_unmarshal_UINT32(&ptr, &len, (UINT32*)&dmi_id)
   7.858 ++#endif
   7.859 ++	   || tpm_unmarshal_UINT16(&ptr, &len, &tag)
   7.860 ++	   || tpm_unmarshal_UINT32(&ptr, &len, &in_rsp_size)
   7.861 ++	   || tpm_unmarshal_UINT32(&ptr, &len, &result) ) {
   7.862 ++	  free(in_header);
   7.863 ++	  return -1;
   7.864 ++  }
   7.865 ++  free(in_header);
   7.866 ++  
   7.867 ++  if (result != VTPM_SUCCESS) {
   7.868 ++      return -1;  
   7.869 ++  }
   7.870 ++
   7.871 ++#ifdef VTPM_MUTLI_VM
   7.872 ++  close(vtpm_tx_fh); close(vtpm_rx_fh);
   7.873 ++#endif
   7.874 ++	  
   7.875 ++  printf("\tFinishing up SaveNVM\n");
   7.876 ++  return (0);
   7.877 + }
   7.878 + 
   7.879 + static int read_from_file(uint8_t **data, size_t *data_length)
   7.880 + {
   7.881 +-  int res;
   7.882 +-  struct file *fp;
   7.883 +-  mm_segment_t old_fs = get_fs();
   7.884 +-  fp = filp_open(TPM_STORAGE_FILE, O_RDONLY, 0);
   7.885 +-  if (IS_ERR(fp)) return -1;
   7.886 +-  *data_length = (size_t)fp->f_dentry->d_inode->i_size;
   7.887 +-  /* *data_length = i_size_read(fp->f_dentry->d_inode); */
   7.888 +-  *data = tpm_malloc(*data_length);
   7.889 +-  if (*data == NULL) {
   7.890 +-    filp_close(fp, NULL);
   7.891 ++  int res, out_data_size, in_header_size;
   7.892 ++  uint8_t *ptr, *out_data, *in_header;
   7.893 ++  UINT16 tag = VTPM_TAG_REQ;
   7.894 ++  UINT32 len, in_rsp_size, result;
   7.895 ++#ifdef VTPM_MUTLI_VM
   7.896 ++	int vtpm_rx_fh, vtpm_tx_fh;
   7.897 ++#endif
   7.898 ++	
   7.899 ++  printf("Loading NVM.\n");
   7.900 ++  if (vtpm_tx_fh < 0) {
   7.901 ++#ifdef VTPM_MUTLI_VM
   7.902 ++    vtpm_tx_fh = open(DEV_FE, O_RDWR);
   7.903 ++#else
   7.904 ++	vtpm_tx_fh = open(VTPM_TX_FIFO, O_WRONLY);
   7.905 ++#endif
   7.906 ++  }
   7.907 ++
   7.908 ++  if (vtpm_tx_fh < 0) {
   7.909 ++		return -1;
   7.910 ++  }
   7.911 ++ 
   7.912 ++  // Send request to VTPM Manager to encrypt data
   7.913 ++#ifdef VTPM_MUTLI_VM
   7.914 ++  out_data_size = len = VTPM_COMMAND_HEADER_SIZE_CLT;
   7.915 ++#else
   7.916 ++  out_data_size = len = VTPM_COMMAND_HEADER_SIZE_SRV;
   7.917 ++#endif
   7.918 ++  out_data = ptr = (BYTE *) malloc(len);
   7.919 ++
   7.920 ++  if (ptr == NULL
   7.921 ++#ifndef VTPM_MUTLI_VM
   7.922 ++      || tpm_marshal_UINT32(&ptr, &len, dmi_id)
   7.923 ++#endif  
   7.924 ++      || tpm_marshal_UINT16(&ptr, &len, tag)
   7.925 ++#ifdef VTPM_MUTLI_VM
   7.926 ++      || tpm_marshal_UINT32(&ptr, &len, out_data_size)
   7.927 ++#else
   7.928 ++      || tpm_marshal_UINT32(&ptr, &len, out_data_size - sizeof(uint32_t))
   7.929 ++#endif
   7.930 ++      || tpm_marshal_UINT32(&ptr, &len, VTPM_ORD_LOADNVM)) {
   7.931 ++    free(out_data);
   7.932 +     return -1;
   7.933 +   }
   7.934 +-  set_fs(get_ds());
   7.935 +-  res = fp->f_op->read(fp, *data, *data_length, &fp->f_pos);
   7.936 +-  set_fs(old_fs);
   7.937 +-  filp_close(fp, NULL);
   7.938 ++
   7.939 ++  printf("\tSending LoadNVM command\n");
   7.940 ++  res = write(vtpm_tx_fh, out_data, out_data_size);
   7.941 ++  free(out_data);
   7.942 ++  if (res != out_data_size) return -1;
   7.943 ++
   7.944 ++    if (vtpm_rx_fh < 0) {
   7.945 ++#ifdef VTPM_MUTLI_VM
   7.946 ++    vtpm_rx_fh = vtpm_tx_fh;
   7.947 ++#else
   7.948 ++    if (vtpm_rx_name == NULL) {
   7.949 ++      vtpm_rx_name = malloc(10 + strlen(VTPM_RX_FIFO_D));
   7.950 ++      sprintf(vtpm_rx_name, VTPM_RX_FIFO_D, (uint32_t) dmi_id);
   7.951 ++    }
   7.952 ++	vtpm_rx_fh = open(vtpm_rx_name, O_RDONLY);
   7.953 ++#endif
   7.954 ++  }
   7.955 ++
   7.956 ++  if (vtpm_rx_fh < 0) {
   7.957 ++		return -1;
   7.958 ++  }
   7.959 ++  
   7.960 ++  // Read Header of response so we can get the size & status
   7.961 ++#ifdef VTPM_MUTLI_VM
   7.962 ++  in_header_size = len = VTPM_COMMAND_HEADER_SIZE_CLT;
   7.963 ++#else
   7.964 ++  in_header_size = len = VTPM_COMMAND_HEADER_SIZE_SRV;
   7.965 ++#endif
   7.966 ++  in_header = ptr = malloc(in_header_size);
   7.967 ++  
   7.968 ++  printf("\tReading LoadNVM header\n");
   7.969 ++  res = read(vtpm_rx_fh, in_header, in_header_size);
   7.970 ++
   7.971 ++  if ( (res != in_header_size)
   7.972 ++#ifndef VTPM_MUTLI_VM
   7.973 ++       || tpm_unmarshal_UINT32(&ptr, &len, (UINT32*)&dmi_id)
   7.974 ++#endif
   7.975 ++       || tpm_unmarshal_UINT16(&ptr, &len, &tag)
   7.976 ++       || tpm_unmarshal_UINT32(&ptr, &len, &in_rsp_size)
   7.977 ++       || tpm_unmarshal_UINT32(&ptr, &len, &result) ) {
   7.978 ++      free(in_header);
   7.979 ++      return -1;
   7.980 ++  }
   7.981 ++  free(in_header);
   7.982 ++  
   7.983 ++  if (result != VTPM_SUCCESS) {
   7.984 ++      return -1;  
   7.985 ++  }
   7.986 ++
   7.987 ++  // Read Encrypted data from VTPM Manager
   7.988 ++  *data_length = in_rsp_size - VTPM_COMMAND_HEADER_SIZE_CLT;
   7.989 ++  *data = (uint8_t *) malloc(*data_length);
   7.990 ++
   7.991 ++  printf("\tReading clear data from LoadNVM.\n");
   7.992 ++  res = read(vtpm_rx_fh, *data, *data_length);
   7.993 ++#ifdef VTPM_MUTLI_VM
   7.994 ++  close(vtpm_rx_fh);close(vtpm_tx_fh);
   7.995 ++#endif 
   7.996 ++	
   7.997 ++  printf("\tReturing from loading NVM\n");
   7.998 +   if (res != *data_length) {
   7.999 +-    tpm_free(*data);
  7.1000 +-    return -1;
  7.1001 ++      free(*data);
  7.1002 ++      return -1;
  7.1003 ++  } else {
  7.1004 ++      return 0;
  7.1005 +   }
  7.1006 +-  return 0;
  7.1007 ++
  7.1008 + }
  7.1009 + 
  7.1010 + #else
  7.1011 +@@ -231,7 +431,6 @@ int tpm_restore_permanent_data(void)
  7.1012 + 
  7.1013 + int tpm_erase_permanent_data(void)
  7.1014 + {
  7.1015 +-  int res = write_to_file("", 0);
  7.1016 ++  int res = write_to_file((uint8_t*)"", 0);
  7.1017 +   return res;
  7.1018 + }
  7.1019 +-
  7.1020 +diff -uprN orig/tpm_emulator-0.2/tpm/tpm_deprecated.c vtpm/tpm/tpm_deprecated.c
  7.1021 +--- orig/tpm_emulator-0.2/tpm/tpm_deprecated.c	2005-08-17 10:58:36.000000000 -0700
  7.1022 ++++ vtpm/tpm/tpm_deprecated.c	2005-08-17 10:55:52.000000000 -0700
  7.1023 +@@ -1,6 +1,7 @@
  7.1024 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  7.1025 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  7.1026 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
  7.1027 ++ * Copyright (C) 2005 INTEL Corp
  7.1028 +  *
  7.1029 +  * This module is free software; you can redistribute it and/or modify
  7.1030 +  * it under the terms of the GNU General Public License as published
  7.1031 +@@ -50,7 +51,7 @@ TPM_RESULT TPM_SaveKeyContext(TPM_KEY_HA
  7.1032 +   BYTE *ptr;
  7.1033 +   UINT32 len;
  7.1034 +   info("TPM_SaveKeyContext()");
  7.1035 +-  res = TPM_SaveContext(keyHandle, TPM_RT_KEY, "SaveKeyContext..", 
  7.1036 ++  res = TPM_SaveContext(keyHandle, TPM_RT_KEY, (BYTE*)"SaveKeyContext..", 
  7.1037 +                         keyContextSize, &contextBlob);
  7.1038 +   if (res != TPM_SUCCESS) return res;
  7.1039 +   len = *keyContextSize;
  7.1040 +@@ -82,7 +83,7 @@ TPM_RESULT TPM_SaveAuthContext(TPM_AUTHH
  7.1041 +   BYTE *ptr;
  7.1042 +   UINT32 len;
  7.1043 +   info("TPM_SaveAuthContext()");
  7.1044 +-  res = TPM_SaveContext(authHandle, TPM_RT_KEY, "SaveAuthContext.", 
  7.1045 ++  res = TPM_SaveContext(authHandle, TPM_RT_KEY, (BYTE*)"SaveAuthContext.", 
  7.1046 +                         authContextSize, &contextBlob);
  7.1047 +   if (res != TPM_SUCCESS) return res;
  7.1048 +   len = *authContextSize;
  7.1049 +diff -uprN orig/tpm_emulator-0.2/tpm/tpm_emulator.h vtpm/tpm/tpm_emulator.h
  7.1050 +--- orig/tpm_emulator-0.2/tpm/tpm_emulator.h	2005-08-17 10:58:36.000000000 -0700
  7.1051 ++++ vtpm/tpm/tpm_emulator.h	2005-08-17 10:55:52.000000000 -0700
  7.1052 +@@ -1,5 +1,6 @@
  7.1053 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  7.1054 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  7.1055 ++ * Copyright (C) 2005 INTEL Corp
  7.1056 +  *
  7.1057 +  * This module is free software; you can redistribute it and/or modify
  7.1058 +  * it under the terms of the GNU General Public License as published
  7.1059 +@@ -22,7 +23,8 @@
  7.1060 + /* TPM configuration */
  7.1061 + #define TPM_STORE_TO_FILE       1
  7.1062 + #undef  TPM_STRONG_PERSISTENCE
  7.1063 +-#undef  TPM_GENERATE_EK
  7.1064 ++//#undef  TPM_GENERATE_EK
  7.1065 ++#define  TPM_GENERATE_EK
  7.1066 + 
  7.1067 + /**
  7.1068 +  * tpm_emulator_init - initialises and starts the TPM emulator
  7.1069 +diff -uprN orig/tpm_emulator-0.2/tpm/tpm_integrity.c vtpm/tpm/tpm_integrity.c
  7.1070 +--- orig/tpm_emulator-0.2/tpm/tpm_integrity.c	2005-08-17 10:58:36.000000000 -0700
  7.1071 ++++ vtpm/tpm/tpm_integrity.c	2005-08-17 10:55:52.000000000 -0700
  7.1072 +@@ -1,6 +1,7 @@
  7.1073 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  7.1074 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  7.1075 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
  7.1076 ++ * Copyright (C) 2005 INTEL Corp
  7.1077 +  *
  7.1078 +  * This module is free software; you can redistribute it and/or modify
  7.1079 +  * it under the terms of the GNU General Public License as published
  7.1080 +@@ -194,4 +195,3 @@ TPM_RESULT tpm_verify_pcr(TPM_KEY_DATA *
  7.1081 +   }
  7.1082 +   return TPM_SUCCESS;
  7.1083 + }
  7.1084 +-
  7.1085 +diff -uprN orig/tpm_emulator-0.2/tpm/tpm_structures.h vtpm/tpm/tpm_structures.h
  7.1086 +--- orig/tpm_emulator-0.2/tpm/tpm_structures.h	2005-08-17 10:58:36.000000000 -0700
  7.1087 ++++ vtpm/tpm/tpm_structures.h	2005-08-17 10:55:52.000000000 -0700
  7.1088 +@@ -1,6 +1,7 @@
  7.1089 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  7.1090 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  7.1091 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
  7.1092 ++ * Copyright (C) 2005 INTEL Corp
  7.1093 +  *
  7.1094 +  * This module is free software; you can redistribute it and/or modify
  7.1095 +  * it under the terms of the GNU General Public License as published
  7.1096 +@@ -18,7 +19,7 @@
  7.1097 + #ifndef _TPM_STRUCTURES_H_
  7.1098 + #define _TPM_STRUCTURES_H_
  7.1099 + 
  7.1100 +-#include <linux/types.h>
  7.1101 ++//#include <linux/types.h>
  7.1102 + #include "crypto/rsa.h"
  7.1103 + 
  7.1104 + /*
  7.1105 +diff -uprN orig/tpm_emulator-0.2/tpm/tpm_testing.c vtpm/tpm/tpm_testing.c
  7.1106 +--- orig/tpm_emulator-0.2/tpm/tpm_testing.c	2005-08-17 10:58:36.000000000 -0700
  7.1107 ++++ vtpm/tpm/tpm_testing.c	2005-08-17 10:55:52.000000000 -0700
  7.1108 +@@ -1,6 +1,7 @@
  7.1109 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  7.1110 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  7.1111 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
  7.1112 ++ * Copyright (C) 2005 INTEL Corp
  7.1113 +  *
  7.1114 +  * This module is free software; you can redistribute it and/or modify
  7.1115 +  * it under the terms of the GNU General Public License as published
  7.1116 +@@ -95,24 +96,24 @@ static int tpm_test_sha1(void)
  7.1117 +   struct {
  7.1118 +     uint8_t *data; uint32_t repetitions; uint8_t *digest;
  7.1119 +   } test_cases[] =  {{
  7.1120 +-    "abc", 1,
  7.1121 +-    "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D"
  7.1122 ++	(uint8_t*)"abc", 1,
  7.1123 ++    (uint8_t*)"\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D"
  7.1124 +   }, {
  7.1125 +-    "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
  7.1126 +-    "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1"
  7.1127 ++    (uint8_t*)"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
  7.1128 ++    (uint8_t*)"\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1"
  7.1129 +   }, {
  7.1130 +-    "a", 1000000,
  7.1131 +-    "\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F"
  7.1132 ++    (uint8_t*)"a", 1000000,
  7.1133 ++    (uint8_t*)"\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F"
  7.1134 +   }, {
  7.1135 +-    "0123456701234567012345670123456701234567012345670123456701234567", 10,
  7.1136 +-    "\xDE\xA3\x56\xA2\xCD\xDD\x90\xC7\xA7\xEC\xED\xC5\xEB\xB5\x63\x93\x4F\x46\x04\x52"
  7.1137 ++    (uint8_t*)"0123456701234567012345670123456701234567012345670123456701234567", 10,
  7.1138 ++    (uint8_t*)"\xDE\xA3\x56\xA2\xCD\xDD\x90\xC7\xA7\xEC\xED\xC5\xEB\xB5\x63\x93\x4F\x46\x04\x52"
  7.1139 +   }};
  7.1140 + 
  7.1141 +   debug("tpm_test_sha1()");
  7.1142 +   for (i = 0; i < sizeof(test_cases) / sizeof(test_cases[0]); i++) {
  7.1143 +     sha1_init(&ctx);
  7.1144 +     for (j = 0; j < test_cases[i].repetitions; j++)
  7.1145 +-      sha1_update(&ctx, test_cases[i].data, strlen(test_cases[i].data));
  7.1146 ++      sha1_update(&ctx, test_cases[i].data, strlen((char*)test_cases[i].data));
  7.1147 +     sha1_final(&ctx, digest);
  7.1148 +     if (memcmp(digest, test_cases[i].digest, SHA1_DIGEST_LENGTH) != 0) return -1;
  7.1149 +   }
  7.1150 +@@ -128,41 +129,41 @@ static int tpm_test_hmac(void)
  7.1151 +   struct {
  7.1152 +     uint8_t *key, key_len, *data, data_len, *digest;
  7.1153 +   } test_cases[] = {{
  7.1154 +-    "\x0b", 20, "Hi There", 8,
  7.1155 +-    "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00"
  7.1156 ++    (uint8_t*)"\x0b", 20, (uint8_t*)"Hi There", 8,
  7.1157 ++    (uint8_t*)"\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00"
  7.1158 +   }, {
  7.1159 +-    "Jefe", 4, "what do ya want for nothing?", 28,
  7.1160 +-    "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79"
  7.1161 ++    (uint8_t*)"Jefe", 4, (uint8_t*)"what do ya want for nothing?", 28,
  7.1162 ++    (uint8_t*)"\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79"
  7.1163 +   }, {
  7.1164 +-    "\xaa", 20, "\xdd", 50,
  7.1165 +-    "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3"
  7.1166 ++    (uint8_t*)"\xaa", 20, (uint8_t*)"\xdd", 50,
  7.1167 ++    (uint8_t*)"\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3"
  7.1168 +   }, {
  7.1169 +-    "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
  7.1170 +-    "\x15\x16\x17\x18\x19", 25, "\xcd", 50,
  7.1171 +-    "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda"
  7.1172 ++    (uint8_t*)"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
  7.1173 ++    "\x15\x16\x17\x18\x19", 25, (uint8_t*)"\xcd", 50,
  7.1174 ++    (uint8_t*)"\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda"
  7.1175 +   }, {
  7.1176 +-    "\x0c", 20, "Test With Truncation", 20,
  7.1177 +-    "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04"
  7.1178 ++    (uint8_t*)"\x0c", 20, (uint8_t*)"Test With Truncation", 20,
  7.1179 ++    (uint8_t*)"\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04"
  7.1180 +   }, {
  7.1181 +-    "\xaa", 80, "Test Using Larger Than Block-Size Key - Hash Key First", 54,
  7.1182 +-    "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12"
  7.1183 ++    (uint8_t*)"\xaa", 80, (uint8_t*)"Test Using Larger Than Block-Size Key - Hash Key First", 54,
  7.1184 ++    (uint8_t*)"\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12"
  7.1185 +   }, {
  7.1186 +-    "\xaa", 80,
  7.1187 +-    "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data", 73,
  7.1188 +-    "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91"
  7.1189 ++    (uint8_t*)"\xaa", 80,
  7.1190 ++    (uint8_t*)"Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data", 73,
  7.1191 ++    (uint8_t*)"\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91"
  7.1192 +   }};
  7.1193 + 
  7.1194 +   debug("tpm_test_hmac()");
  7.1195 +   for (i = 0; i < sizeof(test_cases) / sizeof(test_cases[0]); i++) {
  7.1196 +-    if (strlen(test_cases[i].key) < test_cases[i].key_len) {
  7.1197 ++    if (strlen((char*)test_cases[i].key) < test_cases[i].key_len) {
  7.1198 +       uint8_t key[test_cases[i].key_len];
  7.1199 +       memset(key, test_cases[i].key[0], test_cases[i].key_len);
  7.1200 +       hmac_init(&ctx, key, test_cases[i].key_len);
  7.1201 +     } else {
  7.1202 +       hmac_init(&ctx, test_cases[i].key, test_cases[i].key_len);
  7.1203 +     }
  7.1204 +-    for (j = 0; j < test_cases[i].data_len; j += strlen(test_cases[i].data)) {
  7.1205 +-      hmac_update(&ctx, test_cases[i].data, strlen(test_cases[i].data));
  7.1206 ++    for (j = 0; j < test_cases[i].data_len; j += strlen((char*)test_cases[i].data)) {
  7.1207 ++      hmac_update(&ctx, test_cases[i].data, strlen((char*)test_cases[i].data));
  7.1208 +     }
  7.1209 +     hmac_final(&ctx, digest);
  7.1210 +     if (memcmp(digest, test_cases[i].digest, SHA1_DIGEST_LENGTH) != 0) return -1;
  7.1211 +@@ -173,9 +174,9 @@ static int tpm_test_hmac(void)
  7.1212 + static int tpm_test_rsa_EK(void)
  7.1213 + {
  7.1214 +   int res = 0;
  7.1215 +-  char *data = "RSA PKCS #1 v1.5 Test-String";
  7.1216 ++  uint8_t *data = (uint8_t*)"RSA PKCS #1 v1.5 Test-String";
  7.1217 +   uint8_t buf[256];
  7.1218 +-  size_t buf_len, data_len = strlen(data);
  7.1219 ++  size_t buf_len, data_len = strlen((char*)data);
  7.1220 +   rsa_private_key_t priv_key;
  7.1221 +   rsa_public_key_t pub_key;
  7.1222 + 
  7.1223 +diff -uprN orig/tpm_emulator-0.2/tpm/tpm_ticks.c vtpm/tpm/tpm_ticks.c
  7.1224 +--- orig/tpm_emulator-0.2/tpm/tpm_ticks.c	2005-08-17 10:58:36.000000000 -0700
  7.1225 ++++ vtpm/tpm/tpm_ticks.c	2005-08-17 10:55:52.000000000 -0700
  7.1226 +@@ -1,6 +1,7 @@
  7.1227 + /* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  7.1228 +  * Copyright (C) 2004 Mario Strasser <mast@gmx.net>,
  7.1229 +  *                    Swiss Federal Institute of Technology (ETH) Zurich
  7.1230 ++ * Copyright (C) 2005 INTEL Corp
  7.1231 +  *
  7.1232 +  * This module is free software; you can redistribute it and/or modify
  7.1233 +  * it under the terms of the GNU General Public License as published
  7.1234 +@@ -37,9 +38,7 @@ TPM_RESULT TPM_SetTickType(TPM_TICKTYPE 
  7.1235 + TPM_RESULT TPM_GetTicks(TPM_CURRENT_TICKS *currentTime)
  7.1236 + {
  7.1237 +   info("TPM_GetTicks()");
  7.1238 +-  memcpy(currentTime, &tpmData.stany.data.currentTicks, 
  7.1239 +-    sizeof(TPM_CURRENT_TICKS));
  7.1240 +-  return TPM_SUCCESS;
  7.1241 ++  return TPM_DISABLED_CMD;
  7.1242 + }
  7.1243 + 
  7.1244 + TPM_RESULT TPM_TickStampBlob(TPM_KEY_HANDLE keyHandle, TPM_NONCE *antiReplay,
  7.1245 +@@ -47,61 +46,12 @@ TPM_RESULT TPM_TickStampBlob(TPM_KEY_HAN
  7.1246 +                              TPM_CURRENT_TICKS *currentTicks, 
  7.1247 +                              UINT32 *sigSize, BYTE **sig)
  7.1248 + {
  7.1249 +-  TPM_RESULT res;
  7.1250 +-  TPM_KEY_DATA *key;
  7.1251 +-  BYTE *info, *p;
  7.1252 +-  UINT32 info_length, length;
  7.1253 +   info("TPM_TickStampBlob()");
  7.1254 +-  /* get key */
  7.1255 +-  key = tpm_get_key(keyHandle);
  7.1256 +-  if (key == NULL) return TPM_INVALID_KEYHANDLE;
  7.1257 +-  /* verify authorization */ 
  7.1258 +-  res = tpm_verify_auth(auth1, key->usageAuth, keyHandle);
  7.1259 +-  if (res != TPM_SUCCESS) return res;
  7.1260 +-  if (key->keyUsage != TPM_KEY_SIGNING && key->keyUsage != TPM_KEY_LEGACY
  7.1261 +-      && key->keyUsage != TPM_KEY_IDENTITY) return TPM_INVALID_KEYUSAGE;
  7.1262 +-  /* get current ticks */
  7.1263 +-  TPM_GetTicks(currentTicks);
  7.1264 +-  /* sign data using signature scheme PKCS1_SHA1 and TPM_SIGN_INFO container */
  7.1265 +-  *sigSize = key->key.size >> 3;
  7.1266 +-  *sig = tpm_malloc(*sigSize);
  7.1267 +-  if (*sig == NULL) return TPM_FAIL; 
  7.1268 +-  /* setup TPM_SIGN_INFO structure */
  7.1269 +-  info_length = 30 + sizeof(TPM_DIGEST) + sizeof_TPM_CURRENT_TICKS(currentTicks);
  7.1270 +-  info = tpm_malloc(info_length);
  7.1271 +-  if (info == NULL) {
  7.1272 +-    tpm_free(*sig);
  7.1273 +-    return TPM_FAIL;
  7.1274 +-  }
  7.1275 +-  memcpy(&info[0], "\x05\x00TSTP", 6);
  7.1276 +-  memcpy(&info[6], antiReplay->nonce, 20);
  7.1277 +-  *(UINT32*)&info[26] = cpu_to_be32(20
  7.1278 +-                        + sizeof_TPM_CURRENT_TICKS(currentTicks));
  7.1279 +-  memcpy(&info[30], digestToStamp->digest, sizeof(TPM_DIGEST));
  7.1280 +-  p = &info[30 + sizeof(TPM_DIGEST)]; 
  7.1281 +-  length = sizeof_TPM_CURRENT_TICKS(currentTicks);
  7.1282 +-  if (tpm_marshal_TPM_CURRENT_TICKS(&p, &length, currentTicks)
  7.1283 +-      || rsa_sign(&key->key, RSA_SSA_PKCS1_SHA1, info, info_length, *sig)) {   
  7.1284 +-    tpm_free(*sig);
  7.1285 +-    tpm_free(info);
  7.1286 +-    return TPM_FAIL;
  7.1287 +-  } 
  7.1288 +-  return TPM_SUCCESS;
  7.1289 ++  return TPM_DISABLED_CMD;
  7.1290 + }
  7.1291 + 
  7.1292 + void tpm_update_ticks(void)
  7.1293 + {
  7.1294 +-  if (tpmData.stany.data.currentTicks.tag == 0) {
  7.1295 +-    tpmData.stany.data.currentTicks.tag = TPM_TAG_CURRENT_TICKS;
  7.1296 +-    tpmData.stany.data.currentTicks.currentTicks += tpm_get_ticks();
  7.1297 +-    tpmData.stany.data.currentTicks.tickType = tpmData.permanent.data.tickType;
  7.1298 +-    tpm_get_random_bytes(tpmData.stany.data.currentTicks.tickNonce.nonce, 
  7.1299 +-      sizeof(TPM_NONCE));
  7.1300 +-    tpmData.stany.data.currentTicks.tickRate = 1;
  7.1301 +-    tpmData.stany.data.currentTicks.tickSecurity = TICK_SEC_NO_CHECK;
  7.1302 +-  } else {
  7.1303 +-    tpmData.stany.data.currentTicks.currentTicks += tpm_get_ticks();   
  7.1304 +-  }
  7.1305 + }
  7.1306 +   
  7.1307 + 
  7.1308 +diff -uprN orig/tpm_emulator-0.2/tpm/vtpm_manager.h vtpm/tpm/vtpm_manager.h
  7.1309 +--- orig/tpm_emulator-0.2/tpm/vtpm_manager.h	1969-12-31 16:00:00.000000000 -0800
  7.1310 ++++ vtpm/tpm/vtpm_manager.h	2005-08-17 10:55:52.000000000 -0700
  7.1311 +@@ -0,0 +1,126 @@
  7.1312 ++// ===================================================================
  7.1313 ++// 
  7.1314 ++// Copyright (c) 2005, Intel Corp.
  7.1315 ++// All rights reserved.
  7.1316 ++//
  7.1317 ++// Redistribution and use in source and binary forms, with or without 
  7.1318 ++// modification, are permitted provided that the following conditions 
  7.1319 ++// are met:
  7.1320 ++//
  7.1321 ++//   * Redistributions of source code must retain the above copyright 
  7.1322 ++//     notice, this list of conditions and the following disclaimer.
  7.1323 ++//   * Redistributions in binary form must reproduce the above 
  7.1324 ++//     copyright notice, this list of conditions and the following 
  7.1325 ++//     disclaimer in the documentation and/or other materials provided 
  7.1326 ++//     with the distribution.
  7.1327 ++//   * Neither the name of Intel Corporation nor the names of its 
  7.1328 ++//     contributors may be used to endorse or promote products derived
  7.1329 ++//     from this software without specific prior written permission.
  7.1330 ++//
  7.1331 ++// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  7.1332 ++// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  7.1333 ++// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  7.1334 ++// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  7.1335 ++// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  7.1336 ++// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  7.1337 ++// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  7.1338 ++// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  7.1339 ++// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  7.1340 ++// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  7.1341 ++// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  7.1342 ++// OF THE POSSIBILITY OF SUCH DAMAGE.
  7.1343 ++// ===================================================================
  7.1344 ++// 
  7.1345 ++// vtpm_manager.h
  7.1346 ++// 
  7.1347 ++//  Public Interface header for VTPM Manager
  7.1348 ++//
  7.1349 ++// ==================================================================
  7.1350 ++
  7.1351 ++#ifndef __VTPM_MANAGER_H__
  7.1352 ++#define __VTPM_MANAGER_H__
  7.1353 ++
  7.1354 ++#define VTPM_TAG_REQ 0x01c1
  7.1355 ++#define VTPM_TAG_RSP 0x01c4
  7.1356 ++#define COMMAND_BUFFER_SIZE 4096
  7.1357 ++
  7.1358 ++// Header sizes. Note Header MAY include the DMI
  7.1359 ++#define VTPM_COMMAND_HEADER_SIZE_SRV ( sizeof(UINT32) + sizeof(TPM_TAG) + sizeof(UINT32) + sizeof(TPM_COMMAND_CODE))
  7.1360 ++#define VTPM_COMMAND_HEADER_SIZE_CLT (                  sizeof(TPM_TAG) + sizeof(UINT32) + sizeof(TPM_COMMAND_CODE))
  7.1361 ++
  7.1362 ++//************************ Command Codes ****************************
  7.1363 ++#define VTPM_ORD_OPEN              1   // ULM Creates New DMI
  7.1364 ++#define VTPM_ORD_CLOSE             2   // ULM Closes a DMI
  7.1365 ++#define VTPM_ORD_DELETE            3   // ULM Permemently Deletes DMI
  7.1366 ++#define VTPM_ORD_SAVENVM          4   // DMI requests Secrets Unseal
  7.1367 ++#define VTPM_ORD_LOADNVM          5   // DMI requests Secrets Saved
  7.1368 ++#define VTPM_ORD_TPMCOMMAND       6   // DMI issues HW TPM Command
  7.1369 ++
  7.1370 ++//************************ Return Codes ****************************
  7.1371 ++#define VTPM_SUCCESS               0
  7.1372 ++#define VTPM_FAIL                  1
  7.1373 ++#define VTPM_UNSUPPORTED           2
  7.1374 ++#define VTPM_FORBIDDEN             3
  7.1375 ++#define VTPM_RESTORE_CONTEXT_FAILED    4
  7.1376 ++#define VTPM_INVALID_REQUEST       5
  7.1377 ++
  7.1378 ++/******************* Command Parameter API *************************
  7.1379 ++
  7.1380 ++VTPM Command Format
  7.1381 ++  dmi: 4 bytes                  // Source of message. 
  7.1382 ++                                // WARNING: This is prepended by the channel. 
  7.1383 ++                                // Thus it is received by VTPM Manager, 
  7.1384 ++                                // but not sent by DMI
  7.1385 ++  tpm tag: 2 bytes
  7.1386 ++  command size: 4 bytes         // Size of command including header but not DMI
  7.1387 ++  ord: 4 bytes                  // Command ordinal above
  7.1388 ++  parameters: size - 10 bytes   // Command Parameter
  7.1389 ++
  7.1390 ++VTPM Response Format
  7.1391 ++  tpm tag: 2 bytes
  7.1392 ++  response_size: 4 bytes
  7.1393 ++  status: 4 bytes         
  7.1394 ++  parameters: size - 10 bytes
  7.1395 ++
  7.1396 ++
  7.1397 ++VTPM_Open:
  7.1398 ++  Input Parameters:
  7.1399 ++    Domain_type: 1 byte
  7.1400 ++    domain_id: 4 bytes
  7.1401 ++    instance_id: 4 bytes
  7.1402 ++  Output Parameters:
  7.1403 ++    None
  7.1404 ++    
  7.1405 ++VTPM_Close
  7.1406 ++  Input Parameters:
  7.1407 ++    instance_id: 4 bytes
  7.1408 ++  Output Parameters:
  7.1409 ++    None
  7.1410 ++
  7.1411 ++VTPM_Delete
  7.1412 ++  Input Parameters:
  7.1413 ++    instance_id: 4 bytes
  7.1414 ++  Output Parameters:
  7.1415 ++    None
  7.1416 ++
  7.1417 ++VTPM_SaveNVM
  7.1418 ++  Input Parameters:
  7.1419 ++    data: n bytes (Header indicates size of data)
  7.1420 ++  Output Parameters:
  7.1421 ++    None
  7.1422 ++
  7.1423 ++VTPM_LoadNVM
  7.1424 ++  Input Parameters:
  7.1425 ++    None
  7.1426 ++  Output Parameters:
  7.1427 ++    data: n bytes (Header indicates size of data)
  7.1428 ++
  7.1429 ++VTPM_TPMCommand
  7.1430 ++  Input Parameters:
  7.1431 ++    TPM Command Byte Stream: n bytes 
  7.1432 ++  Output Parameters:
  7.1433 ++    TPM Reponse Byte Stream: n bytes 
  7.1434 ++
  7.1435 ++*********************************************************************/
  7.1436 ++
  7.1437 ++#endif //_VTPM_MANAGER_H_
  7.1438 +diff -uprN orig/tpm_emulator-0.2/tpmd.c vtpm/tpmd.c
  7.1439 +--- orig/tpm_emulator-0.2/tpmd.c	1969-12-31 16:00:00.000000000 -0800
  7.1440 ++++ vtpm/tpmd.c	2005-08-17 10:55:52.000000000 -0700
  7.1441 +@@ -0,0 +1,207 @@
  7.1442 ++/* Software-Based Trusted Platform Module (TPM) Emulator for Linux
  7.1443 ++ * Copyright (C) 2005 INTEL Corp
  7.1444 ++ *
  7.1445 ++ * This module is free software; you can redistribute it and/or modify
  7.1446 ++ * it under the terms of the GNU General Public License as published
  7.1447 ++ * by the Free Software Foundation; either version 2 of the License,
  7.1448 ++ * or (at your option) any later version.
  7.1449 ++ *
  7.1450 ++ * This module is distributed in the hope that it will be useful,
  7.1451 ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7.1452 ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  7.1453 ++ * GNU General Public License for more details.
  7.1454 ++ *
  7.1455 ++ */
  7.1456 ++
  7.1457 ++#include <stdio.h>
  7.1458 ++#include <stdlib.h>
  7.1459 ++#include <unistd.h>
  7.1460 ++#include <string.h>
  7.1461 ++#include <sys/types.h>
  7.1462 ++#include <sys/stat.h>
  7.1463 ++#include <fcntl.h>
  7.1464 ++#include <sys/time.h>
  7.1465 ++
  7.1466 ++#include "tpm_emulator.h"
  7.1467 ++#include "vtpm_manager.h"
  7.1468 ++
  7.1469 ++#ifdef VTPM_MULTI_VM
  7.1470 ++ #define DEV_BE "/dev/vtpm"
  7.1471 ++#else
  7.1472 ++ #define GUEST_RX_FIFO_D "/var/vtpm/fifos/guest-to-%d.fifo"
  7.1473 ++ #define GUEST_TX_FIFO "/var/vtpm/fifos/guest-from-all.fifo"
  7.1474 ++
  7.1475 ++ int dmi_id;
  7.1476 ++#endif
  7.1477 ++						
  7.1478 ++#define BUFFER_SIZE 2048
  7.1479 ++
  7.1480 ++static uint8_t ctrl_msg[] = { 0, 0, 0, 0,   // destination
  7.1481 ++                              1, 193,       // VTPM_TAG
  7.1482 ++                              0, 0, 0, 10,  // Size
  7.1483 ++                              0, 0, 0, 0};  // TPM_SUCCESS
  7.1484 ++                            
  7.1485 ++
  7.1486 ++static int devurandom=0;
  7.1487 ++
  7.1488 ++	  
  7.1489 ++void get_random_bytes(void *buf, int nbytes) {
  7.1490 ++  
  7.1491 ++  if (devurandom == 0) {
  7.1492 ++    devurandom = open("/dev/urandom", O_RDONLY);
  7.1493 ++  }
  7.1494 ++
  7.1495 ++  if (read(devurandom, buf, nbytes) != nbytes) {
  7.1496 ++      printf("Can't get random number.\n");
  7.1497 ++      exit(-1);
  7.1498 ++  }
  7.1499 ++}
  7.1500 ++
  7.1501 ++uint64_t tpm_get_ticks(void)
  7.1502 ++{
  7.1503 ++  //struct timeval tv;
  7.1504 ++  //int gettimeofday(&tv, struct timezone *tz);
  7.1505 ++  return 0;
  7.1506 ++}
  7.1507 ++
  7.1508 ++int main(int argc, char **argv)
  7.1509 ++{
  7.1510 ++  uint8_t in[BUFFER_SIZE], *out, *addressed_out;
  7.1511 ++  uint32_t out_size;
  7.1512 ++  int in_size, written ;
  7.1513 ++  int i, guest_id=-1;
  7.1514 ++ 
  7.1515 ++  int vtpm_tx_fh=-1, vtpm_rx_fh=-1;
  7.1516 ++#ifdef VTPM_MULTI_VM
  7.1517 ++  if (argc < 2) {
  7.1518 ++    printf("Usage: tpmd clear|save|deactivated\n" );
  7.1519 ++#else
  7.1520 ++  if (argc < 3) {
  7.1521 ++    printf("Usage: tpmd clear|save|deactivated vtpmid\n" );
  7.1522 ++#endif
  7.1523 ++	  return -1;
  7.1524 ++  }
  7.1525 ++
  7.1526 ++#ifndef VTPM_MULTI_VM
  7.1527 ++  dmi_id = atoi(argv[2]);
  7.1528 ++#endif
  7.1529 ++
  7.1530 ++  /* initialize TPM emulator */
  7.1531 ++  if (!strcmp(argv[1], "clear")) {
  7.1532 ++    printf("Initializing tpm: %s\n", argv[1]);
  7.1533 ++    tpm_emulator_init(1);
  7.1534 ++  } else if (!strcmp(argv[1], "save")) { 
  7.1535 ++    printf("Initializing tpm: %s\n", argv[1]);
  7.1536 ++    tpm_emulator_init(2);
  7.1537 ++  } else if (!strcmp(argv[1], "deactivated")) {
  7.1538 ++    printf("Initializing tpm: %s\n", argv[1]);
  7.1539 ++    tpm_emulator_init(3);
  7.1540 ++  } else {
  7.1541 ++    printf("invalid startup mode '%s'; must be 'clear', "
  7.1542 ++      "'save' (default) or 'deactivated", argv[1]);
  7.1543 ++    return -1;
  7.1544 ++  }
  7.1545 ++
  7.1546 ++  char *guest_rx_file = malloc(10 + strlen(GUEST_RX_FIFO_D));
  7.1547 ++  sprintf(guest_rx_file, GUEST_RX_FIFO_D, (uint32_t) dmi_id);
  7.1548 ++  
  7.1549 ++  while (1) {
  7.1550 ++abort_command:
  7.1551 ++    if (vtpm_rx_fh < 0) {
  7.1552 ++#ifdef VTPM_MUTLI_VM
  7.1553 ++	  vtpm_rx_fh = open(DEV_BE, O_RDWR);
  7.1554 ++#else
  7.1555 ++      vtpm_rx_fh = open(guest_rx_file, O_RDONLY);
  7.1556 ++#endif
  7.1557 ++    }
  7.1558 ++    
  7.1559 ++    if (vtpm_rx_fh < 0) {
  7.1560 ++      printf("ERROR: failed to open devices to listen to guest.\n");
  7.1561 ++      return -1;
  7.1562 ++    }
  7.1563 ++    
  7.1564 ++    in_size = read(vtpm_rx_fh, in, BUFFER_SIZE);
  7.1565 ++    if (in_size < 6) { // Magic size of minium TPM command
  7.1566 ++      printf("Recv[%d] to small: 0x", in_size);
  7.1567 ++      if (in_size <= 0) {
  7.1568 ++          close(vtpm_rx_fh);
  7.1569 ++          vtpm_rx_fh = -1;
  7.1570 ++          goto abort_command;
  7.1571 ++      }
  7.1572 ++    } else { 
  7.1573 ++      printf("Recv[%d]: 0x", in_size);
  7.1574 ++      for (i=0; i< in_size; i++) 
  7.1575 ++        printf("%x ", in[i]);
  7.1576 ++      printf("\n");
  7.1577 ++    }
  7.1578 ++
  7.1579 ++    if (guest_id == -1) {
  7.1580 ++        guest_id = *((uint32_t *) in);
  7.1581 ++        *((uint32_t *) ctrl_msg) = *((uint32_t *) in);
  7.1582 ++    } else {
  7.1583 ++        if (guest_id != *((uint32_t *) in) ) {
  7.1584 ++            printf("WARNING: More than one guest attached\n");
  7.1585 ++        }
  7.1586 ++    }
  7.1587 ++
  7.1588 ++    if (vtpm_tx_fh < 0) {
  7.1589 ++#ifdef VTPM_MUTLI_VM
  7.1590 ++	  vtpm_tx_fh = open(DEV_BE, O_RDWR);
  7.1591 ++	  vtpm_rx_fh = vtpm_tx_fh;
  7.1592 ++#else
  7.1593 ++      vtpm_tx_fh = open(GUEST_TX_FIFO, O_WRONLY);
  7.1594 ++#endif
  7.1595 ++    }
  7.1596 ++
  7.1597 ++    if (vtpm_tx_fh < 0) {
  7.1598 ++      printf("ERROR: failed to open devices to respond to guest.\n");
  7.1599 ++      return -1;
  7.1600 ++    }
  7.1601 ++    
  7.1602 ++    // Handle command, but we need to skip the identifier
  7.1603 ++    if (  BE16_TO_CPU( ((uint16_t *) in)[2] ) == VTPM_TAG_REQ ) { // Control message from xend
  7.1604 ++      // This DM doesn't really care about ctrl messages. Just ACK the message
  7.1605 ++      written = write(vtpm_tx_fh, ctrl_msg, sizeof(ctrl_msg));
  7.1606 ++
  7.1607 ++      if (written != sizeof(ctrl_msg)) {
  7.1608 ++        printf("ERROR: Part of response not written %d/%d.\n", written, sizeof(ctrl_msg));
  7.1609 ++      } else {
  7.1610 ++        printf("Send Ctrl Message confermation\n");
  7.1611 ++      }
  7.1612 ++    } else { // Message from Guest
  7.1613 ++      if (tpm_handle_command(in + sizeof(uint32_t), in_size - sizeof(uint32_t), &out, &out_size) != 0) { 
  7.1614 ++        printf("ERROR: Handler Failed.\n");
  7.1615 ++      }
  7.1616 ++
  7.1617 ++      addressed_out = (uint8_t *) tpm_malloc(sizeof(uint32_t) + out_size);
  7.1618 ++      *(uint32_t *) addressed_out = *(uint32_t *) in;
  7.1619 ++      memcpy(addressed_out + sizeof(uint32_t), out, out_size);
  7.1620 ++
  7.1621 ++      written = write(vtpm_tx_fh, addressed_out, out_size + sizeof(uint32_t));
  7.1622 ++
  7.1623 ++      if (written != out_size + sizeof(uint32_t)) {
  7.1624 ++        printf("ERROR: Part of response not written %d/%d.\n", written, out_size);
  7.1625 ++        for (i=0; i< out_size+ sizeof(uint32_t); i++)
  7.1626 ++          printf("%x ", addressed_out[i]);
  7.1627 ++        printf("\n");
  7.1628 ++      } else {
  7.1629 ++        printf("Sent[%d]: ", out_size + sizeof(uint32_t));
  7.1630 ++        for (i=0; i< out_size+ sizeof(uint32_t); i++)
  7.1631 ++          printf("%x ", addressed_out[i]);
  7.1632 ++        printf("\n");
  7.1633 ++      }
  7.1634 ++      tpm_free(out);
  7.1635 ++      tpm_free(addressed_out);
  7.1636 ++    }
  7.1637 ++
  7.1638 ++  } // loop
  7.1639 ++
  7.1640 ++  tpm_emulator_shutdown();
  7.1641 ++
  7.1642 ++  close(vtpm_tx_fh);
  7.1643 ++#ifndef VTPM_MUTLI_VM
  7.1644 ++  close(vtpm_rx_fh);
  7.1645 ++  free (guest_rx_file);
  7.1646 ++#endif
  7.1647 ++
  7.1648 ++}