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
12 #include <asm/unaligned.h>
13 #include "sandbox_common.h"
15 #define TPM_ERR_CODE_OFS (2 + 4) /* after tag and size */
17 int sb_tpm_index_to_seq(u32 index)
19 index &= ~HR_NV_INDEX;
21 case FIRMWARE_NV_INDEX:
22 return NV_SEQ_FIRMWARE;
29 case MRC_REC_HASH_NV_INDEX:
30 return NV_SEQ_REC_HASH;
32 return NV_SEQ_GLOBAL_LOCK;
33 case TPM_NV_INDEX_LOCK:
34 return NV_SEQ_ENABLE_LOCKING;
37 printf("Invalid nv index %#x\n", index);
41 void sb_tpm_read_data(const struct nvdata_state nvdata[NV_SEQ_COUNT],
42 enum sandbox_nv_space seq, u8 *buf, int data_ofs,
45 const struct nvdata_state *nvd = &nvdata[seq];
48 put_unaligned_be32(TPM_BADINDEX, buf + TPM_ERR_CODE_OFS);
49 else if (length > nvd->length)
50 put_unaligned_be32(TPM_BAD_DATASIZE, buf + TPM_ERR_CODE_OFS);
52 memcpy(buf + data_ofs, &nvd->data, length);
55 void sb_tpm_write_data(struct nvdata_state nvdata[NV_SEQ_COUNT],
56 enum sandbox_nv_space seq, const u8 *buf, int data_ofs,
59 struct nvdata_state *nvd = &nvdata[seq];
61 if (length > nvd->length)
62 log_err("Invalid length %x (max %x)\n", length, nvd->length);
64 memcpy(&nvdata[seq].data, buf + data_ofs, length);
67 void sb_tpm_define_data(struct nvdata_state nvdata[NV_SEQ_COUNT],
68 enum sandbox_nv_space seq, int length)
70 struct nvdata_state *nvd = &nvdata[seq];
72 if (length > NV_DATA_SIZE)
73 log_err("Invalid length %x (max %x)\n", length, NV_DATA_SIZE);