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
|
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/usr/i2c/test/tpmddtest.H $ */
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2015,2017 */
/* [+] 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 */
#ifndef __TPMDDTEST_H
#define __TPMDDTEST_H
/**
* @file tpmddtest.H
*
* @brief Test cases for the tpm dd code
*/
#include <sys/time.h>
#include <cxxtest/TestSuite.H>
#include <errl/errlmanager.H>
#include <errl/errlentry.H>
#include <devicefw/driverif.H>
#include <i2c/tpmddreasoncodes.H>
#include <targeting/common/commontargeting.H>
#include <secureboot/trustedbootif.H>
#include "i2ctest.H"
#include "../tpmdd.H"
#include "../../secureboot/trusted/trustedTypes.H"
extern trace_desc_t* g_trac_tpmdd;
// NOTE: TRACUCOMP defined/controlled in i2ctest.H
using namespace TARGETING;
using namespace TPMDD;
class TPMDDTest: public CxxTest::TestSuite
{
public:
/**
* @brief Retrieve a TPM target to test with
*/
TARGETING::Target* getTestTarget()
{
TARGETING::Target* testTarget = NULL;
TARGETING::TargetHandleList tpmList;
TARGETING::getAllChips(tpmList,
TARGETING::TYPE_TPM,
true); // only functional
if (tpmList.size() > 0)
{
testTarget = tpmList[0];
TRACFCOMP( g_trac_tpmdd,
"getTestTarget tpm tgt=0x%08X",
TARGETING::get_huid(testTarget));
// Let's see if the requested chip is functional
tpm_info_t tpmInfo;
memset(&tpmInfo, 0, sizeof(tpm_info_t));
errlHndl_t err = tpmReadAttributes (testTarget, tpmInfo,
TPM_LOCALITY_0);
if (NULL != err)
{
testTarget = NULL;
delete err;
err = NULL;
}
else if (!tpmInfo.tpmEnabled)
{
TRACFCOMP(g_trac_tpmdd, "getTestTarget - "
"Chip not enabled");
testTarget = NULL;
}
else
{
TRACFCOMP(g_trac_tpmdd, "getTestTarget - "
"Chip enabled");
}
}
return testTarget;
}
/**
* @brief TPM Read VendorID Test
*/
void testTPMReadVendorID ( void )
{
errlHndl_t err = NULL;
int64_t fails = 0, num_ops = 0;
uint32_t data = 0x0;
size_t dataSize = sizeof(data);
TRACFCOMP( g_trac_tpmdd,
"testTPMReadVendorID - Start" );
do
{
// Get a TPM Target
TARGETING::Target* testTarget = getTestTarget();
if (NULL == testTarget)
{
continue;
}
num_ops++;
err = deviceRead(testTarget,
&data,
dataSize,
DEVICE_TPM_ADDRESS( TPM_OP_READVENDORID,
0,
TPM_LOCALITY_0) );
if( NULL != err )
{
fails++;
TS_FAIL( "testTPMReadVendorID - Error detected" );
errlCommit( err,
TPMDD_COMP_ID );
delete err;
err = NULL;
continue;
}
else if ((data & TPMDD::TPM_VENDORID_MASK)
!= TPMDD::TPM_VENDORID)
{
fails++;
TS_FAIL( "testTPMReadVendorID - Failed to read "
"correct vendor id ID=0x%X", data);
continue;
}
else
{
TRACUCOMP(g_trac_tpmdd, "testTPMReadVendorID - "
"VendorID returned as expected. ID=0x%X",
data);
continue;
}
} while( 0 );
TRACFCOMP( g_trac_tpmdd,
"testTPMReadVendorID - End: %d/%d fails",
fails, num_ops );
}
/**
* @brief TPM Invalid Operation Test
* This test will pass in an invalid Operation type. It
* is expected that an error log is to be returned.
*/
void testTPMInvalidOperation ( void )
{
errlHndl_t err = NULL;
int64_t fails = 0, num_ops = 0;
uint64_t data = 0x0ull;
size_t dataSize = 0;
TRACFCOMP( g_trac_tpmdd,
"testTPMInvalidOperation - Start" );
do
{
// Get a TPM Target
TARGETING::Target* testTarget = getTestTarget();
if (NULL == testTarget)
{
continue;
}
num_ops++;
err = deviceRead(testTarget,
&data,
dataSize,
DEVICE_TPM_ADDRESS( TPM_OP_LASTOP,
0,
TPM_LOCALITY_0) );
if( NULL == err )
{
fails++;
TS_FAIL( "testTPMInvalidOperation - Error should've "
" resulted in Operation type of TPM_OP_LASTOP!" );
}
else
{
TRACUCOMP(g_trac_tpmdd, "testTPMInvalidOperation - "
"TPM_OP_LASTOP : Error log returned as expected. "
"RC=0x%X",
err->reasonCode() );
delete err;
err = NULL;
}
num_ops++;
err = deviceOp( DeviceFW::LAST_OP_TYPE,
testTarget,
&data,
dataSize,
DEVICE_TPM_ADDRESS( TPM_OP_READVENDORID,
0,
TPM_LOCALITY_0) );
if( NULL == err )
{
fails++;
TS_FAIL( "testTPMInvalidOperation - Error should've "
" resulted in Operation type of LAST_OP_TYPE!" );
}
else
{
TRACUCOMP(g_trac_tpmdd, "testTPMInvalidOperation - "
"DevFW::LastOp : Error log returned as expected. "
"RC=0x%X",
err->reasonCode() );
delete err;
err = NULL;
}
} while( 0 );
TRACFCOMP( g_trac_tpmdd,
"testTPMInvalidOperation - End: %d/%d fails",
fails, num_ops );
}
/**
* @brief TPM Overflow Test
* This test will pass in a read length too long for the
* device. It is expected that an error log is to be
* returned.
*/
void testTPMOverflow ( void )
{
errlHndl_t err = NULL;
int64_t fails = 0, num_ops = 0;
uint64_t data = 0x0ull;
size_t dataSize = 0;
TRACFCOMP( g_trac_tpmdd,
"testTPMOverflow - Start" );
do
{
// Get a TPM Target
TARGETING::Target* testTarget = getTestTarget();
if (NULL == testTarget)
{
continue;
}
// Set max length
dataSize = 0xFFFFFFFFFFFFFFFF;
num_ops++;
err = deviceRead(testTarget,
&data,
dataSize,
DEVICE_TPM_ADDRESS( TPM_OP_READVENDORID,
0,
TPM_LOCALITY_0) );
if( NULL == err ||
err->reasonCode() != TPM_OVERFLOW_ERROR)
{
fails++;
TS_FAIL( "testTPMOverflow - ReadVendorId: Error should've "
"resulted from overflow length: "
"length = 0x%x",
dataSize);
}
else
{
TRACUCOMP(g_trac_tpmdd, "testTPMOverflow - "
"ReadVendorId: Error log returned as expected. "
"RC=0x%X",
err->reasonCode() );
delete err;
err = NULL;
}
} while( 0 );
TRACFCOMP( g_trac_tpmdd,
"testTPMOverflow - End: %d/%d fails",
fails, num_ops );
}
/**
* @brief TPM Presence Test
*/
void testTPMPresence ( void )
{
int64_t fails = 0, num_ops = 0;
bool presence = false;
TRACFCOMP( g_trac_tpmdd,
"testTPMPresence - Start" );
do
{
// Get a processor Target instead of a tpm target
TARGETING::TargetService& tS = TARGETING::targetService();
TARGETING::Target* testTarget = NULL;
tS.masterProcChipTargetHandle( testTarget );
assert(testTarget != NULL);
// Skip this target if target is non-functional
if(!testTarget->getAttr<TARGETING::ATTR_HWAS_STATE>()
.functional)
{
continue;
}
num_ops++;
// Test with invalid proc target
presence = TPMDD::tpmPresence(testTarget);
if( presence == true )
{
fails++;
TS_FAIL( "testTPMPresence - ProcTarget: Error when "
"using processor target, false presence!" );
}
else
{
TRACUCOMP(g_trac_tpmdd, "testTPMPresence - "
"ProcTarget: False returned as expected. ");
}
testTarget = getTestTarget();
if (NULL != testTarget)
{
// Now test with valid primary target
num_ops++;
presence = TPMDD::tpmPresence(testTarget);
if( presence == false )
{
fails++;
TS_FAIL( "testTPMPresence - TPMPrimary: true should've "
"resulted in using backup TPM!" );
}
else
{
TRACUCOMP(g_trac_tpmdd, "testTPMPresence - "
"TPMPrimary: true returned as expected. ");
}
}
} while( 0 );
TRACFCOMP( g_trac_tpmdd,
"testTPMPresence - End: %d/%d fails",
fails, num_ops );
}
/**
* @brief TPM Transmit Test
*/
void testTPMTransmit ( void )
{
errlHndl_t err = NULL;
int64_t fails = 0, num_ops = 0;
uint8_t data[256];
size_t dataSize = sizeof(data);
size_t cmdSize = 0;
TRACFCOMP( g_trac_tpmdd,
"testTPMTransmit - Start" );
do
{
// Get a TPM Target
TARGETING::Target* testTarget = getTestTarget();
if (NULL == testTarget)
{
continue;
}
// Build our command block for a get capability
dataSize = sizeof(data);
memset(data, 0xFE, sizeof(data));
TRUSTEDBOOT::TPM2_GetCapabilityIn* cmd =
reinterpret_cast<TRUSTEDBOOT::TPM2_GetCapabilityIn*>
(data);
cmd->base.commandSize =
sizeof (TRUSTEDBOOT::TPM2_GetCapabilityIn);
cmd->base.tag = TRUSTEDBOOT::TPM_ST_NO_SESSIONS;
cmd->base.commandCode = TRUSTEDBOOT::TPM_CC_GetCapability;
cmd->capability = TRUSTEDBOOT::TPM_CAP_TPM_PROPERTIES;
cmd->property = TRUSTEDBOOT::TPM_PT_MANUFACTURER;
cmd->propertyCount = 1;
cmdSize = cmd->base.commandSize;
num_ops++;
err = deviceRead(testTarget,
&data,
dataSize,
DEVICE_TPM_ADDRESS( TPM_OP_TRANSMIT,
cmdSize,
TPM_LOCALITY_0) );
TRUSTEDBOOT::TPM2_BaseOut* resp =
reinterpret_cast<TRUSTEDBOOT::TPM2_BaseOut*>(data);
if( NULL != err )
{
fails++;
TS_FAIL( "testTPMTransmit - GetCap : Error detected" );
errlCommit( err,
TPMDD_COMP_ID );
delete err;
err = NULL;
continue;
}
else if (TRUSTEDBOOT::TPM_SUCCESS !=
resp->responseCode)
{
fails++;
TS_FAIL( "testTPMTransmit - GetCap : "
"TPM return non-success : %d",
resp->responseCode);
continue;
}
else
{
TRACUCOMP(g_trac_tpmdd, "testTPMTransmit - GetCap : "
"Transmit returned as expected. len=%d",
dataSize);
}
} while( 0 );
TRACFCOMP( g_trac_tpmdd,
"testTPMTransmit - End: %d/%d fails",
fails, num_ops );
}
/**
* @brief TPM Transmit Over/Under flow Test
*/
void testTPMTransmitOverUnder ( void )
{
errlHndl_t err = NULL;
int64_t fails = 0, num_ops = 0;
uint8_t data[256];
size_t dataSize = sizeof(data);
size_t cmdSize = 0;
TRACFCOMP( g_trac_tpmdd,
"testTPMTransmitOverUnder - Start" );
do
{
// Get a TPM Target
TARGETING::Target* testTarget = getTestTarget();
if (NULL == testTarget)
{
continue;
}
dataSize = sizeof(data);
memset(data, 0xFE, sizeof(data));
// Test a TPM data overflow
TRUSTEDBOOT::TPM2_GetCapabilityIn* cmd =
reinterpret_cast<TRUSTEDBOOT::TPM2_GetCapabilityIn*>(data);
cmd->base.tag = TRUSTEDBOOT::TPM_ST_NO_SESSIONS;
cmd->base.commandSize =
sizeof (TRUSTEDBOOT::TPM2_GetCapabilityIn);
cmd->base.commandCode = TRUSTEDBOOT::TPM_CC_GetCapability;
cmd->capability = TRUSTEDBOOT::TPM_CAP_TPM_PROPERTIES;
cmd->property = TRUSTEDBOOT::TPM_PT_MANUFACTURER;
cmd->propertyCount = 1;
cmdSize = cmd->base.commandSize;
num_ops++;
// Force datasize to be too small
dataSize = sizeof(TRUSTEDBOOT::TPM2_BaseOut);
err = deviceRead(testTarget,
&data,
dataSize,
DEVICE_TPM_ADDRESS(TPM_OP_TRANSMIT,
cmdSize,
TPM_LOCALITY_0) );
if( NULL == err ||
err->reasonCode() != TPM_OVERFLOW_ERROR)
{
fails++;
TS_FAIL( "testTPMTransmitOverUnder - Error "
"data overflow not detected" );
errlCommit( err,
TPMDD_COMP_ID );
delete err;
err = NULL;
}
else
{
TRACUCOMP(g_trac_tpmdd, "testTPMTransmitOverUnder - "
"DataOver Transmit returned as expected. len=%d",
dataSize);
}
} while( 0 );
TRACFCOMP( g_trac_tpmdd,
"testTPMTransmitOverUnder - End: %d/%d fails",
fails, num_ops );
}
};
#endif
|