summaryrefslogtreecommitdiffstats
path: root/src/usr/secureboot/trusted/trustedbootCmds.C
blob: 9e587593325c0be607ba3220913291555a81907e (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
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
/* 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,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                                                     */
/**
 * @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,
                              tpm_locality_t i_locality)
{
    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,
                          i_locality);

        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;
    uint32_t* sSizePtr = NULL;
    size_t curSize = 0;
    int stage = 0;
    TPM2_BaseIn* baseCmd =
        (TPM2_BaseIn*)o_outbuf;
    TPMS_AUTH_COMMAND cmdAuth;

    *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;
        if (TPM_CC_PCR_Extend == i_cmd->commandCode)
        {
            TPM2_ExtendIn* cmdPtr = (TPM2_ExtendIn*)i_cmd;
            sBuf = TPM2_ExtendIn_marshalHandle(cmdPtr,
                                               sBuf,
                                               i_bufsize,
                                               o_cmdSize);
            if (NULL == sBuf)
            {
                break;
            }
        }

        // Marshal the authorizations
        stage = 2;
        if (TPM_CC_PCR_Extend == i_cmd->commandCode)
        {
            // Insert a password authorization with a null pw
            // Make room for the 4 byte size field at the beginning
            sSizePtr = (uint32_t*)sBuf;
            sBuf += sizeof(uint32_t);
            *o_cmdSize += sizeof(uint32_t);
            i_bufsize -= sizeof(uint32_t);
            curSize = *o_cmdSize;

            cmdAuth.sessionHandle = TPM_RS_PW;
            cmdAuth.nonceSize = 0;
            cmdAuth.sessionAttributes = 0;
            cmdAuth.hmacSize = 0;

            sBuf = TPMS_AUTH_COMMAND_marshal(&cmdAuth, sBuf, i_bufsize,
                                             o_cmdSize);

            if (NULL == sBuf)
            {
                break;
            }
            // Put in the size of the auth area
            *sSizePtr = (*o_cmdSize - curSize);

        }

        // Marshal the command 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;
          case TPM_CC_PCR_Read:
              {
                  TPM2_PcrReadIn* cmdPtr = (TPM2_PcrReadIn*)i_cmd;
                  sBuf = TPM2_PcrReadIn_marshal(cmdPtr, sBuf,
                                                i_bufsize - (sBuf - o_outbuf),
                                                o_cmdSize);
              }
              break;

          case TPM_CC_PCR_Extend:
              {
                  TPM2_ExtendIn* cmdPtr = (TPM2_ExtendIn*)i_cmd;
                  sBuf = TPM2_ExtendIn_marshalParms(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
                   * @custdesc       Failure detected in security subsystem
                   */
                  err = tpmCreateErrorLog(MOD_TPM_MARSHALCMDDATA,
                                          RC_TPM_MARSHAL_INVALID_CMD,
                                          i_cmd->commandCode,
                                          0);
              }
              break;
        };

        if (TB_SUCCESS != err || NULL == sBuf)
        {
            break;
        }

        // Do a verification that the cmdSize equals what we used
        if (((size_t)(sBuf - o_outbuf)) != *o_cmdSize)
        {
            TRACFCOMP( g_trac_trustedboot,
                       "TPM MARSHAL MARSHAL SIZE MISMATCH : %d %d",
                       (int)(sBuf - o_outbuf), (int)(*o_cmdSize) );
            sBuf = NULL;
        }

        // 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
         * @custdesc       Failure detected in security subsystem
         */
        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;
    const 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:
          case TPM_CC_PCR_Extend:
            // 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;

          case TPM_CC_PCR_Read:
              {
                  TPM2_PcrReadOut* respPtr = (TPM2_PcrReadOut*)o_outBuf;
                  sBuf = TPM2_PcrReadOut_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
                   * @custdesc       Failure detected in security subsystem
                   */
                  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
         * @custdesc       Failure detected in security subsystem
         */
        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),
                                 TPM_LOCALITY_0);

        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 Ret(0x%X) : ",
                       resp->responseCode);

            /*@
             * @errortype
             * @reasoncode     RC_TPM_START_FAIL
             * @severity       ERRL_SEV_UNRECOVERABLE
             * @moduleid       MOD_TPM_CMD_STARTUP
             * @userdata1      responseCode
             * @userdata2      0
             * @devdesc        TPM_Startup operation failure.
             * @custdesc       Failure detected in security subsystem
             */
            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),
                                 TPM_LOCALITY_0);

        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 Ret(0x%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.
             * @custdesc       Failure detected in security subsystem
             */
            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.
                 * @custdesc       Failure detected in security subsystem
                 */
                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),
                                 TPM_LOCALITY_0);

        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 Ret(0x%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.
             * @custdesc       Failure detected in security subsystem
             */
            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.
                 * @custdesc       Failure detected in security subsystem
                 */
                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;
}


