blob: 5581b219fa0a6b960f6fdd0acddb116f03adecf4 [file] [log] [blame]
Olivier Deprezaf650a32019-11-06 14:00:43 +01001/*
2 * Copyright (c) 2019, 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
21struct fipfile {
22 chan_t *c;
23 long offset[NR_FILES];
24 long size[NR_FILES];
25};
26
27struct fip_entry {
28 uuid_t uuid;
29 long long offset_address;
30 long long size;
31 long long flags;
32};
33
34struct 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 ******************************************************************************/
47static 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 {"rot-k.crt", UUID_ROT_KEY_CERT},
77 {"nt-k.crt", UUID_NON_TRUSTED_WORLD_KEY_CERT}
78};
79
80/*******************************************************************************
81 * This array contains all the available FIP files.
82 ******************************************************************************/
83static struct fipfile archives[NR_FIPS];
84
85/*******************************************************************************
86 * This variable stores the current number of registered FIP files.
87 ******************************************************************************/
88static int nfips;
89
90/*******************************************************************************
91 * This function parses the ToC of the FIP.
92 ******************************************************************************/
93static int get_entry(chan_t *c, struct fip_entry *entry)
94{
95 int n;
96
97 n = devtab[c->index]->read(c, entry, sizeof(struct fip_entry));
98 if (n <= 0) {
99 return n;
100 }
101
102 if (n != sizeof(struct fip_entry)) {
103 return -1;
104 }
105
106 if ((entry->size > LONG_MAX) || (entry->offset_address > LONG_MAX)) {
107 return -1;
108 }
109
110 if (entry->size == 0) {
111 return 0;
112 }
113
114 return 1;
115}
116
117/*******************************************************************************
118 * This function exposes the FIP images as files.
119 ******************************************************************************/
120static int fipgen(chan_t *c, const dirtab_t *tab, int ntab, int n, dir_t *dir)
121{
122 int i, r;
123 long off;
124 chan_t nc;
125 struct fip_entry entry;
126 struct fipfile *fip;
127 static const char unk[] = "unknown";
128
129 if (c->dev >= nfips) {
130 panic();
131 }
132
133 clone(archives[c->dev].c, &nc);
134 fip = &archives[nc.dev];
135
136 off = STOC_HEADER;
137 for (i = 0; i <= n; i++) {
138 if (fip->offset[i] == -1) {
139 return 0;
140 }
141
142 if (devtab[nc.index]->seek(&nc, off, KSEEK_SET) < 0) {
143 return -1;
144 }
145
146 r = get_entry(&nc, &entry);
147 if (r <= 0) {
148 return r;
149 }
150
151 off += sizeof(entry);
152 }
153
154 for (i = 1; i < NELEM(uuidnames); i++) {
155 if (memcmp(&uuidnames[i].uuid,
156 &entry.uuid, sizeof(uuid_t)) == 0) {
157 break;
158 }
159 }
160
161 if (i < NELEM(uuidnames)) {
162 make_dir_entry(c, dir, uuidnames[i].name,
163 entry.size, n, O_READ);
164 } else {
165 // TODO: set name depending on uuid node value
166 make_dir_entry(c, dir, unk, entry.size, n, O_READ);
167 }
168
169 return 1;
170}
171
172static int fipwalk(chan_t *c, const char *name)
173{
174 return devwalk(c, name, NULL, 0, fipgen);
175}
176
177static int fipstat(chan_t *c, const char *file, dir_t *dir)
178{
179 return devstat(c, file, dir, NULL, 0, fipgen);
180}
181
182/*******************************************************************************
183 * This function copies at most n bytes of the FIP image referred by c into
184 * buf.
185 ******************************************************************************/
186static int fipread(chan_t *c, void *buf, int n)
187{
188 long off;
189 chan_t cs;
190 struct fipfile *fip;
191 long size;
192
193 /* Only makes sense when using debug language */
194 assert(c->qid != CHDIR);
195
196 if ((c->dev >= nfips) || ((c->qid & CHDIR) != 0)) {
197 panic();
198 }
199
200 fip = &archives[c->dev];
201
202 if ((c->qid >= NR_FILES) || (fip->offset[c->qid] < 0)) {
203 panic();
204 }
205
206 clone(fip->c, &cs);
207
208 size = fip->size[c->qid];
209 if (c->offset >= size) {
210 return 0;
211 }
212
213 if (n < 0) {
214 return -1;
215 }
216
217 if (n > (size - c->offset)) {
218 n = size - c->offset;
219 }
220
221 off = fip->offset[c->qid] + c->offset;
222 if (devtab[cs.index]->seek(&cs, off, KSEEK_SET) < 0) {
223 return -1;
224 }
225
226 n = devtab[cs.index]->read(&cs, buf, n);
227 if (n > 0) {
228 c->offset += n;
229 }
230
231 return n;
232}
233
234/*******************************************************************************
235 * This function parses the FIP spec and registers its images in order to
236 * expose them as files in the driver namespace.
237 * It acts as an initialization function for the FIP driver.
238 * It returns a pointer to the newly created channel.
239 ******************************************************************************/
240static chan_t *fipmount(chan_t *c, const char *spec)
241{
242 int r, n, t;
243 chan_t *cspec;
244 uint32_t hname;
245 struct fip_entry entry;
246 struct fipfile *fip;
247 dir_t dir;
248
249 if (nfips == NR_FIPS) {
250 return NULL;
251 }
252
253 fip = &archives[nfips];
254
255 for (n = 0; n < NR_FILES; n++) {
256 fip->offset[n] = -1;
257 }
258
259 cspec = path_to_channel(spec, O_READ);
260 if (cspec == NULL) {
261 return NULL;
262 }
263
264 fip->c = cspec;
265
266 r = devtab[cspec->index]->read(cspec, &hname, sizeof(hname));
267 if (r < 0) {
268 goto err;
269 }
270
271 if ((r != sizeof(hname)) || (hname != TOC_HEADER_NAME)) {
272 goto err;
273 }
274
275 if (stat(spec, &dir) < 0) {
276 goto err;
277 }
278
279 t = cspec->index;
280 if (devtab[t]->seek(cspec, STOC_HEADER, KSEEK_SET) < 0) {
281 goto err;
282 }
283
284 for (n = 0; n < NR_FILES; n++) {
285 switch (get_entry(cspec, &entry)) {
286 case 0:
287 return attach('F', nfips++);
288 case -1:
289 goto err;
290 default:
291 if ((entry.offset_address + entry.size) > dir.length) {
292 goto err;
293 }
294
295 fip->offset[n] = entry.offset_address;
296 fip->size[n] = entry.size;
297 break;
298 }
299 }
300
301err:
302 channel_close(cspec);
303 return NULL;
304}
305
306const dev_t fipdevtab = {
307 .id = 'F',
308 .stat = fipstat,
309 .clone = devclone,
310 .attach = devattach,
311 .walk = fipwalk,
312 .read = fipread,
313 .write = deverrwrite,
314 .mount = fipmount,
315 .seek = devseek
316};
317