source: trunk/packages/xen-3.1/xen-3.1/tools/vtpm_manager/migration/vtpm_manager_if.c @ 34

Last change on this file since 34 was 34, checked in by hartmans, 17 years ago

Add xen and xen-common

  • Property svn:mime-type set to text/cpp
File size: 6.4 KB
Line 
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// ===================================================================
33//
34// vtpm_manager_if.c
35//
36//  Provides functions to call local vtpm manager interface (Hotplug)
37//
38// ==================================================================
39
40#include <stdio.h>
41#include <fcntl.h>
42#include <malloc.h>
43#include <string.h>
44
45#include "tcg.h"
46#include "buffer.h"
47#include "log.h"
48#include "vtpm_ipc.h"
49#include "bsg.h"
50#include "vtpm_migrator.h"
51#include "vtpm_manager.h"
52
53#define VTPM_TX_HP_FNAME       "/var/vtpm/fifos/from_console.fifo"
54#define VTPM_RX_HP_FNAME       "/var/vtpm/fifos/to_console.fifo"
55
56static vtpm_ipc_handle_t tx_ipc_h, rx_ipc_h;
57
58TPM_RESULT vtpm_manager_open(){
59
60  if ( (vtpm_ipc_init(&tx_ipc_h,  VTPM_TX_HP_FNAME, O_RDWR, TRUE) != 0) ||  //FIXME: wronly
61       (vtpm_ipc_init(&rx_ipc_h,  VTPM_RX_HP_FNAME, O_RDWR, TRUE) != 0) ) { //FIXME: rdonly
62    vtpmlogerror(VTPM_LOG_VTPM, "Unable to connect to vtpm_manager.\n");
63    return TPM_IOERROR;
64  }
65
66  return TPM_SUCCESS;
67}
68
69void vtpm_manager_close() {
70
71  vtpm_ipc_close(&tx_ipc_h);
72  vtpm_ipc_close(&rx_ipc_h);
73}
74
75
76TPM_RESULT vtpm_manager_command(TPM_COMMAND_CODE ord,
77                                buffer_t *command_param_buf,
78                                TPM_RESULT *cmd_status, /* out */
79                                buffer_t *result_param_buf) {
80
81  TPM_RESULT status = TPM_FAIL;
82  int  size_read, size_write, i;
83  BYTE *adj_command, response_header[VTPM_COMMAND_HEADER_SIZE_SRV];
84  UINT32 dmi_id=0, adj_command_size, out_param_size, adj_param_size;
85  TPM_TAG tag=VTPM_TAG_REQ;
86
87  if ( (!command_param_buf) || (!result_param_buf) || (!cmd_status) ) {
88    status = TPM_BAD_PARAMETER;
89    goto abort_egress;
90  }   
91 
92  adj_command_size = VTPM_COMMAND_HEADER_SIZE_SRV + buffer_len(command_param_buf);
93  adj_command = (BYTE *) malloc( adj_command_size );
94  if (!adj_command) {
95    status = TPM_RESOURCES;
96    goto abort_egress;
97  }
98
99  out_param_size = VTPM_COMMAND_HEADER_SIZE + buffer_len(command_param_buf);
100  BSG_PackList(adj_command, 4,
101                 BSG_TYPE_UINT32, &dmi_id,
102                 BSG_TPM_TAG, &tag,
103                 BSG_TYPE_UINT32, &out_param_size,
104                 BSG_TPM_COMMAND_CODE, &ord );
105
106  memcpy(adj_command + VTPM_COMMAND_HEADER_SIZE_SRV, command_param_buf->bytes, buffer_len(command_param_buf));
107
108  size_write = vtpm_ipc_write(&tx_ipc_h, NULL, adj_command, adj_command_size);
109
110  if (size_write > 0) {
111    vtpmloginfo(VTPM_LOG_VTPM_DEEP, "SENT (MGR): 0x");
112    for (i=0; i< adj_command_size; i++) {
113      vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", adj_command[i]);
114    }
115    vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
116  } else {
117    vtpmlogerror(VTPM_LOG_VTPM, "Error writing VTPM Manager console.\n");
118    status = TPM_IOERROR;
119    goto abort_egress;
120  }
121
122  if (size_write != (int) adj_command_size )
123    vtpmlogerror(VTPM_LOG_VTPM, "Could not write entire command to mgr (%d/%d)\n", size_write, adj_command_size);
124
125  // Read header for response to manager command
126  size_read = vtpm_ipc_read(&rx_ipc_h, NULL, response_header, VTPM_COMMAND_HEADER_SIZE_SRV);
127  if (size_read > 0) {
128    vtpmloginfo(VTPM_LOG_VTPM_DEEP, "RECV (MGR): 0x");
129    for (i=0; i<size_read; i++)
130      vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", response_header[i]);
131
132  } else {
133    vtpmlogerror(VTPM_LOG_VTPM, "Error reading from vtpm manager.\n");
134    status = TPM_IOERROR;
135    goto abort_egress;
136  }
137
138  if (size_read < (int) VTPM_COMMAND_HEADER_SIZE_SRV) {
139    vtpmlogerror(VTPM_LOG_VTPM, "Command from vtpm_manager shorter than std header.\n");
140    status = TPM_IOERROR;
141    goto abort_egress;
142  }
143
144  // Unpack response from DMI for TPM command
145  BSG_UnpackList(response_header, 4,
146                 BSG_TYPE_UINT32, &dmi_id,
147                 BSG_TPM_TAG, &tag,
148                 BSG_TYPE_UINT32, &out_param_size,
149                 BSG_TPM_COMMAND_CODE, cmd_status );
150
151  // If response has parameters, read them.
152  // Note that out_param_size is in the client's context
153  adj_param_size = out_param_size - VTPM_COMMAND_HEADER_SIZE;
154  if (adj_param_size > 0) {
155    TPMTRYRETURN( buffer_init( result_param_buf, adj_param_size, NULL) );
156    size_read = vtpm_ipc_read(&rx_ipc_h, NULL, result_param_buf->bytes, adj_param_size);
157    if (size_read > 0) {
158      for (i=0; i< size_read; i++)
159        vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", result_param_buf->bytes[i]);
160
161    } else {
162      vtpmlogerror(VTPM_LOG_VTPM, "Error reading from vtpm manager.\n");
163      goto abort_egress;
164    }
165    vtpmloginfomore(VTPM_LOG_VTPM, "\n");
166
167    if (size_read < (int)adj_param_size) {
168      vtpmloginfomore(VTPM_LOG_VTPM, "\n");
169      vtpmlogerror(VTPM_LOG_VTPM, "Command read(%d) is shorter than header indicates(%d).\n", size_read, adj_param_size);
170      status = TPM_IOERROR;
171      goto abort_egress;
172    }
173  } else {
174    vtpmloginfomore(VTPM_LOG_VTPM, "\n");
175  }
176
177  status=TPM_SUCCESS;
178  goto egress;
179
180 abort_egress:
181 egress:
182
183  return status;
184}
185
186
Note: See TracBrowser for help on using the repository browser.