summaryrefslogtreecommitdiffstats
path: root/tboot/tpm.c
blob: d4e0b08c4f88e9b754c167f38e84606705f06cff (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
/*
 * tpm.c: TPM-related support functions
 *
 * Copyright (c) 2006-2010, Intel Corporation
 * 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 the 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.
 *
 */

#include <config.h>
#include <efibase.h>
#include <types.h>
#include <stdbool.h>
#include <printk.h>
#include <misc.h>
#include <compiler.h>
#include <processor.h>
#include <io.h>
#include <string.h>
#include <tpm.h>
#include <sha1.h>

__data struct tpm_if *g_tpm = NULL;
u16 tboot_alg_list[2] = {TB_HALG_SHA1, TB_HALG_SHA256};






/* Global variables for TPM status register */
static tpm20_reg_sts_t       g_reg_sts, *g_reg_sts_20 = &g_reg_sts;
static tpm12_reg_sts_t       *g_reg_sts_12 = (tpm12_reg_sts_t *)&g_reg_sts;

uint8_t g_tpm_family = 0;

/* TPM_DATA_FIFO_x */
#define TPM_REG_DATA_FIFO        0x24
typedef union {
        uint8_t _raw[1];                      /* 1-byte reg */
} tpm_reg_data_fifo_t;

typedef union {
        uint8_t _raw[1];
} tpm_reg_data_crb_t;

#define TPM_ACTIVE_LOCALITY_TIME_OUT    \
          (TIMEOUT_UNIT * g_tpm->timeout.timeout_a)  /* according to spec */
#define TPM_CMD_READY_TIME_OUT          \
          (TIMEOUT_UNIT * g_tpm->timeout.timeout_b)  /* according to spec */
#define TPM_CMD_WRITE_TIME_OUT          \
          (TIMEOUT_UNIT * g_tpm->timeout.timeout_d)  /* let it long enough */
#define TPM_DATA_AVAIL_TIME_OUT         \
          (TIMEOUT_UNIT * g_tpm->timeout.timeout_c)  /* let it long enough */
#define TPM_RSP_READ_TIME_OUT           \
          (TIMEOUT_UNIT * g_tpm->timeout.timeout_d)  /* let it long enough */
#define TPM_VALIDATE_LOCALITY_TIME_OUT  0x100

#define read_tpm_sts_reg(locality) { \
if ( g_tpm_family == 0 ) \
    read_tpm_reg(locality, TPM_REG_STS, g_reg_sts_12); \
else \
    read_tpm_reg(locality, TPM_REG_STS, g_reg_sts_20); \
}

#define write_tpm_sts_reg(locality) { \
if ( g_tpm_family == 0 ) \
    write_tpm_reg(locality, TPM_REG_STS, g_reg_sts_12); \
else \
    write_tpm_reg(locality, TPM_REG_STS, g_reg_sts_20); \
}

static void tpm_send_cmd_ready_status(uint32_t locality)
{
    /* write 1 to TPM_STS_x.commandReady to let TPM enter ready state */
    memset((void *)&g_reg_sts, 0, sizeof(g_reg_sts));
    g_reg_sts.command_ready = 1;
    write_tpm_sts_reg(locality);
}


