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