]> 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 da3b71c77ff73b7dba0b8988382d2473a5a7b5c7..36fdf18e89ef90553cb54a3e3c6a97cb0f8e8a78 100644 (file)
@@ -105,14 +105,14 @@ struct flash_layout
 #define AU_FL_APP_ND           0x005BFFFF
 #define AU_FL_DISK_ST          0x005C0000
 #define AU_FL_DISK_ND          0x00FFFFFF
-#else                                          /*  8 MB Flash, 16 MB RAM */
+#else                                          /*  8 MB Flash, 32 MB RAM */
 #define AU_FL_FIRMWARE_ST      0x00000000
-#define AU_FL_FIRMWARE_ND      0x0003FFFF
-#define AU_FL_KERNEL_ST                0x00040000
-#define AU_FL_KERNEL_ND                0x0011FFFF
-#define AU_FL_APP_ST           0x00120000
-#define AU_FL_APP_ND           0x003FFFFF
-#define AU_FL_DISK_ST          0x00400000
+#define AU_FL_FIRMWARE_ND      0x0005FFFF
+#define AU_FL_KERNEL_ST                0x00060000
+#define AU_FL_KERNEL_ND                0x0013FFFF
+#define AU_FL_APP_ST           0x00140000
+#define AU_FL_APP_ND           0x0067FFFF
+#define AU_FL_DISK_ST          0x00680000
 #define AU_FL_DISK_ND          0x007DFFFF
 #define AU_FL_VFD_ST           0x007E0000
 #define AU_FL_VFD_ND           0x007FFFFF
