env_nand.c 10.4 KB
Newer Older
wdenk's avatar
wdenk committed
1
/*
2 3 4
 * (C) Copyright 2000-2010
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
5 6 7
 * (C) Copyright 2008
 * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
 *
wdenk's avatar
wdenk committed
8 9 10 11 12
 * (C) Copyright 2004
 * Jian Zhang, Texas Instruments, jzhang@ti.com.
 *
 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
 * Andreas Heppel <aheppel@sysgo.de>
13
 *
wdenk's avatar
wdenk committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 * 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
 */

#include <common.h>
#include <command.h>
#include <environment.h>
#include <linux/stddef.h>
37
#include <malloc.h>
38
#include <nand.h>
39 40
#include <search.h>
#include <errno.h>
wdenk's avatar
wdenk committed
41

42
#if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
wdenk's avatar
wdenk committed
43
#define CMD_SAVEENV
44
#elif defined(CONFIG_ENV_OFFSET_REDUND)
45
#error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
wdenk's avatar
wdenk committed
46 47
#endif

48 49
#if defined(CONFIG_ENV_SIZE_REDUND) &&	\
	(CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
50
#error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
wdenk's avatar
wdenk committed
51 52
#endif

53 54
#ifndef CONFIG_ENV_RANGE
#define CONFIG_ENV_RANGE	CONFIG_ENV_SIZE
55 56
#endif

57
char *env_name_spec = "NAND";
wdenk's avatar
wdenk committed
58

59
#if defined(ENV_IS_EMBEDDED)
60
env_t *env_ptr = &environment;
61 62
#elif defined(CONFIG_NAND_ENV_DST)
env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
wdenk's avatar
wdenk committed
63
#else /* ! ENV_IS_EMBEDDED */
64
env_t *env_ptr;
wdenk's avatar
wdenk committed
65 66
#endif /* ENV_IS_EMBEDDED */

67
DECLARE_GLOBAL_DATA_PTR;
wdenk's avatar
wdenk committed
68

69 70 71 72 73 74
/*
 * 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 will call our
 * relocate function which does the real validation.
75 76
 *
 * When using a NAND boot image (like sequoia_nand), the environment
77 78 79
 * 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.
wdenk's avatar
wdenk committed
80 81 82
 */
int env_init(void)
{
83
#if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
84
	int crc1_ok = 0, crc2_ok = 0;
85 86 87 88
	env_t *tmp_env1;

#ifdef CONFIG_ENV_OFFSET_REDUND
	env_t *tmp_env2;
89

90
	tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
91
	crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
92 93
#endif
	tmp_env1 = env_ptr;
94
	crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
95

96
	if (!crc1_ok && !crc2_ok) {
97 98
		gd->env_addr	= 0;
		gd->env_valid	= 0;
99 100 101

		return 0;
	} else if (crc1_ok && !crc2_ok) {
102
		gd->env_valid = 1;
103 104 105
	}
#ifdef CONFIG_ENV_OFFSET_REDUND
	else if (!crc1_ok && crc2_ok) {
106
		gd->env_valid = 2;
107
	} else {
108
		/* both ok - check serial */
109
		if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
110
			gd->env_valid = 2;
111
		else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
112
			gd->env_valid = 1;
113
		else if (tmp_env1->flags > tmp_env2->flags)
114
			gd->env_valid = 1;
115
		else if (tmp_env2->flags > tmp_env1->flags)
116 117 118 119 120
			gd->env_valid = 2;
		else /* flags are equal - almost impossible */
			gd->env_valid = 1;
	}

121 122 123 124
	if (gd->env_valid == 2)
		env_ptr = tmp_env2;
	else
#endif
125 126
	if (gd->env_valid == 1)
		env_ptr = tmp_env1;
127 128 129 130

	gd->env_addr = (ulong)env_ptr->data;

#else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
131 132
	gd->env_addr	= (ulong)&default_environment[0];
	gd->env_valid	= 1;
133
#endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
wdenk's avatar
wdenk committed
134

135
	return 0;
wdenk's avatar
wdenk committed
136 137 138
}

#ifdef CMD_SAVEENV
139 140 141 142
/*
 * 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.
 */
143 144
int writeenv(size_t offset, u_char *buf)
{
145
	size_t end = offset + CONFIG_ENV_RANGE;
146
	size_t amount_saved = 0;
147
	size_t blocksize, len;
148 149 150
	u_char *char_ptr;

	blocksize = nand_info[0].erasesize;
151
	len = min(blocksize, CONFIG_ENV_SIZE);
152

153
	while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
154 155 156 157
		if (nand_block_isbad(&nand_info[0], offset)) {
			offset += blocksize;
		} else {
			char_ptr = &buf[amount_saved];
158
			if (nand_write(&nand_info[0], offset, &len, char_ptr))
159
				return 1;
160

161
			offset += blocksize;
162
			amount_saved += len;
163 164
		}
	}
165
	if (amount_saved != CONFIG_ENV_SIZE)
166 167 168 169
		return 1;

	return 0;
}
170

