]> git.ipfire.org Git - people/ms/u-boot.git/blobdiff - common/cmd_nand.c
Merge remote branch 'u-boot-avr32/master'
[people/ms/u-boot.git] / common / cmd_nand.c
index b011b5e3ded1602bcc11ebf593c7c8e9439aeef4..37198d21e8c6e1638f112a63205884317fba5af4 100644 (file)
  */
 #include <common.h>
 
-#if (CONFIG_COMMANDS & CFG_CMD_NAND)
+#if defined(CONFIG_CMD_NAND)
 
 #include <command.h>
 #include <watchdog.h>
 #include <malloc.h>
 #include <asm/byteorder.h>
-
-#ifdef CONFIG_SHOW_BOOT_PROGRESS
-# include <status_led.h>
-# define SHOW_BOOT_PROGRESS(arg)       show_boot_progress(arg)
-#else
-# define SHOW_BOOT_PROGRESS(arg)
-#endif
-
 #include <jffs2/jffs2.h>
 #include <nand.h>
 
-#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
+#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
 
 /* parition handling routines */
 int mtdparts_init(void);
@@ -45,8 +37,6 @@ int find_dev_and_part(const char *id, struct mtd_device **dev,
                u8 *part_num, struct part_info **part);
 #endif
 
-extern nand_info_t nand_info[];       /* info for NAND chips */
-
 static int nand_dump_oob(nand_info_t *nand, ulong off)
 {
        return 0;
@@ -101,10 +91,10 @@ static inline int str2long(char *p, ulong *num)
 }
 
 static int
-arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, ulong *size)
+arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
 {
        int idx = nand_curr_device;
-#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
+#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
        struct mtd_device *dev;
        struct part_info *part;
        u8 pnum;
@@ -118,7 +108,7 @@ arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, ulong *size)
                        }
                        *off = part->offset;
                        if (argc >= 2) {
-                               if (!(str2long(argv[1], size))) {
+                               if (!(str2long(argv[1], (ulong *)size))) {
                                        printf("'%s' is not a number\n", argv[1]);
                                        return -1;
                                }
@@ -144,7 +134,7 @@ arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, ulong *size)
        }
 
        if (argc >= 2) {
-               if (!(str2long(argv[1], size))) {
+               if (!(str2long(argv[1], (ulong *)size))) {
                        printf("'%s' is not a number\n", argv[1]);
                        return -1;
                }
@@ -152,7 +142,7 @@ arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, ulong *size)
                *size = nand->size - *off;
        }
 
-#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
+#if  defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
 out:
 #endif
        printf("device %d ", idx);
@@ -166,10 +156,15 @@ out:
 int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 {
        int i, dev, ret;
-       ulong addr, off, size;
+       ulong addr, off;
+       size_t size;
        char *cmd, *s;
        nand_info_t *nand;
+#ifdef CFG_NAND_QUIET
+       int quiet = CFG_NAND_QUIET;
+#else
        int quiet = 0;
+#endif
        const char *quiet_str = getenv("quiet");
 
        /* at least two arguments please */
@@ -351,6 +346,14 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
                                opts.quiet      = quiet;
                                ret = nand_write_opts(nand, &opts);
                        }
