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