blob: 734c2c9d5398acf75939dfefc5f9ec5e7c42b87d [file] [log] [blame]
Miquel Raynal4c6759e2018-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/**
30 * struct tpm_chip_priv - Information about a TPM, stored by the uclass
31 *
32 * These values must be set up by the device's probe() method before
33 * communcation is attempted. If the device has an xfer() method, this is
34 * not needed. There is no need to set up @buf.
35 *
36 * @duration_ms: Length of each duration type in milliseconds
37 * @retry_time_ms: Time to wait before retrying receive
Miquel Raynalf3b43502018-05-15 11:57:08 +020038 * @pcr_count: Number of PCR per bank
39 * @pcr_select_min: Minimum size in bytes of the pcrSelect array
Miquel Raynalf7147e52018-05-15 11:57:07 +020040 * @buf: Buffer used during the exchanges with the chip
Miquel Raynal4c6759e2018-05-15 11:57:06 +020041 */
42struct tpm_chip_priv {
43 uint duration_ms[TPM_DURATION_COUNT];
44 uint retry_time_ms;
Miquel Raynalf3b43502018-05-15 11:57:08 +020045#if defined(CONFIG_TPM_V2)
46 uint pcr_count;
47 uint pcr_select_min;
48#endif
Miquel Raynal4c6759e2018-05-15 11:57:06 +020049 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
50};
51
52/**
53 * struct tpm_ops - low-level TPM operations
54 *
55 * These are designed to avoid loops and delays in the driver itself. These
56 * should be handled in the uclass.
57 *
58 * In gneral you should implement everything except xfer(). Where you need
59 * complete control of the transfer, then xfer() can be provided and will
60 * override the other methods.
61 *
62 * This interface is for low-level TPM access. It does not understand the
63 * concept of localities or the various TPM messages. That interface is
64 * defined in the functions later on in this file, but they all translate
65 * to bytes which are sent and received.
66 */
67struct tpm_ops {
68 /**
69 * open() - Request access to locality 0 for the caller
70 *
71 * After all commands have been completed the caller should call
72 * close().
73 *
74 * @dev: Device to close
75 * @return 0 ok OK, -ve on error
76 */
77 int (*open)(struct udevice *dev);
78
79 /**
80 * close() - Close the current session
81 *
82 * Releasing the locked locality. Returns 0 on success, -ve 1 on
83 * failure (in case lock removal did not succeed).
84 *
85 * @dev: Device to close
86 * @return 0 ok OK, -ve on error
87 */
88 int (*close)(struct udevice *dev);
89
90 /**
91 * get_desc() - Get a text description of the TPM
92 *
93 * @dev: Device to check
94 * @buf: Buffer to put the string
95 * @size: Maximum size of buffer
96 * @return length of string, or -ENOSPC it no space
97 */
98 int (*get_desc)(struct udevice *dev, char *buf, int size);
99
100 /**
101 * send() - send data to the TPM
102 *
103 * @dev: Device to talk to
104 * @sendbuf: Buffer of the data to send
105 * @send_size: Size of the data to send
106 *
107 * Returns 0 on success or -ve on failure.
108 */
109 int (*send)(struct udevice *dev, const u8 *sendbuf, size_t send_size);
110
111 /**
112 * recv() - receive a response from the TPM
113 *
114 * @dev: Device to talk to
115 * @recvbuf: Buffer to save the response to
116 * @max_size: Maximum number of bytes to receive
117 *
118 * Returns number of bytes received on success, -EAGAIN if the TPM
119 * response is not ready, -EINTR if cancelled, or other -ve value on
120 * failure.
121 */
122 int (*recv)(struct udevice *dev, u8 *recvbuf, size_t max_size);
123
124 /**
125 * cleanup() - clean up after an operation in progress
126 *
127 * This is called if receiving times out. The TPM may need to abort
128 * the current transaction if it did not complete, and make itself
129 * ready for another.
130 *
131 * @dev: Device to talk to
132 */
133 int (*cleanup)(struct udevice *dev);
134
135 /**
136 * xfer() - send data to the TPM and get response
137 *
138 * This method is optional. If it exists it is used in preference
139 * to send(), recv() and cleanup(). It should handle all aspects of
140 * TPM communication for a single transfer.
141 *
142 * @dev: Device to talk to
143 * @sendbuf: Buffer of the data to send
144 * @send_size: Size of the data to send
145 * @recvbuf: Buffer to save the response to
146 * @recv_size: Pointer to the size of the response buffer
147 *
148 * Returns 0 on success (and places the number of response bytes at
149 * recv_size) or -ve on failure.
150 */
151 int (*xfer)(struct udevice *dev, const u8 *sendbuf, size_t send_size,
152 u8 *recvbuf, size_t *recv_size);
153};
154
155#define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev))
156
157#define MAKE_TPM_CMD_ENTRY(cmd) \
158 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
159
160#define TPM_COMMAND_NO_ARG(cmd) \
161int do_##cmd(cmd_tbl_t *cmdtp, int flag, \
162 int argc, char * const argv[]) \
163{ \
164 if (argc != 1) \
165 return CMD_RET_USAGE; \
166 return report_return_code(cmd()); \
167}
168
169/**
170 * tpm_get_desc() - Get a text description of the TPM
171 *
172 * @dev: Device to check
173 * @buf: Buffer to put the string
174 * @size: Maximum size of buffer
175 * @return length of string, or -ENOSPC it no space
176 */
177int tpm_get_desc(struct udevice *dev, char *buf, int size);
178
179/**
180 * tpm_xfer() - send data to the TPM and get response
181 *
182 * This first uses the device's send() method to send the bytes. Then it calls
183 * recv() to get the reply. If recv() returns -EAGAIN then it will delay a
184 * short time and then call recv() again.
185 *
186 * Regardless of whether recv() completes successfully, it will then call
187 * cleanup() to finish the transaction.
188 *
189 * Note that the outgoing data is inspected to determine command type
190 * (ordinal) and a timeout is used for that command type.
191 *
192 * @sendbuf - buffer of the data to send
193 * @send_size size of the data to send
194 * @recvbuf - memory to save the response to
195 * @recv_len - pointer to the size of the response buffer
196 *
197 * Returns 0 on success (and places the number of response bytes at
198 * recv_len) or -ve on failure.
199 */
200int tpm_xfer(struct udevice *dev, const u8 *sendbuf, size_t send_size,
201 u8 *recvbuf, size_t *recv_size);
202
203/**
204 * Initialize TPM device. It must be called before any TPM commands.
205 *
206 * @return 0 on success, non-0 on error.
207 */
208int tpm_init(void);
209
210/**
211 * Retrieve the array containing all the commands.
212 *
213 * @return a cmd_tbl_t array.
214 */
215cmd_tbl_t *get_tpm_commands(unsigned int *size);
216
217#endif /* __TPM_COMMON_H */