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