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