Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2006 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
| 9 | #include <malloc.h> |
| 10 | #include <image.h> |
| 11 | #include <asm/byteorder.h> |
| 12 | #include <usb.h> |
Grant Likely | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 13 | #include <part.h> |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 14 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 15 | #ifdef CONFIG_AUTO_UPDATE |
| 16 | |
| 17 | #ifndef CONFIG_USB_OHCI |
| 18 | #error "must define CONFIG_USB_OHCI" |
| 19 | #endif |
| 20 | |
| 21 | #ifndef CONFIG_USB_STORAGE |
| 22 | #error "must define CONFIG_USB_STORAGE" |
| 23 | #endif |
| 24 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 25 | #ifndef CONFIG_SYS_HUSH_PARSER |
| 26 | #error "must define CONFIG_SYS_HUSH_PARSER" |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 27 | #endif |
| 28 | |
Jon Loeliger | 3fe0010 | 2007-07-09 18:38:39 -0500 | [diff] [blame] | 29 | #if !defined(CONFIG_CMD_FAT) |
Jon Loeliger | d39b574 | 2007-07-10 10:48:22 -0500 | [diff] [blame] | 30 | #error "must define CONFIG_CMD_FAT" |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 31 | #endif |
| 32 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 33 | #undef AU_DEBUG |
| 34 | |
| 35 | #undef debug |
| 36 | #ifdef AU_DEBUG |
| 37 | #define debug(fmt,args...) printf (fmt ,##args) |
| 38 | #else |
| 39 | #define debug(fmt,args...) |
| 40 | #endif /* AU_DEBUG */ |
| 41 | |
| 42 | /* possible names of files on the USB stick. */ |
| 43 | #define AU_FIRMWARE "u-boot.img" |
| 44 | #define AU_KERNEL "kernel.img" |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 45 | #define AU_ROOTFS "rootfs.img" |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 46 | |
Wolfgang Denk | 82e5236 | 2006-12-22 10:30:26 +0100 | [diff] [blame] | 47 | struct flash_layout { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 48 | long start; |
| 49 | long end; |
| 50 | }; |
| 51 | |
| 52 | /* layout of the FLASH. ST = start address, ND = end address. */ |
| 53 | #define AU_FL_FIRMWARE_ST 0xfC000000 |
| 54 | #define AU_FL_FIRMWARE_ND 0xfC03FFFF |
| 55 | #define AU_FL_KERNEL_ST 0xfC0C0000 |
| 56 | #define AU_FL_KERNEL_ND 0xfC1BFFFF |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 57 | #define AU_FL_ROOTFS_ST 0xFC1C0000 |
| 58 | #define AU_FL_ROOTFS_ND 0xFCFBFFFF |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 59 | |
| 60 | static int au_usb_stor_curr_dev; /* current device */ |
| 61 | |
| 62 | /* index of each file in the following arrays */ |
| 63 | #define IDX_FIRMWARE 0 |
| 64 | #define IDX_KERNEL 1 |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 65 | #define IDX_ROOTFS 2 |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 66 | |
| 67 | /* max. number of files which could interest us */ |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 68 | #define AU_MAXFILES 3 |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 69 | |
| 70 | /* pointers to file names */ |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 71 | char *aufile[AU_MAXFILES] = { |
| 72 | AU_FIRMWARE, |
| 73 | AU_KERNEL, |
| 74 | AU_ROOTFS |
| 75 | }; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 76 | |
| 77 | /* sizes of flash areas for each file */ |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 78 | long ausize[AU_MAXFILES] = { |
| 79 | (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST, |
Wolfgang Denk | f5fcc3c | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 80 | (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST, |
| 81 | (AU_FL_ROOTFS_ND + 1) - AU_FL_ROOTFS_ST, |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 82 | }; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 83 | |
| 84 | /* array of flash areas start and end addresses */ |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 85 | struct flash_layout aufl_layout[AU_MAXFILES] = { |
Wolfgang Denk | f5fcc3c | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 86 | { AU_FL_FIRMWARE_ST, AU_FL_FIRMWARE_ND, }, |
| 87 | { AU_FL_KERNEL_ST, AU_FL_KERNEL_ND, }, |
| 88 | { AU_FL_ROOTFS_ST, AU_FL_ROOTFS_ND, }, |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 89 | }; |
| 90 | |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 91 | ulong totsize; |
| 92 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 93 | /* where to load files into memory */ |
| 94 | #define LOAD_ADDR ((unsigned char *)0x00200000) |
| 95 | |
Wolfgang Denk | f5fcc3c | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 96 | /* the root file system is the largest image */ |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 97 | #define MAX_LOADSZ ausize[IDX_ROOTFS] |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 98 | |
| 99 | /*i2c address of the keypad status*/ |
| 100 | #define I2C_PSOC_KEYPAD_ADDR 0x53 |
| 101 | |
| 102 | /* keypad mask */ |
Wolfgang Denk | 787fa15 | 2007-01-10 01:28:39 +0100 | [diff] [blame] | 103 | #define KEYPAD_ROW 2 |
| 104 | #define KEYPAD_COL 2 |
| 105 | #define KEYPAD_MASK_LO ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))&0xFF) |
| 106 | #define KEYPAD_MASK_HI ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))>>8) |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 107 | |
| 108 | /* externals */ |
| 109 | extern int fat_register_device(block_dev_desc_t *, int); |
| 110 | extern int file_fat_detectfs(void); |
| 111 | extern long file_fat_read(const char *, void *, unsigned long); |
| 112 | extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int); |
| 113 | extern int flash_sect_erase(ulong, ulong); |
| 114 | extern int flash_sect_protect (int, ulong, ulong); |
| 115 | extern int flash_write (char *, ulong, ulong); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 116 | extern int u_boot_hush_start(void); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 117 | #ifdef CONFIG_PROGRESSBAR |
| 118 | extern void show_progress(int, int); |
| 119 | extern void lcd_puts (char *); |
| 120 | extern void lcd_enable(void); |
| 121 | #endif |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 122 | |
Wolfgang Denk | 82e5236 | 2006-12-22 10:30:26 +0100 | [diff] [blame] | 123 | int au_check_cksum_valid(int idx, long nbytes) |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 124 | { |
| 125 | image_header_t *hdr; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 126 | |
| 127 | hdr = (image_header_t *)LOAD_ADDR; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 128 | #if defined(CONFIG_FIT) |
Marian Balakowicz | 9a4daad | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 129 | if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 130 | puts ("Non legacy image format not supported\n"); |
| 131 | return -1; |
| 132 | } |
| 133 | #endif |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 134 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 135 | if (nbytes != image_get_image_size (hdr)) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 136 | printf ("Image %s bad total SIZE\n", aufile[idx]); |
| 137 | return -1; |
| 138 | } |
| 139 | /* check the data CRC */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 140 | if (!image_check_dcrc (hdr)) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 141 | printf ("Image %s bad data checksum\n", aufile[idx]); |
| 142 | return -1; |
| 143 | } |
| 144 | return 0; |
| 145 | } |
| 146 | |
Wolfgang Denk | 82e5236 | 2006-12-22 10:30:26 +0100 | [diff] [blame] | 147 | int au_check_header_valid(int idx, long nbytes) |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 148 | { |
| 149 | image_header_t *hdr; |
Sergei Poselenov | e344568 | 2007-02-27 20:15:30 +0300 | [diff] [blame] | 150 | unsigned long checksum, fsize; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 151 | |
| 152 | hdr = (image_header_t *)LOAD_ADDR; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 153 | #if defined(CONFIG_FIT) |
Marian Balakowicz | 9a4daad | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 154 | if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 155 | puts ("Non legacy image format not supported\n"); |
| 156 | return -1; |
| 157 | } |
| 158 | #endif |
| 159 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 160 | /* check the easy ones first */ |
| 161 | #undef CHECK_VALID_DEBUG |
| 162 | #ifdef CHECK_VALID_DEBUG |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 163 | printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC); |
| 164 | printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_ARM); |
| 165 | printf("size %#x %#lx ", image_get_data_size (hdr), nbytes); |
| 166 | printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 167 | #endif |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 168 | if (nbytes < image_get_header_size ()) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 169 | printf ("Image %s bad header SIZE\n", aufile[idx]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 170 | ausize[idx] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 171 | return -1; |
| 172 | } |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 173 | if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 174 | printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 175 | ausize[idx] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 176 | return -1; |
| 177 | } |
| 178 | /* check the hdr CRC */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 179 | if (!image_check_hcrc (hdr)) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 180 | printf ("Image %s bad header checksum\n", aufile[idx]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 181 | ausize[idx] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 182 | return -1; |
| 183 | } |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 184 | /* check the type - could do this all in one gigantic if() */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 185 | if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 186 | printf ("Image %s wrong type\n", aufile[idx]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 187 | ausize[idx] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 188 | return -1; |
| 189 | } |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 190 | if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 191 | printf ("Image %s wrong type\n", aufile[idx]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 192 | ausize[idx] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 193 | return -1; |
| 194 | } |
Wolfgang Denk | f5fcc3c | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 195 | if ((idx == IDX_ROOTFS) && |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 196 | (!image_check_type (hdr, IH_TYPE_RAMDISK) && |
| 197 | !image_check_type (hdr, IH_TYPE_FILESYSTEM))) { |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 198 | printf ("Image %s wrong type\n", aufile[idx]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 199 | ausize[idx] = 0; |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 200 | return -1; |
| 201 | } |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 202 | /* recycle checksum */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 203 | checksum = image_get_data_size (hdr); |
Sergei Poselenov | e344568 | 2007-02-27 20:15:30 +0300 | [diff] [blame] | 204 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 205 | fsize = checksum + image_get_header_size (); |
Sergei Poselenov | e344568 | 2007-02-27 20:15:30 +0300 | [diff] [blame] | 206 | /* for kernel and ramdisk the image header must also fit into flash */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 207 | if (idx == IDX_KERNEL || image_check_type (hdr, IH_TYPE_RAMDISK)) |
| 208 | checksum += image_get_header_size (); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 209 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 210 | /* check the size does not exceed space in flash. HUSH scripts */ |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 211 | if ((ausize[idx] != 0) && (ausize[idx] < checksum)) { |
| 212 | printf ("Image %s is bigger than FLASH\n", aufile[idx]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 213 | ausize[idx] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 214 | return -1; |
| 215 | } |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 216 | /* Update with the real filesize */ |
Sergei Poselenov | e344568 | 2007-02-27 20:15:30 +0300 | [diff] [blame] | 217 | ausize[idx] = fsize; |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 218 | |
| 219 | return checksum; /* return size to be written to flash */ |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Wolfgang Denk | 82e5236 | 2006-12-22 10:30:26 +0100 | [diff] [blame] | 222 | int au_do_update(int idx, long sz) |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 223 | { |
| 224 | image_header_t *hdr; |
| 225 | char *addr; |
| 226 | long start, end; |
| 227 | int off, rc; |
| 228 | uint nbytes; |
| 229 | |
| 230 | hdr = (image_header_t *)LOAD_ADDR; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 231 | #if defined(CONFIG_FIT) |
Marian Balakowicz | 9a4daad | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 232 | if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 233 | puts ("Non legacy image format not supported\n"); |
| 234 | return -1; |
| 235 | } |
| 236 | #endif |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 237 | |
| 238 | /* execute a script */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 239 | if (image_check_type (hdr, IH_TYPE_SCRIPT)) { |
| 240 | addr = (char *)((char *)hdr + image_get_header_size ()); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 241 | /* stick a NULL at the end of the script, otherwise */ |
| 242 | /* parse_string_outer() runs off the end. */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 243 | addr[image_get_data_size (hdr)] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 244 | addr += 8; |
Simon Glass | ae4223f | 2014-04-10 20:01:23 -0600 | [diff] [blame] | 245 | run_command_list(addr, -1, 0); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | start = aufl_layout[idx].start; |
| 250 | end = aufl_layout[idx].end; |
| 251 | |
| 252 | /* unprotect the address range */ |
| 253 | /* this assumes that ONLY the firmware is protected! */ |
| 254 | if (idx == IDX_FIRMWARE) { |
| 255 | #undef AU_UPDATE_TEST |
| 256 | #ifdef AU_UPDATE_TEST |
| 257 | /* erase it where Linux goes */ |
| 258 | start = aufl_layout[1].start; |
| 259 | end = aufl_layout[1].end; |
| 260 | #endif |
| 261 | flash_sect_protect(0, start, end); |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * erase the address range. |
| 266 | */ |
| 267 | debug ("flash_sect_erase(%lx, %lx);\n", start, end); |
| 268 | flash_sect_erase(start, end); |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 269 | mdelay(100); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 270 | #ifdef CONFIG_PROGRESSBAR |
| 271 | show_progress(end - start, totsize); |
| 272 | #endif |
| 273 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 274 | /* strip the header - except for the kernel and ramdisk */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 275 | if (image_check_type (hdr, IH_TYPE_KERNEL) || |
| 276 | image_check_type (hdr, IH_TYPE_RAMDISK)) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 277 | addr = (char *)hdr; |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 278 | off = image_get_header_size (); |
| 279 | nbytes = image_get_image_size (hdr); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 280 | } else { |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 281 | addr = (char *)((char *)hdr + image_get_header_size ()); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 282 | #ifdef AU_UPDATE_TEST |
| 283 | /* copy it to where Linux goes */ |
| 284 | if (idx == IDX_FIRMWARE) |
| 285 | start = aufl_layout[1].start; |
| 286 | #endif |
| 287 | off = 0; |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 288 | nbytes = image_get_data_size (hdr); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* copy the data from RAM to FLASH */ |
| 292 | debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes); |
| 293 | rc = flash_write(addr, start, nbytes); |
| 294 | if (rc != 0) { |
| 295 | printf("Flashing failed due to error %d\n", rc); |
| 296 | return -1; |
| 297 | } |
| 298 | |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 299 | #ifdef CONFIG_PROGRESSBAR |
| 300 | show_progress(nbytes, totsize); |
| 301 | #endif |
| 302 | |
Wolfgang Denk | f5fcc3c | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 303 | /* check the data CRC of the copy */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 304 | if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) != |
| 305 | image_get_dcrc (hdr)) { |
Wolfgang Denk | f5fcc3c | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 306 | printf ("Image %s Bad Data Checksum after COPY\n", aufile[idx]); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | /* protect the address range */ |
| 311 | /* this assumes that ONLY the firmware is protected! */ |
| 312 | if (idx == IDX_FIRMWARE) |
| 313 | flash_sect_protect(1, start, end); |
| 314 | return 0; |
| 315 | } |
| 316 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 317 | /* |
| 318 | * this is called from board_init() after the hardware has been set up |
| 319 | * and is usable. That seems like a good time to do this. |
| 320 | * Right now the return value is ignored. |
| 321 | */ |
Wolfgang Denk | 82e5236 | 2006-12-22 10:30:26 +0100 | [diff] [blame] | 322 | int do_auto_update(void) |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 323 | { |
| 324 | block_dev_desc_t *stor_dev; |
| 325 | long sz; |
Wolfgang Denk | 55b9788 | 2011-11-04 15:55:11 +0000 | [diff] [blame] | 326 | int i, res = 0, cnt, old_ctrlc; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 327 | char *env; |
| 328 | long start, end; |
Wolfgang Denk | a4d2636 | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 329 | |
| 330 | #if 0 /* disable key-press detection to speed up boot-up time */ |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 331 | uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0}; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 332 | |
| 333 | /* |
| 334 | * Read keypad status |
| 335 | */ |
| 336 | i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2); |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 337 | mdelay(500); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 338 | i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2); |
| 339 | |
| 340 | /* |
| 341 | * Check keypad |
| 342 | */ |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 343 | if ( !(keypad_status1[1] & KEYPAD_MASK_LO) || |
| 344 | (keypad_status1[1] != keypad_status2[1])) { |
| 345 | return 0; |
| 346 | } |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 347 | |
Wolfgang Denk | a4d2636 | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 348 | #endif |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 349 | au_usb_stor_curr_dev = -1; |
| 350 | /* start USB */ |
| 351 | if (usb_stop() < 0) { |
| 352 | debug ("usb_stop failed\n"); |
| 353 | return -1; |
| 354 | } |
| 355 | if (usb_init() < 0) { |
| 356 | debug ("usb_init failed\n"); |
| 357 | return -1; |
| 358 | } |
| 359 | /* |
| 360 | * check whether a storage device is attached (assume that it's |
| 361 | * a USB memory stick, since nothing else should be attached). |
| 362 | */ |
| 363 | au_usb_stor_curr_dev = usb_stor_scan(0); |
| 364 | if (au_usb_stor_curr_dev == -1) { |
| 365 | debug ("No device found. Not initialized?\n"); |
Wolfgang Denk | a4d2636 | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 366 | res = -1; |
| 367 | goto xit; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 368 | } |
| 369 | /* check whether it has a partition table */ |
| 370 | stor_dev = get_dev("usb", 0); |
| 371 | if (stor_dev == NULL) { |
| 372 | debug ("uknown device type\n"); |
Wolfgang Denk | a4d2636 | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 373 | res = -1; |
| 374 | goto xit; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 375 | } |
| 376 | if (fat_register_device(stor_dev, 1) != 0) { |
| 377 | debug ("Unable to use USB %d:%d for fatls\n", |
| 378 | au_usb_stor_curr_dev, 1); |
Wolfgang Denk | a4d2636 | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 379 | res = -1; |
| 380 | goto xit; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 381 | } |
| 382 | if (file_fat_detectfs() != 0) { |
| 383 | debug ("file_fat_detectfs failed\n"); |
| 384 | } |
| 385 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 386 | /* |
| 387 | * now check whether start and end are defined using environment |
| 388 | * variables. |
| 389 | */ |
| 390 | start = -1; |
| 391 | end = 0; |
| 392 | env = getenv("firmware_st"); |
| 393 | if (env != NULL) |
| 394 | start = simple_strtoul(env, NULL, 16); |
| 395 | env = getenv("firmware_nd"); |
| 396 | if (env != NULL) |
| 397 | end = simple_strtoul(env, NULL, 16); |
| 398 | if (start >= 0 && end && end > start) { |
| 399 | ausize[IDX_FIRMWARE] = (end + 1) - start; |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 400 | aufl_layout[IDX_FIRMWARE].start = start; |
| 401 | aufl_layout[IDX_FIRMWARE].end = end; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 402 | } |
| 403 | start = -1; |
| 404 | end = 0; |
| 405 | env = getenv("kernel_st"); |
| 406 | if (env != NULL) |
| 407 | start = simple_strtoul(env, NULL, 16); |
| 408 | env = getenv("kernel_nd"); |
| 409 | if (env != NULL) |
| 410 | end = simple_strtoul(env, NULL, 16); |
| 411 | if (start >= 0 && end && end > start) { |
| 412 | ausize[IDX_KERNEL] = (end + 1) - start; |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 413 | aufl_layout[IDX_KERNEL].start = start; |
| 414 | aufl_layout[IDX_KERNEL].end = end; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 415 | } |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 416 | start = -1; |
| 417 | end = 0; |
| 418 | env = getenv("rootfs_st"); |
| 419 | if (env != NULL) |
| 420 | start = simple_strtoul(env, NULL, 16); |
| 421 | env = getenv("rootfs_nd"); |
| 422 | if (env != NULL) |
| 423 | end = simple_strtoul(env, NULL, 16); |
| 424 | if (start >= 0 && end && end > start) { |
| 425 | ausize[IDX_ROOTFS] = (end + 1) - start; |
| 426 | aufl_layout[IDX_ROOTFS].start = start; |
| 427 | aufl_layout[IDX_ROOTFS].end = end; |
| 428 | } |
| 429 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 430 | /* make certain that HUSH is runnable */ |
| 431 | u_boot_hush_start(); |
| 432 | /* make sure that we see CTRL-C and save the old state */ |
| 433 | old_ctrlc = disable_ctrlc(0); |
| 434 | |
Wolfgang Denk | 7435711 | 2007-02-27 14:26:04 +0100 | [diff] [blame] | 435 | /* validate the images first */ |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 436 | for (i = 0; i < AU_MAXFILES; i++) { |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 437 | ulong imsize; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 438 | /* just read the header */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 439 | sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ()); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 440 | debug ("read %s sz %ld hdr %d\n", |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 441 | aufile[i], sz, image_get_header_size ()); |
| 442 | if (sz <= 0 || sz < image_get_header_size ()) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 443 | debug ("%s not found\n", aufile[i]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 444 | ausize[i] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 445 | continue; |
| 446 | } |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 447 | /* au_check_header_valid() updates ausize[] */ |
| 448 | if ((imsize = au_check_header_valid(i, sz)) < 0) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 449 | debug ("%s header not valid\n", aufile[i]); |
| 450 | continue; |
| 451 | } |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 452 | /* totsize accounts for image size and flash erase size */ |
Wolfgang Denk | 7435711 | 2007-02-27 14:26:04 +0100 | [diff] [blame] | 453 | totsize += (imsize + (aufl_layout[i].end - aufl_layout[i].start)); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | #ifdef CONFIG_PROGRESSBAR |
| 457 | if (totsize) { |
| 458 | lcd_puts(" Update in progress\n"); |
| 459 | lcd_enable(); |
| 460 | } |
| 461 | #endif |
| 462 | |
| 463 | /* just loop thru all the possible files */ |
| 464 | for (i = 0; i < AU_MAXFILES && totsize; i++) { |
| 465 | if (!ausize[i]) { |
| 466 | continue; |
| 467 | } |
| 468 | sz = file_fat_read(aufile[i], LOAD_ADDR, ausize[i]); |
| 469 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 470 | debug ("read %s sz %ld hdr %d\n", |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 471 | aufile[i], sz, image_get_header_size ()); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 472 | |
| 473 | if (sz != ausize[i]) { |
Wolfgang Denk | 9b55a25 | 2008-07-11 01:16:00 +0200 | [diff] [blame] | 474 | printf ("%s: size %ld read %ld?\n", aufile[i], ausize[i], sz); |
Wolfgang Denk | 7435711 | 2007-02-27 14:26:04 +0100 | [diff] [blame] | 475 | continue; |
| 476 | } |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 477 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 478 | if (sz <= 0 || sz <= image_get_header_size ()) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 479 | debug ("%s not found\n", aufile[i]); |
| 480 | continue; |
| 481 | } |
| 482 | if (au_check_cksum_valid(i, sz) < 0) { |
| 483 | debug ("%s checksum not valid\n", aufile[i]); |
| 484 | continue; |
| 485 | } |
| 486 | /* this is really not a good idea, but it's what the */ |
| 487 | /* customer wants. */ |
| 488 | cnt = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 489 | do { |
| 490 | res = au_do_update(i, sz); |
| 491 | /* let the user break out of the loop */ |
| 492 | if (ctrlc() || had_ctrlc()) { |
| 493 | clear_ctrlc(); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 494 | break; |
| 495 | } |
| 496 | cnt++; |
| 497 | #ifdef AU_TEST_ONLY |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 498 | } while (res < 0 && cnt < (AU_MAXFILES + 1)); |
| 499 | if (cnt < (AU_MAXFILES + 1)) |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 500 | #else |
| 501 | } while (res < 0); |
| 502 | #endif |
| 503 | } |
Wolfgang Denk | a4d2636 | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 504 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 505 | /* restore the old state */ |
| 506 | disable_ctrlc(old_ctrlc); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 507 | #ifdef CONFIG_PROGRESSBAR |
| 508 | if (totsize) { |
| 509 | if (!res) { |
| 510 | lcd_puts("\n Update completed\n"); |
| 511 | } else { |
| 512 | lcd_puts("\n Update error\n"); |
| 513 | } |
| 514 | lcd_enable(); |
| 515 | } |
| 516 | #endif |
Wolfgang Denk | a4d2636 | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 517 | xit: |
| 518 | usb_stop(); |
| 519 | return res; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 520 | } |
| 521 | #endif /* CONFIG_AUTO_UPDATE */ |