fiptool.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. /*
  2. * Copyright (c) 2016-2023, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef _MSC_VER
  7. #include <sys/mount.h>
  8. #endif
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <assert.h>
  12. #include <errno.h>
  13. #include <limits.h>
  14. #include <stdarg.h>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "fiptool.h"
  20. #include "tbbr_config.h"
  21. #define OPT_TOC_ENTRY 0
  22. #define OPT_PLAT_TOC_FLAGS 1
  23. #define OPT_ALIGN 2
  24. static int info_cmd(int argc, char *argv[]);
  25. static void info_usage(int);
  26. static int create_cmd(int argc, char *argv[]);
  27. static void create_usage(int);
  28. static int update_cmd(int argc, char *argv[]);
  29. static void update_usage(int);
  30. static int unpack_cmd(int argc, char *argv[]);
  31. static void unpack_usage(int);
  32. static int remove_cmd(int argc, char *argv[]);
  33. static void remove_usage(int);
  34. static int version_cmd(int argc, char *argv[]);
  35. static void version_usage(int);
  36. static int help_cmd(int argc, char *argv[]);
  37. static void usage(void);
  38. /* Available subcommands. */
  39. static cmd_t cmds[] = {
  40. { .name = "info", .handler = info_cmd, .usage = info_usage },
  41. { .name = "create", .handler = create_cmd, .usage = create_usage },
  42. { .name = "update", .handler = update_cmd, .usage = update_usage },
  43. { .name = "unpack", .handler = unpack_cmd, .usage = unpack_usage },
  44. { .name = "remove", .handler = remove_cmd, .usage = remove_usage },
  45. { .name = "version", .handler = version_cmd, .usage = version_usage },
  46. { .name = "help", .handler = help_cmd, .usage = NULL },
  47. };
  48. static image_desc_t *image_desc_head;
  49. static size_t nr_image_descs;
  50. static const uuid_t uuid_null;
  51. static int verbose;
  52. static void vlog(int prio, const char *msg, va_list ap)
  53. {
  54. char *prefix[] = { "DEBUG", "WARN", "ERROR" };
  55. fprintf(stderr, "%s: ", prefix[prio]);
  56. vfprintf(stderr, msg, ap);
  57. fputc('\n', stderr);
  58. }
  59. static void log_dbgx(const char *msg, ...)
  60. {
  61. va_list ap;
  62. va_start(ap, msg);
  63. vlog(LOG_DBG, msg, ap);
  64. va_end(ap);
  65. }
  66. static void log_warnx(const char *msg, ...)
  67. {
  68. va_list ap;
  69. va_start(ap, msg);
  70. vlog(LOG_WARN, msg, ap);
  71. va_end(ap);
  72. }
  73. static void log_err(const char *msg, ...)
  74. {
  75. char buf[512];
  76. va_list ap;
  77. va_start(ap, msg);
  78. snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno));
  79. vlog(LOG_ERR, buf, ap);
  80. va_end(ap);
  81. exit(1);
  82. }
  83. static void log_errx(const char *msg, ...)
  84. {
  85. va_list ap;
  86. va_start(ap, msg);
  87. vlog(LOG_ERR, msg, ap);
  88. va_end(ap);
  89. exit(1);
  90. }
  91. static char *xstrdup(const char *s, const char *msg)
  92. {
  93. char *d;
  94. d = strdup(s);
  95. if (d == NULL)
  96. log_errx("strdup: %s", msg);
  97. return d;
  98. }
  99. static void *xmalloc(size_t size, const char *msg)
  100. {
  101. void *d;
  102. d = malloc(size);
  103. if (d == NULL)
  104. log_errx("malloc: %s", msg);
  105. return d;
  106. }
  107. static void *xzalloc(size_t size, const char *msg)
  108. {
  109. return memset(xmalloc(size, msg), 0, size);
  110. }
  111. static void xfwrite(void *buf, size_t size, FILE *fp, const char *filename)
  112. {
  113. if (fwrite(buf, 1, size, fp) != size)
  114. log_errx("Failed to write %s", filename);
  115. }
  116. static image_desc_t *new_image_desc(const uuid_t *uuid,
  117. const char *name, const char *cmdline_name)
  118. {
  119. image_desc_t *desc;
  120. desc = xzalloc(sizeof(*desc),
  121. "failed to allocate memory for image descriptor");
  122. memcpy(&desc->uuid, uuid, sizeof(uuid_t));
  123. desc->name = xstrdup(name,
  124. "failed to allocate memory for image name");
  125. desc->cmdline_name = xstrdup(cmdline_name,
  126. "failed to allocate memory for image command line name");
  127. desc->action = DO_UNSPEC;
  128. return desc;
  129. }
  130. static void set_image_desc_action(image_desc_t *desc, int action,
  131. const char *arg)
  132. {
  133. assert(desc != NULL);
  134. if (desc->action_arg != (char *)DO_UNSPEC)
  135. free(desc->action_arg);
  136. desc->action = action;
  137. desc->action_arg = NULL;
  138. if (arg != NULL)
  139. desc->action_arg = xstrdup(arg,
  140. "failed to allocate memory for argument");
  141. }
  142. static void free_image_desc(image_desc_t *desc)
  143. {
  144. free(desc->name);
  145. free(desc->cmdline_name);
  146. free(desc->action_arg);
  147. if (desc->image) {
  148. free(desc->image->buffer);
  149. free(desc->image);
  150. }
  151. free(desc);
  152. }
  153. static void add_image_desc(image_desc_t *desc)
  154. {
  155. image_desc_t **p = &image_desc_head;
  156. while (*p)
  157. p = &(*p)->next;
  158. assert(*p == NULL);
  159. *p = desc;
  160. nr_image_descs++;
  161. }
  162. static void free_image_descs(void)
  163. {
  164. image_desc_t *desc = image_desc_head, *tmp;
  165. while (desc != NULL) {
  166. tmp = desc->next;
  167. free_image_desc(desc);
  168. desc = tmp;
  169. nr_image_descs--;
  170. }
  171. assert(nr_image_descs == 0);
  172. }
  173. static void fill_image_descs(void)
  174. {
  175. toc_entry_t *toc_entry;
  176. for (toc_entry = toc_entries;
  177. toc_entry->cmdline_name != NULL;
  178. toc_entry++) {
  179. image_desc_t *desc;
  180. desc = new_image_desc(&toc_entry->uuid,
  181. toc_entry->name,
  182. toc_entry->cmdline_name);
  183. add_image_desc(desc);
  184. }
  185. #ifdef PLAT_DEF_FIP_UUID
  186. for (toc_entry = plat_def_toc_entries;
  187. toc_entry->cmdline_name != NULL;
  188. toc_entry++) {
  189. image_desc_t *desc;
  190. desc = new_image_desc(&toc_entry->uuid,
  191. toc_entry->name,
  192. toc_entry->cmdline_name);
  193. add_image_desc(desc);
  194. }
  195. #endif
  196. }
  197. static image_desc_t *lookup_image_desc_from_uuid(const uuid_t *uuid)
  198. {
  199. image_desc_t *desc;
  200. for (desc = image_desc_head; desc != NULL; desc = desc->next)
  201. if (memcmp(&desc->uuid, uuid, sizeof(uuid_t)) == 0)
  202. return desc;
  203. return NULL;
  204. }
  205. static image_desc_t *lookup_image_desc_from_opt(const char *opt)
  206. {
  207. image_desc_t *desc;
  208. for (desc = image_desc_head; desc != NULL; desc = desc->next)
  209. if (strcmp(desc->cmdline_name, opt) == 0)
  210. return desc;
  211. return NULL;
  212. }
  213. static void uuid_to_str(char *s, size_t len, const uuid_t *u)
  214. {
  215. assert(len >= (_UUID_STR_LEN + 1));
  216. snprintf(s, len,
  217. "%02X%02X%02X%02X-%02X%02X-%02X%02X-%04X-%04X%04X%04X",
  218. u->time_low[0], u->time_low[1], u->time_low[2], u->time_low[3],
  219. u->time_mid[0], u->time_mid[1],
  220. u->time_hi_and_version[0], u->time_hi_and_version[1],
  221. (u->clock_seq_hi_and_reserved << 8) | u->clock_seq_low,
  222. (u->node[0] << 8) | u->node[1],
  223. (u->node[2] << 8) | u->node[3],
  224. (u->node[4] << 8) | u->node[5]);
  225. }
  226. static void uuid_from_str(uuid_t *u, const char *s)
  227. {
  228. int n;
  229. if (s == NULL)
  230. log_errx("UUID cannot be NULL");
  231. if (strlen(s) != _UUID_STR_LEN)
  232. log_errx("Invalid UUID: %s", s);
  233. n = sscanf(s,
  234. "%2hhx%2hhx%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
  235. &u->time_low[0], &u->time_low[1], &u->time_low[2], &u->time_low[3],
  236. &u->time_mid[0], &u->time_mid[1],
  237. &u->time_hi_and_version[0], &u->time_hi_and_version[1],
  238. &u->clock_seq_hi_and_reserved, &u->clock_seq_low,
  239. &u->node[0], &u->node[1],
  240. &u->node[2], &u->node[3],
  241. &u->node[4], &u->node[5]);
  242. /*
  243. * Given the format specifier above, we expect 16 items to be scanned
  244. * for a properly formatted UUID.
  245. */
  246. if (n != 16)
  247. log_errx("Invalid UUID: %s", s);
  248. }
  249. static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out)
  250. {
  251. struct BLD_PLAT_STAT st;
  252. FILE *fp;
  253. char *buf, *bufend;
  254. fip_toc_header_t *toc_header;
  255. fip_toc_entry_t *toc_entry;
  256. int terminated = 0;
  257. size_t st_size;
  258. fp = fopen(filename, "rb");
  259. if (fp == NULL)
  260. log_err("fopen %s", filename);
  261. if (fstat(fileno(fp), &st) == -1)
  262. log_err("fstat %s", filename);
  263. st_size = st.st_size;
  264. #ifdef BLKGETSIZE64
  265. if ((st.st_mode & S_IFBLK) != 0)
  266. if (ioctl(fileno(fp), BLKGETSIZE64, &st_size) == -1)
  267. log_err("ioctl %s", filename);
  268. #endif
  269. buf = xmalloc(st_size, "failed to load file into memory");
  270. if (fread(buf, 1, st_size, fp) != st_size)
  271. log_errx("Failed to read %s", filename);
  272. bufend = buf + st_size;
  273. fclose(fp);
  274. if (st_size < sizeof(fip_toc_header_t))
  275. log_errx("FIP %s is truncated", filename);
  276. toc_header = (fip_toc_header_t *)buf;
  277. toc_entry = (fip_toc_entry_t *)(toc_header + 1);
  278. if (toc_header->name != TOC_HEADER_NAME)
  279. log_errx("%s is not a FIP file", filename);
  280. /* Return the ToC header if the caller wants it. */
  281. if (toc_header_out != NULL)
  282. *toc_header_out = *toc_header;
  283. /* Walk through each ToC entry in the file. */
  284. while ((char *)toc_entry + sizeof(*toc_entry) - 1 < bufend) {
  285. image_t *image;
  286. image_desc_t *desc;
  287. /* Found the ToC terminator, we are done. */
  288. if (memcmp(&toc_entry->uuid, &uuid_null, sizeof(uuid_t)) == 0) {
  289. terminated = 1;
  290. break;
  291. }
  292. /*
  293. * Build a new image out of the ToC entry and add it to the
  294. * table of images.
  295. */
  296. image = xzalloc(sizeof(*image),
  297. "failed to allocate memory for image");
  298. image->toc_e = *toc_entry;
  299. image->buffer = xmalloc(toc_entry->size,
  300. "failed to allocate image buffer, is FIP file corrupted?");
  301. /* Overflow checks before memory copy. */
  302. if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address)
  303. log_errx("FIP %s is corrupted: entry size exceeds 64 bit address space",
  304. filename);
  305. if (toc_entry->size + toc_entry->offset_address > st_size)
  306. log_errx("FIP %s is corrupted: entry size exceeds FIP file size",
  307. filename);
  308. memcpy(image->buffer, buf + toc_entry->offset_address,
  309. toc_entry->size);
  310. /* If this is an unknown image, create a descriptor for it. */
  311. desc = lookup_image_desc_from_uuid(&toc_entry->uuid);
  312. if (desc == NULL) {
  313. char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
  314. uuid_to_str(name, sizeof(name), &toc_entry->uuid);
  315. snprintf(filename, sizeof(filename), "%s%s",
  316. name, ".bin");
  317. desc = new_image_desc(&toc_entry->uuid, name, "blob");
  318. desc->action = DO_UNPACK;
  319. desc->action_arg = xstrdup(filename,
  320. "failed to allocate memory for blob filename");
  321. add_image_desc(desc);
  322. }
  323. assert(desc->image == NULL);
  324. desc->image = image;
  325. toc_entry++;
  326. }
  327. if (terminated == 0)
  328. log_errx("FIP %s does not have a ToC terminator entry",
  329. filename);
  330. free(buf);
  331. return 0;
  332. }
  333. static image_t *read_image_from_file(const uuid_t *uuid, const char *filename)
  334. {
  335. struct BLD_PLAT_STAT st;
  336. image_t *image;
  337. FILE *fp;
  338. assert(uuid != NULL);
  339. assert(filename != NULL);
  340. fp = fopen(filename, "rb");
  341. if (fp == NULL)
  342. log_err("fopen %s", filename);
  343. if (fstat(fileno(fp), &st) == -1)
  344. log_errx("fstat %s", filename);
  345. image = xzalloc(sizeof(*image), "failed to allocate memory for image");
  346. image->toc_e.uuid = *uuid;
  347. image->buffer = xmalloc(st.st_size, "failed to allocate image buffer");
  348. if (fread(image->buffer, 1, st.st_size, fp) != st.st_size)
  349. log_errx("Failed to read %s", filename);
  350. image->toc_e.size = st.st_size;
  351. fclose(fp);
  352. return image;
  353. }
  354. static int write_image_to_file(const image_t *image, const char *filename)
  355. {
  356. FILE *fp;
  357. fp = fopen(filename, "wb");
  358. if (fp == NULL)
  359. log_err("fopen");
  360. xfwrite(image->buffer, image->toc_e.size, fp, filename);
  361. fclose(fp);
  362. return 0;
  363. }
  364. static struct option *add_opt(struct option *opts, size_t *nr_opts,
  365. const char *name, int has_arg, int val)
  366. {
  367. opts = realloc(opts, (*nr_opts + 1) * sizeof(*opts));
  368. if (opts == NULL)
  369. log_err("realloc");
  370. opts[*nr_opts].name = name;
  371. opts[*nr_opts].has_arg = has_arg;
  372. opts[*nr_opts].flag = NULL;
  373. opts[*nr_opts].val = val;
  374. ++*nr_opts;
  375. return opts;
  376. }
  377. static struct option *fill_common_opts(struct option *opts, size_t *nr_opts,
  378. int has_arg)
  379. {
  380. image_desc_t *desc;
  381. for (desc = image_desc_head; desc != NULL; desc = desc->next)
  382. opts = add_opt(opts, nr_opts, desc->cmdline_name, has_arg,
  383. OPT_TOC_ENTRY);
  384. return opts;
  385. }
  386. #if !STATIC
  387. static void md_print(const unsigned char *md, size_t len)
  388. {
  389. size_t i;
  390. for (i = 0; i < len; i++)
  391. printf("%02x", md[i]);
  392. }
  393. #endif
  394. static int info_cmd(int argc, char *argv[])
  395. {
  396. image_desc_t *desc;
  397. fip_toc_header_t toc_header;
  398. if (argc != 2)
  399. info_usage(EXIT_FAILURE);
  400. argc--, argv++;
  401. parse_fip(argv[0], &toc_header);
  402. if (verbose) {
  403. log_dbgx("toc_header[name]: 0x%llX",
  404. (unsigned long long)toc_header.name);
  405. log_dbgx("toc_header[serial_number]: 0x%llX",
  406. (unsigned long long)toc_header.serial_number);
  407. log_dbgx("toc_header[flags]: 0x%llX",
  408. (unsigned long long)toc_header.flags);
  409. }
  410. for (desc = image_desc_head; desc != NULL; desc = desc->next) {
  411. image_t *image = desc->image;
  412. if (image == NULL)
  413. continue;
  414. printf("%s: offset=0x%llX, size=0x%llX, cmdline=\"--%s\"",
  415. desc->name,
  416. (unsigned long long)image->toc_e.offset_address,
  417. (unsigned long long)image->toc_e.size,
  418. desc->cmdline_name);
  419. /*
  420. * Omit this informative code portion for:
  421. * Visual Studio missing SHA256.
  422. * Statically linked builds.
  423. */
  424. #if !defined(_MSC_VER) && !STATIC
  425. if (verbose) {
  426. unsigned char md[SHA256_DIGEST_LENGTH];
  427. SHA256(image->buffer, image->toc_e.size, md);
  428. printf(", sha256=");
  429. md_print(md, sizeof(md));
  430. }
  431. #endif
  432. putchar('\n');
  433. }
  434. return 0;
  435. }
  436. static void info_usage(int exit_status)
  437. {
  438. printf("fiptool info FIP_FILENAME\n");
  439. exit(exit_status);
  440. }
  441. static int pack_images(const char *filename, uint64_t toc_flags, unsigned long align)
  442. {
  443. FILE *fp;
  444. image_desc_t *desc;
  445. fip_toc_header_t *toc_header;
  446. fip_toc_entry_t *toc_entry;
  447. char *buf;
  448. uint64_t entry_offset, buf_size, payload_size = 0, pad_size;
  449. size_t nr_images = 0;
  450. for (desc = image_desc_head; desc != NULL; desc = desc->next)
  451. if (desc->image != NULL)
  452. nr_images++;
  453. buf_size = sizeof(fip_toc_header_t) +
  454. sizeof(fip_toc_entry_t) * (nr_images + 1);
  455. buf = calloc(1, buf_size);
  456. if (buf == NULL)
  457. log_err("calloc");
  458. /* Build up header and ToC entries from the image table. */
  459. toc_header = (fip_toc_header_t *)buf;
  460. toc_header->name = TOC_HEADER_NAME;
  461. toc_header->serial_number = TOC_HEADER_SERIAL_NUMBER;
  462. toc_header->flags = toc_flags;
  463. toc_entry = (fip_toc_entry_t *)(toc_header + 1);
  464. entry_offset = buf_size;
  465. for (desc = image_desc_head; desc != NULL; desc = desc->next) {
  466. image_t *image = desc->image;
  467. if (image == NULL || (image->toc_e.size == 0ULL))
  468. continue;
  469. payload_size += image->toc_e.size;
  470. entry_offset = (entry_offset + align - 1) & ~(align - 1);
  471. image->toc_e.offset_address = entry_offset;
  472. *toc_entry++ = image->toc_e;
  473. entry_offset += image->toc_e.size;
  474. }
  475. /*
  476. * Append a null uuid entry to mark the end of ToC entries.
  477. * NOTE the offset address for the last toc_entry must match the fip
  478. * size.
  479. */
  480. memset(toc_entry, 0, sizeof(*toc_entry));
  481. toc_entry->offset_address = (entry_offset + align - 1) & ~(align - 1);
  482. /* Generate the FIP file. */
  483. fp = fopen(filename, "wb");
  484. if (fp == NULL)
  485. log_err("fopen %s", filename);
  486. if (verbose)
  487. log_dbgx("Metadata size: %zu bytes", buf_size);
  488. xfwrite(buf, buf_size, fp, filename);
  489. if (verbose)
  490. log_dbgx("Payload size: %zu bytes", payload_size);
  491. for (desc = image_desc_head; desc != NULL; desc = desc->next) {
  492. image_t *image = desc->image;
  493. if (image == NULL)
  494. continue;
  495. if (fseek(fp, image->toc_e.offset_address, SEEK_SET))
  496. log_errx("Failed to set file position");
  497. xfwrite(image->buffer, image->toc_e.size, fp, filename);
  498. }
  499. if (fseek(fp, entry_offset, SEEK_SET))
  500. log_errx("Failed to set file position");
  501. pad_size = toc_entry->offset_address - entry_offset;
  502. while (pad_size--)
  503. fputc(0x0, fp);
  504. free(buf);
  505. fclose(fp);
  506. return 0;
  507. }
  508. /*
  509. * This function is shared between the create and update subcommands.
  510. * The difference between the two subcommands is that when the FIP file
  511. * is created, the parsing of an existing FIP is skipped. This results
  512. * in update_fip() creating the new FIP file from scratch because the
  513. * internal image table is not populated.
  514. */
  515. static void update_fip(void)
  516. {
  517. image_desc_t *desc;
  518. /* Add or replace images in the FIP file. */
  519. for (desc = image_desc_head; desc != NULL; desc = desc->next) {
  520. image_t *image;
  521. if (desc->action != DO_PACK)
  522. continue;
  523. image = read_image_from_file(&desc->uuid,
  524. desc->action_arg);
  525. if (desc->image != NULL) {
  526. if (verbose) {
  527. log_dbgx("Replacing %s with %s",
  528. desc->cmdline_name,
  529. desc->action_arg);
  530. }
  531. free(desc->image);
  532. desc->image = image;
  533. } else {
  534. if (verbose)
  535. log_dbgx("Adding image %s",
  536. desc->action_arg);
  537. desc->image = image;
  538. }
  539. }
  540. }
  541. static void parse_plat_toc_flags(const char *arg, unsigned long long *toc_flags)
  542. {
  543. unsigned long long flags;
  544. char *endptr;
  545. errno = 0;
  546. flags = strtoull(arg, &endptr, 16);
  547. if (*endptr != '\0' || flags > UINT16_MAX || errno != 0)
  548. log_errx("Invalid platform ToC flags: %s", arg);
  549. /* Platform ToC flags is a 16-bit field occupying bits [32-47]. */
  550. *toc_flags |= flags << 32;
  551. }
  552. static int is_power_of_2(unsigned long x)
  553. {
  554. return x && !(x & (x - 1));
  555. }
  556. static unsigned long get_image_align(char *arg)
  557. {
  558. char *endptr;
  559. unsigned long align;
  560. errno = 0;
  561. align = strtoul(arg, &endptr, 0);
  562. if (*endptr != '\0' || !is_power_of_2(align) || errno != 0)
  563. log_errx("Invalid alignment: %s", arg);
  564. return align;
  565. }
  566. static void parse_blob_opt(char *arg, uuid_t *uuid, char *filename, size_t len)
  567. {
  568. char *p;
  569. for (p = strtok(arg, ","); p != NULL; p = strtok(NULL, ",")) {
  570. if (strncmp(p, "uuid=", strlen("uuid=")) == 0) {
  571. p += strlen("uuid=");
  572. uuid_from_str(uuid, p);
  573. } else if (strncmp(p, "file=", strlen("file=")) == 0) {
  574. p += strlen("file=");
  575. snprintf(filename, len, "%s", p);
  576. }
  577. }
  578. }
  579. static int create_cmd(int argc, char *argv[])
  580. {
  581. struct option *opts = NULL;
  582. size_t nr_opts = 0;
  583. unsigned long long toc_flags = 0;
  584. unsigned long align = 1;
  585. if (argc < 2)
  586. create_usage(EXIT_FAILURE);
  587. opts = fill_common_opts(opts, &nr_opts, required_argument);
  588. opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
  589. OPT_PLAT_TOC_FLAGS);
  590. opts = add_opt(opts, &nr_opts, "align", required_argument, OPT_ALIGN);
  591. opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
  592. opts = add_opt(opts, &nr_opts, NULL, 0, 0);
  593. while (1) {
  594. int c, opt_index = 0;
  595. c = getopt_long(argc, argv, "b:", opts, &opt_index);
  596. if (c == -1)
  597. break;
  598. switch (c) {
  599. case OPT_TOC_ENTRY: {
  600. image_desc_t *desc;
  601. desc = lookup_image_desc_from_opt(opts[opt_index].name);
  602. set_image_desc_action(desc, DO_PACK, optarg);
  603. break;
  604. }
  605. case OPT_PLAT_TOC_FLAGS:
  606. parse_plat_toc_flags(optarg, &toc_flags);
  607. break;
  608. case OPT_ALIGN:
  609. align = get_image_align(optarg);
  610. break;
  611. case 'b': {
  612. char name[_UUID_STR_LEN + 1];
  613. char filename[PATH_MAX] = { 0 };
  614. uuid_t uuid = uuid_null;
  615. image_desc_t *desc;
  616. parse_blob_opt(optarg, &uuid,
  617. filename, sizeof(filename));
  618. if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
  619. filename[0] == '\0')
  620. create_usage(EXIT_FAILURE);
  621. desc = lookup_image_desc_from_uuid(&uuid);
  622. if (desc == NULL) {
  623. uuid_to_str(name, sizeof(name), &uuid);
  624. desc = new_image_desc(&uuid, name, "blob");
  625. add_image_desc(desc);
  626. }
  627. set_image_desc_action(desc, DO_PACK, filename);
  628. break;
  629. }
  630. default:
  631. create_usage(EXIT_FAILURE);
  632. }
  633. }
  634. argc -= optind;
  635. argv += optind;
  636. free(opts);
  637. if (argc == 0)
  638. create_usage(EXIT_SUCCESS);
  639. update_fip();
  640. pack_images(argv[0], toc_flags, align);
  641. return 0;
  642. }
  643. static void create_usage(int exit_status)
  644. {
  645. toc_entry_t *toc_entry = toc_entries;
  646. printf("fiptool create [opts] FIP_FILENAME\n");
  647. printf("\n");
  648. printf("Options:\n");
  649. printf(" --align <value>\t\tEach image is aligned to <value> (default: 1).\n");
  650. printf(" --blob uuid=...,file=...\tAdd an image with the given UUID pointed to by file.\n");
  651. printf(" --plat-toc-flags <value>\t16-bit platform specific flag field occupying bits 32-47 in 64-bit ToC header.\n");
  652. printf("\n");
  653. printf("Specific images are packed with the following options:\n");
  654. for (; toc_entry->cmdline_name != NULL; toc_entry++)
  655. printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
  656. toc_entry->name);
  657. #ifdef PLAT_DEF_FIP_UUID
  658. toc_entry = plat_def_toc_entries;
  659. for (; toc_entry->cmdline_name != NULL; toc_entry++)
  660. printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
  661. toc_entry->name);
  662. #endif
  663. exit(exit_status);
  664. }
  665. static int update_cmd(int argc, char *argv[])
  666. {
  667. struct option *opts = NULL;
  668. size_t nr_opts = 0;
  669. char outfile[PATH_MAX] = { 0 };
  670. fip_toc_header_t toc_header = { 0 };
  671. unsigned long long toc_flags = 0;
  672. unsigned long align = 1;
  673. int pflag = 0;
  674. if (argc < 2)
  675. update_usage(EXIT_FAILURE);
  676. opts = fill_common_opts(opts, &nr_opts, required_argument);
  677. opts = add_opt(opts, &nr_opts, "align", required_argument, OPT_ALIGN);
  678. opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
  679. opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
  680. opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
  681. OPT_PLAT_TOC_FLAGS);
  682. opts = add_opt(opts, &nr_opts, NULL, 0, 0);
  683. while (1) {
  684. int c, opt_index = 0;
  685. c = getopt_long(argc, argv, "b:o:", opts, &opt_index);
  686. if (c == -1)
  687. break;
  688. switch (c) {
  689. case OPT_TOC_ENTRY: {
  690. image_desc_t *desc;
  691. desc = lookup_image_desc_from_opt(opts[opt_index].name);
  692. set_image_desc_action(desc, DO_PACK, optarg);
  693. break;
  694. }
  695. case OPT_PLAT_TOC_FLAGS:
  696. parse_plat_toc_flags(optarg, &toc_flags);
  697. pflag = 1;
  698. break;
  699. case 'b': {
  700. char name[_UUID_STR_LEN + 1];
  701. char filename[PATH_MAX] = { 0 };
  702. uuid_t uuid = uuid_null;
  703. image_desc_t *desc;
  704. parse_blob_opt(optarg, &uuid,
  705. filename, sizeof(filename));
  706. if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
  707. filename[0] == '\0')
  708. update_usage(EXIT_FAILURE);
  709. desc = lookup_image_desc_from_uuid(&uuid);
  710. if (desc == NULL) {
  711. uuid_to_str(name, sizeof(name), &uuid);
  712. desc = new_image_desc(&uuid, name, "blob");
  713. add_image_desc(desc);
  714. }
  715. set_image_desc_action(desc, DO_PACK, filename);
  716. break;
  717. }
  718. case OPT_ALIGN:
  719. align = get_image_align(optarg);
  720. break;
  721. case 'o':
  722. snprintf(outfile, sizeof(outfile), "%s", optarg);
  723. break;
  724. default:
  725. update_usage(EXIT_FAILURE);
  726. }
  727. }
  728. argc -= optind;
  729. argv += optind;
  730. free(opts);
  731. if (argc == 0)
  732. update_usage(EXIT_SUCCESS);
  733. if (outfile[0] == '\0')
  734. snprintf(outfile, sizeof(outfile), "%s", argv[0]);
  735. if (access(argv[0], F_OK) == 0)
  736. parse_fip(argv[0], &toc_header);
  737. if (pflag)
  738. toc_header.flags &= ~(0xffffULL << 32);
  739. toc_flags = (toc_header.flags |= toc_flags);
  740. update_fip();
  741. pack_images(outfile, toc_flags, align);
  742. return 0;
  743. }
  744. static void update_usage(int exit_status)
  745. {
  746. toc_entry_t *toc_entry = toc_entries;
  747. printf("fiptool update [opts] FIP_FILENAME\n");
  748. printf("\n");
  749. printf("Options:\n");
  750. printf(" --align <value>\t\tEach image is aligned to <value> (default: 1).\n");
  751. printf(" --blob uuid=...,file=...\tAdd or update an image with the given UUID pointed to by file.\n");
  752. printf(" --out FIP_FILENAME\t\tSet an alternative output FIP file.\n");
  753. printf(" --plat-toc-flags <value>\t16-bit platform specific flag field occupying bits 32-47 in 64-bit ToC header.\n");
  754. printf("\n");
  755. printf("Specific images are packed with the following options:\n");
  756. for (; toc_entry->cmdline_name != NULL; toc_entry++)
  757. printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
  758. toc_entry->name);
  759. #ifdef PLAT_DEF_FIP_UUID
  760. toc_entry = plat_def_toc_entries;
  761. for (; toc_entry->cmdline_name != NULL; toc_entry++)
  762. printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
  763. toc_entry->name);
  764. #endif
  765. exit(exit_status);
  766. }
  767. static int unpack_cmd(int argc, char *argv[])
  768. {
  769. struct option *opts = NULL;
  770. size_t nr_opts = 0;
  771. char outdir[PATH_MAX] = { 0 };
  772. image_desc_t *desc;
  773. int fflag = 0;
  774. int unpack_all = 1;
  775. if (argc < 2)
  776. unpack_usage(EXIT_FAILURE);
  777. opts = fill_common_opts(opts, &nr_opts, required_argument);
  778. opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
  779. opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
  780. opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
  781. opts = add_opt(opts, &nr_opts, NULL, 0, 0);
  782. while (1) {
  783. int c, opt_index = 0;
  784. c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
  785. if (c == -1)
  786. break;
  787. switch (c) {
  788. case OPT_TOC_ENTRY: {
  789. image_desc_t *desc;
  790. desc = lookup_image_desc_from_opt(opts[opt_index].name);
  791. set_image_desc_action(desc, DO_UNPACK, optarg);
  792. unpack_all = 0;
  793. break;
  794. }
  795. case 'b': {
  796. char name[_UUID_STR_LEN + 1];
  797. char filename[PATH_MAX] = { 0 };
  798. uuid_t uuid = uuid_null;
  799. image_desc_t *desc;
  800. parse_blob_opt(optarg, &uuid,
  801. filename, sizeof(filename));
  802. if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
  803. filename[0] == '\0')
  804. unpack_usage(EXIT_FAILURE);
  805. desc = lookup_image_desc_from_uuid(&uuid);
  806. if (desc == NULL) {
  807. uuid_to_str(name, sizeof(name), &uuid);
  808. desc = new_image_desc(&uuid, name, "blob");
  809. add_image_desc(desc);
  810. }
  811. set_image_desc_action(desc, DO_UNPACK, filename);
  812. unpack_all = 0;
  813. break;
  814. }
  815. case 'f':
  816. fflag = 1;
  817. break;
  818. case 'o':
  819. snprintf(outdir, sizeof(outdir), "%s", optarg);
  820. break;
  821. default:
  822. unpack_usage(EXIT_FAILURE);
  823. }
  824. }
  825. argc -= optind;
  826. argv += optind;
  827. free(opts);
  828. if (argc == 0)
  829. unpack_usage(EXIT_SUCCESS);
  830. parse_fip(argv[0], NULL);
  831. if (outdir[0] != '\0')
  832. if (chdir(outdir) == -1)
  833. log_err("chdir %s", outdir);
  834. /* Unpack all specified images. */
  835. for (desc = image_desc_head; desc != NULL; desc = desc->next) {
  836. char file[PATH_MAX];
  837. image_t *image = desc->image;
  838. if (!unpack_all && desc->action != DO_UNPACK)
  839. continue;
  840. /* Build filename. */
  841. if (desc->action_arg == NULL)
  842. snprintf(file, sizeof(file), "%s.bin",
  843. desc->cmdline_name);
  844. else
  845. snprintf(file, sizeof(file), "%s",
  846. desc->action_arg);
  847. if (image == NULL) {
  848. if (!unpack_all)
  849. log_warnx("%s does not exist in %s",
  850. file, argv[0]);
  851. continue;
  852. }
  853. if (access(file, F_OK) != 0 || fflag) {
  854. if (verbose)
  855. log_dbgx("Unpacking %s", file);
  856. write_image_to_file(image, file);
  857. } else {
  858. log_warnx("File %s already exists, use --force to overwrite it",
  859. file);
  860. }
  861. }
  862. return 0;
  863. }
  864. static void unpack_usage(int exit_status)
  865. {
  866. toc_entry_t *toc_entry = toc_entries;
  867. printf("fiptool unpack [opts] FIP_FILENAME\n");
  868. printf("\n");
  869. printf("Options:\n");
  870. printf(" --blob uuid=...,file=...\tUnpack an image with the given UUID to file.\n");
  871. printf(" --force\t\t\tIf the output file already exists, use --force to overwrite it.\n");
  872. printf(" --out path\t\t\tSet the output directory path.\n");
  873. printf("\n");
  874. printf("Specific images are unpacked with the following options:\n");
  875. for (; toc_entry->cmdline_name != NULL; toc_entry++)
  876. printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
  877. toc_entry->name);
  878. #ifdef PLAT_DEF_FIP_UUID
  879. toc_entry = plat_def_toc_entries;
  880. for (; toc_entry->cmdline_name != NULL; toc_entry++)
  881. printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
  882. toc_entry->name);
  883. #endif
  884. printf("\n");
  885. printf("If no options are provided, all images will be unpacked.\n");
  886. exit(exit_status);
  887. }
  888. static int remove_cmd(int argc, char *argv[])
  889. {
  890. struct option *opts = NULL;
  891. size_t nr_opts = 0;
  892. char outfile[PATH_MAX] = { 0 };
  893. fip_toc_header_t toc_header;
  894. image_desc_t *desc;
  895. unsigned long align = 1;
  896. int fflag = 0;
  897. if (argc < 2)
  898. remove_usage(EXIT_FAILURE);
  899. opts = fill_common_opts(opts, &nr_opts, no_argument);
  900. opts = add_opt(opts, &nr_opts, "align", required_argument, OPT_ALIGN);
  901. opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
  902. opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
  903. opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
  904. opts = add_opt(opts, &nr_opts, NULL, 0, 0);
  905. while (1) {
  906. int c, opt_index = 0;
  907. c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
  908. if (c == -1)
  909. break;
  910. switch (c) {
  911. case OPT_TOC_ENTRY: {
  912. image_desc_t *desc;
  913. desc = lookup_image_desc_from_opt(opts[opt_index].name);
  914. set_image_desc_action(desc, DO_REMOVE, NULL);
  915. break;
  916. }
  917. case OPT_ALIGN:
  918. align = get_image_align(optarg);
  919. break;
  920. case 'b': {
  921. char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
  922. uuid_t uuid = uuid_null;
  923. image_desc_t *desc;
  924. parse_blob_opt(optarg, &uuid,
  925. filename, sizeof(filename));
  926. if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0)
  927. remove_usage(EXIT_FAILURE);
  928. desc = lookup_image_desc_from_uuid(&uuid);
  929. if (desc == NULL) {
  930. uuid_to_str(name, sizeof(name), &uuid);
  931. desc = new_image_desc(&uuid, name, "blob");
  932. add_image_desc(desc);
  933. }
  934. set_image_desc_action(desc, DO_REMOVE, NULL);
  935. break;
  936. }
  937. case 'f':
  938. fflag = 1;
  939. break;
  940. case 'o':
  941. snprintf(outfile, sizeof(outfile), "%s", optarg);
  942. break;
  943. default:
  944. remove_usage(EXIT_FAILURE);
  945. }
  946. }
  947. argc -= optind;
  948. argv += optind;
  949. free(opts);
  950. if (argc == 0)
  951. remove_usage(EXIT_SUCCESS);
  952. if (outfile[0] != '\0' && access(outfile, F_OK) == 0 && !fflag)
  953. log_errx("File %s already exists, use --force to overwrite it",
  954. outfile);
  955. if (outfile[0] == '\0')
  956. snprintf(outfile, sizeof(outfile), "%s", argv[0]);
  957. parse_fip(argv[0], &toc_header);
  958. for (desc = image_desc_head; desc != NULL; desc = desc->next) {
  959. if (desc->action != DO_REMOVE)
  960. continue;
  961. if (desc->image != NULL) {
  962. if (verbose)
  963. log_dbgx("Removing %s",
  964. desc->cmdline_name);
  965. free(desc->image);
  966. desc->image = NULL;
  967. } else {
  968. log_warnx("%s does not exist in %s",
  969. desc->cmdline_name, argv[0]);
  970. }
  971. }
  972. pack_images(outfile, toc_header.flags, align);
  973. return 0;
  974. }
  975. static void remove_usage(int exit_status)
  976. {
  977. toc_entry_t *toc_entry = toc_entries;
  978. printf("fiptool remove [opts] FIP_FILENAME\n");
  979. printf("\n");
  980. printf("Options:\n");
  981. printf(" --align <value>\tEach image is aligned to <value> (default: 1).\n");
  982. printf(" --blob uuid=...\tRemove an image with the given UUID.\n");
  983. printf(" --force\t\tIf the output FIP file already exists, use --force to overwrite it.\n");
  984. printf(" --out FIP_FILENAME\tSet an alternative output FIP file.\n");
  985. printf("\n");
  986. printf("Specific images are removed with the following options:\n");
  987. for (; toc_entry->cmdline_name != NULL; toc_entry++)
  988. printf(" --%-16s\t%s\n", toc_entry->cmdline_name,
  989. toc_entry->name);
  990. #ifdef PLAT_DEF_FIP_UUID
  991. toc_entry = plat_def_toc_entries;
  992. for (; toc_entry->cmdline_name != NULL; toc_entry++)
  993. printf(" --%-16s\t%s\n", toc_entry->cmdline_name,
  994. toc_entry->name);
  995. #endif
  996. exit(exit_status);
  997. }
  998. static int version_cmd(int argc, char *argv[])
  999. {
  1000. #ifdef VERSION
  1001. puts(VERSION);
  1002. #else
  1003. /* If built from fiptool directory, VERSION is not set. */
  1004. puts("Unknown version");
  1005. #endif
  1006. return 0;
  1007. }
  1008. static void version_usage(int exit_status)
  1009. {
  1010. printf("fiptool version\n");
  1011. exit(exit_status);
  1012. }
  1013. static int help_cmd(int argc, char *argv[])
  1014. {
  1015. int i;
  1016. if (argc < 2)
  1017. usage();
  1018. argc--, argv++;
  1019. for (i = 0; i < NELEM(cmds); i++) {
  1020. if (strcmp(cmds[i].name, argv[0]) == 0 &&
  1021. cmds[i].usage != NULL)
  1022. cmds[i].usage(EXIT_SUCCESS);
  1023. }
  1024. if (i == NELEM(cmds))
  1025. printf("No help for subcommand '%s'\n", argv[0]);
  1026. return 0;
  1027. }
  1028. static void usage(void)
  1029. {
  1030. printf("usage: fiptool [--verbose] <command> [<args>]\n");
  1031. printf("Global options supported:\n");
  1032. printf(" --verbose\tEnable verbose output for all commands.\n");
  1033. printf("\n");
  1034. printf("Commands supported:\n");
  1035. printf(" info\t\tList images contained in FIP.\n");
  1036. printf(" create\tCreate a new FIP with the given images.\n");
  1037. printf(" update\tUpdate an existing FIP with the given images.\n");
  1038. printf(" unpack\tUnpack images from FIP.\n");
  1039. printf(" remove\tRemove images from FIP.\n");
  1040. printf(" version\tShow fiptool version.\n");
  1041. printf(" help\t\tShow help for given command.\n");
  1042. exit(EXIT_SUCCESS);
  1043. }
  1044. int main(int argc, char *argv[])
  1045. {
  1046. int i, ret = 0;
  1047. while (1) {
  1048. int c, opt_index = 0;
  1049. static struct option opts[] = {
  1050. { "verbose", no_argument, NULL, 'v' },
  1051. { NULL, no_argument, NULL, 0 }
  1052. };
  1053. /*
  1054. * Set POSIX mode so getopt stops at the first non-option
  1055. * which is the subcommand.
  1056. */
  1057. c = getopt_long(argc, argv, "+v", opts, &opt_index);
  1058. if (c == -1)
  1059. break;
  1060. switch (c) {
  1061. case 'v':
  1062. verbose = 1;
  1063. break;
  1064. default:
  1065. usage();
  1066. }
  1067. }
  1068. argc -= optind;
  1069. argv += optind;
  1070. /* Reset optind for subsequent getopt processing. */
  1071. optind = 0;
  1072. if (argc == 0)
  1073. usage();
  1074. fill_image_descs();
  1075. for (i = 0; i < NELEM(cmds); i++) {
  1076. if (strcmp(cmds[i].name, argv[0]) == 0) {
  1077. ret = cmds[i].handler(argc, argv);
  1078. break;
  1079. }
  1080. }
  1081. if (i == NELEM(cmds))
  1082. usage();
  1083. free_image_descs();
  1084. return ret;
  1085. }