bootm: refactor ramdisk locating code

Move determing if we have a ramdisk and where its located into the
common code. Keep track of the ramdisk start and end in the
bootm_headers_t image struct.

Signed-off-by: Kumar Gala <galak@kernel.crashing.org>
diff --git a/common/cmd_bootm.c b/common/cmd_bootm.c
index 3f63b84..56236b9 100644
--- a/common/cmd_bootm.c
+++ b/common/cmd_bootm.c
@@ -116,6 +116,33 @@
 }
 void board_lmb_reserve(struct lmb *lmb) __attribute__((weak, alias("__board_lmb_reserve")));
 
+#if defined(__ARM__)
+  #define IH_INITRD_ARCH IH_ARCH_ARM
+#elif defined(__avr32__)
+  #define IH_INITRD_ARCH IH_ARCH_AVR32
+#elif defined(__bfin__)
+  #define IH_INITRD_ARCH IH_ARCH_BLACKFIN
+#elif defined(__I386__)
+  #define IH_INITRD_ARCH IH_ARCH_I386
+#elif defined(__M68K__)
+  #define IH_INITRD_ARCH IH_ARCH_M68K
+#elif defined(__microblaze__)
+  #define IH_INITRD_ARCH IH_ARCH_MICROBLAZE
+#elif defined(__mips__)
+  #define IH_INITRD_ARCH IH_ARCH_MIPS
+#elif defined(__nios__)
+  #define IH_INITRD_ARCH IH_ARCH_NIOS
+#elif defined(__nios2__)
+  #define IH_INITRD_ARCH IH_ARCH_NIOS2
+#elif defined(__PPC__)
+  #define IH_INITRD_ARCH IH_ARCH_PPC
+#elif defined(__sh__)
+  #define IH_INITRD_ARCH IH_ARCH_SH
+#elif defined(__sparc__)
+  #define IH_INITRD_ARCH IH_ARCH_SPARC
+#else
+# error Unknown CPU type
+#endif
 
 /*******************************************************************/
 /* bootm - boot application image from image in memory */
@@ -133,6 +160,7 @@
 	ulong		load_start, load_end;
 	ulong		mem_start;
 	phys_size_t	mem_size;
+	int		ret;
 
 	struct lmb lmb;
 
@@ -222,6 +250,16 @@
 		return 1;
 	}
 
+	if (os == IH_OS_LINUX) {
+		/* find ramdisk */
+		ret = boot_get_ramdisk (argc, argv, &images, IH_INITRD_ARCH,
+				&images.rd_start, &images.rd_end);
+		if (ret) {
+			puts ("Ramdisk image is corrupt\n");
+			return 1;
+		}
+	}
+
 	image_start = (ulong)os_hdr;
 	load_end = 0;
 	type_name = genimg_get_type_name (type);
diff --git a/include/image.h b/include/image.h
index e16c253..b8577a0 100644
--- a/include/image.h
+++ b/include/image.h
@@ -221,6 +221,8 @@
 
 	ulong		ep;		/* entry point of OS */
 
+	ulong		rd_start, rd_end;/* ramdisk start/end */
+
 	int		verify;		/* getenv("verify")[0] != 'n' */
 	struct lmb	*lmb;		/* for memory mgmt */
 } bootm_headers_t;
diff --git a/lib_arm/bootm.c b/lib_arm/bootm.c
index 5660a50..f9f702f 100644
--- a/lib_arm/bootm.c
+++ b/lib_arm/bootm.c
@@ -61,7 +61,6 @@
 void do_bootm_linux (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
 		     bootm_headers_t *images)
 {
-	ulong	initrd_start, initrd_end;
 	bd_t	*bd = gd->bd;
 	char	*s;
 	int	machid = bd->bi_arch_number;
@@ -80,11 +79,6 @@
 		printf ("Using machid 0x%x from environment\n", machid);
 	}
 