static bool tpm_send_cmd_ready_status_crb(uint32_t locality)
{
      tpm_reg_ctrl_request_t reg_ctrl_request;
      tpm_reg_ctrl_sts_t reg_ctrl_sts;

      read_tpm_reg(locality, TPM_CRB_CTRL_STS, &reg_ctrl_sts);

#ifdef TPM_TRACE
      printk(TBOOT_INFO"1. reg_ctrl_sts.tpmidle: 0x%x\n", reg_ctrl_sts.tpmidle); 	
      printk(TBOOT_INFO"1. reg_ctrl_sts.tpmsts: 0x%x\n", reg_ctrl_sts.tpmsts); 	
#endif

	if ( reg_ctrl_sts.tpmidle== 1) {
           reg_ctrl_request._raw[0] = 0;
           reg_ctrl_request.cmdReady = 1;
	    write_tpm_reg(locality, TPM_CRB_CTRL_REQ, &reg_ctrl_request);

	    return true;
	}

      reg_ctrl_request._raw[0] = 0;
      reg_ctrl_request.goIdle = 1;
      write_tpm_reg(locality, TPM_CRB_CTRL_REQ, &reg_ctrl_request);
	  
      uint32_t i = 0;
      do {
          read_tpm_reg(locality, TPM_CRB_CTRL_REQ, &reg_ctrl_request);
          if ( reg_ctrl_request.goIdle == 0) 
		break;
          else {
              cpu_relax(); 
	       read_tpm_reg(locality, TPM_CRB_CTRL_REQ, &reg_ctrl_request);

#ifdef TPM_TRACE
		printk(TBOOT_INFO"1. reg_ctrl_request.goIdle: 0x%x\n", reg_ctrl_request.goIdle);
		printk(TBOOT_INFO"1. reg_ctrl_request.cmdReady: 0x%x\n", reg_ctrl_request.cmdReady);
#endif

          }
          i++;
       } while ( i <= TPM_DATA_AVAIL_TIME_OUT);

       if ( i > TPM_DATA_AVAIL_TIME_OUT ) {
            printk(TBOOT_ERR"TPM: reg_ctrl_request.goidle timeout!\n");
            return false;
       }

	read_tpm_reg(locality, TPM_CRB_CTRL_STS, &reg_ctrl_sts);

#ifdef TPM_TRACE
	printk(TBOOT_INFO"2. reg_ctrl_sts.tpmidle: 0x%x\n", reg_ctrl_sts.tpmidle); 	
       printk(TBOOT_INFO"2. reg_ctrl_sts.tpmsts: 0x%x\n", reg_ctrl_sts.tpmsts); 	
#endif

       reg_ctrl_request._raw[0] = 0;
       reg_ctrl_request.cmdReady = 1;
	write_tpm_reg(locality, TPM_CRB_CTRL_REQ, &reg_ctrl_request);

#ifdef TPM_TRACE	
	printk(TBOOT_INFO"2. reg_ctrl_request.goIdle: 0x%x\n", reg_ctrl_request.goIdle);
	printk(TBOOT_INFO"2. reg_ctrl_request.cmdReady: 0x%x\n", reg_ctrl_request.cmdReady);
#endif
 
	read_tpm_reg(locality, TPM_CRB_CTRL_STS, &reg_ctrl_sts);

#ifdef TPM_TRACE
	printk(TBOOT_INFO"2. reg_ctrl_sts.tpmidle: 0x%x\n", reg_ctrl_sts.tpmidle); 	
       printk(TBOOT_INFO"2. reg_ctrl_sts.tpmsts: 0x%x\n", reg_ctrl_sts.tpmsts); 	
#endif

	return true;
	
}

static bool tpm_check_cmd_ready_status_crb(uint32_t locality)
{
    tpm_reg_ctrl_request_t reg_ctrl_request; 
    read_tpm_reg(locality, TPM_CRB_CTRL_REQ, &reg_ctrl_request);

#ifdef TPM_TRACE
    printk(TBOOT_INFO"3. reg_ctrl_request.goIdle: 0x%x\n", reg_ctrl_request.goIdle);
    printk(TBOOT_INFO"3. reg_ctrl_request.cmdReady: 0x%x\n", reg_ctrl_request.cmdReady);
#endif

    if ( reg_ctrl_request.cmdReady == 0) 
		return true;
    else
		return false;

}

static bool tpm_check_cmd_ready_status(uint32_t locality)
{
    read_tpm_sts_reg(locality);
#ifdef TPM_TRACE
    printk(TBOOT_INFO".");
#endif
    return g_reg_sts.command_ready;
}

static void tpm_print_status_register(void)
{
    if ( g_tpm_family == 0 )
    {
        printk(TBOOT_DETA"TPM: status reg content: %02x %02x %02x\n",
            (uint32_t)g_reg_sts_12->_raw[0],
            (uint32_t)g_reg_sts_12->_raw[1],
            (uint32_t)g_reg_sts_12->_raw[2]);
    }
    else
    {
        printk(TBOOT_DETA"TPM: status reg content: %02x %02x %02x %02x\n",
            (uint32_t)g_reg_sts_20->_raw[0],
            (uint32_t)g_reg_sts_20->_raw[1],
            (uint32_t)g_reg_sts_20->_raw[2],
            (uint32_t)g_reg_sts_20->_raw[3]);
    }
}

static u16 tpm_get_burst_count(uint32_t locality)
{
    read_tpm_sts_reg(locality);
    return g_reg_sts.burst_count;
}

static bool tpm_check_expect_status(uint32_t locality)
{
    read_tpm_sts_reg(locality);
#ifdef TPM_TRACE
    printk(TBOOT_INFO"Wait on Expect = 0, Status register %02x\n", g_reg_sts._raw[0]);
#endif
    return g_reg_sts.sts_valid == 1 && g_reg_sts.expect == 0;
}

