Merge pull request #810 from masahir0y/fiptool_fix

Fix fiptool bug introduced by recent rework
diff --git a/tools/fiptool/fiptool.c b/tools/fiptool/fiptool.c
index fc0c8d6..6145e26 100644
--- a/tools/fiptool/fiptool.c
+++ b/tools/fiptool/fiptool.c
@@ -155,12 +155,17 @@
 	return d;
 }
 
+static void *xzalloc(size_t size, const char *msg)
+{
+	return memset(xmalloc(size, msg), 0, size);
+}
+
 static image_desc_t *new_image_desc(const uuid_t *uuid,
     const char *name, const char *cmdline_name)
 {
 	image_desc_t *desc;
 
-	desc = xmalloc(sizeof(*desc),
+	desc = xzalloc(sizeof(*desc),
 	    "failed to allocate memory for image descriptor");
 	memcpy(&desc->uuid, uuid, sizeof(uuid_t));
 	desc->name = xstrdup(name,
@@ -168,7 +173,6 @@
 	desc->cmdline_name = xstrdup(cmdline_name,
 	    "failed to allocate memory for image command line name");
 	desc->action = DO_UNSPEC;
-	desc->action_arg = NULL;
 	return desc;
 }
 
@@ -196,9 +200,14 @@
 
 static void add_image_desc(image_desc_t *desc)
 {
+	image_desc_t **p = &image_desc_head;
+
 	assert(lookup_image_desc_from_uuid(&desc->uuid) == NULL);
-	desc->next = image_desc_head;
-	image_desc_head = desc;
+
+	while (*p)
+		p = &(*p)->next;
+
+	*p = desc;
 	nr_image_descs++;
 }
 
@@ -233,9 +242,15 @@
 
 static void add_image(image_t *image)
 {
+	image_t **p = &image_head;
+
 	assert(lookup_image_from_uuid(&image->uuid) == NULL);
-	image->next = image_head;
-	image_head = image;
+
+	while (*p)
+		p = &(*p)->next;
+
+	*p = image;
+
 	nr_images++;
 }
 
@@ -393,7 +408,7 @@
 		 * Build a new image out of the ToC entry and add it to the
 		 * table of images.
 		 */
-		image = xmalloc(sizeof(*image),
+		image = xzalloc(sizeof(*image),
 		    "failed to allocate memory for image");
 		memcpy(&image->uuid, &toc_entry->uuid, sizeof(uuid_t));
 		image->buffer = xmalloc(toc_entry->size,
@@ -450,7 +465,7 @@
 	if (fstat(fileno(fp), &st) == -1)
 		log_errx("fstat %s", filename);
 
-	image = xmalloc(sizeof(*image), "failed to allocate memory for image");
+	image = xzalloc(sizeof(*image), "failed to allocate memory for image");
 	memcpy(&image->uuid, uuid, sizeof(uuid_t));
 	image->buffer = xmalloc(st.st_size, "failed to allocate image buffer");
 	if (fread(image->buffer, 1, st.st_size, fp) != st.st_size)