[new uImage] Move kernel data find code to get_kernel() routine

Verification of the kernel image (in old format) and finding kernel
data is moved to a dedicated routine. The routine will also hold
support for, to be added, new image format.

Signed-off-by: Marian Balakowicz <m8@semihalf.com>
diff --git a/common/cmd_bootm.c b/common/cmd_bootm.c
index 8b6616b..2ddb191 100644
--- a/common/cmd_bootm.c
+++ b/common/cmd_bootm.c
@@ -74,6 +74,9 @@
 #endif
 
 static void print_type (image_header_t *hdr);
+static image_header_t *get_kernel (cmd_tbl_t *cmdtp, int flag,
+		int argc, char *argv[], int verify,
+		ulong *os_data, ulong *os_len);
 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
 
 /*
@@ -121,85 +124,17 @@
 	int		verify = getenv_verify();
 
 	image_header_t	*hdr;
-	ulong		img_addr;
 	ulong		os_data, os_len;
 
 	ulong		image_start, image_end;
 	ulong		load_start, load_end;
 
-
-	if (argc < 2) {
-		img_addr = load_addr;
-	} else {
-		img_addr = simple_strtoul(argv[1], NULL, 16);
-	}
-
-	show_boot_progress (1);
-	printf ("## Booting image at %08lx ...\n", img_addr);
-
-#ifdef CONFIG_HAS_DATAFLASH
-	if (addr_dataflash (img_addr)){
-		hdr = (image_header_t *)CFG_LOAD_ADDR;
-		read_dataflash (img_addr, image_get_header_size (), (char *)hdr);
-	} else
-#endif
-	hdr = (image_header_t *)img_addr;
-
-	if (!image_check_magic(hdr)) {
-		puts ("Bad Magic Number\n");
-		show_boot_progress (-1);
-		return 1;
-	}
-	show_boot_progress (2);
-
-	if (!image_check_hcrc (hdr)) {
-		puts ("Bad Header Checksum\n");
-		show_boot_progress (-2);
-		return 1;
-	}
-	show_boot_progress (3);
-
-#ifdef CONFIG_HAS_DATAFLASH
-	if (addr_dataflash (img_addr))
-		read_dataflash (img_addr + image_get_header_size (),
-				image_get_data_size (hdr),
-				(char *)image_get_data (hdr));
-#endif
-
-	/* uImage is in a system RAM, pointed to by hdr */
-	print_image_hdr (hdr);
-
-	if (verify) {
-		puts ("   Verifying Checksum ... ");
-		if (!image_check_dcrc (hdr)) {
-			printf ("Bad Data CRC\n");
-			show_boot_progress (-3);
-			return 1;
-		}
-		puts ("OK\n");
-	}
-	show_boot_progress (4);
-
-	if (!image_check_target_arch (hdr)) {
-		printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
-		show_boot_progress (-4);
+	/* get kernel image header, start address and length */
+	hdr = get_kernel (cmdtp, flag, argc, argv, verify,
+			&os_data, &os_len);
+	if (hdr == NULL)
 		return 1;
-	}
-	show_boot_progress (5);
 
