fix: add support for bubt to another flash

       - Added a wrapper to the interface bubt functions which chooses
         the desired bubt interface according to an argument to the
         bubt command.
       - Current bubt usage is bubt [file_name] [destination_interface
         [load_from_usb]]
       - SYSTEMSW-1303

Change-Id: Iad09a59f570f50ea3f5b3e3580af62f468e3b522
Signed-off-by: Zion Gal <galz@marvell.com>
Reviewed-on: http://vgitil04.il.marvell.com:8080/17823
Reviewed-by: Omri Itach <omrii@marvell.com>
Tested-by: Omri Itach <omrii@marvell.com>
diff --git a/board/mv_ebu/common/USP/cmd_bubt.c b/board/mv_ebu/common/USP/cmd_bubt.c
index 63852ad..19d5155 100755
--- a/board/mv_ebu/common/USP/cmd_bubt.c
+++ b/board/mv_ebu/common/USP/cmd_bubt.c
@@ -31,6 +31,8 @@
 
 #include <fs.h>
 
+#define	DESTINATION_STRING	10
+
 #if defined(CONFIG_CMD_NAND)
 #include <nand.h>
 extern nand_info_t nand_info[];       /* info for NAND chips */
@@ -131,11 +133,13 @@
 /*
  * Extract arguments from bubt command line
  * argc, argv are the input arguments of bubt command line
- * loadfrom is pointer to the extracted argument: from where to load the u-boot bin file
+ * loadfrom is a pointer to the extracted argument: from where to load the u-boot bin file
+ * destination_burn is a pointer to a string which denotes the bubt interface 
  */
