2 * Copyright (C) 2016 IBM Corporation
5 * Nayna Jain <nayna@linux.vnet.ibm.com>
7 * Access to TPM 2.0 event log as written by Firmware.
8 * It assumes that writer of event log has followed TCG Specification
9 * for Family "2.0" and written the event data in little endian.
10 * With that, it doesn't need any endian conversion for structure
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
19 #include <linux/seq_file.h>
21 #include <linux/security.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
26 #include "tpm_eventlog.h"
29 * calc_tpm2_event_size() - calculate the event size, where event
30 * is an entry in the TPM 2.0 event log. The event is of type Crypto
31 * Agile Log Entry Format as defined in TCG EFI Protocol Specification
34 * @event: event whose size is to be calculated.
35 * @event_header: the first event in the event log.
37 * Returns size of the event. If it is an invalid event, returns 0.
39 static int calc_tpm2_event_size(struct tcg_pcr_event2
*event
,
40 struct tcg_pcr_event
*event_header
)
42 struct tcg_efi_specid_event
*efispecid
;
43 struct tcg_event_field
*event_field
;
53 marker_start
= marker
;
54 marker
= marker
+ sizeof(event
->pcr_idx
) + sizeof(event
->event_type
)
55 + sizeof(event
->count
);
57 efispecid
= (struct tcg_efi_specid_event
*)event_header
->event
;
59 for (i
= 0; (i
< event
->count
) && (i
< TPM2_ACTIVE_PCR_BANKS
);
61 halg_size
= sizeof(event
->digests
[i
].alg_id
);
62 memcpy(&halg
, marker
, halg_size
);
63 marker
= marker
+ halg_size
;
64 for (j
= 0; (j
< efispecid
->num_algs
); j
++) {
65 if (halg
== efispecid
->digest_sizes
[j
].alg_id
) {
67 efispecid
->digest_sizes
[j
].digest_size
;
73 event_field
= (struct tcg_event_field
*)marker
;
74 marker
= marker
+ sizeof(event_field
->event_size
)
75 + event_field
->event_size
;
76 size
= marker
- marker_start
;
78 if ((event
->event_type
== 0) && (event_field
->event_size
== 0))
84 static void *tpm2_bios_measurements_start(struct seq_file
*m
, loff_t
*pos
)
86 struct tpm_chip
*chip
= m
->private;
87 struct tpm_bios_log
*log
= &chip
->log
;
88 void *addr
= log
->bios_event_log
;
89 void *limit
= log
->bios_event_log_end
;
90 struct tcg_pcr_event
*event_header
;
91 struct tcg_pcr_event2
*event
;
96 size
= sizeof(struct tcg_pcr_event
) - sizeof(event_header
->event
)
97 + event_header
->event_size
;
100 if (addr
+ size
< limit
) {
101 if ((event_header
->event_type
== 0) &&
102 (event_header
->event_size
== 0))
104 return SEQ_START_TOKEN
;
111 size
= calc_tpm2_event_size(event
, event_header
);
112 if ((addr
+ size
>= limit
) || (size
== 0))
116 for (i
= 0; i
< (*pos
- 1); i
++) {
118 size
= calc_tpm2_event_size(event
, event_header
);
120 if ((addr
+ size
>= limit
) || (size
== 0))
128 static void *tpm2_bios_measurements_next(struct seq_file
*m
, void *v
,
131 struct tcg_pcr_event
*event_header
;
132 struct tcg_pcr_event2
*event
;
133 struct tpm_chip
*chip
= m
->private;
134 struct tpm_bios_log
*log
= &chip
->log
;
135 void *limit
= log
->bios_event_log_end
;
139 event_header
= log
->bios_event_log
;
141 if (v
== SEQ_START_TOKEN
) {
142 event_size
= sizeof(struct tcg_pcr_event
) -
143 sizeof(event_header
->event
) + event_header
->event_size
;
144 marker
= event_header
;
147 event_size
= calc_tpm2_event_size(event
, event_header
);
153 marker
= marker
+ event_size
;
159 event_size
= calc_tpm2_event_size(event
, event_header
);
160 if (((v
+ event_size
) >= limit
) || (event_size
== 0))
167 static void tpm2_bios_measurements_stop(struct seq_file
*m
, void *v
)
171 static int tpm2_binary_bios_measurements_show(struct seq_file
*m
, void *v
)
173 struct tpm_chip
*chip
= m
->private;
174 struct tpm_bios_log
*log
= &chip
->log
;
175 struct tcg_pcr_event
*event_header
= log
->bios_event_log
;
176 struct tcg_pcr_event2
*event
= v
;
180 if (v
== SEQ_START_TOKEN
) {
181 size
= sizeof(struct tcg_pcr_event
) -
182 sizeof(event_header
->event
) + event_header
->event_size
;
184 temp_ptr
= event_header
;
187 seq_write(m
, temp_ptr
, size
);
189 size
= calc_tpm2_event_size(event
, event_header
);
192 seq_write(m
, temp_ptr
, size
);
198 const struct seq_operations tpm2_binary_b_measurements_seqops
= {
199 .start
= tpm2_bios_measurements_start
,
200 .next
= tpm2_bios_measurements_next
,
201 .stop
= tpm2_bios_measurements_stop
,
202 .show
= tpm2_binary_bios_measurements_show
,