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>
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 int nand_legacy_rw (struct nand_chip* nand, int cmd,
59 size_t start, size_t len,
60 size_t * retlen, u_char * buf);
62 /* info for NAND chips, defined in drivers/nand/nand.c */
63 extern nand_info_t nand_info[];
65 /* references to names in env_common.c */
66 extern uchar default_environment[];
67 extern int default_environment_size;
69 char * env_name_spec = "NAND";
72 #ifdef ENV_IS_EMBEDDED
73 extern uchar environment[];
74 env_t *env_ptr = (env_t *)(&environment[0]);
75 #else /* ! ENV_IS_EMBEDDED */
77 #endif /* ENV_IS_EMBEDDED */
81 static void use_default(void);
84 uchar env_get_char_spec (int index)
86 DECLARE_GLOBAL_DATA_PTR;
88 return ( *((uchar *)(gd->env_addr + index)) );
92 /* this is called before nand_init()
93 * so we can't read Nand to validate env data.
94 * Mark it OK for now. env_relocate() in env_common.c
95 * will call our relocate function which will does
96 * the real validation.
100 DECLARE_GLOBAL_DATA_PTR;
102 gd->env_addr = (ulong)&default_environment[0];
110 * The legacy NAND code saved the environment in the first NAND device i.e.,
111 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
118 puts ("Erasing Nand...");
119 if (nand_erase(&nand_info[0], CFG_ENV_OFFSET, CFG_ENV_SIZE))
122 puts ("Writing to Nand... ");
123 total = CFG_ENV_SIZE;
124 ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr);
125 if (ret || total != CFG_ENV_SIZE)
131 #endif /* CMD_SAVEENV */
135 * The legacy NAND code saved the environment in the first NAND device i.e.,
136 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
138 void env_relocate_spec (void)
140 #if !defined(ENV_IS_EMBEDDED)
144 total = CFG_ENV_SIZE;
145 ret = nand_read(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr);
146 if (ret || total != CFG_ENV_SIZE)
147 return use_default();
149 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
150 return use_default();
151 #endif /* ! ENV_IS_EMBEDDED */
155 static void use_default()
157 DECLARE_GLOBAL_DATA_PTR;
159 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
161 if (default_environment_size > CFG_ENV_SIZE){
162 puts ("*** Error - default environment is too large\n\n");
166 memset (env_ptr, 0, sizeof(env_t));
167 memcpy (env_ptr->data,
169 default_environment_size);
170 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
175 #endif /* CFG_ENV_IS_IN_NAND */