171
#ifdef CONFIG_ENV_OFFSET_REDUND
172 173
static unsigned char env_flags;

wdenk's avatar
wdenk committed
174 175
int saveenv(void)
{
176 177 178 179
	env_t	env_new;
	ssize_t	len;
	char	*res;
	int	ret = 0;
180
	nand_erase_options_t nand_erase_options;
181

182
	memset(&nand_erase_options, 0, sizeof(nand_erase_options));
183
	nand_erase_options.length = CONFIG_ENV_RANGE;
184

185
	if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
186
		return 1;
187 188

	res = (char *)&env_new.data;
189
	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
190 191 192 193
	if (len < 0) {
		error("Cannot export environment: errno = %d\n", errno);
		return 1;
	}
194 195
	env_new.crc	= crc32(0, env_new.data, ENV_SIZE);
	env_new.flags	= ++env_flags; /* increase the serial */
196

197
	if (gd->env_valid == 1) {
198
		puts("Erasing redundant NAND...\n");
199
		nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
200
		if (nand_erase_opts(&nand_info[0], &nand_erase_options))
201
			return 1;
202

203
		puts("Writing to redundant NAND... ");
204
		ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *)&env_new);
205
	} else {
206
		puts("Erasing NAND...\n");
207
		nand_erase_options.offset = CONFIG_ENV_OFFSET;
208
		if (nand_erase_opts(&nand_info[0], &nand_erase_options))
209 210
			return 1;

211
		puts("Writing to NAND... ");
212
		ret = writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new);
213
	}
214 215
	if (ret) {
		puts("FAILED!\n");
216
		return 1;
217
	}
218

219 220
	puts("done\n");

221
	gd->env_valid = gd->env_valid == 2 ? 1 : 2;
222

223 224
	return ret;
}
225
#else /* ! CONFIG_ENV_OFFSET_REDUND */
226 227
int saveenv(void)
{
228
	int	ret = 0;
229 230 231
	env_t	env_new;
	ssize_t	len;
	char	*res;
232
	nand_erase_options_t nand_erase_options;
Wolfgang Denk's avatar
Wolfgang Denk committed
233

234
	memset(&nand_erase_options, 0, sizeof(nand_erase_options));
235 236
	nand_erase_options.length = CONFIG_ENV_RANGE;
	nand_erase_options.offset = CONFIG_ENV_OFFSET;
237

238
	if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
239
		return 1;
240 241

	res = (char *)&env_new.data;
242
	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
243 244 245 246
	if (len < 0) {
		error("Cannot export environment: errno = %d\n", errno);
		return 1;
	}
247
	env_new.crc = crc32(0, env_new.data, ENV_SIZE);
248 249

	puts("Erasing Nand...\n");
250
	if (nand_erase_opts(&nand_info[0], &nand_erase_options))
251
		return 1;
wdenk's avatar
wdenk committed
252

253 254
	puts("Writing to Nand... ");
	if (writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new)) {
255
		puts("FAILED!\n");
wdenk's avatar
wdenk committed
256
		return 1;
257
	}
wdenk's avatar
wdenk committed
258

259
	puts("done\n");
260
	return ret;
wdenk's avatar
wdenk committed
261
}
262
#endif /* CONFIG_ENV_OFFSET_REDUND */
wdenk's avatar
wdenk committed
263 264
#endif /* CMD_SAVEENV */

265
int readenv(size_t offset, u_char *buf)
266
{
267
	size_t end = offset + CONFIG_ENV_RANGE;
268
	size_t amount_loaded = 0;
269
	size_t blocksize, len;
270 271 272
	u_char *char_ptr;

	blocksize = nand_info[0].erasesize;
273 274
	if (!blocksize)
		return 1;
275

276
	len = min(blocksize, CONFIG_ENV_SIZE);
277

278
	while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
279 280 281 282
		if (nand_block_isbad(&nand_info[0], offset)) {
			offset += blocksize;
		} else {
			char_ptr = &buf[amount_loaded];
283 284
			if (nand_read_skip_bad(&nand_info[0], offset,
					       &len, char_ptr))
285
				return 1;
286

287
			offset += blocksize;
288
			amount_loaded += len;
289 290
		}
	}
