aboutsummaryrefslogtreecommitdiff
path: root/core/arch/arm/kernel/fip_sp.c
blob: 4c938a715233dfa7ea81787aa8e73c83f01fc94f (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2021, Arm Limited
 */

#include <crypto/crypto.h>
#include <initcall.h>
#include <kernel/boot.h>
#include <kernel/fip_sp.h>
#include <kernel/panic.h>
#include <kernel/user_ta_store.h>
#include <libfdt.h>
#include <mm/core_memprot.h>
#include <mm/core_mmu.h>
#include <tee/uuid.h>
#include <util.h>

#define SP_PKG_HEADER_MAGIC (0x474b5053)
#define SP_PKG_HEADER_VERSION (0x1)

struct sp_pkg_header {
	uint32_t magic;
	uint32_t version;
	uint32_t pm_offset;
	uint32_t pm_size;
	uint32_t img_offset;
	uint32_t img_size;
};

struct fip_sp_handle {
	const struct fip_sp *fip_sp;
	vaddr_t addr;
	size_t offs;
};

struct fip_sp_list_t fip_sp_list;

static TEE_Result fip_sp_open(const TEE_UUID *uuid,
			      struct user_ta_store_handle **h)
{
	struct fip_sp_handle *handle = NULL;
	struct fip_sp *sp = NULL;
	vaddr_t va = 0;

	if (!uuid || !h)
		return TEE_ERROR_BAD_PARAMETERS;

	for_each_fip_sp(sp) {
		if (!memcmp(&sp->uuid, uuid, sizeof(*uuid))) {
			DMSG("Found SP in FIP: %pUl", (void *)uuid);
			break;
		}
	}

	if (!sp)
		return TEE_ERROR_ITEM_NOT_FOUND;

	assert(cpu_mmu_enabled());

	if (!core_mmu_add_mapping(MEM_AREA_RAM_SEC, sp->pa, sp->size)) {
		EMSG("Failed to map %u bytes at PA 0x%"PRIxPA,
		     sp->size, sp->pa);
		return TEE_ERROR_GENERIC;
	}

	va = (vaddr_t)phys_to_virt(sp->pa, MEM_AREA_RAM_SEC);
	if (!va)
		return TEE_ERROR_GENERIC;

	handle = calloc(1, sizeof(*handle));
	if (!handle)
		return TEE_ERROR_OUT_OF_MEMORY;

	handle->fip_sp = sp;
	handle->addr = va;
	*h = (struct user_ta_store_handle *)handle;

	return TEE_SUCCESS;
}

static TEE_Result fip_sp_get_size(const struct user_ta_store_handle *h,
				  size_t *size)
{
	const struct fip_sp_handle *handle = NULL;

	if (!h || !size)
		return TEE_ERROR_BAD_PARAMETERS;

	handle = (const struct fip_sp_handle *)h;
	*size = handle->fip_sp->size;

	return TEE_SUCCESS;
}

static TEE_Result fip_sp_get_tag(const struct user_ta_store_handle *h,
				 uint8_t *tag, unsigned int *tag_len)
{
	const struct fip_sp_handle *handle = NULL;
	TEE_Result res = TEE_SUCCESS;
	void *ctx = NULL;

	if (!h || !tag_len)
		return TEE_ERROR_BAD_PARAMETERS;

	if (!tag || *tag_len < TEE_SHA256_HASH_SIZE) {
		*tag_len = TEE_SHA256_HASH_SIZE;
		return TEE_ERROR_SHORT_BUFFER;
	}
	*tag_len = TEE_SHA256_HASH_SIZE;

	handle = (const struct fip_sp_handle *)h;

	res = crypto_hash_alloc_ctx(&ctx, TEE_ALG_SHA256);
	if (res)
		return res;
	res = crypto_hash_init(ctx);
	if (res)
		goto out;
	res = crypto_hash_update(ctx, (const uint8_t *)handle->addr,
				 handle->fip_sp->size);
	if (res)
		goto out;
	res = crypto_hash_final(ctx, tag, *tag_len);

out:
	crypto_hash_free_ctx(ctx);
	return res;
}

static TEE_Result fip_sp_read(struct user_ta_store_handle *h, void *data,
			      size_t len)
{
	struct fip_sp_handle *handle = NULL;
	uint8_t *src = NULL;
	size_t next_offs = 0;

	if (!h)
		return TEE_ERROR_BAD_PARAMETERS;

	handle = (struct fip_sp_handle *)h;
	src = (uint8_t *)handle->addr + handle->offs;

	if (ADD_OVERFLOW(handle->offs, len, &next_offs) ||
	    next_offs > handle->fip_sp->size)
		return TEE_ERROR_BAD_PARAMETERS;

	if (data)
		memcpy(data, src, len);

	handle->offs = next_offs;

	return TEE_SUCCESS;
}

static void fip_sp_close(struct user_ta_store_handle *h)
{
	if (h)
		free(h);
}

TEE_TA_REGISTER_TA_STORE(3) = {
	.description = "FIP SP",
	.open = fip_sp_open,
	.get_size = fip_sp_get_size,
	.get_tag = fip_sp_get_tag,
	.read = fip_sp_read,
	.close = fip_sp_close,
};

static TEE_Result process_sp_pkg(paddr_t sp_pkg_pa)
{
	enum teecore_memtypes mtype = MEM_AREA_RAM_SEC;
	struct sp_pkg_header *sp_pkg_header = NULL;
	vaddr_t sp_pkg_va = 0;
	const void *sp_dtb = NULL;
	size_t sp_dtb_size = 0;
	struct fip_sp *sp = NULL;
	int root = 0;
	TEE_Result res = TEE_SUCCESS;

	assert(cpu_mmu_enabled());

	/* Map only the first page of SP package to parse the header */
	if (!core_mmu_add_mapping(mtype, sp_pkg_pa, SMALL_PAGE_SIZE))
		return TEE_ERROR_BAD_PARAMETERS;

	sp_pkg_va = (vaddr_t)phys_to_virt(sp_pkg_pa, mtype);
	if (!sp_pkg_va)
		return TEE_ERROR_GENERIC;

	sp_pkg_header = (struct sp_pkg_header *)sp_pkg_va;

	if (sp_pkg_header->magic != SP_PKG_HEADER_MAGIC) {
		EMSG("Invalid SP package magic");
		return TEE_ERROR_BAD_FORMAT;
	}

	if (sp_pkg_header->version != SP_PKG_HEADER_VERSION) {
		EMSG("Invalid SP header version");
		return TEE_ERROR_BAD_FORMAT;
	}

	/* Map rest of dtb if it's larger than one page */
	sp_dtb_size = ROUNDUP(sizeof(struct sp_pkg_header) +
			      sp_pkg_header->pm_size,
			      SMALL_PAGE_SIZE);

	if (sp_dtb_size > SMALL_PAGE_SIZE) {
		if (!core_mmu_add_mapping(mtype, sp_pkg_pa, sp_dtb_size))
			return TEE_ERROR_BAD_PARAMETERS;
	}

	sp_dtb = (void *)(sp_pkg_va + sp_pkg_header->pm_offset);

	sp = calloc(1, sizeof(struct fip_sp));
	if (!sp)
		return TEE_ERROR_OUT_OF_MEMORY;

	root = fdt_path_offset(sp_dtb, "/");
	res = sp_dt_get_uuid(sp_dtb, root, &sp->uuid);
	if (res) {
		free(sp);
		return res;
	}

	sp->pa = sp_pkg_pa + sp_pkg_header->img_offset;
	sp->size = sp_pkg_header->img_size;
	sp->manifest = sp_dtb;

	SLIST_INSERT_HEAD(&fip_sp_list, sp, link);

	DMSG("FIP SP %pUl size %u", (void *)&sp->uuid, sp->size);

	return TEE_SUCCESS;
}

static TEE_Result fip_sp_init(void)
{
	TEE_Result res = TEE_SUCCESS;
	const void *fdt = NULL;
	int root = 0;
	int spmc_node = 0;
	int subnode = 0;
	uint64_t sp_pkg_addr = 0;

	SLIST_INIT(&fip_sp_list);

	fdt = get_spmc_manifest_dt();
	if (!fdt) {
		EMSG("No SPMC manifest found");
		return TEE_ERROR_GENERIC;
	}

	root = fdt_path_offset(fdt, "/");
	if (root < 0)
		return TEE_ERROR_BAD_FORMAT;

	if (fdt_node_check_compatible(fdt, root, "arm,ffa-core-manifest-1.0"))
		return TEE_ERROR_BAD_FORMAT;

	spmc_node = fdt_node_offset_by_compatible(fdt, root, "arm,optee-spmc");
	if (spmc_node < 0)
		return TEE_ERROR_BAD_FORMAT;

	fdt_for_each_subnode(subnode, fdt, spmc_node) {
		res = sp_dt_get_u64(fdt, subnode, "load_address", &sp_pkg_addr);
		if (res) {
			EMSG("Invalid FIP SP load address");
			panic();
		}

		res = process_sp_pkg(sp_pkg_addr);
		if (res) {
			EMSG("Invalid FIP SP package");
			panic();
		}
	}

	return TEE_SUCCESS;
}

service_init(fip_sp_init);