xref: /openbmc/u-boot/lib/tpm-utils.h (revision 9450ab2b)
1d677bfe2SMiquel Raynal /* SPDX-License-Identifier: GPL-2.0+ */
2d677bfe2SMiquel Raynal /*
3d677bfe2SMiquel Raynal  * Copyright (c) 2013 The Chromium OS Authors.
4d677bfe2SMiquel Raynal  * Coypright (c) 2013 Guntermann & Drunck GmbH
5d677bfe2SMiquel Raynal  */
6d677bfe2SMiquel Raynal 
7d677bfe2SMiquel Raynal #ifndef __TPM_UTILS_H
8d677bfe2SMiquel Raynal #define __TPM_UTILS_H
9d677bfe2SMiquel Raynal 
10d677bfe2SMiquel Raynal #define COMMAND_BUFFER_SIZE 256
11d677bfe2SMiquel Raynal 
12d677bfe2SMiquel Raynal /* Internal error of TPM command library */
13d677bfe2SMiquel Raynal #define TPM_LIB_ERROR ((u32)~0u)
14d677bfe2SMiquel Raynal 
15f6872816SMiquel Raynal /* To make strings of commands more easily */
16f6872816SMiquel Raynal #define __MSB(x) ((x) >> 8)
17f6872816SMiquel Raynal #define __LSB(x) ((x) & 0xFF)
18f6872816SMiquel Raynal #define tpm_u16(x) __MSB(x), __LSB(x)
19f6872816SMiquel Raynal #define tpm_u32(x) tpm_u16((x) >> 16), tpm_u16((x) & 0xFFFF)
20f6872816SMiquel Raynal 
21d677bfe2SMiquel Raynal /**
22d677bfe2SMiquel Raynal  * Pack data into a byte string.  The data types are specified in
23d677bfe2SMiquel Raynal  * the format string: 'b' means unsigned byte, 'w' unsigned word,
24d677bfe2SMiquel Raynal  * 'd' unsigned double word, and 's' byte string.  The data are a
25d677bfe2SMiquel Raynal  * series of offsets and values (for type byte string there are also
26d677bfe2SMiquel Raynal  * lengths).  The data values are packed into the byte string
27d677bfe2SMiquel Raynal  * sequentially, and so a latter value could over-write a former
28d677bfe2SMiquel Raynal  * value.
29d677bfe2SMiquel Raynal  *
30d677bfe2SMiquel Raynal  * @param str		output string
31d677bfe2SMiquel Raynal  * @param size		size of output string
32d677bfe2SMiquel Raynal  * @param format	format string
33d677bfe2SMiquel Raynal  * @param ...		data points
34d677bfe2SMiquel Raynal  * @return 0 on success, non-0 on error
35d677bfe2SMiquel Raynal  */
36d677bfe2SMiquel Raynal int pack_byte_string(u8 *str, size_t size, const char *format, ...);
37d677bfe2SMiquel Raynal 
38d677bfe2SMiquel Raynal /**
39d677bfe2SMiquel Raynal  * Unpack data from a byte string.  The data types are specified in
40d677bfe2SMiquel Raynal  * the format string: 'b' means unsigned byte, 'w' unsigned word,
41d677bfe2SMiquel Raynal  * 'd' unsigned double word, and 's' byte string.  The data are a
42d677bfe2SMiquel Raynal  * series of offsets and pointers (for type byte string there are also
43d677bfe2SMiquel Raynal  * lengths).
44d677bfe2SMiquel Raynal  *
45d677bfe2SMiquel Raynal  * @param str		output string
46d677bfe2SMiquel Raynal  * @param size		size of output string
47d677bfe2SMiquel Raynal  * @param format	format string
48d677bfe2SMiquel Raynal  * @param ...		data points
49d677bfe2SMiquel Raynal  * @return 0 on success, non-0 on error
50d677bfe2SMiquel Raynal  */
51d677bfe2SMiquel Raynal int unpack_byte_string(const u8 *str, size_t size, const char *format, ...);
52d677bfe2SMiquel Raynal 
53d677bfe2SMiquel Raynal /**
54d677bfe2SMiquel Raynal  * Get TPM command size.
55d677bfe2SMiquel Raynal  *
56d677bfe2SMiquel Raynal  * @param command	byte string of TPM command
57d677bfe2SMiquel Raynal  * @return command size of the TPM command
58d677bfe2SMiquel Raynal  */
59d677bfe2SMiquel Raynal u32 tpm_command_size(const void *command);
60d677bfe2SMiquel Raynal 
61d677bfe2SMiquel Raynal /**
62d677bfe2SMiquel Raynal  * Get TPM response return code, which is one of TPM_RESULT values.
63d677bfe2SMiquel Raynal  *
64d677bfe2SMiquel Raynal  * @param response	byte string of TPM response
65d677bfe2SMiquel Raynal  * @return return code of the TPM response
66d677bfe2SMiquel Raynal  */
67d677bfe2SMiquel Raynal u32 tpm_return_code(const void *response);
68d677bfe2SMiquel Raynal 
69d677bfe2SMiquel Raynal /**
70d677bfe2SMiquel Raynal  * Send a TPM command and return response's return code, and optionally
71d677bfe2SMiquel Raynal  * return response to caller.
72d677bfe2SMiquel Raynal  *
73d677bfe2SMiquel Raynal  * @param command	byte string of TPM command
74d677bfe2SMiquel Raynal  * @param response	output buffer for TPM response, or NULL if the
75d677bfe2SMiquel Raynal  *			caller does not care about it
76d677bfe2SMiquel Raynal  * @param size_ptr	output buffer size (input parameter) and TPM
77d677bfe2SMiquel Raynal  *			response length (output parameter); this parameter
78d677bfe2SMiquel Raynal  *			is a bidirectional
79d677bfe2SMiquel Raynal  * @return return code of the TPM response
80d677bfe2SMiquel Raynal  */
81*abdc7b8aSSimon Glass u32 tpm_sendrecv_command(struct udevice *dev, const void *command,
82*abdc7b8aSSimon Glass 			 void *response, size_t *size_ptr);
83d677bfe2SMiquel Raynal 
84d677bfe2SMiquel Raynal #endif /* __TPM_UTILS_H */
85