-MV_STATUS fetch_bubt_cmd_args(int argc, char * const argv[], MV_U32 *loadfrom)
+MV_STATUS fetch_bubt_cmd_args(int argc, char * const argv[], int *loadfrom, char *destination_burn)
 {
 	*loadfrom = 0;
+	strcpy(destination_burn,"default");
 	/* bubt */
 	if (argc < 2) {
 		copy_filename (BootFile, "u-boot.bin", sizeof(BootFile));
@@ -146,6 +150,7 @@
 		if ((0 == strcmp(argv[1], "spi")) || (0 == strcmp(argv[1], "nand"))
 				|| (0 == strcmp(argv[1], "nor")))
 		{
+			strcpy(destination_burn, argv[1]);
 			copy_filename (BootFile, "u-boot.bin", sizeof(BootFile));
 			printf("Using default filename \"u-boot.bin\" \n");
 		}
@@ -159,6 +164,7 @@
 		if ((0 == strcmp(argv[1], "spi")) || (0 == strcmp(argv[1], "nand"))
 				|| (0 == strcmp(argv[1], "nor")))
 		{
+			strcpy(destination_burn, argv[1]);
 			copy_filename (BootFile, "u-boot.bin", sizeof(BootFile));
 			printf("Using default filename \"u-boot.bin\" \n");
 
@@ -174,12 +180,16 @@
 		}
 		else
 		{
+			strcpy(destination_burn, argv[2]);
+
 			copy_filename (BootFile, argv[1], sizeof(BootFile));
 		}
 	}
 	/* "bubt filename destination source" */
 	else
 	{
+		strcpy(destination_burn, argv[2]);
+
 		copy_filename (BootFile, argv[1], sizeof(BootFile));
 
 		if (0 == strcmp("usb", argv[3])) {
@@ -230,20 +240,19 @@
 	return filesize;
 }
 
-#if defined(MV_NAND_BOOT)
+#if defined(MV_NAND_BOOT) || defined(MV_NAND)
 /* Boot from NAND flash */
 /* Write u-boot image into the nand flash */
-int nand_burn_uboot_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+int nand_burn_uboot_cmd(cmd_tbl_t *cmdtp, int flag, int loadfrom, int argc, char * const argv[])
 {
 	int filesize = 0;
 	MV_U32 ret = 0;
 	extern char console_buffer[];
 	nand_info_t *nand = &nand_info[0];
 	size_t blocksize = nand_info[0].erasesize;
-	size_t env_offset = CONFIG_ENV_OFFSET;
+	size_t env_offset = CONFIG_ENV_OFFSET_NAND;
 	size_t size = CONFIG_UBOOT_SIZE;
 	size_t offset = 0;
-	MV_U32 loadfrom = 0; /* 0 - from tftp, 1 - from USB */
 
 	/* Align U-Boot size to currently used blocksize */
 	size = ((size + (blocksize - 1)) & (~(blocksize-1)));
@@ -267,9 +276,6 @@
 #endif
 
 	/* verify requested source is valid */
-	if (fetch_bubt_cmd_args(argc, argv, &loadfrom) != MV_OK)
-		return 0;
-
 	if ((filesize = fetch_uboot_file (loadfrom)) <= 0)
 		return 0;
 
@@ -284,8 +290,8 @@
 	    strcmp(console_buffer,"yes") == 0 ||
 	    strcmp(console_buffer,"y") == 0 ) {
 
-		printf("Erasing 0x%x - 0x%x:",env_offset, env_offset + CONFIG_ENV_RANGE);
-		nand_erase(nand, env_offset, CONFIG_ENV_RANGE);
+		printf("Erasing 0x%x - 0x%x:",env_offset, env_offset + CONFIG_ENV_RANGE_NAND);
+		nand_erase(nand, env_offset, CONFIG_ENV_RANGE_NAND);
 		printf("\t[Done]\n");
 	}
 
@@ -303,27 +309,18 @@
 	return 1;
 }
 
-U_BOOT_CMD(
-		bubt,      4,     1,      nand_burn_uboot_cmd,
-		"bubt	- Burn an image on the Boot Nand Flash.\n",
-		"[file-name] [destination [source]] \n"
-		"\tBurn a binary image on the Boot Flash, default file-name is u-boot.bin .\n"
-		"\tdestination is nand, spi or nor.\n"
-		"\tsource can be tftp or usb, default is tftp.\n"
-);
 #endif /* defined(CONFIG_NAND_BOOT) */
 
-#if defined(MV_SPI_BOOT)
+#if defined(MV_SPI_BOOT) || defined(MV_INCLUDE_SPI)
 
 /* Boot from SPI flash */
 /* Write u-boot image into the SPI flash */
-int spi_burn_uboot_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+int spi_burn_uboot_cmd(cmd_tbl_t *cmdtp, int flag, int loadfrom, int argc, char * const argv[])
 {
 	int filesize = 0;
 	MV_U32 ret = 0;
 	extern char console_buffer[];
 	load_addr = CONFIG_SYS_LOAD_ADDR;
-	MV_U32 loadfrom = 0; /* 0 - from tftp, 1 - from USB */
 
 	if(!flash) {
 		flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
@@ -336,9 +333,6 @@
 	}
 
 	/* verify requested source is valid */
-	if (fetch_bubt_cmd_args(argc, argv, &loadfrom) != MV_OK)
-		return 0;
-
 	if ((filesize = fetch_uboot_file (loadfrom)) <= 0)
 		return 0;
 
@@ -355,17 +349,17 @@
 	    strcmp(console_buffer,"yes") == 0 ||
 	    strcmp(console_buffer,"y") == 0 ) {
 
-		printf("Erasing 0x%x - 0x%x:",CONFIG_ENV_OFFSET, CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE);
-		spi_flash_erase(flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE);
+		printf("Erasing 0x%x - 0x%x:",CONFIG_ENV_OFFSET_SPI, CONFIG_ENV_OFFSET_SPI + CONFIG_ENV_SIZE_SPI);
+		spi_flash_erase(flash, CONFIG_ENV_OFFSET_SPI, CONFIG_ENV_SIZE_SPI);
 		printf("\t[Done]\n");
 	}
-	if (filesize > CONFIG_ENV_OFFSET)
+	if (filesize > CONFIG_ENV_OFFSET_SPI)
 	{
 		printf("Error: Image size (%x) exceeds environment variables offset (%x). ",filesize,CONFIG_ENV_OFFSET);
 		return 0;
 	}
-	printf("Erasing 0x%x - 0x%x: ",0, 0 + CONFIG_ENV_OFFSET);
-	spi_flash_erase(flash, 0, CONFIG_ENV_OFFSET);
+	printf("Erasing 0x%x - 0x%x: ",0, 0 + CONFIG_ENV_OFFSET_SPI);
+	spi_flash_erase(flash, 0, CONFIG_ENV_OFFSET_SPI);
 	printf("\t\t[Done]\n");
 
 	printf("Writing image to flash:");
@@ -384,22 +378,13 @@
 	return 1;
 }
 
