fiptool: embed fip_toc_entry in struct image

The struct image has "uuid" and "size" to memorize the field values
they had in the TOC entry.  So, parse_fip() copies them from struct
fip_toc_entry to struct image, then pack_images() copies them back
to struct fip_toc_entry.

The next commit (support --align option) will require to save the
"offset" field as well.  This makes me realize that struct image
can embed struct fip_toc_entry.

This commit will allow the "flags" field to persevere the "update"
command.  At this moment, the "flags" is not used in a useful way.
(Yet, platforms can save their own parameters in the flags field.)
It makes sense to save it unless users explicitly replace the image.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
diff --git a/tools/fiptool/fiptool.c b/tools/fiptool/fiptool.c
index b02635f..6288cbf 100644
--- a/tools/fiptool/fiptool.c
+++ b/tools/fiptool/fiptool.c
@@ -263,7 +263,8 @@
 	image_t **p = &image_head;
 
 	while (*p) {
-		if (!memcmp(&(*p)->uuid, &image->uuid, sizeof(image->uuid)))
+		if (!memcmp(&(*p)->toc_e.uuid, &image->toc_e.uuid,
+			    sizeof(image->toc_e.uuid)))
 			break;
 		p = &(*p)->next;
 	}
@@ -337,7 +338,7 @@
 	image_t *image;
 
 	for (image = image_head; image != NULL; image = image->next)
-		if (memcmp(&image->uuid, uuid, sizeof(uuid_t)) == 0)
+		if (!memcmp(&image->toc_e.uuid, uuid, sizeof(*uuid)))
 			return image;
 	return NULL;
 }
@@ -430,7 +431,7 @@
 		 */
 		image = xzalloc(sizeof(*image),
 		    "failed to allocate memory for image");
-		memcpy(&image->uuid, &toc_entry->uuid, sizeof(uuid_t));
+		image->toc_e = *toc_entry;
 		image->buffer = xmalloc(toc_entry->size,
 		    "failed to allocate image buffer, is FIP file corrupted?");
 		/* Overflow checks before memory copy. */
@@ -441,17 +442,16 @@
 
 		memcpy(image->buffer, buf + toc_entry->offset_address,
 		    toc_entry->size);
-		image->size = toc_entry->size;
 
 		/* If this is an unknown image, create a descriptor for it. */
-		desc = lookup_image_desc_from_uuid(&image->uuid);
+		desc = lookup_image_desc_from_uuid(&toc_entry->uuid);
 		if (desc == NULL) {
 			char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
 
-			uuid_to_str(name, sizeof(name), &image->uuid);
+			uuid_to_str(name, sizeof(name), &toc_entry->uuid);
 			snprintf(filename, sizeof(filename), "%s%s",
 			    name, ".bin");
-			desc = new_image_desc(&image->uuid, name, "blob");
+			desc = new_image_desc(&toc_entry->uuid, name, "blob");
 			desc->action = DO_UNPACK;
 			desc->action_arg = xstrdup(filename,
 			    "failed to allocate memory for blob filename");
@@ -486,11 +486,11 @@
 		log_errx("fstat %s", filename);
 
 	image = xzalloc(sizeof(*image), "failed to allocate memory for image");
-	memcpy(&image->uuid, uuid, sizeof(uuid_t));
+	image->toc_e.uuid = *uuid;
 	image->buffer = xmalloc(st.st_size, "failed to allocate image buffer");
 	if (fread(image->buffer, 1, st.st_size, fp) != st.st_size)
 		log_errx("Failed to read %s", filename);
-	image->size = st.st_size;
+	image->toc_e.size = st.st_size;
 
 	fclose(fp);
 	return image;
@@ -503,7 +503,7 @@
 	fp = fopen(filename, "w");
 	if (fp == NULL)
 		log_err("fopen");
-	xfwrite(image->buffer, image->size, fp, filename);
+	xfwrite(image->buffer, image->toc_e.size, fp, filename);
 	fclose(fp);
 	return 0;
 }
@@ -544,8 +544,6 @@
 static int info_cmd(int argc, char *argv[])
 {
 	image_t *image;
-	uint64_t image_offset;
-	uint64_t image_size;
 	fip_toc_header_t toc_header;
 
 	if (argc != 2)
@@ -563,29 +561,24 @@
 		    (unsigned long long)toc_header.flags);
 	}
 
