diff --git a/tools/fiptool/fiptool.c b/tools/fiptool/fiptool.c index b02635f67..6288cbfd4 100644 --- a/tools/fiptool/fiptool.c +++ b/tools/fiptool/fiptool.c @@ -263,7 +263,8 @@ static void replace_image(image_t *image) 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 @@ static image_t *lookup_image_from_uuid(const uuid_t *uuid) 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 @@ static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out) */ 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 @@ static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out) 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 @@ static image_t *read_image_from_file(const uuid_t *uuid, const char *filename) 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 @@ static int write_image_to_file(const image_t *image, const char *filename) 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 void md_print(const unsigned char *md, size_t len) 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 @@ static int info_cmd(int argc, char *argv[]) (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 @@ static int pack_images(const char *filename, uint64_t toc_flags) 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 @@ static int pack_images(const char *filename, uint64_t toc_flags) 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 @@ static int pack_images(const char *filename, uint64_t toc_flags) 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 6ace400d2..be0c6f0da 100644 --- a/tools/fiptool/fiptool.h +++ b/tools/fiptool/fiptool.h @@ -34,6 +34,7 @@ #include #include +#include "firmware_image_package.h" #include "uuid.h" #define NELEM(x) (sizeof (x) / sizeof *(x)) @@ -61,10 +62,9 @@ typedef struct image_desc { } 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 {