All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot-Users] [PATCH, resend] Support dynamic/patched NAND ENV offset
@ 2008-07-06 16:28 Harald Welte
  2008-07-07 18:47 ` Scott Wood
  0 siblings, 1 reply; 104+ messages in thread
From: Harald Welte @ 2008-07-06 16:28 UTC (permalink / raw)
  To: u-boot

Hi!

I've first sent this on Feb 17, 2007.  Unfortunately no reply was
received.  I think this is a quite useful feature, since a compile time
offset into the NAND flash for the environment just doesn't work well
with bad blocks ;)

This is the current version of the patch.  I'd love to see it included.
Thanks!

---

This patch adds support for CFG_ENV_OFFSET_PATCHED and 
CFG_ENV_OFFSET_OOB.

Both try to solve the problem of fixing the environment location in NAND flash
at compile time, which doesn't work well if the NAND flash has a bad block at
exactly that location.

CFG_ENV_OFFSET_PATCHED puts the environment in a global variable.  You can then
use the linker script to put that variable to a fixed location in the u-boot
image.  Then you can use bianry patching during the production flash process.

The idea of CFG_ENV_OFFSET_OOB is to store the environment offset in the NAND
OOB data of block 0.  We can do this in case the vendor makes a guarantee that
block 0 never is a factory-default bad block. 

Signed-off-by: Harald Welte <laforge@openmoko.org>

Index: u-boot/common/Makefile
===================================================================
--- u-boot.orig/common/Makefile
+++ u-boot/common/Makefile
@@ -55,6 +55,7 @@
 COBJS-$(CONFIG_CMD_DISPLAY) += cmd_display.o
 COBJS-$(CONFIG_CMD_DOC) += cmd_doc.o
 COBJS-$(CONFIG_CMD_DTT) += cmd_dtt.o
+COBJS-y += cmd_dynenv.o
 COBJS-y += cmd_eeprom.o
 COBJS-$(CONFIG_CMD_ELF) += cmd_elf.o
 COBJS-$(CONFIG_CMD_EXT2) += cmd_ext2.o
Index: u-boot/common/cmd_dynenv.c
===================================================================
--- /dev/null
+++ u-boot/common/cmd_dynenv.c
@@ -0,0 +1,106 @@
+/*
+ * (C) Copyright 2006-2007 OpenMoko, Inc.
+ * Author: Harald Welte <laforge@openmoko.org>
+ *
+ * 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 <command.h>
+#include <malloc.h>
+#include <environment.h>
+#include <nand.h>
+#include <asm/errno.h>
+#include "cmd_nand.h"
+
+#if defined(CFG_ENV_OFFSET_OOB)
+
+int do_dynenv(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+{
+	struct mtd_info *mtd = &nand_info[0];
+	int ret, size = 8;
+	uint8_t *buf;
+
+	char *cmd = argv[1];
+
+	buf = malloc(mtd->oobsize);
+	if (!buf)
+		return -ENOMEM;
+
+	ret = mtd->read_oob(mtd, 8, size, (size_t *) &size, (u_char *) buf);
+	if (!strcmp(cmd, "get")) {
+
+		if (buf[0] == 'E' && buf[1] == 'N' &&
+		    buf[2] == 'V' && buf[3] == '0')
+			printf("0x%08x\n", *((u_int32_t *) &buf[4]));
+		else
+			printf("No dynamic environment marker in OOB block 0\n");
+
+	} else if (!strcmp(cmd, "set")) {
+		unsigned long addr, dummy;
+
+		if (argc < 3)
+			goto usage;
+
+		buf[0] = 'E';
+		buf[1] = 'N';
+		buf[2] = 'V';
+		buf[3] = '0';
+
+		if (arg_off_size(argc-2, argv+2, mtd, &addr, &dummy) < 0) {
+			printf("Offset or partition name expected\n");
+			goto fail;
+		}
+		if (!ret) {
+			uint8_t tmp[4];
+			int i;
+
+			memcpy(&tmp, &addr, 4);
+			for (i = 0; i != 4; i++)
+				if (tmp[i] & ~buf[i+4]) {
+					printf("ERROR: erase OOB block to "
+					  "write this value\n");
+					goto fail;
+				}
+		}
+		memcpy(buf+4, &addr, 4);
+
+		printf("%02x %02x %02x %02x - %02x %02x %02x %02x\n",
+			buf[0], buf[1], buf[2], buf[3],
+			buf[4], buf[5], buf[6], buf[7]);
+
+		ret = mtd->write_oob(mtd, 8, size, (size_t *) &size, (u_char *) buf);
+		if (!ret)
+			CFG_ENV_OFFSET = addr;
+	} else
+		goto usage;
+
+	free(buf);
+	return ret;
+
+usage:
+	printf("Usage:\n%s\n", cmdtp->usage);
+fail:
+	free(buf);
+	return 1;
+}
+
+U_BOOT_CMD(dynenv, 3, 1, do_dynenv,
+	"dynenv  - dynamically placed (NAND) environment\n",
+	"dynenv set off	- set enviromnent offset\n"
+	"dynenv get	- get environment offset\n");
+
+#endif /* CFG_ENV_OFFSET_OOB */
Index: u-boot/common/env_nand.c
===================================================================
--- u-boot.orig/common/env_nand.c
+++ u-boot/common/env_nand.c
@@ -292,6 +292,33 @@
 	int crc1_ok = 0, crc2_ok = 0;
 	env_t *tmp_env1, *tmp_env2;
 
+#if defined(CFG_ENV_OFFSET_OOB)
+	struct mtd_info *mtd = &nand_info[0];
+	struct nand_chip *this = mtd->priv;
+	int buf_len;
+	uint8_t *buf;
+
+	buf_len = (1 << this->bbt_erase_shift);
+	buf_len += (buf_len >> this->page_shift) * mtd->oobsize;
+	buf = malloc(buf_len);
+	if (!buf)
+		return;
+
+	nand_read_raw(mtd, buf, 0, mtd->oobblock, mtd->oobsize);
+	if (buf[mtd->oobblock + 8 + 0] == 'E' &&
+	    buf[mtd->oobblock + 8 + 1] == 'N' &&
+	    buf[mtd->oobblock + 8 + 2] == 'V' &&
+	    buf[mtd->oobblock + 8 + 3] == '0') {
+		CFG_ENV_OFFSET = *((unsigned long *) &buf[mtd->oobblock + 8 + 4]);
+		/* fall through to the normal environment reading code below */
+		free(buf);
+		puts("Found Environment offset in OOB..\n");
+	} else {
+		free(buf);
+		return use_default();
+	}
+#endif
+
 	total = CFG_ENV_SIZE;
 
 	tmp_env1 = (env_t *) malloc(CFG_ENV_SIZE);
Index: u-boot/common/environment.c
===================================================================
--- u-boot.orig/common/environment.c
+++ u-boot/common/environment.c
@@ -29,6 +29,12 @@
 #undef	__ASSEMBLY__
 #include <environment.h>
 
+#if defined(CFG_ENV_OFFSET_PATCHED)
+unsigned long env_offset = CFG_ENV_OFFSET_PATCHED;
+#elif defined(CFG_ENV_OFFSET_OOB)
+unsigned long env_offset = CFG_ENV_OFFSET_OOB;
+#endif
+
 /*
  * Handle HOSTS that have prepended
  * crap on symbol names, not TARGETS.
Index: u-boot/include/environment.h
===================================================================
--- u-boot.orig/include/environment.h
+++ u-boot/include/environment.h
@@ -70,6 +70,10 @@
 #endif	/* CFG_ENV_IS_IN_FLASH */
 
 #if defined(CFG_ENV_IS_IN_NAND)
+#if defined(CFG_ENV_OFFSET_PATCHED) || defined(CFG_ENV_OFFSET_OOB)
+extern unsigned long env_offset;
+#define CFG_ENV_OFFSET env_offset
+#else
 # ifndef CFG_ENV_OFFSET
 #  error "Need to define CFG_ENV_OFFSET when using CFG_ENV_IS_IN_NAND"
 # endif
@@ -82,6 +86,7 @@
 # ifdef CFG_ENV_IS_EMBEDDED
 #  define ENV_IS_EMBEDDED	1
 # endif
+#endif /* CFG_ENV_NAND_PATCHED */
 #endif /* CFG_ENV_IS_IN_NAND */
 
 #ifdef USE_HOSTCC
Index: u-boot/common/cmd_nand.h
===================================================================
--- /dev/null
+++ u-boot/common/cmd_nand.h
@@ -0,0 +1,33 @@
+/*
+ * cmd_nand.h - Convenience functions
+ *
+ * (C) Copyright 2006-2007 OpenMoko, Inc.
+ * Author: Werner Almesberger <werner@openmoko.org>
+ *
+ * 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
+ */
+
+#ifndef CMD_NAND_H
+#define CMD_NAND_H
+
+#include <nand.h>
+
+
+/* common/cmd_nand.c */
+int arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off,
+  ulong *size);
+
+#endif /* CMD_NAND_H */
Index: u-boot/common/cmd_nand.c
===================================================================
--- u-boot.orig/common/cmd_nand.c
+++ u-boot/common/cmd_nand.c
@@ -90,7 +90,7 @@
 	return (*p != '\0' && *endptr == '\0') ? 1 : 0;
 }
 
-static int
+int
 arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
 {
 	int idx = nand_curr_device;
-- 
- Harald Welte <laforge@openmoko.org>          	        http://openmoko.org/
============================================================================
Software for the world's first truly open Free Software mobile phone

^ permalink raw reply	[flat|nested] 104+ messages in thread

end of thread, other threads:[~2010-09-18 19:42 UTC | newest]

Thread overview: 104+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-06 16:28 [U-Boot-Users] [PATCH, resend] Support dynamic/patched NAND ENV offset Harald Welte
2008-07-07 18:47 ` Scott Wood
2008-07-08  0:09   ` Harald Welte
2008-07-08 16:05     ` Scott Wood
2008-07-08 21:12       ` Wolfgang Denk
2008-07-09  0:23         ` Harald Welte
2008-07-09  7:05           ` Wolfgang Denk
2008-07-09  7:25             ` Harald Welte
2008-07-09  8:04               ` Wolfgang Denk
2008-07-09 12:13                 ` Harald Welte
2010-06-01 20:23                 ` [U-Boot] mtdparts: add bad-block skipping Ben Gardiner
2010-06-01 22:20                   ` Wolfgang Denk
2010-06-01 22:34                   ` Wolfgang Denk
2010-06-02  5:08                     ` Ben Gardiner
2010-06-02 15:58                   ` [U-Boot] [PATCH 0/4 v2] " Ben Gardiner
2010-07-05 21:43                     ` [U-Boot] [PATCH v3 0/4] " Ben Gardiner
2010-07-12 19:06                       ` Ben Gardiner
2010-08-09 20:43                       ` [U-Boot] [PATCH v4 " Ben Gardiner
2010-08-30 17:38                         ` [U-Boot] [PATCH v5 0/5] " Ben Gardiner
2010-08-30 17:38                           ` [U-Boot] [PATCH v5 2/5] mtd: add an mtd method for get_len_incl_bad() Ben Gardiner
2010-08-30 20:57                             ` Scott Wood
2010-08-31 13:50                               ` Ben Gardiner
2010-08-31 21:48                             ` [U-Boot] [PATCH v6 " Ben Gardiner
     [not found]                           ` <1283185640-0-git-send-email-bengardiner@nanometrics.ca>
2010-08-30 17:38                             ` [U-Boot] [PATCH v5 3/5] mtdparts: show net size in mtdparts list Ben Gardiner
2010-08-30 20:50                               ` Scott Wood
2010-08-31 13:51                                 ` Ben Gardiner
2010-08-31 15:57                                   ` Scott Wood
2010-08-31 21:48                               ` [U-Boot] [PATCH v6 " Ben Gardiner
2010-08-31 21:47                           ` [U-Boot] [PATCH v6 0/5] mtdparts: add bad-block skipping Ben Gardiner
2010-09-09 20:54                             ` Scott Wood
2010-08-09 20:43                       ` [U-Boot] [PATCH v4 1/4] mtdparts: regroup calls to get_mtd_device_nm Ben Gardiner
2010-08-09 20:43                       ` [U-Boot] [PATCH v4 2/4] mtdparts: show net size in mtdparts list Ben Gardiner
2010-08-26 18:57                         ` Scott Wood
2010-08-27 13:51                           ` Ben Gardiner
2010-08-27 15:44                           ` Ben Gardiner
2010-08-27 16:02                             ` Scott Wood
2010-08-27 16:45                               ` Ben Gardiner
2010-08-09 20:43                       ` [U-Boot] [PATCH v4 3/4] mtdparts: add new sub-command "spread" Ben Gardiner
2010-08-26 21:12                         ` Scott Wood
2010-08-27 13:51                           ` Ben Gardiner
2010-08-27 21:36                             ` Ben Gardiner
2010-08-27 21:46                               ` Scott Wood
2010-08-27 21:52                                 ` Ben Gardiner
2010-08-27 21:59                                 ` Scott Wood
2010-08-28  3:59                                   ` Ben Gardiner
2010-08-30 20:24                                     ` Scott Wood
2010-08-30 20:30                                       ` Ben Gardiner
2010-08-30 17:38                         ` [U-Boot] [PATCH v5 4/5] " Ben Gardiner
2010-08-30 21:01                           ` Scott Wood
2010-08-30 21:05                             ` Scott Wood
2010-08-31 13:51                             ` Ben Gardiner
2010-08-31 21:48                           ` [U-Boot] [PATCH v6 " Ben Gardiner
2010-08-09 20:44                       ` [U-Boot] [PATCH v4 4/4] mtdparts: new add.spread: add part skipping bad blocks Ben Gardiner
2010-08-26 22:26                         ` Scott Wood
2010-08-27 13:52                           ` Ben Gardiner
2010-08-30 17:39                         ` [U-Boot] [PATCH v5 5/5] " Ben Gardiner
2010-08-31 21:48                           ` [U-Boot] [PATCH v6 " Ben Gardiner
2010-07-05 21:43                     ` [U-Boot] [PATCH v3 1/4] mtdparts: regroup calls to get_mtd_device_nm Ben Gardiner
2010-08-30 17:38                       ` [U-Boot] [PATCH v5 1/5] " Ben Gardiner
2010-08-31 21:48                         ` [U-Boot] [PATCH v6 " Ben Gardiner
2010-07-05 21:43                     ` [U-Boot] [PATCH v3 2/4] mtdparts: show net size in mtdparts list Ben Gardiner
2010-08-07 20:08                       ` Wolfgang Denk
2010-08-08  4:06                         ` Harald Welte
2010-08-08 13:16                           ` Wolfgang Denk
2010-08-09 14:45                         ` Ben Gardiner
2010-09-18 19:42                           ` Wolfgang Denk
2010-07-05 21:43                     ` [U-Boot] [PATCH v3 3/4] mtdparts: add new sub-command "spread" Ben Gardiner
2010-08-07 20:12                       ` Wolfgang Denk
2010-07-05 21:43                     ` [U-Boot] [PATCH v3 4/4] mtdparts: new add.e: add part skipping bad blocks Ben Gardiner
2010-08-07 20:16                       ` Wolfgang Denk
2010-06-02 15:58                   ` [U-Boot] [PATCH 1/4 v2] mtdparts: regroup calls to get_mtd_device_nm Ben Gardiner
2010-06-02 15:58                   ` [U-Boot] [PATCH 2/4 v2] mtdparts: show net size in mtdparts list Ben Gardiner
2010-06-02 15:58                   ` [U-Boot] [PATCH 3/4 v2] mtdparts: add new sub-command "spread" Ben Gardiner
2010-06-02 15:58                   ` [U-Boot] [PATCH 4/4 v2] mtdparts: new add.e: add part skipping bad blocks Ben Gardiner
2010-08-09 18:25                     ` Scott Wood
2010-08-09 18:39                       ` Ben Gardiner
2010-08-09 18:51                         ` Scott Wood
2010-06-01 20:23                 ` [U-Boot] [PATCH 1/4] mtdparts: regroup calls to get_mtd_device_nm Ben Gardiner
2010-06-02  7:06                   ` Stefan Roese
2010-06-01 20:23                 ` [U-Boot] [PATCH 2/4] mtdparts: show net size in mtdparts list Ben Gardiner
2010-06-02  7:15                   ` Stefan Roese
2010-06-01 20:23                 ` [U-Boot] [PATCH 3/4] mtdparts: add new sub-command "spread" Ben Gardiner
2010-06-01 20:23                 ` [U-Boot] [PATCH 4/4] mtdparts: new add.e: add part skipping bad blocks Ben Gardiner
2008-07-09  0:18       ` [U-Boot-Users] [PATCH, resend] Support dynamic/patched NAND ENV offset Harald Welte
2008-07-09  5:28       ` Harald Welte
2008-07-09  7:07         ` Harald Welte
2008-07-09  8:11       ` [U-Boot-Users] [PATCH] " Harald Welte
2008-07-11 17:28         ` Scott Wood
2010-05-17 21:04           ` [U-Boot] [PATCH] NAND: Support dynamic location of enviromnent (CONFIG_ENV_OFFSET_OOB) Ben Gardiner
2010-05-26 22:58             ` Scott Wood
2010-05-26 23:38               ` Ben Gardiner
2010-05-31 21:29               ` [U-Boot] [PATCH v2] NAND: environment offset in OOB (CONFIG_ENV_OFFSET_OOB) Ben Gardiner
2010-06-30 21:32                 ` [U-Boot] [PATCH v3] " Ben Gardiner
2010-06-30 21:41                   ` Wolfgang Denk
2010-07-01  4:09                     ` Ben Gardiner
2010-07-01  3:37                   ` Vipin KUMAR
2010-07-01  5:29                     ` Ben Gardiner
2010-07-01  7:17                   ` Harald Welte
2010-07-05 13:57                     ` Ben Gardiner
2010-07-05 17:27                     ` [U-Boot] [PATCH v4] " Ben Gardiner
2010-07-12 16:17                       ` Ben Gardiner
2010-07-12 16:19                         ` Scott Wood
2010-07-13 19:26                       ` Scott Wood
2010-07-13 21:17                         ` Ben Gardiner

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.