3 * Jian Zhang, Texas Instruments, jzhang@ti.com.
5 * (C) Copyright 2000-2004
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9 * Andreas Heppel <aheppel@sysgo.de>
11 * See file CREDITS for list of people who contributed to this
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 #if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */
37 #include <environment.h>
38 #include <linux/stddef.h>
40 #include <linux/mtd/nand.h>
42 #if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_NAND)) == (CFG_CMD_ENV|CFG_CMD_NAND))
46 #if defined(CFG_ENV_SIZE_REDUND)
47 #error CFG_ENV_SIZE_REDUND not supported yet
50 #if defined(CFG_ENV_ADDR_REDUND)
51 #error CFG_ENV_ADDR_REDUND and CFG_ENV_IS_IN_NAND not supported yet
56 #error CONFIG_INFERNO not supported yet
59 /* references to names in cmd_nand.c */
60 #define NANDRW_READ 0x01
61 #define NANDRW_WRITE 0x00
62 #define NANDRW_JFFS2 0x02
63 extern struct nand_chip nand_dev_desc[];
64 int nand_rw (struct nand_chip* nand, int cmd,
65 size_t start, size_t len,
66 size_t * retlen, u_char * buf);
67 int nand_erase(struct nand_chip* nand, size_t ofs,
68 size_t len, int clean);
70 /* references to names in env_common.c */
71 extern uchar default_environment[];
72 extern int default_environment_size;
74 char * env_name_spec = "NAND";
77 #ifdef ENV_IS_EMBEDDED
78 extern uchar environment[];
79 env_t *env_ptr = (env_t *)(&environment[0]);
80 #else /* ! ENV_IS_EMBEDDED */
82 #endif /* ENV_IS_EMBEDDED */
86 static void use_default(void);
89 uchar env_get_char_spec (int index)
91 DECLARE_GLOBAL_DATA_PTR;
93 return ( *((uchar *)(gd->env_addr + index)) );
97 /* this is called before nand_init()
98 * so we can't read Nand to validate env data.
99 * Mark it OK for now. env_relocate() in env_common.c
100 * will call our relocate function which will does
101 * the real validation.
105 DECLARE_GLOBAL_DATA_PTR;
107 gd->env_addr = (ulong)&default_environment[0];
117 puts ("Erasing Nand...");
118 if (nand_erase(nand_dev_desc + 0, CFG_ENV_OFFSET, CFG_ENV_SIZE, 0))
121 puts ("Writing to Nand... ");
122 ret = nand_rw(nand_dev_desc + 0,
123 NANDRW_WRITE | NANDRW_JFFS2, CFG_ENV_OFFSET, CFG_ENV_SIZE,
124 &total, (u_char*)env_ptr);
125 if (ret || total != CFG_ENV_SIZE)
131 #endif /* CMD_SAVEENV */
134 void env_relocate_spec (void)
136 #if !defined(ENV_IS_EMBEDDED)
139 ret = nand_rw(nand_dev_desc + 0,
140 NANDRW_READ | NANDRW_JFFS2, CFG_ENV_OFFSET, CFG_ENV_SIZE,
141 &total, (u_char*)env_ptr);
142 if (ret || total != CFG_ENV_SIZE)
143 return use_default();
145 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
146 return use_default();
147 #endif /* ! ENV_IS_EMBEDDED */
151 static void use_default()
153 DECLARE_GLOBAL_DATA_PTR;
155 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
157 if (default_environment_size > CFG_ENV_SIZE){
158 puts ("*** Error - default environment is too large\n\n");
162 memset (env_ptr, 0, sizeof(env_t));
163 memcpy (env_ptr->data,
165 default_environment_size);
166 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
171 #endif /* CFG_ENV_IS_IN_NAND */