From e3cefda3b26c9aea3021b20725ce7b31b33eebc4 Mon Sep 17 00:00:00 2001 From: Mattias Barthel <mattias.barthel@iopsys.eu> Date: Thu, 4 Aug 2022 15:37:38 +0200 Subject: [PATCH] fdtextract: Add option -z to extract embedded image size --- README.md | 1 + fdtextract.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5ef474d..3f8bf9a 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Options: -[le:o:s:a:hV] -o, --out <arg> Output image name -s, --hash <arg> Hash of image -a, --attribute <arg> Get attribute of FIT + -z --size <arg> Size of embedded image in FIT -h, --help Print this help and exit -V, --version Print version and exit ``` diff --git a/fdtextract.c b/fdtextract.c index fac6c27..0e5c005 100644 --- a/fdtextract.c +++ b/fdtextract.c @@ -45,7 +45,7 @@ /* Usage related data. */ static const char usage_synopsis[] = "fdtextract [options] <file>"; -static const char usage_short_opts[] = "le:o:s:a:i:" USAGE_COMMON_SHORT_OPTS; +static const char usage_short_opts[] = "le:o:s:a:i:z:" USAGE_COMMON_SHORT_OPTS; static struct option const usage_long_opts[] = { {"list", no_argument, NULL, 'l'}, {"extract", a_argument, NULL, 'e'}, @@ -53,6 +53,7 @@ static struct option const usage_long_opts[] = { {"hash", a_argument, NULL, 's'}, {"attribute", a_argument, NULL, 'a'}, {"image", a_argument, NULL, 'i'}, + {"size", a_argument, NULL, 'z'}, USAGE_COMMON_LONG_OPTS }; static const char * const usage_opts_help[] = { @@ -62,6 +63,7 @@ static const char * const usage_opts_help[] = { "SHA256 hash of image", "Get attribute of FIT", "Use image in FIT to get attribute", + "Size of embedded image in FIT", USAGE_COMMON_OPTS_HELP }; @@ -91,6 +93,38 @@ static int list_images(char *buf) return 0; } +/* Print the size of an image embedded in the FIT. */ +static int get_size(char *buf, char *name) +{ + char path[MAX_PATH_LEN] = {0}; + int noffset, data_size; + const fdt32_t *val; + + snprintf(path, MAX_PATH_LEN, "/images/%s", name); + noffset = fdt_path_offset(buf, path); + if (noffset < 0) { + fprintf(stderr, "Error: could not find image: %s.\n", name); + return -1; + } + + val = fdt_getprop(buf, noffset, "data-size", NULL); + if (val) { + data_size = fdt32_to_cpu(*val); + } else { + fdt_getprop(buf, noffset, "data", &data_size); + } + + if (data_size < 0) { + fprintf(stderr, "Error: Could not get image data size: %s.\n", name); + return -1; + } + + printf("%d\n", data_size); + + return 0; +} + + static ssize_t copy_data(int out_fd, int in_fd, ssize_t size) { ssize_t left = size; @@ -311,7 +345,7 @@ int main(int argc, char *argv[]) int opt, ret = 0; char *buf, *name = NULL, *out = NULL, *imagename = NULL; bool list = false, extract = false, - hash = false, attribute = false; + hash = false, attribute = false, size = false; int in_fd, out_fd = STDOUT_FILENO; while ((opt = util_getopt_long()) != EOF) { @@ -325,6 +359,10 @@ int main(int argc, char *argv[]) case 'l': list = true; break; + case 'z': + size = true; + name = optarg; + break; case 'e': extract = true; name = optarg; @@ -379,6 +417,9 @@ int main(int argc, char *argv[]) if (list) ret = list_images(buf); + if (size) + ret = get_size(buf, name); + if (hash) ret = get_hash(buf, name); @@ -395,5 +436,5 @@ int main(int argc, char *argv[]) free(buf); close(in_fd); - return ret; + return ret ? 1 : 0; } -- GitLab