2004-06-09 14:58:14 +00:00
|
|
|
/*
|
2008-05-30 20:05:28 +00:00
|
|
|
* (C) Copyright 2008
|
|
|
|
* Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
|
|
|
|
*
|
2004-06-09 14:58:14 +00:00
|
|
|
* (C) Copyright 2004
|
|
|
|
* Jian Zhang, Texas Instruments, jzhang@ti.com.
|
|
|
|
|
2006-09-12 18:19:10 +00:00
|
|
|
* (C) Copyright 2000-2006
|
2004-06-09 14:58:14 +00:00
|
|
|
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
|
|
|
*
|
|
|
|
* (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
|
|
|
|
* Andreas Heppel <aheppel@sysgo.de>
|
|
|
|
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* #define DEBUG */
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <environment.h>
|
|
|
|
#include <linux/stddef.h>
|
2006-03-20 17:02:44 +00:00
|
|
|
#include <malloc.h>
|
2006-03-05 17:57:33 +00:00
|
|
|
#include <nand.h>
|
2010-07-05 17:27:07 +00:00
|
|
|
#include <asm/errno.h>
|
2004-06-09 14:58:14 +00:00
|
|
|
|
2009-01-29 00:08:14 +00:00
|
|
|
#if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
|
2004-06-09 14:58:14 +00:00
|
|
|
#define CMD_SAVEENV
|
2008-09-10 20:48:06 +00:00
|
|
|
#elif defined(CONFIG_ENV_OFFSET_REDUND)
|
2009-01-29 00:08:14 +00:00
|
|
|
#error Cannot use CONFIG_ENV_OFFSET_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
|
2004-06-09 14:58:14 +00:00
|
|
|
#endif
|
|
|
|
|
2008-09-10 20:48:06 +00:00
|
|
|
#if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
|
|
|
|
#error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
|
2004-06-09 14:58:14 +00:00
|
|
|
#endif
|
|
|
|
|
2008-09-10 20:48:06 +00:00
|
|
|
#ifndef CONFIG_ENV_RANGE
|
|
|
|
#define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
|
2008-05-30 20:05:28 +00:00
|
|
|
#endif
|
|
|
|
|
2004-06-09 14:58:14 +00:00
|
|
|
/* references to names in env_common.c */
|
|
|
|
extern uchar default_environment[];
|
|
|
|
|
|
|
|
char * env_name_spec = "NAND";
|
|
|
|
|
|
|
|
|
2009-05-18 14:07:22 +00:00
|
|
|
#if defined(ENV_IS_EMBEDDED)
|
2004-06-09 14:58:14 +00:00
|
|
|
extern uchar environment[];
|
|
|
|
env_t *env_ptr = (env_t *)(&environment[0]);
|
2009-05-18 14:07:22 +00:00
|
|
|
#elif defined(CONFIG_NAND_ENV_DST)
|
|
|
|
env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
|
2004-06-09 14:58:14 +00:00
|
|
|
#else /* ! ENV_IS_EMBEDDED */
|
2004-06-19 21:19:10 +00:00
|
|
|
env_t *env_ptr = 0;
|
2004-06-09 14:58:14 +00:00
|
|
|
#endif /* ENV_IS_EMBEDDED */
|
|
|
|
|
|
|
|
|
|
|
|
/* local functions */
|
2006-09-12 18:19:10 +00:00
|
|
|
#if !defined(ENV_IS_EMBEDDED)
|
2004-06-09 14:58:14 +00:00
|
|
|
static void use_default(void);
|
2006-09-12 18:19:10 +00:00
|
|
|
#endif
|
2004-06-09 14:58:14 +00:00
|
|
|
|
2006-03-31 16:32:53 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
2004-06-09 14:58:14 +00:00
|
|
|
|
|
|
|
uchar env_get_char_spec (int index)
|
|
|
|
{
|
|
|
|
return ( *((uchar *)(gd->env_addr + index)) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* this is called before nand_init()
|
|
|
|
* so we can't read Nand to validate env data.
|
|
|
|
* Mark it OK for now. env_relocate() in env_common.c
|
2008-06-22 14:13:46 +00:00
|
|
|
* will call our relocate function which does the real
|
|
|
|
* validation.
|
2006-09-12 18:19:10 +00:00
|
|
|
*
|
|
|
|
* When using a NAND boot image (like sequoia_nand), the environment
|
|
|
|
* can be embedded or attached to the U-Boot image in NAND flash. This way
|
|
|
|
* the SPL loads not only the U-Boot image from NAND but also the
|
|
|
|
* environment.
|
2004-06-09 14:58:14 +00:00
|
|
|
*/
|
|
|
|
int env_init(void)
|
|
|
|
{
|
2009-05-18 14:07:22 +00:00
|
|
|
#if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
|
2006-09-12 18:19:10 +00:00
|
|
|
int crc1_ok = 0, crc2_ok = 0;
|
2009-05-18 14:07:22 +00:00
|
|
|
env_t *tmp_env1;
|
|
|
|
|
|
|
|
#ifdef CONFIG_ENV_OFFSET_REDUND
|
|
|
|
env_t *tmp_env2;
|
2006-09-12 18:19:10 +00:00
|
|
|
|
2008-09-10 20:48:06 +00:00
|
|
|
tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
|
2009-05-18 14:07:22 +00:00
|
|
|
crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tmp_env1 = env_ptr;
|
2006-09-12 18:19:10 +00:00
|
|
|
|
|
|
|
crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
|
|
|
|
|
2009-05-18 14:07:22 +00:00
|
|
|
if (!crc1_ok && !crc2_ok) {
|
|
|
|
gd->env_addr = 0;
|
2006-09-12 18:19:10 +00:00
|
|
|
gd->env_valid = 0;
|
2009-05-18 14:07:22 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
} else if (crc1_ok && !crc2_ok) {
|
2006-09-12 18:19:10 +00:00
|
|
|
gd->env_valid = 1;
|
2009-05-18 14:07:22 +00:00
|
|
|
}
|
|
|
|
#ifdef CONFIG_ENV_OFFSET_REDUND
|
|
|
|
else if (!crc1_ok && crc2_ok) {
|
2006-09-12 18:19:10 +00:00
|
|
|
gd->env_valid = 2;
|
2009-05-18 14:07:22 +00:00
|
|
|
} else {
|
2006-09-12 18:19:10 +00:00
|
|
|
/* both ok - check serial */
|
|
|
|
if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
|
|
|
|
gd->env_valid = 2;
|
|
|
|
else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
|
|
|
|
gd->env_valid = 1;
|
|
|
|
else if(tmp_env1->flags > tmp_env2->flags)
|
|
|
|
gd->env_valid = 1;
|
|
|
|
else if(tmp_env2->flags > tmp_env1->flags)
|
|
|
|
gd->env_valid = 2;
|
|
|
|
else /* flags are equal - almost impossible */
|
|
|
|
gd->env_valid = 1;
|
|
|
|
}
|
|
|
|
|
2009-05-18 14:07:22 +00:00
|
|
|
if (gd->env_valid == 2)
|
|
|
|
env_ptr = tmp_env2;
|
|
|
|
else
|
|
|
|
#endif
|
2006-09-12 18:19:10 +00:00
|
|
|
if (gd->env_valid == 1)
|
|
|
|
env_ptr = tmp_env1;
|
2009-05-18 14:07:22 +00:00
|
|
|
|
|
|
|
gd->env_addr = (ulong)env_ptr->data;
|
|
|
|
|
|
|
|
#else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
|
2006-03-20 17:02:44 +00:00
|
|
|
gd->env_addr = (ulong)&default_environment[0];
|
2004-06-09 14:58:14 +00:00
|
|
|
gd->env_valid = 1;
|
2009-05-18 14:07:22 +00:00
|
|
|
#endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
|
2004-06-09 14:58:14 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CMD_SAVEENV
|
2006-03-05 17:57:33 +00:00
|
|
|
/*
|
|
|
|
* The legacy NAND code saved the environment in the first NAND device i.e.,
|
|
|
|
* nand_dev_desc + 0. This is also the behaviour using the new NAND code.
|
|
|
|
*/
|
2008-05-30 20:05:28 +00:00
|
|
|
int writeenv(size_t offset, u_char *buf)
|
|
|
|
{
|
2008-09-10 20:48:06 +00:00
|
|
|
size_t end = offset + CONFIG_ENV_RANGE;
|
2008-05-30 20:05:28 +00:00
|
|
|
size_t amount_saved = 0;
|
2008-07-31 10:38:26 +00:00
|
|
|
size_t blocksize, len;
|
2008-05-30 20:05:28 +00:00
|
|
|
|
|
|
|
u_char *char_ptr;
|
|
|
|
|
|
|
|
blocksize = nand_info[0].erasesize;
|
2008-09-10 20:48:06 +00:00
|
|
|
len = min(blocksize, CONFIG_ENV_SIZE);
|
2008-05-30 20:05:28 +00:00
|
|
|
|
2008-09-10 20:48:06 +00:00
|
|
|
while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
|
2008-05-30 20:05:28 +00:00
|
|
|
if (nand_block_isbad(&nand_info[0], offset)) {
|
|
|
|
offset += blocksize;
|
|
|
|
} else {
|
|
|
|
char_ptr = &buf[amount_saved];
|
2008-07-31 10:38:26 +00:00
|
|
|
if (nand_write(&nand_info[0], offset, &len,
|
2008-05-30 20:05:28 +00:00
|
|
|
char_ptr))
|
|
|
|
return 1;
|
|
|
|
offset += blocksize;
|
2008-07-31 10:38:26 +00:00
|
|
|
amount_saved += len;
|
2008-05-30 20:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-10 20:48:06 +00:00
|
|
|
if (amount_saved != CONFIG_ENV_SIZE)
|
2008-05-30 20:05:28 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-09-10 20:48:06 +00:00
|
|
|
#ifdef CONFIG_ENV_OFFSET_REDUND
|
2004-06-09 14:58:14 +00:00
|
|
|
int saveenv(void)
|
|
|
|
{
|
2006-03-24 14:43:16 +00:00
|
|
|
int ret = 0;
|
2008-05-30 20:05:28 +00:00
|
|
|
nand_erase_options_t nand_erase_options;
|
2006-03-20 17:02:44 +00:00
|
|
|
|
|
|
|
env_ptr->flags++;
|
|
|
|
|
2008-09-10 20:48:06 +00:00
|
|
|
nand_erase_options.length = CONFIG_ENV_RANGE;
|
2008-05-30 20:05:28 +00:00
|
|
|
nand_erase_options.quiet = 0;
|
|
|
|
nand_erase_options.jffs2 = 0;
|
|
|
|
nand_erase_options.scrub = 0;
|
|
|
|
|
2008-09-10 20:48:06 +00:00
|
|
|
if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
|
2008-05-30 20:05:28 +00:00
|
|
|
return 1;
|
2006-03-20 17:02:44 +00:00
|
|
|
if(gd->env_valid == 1) {
|
2008-05-30 20:05:28 +00:00
|
|
|
puts ("Erasing redundant Nand...\n");
|
2008-09-10 20:48:06 +00:00
|
|
|
nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
|
2008-05-30 20:05:28 +00:00
|
|
|
if (nand_erase_opts(&nand_info[0], &nand_erase_options))
|
2006-03-20 17:02:44 +00:00
|
|
|
return 1;
|
2008-05-30 20:05:28 +00:00
|
|
|
|
2006-03-20 17:02:44 +00:00
|
|
|
puts ("Writing to redundant Nand... ");
|
2008-09-10 20:48:06 +00:00
|
|
|
ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) env_ptr);
|
2006-03-20 17:02:44 +00:00
|
|
|
} else {
|
2008-05-30 20:05:28 +00:00
|
|
|
puts ("Erasing Nand...\n");
|
2008-09-10 20:48:06 +00:00
|
|
|
nand_erase_options.offset = CONFIG_ENV_OFFSET;
|
2008-05-30 20:05:28 +00:00
|
|
|
if (nand_erase_opts(&nand_info[0], &nand_erase_options))
|
2006-03-20 17:02:44 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
puts ("Writing to Nand... ");
|
2008-09-10 20:48:06 +00:00
|
|
|
ret = writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
|
2006-03-20 17:02:44 +00:00
|
|
|
}
|
2008-05-30 20:05:28 +00:00
|
|
|
if (ret) {
|
|
|
|
puts("FAILED!\n");
|
2006-03-20 17:02:44 +00:00
|
|
|
return 1;
|
2008-05-30 20:05:28 +00:00
|
|
|
}
|
2006-03-20 17:02:44 +00:00
|
|
|
|
|
|
|
puts ("done\n");
|
|
|
|
gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
|
|
|
|
return ret;
|
|
|
|
}
|
2008-09-10 20:48:06 +00:00
|
|
|
#else /* ! CONFIG_ENV_OFFSET_REDUND */
|
2006-03-20 17:02:44 +00:00
|
|
|
int saveenv(void)
|
|
|
|
{
|
2006-03-11 21:53:33 +00:00
|
|
|
int ret = 0;
|
2008-06-16 12:58:07 +00:00
|
|
|
nand_erase_options_t nand_erase_options;
|
2008-06-28 21:34:37 +00:00
|
|
|
|
2008-09-10 20:48:06 +00:00
|
|
|
nand_erase_options.length = CONFIG_ENV_RANGE;
|
2008-05-30 20:05:28 +00:00
|
|
|
nand_erase_options.quiet = 0;
|
|
|
|
nand_erase_options.jffs2 = 0;
|
|
|
|
nand_erase_options.scrub = 0;
|
2008-09-10 20:48:06 +00:00
|
|
|
nand_erase_options.offset = CONFIG_ENV_OFFSET;
|
2008-05-30 20:05:28 +00:00
|
|
|
|
2008-09-10 20:48:06 +00:00
|
|
|
if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
|
2008-05-30 20:05:28 +00:00
|
|
|
return 1;
|
|
|
|
puts ("Erasing Nand...\n");
|
|
|
|
if (nand_erase_opts(&nand_info[0], &nand_erase_options))
|
2006-03-05 17:57:33 +00:00
|
|
|
return 1;
|
2004-06-09 14:58:14 +00:00
|
|
|
|
|
|
|
puts ("Writing to Nand... ");
|
2008-09-10 20:48:06 +00:00
|
|
|
if (writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr)) {
|
2008-05-30 20:05:28 +00:00
|
|
|
puts("FAILED!\n");
|
2004-06-09 14:58:14 +00:00
|
|
|
return 1;
|
2008-05-30 20:05:28 +00:00
|
|
|
}
|
2004-06-09 14:58:14 +00:00
|
|
|
|
2006-03-05 17:57:33 +00:00
|
|
|
puts ("done\n");
|
|
|
|
return ret;
|
2004-06-09 14:58:14 +00:00
|
|
|
}
|
2008-09-10 20:48:06 +00:00
|
|
|
#endif /* CONFIG_ENV_OFFSET_REDUND */
|
2004-06-09 14:58:14 +00:00
|
|
|
#endif /* CMD_SAVEENV */
|
|
|
|
|
2008-05-30 20:05:28 +00:00
|
|
|
int readenv (size_t offset, u_char * buf)
|
|
|
|
{
|
2008-09-10 20:48:06 +00:00
|
|
|
size_t end = offset + CONFIG_ENV_RANGE;
|
2008-05-30 20:05:28 +00:00
|
|
|
size_t amount_loaded = 0;
|
2008-07-31 10:38:26 +00:00
|
|
|
size_t blocksize, len;
|
2008-05-30 20:05:28 +00:00
|
|
|
|
|
|
|
u_char *char_ptr;
|
|
|
|
|
|
|
|
blocksize = nand_info[0].erasesize;
|
2010-08-12 03:42:26 +00:00
|
|
|
if (!blocksize)
|
|
|
|
return 1;
|
2008-09-10 20:48:06 +00:00
|
|
|
len = min(blocksize, CONFIG_ENV_SIZE);
|
2008-05-30 20:05:28 +00:00
|
|
|
|
2008-09-10 20:48:06 +00:00
|
|
|
while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
|
2008-05-30 20:05:28 +00:00
|
|
|
if (nand_block_isbad(&nand_info[0], offset)) {
|
|
|
|
offset += blocksize;
|
|
|
|
} else {
|
|
|
|
char_ptr = &buf[amount_loaded];
|
2008-07-31 10:38:26 +00:00
|
|
|
if (nand_read(&nand_info[0], offset, &len, char_ptr))
|
2008-05-30 20:05:28 +00:00
|
|
|
return 1;
|
|
|
|
offset += blocksize;
|
2008-07-31 10:38:26 +00:00
|
|
|
amount_loaded += len;
|
2008-05-30 20:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-10 20:48:06 +00:00
|
|
|
if (amount_loaded != CONFIG_ENV_SIZE)
|
2008-05-30 20:05:28 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-05 17:27:07 +00:00
|
|
|
#ifdef CONFIG_ENV_OFFSET_OOB
|
|
|
|
int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
|
|
|
|
{
|
|
|
|
struct mtd_oob_ops ops;
|
|
|
|
uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ops.datbuf = NULL;
|
|
|
|
ops.mode = MTD_OOB_AUTO;
|
|
|
|
ops.ooboffs = 0;
|
|
|
|
ops.ooblen = ENV_OFFSET_SIZE;
|
|
|
|
ops.oobbuf = (void *) oob_buf;
|
|
|
|
|
|
|
|
ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
|
2010-07-12 23:17:40 +00:00
|
|
|
if (ret) {
|
|
|
|
printf("error reading OOB block 0\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2010-07-05 17:27:07 +00:00
|
|
|
|
2010-07-12 23:17:40 +00:00
|
|
|
if (oob_buf[0] == ENV_OOB_MARKER) {
|
|
|
|
*result = oob_buf[1] * nand->erasesize;
|
|
|
|
} else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
|
|
|
|
*result = oob_buf[1];
|
2010-07-05 17:27:07 +00:00
|
|
|
} else {
|
2010-07-12 23:17:40 +00:00
|
|
|
printf("No dynamic environment marker in OOB block 0\n");
|
|
|
|
return -ENOENT;
|
2010-07-05 17:27:07 +00:00
|
|
|
}
|
2010-07-12 23:17:40 +00:00
|
|
|
|
|
|
|
return 0;
|
2010-07-05 17:27:07 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-09-10 20:48:06 +00:00
|
|
|
#ifdef CONFIG_ENV_OFFSET_REDUND
|
2006-03-20 17:02:44 +00:00
|
|
|
void env_relocate_spec (void)
|
|
|
|
{
|
|
|
|
#if !defined(ENV_IS_EMBEDDED)
|
2006-03-24 14:43:16 +00:00
|
|
|
int crc1_ok = 0, crc2_ok = 0;
|
2006-03-20 17:02:44 +00:00
|
|
|
env_t *tmp_env1, *tmp_env2;
|
|
|
|
|
2008-09-10 20:48:06 +00:00
|
|
|
tmp_env1 = (env_t *) malloc(CONFIG_ENV_SIZE);
|
|
|
|
tmp_env2 = (env_t *) malloc(CONFIG_ENV_SIZE);
|
2006-03-20 17:02:44 +00:00
|
|
|
|
2010-01-17 04:50:26 +00:00
|
|
|
if ((tmp_env1 == NULL) || (tmp_env2 == NULL)) {
|
|
|
|
puts("Can't allocate buffers for environment\n");
|
|
|
|
free (tmp_env1);
|
|
|
|
free (tmp_env2);
|
|
|
|
return use_default();
|
|
|
|
}
|
|
|
|
|
2008-09-10 20:48:06 +00:00
|
|
|
if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
|
2008-05-30 20:05:28 +00:00
|
|
|
puts("No Valid Environment Area Found\n");
|
2008-09-10 20:48:06 +00:00
|
|
|
if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
|
2008-05-30 20:05:28 +00:00
|
|
|
puts("No Valid Reundant Environment Area Found\n");
|
2006-03-20 17:02:44 +00:00
|
|
|
|
|
|
|
crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
|
|
|
|
crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
|
|
|
|
|
2009-01-26 21:08:17 +00:00
|
|
|
if(!crc1_ok && !crc2_ok) {
|
|
|
|
free(tmp_env1);
|
|
|
|
free(tmp_env2);
|
2006-03-20 17:02:44 +00:00
|
|
|
return use_default();
|
2009-01-26 21:08:17 +00:00
|
|
|
} else if(crc1_ok && !crc2_ok)
|
2006-03-20 17:02:44 +00:00
|
|
|
gd->env_valid = 1;
|
|
|
|
else if(!crc1_ok && crc2_ok)
|
|
|
|
gd->env_valid = 2;
|
|
|
|
else {
|
|
|
|
/* both ok - check serial */
|
|
|
|
if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
|
|
|
|
gd->env_valid = 2;
|
|
|
|
else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
|
|
|
|
gd->env_valid = 1;
|
|
|
|
else if(tmp_env1->flags > tmp_env2->flags)
|
|
|
|
gd->env_valid = 1;
|
|
|
|
else if(tmp_env2->flags > tmp_env1->flags)
|
|
|
|
gd->env_valid = 2;
|
|
|
|
else /* flags are equal - almost impossible */
|
|
|
|
gd->env_valid = 1;
|
2004-06-09 14:58:14 +00:00
|
|
|
|
2006-03-20 17:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(env_ptr);
|
|
|
|
if(gd->env_valid == 1) {
|
|
|
|
env_ptr = tmp_env1;
|
|
|
|
free(tmp_env2);
|
|
|
|
} else {
|
|
|
|
env_ptr = tmp_env2;
|
|
|
|
free(tmp_env1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ! ENV_IS_EMBEDDED */
|
|
|
|
}
|
2008-09-10 20:48:06 +00:00
|
|
|
#else /* ! CONFIG_ENV_OFFSET_REDUND */
|
2006-03-05 17:57:33 +00:00
|
|
|
/*
|
|
|
|
* The legacy NAND code saved the environment in the first NAND device i.e.,
|
|
|
|
* nand_dev_desc + 0. This is also the behaviour using the new NAND code.
|
|
|
|
*/
|
2004-06-09 14:58:14 +00:00
|
|
|
void env_relocate_spec (void)
|
|
|
|
{
|
|
|
|
#if !defined(ENV_IS_EMBEDDED)
|
2006-03-11 21:53:33 +00:00
|
|
|
int ret;
|
2004-06-09 14:58:14 +00:00
|
|
|
|
2010-07-05 17:27:07 +00:00
|
|
|
#if defined(CONFIG_ENV_OFFSET_OOB)
|
|
|
|
ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
|
|
|
|
/* If unable to read environment offset from NAND OOB then fall through
|
|
|
|
* to the normal environment reading code below
|
|
|
|
*/
|
|
|
|
if (!ret)
|
|
|
|
printf("Found Environment offset in OOB..\n");
|
|
|
|
else
|
|
|
|
return use_default();
|
|
|
|
#endif
|
|
|
|
|
2008-09-10 20:48:06 +00:00
|
|
|
ret = readenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
|
2008-07-31 10:38:26 +00:00
|
|
|
if (ret)
|
2004-06-09 14:58:14 +00:00
|
|
|
return use_default();
|
|
|
|
|
|
|
|
if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
|
|
|
|
return use_default();
|
|
|
|
#endif /* ! ENV_IS_EMBEDDED */
|
|
|
|
}
|
2008-09-10 20:48:06 +00:00
|
|
|
#endif /* CONFIG_ENV_OFFSET_REDUND */
|
2004-06-09 14:58:14 +00:00
|
|
|
|
2006-09-12 18:19:10 +00:00
|
|
|
#if !defined(ENV_IS_EMBEDDED)
|
2004-06-09 14:58:14 +00:00
|
|
|
static void use_default()
|
|
|
|
{
|
|
|
|
puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
|
2008-07-07 07:40:39 +00:00
|
|
|
set_default_env();
|
2004-06-09 14:58:14 +00:00
|
|
|
}
|
2006-09-12 18:19:10 +00:00
|
|
|
#endif
|