aboutsummaryrefslogtreecommitdiff
path: root/drivers/tpm/sandbox_common.c
blob: 596e0156389995a1274c96ae9d899cb01acd5fba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// SPDX-License-Identifier: GPL-2.0+
/*
 * Common features for sandbox TPM1 and TPM2 implementations
 *
 * Copyright 2021 Google LLC
 */

#define LOG_CATEGORY	UCLASS_TPM

#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;
}