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