+               } else if (s != NULL && !strcmp(s, ".oob")) {
+                       /* read out-of-band data */
+                       if (read)
+                               ret = nand->read_oob(nand, off, size, &size,
+                                                    (u_char *) addr);
+                       else
+                               ret = nand->write_oob(nand, off, size, &size,
+                                                     (u_char *) addr);
                } else {
                        if (read)
                                ret = nand_read(nand, off, &size, (u_char *)addr);
@@ -460,7 +463,7 @@ U_BOOT_CMD(nand, 5, 1, do_nand,
        "info                  - show available NAND devices\n"
        "nand device [dev]     - show or set current device\n"
        "nand read[.jffs2]     - addr off|partition size\n"
-       "nand write[.jffs2]    - addr off|partiton size - read/write `size' bytes starting\n"
+       "nand write[.jffs2]    - addr off|partition size - read/write `size' bytes starting\n"
        "    at offset `off' to/from memory address `addr'\n"
        "nand erase [clean] [off size] - erase `size' bytes from\n"
        "    offset `off' (entire device if not specified)\n"
@@ -476,38 +479,94 @@ static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
                           ulong offset, ulong addr, char *cmd)
 {
        int r;
-       char *ep;
-       ulong cnt;
+       char *ep, *s;
+       size_t cnt;
        image_header_t *hdr;
+       int jffs2 = 0;
+#if defined(CONFIG_FIT)
+       const void *fit_hdr;
+#endif
+
+       s = strchr(cmd, '.');
+       if (s != NULL &&
+           (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i")))
+               jffs2 = 1;
 
        printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
 
        cnt = nand->oobblock;
-       r = nand_read(nand, offset, &cnt, (u_char *) addr);
+       if (jffs2) {
+               nand_read_options_t opts;
+               memset(&opts, 0, sizeof(opts));
+               opts.buffer     = (u_char*) addr;
+               opts.length     = cnt;
+               opts.offset     = offset;
+               opts.quiet      = 1;
+               r = nand_read_opts(nand, &opts);
+       } else {
+               r = nand_read(nand, offset, &cnt, (u_char *) addr);
+       }
+
        if (r) {
                puts("** Read error\n");
-               SHOW_BOOT_PROGRESS(-1);
+               show_boot_progress (-56);
                return 1;
        }
+       show_boot_progress (56);
+
+       switch (genimg_get_format ((void *)addr)) {
+       case IMAGE_FORMAT_LEGACY:
+               hdr = (image_header_t *)addr;
 
-       hdr = (image_header_t *) addr;
+               show_boot_progress (57);
+               image_print_contents (hdr);
 
-       if (ntohl(hdr->ih_magic) != IH_MAGIC) {
-               printf("\n** Bad Magic Number 0x%x **\n", hdr->ih_magic);
-               SHOW_BOOT_PROGRESS(-1);
+               cnt = image_get_image_size (hdr);
+               break;
+#if defined(CONFIG_FIT)
+       case IMAGE_FORMAT_FIT:
+               fit_hdr = (const void *)addr;
+               if (!fit_check_format (fit_hdr)) {
+                       show_boot_progress (-150);
+                       puts ("** Bad FIT image format\n");
+                       return 1;
+               }
+               show_boot_progress (151);
+               puts ("Fit image detected...\n");
+
+               cnt = fit_get_size (fit_hdr);
+               break;
+#endif
+       default:
+               show_boot_progress (-57);
+               puts ("** Unknown image type\n");
                return 1;
        }
 
-       print_image_hdr(hdr);
-
-       cnt = (ntohl(hdr->ih_size) + sizeof (image_header_t));
+       if (jffs2) {
+               nand_read_options_t opts;
+               memset(&opts, 0, sizeof(opts));
+               opts.buffer     = (u_char*) addr;
+               opts.length     = cnt;
+               opts.offset     = offset;
+               opts.quiet      = 1;
+               r = nand_read_opts(nand, &opts);
+       } else {
+               r = nand_read(nand, offset, &cnt, (u_char *) addr);
+       }
 
-       r = nand_read(nand, offset, &cnt, (u_char *) addr);
        if (r) {
                puts("** Read error\n");
-               SHOW_BOOT_PROGRESS(-1);
+               show_boot_progress (-58);
                return 1;
        }
+       show_boot_progress (58);
+
+#if defined(CONFIG_FIT)
+       /* This cannot be done earlier, we need complete FIT image in RAM first */
+       if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT)
+               fit_print_contents ((const void *)addr);
+#endif
 
        /* Loading ok, update default load address */
 
@@ -534,7 +593,7 @@ int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
        char *boot_device = NULL;
        int idx;
        ulong addr, offset = 0;
-#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
+#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
        struct mtd_device *dev;
        struct part_info *part;
        u8 pnum;
@@ -550,7 +609,7 @@ int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
                        if (argc > 3)
                                goto usage;
                        if (argc == 3)
-                               addr = simple_strtoul(argv[2], NULL, 16);
+                               addr = simple_strtoul(argv[1], NULL, 16);
                        else
                                addr = CFG_LOAD_ADDR;
                        return nand_load_image(cmdtp, &nand_info[dev->id->num],
@@ -559,6 +618,7 @@ int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
        }
 #endif
 
+       show_boot_progress(52);
        switch (argc) {
        case 1:
                addr = CFG_LOAD_ADDR;
@@ -578,36 +638,39 @@ int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
                offset = simple_strtoul(argv[3], NULL, 16);
                break;
        default:
-#if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
+#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
 usage:
 #endif
                printf("Usage:\n%s\n", cmdtp->usage);
-               SHOW_BOOT_PROGRESS(-1);
+               show_boot_progress(-53);
                return 1;
        }
 
+       show_boot_progress(53);
        if (!boot_device) {
                puts("\n** No boot device **\n");
-               SHOW_BOOT_PROGRESS(-1);
+               show_boot_progress(-54);
                return 1;
        }
+       show_boot_progress(54);
 
        idx = simple_strtoul(boot_device, NULL, 16);
 
        if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
                printf("\n** Device %d not available\n", idx);
-               SHOW_BOOT_PROGRESS(-1);
+               show_boot_progress(-55);
                return 1;
        }
+       show_boot_progress(55);
 
        return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
 }
 
 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
        "nboot   - boot from NAND device\n",
-       "[partition] | [[[loadAddr] dev] offset]\n");
+       "[.jffs2] [partition] | [[[loadAddr] dev] offset]\n");
 