-U_BOOT_CMD(
-		bubt,      4,     1,      spi_burn_uboot_cmd,
-		"bubt	- Burn an image on the Boot SPI Flash.\n",
-		" file-name \n"
-		"[file-name] [destination [source]] \n"
-		"\tBurn a binary image on the Boot Flash, default file-name is u-boot.bin .\n"
-		"\tdestination is nand, spi or nor.\n"
-		"\tsource can be tftp or usb, default is tftp.\n"
-);
 #endif
 
 
-#if defined(MV_NOR_BOOT)
+#if defined(MV_NOR_BOOT) || (MV_INCLUDE_NOR)
 
 /* Boot from Nor flash */
-int nor_burn_uboot_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+int nor_burn_uboot_cmd(cmd_tbl_t *cmdtp, int flag, int loadfrom, int argc, char * const argv[])
 {
 	int filesize = 0;
 	MV_U32 ret = 0;
@@ -414,12 +399,8 @@
 //	s_end = flash_in_which_sec(&flash_info[0], CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN -1);
 //	env_sec = flash_in_which_sec(&flash_info[0], CONFIG_ENV_ADDR);
 
-	MV_U32 loadfrom = 0; /* 0 - from tftp, 1 - from USB */
 
 	/* verify requested source is valid */
-	if (fetch_bubt_cmd_args(argc, argv, &loadfrom) != MV_OK)
-		return 0;
-
 	if ((filesize = fetch_uboot_file (loadfrom)) <= 0)
 		return 0;
 
@@ -463,27 +444,17 @@
 	return 1;
 }
 
-U_BOOT_CMD(
-		bubt,      4,     1,      nor_burn_uboot_cmd,
-		"bubt	- Burn an image on the Boot Flash.\n",
-		" file-name \n"
-		"[file-name] [destination [source]] \n"
-		"\tBurn a binary image on the Boot Flash, default file-name is u-boot.bin .\n"
-		"\tdestination is nand, spi or nor.\n"
-		"\tsource can be tftp or usb, default is tftp.\n"
-);
 #endif /* MV_NOR_BOOT */
 
 #if defined(MV_MMC_BOOT)
 
 /* Boot from SD/MMC/eMMC */
 /* Write u-boot image into SD/MMC/eMMC device */
-int mmc_burn_uboot_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+int mmc_burn_uboot_cmd(cmd_tbl_t *cmdtp, int flag, int loadfrom, int argc, char * const argv[])
 {
 	int filesize = 0;
 	extern char console_buffer[];
 	load_addr = CONFIG_SYS_LOAD_ADDR;
-	MV_U32 loadfrom = 0; /* 0 - from tftp, 1 - from USB */
 	lbaint_t	start_lba;
 	lbaint_t	blk_count;
 	ulong		blk_written;
@@ -514,9 +485,6 @@
 #endif
 
 	/* verify requested source is valid */
-	if (fetch_bubt_cmd_args(argc, argv, &loadfrom) != MV_OK)
-		return 0;
-
 	if ((filesize = fetch_uboot_file (loadfrom)) <= 0)
 		return 0;
 
@@ -565,12 +533,76 @@
 	return 1;
 }
 
