[U-Boot] [PATCH 6/7] Add support for save the env to SDcard

Mingkai Hu Mingkai.hu at freescale.com
Mon Mar 16 03:52:22 CET 2009


When after booting from SDcard, the environment variables can to
be saved on it, this patch add the operation support.

Signed-off-by: Jason Jin <Jason.jin at freescale.com>
Signed-off-by: Mingkai Hu <Mingkai.hu at freescale.com>
---
 common/Makefile     |    1 +
 common/cmd_nvedit.c |    3 +-
 common/env_common.c |    3 +-
 common/env_sdcard.c |  129 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 134 insertions(+), 2 deletions(-)
 create mode 100644 common/env_sdcard.c

diff --git a/common/Makefile b/common/Makefile
index f13cd11..c768344 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -57,6 +57,7 @@ COBJS-$(CONFIG_ENV_IS_IN_NAND) += env_nand.o
 COBJS-$(CONFIG_ENV_IS_IN_NVRAM) += env_nvram.o
 COBJS-$(CONFIG_ENV_IS_IN_ONENAND) += env_onenand.o
 COBJS-$(CONFIG_ENV_IS_IN_SPI_FLASH) += env_sf.o
+COBJS-$(CONFIG_ENV_IS_IN_SDCARD) += env_sdcard.o
 COBJS-$(CONFIG_ENV_IS_NOWHERE) += env_nowhere.o
 
 # command
diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index 68c673e..fb6ba8d 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -59,8 +59,9 @@ DECLARE_GLOBAL_DATA_PTR;
     !defined(CONFIG_ENV_IS_IN_NAND)	&& \
     !defined(CONFIG_ENV_IS_IN_ONENAND)	&& \
     !defined(CONFIG_ENV_IS_IN_SPI_FLASH)	&& \
+    !defined(CONFIG_ENV_IS_IN_SDCARD)	&& \
     !defined(CONFIG_ENV_IS_NOWHERE)
-# error Define one of CONFIG_ENV_IS_IN_{NVRAM|EEPROM|FLASH|DATAFLASH|ONENAND|SPI_FLASH|NOWHERE}
+# error Define one of CONFIG_ENV_IS_IN_{NVRAM|EEPROM|FLASH|DATAFLASH|ONENAND|SPI_FLASH|SDCARD|NOWHERE}
 #endif
 
 #define XMK_STR(x)	#x
diff --git a/common/env_common.c b/common/env_common.c
index 6be3bb0..f1aaed2 100644
--- a/common/env_common.c
+++ b/common/env_common.c
@@ -140,7 +140,8 @@ uchar default_environment[] = {
 };
 
 #if defined(CONFIG_ENV_IS_IN_NAND)		/* Environment is in Nand Flash */ \
-	|| defined(CONFIG_ENV_IS_IN_SPI_FLASH)
+	|| defined(CONFIG_ENV_IS_IN_SPI_FLASH)	\
+	|| defined(CONFIG_ENV_IS_IN_SDCARD)
 int default_environment_size = sizeof(default_environment);
 #endif
 
diff --git a/common/env_sdcard.c b/common/env_sdcard.c
new file mode 100644
index 0000000..1afcbbb
--- /dev/null
+++ b/common/env_sdcard.c
@@ -0,0 +1,129 @@
+ /*
+ * Copyright (C) 2009 Freescale Semiconductor, Inc. All rights reserved.
+ * Author: Jason.jin <jason.jin at freescale.com>
+ *
+ * Changelog:
+ * July 2008, Intial version.
+ * March 2009, Use the MMC framework's read/write function.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This file is only used for booting from SD card, During the
+ * booting up process, the environments was reading into memory
+ * together with the u-boot images. The environments locate
+ * from the last 8K of the 512k special version u-boot image.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+#include <common.h>
+#include <mmc.h>
+
+#ifdef CONFIG_ENV_IS_IN_SDCARD
+
+#include <environment.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#define SD_BLK_SIZE	512
+
+/* references to names in env_common.c */
+extern uchar default_environment[];
+extern int default_environment_size;
+
+char *env_name_spec = "SD CARD";
+env_t *env_ptr;
+
+uchar env_get_char_spec(int index)
+{
+	return *((uchar *)(gd->env_addr + index));
+}
+
+int saveenv(void)
+{
+	int env_blknr, env_blkcnt;
+	int code_offset, code_len, env_addr;
+	uchar tmp_buf[512];
+	int n, tmp_crc;
+	struct mmc *mmc = find_mmc_device(0);
+
+	mmc_init(mmc);
+
+	/* read out the first block, get the config data information */
+	env_blknr = 0;
+	env_blkcnt = 1;
+	n = mmc->block_dev.block_read(0, env_blknr, env_blkcnt, tmp_buf);
+	if (n != env_blkcnt)
+		return 1;
+
+	/* Get the Source Address, from offset 0x50 */
+	code_offset = *(unsigned long *)(tmp_buf + 0x50);
+
+	/* Get the code size from offset 0x48, the size incude the env size 8K */
+	code_len = *(unsigned long *)(tmp_buf + 0x48);
+	env_addr = code_offset + code_len - CONFIG_ENV_SIZE;
+
+	env_blknr = env_addr / SD_BLK_SIZE;
+	env_blkcnt = CONFIG_ENV_SIZE / SD_BLK_SIZE;
+
+	n = mmc->block_dev.block_write(0, env_blknr, env_blkcnt, env_ptr);
+	if (n != env_blkcnt)
+		return 1;
+
+	puts("done\n");
+	return 0;
+}
+
+void env_relocate_spec(void)
+{
+	/*Just copy to relocate place*/
+	memcpy((u8 *)env_ptr, (u8 *)CONFIG_ENV_ADDR_IN_RAM, CONFIG_ENV_SIZE);
+
+	if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc) {
+		puts("*** Warning - bad CRC, using default environment\n\n");
+
+		if (default_environment_size > CONFIG_ENV_SIZE) {
+			gd->env_valid = 0;
+			puts("*** Error - default environment is too large\n\n");
+			return;
+		}
+
+		memset(env_ptr, 0, sizeof(env_t));
+		memcpy(env_ptr->data, default_environment, default_environment_size);
+		env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
+	}
+	gd->env_valid = 1;
+}
+
+int env_init(void)
+{
+	/* The ROM code has copied the u-boot and the env to RAM
+	 * But we need to know if there're env settings there by crc check
+	 */
+
+	env_t *env_ptr_tmp = (env_t *)CONFIG_ENV_ADDR_IN_RAM;
+	if (crc32(0, env_ptr_tmp->data, ENV_SIZE) == env_ptr_tmp->crc) {
+		gd->env_addr  = (ulong)&(env_ptr_tmp->data);
+		gd->env_valid = 1;
+		return 0;
+	}
+
+	gd->env_addr  = (ulong)&default_environment[0];
+	gd->env_valid = 0;
+	return 0;
+}
+
+#endif /* CONFIG_ENV_IS_IN_SDCARD */
-- 
1.5.4



More information about the U-Boot mailing list