source: trunk/packages/xen-3.1/xen-3.1/tools/vtpm_manager/manager/dmictl.c @ 34

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

Add xen and xen-common

  • Property svn:mime-type set to text/cpp
File size: 8.0 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//   dmictl.c
35//
36//     Functions for creating and destroying DMIs
37//
38// ==================================================================
39
40#include <stdio.h>
41#include <unistd.h>
42#include <string.h>
43
44#include "vtpmpriv.h"
45#include "bsg.h"
46#include "buffer.h"
47#include "log.h"
48#include "hashtable.h"
49#include "hashtable_itr.h"
50#include "vtpm_ipc.h"
51
52#define TPM_EMULATOR_PATH "/usr/bin/vtpmd"
53
54// if dmi_res is non-null, then return a pointer to new object.
55// Also, this does not fill in the measurements. They should be filled by
56// design dependent code or saveNVM
57TPM_RESULT init_dmi(UINT32 dmi_id, BYTE dmi_type, VTPM_DMI_RESOURCE **dmi_res) {
58
59  TPM_RESULT status=TPM_SUCCESS;
60  VTPM_DMI_RESOURCE *new_dmi=NULL;
61  UINT32 *dmi_id_key=NULL;
62
63  if ((new_dmi = (VTPM_DMI_RESOURCE *) malloc (sizeof(VTPM_DMI_RESOURCE))) == NULL) {
64      status = TPM_RESOURCES;
65      goto abort_egress;
66  }
67  memset(new_dmi, 0, sizeof(VTPM_DMI_RESOURCE));
68  new_dmi->dmi_id = dmi_id;
69  new_dmi->dmi_type = dmi_type;
70  new_dmi->connected = FALSE;
71  new_dmi->TCSContext = 0;
72
73  new_dmi->NVMLocation = (char *) malloc(11 + strlen(DMI_NVM_FILE));
74  sprintf(new_dmi->NVMLocation, DMI_NVM_FILE, (uint32_t) new_dmi->dmi_id);
75
76  if ((dmi_id_key = (UINT32 *) malloc (sizeof(UINT32))) == NULL) {
77    status = TPM_RESOURCES;
78    goto abort_egress;
79  }
80  *dmi_id_key = new_dmi->dmi_id;
81
82  // install into map
83  if (!hashtable_insert(vtpm_globals->dmi_map, dmi_id_key, new_dmi)){
84    vtpmlogerror(VTPM_LOG_VTPM, "Failed to insert instance into table. Aborting.\n", dmi_id);
85    status = TPM_FAIL;
86    goto abort_egress;
87  }
88
89  if (dmi_res)
90    *dmi_res = new_dmi;
91
92  goto egress;
93
94 abort_egress:
95  if (new_dmi) {
96    free(new_dmi->NVMLocation);
97    free(new_dmi);
98  }
99  free(dmi_id_key);
100
101 egress:
102  return status;
103}
104
105TPM_RESULT close_dmi(VTPM_DMI_RESOURCE *dmi_res) {
106  if (dmi_res == NULL)
107    return TPM_SUCCESS;
108
109  if (dmi_res->dmi_id == VTPM_CTL_DM)
110    return(TPM_BAD_PARAMETER);
111
112  TCS_CloseContext(dmi_res->TCSContext);
113  dmi_res->connected = FALSE;
114
115  vtpm_globals->connected_dmis--;
116
117  return (VTPM_Close_DMI_Extra(dmi_res) );
118}
119       
120TPM_RESULT VTPM_Handle_New_DMI(const buffer_t *param_buf) {
121 
122  VTPM_DMI_RESOURCE *new_dmi=NULL;
123  TPM_RESULT status=TPM_FAIL;
124  BYTE dmi_type, vm_type, startup_mode;
125  UINT32 dmi_id;
126
127  if (param_buf == NULL) { // Assume creation of Dom 0 control
128    dmi_type = VTPM_TYPE_NON_MIGRATABLE;
129    dmi_id = VTPM_CTL_DM;
130  } else if (buffer_len(param_buf) != sizeof(BYTE) * 3  + sizeof(UINT32)) {
131    vtpmloginfo(VTPM_LOG_VTPM, "New DMI command wrong length: %d.\n", buffer_len(param_buf));
132    status = TPM_BAD_PARAMETER;
133    goto abort_egress;
134  } else {
135    vtpm_globals->connected_dmis++; // Put this here so we don't count Dom0
136    BSG_UnpackList( param_buf->bytes, 4,
137                    BSG_TYPE_BYTE, &dmi_type,
138                    BSG_TYPE_BYTE, &startup_mode,
139                    BSG_TYPE_BYTE, &vm_type,
140                    BSG_TYPE_UINT32,  &dmi_id);
141  }
142
143  if ((dmi_type != VTPM_TYPE_NON_MIGRATABLE) && (dmi_type != VTPM_TYPE_MIGRATABLE)) {
144    vtpmlogerror(VTPM_LOG_VTPM, "Creation of VTPM with illegal type.\n");
145    status = TPM_BAD_PARAMETER;
146    goto abort_egress;
147  }
148
149  new_dmi = (VTPM_DMI_RESOURCE *) hashtable_search(vtpm_globals->dmi_map, &dmi_id);
150  if (new_dmi == NULL) {
151    vtpmloginfo(VTPM_LOG_VTPM, "Creating new DMI instance %d attached.\n", dmi_id );
152    // Brand New DMI. Initialize the persistent pieces
153    TPMTRYRETURN(init_dmi(dmi_id, dmi_type, &new_dmi) ); 
154  } else
155    vtpmloginfo(VTPM_LOG_VTPM, "Re-attaching DMI instance %d.\n", dmi_id);
156
157  if (new_dmi->connected) {
158    vtpmlogerror(VTPM_LOG_VTPM, "Attempt to re-attach, currently attached instance %d. Ignoring\n", dmi_id);
159    status = TPM_BAD_PARAMETER;
160    goto abort_egress;
161  }
162 
163  if (new_dmi->dmi_type == VTPM_TYPE_MIGRATED) {
164    vtpmlogerror(VTPM_LOG_VTPM, "Attempt to re-attach previously migrated instance %d without recovering first. Ignoring\n", dmi_id);
165    status = TPM_BAD_PARAMETER;
166    goto abort_egress;
167  }
168
169  // Initialize the Non-persistent pieces
170  TPMTRYRETURN( TCS_OpenContext(&new_dmi->TCSContext) );
171 
172  new_dmi->connected = TRUE; 
173
174  // Design specific new DMI code.
175  // Includes: create IPCs, Measuring DMI, and maybe launching DMI
176  TPMTRYRETURN(VTPM_New_DMI_Extra(new_dmi, vm_type, startup_mode) );
177  goto egress;
178 
179 abort_egress:
180  vtpmlogerror(VTPM_LOG_VTPM, "Failed to create DMI id=%d due to status=%s. Cleaning.\n", dmi_id, tpm_get_error_name(status));
181  close_dmi(new_dmi );
182       
183 egress:
184  return status;
185}
186
187TPM_RESULT VTPM_Handle_Close_DMI( const buffer_t *param_buf) {
188 
189  TPM_RESULT status=TPM_FAIL;
190  VTPM_DMI_RESOURCE *dmi_res=NULL;
191  UINT32 dmi_id;
192 
193  if ((param_buf == NULL) || (buffer_len(param_buf) != sizeof(UINT32)) ) {
194    vtpmlogerror(VTPM_LOG_VTPM, "Closing DMI has bad size.");
195    status = TPM_BAD_PARAMETER;
196    goto abort_egress;
197  }
198 
199  BSG_UnpackList( param_buf->bytes, 1,
200                  BSG_TYPE_UINT32, &dmi_id);
201 
202  vtpmloginfo(VTPM_LOG_VTPM, "Closing DMI %d.\n", dmi_id);
203 
204  dmi_res = (VTPM_DMI_RESOURCE *) hashtable_search(vtpm_globals->dmi_map, &dmi_id);
205  if (dmi_res == NULL ) {
206    vtpmlogerror(VTPM_LOG_VTPM, "Trying to close nonexistent DMI.\n");
207    status = TPM_BAD_PARAMETER;
208    goto abort_egress;
209  }
210       
211  if (!dmi_res->connected) {
212    vtpmlogerror(VTPM_LOG_VTPM, "Closing non-connected DMI.\n");
213    status = TPM_BAD_PARAMETER;
214    goto abort_egress;
215  }
216 
217  // Close Dmi
218        TPMTRYRETURN(close_dmi( dmi_res ));
219 
220  status=TPM_SUCCESS;   
221  goto egress;
222 
223 abort_egress:
224 egress:
225 
226  return status;
227}
228
229TPM_RESULT VTPM_Handle_Delete_DMI( const buffer_t *param_buf) {
230 
231  TPM_RESULT status=TPM_FAIL;
232  VTPM_DMI_RESOURCE *dmi_res=NULL;
233  UINT32 dmi_id;
234   
235  if ((param_buf == NULL) || (buffer_len(param_buf) != sizeof(UINT32)) ) {
236    vtpmlogerror(VTPM_LOG_VTPM, "Closing DMI has bad size.\n");
237    status = TPM_BAD_PARAMETER;
238    goto abort_egress;
239  }
240 
241  BSG_UnpackList( param_buf->bytes, 1,
242                  BSG_TYPE_UINT32, &dmi_id);
243 
244  vtpmloginfo(VTPM_LOG_VTPM, "Deleting DMI %d.\n", dmi_id);   
245 
246  dmi_res = (VTPM_DMI_RESOURCE *) hashtable_remove(vtpm_globals->dmi_map, &dmi_id);
247  if (dmi_res == NULL) {
248    vtpmlogerror(VTPM_LOG_VTPM, "Closing non-existent DMI.\n");
249    status = TPM_BAD_PARAMETER;
250    goto abort_egress;
251  }
252 
253  //vtpm scripts delete file dmi_res->NVMLocation for us
254 
255  // Close DMI first
256  TPMTRYRETURN(close_dmi( dmi_res ));
257  free ( dmi_res );
258       
259  status=TPM_SUCCESS;   
260  goto egress;
261 
262 abort_egress:
263 egress:
264 
265  return status;
266}
Note: See TracBrowser for help on using the repository browser.