static bool tpm_check_da_status(uint32_t locality)
{
    read_tpm_sts_reg(locality);
#ifdef TPM_TRACE
    printk(TBOOT_INFO"Waiting for DA Flag, Status register %02x\n", g_reg_sts._raw[0]);
#endif
    return g_reg_sts.sts_valid == 1 && g_reg_sts.data_avail == 1;
}

static void tpm_execute_cmd(uint32_t locality)
{
    memset((void *)&g_reg_sts, 0, sizeof(g_reg_sts));
    g_reg_sts.tpm_go = 1;
    write_tpm_sts_reg(locality);
}

bool tpm_validate_locality(uint32_t locality)
{
    uint32_t i;
    tpm_reg_access_t reg_acc;
    
    for ( i = TPM_VALIDATE_LOCALITY_TIME_OUT; i > 0; i-- ) {
        /*
         * TCG spec defines reg_acc.tpm_reg_valid_sts bit to indicate whether
         * other bits of access reg are valid.( but this bit will also be 1
         * while this locality is not available, so check seize bit too)
         * It also defines that reading reg_acc.seize should always return 0
         */
        read_tpm_reg(locality, TPM_REG_ACCESS, &reg_acc);
        if ( reg_acc.tpm_reg_valid_sts == 1 && reg_acc.seize == 0)
            return true;
        cpu_relax();
    }
    if ( i <= 0 )
        printk(TBOOT_ERR"TPM: tpm_validate_locality timeout\n");

    return false;
}

bool tpm_validate_locality_crb(uint32_t locality)
{
    uint32_t i;
    tpm_reg_loc_state_t reg_loc_state;

    for ( i = TPM_VALIDATE_LOCALITY_TIME_OUT; i > 0; i-- ) {
        /*
         *  Platfrom Tpm  Profile for TPM 2.0 SPEC
         */
        read_tpm_reg(locality, TPM_REG_LOC_STATE, &reg_loc_state);
 	 if ( reg_loc_state.tpm_reg_valid_sts == 1 && reg_loc_state.loc_assigned == 1 && reg_loc_state.active_locality == locality) {
			 printk(TBOOT_INFO"TPM: reg_loc_state._raw[0]:  0x%x\n", reg_loc_state._raw[0]);
			 return true;
        	}
        cpu_relax(); 
    }

    printk(TBOOT_ERR"TPM: tpm_validate_locality_crb timeout\n");
    printk(TBOOT_INFO"TPM: reg_loc_state._raw[0]: 0x%x\n", reg_loc_state._raw[0]);
    return false;
}


bool tpm_wait_cmd_ready(uint32_t locality)
{
    uint32_t            i;
    tpm_reg_access_t    reg_acc;

#if 0 /* some tpms doesn't always return 1 for reg_acc.tpm_reg_valid_sts */
      /* and this bit was checked in tpm_validate_locality() already, */
      /* so safe to skip the check here */
    /* ensure the contents of the ACCESS register are valid */
    read_tpm_reg(locality, TPM_REG_ACCESS, &reg_acc);
#ifdef TPM_TRACE
    printk(TBOOT_INFO"TPM: Access reg content: 0x%02x\n", (uint32_t)reg_acc._raw[0]);
#endif
    if ( reg_acc.tpm_reg_valid_sts == 0 ) {
        printk(TBOOT_ERR"TPM: Access reg not valid\n");
        return false;
    }
#endif
    /* request access to the TPM from locality N */
    reg_acc._raw[0] = 0;
    reg_acc.request_use = 1;
    write_tpm_reg(locality, TPM_REG_ACCESS, &reg_acc);

    i = 0;
    do {
        read_tpm_reg(locality, TPM_REG_ACCESS, &reg_acc);
        if ( reg_acc.active_locality == 1 )
            break;
        else
            cpu_relax();
        i++;
    } while ( i <= TPM_ACTIVE_LOCALITY_TIME_OUT);

    if ( i > TPM_ACTIVE_LOCALITY_TIME_OUT ) {
        printk(TBOOT_ERR"TPM: FIFO_INF access reg request use timeout\n");
        return false;
    }

    /* ensure the TPM is ready to accept a command */
#ifdef TPM_TRACE
    printk(TBOOT_INFO"TPM: wait for cmd ready \n");
#endif
    i = 0;
    do {
        tpm_send_cmd_ready_status(locality);
        cpu_relax();
        /* then see if it has */

        if ( tpm_check_cmd_ready_status(locality) )
            break;
        else
            cpu_relax();
        i++;
    } while ( i <= TPM_CMD_READY_TIME_OUT );
#ifdef TPM_TRACE
    printk(TBOOT_INFO"\n");
#endif

    if ( i > TPM_CMD_READY_TIME_OUT ) {
        tpm_print_status_register();
        printk(TBOOT_INFO"TPM: tpm timeout for command_ready\n");
        goto RelinquishControl;
    }

    return true;

RelinquishControl:
    /* deactivate current locality */
    reg_acc._raw[0] = 0;
    reg_acc.active_locality = 1;
    write_tpm_reg(locality, TPM_REG_ACCESS, &reg_acc);

    return false;
}

