env_nand.c 10.5 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
uchar env_get_char_spec(int index)
wdenk's avatar
wdenk committed
70
{
71
	return *((uchar *)(gd->env_addr + index));
wdenk's avatar
wdenk committed
72 73
}

74 75 76 77 78 79
/*
 * 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.
80 81
 *
 * When using a NAND boot image (like sequoia_nand), the environment
82 83 84
 * 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
85 86 87
 */
int env_init(void)
{
88
#if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
89
	int crc1_ok = 0, crc2_ok = 0;
90 91 92 93
	env_t *tmp_env1;

#ifdef CONFIG_ENV_OFFSET_REDUND
	env_t *tmp_env2;
94

95
	tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
96
	crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
97 98
#endif
	tmp_env1 = env_ptr;
99
	crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
100

101
	if (!crc1_ok && !crc2_ok) {
102 103
		gd->env_addr	= 0;
		gd->env_valid	= 0;
104 105 106

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

126 127 128 129
	if (gd->env_valid == 2)
		env_ptr = tmp_env2;
	else
#endif
130 131
	if (gd->env_valid == 1)
		env_ptr = tmp_env1;
132 133 134 135

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

#else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
136 137
	gd->env_addr	= (ulong)&default_environment[0];
	gd->env_valid	= 1;
138
#endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
wdenk's avatar
wdenk committed
139

140
	return 0;
wdenk's avatar
wdenk committed
141 142 143
}

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

	blocksize = nand_info[0].erasesize;
156
	len = min(blocksize, CONFIG_ENV_SIZE);
157

158
	while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
159 160 161 162
		if (nand_block_isbad(&nand_info[0], offset)) {
			offset += blocksize;
		} else {
			char_ptr = &buf[amount_saved];
163
			if (nand_write(&nand_info[0], offset, &len, char_ptr))
164
				return 1;
165

166
			offset += blocksize;
167
			amount_saved += len;
168 169
		}
	}
170
	if (amount_saved != CONFIG_ENV_SIZE)
171 172 173 174
		return 1;

	return 0;
}
175

176
#ifdef CONFIG_ENV_OFFSET_REDUND
177 178
static unsigned char env_flags;

wdenk's avatar
wdenk committed
179 180
int saveenv(void)
{
181 182 183 184
	env_t	env_new;
	ssize_t	len;
	char	*res;
	int	ret = 0;
185
	nand_erase_options_t nand_erase_options;
186

187
	memset(&nand_erase_options, 0, sizeof(nand_erase_options));
188
	nand_erase_options.length = CONFIG_ENV_RANGE;
189

190
	if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
191
		return 1;
192 193

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

202
	if (gd->env_valid == 1) {
203
		puts("Erasing redundant NAND...\n");
204
		nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
205
		if (nand_erase_opts(&nand_info[0], &nand_erase_options))
206
			return 1;
207

208
		puts("Writing to redundant NAND... ");
209
		ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *)&env_new);
210
	} else {
211
		puts("Erasing NAND...\n");
212
		nand_erase_options.offset = CONFIG_ENV_OFFSET;
213
		if (nand_erase_opts(&nand_info[0], &nand_erase_options))
214 215
			return 1;

216
		puts("Writing to NAND... ");
217
		ret = writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new);
218
	}
219 220
	if (ret) {
		puts("FAILED!\n");
221
		return 1;
222
	}
223

224 225
	puts("done\n");

226
	gd->env_valid = gd->env_valid == 2 ? 1 : 2;
227