+#endif
+
+int burn_uboot_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	char	destination_burn[DESTINATION_STRING];
+	int	loadfrom = 0; /* 0 - from tftp, 1 - from USB */
+
+	memset(destination_burn, '\0', sizeof(destination_burn));
+
+	if(fetch_bubt_cmd_args(argc, argv, &loadfrom,destination_burn) != MV_OK)
+		return 0;
+
+#if defined(MV_CROSS_FLASH_BOOT)
+	if (0 == strcmp(destination_burn, "nand")) {
+#if defined(MV_NAND) || defined(MV_NAND_BOOT)
+		return nand_burn_uboot_cmd(cmdtp, flag, loadfrom, argc, argv);
+#endif
+		printf("\t[FAIL] - u-boot does not support a write to %s interface.\n",destination_burn);
+		return 0;
+	}
+
+	if (0 == strcmp(destination_burn, "spi")) {
+#if defined(MV_INCLUDE_SPI) || defined (MV_SPI_BOOT)
+		return spi_burn_uboot_cmd(cmdtp, flag, loadfrom, argc, argv);
+#endif
+		printf("\t[FAIL] - u-boot does not support a write to %s interface.\n",destination_burn);
+		return 0;
+	}
+
+	if (0 == strcmp(destination_burn, "nor")) {
+#if defined(MV_INCLUDE_NOR) || defined (MV_NOR_BOOT)
+		return nor_burn_uboot_cmd(cmdtp, flag, loadfrom, argc, argv);
+#endif
+		printf("\t[FAIL] - u-boot does not support a write to %s interface.\n",destination_burn);
+		return 0;
+	}
+#endif
+
+#if defined(MV_NAND_BOOT)
+		return nand_burn_uboot_cmd(cmdtp, flag, loadfrom, argc, argv);
+#endif
+#if defined(MV_SPI_BOOT)
+		return spi_burn_uboot_cmd(cmdtp, flag, loadfrom, argc, argv);
+#endif
+#if defined(MV_NOR_BOOT)
+		return nor_burn_uboot_cmd(cmdtp, flag, loadfrom, argc, argv);
+#endif
+#if defined(MV_MMC_BOOT)
+		return mmc_burn_uboot_cmd(cmdtp, flag, loadfrom, argc, argv);
+#endif
+
+		return 1;
+}
+
+#if defined(MV_MMC_BOOT)
 U_BOOT_CMD(
-		bubt,      3,     1,      mmc_burn_uboot_cmd,
-		"bubt	- Burn an image on the Boot SD/MMC/eMMC device.\n",
+		bubt,      3,     1,      burn_uboot_cmd,
+		"bubt	- Burn an image on the Boot device.\n",
 		" file-name \n"
 		"[file-name] [source] \n"
 		"\tBurn a binary image on the Boot Device, default file-name is u-boot.bin .\n"
 		"\tsource can be tftp or usb, default is tftp.\n"
 );
+#else
+U_BOOT_CMD(
+		bubt,      4,     1,      burn_uboot_cmd,
+		"bubt	- Burn an image on the Boot flash device.\n",
+		"[file-name] [destination [source]] \n"
+		"\tBurn a binary image on the Boot Device, default file-name is u-boot.bin .\n"
+		"\tsource can be tftp or usb, default is tftp.\n"
+		"\texample: bubt u-boot.bin nand tftp\n"
+		);
 #endif
diff --git a/drivers/mtd/nand/nand.c b/drivers/mtd/nand/nand.c
index d79bc1c..ed40ab6 100755
--- a/drivers/mtd/nand/nand.c
+++ b/drivers/mtd/nand/nand.c
@@ -121,7 +121,7 @@
 	board_nand_select_device(nand_info[nand_curr_device].priv, nand_curr_device);
 #endif
 }
