]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - board/mcc200/auto_update.c
Merge branch 'master' of git://git.denx.de/u-boot-nds32
[karo-tx-uboot.git] / board / mcc200 / auto_update.c
index 43ef42bcc2f8833ab27b1281bb61f6f046fc3ead..2f622b0846e103f5b3036322269e5577ca451427 100644 (file)
@@ -2,20 +2,7 @@
  * (C) Copyright 2006
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 #include <common.h>
 #include <command.h>
@@ -23,8 +10,9 @@
 #include <image.h>
 #include <asm/byteorder.h>
 #include <usb.h>
+#include <part.h>
 
-#ifdef CFG_HUSH_PARSER
+#ifdef CONFIG_SYS_HUSH_PARSER
 #include <hush.h>
 #endif
 
 #error "must define CONFIG_USB_STORAGE"
 #endif
 
-#ifndef CFG_HUSH_PARSER
-#error "must define CFG_HUSH_PARSER"
+#ifndef CONFIG_SYS_HUSH_PARSER
+#error "must define CONFIG_SYS_HUSH_PARSER"
 #endif
 
-#if !(CONFIG_COMMANDS & CFG_CMD_FAT)
-#error "must define CFG_CMD_FAT"
+#if !defined(CONFIG_CMD_FAT)
+#error "must define CONFIG_CMD_FAT"
 #endif
 
 #undef AU_DEBUG
@@ -105,6 +93,8 @@ struct flash_layout aufl_layout[AU_MAXFILES] = {
        { AU_FL_ROOTFS_ST,      AU_FL_ROOTFS_ND,   },
 };
 
+ulong totsize;
+
 /* where to load files into memory */
 #define LOAD_ADDR ((unsigned char *)0x00200000)
 
@@ -128,25 +118,31 @@ extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
 extern int flash_sect_erase(ulong, ulong);
 extern int flash_sect_protect (int, ulong, ulong);
 extern int flash_write (char *, ulong, ulong);
-/* change char* to void* to shutup the compiler */
-extern block_dev_desc_t *get_dev (char*, int);
 extern int u_boot_hush_start(void);
+#ifdef CONFIG_PROGRESSBAR
+extern void show_progress(int, int);
+extern void lcd_puts (char *);
+extern void lcd_enable(void);
+#endif
 
 int au_check_cksum_valid(int idx, long nbytes)
 {
        image_header_t *hdr;
-       unsigned long checksum;
 
        hdr = (image_header_t *)LOAD_ADDR;
+#if defined(CONFIG_FIT)
+       if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
+               puts ("Non legacy image format not supported\n");
+               return -1;
+       }
+#endif
 
-       if (nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size))) {
+       if (nbytes != image_get_image_size (hdr)) {
                printf ("Image %s bad total SIZE\n", aufile[idx]);
                return -1;
        }
        /* check the data CRC */
-       checksum = ntohl(hdr->ih_dcrc);
-
-       if (crc32 (0, (uchar *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size)) != checksum) {
+       if (!image_check_dcrc (hdr)) {
                printf ("Image %s bad data checksum\n", aufile[idx]);
                return -1;
        }
@@ -156,61 +152,76 @@ int au_check_cksum_valid(int idx, long nbytes)
 int au_check_header_valid(int idx, long nbytes)
 {
        image_header_t *hdr;
-       unsigned long checksum;
+       unsigned long checksum, fsize;
 
        hdr = (image_header_t *)LOAD_ADDR;
+#if defined(CONFIG_FIT)
+       if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
+               puts ("Non legacy image format not supported\n");
+               return -1;
+       }
+#endif
+
        /* check the easy ones first */
 #undef CHECK_VALID_DEBUG
 #ifdef CHECK_VALID_DEBUG
-       printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
-       printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_ARM);
-       printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
-       printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
+       printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC);
+       printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_ARM);
+       printf("size %#x %#lx ", image_get_data_size (hdr), nbytes);
+       printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL);
 #endif