228 229
	return ret;
}
230
#else /* ! CONFIG_ENV_OFFSET_REDUND */
231 232
int saveenv(void)
{
233
	int	ret = 0;
234 235 236
	env_t	env_new;
	ssize_t	len;
	char	*res;
237
	nand_erase_options_t nand_erase_options;
Wolfgang Denk's avatar
Wolfgang Denk committed
238

239
	memset(&nand_erase_options, 0, sizeof(nand_erase_options));
240 241
	nand_erase_options.length = CONFIG_ENV_RANGE;
	nand_erase_options.offset = CONFIG_ENV_OFFSET;
242

243
	if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
244
		return 1;
245 246

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

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

258 259
	puts("Writing to Nand... ");
	if (writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new)) {
260
		puts("FAILED!\n");
wdenk's avatar
wdenk committed
261
		return 1;
262
	}
wdenk's avatar
wdenk committed
263

264
	puts("done\n");
265
	return ret;
wdenk's avatar
wdenk committed
266
}
267
#endif /* CONFIG_ENV_OFFSET_REDUND */
wdenk's avatar
wdenk committed
268 269
#endif /* CMD_SAVEENV */

270
int readenv(size_t offset, u_char *buf)
271
{
272
	size_t end = offset + CONFIG_ENV_RANGE;
273
	size_t amount_loaded = 0;
274
	size_t blocksize, len;
275 276 277
	u_char *char_ptr;

	blocksize = nand_info[0].erasesize;
278 279
	if (!blocksize)
		return 1;
280

281
	len = min(blocksize, CONFIG_ENV_SIZE);
282

283
	while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
284 285 286 287
		if (nand_block_isbad(&nand_info[0], offset)) {
			offset += blocksize;
		} else {
			char_ptr = &buf[amount_loaded];
288 289
			if (nand_read_skip_bad(&nand_info[0], offset,
					       &len, char_ptr))
290
				return 1;
291

292
			offset += blocksize;
293
			amount_loaded += len;
294 295
		}
	}
296

297
	if (amount_loaded != CONFIG_ENV_SIZE)
298 299 300 301 302
		return 1;

	return 0;
}

303 304 305 306
#ifdef CONFIG_ENV_OFFSET_OOB
int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
{
	struct mtd_oob_ops ops;
307
	uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
308 309
	int ret;

310 311 312 313 314
	ops.datbuf	= NULL;
	ops.mode	= MTD_OOB_AUTO;
	ops.ooboffs	= 0;
	ops.ooblen	= ENV_OFFSET_SIZE;
	ops.oobbuf	= (void *)oob_buf;
315 316

	ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
317 318 319 320
	if (ret) {
		printf("error reading OOB block 0\n");
		return ret;
	}
321

322 323 324 325
	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];
326
	} else {
327 328
		printf("No dynamic environment marker in OOB block 0\n");
		return -ENOENT;
329
	}
330 331

	return 0;
332 333 334
}
#endif

335
#ifdef CONFIG_ENV_OFFSET_REDUND
336
void env_relocate_spec(void)
337 338
{
#if !defined(ENV_IS_EMBEDDED)
339
	int crc1_ok = 0, crc2_ok = 0;
340
	env_t *ep, *tmp_env1, *tmp_env2;
341

342 343
	tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
	tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
344
	if (tmp_env1 == NULL || tmp_env2 == NULL) {
345
		puts("Can't allocate buffers for environment\n");
346
		set_default_env("!malloc() failed");
347
		goto done;
348 349
	}

350
	if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
351 352
		puts("No Valid Environment Area found\n");

353
	if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
354
		puts("No Valid Redundant Environment Area found\n");
355

356 357
	crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
	crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
358

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

	free(env_ptr);
381 382 383 384 385 386

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

387
	env_flags = ep->flags;
388 389
	env_import((char *)ep, 0);

390
done:
391 392
	free(tmp_env1);
	free(tmp_env2);
393 394 395

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

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

422 423 424 425 426
	ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
	if (ret) {
		set_default_env("!readenv() failed");
		return;
	}
wdenk's avatar
wdenk committed
427

428
	env_import(buf, 1);
wdenk's avatar
wdenk committed
429 430
#endif /* ! ENV_IS_EMBEDDED */
}
431
#endif /* CONFIG_ENV_OFFSET_REDUND */