xref: /rk3399_ARM-atf/lib/debugfs/devfip.c (revision 007be5ecd14542a5da8533c14293faa1c44c3a7e)
1 /*
2  * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <lib/debugfs.h>
9 #include <limits.h>
10 #include <plat/arm/common/plat_arm.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <tools_share/firmware_image_package.h>
14 
15 #include "dev.h"
16 
17 #define NR_FIPS		1
18 #define STOC_HEADER	(sizeof(fip_toc_header_t))
19 #define STOC_ENTRY	(sizeof(fip_toc_entry_t))
20 
21 struct fipfile {
22 	chan_t	*c;
23 	long	offset[NR_FILES];
24 	long	size[NR_FILES];
25 };
26 
27 struct fip_entry {
28 	uuid_t		uuid;
29 	long long	offset_address;
30 	long long	size;
31 	long long	flags;
32 };
33 
34 struct uuidnames {
35 	const char   name[NAMELEN];
36 	const uuid_t uuid;
37 };
38 
39 /*******************************************************************************
40  * This array links the FIP file names to their UUID.
41  * The elements are ordered according to the image number stored in
42  * tbbr_img_def.h, starting at index 1.
43  *
44  * TODO: this name to uuid binding will preferably be done using
45  * the coming Property Access Layer / Firmware CONFiguration feature.
46  ******************************************************************************/
47 static const struct uuidnames uuidnames[] = {
48 	{"",			{ {0}, {0}, {0}, 0, 0, {0} } },
49 	{"bl2.bin",		UUID_TRUSTED_BOOT_FIRMWARE_BL2},
50 	{"scp-bl2.bin",		UUID_SCP_FIRMWARE_SCP_BL2},
51 	{"bl31.bin",		UUID_EL3_RUNTIME_FIRMWARE_BL31},
52 	{"bl32.bin",		UUID_SECURE_PAYLOAD_BL32},
53 	{"bl33.bin",		UUID_NON_TRUSTED_FIRMWARE_BL33},
54 	{"tb-fw.crt",		UUID_TRUSTED_BOOT_FW_CERT},
55 	{"trstd-k.crt",		UUID_TRUSTED_KEY_CERT},
56 	{"scp-fw-k.crt",	UUID_SCP_FW_KEY_CERT},
57 	{"soc-fw-k.crt",	UUID_SOC_FW_KEY_CERT},
58 	{"tos-fw-k.crt",	UUID_TRUSTED_OS_FW_KEY_CERT},
59 	{"nt-fw-k.crt",		UUID_NON_TRUSTED_FW_KEY_CERT},
60 	{"scp-fw-c.crt",	UUID_SCP_FW_CONTENT_CERT},
61 	{"soc-fw-c.crt",	UUID_SOC_FW_CONTENT_CERT},
62 	{"tos-fw-c.crt",	UUID_TRUSTED_OS_FW_CONTENT_CERT},
63 	{"nt-fw-c.crt",		UUID_NON_TRUSTED_FW_CONTENT_CERT},
64 	{ },
65 	{"fwu.crt",		UUID_TRUSTED_FWU_CERT},
66 	{"scp-bl2u.bin",	UUID_TRUSTED_UPDATE_FIRMWARE_SCP_BL2U},
67 	{"bl2u.bin",		UUID_TRUSTED_UPDATE_FIRMWARE_BL2U},
68 	{"ns-bl2u.bin",		UUID_TRUSTED_UPDATE_FIRMWARE_NS_BL2U},
69 	{"bl32-xtr1.bin",	UUID_SECURE_PAYLOAD_BL32_EXTRA1},
70 	{"bl32-xtr2.bin",	UUID_SECURE_PAYLOAD_BL32_EXTRA2},
71 	{"hw.cfg",		UUID_HW_CONFIG},
72 	{"tb-fw.cfg",		UUID_TB_FW_CONFIG},
73 	{"soc-fw.cfg",		UUID_SOC_FW_CONFIG},
74 	{"tos-fw.cfg",		UUID_TOS_FW_CONFIG},
75 	{"nt-fw.cfg",		UUID_NT_FW_CONFIG},
76 	{"fw.cfg",		UUID_FW_CONFIG},
77 	{"rot-k.crt",		UUID_ROT_KEY_CERT},
78 	{"nt-k.crt",		UUID_NON_TRUSTED_WORLD_KEY_CERT},
79 	{"sip-sp.crt",		UUID_SIP_SECURE_PARTITION_CONTENT_CERT},
80 	{"plat-sp.crt",		UUID_PLAT_SECURE_PARTITION_CONTENT_CERT}
81 };
82 
83 /*******************************************************************************
84  * This array contains all the available FIP files.
85  ******************************************************************************/
86 static struct fipfile archives[NR_FIPS];
87 
88 /*******************************************************************************
89  * This variable stores the current number of registered FIP files.
90  ******************************************************************************/
91 static int nfips;
92 
93 /*******************************************************************************
94  * This function parses the ToC of the FIP.
95  ******************************************************************************/
96 static int get_entry(chan_t *c, struct fip_entry *entry)
97 {
98 	int n;
99 
100 	n = devtab[c->index]->read(c, entry, sizeof(struct fip_entry));
101 	if (n <= 0) {
102 		return n;
103 	}
104 
105 	if (n != sizeof(struct fip_entry)) {
106 		return -1;
107 	}
108 
109 	if (entry->size == 0) {
110 		return 0;
111 	}
112 
113 	return 1;
114 }
115 
116 /*******************************************************************************
117  * This function exposes the FIP images as files.
118  ******************************************************************************/
119 static int fipgen(chan_t *c, const dirtab_t *tab, int ntab, int n, dir_t *dir)
120 {
121 	int i, r;
122 	long off;
123 	chan_t nc;
124 	struct fip_entry entry;
125 	struct fipfile *fip;
126 	static const char unk[] = "unknown";
127 
128 	if (c->dev >= nfips) {
129 		panic();
130 	}
131 
132 	clone(archives[c->dev].c, &nc);
133 	fip = &archives[nc.dev];
134 
135 	off = STOC_HEADER;
136 	for (i = 0; i <= n; i++) {
137 		if (fip->offset[i] == -1) {
138 			return 0;
139 		}
140 
141 		if (devtab[nc.index]->seek(&nc, off, KSEEK_SET) < 0) {
142 			return -1;
143 		}
144 
145 		r = get_entry(&nc, &entry);
146 		if (r <= 0) {
147 			return r;
148 		}
149 
150 		off += sizeof(entry);
151 	}
152 
153 	for (i = 1; i < NELEM(uuidnames); i++) {
154 		if (memcmp(&uuidnames[i].uuid,
155 			   &entry.uuid, sizeof(uuid_t)) == 0) {
156 			break;
157 		}
158 	}
159 
160 	if (i < NELEM(uuidnames)) {
161 		make_dir_entry(c, dir, uuidnames[i].name,
162 			       entry.size, n, O_READ);
163 	} else {
164 		// TODO: set name depending on uuid node value
165 		make_dir_entry(c, dir, unk, entry.size, n, O_READ);
166 	}
167 
168 	return 1;
169 }
170 
171 static int fipwalk(chan_t *c, const char *name)
172 {
173 	return devwalk(c, name, NULL, 0, fipgen);
174 }
175 
176 static int fipstat(chan_t *c, const char *file, dir_t *dir)
177 {
178 	return devstat(c, file, dir, NULL, 0, fipgen);
179 }
180 
181 /*******************************************************************************
182  * This function copies at most n bytes of the FIP image referred by c into
183  * buf.
184  ******************************************************************************/
185 static int fipread(chan_t *c, void *buf, int n)
186 {
187 	long off;
188 	chan_t cs;
189 	struct fipfile *fip;
190 	long size;
191 
192 	/* Only makes sense when using debug language */
193 	assert(c->qid != CHDIR);
194 
195 	if ((c->dev >= nfips) || ((c->qid & CHDIR) != 0)) {
196 		panic();
197 	}
198 
199 	fip = &archives[c->dev];
200 
201 	if ((c->qid >= NR_FILES) || (fip->offset[c->qid] < 0)) {
202 		panic();
203 	}
204 
205 	clone(fip->c, &cs);
206 
207 	size = fip->size[c->qid];
208 	if (c->offset >= size) {
209 		return 0;
210 	}
211 
212 	if (n < 0) {
213 		return -1;
214 	}
215 
216 	if (n > (size - c->offset)) {
217 		n = size - c->offset;
218 	}
219 
220 	off = fip->offset[c->qid] + c->offset;
221 	if (devtab[cs.index]->seek(&cs, off, KSEEK_SET) < 0) {
222 		return -1;
223 	}
224 
225 	n = devtab[cs.index]->read(&cs, buf, n);
226 	if (n > 0) {
227 		c->offset += n;
228 	}
229 
230 	return n;
231 }
232 
233 /*******************************************************************************
234  * This function parses the FIP spec and registers its images in order to
235  * expose them as files in the driver namespace.
236  * It acts as an initialization function for the FIP driver.
237  * It returns a pointer to the newly created channel.
238  ******************************************************************************/
239 static chan_t *fipmount(chan_t *c, const char *spec)
240 {
241 	int r, n, t;
242 	chan_t *cspec;
243 	uint32_t hname;
244 	struct fip_entry entry;
245 	struct fipfile *fip;
246 	dir_t dir;
247 
248 	if (nfips == NR_FIPS) {
249 		return NULL;
250 	}
251 
252 	fip = &archives[nfips];
253 
254 	for (n = 0; n < NR_FILES; n++) {
255 		fip->offset[n] = -1;
256 	}
257 
258 	cspec = path_to_channel(spec, O_READ);
259 	if (cspec == NULL) {
260 		return NULL;
261 	}
262 
263 	fip->c = cspec;
264 
265 	r = devtab[cspec->index]->read(cspec, &hname, sizeof(hname));
266 	if (r < 0) {
267 		goto err;
268 	}
269 
270 	if ((r != sizeof(hname)) || (hname != TOC_HEADER_NAME)) {
271 		goto err;
272 	}
273 
274 	if (stat(spec, &dir) < 0) {
275 		goto err;
276 	}
277 
278 	t = cspec->index;
279 	if (devtab[t]->seek(cspec, STOC_HEADER, KSEEK_SET) < 0) {
280 		goto err;
281 	}
282 
283 	for (n = 0; n < NR_FILES; n++) {
284 		switch (get_entry(cspec, &entry)) {
285 		case 0:
286 			return attach('F', nfips++);
287 		case -1:
288 			goto err;
289 		default:
290 			if ((entry.offset_address + entry.size) > dir.length) {
291 				goto err;
292 			}
293 
294 			fip->offset[n] = entry.offset_address;
295 			fip->size[n] = entry.size;
296 			break;
297 		}
298 	}
299 
300 err:
301 	channel_close(cspec);
302 	return NULL;
303 }
304 
305 const dev_t fipdevtab = {
306 	.id = 'F',
307 	.stat = fipstat,
308 	.clone = devclone,
309 	.attach = devattach,
310 	.walk = fipwalk,
311 	.read = fipread,
312 	.write = deverrwrite,
313 	.mount = fipmount,
314 	.seek = devseek
315 };
316 
317