diff --git a/tools/fiptool/fiptool.c b/tools/fiptool/fiptool.c index 4b91b1f9f..6145e26d4 100644 --- a/tools/fiptool/fiptool.c +++ b/tools/fiptool/fiptool.c @@ -200,9 +200,14 @@ static void free_image_desc(image_desc_t *desc) 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++; } @@ -237,9 +242,15 @@ static void fill_image_descs(void) 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++; } @@ -397,7 +408,7 @@ static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out) * 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, @@ -454,7 +465,7 @@ static image_t *read_image_from_file(const uuid_t *uuid, const char *filename) 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)