1 /*
2  * Copyright (c) 2020-2022, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef TCG_H
8 #define TCG_H
9 
10 #include <stdint.h>
11 
12 #define TCG_ID_EVENT_SIGNATURE_03	"Spec ID Event03"
13 #define TCG_STARTUP_LOCALITY_SIGNATURE	"StartupLocality"
14 
15 #define TCG_SPEC_VERSION_MAJOR_TPM2   2
16 #define TCG_SPEC_VERSION_MINOR_TPM2   0
17 #define TCG_SPEC_ERRATA_TPM2          2
18 
19 /*
20  * Event types
21  * Ref. Table 9 Events
22  * TCG PC Client Platform Firmware Profile Specification.
23  */
24 #define EV_PREBOOT_CERT				U(0x00000000)
25 #define EV_POST_CODE				U(0x00000001)
26 #define	EV_UNUSED				U(0x00000002)
27 #define EV_NO_ACTION				U(0x00000003)
28 #define EV_SEPARATOR				U(0x00000004)
29 #define EV_ACTION				U(0x00000005)
30 #define	EV_EVENT_TAG				U(0x00000006)
31 #define EV_S_CRTM_CONTENTS			U(0x00000007)
32 #define EV_S_CRTM_VERSION			U(0x00000008)
33 #define EV_CPU_MICROCODE			U(0x00000009)
34 #define EV_PLATFORM_CONFIG_FLAGS		U(0x0000000A)
35 #define EV_TABLE_OF_DEVICES			U(0x0000000B)
36 #define EV_COMPACT_HASH				U(0x0000000C)
37 #define	EV_IPL					U(0x0000000D)
38 #define	EV_IPL_PARTITION_DATA			U(0x0000000E)
39 #define EV_NONHOST_CODE				U(0x0000000F)
40 #define EV_NONHOST_CONFIG			U(0x00000010)
41 #define EV_NONHOST_INFO				U(0x00000011)
42 #define EV_OMIT_BOOT_DEVICE_EVENTS		U(0x00000012)
43 #define	EV_EFI_EVENT_BASE			U(0x80000000)
44 #define	EV_EFI_VARIABLE_DRIVER_CONFIG		U(0x80000001)
45 #define EV_EFI_VARIABLE_BOOT			U(0x80000002)
46 #define	EV_EFI_BOOT_SERVICES_APPLICATION	U(0x80000003)
47 #define	EV_EFI_BOOT_SERVICES_DRIVER		U(0x80000004)
48 #define	EV_EFI_RUNTIME_SERVICES_DRIVER		U(0x80000005)
49 #define	EV_EFI_GPT_EVENT			U(0x80000006)
50 #define	EV_EFI_ACTION				U(0x80000007)
51 #define	EV_EFI_PLATFORM_FIRMWARE_BLOB		U(0x80000008)
52 #define	EV_EFI_HANDOFF_TABLES			U(0x80000009)
53 #define	EV_EFI_HCRTM_EVENT			U(0x80000010)
54 #define	EV_EFI_VARIABLE_AUTHORITY		U(0x800000E0)
55 
56 /*
57  * TPM_ALG_ID constants.
58  * Ref. Table 9 - Definition of (UINT16) TPM_ALG_ID Constants
59  * Trusted Platform Module Library. Part 2: Structures
60  */
61 #define TPM_ALG_SHA256		0x000B
62 #define TPM_ALG_SHA384		0x000C
63 #define TPM_ALG_SHA512		0x000D
64 
65 /* TCG Platform Type */
66 #define PLATFORM_CLASS_CLIENT   0
67 #define PLATFORM_CLASS_SERVER   1
68 
69 /* SHA digest sizes in bytes */
70 #define SHA1_DIGEST_SIZE	20
71 #define SHA256_DIGEST_SIZE	32
72 #define SHA384_DIGEST_SIZE	48
73 #define SHA512_DIGEST_SIZE	64
74 
75 enum {
76 	/*
77 	 * SRTM, BIOS, Host Platform Extensions, Embedded
78 	 * Option ROMs and PI Drivers
79 	 */
80 	PCR_0 = 0,
81 	/* Host Platform Configuration */
82 	PCR_1,
83 	/* UEFI driver and application Code */
84 	PCR_2,
85 	/* UEFI driver and application Configuration and Data */
86 	PCR_3,
87 	/* UEFI Boot Manager Code (usually the MBR) and Boot Attempts */
88 	PCR_4,
89 	/*
90 	 * Boot Manager Code Configuration and Data (for use
91 	 * by the Boot Manager Code) and GPT/Partition Table
92 	 */
93 	PCR_5,
94 	/* Host Platform Manufacturer Specific */
95 	PCR_6,
96 	/* Secure Boot Policy */
97 	PCR_7,
98 	/* 8-15: Defined for use by the Static OS */
99 	PCR_8,
100 	/* Debug */
101 	PCR_16 = 16,
102 
103 	/* D-CRTM-measurements by DRTM implementation */
104 	PCR_17 = 17,
105 	/* DCE measurements by DRTM implementation */
106 	PCR_18 = 18
107 };
108 
109 #pragma pack(push, 1)
110 
111 /*
112  * PCR Event Header
113  * TCG EFI Protocol Specification
114  * 5.3 Event Log Header
115  */
116 typedef struct {
117 	/* PCRIndex:
118 	 * The PCR Index to which this event is extended
119 	 */
120 	uint32_t	pcr_index;
121 
122 	/* EventType:
123 	 * SHALL be an EV_NO_ACTION event
124 	 */
125 	uint32_t	event_type;
126 
127 	/* SHALL be 20 Bytes of 0x00 */
128 	uint8_t		digest[SHA1_DIGEST_SIZE];
129 
130 	/* The size of the event */
131 	uint32_t	event_size;
132 
133 	/* SHALL be a TCG_EfiSpecIdEvent */
134 	uint8_t		event[];	/* [event_data_size] */
135 } tcg_pcr_event_t;
136 
137 /*
138  * Log Header Entry Data
139  * Ref. Table 14 TCG_EfiSpecIdEventAlgorithmSize
140  * TCG PC Client Platform Firmware Profile 9.4.5.1
141  */
142 typedef struct {
143 	/* Algorithm ID (hashAlg) of the Hash used by BIOS */
144 	uint16_t	algorithm_id;
145 
146 	/* The size of the digest produced by the implemented Hash algorithm */
147 	uint16_t	digest_size;
148 } id_event_algorithm_size_t;
149 
150 /*
151  * TCG_EfiSpecIdEvent structure
152  * Ref. Table 15 TCG_EfiSpecIdEvent
153  * TCG PC Client Platform Firmware Profile 9.4.5.1
154  */
155 typedef struct {
156 	/*
157 	 * The NUL-terminated ASCII string "Spec ID Event03".
158 	 * SHALL be set to {0x53, 0x70, 0x65, 0x63, 0x20, 0x49, 0x44,
159 	 * 0x20, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x30, 0x33, 0x00}.
160 	 */
161 	uint8_t		signature[16];
162 
163 	/*
164 	 * The value for the Platform Class.
165 	 * The enumeration is defined in the TCG ACPI Specification Client
166 	 * Common Header.
167 	 */
168 	uint32_t	platform_class;
169 
170 	/*
171 	 * The PC Client Platform Profile Specification minor version number
172 	 * this BIOS supports.
173 	 * Any BIOS supporting this version (2.0) MUST set this value to 0x00.
174 	 */
175 	uint8_t		spec_version_minor;
176 
177 	/*
178 	 * The PC Client Platform Profile Specification major version number
179 	 * this BIOS supports.
180 	 * Any BIOS supporting this version (2.0) MUST set this value to 0x02.
181 	 */
182 	uint8_t		spec_version_major;
183 
184 	/*
185 	 * The PC Client Platform Profile Specification errata version number
186 	 * this BIOS supports.
187 	 * Any BIOS supporting this version (2.0) MUST set this value to 0x02.
188 	 */
189 	uint8_t		spec_errata;
190 
191 	/*
192 	 * Specifies the size of the UINTN fields used in various data
193 	 * structures used in this specification.
194 	 * 0x01 indicates UINT32 and 0x02 indicates UINT64.
195 	 */
196 	uint8_t		uintn_size;
197 
198 	/*
199 	 * The number of Hash algorithms in the digestSizes field.
200 	 * This field MUST be set to a value of 0x01 or greater.
201 	 */
202 	uint32_t	number_of_algorithms;
203 
204 	/*
205 	 * Each TCG_EfiSpecIdEventAlgorithmSize SHALL contain an algorithmId
206 	 * and digestSize for each hash algorithm used in the TCG_PCR_EVENT2
207 	 * structure, the first of which is a Hash algorithmID and the second
208 	 * is the size of the respective digest.
209 	 */
210 	id_event_algorithm_size_t    digest_size[]; /* number_of_algorithms */
211 } id_event_struct_header_t;
212 
213 typedef struct {
214 	/*
215 	 * Size in bytes of the VendorInfo field.
216 	 * Maximum value MUST be FFh bytes.
217 	 */
218 	uint8_t		vendor_info_size;
219 
220 	/*
221 	 * Provided for use by Platform Firmware implementer. The value might
222 	 * be used, for example, to provide more detailed information about the
223 	 * specific BIOS such as BIOS revision numbers, etc. The values within
224 	 * this field are not standardized and are implementer-specific.
225 	 * Platform-specific or -unique information MUST NOT be provided in
226 	 * this field.
227 	 *
228 	 */
229 	uint8_t		vendor_info[];	/* [vendorInfoSize] */
230 } id_event_struct_data_t;
231 
232 typedef struct {
233 	id_event_struct_header_t	struct_header;
234 	id_event_struct_data_t		struct_data;
235 } id_event_struct_t;
236 
237 typedef struct {
238 	tcg_pcr_event_t			header;
239 	id_event_struct_header_t	struct_header;
240 } id_event_headers_t;
241 
242 /* TPMT_HA Structure */
243 typedef struct {
244 	/* Selector of the hash contained in the digest that implies
245 	 * the size of the digest
246 	 */
247 	uint16_t	algorithm_id;	/* AlgorithmId */
248 
249 	/* Digest, depends on AlgorithmId */
250 	uint8_t		digest[];	/* Digest[] */
251 } tpmt_ha;
252 
253 /*
254  * TPML_DIGEST_VALUES Structure
255  */
256 typedef struct {
257 	/* The number of digests in the list */
258 	uint32_t	count;			/* Count */
259 
260 	/* The list of tagged digests, as sent to the TPM as part of a
261 	 * TPM2_PCR_Extend or as received from a TPM2_PCR_Event command
262 	 */
263 	tpmt_ha		digests[];		/* Digests[Count] */
264 } tpml_digest_values;
265 
266 /*
267  * TCG_PCR_EVENT2 header
268  */
269 typedef struct {
270 	 /* The PCR Index to which this event was extended */
271 	uint32_t		pcr_index;	/* PCRIndex */
272 
273 	/* Type of event */
274 	uint32_t		event_type;	/* EventType */
275 
276 	/* Digests:
277 	 * A counted list of tagged digests, which contain the digest of
278 	 * the event data (or external data) for all active PCR banks
279 	 */
280 	tpml_digest_values	digests;	/* Digests */
281 } event2_header_t;
282 
283 typedef struct event2_data {
284 	/* The size of the event data */
285 	uint32_t		event_size;	/* EventSize */
286 
287 	/* The data of the event */
288 	uint8_t			event[];	/* Event[EventSize] */
289 } event2_data_t;
290 
291 /*
292  * Startup Locality Event
293  * Ref. TCG PC Client Platform Firmware Profile 9.4.5.3
294  */
295 typedef struct {
296 	/*
297 	 * The NUL-terminated ASCII string "StartupLocality" SHALL be
298 	 * set to {0x53 0x74 0x61 0x72 0x74 0x75 0x70 0x4C 0x6F 0x63
299 	 * 0x61 0x6C 0x69 0x74 0x79 0x00}
300 	 */
301 	uint8_t		signature[16];
302 
303 	/* The Locality Indicator which sent the TPM2_Startup command */
304 	uint8_t		startup_locality;
305 } startup_locality_event_t;
306 
307 #pragma pack(pop)
308 
309 #endif /* TCG_H */
310