debuggers.hg

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

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