xref: /openbmc/u-boot/lib/tpm-utils.h (revision 6d9a98c5)
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  * Pack data into a byte string.  The data types are specified in
23  * the format string: 'b' means unsigned byte, 'w' unsigned word,
24  * 'd' unsigned double word, and 's' byte string.  The data are a
25  * series of offsets and values (for type byte string there are also
26  * lengths).  The data values are packed into the byte string
27  * sequentially, and so a latter value could over-write a former
28  * value.
29  *
30  * @param str		output string
31  * @param size		size of output string
32  * @param format	format string
33  * @param ...		data points
34  * @return 0 on success, non-0 on error
35  */
36 int pack_byte_string(u8 *str, size_t size, const char *format, ...);
37 
38 /**
39  * Unpack data from a byte string.  The data types are specified in
40  * the format string: 'b' means unsigned byte, 'w' unsigned word,
41  * 'd' unsigned double word, and 's' byte string.  The data are a
42  * series of offsets and pointers (for type byte string there are also
43  * lengths).
44  *
45  * @param str		output string
46  * @param size		size of output string
47  * @param format	format string
48  * @param ...		data points
49  * @return 0 on success, non-0 on error
50  */
51 int unpack_byte_string(const u8 *str, size_t size, const char *format, ...);
52 
53 /**
54  * Get TPM command size.
55  *
56  * @param command	byte string of TPM command
57  * @return command size of the TPM command
58  */
59 u32 tpm_command_size(const void *command);
60 
61 /**
62  * Get TPM response return code, which is one of TPM_RESULT values.
63  *
64  * @param response	byte string of TPM response
65  * @return return code of the TPM response
66  */
67 u32 tpm_return_code(const void *response);
68 
69 /**
70  * Send a TPM command and return response's return code, and optionally
71  * return response to caller.
72  *
73  * @param command	byte string of TPM command
74  * @param response	output buffer for TPM response, or NULL if the
75  *			caller does not care about it
76  * @param size_ptr	output buffer size (input parameter) and TPM
77  *			response length (output parameter); this parameter
78  *			is a bidirectional
79  * @return return code of the TPM response
80  */
81 u32 tpm_sendrecv_command(struct udevice *dev, const void *command,
82 			 void *response, size_t *size_ptr);
83 
84 #endif /* __TPM_UTILS_H */
85