-	switch (image_get_type (hdr)) {
-	case IH_TYPE_KERNEL:
-		os_data = image_get_data (hdr);
-		os_len = image_get_data_size (hdr);
-		break;
-	case IH_TYPE_MULTI:
-		image_multi_getimg (hdr, 0, &os_data, &os_len);
-		break;
-	default:
-		printf ("Wrong Image Type for %s command\n", cmdtp->name);
-		show_boot_progress (-5);
-		return 1;
-	}
 	show_boot_progress (6);
 
 	/*
@@ -229,7 +164,7 @@
 
 	switch (image_get_comp (hdr)) {
 	case IH_COMP_NONE:
-		if (image_get_load (hdr) == img_addr) {
+		if (image_get_load (hdr) == (ulong)hdr) {
 			printf ("   XIP %s ... ", type_name);
 		} else {
 			printf ("   Loading %s ... ", type_name);
@@ -280,6 +215,7 @@
 		return 1;
 	}
 	puts ("OK\n");
+	debug ("   kernel loaded at 0x%08lx, end = 0x%08lx\n", load_start, load_end);
 	show_boot_progress (7);
 
 	if ((load_start < image_end) && (load_end > image_start)) {
@@ -340,6 +276,103 @@
 	return 1;
 }
 
+/**
+ * get_kernel - find kernel image
+ * @os_data: pointer to a ulong variable, will hold os data start address
+ * @os_len: pointer to a ulong variable, will hold os data length
+ *
+ * get_kernel() tries to find a kernel image, verifies its integrity
+ * and locates kernel data.
+ *
+ * returns:
+ *     pointer to image header if valid image was found, plus kernel start
+ *     address and length, otherwise NULL
+ */
+static image_header_t *get_kernel (cmd_tbl_t *cmdtp, int flag,
+		int argc, char *argv[], int verify,
+		ulong *os_data, ulong *os_len)
+{
+	image_header_t	*hdr;
+	ulong		img_addr;
+
+	if (argc < 2) {
+		img_addr = load_addr;
+	} else {
+		img_addr = simple_strtoul(argv[1], NULL, 16);
+	}
+
+	show_boot_progress (1);
+	printf ("## Booting image at %08lx ...\n", img_addr);
+
+#ifdef CONFIG_HAS_DATAFLASH
+	if (addr_dataflash (img_addr)){
+		hdr = (image_header_t *)CFG_LOAD_ADDR;
+		read_dataflash (img_addr, image_get_header_size (), (char *)hdr);
+	} else
+#endif
+	hdr = (image_header_t *)img_addr;
+
+	if (!image_check_magic(hdr)) {
+		puts ("Bad Magic Number\n");
+		show_boot_progress (-1);
+		return NULL;
+	}
+	show_boot_progress (2);
+
+	if (!image_check_hcrc (hdr)) {
+		puts ("Bad Header Checksum\n");
+		show_boot_progress (-2);
+		return NULL;
+	}
+	show_boot_progress (3);
+
+#ifdef CONFIG_HAS_DATAFLASH
+	if (addr_dataflash (img_addr))
+		read_dataflash (img_addr + image_get_header_size (),
+				image_get_data_size (hdr),
+				(char *)image_get_data (hdr));
+#endif
+
+	/* uImage is in a system RAM, pointed to by hdr */
+	print_image_hdr (hdr);
+
+	if (verify) {
+		puts ("   Verifying Checksum ... ");
+		if (!image_check_dcrc (hdr)) {
+			printf ("Bad Data CRC\n");
+			show_boot_progress (-3);
+			return NULL;
+		}
+		puts ("OK\n");
+	}
+	show_boot_progress (4);
+
+	if (!image_check_target_arch (hdr)) {
+		printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
+		show_boot_progress (-4);
+		return NULL;
+	}
+	show_boot_progress (5);
+
+	switch (image_get_type (hdr)) {
+	case IH_TYPE_KERNEL:
+		*os_data = image_get_data (hdr);
+		*os_len = image_get_data_size (hdr);
+		break;
+	case IH_TYPE_MULTI:
+		image_multi_getimg (hdr, 0, os_data, os_len);
+		break;
+	default:
+		printf ("Wrong Image Type for %s command\n", cmdtp->name);
+		show_boot_progress (-5);
+		return NULL;
+	}
+	debug ("   kernel data at 0x%08lx, end = 0x%08lx\n",
+			*os_data, *os_data + *os_len);
+
+	return hdr;
+}
+
 U_BOOT_CMD(
 	bootm,	CFG_MAXARGS,	1,	do_bootm,
 	"bootm   - boot application image from memory\n",
@@ -502,7 +535,7 @@
 #endif
 
 /*******************************************************************/
-/* */
+/* helper routines */
 /*******************************************************************/
 void print_image_hdr (image_header_t *hdr)
 {