diff --git a/tools/fiptool/fiptool.c b/tools/fiptool/fiptool.c index 4b91b1f..6145e26 100644 --- a/tools/fiptool/fiptool.c +++ b/tools/fiptool/fiptool.c @@ -200,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++; } @@ -237,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++; } @@ -397,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, @@ -454,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)