[U-Boot] [PATCH 4/8] tools: mkimage: Implemented callback function for default image support

Prafulla Wadaskar prafulla at marvell.com
Tue Jul 28 20:04:26 CEST 2009


This is Third step towards cleaning mkimage code for kwbimage
	support in clean way. In this patch-
1. callback functions are used in mkimage.c those are defined
	in defaut_image.c for currently supported images.
2. scan loop implemented for image header verify and print

Signed-off-by: Prafulla Wadaskar <prafulla at marvell.com>
---
 include/image.h       |    1 +
 tools/default_image.c |   38 +++++++++++++++++++++++++++++++---
 tools/default_image.h |    3 +-
 tools/mkimage.c       |   53 ++++++++++++++++++++++++++++++++++++++++--------
 tools/mkimage.h       |   15 +++++++++++++
 5 files changed, 96 insertions(+), 14 deletions(-)

diff --git a/include/image.h b/include/image.h
index beb3a16..b519e5e 100644
--- a/include/image.h
+++ b/include/image.h
@@ -155,6 +155,7 @@
 #define IH_TYPE_SCRIPT		6	/* Script file			*/
 #define IH_TYPE_FILESYSTEM	7	/* Filesystem Image (any type)	*/
 #define IH_TYPE_FLATDT		8	/* Binary Flat Device Tree Blob	*/
+#define IH_TYPE_MAX		IH_TYPE_FLATDT	/* Max supported types  */
 
 /*
  * Compression Types
diff --git a/tools/default_image.c b/tools/default_image.c
index e1a9bc7..78e29f2 100644
--- a/tools/default_image.c
+++ b/tools/default_image.c
@@ -37,6 +37,10 @@ image_header_t header;
 struct image_type_params defimage_params = {
 	.header_size = sizeof(image_header_t),
 	.hdr = (void*)&header,
+	.check_params = image_check_params,
+	.verify_header = image_verify_header,
+	.print_header = image_print_header,
+	.set_header = image_set_header,
 };
 
 void *image_get_tparams (void){
@@ -101,12 +105,10 @@ image_verify_header (char *ptr, int image_size,
 
 void image_print_header (char *ptr)
 {
-	image_header_t * hdr = (image_header_t *)ptr;
-
-	image_print_contents (hdr);
+	image_print_contents ((image_header_t *)ptr);
 }
 
-void image_set_header (char *ptr, struct stat *sbuf,
+void image_set_header (char *ptr, struct stat *sbuf, int ifd,
 				struct mkimage_params *params)
 {
 	uint32_t checksum;
@@ -136,6 +138,34 @@ void image_set_header (char *ptr, struct stat *sbuf,
 	image_set_hcrc (hdr, checksum);
 }
 
+/*
+ * FIT image support
+ */
+int
+fitimage_verify_header (char *ptr, int image_size,
+			struct mkimage_params *params)
+{
+	fdt_check_header ((void *)ptr);
+}
+
+void fitimage_print_header (char *ptr)
+{
+	fit_print_contents ((void *)ptr);
+}
+
+struct image_type_params fitimage_params = {
+	.header_size = sizeof(image_header_t),
+	.hdr = (void*)&header,
+	.check_params = image_check_params,
+	.verify_header = fitimage_verify_header,
+	.print_header = fitimage_print_header,
+	.set_header = image_set_header,
+};
+
+void *fitimage_get_tparams (void){
+	return (void *)&fitimage_params;
+}
+
 /**
  * fit_handle_file - main FIT file processing function
  *
diff --git a/tools/default_image.h b/tools/default_image.h
index 2311ede..ba95521 100644
--- a/tools/default_image.h
+++ b/tools/default_image.h
@@ -25,12 +25,13 @@
 #ifndef _DEFAULT_IMAGE_H_
 #define _DEFAULT_IMAGE_H_
 
+void *fitimage_get_tparams (void);
 void *image_get_tparams (void);
 int image_check_params (struct mkimage_params *params);
 int image_verify_header (char *ptr, int image_size,
 			struct mkimage_params *params);
 void image_print_header (char *ptr);
-void image_set_header (char *ptr, struct stat *sbuf,
+void image_set_header (char *ptr, struct stat *sbuf, int ifd,
 			struct mkimage_params *params);
 void fit_handle_file (struct mkimage_params *params);
 
diff --git a/tools/mkimage.c b/tools/mkimage.c
index d1636d8..3a3cb19 100644
--- a/tools/mkimage.c
+++ b/tools/mkimage.c
@@ -49,14 +49,28 @@ struct mkimage_params params = {
 	.cmdname = "",
 };
 
+static struct image_functions image_ftable[] = {
+	{image_get_tparams,},	/* for IH_TYPE_INVALID */
+	{image_get_tparams,},	/* for IH_TYPE_FILESYSTEM */
+	{image_get_tparams,},	/* for IH_TYPE_FIRMWARE */
+	{image_get_tparams,},	/* for IH_TYPE_KERNEL */
+	{image_get_tparams,},	/* for IH_TYPE_MULTI */
+	{image_get_tparams,},	/* for IH_TYPE_RAMDISK */
+	{image_get_tparams,},	/* for IH_TYPE_SCRIPT */
+	{image_get_tparams,},	/* for IH_TYPE_STANDALONE */
+	{fitimage_get_tparams,},	/* for IH_TYPE_FLATDT */
+};
+
 int
 main (int argc, char **argv)
 {
 	int ifd = -1;
 	struct stat sbuf;
 	unsigned char *ptr;
-	int retval;
+	int i, retval;
 	struct image_type_params *tparams = image_get_tparams ();
+	struct image_type_params *scantparams;
+	int image_ftable_size = ARRAY_SIZE(image_ftable);
 
 	params.cmdname = *argv;
 
@@ -97,6 +111,15 @@ main (int argc, char **argv)
 					(params.opt_type =
 					genimg_get_type_id (*++argv)) < 0)
 					usage ();