-	ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_ARM,
-			&initrd_start, &initrd_end);
-	if (ret)
-		goto error;
-
 	show_boot_progress (15);
 
 	debug ("## Transferring control to Linux (at address %08lx) ...\n",
@@ -111,8 +105,8 @@
 	setup_commandline_tag (bd, commandline);
 #endif
 #ifdef CONFIG_INITRD_TAG
-	if (initrd_start && initrd_end)
-		setup_initrd_tag (bd, initrd_start, initrd_end);
+	if (images->rd_start && images->rd_end)
+		setup_initrd_tag (bd, images->rd_start, images->rd_end);
 #endif
 #if defined (CONFIG_VFD) || defined (CONFIG_LCD)
 	setup_videolfb_tag ((gd_t *) gd);
diff --git a/lib_avr32/bootm.c b/lib_avr32/bootm.c
index 7beab99..762701f 100644
--- a/lib_avr32/bootm.c
+++ b/lib_avr32/bootm.c
@@ -176,7 +176,6 @@
 void do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
 		    bootm_headers_t *images)
 {
-	ulong	initrd_start, initrd_end;
 	void	(*theKernel)(int magic, void *tagtable);
 	struct	tag *params, *params_start;
 	char	*commandline = getenv("bootargs");
@@ -184,20 +183,15 @@
 
 	theKernel = (void *)images->ep;
 
-	ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_AVR32,
-			&initrd_start, &initrd_end);
-	if (ret)
-		goto error;
-
 	show_boot_progress (15);
 
 	params = params_start = (struct tag *)gd->bd->bi_boot_params;
 	params = setup_start_tag(params);
 	params = setup_memory_tags(params);
-	if (initrd_start) {
+	if (images->rd_start) {
 		params = setup_ramdisk_tag(params,
-					   PHYSADDR(initrd_start),
-					   PHYSADDR(initrd_end));
+					   PHYSADDR(images->rd_start),
+					   PHYSADDR(images->rd_end));
 	}
 	params = setup_commandline_tag(params, commandline);
 	params = setup_clock_tags(params);
diff --git a/lib_i386/bootm.c b/lib_i386/bootm.c
index 452eef7..8c0b225 100644
--- a/lib_i386/bootm.c
+++ b/lib_i386/bootm.c
@@ -36,7 +36,6 @@
 {
 	void		*base_ptr;
 	ulong		os_data, os_len;
-	ulong		initrd_start, initrd_end;
 	image_header_t	*hdr;
 	int		ret;
 #if defined(CONFIG_FIT)
@@ -44,11 +43,6 @@
 	size_t		len;
 #endif
 
-	ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_I386,
-			&initrd_start, &initrd_end);
-	if (ret)
-		goto error;
-
 	if (images->legacy_hdr_valid) {
 		hdr = images->legacy_hdr_os;
 		if (image_check_type (hdr, IH_TYPE_MULTI)) {
@@ -76,7 +70,7 @@
 	}
 
 	base_ptr = load_zimage ((void*)os_data, os_len,
-			initrd_start, initrd_end - initrd_start, 0);
+			images->rd_start, images->rd_end - images->rd_start, 0);
 
 	if (NULL == base_ptr) {
 		printf ("## Kernel loading failed ...\n");
diff --git a/lib_m68k/bootm.c b/lib_m68k/bootm.c
index fe658fe..241721d 100644
--- a/lib_m68k/bootm.c
+++ b/lib_m68k/bootm.c
@@ -50,7 +50,7 @@
 {
 	ulong sp;
 
-	ulong rd_data_start, rd_data_end, rd_len;
+	ulong rd_len;
 	ulong initrd_start, initrd_end;
 	int ret;
 
@@ -95,14 +95,8 @@
 
 	kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))images->ep;
 
