Skip to content
Snippets Groups Projects
Commit 573bf954 authored by Jonas Höglund's avatar Jonas Höglund
Browse files

Only read the FIT header when possible to speed up reading attributes.

parent 485a38bc
No related branches found
No related tags found
1 merge request!2Only read the FIT header when possible to speed up reading attributes.
/* /*
* dumpimage -- Tool to extract sub images from FIT image. * fdtextract -- Tool to extract sub images from FIT image.
* *
* Copyright (C) 2021 IOPSYS Software Solutions AB. All rights reserved. * Copyright (C) 2021 IOPSYS Software Solutions AB. All rights reserved.
* *
...@@ -89,12 +89,19 @@ static int list_images(char *buf) ...@@ -89,12 +89,19 @@ static int list_images(char *buf)
} }
/* Extract an image embedded in the FIT. */ /* Extract an image embedded in the FIT. */
static int extract_image(char *buf, char *name, char *out) static int extract_image(const char *file, char *name, char *out)
{ {
char path[MAX_PATH_LEN]; char path[MAX_PATH_LEN];
int noffset, fd, count; int noffset, fd, count;
unsigned int data_size, data_offset; unsigned int data_size, data_offset;
const fdt32_t *val; const fdt32_t *val;
char *buf;
size_t len;
buf = utilfdt_read(file, &len);
if (!buf) {
die("could not read: %s\n", file);
}
snprintf(path, MAX_PATH_LEN, "/images/%s", name); snprintf(path, MAX_PATH_LEN, "/images/%s", name);
noffset = fdt_path_offset(buf, path); noffset = fdt_path_offset(buf, path);
...@@ -205,6 +212,38 @@ static int get_attribute(char *buf, char *name) ...@@ -205,6 +212,38 @@ static int get_attribute(char *buf, char *name)
return 0; return 0;
} }
char *read_header(const char *file)
{
char *buf;
int fd, total_size;
size_t len;
fd = open(file, O_RDONLY);
if (fd < 0)
return NULL;
/* Read minimal static struct */
buf = malloc(FDT_V1_SIZE);
if (!buf)
return NULL;
len = read(fd, buf, FDT_V1_SIZE);
if (len < FDT_V1_SIZE)
return NULL;
/* Read rest of header */
total_size = fdt_totalsize(buf);
buf = realloc(buf, total_size);
if (!buf)
return NULL;
len = read(fd, buf + FDT_V1_SIZE, total_size - FDT_V1_SIZE);
if (len < total_size - FDT_V1_SIZE)
return NULL;
return buf;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
const char *file; const char *file;
...@@ -212,7 +251,6 @@ int main(int argc, char *argv[]) ...@@ -212,7 +251,6 @@ int main(int argc, char *argv[])
char *buf, *name = NULL, *out = NULL; char *buf, *name = NULL, *out = NULL;
bool list = false, extract = false, bool list = false, extract = false,
hash = false, attribute = false; hash = false, attribute = false;
size_t len;
while ((opt = util_getopt_long()) != EOF) { while ((opt = util_getopt_long()) != EOF) {
switch (opt) { switch (opt) {
...@@ -242,26 +280,28 @@ int main(int argc, char *argv[]) ...@@ -242,26 +280,28 @@ int main(int argc, char *argv[])
usage("missing input filename"); usage("missing input filename");
file = argv[optind]; file = argv[optind];
buf = utilfdt_read(file, &len); buf = read_header(file);
if (!buf) { if (!buf) {
die("could not read: %s\n", file); die("could not read header from: %s\n", file);
} }
if (fdt_check_header(buf)) { if (fdt_check_header(buf)) {
die("Bad header in %s\n", file); die("Bad header in %s\n", file);
} }
/* Pass the pointer to the header to read attributes. */
if (list) if (list)
ret = list_images(buf); ret = list_images(buf);
if (extract)
ret = extract_image(buf, name, out);
if (hash) if (hash)
ret = get_hash(buf, name); ret = get_hash(buf, name);
if (attribute) if (attribute)
ret = get_attribute(buf, name); ret = get_attribute(buf, name);
/* Let extract image read the entire file. */
if (extract)
ret = extract_image(file, name, out);
return ret; return ret;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment