aboutsummaryrefslogtreecommitdiffstats
path: root/tools/vtpm_manager/manager/dmictl.c
blob: fee0cb95998594486450387625cac7bb2845bdf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// ===================================================================
// 
// Copyright (c) 2005, Intel Corp.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions 
// are met:
//
//   * Redistributions of source code must retain the above copyright 
//     notice, this list of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above 
//     copyright notice, this list of conditions and the following 
//     disclaimer in the documentation and/or other materials provided 
//     with the distribution.
//   * Neither the name of Intel Corporation nor the names of its 
//     contributors may be used to endorse or promote products derived
//     from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
// ===================================================================
// 
//   dmictl.c
// 
//     Functions for creating and destroying DMIs
//
// ==================================================================

#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include "vtpmpriv.h"
#include "bsg.h"
#include "buffer.h"
#include "log.h"
#include "hashtable.h"
#include "hashtable_itr.h"
#include "vtpm_ipc.h"

#define TPM_EMULATOR_PATH "/usr/bin/vtpmd"

// if dmi_res is non-null, then return a pointer to new object.
// Also, this does not fill in the measurements. They should be filled by
// design dependent code or saveNVM
TPM_RESULT init_dmi(UINT32 dmi_id, BYTE type,  VTPM_DMI_RESOURCE **dmi_res) {

  TPM_RESULT status=TPM_SUCCESS;
  VTPM_DMI_RESOURCE *new_dmi=NULL;
  UINT32 *dmi_id_key=NULL;

  if ((new_dmi = (VTPM_DMI_RESOURCE *) malloc (sizeof(VTPM_DMI_RESOURCE))) == NULL) {
      status = TPM_RESOURCES;
      goto abort_egress;
  }
  memset(new_dmi, 0, sizeof(VTPM_DMI_RESOURCE));
  new_dmi->dmi_id = dmi_id;
  new_dmi->connected = FALSE;
  new_dmi->TCSContext = 0;

  new_dmi->NVMLocation = (char *) malloc(11 + strlen(DMI_NVM_FILE));
  sprintf(new_dmi->NVMLocation, DMI_NVM_FILE, (uint32_t) new_dmi->dmi_id);

  if ((dmi_id_key = (UINT32 *) malloc (sizeof(UINT32))) == NULL) {
    status = TPM_RESOURCES;
    goto abort_egress;
  }
  *dmi_id_key = new_dmi->dmi_id;

  // install into map
  if (!hashtable_insert(vtpm_globals->dmi_map, dmi_id_key, new_dmi)){
    vtpmlogerror(VTPM_LOG_VTPM, "Failed to insert instance into table. Aborting.\n", dmi_id);
    status = TPM_FAIL;
    goto abort_egress;
  }

  if (dmi_res)
    *dmi_res = new_dmi;

  goto egress;

 abort_egress:
  if (new_dmi) {
    free(new_dmi->NVMLocation);
    free(new_dmi);
  }
  free(dmi_id_key);

 egress:
  return status;
}

TPM_RESULT close_dmi(VTPM_DMI_RESOURCE *dmi_res) {
  if (dmi_res == NULL) 
    return TPM_SUCCESS;

  if (dmi_res->dmi_id == VTPM_CTL_DM) 
    return(TPM_BAD_PARAMETER);

  TCS_CloseContext(dmi_res->TCSContext);
  dmi_res->connected = FALSE;

  vtpm_globals->connected_dmis--;

  return (VTPM_Close_DMI_Extra(dmi_res) );
}
	
