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