@@ -183,11 +183,9 @@ struct flash_layout aufl_layout[AU_MAXFILES - 3] = { \
 #define FIDX_TO_LIDX(idx) ((idx) - 2)
 
 /* where to load files into memory */
-#define LOAD_ADDR ((unsigned char *)0x0C100100)
-/* where to build strings in memory - 256 bytes should be enough */
-#define STRING_ADDR ((char *)0x0C100000)
-/* the disk is the largest image */
-#define MAX_LOADSZ ausize[IDX_DISK]
+#define LOAD_ADDR ((unsigned char *)0x0C100000)
+/* the app is the largest image */
+#define MAX_LOADSZ ausize[IDX_APP]
 
 /* externals */
 extern int fat_register_device(block_dev_desc_t *, int);
@@ -199,12 +197,42 @@ extern int i2c_write (uchar, uint, int , uchar* , int);
 extern int trab_vfd (ulong);
 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 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;
@@ -219,69 +247,76 @@ 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 < ntohl(hdr->ih_size)) {
-           printf ("Image %s bad MAGIC or ARCH or SIZE\n", aufile[idx]);
-           return -1;
+       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\n", aufile[idx]);
+               return -1;
        }
        /* check the hdr CRC */
        checksum = ntohl(hdr->ih_hcrc);
        hdr->ih_hcrc = 0;
 
        if (crc32 (0, (char *)hdr, sizeof(*hdr)) != checksum) {
-           printf ("Image %s bad header checksum\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;
+               printf ("Image %s bad header checksum\n", aufile[idx]);
+               return -1;
        }
+       hdr->ih_hcrc = htonl(checksum);
        /* 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]);
-           return -1;
+               printf ("Image %s wrong type\n", aufile[idx]);
+               return -1;
        }
        if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
-           printf ("Image %s wrong type\n", aufile[idx]);
-           return -1;
+               printf ("Image %s wrong type\n", aufile[idx]);
+               return -1;
        }
-       if ((idx == IDX_DISK || idx == IDX_APP)
-               && (hdr->ih_type != IH_TYPE_RAMDISK))
-       {
-           printf ("Image %s wrong type\n", aufile[idx]);
-           return -1;
+       if ((idx == IDX_DISK) && (hdr->ih_type != IH_TYPE_FILESYSTEM)) {
+               printf ("Image %s wrong type\n", aufile[idx]);
+               return -1;
+       }
+       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;
        }
        if ((idx == IDX_PREPARE || idx == IDX_PREINST || idx == IDX_POSTINST)
                && (hdr->ih_type != IH_TYPE_SCRIPT))
        {
-           printf ("Image %s wrong type\n", aufile[idx]);
-           return -1;
+               printf ("Image %s wrong type\n", aufile[idx]);
+               return -1;
        }
        /* special case for prepare.img */
        if (idx == IDX_PREPARE)
                return 0;
-       /* check the size does not exceed space in flash */
-       if ((ausize[idx] != 0) && (ausize[idx] < ntohl(hdr->ih_size))) {
-           printf ("Image %s is bigger than FLASH\n", aufile[idx]);
-           return -1;
+       /* recycle checksum */
+       checksum = ntohl(hdr->ih_size);
+       /* for kernel and app the image header must also fit into flash */
+       if (idx != IDX_DISK)
+               checksum += sizeof(*hdr);
+       /* check the size does not exceed space in flash. HUSH scripts */
+       /* all have ausize[] set to 0 */
+       if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
+               printf ("Image %s is bigger than FLASH\n", aufile[idx]);
+               return -1;
        }
        /* check the time stamp from the EEPROM */
        /* read it in */
        i2c_read_multiple(0x54, auee_off[idx].time, 1, buf, sizeof(buf));
 #ifdef CHECK_VALID_DEBUG
-printf("buf[0] %#x buf[1] %#x buf[2] %#x buf[3] %#x as int %#x time %#x\n",
-buf[0], buf[1], buf[2], buf[3], *((unsigned int *)buf), ntohl(hdr->ih_time));
+       printf ("buf[0] %#x buf[1] %#x buf[2] %#x buf[3] %#x "
+               "as int %#x time %#x\n",
+               buf[0], buf[1], buf[2], buf[3],
+               *((unsigned int *)buf), ntohl(hdr->ih_time));
 #endif
        /* check it */
        if (*((unsigned int *)buf) >= ntohl(hdr->ih_time)) {
-           printf ("Image %s is too old\n", aufile[idx]);
-           return -1;
+               printf ("Image %s is too old\n", aufile[idx]);
+               return -1;
        }
 
        return 0;
@@ -297,8 +332,7 @@ au_do_update(int idx, long sz)
        image_header_t *hdr;
        char *addr;
        long start, end;
-       char *strbuf = STRING_ADDR;
-       int off;
+       int off, rc;
        uint nbytes;
 
        hdr = (image_header_t *)LOAD_ADDR;
@@ -308,9 +342,12 @@ au_do_update(int idx, long sz)
 
        /* execute a script */
        if (hdr->ih_type == IH_TYPE_SCRIPT) {
-               addr = (char *)((char *)hdr + sizeof(*hdr) + 8);
-               parse_string_outer(addr,
-                       FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
+               addr = (char *)((char *)hdr + sizeof(*hdr));
+               /* stick a NULL at the end of the script, otherwise */
+               /* parse_string_outer() runs off the end. */
+               addr[ntohl(hdr->ih_size)] = 0;
+               addr += 8;
+               parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
                return 0;
        }
 
@@ -326,20 +363,21 @@ au_do_update(int idx, long sz)
                start = aufl_layout[1].start;
                end = aufl_layout[1].end;
 #endif
-               debug ("protect off %lx %lx\n", start, end);
-               sprintf(strbuf, "protect off %lx %lx\n", start, end);
-               parse_string_outer(strbuf,
-                       FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
+               flash_sect_protect(0, start, end);
        }
 
-       /* erase the address range */
-       debug ("erase %lx %lx\n", start, end);
-       sprintf(strbuf, "erase %lx %lx\n", start, end);
-       parse_string_outer(strbuf,
-               FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
-
-       /* strip the header - except for the kernel */
-       if (idx == IDX_FIRMWARE || idx == IDX_DISK || idx == IDX_APP) {
+       /*
+        * erase the address range.
+        */
+       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 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 */
@@ -348,33 +386,26 @@ 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 */
-       debug ("cp.b %p %lx %x\n", addr, start, nbytes);
-       sprintf(strbuf, "cp.b %p %lx %x\n", addr, start, nbytes);
-       parse_string_outer(strbuf,
-               FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
+       debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
+       rc = flash_write(addr, start, nbytes);
+       if (rc != 0) {
+               printf("Flashing failed due to error %d\n", rc);
+               return -1;
+       }
 
        /* check the dcrc of the copy */
-       if (crc32 (0, (char *)(start + off), ntohl(hdr->ih_size))
-               != ntohl(hdr->ih_dcrc)) {
-           printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
-           return -1;
+       if (crc32 (0, (char *)(start + off), ntohl(hdr->ih_size)) != ntohl(hdr->ih_dcrc)) {
+               printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
+               return -1;
        }
 
        /* protect the address range */
        /* this assumes that ONLY the firmware is protected! */
-       if (idx == IDX_FIRMWARE) {
-               printf("protect on %lx %lx\n", start, end);
-               sprintf(strbuf, "protect on %lx %lx\n", start, end);
-               parse_string_outer(strbuf,
-                       FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
-       }
+       if (idx == IDX_FIRMWARE)
+               flash_sect_protect(1, start, end);
        return 0;
 }
 
@@ -413,8 +444,9 @@ do_auto_update(void)
 {
        block_dev_desc_t *stor_dev;
        long sz;
-       int i, res, bitmap_first, cnt;
+       int i, res, bitmap_first, cnt, old_ctrlc, got_ctrlc;
        char *env;
+       long start, end;
 
 #undef ERASE_EEPROM
 #ifdef ERASE_EEPROM
@@ -436,14 +468,14 @@ 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;
        }
        /* check whether it has a partition table */
-       stor_dev = usb_stor_get_dev(au_usb_stor_curr_dev);
-       if (stor_dev->type == DEV_TYPE_UNKNOWN) {
+       stor_dev = get_dev("usb", 0);
+       if (stor_dev == NULL) {
                debug ("uknown device type\n");
                return -1;
        }
@@ -471,9 +503,82 @@ do_auto_update(void)
        ausize[IDX_KERNEL] = (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST;
        ausize[IDX_APP] = (AU_FL_APP_ND + 1) - AU_FL_APP_ST;
        ausize[IDX_DISK] = (AU_FL_DISK_ND + 1) - AU_FL_DISK_ST;
+       /*
+        * now check whether start and end are defined using environment
+        * variables.
+        */
+       start = -1;
+       end = 0;
+       env = getenv("firmware_st");
+       if (env != NULL)
+               start = simple_strtoul(env, NULL, 16);
+       env = getenv("firmware_nd");
+       if (env != NULL)
+               end = simple_strtoul(env, NULL, 16);
+       if (start >= 0 && end && end > start) {
+               ausize[IDX_FIRMWARE] = (end + 1) - start;
+               aufl_layout[0].start = start;
+               aufl_layout[0].end = end;
+       }
+       start = -1;
+       end = 0;
+       env = getenv("kernel_st");
+       if (env != NULL)
+               start = simple_strtoul(env, NULL, 16);
+       env = getenv("kernel_nd");
+       if (env != NULL)
+               end = simple_strtoul(env, NULL, 16);
+       if (start >= 0 && end && end > start) {
+               ausize[IDX_KERNEL] = (end + 1) - start;
+               aufl_layout[1].start = start;
+               aufl_layout[1].end = end;
+       }
+       start = -1;
+       end = 0;
+       env = getenv("app_st");
+       if (env != NULL)
+               start = simple_strtoul(env, NULL, 16);
+       env = getenv("app_nd");
+       if (env != NULL)
+               end = simple_strtoul(env, NULL, 16);
+       if (start >= 0 && end && end > start) {
+               ausize[IDX_APP] = (end + 1) - start;
+               aufl_layout[2].start = start;
+               aufl_layout[2].end = end;
+       }
+       start = -1;
+       end = 0;
+       env = getenv("disk_st");
+       if (env != NULL)
+               start = simple_strtoul(env, NULL, 16);
+       env = getenv("disk_nd");
+       if (env != NULL)
+               end = simple_strtoul(env, NULL, 16);
+       if (start >= 0 && end && end > start) {
+               ausize[IDX_DISK] = (end + 1) - start;
+               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));
@@ -481,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
@@ -509,18 +614,36 @@ do_auto_update(void)
                /* this is really not a good idea, but it's what the */
                /* customer wants. */
                cnt = 0;
+               got_ctrlc = 0;
                do {
                        res = au_do_update(i, sz);
-#ifdef AU_TEST_ONLY
+                       /* let the user break out of the loop */
+                       if (ctrlc() || had_ctrlc()) {
+                               clear_ctrlc();
+                               if (res < 0)
+                                       got_ctrlc = 1;
+                               break;
+                       }
                        cnt++;
+#ifdef AU_TEST_ONLY
                } while (res < 0 && cnt < 3);
                if (cnt < 3)
 #else
                } while (res < 0);
 #endif
-               au_update_eeprom(i);
+               /*
+                * it doesn't make sense to update the EEPROM if the
+                * update was interrupted by the user due to errors.
+                */
+               if (got_ctrlc == 0)
+                       au_update_eeprom(i);
+               else
+                       /* enable the power switch */
+                       *CPLD_VFD_BK &= ~POWER_OFF;
        }
        usb_stop();
+       /* restore the old state */
+       disable_ctrlc(old_ctrlc);
        return 0;
 }
 #endif /* CONFIG_AUTO_UPDATE */