Commit 65caa3d0 authored by Masahiro Yamada's avatar Masahiro Yamada
Browse files

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: default avatarMasahiro Yamada <yamada.masahiro@socionext.com>
parent a1da83f5
...@@ -263,7 +263,8 @@ static void replace_image(image_t *image) ...@@ -263,7 +263,8 @@ static void replace_image(image_t *image)
image_t **p = &image_head; image_t **p = &image_head;
while (*p) { 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; break;
p = &(*p)->next; p = &(*p)->next;
} }
...@@ -337,7 +338,7 @@ static image_t *lookup_image_from_uuid(const uuid_t *uuid) ...@@ -337,7 +338,7 @@ static image_t *lookup_image_from_uuid(const uuid_t *uuid)
image_t *image; image_t *image;
for (image = image_head; image != NULL; image = image->next) 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 image;
return NULL; return NULL;
} }
...@@ -430,7 +431,7 @@ static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out) ...@@ -430,7 +431,7 @@ static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out)
*/ */
image = xzalloc(sizeof(*image), image = xzalloc(sizeof(*image),
"failed to allocate memory for 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, image->buffer = xmalloc(toc_entry->size,
"failed to allocate image buffer, is FIP file corrupted?"); "failed to allocate image buffer, is FIP file corrupted?");
/* Overflow checks before memory copy. */ /* Overflow checks before memory copy. */
...@@ -441,17 +442,16 @@ static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out) ...@@ -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, memcpy(image->buffer, buf + toc_entry->offset_address,
toc_entry->size); toc_entry->size);
image->size = toc_entry->size;
/* If this is an unknown image, create a descriptor for it. */ /* 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) { if (desc == NULL) {
char name[_UUID_STR_LEN + 1], filename[PATH_MAX]; 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", snprintf(filename, sizeof(filename), "%s%s",
name, ".bin"); 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 = DO_UNPACK;
desc->action_arg = xstrdup(filename, desc->action_arg = xstrdup(filename,
"failed to allocate memory for blob 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) ...@@ -486,11 +486,11 @@ static image_t *read_image_from_file(const uuid_t *uuid, const char *filename)
log_errx("fstat %s", filename); log_errx("fstat %s", filename);
image = xzalloc(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->toc_e.uuid = *uuid;
image->buffer = xmalloc(st.st_size, "failed to allocate image buffer"); image->buffer = xmalloc(st.st_size, "failed to allocate image buffer");
if (fread(image->buffer, 1, st.st_size, fp) != st.st_size) if (fread(image->buffer, 1, st.st_size, fp) != st.st_size)
log_errx("Failed to read %s", filename); log_errx("Failed to read %s", filename);
image->size = st.st_size; image->toc_e.size = st.st_size;
fclose(fp); fclose(fp);
return image; return image;
...@@ -503,7 +503,7 @@ static int write_image_to_file(const image_t *image, const char *filename) ...@@ -503,7 +503,7 @@ static int write_image_to_file(const image_t *image, const char *filename)
fp = fopen(filename, "w"); fp = fopen(filename, "w");
if (fp == NULL) if (fp == NULL)
log_err("fopen"); log_err("fopen");
xfwrite(image->buffer, image->size, fp, filename); xfwrite(image->buffer, image->toc_e.size, fp, filename);
fclose(fp); fclose(fp);
return 0; return 0;
} }
...@@ -544,8 +544,6 @@ static void md_print(const unsigned char *md, size_t len) ...@@ -544,8 +544,6 @@ static void md_print(const unsigned char *md, size_t len)
static int info_cmd(int argc, char *argv[]) static int info_cmd(int argc, char *argv[])
{ {
image_t *image; image_t *image;
uint64_t image_offset;
uint64_t image_size;
fip_toc_header_t toc_header; fip_toc_header_t toc_header;
if (argc != 2) if (argc != 2)
...@@ -563,29 +561,24 @@ static int info_cmd(int argc, char *argv[]) ...@@ -563,29 +561,24 @@ static int info_cmd(int argc, char *argv[])
(unsigned long long)toc_header.flags); (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) { for (image = image_head; image != NULL; image = image->next) {
image_desc_t *desc; 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); assert(desc != NULL);
printf("%s: ", desc->name); printf("%s: offset=0x%llX, size=0x%llX, cmdline=\"--%s\"",
image_size = image->size; desc->name,
printf("offset=0x%llX, size=0x%llX, cmdline=\"--%s\"", (unsigned long long)image->toc_e.offset_address,
(unsigned long long)image_offset, (unsigned long long)image->toc_e.size,
(unsigned long long)image_size,
desc->cmdline_name); desc->cmdline_name);
if (verbose) { if (verbose) {
unsigned char md[SHA256_DIGEST_LENGTH]; unsigned char md[SHA256_DIGEST_LENGTH];
SHA256(image->buffer, image_size, md); SHA256(image->buffer, image->toc_e.size, md);
printf(", sha256="); printf(", sha256=");
md_print(md, sizeof(md)); md_print(md, sizeof(md));
} }
putchar('\n'); putchar('\n');
image_offset += image_size;
} }
free_images(); free_images();
...@@ -605,12 +598,7 @@ static int pack_images(const char *filename, uint64_t toc_flags) ...@@ -605,12 +598,7 @@ static int pack_images(const char *filename, uint64_t toc_flags)
fip_toc_header_t *toc_header; fip_toc_header_t *toc_header;
fip_toc_entry_t *toc_entry; fip_toc_entry_t *toc_entry;
char *buf; char *buf;
uint64_t entry_offset, buf_size, payload_size; uint64_t entry_offset, buf_size, payload_size = 0;
/* Calculate total payload size and allocate scratch buffer. */
payload_size = 0;
for (image = image_head; image != NULL; image = image->next)
payload_size += image->size;
buf_size = sizeof(fip_toc_header_t) + buf_size = sizeof(fip_toc_header_t) +
sizeof(fip_toc_entry_t) * (nr_images + 1); sizeof(fip_toc_entry_t) * (nr_images + 1);
...@@ -628,19 +616,15 @@ static int pack_images(const char *filename, uint64_t toc_flags) ...@@ -628,19 +616,15 @@ static int pack_images(const char *filename, uint64_t toc_flags)
entry_offset = buf_size; entry_offset = buf_size;
for (image = image_head; image != NULL; image = image->next) { for (image = image_head; image != NULL; image = image->next) {
memcpy(&toc_entry->uuid, &image->uuid, sizeof(uuid_t)); payload_size += image->toc_e.size;
toc_entry->offset_address = entry_offset; image->toc_e.offset_address = entry_offset;
toc_entry->size = image->size; *toc_entry++ = image->toc_e;
toc_entry->flags = 0; entry_offset += image->toc_e.size;
entry_offset += toc_entry->size;
toc_entry++;
} }
/* Append a null uuid entry to mark the end of ToC entries. */ /* 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->offset_address = entry_offset;
toc_entry->size = 0;
toc_entry->flags = 0;
/* Generate the FIP file. */ /* Generate the FIP file. */
fp = fopen(filename, "w"); fp = fopen(filename, "w");
...@@ -657,7 +641,7 @@ static int pack_images(const char *filename, uint64_t toc_flags) ...@@ -657,7 +641,7 @@ static int pack_images(const char *filename, uint64_t toc_flags)
log_dbgx("Payload size: %zu bytes", payload_size); log_dbgx("Payload size: %zu bytes", payload_size);
for (image = image_head; image != NULL; image = image->next) 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); fclose(fp);
return 0; return 0;
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include "firmware_image_package.h"
#include "uuid.h" #include "uuid.h"
#define NELEM(x) (sizeof (x) / sizeof *(x)) #define NELEM(x) (sizeof (x) / sizeof *(x))
...@@ -61,10 +62,9 @@ typedef struct image_desc { ...@@ -61,10 +62,9 @@ typedef struct image_desc {
} image_desc_t; } image_desc_t;
typedef struct image { typedef struct image {
uuid_t uuid; struct fip_toc_entry toc_e;
size_t size; void *buffer;
void *buffer; struct image *next;
struct image *next;
} image_t; } image_t;
typedef struct cmd { typedef struct cmd {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment