blob: 5f8bc6bc5287189d48ee1829d60fd5a9be4c4bc4 [file] [log] [blame]
Miquel Raynald677bfe2018-05-15 11:57:06 +02001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright (c) 2013 The Chromium OS Authors.
4 * Coypright (c) 2013 Guntermann & Drunck GmbH
5 */
6
7#ifndef __TPM_COMMON_H
8#define __TPM_COMMON_H
9
10enum tpm_duration {
11 TPM_SHORT = 0,
12 TPM_MEDIUM = 1,
13 TPM_LONG = 2,
14 TPM_UNDEFINED,
15
16 TPM_DURATION_COUNT,
17};
18
19/*
20 * Here is a partial implementation of TPM commands. Please consult TCG Main
21 * Specification for definitions of TPM commands.
22 */
23
24#define TPM_HEADER_SIZE 10
25
26/* Max buffer size supported by our tpm */
27#define TPM_DEV_BUFSIZE 1260
28
29/**
Miquel Raynal2a2096e2018-07-19 22:35:09 +020030 * enum tpm_version - The version of the TPM stack to be used
31 * @TPM_V1: Use TPM v1.x stack
32 * @TPM_V2: Use TPM v2.x stack
33 */
34enum tpm_version {
35 TPM_V1 = 0,
36 TPM_V2,
37};
38
39/**
Miquel Raynald677bfe2018-05-15 11:57:06 +020040 * struct tpm_chip_priv - Information about a TPM, stored by the uclass
41 *
42 * These values must be set up by the device's probe() method before
43 * communcation is attempted. If the device has an xfer() method, this is
44 * not needed. There is no need to set up @buf.
45 *
Miquel Raynal2a2096e2018-07-19 22:35:09 +020046 * @version: TPM stack to be used
Miquel Raynald677bfe2018-05-15 11:57:06 +020047 * @duration_ms: Length of each duration type in milliseconds
48 * @retry_time_ms: Time to wait before retrying receive
Miquel Raynal2a2096e2018-07-19 22:35:09 +020049 * @buf: Buffer used during the exchanges with the chip
Miquel Raynalff322452018-05-15 11:57:08 +020050 * @pcr_count: Number of PCR per bank
51 * @pcr_select_min: Minimum size in bytes of the pcrSelect array
Miquel Raynald677bfe2018-05-15 11:57:06 +020052 */
53struct tpm_chip_priv {
Miquel Raynal2a2096e2018-07-19 22:35:09 +020054 enum tpm_version version;
55
Miquel Raynald677bfe2018-05-15 11:57:06 +020056 uint duration_ms[TPM_DURATION_COUNT];
57 uint retry_time_ms;
Miquel Raynal2a2096e2018-07-19 22:35:09 +020058 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
59
60 /* TPM v2 specific data */
Miquel Raynalff322452018-05-15 11:57:08 +020061 uint pcr_count;
62 uint pcr_select_min;
Miquel Raynald677bfe2018-05-15 11:57:06 +020063};
64
65/**
66 * struct tpm_ops - low-level TPM operations
67 *
68 * These are designed to avoid loops and delays in the driver itself. These
69 * should be handled in the uclass.
70 *
71 * In gneral you should implement everything except xfer(). Where you need
72 * complete control of the transfer, then xfer() can be provided and will
73 * override the other methods.
74 *
75 * This interface is for low-level TPM access. It does not understand the
76 * concept of localities or the various TPM messages. That interface is
77 * defined in the functions later on in this file, but they all translate
78 * to bytes which are sent and received.
79 */
80struct tpm_ops {
81 /**
82 * open() - Request access to locality 0 for the caller
83 *
84 * After all commands have been completed the caller should call
85 * close().
86 *
Miquel Raynal350988f2018-07-19 22:35:06 +020087 * @dev: Device to open
Miquel Raynald677bfe2018-05-15 11:57:06 +020088 * @return 0 ok OK, -ve on error
89 */
90 int (*open)(struct udevice *dev);
91
92 /**
93 * close() - Close the current session
94 *
95 * Releasing the locked locality. Returns 0 on success, -ve 1 on
96 * failure (in case lock removal did not succeed).
97 *
98 * @dev: Device to close
99 * @return 0 ok OK, -ve on error
100 */
101 int (*close)(struct udevice *dev);
102
103 /**
104 * get_desc() - Get a text description of the TPM
105 *
106 * @dev: Device to check
107 * @buf: Buffer to put the string
108 * @size: Maximum size of buffer
109 * @return length of string, or -ENOSPC it no space
110 */
111 int (*get_desc)(struct udevice *dev, char *buf, int size);
112
113 /**
114 * send() - send data to the TPM
115 *
116 * @dev: Device to talk to
117 * @sendbuf: Buffer of the data to send
118 * @send_size: Size of the data to send
119 *
120 * Returns 0 on success or -ve on failure.
121 */
122 int (*send)(struct udevice *dev, const u8 *sendbuf, size_t send_size);
123
124 /**
125 * recv() - receive a response from the TPM
126 *
127 * @dev: Device to talk to
128 * @recvbuf: Buffer to save the response to
129 * @max_size: Maximum number of bytes to receive
130 *
131 * Returns number of bytes received on success, -EAGAIN if the TPM
132 * response is not ready, -EINTR if cancelled, or other -ve value on
133 * failure.
134 */
135 int (*recv)(struct udevice *dev, u8 *recvbuf, size_t max_size);
136
137 /**
138 * cleanup() - clean up after an operation in progress
139 *
140 * This is called if receiving times out. The TPM may need to abort
141 * the current transaction if it did not complete, and make itself
142 * ready for another.
143 *
144 * @dev: Device to talk to
145 */
146 int (*cleanup)(struct udevice *dev);
147
148 /**
149 * xfer() - send data to the TPM and get response
150 *
151 * This method is optional. If it exists it is used in preference
152 * to send(), recv() and cleanup(). It should handle all aspects of
153 * TPM communication for a single transfer.
154 *
155 * @dev: Device to talk to
156 * @sendbuf: Buffer of the data to send
157 * @send_size: Size of the data to send
158 * @recvbuf: Buffer to save the response to
159 * @recv_size: Pointer to the size of the response buffer
160 *
161 * Returns 0 on success (and places the number of response bytes at
162 * recv_size) or -ve on failure.
163 */
164 int (*xfer)(struct udevice *dev, const u8 *sendbuf, size_t send_size,
165 u8 *recvbuf, size_t *recv_size);
166};
167
168#define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev))
169
170#define MAKE_TPM_CMD_ENTRY(cmd) \
171 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
172
173#define TPM_COMMAND_NO_ARG(cmd) \
174int do_##cmd(cmd_tbl_t *cmdtp, int flag, \
175 int argc, char * const argv[]) \
176{ \
177 if (argc != 1) \
178 return CMD_RET_USAGE; \
179 return report_return_code(cmd()); \
180}
181
182/**
183 * tpm_get_desc() - Get a text description of the TPM
184 *
185 * @dev: Device to check
186 * @buf: Buffer to put the string
187 * @size: Maximum size of buffer
188 * @return length of string, or -ENOSPC it no space
189 */
190int tpm_get_desc(struct udevice *dev, char *buf, int size);
191
192/**
193 * tpm_xfer() - send data to the TPM and get response
194 *
195 * This first uses the device's send() method to send the bytes. Then it calls
196 * recv() to get the reply. If recv() returns -EAGAIN then it will delay a
197 * short time and then call recv() again.
198 *
199 * Regardless of whether recv() completes successfully, it will then call
200 * cleanup() to finish the transaction.
201 *
202 * Note that the outgoing data is inspected to determine command type
203 * (ordinal) and a timeout is used for that command type.
204 *
205 * @sendbuf - buffer of the data to send
206 * @send_size size of the data to send
207 * @recvbuf - memory to save the response to
208 * @recv_len - pointer to the size of the response buffer
209 *
210 * Returns 0 on success (and places the number of response bytes at
211 * recv_len) or -ve on failure.
212 */
213int tpm_xfer(struct udevice *dev, const u8 *sendbuf, size_t send_size,
214 u8 *recvbuf, size_t *recv_size);
215
216/**
217 * Initialize TPM device. It must be called before any TPM commands.
218 *
219 * @return 0 on success, non-0 on error.
220 */
221int tpm_init(void);
222
223/**
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200224 * Retrieve the array containing all the v1 (resp. v2) commands.
Miquel Raynald677bfe2018-05-15 11:57:06 +0200225 *
226 * @return a cmd_tbl_t array.
227 */
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200228#if defined(CONFIG_TPM_V1)
229cmd_tbl_t *get_tpm1_commands(unsigned int *size);
230#else
231static inline cmd_tbl_t *get_tpm1_commands(unsigned int *size)
232{
233 return NULL;
234}
235#endif
236#if defined(CONFIG_TPM_V2)
237cmd_tbl_t *get_tpm2_commands(unsigned int *size);
238#else
239static inline cmd_tbl_t *get_tpm2_commands(unsigned int *size)
240{
241 return NULL;
242}
243#endif
Miquel Raynald677bfe2018-05-15 11:57:06 +0200244
245#endif /* __TPM_COMMON_H */