]> Git Repo - J-u-boot.git/blame - cmd/nand.c
env: Move non-cli env functions to env/common.c
[J-u-boot.git] / cmd / nand.c
CommitLineData
dc7c9a1a
WD
1/*
2 * Driver for NAND support, Rick Bronson
3 * borrowed heavily from:
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <[email protected]>
384cc687 6 *
c9f7351b
BG
7 * Ported 'dynenv' to 'nand env.oob' command
8 * (C) 2010 Nanometrics, Inc.
9 * 'dynenv' -- Dynamic environment offset in NAND OOB
10 * (C) Copyright 2006-2007 OpenMoko, Inc.
384cc687
WD
11 * Added 16-bit nand support
12 * (C) 2004 Texas Instruments
ea533c26 13 *
418396e2 14 * Copyright 2010, 2012 Freescale Semiconductor
ea533c26
SW
15 * The portions of this file whose copyright is held by Freescale and which
16 * are not considered a derived work of GPL v2-only code may be distributed
17 * and/or modified under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of the
19 * License, or (at your option) any later version.
dc7c9a1a
WD
20 */
21
22#include <common.h>
52f24238 23#include <bootstage.h>
8e8ccfe1 24#include <image.h>
90526e9f 25#include <asm/cache.h>
cfa460ad 26#include <linux/mtd/mtd.h>
1cefed1e 27#include <linux/mtd/rawnand.h>
addb2e16 28#include <command.h>
24b852a7 29#include <console.h>
c7694dd4 30#include <env.h>
addb2e16
BS
31#include <watchdog.h>
32#include <malloc.h>
33#include <asm/byteorder.h>
addb2e16
BS
34#include <jffs2/jffs2.h>
35#include <nand.h>
36
eb446ef6
MR
37#include "legacy-mtd-utils.h"
38
0c8a8491 39#if defined(CONFIG_CMD_MTDPARTS)
856f0544 40
445093d1 41/* partition handling routines */
856f0544 42int mtdparts_init(void);
856f0544 43int find_dev_and_part(const char *id, struct mtd_device **dev,
4b070809 44 u8 *part_num, struct part_info **part);
856f0544
SR
45#endif
46
151c06ec
SW
47static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
48 int repeat)
addb2e16
BS
49{
50 int i;
9ad754fe 51 u_char *datbuf, *oobbuf, *p;
8c5659a6 52 static loff_t last;
e40520b5 53 int ret = 0;
8c5659a6
SW
54
55 if (repeat)
151c06ec 56 off = last + mtd->writesize;
8c5659a6
SW
57
58 last = off;
addb2e16 59
151c06ec 60 datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize);
e40520b5 61 if (!datbuf) {
addb2e16
BS
62 puts("No memory for page buffer\n");
63 return 1;
64 }
e40520b5 65
151c06ec 66 oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize);
e40520b5
MY
67 if (!oobbuf) {
68 puts("No memory for page buffer\n");
69 ret = 1;
70 goto free_dat;
71 }
151c06ec 72 off &= ~(mtd->writesize - 1);
cfa460ad 73 loff_t addr = (loff_t) off;
9ad754fe
WJ
74 struct mtd_oob_ops ops;
75 memset(&ops, 0, sizeof(ops));
76 ops.datbuf = datbuf;
cfdae12f 77 ops.oobbuf = oobbuf;
151c06ec
SW
78 ops.len = mtd->writesize;
79 ops.ooblen = mtd->oobsize;
dfe64e2c 80 ops.mode = MTD_OPS_RAW;
151c06ec 81 i = mtd_read_oob(mtd, addr, &ops);
addb2e16 82 if (i < 0) {
e870690b 83 printf("Error (%d) reading page %08lx\n", i, off);
e40520b5
MY
84 ret = 1;
85 goto free_all;
addb2e16 86 }
e870690b 87 printf("Page %08lx dump:\n", off);
4b070809 88
7d25cd34 89 if (!only_oob) {
151c06ec 90 i = mtd->writesize >> 4;
7d25cd34
MY
91 p = datbuf;
92
93 while (i--) {
9ad754fe
WJ
94 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
95 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
96 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
dfbf617f
SW
97 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
98 p[15]);
7d25cd34
MY
99 p += 16;
100 }
addb2e16 101 }
7d25cd34 102
addb2e16 103 puts("OOB:\n");
151c06ec 104 i = mtd->oobsize >> 3;
cfdae12f 105 p = oobbuf;
addb2e16 106 while (i--) {
cfa460ad
WJ
107 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
108 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
addb2e16
BS
109 p += 8;
110 }
e40520b5
MY
111
112free_all:
9ad754fe 113 free(oobbuf);
e40520b5
MY
114free_dat:
115 free(datbuf);
addb2e16 116
e40520b5 117 return ret;
addb2e16
BS
118}
119
120/* ------------------------------------------------------------------------- */
121
ea533c26
SW
122static int set_dev(int dev)
123{
ad92dff2
M
124 struct mtd_info *mtd = get_nand_dev_by_index(dev);
125
126 if (!mtd)
127 return -ENODEV;
ea533c26
SW
128
129 if (nand_curr_device == dev)
130 return 0;
131
ad92dff2 132 printf("Device %d: %s", dev, mtd->name);
ea533c26
SW
133 puts("... is now current device\n");
134 nand_curr_device = dev;
135
136#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
6308e71b 137 board_nand_select_device(mtd_to_nand(mtd), dev);
ea533c26
SW
138#endif
139
140 return 0;
141}
142
50657c27
NM
143#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
144static void print_status(ulong start, ulong end, ulong erasesize, int status)
145{
e70bfa29
JH
146 /*
147 * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is
148 * not the same as others. Instead of bit 1 being lock, it is
149 * #lock_tight. To make the driver support either format, ignore bit 1
150 * and use only bit 0 and bit 2.
151 */
50657c27
NM
152 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
153 start,
154 end - 1,
155 (end - start) / erasesize,
156 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
e70bfa29 157 (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""),
50657c27
NM
158 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
159}
160
151c06ec 161static void do_nand_status(struct mtd_info *mtd)
50657c27
NM
162{
163 ulong block_start = 0;
164 ulong off;
165 int last_status = -1;
166
17cb4b8f 167 struct nand_chip *nand_chip = mtd_to_nand(mtd);
50657c27 168 /* check the WP bit */
151c06ec 169 nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
50657c27 170 printf("device is %swrite protected\n",
151c06ec
SW
171 (nand_chip->read_byte(mtd) & 0x80 ?
172 "NOT " : ""));
50657c27 173
151c06ec
SW
174 for (off = 0; off < mtd->size; off += mtd->erasesize) {
175 int s = nand_get_lock_status(mtd, off);
50657c27
NM
176
177 /* print message only if status has changed */
178 if (s != last_status && off != 0) {
151c06ec 179 print_status(block_start, off, mtd->erasesize,
50657c27
NM
180 last_status);
181 block_start = off;
182 }
183 last_status = s;
184 }
185 /* Print the last block info */
151c06ec 186 print_status(block_start, off, mtd->erasesize, last_status);
50657c27
NM
187}
188#endif
189
c9f7351b
BG
190#ifdef CONFIG_ENV_OFFSET_OOB
191unsigned long nand_env_oob_offset;
192
09140113 193int do_nand_env_oob(struct cmd_tbl *cmdtp, int argc, char *const argv[])
c9f7351b
BG
194{
195 int ret;
196 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
ad92dff2 197 struct mtd_info *mtd = get_nand_dev_by_index(0);
c9f7351b
BG
198 char *cmd = argv[1];
199
8b7d5124 200 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) {
ea533c26
SW
201 puts("no devices available\n");
202 return 1;
203 }
204
205 set_dev(0);
206
c9f7351b 207 if (!strcmp(cmd, "get")) {
151c06ec 208 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
53504a27 209 if (ret)
c9f7351b 210 return 1;
53504a27
SW
211
212 printf("0x%08lx\n", nand_env_oob_offset);
c9f7351b 213 } else if (!strcmp(cmd, "set")) {
ea533c26
SW
214 loff_t addr;
215 loff_t maxsize;
c9f7351b 216 struct mtd_oob_ops ops;
ea533c26 217 int idx = 0;
c9f7351b
BG
218
219 if (argc < 3)
220 goto usage;
221
ad92dff2 222 mtd = get_nand_dev_by_index(idx);
c39d6a0e 223 /* We don't care about size, or maxsize. */
09c32807 224 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
ad92dff2 225 MTD_DEV_TYPE_NAND, mtd->size)) {
09c32807
HS
226 puts("Offset or partition name expected\n");
227 return 1;
228 }
229 if (set_dev(idx)) {
ea533c26
SW
230 puts("Offset or partition name expected\n");
231 return 1;
232 }
233
234 if (idx != 0) {
235 puts("Partition not on first NAND device\n");
c9f7351b
BG
236 return 1;
237 }
238
151c06ec 239 if (mtd->oobavail < ENV_OFFSET_SIZE) {
53504a27
SW
240 printf("Insufficient available OOB bytes:\n"
241 "%d OOB bytes available but %d required for "
242 "env.oob support\n",
151c06ec 243 mtd->oobavail, ENV_OFFSET_SIZE);
c9f7351b
BG
244 return 1;
245 }
246
151c06ec 247 if ((addr & (mtd->erasesize - 1)) != 0) {
c9f7351b
BG
248 printf("Environment offset must be block-aligned\n");
249 return 1;
250 }
251
252 ops.datbuf = NULL;
253 ops.mode = MTD_OOB_AUTO;
254 ops.ooboffs = 0;
255 ops.ooblen = ENV_OFFSET_SIZE;
256 ops.oobbuf = (void *) oob_buf;
257
258 oob_buf[0] = ENV_OOB_MARKER;
151c06ec 259 oob_buf[1] = addr / mtd->erasesize;
c9f7351b 260
151c06ec 261 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops);
53504a27 262 if (ret) {
c9f7351b
BG
263 printf("Error writing OOB block 0\n");
264 return ret;
265 }
53504a27 266
151c06ec 267 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
53504a27
SW
268 if (ret) {
269 printf("Error reading env offset in OOB\n");
270 return ret;
271 }
272
273 if (addr != nand_env_oob_offset) {
274 printf("Verification of env offset in OOB failed: "
ea533c26
SW
275 "0x%08llx expected but got 0x%08lx\n",
276 (unsigned long long)addr, nand_env_oob_offset);
53504a27
SW
277 return 1;
278 }
c9f7351b
BG
279 } else {
280 goto usage;
281 }
282
283 return ret;
284
285usage:
4c12eeb8 286 return CMD_RET_USAGE;
c9f7351b
BG
287}
288
289#endif
290
ce80ddc1 291static void nand_print_and_set_info(int idx)
672ed2ae 292{
ad92dff2
M
293 struct mtd_info *mtd;
294 struct nand_chip *chip;
295
296 mtd = get_nand_dev_by_index(idx);
297 if (!mtd)
298 return;
ce80ddc1 299
ad92dff2 300 chip = mtd_to_nand(mtd);
672ed2ae
WG
301 printf("Device %d: ", idx);
302 if (chip->numchips > 1)
303 printf("%dx ", chip->numchips);
304 printf("%s, sector size %u KiB\n",
151c06ec
SW
305 mtd->name, mtd->erasesize >> 10);
306 printf(" Page size %8d b\n", mtd->writesize);
307 printf(" OOB size %8d b\n", mtd->oobsize);
308 printf(" Erase size %8d b\n", mtd->erasesize);
5db73feb 309 printf(" subpagesize %8d b\n", chip->subpagesize);
66dc09c5
LW
310 printf(" options 0x%08x\n", chip->options);
311 printf(" bbt options 0x%08x\n", chip->bbt_options);
ce80ddc1
MV
312
313 /* Set geometry info */
018f5303
SG
314 env_set_hex("nand_writesize", mtd->writesize);
315 env_set_hex("nand_oobsize", mtd->oobsize);
316 env_set_hex("nand_erasesize", mtd->erasesize);
672ed2ae
WG
317}
318
151c06ec 319static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
2dc3c483 320 ulong count, int read, int no_verify)
418396e2
SW
321{
322 int ret = 0;
418396e2
SW
323
324 while (count--) {
325 /* Raw access */
326 mtd_oob_ops_t ops = {
327 .datbuf = (u8 *)addr,
151c06ec
SW
328 .oobbuf = ((u8 *)addr) + mtd->writesize,
329 .len = mtd->writesize,
330 .ooblen = mtd->oobsize,
dfe64e2c 331 .mode = MTD_OPS_RAW
418396e2
SW
332 };
333
6b94f118 334 if (read) {
151c06ec 335 ret = mtd_read_oob(mtd, off, &ops);
6b94f118 336 } else {
151c06ec 337 ret = mtd_write_oob(mtd, off, &ops);
2dc3c483 338 if (!ret && !no_verify)
151c06ec 339 ret = nand_verify_page_oob(mtd, &ops, off);
6b94f118 340 }
418396e2
SW
341
342 if (ret) {
343 printf("%s: error at offset %llx, ret %d\n",
344 __func__, (long long)off, ret);
345 break;
346 }
347
151c06ec
SW
348 addr += mtd->writesize + mtd->oobsize;
349 off += mtd->writesize;
418396e2
SW
350 }
351
352 return ret;
353}
354
e834402f 355/* Adjust a chip/partition size down for bad blocks so we don't
9b80aa8e 356 * read/write past the end of a chip/partition by accident.
e834402f
HC
357 */
358static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev)
359{
360 /* We grab the nand info object here fresh because this is usually
361 * called after arg_off_size() which can change the value of dev.
362 */
ad92dff2 363 struct mtd_info *mtd = get_nand_dev_by_index(dev);
e834402f
HC
364 loff_t maxoffset = offset + *size;
365 int badblocks = 0;
366
367 /* count badblocks in NAND from offset to offset + size */
151c06ec
SW
368 for (; offset < maxoffset; offset += mtd->erasesize) {
369 if (nand_block_isbad(mtd, offset))
e834402f
HC
370 badblocks++;
371 }
372 /* adjust size if any bad blocks found */
373 if (badblocks) {
151c06ec 374 *size -= badblocks * mtd->erasesize;
e834402f
HC
375 printf("size adjusted to 0x%llx (%d bad blocks)\n",
376 (unsigned long long)*size, badblocks);
377 }
378}
379
09140113
SG
380static int do_nand(struct cmd_tbl *cmdtp, int flag, int argc,
381 char *const argv[])
addb2e16 382{
ea533c26
SW
383 int i, ret = 0;
384 ulong addr;
c39d6a0e 385 loff_t off, size, maxsize;
addb2e16 386 char *cmd, *s;
151c06ec 387 struct mtd_info *mtd;
6d0f6bcf
JCPV
388#ifdef CONFIG_SYS_NAND_QUIET
389 int quiet = CONFIG_SYS_NAND_QUIET;
c750d2e6 390#else
2255b2d2 391 int quiet = 0;
c750d2e6 392#endif
00caae6d 393 const char *quiet_str = env_get("quiet");
ea533c26 394 int dev = nand_curr_device;
8c5659a6 395 int repeat = flag & CMD_FLAG_REPEAT;
addb2e16
BS
396
397 /* at least two arguments please */
398 if (argc < 2)
399 goto usage;
400
2255b2d2
SR
401 if (quiet_str)
402 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
403
addb2e16
BS
404 cmd = argv[1];
405
8c5659a6
SW
406 /* Only "dump" is repeatable. */
407 if (repeat && strcmp(cmd, "dump"))
408 return 0;
409
addb2e16
BS
410 if (strcmp(cmd, "info") == 0) {
411
412 putc('\n');
ad92dff2
M
413 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
414 nand_print_and_set_info(i);
addb2e16
BS
415 return 0;
416 }
417
418 if (strcmp(cmd, "device") == 0) {
addb2e16 419 if (argc < 3) {
672ed2ae 420 putc('\n');
ea533c26 421 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
672ed2ae 422 puts("no devices available\n");
addb2e16 423 else
ce80ddc1 424 nand_print_and_set_info(dev);
addb2e16
BS
425 return 0;
426 }
43a2b0e7 427
0b1284eb 428 dev = (int)dectoul(argv[2], NULL);
ea533c26 429 set_dev(dev);
43a2b0e7 430
addb2e16
BS
431 return 0;
432 }
433
c9f7351b
BG
434#ifdef CONFIG_ENV_OFFSET_OOB
435 /* this command operates only on the first nand device */
ea533c26
SW
436 if (strcmp(cmd, "env.oob") == 0)
437 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
c9f7351b
BG
438#endif
439
ea533c26
SW
440 /* The following commands operate on the current device, unless
441 * overridden by a partition specifier. Note that if somehow the
442 * current device is invalid, it will have to be changed to a valid
443 * one before these commands can run, even if a partition specifier
444 * for another device is to be used.
445 */
ad92dff2
M
446 mtd = get_nand_dev_by_index(dev);
447 if (!mtd) {
addb2e16
BS
448 puts("\nno devices available\n");
449 return 1;
450 }
addb2e16
BS
451
452 if (strcmp(cmd, "bad") == 0) {
ea533c26 453 printf("\nDevice %d bad blocks:\n", dev);
151c06ec
SW
454 for (off = 0; off < mtd->size; off += mtd->erasesize)
455 if (nand_block_isbad(mtd, off))
ea533c26 456 printf(" %08llx\n", (unsigned long long)off);
addb2e16
BS
457 return 0;
458 }
459
856f0544
SR
460 /*
461 * Syntax is:
462 * 0 1 2 3 4
463 * nand erase [clean] [off size]
464 */
30486322 465 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
2255b2d2 466 nand_erase_options_t opts;
856f0544 467 /* "clean" at index 2 means request to write cleanmarker */
3043c045 468 int clean = argc > 2 && !strcmp("clean", argv[2]);
60899816
MV
469 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
470 int o = (clean || scrub_yes) ? 3 : 2;
30486322 471 int scrub = !strncmp(cmd, "scrub", 5);
30486322
SW
472 int spread = 0;
473 int args = 2;
60899816
MV
474 const char *scrub_warn =
475 "Warning: "
476 "scrub option will erase all factory set bad blocks!\n"
477 " "
478 "There is no reliable way to recover them.\n"
479 " "
480 "Use this command only for testing purposes if you\n"
481 " "
482 "are sure of what you are doing!\n"
483 "\nReally scrub this NAND flash? <y/N>\n";
30486322
SW
484
485 if (cmd[5] != 0) {
486 if (!strcmp(&cmd[5], ".spread")) {
487 spread = 1;
488 } else if (!strcmp(&cmd[5], ".part")) {
30486322
SW
489 args = 1;
490 } else if (!strcmp(&cmd[5], ".chip")) {
30486322
SW
491 args = 0;
492 } else {
493 goto usage;
494 }
495 }
496
497 /*
498 * Don't allow missing arguments to cause full chip/partition
499 * erases -- easy to do accidentally, e.g. with a misspelled
500 * variable name.
501 */
502 if (argc != o + args)
503 goto usage;
2255b2d2 504
30486322 505 printf("\nNAND %s: ", cmd);
856f0544 506 /* skip first two or three arguments, look for offset and size */
09c32807
HS
507 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
508 &maxsize, MTD_DEV_TYPE_NAND,
ad92dff2 509 mtd->size) != 0)
09c32807
HS
510 return 1;
511
512 if (set_dev(dev))
856f0544 513 return 1;
2255b2d2 514
ad92dff2 515 mtd = get_nand_dev_by_index(dev);
ea533c26 516
2255b2d2
SR
517 memset(&opts, 0, sizeof(opts));
518 opts.offset = off;
519 opts.length = size;
520 opts.jffs2 = clean;
521 opts.quiet = quiet;
30486322 522 opts.spread = spread;
2255b2d2
SR
523
524 if (scrub) {
a5dffa4b 525 if (scrub_yes) {
60899816 526 opts.scrub = 1;
a5dffa4b
PA
527 } else {
528 puts(scrub_warn);
529 if (confirm_yesno()) {
6b94b496 530 opts.scrub = 1;
a5dffa4b 531 } else {
6b94b496 532 puts("scrub aborted\n");
46aabcc4 533 return 1;
6b94b496 534 }
2255b2d2
SR
535 }
536 }
151c06ec 537 ret = nand_erase_opts(mtd, &opts);
addb2e16
BS
538 printf("%s\n", ret ? "ERROR" : "OK");
539
540 return ret == 0 ? 0 : 1;
541 }
542
543 if (strncmp(cmd, "dump", 4) == 0) {
544 if (argc < 3)
545 goto usage;
546
7e5f460e 547 off = (int)hextoul(argv[2], NULL);
151c06ec 548 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
addb2e16
BS
549
550 return ret == 0 ? 1 : 0;
addb2e16
BS
551 }
552
addb2e16 553 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
ea533c26 554 size_t rwsize;
418396e2 555 ulong pagecount = 1;
2255b2d2 556 int read;
ced199dc 557 int raw = 0;
2dc3c483 558 int no_verify = 0;
2255b2d2 559
addb2e16
BS
560 if (argc < 4)
561 goto usage;
2255b2d2 562
7e5f460e 563 addr = (ulong)hextoul(argv[2], NULL);
addb2e16 564
2255b2d2 565 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
856f0544 566 printf("\nNAND %s: ", read ? "read" : "write");
4cbb651b 567
2255b2d2 568 s = strchr(cmd, '.');
418396e2 569
2dc3c483 570 if (s && !strncmp(s, ".raw", 4)) {
418396e2
SW
571 raw = 1;
572
2dc3c483
BB
573 if (!strcmp(s, ".raw.noverify"))
574 no_verify = 1;
575
09c32807
HS
576 if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
577 MTD_DEV_TYPE_NAND,
ad92dff2 578 mtd->size))
09c32807
HS
579 return 1;
580
581 if (set_dev(dev))
418396e2
SW
582 return 1;
583
ad92dff2 584 mtd = get_nand_dev_by_index(dev);
93d3232d 585
418396e2
SW
586 if (argc > 4 && !str2long(argv[4], &pagecount)) {
587 printf("'%s' is not a number\n", argv[4]);
588 return 1;
589 }
590
151c06ec 591 if (pagecount * mtd->writesize > size) {
418396e2
SW
592 puts("Size exceeds partition or device limit\n");
593 return -1;
594 }
595
151c06ec 596 rwsize = pagecount * (mtd->writesize + mtd->oobsize);
418396e2 597 } else {
09c32807
HS
598 if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
599 &size, &maxsize,
600 MTD_DEV_TYPE_NAND,
ad92dff2 601 mtd->size) != 0)
09c32807
HS
602 return 1;
603
604 if (set_dev(dev))
418396e2
SW
605 return 1;
606
e834402f
HC
607 /* size is unspecified */
608 if (argc < 5)
609 adjust_size_for_badblocks(&size, off, dev);
418396e2
SW
610 rwsize = size;
611 }
612
ad92dff2 613 mtd = get_nand_dev_by_index(dev);
93d3232d 614
984e03cd
SW
615 if (!s || !strcmp(s, ".jffs2") ||
616 !strcmp(s, ".e") || !strcmp(s, ".i")) {
dfbf617f 617 if (read)
151c06ec 618 ret = nand_read_skip_bad(mtd, off, &rwsize,
c39d6a0e 619 NULL, maxsize,
4b070809 620 (u_char *)addr);
dfbf617f 621 else
151c06ec 622 ret = nand_write_skip_bad(mtd, off, &rwsize,
c39d6a0e 623 NULL, maxsize,
6b94f118
PT
624 (u_char *)addr,
625 WITH_WR_VERIFY);
c9494866
BG
626#ifdef CONFIG_CMD_NAND_TRIMFFS
627 } else if (!strcmp(s, ".trimffs")) {
628 if (read) {
629 printf("Unknown nand command suffix '%s'\n", s);
630 return 1;
631 }
151c06ec 632 ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
c39d6a0e 633 maxsize, (u_char *)addr,
6b94f118 634 WITH_DROP_FFS | WITH_WR_VERIFY);
47fc18f1 635#endif
dfc99e14 636 } else if (!strcmp(s, ".oob")) {
cfa460ad
WJ
637 /* out-of-band data */
638 mtd_oob_ops_t ops = {
639 .oobbuf = (u8 *)addr,
ea533c26 640 .ooblen = rwsize,
dfe64e2c 641 .mode = MTD_OPS_RAW
cfa460ad
WJ
642 };
643
fb3659ac 644 if (read)
151c06ec 645 ret = mtd_read_oob(mtd, off, &ops);
fb3659ac 646 else
151c06ec 647 ret = mtd_write_oob(mtd, off, &ops);
418396e2 648 } else if (raw) {
2dc3c483
BB
649 ret = raw_access(mtd, addr, off, pagecount, read,
650 no_verify);
856f0544 651 } else {
984e03cd
SW
652 printf("Unknown nand command suffix '%s'.\n", s);
653 return 1;
2255b2d2
SR
654 }
655
ea533c26 656 printf(" %zu bytes %s: %s\n", rwsize,
2255b2d2 657 read ? "read" : "written", ret ? "ERROR" : "OK");
addb2e16
BS
658
659 return ret == 0 ? 0 : 1;
660 }
2255b2d2 661
3287f6d3
BT
662#ifdef CONFIG_CMD_NAND_TORTURE
663 if (strcmp(cmd, "torture") == 0) {
1866be7d
MK
664 loff_t endoff;
665 unsigned int failed = 0, passed = 0;
666
3287f6d3
BT
667 if (argc < 3)
668 goto usage;
669
670 if (!str2off(argv[2], &off)) {
671 puts("Offset is not a valid number\n");
672 return 1;
673 }
674
1866be7d
MK
675 size = mtd->erasesize;
676 if (argc > 3) {
677 if (!str2off(argv[3], &size)) {
678 puts("Size is not a valid number\n");
679 return 1;
680 }
681 }
3287f6d3 682
1866be7d
MK
683 endoff = off + size;
684 if (endoff > mtd->size) {
685 puts("Arguments beyond end of NAND\n");
686 return 1;
687 }
688
689 off = round_down(off, mtd->erasesize);
690 endoff = round_up(endoff, mtd->erasesize);
691 size = endoff - off;
692 printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n",
693 dev, off, size, mtd->erasesize);
694 while (off < endoff) {
695 ret = nand_torture(mtd, off);
696 if (ret) {
697 failed++;
698 printf(" block at 0x%llx failed\n", off);
699 } else {
700 passed++;
701 }
702 off += mtd->erasesize;
703 }
704 printf(" Passed: %u, failed: %u\n", passed, failed);
705 return failed != 0;
3287f6d3
BT
706 }
707#endif
708
2255b2d2 709 if (strcmp(cmd, "markbad") == 0) {
8360b66b
WD
710 argc -= 2;
711 argv += 2;
2255b2d2 712
8360b66b
WD
713 if (argc <= 0)
714 goto usage;
715
716 while (argc > 0) {
7e5f460e 717 addr = hextoul(*argv, NULL);
8360b66b 718
151c06ec 719 if (mtd_block_markbad(mtd, addr)) {
8360b66b
WD
720 printf("block 0x%08lx NOT marked "
721 "as bad! ERROR %d\n",
722 addr, ret);
723 ret = 1;
724 } else {
725 printf("block 0x%08lx successfully "
726 "marked as bad\n",
727 addr);
728 }
729 --argc;
730 ++argv;
2255b2d2 731 }
8360b66b 732 return ret;
2255b2d2 733 }
dfbf617f 734
2255b2d2
SR
735 if (strcmp(cmd, "biterr") == 0) {
736 /* todo */
737 return 1;
738 }
739
50657c27 740#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
2255b2d2 741 if (strcmp(cmd, "lock") == 0) {
50657c27 742 int tight = 0;
2255b2d2
SR
743 int status = 0;
744 if (argc == 3) {
745 if (!strcmp("tight", argv[2]))
746 tight = 1;
747 if (!strcmp("status", argv[2]))
748 status = 1;
749 }
2255b2d2 750 if (status) {
151c06ec 751 do_nand_status(mtd);
cfa460ad 752 } else {
151c06ec 753 if (!nand_lock(mtd, tight)) {
5e1dae5c
WJ
754 puts("NAND flash successfully locked\n");
755 } else {
756 puts("Error locking NAND flash\n");
757 return 1;
758 }
2255b2d2
SR
759 }
760 return 0;
761 }
762
eee623a5
JH
763 if (strncmp(cmd, "unlock", 5) == 0) {
764 int allexcept = 0;
765
766 s = strchr(cmd, '.');
767
768 if (s && !strcmp(s, ".allexcept"))
769 allexcept = 1;
770
09c32807
HS
771 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
772 &maxsize, MTD_DEV_TYPE_NAND,
ad92dff2 773 mtd->size) < 0)
09c32807
HS
774 return 1;
775
776 if (set_dev(dev))
856f0544 777 return 1;
2255b2d2 778
ad92dff2
M
779 mtd = get_nand_dev_by_index(dev);
780
781 if (!nand_unlock(mtd, off, size, allexcept)) {
5e1dae5c
WJ
782 puts("NAND flash successfully unlocked\n");
783 } else {
784 puts("Error unlocking NAND flash, "
3043c045 785 "write and erase will probably fail\n");
5e1dae5c
WJ
786 return 1;
787 }
2255b2d2
SR
788 return 0;
789 }
50657c27 790#endif
2255b2d2 791
addb2e16 792usage:
4c12eeb8 793 return CMD_RET_USAGE;
addb2e16
BS
794}
795
088f1b19
KP
796#ifdef CONFIG_SYS_LONGHELP
797static char nand_help_text[] =
a89c33db
WD
798 "info - show available NAND devices\n"
799 "nand device [dev] - show or set current device\n"
800 "nand read - addr off|partition size\n"
801 "nand write - addr off|partition size\n"
802 " read/write 'size' bytes starting at offset 'off'\n"
803 " to/from memory address 'addr', skipping bad blocks.\n"
418396e2 804 "nand read.raw - addr off|partition [count]\n"
2dc3c483 805 "nand write.raw[.noverify] - addr off|partition [count]\n"
418396e2 806 " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
c9494866
BG
807#ifdef CONFIG_CMD_NAND_TRIMFFS
808 "nand write.trimffs - addr off|partition size\n"
809 " write 'size' bytes starting at offset 'off' from memory address\n"
810 " 'addr', skipping bad blocks and dropping any pages at the end\n"
811 " of eraseblocks that contain only 0xFF\n"
47fc18f1 812#endif
eb3abce8 813 "nand erase[.spread] [clean] off size - erase 'size' bytes "
30486322
SW
814 "from offset 'off'\n"
815 " With '.spread', erase enough for given file size, otherwise,\n"
816 " 'size' includes skipped bad blocks.\n"
817 "nand erase.part [clean] partition - erase entire mtd partition'\n"
818 "nand erase.chip [clean] - erase entire chip'\n"
a89c33db
WD
819 "nand bad - show bad blocks\n"
820 "nand dump[.oob] off - dump page\n"
3287f6d3 821#ifdef CONFIG_CMD_NAND_TORTURE
1866be7d
MK
822 "nand torture off - torture one block at offset\n"
823 "nand torture off [size] - torture blocks from off to off+size\n"
3287f6d3 824#endif
60899816 825 "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
30486322 826 " really clean NAND erasing bad blocks (UNSAFE)\n"
a89c33db
WD
827 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
828 "nand biterr off - make a bit error at offset (UNSAFE)"
50657c27 829#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
a89c33db
WD
830 "\n"
831 "nand lock [tight] [status]\n"
832 " bring nand to lock state or display locked pages\n"
eee623a5 833 "nand unlock[.allexcept] [offset] [size] - unlock section"
50657c27 834#endif
c9f7351b
BG
835#ifdef CONFIG_ENV_OFFSET_OOB
836 "\n"
837 "nand env.oob - environment offset in OOB of block 0 of"
838 " first device.\n"
839 "nand env.oob set off|partition - set enviromnent offset\n"
840 "nand env.oob get - get environment offset"
841#endif
088f1b19
KP
842 "";
843#endif
844
845U_BOOT_CMD(
846 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
847 "NAND sub-system", nand_help_text
50657c27 848);
addb2e16 849
09140113 850static int nand_load_image(struct cmd_tbl *cmdtp, struct mtd_info *mtd,
4b070809 851 ulong offset, ulong addr, char *cmd)
addb2e16 852{
addb2e16 853 int r;
67d668bf 854 char *s;
4ca79f47 855 size_t cnt;
c76c93a3 856#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
addb2e16 857 image_header_t *hdr;
21d29f7f 858#endif
09475f75 859#if defined(CONFIG_FIT)
3bab76a2 860 const void *fit_hdr = NULL;
09475f75 861#endif
10e03893
TK
862
863 s = strchr(cmd, '.');
864 if (s != NULL &&
65d8bc94 865 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
984e03cd 866 printf("Unknown nand load suffix '%s'\n", s);
770605e4 867 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
984e03cd
SW
868 return 1;
869 }
addb2e16 870
151c06ec 871 printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset);
addb2e16 872
151c06ec
SW
873 cnt = mtd->writesize;
874 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
875 (u_char *)addr);
addb2e16 876 if (r) {
856f0544 877 puts("** Read error\n");
770605e4 878 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
addb2e16
BS
879 return 1;
880 }
770605e4 881 bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
addb2e16 882
9a4daad0 883 switch (genimg_get_format ((void *)addr)) {
c76c93a3 884#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
d5934ad7
MB
885 case IMAGE_FORMAT_LEGACY:
886 hdr = (image_header_t *)addr;
887
770605e4 888 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
d5934ad7 889 image_print_contents (hdr);
addb2e16 890
d5934ad7
MB
891 cnt = image_get_image_size (hdr);
892 break;
21d29f7f 893#endif
d5934ad7
MB
894#if defined(CONFIG_FIT)
895 case IMAGE_FORMAT_FIT:
09475f75 896 fit_hdr = (const void *)addr;
09475f75
MB
897 puts ("Fit image detected...\n");
898
899 cnt = fit_get_size (fit_hdr);
900 break;
d5934ad7
MB
901#endif
902 default:
770605e4 903 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
d5934ad7 904 puts ("** Unknown image type\n");
addb2e16
BS
905 return 1;
906 }
770605e4 907 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
addb2e16 908
151c06ec
SW
909 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
910 (u_char *)addr);
addb2e16 911 if (r) {
856f0544 912 puts("** Read error\n");
770605e4 913 bootstage_error(BOOTSTAGE_ID_NAND_READ);
addb2e16
BS
914 return 1;
915 }
770605e4 916 bootstage_mark(BOOTSTAGE_ID_NAND_READ);
addb2e16 917
09475f75
MB
918#if defined(CONFIG_FIT)
919 /* This cannot be done earlier, we need complete FIT image in RAM first */
3bab76a2 920 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
c5819701 921 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
770605e4 922 bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
3bab76a2
MB
923 puts ("** Bad FIT image format\n");
924 return 1;
925 }
770605e4 926 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
3bab76a2
MB
927 fit_print_contents (fit_hdr);
928 }
09475f75
MB
929#endif
930
addb2e16
BS
931 /* Loading ok, update default load address */
932
bb872dd9 933 image_load_addr = addr;
addb2e16 934
67d668bf 935 return bootm_maybe_autostart(cmdtp, cmd);
addb2e16
BS
936}
937
09140113
SG
938static int do_nandboot(struct cmd_tbl *cmdtp, int flag, int argc,
939 char *const argv[])
856f0544
SR
940{
941 char *boot_device = NULL;
942 int idx;
943 ulong addr, offset = 0;
ad92dff2 944 struct mtd_info *mtd;
0c8a8491 945#if defined(CONFIG_CMD_MTDPARTS)
856f0544
SR
946 struct mtd_device *dev;
947 struct part_info *part;
948 u8 pnum;
949
950 if (argc >= 2) {
951 char *p = (argc == 2) ? argv[1] : argv[2];
952 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
953 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
954 if (dev->id->type != MTD_DEV_TYPE_NAND) {
955 puts("Not a NAND device\n");
956 return 1;
957 }
958 if (argc > 3)
959 goto usage;
960 if (argc == 3)
7e5f460e 961 addr = hextoul(argv[1], NULL);
856f0544 962 else
6d0f6bcf 963 addr = CONFIG_SYS_LOAD_ADDR;
ad92dff2
M
964
965 mtd = get_nand_dev_by_index(dev->id->num);
966 return nand_load_image(cmdtp, mtd, part->offset,
967 addr, argv[0]);
856f0544
SR
968 }
969 }
970#endif
971
770605e4 972 bootstage_mark(BOOTSTAGE_ID_NAND_PART);
856f0544
SR
973 switch (argc) {
974 case 1:
6d0f6bcf 975 addr = CONFIG_SYS_LOAD_ADDR;
00caae6d 976 boot_device = env_get("bootdevice");
856f0544
SR
977 break;
978 case 2:
7e5f460e 979 addr = hextoul(argv[1], NULL);
00caae6d 980 boot_device = env_get("bootdevice");
856f0544
SR
981 break;
982 case 3:
7e5f460e 983 addr = hextoul(argv[1], NULL);
856f0544
SR
984 boot_device = argv[2];
985 break;
986 case 4:
7e5f460e 987 addr = hextoul(argv[1], NULL);
856f0544 988 boot_device = argv[2];
7e5f460e 989 offset = hextoul(argv[3], NULL);
856f0544
SR
990 break;
991 default:
0c8a8491 992#if defined(CONFIG_CMD_MTDPARTS)
856f0544
SR
993usage:
994#endif
770605e4 995 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
4c12eeb8 996 return CMD_RET_USAGE;
856f0544 997 }
770605e4 998 bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
856f0544
SR
999
1000 if (!boot_device) {
1001 puts("\n** No boot device **\n");
770605e4 1002 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
856f0544
SR
1003 return 1;
1004 }
770605e4 1005 bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
addb2e16 1006
7e5f460e 1007 idx = hextoul(boot_device, NULL);
856f0544 1008
ad92dff2
M
1009 mtd = get_nand_dev_by_index(idx);
1010 if (!mtd) {
856f0544 1011 printf("\n** Device %d not available\n", idx);
770605e4 1012 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
856f0544
SR
1013 return 1;
1014 }
770605e4 1015 bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
856f0544 1016
ad92dff2 1017 return nand_load_image(cmdtp, mtd, offset, addr, argv[0]);
856f0544
SR
1018}
1019
1020U_BOOT_CMD(nboot, 4, 1, do_nandboot,
2fb2604d 1021 "boot from NAND device",
a89c33db
WD
1022 "[partition] | [[[loadAddr] dev] offset]"
1023);
This page took 0.726674 seconds and 4 git commands to generate.