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