blob: 7e0b2502e35da3de02db0823efee686dc6a807d1 [file] [log] [blame]
Simon Glass974c98f2021-07-18 14:17:57 -06001// 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#define LOG_CATEGORY UCLASS_TPM
9
10#include <common.h>
11#include <tpm-v1.h>
12#include <tpm-v2.h>
13#include <asm/unaligned.h>
14#include "sandbox_common.h"
15
16#define TPM_ERR_CODE_OFS (2 + 4) /* after tag and size */
17
18int sb_tpm_index_to_seq(u32 index)
19{
20 index &= ~HR_NV_INDEX;
21 switch (index) {
22 case FIRMWARE_NV_INDEX:
23 return NV_SEQ_FIRMWARE;
24 case KERNEL_NV_INDEX:
25 return NV_SEQ_KERNEL;
26 case BACKUP_NV_INDEX:
27 return NV_SEQ_BACKUP;
28 case FWMP_NV_INDEX:
29 return NV_SEQ_FWMP;
30 case MRC_REC_HASH_NV_INDEX:
31 return NV_SEQ_REC_HASH;
32 case 0:
33 return NV_SEQ_GLOBAL_LOCK;
34 case TPM_NV_INDEX_LOCK:
35 return NV_SEQ_ENABLE_LOCKING;
36 }
37
38 printf("Invalid nv index %#x\n", index);
39 return -1;
40}
41
42void sb_tpm_read_data(const struct nvdata_state nvdata[NV_SEQ_COUNT],
43 enum sandbox_nv_space seq, u8 *buf, int data_ofs,
44 int length)
45{
46 const struct nvdata_state *nvd = &nvdata[seq];
47
48 if (!nvd->present)
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);
52 else
53 memcpy(buf + data_ofs, &nvd->data, length);
54}
55
56void sb_tpm_write_data(struct nvdata_state nvdata[NV_SEQ_COUNT],
57 enum sandbox_nv_space seq, const u8 *buf, int data_ofs,
58 int length)
59{
60 struct nvdata_state *nvd = &nvdata[seq];
61
62 if (length > nvd->length)
63 log_err("Invalid length %x (max %x)\n", length, nvd->length);
64 else
65 memcpy(&nvdata[seq].data, buf + data_ofs, length);
66}
Simon Glassf9143c12021-07-18 14:17:59 -060067
68void sb_tpm_define_data(struct nvdata_state nvdata[NV_SEQ_COUNT],
69 enum sandbox_nv_space seq, int length)
70{
71 struct nvdata_state *nvd = &nvdata[seq];
72
73 if (length > NV_DATA_SIZE)
74 log_err("Invalid length %x (max %x)\n", length, NV_DATA_SIZE);
75 nvd->length = length;
76 nvd->present = true;
77}