1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #ifndef TSPI_H_
4 #define TSPI_H_
5
6 #include <security/tpm/tpm1_log_serialized.h>
7 #include <security/tpm/tpm2_log_serialized.h>
8 #include <security/tpm/tspi/logs.h>
9 #include <security/tpm/tss.h>
10 #include <commonlib/tpm_log_serialized.h>
11 #include <commonlib/region.h>
12 #include <vb2_api.h>
13
14 #define TPM_PCR_MAX_LEN 64
15 #define HASH_DATA_CHUNK_SIZE 1024
16 #define MAX_TPM_LOG_ENTRIES 50
17 /* Assumption of 2K TCPA log size reserved for CAR/SRAM */
18 #define MAX_PRERAM_TPM_LOG_ENTRIES 15
19
20 /**
21 * Get the pointer to the single instance of global
22 * TPM log data, and initialize it when necessary
23 */
24 void *tpm_log_init(void);
25
26 /**
27 * Get the pointer to the single CBMEM instance of global
28 * TPM log data, and initialize it when necessary
29 */
tpm_log_cbmem_init(void)30 static inline void *tpm_log_cbmem_init(void)
31 {
32 if (CONFIG(TPM_LOG_CB))
33 return tpm_cb_log_cbmem_init();
34 if (CONFIG(TPM_LOG_TPM1))
35 return tpm1_log_cbmem_init();
36 if (CONFIG(TPM_LOG_TPM2))
37 return tpm2_log_cbmem_init();
38 return NULL;
39 }
40
41 /**
42 * Clears the pre-RAM TPM log data and initializes
43 * any content with default values
44 */
tpm_preram_log_clear(void)45 static inline void tpm_preram_log_clear(void)
46 {
47 if (CONFIG(TPM_LOG_CB))
48 tpm_cb_preram_log_clear();
49 else if (CONFIG(TPM_LOG_TPM1))
50 tpm1_preram_log_clear();
51 else if (CONFIG(TPM_LOG_TPM2))
52 tpm2_preram_log_clear();
53 }
54
55 /**
56 * Retrieves number of entries currently stored in the log.
57 */
tpm_log_get_size(const void * log_table)58 static inline uint16_t tpm_log_get_size(const void *log_table)
59 {
60 if (CONFIG(TPM_LOG_CB))
61 return tpm_cb_log_get_size(log_table);
62 if (CONFIG(TPM_LOG_TPM1))
63 return tpm1_log_get_size(log_table);
64 if (CONFIG(TPM_LOG_TPM2))
65 return tpm2_log_get_size(log_table);
66 return 0;
67 }
68
69 /**
70 * Copies data from pre-RAM TPM log to CBMEM (RAM) log
71 */
tpm_log_copy_entries(const void * from,void * to)72 static inline void tpm_log_copy_entries(const void *from, void *to)
73 {
74 if (CONFIG(TPM_LOG_CB))
75 tpm_cb_log_copy_entries(from, to);
76 else if (CONFIG(TPM_LOG_TPM1))
77 tpm1_log_copy_entries(from, to);
78 else if (CONFIG(TPM_LOG_TPM2))
79 tpm2_log_copy_entries(from, to);
80 }
81
82 /**
83 * Retrieves an entry from a log. Returns non-zero on invalid index or error.
84 */
tpm_log_get(int entry_idx,int * pcr,const uint8_t ** digest_data,enum vb2_hash_algorithm * digest_algo,const char ** event_name)85 static inline int tpm_log_get(int entry_idx, int *pcr, const uint8_t **digest_data,
86 enum vb2_hash_algorithm *digest_algo, const char **event_name)
87 {
88 if (CONFIG(TPM_LOG_CB))
89 return tpm_cb_log_get(entry_idx, pcr, digest_data, digest_algo, event_name);
90 if (CONFIG(TPM_LOG_TPM1))
91 return tpm1_log_get(entry_idx, pcr, digest_data, digest_algo, event_name);
92 if (CONFIG(TPM_LOG_TPM2))
93 return tpm2_log_get(entry_idx, pcr, digest_data, digest_algo, event_name);
94 return 1;
95 }
96
97 /**
98 * Add table entry for cbmem TPM log.
99 * @param name Name of the hashed data
100 * @param pcr PCR used to extend hashed data
101 * @param diget_algo sets the digest algorithm
102 * @param digest sets the hash extended into the tpm
103 * @param digest_len the length of the digest
104 */
tpm_log_add_table_entry(const char * name,const uint32_t pcr,enum vb2_hash_algorithm digest_algo,const uint8_t * digest,const size_t digest_len)105 static inline void tpm_log_add_table_entry(const char *name, const uint32_t pcr,
106 enum vb2_hash_algorithm digest_algo,
107 const uint8_t *digest,
108 const size_t digest_len)
109 {
110 if (CONFIG(TPM_LOG_CB))
111 tpm_cb_log_add_table_entry(name, pcr, digest_algo, digest, digest_len);
112 else if (CONFIG(TPM_LOG_TPM1))
113 tpm1_log_add_table_entry(name, pcr, digest_algo, digest, digest_len);
114 else if (CONFIG(TPM_LOG_TPM2))
115 tpm2_log_add_table_entry(name, pcr, digest_algo, digest, digest_len);
116 }
117
118 /**
119 * Dump TPM log entries on console
120 */
tpm_log_dump(void * unused)121 static inline void tpm_log_dump(void *unused)
122 {
123 if (CONFIG(TPM_LOG_CB))
124 tpm_cb_log_dump();
125 else if (CONFIG(TPM_LOG_TPM1))
126 tpm1_log_dump();
127 else if (CONFIG(TPM_LOG_TPM2))
128 tpm2_log_dump();
129 }
130
131 /**
132 * Ask vboot for a digest and extend a TPM PCR with it.
133 * @param pcr sets the pcr index
134 * @param diget_algo sets the digest algorithm
135 * @param digest sets the hash to extend into the tpm
136 * @param digest_len the length of the digest
137 * @param name sets additional info where the digest comes from
138 * @return TPM_SUCCESS on success. If not a tpm error is returned
139 */
140 tpm_result_t tpm_extend_pcr(int pcr, enum vb2_hash_algorithm digest_algo,
141 const uint8_t *digest, size_t digest_len,
142 const char *name);
143
144 /**
145 * Issue a TPM_Clear and re-enable/reactivate the TPM.
146 * @return TPM_SUCCESS on success. If not a tpm error is returned
147 */
148 tpm_result_t tpm_clear_and_reenable(void);
149
150 /**
151 * Start the TPM and establish the root of trust.
152 * @param s3flag tells the tpm setup if we wake up from a s3 state on x86
153 * @return TPM_SUCCESS on success. If not a tpm error is returned
154 */
155 tpm_result_t tpm_setup(int s3flag);
156
157 /**
158 * Measure a given region device and extend given PCR with the result.
159 * @param *rdev Pointer to the region device to measure
160 * @param pcr Index of the PCR which will be extended by this measure
161 * @param *rname Name of the region that is measured
162 * @return TPM error code in case of error otherwise TPM_SUCCESS
163 */
164 tpm_result_t tpm_measure_region(const struct region_device *rdev, uint8_t pcr,
165 const char *rname);
166
167 #endif /* TSPI_H_ */
168