291

292
	if (amount_loaded != CONFIG_ENV_SIZE)
293 294 295 296 297
		return 1;

	return 0;
}

298 299 300 301
#ifdef CONFIG_ENV_OFFSET_OOB
int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
{
	struct mtd_oob_ops ops;
302
	uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
303 304
	int ret;

305 306 307 308 309
	ops.datbuf	= NULL;
	ops.mode	= MTD_OOB_AUTO;
	ops.ooboffs	= 0;
	ops.ooblen	= ENV_OFFSET_SIZE;
	ops.oobbuf	= (void *)oob_buf;
310 311

	ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
312 313 314 315
	if (ret) {
		printf("error reading OOB block 0\n");
		return ret;
	}
316

317 318 319 320
	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];
321
	} else {
322 323
		printf("No dynamic environment marker in OOB block 0\n");
		return -ENOENT;
324
	}
325 326

	return 0;
327 328 329
}
#endif

330
#ifdef CONFIG_ENV_OFFSET_REDUND
331
void env_relocate_spec(void)
332 333
{
#if !defined(ENV_IS_EMBEDDED)
334
	int crc1_ok = 0, crc2_ok = 0;
335
	env_t *ep, *tmp_env1, *tmp_env2;
336

337 338
	tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
	tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
339
	if (tmp_env1 == NULL || tmp_env2 == NULL) {
340
		puts("Can't allocate buffers for environment\n");
341
		set_default_env("!malloc() failed");
342
		goto done;
343 344
	}

345
	if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
346 347
		puts("No Valid Environment Area found\n");

348
	if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
349
		puts("No Valid Redundant Environment Area found\n");
350

351 352
	crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
	crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
353

354 355
	if (!crc1_ok && !crc2_ok) {
		set_default_env("!bad CRC");
356
		goto done;
357
	} else if (crc1_ok && !crc2_ok) {
358
		gd->env_valid = 1;
359
	} else if (!crc1_ok && crc2_ok) {
360
		gd->env_valid = 2;
361
	} else {
362
		/* both ok - check serial */
363
		if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
364
			gd->env_valid = 2;
365
		else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
366
			gd->env_valid = 1;
367
		else if (tmp_env1->flags > tmp_env2->flags)
368
			gd->env_valid = 1;
369
		else if (tmp_env2->flags > tmp_env1->flags)
370 371 372 373 374 375
			gd->env_valid = 2;
		else /* flags are equal - almost impossible */
			gd->env_valid = 1;
	}

	free(env_ptr);
376 377 378 379 380 381

	if (gd->env_valid == 1)
		ep = tmp_env1;
	else
		ep = tmp_env2;

382
	env_flags = ep->flags;
383 384
	env_import((char *)ep, 0);

385
done:
386 387
	free(tmp_env1);
	free(tmp_env2);
388 389 390

#endif /* ! ENV_IS_EMBEDDED */
}
391
#else /* ! CONFIG_ENV_OFFSET_REDUND */
392
/*
393 394 395
 * 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.
396
 */
397
void env_relocate_spec(void)
wdenk's avatar
wdenk committed
398 399
{
#if !defined(ENV_IS_EMBEDDED)
400
	int ret;
401
	char buf[CONFIG_ENV_SIZE];
wdenk's avatar
wdenk committed
402

403 404
#if defined(CONFIG_ENV_OFFSET_OOB)
	ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
405 406
	/*
	 * If unable to read environment offset from NAND OOB then fall through
407 408
	 * to the normal environment reading code below
	 */
409
	if (!ret) {
410
		printf("Found Environment offset in OOB..\n");
411 412 413 414
	} else {
		set_default_env("!no env offset in OOB");
		return;
	}
415 416
#endif

417 418 419 420 421
	ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
	if (ret) {
		set_default_env("!readenv() failed");
		return;
	}
wdenk's avatar
wdenk committed
422

423
	env_import(buf, 1);
wdenk's avatar
wdenk committed
424 425
#endif /* ! ENV_IS_EMBEDDED */
}
426
#endif /* CONFIG_ENV_OFFSET_REDUND */