util/docker/doc.coreboot.org: Allow git to work in envs owned by root
[coreboot2.git] / src / security / tpm / tspi.h
blob3e7e5f10f568f5e6f1b3e86ddbaa536a5f119478
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #ifndef TSPI_H_
4 #define TSPI_H_
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>
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
20 /**
21 * Get the pointer to the single instance of global
22 * TPM log data, and initialize it when necessary
24 void *tpm_log_init(void);
26 /**
27 * Get the pointer to the single CBMEM instance of global
28 * TPM log data, and initialize it when necessary
30 static inline void *tpm_log_cbmem_init(void)
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;
41 /**
42 * Clears the pre-RAM TPM log data and initializes
43 * any content with default values
45 static inline void tpm_preram_log_clear(void)
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();
55 /**
56 * Retrieves number of entries currently stored in the log.
58 static inline uint16_t tpm_log_get_size(const void *log_table)
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;
69 /**
70 * Copies data from pre-RAM TPM log to CBMEM (RAM) log
72 static inline void tpm_log_copy_entries(const void *from, void *to)
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);
82 /**
83 * Retrieves an entry from a log. Returns non-zero on invalid index or error.
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)
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;
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
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)
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);
119 * Dump TPM log entries on console
121 static inline void tpm_log_dump(void *unused)
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();
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
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);
145 * Issue a TPM_Clear and re-enable/reactivate the TPM.
146 * @return TPM_SUCCESS on success. If not a tpm error is returned
148 tpm_result_t tpm_clear_and_reenable(void);
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
155 tpm_result_t tpm_setup(int s3flag);
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
164 tpm_result_t tpm_measure_region(const struct region_device *rdev, uint8_t pcr,
165 const char *rname);
167 #endif /* TSPI_H_ */