summaryrefslogtreecommitdiff
path: root/lib/avb/fsl/fsl_avbkey.c
blob: db0f068c56b21440bb997328c39a04811f52ee21 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (C) 2016 Freescale Semiconductor, Inc.
 *
 * SPDX-License-Identifier:     GPL-2.0+
 */

#include <common.h>
#include <stdlib.h>
#include <fsl_caam.h>
#include <fuse.h>

#include "fsl_avb.h"
#include "fsl_avbkey.h"
#include "debug.h"

/* bank 15, GP7, 0xc80[31:0] */
#define AVBKEY_FUSE_BANK 15
#define AVBKEY_FUSE_WORD 0
#define AVBKEY_FUSE_MASK 0xffffffff
#define AVBKEY_FUSE_INIT 0x4156424b /* 'avbk' */


static int encrypt_write(uint8_t *plain, uint32_t len, const char * part, size_t offset) {

	uint8_t *blb;
	uint32_t blbsize;
	int ret;

	blbsize = len + AVB_CAAM_PAD;
	blb = (uint8_t *)malloc(blbsize);
	if (blb == NULL)
		return -1;

	caam_open();
	if (caam_gen_blob((uint32_t)plain, (uint32_t)blb, len)) {
		ret = -1;
		goto fail;
	}

	if (fsl_write_to_partition(NULL, part, offset, blbsize,
			(void *)blb) != AVB_IO_RESULT_OK) {
		ret = -1;
		goto fail;
	}
	ret = 0;

fail:
	free(blb);
	return ret;
}

int rbkidx_erase(const char * kblb_part) {
	int i;
	size_t num_read;
	kblb_hdr_t hdr;
	kblb_tag_t *tag;
	/* read the kblb header */
	if (fsl_read_from_partition(NULL, kblb_part, 0, sizeof(hdr),
			(void *)&hdr, &num_read) != AVB_IO_RESULT_OK) {
		ERR("read partition avbkey error\n");
		return -1;
	}
	if (num_read != sizeof(hdr) ||
			memcmp(hdr.magic, AVB_KBLB_MAGIC, AVB_KBLB_MAGIC_LEN) != 0) {
		ERR("avbkey partition magic not match\n");
		return -1;
	}
	/* reset rollback index */
	uint32_t offset = AVB_RBIDX_START;
	uint32_t rbidx_len = AVB_RBIDX_LEN;
	uint8_t *rbidx = malloc(rbidx_len);
	if (rbidx == NULL)
		return -1;
	memset(rbidx, 0, rbidx_len);
	*(uint64_t *)rbidx = AVB_RBIDX_INITVAL;
	for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_SLOTS; i++) {
		tag = &hdr.rbk_tags[i];
		tag->flag = AVB_RBIDX_FLAG;
		tag->offset = offset;
		tag->len = rbidx_len;
		/* caam encrypt and write */
		if (encrypt_write(rbidx, tag->len, kblb_part, tag->offset) != 0) {
			ERR("write rollback index keyblob error\n");
			free(rbidx);
			return -1;
		}
		offset += AVB_RBIDX_ALIGN;
	}
	free(rbidx);
	/* write hdr */
	if (fsl_write_to_partition(NULL, kblb_part, 0,
				sizeof(hdr), (void *)&hdr) != AVB_IO_RESULT_OK) {
		ERR("write avbkey hdr error\n");
		return -1;
	}
	return 0;
}

int avbkeyblb_init(uint8_t *plainkey, uint32_t keylen, const char * kblb_part) {
	int i;
	kblb_hdr_t hdr;
	kblb_tag_t *tag;
	uint32_t fuse_val;

	/* read fuse to check enable init */
	/* fuse_read read the shadow reg of fuse
	 * use fuse_sense to real read fuse */
#ifdef CONFIG_AVB_FUSE
	if (fuse_sense(AVBKEY_FUSE_BANK, AVBKEY_FUSE_WORD, &fuse_val)) {
#else
	if (fuse_read(AVBKEY_FUSE_BANK, AVBKEY_FUSE_WORD, &fuse_val)) {
#endif
		ERR("read fuse error\n");
		return -1;
	}
	if ((fuse_val & AVBKEY_FUSE_MASK) == AVBKEY_FUSE_INIT) {
		ERR("key already init\n");
		return -1;
	}
	fuse_val = AVBKEY_FUSE_MASK & AVBKEY_FUSE_INIT;

	/* write fuse to prevent init again */
	/* fuse_override write the shadow reg of fuse
	 * use fuse_prog to PERMANENT write fuse */
#ifdef CONFIG_AVB_FUSE
	if (fuse_prog(AVBKEY_FUSE_BANK, AVBKEY_FUSE_WORD, fuse_val)) {
#else
	if (fuse_override(AVBKEY_FUSE_BANK, AVBKEY_FUSE_WORD, fuse_val)) {
#endif
		ERR("write fuse error\n");
		return -1;
	}

	assert(plainkey != NULL);

	/* check overflow */
	if (keylen > AVB_RBIDX_START - AVB_PUBKY_OFFSET) {
		ERR("key len overflow\n");
		return -1;
	}

	/* init pubkey */
	tag = &hdr.pubk_tag;
	tag->flag = AVB_PUBKY_FLAG;
	tag->offset = AVB_PUBKY_OFFSET;
	tag->len = keylen;
	/* caam encrypt and write */
	if (encrypt_write(plainkey, tag->len, kblb_part, tag->offset) != 0) {
		ERR("write pubkey keyblob error\n");
		return -1;
	}

	/* init rollback index */
	uint32_t offset = AVB_RBIDX_START;
	uint32_t rbidx_len = AVB_RBIDX_LEN;
	uint8_t *rbidx = malloc(rbidx_len);
	if (rbidx == NULL)
		return -1;
	memset(rbidx, 0, rbidx_len);
	*(uint64_t *)rbidx = AVB_RBIDX_INITVAL;
	for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_SLOTS; i++) {
		tag = &hdr.rbk_tags[i];
		tag->flag = AVB_RBIDX_FLAG;
		tag->offset = offset;
		tag->len = rbidx_len;
		/* caam encrypt and write */
		if (encrypt_write(rbidx, tag->len, kblb_part, tag->offset) != 0) {
			ERR("write rollback index keyblob error\n");
			free(rbidx);
			return -1;
		}
		offset += AVB_RBIDX_ALIGN;
	}
	free(rbidx);

	/* init hdr */
	memcpy(hdr.magic, AVB_KBLB_MAGIC, AVB_KBLB_MAGIC_LEN);
	if (fsl_write_to_partition(NULL, kblb_part, 0,
				sizeof(hdr), (void *)&hdr) != AVB_IO_RESULT_OK) {
		ERR("write avbkey hdr error\n");
		return -1;
	}
	return 0;
}