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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
|
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/usr/secureboot/trusted/trustedbootCmds.C $ */
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2015,2016 */
/* [+] International Business Machines Corp. */
/* */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */
/* implied. See the License for the specific language governing */
/* permissions and limitations under the License. */
/* */
/* IBM_PROLOG_END_TAG */
/**
* @file trustedbootCmds.C
*
* @brief Trusted boot TPM command interfaces
*/
/////////////////////////////////////////////////////////////////
// NOTE: This file is exportable as TSS-Lite for skiboot/PHYP //
/////////////////////////////////////////////////////////////////
// ----------------------------------------------
// Includes
// ----------------------------------------------
#include <string.h>
#include <stdlib.h>
#ifdef __HOSTBOOT_MODULE
#include <secureboot/trustedboot_reasoncodes.H>
#else
#include "trustedboot_reasoncodes.H"
#endif
#include "trustedbootCmds.H"
#include "trustedbootUtils.H"
#include "trustedboot.H"
#include "trustedTypes.H"
#ifdef __cplusplus
namespace TRUSTEDBOOT
{
#endif
errlHndl_t tpmTransmitCommand(TpmTarget * io_target,
uint8_t* io_buffer,
size_t i_bufsize )
{
errlHndl_t err = TB_SUCCESS;
uint8_t* transmitBuf = NULL;
size_t cmdSize = 0;
size_t dataSize = 0;
TPM2_BaseIn* cmd = (TPM2_BaseIn*)io_buffer;
TPM2_BaseOut* resp = (TPM2_BaseOut*)io_buffer;
TRACUCOMP( g_trac_trustedboot,
">>TPM TRANSMIT CMD START : BufLen %d : %016llx",
(int)i_bufsize,
*((uint64_t*)io_buffer) );
do
{
transmitBuf = (uint8_t*)malloc(MAX_TRANSMIT_SIZE);
// Marshal the data into a byte array for transfer to the TPM
err = tpmMarshalCommandData(cmd,
transmitBuf,
MAX_TRANSMIT_SIZE,
&cmdSize);
if (TB_SUCCESS != err)
{
break;
}
// Send to the TPM
dataSize = MAX_TRANSMIT_SIZE;
err = tpmTransmit(io_target,
transmitBuf,
cmdSize,
dataSize);
if (TB_SUCCESS != err)
{
break;
}
// Unmarshal the response
err = tpmUnmarshalResponseData(cmd->commandCode,
transmitBuf,
dataSize,
resp,
i_bufsize);
} while ( 0 );
free(transmitBuf);
TRACUCOMP( g_trac_trustedboot,
"<<tpmTransmitCommand() - %s",
((TB_SUCCESS == err) ? "No Error" : "With Error") );
return err;
}
errlHndl_t tpmMarshalCommandData(TPM2_BaseIn* i_cmd,
uint8_t* o_outbuf,
size_t i_bufsize,
size_t* o_cmdSize)
{
errlHndl_t err = TB_SUCCESS;
uint8_t* sBuf = o_outbuf;
int stage = 0;
TPM2_BaseIn* baseCmd =
(TPM2_BaseIn*)o_outbuf;
*o_cmdSize = 0;
TRACDCOMP( g_trac_trustedboot,
">>tpmMarshalCommandData()" );
do
{
TRACUCOMP( g_trac_trustedboot,
"TPM MARSHAL START : BufLen %d : %016llx",
(int)i_bufsize,
*((uint64_t*)i_cmd) );
// Start with the command header
sBuf = TPM2_BaseIn_marshal(i_cmd, sBuf, i_bufsize, o_cmdSize);
if (NULL == sBuf)
{
break;
}
// Marshal the handles
stage = 1;
// Marshal the authorizations
stage = 2;
// Marshal the parameters
stage = 3;
switch (i_cmd->commandCode)
{
// Two byte parm fields
case TPM_CC_Startup:
{
TPM2_2ByteIn* cmdPtr =
(TPM2_2ByteIn*)i_cmd;
sBuf = TPM2_2ByteIn_marshal(cmdPtr, sBuf,
i_bufsize, o_cmdSize);
}
break;
case TPM_CC_GetCapability:
{
TPM2_GetCapabilityIn* cmdPtr =
(TPM2_GetCapabilityIn*)i_cmd;
sBuf = TPM2_GetCapabilityIn_marshal(cmdPtr,sBuf,
i_bufsize, o_cmdSize);
}
break;
default:
{
// Command code not supported
TRACFCOMP( g_trac_trustedboot,
"TPM MARSHAL INVALID COMMAND : %X",
i_cmd->commandCode );
sBuf = NULL;
/*@
* @errortype
* @reasoncode RC_TPM_MARSHAL_INVALID_CMD
* @severity ERRL_SEV_UNRECOVERABLE
* @moduleid MOD_TPM_MARSHALCMDDATA
* @userdata1 Command Code
* @userdata2 0
* @devdesc Unsupported command code during marshal
*/
err = tpmCreateErrorLog(MOD_TPM_MARSHALCMDDATA,
RC_TPM_MARSHAL_INVALID_CMD,
i_cmd->commandCode,
0);
}
break;
};
if (TB_SUCCESS != err)
{
break;
}
// Lastly now that we know the size update the byte stream
baseCmd->commandSize = *o_cmdSize;
} while ( 0 );
if (NULL == sBuf && TB_SUCCESS == err)
{
TRACFCOMP( g_trac_trustedboot,
"TPM MARSHAL FAILURE : Stage %d", stage);
/*@
* @errortype
* @reasoncode RC_TPM_MARSHALING_FAIL
* @severity ERRL_SEV_UNRECOVERABLE
* @moduleid MOD_TPM_MARSHALCMDDATA
* @userdata1 stage
* @userdata2 0
* @devdesc Marshaling error detected
*/
err = tpmCreateErrorLog(MOD_TPM_MARSHALCMDDATA,
RC_TPM_MARSHALING_FAIL,
stage,
0 );
}
TRACUBIN(g_trac_trustedboot, "Marshal Out",
o_outbuf, *o_cmdSize);
TRACUCOMP( g_trac_trustedboot,
"TPM MARSHAL END : CmdSize: %d : %016llx ",
(int)(*o_cmdSize),
*((uint64_t*)o_outbuf) );
TRACDCOMP( g_trac_trustedboot,
"<<tpmMarshalCommandData()" );
return err;
}
errlHndl_t tpmUnmarshalResponseData(uint32_t i_commandCode,
uint8_t* i_respBuf,
size_t i_respBufSize,
TPM2_BaseOut* o_outBuf,
size_t i_outBufSize)
{
errlHndl_t err = TB_SUCCESS;
uint8_t* sBuf = i_respBuf;
int stage = 0;
TRACDCOMP( g_trac_trustedboot,
">>tpmUnmarshalResponseData()" );
do {
TRACUCOMP( g_trac_trustedboot,
"TPM UNMARSHAL START : RespBufLen %d : OutBufLen %d",
(int)i_respBufSize, (int)i_outBufSize);
TRACUBIN(g_trac_trustedboot,"Unmarshal In",
i_respBuf, i_respBufSize);
// Start with the response header
stage = 1;
sBuf = TPM2_BaseOut_unmarshal(o_outBuf, sBuf,
&i_respBufSize, i_outBufSize);
if (NULL == sBuf)
{
break;
}
// If the TPM returned a failure it will not send the rest
// Let the caller deal with the RC
if (TPM_SUCCESS != o_outBuf->responseCode)
{
break;
}
// Unmarshal the parameters
stage = 2;
switch (i_commandCode)
{
// Empty response commands
case TPM_CC_Startup:
// Nothing to do
break;
case TPM_CC_GetCapability:
{
TPM2_GetCapabilityOut* respPtr =
(TPM2_GetCapabilityOut*)o_outBuf;
sBuf = TPM2_GetCapabilityOut_unmarshal(respPtr, sBuf,
&i_respBufSize,
i_outBufSize);
}
break;
default:
{
// Command code not supported
TRACFCOMP( g_trac_trustedboot,
"TPM UNMARSHAL INVALID COMMAND : %X",
i_commandCode );
sBuf = NULL;
/*@
* @errortype
* @reasoncode RC_TPM_UNMARSHAL_INVALID_CMD
* @severity ERRL_SEV_UNRECOVERABLE
* @moduleid MOD_TPM_UNMARSHALRESPDATA
* @userdata1 commandcode
* @userdata2 stage
* @devdesc Unsupported command code during unmarshal
*/
err = tpmCreateErrorLog(MOD_TPM_UNMARSHALRESPDATA,
RC_TPM_UNMARSHAL_INVALID_CMD,
i_commandCode,
stage);
}
break;
}
} while ( 0 );
if (NULL == sBuf && TB_SUCCESS == err)
{
TRACFCOMP( g_trac_trustedboot,
"TPM UNMARSHAL FAILURE : Stage %d", stage);
/*@
* @errortype
* @reasoncode RC_TPM_UNMARSHALING_FAIL
* @severity ERRL_SEV_UNRECOVERABLE
* @moduleid MOD_TPM_UNMARSHALRESPDATA
* @userdata1 Stage
* @userdata2 Remaining response buffer size
* @devdesc Unmarshaling error detected
*/
err = tpmCreateErrorLog(MOD_TPM_UNMARSHALRESPDATA,
RC_TPM_UNMARSHALING_FAIL,
stage,
i_respBufSize);
}
TRACUCOMP( g_trac_trustedboot,
"TPM UNMARSHAL END : %016llx ",
*((uint64_t*)o_outBuf) );
TRACDCOMP( g_trac_trustedboot,
"<<tpmUnmarshalResponseData()" );
return err;
}
errlHndl_t tpmCmdStartup(TpmTarget* io_target)
{
errlHndl_t err = TB_SUCCESS;
uint8_t dataBuf[BUFSIZE];
TPM2_BaseOut* resp =
(TPM2_BaseOut*)(dataBuf);
TPM2_2ByteIn* cmd =
(TPM2_2ByteIn*)(dataBuf);
TRACUCOMP( g_trac_trustedboot,
">>tpmCmdStartup()" );
do
{
// Send the TPM startup command
// Build our command block for a startup
memset(dataBuf, 0, sizeof(dataBuf));
cmd->base.tag = TPM_ST_NO_SESSIONS;
cmd->base.commandCode = TPM_CC_Startup;
cmd->param = TPM_SU_CLEAR;
err = tpmTransmitCommand(io_target,
dataBuf,
sizeof(dataBuf));
if (TB_SUCCESS != err)
{
TRACFCOMP( g_trac_trustedboot,
"TPM STARTUP transmit Fail");
break;
}
else if (TPM_SUCCESS != resp->responseCode)
{
TRACFCOMP( g_trac_trustedboot,
"TPM STARTUP OP Fail %X : ",
resp->responseCode);
/*@
* @errortype
* @reasoncode RC_TPM_START_FAIL
* @severity ERRL_SEV_UNRECOVERABLE
* @moduleid MOD_TPM_CMD_STARTUP
* @userdata1 responseCode
* @userdata2 0
* @devdesc Invalid operation type.
*/
err = tpmCreateErrorLog(MOD_TPM_CMD_STARTUP,
RC_TPM_START_FAIL,
resp->responseCode,
0);
break;
}
} while ( 0 );
TRACUCOMP( g_trac_trustedboot,
"<<tpmCmdStartup() - %s",
((TB_SUCCESS == err) ? "No Error" : "With Error") );
return err;
}
errlHndl_t tpmCmdGetCapFwVersion(TpmTarget* io_target)
{
errlHndl_t err = TB_SUCCESS;
uint8_t dataBuf[BUFSIZE];
size_t dataSize = BUFSIZE;
uint16_t fwVersion[4] = {0xFF, 0xFF, 0xFF, 0xFF};
TPM2_GetCapabilityOut* resp =
(TPM2_GetCapabilityOut*)dataBuf;
TPM2_GetCapabilityIn* cmd =
(TPM2_GetCapabilityIn*)dataBuf;
TRACUCOMP( g_trac_trustedboot,
">>tpmCmdGetCapFwVersion()" );
do
{
// Build our command block for a get capability of the FW version
memset(dataBuf, 0, dataSize);
cmd->base.tag = TPM_ST_NO_SESSIONS;
cmd->base.commandCode = TPM_CC_GetCapability;
cmd->capability = TPM_CAP_TPM_PROPERTIES;
cmd->property = TPM_PT_FIRMWARE_VERSION_1;
cmd->propertyCount = 1;
err = tpmTransmitCommand(io_target,
dataBuf,
sizeof(dataBuf));
if (TB_SUCCESS != err)
{
TRACFCOMP( g_trac_trustedboot,
"TPM GETCAP Transmit Fail");
break;
}
if (TPM_SUCCESS != resp->base.responseCode)
{
TRACFCOMP( g_trac_trustedboot,
"TPM GETCAP OP Fail %X Size(%d) ",
resp->base.responseCode,
(int)dataSize);
/*@
* @errortype
* @reasoncode RC_TPM_GETCAP_FAIL
* @severity ERRL_SEV_UNRECOVERABLE
* @moduleid MOD_TPM_CMD_GETCAPFWVERSION
* @userdata1 responseCode
* @userdata2 0
* @devdesc Command failure reading TPM FW version.
*/
err = tpmCreateErrorLog(MOD_TPM_CMD_GETCAPFWVERSION,
RC_TPM_GETCAP_FAIL,
resp->base.responseCode,
0);
break;
}
else
{
// Walk the reponse data to pull the high order bytes out
if (resp->capData.capability != TPM_CAP_TPM_PROPERTIES ||
resp->capData.data.tpmProperties.count != 1 ||
resp->capData.data.tpmProperties.tpmProperty[0].property !=
TPM_PT_FIRMWARE_VERSION_1) {
TRACFCOMP( g_trac_trustedboot,
"TPM GETCAP FW INVALID DATA "
"Cap(%X) Cnt(%X) Prop(%X)",
resp->capData.capability,
resp->capData.data.tpmProperties.count,
resp->capData.data.tpmProperties.
tpmProperty[0].property);
/*@
* @errortype
* @reasoncode RC_TPM_GETCAP_FW_INVALID_RESP
* @severity ERRL_SEV_UNRECOVERABLE
* @moduleid MOD_TPM_CMD_GETCAPFWVERSION
* @userdata1 capability
* @userdata2 property
* @devdesc Command failure reading TPM FW version.
*/
err = tpmCreateErrorLog(MOD_TPM_CMD_GETCAPFWVERSION,
RC_TPM_GETCAP_FW_INVALID_RESP,
resp->capData.capability,
resp->capData.data.tpmProperties.
tpmProperty[0].property);
break;
}
else
{
fwVersion[0] =
(resp->capData.data.
tpmProperties.tpmProperty[0].value >> 16);
fwVersion[1] =
(resp->capData.data.
tpmProperties.tpmProperty[0].value & 0xFFFF);
}
}
// Read part 2 of the version
dataSize = BUFSIZE;
memset(dataBuf, 0, dataSize);
cmd->base.tag = TPM_ST_NO_SESSIONS;
cmd->base.commandCode = TPM_CC_GetCapability;
cmd->capability = TPM_CAP_TPM_PROPERTIES;
cmd->property = TPM_PT_FIRMWARE_VERSION_2;
cmd->propertyCount = 1;
err = tpmTransmitCommand(io_target,
dataBuf,
sizeof(dataBuf));
if (TB_SUCCESS != err)
{
TRACFCOMP( g_trac_trustedboot,
"TPM GETCAP2 Transmit Fail");
break;
}
if ((sizeof(TPM2_GetCapabilityOut) > dataSize) ||
(TPM_SUCCESS != resp->base.responseCode))
{
TRACFCOMP( g_trac_trustedboot,
"TPM GETCAP2 OP Fail %X Size(%d) ",
resp->base.responseCode,
(int)dataSize);
/*@
* @errortype
* @reasoncode RC_TPM_GETCAP2_FAIL
* @severity ERRL_SEV_UNRECOVERABLE
* @moduleid MOD_TPM_CMD_GETCAPFWVERSION
* @userdata1 responseCode
* @userdata2 0
* @devdesc Command failure reading TPM FW version.
*/
err = tpmCreateErrorLog(MOD_TPM_CMD_GETCAPFWVERSION,
RC_TPM_GETCAP2_FAIL,
resp->base.responseCode,
0);
break;
}
else
{
// Walk the reponse data to pull the high order bytes out
if (resp->capData.capability != TPM_CAP_TPM_PROPERTIES ||
resp->capData.data.tpmProperties.count != 1 ||
resp->capData.data.tpmProperties.tpmProperty[0].property !=
TPM_PT_FIRMWARE_VERSION_2) {
TRACFCOMP( g_trac_trustedboot,
"TPM GETCAP2 FW INVALID DATA "
"Cap(%X) Cnt(%X) Prop(%X)",
resp->capData.capability,
resp->capData.data.tpmProperties.count,
resp->capData.data.tpmProperties.
tpmProperty[0].property);
/*@
* @errortype
* @reasoncode RC_TPM_GETCAP2_FW_INVALID_RESP
* @severity ERRL_SEV_UNRECOVERABLE
* @moduleid MOD_TPM_CMD_GETCAPFWVERSION
* @userdata1 capability
* @userdata2 property
* @devdesc Command failure reading TPM FW version.
*/
err = tpmCreateErrorLog(MOD_TPM_CMD_GETCAPFWVERSION,
RC_TPM_GETCAP2_FW_INVALID_RESP,
resp->capData.capability,
resp->capData.data.tpmProperties.
tpmProperty[0].property);
break;
}
else
{
fwVersion[2] =
(resp->capData.data.tpmProperties.
tpmProperty[0].value >> 16);
fwVersion[3] =
(resp->capData.data.tpmProperties.
tpmProperty[0].value & 0xFFFF);
}
// Trace the response
TRACFCOMP( g_trac_trustedboot,
"TPM GETCAP FW Level %d.%d.%d.%d",
fwVersion[0],fwVersion[1],fwVersion[2],fwVersion[3]
);
}
} while ( 0 );
TRACDCOMP( g_trac_trustedboot,
"<<tpmCmdGetCapFwVersion() - %s",
((TB_SUCCESS == err) ? "No Error" : "With Error") );
return err;
}
#ifdef __cplusplus
} // end TRUSTEDBOOT
#endif
|