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