Skip to contentSkip to conversation

Add dumpimage tool to extract files from U-Boot images

PATCH New Simon Glass 3 participants 12 messages Dec 1, 2013 – Dec 18, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover Add dumpimage tool to extract files from U-Boot images

This new dumpimage tool makes it easy to unpack images created by mkimage.

The tool was written by:

Guilherme Maciel Ferreira <[email protected]>

I have tidied it up a little and resubmitted it, since no response was received to my earlier email.

You can review this change here (complete with patman tags):

https://u-boot-review.googlesource.com/#/c/1538/

Changes in v2:
- Add additional -o option for when the file name is not valid
- Correct checkpatch problems
- Tidy up shell script style a little
- Updated test to include coverage for the new -o option

Guilherme Maciel Ferreira (4):
mkimage: added 'static' specifier to match function's prototype.
tools: moved code common to all image tools to a separated module. Add dumpimage, a tool to extract data from U-Boot images
sandbox: dumpimage: Test dumpimage

 Makefile                      |   1 +
 README                        |   9 ++
 test/image/test-imagetools.sh | 141 +++++++++++++++++++
 tools/.gitignore              |   1 +
 tools/Makefile                |  28 ++++
 tools/aisimage.c              |  16 +--
 tools/default_image.c         |  67 +++++++++-
 tools/dumpimage.c             | 305 ++++++++++++++++++++++++++++++++++++++++++
 tools/dumpimage.h             |  33 +++++
 tools/fit_image.c             |  11 +-
 tools/imagetool.c             |  58 ++++++++
 tools/imagetool.h             | 173 ++++++++++++++++++++++++
 tools/imximage.c              |  12 +-
 tools/kwbimage.c              |  10 +-
 tools/mkimage.c               |  25 +---
 tools/mkimage.h               | 123 +----------------
 tools/mxsimage.c              |  12 +-
 tools/omapimage.c             |  10 +-
 tools/pblimage.c              |  10 +-
 tools/ublimage.c              |  10 +-
 20 files changed, 862 insertions(+), 193 deletions(-)
 create mode 100755 test/image/test-imagetools.sh
 create mode 100644 tools/dumpimage.c
 create mode 100644 tools/dumpimage.h
 create mode 100644 tools/imagetool.c
 create mode 100644 tools/imagetool.h
Signature · 1 line
1.8.4.1
#1 lore raw

PATCH v2 1/4 mkimage: added 'static' specifier to match function's prototype.

From: Guilherme Maciel Ferreira <[email protected]>

This function should be declared static.

---

Changes in v2:
- Correct checkpatch problems

 tools/mkimage.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
tools/mkimage.c +1 -2
index 7f22101..e12bc31 100644
@@ -632,8 +632,7 @@ copy_file (int ifd, const char *datafile, int pad)
 	(void) close (dfd);
 }
 