+				if (image_ftable_size <= params.opt_type) {
+					fprintf (stderr,
+						"%s: Unsupported image type %s\n",
+						params.cmdname, *argv);
+					exit (EXIT_FAILURE);
+				}
+				tparams = image_ftable[params.opt_type]
+						.get_tparams ();
+
 				goto NXTARG;
 
 			case 'a':
@@ -154,7 +177,7 @@ main (int argc, char **argv)
 NXTARG:		;
 	}
 
-	if ((argc != 1) || image_check_params (&params))
+	if ((argc != 1) || tparams->check_params (&params))
 		usage ();
 
 	if (!params.eflag) {
@@ -222,12 +245,24 @@ NXTARG:		;
 			exit (EXIT_FAILURE);
 		}
 
-		if (!(retval = fdt_check_header (ptr)))	/* FIT image */
-			fit_print_contents (ptr);
-		else if (!(retval = image_verify_header (
+		/*
+		 * Scan image headers for all supported types
+		 * and print if veryfy_header sucessful
+		 */
+		for (i = image_ftable_size - 1; i != IH_TYPE_INVALID; i--) {
+			scantparams = image_ftable[i].get_tparams ();
+			fprintf (stderr, "%s: Verify header for type=%s\n",
+				params.cmdname, genimg_get_type_name (i));
+
+			retval = scantparams->verify_header (
 				(char *)ptr, sbuf.st_size,
-				&params))) /* old-style image */
-			image_print_contents ((image_header_t *)ptr);
+				&params);
+
+			if (retval == 0) {
+				scantparams->print_header ((char *)ptr);
+				break;
+			}
+		}
 
 		(void) munmap((void *)ptr, sbuf.st_size);
 		(void) close (ifd);
@@ -333,9 +368,9 @@ NXTARG:		;
 		exit (EXIT_FAILURE);
 	}
 
-	image_set_header (ptr, &sbuf, &params);
+	tparams->set_header (ptr, &sbuf, ifd, &params);
 
-	image_print_header (ptr);
+	tparams->print_header (ptr);
 
 	(void) munmap((void *)ptr, sbuf.st_size);
 
diff --git a/tools/mkimage.h b/tools/mkimage.h
index 3e94911..a9f20a7 100644
--- a/tools/mkimage.h
+++ b/tools/mkimage.h
@@ -71,10 +71,25 @@ struct mkimage_params {
 
 /*
  * These are image type specific variables
+ * and callback functions
  */
 struct image_type_params {
 	uint32_t header_size;
 	void *hdr;
+	int (*check_params) (struct mkimage_params *);
+	int (*verify_header) (char *, int, struct mkimage_params *);
+	void (*print_header) (char *);
+	void (*set_header) (char *, struct stat *, int,
+					struct mkimage_params *);
 };
 
+/*
+ * Image type specific function pointers list
+ */
+struct image_functions {
+	void *	(*get_tparams) (void);
+};
+
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
 #endif /* _MKIIMAGE_H_ */
-- 
1.5.3.3



More information about the U-Boot mailing list