TPM_RESULT VTPM_Handle_New_DMI(const buffer_t *param_buf) {
  
  VTPM_DMI_RESOURCE *new_dmi=NULL;
  TPM_RESULT status=TPM_FAIL;
  BYTE type, startup_mode;
  UINT32 dmi_id; 

  if (param_buf == NULL) { // Assume creation of Dom 0 control
    type = VTPM_TYPE_NON_MIGRATABLE;
    dmi_id = VTPM_CTL_DM;
  } else if (buffer_len(param_buf) != sizeof(BYTE) + sizeof(BYTE) + sizeof(UINT32)) {
    vtpmloginfo(VTPM_LOG_VTPM, "New DMI command wrong length: %d.\n", buffer_len(param_buf));
    status = TPM_BAD_PARAMETER;
    goto abort_egress;
  } else {
    vtpm_globals->connected_dmis++; // Put this here so we don't count Dom0
    BSG_UnpackList( param_buf->bytes, 3,
		    BSG_TYPE_BYTE, &type,
		    BSG_TYPE_BYTE, &startup_mode,
		    BSG_TYPE_UINT32,  &dmi_id);
  }

  new_dmi = (VTPM_DMI_RESOURCE *) hashtable_search(vtpm_globals->dmi_map, &dmi_id);
  if (new_dmi == NULL) { 
    vtpmloginfo(VTPM_LOG_VTPM, "Creating new DMI instance %d attached.\n", dmi_id );
    // Brand New DMI. Initialize the persistent pieces
    TPMTRYRETURN(init_dmi(dmi_id, type, &new_dmi) );  
  } else 
    vtpmloginfo(VTPM_LOG_VTPM, "Re-attaching DMI instance %d.\n", dmi_id);

  if (type != VTPM_TYPE_MIGRATED) {
    new_dmi->dmi_type = type;
  } else {
    vtpmlogerror(VTPM_LOG_VTPM, "Creation of VTPM with illegal type.\n");
    status = TPM_BAD_PARAMETER;
    goto abort_egress;
  }
  
  if (new_dmi->connected) {
    vtpmlogerror(VTPM_LOG_VTPM, "Attempt to re-attach, currently attached instance %d. Ignoring\n", dmi_id);
    status = TPM_BAD_PARAMETER;
    goto abort_egress;
  }
  
  if (type == VTPM_TYPE_MIGRATED) {
    vtpmlogerror(VTPM_LOG_VTPM, "Attempt to re-attach previously migrated instance %d without recovering first. Ignoring\n", dmi_id);
    status = TPM_BAD_PARAMETER;
    goto abort_egress;
  }

  // Initialize the Non-persistent pieces
  TPMTRYRETURN( TCS_OpenContext(&new_dmi->TCSContext) );
  
  new_dmi->connected = TRUE;  

  // Design specific new DMI code. 
  // Includes: create IPCs, Measuring DMI, and maybe launching DMI
  status = VTPM_New_DMI_Extra(new_dmi, startup_mode);
  goto egress;
  
 abort_egress:
  vtpmlogerror(VTPM_LOG_VTPM, "Failed to create DMI id=%d due to status=%s. Cleaning.\n", dmi_id, tpm_get_error_name(status));
  close_dmi(new_dmi );
	
 egress:
  return status;
}

TPM_RESULT VTPM_Handle_Close_DMI( const buffer_t *param_buf) {
  
  TPM_RESULT status=TPM_FAIL;
  VTPM_DMI_RESOURCE *dmi_res=NULL;
  UINT32 dmi_id;
  
  if ((param_buf == NULL) || (buffer_len(param_buf) != sizeof(UINT32)) ) {
    vtpmlogerror(VTPM_LOG_VTPM, "Closing DMI has bad size.");
    status = TPM_BAD_PARAMETER;
    goto abort_egress;
  }
  
  BSG_UnpackList( param_buf->bytes, 1,
		  BSG_TYPE_UINT32, &dmi_id);
  
  vtpmloginfo(VTPM_LOG_VTPM, "Closing DMI %d.\n", dmi_id);
  
  dmi_res = (VTPM_DMI_RESOURCE *) hashtable_search(vtpm_globals->dmi_map, &dmi_id);
  if (dmi_res == NULL ) {
    vtpmlogerror(VTPM_LOG_VTPM, "Trying to close nonexistent DMI.\n");
    status = TPM_BAD_PARAMETER;
    goto abort_egress;
  }
	
  if (!dmi_res->connected) {
    vtpmlogerror(VTPM_LOG_VTPM, "Closing non-connected DMI.\n");
    status = TPM_BAD_PARAMETER;
    goto abort_egress;
  }
  
  // Close Dmi
	TPMTRYRETURN(close_dmi( dmi_res ));
  
  status=TPM_SUCCESS;    
  goto egress;
  
 abort_egress:
 egress:
  
  return status;
}

TPM_RESULT VTPM_Handle_Delete_DMI( const buffer_t *param_buf) {
  
  TPM_RESULT status=TPM_FAIL;
  VTPM_DMI_RESOURCE *dmi_res=NULL;
  UINT32 dmi_id;
    
  if ((param_buf == NULL) || (buffer_len(param_buf) != sizeof(UINT32)) ) {
    vtpmlogerror(VTPM_LOG_VTPM, "Closing DMI has bad size.\n");
    status = TPM_BAD_PARAMETER;
    goto abort_egress;
  }
  
  BSG_UnpackList( param_buf->bytes, 1,
		  BSG_TYPE_UINT32, &dmi_id);
  
  vtpmloginfo(VTPM_LOG_VTPM, "Deleting DMI %d.\n", dmi_id);    
  
  dmi_res = (VTPM_DMI_RESOURCE *) hashtable_remove(vtpm_globals->dmi_map, &dmi_id);
  if (dmi_res == NULL) {
    vtpmlogerror(VTPM_LOG_VTPM, "Closing non-existent DMI.\n");
    status = TPM_BAD_PARAMETER;
    goto abort_egress;
  }
  
  //vtpm scripts delete file dmi_res->NVMLocation for us
  
  // Close DMI first
  TPMTRYRETURN(close_dmi( dmi_res ));
  free ( dmi_res );
	
  status=TPM_SUCCESS;    
  goto egress;
  
 abort_egress:
 egress:
  
  return status;
}