static bool tpm_wait_cmd_ready_crb(uint32_t locality)
{
    uint32_t i;

    /* ensure the TPM is ready to accept a command */
#ifdef TPM_TRACE
    printk(TBOOT_INFO"TPM: wait for cmd ready \n");
#endif
    tpm_send_cmd_ready_status_crb(locality);
    i = 0;
    do {
        if ( tpm_check_cmd_ready_status_crb(locality) )
            break;
        else
            cpu_relax();
        i++;
    } while ( i <= TPM_CMD_READY_TIME_OUT );

    if ( i > TPM_CMD_READY_TIME_OUT ) {
        //tpm_print_status_register();
        printk(TBOOT_INFO"TPM: tpm timeout for command_ready\n");
        goto RelinquishControl;
    }

    return true;

RelinquishControl:
    /* deactivate current locality */
	  //tpm_reg_loc_ctrl_t    reg_loc_ctrl;
        //reg_loc_ctrl._raw[0] = 0;
    //reg_loc_ctrl.relinquish = 1;
    //write_tpm_reg(locality, TPM_REG_LOC_CTRL, &reg_loc_ctrl);

    return false;
}

bool tpm_submit_cmd(u32 locality, u8 *in, u32 in_size,  u8 *out, u32 *out_size)
{
    u32 i, rsp_size, offset;
    u16 row_size;
    tpm_reg_access_t    reg_acc;
    bool ret = true;

    if ( locality >= TPM_NR_LOCALITIES ) {
        printk(TBOOT_WARN"TPM: Invalid locality for tpm_write_cmd_fifo()\n");
        return false;
    }
    if ( in == NULL || out == NULL || out_size == NULL ) {
        printk(TBOOT_WARN"TPM: Invalid parameter for tpm_write_cmd_fifo()\n");
        return false;
    }
    if ( in_size < CMD_HEAD_SIZE || *out_size < RSP_HEAD_SIZE ) {
        printk(TBOOT_WARN"TPM: in/out buf size must be larger than 10 bytes\n");
        return false;
    }

    if ( !tpm_validate_locality(locality) ) {
        printk(TBOOT_WARN"TPM: Locality %d is not open\n", locality);
        return false;
    }

    if ( !tpm_wait_cmd_ready(locality) )   return false;

#ifdef TPM_TRACE
    {
        printk(TBOOT_DETA"TPM: cmd size = 0x%x\nTPM: cmd content: ", in_size);
        print_hex("TPM: \t", in, in_size);
    }
#endif

    /* write the command to the TPM FIFO */
    offset = 0;
    do {
        i = 0;
        do {
            /* find out how many bytes the TPM can accept in a row */
            row_size = tpm_get_burst_count(locality);
            if ( row_size > 0 )   break;
            else  cpu_relax();
            i++;
        } while ( i <= TPM_CMD_WRITE_TIME_OUT );
        if ( i > TPM_CMD_WRITE_TIME_OUT ) {
            printk(TBOOT_ERR"TPM: write cmd timeout\n");
            ret = false;
            goto RelinquishControl;
        }

        for ( ; row_size > 0 && offset < in_size; row_size--, offset++ )  write_tpm_reg(locality, TPM_REG_DATA_FIFO,  (tpm_reg_data_fifo_t *)&in[offset]);
    } while ( offset < in_size );

    i = 0;
    do {
        if ( tpm_check_expect_status(locality) )  break;
        else   cpu_relax();
        i++;
    } while ( i <= TPM_DATA_AVAIL_TIME_OUT );
    if ( i > TPM_DATA_AVAIL_TIME_OUT ) {
        printk(TBOOT_ERR"TPM: wait for expect becoming 0 timeout\n");
        ret = false;
        goto RelinquishControl;
    }

    /* command has been written to the TPM, it is time to execute it. */
    tpm_execute_cmd(locality);

    /* check for data available */
    i = 0;
    do {
        if ( tpm_check_da_status(locality) )  break;
        else  cpu_relax();
        i++;
    } while ( i <= TPM_DATA_AVAIL_TIME_OUT );
    if ( i > TPM_DATA_AVAIL_TIME_OUT ) {
        printk(TBOOT_ERR"TPM: wait for data available timeout\n");
        ret = false;
        goto RelinquishControl;
    }

    rsp_size = 0;
    offset = 0;
    do {
        /* find out how many bytes the TPM returned in a row */
        i = 0;
        do {
            row_size = tpm_get_burst_count(locality);
            if ( row_size > 0 )  break;
            else cpu_relax();
            i++;
        } while ( i <= TPM_RSP_READ_TIME_OUT );
        if ( i > TPM_RSP_READ_TIME_OUT ) {
            printk(TBOOT_ERR"TPM: read rsp timeout\n");
            ret = false;
            goto RelinquishControl;
        }

        for ( ; row_size > 0 && offset < *out_size; row_size--, offset++ ) {
            if ( offset < *out_size )  read_tpm_reg(locality, TPM_REG_DATA_FIFO, (tpm_reg_data_fifo_t *)&out[offset]);
            else {
                /* discard the responded bytes exceeding out buf size */
                tpm_reg_data_fifo_t discard;
                read_tpm_reg(locality, TPM_REG_DATA_FIFO,  (tpm_reg_data_fifo_t *)&discard);
            }

            /* get outgoing data size */
            if ( offset == RSP_RST_OFFSET - 1 ) {
                reverse_copy(&rsp_size, &out[RSP_SIZE_OFFSET], sizeof(rsp_size));
            }
        }
    } while ( offset < RSP_RST_OFFSET || (offset < rsp_size && offset < *out_size) );

    *out_size = (*out_size > rsp_size) ? rsp_size : *out_size;

#ifdef TPM_TRACE
    {
        printk(TBOOT_INFO"TPM: response size = %d\n", *out_size);
        printk(TBOOT_DETA"TPM: response content: ");
        print_hex("TPM: \t", out, *out_size);
    }
#endif

    tpm_send_cmd_ready_status(locality);

RelinquishControl:
    /* deactivate current locality */
    reg_acc._raw[0] = 0;
    reg_acc.active_locality = 1;
    write_tpm_reg(locality, TPM_REG_ACCESS, &reg_acc);

    return ret;
}


