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)
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;
......
......@@ -34,6 +34,7 @@
#include <stddef.h>
#include <stdint.h>
#include "firmware_image_package.h"
#include "uuid.h"
#define NELEM(x) (sizeof (x) / sizeof *(x))
......@@ -61,8 +62,7 @@ typedef struct image_desc {
} image_desc_t;
typedef struct image {
uuid_t uuid;
size_t size;
struct fip_toc_entry toc_e;
void *buffer;
struct image *next;
} image_t;
......
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