-       if (nbytes < sizeof(*hdr)) {
+       if (nbytes < image_get_header_size ()) {
                printf ("Image %s bad header SIZE\n", aufile[idx]);
+               ausize[idx] = 0;
                return -1;
        }
-       if (ntohl(hdr->ih_magic) != IH_MAGIC || hdr->ih_arch != IH_CPU_PPC) {
+       if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) {
                printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
+               ausize[idx] = 0;
                return -1;
        }
        /* check the hdr CRC */
-       checksum = ntohl(hdr->ih_hcrc);
-       hdr->ih_hcrc = 0;
-
-       if (crc32 (0, (uchar *)hdr, sizeof(*hdr)) != checksum) {
+       if (!image_check_hcrc (hdr)) {
                printf ("Image %s bad header checksum\n", aufile[idx]);
+               ausize[idx] = 0;
                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)) {
+       if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
                printf ("Image %s wrong type\n", aufile[idx]);
+               ausize[idx] = 0;
                return -1;
        }
-       if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
+       if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) {
                printf ("Image %s wrong type\n", aufile[idx]);
+               ausize[idx] = 0;
                return -1;
        }
        if ((idx == IDX_ROOTFS) &&
-               ( (hdr->ih_type != IH_TYPE_RAMDISK) || (hdr->ih_type != IH_TYPE_FILESYSTEM) )
-          ) {
+                       (!image_check_type (hdr, IH_TYPE_RAMDISK) &&
+                       !image_check_type (hdr, IH_TYPE_FILESYSTEM))) {
                printf ("Image %s wrong type\n", aufile[idx]);
+               ausize[idx] = 0;
                return -1;
        }
        /* recycle checksum */
-       checksum = ntohl(hdr->ih_size);
-       /* for kernel and app the image header must also fit into flash */
-       if (idx != IDX_FIRMWARE)
-               checksum += sizeof(*hdr);
+       checksum = image_get_data_size (hdr);
+
+       fsize = checksum + image_get_header_size ();
+       /* for kernel and ramdisk the image header must also fit into flash */
+       if (idx == IDX_KERNEL || image_check_type (hdr, IH_TYPE_RAMDISK))
+               checksum += image_get_header_size ();
+
        /* 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]);
+               ausize[idx] = 0;
                return -1;
        }
-       return 0;
+       /* Update with the real filesize */
+       ausize[idx] = fsize;
+
+       return checksum; /* return size to be written to flash */
 }
 
 int au_do_update(int idx, long sz)
@@ -222,13 +233,19 @@ int au_do_update(int idx, long sz)
        uint nbytes;
 
        hdr = (image_header_t *)LOAD_ADDR;
+#if defined(CONFIG_FIT)
+       if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
+               puts ("Non legacy image format not supported\n");
+               return -1;
+       }
+#endif
 
        /* execute a script */
-       if (hdr->ih_type == IH_TYPE_SCRIPT) {
-               addr = (char *)((char *)hdr + sizeof(*hdr));
+       if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
+               addr = (char *)((char *)hdr + image_get_header_size ());
                /* stick a NULL at the end of the script, otherwise */
                /* parse_string_outer() runs off the end. */
-               addr[ntohl(hdr->ih_size)] = 0;
+               addr[image_get_data_size (hdr)] = 0;
                addr += 8;
                parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
                return 0;
@@ -254,21 +271,26 @@ int au_do_update(int idx, long sz)
         */
        debug ("flash_sect_erase(%lx, %lx);\n", start, end);
        flash_sect_erase(start, end);
-       wait_ms(100);
+       mdelay(100);
+#ifdef CONFIG_PROGRESSBAR
+       show_progress(end - start, totsize);
+#endif
+
        /* strip the header - except for the kernel and ramdisk */
-       if (hdr->ih_type == IH_TYPE_KERNEL || hdr->ih_type == IH_TYPE_RAMDISK) {
+       if (image_check_type (hdr, IH_TYPE_KERNEL) ||
+                       image_check_type (hdr, IH_TYPE_RAMDISK)) {
                addr = (char *)hdr;
-               off = sizeof(*hdr);
-               nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
+               off = image_get_header_size ();
+               nbytes = image_get_image_size (hdr);
        } else {
-               addr = (char *)((char *)hdr + sizeof(*hdr));
+               addr = (char *)((char *)hdr + image_get_header_size ());
 #ifdef AU_UPDATE_TEST
                /* copy it to where Linux goes */
                if (idx == IDX_FIRMWARE)
                        start = aufl_layout[1].start;
 #endif
                off = 0;
-               nbytes = ntohl(hdr->ih_size);
+               nbytes = image_get_data_size (hdr);
        }
 
        /* copy the data from RAM to FLASH */
@@ -279,8 +301,13 @@ int au_do_update(int idx, long sz)
                return -1;
        }
 
+#ifdef CONFIG_PROGRESSBAR
+       show_progress(nbytes, totsize);
+#endif
+
        /* check the data CRC of the copy */
-       if (crc32 (0, (uchar *)(start + off), ntohl(hdr->ih_size)) != ntohl(hdr->ih_dcrc)) {
+       if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) !=
+           image_get_dcrc (hdr)) {
                printf ("Image %s Bad Data Checksum after COPY\n", aufile[idx]);
                return -1;
        }
