diff --git a/fdtextract.c b/fdtextract.c index 1b0150efd9bfa385eba0ae0ccceb89225e949a86..a810d4f1600e57d5be4ebac0f7aada697f786e1d 100644 --- a/fdtextract.c +++ b/fdtextract.c @@ -40,20 +40,23 @@ #define FDT_MAGIC_SIZE 4 #define MAX_VERSION 17 #define MAX_PATH_LEN 100 +#define SHA_256_LEN 32 /* Usage related data. */ static const char usage_synopsis[] = "fdtextract [options] <file>"; -static const char usage_short_opts[] = "le:o:" USAGE_COMMON_SHORT_OPTS; +static const char usage_short_opts[] = "le:o:s:" USAGE_COMMON_SHORT_OPTS; static struct option const usage_long_opts[] = { {"list", no_argument, NULL, 'l'}, {"extract", a_argument, NULL, 'e'}, {"out", a_argument, NULL, 'o'}, + {"hash", a_argument, NULL, 's'}, USAGE_COMMON_LONG_OPTS }; static const char * const usage_opts_help[] = { "List images embedded in FIT", "Extract image from FIT", "Output image name", + "Hash of image", USAGE_COMMON_OPTS_HELP }; @@ -142,13 +145,31 @@ static int extract_image(char *buf, char *name, char *out) return 0; } +static int get_hash(char *buf, char *name) +{ + char path[MAX_PATH_LEN]; + int noffset, i; + uint8_t *val; + + snprintf(path, MAX_PATH_LEN, "/images/%s/hash-1", name); + noffset = fdt_path_offset(buf, path); + if (noffset < 0) { + printf("Error: could not find image hash: %s.\n", name); + return -1; + } + + val = (uint8_t *)fdt_getprop(buf, noffset, "value", NULL); + for (i=0; i<SHA_256_LEN; i++) + printf("%x", val[i]); + printf("\n"); +} + int main(int argc, char *argv[]) { const char *file; int opt; char *buf, *name = NULL, *out = NULL; - bool list = false; - bool extract = false; + bool list = false, extract = false, hash = false; size_t len; while ((opt = util_getopt_long()) != EOF) { @@ -165,6 +186,10 @@ int main(int argc, char *argv[]) case 'o': out = optarg; break; + case 's': + hash = true; + name = optarg; + break; } } if (optind != argc - 1) @@ -186,5 +211,8 @@ int main(int argc, char *argv[]) if (extract) extract_image(buf, name, out); + if (hash) + get_hash(buf, name); + return 0; }