-#if defined(CONFIG_ENV_IS_IN_NAND)
+
 int nand_get_env_offs(void)
 {
 	size_t offset = 0;
@@ -145,7 +145,7 @@
                         offset = (i + 1) * blocksize;
                 }
                 i++;
-                if (sum >= CONFIG_ENV_RANGE)
+                if (sum >= CONFIG_ENV_RANGE_NAND)
                         break;
 
         }
@@ -166,11 +166,10 @@
 	size_t env_size = 0;
 	size_t blocksize = nand_info[0].erasesize;
 
-	if (blocksize < CONFIG_ENV_SIZE) {
-		env_size = ((CONFIG_ENV_SIZE + (blocksize - 1)) & (~(blocksize-1)));
+	if (blocksize < CONFIG_ENV_SIZE_NAND) {
+		env_size = ((CONFIG_ENV_SIZE_NAND + (blocksize - 1)) & (~(blocksize-1)));
 		return env_size;
 	} else
 		return blocksize;
 }
-#endif
 
diff --git a/include/configs/armada_375.h b/include/configs/armada_375.h
index 83faa99..4c470a6 100755
--- a/include/configs/armada_375.h
+++ b/include/configs/armada_375.h
@@ -265,20 +265,26 @@
 #define CONFIG_SF_DEFAULT_BUS		CONFIG_ENV_SPI_BUS
 #endif
 
+#if defined(MV_SEC_64K)
+#define CONFIG_ENV_SECT_SIZE_SPI	0x10000
+#elif defined(MV_SEC_128K)
+#define CONFIG_ENV_SECT_SIZE_SPI	0x20000
+#elif defined(MV_SEC_256K)
+#define CONFIG_ENV_SECT_SIZE_SPI	0x40000
+#endif
+
+#define	CONFIG_ENV_OFFSET_SPI		0x100000
+#define	CONFIG_ENV_SIZE_SPI		CONFIG_ENV_SECT_SIZE_SPI
+
+
 /* Boot from SPI settings */
 	#if defined(MV_SPI_BOOT)
 		#define CONFIG_ENV_IS_IN_SPI_FLASH
 
-		#if defined(MV_SEC_64K)
-			#define CONFIG_ENV_SECT_SIZE            0x10000
-		#elif defined(MV_SEC_128K)
-			#define CONFIG_ENV_SECT_SIZE            0x20000
-		#elif defined(MV_SEC_256K)
-			#define CONFIG_ENV_SECT_SIZE            0x40000
-		#endif
+		#define CONFIG_ENV_SECT_SIZE			CONFIG_ENV_SECT_SIZE_SPI
 
-		#define CONFIG_ENV_SIZE                         CONFIG_ENV_SECT_SIZE    /* environment takes one sector */
-		#define CONFIG_ENV_OFFSET                       0x100000                /* (1MB For Image) environment starts here  */
+		#define CONFIG_ENV_SIZE                         CONFIG_ENV_SECT_SIZE_SPI    /* environment takes one sector */
+		#define CONFIG_ENV_OFFSET                       CONFIG_ENV_OFFSET_SPI   /* (1MB For Image) environment starts here  */
 		#define CONFIG_ENV_ADDR                         CONFIG_ENV_OFFSET
 		#define MONITOR_HEADER_LEN                      0x200
 		#define CONFIG_SYS_MONITOR_BASE                 0
@@ -301,6 +307,9 @@
 /* NAND-FLASH stuff     */
 /************************/
 #ifdef MV_NAND
+	#ifndef __ASSEMBLY__
+extern int nand_get_env_offs(void);
+	#endif /* __ASSEMBLY__ */
 	#define MV_NAND_PIO_MODE
 	#define MV_NAND_1CS_MODE
 	/*
@@ -324,17 +333,21 @@
 	#undef MV_NFC_DBG
 	#define CONFIG_JFFS2_NAND
 
+	#define	CONFIG_ENV_SIZE_NAND			0x80000
+	#define CONFIG_ENV_OFFSET_NAND			nand_get_env_offs()
+	#define	CONFIG_ENV_RANGE_NAND			CONFIG_ENV_SIZE_NAND * 8
+
 /* Boot from NAND settings */
 	#if defined(MV_NAND_BOOT)
 		#define CONFIG_ENV_IS_IN_NAND
 
