From patchwork Wed Sep 18 00:42:16 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guilherme Maciel Ferreira X-Patchwork-Id: 275591 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 7C90D2C00CB for ; Wed, 18 Sep 2013 14:23:00 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 519CF4A133; Wed, 18 Sep 2013 06:22:45 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id mIhKo5Eq0asF; Wed, 18 Sep 2013 06:22:45 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 2A83A4A143; Wed, 18 Sep 2013 06:22:22 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 4701F4A120 for ; Wed, 18 Sep 2013 02:49:11 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id y67ZFbMV6+MO for ; Wed, 18 Sep 2013 02:49:04 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 BL_NJABL=ERR(-1.5) (only DNSBL check requested) Received: from mail-ye0-f174.google.com (mail-ye0-f174.google.com [209.85.213.174]) by theia.denx.de (Postfix) with ESMTPS id 62A224A11F for ; Wed, 18 Sep 2013 02:48:53 +0200 (CEST) Received: by mail-ye0-f174.google.com with SMTP id q4so2552576yen.5 for ; Tue, 17 Sep 2013 17:48:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=Ot3KuImP3NeI+5ZyZT/4vTVdv6H0PXMqo+U4lKPr/rc=; b=rijMOH25JS59ibJY11ekw0CUHWOSnH0HHchvP+4svJfS59axt+KAujnWhbwhalSZB5 0amEuA1afv5hzL6QweItdUyObLAHP3l5B3ajbMP5TkcNSo/P7JCK2EWXcpkWgood2CWP 7Ouub8RlNKVBsTYqbpOLUk1ZG5I1Q8zjSSU5S9X5Aqst/894mI+ldK69N6JeRPz/uf4z cZhWh+fqiIBLZPm17xk9BUt3KjBzDp0jhmls+4tP77xlt+EGQixNWzNHDv5oEynvsfI5 U/9lK9KpN/FQElFSJzJb81P8J6ErXceZql/c7f4u7ZZncP0QeFZow/eeS4Kv6CC++duE GjbA== X-Received: by 10.236.128.232 with SMTP id f68mr4351060yhi.70.1379464951543; Tue, 17 Sep 2013 17:42:31 -0700 (PDT) Received: from localhost.localdomain ([186.222.22.148]) by mx.google.com with ESMTPSA id w42sm50432742yhb.8.1969.12.31.16.00.00 (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 17 Sep 2013 17:42:31 -0700 (PDT) From: guilherme.maciel.ferreira@gmail.com To: wd@denx.de Date: Tue, 17 Sep 2013 21:42:16 -0300 Message-Id: <1379464936-11475-1-git-send-email-guilherme.maciel.ferreira@gmail.com> X-Mailer: git-send-email 1.7.0.4 X-Mailman-Approved-At: Wed, 18 Sep 2013 06:22:18 +0200 Cc: u-boot@lists.denx.de Subject: [U-Boot] [PATCH 3/4] dumpimage: Added a tool to extract "data files" from U-Boot multifile images X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de From: Guilherme Maciel Ferreira Given a multifile image created through the mkimage's -d option: $ mkimage -A x86 -O linux -T multi -n x86 -d vmlinuz:initrd.img:System.map \ multi.img Image Name: x86 Created: Thu Jul 25 10:29:13 2013 Image Type: Intel x86 Linux Multi-File Image (gzip compressed) Data Size: 13722956 Bytes = 13401.32 kB = 13.09 MB Load Address: 00000000 Entry Point: 00000000 Contents: Image 0: 4040128 Bytes = 3945.44 kB = 3.85 MB Image 1: 7991719 Bytes = 7804.41 kB = 7.62 MB Image 2: 1691092 Bytes = 1651.46 kB = 1.61 MB It is possible to perform the converse operation -- extracting any file from the image -- by using the dumpimage's -i option: $ dumpimage -i multi.img -p 2 System.map Although it's feasible to retrieve "data files" from image through scripting, the requirement to embed tools such 'dd', 'awk' and 'sed' for this sole purpose is cumbersome and unreliable -- once you must keep track of file sizes inside the image. Furthermore, extracting data files using "dumpimage" tool is faster than through scripting. Signed-off-by: Guilherme Maciel Ferreira --- Makefile | 1 + README | 9 ++ tools/.gitignore | 1 + tools/Makefile | 26 ++++ tools/default_image.c | 55 +++++++++ tools/dumpimage.c | 318 +++++++++++++++++++++++++++++++++++++++++++++++++ tools/dumpimage.h | 33 +++++ tools/imagetool.h | 11 ++ 8 files changed, 454 insertions(+), 0 deletions(-) create mode 100644 tools/dumpimage.c create mode 100644 tools/dumpimage.h diff --git a/Makefile b/Makefile index 28ddb7e..c9b6c4d 100644 --- a/Makefile +++ b/Makefile @@ -866,6 +866,7 @@ clean: $(obj)tools/envcrc \ $(obj)tools/gdb/{astest,gdbcont,gdbsend} \ $(obj)tools/gen_eth_addr $(obj)tools/img2srec \ + $(obj)tools/dump{env,}image \ $(obj)tools/mk{env,}image $(obj)tools/mpc86x_clk \ $(obj)tools/mk{$(BOARD),}spl \ $(obj)tools/mxsboot \ diff --git a/README b/README index ccd47fa..da1c1e8 100644 --- a/README +++ b/README @@ -5097,6 +5097,15 @@ when your kernel is intended to use an initial ramdisk: Load Address: 0x00000000 Entry Point: 0x00000000 +The "dumpimage" is a tool to disassemble images built by mkimage. Its "-i" +option performs the converse operation of the mkimage's second form (the "-d" +option). Given an image built by mkimage, the dumpimage extracts a "data file" +from the image: + + tools/dumpimage -i image -p position data_file + -i ==> extract from the 'image' a specific 'data_file', \ + indexed by 'position' + Installing a Linux Image: ------------------------- diff --git a/tools/.gitignore b/tools/.gitignore index a7fee26..2320fd8 100644 --- a/tools/.gitignore +++ b/tools/.gitignore @@ -3,6 +3,7 @@ /gen_eth_addr /img2srec /kwboot +/dumpimage /mkenvimage /mkimage /mpc86x_clk diff --git a/tools/Makefile b/tools/Makefile index 4936d54..ea9e51a 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -50,6 +50,7 @@ BIN_FILES-$(CONFIG_BUILD_ENVCRC) += envcrc$(SFX) BIN_FILES-$(CONFIG_CMD_NET) += gen_eth_addr$(SFX) BIN_FILES-$(CONFIG_CMD_LOADS) += img2srec$(SFX) BIN_FILES-$(CONFIG_XWAY_SWAP_BYTES) += xway-swap-bytes$(SFX) +BIN_FILES-y += dumpimage$(SFX) BIN_FILES-y += mkenvimage$(SFX) BIN_FILES-y += mkimage$(SFX) BIN_FILES-$(CONFIG_EXYNOS5250) += mk$(BOARD)spl$(SFX) @@ -72,6 +73,7 @@ EXT_OBJ_FILES-y += lib/sha1.o # Source files located in the tools directory NOPED_OBJ_FILES-y += aisimage.o NOPED_OBJ_FILES-y += default_image.o +NOPED_OBJ_FILES-y += dumpimage.o NOPED_OBJ_FILES-y += fit_image.o NOPED_OBJ_FILES-y += image-host.o NOPED_OBJ_FILES-y += imximage.o @@ -200,6 +202,30 @@ $(obj)xway-swap-bytes$(SFX): $(obj)xway-swap-bytes.o $(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^ $(HOSTSTRIP) $@ +$(obj)dumpimage$(SFX): $(obj)aisimage.o \ + $(FIT_SIG_OBJS) \ + $(obj)crc32.o \ + $(obj)default_image.o \ + $(obj)fit_image.o \ + $(obj)image-fit.o \ + $(obj)image.o \ + $(obj)image-host.o \ + $(obj)imagetool.o \ + $(obj)imximage.o \ + $(obj)kwbimage.o \ + $(obj)dumpimage.o \ + $(obj)md5.o \ + $(obj)mxsimage.o \ + $(obj)omapimage.o \ + $(obj)os_support.o \ + $(obj)pblimage.o \ + $(obj)sha1.o \ + $(obj)ublimage.o \ + $(LIBFDT_OBJS) \ + $(RSA_OBJS) + $(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^ $(HOSTLIBS) + $(HOSTSTRIP) $@ + $(obj)mkenvimage$(SFX): $(obj)crc32.o $(obj)mkenvimage.o \ $(obj)os_support.o $(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^ diff --git a/tools/default_image.c b/tools/default_image.c index 9211c68..b0a0d40 100644 --- a/tools/default_image.c +++ b/tools/default_image.c @@ -117,6 +117,60 @@ static void image_set_header(void *ptr, struct stat *sbuf, int ifd, image_set_hcrc(hdr, checksum); } +static int image_save_datafile (struct image_tool_params *params, + ulong file_data, ulong file_len) +{ + int dfd; + const char *datafile = params->datafile; + + dfd = open (datafile, O_RDWR|O_CREAT|O_BINARY, S_IRUSR|S_IWUSR); + if (dfd < 0) { + fprintf (stderr, "%s: Can't open \"%s\": %s\n", + params->cmdname, datafile, strerror(errno)); + return -1; + } + + if (write (dfd, (void *)file_data, file_len) != (ssize_t)file_len) { + fprintf (stderr, "%s: Write error on \"%s\": %s\n", + params->cmdname, datafile, strerror(errno)); + close (dfd); + return -1; + } + + close (dfd); + + return 0; +} + +static int image_extract_datafile (void *ptr, struct image_tool_params *params) +{ + const image_header_t *hdr = (const image_header_t *)ptr; + + if (image_check_type (hdr, IH_TYPE_MULTI)) { + ulong idx = params->pflag; + ulong count; + ulong file_data = 0; + ulong file_len = 0; + + /* get the number of data files present in the image */ + count = image_multi_count (hdr); + + /* retrieve the "data file" at the idx position */ + image_multi_getimg (hdr, idx, &file_data, &file_len); + + if ((file_len == 0) || (idx >= count)) { + fprintf (stderr, "%s: No such data file %ld in \"%s\"\n", + params->cmdname, idx, params->imagefile); + return -1; + } + + /* save the "data file" into the file system */ + return image_save_datafile (params, file_data, file_len); + } + + return -1; +} + /* * Default image type parameters definition */ @@ -128,6 +182,7 @@ static struct image_type_params defimage_params = { .verify_header = image_verify_header, .print_header = image_print_contents, .set_header = image_set_header, + .extract_datafile = image_extract_datafile, .check_params = image_check_params, }; diff --git a/tools/dumpimage.c b/tools/dumpimage.c new file mode 100644 index 0000000..cb5cb44 --- /dev/null +++ b/tools/dumpimage.c @@ -0,0 +1,318 @@ +/* + * Based on mkimage.c. + * + * Written by Guilherme Maciel Ferreira + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include "dumpimage.h" +#include +#include + +static void usage(void); + +/* image_type_params linked list to maintain registered image types supports */ +static struct image_type_params *dumpimage_tparams = NULL; + +/* parameters initialized by core will be used by the image type code */ +static struct image_tool_params params = { + .type = IH_TYPE_KERNEL, +}; + +/* + * dumpimage_register - + * + * It is used to register respective image generation/list support to the + * dumpimage core + * + * the input struct image_type_params is checked and appended to the link + * list, if the input structure is already registered, issue an error + */ +static void +dumpimage_register (struct image_type_params *tparams) +{ + struct image_type_params **tp; + + if (!tparams) { + fprintf (stderr, "%s: %s: Null input\n", + params.cmdname, __FUNCTION__); + exit (EXIT_FAILURE); + } + + /* scan the linked list, check for registry and point the last one */ + for (tp = &dumpimage_tparams; *tp != NULL; tp = &(*tp)->next) { + if (!strcmp((*tp)->name, tparams->name)) { + fprintf (stderr, "%s: %s already registered\n", + params.cmdname, tparams->name); + return; + } + } + + /* add input struct entry at the end of link list */ + *tp = tparams; + /* mark input entry as last entry in the link list */ + tparams->next = NULL; + + debug ("Registered %s\n", tparams->name); +} + +/* + * dumpimage_get_type - + * + * It scans all registered image types and + * checks the input type_id for each supported image type + * + * if successful, + * returns respective image_type_params pointer if success + * if input type is not supported by any of registered image types, + * returns NULL + */ +static struct image_type_params * +dumpimage_get_type (int type) +{ + struct image_type_params *curr; + + for (curr = dumpimage_tparams; curr != NULL; curr = curr->next) { + if (curr->check_image_type) { + if (!curr->check_image_type (type)) + return curr; + } + } + return NULL; +} + +/* + * dumpimage_verify_print_header - + * + * It scans registered image types, + * verifies image_header for each supported image type + * if verification is successful, prints respective header + * + * returns negative if input image format does not match with any of + * supported image types + */ +static int +dumpimage_verify_print_header (void *ptr, struct stat *sbuf) +{ + int retval = -1; + struct image_type_params *curr; + + for (curr = dumpimage_tparams; curr != NULL; curr = curr->next ) { + if (curr->verify_header) { + retval = curr->verify_header ( + (unsigned char *)ptr, sbuf->st_size, + ¶ms); + + if (retval == 0) { + /* + * Print the image information + * if verify is successful + */ + if (curr->print_header) + curr->print_header (ptr); + else { + fprintf (stderr, + "%s: print_header undefined for %s\n", + params.cmdname, curr->name); + } + break; + } + } + } + return retval; +} + +/* + * dumpimage_extract_datafile - + * + * It scans all registered image types, + * verifies image_header for each supported image type + * if verification is successful, it extracts the desired file, + * indexed by pflag, from the image + * + * returns negative if input image format does not match with any of + * supported image types + */ +static int +dumpimage_extract_datafile (void *ptr, struct stat *sbuf) +{ + int retval = -1; + struct image_type_params *curr; + + for (curr = dumpimage_tparams; curr != NULL; curr = curr->next ) { + if (curr->verify_header) { + retval = curr->verify_header ( + (unsigned char *)ptr, sbuf->st_size, + ¶ms); + + if (retval == 0) { + /* + * Extract the file from the image + * if verify is successful + */ + if (curr->extract_datafile) + curr->extract_datafile (ptr, ¶ms); + else { + fprintf (stderr, + "%s: extract_datafile undefined for %s\n", + params.cmdname, curr->name); + } + break; + } + } + } + + return retval; +} + +int +main (int argc, char **argv) +{ + int ifd = -1; + struct stat sbuf; + char *ptr; + int retval = 0; + struct image_type_params *tparams = NULL; + + /* Init all image generation/list support */ + register_image_tool (dumpimage_register); + + params.cmdname = *argv; + + while (--argc > 0 && **++argv == '-') { + while (*++*argv) { + switch (**argv) { + case 'l': + params.lflag = 1; + break; + case 'i': + if (--argc <= 0) + usage (); + params.imagefile = *(++argv); + params.iflag = 1; + goto NXTARG; + case 'p': + if (--argc <= 0) + usage (); + params.pflag = strtoul (*++argv, &ptr, 10); + if (*ptr) { + fprintf (stderr, + "%s: invalid file position %s\n", + params.cmdname, *argv); + exit (EXIT_FAILURE); + } + goto NXTARG; + case 'V': + printf ("dumpimage version %s\n", PLAIN_VERSION); + exit (EXIT_SUCCESS); + default: + usage (); + } + } +NXTARG: ; + } + + if (argc != 1) + usage (); + + /* set tparams as per input type_id */ + tparams = dumpimage_get_type (params.type); + if (tparams == NULL) { + fprintf (stderr, "%s: unsupported type %s\n", + params.cmdname, genimg_get_type_name(params.type)); + exit (EXIT_FAILURE); + } + + /* + * check the passed arguments parameters meets the requirements + * as per image type to be generated/listed + */ + if (tparams->check_params) + if (tparams->check_params (¶ms)) + usage (); + + if (params.iflag) { + params.datafile = *argv; + } + else { + params.imagefile = *argv; + } + + ifd = open (params.imagefile, O_RDONLY|O_BINARY); + if (ifd < 0) { + fprintf (stderr, "%s: Can't open \"%s\": %s\n", + params.cmdname, params.imagefile, + strerror(errno)); + exit (EXIT_FAILURE); + } + + if (params.lflag || params.iflag) { + + if (fstat(ifd, &sbuf) < 0) { + fprintf (stderr, "%s: Can't stat \"%s\": %s\n", + params.cmdname, params.imagefile, + strerror(errno)); + exit (EXIT_FAILURE); + } + + if ((unsigned)sbuf.st_size < tparams->header_size) { + fprintf (stderr, + "%s: Bad size: \"%s\" is not valid image\n", + params.cmdname, params.imagefile); + exit (EXIT_FAILURE); + } + + ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); + if (ptr == MAP_FAILED) { + fprintf (stderr, "%s: Can't read \"%s\": %s\n", + params.cmdname, params.imagefile, + strerror(errno)); + exit (EXIT_FAILURE); + } + + /* + * Both calls bellow scan through dumpimage registry for all supported + * image types and verify the input image file header for match + */ + + if (params.iflag) { + /* + * Extract the data files from within the matched image type + * Returns the error code if not matched + */ + retval = dumpimage_extract_datafile (ptr, &sbuf); + } + else { + /* + * Print the image information for matched image type + * Returns the error code if not matched + */ + retval = dumpimage_verify_print_header (ptr, &sbuf); + } + + (void) munmap((void *)ptr, sbuf.st_size); + (void) close (ifd); + + exit (retval); + } + + (void) close (ifd); + exit (EXIT_SUCCESS); +} + +static void +usage (void) +{ + fprintf (stderr, "Usage: %s -l image\n" + " -l ==> list image header information\n", + params.cmdname); + fprintf (stderr, " %s -i image -p position data_file\n" + " -i ==> extract from the 'image' a specific 'data_file'" + ", indexed by 'position' (starting at 0)\n", + params.cmdname); + fprintf (stderr, " %s -V ==> print version information and exit\n", + params.cmdname); + + exit (EXIT_FAILURE); +} diff --git a/tools/dumpimage.h b/tools/dumpimage.h new file mode 100644 index 0000000..f749444 --- /dev/null +++ b/tools/dumpimage.h @@ -0,0 +1,33 @@ +/* + * Based on mkimage.c. + * + * Written by Guilherme Maciel Ferreira + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#ifndef _DUMPIMAGE_H_ +#define _DUMPIMAGE_H_ + +#include "os_support.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "fdt_host.h" +#include "imagetool.h" + +#undef DUMPIMAGE_DEBUG + +#ifdef DUMPIMAGE_DEBUG +#define debug(fmt,args...) printf (fmt ,##args) +#else +#define debug(fmt,args...) +#endif /* DUMPIMAGE_DEBUG */ + +#endif /* _DUMPIMAGE_H_ */ diff --git a/tools/imagetool.h b/tools/imagetool.h index 7666882..d0dd4ac 100644 --- a/tools/imagetool.h +++ b/tools/imagetool.h @@ -34,7 +34,9 @@ struct image_tool_params { int dflag; int eflag; int fflag; + int iflag; int lflag; + int pflag; int vflag; int xflag; int skipcpy; @@ -97,6 +99,15 @@ struct image_type_params { void (*set_header) (void *, struct stat *, int, struct image_tool_params *); /* + * This function is used by the command to retrieve a data file from + * the image (i.e. dumpimage -i -p ). Thus, + * the code to extract a file from an image must be put here. + * + * Returns 0 if the file was successfully retrieved from the image, + * or a negative value on error. + */ + int (*extract_datafile) (void *, struct image_tool_params *); + /* * Some image generation support for ex (default image type) supports * more than one type_ids, this callback function is used to check * whether input (-T ) is supported by registered image