-	/* find ramdisk */
-	ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_M68K,
-			&rd_data_start, &rd_data_end);
-	if (ret)
-		goto error;
-
-	rd_len = rd_data_end - rd_data_start;
-	ret = boot_ramdisk_high (lmb, rd_data_start, rd_len,
+	rd_len = images->rd_end - images->rd_start;
+	ret = boot_ramdisk_high (lmb, images->rd_start, rd_len,
 			&initrd_start, &initrd_end);
 	if (ret)
 		goto error;
diff --git a/lib_mips/bootm.c b/lib_mips/bootm.c
index 7df6ce6..9869c33 100644
--- a/lib_mips/bootm.c
+++ b/lib_mips/bootm.c
@@ -48,7 +48,6 @@
 void do_bootm_linux (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[],
 		     bootm_headers_t *images)
 {
-	ulong	initrd_start, initrd_end;
 	void	(*theKernel) (int, char **, char **, int *);
 	char	*commandline = getenv ("bootargs");
 	char	env_buf[12];
@@ -58,11 +57,6 @@
 	/* find kernel entry point */
 	theKernel = (void (*)(int, char **, char **, int *))images->ep;
 
-	ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_MIPS,
-			&initrd_start, &initrd_end);
-	if (ret)
-		goto error;
-
 	show_boot_progress (15);
 
 #ifdef DEBUG
@@ -82,10 +76,10 @@
 
 	linux_env_set ("memsize", env_buf);
 
-	sprintf (env_buf, "0x%08X", (uint) UNCACHED_SDRAM (initrd_start));
-	linux_env_set ("initrd_start", env_buf);
+	sprintf (env_buf, "0x%08X", (uint) UNCACHED_SDRAM (images->rd_start));
+	linux_env_set ("images->rd_start", env_buf);
 
-	sprintf (env_buf, "0x%X", (uint) (initrd_end - initrd_start));
+	sprintf (env_buf, "0x%X", (uint) (images->rd_end - images->rd_start));
 	linux_env_set ("initrd_size", env_buf);
 
 	sprintf (env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
diff --git a/lib_ppc/bootm.c b/lib_ppc/bootm.c
index b08ee0d..e6a6e44 100644
--- a/lib_ppc/bootm.c
+++ b/lib_ppc/bootm.c
@@ -73,7 +73,7 @@
 	ulong	sp;
 
 	ulong	initrd_start, initrd_end;
-	ulong	rd_data_start, rd_data_end, rd_len;
+	ulong	rd_len;
 	ulong	size;
 	phys_size_t bootm_size;
 
@@ -153,13 +153,7 @@
 		set_clocks_in_mhz(kbd);
 	}
 
-	/* find ramdisk */
-	ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_PPC,
-			&rd_data_start, &rd_data_end);
-	if (ret)
-		goto error;
-
-	rd_len = rd_data_end - rd_data_start;
+	rd_len = images->rd_end - images->rd_start;
 
 #if defined(CONFIG_OF_LIBFDT)
 	ret = boot_relocate_fdt (lmb, bootmap_base,
@@ -171,7 +165,7 @@
 	 */
 	if (of_size) {
 		/* pass in dummy initrd info, we'll fix up later */
-		if (fdt_chosen(of_flat_tree, rd_data_start, rd_data_end, 0) < 0) {
+		if (fdt_chosen(of_flat_tree, images->rd_start, images->rd_end, 0) < 0) {
 			fdt_error ("/chosen node create failed");
 			goto error;
 		}
@@ -221,7 +215,7 @@
 	}
 #endif	/* CONFIG_OF_LIBFDT */
 
-	ret = boot_ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
+	ret = boot_ramdisk_high (lmb, images->rd_start, rd_len, &initrd_start, &initrd_end);
 	if (ret)
 		goto error;
 
@@ -235,7 +229,7 @@
 		/* Look for the dummy entry and delete it */
 		for (j = 0; j < total; j++) {
 			fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
-			if (addr == rd_data_start) {
+			if (addr == images->rd_start) {
 				fdt_del_mem_rsv(of_flat_tree, j);
 				break;
 			}
diff --git a/lib_sparc/bootm.c b/lib_sparc/bootm.c
index ac15396..71d34ba 100644
--- a/lib_sparc/bootm.c
+++ b/lib_sparc/bootm.c
@@ -89,7 +89,7 @@
 	char *bootargs;
 	ulong load;
 	ulong initrd_start, initrd_end;
-	ulong rd_data_start, rd_data_end, rd_len;
+	ulong rd_len;
 	unsigned int data, len, checksum;
 	unsigned int initrd_addr, kernend;
 	void (*kernel) (struct linux_romvec *, void *);
@@ -139,19 +139,8 @@
 	 * extracted and is writeable.
 	 */
 
-	/*
-	 * Are we going to use an initrd image?
-	 */
-	ret = boot_get_ramdisk(argc, argv, images, IH_ARCH_SPARC,
-			       &rd_data_start, &rd_data_end);
-	if (ret) {
-		/* RAM disk found but was corrupt */
-		puts("RAM Disk corrupt\n");
-		goto error;
-	}
-
 	/* Calc length of RAM disk, if zero no ramdisk available */
-	rd_len = rd_data_end - rd_data_start;
+	rd_len = images->rd_end - images->rd_start;
 
 	if (rd_len) {
 
@@ -161,7 +150,7 @@
 		 */
 		lmb_reserve(lmb, CFG_RELOC_MONITOR_BASE, CFG_RAM_END);
 
-		ret = boot_ramdisk_high(lmb, rd_data_start, rd_len,
+		ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
 					&initrd_start, &initrd_end);
 		if (ret) {
 			puts("### Failed to relocate RAM disk\n");