]> git.sur5r.net Git - u-boot/blob - common/cmd_nand.c
Merge branch 'master' of ../master
[u-boot] / common / 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
15 #include <common.h>
16 #include <linux/mtd/mtd.h>
17 #include <command.h>
18 #include <watchdog.h>
19 #include <malloc.h>
20 #include <asm/byteorder.h>
21 #include <jffs2/jffs2.h>
22 #include <nand.h>
23
24 #if defined(CONFIG_CMD_MTDPARTS)
25
26 /* partition handling routines */
27 int mtdparts_init(void);
28 int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
29 int find_dev_and_part(const char *id, struct mtd_device **dev,
30                       u8 *part_num, struct part_info **part);
31 #endif
32
33 static int nand_dump(nand_info_t *nand, ulong off, int only_oob)
34 {
35         int i;
36         u_char *datbuf, *oobbuf, *p;
37
38         datbuf = malloc(nand->writesize + nand->oobsize);
39         oobbuf = malloc(nand->oobsize);
40         if (!datbuf || !oobbuf) {
41                 puts("No memory for page buffer\n");
42                 return 1;
43         }
44         off &= ~(nand->writesize - 1);
45         loff_t addr = (loff_t) off;
46         struct mtd_oob_ops ops;
47         memset(&ops, 0, sizeof(ops));
48         ops.datbuf = datbuf;
49         ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */
50         ops.len = nand->writesize;
51         ops.ooblen = nand->oobsize;
52         ops.mode = MTD_OOB_RAW;
53         i = nand->read_oob(nand, addr, &ops);
54         if (i < 0) {
55                 printf("Error (%d) reading page %08lx\n", i, off);
56                 free(datbuf);
57                 free(oobbuf);
58                 return 1;
59         }
60         printf("Page %08lx dump:\n", off);
61         i = nand->writesize >> 4;
62         p = datbuf;
63
64         while (i--) {
65                 if (!only_oob)
66                         printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
67                                "  %02x %02x %02x %02x %02x %02x %02x %02x\n",
68                                p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
69                                p[8], p[9], p[10], p[11], p[12], p[13], p[14],
70                                p[15]);
71                 p += 16;
72         }
73         puts("OOB:\n");
74         i = nand->oobsize >> 3;
75         while (i--) {
76                 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
77                        p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
78                 p += 8;
79         }
80         free(datbuf);
81         free(oobbuf);
82
83         return 0;
84 }
85
86 /* ------------------------------------------------------------------------- */
87
88 static inline int str2long(char *p, ulong *num)
89 {
90         char *endptr;
91
92         *num = simple_strtoul(p, &endptr, 16);
93         return (*p != '\0' && *endptr == '\0') ? 1 : 0;
94 }
95
96 static int
97 arg_off_size(int argc, char * const argv[], nand_info_t *nand, ulong *off, size_t *size)
98 {
99         int idx = nand_curr_device;
100 #if defined(CONFIG_CMD_MTDPARTS)
101         struct mtd_device *dev;
102         struct part_info *part;
103         u8 pnum;
104
105         if (argc >= 1 && !(str2long(argv[0], off))) {
106                 if ((mtdparts_init() == 0) &&
107                     (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
108                         if (dev->id->type != MTD_DEV_TYPE_NAND) {
109                                 puts("not a NAND device\n");
110                                 return -1;
111                         }
112                         *off = part->offset;
113                         if (argc >= 2) {
114                                 if (!(str2long(argv[1], (ulong *)size))) {
115                                         printf("'%s' is not a number\n", argv[1]);
116                                         return -1;
117                                 }
118                                 if (*size > part->size)
119                                         *size = part->size;
120                         } else {
121                                 *size = part->size;
122                         }
123                         idx = dev->id->num;
124                         *nand = nand_info[idx];
125                         goto out;
126                 }
127         }
128 #endif
129
130         if (argc >= 1) {
131                 if (!(str2long(argv[0], off))) {
132                         printf("'%s' is not a number\n", argv[0]);
133                         return -1;
134                 }
135         } else {
136                 *off = 0;
137         }
138
139         if (argc >= 2) {
140                 if (!(str2long(argv[1], (ulong *)size))) {
141                         printf("'%s' is not a number\n", argv[1]);
142                         return -1;
143                 }
144         } else {
145                 *size = nand->size - *off;
146         }
147
148 #if defined(CONFIG_CMD_MTDPARTS)
149 out:
150 #endif
151         printf("device %d ", idx);
152         if (*size == nand->size)
153                 puts("whole chip\n");
154         else
155                 printf("offset 0x%lx, size 0x%zx\n", *off, *size);
156         return 0;
157 }
158
159 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
160 static void print_status(ulong start, ulong end, ulong erasesize, int status)
161 {
162         printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
163                 start,
164                 end - 1,
165                 (end - start) / erasesize,
166                 ((status & NAND_LOCK_STATUS_TIGHT) ?  "TIGHT " : ""),
167                 ((status & NAND_LOCK_STATUS_LOCK) ?  "LOCK " : ""),
168                 ((status & NAND_LOCK_STATUS_UNLOCK) ?  "UNLOCK " : ""));
169 }
170
171 static void do_nand_status(nand_info_t *nand)
172 {
173         ulong block_start = 0;
174         ulong off;
175         int last_status = -1;
176
177         struct nand_chip *nand_chip = nand->priv;
178         /* check the WP bit */
179         nand_chip->cmdfunc(nand, NAND_CMD_STATUS, -1, -1);
180         printf("device is %swrite protected\n",
181                 (nand_chip->read_byte(nand) & 0x80 ?
182                 "NOT " : ""));
183
184         for (off = 0; off < nand->size; off += nand->erasesize) {
185                 int s = nand_get_lock_status(nand, off);
186
187                 /* print message only if status has changed */
188                 if (s != last_status && off != 0) {
189                         print_status(block_start, off, nand->erasesize,
190                                         last_status);
191                         block_start = off;
192                 }
193                 last_status = s;
194         }
195         /* Print the last block info */
196         print_status(block_start, off, nand->erasesize, last_status);
197 }
198 #endif
199
200 #ifdef CONFIG_ENV_OFFSET_OOB
201 unsigned long nand_env_oob_offset;
202
203 int do_nand_env_oob(cmd_tbl_t *cmdtp, nand_info_t *nand,
204                     int argc, char * const argv[])
205 {
206         int ret;
207         uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
208
209         char *cmd = argv[1];
210
211         if (!strcmp(cmd, "get")) {
212                 ret = get_nand_env_oob(nand, &nand_env_oob_offset);
213                 if (ret)
214                         return 1;
215
216                 printf("0x%08lx\n", nand_env_oob_offset);
217         } else if (!strcmp(cmd, "set")) {
218                 ulong addr;
219                 size_t dummy_size;
220                 struct mtd_oob_ops ops;
221
222                 if (argc < 3)
223                         goto usage;
224
225                 if (arg_off_size(argc - 2, argv + 2, nand, &addr,
226                                  &dummy_size) < 0) {
227                         printf("Offset or partition name expected\n");
228                         return 1;
229                 }
230
231                 if (nand->oobavail < ENV_OFFSET_SIZE) {
232                         printf("Insufficient available OOB bytes:\n"
233                                "%d OOB bytes available but %d required for "
234                                "env.oob support\n",
235                                nand->oobavail, ENV_OFFSET_SIZE);
236                         return 1;
237                 }
238
239                 if ((addr & (nand->erasesize - 1)) != 0) {
240                         printf("Environment offset must be block-aligned\n");
241                         return 1;
242                 }
243
244                 ops.datbuf = NULL;
245                 ops.mode = MTD_OOB_AUTO;
246                 ops.ooboffs = 0;
247                 ops.ooblen = ENV_OFFSET_SIZE;
248                 ops.oobbuf = (void *) oob_buf;
249
250                 oob_buf[0] = ENV_OOB_MARKER;
251                 oob_buf[1] = addr / nand->erasesize;
252
253                 ret = nand->write_oob(nand, ENV_OFFSET_SIZE, &ops);
254                 if (ret) {
255                         printf("Error writing OOB block 0\n");
256                         return ret;
257                 }
258
259                 ret = get_nand_env_oob(nand, &nand_env_oob_offset);
260                 if (ret) {
261                         printf("Error reading env offset in OOB\n");
262                         return ret;
263                 }
264
265                 if (addr != nand_env_oob_offset) {
266                         printf("Verification of env offset in OOB failed: "
267                                "0x%08lx expected but got 0x%08lx\n",
268                                addr, nand_env_oob_offset);
269                         return 1;
270                 }
271         } else {
272                 goto usage;
273         }
274
275         return ret;
276
277 usage:
278         return cmd_usage(cmdtp);
279 }
280
281 #endif
282
283 static void nand_print_info(int idx)
284 {
285         nand_info_t *nand = &nand_info[idx];
286         struct nand_chip *chip = nand->priv;
287         printf("Device %d: ", idx);
288         if (chip->numchips > 1)
289                 printf("%dx ", chip->numchips);
290         printf("%s, sector size %u KiB\n",
291                nand->name, nand->erasesize >> 10);
292 }
293
294 int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
295 {
296         int i, dev, ret = 0;
297         ulong addr, off;
298         size_t size;
299         char *cmd, *s;
300         nand_info_t *nand;
301 #ifdef CONFIG_SYS_NAND_QUIET
302         int quiet = CONFIG_SYS_NAND_QUIET;
303 #else
304         int quiet = 0;
305 #endif
306         const char *quiet_str = getenv("quiet");
307
308         /* at least two arguments please */
309         if (argc < 2)
310                 goto usage;
311
312         if (quiet_str)
313                 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
314
315         cmd = argv[1];
316
317         if (strcmp(cmd, "info") == 0) {
318
319                 putc('\n');
320                 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
321                         if (nand_info[i].name)
322                                 nand_print_info(i);
323                 }
324                 return 0;
325         }
326
327         if (strcmp(cmd, "device") == 0) {
328
329                 if (argc < 3) {
330                         putc('\n');
331                         if ((nand_curr_device < 0) ||
332                             (nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE))
333                                 puts("no devices available\n");
334                         else
335                                 nand_print_info(nand_curr_device);
336                         return 0;
337                 }
338                 dev = (int)simple_strtoul(argv[2], NULL, 10);
339                 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev].name) {
340                         puts("No such device\n");
341                         return 1;
342                 }
343                 printf("Device %d: %s", dev, nand_info[dev].name);
344                 puts("... is now current device\n");
345                 nand_curr_device = dev;
346
347 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
348                 /*
349                  * Select the chip in the board/cpu specific driver
350                  */
351                 board_nand_select_device(nand_info[dev].priv, dev);
352 #endif
353
354                 return 0;
355         }
356
357         if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
358             strncmp(cmd, "dump", 4) != 0 &&
359             strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
360             strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
361             strcmp(cmd, "biterr") != 0 &&
362             strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0
363 #ifdef CONFIG_ENV_OFFSET_OOB
364             && strcmp(cmd, "env.oob") != 0
365 #endif
366             )
367                 goto usage;
368
369 #ifdef CONFIG_ENV_OFFSET_OOB
370         /* this command operates only on the first nand device */
371         if (strcmp(cmd, "env.oob") == 0) {
372                 return do_nand_env_oob(cmdtp, &nand_info[0],
373                                        argc - 1, argv + 1);
374         }
375 #endif
376
377         /* the following commands operate on the current device */
378         if (nand_curr_device < 0 || nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
379             !nand_info[nand_curr_device].name) {
380                 puts("\nno devices available\n");
381                 return 1;
382         }
383         nand = &nand_info[nand_curr_device];
384
385         if (strcmp(cmd, "bad") == 0) {
386                 printf("\nDevice %d bad blocks:\n", nand_curr_device);
387                 for (off = 0; off < nand->size; off += nand->erasesize)
388                         if (nand_block_isbad(nand, off))
389                                 printf("  %08lx\n", off);
390                 return 0;
391         }
392
393         /*
394          * Syntax is:
395          *   0    1     2       3    4
396          *   nand erase [clean] [off size]
397          */
398         if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
399                 nand_erase_options_t opts;
400                 /* "clean" at index 2 means request to write cleanmarker */
401                 int clean = argc > 2 && !strcmp("clean", argv[2]);
402                 int o = clean ? 3 : 2;
403                 int scrub = !strcmp(cmd, "scrub");
404
405                 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
406                 /* skip first two or three arguments, look for offset and size */
407                 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
408                         return 1;
409
410                 memset(&opts, 0, sizeof(opts));
411                 opts.offset = off;
412                 opts.length = size;
413                 opts.jffs2  = clean;
414                 opts.quiet  = quiet;
415
416                 if (scrub) {
417                         puts("Warning: "
418                              "scrub option will erase all factory set "
419                              "bad blocks!\n"
420                              "         "
421                              "There is no reliable way to recover them.\n"
422                              "         "
423                              "Use this command only for testing purposes "
424                              "if you\n"
425                              "         "
426                              "are sure of what you are doing!\n"
427                              "\nReally scrub this NAND flash? <y/N>\n");
428
429                         if (getc() == 'y') {
430                                 puts("y");
431                                 if (getc() == '\r')
432                                         opts.scrub = 1;
433                                 else {
434                                         puts("scrub aborted\n");
435                                         return -1;
436                                 }
437                         } else {
438                                 puts("scrub aborted\n");
439                                 return -1;
440                         }
441                 }
442                 ret = nand_erase_opts(nand, &opts);
443                 printf("%s\n", ret ? "ERROR" : "OK");
444
445                 return ret == 0 ? 0 : 1;
446         }
447
448         if (strncmp(cmd, "dump", 4) == 0) {
449                 if (argc < 3)
450                         goto usage;
451
452                 s = strchr(cmd, '.');
453                 off = (int)simple_strtoul(argv[2], NULL, 16);
454
455                 if (s != NULL && strcmp(s, ".oob") == 0)
456                         ret = nand_dump(nand, off, 1);
457                 else
458                         ret = nand_dump(nand, off, 0);
459
460                 return ret == 0 ? 1 : 0;
461
462         }
463
464         if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
465                 int read;
466
467                 if (argc < 4)
468                         goto usage;
469
470                 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
471
472                 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
473                 printf("\nNAND %s: ", read ? "read" : "write");
474                 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
475                         return 1;
476
477                 s = strchr(cmd, '.');
478                 if (!s || !strcmp(s, ".jffs2") ||
479                     !strcmp(s, ".e") || !strcmp(s, ".i")) {
480                         if (read)
481                                 ret = nand_read_skip_bad(nand, off, &size,
482                                                          (u_char *)addr);
483                         else
484                                 ret = nand_write_skip_bad(nand, off, &size,
485                                                           (u_char *)addr);
486                 } else if (!strcmp(s, ".oob")) {
487                         /* out-of-band data */
488                         mtd_oob_ops_t ops = {
489                                 .oobbuf = (u8 *)addr,
490                                 .ooblen = size,
491                                 .mode = MTD_OOB_RAW
492                         };
493
494                         if (read)
495                                 ret = nand->read_oob(nand, off, &ops);
496                         else
497                                 ret = nand->write_oob(nand, off, &ops);
498                 } else {
499                         printf("Unknown nand command suffix '%s'.\n", s);
500                         return 1;
501                 }
502
503                 printf(" %zu bytes %s: %s\n", size,
504                        read ? "read" : "written", ret ? "ERROR" : "OK");
505
506                 return ret == 0 ? 0 : 1;
507         }
508
509         if (strcmp(cmd, "markbad") == 0) {
510                 argc -= 2;
511                 argv += 2;
512
513                 if (argc <= 0)
514                         goto usage;
515
516                 while (argc > 0) {
517                         addr = simple_strtoul(*argv, NULL, 16);
518
519                         if (nand->block_markbad(nand, addr)) {
520                                 printf("block 0x%08lx NOT marked "
521                                         "as bad! ERROR %d\n",
522                                         addr, ret);
523                                 ret = 1;
524                         } else {
525                                 printf("block 0x%08lx successfully "
526                                         "marked as bad\n",
527                                         addr);
528                         }
529                         --argc;
530                         ++argv;
531                 }
532                 return ret;
533         }
534
535         if (strcmp(cmd, "biterr") == 0) {
536                 /* todo */
537                 return 1;
538         }
539
540 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
541         if (strcmp(cmd, "lock") == 0) {
542                 int tight = 0;
543                 int status = 0;
544                 if (argc == 3) {
545                         if (!strcmp("tight", argv[2]))
546                                 tight = 1;
547                         if (!strcmp("status", argv[2]))
548                                 status = 1;
549                 }
550                 if (status) {
551                         do_nand_status(nand);
552                 } else {
553                         if (!nand_lock(nand, tight)) {
554                                 puts("NAND flash successfully locked\n");
555                         } else {
556                                 puts("Error locking NAND flash\n");
557                                 return 1;
558                         }
559                 }
560                 return 0;
561         }
562
563         if (strcmp(cmd, "unlock") == 0) {
564                 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
565                         return 1;
566
567                 if (!nand_unlock(nand, off, size)) {
568                         puts("NAND flash successfully unlocked\n");
569                 } else {
570                         puts("Error unlocking NAND flash, "
571                              "write and erase will probably fail\n");
572                         return 1;
573                 }
574                 return 0;
575         }
576 #endif
577
578 usage:
579         return cmd_usage(cmdtp);
580 }
581
582 U_BOOT_CMD(
583         nand, CONFIG_SYS_MAXARGS, 1, do_nand,
584         "NAND sub-system",
585         "info - show available NAND devices\n"
586         "nand device [dev] - show or set current device\n"
587         "nand read - addr off|partition size\n"
588         "nand write - addr off|partition size\n"
589         "    read/write 'size' bytes starting at offset 'off'\n"
590         "    to/from memory address 'addr', skipping bad blocks.\n"
591         "nand erase [clean] [off size] - erase 'size' bytes from\n"
592         "    offset 'off' (entire device if not specified)\n"
593         "nand bad - show bad blocks\n"
594         "nand dump[.oob] off - dump page\n"
595         "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
596         "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
597         "nand biterr off - make a bit error at offset (UNSAFE)"
598 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
599         "\n"
600         "nand lock [tight] [status]\n"
601         "    bring nand to lock state or display locked pages\n"
602         "nand unlock [offset] [size] - unlock section"
603 #endif
604 #ifdef CONFIG_ENV_OFFSET_OOB
605         "\n"
606         "nand env.oob - environment offset in OOB of block 0 of"
607         "    first device.\n"
608         "nand env.oob set off|partition - set enviromnent offset\n"
609         "nand env.oob get - get environment offset"
610 #endif
611 );
612
613 static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
614                            ulong offset, ulong addr, char *cmd)
615 {
616         int r;
617         char *ep, *s;
618         size_t cnt;
619         image_header_t *hdr;
620 #if defined(CONFIG_FIT)
621         const void *fit_hdr = NULL;
622 #endif
623
624         s = strchr(cmd, '.');
625         if (s != NULL &&
626             (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
627                 printf("Unknown nand load suffix '%s'\n", s);
628                 show_boot_progress(-53);
629                 return 1;
630         }
631
632         printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
633
634         cnt = nand->writesize;
635         r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
636         if (r) {
637                 puts("** Read error\n");
638                 show_boot_progress (-56);
639                 return 1;
640         }
641         show_boot_progress (56);
642
643         switch (genimg_get_format ((void *)addr)) {
644         case IMAGE_FORMAT_LEGACY:
645                 hdr = (image_header_t *)addr;
646
647                 show_boot_progress (57);
648                 image_print_contents (hdr);
649
650                 cnt = image_get_image_size (hdr);
651                 break;
652 #if defined(CONFIG_FIT)
653         case IMAGE_FORMAT_FIT:
654                 fit_hdr = (const void *)addr;
655                 puts ("Fit image detected...\n");
656
657                 cnt = fit_get_size (fit_hdr);
658                 break;
659 #endif
660         default:
661                 show_boot_progress (-57);
662                 puts ("** Unknown image type\n");
663                 return 1;
664         }
665         show_boot_progress (57);
666
667         r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
668         if (r) {
669                 puts("** Read error\n");
670                 show_boot_progress (-58);
671                 return 1;
672         }
673         show_boot_progress (58);
674
675 #if defined(CONFIG_FIT)
676         /* This cannot be done earlier, we need complete FIT image in RAM first */
677         if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
678                 if (!fit_check_format (fit_hdr)) {
679                         show_boot_progress (-150);
680                         puts ("** Bad FIT image format\n");
681                         return 1;
682                 }
683                 show_boot_progress (151);
684                 fit_print_contents (fit_hdr);
685         }
686 #endif
687
688         /* Loading ok, update default load address */
689
690         load_addr = addr;
691
692         /* Check if we should attempt an auto-start */
693         if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
694                 char *local_args[2];
695                 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
696
697                 local_args[0] = cmd;
698                 local_args[1] = NULL;
699
700                 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
701
702                 do_bootm(cmdtp, 0, 1, local_args);
703                 return 1;
704         }
705         return 0;
706 }
707
708 int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
709 {
710         char *boot_device = NULL;
711         int idx;
712         ulong addr, offset = 0;
713 #if defined(CONFIG_CMD_MTDPARTS)
714         struct mtd_device *dev;
715         struct part_info *part;
716         u8 pnum;
717
718         if (argc >= 2) {
719                 char *p = (argc == 2) ? argv[1] : argv[2];
720                 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
721                     (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
722                         if (dev->id->type != MTD_DEV_TYPE_NAND) {
723                                 puts("Not a NAND device\n");
724                                 return 1;
725                         }
726                         if (argc > 3)
727                                 goto usage;
728                         if (argc == 3)
729                                 addr = simple_strtoul(argv[1], NULL, 16);
730                         else
731                                 addr = CONFIG_SYS_LOAD_ADDR;
732                         return nand_load_image(cmdtp, &nand_info[dev->id->num],
733                                                part->offset, addr, argv[0]);
734                 }
735         }
736 #endif
737
738         show_boot_progress(52);
739         switch (argc) {
740         case 1:
741                 addr = CONFIG_SYS_LOAD_ADDR;
742                 boot_device = getenv("bootdevice");
743                 break;
744         case 2:
745                 addr = simple_strtoul(argv[1], NULL, 16);
746                 boot_device = getenv("bootdevice");
747                 break;
748         case 3:
749                 addr = simple_strtoul(argv[1], NULL, 16);
750                 boot_device = argv[2];
751                 break;
752         case 4:
753                 addr = simple_strtoul(argv[1], NULL, 16);
754                 boot_device = argv[2];
755                 offset = simple_strtoul(argv[3], NULL, 16);
756                 break;
757         default:
758 #if defined(CONFIG_CMD_MTDPARTS)
759 usage:
760 #endif
761                 show_boot_progress(-53);
762                 return cmd_usage(cmdtp);
763         }
764
765         show_boot_progress(53);
766         if (!boot_device) {
767                 puts("\n** No boot device **\n");
768                 show_boot_progress(-54);
769                 return 1;
770         }
771         show_boot_progress(54);
772
773         idx = simple_strtoul(boot_device, NULL, 16);
774
775         if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
776                 printf("\n** Device %d not available\n", idx);
777                 show_boot_progress(-55);
778                 return 1;
779         }
780         show_boot_progress(55);
781
782         return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
783 }
784
785 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
786         "boot from NAND device",
787         "[partition] | [[[loadAddr] dev] offset]"
788 );