bool tpm_submit_cmd_crb(u32 locality, u8 *in, u32 in_size,  u8 *out, u32 *out_size)
{
    uint32_t i;
    
    bool ret = true;

    //tpm_reg_loc_ctrl_t reg_loc_ctrl;
    tpm_reg_ctrl_start_t start;

    tpm_reg_ctrl_cmdsize_t  CmdSize;
    tpm_reg_ctrl_cmdaddr_t  CmdAddr;
    tpm_reg_ctrl_rspsize_t  RspSize;
    tpm_reg_ctrl_rspaddr_t  RspAddr;
    uint32_t  tpm_crb_data_buffer_base;
	
    if ( locality >= TPM_NR_LOCALITIES ) {
        printk(TBOOT_WARN"TPM: Invalid locality for tpm_submit_cmd_crb()\n");
        return false;
    }
    if ( in == NULL || out == NULL || out_size == NULL ) {
        printk(TBOOT_WARN"TPM: Invalid parameter for tpm_submit_cmd_crb()\n");
        return false;
    }
    if ( in_size < CMD_HEAD_SIZE || *out_size < RSP_HEAD_SIZE ) {
        printk(TBOOT_WARN"TPM: in/out buf size must be larger than 10 bytes\n");
        return false;
    }

    if ( !tpm_validate_locality_crb(locality) ) {
        printk(TBOOT_WARN"TPM: CRB Interface Locality %d is not open\n", locality);
        return false;
    }

    if ( !tpm_wait_cmd_ready_crb(locality) ) {
        printk(TBOOT_WARN"TPM: tpm_wait_cmd_read_crb failed\n");
	 return false;
    }

#ifdef TPM_TRACE
    {
        printk(TBOOT_DETA"TPM: Before submit, cmd size = 0x%x\nTPM: Before submit, cmd content: ", in_size);
        print_hex("TPM: \t", in, in_size);
    }
#endif

    /* write the command to the TPM CRB  buffer 01-04-2016  */
//copy *in and size to crb buffer



    CmdAddr.cmdladdr = TPM_LOCALITY_CRB_BASE_N(locality) | TPM_CRB_DATA_BUFFER;
    CmdAddr.cmdhaddr = 0;
    RspAddr.rspaddr = TPM_LOCALITY_CRB_BASE_N(locality) | TPM_CRB_DATA_BUFFER;
    CmdSize.cmdsize = TPMCRBBUF_LEN;
    RspSize.rspsize = TPMCRBBUF_LEN;
    tpm_crb_data_buffer_base = TPM_CRB_DATA_BUFFER;
	

 #ifdef TPM_TRACE  
       printk(TBOOT_INFO"CmdAddr.cmdladdr is 0x%x\n",CmdAddr.cmdladdr);
       printk(TBOOT_INFO"CmdAddr.cmdhaddr is 0x%x\n",CmdAddr.cmdhaddr);

	printk(TBOOT_INFO"CmdSize.cmdsize is 0x%x\n",CmdSize.cmdsize);
	printk(TBOOT_INFO"RspAddr.rspaddr is 0x%Lx\n",RspAddr.rspaddr);
	printk(TBOOT_INFO"RspSize.rspsize is 0x%x\n",RspSize.rspsize);
	
#endif
    
    write_tpm_reg(locality, TPM_CRB_CTRL_CMD_ADDR, &CmdAddr);
    write_tpm_reg(locality, TPM_CRB_CTRL_CMD_SIZE, &CmdSize);
    write_tpm_reg(locality, TPM_CRB_CTRL_RSP_ADDR, &RspAddr);
    write_tpm_reg(locality, TPM_CRB_CTRL_RSP_SIZE, &RspSize);
    // write the command to the buffer
    for ( i = 0 ; i< in_size; i++ )  {
        write_tpm_reg(locality, tpm_crb_data_buffer_base++,  (tpm_reg_data_crb_t *)&in[i]);
        //tpm_crb_data_buffer_base++;
    }

    /* command has been written to the TPM, it is time to execute it. */
    start.start = 1;
    write_tpm_reg(locality, TPM_CRB_CTRL_START, &start);
    //read_tpm_reg(locality, TPM_CRB_CTRL_START, &start);
    printk(TBOOT_INFO"tpm_ctrl_start.start is 0x%x\n",start.start);
	
    /* check for data available */
    i = 0;
    do {
	   read_tpm_reg(locality, TPM_CRB_CTRL_START, &start);
        //printk(TBOOT_INFO"tpm_ctrl_start.start is 0x%x\n",start.start);
          if ( start.start == 0 ) break;
          else  cpu_relax();
          i++;
    } while ( i <= TPM_DATA_AVAIL_TIME_OUT );

    if ( i > TPM_DATA_AVAIL_TIME_OUT ) {
        printk(TBOOT_ERR"TPM: wait for data available timeout\n");
        ret = false;
        goto RelinquishControl;
    }

    tpm_crb_data_buffer_base = TPM_CRB_DATA_BUFFER;

    for ( i = 0 ; i< *out_size; i++ )  {
        read_tpm_reg(locality, tpm_crb_data_buffer_base++, (tpm_reg_data_crb_t *)&out[i]);
        //tpm_crb_data_buffer_base++;
    }

  

#ifdef TPM_TRACE
    {
        printk(TBOOT_INFO"TPM: After cmd submit, response size = 0x%x\n", *out_size);
        printk(TBOOT_DETA"TPM: After cmd submit, response content: ");
        print_hex("TPM: \t", out, *out_size);
    }
#endif

    //tpm_send_cmd_ready_status_crb(locality);

RelinquishControl:
    /* deactivate current locality */
   // reg_loc_ctrl._raw[0] = 0;
    //reg_loc_ctrl.relinquish = 1;
    //write_tpm_reg(locality, TPM_REG_LOC_CTRL, &reg_loc_ctrl);

    return ret;

}


