Merge with /home/hs/jupiter/u-boot
[oweals/u-boot.git] / board / esd / common / auto_update.c
index d4009b7c4ee7e218e93232005cc6a1a173c7f459..001fd68da42e0bbea25e6b5930067b5283eb1991 100644 (file)
  */
 
 #include <common.h>
+
+#if (CONFIG_COMMANDS & CFG_CMD_NAND) && !defined(CFG_NAND_LEGACY)
+#warning CFG_NAND_LEGACY not defined in a file using the legacy NAND support!
+#endif
+
 #include <command.h>
 #include <image.h>
 #include <asm/byteorder.h>
-#include <linux/mtd/nand.h>
+#include <linux/mtd/nand_legacy.h>
 #include <fat.h>
+#include <part.h>
 
 #include "auto_update.h"
 
@@ -65,21 +71,19 @@ extern int transfer_pic(unsigned char, unsigned char *, int, int);
 #endif
 extern int flash_sect_erase(ulong, ulong);
 extern int flash_sect_protect (int, ulong, ulong);
-extern int flash_write (uchar *, ulong, ulong);
-/* change char* to void* to shutup the compiler */
-extern block_dev_desc_t *get_dev (char*, int);
+extern int flash_write (char *, ulong, ulong);
 
-#if (CONFIG_COMMANDS & CFG_CMD_NAND)
+#if (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
 /* references to names in cmd_nand.c */
 #define NANDRW_READ    0x01
 #define NANDRW_WRITE   0x00
 #define NANDRW_JFFS2   0x02
 #define NANDRW_JFFS2_SKIP      0x04
 extern struct nand_chip nand_dev_desc[];
-extern int nand_rw(struct nand_chip* nand, int cmd, size_t start, size_t len,
+extern int nand_legacy_rw(struct nand_chip* nand, int cmd, size_t start, size_t len,
                   size_t * retlen, u_char * buf);
-extern int nand_erase(struct nand_chip* nand, size_t ofs, size_t len, int clean);
-#endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) */
+extern int nand_legacy_erase(struct nand_chip* nand, size_t ofs, size_t len, int clean);
+#endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY) */
 
 extern block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
 
@@ -103,7 +107,7 @@ int au_check_cksum_valid(int i, long nbytes)
        /* check the data CRC */
        checksum = ntohl(hdr->ih_dcrc);
 
-       if (crc32 (0, (char *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size))
+       if (crc32 (0, (uchar *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size))
                != checksum) {
                printf ("Image %s bad data checksum\n", au_image[i].name);
                return -1;
@@ -140,7 +144,7 @@ int au_check_header_valid(int i, long nbytes)
        checksum = ntohl(hdr->ih_hcrc);
        hdr->ih_hcrc = 0;
 
-       if (crc32 (0, (char *)hdr, sizeof(*hdr)) != checksum) {
+       if (crc32 (0, (uchar *)hdr, sizeof(*hdr)) != checksum) {
                printf ("Image %s bad header checksum\n", au_image[i].name);
                return -1;
        }
@@ -183,7 +187,7 @@ int au_do_update(int i, long sz)
        int off, rc;
        uint nbytes;
        int k;
-#if (CONFIG_COMMANDS & CFG_CMD_NAND)
+#if (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
        int total;
 #endif
 
@@ -224,6 +228,25 @@ int au_do_update(int i, long sz)
                start = au_image[i].start;
                end = au_image[i].start + au_image[i].size - 1;
 
+               /*
+                * do not update firmware when image is already in flash.
+                */
+               if (au_image[i].type == AU_FIRMWARE) {
+                       char *orig = (char*)start;
+                       char *new  = (char *)((char *)hdr + sizeof(*hdr));
+                       nbytes = ntohl(hdr->ih_size);
+
+                       while(--nbytes) {
+                               if (*orig++ != *new++) {
+                                       break;
+                               }
+                       }
+                       if (!nbytes) {
+                               printf("Skipping firmware update - images are identical\n");
+                               break;
+                       }
+               }
+
                /* unprotect the address range */
                /* this assumes that ONLY the firmware is protected! */
                if (au_image[i].type == AU_FIRMWARE) {
@@ -238,11 +261,11 @@ int au_do_update(int i, long sz)
                        debug ("flash_sect_erase(%lx, %lx);\n", start, end);
                        flash_sect_erase(start, end);
                } else {
-#if (CONFIG_COMMANDS & CFG_CMD_NAND)
+#if (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
                        printf("Updating NAND FLASH with image %s\n", au_image[i].name);
-                       debug ("nand_erase(%lx, %lx);\n", start, end);
-                       rc = nand_erase (nand_dev_desc, start, end - start + 1, 0);
-                       debug ("nand_erase returned %x\n", rc);
+                       debug ("nand_legacy_erase(%lx, %lx);\n", start, end);
+                       rc = nand_legacy_erase (nand_dev_desc, start, end - start + 1, 0);
+                       debug ("nand_legacy_erase returned %x\n", rc);
 #endif
                }
 
@@ -264,13 +287,13 @@ int au_do_update(int i, long sz)
                 */
                if (au_image[i].type != AU_NAND) {
                        debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
-                       rc = flash_write(addr, start, nbytes);
+                       rc = flash_write((char *)addr, start, nbytes);
                } else {
-#if (CONFIG_COMMANDS & CFG_CMD_NAND)
-                       debug ("nand_rw(%p, %lx %x)\n", addr, start, nbytes);
-                       rc = nand_rw(nand_dev_desc, NANDRW_WRITE | NANDRW_JFFS2,
-                                    start, nbytes, &total, addr);
-                       debug ("nand_rw: ret=%x total=%d nbytes=%d\n", rc, total, nbytes);
+#if (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
+                       debug ("nand_legacy_rw(%p, %lx %x)\n", addr, start, nbytes);
+                       rc = nand_legacy_rw(nand_dev_desc, NANDRW_WRITE | NANDRW_JFFS2,
+                                    start, nbytes, (size_t *)&total, (uchar *)addr);
+                       debug ("nand_legacy_rw: ret=%x total=%d nbytes=%d\n", rc, total, nbytes);
 #endif
                }
                if (rc != 0) {
@@ -282,12 +305,12 @@ int au_do_update(int i, long sz)
                 * check the dcrc of the copy
                 */
                if (au_image[i].type != AU_NAND) {
-                       rc = crc32 (0, (char *)(start + off), ntohl(hdr->ih_size));
+                       rc = crc32 (0, (uchar *)(start + off), ntohl(hdr->ih_size));
                } else {
-#if (CONFIG_COMMANDS & CFG_CMD_NAND)
-                       rc = nand_rw(nand_dev_desc, NANDRW_READ | NANDRW_JFFS2 | NANDRW_JFFS2_SKIP,
-                                    start, nbytes, &total, addr);
-                       rc = crc32 (0, (char *)(addr + off), ntohl(hdr->ih_size));
+#if (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
+                       rc = nand_legacy_rw(nand_dev_desc, NANDRW_READ | NANDRW_JFFS2 | NANDRW_JFFS2_SKIP,
+                                    start, nbytes, (size_t *)&total, (uchar *)addr);
+                       rc = crc32 (0, (uchar *)(addr + off), ntohl(hdr->ih_size));
 #endif
                }
                if (rc != ntohl(hdr->ih_dcrc)) {