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