-#endif                         /* (CONFIG_COMMANDS & CFG_CMD_NAND) */
+#endif
 
 #else /* CFG_NAND_LEGACY */
 /*
@@ -620,14 +683,14 @@ U_BOOT_CMD(nboot, 4, 1, do_nandboot,
 #include <asm/io.h>
 #include <watchdog.h>
 
-#ifdef CONFIG_SHOW_BOOT_PROGRESS
+#ifdef CONFIG_show_boot_progress
 # include <status_led.h>
-# define SHOW_BOOT_PROGRESS(arg)       show_boot_progress(arg)
+# define show_boot_progress(arg)       show_boot_progress(arg)
 #else
-# define SHOW_BOOT_PROGRESS(arg)
+# define show_boot_progress(arg)
 #endif
 
-#if (CONFIG_COMMANDS & CFG_CMD_NAND)
+#if defined(CONFIG_CMD_NAND)
 #include <linux/mtd/nand_legacy.h>
 #if 0
 #include <linux/mtd/nand_ids.h>
@@ -775,23 +838,24 @@ int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 
                if (strncmp (argv[1], "read", 4) == 0 ||
                    strncmp (argv[1], "write", 5) == 0) {
-                       ulong addr = simple_strtoul (argv[2], NULL, 16);
-                       ulong off = simple_strtoul (argv[3], NULL, 16);
-                       ulong size = simple_strtoul (argv[4], NULL, 16);
-                       int cmd = (strncmp (argv[1], "read", 4) == 0) ?
-                               NANDRW_READ : NANDRW_WRITE;
-                       int ret, total;
+                       ulong   addr = simple_strtoul (argv[2], NULL, 16);
+                       off_t   off  = simple_strtoul (argv[3], NULL, 16);
+                       size_t  size = simple_strtoul (argv[4], NULL, 16);
+                       int     cmd = (strncmp (argv[1], "read", 4) == 0) ?
+                                       NANDRW_READ : NANDRW_WRITE;
+                       size_t total;
+                       int ret;
                        char *cmdtail = strchr (argv[1], '.');
 
                        if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
                                /* read out-of-band data */
                                if (cmd & NANDRW_READ) {
                                        ret = nand_read_oob (nand_dev_desc + curr_device,
-                                                            off, size, (size_t *) & total,
+                                                            off, size, &total,
                                                             (u_char *) addr);
                                } else {
                                        ret = nand_write_oob (nand_dev_desc + curr_device,
-                                                             off, size, (size_t *) & total,
+                                                             off, size, &total,
                                                              (u_char *) addr);
                                }
                                return ret;
