xref: /aosp_15_r20/external/coreboot/src/vendorcode/eltan/security/mboot/mboot.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef MBOOT_H
4 #define MBOOT_H
5 
6 #include <arch/io.h>
7 #include <acpi/acpi.h>
8 #include <string.h>
9 #include <console/console.h>
10 #include <cbfs.h>
11 #include <lib.h>
12 #include <boot/coreboot_tables.h>
13 #include <security/tpm/tss/tcg-2.0/tss_structures.h>
14 #include <security/tpm/tss.h>
15 #include <swab.h>
16 
17 /* TPM2 interface */
18 #define EFI_TPM2_ACPI_TABLE_START_METHOD_TIS	6
19 #define TPM_SHA1_160_HASH_LEN	0x14
20 
21 /* Part 2, section 5.4: TPM_DIGEST */
22 
23 /* Index to a PCR register */
24 typedef uint32_t TPM_PCRINDEX;
25 typedef uint32_t TCG_EVENTTYPE;
26 typedef TPM_PCRINDEX TCG_PCRINDEX;
27 typedef int8_t TCG_DIGEST;
28 
29 /* TCG_PCR_EVENT_HDR */
30 typedef struct {
31 	TCG_PCRINDEX pcrIndex;
32 	TCG_EVENTTYPE eventType;
33 	TCG_DIGEST digest[TPM_SHA1_160_HASH_LEN];
34 	uint32_t eventSize;
35 } __packed TCG_PCR_EVENT_HDR;
36 
37 /* TCG_PCR_EVENT2_HDR */
38 typedef struct {
39 	TCG_PCRINDEX pcrIndex;
40 	TCG_EVENTTYPE eventType;
41 	TPML_DIGEST_VALUES digest;
42 	uint32_t eventSize;
43 } __packed TCG_PCR_EVENT2_HDR;
44 
45 typedef uint32_t EFI_TCG2_EVENT_ALGORITHM_BITMAP;
46 
47 #define EFI_TCG2_BOOT_HASH_ALG_SHA1	0x00000001
48 #define EFI_TCG2_BOOT_HASH_ALG_SHA256	0x00000002
49 #define EFI_TCG2_BOOT_HASH_ALG_SHA384	0x00000004
50 #define EFI_TCG2_BOOT_HASH_ALG_SHA512	0x00000008
51 #define EFI_TCG2_BOOT_HASH_ALG_SM3_256	0x00000010
52 
53 /* Standard event types */
54 #define EV_POST_CODE		((TCG_EVENTTYPE) 0x00000001)
55 #define EV_NO_ACTION		((TCG_EVENTTYPE) 0x00000003)
56 #define EV_SEPARATOR		((TCG_EVENTTYPE) 0x00000004)
57 #define EV_S_CRTM_CONTENTS	((TCG_EVENTTYPE) 0x00000007)
58 #define EV_S_CRTM_VERSION	((TCG_EVENTTYPE) 0x00000008)
59 #define EV_CPU_MICROCODE	((TCG_EVENTTYPE) 0x00000009)
60 #define EV_TABLE_OF_DEVICES	((TCG_EVENTTYPE) 0x0000000B)
61 
62 #define MBOOT_PCR_INDEX_0	0x0
63 #define MBOOT_PCR_INDEX_1	0x1
64 #define MBOOT_PCR_INDEX_2	0x2
65 #define MBOOT_PCR_INDEX_3	0x3
66 #define MBOOT_PCR_INDEX_4	0x4
67 #define MBOOT_PCR_INDEX_5	0x5
68 #define MBOOT_PCR_INDEX_6	0x6
69 #define MBOOT_PCR_INDEX_7	0x7
70 
71 /*
72  * used to indicate a hash is provide so there is no need to perform the
73  * measurement
74  */
75 #define MBOOT_HASH_PROVIDED (0x00000001)
76 
77 int is_zero_buffer(void *buffer, unsigned int size);
78 
79 tpm_result_t mboot_hash_extend_log(uint64_t flags, uint8_t *hashData, uint32_t hashDataLen,
80 			  TCG_PCR_EVENT2_HDR *newEventHdr, uint8_t *eventLog);
81 
82 void mboot_print_buffer(uint8_t *buffer, uint32_t bufferSize);
83 
84 tpm_result_t mb_crtm(void);
85 
86 typedef struct {
87 	const char *cbfs_name;
88 	uint32_t cbfs_type;
89 	uint32_t pcr;
90 	TCG_EVENTTYPE eventType;
91 	const char *event_msg;
92 } mboot_measure_item_t;
93 
94 tpm_result_t mb_measure_log_worker(const char *name, uint32_t type, uint32_t pcr,
95 			  TCG_EVENTTYPE eventType, const char *event_msg);
96 
97 tpm_result_t mb_measure_log_start(void);
98 void invalidate_pcrs(void);
99 
100 EFI_TCG2_EVENT_ALGORITHM_BITMAP tpm2_get_active_pcrs(void);
101 
102 tpm_result_t tpm2_get_capability_pcrs(TPML_PCR_SELECTION *Pcrs);
103 
104 tpm_result_t mb_measure(int wake_from_s3);
105 tpm_result_t mb_entry(int wake_from_s3);
106 
107 int log_efi_specid_event(void);
108 int log_event_tcg_20_format(TCG_PCR_EVENT2_HDR *EventHdr, uint8_t *EventLog);
109 int log_event_tcg_12_format(TCG_PCR_EVENT2_HDR *EventHdr, uint8_t *EventLog);
110 
111 int get_intel_me_hash(uint8_t *hash);
112 
113 #endif /* MBOOT_H */
114