-		#define CONFIG_ENV_SIZE                 0x80000                 /* environment takes one erase block */
-		#define CONFIG_ENV_OFFSET               nand_get_env_offs()     /* environment starts here  */
+		#define CONFIG_ENV_SIZE                 CONFIG_ENV_SIZE_NAND                 /* environment takes one erase block */
+		#define CONFIG_ENV_OFFSET               CONFIG_ENV_OFFSET_NAND     /* environment starts here  */
 		#define CONFIG_ENV_ADDR                 CONFIG_ENV_OFFSET
 		#define MONITOR_HEADER_LEN              0x200
 		#define CONFIG_SYS_MONITOR_BASE         0
 		#define CONFIG_SYS_MONITOR_LEN          0x80000           /* Reserve 512 kB for Monitor */
-		#define CONFIG_ENV_RANGE                CONFIG_ENV_SIZE * 8
+		#define CONFIG_ENV_RANGE                CONFIG_ENV_RANGE_NAND
 
 		#define MV_NBOOT_BASE                   0
 		#define MV_NBOOT_LEN                    (4 << 10)       /* Reserved 4KB for boot strap */
diff --git a/include/configs/armada_38x.h b/include/configs/armada_38x.h
index 38024b1..e1188b5 100644
--- a/include/configs/armada_38x.h
+++ b/include/configs/armada_38x.h
@@ -167,6 +167,7 @@
 */
 #ifdef CONFIG_ARMADA_39X
 	#ifndef CONFIG_CUSTOMER_BOARD_SUPPORT
+		#define MV_CROSS_FLASH_BOOT
 		#define CONFIG_CMD_BOARDCFG
 	#endif /* CONFIG_CUSTOMER_BOARD_SUPPORT */
 #endif /* CONFIG_ARMADA_39X */
@@ -292,6 +293,17 @@
 	#define CONFIG_ENV_SPI_CS               0
 	#define CONFIG_ENV_SPI_BUS              mvBoardSpiBusGet()
 
+#if defined(MV_SEC_64K)
+	#define CONFIG_ENV_SECT_SIZE_SPI		0x10000
+#elif defined(MV_SEC_128K)
+	#define CONFIG_ENV_SECT_SIZE_SPI		0x20000
+#elif defined(MV_SEC_256K)
+	#define CONFIG_ENV_SECT_SIZE_SPI		0x40000
+#endif
+
+#define CONFIG_ENV_SIZE_SPI		CONFIG_ENV_SECT_SIZE_SPI	/* environment takes one sector */
+#define CONFIG_ENV_OFFSET_SPI		0x100000			/* (1MB For Image) environment starts here  */
+
 #ifndef CONFIG_SF_DEFAULT_SPEED
 # define CONFIG_SF_DEFAULT_SPEED        CONFIG_ENV_SPI_MAX_HZ
 #endif
@@ -309,16 +321,10 @@
 	#if defined(MV_SPI_BOOT)
 		#define CONFIG_ENV_IS_IN_SPI_FLASH
 
-		#if defined(MV_SEC_64K)
-			#define CONFIG_ENV_SECT_SIZE            0x10000
-		#elif defined(MV_SEC_128K)
-			#define CONFIG_ENV_SECT_SIZE            0x20000
-		#elif defined(MV_SEC_256K)
-			#define CONFIG_ENV_SECT_SIZE            0x40000
-		#endif
+		#define CONFIG_ENV_SECT_SIZE           		CONFIG_ENV_SECT_SIZE_SPI
 
 		#define CONFIG_ENV_SIZE                         CONFIG_ENV_SECT_SIZE    /* environment takes one sector */
