summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMingkai Hu <Mingkai.hu@freescale.com>2009-06-26 14:55:14 +0800
committerScott Sweeny <scott.sweeny@timesys.com>2010-09-29 17:32:29 -0400
commit78a135ffb982e9ba2278634c3e8d2d5291d2566b (patch)
tree0510eda7711098c9df264cb7a6c97a5483f92d99
parentef4d042ed65388dd0abad4de86d452cc190f3b76 (diff)
Add support for save environment variable to MMC/SD card
Whether booting from MMC/SD card or not, the environment variables can be saved on it, this patch add the operation support. Signed-off-by: Mingkai Hu <Mingkai.hu@freescale.com>
-rw-r--r--common/Makefile1
-rw-r--r--common/cmd_nvedit.c3
-rw-r--r--common/env_sdcard.c135
3 files changed, 138 insertions, 1 deletions
diff --git a/common/Makefile b/common/Makefile
index 47f6a71e2e..f6eaff93ba 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -60,6 +60,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 eb89e9e60a..ffc3c58d28 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -63,9 +63,10 @@ DECLARE_GLOBAL_DATA_PTR;
!defined(CONFIG_ENV_IS_IN_NVRAM) && \
!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_{EEPROM|FLASH|DATAFLASH|ONENAND|\
-SPI_FLASH|MG_DISK|NVRAM|NOWHERE}
+SPI_FLASH|MG_DISK|NVRAM|SDCARD|NOWHERE}
#endif
#define XMK_STR(x) #x
diff --git a/common/env_sdcard.c b/common/env_sdcard.c
new file mode 100644
index 0000000000..ce73358f20
--- /dev/null
+++ b/common/env_sdcard.c
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2009 Freescale Semiconductor, Inc.
+ *
+ * Changelog:
+ * July 2008, Intial version.
+ * June 2009, align to the MMC framework.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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>
+
+#include <environment.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* references to names in env_common.c */
+extern uchar default_environment[];
+extern int mmc_get_env_addr(int dev, u32 *env_addr);
+
+char *env_name_spec = "SD CARD";
+env_t *env_ptr;
+
+uchar env_get_char_spec(int index)
+{
+ return *((uchar *)(gd->env_addr + index));
+}
+
+static int readenv(int dev, size_t offset, u_char *buf)
+{
+ int ret;
+ struct mmc *mmc = find_mmc_device(dev);
+
+ mmc_init(mmc);
+
+ ret = mmc_read(mmc, offset, buf, CONFIG_ENV_SIZE);
+ if (ret)
+ return 1;
+
+ return 0;
+}
+
+static int writeenv(int dev, size_t offset, u_char *buf)
+{
+ int env_blknr, env_blkcnt, n;
+ uint blklen;
+ struct mmc *mmc = find_mmc_device(dev);
+
+ mmc_init(mmc);
+
+ blklen = mmc->write_bl_len;
+ env_blknr = offset / blklen;
+ env_blkcnt = CONFIG_ENV_SIZE / blklen;
+
+ n = mmc->block_dev.block_write(dev, env_blknr, env_blkcnt, buf);
+ if (n != env_blkcnt)
+ return 1;
+
+ return 0;
+}
+
+int saveenv(void)
+{
+ int ret;
+ int dev = 0;
+ u32 env_addr;
+
+ ret = mmc_get_env_addr(dev, &env_addr);
+ if (ret) {
+ puts("FAILED!\n");
+ return 1;
+ }
+
+ ret = writeenv(dev, env_addr, (u_char *) env_ptr);
+ if (ret)
+ return 1;
+
+ puts("done\n");
+ gd->env_valid = 1;
+
+ return ret;
+}
+
+void env_relocate_spec(void)
+{
+ int ret;
+ int dev = 0;
+ u32 env_addr;
+
+ ret = mmc_get_env_addr(dev, &env_addr);
+ if (ret)
+ goto err_read;
+
+ ret = readenv(dev, env_addr, (u_char *) env_ptr);
+ if (ret)
+ goto err_read;
+
+ if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
+ goto err_crc;
+
+ gd->env_valid = 1;
+
+ return;
+
+err_read:
+err_crc:
+ puts("*** Warning - bad CRC, using default environment\n\n");
+
+ set_default_env();
+}
+
+int env_init(void)
+{
+ /* eSDHC isn't usable before relocation */
+ gd->env_addr = (ulong)&default_environment[0];
+ gd->env_valid = 1;
+
+ return 0;
+}