1 /*
2  * Copyright (c) 2021, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _FTPM_EVENT_LOG_PRIVATE_H
8 #define _FTPM_EVENT_LOG_PRIVATE_H
9 
10 #include <stdint.h>
11 #include <TpmTypes.h>
12 
13 #define TCG_STARTUP_LOCALITY_SIGNATURE	"StartupLocality"
14 
15 /* Event types */
16 #define EV_PREBOOT_CERT             0x00000000
17 #define EV_POST_CODE                0x00000001
18 #define EV_NO_ACTION                0x00000003
19 #define EV_SEPARATOR                0x00000004
20 #define EV_ACTION                   0x00000005
21 #define EV_S_CRTM_CONTENTS          0x00000007
22 #define EV_S_CRTM_VERSION           0x00000008
23 #define EV_CPU_MICROCODE            0x00000009
24 #define EV_PLATFORM_CONFIG_FLAGS    0x0000000A
25 #define EV_TABLE_OF_DEVICES         0x0000000B
26 #define EV_COMPACT_HASH             0x0000000C
27 #define EV_NONHOST_CODE             0x0000000F
28 #define EV_NONHOST_CONFIG           0x00000010
29 #define EV_NONHOST_INFO             0x00000011
30 #define EV_OMIT_BOOT_DEVICE_EVENTS  0x00000012
31 
32 /*
33  * Section 5.3 of TCG EFI Protocol Specification. Family 2.0.
34  * Level 00 Revision 00.13
35  * March 30, 2016
36  */
37 #define HEADER_DIGEST_SIZE      20
38 
39 /*
40  * Section 4.40 of Trusted Platform Module Library. Part 1.
41  * Level 00 Revision 01.38. September 29, 2016.
42  */
43 #define AUTH_SIZE               0x00000009
44 
45 #define TPM_PCR_EXTEND          0x00000182
46 #define TPM_ST_SESS             0x8002
47 
48 #pragma pack(1)
49 
50 /*
51  * Log Header Entry Data
52  * Ref. Table 14 TCG_EfiSpecIdEventAlgorithmSize
53  * TCG PC Client Platform Firmware Profile 9.4.5.1
54  */
55 
56 typedef struct tcg_efi_spec_id_event_algorithm_size {
57     /* Algorithm ID (hashAlg) of the Hash used by BIOS */
58     uint16_t    algorithm_id;
59     /* The size of the digest produced by the implemented Hash algorithm */
60     uint16_t    digest_size;
61 } id_event_algorithm_size_t;
62 
63 /*
64  * TCG_EfiSpecIdEvent structure
65  * Ref. Table 15 TCG_EfiSpecIdEvent
66  * TCG PC Client Platform Firmware Profile 9.4.5.1
67  */
68 
69 typedef struct id_event_struct_header {
70     /*
71      * The NUL-terminated ASCII string "Spec ID Event03".
72      * SHALL be set to {0x53, 0x70, 0x65, 0x63, 0x20, 0x49, 0x44,
73      * 0x20, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x30, 0x33, 0x00}.
74      */
75     uint8_t        signature[16];
76 
77     /*
78      * The value for the Platform Class.
79      * The enumeration is defined in the TCG ACPI Specification Client
80      * Common Header.
81      */
82     uint32_t    platform_class;
83 
84      /*
85       * The PC Client Platform Profile Specification minor version number
86       * this BIOS supports.
87       * Any BIOS supporting this version (2.0) MUST set this value to 0x00.
88       */
89     uint8_t        spec_version_minor;
90 
91     /*
92      * The PC Client Platform Profile Specification major version number
93      * this BIOS supports.
94      * Any BIOS supporting this version (2.0) MUST set this value to 0x02.
95      */
96     uint8_t        spec_version_major;
97 
98     /*
99      * The PC Client Platform Profile Specification errata version number
100      * this BIOS supports.
101      * Any BIOS supporting this version (2.0) MUST set this value to 0x02.
102      */
103     uint8_t        spec_errata;
104 
105     /*
106      * Specifies the size of the UINTN fields used in various data
107      * structures used in this specification.
108      * 0x01 indicates UINT32 and 0x02 indicates UINT64.
109      */
110     uint8_t        uintn_size;
111 
112     /*
113      * The number of Hash algorithms in the digestSizes field.
114      * This field MUST be set to a value of 0x01 or greater.
115      */
116     uint32_t    number_of_algorithms;
117 
118     /*
119      * Each TCG_EfiSpecIdEventAlgorithmSize SHALL contain an algorithmId
120      * and digestSize for each hash algorithm used in the TCG_PCR_EVENT2
121      * structure, the first of which is a Hash algorithmID and the second
122      * is the size of the respective digest.
123      */
124     id_event_algorithm_size_t    digest_size[]; /* number_of_algorithms */
125 } id_event_struct_header_t;
126 
127 typedef struct id_event_struct_data {
128     /*
129      * Size in bytes of the VendorInfo field.
130      * Maximum value MUST be FFh bytes.
131      */
132     uint8_t        vendor_info_size;
133 
134     /*
135      * Provided for use by Platform Firmware implementer. The value might
136      * be used, for example, to provide more detailed information about the
137      * specific BIOS such as BIOS revision numbers, etc. The values within
138      * this field are not standardized and are implementer-specific.
139      * Platform-specific or -unique information MUST NOT be provided in
140      * this field.
141      *
142      */
143     uint8_t        vendor_info[];    /* [vendorInfoSize] */
144 } id_event_struct_data_t;
145 
146 typedef struct tcg_efi_spec_id_event_struct {
147     id_event_struct_header_t    struct_header;
148     id_event_struct_data_t        struct_data;
149 } id_event_struct_t;
150 
151 typedef uint16_t TPM_ST;
152 typedef uint32_t TPM_CC;
153 typedef uint32_t TPM_HANDLE;
154 typedef TPM_HANDLE TPMI_DH_PCR;
155 
156 typedef struct {
157     TPM_ST tag;
158     uint32_t paramSize;
159     TPM_CC commandCode;
160 } TPM2_COMMAND_HEADER;
161 
162 typedef struct {
163     TPM2_COMMAND_HEADER    Header;
164     TPMI_DH_PCR            PcrHandle;
165     uint32_t               AuthorizationSize;
166     TPMS_AUTH_COMMAND      AuthSessionPcr;
167     TPML_DIGEST_VALUES     DigestValues;
168 } TPM2_PCR_EXTEND_COMMAND;
169 
170 /*
171  * PCR Event Header
172  * TCG EFI Protocol Specification
173  * 5.3 Event Log Header
174  */
175 typedef struct {
176 	/* PCRIndex:
177 	 * The PCR Index to which this event is extended
178 	 */
179 	uint32_t	pcr_index;
180 
181 	/* EventType:
182 	 * SHALL be an EV_NO_ACTION event
183 	 */
184 	uint32_t	event_type;
185 
186 	/* SHALL be 20 Bytes of 0x00 */
187 	uint8_t		digest[SHA1_DIGEST_SIZE];
188 
189 	/* The size of the event */
190 	uint32_t	event_size;
191 
192 	/* SHALL be a TCG_EfiSpecIdEvent */
193 	uint8_t		event[];	/* [event_data_size] */
194 } tcg_pcr_event_t;
195 
196 typedef struct {
197 	tcg_pcr_event_t			header;
198 	id_event_struct_header_t	struct_header;
199 } id_event_headers_t;
200 
201 /* TPMT_HA Structure */
202 typedef struct {
203 	/* Selector of the hash contained in the digest that implies
204 	 * the size of the digest
205 	 */
206 	uint16_t	algorithm_id;	/* AlgorithmId */
207 
208 	/* Digest, depends on AlgorithmId */
209 	uint8_t		digest[];	/* Digest[] */
210 } tpmt_ha;
211 
212 /*
213  * TPML_DIGEST_VALUES Structure
214  */
215 typedef struct {
216 	/* The number of digests in the list */
217 	uint32_t	count;			/* Count */
218 
219 	/* The list of tagged digests, as sent to the TPM as part of a
220 	 * TPM2_PCR_Extend or as received from a TPM2_PCR_Event command
221 	 */
222 	tpmt_ha		digests[];		/* Digests[Count] */ /* FIXME: TPM_TA@TpmTypes.h */
223 } tpml_digest_values;
224 
225 /*
226  * TCG_PCR_EVENT2 header
227  */
228 typedef struct {
229 	 /* The PCR Index to which this event was extended */
230 	uint32_t		pcr_index;	/* PCRIndex */
231 
232 	/* Type of event */
233 	uint32_t		event_type;	/* EventType */
234 
235 	/* Digests:
236 	 * A counted list of tagged digests, which contain the digest of
237 	 * the event data (or external data) for all active PCR banks
238 	 */
239 	tpml_digest_values	digests;	/* Digests */
240 } event2_header_t;
241 
242 typedef struct event2_data {
243 	/* The size of the event data */
244 	uint32_t		event_size;	/* EventSize */
245 
246 	/* The data of the event */
247 	uint8_t			event[];	/* Event[EventSize] */
248 } event2_data_t;
249 
250 /*
251  * Startup Locality Event
252  * Ref. TCG PC Client Platform Firmware Profile 9.4.5.3
253  */
254 typedef struct {
255 	/*
256 	 * The NUL-terminated ASCII string "StartupLocality" SHALL be
257 	 * set to {0x53 0x74 0x61 0x72 0x74 0x75 0x70 0x4C 0x6F 0x63
258 	 * 0x61 0x6C 0x69 0x74 0x79 0x00}
259 	 */
260 	uint8_t		signature[16];
261 
262 	/* The Locality Indicator which sent the TPM2_Startup command */
263 	uint8_t		startup_locality;
264 } startup_locality_event_t;
265 
266 #pragma pack()
267 
268 #endif /* _FTPM_EVENT_LOG_PRIVATE_H */
269