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