bool release_locality(uint32_t locality)
{
    uint32_t i;
#ifdef TPM_TRACE
    printk(TBOOT_DETA"TPM: releasing locality %u\n", locality);
#endif

    if ( !tpm_validate_locality(locality) )   return true;

    tpm_reg_access_t reg_acc;
    read_tpm_reg(locality, TPM_REG_ACCESS, &reg_acc);
    if ( reg_acc.active_locality == 0 )    return true;

    /* make inactive by writing a 1 */
    reg_acc._raw[0] = 0;
    reg_acc.active_locality = 1;
    write_tpm_reg(locality, TPM_REG_ACCESS, &reg_acc);

    i = 0;
    do {
        read_tpm_reg(locality, TPM_REG_ACCESS, &reg_acc);
        if ( reg_acc.active_locality == 0 )
            return true;
        else
            cpu_relax();
        i++;
    } while ( i <= TPM_ACTIVE_LOCALITY_TIME_OUT );

    printk(TBOOT_INFO"TPM: access reg release locality timeout\n");
    return false;
}

bool tpm_relinquish_locality_crb(uint32_t locality)
{
    uint32_t i;
    tpm_reg_loc_state_t reg_loc_state;
    tpm_reg_loc_ctrl_t reg_loc_ctrl;
	
#ifdef TPM_TRACE
    printk(TBOOT_DETA"TPM: releasing CRB_INF locality %u\n", locality);
#endif

    if ( !tpm_validate_locality_crb(locality) )   return true;
    read_tpm_reg(locality, TPM_REG_LOC_STATE, &reg_loc_state);
    if ( reg_loc_state.loc_assigned == 0 )    return true;

    /* make inactive by writing a 1 */
    reg_loc_ctrl._raw[0] = 0;
    reg_loc_ctrl.relinquish = 1;
    write_tpm_reg(locality, TPM_REG_LOC_CTRL, &reg_loc_ctrl);

    i = 0;
    do {
        read_tpm_reg(locality, TPM_REG_LOC_STATE, &reg_loc_state);
        if ( reg_loc_state.loc_assigned == 0 )    return true;
        else cpu_relax();
        i++;
    } while ( i <= TPM_ACTIVE_LOCALITY_TIME_OUT );

    printk(TBOOT_INFO"TPM: CRB_INF release locality timeout\n");
    return false;
}



