]> git.ipfire.org Git - people/ms/u-boot.git/blobdiff - board/trab/auto_update.c
* Patch by Pierre Aubert, 24 Nov 2003:
[people/ms/u-boot.git] / board / trab / auto_update.c
index 345d2d47a3cc508f022772bdba046addae7150e8..36fdf18e89ef90553cb54a3e3c6a97cb0f8e8a78 100644 (file)
@@ -204,9 +204,35 @@ extern int flash_write (uchar *, ulong, ulong);
 extern int i2c_write_multiple (uchar, uint, int, void *, int);
 extern int i2c_read_multiple (uchar, uint, int, void *, int);
 extern block_dev_desc_t *get_dev (char*, int);
+extern int u_boot_hush_start(void);
 
 int
-au_check_valid(int idx, long nbytes)
+au_check_cksum_valid(int idx, long nbytes)
+{
+       image_header_t *hdr;
+       unsigned long checksum;
+
+       hdr = (image_header_t *)LOAD_ADDR;
+
+       if (nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size)))
+       {
+               printf ("Image %s bad total SIZE\n", aufile[idx]);
+               return -1;
+       }
+       /* check the data CRC */
+       checksum = ntohl(hdr->ih_dcrc);
+
+       if (crc32 (0, (char *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size))
+               != checksum)
+       {
+               printf ("Image %s bad data checksum\n", aufile[idx]);
+               return -1;
+       }
+       return 0;
+}
+
+int
+au_check_header_valid(int idx, long nbytes)
 {
        image_header_t *hdr;
        unsigned long checksum;
@@ -221,11 +247,14 @@ au_check_valid(int idx, long nbytes)
        printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
        printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
 #endif
-       if (ntohl(hdr->ih_magic) != IH_MAGIC ||
-           hdr->ih_arch != IH_CPU_ARM ||
-           nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size)))
+       if (nbytes < sizeof(*hdr))
+       {
+               printf ("Image %s bad header SIZE\n", aufile[idx]);
+               return -1;
+       }
+       if (ntohl(hdr->ih_magic) != IH_MAGIC || hdr->ih_arch != IH_CPU_ARM)
        {
-               printf ("Image %s bad MAGIC or ARCH or SIZE\n", aufile[idx]);
+               printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
                return -1;
        }
        /* check the hdr CRC */
@@ -237,15 +266,6 @@ au_check_valid(int idx, long nbytes)
                return -1;
        }
        hdr->ih_hcrc = htonl(checksum);
-       /* check the data CRC */
-       checksum = ntohl(hdr->ih_dcrc);
-
-       if (crc32 (0, (char *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size))
-               != checksum)
-       {
-               printf ("Image %s bad data checksum\n", aufile[idx]);
-               return -1;
-       }
        /* check the type - could do this all in one gigantic if() */
        if ((idx == IDX_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
                printf ("Image %s wrong type\n", aufile[idx]);
@@ -259,7 +279,8 @@ au_check_valid(int idx, long nbytes)
                printf ("Image %s wrong type\n", aufile[idx]);
                return -1;
        }
-       if ((idx == IDX_APP) && (hdr->ih_type != IH_TYPE_RAMDISK)) {
+       if ((idx == IDX_APP) && (hdr->ih_type != IH_TYPE_RAMDISK) 
+            && (hdr->ih_type != IH_TYPE_FILESYSTEM)) {
                printf ("Image %s wrong type\n", aufile[idx]);
                return -1;
        }
@@ -351,8 +372,12 @@ au_do_update(int idx, long sz)
        debug ("flash_sect_erase(%lx, %lx);\n", start, end);
        flash_sect_erase(start, end);
        wait_ms(100);
-       /* strip the header - except for the kernel and app */
-       if (idx == IDX_FIRMWARE || idx == IDX_DISK) {
+       /* strip the header - except for the kernel and ramdisk */
+       if (hdr->ih_type == IH_TYPE_KERNEL || hdr->ih_type == IH_TYPE_RAMDISK) {
+               addr = (char *)hdr;
+               off = sizeof(*hdr);
+               nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
+       } else {
                addr = (char *)((char *)hdr + sizeof(*hdr));
 #ifdef AU_UPDATE_TEST
                /* copy it to where Linux goes */
@@ -361,10 +386,6 @@ au_do_update(int idx, long sz)
 #endif
                off = 0;
                nbytes = ntohl(hdr->ih_size);
-       } else {
-               addr = (char *)hdr;
-               off = sizeof(*hdr);
-               nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
        }
 
        /* copy the data from RAM to FLASH */
@@ -447,7 +468,7 @@ do_auto_update(void)
         * check whether a storage device is attached (assume that it's
         * a USB memory stick, since nothing else should be attached).
         */
-       au_usb_stor_curr_dev = usb_stor_scan(1);
+       au_usb_stor_curr_dev = usb_stor_scan(0);
        if (au_usb_stor_curr_dev == -1) {
                debug ("No device found. Not initialized?\n");
                return -1;
@@ -538,12 +559,26 @@ do_auto_update(void)
                aufl_layout[3].start = start;
                aufl_layout[3].end = end;
        }
+       /* make certain that HUSH is runnable */
+       u_boot_hush_start();
        /* make sure that we see CTRL-C and save the old state */
        old_ctrlc = disable_ctrlc(0);
 
        bitmap_first = 0;
        /* just loop thru all the possible files */
        for (i = 0; i < AU_MAXFILES; i++) {
+               /* just read the header */
+               sz = file_fat_read(aufile[i], LOAD_ADDR, sizeof(image_header_t));
+               debug ("read %s sz %ld hdr %d\n",
+                       aufile[i], sz, sizeof(image_header_t));
+               if (sz <= 0 || sz < sizeof(image_header_t)) {
+                       debug ("%s not found\n", aufile[i]);
+                       continue;
+               }
+               if (au_check_header_valid(i, sz) < 0) {
+                       debug ("%s header not valid\n", aufile[i]);
+                       continue;
+               }
                sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
                debug ("read %s sz %ld hdr %d\n",
                        aufile[i], sz, sizeof(image_header_t));
@@ -551,8 +586,8 @@ do_auto_update(void)
                        debug ("%s not found\n", aufile[i]);
                        continue;
                }
-               if (au_check_valid(i, sz) < 0) {
-                       debug ("%s not valid\n", aufile[i]);
+               if (au_check_cksum_valid(i, sz) < 0) {
+                       debug ("%s checksum not valid\n", aufile[i]);
                        continue;
                }
 #ifdef CONFIG_VFD