Andrei Safronov | 8233624 | 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 | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 26 | #include <part.h> |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 27 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 28 | #ifdef CONFIG_SYS_HUSH_PARSER |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 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 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 43 | #ifndef CONFIG_SYS_HUSH_PARSER |
| 44 | #error "must define CONFIG_SYS_HUSH_PARSER" |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 45 | #endif |
| 46 | |
Jon Loeliger | 145318c | 2007-07-09 18:38:39 -0500 | [diff] [blame] | 47 | #if !defined(CONFIG_CMD_FAT) |
Jon Loeliger | 761ea74 | 2007-07-10 10:48:22 -0500 | [diff] [blame] | 48 | #error "must define CONFIG_CMD_FAT" |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 49 | #endif |
| 50 | |
Andrei Safronov | 8233624 | 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 | 8ceb08f | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 63 | #define AU_ROOTFS "rootfs.img" |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 64 | |
Wolfgang Denk | 8e59e1f | 2006-12-22 10:30:26 +0100 | [diff] [blame] | 65 | struct flash_layout { |
Andrei Safronov | 8233624 | 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 | 8ceb08f | 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 | 8233624 | 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 | 8ceb08f | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 83 | #define IDX_ROOTFS 2 |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 84 | |
| 85 | /* max. number of files which could interest us */ |
Sergei Poselenov | 8ceb08f | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 86 | #define AU_MAXFILES 3 |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 87 | |
| 88 | /* pointers to file names */ |
Sergei Poselenov | 8ceb08f | 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 | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 94 | |
| 95 | /* sizes of flash areas for each file */ |
Sergei Poselenov | 8ceb08f | 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 | f4da2ef | 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 | 8ceb08f | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 100 | }; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 101 | |
| 102 | /* array of flash areas start and end addresses */ |
Sergei Poselenov | 8ceb08f | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 103 | struct flash_layout aufl_layout[AU_MAXFILES] = { |
Wolfgang Denk | f4da2ef | 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 | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 107 | }; |
| 108 | |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 109 | ulong totsize; |
| 110 | |
Andrei Safronov | 8233624 | 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 | f4da2ef | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 114 | /* the root file system is the largest image */ |
Sergei Poselenov | 8ceb08f | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 115 | #define MAX_LOADSZ ausize[IDX_ROOTFS] |
Andrei Safronov | 8233624 | 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 | 299d610 | 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 | 8233624 | 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 | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 134 | extern int u_boot_hush_start(void); |
Sergei Poselenov | 91904cd | 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 | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 140 | |
Wolfgang Denk | 8e59e1f | 2006-12-22 10:30:26 +0100 | [diff] [blame] | 141 | int au_check_cksum_valid(int idx, long nbytes) |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 142 | { |
| 143 | image_header_t *hdr; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 144 | |
| 145 | hdr = (image_header_t *)LOAD_ADDR; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 146 | #if defined(CONFIG_FIT) |
Marian Balakowicz | d7c88a4 | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 147 | if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 148 | puts ("Non legacy image format not supported\n"); |
| 149 | return -1; |
| 150 | } |
| 151 | #endif |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 152 | |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 153 | if (nbytes != image_get_image_size (hdr)) { |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 154 | printf ("Image %s bad total SIZE\n", aufile[idx]); |
| 155 | return -1; |
| 156 | } |
| 157 | /* check the data CRC */ |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 158 | if (!image_check_dcrc (hdr)) { |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 159 | printf ("Image %s bad data checksum\n", aufile[idx]); |
| 160 | return -1; |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
Wolfgang Denk | 8e59e1f | 2006-12-22 10:30:26 +0100 | [diff] [blame] | 165 | int au_check_header_valid(int idx, long nbytes) |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 166 | { |
| 167 | image_header_t *hdr; |
Sergei Poselenov | 854fe36 | 2007-02-27 20:15:30 +0300 | [diff] [blame] | 168 | unsigned long checksum, fsize; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 169 | |
| 170 | hdr = (image_header_t *)LOAD_ADDR; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 171 | #if defined(CONFIG_FIT) |
Marian Balakowicz | d7c88a4 | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 172 | if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 173 | puts ("Non legacy image format not supported\n"); |
| 174 | return -1; |
| 175 | } |
| 176 | #endif |
| 177 | |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 178 | /* check the easy ones first */ |
| 179 | #undef CHECK_VALID_DEBUG |
| 180 | #ifdef CHECK_VALID_DEBUG |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 181 | printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC); |
| 182 | printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_ARM); |
| 183 | printf("size %#x %#lx ", image_get_data_size (hdr), nbytes); |
| 184 | printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL); |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 185 | #endif |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 186 | if (nbytes < image_get_header_size ()) { |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 187 | printf ("Image %s bad header SIZE\n", aufile[idx]); |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 188 | ausize[idx] = 0; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 189 | return -1; |
| 190 | } |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 191 | if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) { |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 192 | printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]); |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 193 | ausize[idx] = 0; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 194 | return -1; |
| 195 | } |
| 196 | /* check the hdr CRC */ |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 197 | if (!image_check_hcrc (hdr)) { |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 198 | printf ("Image %s bad header checksum\n", aufile[idx]); |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 199 | ausize[idx] = 0; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 200 | return -1; |
| 201 | } |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 202 | /* check the type - could do this all in one gigantic if() */ |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 203 | if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) { |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 204 | printf ("Image %s wrong type\n", aufile[idx]); |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 205 | ausize[idx] = 0; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 206 | return -1; |
| 207 | } |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 208 | if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) { |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 209 | printf ("Image %s wrong type\n", aufile[idx]); |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 210 | ausize[idx] = 0; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 211 | return -1; |
| 212 | } |
Wolfgang Denk | f4da2ef | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 213 | if ((idx == IDX_ROOTFS) && |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 214 | (!image_check_type (hdr, IH_TYPE_RAMDISK) && |
| 215 | !image_check_type (hdr, IH_TYPE_FILESYSTEM))) { |
Sergei Poselenov | 8ceb08f | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 216 | printf ("Image %s wrong type\n", aufile[idx]); |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 217 | ausize[idx] = 0; |
Sergei Poselenov | 8ceb08f | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 218 | return -1; |
| 219 | } |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 220 | /* recycle checksum */ |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 221 | checksum = image_get_data_size (hdr); |
Sergei Poselenov | 854fe36 | 2007-02-27 20:15:30 +0300 | [diff] [blame] | 222 | |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 223 | fsize = checksum + image_get_header_size (); |
Sergei Poselenov | 854fe36 | 2007-02-27 20:15:30 +0300 | [diff] [blame] | 224 | /* for kernel and ramdisk the image header must also fit into flash */ |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 225 | if (idx == IDX_KERNEL || image_check_type (hdr, IH_TYPE_RAMDISK)) |
| 226 | checksum += image_get_header_size (); |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 227 | |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 228 | /* check the size does not exceed space in flash. HUSH scripts */ |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 229 | if ((ausize[idx] != 0) && (ausize[idx] < checksum)) { |
| 230 | printf ("Image %s is bigger than FLASH\n", aufile[idx]); |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 231 | ausize[idx] = 0; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 232 | return -1; |
| 233 | } |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 234 | /* Update with the real filesize */ |
Sergei Poselenov | 854fe36 | 2007-02-27 20:15:30 +0300 | [diff] [blame] | 235 | ausize[idx] = fsize; |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 236 | |
| 237 | return checksum; /* return size to be written to flash */ |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Wolfgang Denk | 8e59e1f | 2006-12-22 10:30:26 +0100 | [diff] [blame] | 240 | int au_do_update(int idx, long sz) |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 241 | { |
| 242 | image_header_t *hdr; |
| 243 | char *addr; |
| 244 | long start, end; |
| 245 | int off, rc; |
| 246 | uint nbytes; |
| 247 | |
| 248 | hdr = (image_header_t *)LOAD_ADDR; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 249 | #if defined(CONFIG_FIT) |
Marian Balakowicz | d7c88a4 | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 250 | if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 251 | puts ("Non legacy image format not supported\n"); |
| 252 | return -1; |
| 253 | } |
| 254 | #endif |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 255 | |
| 256 | /* execute a script */ |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 257 | if (image_check_type (hdr, IH_TYPE_SCRIPT)) { |
| 258 | addr = (char *)((char *)hdr + image_get_header_size ()); |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 259 | /* stick a NULL at the end of the script, otherwise */ |
| 260 | /* parse_string_outer() runs off the end. */ |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 261 | addr[image_get_data_size (hdr)] = 0; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 262 | addr += 8; |
| 263 | parse_string_outer(addr, FLAG_PARSE_SEMICOLON); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | start = aufl_layout[idx].start; |
| 268 | end = aufl_layout[idx].end; |
| 269 | |
| 270 | /* unprotect the address range */ |
| 271 | /* this assumes that ONLY the firmware is protected! */ |
| 272 | if (idx == IDX_FIRMWARE) { |
| 273 | #undef AU_UPDATE_TEST |
| 274 | #ifdef AU_UPDATE_TEST |
| 275 | /* erase it where Linux goes */ |
| 276 | start = aufl_layout[1].start; |
| 277 | end = aufl_layout[1].end; |
| 278 | #endif |
| 279 | flash_sect_protect(0, start, end); |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * erase the address range. |
| 284 | */ |
| 285 | debug ("flash_sect_erase(%lx, %lx);\n", start, end); |
| 286 | flash_sect_erase(start, end); |
Mike Frysinger | 60ce19a | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 287 | mdelay(100); |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 288 | #ifdef CONFIG_PROGRESSBAR |
| 289 | show_progress(end - start, totsize); |
| 290 | #endif |
| 291 | |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 292 | /* strip the header - except for the kernel and ramdisk */ |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 293 | if (image_check_type (hdr, IH_TYPE_KERNEL) || |
| 294 | image_check_type (hdr, IH_TYPE_RAMDISK)) { |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 295 | addr = (char *)hdr; |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 296 | off = image_get_header_size (); |
| 297 | nbytes = image_get_image_size (hdr); |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 298 | } else { |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 299 | addr = (char *)((char *)hdr + image_get_header_size ()); |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 300 | #ifdef AU_UPDATE_TEST |
| 301 | /* copy it to where Linux goes */ |
| 302 | if (idx == IDX_FIRMWARE) |
| 303 | start = aufl_layout[1].start; |
| 304 | #endif |
| 305 | off = 0; |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 306 | nbytes = image_get_data_size (hdr); |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* copy the data from RAM to FLASH */ |
| 310 | debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes); |
| 311 | rc = flash_write(addr, start, nbytes); |
| 312 | if (rc != 0) { |
| 313 | printf("Flashing failed due to error %d\n", rc); |
| 314 | return -1; |
| 315 | } |
| 316 | |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 317 | #ifdef CONFIG_PROGRESSBAR |
| 318 | show_progress(nbytes, totsize); |
| 319 | #endif |
| 320 | |
Wolfgang Denk | f4da2ef | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 321 | /* check the data CRC of the copy */ |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 322 | if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) != |
| 323 | image_get_dcrc (hdr)) { |
Wolfgang Denk | f4da2ef | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 324 | printf ("Image %s Bad Data Checksum after COPY\n", aufile[idx]); |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 325 | return -1; |
| 326 | } |
| 327 | |
| 328 | /* protect the address range */ |
| 329 | /* this assumes that ONLY the firmware is protected! */ |
| 330 | if (idx == IDX_FIRMWARE) |
| 331 | flash_sect_protect(1, start, end); |
| 332 | return 0; |
| 333 | } |
| 334 | |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 335 | /* |
| 336 | * this is called from board_init() after the hardware has been set up |
| 337 | * and is usable. That seems like a good time to do this. |
| 338 | * Right now the return value is ignored. |
| 339 | */ |
Wolfgang Denk | 8e59e1f | 2006-12-22 10:30:26 +0100 | [diff] [blame] | 340 | int do_auto_update(void) |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 341 | { |
| 342 | block_dev_desc_t *stor_dev; |
| 343 | long sz; |
Wolfgang Denk | 8af4919 | 2011-11-04 15:55:11 +0000 | [diff] [blame] | 344 | int i, res = 0, cnt, old_ctrlc; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 345 | char *env; |
| 346 | long start, end; |
Wolfgang Denk | 036384c | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 347 | |
| 348 | #if 0 /* disable key-press detection to speed up boot-up time */ |
Sergei Poselenov | 8ceb08f | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 349 | uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0}; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 350 | |
| 351 | /* |
| 352 | * Read keypad status |
| 353 | */ |
| 354 | i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2); |
Mike Frysinger | 60ce19a | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 355 | mdelay(500); |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 356 | i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2); |
| 357 | |
| 358 | /* |
| 359 | * Check keypad |
| 360 | */ |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 361 | if ( !(keypad_status1[1] & KEYPAD_MASK_LO) || |
| 362 | (keypad_status1[1] != keypad_status2[1])) { |
| 363 | return 0; |
| 364 | } |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 365 | |
Wolfgang Denk | 036384c | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 366 | #endif |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 367 | au_usb_stor_curr_dev = -1; |
| 368 | /* start USB */ |
| 369 | if (usb_stop() < 0) { |
| 370 | debug ("usb_stop failed\n"); |
| 371 | return -1; |
| 372 | } |
| 373 | if (usb_init() < 0) { |
| 374 | debug ("usb_init failed\n"); |
| 375 | return -1; |
| 376 | } |
| 377 | /* |
| 378 | * check whether a storage device is attached (assume that it's |
| 379 | * a USB memory stick, since nothing else should be attached). |
| 380 | */ |
| 381 | au_usb_stor_curr_dev = usb_stor_scan(0); |
| 382 | if (au_usb_stor_curr_dev == -1) { |
| 383 | debug ("No device found. Not initialized?\n"); |
Wolfgang Denk | 036384c | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 384 | res = -1; |
| 385 | goto xit; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 386 | } |
| 387 | /* check whether it has a partition table */ |
| 388 | stor_dev = get_dev("usb", 0); |
| 389 | if (stor_dev == NULL) { |
| 390 | debug ("uknown device type\n"); |
Wolfgang Denk | 036384c | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 391 | res = -1; |
| 392 | goto xit; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 393 | } |
| 394 | if (fat_register_device(stor_dev, 1) != 0) { |
| 395 | debug ("Unable to use USB %d:%d for fatls\n", |
| 396 | au_usb_stor_curr_dev, 1); |
Wolfgang Denk | 036384c | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 397 | res = -1; |
| 398 | goto xit; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 399 | } |
| 400 | if (file_fat_detectfs() != 0) { |
| 401 | debug ("file_fat_detectfs failed\n"); |
| 402 | } |
| 403 | |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 404 | /* |
| 405 | * now check whether start and end are defined using environment |
| 406 | * variables. |
| 407 | */ |
| 408 | start = -1; |
| 409 | end = 0; |
| 410 | env = getenv("firmware_st"); |
| 411 | if (env != NULL) |
| 412 | start = simple_strtoul(env, NULL, 16); |
| 413 | env = getenv("firmware_nd"); |
| 414 | if (env != NULL) |
| 415 | end = simple_strtoul(env, NULL, 16); |
| 416 | if (start >= 0 && end && end > start) { |
| 417 | ausize[IDX_FIRMWARE] = (end + 1) - start; |
Sergei Poselenov | 8ceb08f | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 418 | aufl_layout[IDX_FIRMWARE].start = start; |
| 419 | aufl_layout[IDX_FIRMWARE].end = end; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 420 | } |
| 421 | start = -1; |
| 422 | end = 0; |
| 423 | env = getenv("kernel_st"); |
| 424 | if (env != NULL) |
| 425 | start = simple_strtoul(env, NULL, 16); |
| 426 | env = getenv("kernel_nd"); |
| 427 | if (env != NULL) |
| 428 | end = simple_strtoul(env, NULL, 16); |
| 429 | if (start >= 0 && end && end > start) { |
| 430 | ausize[IDX_KERNEL] = (end + 1) - start; |
Sergei Poselenov | 8ceb08f | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 431 | aufl_layout[IDX_KERNEL].start = start; |
| 432 | aufl_layout[IDX_KERNEL].end = end; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 433 | } |
Sergei Poselenov | 8ceb08f | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 434 | start = -1; |
| 435 | end = 0; |
| 436 | env = getenv("rootfs_st"); |
| 437 | if (env != NULL) |
| 438 | start = simple_strtoul(env, NULL, 16); |
| 439 | env = getenv("rootfs_nd"); |
| 440 | if (env != NULL) |
| 441 | end = simple_strtoul(env, NULL, 16); |
| 442 | if (start >= 0 && end && end > start) { |
| 443 | ausize[IDX_ROOTFS] = (end + 1) - start; |
| 444 | aufl_layout[IDX_ROOTFS].start = start; |
| 445 | aufl_layout[IDX_ROOTFS].end = end; |
| 446 | } |
| 447 | |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 448 | /* make certain that HUSH is runnable */ |
| 449 | u_boot_hush_start(); |
| 450 | /* make sure that we see CTRL-C and save the old state */ |
| 451 | old_ctrlc = disable_ctrlc(0); |
| 452 | |
Wolfgang Denk | 52232fd | 2007-02-27 14:26:04 +0100 | [diff] [blame] | 453 | /* validate the images first */ |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 454 | for (i = 0; i < AU_MAXFILES; i++) { |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 455 | ulong imsize; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 456 | /* just read the header */ |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 457 | sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ()); |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 458 | debug ("read %s sz %ld hdr %d\n", |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 459 | aufile[i], sz, image_get_header_size ()); |
| 460 | if (sz <= 0 || sz < image_get_header_size ()) { |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 461 | debug ("%s not found\n", aufile[i]); |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 462 | ausize[i] = 0; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 463 | continue; |
| 464 | } |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 465 | /* au_check_header_valid() updates ausize[] */ |
| 466 | if ((imsize = au_check_header_valid(i, sz)) < 0) { |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 467 | debug ("%s header not valid\n", aufile[i]); |
| 468 | continue; |
| 469 | } |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 470 | /* totsize accounts for image size and flash erase size */ |
Wolfgang Denk | 52232fd | 2007-02-27 14:26:04 +0100 | [diff] [blame] | 471 | totsize += (imsize + (aufl_layout[i].end - aufl_layout[i].start)); |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | #ifdef CONFIG_PROGRESSBAR |
| 475 | if (totsize) { |
| 476 | lcd_puts(" Update in progress\n"); |
| 477 | lcd_enable(); |
| 478 | } |
| 479 | #endif |
| 480 | |
| 481 | /* just loop thru all the possible files */ |
| 482 | for (i = 0; i < AU_MAXFILES && totsize; i++) { |
| 483 | if (!ausize[i]) { |
| 484 | continue; |
| 485 | } |
| 486 | sz = file_fat_read(aufile[i], LOAD_ADDR, ausize[i]); |
| 487 | |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 488 | debug ("read %s sz %ld hdr %d\n", |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 489 | aufile[i], sz, image_get_header_size ()); |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 490 | |
| 491 | if (sz != ausize[i]) { |
Wolfgang Denk | 12cec0a | 2008-07-11 01:16:00 +0200 | [diff] [blame] | 492 | printf ("%s: size %ld read %ld?\n", aufile[i], ausize[i], sz); |
Wolfgang Denk | 52232fd | 2007-02-27 14:26:04 +0100 | [diff] [blame] | 493 | continue; |
| 494 | } |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 495 | |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 496 | if (sz <= 0 || sz <= image_get_header_size ()) { |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 497 | debug ("%s not found\n", aufile[i]); |
| 498 | continue; |
| 499 | } |
| 500 | if (au_check_cksum_valid(i, sz) < 0) { |
| 501 | debug ("%s checksum not valid\n", aufile[i]); |
| 502 | continue; |
| 503 | } |
| 504 | /* this is really not a good idea, but it's what the */ |
| 505 | /* customer wants. */ |
| 506 | cnt = 0; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 507 | do { |
| 508 | res = au_do_update(i, sz); |
| 509 | /* let the user break out of the loop */ |
| 510 | if (ctrlc() || had_ctrlc()) { |
| 511 | clear_ctrlc(); |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 512 | break; |
| 513 | } |
| 514 | cnt++; |
| 515 | #ifdef AU_TEST_ONLY |
Sergei Poselenov | 8ceb08f | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 516 | } while (res < 0 && cnt < (AU_MAXFILES + 1)); |
| 517 | if (cnt < (AU_MAXFILES + 1)) |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 518 | #else |
| 519 | } while (res < 0); |
| 520 | #endif |
| 521 | } |
Wolfgang Denk | 036384c | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 522 | |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 523 | /* restore the old state */ |
| 524 | disable_ctrlc(old_ctrlc); |
Sergei Poselenov | 91904cd | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 525 | #ifdef CONFIG_PROGRESSBAR |
| 526 | if (totsize) { |
| 527 | if (!res) { |
| 528 | lcd_puts("\n Update completed\n"); |
| 529 | } else { |
| 530 | lcd_puts("\n Update error\n"); |
| 531 | } |
| 532 | lcd_enable(); |
| 533 | } |
| 534 | #endif |
Wolfgang Denk | 036384c | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 535 | xit: |
| 536 | usb_stop(); |
| 537 | return res; |
Andrei Safronov | 8233624 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 538 | } |
| 539 | #endif /* CONFIG_AUTO_UPDATE */ |