debuggers.hg

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