xref: /openbmc/u-boot/lib/tpm-utils.h (revision f77d4410)
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_UTILS_H
8 #define __TPM_UTILS_H
9 
10 #define COMMAND_BUFFER_SIZE 256
11 
12 /* Internal error of TPM command library */
13 #define TPM_LIB_ERROR ((u32)~0u)
14 
15 /* To make strings of commands more easily */
16 #define __MSB(x) ((x) >> 8)
17 #define __LSB(x) ((x) & 0xFF)
18 #define tpm_u16(x) __MSB(x), __LSB(x)
19 #define tpm_u32(x) tpm_u16((x) >> 16), tpm_u16((x) & 0xFFFF)
20 
21 /**
22  * tpm_open() - Request access to locality 0 for the caller
23  *
24  * After all commands have been completed the caller is supposed to
25  * call tpm_close().
26  *
27  * Returns 0 on success, -ve on failure.
28  */
29 int tpm_open(struct udevice *dev);
30 
31 /**
32  * tpm_close() - Close the current session
33  *
34  * Releasing the locked locality. Returns 0 on success, -ve 1 on
35  * failure (in case lock removal did not succeed).
36  */
37 int tpm_close(struct udevice *dev);
38 
39 /**
40  * Pack data into a byte string.  The data types are specified in
41  * the format string: 'b' means unsigned byte, 'w' unsigned word,
42  * 'd' unsigned double word, and 's' byte string.  The data are a
43  * series of offsets and values (for type byte string there are also
44  * lengths).  The data values are packed into the byte string
45  * sequentially, and so a latter value could over-write a former
46  * value.
47  *
48  * @param str		output string
49  * @param size		size of output string
50  * @param format	format string
51  * @param ...		data points
52  * @return 0 on success, non-0 on error
53  */
54 int pack_byte_string(u8 *str, size_t size, const char *format, ...);
55 
56 /**
57  * Unpack data from a byte string.  The data types are specified in
58  * the format string: 'b' means unsigned byte, 'w' unsigned word,
59  * 'd' unsigned double word, and 's' byte string.  The data are a
60  * series of offsets and pointers (for type byte string there are also
61  * lengths).
62  *
63  * @param str		output string
64  * @param size		size of output string
65  * @param format	format string
66  * @param ...		data points
67  * @return 0 on success, non-0 on error
68  */
69 int unpack_byte_string(const u8 *str, size_t size, const char *format, ...);
70 
71 /**
72  * Get TPM command size.
73  *
74  * @param command	byte string of TPM command
75  * @return command size of the TPM command
76  */
77 u32 tpm_command_size(const void *command);
78 
79 /**
80  * Get TPM response return code, which is one of TPM_RESULT values.
81  *
82  * @param response	byte string of TPM response
83  * @return return code of the TPM response
84  */
85 u32 tpm_return_code(const void *response);
86 
87 /**
88  * Send a TPM command and return response's return code, and optionally
89  * return response to caller.
90  *
91  * @param command	byte string of TPM command
92  * @param response	output buffer for TPM response, or NULL if the
93  *			caller does not care about it
94  * @param size_ptr	output buffer size (input parameter) and TPM
95  *			response length (output parameter); this parameter
96  *			is a bidirectional
97  * @return return code of the TPM response
98  */
99 u32 tpm_sendrecv_command(const void *command, void *response, size_t *size_ptr);
100 
101 #endif /* __TPM_UTILS_H */
102