wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Gary Jennejohn, DENX Software Engineering, gj@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <command.h> |
| 26 | #include <malloc.h> |
| 27 | #include <image.h> |
| 28 | #include <asm/byteorder.h> |
| 29 | #include <usb.h> |
| 30 | |
| 31 | #ifdef CFG_HUSH_PARSER |
| 32 | #include <hush.h> |
| 33 | #endif |
| 34 | |
| 35 | #ifdef CONFIG_AUTO_UPDATE |
| 36 | |
| 37 | #ifndef CONFIG_USB_OHCI |
| 38 | #error "must define CONFIG_USB_OHCI" |
| 39 | #endif |
| 40 | |
| 41 | #ifndef CONFIG_USB_STORAGE |
| 42 | #error "must define CONFIG_USB_STORAGE" |
| 43 | #endif |
| 44 | |
| 45 | #ifndef CFG_HUSH_PARSER |
| 46 | #error "must define CFG_HUSH_PARSER" |
| 47 | #endif |
| 48 | |
| 49 | #if !(CONFIG_COMMANDS & CFG_CMD_FAT) |
| 50 | #error "must define CFG_CMD_FAT" |
| 51 | #endif |
| 52 | |
| 53 | /* |
| 54 | * Check whether a USB memory stick is plugged in. |
| 55 | * If one is found: |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 56 | * 1) if prepare.img ist found load it into memory. If it is |
| 57 | * valid then run it. |
| 58 | * 2) if preinst.img is found load it into memory. If it is |
| 59 | * valid then run it. Update the EEPROM. |
| 60 | * 3) if firmware.img is found load it into memory. If it is valid, |
| 61 | * burn it into FLASH and update the EEPROM. |
| 62 | * 4) if kernel.img is found load it into memory. If it is valid, |
| 63 | * burn it into FLASH and update the EEPROM. |
| 64 | * 5) if app.img is found load it into memory. If it is valid, |
| 65 | * burn it into FLASH and update the EEPROM. |
| 66 | * 6) if disk.img is found load it into memory. If it is valid, |
| 67 | * burn it into FLASH and update the EEPROM. |
| 68 | * 7) if postinst.img is found load it into memory. If it is |
| 69 | * valid then run it. Update the EEPROM. |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 70 | */ |
| 71 | |
| 72 | #undef AU_DEBUG |
| 73 | |
| 74 | #undef debug |
| 75 | #ifdef AU_DEBUG |
| 76 | #define debug(fmt,args...) printf (fmt ,##args) |
| 77 | #else |
| 78 | #define debug(fmt,args...) |
| 79 | #endif /* AU_DEBUG */ |
| 80 | |
| 81 | /* possible names of files on the USB stick. */ |
| 82 | #define AU_PREPARE "prepare.img" |
| 83 | #define AU_PREINST "preinst.img" |
| 84 | #define AU_FIRMWARE "firmware.img" |
| 85 | #define AU_KERNEL "kernel.img" |
| 86 | #define AU_APP "app.img" |
| 87 | #define AU_DISK "disk.img" |
| 88 | #define AU_POSTINST "postinst.img" |
| 89 | |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 90 | struct flash_layout |
| 91 | { |
| 92 | long start; |
| 93 | long end; |
| 94 | }; |
| 95 | |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 96 | /* layout of the FLASH. ST = start address, ND = end address. */ |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 97 | #ifndef CONFIG_FLASH_8MB /* 16 MB Flash, 32 MB RAM */ |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 98 | #define AU_FL_FIRMWARE_ST 0x00000000 |
| 99 | #define AU_FL_FIRMWARE_ND 0x0009FFFF |
| 100 | #define AU_FL_VFD_ST 0x000A0000 |
| 101 | #define AU_FL_VFD_ND 0x000BFFFF |
| 102 | #define AU_FL_KERNEL_ST 0x000C0000 |
| 103 | #define AU_FL_KERNEL_ND 0x001BFFFF |
| 104 | #define AU_FL_APP_ST 0x001C0000 |
| 105 | #define AU_FL_APP_ND 0x005BFFFF |
| 106 | #define AU_FL_DISK_ST 0x005C0000 |
| 107 | #define AU_FL_DISK_ND 0x00FFFFFF |
dzu | 8a42eac | 2003-09-29 21:55:54 +0000 | [diff] [blame] | 108 | #else /* 8 MB Flash, 32 MB RAM */ |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 109 | #define AU_FL_FIRMWARE_ST 0x00000000 |
dzu | 8a42eac | 2003-09-29 21:55:54 +0000 | [diff] [blame] | 110 | #define AU_FL_FIRMWARE_ND 0x0005FFFF |
| 111 | #define AU_FL_KERNEL_ST 0x00060000 |
| 112 | #define AU_FL_KERNEL_ND 0x0013FFFF |
| 113 | #define AU_FL_APP_ST 0x00140000 |
| 114 | #define AU_FL_APP_ND 0x0067FFFF |
| 115 | #define AU_FL_DISK_ST 0x00680000 |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 116 | #define AU_FL_DISK_ND 0x007DFFFF |
| 117 | #define AU_FL_VFD_ST 0x007E0000 |
| 118 | #define AU_FL_VFD_ND 0x007FFFFF |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 119 | #endif /* CONFIG_FLASH_8MB */ |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 120 | |
| 121 | /* a structure with the offsets to values in the EEPROM */ |
| 122 | struct eeprom_layout |
| 123 | { |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 124 | int time; |
| 125 | int size; |
| 126 | int dcrc; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | /* layout of the EEPROM - offset from the start. All entries are 32 bit. */ |
| 130 | #define AU_EEPROM_TIME_PREINST 64 |
| 131 | #define AU_EEPROM_SIZE_PREINST 68 |
| 132 | #define AU_EEPROM_DCRC_PREINST 72 |
| 133 | #define AU_EEPROM_TIME_FIRMWARE 76 |
| 134 | #define AU_EEPROM_SIZE_FIRMWARE 80 |
| 135 | #define AU_EEPROM_DCRC_FIRMWARE 84 |
| 136 | #define AU_EEPROM_TIME_KERNEL 88 |
| 137 | #define AU_EEPROM_SIZE_KERNEL 92 |
| 138 | #define AU_EEPROM_DCRC_KERNEL 96 |
| 139 | #define AU_EEPROM_TIME_APP 100 |
| 140 | #define AU_EEPROM_SIZE_APP 104 |
| 141 | #define AU_EEPROM_DCRC_APP 108 |
| 142 | #define AU_EEPROM_TIME_DISK 112 |
| 143 | #define AU_EEPROM_SIZE_DISK 116 |
| 144 | #define AU_EEPROM_DCRC_DISK 120 |
| 145 | #define AU_EEPROM_TIME_POSTINST 124 |
| 146 | #define AU_EEPROM_SIZE_POSTINST 128 |
| 147 | #define AU_EEPROM_DCRC_POSTINST 132 |
| 148 | |
| 149 | static int au_usb_stor_curr_dev; /* current device */ |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 150 | |
| 151 | /* index of each file in the following arrays */ |
| 152 | #define IDX_PREPARE 0 |
| 153 | #define IDX_PREINST 1 |
| 154 | #define IDX_FIRMWARE 2 |
| 155 | #define IDX_KERNEL 3 |
| 156 | #define IDX_APP 4 |
| 157 | #define IDX_DISK 5 |
| 158 | #define IDX_POSTINST 6 |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 159 | /* max. number of files which could interest us */ |
| 160 | #define AU_MAXFILES 7 |
| 161 | /* pointers to file names */ |
| 162 | char *aufile[AU_MAXFILES]; |
| 163 | /* sizes of flash areas for each file */ |
| 164 | long ausize[AU_MAXFILES]; |
| 165 | /* offsets into the EEEPROM */ |
| 166 | struct eeprom_layout auee_off[AU_MAXFILES] = { \ |
| 167 | {0}, \ |
| 168 | {AU_EEPROM_TIME_PREINST, AU_EEPROM_SIZE_PREINST, AU_EEPROM_DCRC_PREINST,}, \ |
| 169 | {AU_EEPROM_TIME_FIRMWARE, AU_EEPROM_SIZE_FIRMWARE, AU_EEPROM_DCRC_FIRMWARE,}, \ |
| 170 | {AU_EEPROM_TIME_KERNEL, AU_EEPROM_SIZE_KERNEL, AU_EEPROM_DCRC_KERNEL,}, \ |
| 171 | {AU_EEPROM_TIME_APP, AU_EEPROM_SIZE_APP, AU_EEPROM_DCRC_APP,}, \ |
| 172 | {AU_EEPROM_TIME_DISK, AU_EEPROM_SIZE_DISK, AU_EEPROM_DCRC_DISK,}, \ |
| 173 | {AU_EEPROM_TIME_POSTINST, AU_EEPROM_SIZE_POSTINST, AU_EEPROM_DCRC_POSTINST,} \ |
| 174 | }; |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 175 | /* array of flash areas start and end addresses */ |
| 176 | struct flash_layout aufl_layout[AU_MAXFILES - 3] = { \ |
| 177 | {AU_FL_FIRMWARE_ST, AU_FL_FIRMWARE_ND,}, \ |
| 178 | {AU_FL_KERNEL_ST, AU_FL_KERNEL_ND,}, \ |
| 179 | {AU_FL_APP_ST, AU_FL_APP_ND,}, \ |
| 180 | {AU_FL_DISK_ST, AU_FL_DISK_ND,}, \ |
| 181 | }; |
| 182 | /* convert the index into aufile[] to an index into aufl_layout[] */ |
| 183 | #define FIDX_TO_LIDX(idx) ((idx) - 2) |
| 184 | |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 185 | /* where to load files into memory */ |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 186 | #define LOAD_ADDR ((unsigned char *)0x0C100100) |
| 187 | /* where to build strings in memory - 256 bytes should be enough */ |
| 188 | #define STRING_ADDR ((char *)0x0C100000) |
dzu | 8a42eac | 2003-09-29 21:55:54 +0000 | [diff] [blame] | 189 | /* the app is the largest image */ |
| 190 | #define MAX_LOADSZ ausize[IDX_APP] |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 191 | |
| 192 | /* externals */ |
| 193 | extern int fat_register_device(block_dev_desc_t *, int); |
| 194 | extern int file_fat_detectfs(void); |
| 195 | extern long file_fat_read(const char *, void *, unsigned long); |
| 196 | extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int); |
| 197 | extern int i2c_write (uchar, uint, int , uchar* , int); |
| 198 | #ifdef CONFIG_VFD |
| 199 | extern int trab_vfd (ulong); |
| 200 | extern int transfer_pic(unsigned char, unsigned char *, int, int); |
| 201 | #endif |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 202 | /* change char* to void* to shutup the compiler */ |
| 203 | extern int i2c_write_multiple (uchar, uint, int, void *, int); |
| 204 | extern int i2c_read_multiple (uchar, uint, int, void *, int); |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 205 | extern block_dev_desc_t *get_dev (char*, int); |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 206 | |
| 207 | int |
| 208 | au_check_valid(int idx, long nbytes) |
| 209 | { |
| 210 | image_header_t *hdr; |
| 211 | unsigned long checksum; |
| 212 | unsigned char buf[4]; |
| 213 | |
| 214 | hdr = (image_header_t *)LOAD_ADDR; |
| 215 | /* check the easy ones first */ |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 216 | #undef CHECK_VALID_DEBUG |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 217 | #ifdef CHECK_VALID_DEBUG |
| 218 | printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC); |
| 219 | printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_ARM); |
| 220 | printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes); |
| 221 | printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL); |
| 222 | #endif |
| 223 | if (ntohl(hdr->ih_magic) != IH_MAGIC || |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 224 | hdr->ih_arch != IH_CPU_ARM || |
dzu | 8a42eac | 2003-09-29 21:55:54 +0000 | [diff] [blame] | 225 | nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size))) |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 226 | { |
| 227 | printf ("Image %s bad MAGIC or ARCH or SIZE\n", aufile[idx]); |
| 228 | return -1; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 229 | } |
| 230 | /* check the hdr CRC */ |
| 231 | checksum = ntohl(hdr->ih_hcrc); |
| 232 | hdr->ih_hcrc = 0; |
| 233 | |
| 234 | if (crc32 (0, (char *)hdr, sizeof(*hdr)) != checksum) { |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 235 | printf ("Image %s bad header checksum\n", aufile[idx]); |
| 236 | return -1; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 237 | } |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 238 | hdr->ih_hcrc = htonl(checksum); |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 239 | /* check the data CRC */ |
| 240 | checksum = ntohl(hdr->ih_dcrc); |
| 241 | |
| 242 | if (crc32 (0, (char *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size)) |
| 243 | != checksum) |
| 244 | { |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 245 | printf ("Image %s bad data checksum\n", aufile[idx]); |
| 246 | return -1; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 247 | } |
| 248 | /* check the type - could do this all in one gigantic if() */ |
| 249 | if ((idx == IDX_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) { |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 250 | printf ("Image %s wrong type\n", aufile[idx]); |
| 251 | return -1; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 252 | } |
| 253 | if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) { |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 254 | printf ("Image %s wrong type\n", aufile[idx]); |
| 255 | return -1; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 256 | } |
wdenk | d9a405a | 2003-10-07 20:01:55 +0000 | [diff] [blame^] | 257 | if ((idx == IDX_DISK) && (hdr->ih_type != IH_TYPE_FILESYSTEM)) { |
| 258 | printf ("Image %s wrong type\n", aufile[idx]); |
| 259 | return -1; |
| 260 | } |
| 261 | if ((idx == IDX_APP) && (hdr->ih_type != IH_TYPE_RAMDISK)) { |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 262 | printf ("Image %s wrong type\n", aufile[idx]); |
| 263 | return -1; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 264 | } |
| 265 | if ((idx == IDX_PREPARE || idx == IDX_PREINST || idx == IDX_POSTINST) |
| 266 | && (hdr->ih_type != IH_TYPE_SCRIPT)) |
| 267 | { |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 268 | printf ("Image %s wrong type\n", aufile[idx]); |
| 269 | return -1; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 270 | } |
| 271 | /* special case for prepare.img */ |
| 272 | if (idx == IDX_PREPARE) |
| 273 | return 0; |
wdenk | d9a405a | 2003-10-07 20:01:55 +0000 | [diff] [blame^] | 274 | /* recycle checksum */ |
| 275 | checksum = ntohl(hdr->ih_size); |
| 276 | /* for kernel and app the image header must also fit into flash */ |
| 277 | if (idx != IDX_DISK) |
| 278 | checksum += sizeof(*hdr); |
| 279 | /* check the size does not exceed space in flash. HUSH scripts */ |
| 280 | /* all have ausize[] set to 0 */ |
| 281 | if ((ausize[idx] != 0) && (ausize[idx] < checksum)) { |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 282 | printf ("Image %s is bigger than FLASH\n", aufile[idx]); |
| 283 | return -1; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 284 | } |
| 285 | /* check the time stamp from the EEPROM */ |
| 286 | /* read it in */ |
| 287 | i2c_read_multiple(0x54, auee_off[idx].time, 1, buf, sizeof(buf)); |
| 288 | #ifdef CHECK_VALID_DEBUG |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 289 | printf ("buf[0] %#x buf[1] %#x buf[2] %#x buf[3] %#x " |
| 290 | "as int %#x time %#x\n", |
| 291 | buf[0], buf[1], buf[2], buf[3], |
| 292 | *((unsigned int *)buf), ntohl(hdr->ih_time)); |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 293 | #endif |
| 294 | /* check it */ |
| 295 | if (*((unsigned int *)buf) >= ntohl(hdr->ih_time)) { |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 296 | printf ("Image %s is too old\n", aufile[idx]); |
| 297 | return -1; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | /* power control defines */ |
| 304 | #define CPLD_VFD_BK ((volatile char *)0x04038002) |
| 305 | #define POWER_OFF (1 << 1) |
| 306 | |
| 307 | int |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 308 | au_do_update(int idx, long sz, int repeat) |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 309 | { |
| 310 | image_header_t *hdr; |
| 311 | char *addr; |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 312 | long start, end; |
| 313 | char *strbuf = STRING_ADDR; |
| 314 | int off; |
| 315 | uint nbytes; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 316 | |
| 317 | hdr = (image_header_t *)LOAD_ADDR; |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 318 | |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 319 | /* disable the power switch */ |
| 320 | *CPLD_VFD_BK |= POWER_OFF; |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 321 | |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 322 | /* execute a script */ |
| 323 | if (hdr->ih_type == IH_TYPE_SCRIPT) { |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 324 | addr = (char *)((char *)hdr + sizeof(*hdr)); |
| 325 | /* stick a NULL at the end of the script, otherwise */ |
| 326 | /* parse_string_outer() runs off the end. */ |
| 327 | addr[ntohl(hdr->ih_size)] = 0; |
| 328 | addr += 8; |
| 329 | parse_string_outer(addr, FLAG_PARSE_SEMICOLON); |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 330 | return 0; |
| 331 | } |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 332 | |
| 333 | start = aufl_layout[FIDX_TO_LIDX(idx)].start; |
| 334 | end = aufl_layout[FIDX_TO_LIDX(idx)].end; |
| 335 | |
| 336 | /* unprotect the address range */ |
| 337 | /* this assumes that ONLY the firmware is protected! */ |
| 338 | if (idx == IDX_FIRMWARE) { |
| 339 | #undef AU_UPDATE_TEST |
| 340 | #ifdef AU_UPDATE_TEST |
| 341 | /* erase it where Linux goes */ |
| 342 | start = aufl_layout[1].start; |
| 343 | end = aufl_layout[1].end; |
| 344 | #endif |
| 345 | debug ("protect off %lx %lx\n", start, end); |
| 346 | sprintf(strbuf, "protect off %lx %lx\n", start, end); |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 347 | parse_string_outer(strbuf, FLAG_PARSE_SEMICOLON); |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 348 | } |
| 349 | |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 350 | /* |
| 351 | * erase the address range. Multiple erases seem to cause |
| 352 | * problems. |
| 353 | */ |
| 354 | if (repeat == 0) { |
| 355 | debug ("erase %lx %lx\n", start, end); |
| 356 | sprintf(strbuf, "erase %lx %lx\n", start, end); |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 357 | parse_string_outer(strbuf, FLAG_PARSE_SEMICOLON); |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 358 | } |
| 359 | wait_ms(100); |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 360 | /* strip the header - except for the kernel and app */ |
| 361 | if (idx == IDX_FIRMWARE || idx == IDX_DISK) { |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 362 | addr = (char *)((char *)hdr + sizeof(*hdr)); |
| 363 | #ifdef AU_UPDATE_TEST |
| 364 | /* copy it to where Linux goes */ |
| 365 | if (idx == IDX_FIRMWARE) |
| 366 | start = aufl_layout[1].start; |
| 367 | #endif |
| 368 | off = 0; |
| 369 | nbytes = ntohl(hdr->ih_size); |
| 370 | } else { |
| 371 | addr = (char *)hdr; |
| 372 | off = sizeof(*hdr); |
| 373 | nbytes = sizeof(*hdr) + ntohl(hdr->ih_size); |
| 374 | } |
| 375 | |
| 376 | /* copy the data from RAM to FLASH */ |
| 377 | debug ("cp.b %p %lx %x\n", addr, start, nbytes); |
| 378 | sprintf(strbuf, "cp.b %p %lx %x\n", addr, start, nbytes); |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 379 | parse_string_outer(strbuf, FLAG_PARSE_SEMICOLON); |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 380 | |
| 381 | /* check the dcrc of the copy */ |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 382 | if (crc32 (0, (char *)(start + off), ntohl(hdr->ih_size)) != ntohl(hdr->ih_dcrc)) { |
| 383 | printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]); |
| 384 | return -1; |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* protect the address range */ |
| 388 | /* this assumes that ONLY the firmware is protected! */ |
| 389 | if (idx == IDX_FIRMWARE) { |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 390 | debug ("protect on %lx %lx\n", start, end); |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 391 | sprintf(strbuf, "protect on %lx %lx\n", start, end); |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 392 | parse_string_outer(strbuf, FLAG_PARSE_SEMICOLON); |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 393 | } |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | int |
| 398 | au_update_eeprom(int idx) |
| 399 | { |
| 400 | image_header_t *hdr; |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 401 | int off; |
| 402 | uint32_t val; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 403 | |
| 404 | hdr = (image_header_t *)LOAD_ADDR; |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 405 | /* write the time field into EEPROM */ |
| 406 | off = auee_off[idx].time; |
| 407 | val = ntohl(hdr->ih_time); |
| 408 | i2c_write_multiple(0x54, off, 1, &val, sizeof(val)); |
| 409 | /* write the size field into EEPROM */ |
| 410 | off = auee_off[idx].size; |
| 411 | val = ntohl(hdr->ih_size); |
| 412 | i2c_write_multiple(0x54, off, 1, &val, sizeof(val)); |
| 413 | /* write the dcrc field into EEPROM */ |
| 414 | off = auee_off[idx].dcrc; |
| 415 | val = ntohl(hdr->ih_dcrc); |
| 416 | i2c_write_multiple(0x54, off, 1, &val, sizeof(val)); |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 417 | /* enable the power switch */ |
| 418 | *CPLD_VFD_BK &= ~POWER_OFF; |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * this is called from board_init() after the hardware has been set up |
| 424 | * and is usable. That seems like a good time to do this. |
| 425 | * Right now the return value is ignored. |
| 426 | */ |
| 427 | int |
| 428 | do_auto_update(void) |
| 429 | { |
| 430 | block_dev_desc_t *stor_dev; |
| 431 | long sz; |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 432 | int i, res, bitmap_first, cnt, old_ctrlc, got_ctrlc; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 433 | char *env; |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 434 | long start, end; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 435 | |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 436 | #undef ERASE_EEPROM |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 437 | #ifdef ERASE_EEPROM |
| 438 | int arr[18]; |
| 439 | memset(arr, 0, sizeof(arr)); |
| 440 | i2c_write_multiple(0x54, 64, 1, arr, sizeof(arr)); |
| 441 | #endif |
| 442 | au_usb_stor_curr_dev = -1; |
| 443 | /* start USB */ |
| 444 | if (usb_stop() < 0) { |
| 445 | debug ("usb_stop failed\n"); |
| 446 | return -1; |
| 447 | } |
| 448 | if (usb_init() < 0) { |
| 449 | debug ("usb_init failed\n"); |
| 450 | return -1; |
| 451 | } |
| 452 | /* |
| 453 | * check whether a storage device is attached (assume that it's |
| 454 | * a USB memory stick, since nothing else should be attached). |
| 455 | */ |
| 456 | au_usb_stor_curr_dev = usb_stor_scan(1); |
| 457 | if (au_usb_stor_curr_dev == -1) { |
| 458 | debug ("No device found. Not initialized?\n"); |
| 459 | return -1; |
| 460 | } |
| 461 | /* check whether it has a partition table */ |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 462 | stor_dev = get_dev("usb", 0); |
| 463 | if (stor_dev == NULL) { |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 464 | debug ("uknown device type\n"); |
| 465 | return -1; |
| 466 | } |
| 467 | if (fat_register_device(stor_dev, 1) != 0) { |
| 468 | debug ("Unable to use USB %d:%d for fatls\n", |
| 469 | au_usb_stor_curr_dev, 1); |
| 470 | return -1; |
| 471 | } |
| 472 | if (file_fat_detectfs() != 0) { |
| 473 | debug ("file_fat_detectfs failed\n"); |
| 474 | } |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 475 | |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 476 | /* initialize the array of file names */ |
| 477 | memset(aufile, 0, sizeof(aufile)); |
| 478 | aufile[IDX_PREPARE] = AU_PREPARE; |
| 479 | aufile[IDX_PREINST] = AU_PREINST; |
| 480 | aufile[IDX_FIRMWARE] = AU_FIRMWARE; |
| 481 | aufile[IDX_KERNEL] = AU_KERNEL; |
| 482 | aufile[IDX_APP] = AU_APP; |
| 483 | aufile[IDX_DISK] = AU_DISK; |
| 484 | aufile[IDX_POSTINST] = AU_POSTINST; |
| 485 | /* initialize the array of flash sizes */ |
| 486 | memset(ausize, 0, sizeof(ausize)); |
| 487 | ausize[IDX_FIRMWARE] = (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST; |
| 488 | ausize[IDX_KERNEL] = (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST; |
| 489 | ausize[IDX_APP] = (AU_FL_APP_ND + 1) - AU_FL_APP_ST; |
| 490 | ausize[IDX_DISK] = (AU_FL_DISK_ND + 1) - AU_FL_DISK_ST; |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 491 | /* |
| 492 | * now check whether start and end are defined using environment |
| 493 | * variables. |
| 494 | */ |
wdenk | 1d70468 | 2003-09-19 08:29:25 +0000 | [diff] [blame] | 495 | start = -1; |
| 496 | end = 0; |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 497 | env = getenv("firmware_st"); |
| 498 | if (env != NULL) |
| 499 | start = simple_strtoul(env, NULL, 16); |
| 500 | env = getenv("firmware_nd"); |
| 501 | if (env != NULL) |
| 502 | end = simple_strtoul(env, NULL, 16); |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 503 | if (start >= 0 && end && end > start) { |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 504 | ausize[IDX_FIRMWARE] = (end + 1) - start; |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 505 | aufl_layout[0].start = start; |
| 506 | aufl_layout[0].end = end; |
| 507 | } |
wdenk | 1d70468 | 2003-09-19 08:29:25 +0000 | [diff] [blame] | 508 | start = -1; |
| 509 | end = 0; |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 510 | env = getenv("kernel_st"); |
| 511 | if (env != NULL) |
| 512 | start = simple_strtoul(env, NULL, 16); |
| 513 | env = getenv("kernel_nd"); |
| 514 | if (env != NULL) |
| 515 | end = simple_strtoul(env, NULL, 16); |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 516 | if (start >= 0 && end && end > start) { |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 517 | ausize[IDX_KERNEL] = (end + 1) - start; |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 518 | aufl_layout[1].start = start; |
| 519 | aufl_layout[1].end = end; |
| 520 | } |
wdenk | 1d70468 | 2003-09-19 08:29:25 +0000 | [diff] [blame] | 521 | start = -1; |
| 522 | end = 0; |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 523 | env = getenv("app_st"); |
| 524 | if (env != NULL) |
| 525 | start = simple_strtoul(env, NULL, 16); |
| 526 | env = getenv("app_nd"); |
| 527 | if (env != NULL) |
| 528 | end = simple_strtoul(env, NULL, 16); |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 529 | if (start >= 0 && end && end > start) { |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 530 | ausize[IDX_APP] = (end + 1) - start; |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 531 | aufl_layout[2].start = start; |
| 532 | aufl_layout[2].end = end; |
| 533 | } |
wdenk | 1d70468 | 2003-09-19 08:29:25 +0000 | [diff] [blame] | 534 | start = -1; |
| 535 | end = 0; |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 536 | env = getenv("disk_st"); |
| 537 | if (env != NULL) |
| 538 | start = simple_strtoul(env, NULL, 16); |
| 539 | env = getenv("disk_nd"); |
| 540 | if (env != NULL) |
| 541 | end = simple_strtoul(env, NULL, 16); |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 542 | if (start >= 0 && end && end > start) { |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 543 | ausize[IDX_DISK] = (end + 1) - start; |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 544 | aufl_layout[3].start = start; |
| 545 | aufl_layout[3].end = end; |
| 546 | } |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 547 | /* make sure that we see CTRL-C and save the old state */ |
| 548 | old_ctrlc = disable_ctrlc(0); |
| 549 | |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 550 | bitmap_first = 0; |
| 551 | /* just loop thru all the possible files */ |
| 552 | for (i = 0; i < AU_MAXFILES; i++) { |
| 553 | sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ); |
| 554 | debug ("read %s sz %ld hdr %d\n", |
| 555 | aufile[i], sz, sizeof(image_header_t)); |
| 556 | if (sz <= 0 || sz <= sizeof(image_header_t)) { |
| 557 | debug ("%s not found\n", aufile[i]); |
| 558 | continue; |
| 559 | } |
| 560 | if (au_check_valid(i, sz) < 0) { |
| 561 | debug ("%s not valid\n", aufile[i]); |
| 562 | continue; |
| 563 | } |
| 564 | #ifdef CONFIG_VFD |
| 565 | /* now that we have a valid file we can display the */ |
| 566 | /* bitmap. */ |
| 567 | if (bitmap_first == 0) { |
| 568 | env = getenv("bitmap2"); |
| 569 | if (env == NULL) { |
| 570 | trab_vfd(0); |
| 571 | } else { |
| 572 | /* not so simple - bitmap2 is supposed to */ |
| 573 | /* contain the address of the bitmap */ |
| 574 | env = (char *)simple_strtoul(env, NULL, 16); |
| 575 | /* NOTE: these are taken from vfd_logo.h. If that file changes then */ |
| 576 | /* these defines MUST also be updated! These may be wrong for bitmap2. */ |
| 577 | #define VFD_LOGO_WIDTH 112 |
| 578 | #define VFD_LOGO_HEIGHT 72 |
| 579 | /* must call transfer_pic directly */ |
| 580 | transfer_pic(3, env, VFD_LOGO_HEIGHT, VFD_LOGO_WIDTH); |
| 581 | } |
| 582 | bitmap_first = 1; |
| 583 | } |
| 584 | #endif |
| 585 | /* this is really not a good idea, but it's what the */ |
| 586 | /* customer wants. */ |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 587 | cnt = 0; |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 588 | got_ctrlc = 0; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 589 | do { |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 590 | res = au_do_update(i, sz, cnt); |
| 591 | /* let the user break out of the loop */ |
| 592 | if (ctrlc() || had_ctrlc()) { |
| 593 | clear_ctrlc(); |
| 594 | if (res < 0) |
| 595 | got_ctrlc = 1; |
| 596 | break; |
| 597 | } |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 598 | cnt++; |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 599 | #ifdef AU_TEST_ONLY |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 600 | } while (res < 0 && cnt < 3); |
| 601 | if (cnt < 3) |
| 602 | #else |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 603 | } while (res < 0); |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 604 | #endif |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 605 | /* |
| 606 | * it doesn't make sense to update the EEPROM if the |
| 607 | * update was interrupted by the user due to errors. |
| 608 | */ |
| 609 | if (got_ctrlc == 0) |
| 610 | au_update_eeprom(i); |
dzu | 8a42eac | 2003-09-29 21:55:54 +0000 | [diff] [blame] | 611 | else |
| 612 | /* enable the power switch */ |
| 613 | *CPLD_VFD_BK &= ~POWER_OFF; |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 614 | } |
wdenk | b0639ca | 2003-09-17 22:48:07 +0000 | [diff] [blame] | 615 | usb_stop(); |
wdenk | 8036986 | 2003-09-18 18:55:25 +0000 | [diff] [blame] | 616 | /* restore the old state */ |
| 617 | disable_ctrlc(old_ctrlc); |
wdenk | f54ebdf | 2003-09-17 15:10:32 +0000 | [diff] [blame] | 618 | return 0; |
| 619 | } |
| 620 | #endif /* CONFIG_AUTO_UPDATE */ |