efi_loader: avoid multiple local copies of lf2_initrd_guid
[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 <linux/mtd/rawnand.h>
28 #include <command.h>
29 #include <console.h>
30 #include <env.h>
31 #include <watchdog.h>
32 #include <malloc.h>
33 #include <asm/byteorder.h>
34 #include <jffs2/jffs2.h>
35 #include <nand.h>
36
37 #include "legacy-mtd-utils.h"
38
39 #if defined(CONFIG_CMD_MTDPARTS)
40
41 /* partition handling routines */
42 int mtdparts_init(void);
43 int find_dev_and_part(const char *id, struct mtd_device **dev,
44                       u8 *part_num, struct part_info **part);
45 #endif
46
47 static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
48                      int repeat)
49 {
50         int i;
51         u_char *datbuf, *oobbuf, *p;
52         static loff_t last;
53         int ret = 0;
54
55         if (repeat)
56                 off = last + mtd->writesize;
57
58         last = off;
59
60         datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize);
61         if (!datbuf) {
62                 puts("No memory for page buffer\n");
63                 return 1;
64         }
65
66         oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize);
67         if (!oobbuf) {
68                 puts("No memory for page buffer\n");
69                 ret = 1;
70                 goto free_dat;
71         }
72         off &= ~(mtd->writesize - 1);
73         loff_t addr = (loff_t) off;
74         struct mtd_oob_ops ops;
75         memset(&ops, 0, sizeof(ops));
76         ops.datbuf = datbuf;
77         ops.oobbuf = oobbuf;
78         ops.len = mtd->writesize;
79         ops.ooblen = mtd->oobsize;
80         ops.mode = MTD_OPS_RAW;
81         i = mtd_read_oob(mtd, addr, &ops);
82         if (i < 0) {
83                 printf("Error (%d) reading page %08lx\n", i, off);
84                 ret = 1;
85                 goto free_all;
86         }
87         printf("Page %08lx dump:\n", off);
88
89         if (!only_oob) {
90                 i = mtd->writesize >> 4;
91                 p = datbuf;
92
93                 while (i--) {
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],
97                                p[8], p[9], p[10], p[11], p[12], p[13], p[14],
98                                p[15]);
99                         p += 16;
100                 }
101         }
102
103         puts("OOB:\n");
104         i = mtd->oobsize >> 3;
105         p = oobbuf;
106         while (i--) {
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]);
109                 p += 8;
110         }
111
112 free_all:
113         free(oobbuf);
114 free_dat:
115         free(datbuf);
116
117         return ret;
118 }
119
120 /* ------------------------------------------------------------------------- */
121
122 static int set_dev(int dev)
123 {
124         struct mtd_info *mtd = get_nand_dev_by_index(dev);
125
126         if (!mtd)
127                 return -ENODEV;
128
129         if (nand_curr_device == dev)
130                 return 0;
131
132         printf("Device %d: %s", dev, mtd->name);
133         puts("... is now current device\n");
134         nand_curr_device = dev;
135
136 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
137         board_nand_select_device(mtd_to_nand(mtd), dev);
138 #endif
139
140         return 0;
141 }
142
143 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
144 static void print_status(ulong start, ulong end, ulong erasesize, int status)
145 {
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          */
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 " : ""),
157                 (!(status & NAND_LOCK_STATUS_UNLOCK) ?  "LOCK " : ""),
158                 ((status & NAND_LOCK_STATUS_UNLOCK) ?  "UNLOCK " : ""));
159 }
160
161 static void do_nand_status(struct mtd_info *mtd)
162 {
163         ulong block_start = 0;
164         ulong off;
165         int last_status = -1;
166
167         struct nand_chip *nand_chip = mtd_to_nand(mtd);
168         /* check the WP bit */
169         nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
170         printf("device is %swrite protected\n",
171                 (nand_chip->read_byte(mtd) & 0x80 ?
172                  "NOT " : ""));
173
174         for (off = 0; off < mtd->size; off += mtd->erasesize) {
175                 int s = nand_get_lock_status(mtd, off);
176
177                 /* print message only if status has changed */
178                 if (s != last_status && off != 0) {
179                         print_status(block_start, off, mtd->erasesize,
180                                         last_status);
181                         block_start = off;
182                 }
183                 last_status = s;
184         }
185         /* Print the last block info */
186         print_status(block_start, off, mtd->erasesize, last_status);
187 }
188 #endif
189
190 #ifdef CONFIG_ENV_OFFSET_OOB
191 unsigned long nand_env_oob_offset;
192
193 int do_nand_env_oob(struct cmd_tbl *cmdtp, int argc, char *const argv[])
194 {
195         int ret;
196         uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
197         struct mtd_info *mtd = get_nand_dev_by_index(0);
198         char *cmd = argv[1];
199
200         if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) {
201                 puts("no devices available\n");
202                 return 1;
203         }
204
205         set_dev(0);
206
207         if (!strcmp(cmd, "get")) {
208                 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
209                 if (ret)
210                         return 1;
211
212                 printf("0x%08lx\n", nand_env_oob_offset);
213         } else if (!strcmp(cmd, "set")) {
214                 loff_t addr;
215                 loff_t maxsize;
216                 struct mtd_oob_ops ops;
217                 int idx = 0;
218
219                 if (argc < 3)
220                         goto usage;
221
222                 mtd = get_nand_dev_by_index(idx);
223                 /* We don't care about size, or maxsize. */
224                 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
225                                 MTD_DEV_TYPE_NAND, mtd->size)) {
226                         puts("Offset or partition name expected\n");
227                         return 1;
228                 }
229                 if (set_dev(idx)) {
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");
236                         return 1;
237                 }
238
239                 if (mtd->oobavail < ENV_OFFSET_SIZE) {
240                         printf("Insufficient available OOB bytes:\n"
241                                "%d OOB bytes available but %d required for "
242                                "env.oob support\n",
243                                mtd->oobavail, ENV_OFFSET_SIZE);
244                         return 1;
245                 }
246
247                 if ((addr & (mtd->erasesize - 1)) != 0) {
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;
259                 oob_buf[1] = addr / mtd->erasesize;
260
261                 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops);
262                 if (ret) {
263                         printf("Error writing OOB block 0\n");
264                         return ret;
265                 }
266
267                 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
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: "
275                                "0x%08llx expected but got 0x%08lx\n",
276                                (unsigned long long)addr, nand_env_oob_offset);
277                         return 1;
278                 }
279         } else {
280                 goto usage;
281         }
282
283         return ret;
284
285 usage:
286         return CMD_RET_USAGE;
287 }
288
289 #endif
290
291 static void nand_print_and_set_info(int idx)
292 {
293         struct mtd_info *mtd;
294         struct nand_chip *chip;
295
296         mtd = get_nand_dev_by_index(idx);
297         if (!mtd)
298                 return;
299
300         chip = mtd_to_nand(mtd);
301         printf("Device %d: ", idx);
302         if (chip->numchips > 1)
303                 printf("%dx ", chip->numchips);
304         printf("%s, sector size %u KiB\n",
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);
309         printf("  subpagesize %8d b\n", chip->subpagesize);
310         printf("  options     0x%08x\n", chip->options);
311         printf("  bbt options 0x%08x\n", chip->bbt_options);
312
313         /* Set geometry info */
314         env_set_hex("nand_writesize", mtd->writesize);
315         env_set_hex("nand_oobsize", mtd->oobsize);
316         env_set_hex("nand_erasesize", mtd->erasesize);
317 }
318
319 static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
320                       ulong count, int read, int no_verify)
321 {
322         int ret = 0;
323
324         while (count--) {
325                 /* Raw access */
326                 mtd_oob_ops_t ops = {
327                         .datbuf = (u8 *)addr,
328                         .oobbuf = ((u8 *)addr) + mtd->writesize,
329                         .len = mtd->writesize,
330                         .ooblen = mtd->oobsize,
331                         .mode = MTD_OPS_RAW
332                 };
333
334                 if (read) {
335                         ret = mtd_read_oob(mtd, off, &ops);
336                 } else {
337                         ret = mtd_write_oob(mtd, off, &ops);
338                         if (!ret && !no_verify)
339                                 ret = nand_verify_page_oob(mtd, &ops, off);
340                 }
341
342                 if (ret) {
343                         printf("%s: error at offset %llx, ret %d\n",
344                                 __func__, (long long)off, ret);
345                         break;
346                 }
347
348                 addr += mtd->writesize + mtd->oobsize;
349                 off += mtd->writesize;
350         }
351
352         return ret;
353 }
354
355 /* Adjust a chip/partition size down for bad blocks so we don't
356  * read/write past the end of a chip/partition by accident.
357  */
358 static 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          */
363         struct mtd_info *mtd = get_nand_dev_by_index(dev);
364         loff_t maxoffset = offset + *size;
365         int badblocks = 0;
366
367         /* count badblocks in NAND from offset to offset + size */
368         for (; offset < maxoffset; offset += mtd->erasesize) {
369                 if (nand_block_isbad(mtd, offset))
370                         badblocks++;
371         }
372         /* adjust size if any bad blocks found */
373         if (badblocks) {
374                 *size -= badblocks * mtd->erasesize;
375                 printf("size adjusted to 0x%llx (%d bad blocks)\n",
376                        (unsigned long long)*size, badblocks);
377         }
378 }
379
380 static int do_nand(struct cmd_tbl *cmdtp, int flag, int argc,
381                    char *const argv[])
382 {
383         int i, ret = 0;
384         ulong addr;
385         loff_t off, size, maxsize;
386         char *cmd, *s;
387         struct mtd_info *mtd;
388 #ifdef CONFIG_SYS_NAND_QUIET
389         int quiet = CONFIG_SYS_NAND_QUIET;
390 #else
391         int quiet = 0;
392 #endif
393         const char *quiet_str = env_get("quiet");
394         int dev = nand_curr_device;
395         int repeat = flag & CMD_FLAG_REPEAT;
396
397         /* at least two arguments please */
398         if (argc < 2)
399                 goto usage;
400
401         if (quiet_str)
402                 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
403
404         cmd = argv[1];
405
406         /* Only "dump" is repeatable. */
407         if (repeat && strcmp(cmd, "dump"))
408                 return 0;
409
410         if (strcmp(cmd, "info") == 0) {
411
412                 putc('\n');
413                 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
414                         nand_print_and_set_info(i);
415                 return 0;
416         }
417
418         if (strcmp(cmd, "device") == 0) {
419                 if (argc < 3) {
420                         putc('\n');
421                         if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
422                                 puts("no devices available\n");
423                         else
424                                 nand_print_and_set_info(dev);
425                         return 0;
426                 }
427
428                 dev = (int)dectoul(argv[2], NULL);
429                 set_dev(dev);
430
431                 return 0;
432         }
433
434 #ifdef CONFIG_ENV_OFFSET_OOB
435         /* this command operates only on the first nand device */
436         if (strcmp(cmd, "env.oob") == 0)
437                 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
438 #endif
439
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          */
446         mtd = get_nand_dev_by_index(dev);
447         if (!mtd) {
448                 puts("\nno devices available\n");
449                 return 1;
450         }
451
452         if (strcmp(cmd, "bad") == 0) {
453                 printf("\nDevice %d bad blocks:\n", dev);
454                 for (off = 0; off < mtd->size; off += mtd->erasesize)
455                         if (nand_block_isbad(mtd, off))
456                                 printf("  %08llx\n", (unsigned long long)off);
457                 return 0;
458         }
459
460         /*
461          * Syntax is:
462          *   0    1     2       3    4
463          *   nand erase [clean] [off size]
464          */
465         if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
466                 nand_erase_options_t opts;
467                 /* "clean" at index 2 means request to write cleanmarker */
468                 int clean = argc > 2 && !strcmp("clean", argv[2]);
469                 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
470                 int o = (clean || scrub_yes) ? 3 : 2;
471                 int scrub = !strncmp(cmd, "scrub", 5);
472                 int spread = 0;
473                 int args = 2;
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";
484
485                 if (cmd[5] != 0) {
486                         if (!strcmp(&cmd[5], ".spread")) {
487                                 spread = 1;
488                         } else if (!strcmp(&cmd[5], ".part")) {
489                                 args = 1;
490                         } else if (!strcmp(&cmd[5], ".chip")) {
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;
504
505                 printf("\nNAND %s: ", cmd);
506                 /* skip first two or three arguments, look for offset and size */
507                 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
508                                      &maxsize, MTD_DEV_TYPE_NAND,
509                                      mtd->size) != 0)
510                         return 1;
511
512                 if (set_dev(dev))
513                         return 1;
514
515                 mtd = get_nand_dev_by_index(dev);
516
517                 memset(&opts, 0, sizeof(opts));
518                 opts.offset = off;
519                 opts.length = size;
520                 opts.jffs2  = clean;
521                 opts.quiet  = quiet;
522                 opts.spread = spread;
523
524                 if (scrub) {
525                         if (scrub_yes) {
526                                 opts.scrub = 1;
527                         } else {
528                                 puts(scrub_warn);
529                                 if (confirm_yesno()) {
530                                         opts.scrub = 1;
531                                 } else {
532                                         puts("scrub aborted\n");
533                                         return 1;
534                                 }
535                         }
536                 }
537                 ret = nand_erase_opts(mtd, &opts);
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
547                 off = (int)hextoul(argv[2], NULL);
548                 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
549
550                 return ret == 0 ? 1 : 0;
551         }
552
553         if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
554                 size_t rwsize;
555                 ulong pagecount = 1;
556                 int read;
557                 int raw = 0;
558                 int no_verify = 0;
559
560                 if (argc < 4)
561                         goto usage;
562
563                 addr = (ulong)hextoul(argv[2], NULL);
564
565                 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
566                 printf("\nNAND %s: ", read ? "read" : "write");
567
568                 s = strchr(cmd, '.');
569
570                 if (s && !strncmp(s, ".raw", 4)) {
571                         raw = 1;
572
573                         if (!strcmp(s, ".raw.noverify"))
574                                 no_verify = 1;
575
576                         if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
577                                         MTD_DEV_TYPE_NAND,
578                                         mtd->size))
579                                 return 1;
580
581                         if (set_dev(dev))
582                                 return 1;
583
584                         mtd = get_nand_dev_by_index(dev);
585
586                         if (argc > 4 && !str2long(argv[4], &pagecount)) {
587                                 printf("'%s' is not a number\n", argv[4]);
588                                 return 1;
589                         }
590
591                         if (pagecount * mtd->writesize > size) {
592                                 puts("Size exceeds partition or device limit\n");
593                                 return -1;
594                         }
595
596                         rwsize = pagecount * (mtd->writesize + mtd->oobsize);
597                 } else {
598                         if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
599                                              &size, &maxsize,
600                                              MTD_DEV_TYPE_NAND,
601                                              mtd->size) != 0)
602                                 return 1;
603
604                         if (set_dev(dev))
605                                 return 1;
606
607                         /* size is unspecified */
608                         if (argc < 5)
609                                 adjust_size_for_badblocks(&size, off, dev);
610                         rwsize = size;
611                 }
612
613                 mtd = get_nand_dev_by_index(dev);
614
615                 if (!s || !strcmp(s, ".jffs2") ||
616                     !strcmp(s, ".e") || !strcmp(s, ".i")) {
617                         if (read)
618                                 ret = nand_read_skip_bad(mtd, off, &rwsize,
619                                                          NULL, maxsize,
620                                                          (u_char *)addr);
621                         else
622                                 ret = nand_write_skip_bad(mtd, off, &rwsize,
623                                                           NULL, maxsize,
624                                                           (u_char *)addr,
625                                                           WITH_WR_VERIFY);
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                         }
632                         ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
633                                                 maxsize, (u_char *)addr,
634                                                 WITH_DROP_FFS | WITH_WR_VERIFY);
635 #endif
636                 } else if (!strcmp(s, ".oob")) {
637                         /* out-of-band data */
638                         mtd_oob_ops_t ops = {
639                                 .oobbuf = (u8 *)addr,
640                                 .ooblen = rwsize,
641                                 .mode = MTD_OPS_RAW
642                         };
643
644                         if (read)
645                                 ret = mtd_read_oob(mtd, off, &ops);
646                         else
647                                 ret = mtd_write_oob(mtd, off, &ops);
648                 } else if (raw) {
649                         ret = raw_access(mtd, addr, off, pagecount, read,
650                                          no_verify);
651                 } else {
652                         printf("Unknown nand command suffix '%s'.\n", s);
653                         return 1;
654                 }
655
656                 printf(" %zu bytes %s: %s\n", rwsize,
657                        read ? "read" : "written", ret ? "ERROR" : "OK");
658
659                 return ret == 0 ? 0 : 1;
660         }
661
662 #ifdef CONFIG_CMD_NAND_TORTURE
663         if (strcmp(cmd, "torture") == 0) {
664                 loff_t endoff;
665                 unsigned int failed = 0, passed = 0;
666
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
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                 }
682
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;
706         }
707 #endif
708
709         if (strcmp(cmd, "markbad") == 0) {
710                 argc -= 2;
711                 argv += 2;
712
713                 if (argc <= 0)
714                         goto usage;
715
716                 while (argc > 0) {
717                         addr = hextoul(*argv, NULL);
718
719                         if (mtd_block_markbad(mtd, addr)) {
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;
731                 }
732                 return ret;
733         }
734
735         if (strcmp(cmd, "biterr") == 0) {
736                 /* todo */
737                 return 1;
738         }
739
740 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
741         if (strcmp(cmd, "lock") == 0) {
742                 int tight = 0;
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                 }
750                 if (status) {
751                         do_nand_status(mtd);
752                 } else {
753                         if (!nand_lock(mtd, tight)) {
754                                 puts("NAND flash successfully locked\n");
755                         } else {
756                                 puts("Error locking NAND flash\n");
757                                 return 1;
758                         }
759                 }
760                 return 0;
761         }
762
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
771                 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
772                                      &maxsize, MTD_DEV_TYPE_NAND,
773                                      mtd->size) < 0)
774                         return 1;
775
776                 if (set_dev(dev))
777                         return 1;
778
779                 mtd = get_nand_dev_by_index(dev);
780
781                 if (!nand_unlock(mtd, off, size, allexcept)) {
782                         puts("NAND flash successfully unlocked\n");
783                 } else {
784                         puts("Error unlocking NAND flash, "
785                              "write and erase will probably fail\n");
786                         return 1;
787                 }
788                 return 0;
789         }
790 #endif
791
792 usage:
793         return CMD_RET_USAGE;
794 }
795
796 #ifdef CONFIG_SYS_LONGHELP
797 static char nand_help_text[] =
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"
804         "nand read.raw - addr off|partition [count]\n"
805         "nand write.raw[.noverify] - addr off|partition [count]\n"
806         "    Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
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"
812 #endif
813         "nand erase[.spread] [clean] off size - erase 'size' bytes "
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"
819         "nand bad - show bad blocks\n"
820         "nand dump[.oob] off - dump page\n"
821 #ifdef CONFIG_CMD_NAND_TORTURE
822         "nand torture off - torture one block at offset\n"
823         "nand torture off [size] - torture blocks from off to off+size\n"
824 #endif
825         "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
826         "    really clean NAND erasing bad blocks (UNSAFE)\n"
827         "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
828         "nand biterr off - make a bit error at offset (UNSAFE)"
829 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
830         "\n"
831         "nand lock [tight] [status]\n"
832         "    bring nand to lock state or display locked pages\n"
833         "nand unlock[.allexcept] [offset] [size] - unlock section"
834 #endif
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
842         "";
843 #endif
844
845 U_BOOT_CMD(
846         nand, CONFIG_SYS_MAXARGS, 1, do_nand,
847         "NAND sub-system", nand_help_text
848 );
849
850 static int nand_load_image(struct cmd_tbl *cmdtp, struct mtd_info *mtd,
851                            ulong offset, ulong addr, char *cmd)
852 {
853         int r;
854         char *s;
855         size_t cnt;
856 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
857         image_header_t *hdr;
858 #endif
859 #if defined(CONFIG_FIT)
860         const void *fit_hdr = NULL;
861 #endif
862
863         s = strchr(cmd, '.');
864         if (s != NULL &&
865             (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
866                 printf("Unknown nand load suffix '%s'\n", s);
867                 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
868                 return 1;
869         }
870
871         printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset);
872
873         cnt = mtd->writesize;
874         r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
875                                (u_char *)addr);
876         if (r) {
877                 puts("** Read error\n");
878                 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
879                 return 1;
880         }
881         bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
882
883         switch (genimg_get_format ((void *)addr)) {
884 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
885         case IMAGE_FORMAT_LEGACY:
886                 hdr = (image_header_t *)addr;
887
888                 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
889                 image_print_contents (hdr);
890
891                 cnt = image_get_image_size (hdr);
892                 break;
893 #endif
894 #if defined(CONFIG_FIT)
895         case IMAGE_FORMAT_FIT:
896                 fit_hdr = (const void *)addr;
897                 puts ("Fit image detected...\n");
898
899                 cnt = fit_get_size (fit_hdr);
900                 break;
901 #endif
902         default:
903                 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
904                 puts ("** Unknown image type\n");
905                 return 1;
906         }
907         bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
908
909         r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
910                                (u_char *)addr);
911         if (r) {
912                 puts("** Read error\n");
913                 bootstage_error(BOOTSTAGE_ID_NAND_READ);
914                 return 1;
915         }
916         bootstage_mark(BOOTSTAGE_ID_NAND_READ);
917
918 #if defined(CONFIG_FIT)
919         /* This cannot be done earlier, we need complete FIT image in RAM first */
920         if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
921                 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
922                         bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
923                         puts ("** Bad FIT image format\n");
924                         return 1;
925                 }
926                 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
927                 fit_print_contents (fit_hdr);
928         }
929 #endif
930
931         /* Loading ok, update default load address */
932
933         image_load_addr = addr;
934
935         return bootm_maybe_autostart(cmdtp, cmd);
936 }
937
938 static int do_nandboot(struct cmd_tbl *cmdtp, int flag, int argc,
939                        char *const argv[])
940 {
941         char *boot_device = NULL;
942         int idx;
943         ulong addr, offset = 0;
944         struct mtd_info *mtd;
945 #if defined(CONFIG_CMD_MTDPARTS)
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)
961                                 addr = hextoul(argv[1], NULL);
962                         else
963                                 addr = CONFIG_SYS_LOAD_ADDR;
964
965                         mtd = get_nand_dev_by_index(dev->id->num);
966                         return nand_load_image(cmdtp, mtd, part->offset,
967                                                addr, argv[0]);
968                 }
969         }
970 #endif
971
972         bootstage_mark(BOOTSTAGE_ID_NAND_PART);
973         switch (argc) {
974         case 1:
975                 addr = CONFIG_SYS_LOAD_ADDR;
976                 boot_device = env_get("bootdevice");
977                 break;
978         case 2:
979                 addr = hextoul(argv[1], NULL);
980                 boot_device = env_get("bootdevice");
981                 break;
982         case 3:
983                 addr = hextoul(argv[1], NULL);
984                 boot_device = argv[2];
985                 break;
986         case 4:
987                 addr = hextoul(argv[1], NULL);
988                 boot_device = argv[2];
989                 offset = hextoul(argv[3], NULL);
990                 break;
991         default:
992 #if defined(CONFIG_CMD_MTDPARTS)
993 usage:
994 #endif
995                 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
996                 return CMD_RET_USAGE;
997         }
998         bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
999
1000         if (!boot_device) {
1001                 puts("\n** No boot device **\n");
1002                 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
1003                 return 1;
1004         }
1005         bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
1006
1007         idx = hextoul(boot_device, NULL);
1008
1009         mtd = get_nand_dev_by_index(idx);
1010         if (!mtd) {
1011                 printf("\n** Device %d not available\n", idx);
1012                 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
1013                 return 1;
1014         }
1015         bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
1016
1017         return nand_load_image(cmdtp, mtd, offset, addr, argv[0]);
1018 }
1019
1020 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
1021         "boot from NAND device",
1022         "[partition] | [[[loadAddr] dev] offset]"
1023 );