xref: /rk3399_ARM-atf/tools/fiptool/fiptool.c (revision 67973fb4f73411e08f0de39fed0ca21f97de73eb)
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 image_desc_t *new_image_desc(const uuid_t *uuid,
164     const char *name, const char *cmdline_name)
165 {
166 	image_desc_t *desc;
167 
168 	desc = xzalloc(sizeof(*desc),
169 	    "failed to allocate memory for image descriptor");
170 	memcpy(&desc->uuid, uuid, sizeof(uuid_t));
171 	desc->name = xstrdup(name,
172 	    "failed to allocate memory for image name");
173 	desc->cmdline_name = xstrdup(cmdline_name,
174 	    "failed to allocate memory for image command line name");
175 	desc->action = DO_UNSPEC;
176 	return desc;
177 }
178 
179 static void set_image_desc_action(image_desc_t *desc, int action,
180     const char *arg)
181 {
182 	assert(desc != NULL);
183 
184 	if (desc->action_arg != DO_UNSPEC)
185 		free(desc->action_arg);
186 	desc->action = action;
187 	desc->action_arg = NULL;
188 	if (arg != NULL)
189 		desc->action_arg = xstrdup(arg,
190 		    "failed to allocate memory for argument");
191 }
192 
193 static void free_image_desc(image_desc_t *desc)
194 {
195 	free(desc->name);
196 	free(desc->cmdline_name);
197 	free(desc->action_arg);
198 	free(desc);
199 }
200 
201 static void add_image_desc(image_desc_t *desc)
202 {
203 	image_desc_t **p = &image_desc_head;
204 
205 	assert(lookup_image_desc_from_uuid(&desc->uuid) == NULL);
206 
207 	while (*p)
208 		p = &(*p)->next;
209 
210 	*p = desc;
211 	nr_image_descs++;
212 }
213 
214 static void free_image_descs(void)
215 {
216 	image_desc_t *desc = image_desc_head, *tmp;
217 
218 	while (desc != NULL) {
219 		tmp = desc->next;
220 		free_image_desc(desc);
221 		desc = tmp;
222 		nr_image_descs--;
223 	}
224 	assert(nr_image_descs == 0);
225 }
226 
227 static void fill_image_descs(void)
228 {
229 	toc_entry_t *toc_entry;
230 
231 	for (toc_entry = toc_entries;
232 	     toc_entry->cmdline_name != NULL;
233 	     toc_entry++) {
234 		image_desc_t *desc;
235 
236 		desc = new_image_desc(&toc_entry->uuid,
237 		    toc_entry->name,
238 		    toc_entry->cmdline_name);
239 		add_image_desc(desc);
240 	}
241 }
242 
243 static void add_image(image_t *image)
244 {
245 	image_t **p = &image_head;
246 
247 	assert(lookup_image_from_uuid(&image->uuid) == NULL);
248 
249 	while (*p)
250 		p = &(*p)->next;
251 
252 	*p = image;
253 
254 	nr_images++;
255 }
256 
257 static void free_image(image_t *image)
258 {
259 	free(image->buffer);
260 	free(image);
261 }
262 
263 static void remove_image(image_t *image)
264 {
265 	image_t *tmp = image_head, *prev;
266 
267 	if (tmp == image) {
268 		image_head = tmp->next;
269 		free_image(tmp);
270 	} else {
271 		while (tmp != NULL && tmp != image) {
272 			prev = tmp;
273 			tmp = tmp->next;
274 		}
275 		assert(tmp != NULL);
276 		prev->next = tmp->next;
277 		free_image(tmp);
278 	}
279 	nr_images--;
280 }
281 
282 static void free_images(void)
283 {
284 	image_t *image = image_head, *tmp;
285 
286 	while (image != NULL) {
287 		tmp = image->next;
288 		free_image(image);
289 		image = tmp;
290 		nr_images--;
291 	}
292 	assert(nr_images == 0);
293 }
294 
295 static image_desc_t *lookup_image_desc_from_uuid(const uuid_t *uuid)
296 {
297 	image_desc_t *desc;
298 
299 	for (desc = image_desc_head; desc != NULL; desc = desc->next)
300 		if (memcmp(&desc->uuid, uuid, sizeof(uuid_t)) == 0)
301 			return desc;
302 	return NULL;
303 }
304 
305 static image_desc_t *lookup_image_desc_from_opt(const char *opt)
306 {
307 	image_desc_t *desc;
308 
309 	for (desc = image_desc_head; desc != NULL; desc = desc->next)
310 		if (strcmp(desc->cmdline_name, opt) == 0)
311 			return desc;
312 	return NULL;
313 }
314 
315 static image_t *lookup_image_from_uuid(const uuid_t *uuid)
316 {
317 	image_t *image;
318 
319 	for (image = image_head; image != NULL; image = image->next)
320 		if (memcmp(&image->uuid, uuid, sizeof(uuid_t)) == 0)
321 			return image;
322 	return NULL;
323 }
324 
325 static void uuid_to_str(char *s, size_t len, const uuid_t *u)
326 {
327 	assert(len >= (_UUID_STR_LEN + 1));
328 
329 	snprintf(s, len, "%08X-%04X-%04X-%04X-%04X%04X%04X",
330 	    u->time_low,
331 	    u->time_mid,
332 	    u->time_hi_and_version,
333 	    ((uint16_t)u->clock_seq_hi_and_reserved << 8) | u->clock_seq_low,
334 	    ((uint16_t)u->node[0] << 8) | u->node[1],
335 	    ((uint16_t)u->node[2] << 8) | u->node[3],
336 	    ((uint16_t)u->node[4] << 8) | u->node[5]);
337 }
338 
339 static void uuid_from_str(uuid_t *u, const char *s)
340 {
341 	int n;
342 
343 	if (s == NULL)
344 		log_errx("UUID cannot be NULL");
345 	if (strlen(s) != _UUID_STR_LEN)
346 		log_errx("Invalid UUID: %s", s);
347 
348 	n = sscanf(s,
349 	    "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
350 	    &u->time_low, &u->time_mid, &u->time_hi_and_version,
351 	    &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
352 	    &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
353 	/*
354 	 * Given the format specifier above, we expect 11 items to be scanned
355 	 * for a properly formatted UUID.
356 	 */
357 	if (n != 11)
358 		log_errx("Invalid UUID: %s", s);
359 }
360 
361 static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out)
362 {
363 	struct stat st;
364 	FILE *fp;
365 	char *buf, *bufend;
366 	fip_toc_header_t *toc_header;
367 	fip_toc_entry_t *toc_entry;
368 	int terminated = 0;
369 
370 	fp = fopen(filename, "r");
371 	if (fp == NULL)
372 		log_err("fopen %s", filename);
373 
374 	if (fstat(fileno(fp), &st) == -1)
375 		log_err("fstat %s", filename);
376 
377 	buf = xmalloc(st.st_size, "failed to load file into memory");
378 	if (fread(buf, 1, st.st_size, fp) != st.st_size)
379 		log_errx("Failed to read %s", filename);
380 	bufend = buf + st.st_size;
381 	fclose(fp);
382 
383 	if (st.st_size < sizeof(fip_toc_header_t))
384 		log_errx("FIP %s is truncated", filename);
385 
386 	toc_header = (fip_toc_header_t *)buf;
387 	toc_entry = (fip_toc_entry_t *)(toc_header + 1);
388 
389 	if (toc_header->name != TOC_HEADER_NAME)
390 		log_errx("%s is not a FIP file", filename);
391 
392 	/* Return the ToC header if the caller wants it. */
393 	if (toc_header_out != NULL)
394 		*toc_header_out = *toc_header;
395 
396 	/* Walk through each ToC entry in the file. */
397 	while ((char *)toc_entry + sizeof(*toc_entry) - 1 < bufend) {
398 		image_t *image;
399 		image_desc_t *desc;
400 
401 		/* Found the ToC terminator, we are done. */
402 		if (memcmp(&toc_entry->uuid, &uuid_null, sizeof(uuid_t)) == 0) {
403 			terminated = 1;
404 			break;
405 		}
406 
407 		/*
408 		 * Build a new image out of the ToC entry and add it to the
409 		 * table of images.
410 		 */
411 		image = xzalloc(sizeof(*image),
412 		    "failed to allocate memory for image");
413 		memcpy(&image->uuid, &toc_entry->uuid, sizeof(uuid_t));
414 		image->buffer = xmalloc(toc_entry->size,
415 		    "failed to allocate image buffer, is FIP file corrupted?");
416 		/* Overflow checks before memory copy. */
417 		if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address)
418 			log_errx("FIP %s is corrupted", filename);
419 		if (toc_entry->size + toc_entry->offset_address > st.st_size)
420 			log_errx("FIP %s is corrupted", filename);
421 
422 		memcpy(image->buffer, buf + toc_entry->offset_address,
423 		    toc_entry->size);
424 		image->size = toc_entry->size;
425 
426 		/* If this is an unknown image, create a descriptor for it. */
427 		desc = lookup_image_desc_from_uuid(&image->uuid);
428 		if (desc == NULL) {
429 			char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
430 
431 			uuid_to_str(name, sizeof(name), &image->uuid);
432 			snprintf(filename, sizeof(filename), "%s%s",
433 			    name, ".bin");
434 			desc = new_image_desc(&image->uuid, name, "blob");
435 			desc->action = DO_UNPACK;
436 			desc->action_arg = xstrdup(filename,
437 			    "failed to allocate memory for blob filename");
438 			add_image_desc(desc);
439 		}
440 
441 		add_image(image);
442 
443 		toc_entry++;
444 	}
445 
446 	if (terminated == 0)
447 		log_errx("FIP %s does not have a ToC terminator entry",
448 		    filename);
449 	free(buf);
450 	return 0;
451 }
452 
453 static image_t *read_image_from_file(const uuid_t *uuid, const char *filename)
454 {
455 	struct stat st;
456 	image_t *image;
457 	FILE *fp;
458 
459 	assert(uuid != NULL);
460 
461 	fp = fopen(filename, "r");
462 	if (fp == NULL)
463 		log_err("fopen %s", filename);
464 
465 	if (fstat(fileno(fp), &st) == -1)
466 		log_errx("fstat %s", filename);
467 
468 	image = xzalloc(sizeof(*image), "failed to allocate memory for image");
469 	memcpy(&image->uuid, uuid, sizeof(uuid_t));
470 	image->buffer = xmalloc(st.st_size, "failed to allocate image buffer");
471 	if (fread(image->buffer, 1, st.st_size, fp) != st.st_size)
472 		log_errx("Failed to read %s", filename);
473 	image->size = st.st_size;
474 
475 	fclose(fp);
476 	return image;
477 }
478 
479 static int write_image_to_file(const image_t *image, const char *filename)
480 {
481 	FILE *fp;
482 
483 	fp = fopen(filename, "w");
484 	if (fp == NULL)
485 		log_err("fopen");
486 	if (fwrite(image->buffer, 1, image->size, fp) != image->size)
487 		log_errx("Failed to write %s", filename);
488 	fclose(fp);
489 	return 0;
490 }
491 
492 static struct option *add_opt(struct option *opts, size_t *nr_opts,
493     const char *name, int has_arg, int val)
494 {
495 	opts = realloc(opts, (*nr_opts + 1) * sizeof(*opts));
496 	if (opts == NULL)
497 		log_err("realloc");
498 	opts[*nr_opts].name = name;
499 	opts[*nr_opts].has_arg = has_arg;
500 	opts[*nr_opts].flag = NULL;
501 	opts[*nr_opts].val = val;
502 	++*nr_opts;
503 	return opts;
504 }
505 
506 static struct option *fill_common_opts(struct option *opts, size_t *nr_opts,
507     int has_arg)
508 {
509 	image_desc_t *desc;
510 
511 	for (desc = image_desc_head; desc != NULL; desc = desc->next)
512 		opts = add_opt(opts, nr_opts, desc->cmdline_name, has_arg,
513 		    OPT_TOC_ENTRY);
514 	return opts;
515 }
516 
517 static void md_print(const unsigned char *md, size_t len)
518 {
519 	size_t i;
520 
521 	for (i = 0; i < len; i++)
522 		printf("%02x", md[i]);
523 }
524 
525 static int info_cmd(int argc, char *argv[])
526 {
527 	image_t *image;
528 	uint64_t image_offset;
529 	uint64_t image_size;
530 	fip_toc_header_t toc_header;
531 
532 	if (argc != 2)
533 		info_usage();
534 	argc--, argv++;
535 
536 	parse_fip(argv[0], &toc_header);
537 
538 	if (verbose) {
539 		log_dbgx("toc_header[name]: 0x%llX",
540 		    (unsigned long long)toc_header.name);
541 		log_dbgx("toc_header[serial_number]: 0x%llX",
542 		    (unsigned long long)toc_header.serial_number);
543 		log_dbgx("toc_header[flags]: 0x%llX",
544 		    (unsigned long long)toc_header.flags);
545 	}
546 
547 	image_offset = sizeof(fip_toc_header_t) +
548 	    (sizeof(fip_toc_entry_t) * (nr_images + 1));
549 
550 	for (image = image_head; image != NULL; image = image->next) {
551 		image_desc_t *desc;
552 
553 		desc = lookup_image_desc_from_uuid(&image->uuid);
554 		assert(desc != NULL);
555 		printf("%s: ", desc->name);
556 		image_size = image->size;
557 		printf("offset=0x%llX, size=0x%llX, cmdline=\"--%s\"",
558 		       (unsigned long long)image_offset,
559 		       (unsigned long long)image_size,
560 		       desc->cmdline_name);
561 		if (verbose) {
562 			unsigned char md[SHA256_DIGEST_LENGTH];
563 
564 			SHA256(image->buffer, image_size, md);
565 			printf(", sha256=");
566 			md_print(md, sizeof(md));
567 		}
568 		putchar('\n');
569 		image_offset += image_size;
570 	}
571 
572 	free_images();
573 	return 0;
574 }
575 
576 static void info_usage(void)
577 {
578 	printf("fiptool info FIP_FILENAME\n");
579 	exit(1);
580 }
581 
582 static int pack_images(const char *filename, uint64_t toc_flags)
583 {
584 	FILE *fp;
585 	image_t *image;
586 	fip_toc_header_t *toc_header;
587 	fip_toc_entry_t *toc_entry;
588 	char *buf;
589 	uint64_t entry_offset, buf_size, payload_size;
590 
591 	/* Calculate total payload size and allocate scratch buffer. */
592 	payload_size = 0;
593 	for (image = image_head; image != NULL; image = image->next)
594 		payload_size += image->size;
595 
596 	buf_size = sizeof(fip_toc_header_t) +
597 	    sizeof(fip_toc_entry_t) * (nr_images + 1);
598 	buf = calloc(1, buf_size);
599 	if (buf == NULL)
600 		log_err("calloc");
601 
602 	/* Build up header and ToC entries from the image table. */
603 	toc_header = (fip_toc_header_t *)buf;
604 	toc_header->name = TOC_HEADER_NAME;
605 	toc_header->serial_number = TOC_HEADER_SERIAL_NUMBER;
606 	toc_header->flags = toc_flags;
607 
608 	toc_entry = (fip_toc_entry_t *)(toc_header + 1);
609 
610 	entry_offset = buf_size;
611 	for (image = image_head; image != NULL; image = image->next) {
612 		memcpy(&toc_entry->uuid, &image->uuid, sizeof(uuid_t));
613 		toc_entry->offset_address = entry_offset;
614 		toc_entry->size = image->size;
615 		toc_entry->flags = 0;
616 		entry_offset += toc_entry->size;
617 		toc_entry++;
618 	}
619 
620 	/* Append a null uuid entry to mark the end of ToC entries. */
621 	memcpy(&toc_entry->uuid, &uuid_null, sizeof(uuid_t));
622 	toc_entry->offset_address = entry_offset;
623 	toc_entry->size = 0;
624 	toc_entry->flags = 0;
625 
626 	/* Generate the FIP file. */
627 	fp = fopen(filename, "w");
628 	if (fp == NULL)
629 		log_err("fopen %s", filename);
630 
631 	if (verbose)
632 		log_dbgx("Metadata size: %zu bytes", buf_size);
633 
634 	if (fwrite(buf, 1, buf_size, fp) != buf_size)
635 		log_errx("Failed to write image to %s", filename);
636 	free(buf);
637 
638 	if (verbose)
639 		log_dbgx("Payload size: %zu bytes", payload_size);
640 
641 	for (image = image_head; image != NULL; image = image->next)
642 		if (fwrite(image->buffer, 1, image->size, fp) != image->size)
643 			log_errx("Failed to write image to %s", filename);
644 
645 	fclose(fp);
646 	return 0;
647 }
648 
649 /*
650  * This function is shared between the create and update subcommands.
651  * The difference between the two subcommands is that when the FIP file
652  * is created, the parsing of an existing FIP is skipped.  This results
653  * in update_fip() creating the new FIP file from scratch because the
654  * internal image table is not populated.
655  */
656 static void update_fip(void)
657 {
658 	image_desc_t *desc;
659 
660 	/* Add or replace images in the FIP file. */
661 	for (desc = image_desc_head; desc != NULL; desc = desc->next) {
662 		image_t *new_image, *old_image;
663 
664 		if (desc->action != DO_PACK)
665 			continue;
666 
667 		new_image = read_image_from_file(&desc->uuid,
668 		    desc->action_arg);
669 		old_image = lookup_image_from_uuid(&desc->uuid);
670 		if (old_image != NULL) {
671 			if (verbose) {
672 				log_dbgx("Replacing %s with %s",
673 				    desc->cmdline_name,
674 				    desc->action_arg);
675 			}
676 			remove_image(old_image);
677 			add_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 [--blob uuid=...,file=...] "
793 	    "[--plat-toc-flags <value>] [opts] FIP_FILENAME\n");
794 	printf("  --blob uuid=...,file=...\tAdd an image with the given UUID "
795 	    "pointed to by file.\n");
796 	printf("  --plat-toc-flags <value>\t16-bit platform specific flag field "
797 	    "occupying bits 32-47 in 64-bit ToC header.\n");
798 	fputc('\n', stderr);
799 	printf("Specific images are packed with the following options:\n");
800 	for (; toc_entry->cmdline_name != NULL; toc_entry++)
801 		printf("  --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
802 		    toc_entry->name);
803 	exit(1);
804 }
805 
806 static int update_cmd(int argc, char *argv[])
807 {
808 	struct option *opts = NULL;
809 	size_t nr_opts = 0;
810 	char outfile[PATH_MAX] = { 0 };
811 	fip_toc_header_t toc_header = { 0 };
812 	unsigned long long toc_flags = 0;
813 	int pflag = 0;
814 
815 	if (argc < 2)
816 		update_usage();
817 
818 	opts = fill_common_opts(opts, &nr_opts, required_argument);
819 	opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
820 	opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
821 	opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
822 	    OPT_PLAT_TOC_FLAGS);
823 	opts = add_opt(opts, &nr_opts, NULL, 0, 0);
824 
825 	while (1) {
826 		int c, opt_index = 0;
827 
828 		c = getopt_long(argc, argv, "b:o:", opts, &opt_index);
829 		if (c == -1)
830 			break;
831 
832 		switch (c) {
833 		case OPT_TOC_ENTRY: {
834 			image_desc_t *desc;
835 
836 			desc = lookup_image_desc_from_opt(opts[opt_index].name);
837 			set_image_desc_action(desc, DO_PACK, optarg);
838 			break;
839 		}
840 		case OPT_PLAT_TOC_FLAGS:
841 			parse_plat_toc_flags(optarg, &toc_flags);
842 			pflag = 1;
843 			break;
844 		case 'b': {
845 			char name[_UUID_STR_LEN + 1];
846 			char filename[PATH_MAX] = { 0 };
847 			uuid_t uuid = { 0 };
848 			image_desc_t *desc;
849 
850 			parse_blob_opt(optarg, &uuid,
851 			    filename, sizeof(filename));
852 
853 			if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
854 			    filename[0] == '\0')
855 				update_usage();
856 
857 			desc = lookup_image_desc_from_uuid(&uuid);
858 			if (desc == NULL) {
859 				uuid_to_str(name, sizeof(name), &uuid);
860 				desc = new_image_desc(&uuid, name, "blob");
861 				add_image_desc(desc);
862 			}
863 			set_image_desc_action(desc, DO_PACK, filename);
864 			break;
865 		}
866 		case 'o':
867 			snprintf(outfile, sizeof(outfile), "%s", optarg);
868 			break;
869 		default:
870 			update_usage();
871 		}
872 	}
873 	argc -= optind;
874 	argv += optind;
875 	free(opts);
876 
877 	if (argc == 0)
878 		update_usage();
879 
880 	if (outfile[0] == '\0')
881 		snprintf(outfile, sizeof(outfile), "%s", argv[0]);
882 
883 	if (access(argv[0], F_OK) == 0)
884 		parse_fip(argv[0], &toc_header);
885 
886 	if (pflag)
887 		toc_header.flags &= ~(0xffffULL << 32);
888 	toc_flags = (toc_header.flags |= toc_flags);
889 
890 	update_fip();
891 
892 	pack_images(outfile, toc_flags);
893 	free_images();
894 	return 0;
895 }
896 
897 static void update_usage(void)
898 {
899 	toc_entry_t *toc_entry = toc_entries;
900 
901 	printf("fiptool update [--blob uuid=...,file=...] [--out FIP_FILENAME] "
902 	    "[--plat-toc-flags <value>] [opts] FIP_FILENAME\n");
903 	printf("  --blob uuid=...,file=...\tAdd or update an image "
904 	    "with the given UUID pointed to by file.\n");
905 	printf("  --out FIP_FILENAME\t\tSet an alternative output FIP file.\n");
906 	printf("  --plat-toc-flags <value>\t16-bit platform specific flag field "
907 	    "occupying bits 32-47 in 64-bit ToC header.\n");
908 	fputc('\n', stderr);
909 	printf("Specific images are packed with the following options:\n");
910 	for (; toc_entry->cmdline_name != NULL; toc_entry++)
911 		printf("  --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
912 		    toc_entry->name);
913 	exit(1);
914 }
915 
916 static int unpack_cmd(int argc, char *argv[])
917 {
918 	struct option *opts = NULL;
919 	size_t nr_opts = 0;
920 	char outdir[PATH_MAX] = { 0 };
921 	image_desc_t *desc;
922 	int fflag = 0;
923 	int unpack_all = 1;
924 
925 	if (argc < 2)
926 		unpack_usage();
927 
928 	opts = fill_common_opts(opts, &nr_opts, required_argument);
929 	opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
930 	opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
931 	opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
932 	opts = add_opt(opts, &nr_opts, NULL, 0, 0);
933 
934 	while (1) {
935 		int c, opt_index = 0;
936 
937 		c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
938 		if (c == -1)
939 			break;
940 
941 		switch (c) {
942 		case OPT_TOC_ENTRY: {
943 			image_desc_t *desc;
944 
945 			desc = lookup_image_desc_from_opt(opts[opt_index].name);
946 			set_image_desc_action(desc, DO_UNPACK, optarg);
947 			unpack_all = 0;
948 			break;
949 		}
950 		case 'b': {
951 			char name[_UUID_STR_LEN + 1];
952 			char filename[PATH_MAX] = { 0 };
953 			uuid_t uuid = { 0 };
954 			image_desc_t *desc;
955 
956 			parse_blob_opt(optarg, &uuid,
957 			    filename, sizeof(filename));
958 
959 			if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
960 			    filename[0] == '\0')
961 				unpack_usage();
962 
963 			desc = lookup_image_desc_from_uuid(&uuid);
964 			if (desc == NULL) {
965 				uuid_to_str(name, sizeof(name), &uuid);
966 				desc = new_image_desc(&uuid, name, "blob");
967 				add_image_desc(desc);
968 			}
969 			set_image_desc_action(desc, DO_UNPACK, filename);
970 			unpack_all = 0;
971 			break;
972 		}
973 		case 'f':
974 			fflag = 1;
975 			break;
976 		case 'o':
977 			snprintf(outdir, sizeof(outdir), "%s", optarg);
978 			break;
979 		default:
980 			unpack_usage();
981 		}
982 	}
983 	argc -= optind;
984 	argv += optind;
985 	free(opts);
986 
987 	if (argc == 0)
988 		unpack_usage();
989 
990 	parse_fip(argv[0], NULL);
991 
992 	if (outdir[0] != '\0')
993 		if (chdir(outdir) == -1)
994 			log_err("chdir %s", outdir);
995 
996 	/* Unpack all specified images. */
997 	for (desc = image_desc_head; desc != NULL; desc = desc->next) {
998 		char file[PATH_MAX];
999 		image_t *image;
1000 
1001 		if (!unpack_all && desc->action != DO_UNPACK)
1002 			continue;
1003 
1004 		/* Build filename. */
1005 		if (desc->action_arg == NULL)
1006 			snprintf(file, sizeof(file), "%s.bin",
1007 			    desc->cmdline_name);
1008 		else
1009 			snprintf(file, sizeof(file), "%s",
1010 			    desc->action_arg);
1011 
1012 		image = lookup_image_from_uuid(&desc->uuid);
1013 		if (image == NULL) {
1014 			if (!unpack_all)
1015 				log_warnx("%s does not exist in %s",
1016 				    file, argv[0]);
1017 			continue;
1018 		}
1019 
1020 		if (access(file, F_OK) != 0 || fflag) {
1021 			if (verbose)
1022 				log_dbgx("Unpacking %s", file);
1023 			write_image_to_file(image, file);
1024 		} else {
1025 			log_warnx("File %s already exists, use --force to overwrite it",
1026 			    file);
1027 		}
1028 	}
1029 
1030 	free_images();
1031 	return 0;
1032 }
1033 
1034 static void unpack_usage(void)
1035 {
1036 	toc_entry_t *toc_entry = toc_entries;
1037 
1038 	printf("fiptool unpack [--blob uuid=...,file=...] [--force] "
1039 	    "[--out <path>] [opts] FIP_FILENAME\n");
1040 	printf("  --blob uuid=...,file=...\tUnpack an image with the given UUID "
1041 	    "to file.\n");
1042 	printf("  --force\t\t\tIf the output file already exists, use --force to "
1043 	    "overwrite it.\n");
1044 	printf("  --out path\t\t\tSet the output directory path.\n");
1045 	fputc('\n', stderr);
1046 	printf("Specific images are unpacked with the following options:\n");
1047 	for (; toc_entry->cmdline_name != NULL; toc_entry++)
1048 		printf("  --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
1049 		    toc_entry->name);
1050 	fputc('\n', stderr);
1051 	printf("If no options are provided, all images will be unpacked.\n");
1052 	exit(1);
1053 }
1054 
1055 static int remove_cmd(int argc, char *argv[])
1056 {
1057 	struct option *opts = NULL;
1058 	size_t nr_opts = 0;
1059 	char outfile[PATH_MAX] = { 0 };
1060 	fip_toc_header_t toc_header;
1061 	image_desc_t *desc;
1062 	int fflag = 0;
1063 
1064 	if (argc < 2)
1065 		remove_usage();
1066 
1067 	opts = fill_common_opts(opts, &nr_opts, no_argument);
1068 	opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
1069 	opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
1070 	opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
1071 	opts = add_opt(opts, &nr_opts, NULL, 0, 0);
1072 
1073 	while (1) {
1074 		int c, opt_index = 0;
1075 
1076 		c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
1077 		if (c == -1)
1078 			break;
1079 
1080 		switch (c) {
1081 		case OPT_TOC_ENTRY: {
1082 			image_desc_t *desc;
1083 
1084 			desc = lookup_image_desc_from_opt(opts[opt_index].name);
1085 			set_image_desc_action(desc, DO_REMOVE, NULL);
1086 			break;
1087 		}
1088 		case 'b': {
1089 			char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
1090 			uuid_t uuid = { 0 };
1091 			image_desc_t *desc;
1092 
1093 			parse_blob_opt(optarg, &uuid,
1094 			    filename, sizeof(filename));
1095 
1096 			if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0)
1097 				remove_usage();
1098 
1099 			desc = lookup_image_desc_from_uuid(&uuid);
1100 			if (desc == NULL) {
1101 				uuid_to_str(name, sizeof(name), &uuid);
1102 				desc = new_image_desc(&uuid, name, "blob");
1103 				add_image_desc(desc);
1104 			}
1105 			set_image_desc_action(desc, DO_REMOVE, NULL);
1106 			break;
1107 		}
1108 		case 'f':
1109 			fflag = 1;
1110 			break;
1111 		case 'o':
1112 			snprintf(outfile, sizeof(outfile), "%s", optarg);
1113 			break;
1114 		default:
1115 			remove_usage();
1116 		}
1117 	}
1118 	argc -= optind;
1119 	argv += optind;
1120 	free(opts);
1121 
1122 	if (argc == 0)
1123 		remove_usage();
1124 
1125 	if (outfile[0] != '\0' && access(outfile, F_OK) == 0 && !fflag)
1126 		log_errx("File %s already exists, use --force to overwrite it",
1127 		    outfile);
1128 
1129 	if (outfile[0] == '\0')
1130 		snprintf(outfile, sizeof(outfile), "%s", argv[0]);
1131 
1132 	parse_fip(argv[0], &toc_header);
1133 
1134 	for (desc = image_desc_head; desc != NULL; desc = desc->next) {
1135 		image_t *image;
1136 
1137 		if (desc->action != DO_REMOVE)
1138 			continue;
1139 
1140 		image = lookup_image_from_uuid(&desc->uuid);
1141 		if (image != NULL) {
1142 			if (verbose)
1143 				log_dbgx("Removing %s",
1144 				    desc->cmdline_name);
1145 			remove_image(image);
1146 		} else {
1147 			log_warnx("%s does not exist in %s",
1148 			    desc->cmdline_name, argv[0]);
1149 		}
1150 	}
1151 
1152 	pack_images(outfile, toc_header.flags);
1153 	free_images();
1154 	return 0;
1155 }
1156 
1157 static void remove_usage(void)
1158 {
1159 	toc_entry_t *toc_entry = toc_entries;
1160 
1161 	printf("fiptool remove [--blob uuid=...] [--force] "
1162 	    "[--out FIP_FILENAME] [opts] FIP_FILENAME\n");
1163 	printf("  --blob uuid=...\tRemove an image with the given UUID.\n");
1164 	printf("  --force\t\tIf the output FIP file already exists, use --force to "
1165 	    "overwrite it.\n");
1166 	printf("  --out FIP_FILENAME\tSet an alternative output FIP file.\n");
1167 	fputc('\n', stderr);
1168 	printf("Specific images are removed with the following options:\n");
1169 	for (; toc_entry->cmdline_name != NULL; toc_entry++)
1170 		printf("  --%-16s\t%s\n", toc_entry->cmdline_name,
1171 		    toc_entry->name);
1172 	exit(1);
1173 }
1174 
1175 static int version_cmd(int argc, char *argv[])
1176 {
1177 #ifdef VERSION
1178 	puts(VERSION);
1179 #else
1180 	/* If built from fiptool directory, VERSION is not set. */
1181 	puts("Unknown version");
1182 #endif
1183 	return 0;
1184 }
1185 
1186 static void version_usage(void)
1187 {
1188 	printf("fiptool version\n");
1189 	exit(1);
1190 }
1191 
1192 static int help_cmd(int argc, char *argv[])
1193 {
1194 	int i;
1195 
1196 	if (argc < 2)
1197 		usage();
1198 	argc--, argv++;
1199 
1200 	for (i = 0; i < NELEM(cmds); i++) {
1201 		if (strcmp(cmds[i].name, argv[0]) == 0 &&
1202 		    cmds[i].usage != NULL)
1203 			cmds[i].usage();
1204 	}
1205 	if (i == NELEM(cmds))
1206 		printf("No help for subcommand '%s'\n", argv[0]);
1207 	return 0;
1208 }
1209 
1210 static void usage(void)
1211 {
1212 	printf("usage: [--verbose] fiptool <command> [<args>]\n");
1213 	printf("Global options supported:\n");
1214 	printf("  --verbose\tEnable verbose output for all commands.\n");
1215 	fputc('\n', stderr);
1216 	printf("Commands supported:\n");
1217 	printf("  info\t\tList images contained in FIP.\n");
1218 	printf("  create\tCreate a new FIP with the given images.\n");
1219 	printf("  update\tUpdate an existing FIP with the given images.\n");
1220 	printf("  unpack\tUnpack images from FIP.\n");
1221 	printf("  remove\tRemove images from FIP.\n");
1222 	printf("  version\tShow fiptool version.\n");
1223 	printf("  help\t\tShow help for given command.\n");
1224 	exit(1);
1225 }
1226 
1227 int main(int argc, char *argv[])
1228 {
1229 	int i, ret = 0;
1230 
1231 	while (1) {
1232 		int c, opt_index = 0;
1233 		static struct option opts[] = {
1234 			{ "verbose", no_argument, NULL, 'v' },
1235 			{ NULL, no_argument, NULL, 0 }
1236 		};
1237 
1238 		/*
1239 		 * Set POSIX mode so getopt stops at the first non-option
1240 		 * which is the subcommand.
1241 		 */
1242 		c = getopt_long(argc, argv, "+v", opts, &opt_index);
1243 		if (c == -1)
1244 			break;
1245 
1246 		switch (c) {
1247 		case 'v':
1248 			verbose = 1;
1249 			break;
1250 		default:
1251 			usage();
1252 		}
1253 	}
1254 	argc -= optind;
1255 	argv += optind;
1256 	/* Reset optind for subsequent getopt processing. */
1257 	optind = 0;
1258 
1259 	if (argc == 0)
1260 		usage();
1261 
1262 	fill_image_descs();
1263 	for (i = 0; i < NELEM(cmds); i++) {
1264 		if (strcmp(cmds[i].name, argv[0]) == 0) {
1265 			ret = cmds[i].handler(argc, argv);
1266 			break;
1267 		}
1268 	}
1269 	if (i == NELEM(cmds))
1270 		usage();
1271 	free_image_descs();
1272 	return ret;
1273 }
1274