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>
39 #include <linux/mtd/nand.h>
41 #if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_NAND)) == (CFG_CMD_ENV|CFG_CMD_NAND))
45 #if defined(CFG_ENV_SIZE_REDUND)
46 #error CFG_ENV_SIZE_REDUND not supported yet
49 #if defined(CFG_ENV_ADDR_REDUND)
50 #error CFG_ENV_ADDR_REDUND and CFG_ENV_IS_IN_NAND not supported yet
55 #error CONFIG_INFERNO not supported yet
58 /* references to names in cmd_nand.c */
59 #define NANDRW_READ 0x01
60 #define NANDRW_WRITE 0x00
61 #define NANDRW_JFFS2 0x02
62 extern struct nand_chip nand_dev_desc[];
63 int nand_rw (struct nand_chip* nand, int cmd,
64 size_t start, size_t len,
65 size_t * retlen, u_char * buf);
66 int nand_erase(struct nand_chip* nand, size_t ofs,
67 size_t len, int clean);
69 /* references to names in env_common.c */
70 extern uchar default_environment[];
71 extern int default_environment_size;
73 char * env_name_spec = "NAND";
76 #ifdef ENV_IS_EMBEDDED
77 extern uchar environment[];
78 env_t *env_ptr = (env_t *)(&environment[0]);
79 #else /* ! ENV_IS_EMBEDDED */
81 #endif /* ENV_IS_EMBEDDED */
85 static void use_default(void);
88 uchar env_get_char_spec (int index)
90 DECLARE_GLOBAL_DATA_PTR;
92 return ( *((uchar *)(gd->env_addr + index)) );
96 /* this is called before nand_init()
97 * so we can't read Nand to validate env data.
98 * Mark it OK for now. env_relocate() in env_common.c
99 * will call our relocate function which will does
100 * the real validation.
104 DECLARE_GLOBAL_DATA_PTR;
106 gd->env_addr = (ulong)&default_environment[0];
116 puts ("Erasing Nand...");
117 if (nand_erase(nand_dev_desc + 0, CFG_ENV_OFFSET, CFG_ENV_SIZE, 0))
120 puts ("Writing to Nand... ");
121 ret = nand_rw(nand_dev_desc + 0,
122 NANDRW_WRITE | NANDRW_JFFS2, CFG_ENV_OFFSET, CFG_ENV_SIZE,
123 &total, (u_char*)env_ptr);
124 if (ret || total != CFG_ENV_SIZE)
130 #endif /* CMD_SAVEENV */
133 void env_relocate_spec (void)
135 #if !defined(ENV_IS_EMBEDDED)
138 ret = nand_rw(nand_dev_desc + 0,
139 NANDRW_READ | NANDRW_JFFS2, CFG_ENV_OFFSET, CFG_ENV_SIZE,
140 &total, (u_char*)env_ptr);
141 if (ret || total != CFG_ENV_SIZE)
142 return use_default();
144 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
145 return use_default();
146 #endif /* ! ENV_IS_EMBEDDED */
150 static void use_default()
152 DECLARE_GLOBAL_DATA_PTR;
154 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
156 if (default_environment_size > CFG_ENV_SIZE){
157 puts ("*** Error - default environment is too large\n\n");
161 memset (env_ptr, 0, sizeof(env_t));
162 memcpy (env_ptr->data,
164 default_environment_size);
165 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
170 #endif /* CFG_ENV_IS_IN_NAND */