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