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