-		#define CONFIG_ENV_OFFSET                       0x100000                /* (1MB For Image) environment starts here  */
+		#define CONFIG_ENV_OFFSET                       CONFIG_ENV_OFFSET_SPI
 		#define CONFIG_ENV_ADDR                         CONFIG_ENV_OFFSET
 		#define MONITOR_HEADER_LEN                      0x200
 		#define CONFIG_SYS_MONITOR_BASE                 0
@@ -341,6 +347,10 @@
 /* NAND-FLASH stuff     */
 /************************/
 #ifdef MV_NAND
+	#ifndef __ASSEMBLY__
+extern int nand_get_env_offs(void);
+	#endif /* __ASSEMBLY__ */
+
 	#define MV_NAND_PIO_MODE
 	#define MV_NAND_1CS_MODE
 	/* The following defines are masked for a38x:
@@ -370,17 +380,22 @@
 	#undef MV_NFC_DBG
 	#define CONFIG_JFFS2_NAND
 
+
+	#define CONFIG_ENV_SIZE_NAND			0x80000
+	#define CONFIG_ENV_OFFSET_NAND			nand_get_env_offs()
+	#define CONFIG_ENV_RANGE_NAND			CONFIG_ENV_SIZE_NAND * 8
+
 /* Boot from NAND settings */
 	#if defined(MV_NAND_BOOT)
 		#define CONFIG_ENV_IS_IN_NAND
 
-		#define CONFIG_ENV_SIZE                 0x80000                 /* environment takes one erase block */
+		#define CONFIG_ENV_SIZE                 CONFIG_ENV_SIZE_NAND                 /* environment takes one erase block */
 		#define CONFIG_ENV_OFFSET               nand_get_env_offs()     /* environment starts here  */
 		#define CONFIG_ENV_ADDR                 CONFIG_ENV_OFFSET
 		#define MONITOR_HEADER_LEN              0x200
 		#define CONFIG_SYS_MONITOR_BASE         0
 		#define CONFIG_SYS_MONITOR_LEN          0x80000           /* Reserve 512 kB for Monitor */
-		#define CONFIG_ENV_RANGE                CONFIG_ENV_SIZE * 8
+		#define CONFIG_ENV_RANGE                CONFIG_ENV_RANGE_NAND
 
 		#define MV_NBOOT_BASE                   0
 		#define MV_NBOOT_LEN                    (4 << 10)       /* Reserved 4KB for boot strap */
diff --git a/include/configs/avanta_lp.h b/include/configs/avanta_lp.h
index 1fbd91c..fa5baf9 100644
--- a/include/configs/avanta_lp.h
+++ b/include/configs/avanta_lp.h
@@ -271,21 +271,27 @@
 #define CONFIG_SF_DEFAULT_BUS		CONFIG_ENV_SPI_BUS
 #endif
 
+#if defined(MV_SEC_64K)
+#define CONFIG_ENV_SECT_SIZE_SPI	0x10000
+#elif defined(MV_SEC_128K)
+#define CONFIG_ENV_SECT_SIZE_SPI	0x20000
+#elif defined(MV_SEC_256K)
+#define CONFIG_ENV_SECT_SIZE_SPI	0x40000
+#endif
+
+#define	CONFIG_ENV_OFFSET_SPI		0x100000
+#define	CONFIG_ENV_SIZE_SPI		CONFIG_ENV_SECT_SIZE_SPI
+
+
 /* Boot from SPI settings */
 	#if defined(MV_SPI_BOOT)
 		#define CONFIG_ENV_IS_IN_SPI_FLASH
 
-		#if defined(MV_SEC_64K)
-			#define CONFIG_ENV_SECT_SIZE            0x10000
-		#elif defined(MV_SEC_128K)
-			#define CONFIG_ENV_SECT_SIZE            0x20000
-		#elif defined(MV_SEC_256K)
-			#define CONFIG_ENV_SECT_SIZE            0x40000
-		#endif
+		#define CONFIG_ENV_SECT_SIZE            	CONFIG_ENV_SECT_SIZE_SPI
 
