aboutsummaryrefslogtreecommitdiff
path: root/drivers/tpm/sandbox_common.c
diff options
context:
space:
mode:
authorTom Rini <trini@konsulko.com>2021-08-01 14:41:22 -0400
committerTom Rini <trini@konsulko.com>2021-08-01 14:41:22 -0400
commit72ffb41a873722993d89c02bae4743a8ad6f16f9 (patch)
tree2508a115df4604adc9395b4f72e8ec3ec8fff90f /drivers/tpm/sandbox_common.c
parent5371593aed56ee11cbb6cc6ac8d058fcd9b8f58c (diff)
parenteec44c7218a3c3ce924a282cc46a59e83feb9de1 (diff)
Merge tag 'dm-pull-1aug21' of https://source.denx.de/u-boot/custodians/u-boot-dm
sandbox TPM-emulator improvements rST documentation and fixes for moveconfig handle empty 'ranges' property in dtoc patman warning for invalid tag clean-ups to 'fdt add' command
Diffstat (limited to 'drivers/tpm/sandbox_common.c')
-rw-r--r--drivers/tpm/sandbox_common.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/drivers/tpm/sandbox_common.c b/drivers/tpm/sandbox_common.c
new file mode 100644
index 0000000000..7e0b2502e3
--- /dev/null
+++ b/drivers/tpm/sandbox_common.c
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Common features for sandbox TPM1 and TPM2 implementations
+ *
+ * Copyright 2021 Google LLC
+ */
+
+#define LOG_CATEGORY UCLASS_TPM
+
+#include <common.h>
+#include <tpm-v1.h>
+#include <tpm-v2.h>
+#include <asm/unaligned.h>
+#include "sandbox_common.h"
+
+#define TPM_ERR_CODE_OFS (2 + 4) /* after tag and size */
+
+int sb_tpm_index_to_seq(u32 index)
+{
+ index &= ~HR_NV_INDEX;
+ switch (index) {
+ case FIRMWARE_NV_INDEX:
+ return NV_SEQ_FIRMWARE;
+ case KERNEL_NV_INDEX:
+ return NV_SEQ_KERNEL;
+ case BACKUP_NV_INDEX:
+ return NV_SEQ_BACKUP;
+ case FWMP_NV_INDEX:
+ return NV_SEQ_FWMP;
+ case MRC_REC_HASH_NV_INDEX:
+ return NV_SEQ_REC_HASH;
+ case 0:
+ return NV_SEQ_GLOBAL_LOCK;
+ case TPM_NV_INDEX_LOCK:
+ return NV_SEQ_ENABLE_LOCKING;
+ }
+
+ printf("Invalid nv index %#x\n", index);
+ return -1;
+}
+
+void sb_tpm_read_data(const struct nvdata_state nvdata[NV_SEQ_COUNT],
+ enum sandbox_nv_space seq, u8 *buf, int data_ofs,
+ int length)
+{
+ const struct nvdata_state *nvd = &nvdata[seq];
+
+ if (!nvd->present)
+ put_unaligned_be32(TPM_BADINDEX, buf + TPM_ERR_CODE_OFS);
+ else if (length > nvd->length)
+ put_unaligned_be32(TPM_BAD_DATASIZE, buf + TPM_ERR_CODE_OFS);
+ else
+ memcpy(buf + data_ofs, &nvd->data, length);
+}
+
+void sb_tpm_write_data(struct nvdata_state nvdata[NV_SEQ_COUNT],
+ enum sandbox_nv_space seq, const u8 *buf, int data_ofs,
+ int length)
+{
+ struct nvdata_state *nvd = &nvdata[seq];
+
+ if (length > nvd->length)
+ log_err("Invalid length %x (max %x)\n", length, nvd->length);
+ else
+ memcpy(&nvdata[seq].data, buf + data_ofs, length);
+}
+
+void sb_tpm_define_data(struct nvdata_state nvdata[NV_SEQ_COUNT],
+ enum sandbox_nv_space seq, int length)
+{
+ struct nvdata_state *nvd = &nvdata[seq];
+
+ if (length > NV_DATA_SIZE)
+ log_err("Invalid length %x (max %x)\n", length, NV_DATA_SIZE);
+ nvd->length = length;
+ nvd->present = true;
+}