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