@@ -301,16 +328,18 @@ int do_auto_update(void)
 {
        block_dev_desc_t *stor_dev;
        long sz;
-       int i, res, bitmap_first, cnt, old_ctrlc, got_ctrlc;
+       int i, res = 0, cnt, old_ctrlc;
        char *env;
        long start, end;
+
+#if 0 /* disable key-press detection to speed up boot-up time */
        uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
 
        /*
         * Read keypad status
         */
        i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
-       wait_ms(500);
+       mdelay(500);
        i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
 
        /*
@@ -320,6 +349,8 @@ int do_auto_update(void)
              (keypad_status1[1] != keypad_status2[1])) {
                return 0;
        }
+
+#endif
        au_usb_stor_curr_dev = -1;
        /* start USB */
        if (usb_stop() < 0) {
@@ -337,18 +368,21 @@ int do_auto_update(void)
        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;
+               res = -1;
+               goto xit;
        }
        /* check whether it has a partition table */
        stor_dev = get_dev("usb", 0);
        if (stor_dev == NULL) {
                debug ("uknown device type\n");
-               return -1;
+               res = -1;
+               goto xit;
        }
        if (fat_register_device(stor_dev, 1) != 0) {
                debug ("Unable to use USB %d:%d for fatls\n",
                        au_usb_stor_curr_dev, 1);
-               return -1;
+               res = -1;
+               goto xit;
        }
        if (file_fat_detectfs() != 0) {
                debug ("file_fat_detectfs failed\n");
@@ -403,25 +437,50 @@ int do_auto_update(void)
        /* 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 */
+       /* validate the images first */
        for (i = 0; i < AU_MAXFILES; i++) {
+               ulong imsize;
                /* just read the header */
-               sz = file_fat_read(aufile[i], LOAD_ADDR, sizeof(image_header_t));
+               sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ());
                debug ("read %s sz %ld hdr %d\n",
-                       aufile[i], sz, sizeof(image_header_t));
-               if (sz <= 0 || sz < sizeof(image_header_t)) {
+                       aufile[i], sz, image_get_header_size ());
+               if (sz <= 0 || sz < image_get_header_size ()) {
                        debug ("%s not found\n", aufile[i]);
+                       ausize[i] = 0;
                        continue;
                }
-               if (au_check_header_valid(i, sz) < 0) {
+               /* au_check_header_valid() updates ausize[] */
+               if ((imsize = 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);
+               /* totsize accounts for image size and flash erase size */
+               totsize += (imsize + (aufl_layout[i].end - aufl_layout[i].start));
+       }
+
+#ifdef CONFIG_PROGRESSBAR
+       if (totsize) {
+               lcd_puts(" Update in progress\n");
+               lcd_enable();
+       }
+#endif
+
+       /* just loop thru all the possible files */
+       for (i = 0; i < AU_MAXFILES && totsize; i++) {
+               if (!ausize[i]) {
+                       continue;
+               }
+               sz = file_fat_read(aufile[i], LOAD_ADDR, ausize[i]);
+
                debug ("read %s sz %ld hdr %d\n",
-                       aufile[i], sz, sizeof(image_header_t));
-               if (sz <= 0 || sz <= sizeof(image_header_t)) {
+                       aufile[i], sz, image_get_header_size ());
+
+               if (sz != ausize[i]) {
+                       printf ("%s: size %ld read %ld?\n", aufile[i], ausize[i], sz);
+                       continue;
+               }
+
+               if (sz <= 0 || sz <= image_get_header_size ()) {
                        debug ("%s not found\n", aufile[i]);
                        continue;
                }
@@ -432,14 +491,11 @@ int 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);
                        /* let the user break out of the loop */
                        if (ctrlc() || had_ctrlc()) {
                                clear_ctrlc();
-                               if (res < 0)
-                                       got_ctrlc = 1;
                                break;
                        }
                        cnt++;
@@ -450,9 +506,21 @@ int do_auto_update(void)
                } while (res < 0);
 #endif
        }
-       usb_stop();
+
        /* restore the old state */
        disable_ctrlc(old_ctrlc);
-       return 0;
+#ifdef CONFIG_PROGRESSBAR
+       if (totsize) {
+               if (!res) {
+                       lcd_puts("\n  Update completed\n");
+               } else {
+                       lcd_puts("\n   Update error\n");
+               }
+               lcd_enable();
+       }
+#endif
+ xit:
+       usb_stop();
+       return res;
 }
 #endif /* CONFIG_AUTO_UPDATE */