errlHndl_t tpmCmdPcrExtend(TpmTarget * io_target,
                           TPM_Pcr i_pcr,
                           TPM_Alg_Id i_algId,
                           const uint8_t* i_digest,
                           size_t  i_digestSize)
{
    return tpmCmdPcrExtend2Hash(io_target, i_pcr,
                                i_algId, i_digest, i_digestSize,
                                TPM_ALG_INVALID_ID, NULL, 0);
}

errlHndl_t tpmCmdPcrExtend2Hash(TpmTarget * io_target,
                                TPM_Pcr i_pcr,
                                TPM_Alg_Id i_algId_1,
                                const uint8_t* i_digest_1,
                                size_t  i_digestSize_1,
                                TPM_Alg_Id i_algId_2,
                                const uint8_t* i_digest_2,
                                size_t  i_digestSize_2)
{
    errlHndl_t err = NULL;
    uint8_t dataBuf[sizeof(TPM2_ExtendIn)];
    size_t dataSize = sizeof(dataBuf);
    size_t fullDigestSize_1 = 0;
    size_t fullDigestSize_2 = 0;
    TPM2_BaseOut* resp = (TPM2_BaseOut*)dataBuf;
    TPM2_ExtendIn* cmd = (TPM2_ExtendIn*)dataBuf;


    TRACDCOMP( g_trac_trustedboot,
               ">>tpmCmdPcrExtend2Hash()" );
    if (NULL == i_digest_2)
    {
        TRACUCOMP( g_trac_trustedboot,
                   ">>tpmCmdPcrExtend2Hash() Pcr(%d) Alg(%X) DS(%d)",
                   i_pcr, i_algId_1, (int)i_digestSize_1);
    }
    else
    {
        TRACUCOMP( g_trac_trustedboot,
                   ">>tpmCmdPcrExtend2Hash() Pcr(%d) Alg(%X:%X) DS(%d:%d)",
                   i_pcr, i_algId_1, i_algId_2,
                   (int)i_digestSize_1, (int)i_digestSize_2);
    }

    do
    {

        fullDigestSize_1 = getDigestSize(i_algId_1);
        if (NULL != i_digest_2)
        {
            fullDigestSize_2 = getDigestSize(i_algId_2);
        }

        // Build our command block
        memset(dataBuf, 0, sizeof(dataBuf));

        // Argument verification
        if (fullDigestSize_1 == 0 ||
            NULL == i_digest_1 ||
            IMPLEMENTATION_PCR < i_pcr ||
            (NULL != i_digest_2 && fullDigestSize_2 == 0)
            )
        {
            TRACFCOMP( g_trac_trustedboot,
                       "TPM PCR EXTEND ARG FAILURE FDS(%d:%d) DS(%d:%d) "
                       "PCR(%d)",
                       (int)fullDigestSize_1, (int)fullDigestSize_2,
                       (int)i_digestSize_1, (int)i_digestSize_2, i_pcr);
            /*@
             * @errortype
             * @reasoncode     RC_TPM_INVALID_ARGS
             * @severity       ERRL_SEV_UNRECOVERABLE
             * @moduleid       MOD_TPM_CMD_PCREXTEND
             * @userdata1      Digest Ptr
             * @userdata2[0:15] Full Digest Size 1
             * @userdata2[16:31] Full Digest Size 2
             * @userdata2[32:63] PCR
             * @devdesc        PCR Extend invalid arguments detected
             * @custdesc       Failure detected in security subsystem
             */
            err = tpmCreateErrorLog(MOD_TPM_CMD_PCREXTEND,
                                    RC_TPM_INVALID_ARGS,
                                    (uint64_t)i_digest_1,
                                    (fullDigestSize_1 << 48) |
                                    (fullDigestSize_2 << 32) |
                                    i_pcr);
            break;
        }

        // Log the input PCR value
        TRACUBIN(g_trac_trustedboot, "PCR In",
                 i_digest_1, fullDigestSize_1);

        cmd->base.tag = TPM_ST_SESSIONS;
        cmd->base.commandCode = TPM_CC_PCR_Extend;
        cmd->pcrHandle = i_pcr;
        cmd->digests.count = 1;
        cmd->digests.digests[0].algorithmId = i_algId_1;
        memcpy(&(cmd->digests.digests[0].digest), i_digest_1,
               (i_digestSize_1 < fullDigestSize_1 ?
                i_digestSize_1 : fullDigestSize_1) );
        if (NULL != i_digest_2)
        {
            cmd->digests.count = 2;
            cmd->digests.digests[1].algorithmId = i_algId_2;
            memcpy(&(cmd->digests.digests[1].digest), i_digest_2,
                   (i_digestSize_2 < fullDigestSize_2 ?
                    i_digestSize_2 : fullDigestSize_2));
        }

        err = tpmTransmitCommand(io_target,
                                 dataBuf,
                                 sizeof(dataBuf),
                                 TPM_LOCALITY_0);

        if (TB_SUCCESS != err)
        {
            TRACFCOMP( g_trac_trustedboot,
                       "TPM PCRExtend Transmit Fail");
            break;

        }
        else if ((sizeof(TPM2_BaseOut) > dataSize)
                 || (TPM_SUCCESS != resp->responseCode))
        {
            TRACFCOMP( g_trac_trustedboot,
                       "TPM PCRExtend OP Fail Ret(0x%X) ExSize(%d) Size(%d) ",
                       resp->responseCode,
                       (int)sizeof(TPM2_BaseOut),
                       (int)dataSize);

            /*@
             * @errortype
             * @reasoncode     RC_TPM_COMMAND_FAIL
             * @severity       ERRL_SEV_UNRECOVERABLE
             * @moduleid       MOD_TPM_CMD_PCREXTEND
             * @userdata1      responseCode
             * @userdata2      dataSize
             * @devdesc        Command failure performing PCR extend.
             * @custdesc       Failure detected in security subsystem
             */
            err = tpmCreateErrorLog(MOD_TPM_CMD_PCREXTEND,
                                    RC_TPM_COMMAND_FAIL,
                                    resp->responseCode,
                                    dataSize);
            break;

        }

    } while ( 0 );


    TRACUCOMP( g_trac_trustedboot,
               "<<tpmCmdPcrExtend() - %s",
               ((TB_SUCCESS == err) ? "No Error" : "With Error") );
    return err;

}