bool is_tpm_crb(void)
{      
     tpm_crb_interface_id_t crb_interface;
     read_tpm_reg(0, TPM_INTERFACE_ID, &crb_interface);
     if (crb_interface.interface_type == TPM_INTERFACE_ID_CRB  ) {
	 printk(TBOOT_INFO"TPM: PTP CRB interface is active...\n");
	 if (g_tpm_family != TPM_IF_20_CRB ) g_tpm_family = TPM_IF_20_CRB;
        return true;
     }
     if (crb_interface.interface_type == TPM_INTERFACE_ID_FIFO_20) {
	  printk(TBOOT_INFO"TPM: TPM 2.0 FIFO interface is active...\n");     
	  if (g_tpm_family != TPM_IF_20_FIFO) g_tpm_family = TPM_IF_20_FIFO;
     }
     return false;	
}


bool prepare_tpm(void)
{
    /*
     * must ensure TPM_ACCESS_0.activeLocality bit is clear
     * (: locality is not active)
     */
   if (is_tpm_crb()) 
//   	return release_locality_crb(0);
       return true;
   else 
   	return release_locality(0);
}

bool tpm_request_locality_crb(uint32_t locality){

    uint32_t            i;
    tpm_reg_loc_state_t  reg_loc_state;
    tpm_reg_loc_ctrl_t    reg_loc_ctrl;
    /* request access to the TPM from locality N */
    reg_loc_ctrl._raw[0] = 0;
    reg_loc_ctrl.requestAccess = 1;
    write_tpm_reg(locality, TPM_REG_LOC_CTRL, &reg_loc_ctrl);

    i = 0;
    do {
        read_tpm_reg(locality, TPM_REG_LOC_STATE, &reg_loc_state);
        if ( reg_loc_state.active_locality == locality && reg_loc_state.loc_assigned == 1)
            break;
        else
            cpu_relax();
        i++;
    } while ( i <= TPM_ACTIVE_LOCALITY_TIME_OUT);

    if ( i > TPM_ACTIVE_LOCALITY_TIME_OUT ) {
        printk(TBOOT_ERR"TPM: access loc request use timeout\n");
        return false;
    }

    return true;

}

