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 /* Check if event is malformed. */
60 if (event
->count
> efispecid
->num_algs
)
63 for (i
= 0; i
< event
->count
; i
++) {
64 halg_size
= sizeof(event
->digests
[i
].alg_id
);
65 memcpy(&halg
, marker
, halg_size
);
66 marker
= marker
+ halg_size
;
67 for (j
= 0; j
< efispecid
->num_algs
; j
++) {
68 if (halg
== efispecid
->digest_sizes
[j
].alg_id
) {
70 efispecid
->digest_sizes
[j
].digest_size
;
74 /* Algorithm without known length. Such event is unparseable. */
75 if (j
== efispecid
->num_algs
)
79 event_field
= (struct tcg_event_field
*)marker
;
80 marker
= marker
+ sizeof(event_field
->event_size
)
81 + event_field
->event_size
;
82 size
= marker
- marker_start
;
84 if ((event
->event_type
== 0) && (event_field
->event_size
== 0))
90 static void *tpm2_bios_measurements_start(struct seq_file
*m
, loff_t
*pos
)
92 struct tpm_chip
*chip
= m
->private;
93 struct tpm_bios_log
*log
= &chip
->log
;
94 void *addr
= log
->bios_event_log
;
95 void *limit
= log
->bios_event_log_end
;
96 struct tcg_pcr_event
*event_header
;
97 struct tcg_pcr_event2
*event
;
102 size
= sizeof(struct tcg_pcr_event
) - sizeof(event_header
->event
)
103 + event_header
->event_size
;
106 if (addr
+ size
< limit
) {
107 if ((event_header
->event_type
== 0) &&
108 (event_header
->event_size
== 0))
110 return SEQ_START_TOKEN
;
117 size
= calc_tpm2_event_size(event
, event_header
);
118 if ((addr
+ size
>= limit
) || (size
== 0))
122 for (i
= 0; i
< (*pos
- 1); i
++) {
124 size
= calc_tpm2_event_size(event
, event_header
);
126 if ((addr
+ size
>= limit
) || (size
== 0))
134 static void *tpm2_bios_measurements_next(struct seq_file
*m
, void *v
,
137 struct tcg_pcr_event
*event_header
;
138 struct tcg_pcr_event2
*event
;
139 struct tpm_chip
*chip
= m
->private;
140 struct tpm_bios_log
*log
= &chip
->log
;
141 void *limit
= log
->bios_event_log_end
;
145 event_header
= log
->bios_event_log
;
147 if (v
== SEQ_START_TOKEN
) {
148 event_size
= sizeof(struct tcg_pcr_event
) -
149 sizeof(event_header
->event
) + event_header
->event_size
;
150 marker
= event_header
;
153 event_size
= calc_tpm2_event_size(event
, event_header
);
159 marker
= marker
+ event_size
;
165 event_size
= calc_tpm2_event_size(event
, event_header
);
166 if (((v
+ event_size
) >= limit
) || (event_size
== 0))
173 static void tpm2_bios_measurements_stop(struct seq_file
*m
, void *v
)
177 static int tpm2_binary_bios_measurements_show(struct seq_file
*m
, void *v
)
179 struct tpm_chip
*chip
= m
->private;
180 struct tpm_bios_log
*log
= &chip
->log
;
181 struct tcg_pcr_event
*event_header
= log
->bios_event_log
;
182 struct tcg_pcr_event2
*event
= v
;
186 if (v
== SEQ_START_TOKEN
) {
187 size
= sizeof(struct tcg_pcr_event
) -
188 sizeof(event_header
->event
) + event_header
->event_size
;
190 temp_ptr
= event_header
;
193 seq_write(m
, temp_ptr
, size
);
195 size
= calc_tpm2_event_size(event
, event_header
);
198 seq_write(m
, temp_ptr
, size
);
204 const struct seq_operations tpm2_binary_b_measurements_seqops
= {
205 .start
= tpm2_bios_measurements_start
,
206 .next
= tpm2_bios_measurements_next
,
207 .stop
= tpm2_bios_measurements_stop
,
208 .show
= tpm2_binary_bios_measurements_show
,