forked from sysml/mini-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpm_tis.c
1354 lines (1184 loc) · 34.6 KB
/
tpm_tis.c
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
/*
* Copyright (c) 2010-2012 United States Government, as represented by
* the Secretary of Defense. All rights reserved.
*
* This code has been derived from drivers/char/tpm.c
* from the linux kernel
*
* Copyright (C) 2004 IBM Corporation
*
* This code has also been derived from drivers/char/tpm/tpm_tis.c
* from the linux kernel
*
* Copyright (C) 2005, 2006 IBM Corporation
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, version 2
* of the License
*/
#include <mini-os/ioremap.h>
#include <mini-os/iorw.h>
#include <mini-os/tpm_tis.h>
#include <mini-os/os.h>
#include <mini-os/sched.h>
#include <mini-os/byteorder.h>
#include <mini-os/events.h>
#include <mini-os/wait.h>
#include <mini-os/xmalloc.h>
#include <errno.h>
#include <stdbool.h>
#ifndef min
#define min( a, b ) ( ((a) < (b)) ? (a) : (b) )
#endif
#define TPM_HEADER_SIZE 10
#define TPM_BUFSIZE 2048
struct tpm_input_header {
uint16_t tag;
uint32_t length;
uint32_t ordinal;
}__attribute__((packed));
struct tpm_output_header {
uint16_t tag;
uint32_t length;
uint32_t return_code;
}__attribute__((packed));
struct stclear_flags_t {
uint16_t tag;
uint8_t deactivated;
uint8_t disableForceClear;
uint8_t physicalPresence;
uint8_t physicalPresenceLock;
uint8_t bGlobalLock;
}__attribute__((packed));
struct tpm_version_t {
uint8_t Major;
uint8_t Minor;
uint8_t revMajor;
uint8_t revMinor;
}__attribute__((packed));
struct tpm_version_1_2_t {
uint16_t tag;
uint8_t Major;
uint8_t Minor;
uint8_t revMajor;
uint8_t revMinor;
}__attribute__((packed));
struct timeout_t {
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
}__attribute__((packed));
struct duration_t {
uint32_t tpm_short;
uint32_t tpm_medium;
uint32_t tpm_long;
}__attribute__((packed));
struct permanent_flags_t {
uint16_t tag;
uint8_t disable;
uint8_t ownership;
uint8_t deactivated;
uint8_t readPubek;
uint8_t disableOwnerClear;
uint8_t allowMaintenance;
uint8_t physicalPresenceLifetimeLock;
uint8_t physicalPresenceHWEnable;
uint8_t physicalPresenceCMDEnable;
uint8_t CEKPUsed;
uint8_t TPMpost;
uint8_t TPMpostLock;
uint8_t FIPS;
uint8_t operator;
uint8_t enableRevokeEK;
uint8_t nvLocked;
uint8_t readSRKPub;
uint8_t tpmEstablished;
uint8_t maintenanceDone;
uint8_t disableFullDALogicInfo;
}__attribute__((packed));
typedef union {
struct permanent_flags_t perm_flags;
struct stclear_flags_t stclear_flags;
bool owned;
uint32_t num_pcrs;
struct tpm_version_t tpm_version;
struct tpm_version_1_2_t tpm_version_1_2;
uint32_t manufacturer_id;
struct timeout_t timeout;
struct duration_t duration;
} cap_t;
struct tpm_getcap_params_in {
uint32_t cap;
uint32_t subcap_size;
uint32_t subcap;
}__attribute__((packed));
struct tpm_getcap_params_out {
uint32_t cap_size;
cap_t cap;
}__attribute__((packed));
struct tpm_readpubek_params_out {
uint8_t algorithm[4];
uint8_t encscheme[2];
uint8_t sigscheme[2];
uint32_t paramsize;
uint8_t parameters[12]; /*assuming RSA*/
uint32_t keysize;
uint8_t modulus[256];
uint8_t checksum[20];
}__attribute__((packed));
typedef union {
struct tpm_input_header in;
struct tpm_output_header out;
} tpm_cmd_header;
#define TPM_DIGEST_SIZE 20
struct tpm_pcrread_out {
uint8_t pcr_result[TPM_DIGEST_SIZE];
}__attribute__((packed));
struct tpm_pcrread_in {
uint32_t pcr_idx;
}__attribute__((packed));
struct tpm_pcrextend_in {
uint32_t pcr_idx;
uint8_t hash[TPM_DIGEST_SIZE];
}__attribute__((packed));
typedef union {
struct tpm_getcap_params_out getcap_out;
struct tpm_readpubek_params_out readpubek_out;
uint8_t readpubek_out_buffer[sizeof(struct tpm_readpubek_params_out)];
struct tpm_getcap_params_in getcap_in;
struct tpm_pcrread_in pcrread_in;
struct tpm_pcrread_out pcrread_out;
struct tpm_pcrextend_in pcrextend_in;
} tpm_cmd_params;
struct tpm_cmd_t {
tpm_cmd_header header;
tpm_cmd_params params;
}__attribute__((packed));
enum tpm_duration {
TPM_SHORT = 0,
TPM_MEDIUM = 1,
TPM_LONG = 2,
TPM_UNDEFINED,
};
#define TPM_MAX_ORDINAL 243
#define TPM_MAX_PROTECTED_ORDINAL 12
#define TPM_PROTECTED_ORDINAL_MASK 0xFF
extern const uint8_t tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL];
extern const uint8_t tpm_ordinal_duration[TPM_MAX_ORDINAL];
#define TPM_DIGEST_SIZE 20
#define TPM_ERROR_SIZE 10
#define TPM_RET_CODE_IDX 6
/* tpm_capabilities */
#define TPM_CAP_FLAG cpu_to_be32(4)
#define TPM_CAP_PROP cpu_to_be32(5)
#define CAP_VERSION_1_1 cpu_to_be32(0x06)
#define CAP_VERSION_1_2 cpu_to_be32(0x1A)
/* tpm_sub_capabilities */
#define TPM_CAP_PROP_PCR cpu_to_be32(0x101)
#define TPM_CAP_PROP_MANUFACTURER cpu_to_be32(0x103)
#define TPM_CAP_FLAG_PERM cpu_to_be32(0x108)
#define TPM_CAP_FLAG_VOL cpu_to_be32(0x109)
#define TPM_CAP_PROP_OWNER cpu_to_be32(0x111)
#define TPM_CAP_PROP_TIS_TIMEOUT cpu_to_be32(0x115)
#define TPM_CAP_PROP_TIS_DURATION cpu_to_be32(0x120)
#define TPM_INTERNAL_RESULT_SIZE 200
#define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
#define TPM_ORD_GET_CAP cpu_to_be32(101)
extern const struct tpm_input_header tpm_getcap_header;
const uint8_t tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
TPM_UNDEFINED, /* 0 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED, /* 5 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_SHORT, /* 10 */
TPM_SHORT,
};
const uint8_t tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
TPM_UNDEFINED, /* 0 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED, /* 5 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_SHORT, /* 10 */
TPM_SHORT,
TPM_MEDIUM,
TPM_LONG,
TPM_LONG,
TPM_MEDIUM, /* 15 */
TPM_SHORT,
TPM_SHORT,
TPM_MEDIUM,
TPM_LONG,
TPM_SHORT, /* 20 */
TPM_SHORT,
TPM_MEDIUM,
TPM_MEDIUM,
TPM_MEDIUM,
TPM_SHORT, /* 25 */
TPM_SHORT,
TPM_MEDIUM,
TPM_SHORT,
TPM_SHORT,
TPM_MEDIUM, /* 30 */
TPM_LONG,
TPM_MEDIUM,
TPM_SHORT,
TPM_SHORT,
TPM_SHORT, /* 35 */
TPM_MEDIUM,
TPM_MEDIUM,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_MEDIUM, /* 40 */
TPM_LONG,
TPM_MEDIUM,
TPM_SHORT,
TPM_SHORT,
TPM_SHORT, /* 45 */
TPM_SHORT,
TPM_SHORT,
TPM_SHORT,
TPM_LONG,
TPM_MEDIUM, /* 50 */
TPM_MEDIUM,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED, /* 55 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_MEDIUM, /* 60 */
TPM_MEDIUM,
TPM_MEDIUM,
TPM_SHORT,
TPM_SHORT,
TPM_MEDIUM, /* 65 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_SHORT, /* 70 */
TPM_SHORT,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED, /* 75 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_LONG, /* 80 */
TPM_UNDEFINED,
TPM_MEDIUM,
TPM_LONG,
TPM_SHORT,
TPM_UNDEFINED, /* 85 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_SHORT, /* 90 */
TPM_SHORT,
TPM_SHORT,
TPM_SHORT,
TPM_SHORT,
TPM_UNDEFINED, /* 95 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_MEDIUM, /* 100 */
TPM_SHORT,
TPM_SHORT,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED, /* 105 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_SHORT, /* 110 */
TPM_SHORT,
TPM_SHORT,
TPM_SHORT,
TPM_SHORT,
TPM_SHORT, /* 115 */
TPM_SHORT,
TPM_SHORT,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_LONG, /* 120 */
TPM_LONG,
TPM_MEDIUM,
TPM_UNDEFINED,
TPM_SHORT,
TPM_SHORT, /* 125 */
TPM_SHORT,
TPM_LONG,
TPM_SHORT,
TPM_SHORT,
TPM_SHORT, /* 130 */
TPM_MEDIUM,
TPM_UNDEFINED,
TPM_SHORT,
TPM_MEDIUM,
TPM_UNDEFINED, /* 135 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_SHORT, /* 140 */
TPM_SHORT,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED, /* 145 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_SHORT, /* 150 */
TPM_MEDIUM,
TPM_MEDIUM,
TPM_SHORT,
TPM_SHORT,
TPM_UNDEFINED, /* 155 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_SHORT, /* 160 */
TPM_SHORT,
TPM_SHORT,
TPM_SHORT,
TPM_UNDEFINED,
TPM_UNDEFINED, /* 165 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_LONG, /* 170 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED, /* 175 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_MEDIUM, /* 180 */
TPM_SHORT,
TPM_MEDIUM,
TPM_MEDIUM,
TPM_MEDIUM,
TPM_MEDIUM, /* 185 */
TPM_SHORT,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED, /* 190 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED, /* 195 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_SHORT, /* 200 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_SHORT,
TPM_SHORT, /* 205 */
TPM_SHORT,
TPM_SHORT,
TPM_SHORT,
TPM_SHORT,
TPM_MEDIUM, /* 210 */
TPM_UNDEFINED,
TPM_MEDIUM,
TPM_MEDIUM,
TPM_MEDIUM,
TPM_UNDEFINED, /* 215 */
TPM_MEDIUM,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_SHORT,
TPM_SHORT, /* 220 */
TPM_SHORT,
TPM_SHORT,
TPM_SHORT,
TPM_SHORT,
TPM_UNDEFINED, /* 225 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_SHORT, /* 230 */
TPM_LONG,
TPM_MEDIUM,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED, /* 235 */
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_UNDEFINED,
TPM_SHORT, /* 240 */
TPM_UNDEFINED,
TPM_MEDIUM,
};
const struct tpm_input_header tpm_getcap_header = {
.tag = TPM_TAG_RQU_COMMAND,
.length = cpu_to_be32(22),
.ordinal = TPM_ORD_GET_CAP
};
enum tis_access {
TPM_ACCESS_VALID = 0x80,
TPM_ACCESS_ACTIVE_LOCALITY = 0x20, /* (R) */
TPM_ACCESS_RELINQUISH_LOCALITY = 0x20,/* (W) */
TPM_ACCESS_REQUEST_PENDING = 0x04, /* (W) */
TPM_ACCESS_REQUEST_USE = 0x02, /* (W) */
};
enum tis_status {
TPM_STS_VALID = 0x80, /* (R) */
TPM_STS_COMMAND_READY = 0x40, /* (R) */
TPM_STS_DATA_AVAIL = 0x10, /* (R) */
TPM_STS_DATA_EXPECT = 0x08, /* (R) */
TPM_STS_GO = 0x20, /* (W) */
};
enum tis_int_flags {
TPM_GLOBAL_INT_ENABLE = 0x80000000,
TPM_INTF_BURST_COUNT_STATIC = 0x100,
TPM_INTF_CMD_READY_INT = 0x080,
TPM_INTF_INT_EDGE_FALLING = 0x040,
TPM_INTF_INT_EDGE_RISING = 0x020,
TPM_INTF_INT_LEVEL_LOW = 0x010,
TPM_INTF_INT_LEVEL_HIGH = 0x008,
TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
TPM_INTF_STS_VALID_INT = 0x002,
TPM_INTF_DATA_AVAIL_INT = 0x001,
};
enum tis_defaults {
TIS_MEM_BASE = 0xFED40000,
TIS_MEM_LEN = 0x5000,
TIS_SHORT_TIMEOUT = 750, /*ms*/
TIS_LONG_TIMEOUT = 2000, /*2 sec */
};
#define TPM_TIMEOUT 5
#define TPM_ACCESS(t, l) (((uint8_t*)t->pages[l]) + 0x0000)
#define TPM_INT_ENABLE(t, l) ((uint32_t*)(((uint8_t*)t->pages[l]) + 0x0008))
#define TPM_INT_VECTOR(t, l) (((uint8_t*)t->pages[l]) + 0x000C)
#define TPM_INT_STATUS(t, l) (((uint8_t*)t->pages[l]) + 0x0010)
#define TPM_INTF_CAPS(t, l) ((uint32_t*)(((uint8_t*)t->pages[l]) + 0x0014))
#define TPM_STS(t, l) ((uint8_t*)(((uint8_t*)t->pages[l]) + 0x0018))
#define TPM_DATA_FIFO(t, l) (((uint8_t*)t->pages[l]) + 0x0024)
#define TPM_DID_VID(t, l) ((uint32_t*)(((uint8_t*)t->pages[l]) + 0x0F00))
#define TPM_RID(t, l) (((uint8_t*)t->pages[l]) + 0x0F04)
struct tpm_chip {
int enabled_localities;
int locality;
unsigned long baseaddr;
uint8_t* pages[5];
int did, vid, rid;
uint8_t data_buffer[TPM_BUFSIZE];
int data_len;
s_time_t timeout_a, timeout_b, timeout_c, timeout_d;
s_time_t duration[3];
#ifdef HAVE_LIBC
int fd;
#endif
unsigned int irq;
struct wait_queue_head read_queue;
struct wait_queue_head int_queue;
};
static void __init_tpm_chip(struct tpm_chip* tpm) {
tpm->enabled_localities = TPM_TIS_EN_LOCLALL;
tpm->locality = -1;
tpm->baseaddr = 0;
tpm->pages[0] = tpm->pages[1] = tpm->pages[2] = tpm->pages[3] = tpm->pages[4] = NULL;
tpm->vid = 0;
tpm->did = 0;
tpm->irq = 0;
init_waitqueue_head(&tpm->read_queue);
init_waitqueue_head(&tpm->int_queue);
tpm->data_len = -1;
#ifdef HAVE_LIBC
tpm->fd = -1;
#endif
}
/*
* Returns max number of nsecs to wait
*/
s_time_t tpm_calc_ordinal_duration(struct tpm_chip *chip,
uint32_t ordinal)
{
int duration_idx = TPM_UNDEFINED;
s_time_t duration = 0;
if (ordinal < TPM_MAX_ORDINAL)
duration_idx = tpm_ordinal_duration[ordinal];
else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
TPM_MAX_PROTECTED_ORDINAL)
duration_idx =
tpm_protected_ordinal_duration[ordinal &
TPM_PROTECTED_ORDINAL_MASK];
if (duration_idx != TPM_UNDEFINED) {
duration = chip->duration[duration_idx];
}
if (duration <= 0) {
return SECONDS(120);
}
else
{
return duration;
}
}
static int locality_enabled(struct tpm_chip* tpm, int l) {
return tpm->enabled_localities & (1 << l);
}
static int check_locality(struct tpm_chip* tpm, int l) {
if(locality_enabled(tpm, l) && (ioread8(TPM_ACCESS(tpm, l)) &
(TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
(TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
return l;
}
return -1;
}
void release_locality(struct tpm_chip* tpm, int l, int force)
{
if (locality_enabled(tpm, l) && (force || (ioread8(TPM_ACCESS(tpm, l)) &
(TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
(TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID))) {
iowrite8(TPM_ACCESS(tpm, l), TPM_ACCESS_RELINQUISH_LOCALITY);
}
}
int tpm_tis_request_locality(struct tpm_chip* tpm, int l) {
s_time_t stop;
/*Make sure locality is valid */
if(!locality_enabled(tpm, l)) {
printk("tpm_tis_change_locality() Tried to change to locality %d, but it is disabled or invalid!\n", l);
return -1;
}
/* Check if we already have the current locality */
if(check_locality(tpm, l) >= 0) {
return tpm->locality = l;
}
/* Set the new locality*/
iowrite8(TPM_ACCESS(tpm, l), TPM_ACCESS_REQUEST_USE);
if(tpm->irq) {
/* Wait for interrupt */
wait_event_deadline(tpm->int_queue, (check_locality(tpm, l) >= 0), NOW() + tpm->timeout_a);
/* FIXME: Handle timeout event, should return error in that case */
return l;
} else {
/* Wait for burstcount */
stop = NOW() + tpm->timeout_a;
do {
if(check_locality(tpm, l) >= 0) {
return tpm->locality = l;
}
msleep(TPM_TIMEOUT);
} while(NOW() < stop);
}
printk("REQ LOCALITY FAILURE\n");
return -1;
}
static uint8_t tpm_tis_status(struct tpm_chip* tpm) {
return ioread8(TPM_STS(tpm, tpm->locality));
}
/* This causes the current command to be aborted */
static void tpm_tis_ready(struct tpm_chip* tpm) {
iowrite8(TPM_STS(tpm, tpm->locality), TPM_STS_COMMAND_READY);
}
#define tpm_tis_cancel_cmd(v) tpm_tis_ready(v)
static int get_burstcount(struct tpm_chip* tpm) {
s_time_t stop;
int burstcnt;
stop = NOW() + tpm->timeout_d;
do {
burstcnt = ioread8((TPM_STS(tpm, tpm->locality) + 1));
burstcnt += ioread8(TPM_STS(tpm, tpm->locality) + 2) << 8;
if (burstcnt) {
return burstcnt;
}
msleep(TPM_TIMEOUT);
} while(NOW() < stop);
return -EBUSY;
}
static int wait_for_stat(struct tpm_chip* tpm, uint8_t mask,
unsigned long timeout, struct wait_queue_head* queue) {
s_time_t stop;
uint8_t status;
status = tpm_tis_status(tpm);
if((status & mask) == mask) {
return 0;
}
if(tpm->irq) {
wait_event_deadline(*queue, ((tpm_tis_status(tpm) & mask) == mask), timeout);
/* FIXME: Check for timeout and return -ETIME */
return 0;
} else {
stop = NOW() + timeout;
do {
msleep(TPM_TIMEOUT);
status = tpm_tis_status(tpm);
if((status & mask) == mask)
return 0;
} while( NOW() < stop);
}
return -ETIME;
}
static int recv_data(struct tpm_chip* tpm, uint8_t* buf, size_t count) {
int size = 0;
int burstcnt;
while( size < count &&
wait_for_stat(tpm,
TPM_STS_DATA_AVAIL | TPM_STS_VALID,
tpm->timeout_c,
&tpm->read_queue)
== 0) {
burstcnt = get_burstcount(tpm);
for(; burstcnt > 0 && size < count; --burstcnt)
{
buf[size++] = ioread8(TPM_DATA_FIFO(tpm, tpm->locality));
}
}
return size;
}
int tpm_tis_recv(struct tpm_chip* tpm, uint8_t* buf, size_t count) {
int size = 0;
int expected, status;
if (count < TPM_HEADER_SIZE) {
size = -EIO;
goto out;
}
/* read first 10 bytes, including tag, paramsize, and result */
if((size =
recv_data(tpm, buf, TPM_HEADER_SIZE)) < TPM_HEADER_SIZE) {
printk("Error reading tpm cmd header\n");
goto out;
}
expected = be32_to_cpu(*((uint32_t*)(buf + 2)));
if(expected > count) {
size = -EIO;
goto out;
}
if((size += recv_data(tpm, & buf[TPM_HEADER_SIZE],
expected - TPM_HEADER_SIZE)) < expected) {
printk("Unable to read rest of tpm command size=%d expected=%d\n", size, expected);
size = -ETIME;
goto out;
}
wait_for_stat(tpm, TPM_STS_VALID, tpm->timeout_c, &tpm->int_queue);
status = tpm_tis_status(tpm);
if(status & TPM_STS_DATA_AVAIL) {
printk("Error: left over data\n");
size = -EIO;
goto out;
}
out:
tpm_tis_ready(tpm);
release_locality(tpm, tpm->locality, 0);
return size;
}
int tpm_tis_send(struct tpm_chip* tpm, uint8_t* buf, size_t len) {
int rc;
int status, burstcnt = 0;
int count = 0;
uint32_t ordinal;
if(tpm_tis_request_locality(tpm, tpm->locality) < 0) {
return -EBUSY;
}
status = tpm_tis_status(tpm);
if((status & TPM_STS_COMMAND_READY) == 0) {
tpm_tis_ready(tpm);
if(wait_for_stat(tpm, TPM_STS_COMMAND_READY, tpm->timeout_b, &tpm->int_queue) < 0) {
rc = -ETIME;
goto out_err;
}
}
while(count < len - 1) {
burstcnt = get_burstcount(tpm);
for(;burstcnt > 0 && count < len -1; --burstcnt) {
iowrite8(TPM_DATA_FIFO(tpm, tpm->locality), buf[count++]);
}
wait_for_stat(tpm, TPM_STS_VALID, tpm->timeout_c, &tpm->int_queue);
status = tpm_tis_status(tpm);
if((status & TPM_STS_DATA_EXPECT) == 0) {
rc = -EIO;
goto out_err;
}
}
/*Write last byte*/
iowrite8(TPM_DATA_FIFO(tpm, tpm->locality), buf[count]);
wait_for_stat(tpm, TPM_STS_VALID, tpm->timeout_c, &tpm->read_queue);
status = tpm_tis_status(tpm);
if((status & TPM_STS_DATA_EXPECT) != 0) {
rc = -EIO;
goto out_err;
}
/*go and do it*/
iowrite8(TPM_STS(tpm, tpm->locality), TPM_STS_GO);
if(tpm->irq) {
/*Wait for interrupt */
ordinal = be32_to_cpu(*(buf + 6));
if(wait_for_stat(tpm,
TPM_STS_DATA_AVAIL | TPM_STS_VALID,
tpm_calc_ordinal_duration(tpm, ordinal),
&tpm->read_queue) < 0) {
rc = -ETIME;
goto out_err;
}
}
#ifdef HAVE_LIBC
if(tpm->fd >= 0) {
files[tpm->fd].read = 0;
files[tpm->fd].tpm_tis.respgot = 0;
files[tpm->fd].tpm_tis.offset = 0;
}
#endif
return len;
out_err:
tpm_tis_ready(tpm);
release_locality(tpm, tpm->locality, 0);
return rc;
}
static void tpm_tis_irq_handler(evtchn_port_t port, struct pt_regs *regs, void* data)
{
struct tpm_chip* tpm = data;
uint32_t interrupt;
int i;
interrupt = ioread32(TPM_INT_STATUS(tpm, tpm->locality));
if(interrupt == 0) {
return;
}
if(interrupt & TPM_INTF_DATA_AVAIL_INT) {
wake_up(&tpm->read_queue);
}
if(interrupt & TPM_INTF_LOCALITY_CHANGE_INT) {
for(i = 0; i < 5; ++i) {
if(check_locality(tpm, i) >= 0) {
break;
}
}
}
if(interrupt & (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
TPM_INTF_CMD_READY_INT)) {
wake_up(&tpm->int_queue);
}
/* Clear interrupts handled with TPM_EOI */
iowrite32(TPM_INT_STATUS(tpm, tpm->locality), interrupt);
ioread32(TPM_INT_STATUS(tpm, tpm->locality));
return;
}
/*
* Internal kernel interface to transmit TPM commands
*/
static ssize_t tpm_transmit(struct tpm_chip *chip, const uint8_t *buf,
size_t bufsiz)
{
ssize_t rc;
uint32_t count, ordinal;
s_time_t stop;
count = be32_to_cpu(*((uint32_t *) (buf + 2)));
ordinal = be32_to_cpu(*((uint32_t *) (buf + 6)));
if (count == 0)
return -ENODATA;
if (count > bufsiz) {
printk("Error: invalid count value %x %zx \n", count, bufsiz);
return -E2BIG;
}
//down(&chip->tpm_mutex);
if ((rc = tpm_tis_send(chip, (uint8_t *) buf, count)) < 0) {
printk("tpm_transmit: tpm_send: error %ld\n", rc);
goto out;
}
if (chip->irq)
goto out_recv;
stop = NOW() + tpm_calc_ordinal_duration(chip, ordinal);
do {
uint8_t status = tpm_tis_status(chip);
if ((status & (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) ==
(TPM_STS_DATA_AVAIL | TPM_STS_VALID))
goto out_recv;
if ((status == TPM_STS_COMMAND_READY)) {
printk("TPM Error: Operation Canceled\n");
rc = -ECANCELED;
goto out;
}
msleep(TPM_TIMEOUT); /* CHECK */
rmb();
} while (NOW() < stop);
/* Cancel the command */
tpm_tis_cancel_cmd(chip);
printk("TPM Operation Timed out\n");
rc = -ETIME;
goto out;
out_recv:
if((rc = tpm_tis_recv(chip, (uint8_t *) buf, bufsiz)) < 0) {
printk("tpm_transmit: tpm_recv: error %d\n", rc);
}
out:
//up(&chip->tpm_mutex);
return rc;
}
static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
int len, const char *desc)
{
int err;
len = tpm_transmit(chip,(uint8_t *) cmd, len);
if (len < 0)
return len;
if (len == TPM_ERROR_SIZE) {
err = be32_to_cpu(cmd->header.out.return_code);
printk("A TPM error (%d) occurred %s\n", err, desc);
return err;
}
return 0;
}
int tpm_get_timeouts(struct tpm_chip *chip)
{
struct tpm_cmd_t tpm_cmd;
struct timeout_t *timeout_cap;
struct duration_t *duration_cap;
ssize_t rc;
uint32_t timeout;
unsigned int scale = 1;
tpm_cmd.header.in = tpm_getcap_header;
tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
if((rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
"attempting to determine the timeouts")) != 0) {
printk("transmit failed %d\n", rc);
goto duration;
}
if(be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
be32_to_cpu(tpm_cmd.header.out.length) !=
sizeof(tpm_cmd.header.out) + sizeof(uint32_t) + 4 * sizeof(uint32_t)) {
return -EINVAL;
}
timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
/* Don't overwrite default if value is 0 */
timeout = be32_to_cpu(timeout_cap->a);
if(timeout && timeout < 1000) {
/* timeouts in msc rather usec */
scale = 1000;
}
if (timeout)
chip->timeout_a = MICROSECS(timeout * scale); /*Convert to msec */
timeout = be32_to_cpu(timeout_cap->b);
if (timeout)