1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* 3 * Common features for sandbox TPM1 and TPM2 implementations 4 * 5 * Copyright 2021 Google LLC 6 */ 7 8 #ifndef __TPM_SANDBOX_COMMON_H 9 #define __TPM_SANDBOX_COMMON_H 10 11 /* 12 * These numbers derive from adding the sizes of command fields as shown in 13 * the TPM commands manual. 14 */ 15 #define TPM_HDR_LEN 10 16 17 /* These are the different non-volatile spaces that we emulate */ 18 enum sandbox_nv_space { 19 NV_SEQ_ENABLE_LOCKING, 20 NV_SEQ_GLOBAL_LOCK, 21 NV_SEQ_FIRMWARE, 22 NV_SEQ_KERNEL, 23 NV_SEQ_BACKUP, 24 NV_SEQ_FWMP, 25 NV_SEQ_REC_HASH, 26 27 NV_SEQ_COUNT, 28 }; 29 30 /* TPM NVRAM location indices */ 31 #define FIRMWARE_NV_INDEX 0x1007 32 #define KERNEL_NV_INDEX 0x1008 33 #define BACKUP_NV_INDEX 0x1009 34 #define FWMP_NV_INDEX 0x100a 35 #define MRC_REC_HASH_NV_INDEX 0x100b 36 37 /* Size of each non-volatile space */ 38 #define NV_DATA_SIZE 0x28 39 40 /** 41 * struct nvdata_state - state of a single non-volatile-data 'space' 42 * 43 * @present: true if present 44 * @length: length in bytes (max NV_DATA_SIZE) 45 * @data: contents of non-volatile space 46 */ 47 struct nvdata_state { 48 bool present; 49 int length; 50 u8 data[NV_DATA_SIZE]; 51 }; 52 53 /** 54 * sb_tpm_index_to_seq() - convert an index into a space sequence number 55 * 56 * This converts the index as used by the vboot code into an internal sequence 57 * number used by the sandbox emulation. 58 * 59 * @index: Index to use (FIRMWARE_NV_INDEX, etc.) 60 * Return: associated space (enum sandbox_nv_space) 61 */ 62 int sb_tpm_index_to_seq(uint index); 63 64 /** 65 * sb_tpm_read_data() - Read non-volatile data 66 * 67 * This handles a TPM read of nvdata. If the nvdata is not present, a 68 * TPM_BADINDEX error is put in the buffer. If @length is too large, 69 * TPM_BAD_DATASIZE is put in the buffer. 70 * 71 * @nvdata: Current nvdata state 72 * @seq: Sequence number to read 73 * @recvbuf: Buffer to update with the TPM response, assumed to contain zeroes 74 * @data_ofs: Offset of the 'data' portion of @recvbuf 75 * @length: Number of bytes to read 76 */ 77 void sb_tpm_read_data(const struct nvdata_state nvdata[NV_SEQ_COUNT], 78 enum sandbox_nv_space seq, u8 *recvbuf, int data_ofs, 79 int length); 80 81 /** 82 * sb_tpm_write_data() - Write non-volatile data 83 * 84 * If @length is too large, an error is logged and nothing is written. 85 * 86 * @nvdata: Current nvdata state 87 * @seq: Sequence number to read 88 * @buf: Buffer containing the data to write 89 * @data_ofs: Offset of the 'data' portion of @buf 90 * @length: Number of bytes to write 91 */ 92 void sb_tpm_write_data(struct nvdata_state nvdata[NV_SEQ_COUNT], 93 enum sandbox_nv_space seq, const u8 *buf, int data_ofs, 94 int length); 95 96 /** 97 * sb_tpm_define_data() - Set up non-volatile data 98 * 99 * If @length is too large, an error is logged and nothing is written. 100 * 101 * @nvdata: Current nvdata state 102 * @seq: Sequence number to set up 103 * @length: Length of space in bytes 104 */ 105 void sb_tpm_define_data(struct nvdata_state nvdata[NV_SEQ_COUNT], 106 enum sandbox_nv_space seq, int length); 107 108 #endif 109