@@ -827,7 +891,7 @@ int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 
                        ret = nand_legacy_rw (nand_dev_desc + curr_device,
                                              cmd, off, size,
-                                             (size_t *) & total,
+                                             &total,
                                              (u_char *) addr);
 
                        printf (" %d bytes %s: %s\n", total,
@@ -887,6 +951,11 @@ int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
        ulong offset = 0;
        image_header_t *hdr;
        int rcode = 0;
+#if defined(CONFIG_FIT)
+       const void *fit_hdr;
+#endif
+
+       show_boot_progress (52);
        switch (argc) {
        case 1:
                addr = CFG_LOAD_ADDR;
@@ -907,24 +976,27 @@ int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
                break;
        default:
                printf ("Usage:\n%s\n", cmdtp->usage);
-               SHOW_BOOT_PROGRESS (-1);
+               show_boot_progress (-53);
                return 1;
        }
 
+       show_boot_progress (53);
        if (!boot_device) {
                puts ("\n** No boot device **\n");
-               SHOW_BOOT_PROGRESS (-1);
+               show_boot_progress (-54);
                return 1;
        }
+       show_boot_progress (54);
 
        dev = simple_strtoul(boot_device, &ep, 16);
 
        if ((dev >= CFG_MAX_NAND_DEVICE) ||
            (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
                printf ("\n** Device %d not available\n", dev);
-               SHOW_BOOT_PROGRESS (-1);
+               show_boot_progress (-55);
                return 1;
        }
+       show_boot_progress (55);
 
        printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
                dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
@@ -933,31 +1005,54 @@ int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
        if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
                        SECTORSIZE, NULL, (u_char *)addr)) {
                printf ("** Read error on %d\n", dev);
-               SHOW_BOOT_PROGRESS (-1);
+               show_boot_progress (-56);
                return 1;
        }
+       show_boot_progress (56);
 
-       hdr = (image_header_t *)addr;
-
-       if (ntohl(hdr->ih_magic) == IH_MAGIC) {
+       switch (genimg_get_format ((void *)addr)) {
+       case IMAGE_FORMAT_LEGACY:
+               hdr = (image_header_t *)addr;
+               image_print_contents (hdr);
 
-               print_image_hdr (hdr);
-
-               cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
+               cnt = image_get_image_size (hdr);
                cnt -= SECTORSIZE;
-       } else {
-               printf ("\n** Bad Magic Number 0x%x **\n", ntohl(hdr->ih_magic));
-               SHOW_BOOT_PROGRESS (-1);
+               break;
+#if defined(CONFIG_FIT)
+       case IMAGE_FORMAT_FIT:
+               fit_hdr = (const void *)addr;
+               if (!fit_check_format (fit_hdr)) {
+                       show_boot_progress (-150);
+                       puts ("** Bad FIT image format\n");
+                       return 1;
+               }
+               show_boot_progress (151);
+               puts ("Fit image detected...\n");
+
+               cnt = fit_get_size (fit_hdr);
+               break;
+#endif
+       default:
+               show_boot_progress (-57);
+               puts ("** Unknown image type\n");
                return 1;
        }
+       show_boot_progress (57);
 
        if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
                        offset + SECTORSIZE, cnt, NULL,
                        (u_char *)(addr+SECTORSIZE))) {
                printf ("** Read error on %d\n", dev);
-               SHOW_BOOT_PROGRESS (-1);
+               show_boot_progress (-58);
                return 1;
        }
+       show_boot_progress (58);
+
+#if defined(CONFIG_FIT)
+       /* This cannot be done earlier, we need complete FIT image in RAM first */
+       if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT)
+               fit_print_contents ((const void *)addr);
+#endif
 
        /* Loading ok, update default load address */
 
@@ -985,6 +1080,6 @@ U_BOOT_CMD(
        "loadAddr dev\n"
 );
 
-#endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) */
+#endif
 
 #endif /* CFG_NAND_LEGACY */