ia64/xen-unstable

view tools/vtpm_manager/tcs/transmit.c @ 6979:f8e7af29daa1

merge?
author cl349@firebug.cl.cam.ac.uk
date Tue Sep 20 09:43:46 2005 +0000 (2005-09-20)
parents 3233e7ecfa9f c0796e18b6a4
children 06d84bf87159
line source
1 // ===================================================================
2 //
3 // Copyright (c) 2005, Intel Corp.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 // * Neither the name of Intel Corporation nor the names of its
17 // contributors may be used to endorse or promote products derived
18 // from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 // OF THE POSSIBILITY OF SUCH DAMAGE.
32 // ===================================================================
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <fcntl.h>
39 #include "tcg.h"
40 #include "buffer.h"
41 #include "log.h"
42 #include "tpmddl.h"
44 // flag to track whether TDDL has been opened
45 static int g_TDDL_open = 0;
46 static int g_fd = -1; // the fd to the TPM
48 TPM_RESULT
49 TDDL_TransmitData( TDDL_BYTE* in,
50 TDDL_UINT32 insize,
51 TDDL_BYTE* out,
52 TDDL_UINT32* outsize) {
53 TPM_RESULT status = TPM_SUCCESS;
54 TDDL_UINT32 i;
56 vtpmloginfo(VTPM_LOG_TXDATA, "Sending buffer = 0x");
57 for(i = 0 ; i < insize ; i++)
58 vtpmloginfomore(VTPM_LOG_TXDATA, "%2.2x ", in[i]);
60 vtpmloginfomore(VTPM_LOG_TXDATA, "\n");
62 ssize_t size = 0;
63 int fd = g_fd;
65 // send the request
66 size = write (fd, in, insize);
67 if (size < 0) {
68 vtpmlogerror(VTPM_LOG_TXDATA, "write() failed");
69 ERRORDIE (TPM_IOERROR);
70 }
71 else if ((TDDL_UINT32) size < insize) {
72 vtpmlogerror(VTPM_LOG_TXDATA, "Wrote %d instead of %d bytes!\n", (int) size, insize);
73 // ... ?
74 }
76 // read the response
77 size = read (fd, out, TCPA_MAX_BUFFER_LENGTH);
78 if (size < 0) {
79 vtpmlogerror(VTPM_LOG_TXDATA, "read() failed");
80 ERRORDIE (TPM_IOERROR);
81 }
83 vtpmloginfo(VTPM_LOG_TXDATA, "Receiving buffer = 0x");
84 for(i = 0 ; i < size ; i++)
85 vtpmloginfomore(VTPM_LOG_TXDATA, "%2.2x ", out[i]);
87 vtpmloginfomore(VTPM_LOG_TXDATA, "\n");
89 *outsize = size;
90 // close connection
91 goto egress;
93 abort_egress:
94 egress:
95 return status;
96 }
98 TPM_RESULT TDDL_Open() {
100 TDDL_RESULT status = TDDL_SUCCESS;
101 int fd = -1;
103 if (g_TDDL_open)
104 return TPM_FAIL;
106 fd = open ("/dev/tpm0", O_RDWR);
107 if (fd < 0) {
108 vtpmlogerror(VTPM_LOG_TXDATA, "TPM open failed");
109 return TPM_IOERROR;
110 }
112 g_fd = fd;
113 g_TDDL_open = 1;
115 return status;
116 }
118 void TDDL_Close() {
119 if (! g_TDDL_open)
120 return;
122 if (g_fd>= 0) {
123 if (close(g_fd) < 0)
124 vtpmlogerror(VTPM_LOG_TXDATA, "closeing tpm failed");
126 g_fd = -1;
127 }
129 g_TDDL_open = 0;
131 }