-		#define CONFIG_ENV_SIZE                         CONFIG_ENV_SECT_SIZE    /* environment takes one sector */
-		#define CONFIG_ENV_OFFSET                       0x100000                /* (1MB For Image) environment starts here  */
-		#define CONFIG_ENV_ADDR                         CONFIG_ENV_OFFSET
+		#define CONFIG_ENV_SIZE                         CONFIG_ENV_SECT_SIZE_SPI    /* environment takes one sector */
+		#define CONFIG_ENV_OFFSET                       CONFIG_ENV_OFFSET_SPI       /* (1MB For Image) environment starts here  */
+		#define CONFIG_ENV_ADDR                         CONFIG_ENV_OFFSET_SPI
 		#define MONITOR_HEADER_LEN                      0x200
 		#define CONFIG_SYS_MONITOR_BASE                 0
 		#define CONFIG_SYS_MONITOR_LEN                  0x80000                 /*(512 << 10) Reserve 512 kB for Monitor */
@@ -307,6 +313,10 @@
 /* NAND-FLASH stuff     */
 /************************/
 #ifdef MV_NAND
+	#ifndef __ASSEMBLY__
+extern int nand_get_env_offs(void);
+extern int nand_get_env_range(void);
+	#endif /* __ASSEMBLY__ */
 	#define MV_NAND_PIO_MODE
 	#define MV_NAND_1CS_MODE
 	#define MV_NAND_4BIT_MODE
@@ -325,17 +335,21 @@
 	#undef CONFIG_CMD_NAND_YAFFS2
 	#undef CONFIG_MTD_NAND_YAFFS2
 
+	#define	CONFIG_ENV_SIZE_NAND			0x10000
+	#define CONFIG_ENV_OFFSET_NAND			nand_get_env_offs()
+	#define	CONFIG_ENV_RANGE_NAND			nand_get_env_range()
+
 /* Boot from NAND settings */
 	#if defined(MV_NAND_BOOT)
 		#define CONFIG_ENV_IS_IN_NAND
 
-		#define CONFIG_ENV_SIZE                 0x10000			/* environment takes one erase block */
-		#define CONFIG_ENV_OFFSET               nand_get_env_offs()     /* environment starts here  */
-		#define CONFIG_ENV_ADDR                 CONFIG_ENV_OFFSET
+		#define CONFIG_ENV_SIZE                 CONFIG_ENV_SIZE_NAND	/* environment takes one erase block */
+		#define CONFIG_ENV_OFFSET               CONFIG_ENV_OFFSET_NAND  /* environment starts here  */
+		#define CONFIG_ENV_ADDR                 CONFIG_ENV_OFFSET_NAND
 		#define MONITOR_HEADER_LEN              0x200
 		#define CONFIG_SYS_MONITOR_BASE         0
 		#define CONFIG_SYS_MONITOR_LEN          0x80000           /* Reserve 512 kB for Monitor */
-		#define CONFIG_ENV_RANGE                nand_get_env_range()
+		#define CONFIG_ENV_RANGE                CONFIG_ENV_RANGE_NAND
 
 		#define MV_NBOOT_BASE                   0
 		#define MV_NBOOT_LEN                    (4 << 10)       /* Reserved 4KB for boot strap */
diff --git a/include/configs/msys.h b/include/configs/msys.h
index 6ef5500..0b33ada 100644
--- a/include/configs/msys.h
+++ b/include/configs/msys.h
@@ -580,6 +580,11 @@
 	#define CONFIG_SYS_NAND_MAX_CHIPS 1
 
 #if defined(MV_NAND)
+
+	#ifndef __ASSEMBLY__
+extern int nand_get_env_offs(void);
+	#endif /* __ASSEMBLY__ */
+
 	#define MV_NAND_PIO_MODE
 	#define MV_NAND_1CS_MODE
 	#define MV_NAND_4BIT_MODE