Skip to content
Snippets Groups Projects
Commit e3cefda3 authored by Mattias Barthel's avatar Mattias Barthel
Browse files

fdtextract: Add option -z to extract embedded image size

parent 0ebaf1e7
Branches
No related tags found
1 merge request!10fdtextract: Add option -z to extract embedded image size
Pipeline #59791 passed
...@@ -11,6 +11,7 @@ Options: -[le:o:s:a:hV] ...@@ -11,6 +11,7 @@ Options: -[le:o:s:a:hV]
-o, --out <arg> Output image name -o, --out <arg> Output image name
-s, --hash <arg> Hash of image -s, --hash <arg> Hash of image
-a, --attribute <arg> Get attribute of FIT -a, --attribute <arg> Get attribute of FIT
-z --size <arg> Size of embedded image in FIT
-h, --help Print this help and exit -h, --help Print this help and exit
-V, --version Print version and exit -V, --version Print version and exit
``` ```
......
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
/* Usage related data. */ /* Usage related data. */
static const char usage_synopsis[] = "fdtextract [options] <file>"; 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[] = { static struct option const usage_long_opts[] = {
{"list", no_argument, NULL, 'l'}, {"list", no_argument, NULL, 'l'},
{"extract", a_argument, NULL, 'e'}, {"extract", a_argument, NULL, 'e'},
...@@ -53,6 +53,7 @@ static struct option const usage_long_opts[] = { ...@@ -53,6 +53,7 @@ static struct option const usage_long_opts[] = {
{"hash", a_argument, NULL, 's'}, {"hash", a_argument, NULL, 's'},
{"attribute", a_argument, NULL, 'a'}, {"attribute", a_argument, NULL, 'a'},
{"image", a_argument, NULL, 'i'}, {"image", a_argument, NULL, 'i'},
{"size", a_argument, NULL, 'z'},
USAGE_COMMON_LONG_OPTS USAGE_COMMON_LONG_OPTS
}; };
static const char * const usage_opts_help[] = { static const char * const usage_opts_help[] = {
...@@ -62,6 +63,7 @@ static const char * const usage_opts_help[] = { ...@@ -62,6 +63,7 @@ static const char * const usage_opts_help[] = {
"SHA256 hash of image", "SHA256 hash of image",
"Get attribute of FIT", "Get attribute of FIT",
"Use image in FIT to get attribute", "Use image in FIT to get attribute",
"Size of embedded image in FIT",
USAGE_COMMON_OPTS_HELP USAGE_COMMON_OPTS_HELP
}; };
...@@ -91,6 +93,38 @@ static int list_images(char *buf) ...@@ -91,6 +93,38 @@ static int list_images(char *buf)
return 0; 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) static ssize_t copy_data(int out_fd, int in_fd, ssize_t size)
{ {
ssize_t left = size; ssize_t left = size;
...@@ -311,7 +345,7 @@ int main(int argc, char *argv[]) ...@@ -311,7 +345,7 @@ int main(int argc, char *argv[])
int opt, ret = 0; int opt, ret = 0;
char *buf, *name = NULL, *out = NULL, *imagename = NULL; char *buf, *name = NULL, *out = NULL, *imagename = NULL;
bool list = false, extract = false, bool list = false, extract = false,
hash = false, attribute = false; hash = false, attribute = false, size = false;
int in_fd, out_fd = STDOUT_FILENO; int in_fd, out_fd = STDOUT_FILENO;
while ((opt = util_getopt_long()) != EOF) { while ((opt = util_getopt_long()) != EOF) {
...@@ -325,6 +359,10 @@ int main(int argc, char *argv[]) ...@@ -325,6 +359,10 @@ int main(int argc, char *argv[])
case 'l': case 'l':
list = true; list = true;
break; break;
case 'z':
size = true;
name = optarg;
break;
case 'e': case 'e':
extract = true; extract = true;
name = optarg; name = optarg;
...@@ -379,6 +417,9 @@ int main(int argc, char *argv[]) ...@@ -379,6 +417,9 @@ int main(int argc, char *argv[])
if (list) if (list)
ret = list_images(buf); ret = list_images(buf);
if (size)
ret = get_size(buf, name);
if (hash) if (hash)
ret = get_hash(buf, name); ret = get_hash(buf, name);
...@@ -395,5 +436,5 @@ int main(int argc, char *argv[]) ...@@ -395,5 +436,5 @@ int main(int argc, char *argv[])
free(buf); free(buf);
close(in_fd); close(in_fd);
return ret; return ret ? 1 : 0;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment