1 // SPDX-License-Identifier: GPL-2.0+
3 * Common features for sandbox TPM1 and TPM2 implementations
5 * Copyright 2021 Google LLC
8 #define LOG_CATEGORY UCLASS_TPM
13 #include <asm/unaligned.h>
14 #include "sandbox_common.h"
16 #define TPM_ERR_CODE_OFS (2 + 4) /* after tag and size */
18 int sb_tpm_index_to_seq(u32 index)
20 index &= ~HR_NV_INDEX;
22 case FIRMWARE_NV_INDEX:
23 return NV_SEQ_FIRMWARE;
30 case MRC_REC_HASH_NV_INDEX:
31 return NV_SEQ_REC_HASH;
33 return NV_SEQ_GLOBAL_LOCK;
34 case TPM_NV_INDEX_LOCK:
35 return NV_SEQ_ENABLE_LOCKING;
38 printf("Invalid nv index %#x\n", index);
42 void sb_tpm_read_data(const struct nvdata_state nvdata[NV_SEQ_COUNT],
43 enum sandbox_nv_space seq, u8 *buf, int data_ofs,
46 const struct nvdata_state *nvd = &nvdata[seq];
49 put_unaligned_be32(TPM_BADINDEX, buf + TPM_ERR_CODE_OFS);
50 else if (length > nvd->length)
51 put_unaligned_be32(TPM_BAD_DATASIZE, buf + TPM_ERR_CODE_OFS);
53 memcpy(buf + data_ofs, &nvd->data, length);
56 void sb_tpm_write_data(struct nvdata_state nvdata[NV_SEQ_COUNT],
57 enum sandbox_nv_space seq, const u8 *buf, int data_ofs,
60 struct nvdata_state *nvd = &nvdata[seq];
62 if (length > nvd->length)
63 log_err("Invalid length %x (max %x)\n", length, nvd->length);
65 memcpy(&nvdata[seq].data, buf + data_ofs, length);
68 void sb_tpm_define_data(struct nvdata_state nvdata[NV_SEQ_COUNT],
69 enum sandbox_nv_space seq, int length)
71 struct nvdata_state *nvd = &nvdata[seq];
73 if (length > NV_DATA_SIZE)
74 log_err("Invalid length %x (max %x)\n", length, NV_DATA_SIZE);