errlHndl_t tpmCmdPcrRead(TpmTarget* io_target,
                         TPM_Pcr i_pcr,
                         TPM_Alg_Id i_algId,
                         uint8_t* o_digest,
                         size_t  i_digestSize)
{
    errlHndl_t err = NULL;
    uint8_t dataBuf[sizeof(TPM2_PcrReadOut)];
    size_t dataSize = sizeof(dataBuf);
    size_t fullDigestSize = 0;
    TPM2_PcrReadOut* resp = (TPM2_PcrReadOut*)dataBuf;
    TPM2_PcrReadIn* cmd = (TPM2_PcrReadIn*)dataBuf;


    TRACDCOMP( g_trac_trustedboot,
               ">>tpmCmdPcrRead()" );
    TRACUCOMP( g_trac_trustedboot,
               ">>tpmCmdPcrRead() Pcr(%d) DS(%d)",
               i_pcr, (int)i_digestSize);

    do
    {

        fullDigestSize = getDigestSize(i_algId);

        // Build our command block
        memset(dataBuf, 0, sizeof(dataBuf));

        // Argument verification
        if (fullDigestSize > i_digestSize ||
            NULL == o_digest ||
            IMPLEMENTATION_PCR < i_pcr
            )
        {
            TRACFCOMP( g_trac_trustedboot,
                       "TPM PCR READ ARG FAILURE FDS(%d) DS(%d) PCR(%d)",
                       (int)fullDigestSize, (int)i_digestSize, i_pcr);
            /*@
             * @errortype
             * @reasoncode     RC_TPM_INVALID_ARGS
             * @severity       ERRL_SEV_UNRECOVERABLE
             * @moduleid       MOD_TPM_CMD_PCRREAD
             * @userdata1      Digest Ptr
             * @userdata2[0:31] Full Digest Size
             * @userdata2[32:63] PCR
             * @devdesc        pcr read invalid arguments
             * @custdesc       Failure detected in security subsystem
             */
            err = tpmCreateErrorLog(MOD_TPM_CMD_PCRREAD,
                                    RC_TPM_INVALID_ARGS,
                                    (uint64_t)o_digest,
                                    (fullDigestSize << 32) |
                                    i_pcr);

            break;
        }

        cmd->base.tag = TPM_ST_NO_SESSIONS;
        cmd->base.commandCode = TPM_CC_PCR_Read;
        cmd->pcrSelectionIn.count = 1; // One algorithm
        cmd->pcrSelectionIn.pcrSelections[0].algorithmId = i_algId;
        cmd->pcrSelectionIn.pcrSelections[0].sizeOfSelect = PCR_SELECT_MAX;
        memset(cmd->pcrSelectionIn.pcrSelections[0].pcrSelect, 0,
               sizeof(cmd->pcrSelectionIn.pcrSelections[0].pcrSelect));
        cmd->pcrSelectionIn.pcrSelections[0].pcrSelect[i_pcr / 8] =
            0x01 << (i_pcr % 8);

        err = tpmTransmitCommand(io_target,
                                 dataBuf,
                                 sizeof(dataBuf),
                                 TPM_LOCALITY_0);

        if (TB_SUCCESS != err)
        {
            TRACFCOMP( g_trac_trustedboot,
                       "TPM PCRRead Transmit Fail ");
            break;

        }
        else if ((sizeof(TPM2_BaseOut) > dataSize) ||
                 (TPM_SUCCESS != resp->base.responseCode) ||
                 (resp->pcrValues.count != 1) ||
                 (resp->pcrValues.digests[0].size != fullDigestSize))
        {
            TRACFCOMP( g_trac_trustedboot,
                       "TPM PCRRead OP Fail Ret(0x%X) ExSize(%d) "
                       "Size(%d) Cnt(%d) DSize(%d)",
                       resp->base.responseCode,
                       (int)sizeof(TPM2_BaseOut),
                       (int)dataSize,
                       resp->pcrValues.count,
                       resp->pcrValues.digests[0].size);

            /*@
             * @errortype
             * @reasoncode     RC_TPM_COMMAND_FAIL
             * @severity       ERRL_SEV_UNRECOVERABLE
             * @moduleid       MOD_TPM_CMD_PCRREAD
             * @userdata1      responseCode
             * @userdata2      dataSize
             * @devdesc        Command failure performing PCR read.
             * @custdesc       Failure detected in security subsystem
             */
            err = tpmCreateErrorLog(MOD_TPM_CMD_PCRREAD,
                                    RC_TPM_COMMAND_FAIL,
                                    resp->base.responseCode,
                                    dataSize);
            break;
        }
        else
        {

            memcpy(o_digest, resp->pcrValues.digests[0].buffer, fullDigestSize);

            // Log the PCR value
            TRACUBIN(g_trac_trustedboot, "PCR Out",
                     o_digest, fullDigestSize);

        }

    } while ( 0 );


    TRACUCOMP( g_trac_trustedboot,
               "<<tpmCmdPcrRead() - %s",
               ((TB_SUCCESS == err) ? "No Error" : "With Error") );
    return err;

}


#ifdef __cplusplus
} // end TRUSTEDBOOT
#endif
OpenPOWER on IntegriCloud