bool tpm_workaround_crb(void)
{
    tpm_reg_ctrl_cmdsize_t  CmdSize;
    tpm_reg_ctrl_cmdaddr_t  CmdAddr;
    tpm_reg_ctrl_rspsize_t  RspSize;
    tpm_reg_ctrl_rspaddr_t  RspAddr;
    u32 locality = 0;

    if (!tpm_request_locality_crb(locality))
        return false;

    CmdAddr.cmdladdr = TPM_LOCALITY_CRB_BASE_N(locality) | TPM_CRB_DATA_BUFFER;
    CmdAddr.cmdhaddr = 0;
    RspAddr.rspaddr = TPM_LOCALITY_CRB_BASE_N(locality) | TPM_CRB_DATA_BUFFER;
    CmdSize.cmdsize = TPMCRBBUF_LEN;
    RspSize.rspsize = TPMCRBBUF_LEN;

    write_tpm_reg(locality, TPM_CRB_CTRL_CMD_ADDR, &CmdAddr);
    write_tpm_reg(locality, TPM_CRB_CTRL_CMD_SIZE, &CmdSize);
    write_tpm_reg(locality, TPM_CRB_CTRL_RSP_ADDR, &RspAddr);
    write_tpm_reg(locality, TPM_CRB_CTRL_RSP_SIZE, &RspSize);

    return true;
}

bool tpm_detect(void)
{
    if (is_tpm_crb()) {
         printk(TBOOT_INFO"TPM: This is Intel PTT, TPM Family 0x%d\n", g_tpm_family);
         if (!txt_is_launched()) {
               if ( tpm_validate_locality_crb(0) ) 
	             printk(TBOOT_INFO"TPM: CRB_INF Locality 0 is open\n");
		 else {
		 	printk(TBOOT_INFO"TPM: CRB_INF request access to Locality 0...\n");
			if (!tpm_request_locality_crb(0)) {
			        printk(TBOOT_ERR"TPM: CRB_INF Locality 0 request failed...\n");
				 return false;
			 }
                }
	  }
    	  else {
              if ( tpm_validate_locality_crb(2) ) 
		     printk(TBOOT_INFO"TPM: CRB_INF Locality 2 is open\n");
		else {	 
		      printk(TBOOT_INFO"TPM: CRB_INF request access to Locality 2...\n");
		      if (!tpm_request_locality_crb(2)) {
		 	     printk(TBOOT_ERR"TPM: CRB_INF Locality 2 request failed...\n");
                          return false;
			}
		}
    	  }
    }
    else {
		g_tpm = &tpm_12_if; /* Don't leave g_tpm as NULL*/
		if ( tpm_validate_locality(0) )  printk(TBOOT_INFO"TPM: FIFO_INF Locality 0 is open\n");
		else {	
			printk(TBOOT_ERR"TPM: FIFO_INF Locality 0 is not open\n");
			return false;
			}
		/* determine TPM family from command check */
		if ( g_tpm->check() )  {
			g_tpm_family = TPM_IF_12;
			printk(TBOOT_INFO"TPM: discrete TPM1.2 Family 0x%d\n", g_tpm_family);	
			}
		else {
			g_tpm_family = TPM_IF_20_FIFO;
			printk(TBOOT_INFO"TPM: discrete TPM2.0 Family 0x%d\n", g_tpm_family);
			}
	}
   
    if (g_tpm_family == TPM_IF_12)  g_tpm = &tpm_12_if;
    if (g_tpm_family == TPM_IF_20_FIFO)  g_tpm = &tpm_20_if;
    if (g_tpm_family == TPM_IF_20_CRB)  g_tpm = &tpm_20_if;

   /*  if (!txt_is_launched()) 
	   g_tpm->cur_loc = 0;
     else 
	   g_tpm->cur_loc = 2;
	 	
    g_tpm->timeout.timeout_a = TIMEOUT_A;
    g_tpm->timeout.timeout_b = TIMEOUT_B;
    g_tpm->timeout.timeout_c = TIMEOUT_C;
    g_tpm->timeout.timeout_d = TIMEOUT_D;
*/
    return g_tpm->init(g_tpm);
}

void tpm_print(struct tpm_if *ti)
{
    if ( ti == NULL )
        return;

    printk(TBOOT_INFO"TPM attribute:\n");
    printk(TBOOT_INFO"\t extend policy: %d\n", ti->extpol);
    printk(TBOOT_INFO"\t current alg id: 0x%x\n", ti->cur_alg);
    printk(TBOOT_INFO"\t timeout values: A: %u, B: %u, C: %u, D: %u\n", ti->timeout.timeout_a, ti->timeout.timeout_b, ti->timeout.timeout_c, ti->timeout.timeout_d);
}

/*
 * Local variables:
 * mode: C
 * c-set-style: "BSD"
 * c-basic-offset: 4
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */