devfip.c 7.5 KB

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