Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 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 | |
Miquel Raynal | c9e0c32 | 2018-05-15 11:57:09 +0200 | [diff] [blame] | 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 | |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 21 | /** |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 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 |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 34 | * Return: 0 on success, non-0 on error |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 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 |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 49 | * Return: 0 on success, non-0 on error |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 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 |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 57 | * Return: command size of the TPM command |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 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 |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 65 | * Return: return code of the TPM response |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 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 |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 79 | * Return: return code of the TPM response |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 80 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 81 | u32 tpm_sendrecv_command(struct udevice *dev, const void *command, |
| 82 | void *response, size_t *size_ptr); |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 83 | |
| 84 | #endif /* __TPM_UTILS_H */ |