-void
-usage ()
+static void usage(void)
 {
 	fprintf (stderr, "Usage: %s -l image\n"
 			 "          -l ==> list image header information\n",
Signature · 1 line
1.8.4.1
#2 lore raw

PATCH v2 1/4 mkimage: added 'static' specifier to match function's prototype.

Quoted 6 lines · Simon Glass

From: Guilherme Maciel Ferreira <[email protected]>

This function should be declared static.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20131216/458816f6/attachment.pgp>
#3 lore raw

PATCH v2 2/4 tools: moved code common to all image tools to a separated module.

In reply to Simon Glass

From: Guilherme Maciel Ferreira <[email protected]>

In order to avoid duplicating code and keep only one point of modification, the functions, structs and defines useful for "dumpimage" were moved from "mkimage" to a common module called "imagetool".

This modification also weakens the coupling between image types (FIT, IMX, MXS, and so on) and image tools (mkimage and dumpimage). Any tool may initialize the "imagetool" through register_image_tool() function, while the image types register themselves within an image tool using the register_image_type() function:

                                                      +---------------+
                                               +------|   fit_image   |
 +--------------+          +-----------+       |      +---------------+
 |    mkimage   |--------> |           | <-----+
 +--------------+          |           |              +---------------+
                           | imagetool | <------------|    imximage   |
 +--------------+          |           |              +---------------+
 |  dumpimage   |--------> |           | <-----+
 +--------------+          +-----------+       |      +---------------+
                                               +------| default_image |
                                                      +---------------+

register_image_tool() register_image_type()

Also, the struct "mkimage_params" was renamed to "image_tool_params" to make clear its general purpose.

---

Changes in v2:
- Correct checkpatch problems

 tools/Makefile        |   2 +
 tools/aisimage.c      |  16 ++---
 tools/default_image.c |  10 ++--
 tools/fit_image.c     |  11 ++--
 tools/imagetool.c     |  58 ++++++++++++++++++
 tools/imagetool.h     | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/imximage.c      |  12 ++--
 tools/kwbimage.c      |  10 ++--
 tools/mkimage.c       |  22 +------
 tools/mkimage.h       | 123 +-------------------------------------
 tools/mxsimage.c      |  12 ++--
 tools/omapimage.c     |  10 ++--
 tools/pblimage.c      |  10 ++--
 tools/ublimage.c      |  10 ++--
 14 files changed, 276 insertions(+), 191 deletions(-)
 create mode 100644 tools/imagetool.c
 create mode 100644 tools/imagetool.h
tools/Makefile +2 -0
index 14d94e3..154eae3 100644
@@ -76,6 +76,7 @@ NOPED_OBJ_FILES-y += fit_image.o
 NOPED_OBJ_FILES-y += image-host.o
 NOPED_OBJ_FILES-y += imximage.o
 NOPED_OBJ_FILES-y += kwbimage.o
+NOPED_OBJ_FILES-y += imagetool.o
 NOPED_OBJ_FILES-y += mkenvimage.o
 NOPED_OBJ_FILES-y += mkimage.o
 NOPED_OBJ_FILES-y += mxsimage.o
@@ -212,6 +213,7 @@ $(obj)mkimage$(SFX):	$(obj)aisimage.o \
 			$(obj)image-fit.o \
 			$(obj)image-host.o \
 			$(obj)image.o \
+			$(obj)imagetool.o \
 			$(obj)imximage.o \
 			$(obj)kwbimage.o \
 			$(obj)md5.o \
tools/aisimage.c +8 -8
index 04fb649..8de370a 100644
@@ -5,7 +5,7 @@
  * SPDX-License-Identifier:	GPL-2.0+
  */
 
-#include "mkimage.h"
+#include "imagetool.h"
 #include "aisimage.h"
 #include <image.h>
 
@@ -176,7 +176,7 @@ static uint32_t *ais_insert_cmd_header(uint32_t cmd, uint32_t nargs,
 
 }
 
-static uint32_t *ais_alloc_buffer(struct mkimage_params *params)
+static uint32_t *ais_alloc_buffer(struct image_tool_params *params)
 {
 	int dfd;
 	struct stat sbuf;
@@ -216,7 +216,7 @@ static uint32_t *ais_alloc_buffer(struct mkimage_params *params)
 	return ptr;
 }
 
-static uint32_t *ais_copy_image(struct mkimage_params *params,
+static uint32_t *ais_copy_image(struct image_tool_params *params,
 	uint32_t *aisptr)
 
 {
@@ -252,7 +252,7 @@ static uint32_t *ais_copy_image(struct mkimage_params *params,
 
 }
 
-static int aisimage_generate(struct mkimage_params *params,
+static int aisimage_generate(struct image_tool_params *params,
 	struct image_type_params *tparams)
 {
 	FILE *fd = NULL;
@@ -370,7 +370,7 @@ static int aisimage_check_image_types(uint8_t type)
 }
 
 static int aisimage_verify_header(unsigned char *ptr, int image_size,
-			struct mkimage_params *params)
+			struct image_tool_params *params)
 {
 	struct ais_header *ais_hdr = (struct ais_header *)ptr;
 
@@ -384,11 +384,11 @@ static int aisimage_verify_header(unsigned char *ptr, int image_size,
 }
 
 static void aisimage_set_header(void *ptr, struct stat *sbuf, int ifd,
-				struct mkimage_params *params)
+				struct image_tool_params *params)
 {
 }
 
-int aisimage_check_params(struct mkimage_params *params)
+int aisimage_check_params(struct image_tool_params *params)
 {
 	if (!params)
 		return CFG_INVALID;
@@ -427,5 +427,5 @@ static struct image_type_params aisimage_params = {
 
 void init_ais_image_type(void)
 {
-	mkimage_register(&aisimage_params);
+	register_image_type(&aisimage_params);
 }
tools/default_image.c +5 -5
index fd8b9f5..cc790b2 100644
@@ -14,7 +14,7 @@
  * SPDX-License-Identifier:	GPL-2.0+
  */
 
-#include "mkimage.h"
+#include "imagetool.h"
 #include <image.h>
 #include <u-boot/crc.h>
 
@@ -29,7 +29,7 @@ static int image_check_image_types(uint8_t type)
 		return EXIT_FAILURE;
 }
 
-static int image_check_params(struct mkimage_params *params)
+static int image_check_params(struct image_tool_params *params)
 {
 	return	((params->dflag && (params->fflag || params->lflag)) ||
 		(params->fflag && (params->dflag || params->lflag)) ||
@@ -37,7 +37,7 @@ static int image_check_params(struct mkimage_params *params)
 }
 
 static int image_verify_header(unsigned char *ptr, int image_size,
-			struct mkimage_params *params)
+			struct image_tool_params *params)
 {
 	uint32_t len;
 	const unsigned char *data;
@@ -86,7 +86,7 @@ static int image_verify_header(unsigned char *ptr, int image_size,
 }
 
 static void image_set_header(void *ptr, struct stat *sbuf, int ifd,
-				struct mkimage_params *params)
+				struct image_tool_params *params)
 {
 	uint32_t checksum;
 
@@ -133,5 +133,5 @@ static struct image_type_params defimage_params = {
 
 void init_default_image_type(void)
 {
-	mkimage_register(&defimage_params);
+	register_image_type(&defimage_params);
 }
tools/fit_image.c +6 -5
index 0400a60..1466164 100644
@@ -14,6 +14,7 @@
  * SPDX-License-Identifier:	GPL-2.0+
  */
 
+#include "imagetool.h"
 #include "mkimage.h"
 #include <image.h>
 #include <u-boot/crc.h>
@@ -21,7 +22,7 @@
 static image_header_t header;
 
 static int fit_verify_header (unsigned char *ptr, int image_size,
-			struct mkimage_params *params)
+			struct image_tool_params *params)
 {
 	return fdt_check_header(ptr);
 }
@@ -34,7 +35,7 @@ static int fit_check_image_types (uint8_t type)
 		return EXIT_FAILURE;
 }
 
-int mmap_fdt(struct mkimage_params *params, const char *fname, void **blobp,
+int mmap_fdt(struct image_tool_params *params, const char *fname, void **blobp,
 		struct stat *sbuf)
 {
 	void *ptr;
@@ -88,7 +89,7 @@ int mmap_fdt(struct mkimage_params *params, const char *fname, void **blobp,
  * returns:
  *     only on success, otherwise calls exit (EXIT_FAILURE);
  */
-static int fit_handle_file (struct mkimage_params *params)
+static int fit_handle_file(struct image_tool_params *params)
 {
 	char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
 	char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
@@ -184,7 +185,7 @@ err_system:
 	return -1;
 }
 
-static int fit_check_params (struct mkimage_params *params)
+static int fit_check_params(struct image_tool_params *params)
 {
 	return	((params->dflag && (params->fflag || params->lflag)) ||
 		(params->fflag && (params->dflag || params->lflag)) ||
@@ -205,5 +206,5 @@ static struct image_type_params fitimage_params = {
 
 void init_fit_image_type (void)
 {
-	mkimage_register (&fitimage_params);
+	register_image_type(&fitimage_params);
 }
tools/imagetool.c new file +58 -0

58 lines not on this page · raw message

tools/imagetool.h new file +161 -0

161 lines not on this page · raw message

tools/imximage.c +6 -6
index 511e3f2..18dc051 100644
@@ -9,7 +9,7 @@
  * SPDX-License-Identifier:	GPL-2.0+
  */
 
-#include "mkimage.h"
+#include "imagetool.h"
 #include <image.h>
 #include "imximage.h"
 
@@ -520,7 +520,7 @@ static int imximage_check_image_types(uint8_t type)
 }
 
 static int imximage_verify_header(unsigned char *ptr, int image_size,
-			struct mkimage_params *params)
+			struct image_tool_params *params)
 {
 	struct imx_header *imx_hdr = (struct imx_header *) ptr;
 
@@ -549,7 +549,7 @@ static void imximage_print_header(const void *ptr)
 }
 
 static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
-				struct mkimage_params *params)
+				struct image_tool_params *params)
 {
 	struct imx_header *imxhdr = (struct imx_header *)ptr;
 	uint32_t dcd_len;
@@ -589,7 +589,7 @@ static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
 	}
 }
 
-int imximage_check_params(struct mkimage_params *params)
+int imximage_check_params(struct image_tool_params *params)
 {
 	if (!params)
 		return CFG_INVALID;
@@ -611,7 +611,7 @@ int imximage_check_params(struct mkimage_params *params)
 		(params->xflag) || !(strlen(params->imagename));
 }
 
-static int imximage_generate(struct mkimage_params *params,
+static int imximage_generate(struct image_tool_params *params,
 	struct image_type_params *tparams)
 {
 	struct imx_header *imxhdr;
@@ -701,5 +701,5 @@ static struct image_type_params imximage_params = {
 
 void init_imx_image_type(void)
 {
-	mkimage_register(&imximage_params);
+	register_image_type(&imximage_params);
 }
tools/kwbimage.c +5 -5
index 1df6b20..109d616 100644
@@ -6,7 +6,7 @@
  * SPDX-License-Identifier:	GPL-2.0+
  */
 
-#include "mkimage.h"
+#include "imagetool.h"
 #include <image.h>
 #include "kwbimage.h"
 
@@ -54,7 +54,7 @@ static int lineno = -1;
 /*
  * Report Error if xflag is set in addition to default
  */
-static int kwbimage_check_params (struct mkimage_params *params)
+static int kwbimage_check_params(struct image_tool_params *params)
 {
 	if (!strlen (params->imagename)) {
 		printf ("Error:%s - Configuration file not specified, "
@@ -288,7 +288,7 @@ INVL_CMD:
 }
 
 static void kwbimage_set_header (void *ptr, struct stat *sbuf, int ifd,
-				struct mkimage_params *params)
+				struct image_tool_params *params)
 {
 	struct kwb_header *hdr = (struct kwb_header *)ptr;
 	bhr_t *mhdr = &hdr->kwb_hdr;
@@ -322,7 +322,7 @@ static void kwbimage_set_header (void *ptr, struct stat *sbuf, int ifd,
 }
 
 static int kwbimage_verify_header (unsigned char *ptr, int image_size,
-			struct mkimage_params *params)
+			struct image_tool_params *params)
 {
 	struct kwb_header *hdr = (struct kwb_header *)ptr;
 	bhr_t *mhdr = &hdr->kwb_hdr;
@@ -382,5 +382,5 @@ static struct image_type_params kwbimage_params = {
 
 void init_kwb_image_type (void)
 {
-	mkimage_register (&kwbimage_params);
+	register_image_type(&kwbimage_params);
 }
tools/mkimage.c +3 -19
index e12bc31..123d0c7 100644
@@ -19,7 +19,7 @@ static void usage(void);
 struct image_type_params *mkimage_tparams = NULL;
 
 /* parameters initialized by core will be used by the image type code */
-struct mkimage_params params = {
+struct image_tool_params params = {
 	.os = IH_OS_LINUX,
 	.arch = IH_ARCH_PPC,
 	.type = IH_TYPE_KERNEL,
@@ -139,24 +139,8 @@ main (int argc, char **argv)
 	struct image_type_params *tparams = NULL;
 	int pad_len = 0;
 
-	/* Init Freescale PBL Boot image generation/list support */
-	init_pbl_image_type();
-	/* Init Kirkwood Boot image generation/list support */
-	init_kwb_image_type ();
-	/* Init Freescale imx Boot image generation/list support */
-	init_imx_image_type ();
-	/* Init Freescale mxs Boot image generation/list support */
-	init_mxs_image_type();
-	/* Init FIT image generation/list support */
-	init_fit_image_type ();
-	/* Init TI OMAP Boot image generation/list support */
-	init_omap_image_type();
-	/* Init Default image generation/list support */
-	init_default_image_type ();
-	/* Init Davinci UBL support */
-	init_ubl_image_type();
-	/* Init Davinci AIS support */
-	init_ais_image_type();
+	/* Init all image generation/list support */
+	register_image_tool(mkimage_register);
 
 	params.cmdname = *argv;
 	params.addr = params.ep = 0;
tools/mkimage.h +1 -122

139 lines not on this page · raw message

tools/mxsimage.c +6 -6
index 5db19b2..b214050 100644
@@ -17,7 +17,7 @@
 
 #include <openssl/evp.h>
 
-#include "mkimage.h"
+#include "imagetool.h"
 #include "mxsimage.h"
 #include <image.h>
 
@@ -2148,11 +2148,11 @@ static int mxsimage_check_image_types(uint8_t type)
 }
 
 static void mxsimage_set_header(void *ptr, struct stat *sbuf, int ifd,
-				struct mkimage_params *params)
+				struct image_tool_params *params)
 {
 }
 
-int mxsimage_check_params(struct mkimage_params *params)
+int mxsimage_check_params(struct image_tool_params *params)
 {
 	if (!params)
 		return -1;
@@ -2193,7 +2193,7 @@ static int mxsimage_verify_print_header(char *file, int silent)
 
 char *imagefile;
 static int mxsimage_verify_header(unsigned char *ptr, int image_size,
-			struct mkimage_params *params)
+			struct image_tool_params *params)
 {
 	struct sb_boot_image_header *hdr;
 
@@ -2291,7 +2291,7 @@ static int sb_build_image(struct sb_image_ctx *ictx,
 	return 0;
 }
 
-static int mxsimage_generate(struct mkimage_params *params,
+static int mxsimage_generate(struct image_tool_params *params,
 	struct image_type_params *tparams)
 {
 	int ret;
@@ -2337,7 +2337,7 @@ static struct image_type_params mxsimage_params = {
 
 void init_mxs_image_type(void)
 {
-	mkimage_register(&mxsimage_params);
+	register_image_type(&mxsimage_params);
 }
 
 #else
tools/omapimage.c +5 -5
index 8774a7e..d59bc4d 100644
@@ -14,7 +14,7 @@
  * SPDX-License-Identifier:	GPL-2.0+
  */
 
-#include "mkimage.h"
+#include "imagetool.h"
 #include <image.h>
 #include "omapimage.h"
 
@@ -69,7 +69,7 @@ static int valid_gph_load_addr(uint32_t load_addr)
 }
 
 static int omapimage_verify_header(unsigned char *ptr, int image_size,
-			struct mkimage_params *params)
+			struct image_tool_params *params)
 {
 	struct ch_toc *toc = (struct ch_toc *)ptr;
 	struct gp_header *gph = (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
@@ -188,7 +188,7 @@ static int toc_offset(void *hdr, void *member)
 }
 
 static void omapimage_set_header(void *ptr, struct stat *sbuf, int ifd,
-				struct mkimage_params *params)
+				struct image_tool_params *params)
 {
 	struct ch_toc *toc = (struct ch_toc *)ptr;
 	struct ch_settings *chs = (struct ch_settings *)
@@ -224,7 +224,7 @@ static void omapimage_set_header(void *ptr, struct stat *sbuf, int ifd,
 	}
 }
 
-int omapimage_check_params(struct mkimage_params *params)
+int omapimage_check_params(struct image_tool_params *params)
 {
 	return	(params->dflag && (params->fflag || params->lflag)) ||
 		(params->fflag && (params->dflag || params->lflag)) ||
@@ -247,5 +247,5 @@ static struct image_type_params omapimage_params = {
 
 void init_omap_image_type(void)
 {
-	mkimage_register(&omapimage_params);
+	register_image_type(&omapimage_params);
 }
tools/pblimage.c +5 -5
index bac5faf..ef3d7f6 100644
@@ -3,7 +3,7 @@
  *
  * SPDX-License-Identifier:	GPL-2.0+
  */
-#include "mkimage.h"
+#include "imagetool.h"
 #include <image.h>
 #include "pblimage.h"
 
@@ -242,7 +242,7 @@ static void add_end_cmd(void)
 	}
 }
 
-void pbl_load_uboot(int ifd, struct mkimage_params *params)
+void pbl_load_uboot(int ifd, struct image_tool_params *params)
 {
 	FILE *fp_uboot;
 	int size;
@@ -281,7 +281,7 @@ static int pblimage_check_image_types(uint8_t type)
 }
 
 static int pblimage_verify_header(unsigned char *ptr, int image_size,
-			struct mkimage_params *params)
+			struct image_tool_params *params)
 {
 	struct pbl_header *pbl_hdr = (struct pbl_header *) ptr;
 
@@ -308,7 +308,7 @@ static void pblimage_print_header(const void *ptr)
 }
 
 static void pblimage_set_header(void *ptr, struct stat *sbuf, int ifd,
-				struct mkimage_params *params)
+				struct image_tool_params *params)
 {
 	/*nothing need to do, pbl_load_uboot takes care of whole file. */
 }
@@ -327,5 +327,5 @@ static struct image_type_params pblimage_params = {
 void init_pbl_image_type(void)
 {
 	pbl_size = 0;
-	mkimage_register(&pblimage_params);
+	register_image_type(&pblimage_params);
 }
tools/ublimage.c +5 -5
index aafe248..cbbbe20 100644
@@ -13,7 +13,7 @@
  * SPDX-License-Identifier:	GPL-2.0+
  */
 
-#include "mkimage.h"
+#include "imagetool.h"
 #include <image.h>
 #include "ublimage.h"
 
@@ -193,7 +193,7 @@ static int ublimage_check_image_types(uint8_t type)
 }
 
 static int ublimage_verify_header(unsigned char *ptr, int image_size,
-			struct mkimage_params *params)
+			struct image_tool_params *params)
 {
 	struct ubl_header *ubl_hdr = (struct ubl_header *)ptr;
 
@@ -211,7 +211,7 @@ static void ublimage_print_header(const void *ptr)
 }
 
 static void ublimage_set_header(void *ptr, struct stat *sbuf, int ifd,
-				struct mkimage_params *params)
+				struct image_tool_params *params)
 {
 	struct ubl_header *ublhdr = (struct ubl_header *)ptr;
 
@@ -219,7 +219,7 @@ static void ublimage_set_header(void *ptr, struct stat *sbuf, int ifd,
 	parse_cfg_file(ublhdr, params->imagename);
 }
 
-int ublimage_check_params(struct mkimage_params *params)
+int ublimage_check_params(struct image_tool_params *params)
 {
 	if (!params)
 		return CFG_INVALID;
@@ -257,5 +257,5 @@ static struct image_type_params ublimage_params = {
 
 void init_ubl_image_type(void)
 {
-	mkimage_register(&ublimage_params);
+	register_image_type(&ublimage_params);
 }
Signature · 1 line
1.8.4.1
#4 lore raw

PATCH v2 2/4 tools: moved code common to all image tools to a separated module.

Quoted 24 lines · Simon Glass

From: Guilherme Maciel Ferreira <[email protected]>

In order to avoid duplicating code and keep only one point of modification, the functions, structs and defines useful for "dumpimage" were moved from "mkimage" to a common module called "imagetool".

This modification also weakens the coupling between image types (FIT, IMX, MXS, and so on) and image tools (mkimage and dumpimage). Any tool may initialize the "imagetool" through register_image_tool() function, while the image types register themselves within an image tool using the register_image_type() function:

                                                      +---------------+
                                               +------|   fit_image   |
 +--------------+          +-----------+       |      +---------------+
 |    mkimage   |--------> |           | <-----+
 +--------------+          |           |              +---------------+
                           | imagetool | <------------|    imximage   |
 +--------------+          |           |              +---------------+
 |  dumpimage   |--------> |           | <-----+
 +--------------+          +-----------+       |      +---------------+
                                               +------| default_image |
                                                      +---------------+
Quoted 7 lines

register_image_tool() register_image_type()

Also, the struct "mkimage_params" was renamed to "image_tool_params" to make clear its general purpose.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20131216/f66a8548/attachment.pgp>
#5 lore raw

PATCH v2 3/4 Add dumpimage, a tool to extract data from U-Boot images

In reply to Simon Glass

From: Guilherme Maciel Ferreira <[email protected]>

Given a multi-file 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 innverse 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.

---

Changes in v2:
- Add additional -o option for when the file name is not valid
- Correct checkpatch problems

 Makefile              |   1 +
 README                |   9 ++
 tools/.gitignore      |   1 +
 tools/Makefile        |  26 +++++
 tools/default_image.c |  57 ++++++++++
 tools/dumpimage.c     | 305 ++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/dumpimage.h     |  33 ++++++
 tools/imagetool.h     |  12 ++
 8 files changed, 444 insertions(+)
 create mode 100644 tools/dumpimage.c
 create mode 100644 tools/dumpimage.h
Makefile +1 -0
index a2fb037..653d3b3 100644
@@ -796,6 +796,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					  \
README +9 -0
index 5def773..5072f08 100644
@@ -5193,6 +5193,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:
 -------------------------
tools/.gitignore +1 -0
index a7fee26..2320fd8 100644
@@ -3,6 +3,7 @@
 /gen_eth_addr
 /img2srec
 /kwboot
+/dumpimage
 /mkenvimage
 /mkimage
 /mpc86x_clk
tools/Makefile +26 -0
index 154eae3..e1264fd 100644
@@ -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 $@ $^
tools/default_image.c +57 -0

69 lines not on this page · raw message

tools/dumpimage.c new file +305 -0

305 lines not on this page · raw message

tools/dumpimage.h new file +33 -0
new file mode 100644
index 0000000..d78523d
@@ -0,0 +1,33 @@
+/*
+ * Based on mkimage.c.
+ *
+ * Written by Guilherme Maciel Ferreira <[email protected]>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#ifndef _DUMPIMAGE_H_
+#define _DUMPIMAGE_H_
+
+#include "os_support.h"
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
+#include <sha1.h>
+#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_ */
tools/imagetool.h +12 -0
index b8adb61..c2c9aea 100644
@@ -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;
@@ -50,6 +52,7 @@ struct image_tool_params {
 	char *datafile;
 	char *imagefile;
 	char *cmdname;
+	const char *outfile;	/* Output filename */
 	const char *keydir;	/* Directory holding private keys */
 	const char *keydest;	/* Destination .dtb for public key */
 	const char *comment;	/* Comment to add to signature node */
@@ -97,6 +100,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 <image> -p <position> <data_file>).
+	 * 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 <image_type>) is supported by registered image
Signature · 1 line
1.8.4.1
#6 lore raw

PATCH v2 3/4 Add dumpimage, a tool to extract data from U-Boot images

Quoted 23 lines · Simon Glass

From: Guilherme Maciel Ferreira <[email protected]>

Given a multi-file 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 innverse operation -- extracting any file from the image -- by using the dumpimage's -i option:

  $ dumpimage -i multi.img -p 2 System.map
Quoted 8 lines

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.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20131216/751575e6/attachment.pgp>
#7 lore raw

PATCH v2 3/4 Add dumpimage, a tool to extract data from U-Boot images

In reply to Simon Glass

Hello Guilherme, Simon

Makefile clean: · line 802

Quoted 12 lines
Makefile +1 -0
index a2fb037..653d3b3 100644
@@ -796,6 +796,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					  \

Where is tools/dumpenvimage created?

Best Regards
Masahiro Yamada

#8 lore raw

PATCH v2 3/4 Add dumpimage, a tool to extract data from U-Boot images

Hi Masahiro,

Quoted 17 lines · Masahiro Yamada

Hello Guilherme, Simon

Makefile clean: · line 802

Quoted 12 lines
Makefile +1 -0
index a2fb037..653d3b3 100644
@@ -796,6 +796,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                                        \

Where is tools/dumpenvimage created?

I don't see it.

Regards,
Simon

Quoted 5 lines

Best Regards
Masahiro Yamada

#9 lore raw

PATCH v2 3/4 Add dumpimage, a tool to extract data from U-Boot images

Hello Simon.

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
Makefile +0 -0
index a2fb037..653d3b3 100644
@@ -796,6 +796,7 @@ clean:
             $(obj)tools/envcrc                                         \
Quoted 8 lines

Makefile clean: · line 802

Quoted 6 lines
Makefile +1 -0
             $(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                                        \

Where is tools/dumpenvimage created?

Quoted 1 line

I don't see it.

OK.
Anyway, I will fix it in my Kbuild series.

Best Regards
Masahiro Yamada

#10 lore raw

PATCH v2 4/4 sandbox: dumpimage: Test dumpimage

In reply to Simon Glass

From: Guilherme Maciel Ferreira <[email protected]>

Add a test for dumpimage.

---

Changes in v2:
- Tidy up shell script style a little
- Updated test to include coverage for the new -o option

 test/image/test-imagetools.sh | 141 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 141 insertions(+)
 create mode 100755 test/image/test-imagetools.sh
test/image/test-imagetools.sh new file +141 -0
new file mode 100755
index 0000000..9e299e1
@@ -0,0 +1,141 @@
+#!/bin/bash
+#
+# Written by Guilherme Maciel Ferreira <[email protected]>
+#
+# Sanity check for mkimage and dumpimage tools
+#
+# SPDX-License-Identifier:	GPL-2.0+
+#
+# To run this:
+#
+# make O=sandbox sandbox_config
+# make O=sandbox
+# ./test/image/test-imagetools.sh
+
+BASEDIR=sandbox
+SRCDIR=sandbox/boot
+IMAGE_NAME="v1.0-test"
+IMAGE=linux.img
+DATAFILE0=vmlinuz
+DATAFILE1=initrd.img
+DATAFILE2=System.map
+DATAFILES="${DATAFILE0} ${DATAFILE1} ${DATAFILE2}"
+TEST_OUT=test_output
+MKIMAGE=${BASEDIR}/tools/mkimage
+DUMPIMAGE=${BASEDIR}/tools/dumpimage
+MKIMAGE_LIST=mkimage.list
+DUMPIMAGE_LIST=dumpimage.list
+
+# Remove all the files we created
+cleanup()
+{
+	local file
+
+	for file in ${DATAFILES}; do
+		rm -f ${file} ${SRCDIR}/${file}
+	done
+	rm -f ${IMAGE} ${DUMPIMAGE_LIST} ${MKIMAGE_LIST} ${TEST_OUT}
+	rmdir ${SRCDIR}
+}
+
+# Check that two files are the same
+assert_equal()
+{
+	if ! diff $1 $2; then
+		echo "Failed."
+		cleanup
+		exit 1
+	fi
+}
+
+# Create some test files
+create_files()
+{
+	local file
+
+	mkdir -p ${SRCDIR}
+	for file in ${DATAFILES}; do
+		head -c $RANDOM /dev/urandom >${SRCDIR}/${file}
+	done
+}
+
+# Run a command, echoing it first
+do_cmd()
+{
+	local cmd="$@"
+
+	echo "# ${cmd}"
+	${cmd} 2>&1
+}
+
+# Run a command, redirecting output
+# Args:
+#    redirect_file
+#    command...
+do_cmd_redir()
+{
+	local redir="$1"
+	shift
+	local cmd="$@"
+
+	echo "# ${cmd}"
+	${cmd} >${redir}
+}
+
+# Write files into an image
+create_image()
+{
+	local files="${SRCDIR}/${DATAFILE0}:${SRCDIR}/${DATAFILE1}"
+	files+=":${SRCDIR}/${DATAFILE2}"
+
+	echo -e "\nBuilding image..."
+	do_cmd ${MKIMAGE} -A x86 -O linux -T multi -n \"${IMAGE_NAME}\" \
+		-d ${files} ${IMAGE}
+	echo "done."
+}
+
+# Extract files from an image
+extract_image()
+{
+	echo -e "\nExtracting image contents..."
+	do_cmd ${DUMPIMAGE} -i ${IMAGE} -p 0 ${DATAFILE0}
+	do_cmd ${DUMPIMAGE} -i ${IMAGE} -p 1 ${DATAFILE1}
+	do_cmd ${DUMPIMAGE} -i ${IMAGE} -p 2 ${DATAFILE2}
+	do_cmd ${DUMPIMAGE} -i ${IMAGE} -p 2 ${DATAFILE2} -o ${TEST_OUT}
+	echo "done."
+}
+
+# List the contents of a file
+list_image()
+{
+	echo -e "\nListing image contents..."
+	do_cmd_redir ${MKIMAGE_LIST} ${MKIMAGE} -l ${IMAGE}
+	do_cmd_redir ${DUMPIMAGE_LIST} ${DUMPIMAGE} -l ${IMAGE}
+	echo "done."
+}
+
+main()
+{
+	local file
+
+	create_files
+
+	# Compress and extract multifile images, compare the result
+	create_image
+	extract_image
+	for file in ${DATAFILES}; do
+		assert_equal ${file} ${SRCDIR}/${file}
+	done
+	assert_equal ${TEST_OUT} ${DATAFILE2}
+
+	# List contents and compares output fro tools
+	list_image
+	assert_equal ${DUMPIMAGE_LIST} ${MKIMAGE_LIST}
+
+	# Remove files created
+	cleanup
+
+	echo "Tests passed."
+}
+
+main
Signature · 1 line
1.8.4.1
#11 lore raw

PATCH v2 4/4 sandbox: dumpimage: Test dumpimage

Quoted 6 lines · Simon Glass

From: Guilherme Maciel Ferreira <[email protected]>

Add a test for dumpimage.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20131216/36872e84/attachment.pgp>