-	image_offset = sizeof(fip_toc_header_t) +
-	    (sizeof(fip_toc_entry_t) * (nr_images + 1));
-
 	for (image = image_head; image != NULL; image = image->next) {
 		image_desc_t *desc;
 
-		desc = lookup_image_desc_from_uuid(&image->uuid);
+		desc = lookup_image_desc_from_uuid(&image->toc_e.uuid);
 		assert(desc != NULL);
-		printf("%s: ", desc->name);
-		image_size = image->size;
-		printf("offset=0x%llX, size=0x%llX, cmdline=\"--%s\"",
-		       (unsigned long long)image_offset,
-		       (unsigned long long)image_size,
+		printf("%s: offset=0x%llX, size=0x%llX, cmdline=\"--%s\"",
+		       desc->name,
+		       (unsigned long long)image->toc_e.offset_address,
+		       (unsigned long long)image->toc_e.size,
 		       desc->cmdline_name);
 		if (verbose) {
 			unsigned char md[SHA256_DIGEST_LENGTH];
 
-			SHA256(image->buffer, image_size, md);
+			SHA256(image->buffer, image->toc_e.size, md);
 			printf(", sha256=");
 			md_print(md, sizeof(md));
 		}
 		putchar('\n');
-		image_offset += image_size;
 	}
 
 	free_images();
@@ -605,12 +598,7 @@
 	fip_toc_header_t *toc_header;
 	fip_toc_entry_t *toc_entry;
 	char *buf;
-	uint64_t entry_offset, buf_size, payload_size;
-
-	/* Calculate total payload size and allocate scratch buffer. */
-	payload_size = 0;
-	for (image = image_head; image != NULL; image = image->next)
-		payload_size += image->size;
+	uint64_t entry_offset, buf_size, payload_size = 0;
 
 	buf_size = sizeof(fip_toc_header_t) +
 	    sizeof(fip_toc_entry_t) * (nr_images + 1);
@@ -628,19 +616,15 @@
 
 	entry_offset = buf_size;
 	for (image = image_head; image != NULL; image = image->next) {
-		memcpy(&toc_entry->uuid, &image->uuid, sizeof(uuid_t));
-		toc_entry->offset_address = entry_offset;
-		toc_entry->size = image->size;
-		toc_entry->flags = 0;
-		entry_offset += toc_entry->size;
-		toc_entry++;
+		payload_size += image->toc_e.size;
+		image->toc_e.offset_address = entry_offset;
+		*toc_entry++ = image->toc_e;
+		entry_offset += image->toc_e.size;
 	}
 
 	/* Append a null uuid entry to mark the end of ToC entries. */
-	memcpy(&toc_entry->uuid, &uuid_null, sizeof(uuid_t));
+	memset(toc_entry, 0, sizeof(*toc_entry));
 	toc_entry->offset_address = entry_offset;
-	toc_entry->size = 0;
-	toc_entry->flags = 0;
 
 	/* Generate the FIP file. */
 	fp = fopen(filename, "w");
@@ -657,7 +641,7 @@
 		log_dbgx("Payload size: %zu bytes", payload_size);
 
 	for (image = image_head; image != NULL; image = image->next)
-		xfwrite(image->buffer, image->size, fp, filename);
+		xfwrite(image->buffer, image->toc_e.size, fp, filename);
 
 	fclose(fp);
 	return 0;
diff --git a/tools/fiptool/fiptool.h b/tools/fiptool/fiptool.h
index 6ace400..be0c6f0 100644
--- a/tools/fiptool/fiptool.h
+++ b/tools/fiptool/fiptool.h
@@ -34,6 +34,7 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include "firmware_image_package.h"
 #include "uuid.h"
 
 #define NELEM(x) (sizeof (x) / sizeof *(x))
@@ -61,10 +62,9 @@
 } image_desc_t;
 
 typedef struct image {
-	uuid_t             uuid;
-	size_t             size;
-	void              *buffer;
-	struct image      *next;
+	struct fip_toc_entry toc_e;
+	void                *buffer;
+	struct image        *next;
 } image_t;
 
 typedef struct cmd {