stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003-2004 |
Detlev Zundel | f1b3f2b | 2009-05-13 10:54:10 +0200 | [diff] [blame] | 3 | * Gary Jennejohn, DENX Software Engineering, garyj@denx.de. |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 4 | * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com |
| 5 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 22 | * MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | #include <common.h> |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 26 | |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 27 | #include <command.h> |
| 28 | #include <image.h> |
| 29 | #include <asm/byteorder.h> |
Jean-Christophe PLAGNIOL-VILLARD | 719bb5f | 2008-08-13 01:40:43 +0200 | [diff] [blame] | 30 | #if defined(CONFIG_NAND_LEGACY) |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 31 | #include <linux/mtd/nand_legacy.h> |
Matthias Fuchs | c0d6f11 | 2007-07-09 10:10:04 +0200 | [diff] [blame] | 32 | #endif |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 33 | #include <fat.h> |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 34 | #include <part.h> |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 35 | |
| 36 | #include "auto_update.h" |
| 37 | |
| 38 | #ifdef CONFIG_AUTO_UPDATE |
| 39 | |
Jon Loeliger | 4ed9ed6 | 2007-07-09 18:24:55 -0500 | [diff] [blame] | 40 | #if !defined(CONFIG_CMD_FAT) |
Jon Loeliger | 13f7599 | 2007-07-10 10:39:10 -0500 | [diff] [blame] | 41 | #error "must define CONFIG_CMD_FAT" |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | extern au_image_t au_image[]; |
| 45 | extern int N_AU_IMAGES; |
| 46 | |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 47 | /* where to load files into memory */ |
| 48 | #define LOAD_ADDR ((unsigned char *)0x100000) |
| 49 | #define MAX_LOADSZ 0x1c00000 |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 50 | |
| 51 | /* externals */ |
| 52 | extern int fat_register_device(block_dev_desc_t *, int); |
| 53 | extern int file_fat_detectfs(void); |
| 54 | extern long file_fat_read(const char *, void *, unsigned long); |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 55 | long do_fat_read (const char *filename, void *buffer, |
| 56 | unsigned long maxsize, int dols); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 57 | extern int flash_sect_erase(ulong, ulong); |
| 58 | extern int flash_sect_protect (int, ulong, ulong); |
Stefan Roese | bcb3b27 | 2005-10-20 16:39:16 +0200 | [diff] [blame] | 59 | extern int flash_write (char *, ulong, ulong); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 60 | |
Jean-Christophe PLAGNIOL-VILLARD | 719bb5f | 2008-08-13 01:40:43 +0200 | [diff] [blame] | 61 | #if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_LEGACY) |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 62 | /* references to names in cmd_nand.c */ |
| 63 | #define NANDRW_READ 0x01 |
| 64 | #define NANDRW_WRITE 0x00 |
| 65 | #define NANDRW_JFFS2 0x02 |
| 66 | #define NANDRW_JFFS2_SKIP 0x04 |
| 67 | extern struct nand_chip nand_dev_desc[]; |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 68 | extern int nand_legacy_rw(struct nand_chip* nand, int cmd, |
| 69 | size_t start, size_t len, |
| 70 | size_t * retlen, u_char * buf); |
| 71 | extern int nand_legacy_erase(struct nand_chip* nand, size_t ofs, |
| 72 | size_t len, int clean); |
Jon Loeliger | 4ed9ed6 | 2007-07-09 18:24:55 -0500 | [diff] [blame] | 73 | #endif |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 74 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 75 | extern block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE]; |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 76 | |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 77 | int au_check_cksum_valid(int i, long nbytes) |
| 78 | { |
| 79 | image_header_t *hdr; |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 80 | |
| 81 | hdr = (image_header_t *)LOAD_ADDR; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 82 | #if defined(CONFIG_FIT) |
Marian Balakowicz | d7c88a4 | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 83 | if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 84 | puts ("Non legacy image format not supported\n"); |
| 85 | return -1; |
| 86 | } |
| 87 | #endif |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 88 | |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 89 | if ((au_image[i].type == AU_FIRMWARE) && |
| 90 | (au_image[i].size != image_get_data_size (hdr))) { |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 91 | printf ("Image %s has wrong size\n", au_image[i].name); |
| 92 | return -1; |
| 93 | } |
| 94 | |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 95 | if (nbytes != (image_get_image_size (hdr))) { |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 96 | printf ("Image %s bad total SIZE\n", au_image[i].name); |
| 97 | return -1; |
| 98 | } |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 99 | |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 100 | /* check the data CRC */ |
| 101 | if (!image_check_dcrc (hdr)) { |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 102 | printf ("Image %s bad data checksum\n", au_image[i].name); |
| 103 | return -1; |
| 104 | } |
| 105 | return 0; |
| 106 | } |
| 107 | |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 108 | int au_check_header_valid(int i, long nbytes) |
| 109 | { |
| 110 | image_header_t *hdr; |
| 111 | unsigned long checksum; |
| 112 | |
| 113 | hdr = (image_header_t *)LOAD_ADDR; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 114 | #if defined(CONFIG_FIT) |
Marian Balakowicz | d7c88a4 | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 115 | if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 116 | puts ("Non legacy image format not supported\n"); |
| 117 | return -1; |
| 118 | } |
| 119 | #endif |
| 120 | |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 121 | /* check the easy ones first */ |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 122 | if (nbytes < image_get_header_size ()) { |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 123 | printf ("Image %s bad header SIZE\n", au_image[i].name); |
| 124 | return -1; |
| 125 | } |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 126 | if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) { |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 127 | printf ("Image %s bad MAGIC or ARCH\n", au_image[i].name); |
| 128 | return -1; |
| 129 | } |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 130 | if (!image_check_hcrc (hdr)) { |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 131 | printf ("Image %s bad header checksum\n", au_image[i].name); |
| 132 | return -1; |
| 133 | } |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 134 | |
| 135 | /* check the type - could do this all in one gigantic if() */ |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 136 | if (((au_image[i].type & AU_TYPEMASK) == AU_FIRMWARE) && |
| 137 | !image_check_type (hdr, IH_TYPE_FIRMWARE)) { |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 138 | printf ("Image %s wrong type\n", au_image[i].name); |
| 139 | return -1; |
| 140 | } |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 141 | if (((au_image[i].type & AU_TYPEMASK) == AU_SCRIPT) && |
| 142 | !image_check_type (hdr, IH_TYPE_SCRIPT)) { |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 143 | printf ("Image %s wrong type\n", au_image[i].name); |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | /* recycle checksum */ |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 148 | checksum = image_get_data_size (hdr); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 149 | |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 153 | int au_do_update(int i, long sz) |
| 154 | { |
| 155 | image_header_t *hdr; |
| 156 | char *addr; |
| 157 | long start, end; |
| 158 | int off, rc; |
| 159 | uint nbytes; |
| 160 | int k; |
Jean-Christophe PLAGNIOL-VILLARD | 719bb5f | 2008-08-13 01:40:43 +0200 | [diff] [blame] | 161 | #if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_LEGACY) |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 162 | int total; |
| 163 | #endif |
| 164 | |
| 165 | hdr = (image_header_t *)LOAD_ADDR; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 166 | #if defined(CONFIG_FIT) |
Marian Balakowicz | d7c88a4 | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 167 | if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 168 | puts ("Non legacy image format not supported\n"); |
| 169 | return -1; |
| 170 | } |
| 171 | #endif |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 172 | |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 173 | switch (au_image[i].type & AU_TYPEMASK) { |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 174 | case AU_SCRIPT: |
| 175 | printf("Executing script %s\n", au_image[i].name); |
| 176 | |
| 177 | /* execute a script */ |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 178 | if (image_check_type (hdr, IH_TYPE_SCRIPT)) { |
| 179 | addr = (char *)((char *)hdr + image_get_header_size ()); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 180 | /* stick a NULL at the end of the script, otherwise */ |
| 181 | /* parse_string_outer() runs off the end. */ |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 182 | addr[image_get_data_size (hdr)] = 0; |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 183 | addr += 8; |
| 184 | |
| 185 | /* |
| 186 | * Replace cr/lf with ; |
| 187 | */ |
| 188 | k = 0; |
| 189 | while (addr[k] != 0) { |
| 190 | if ((addr[k] == 10) || (addr[k] == 13)) { |
| 191 | addr[k] = ';'; |
| 192 | } |
| 193 | k++; |
| 194 | } |
| 195 | |
| 196 | run_command(addr, 0); |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | break; |
| 201 | |
| 202 | case AU_FIRMWARE: |
| 203 | case AU_NOR: |
| 204 | case AU_NAND: |
| 205 | start = au_image[i].start; |
| 206 | end = au_image[i].start + au_image[i].size - 1; |
| 207 | |
stroese | e72a7b7 | 2005-03-16 20:58:31 +0000 | [diff] [blame] | 208 | /* |
| 209 | * do not update firmware when image is already in flash. |
| 210 | */ |
| 211 | if (au_image[i].type == AU_FIRMWARE) { |
| 212 | char *orig = (char*)start; |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 213 | char *new = (char *)((char *)hdr + |
| 214 | image_get_header_size ()); |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 215 | nbytes = image_get_data_size (hdr); |
stroese | e72a7b7 | 2005-03-16 20:58:31 +0000 | [diff] [blame] | 216 | |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 217 | while (--nbytes) { |
stroese | e72a7b7 | 2005-03-16 20:58:31 +0000 | [diff] [blame] | 218 | if (*orig++ != *new++) { |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | if (!nbytes) { |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 223 | printf ("Skipping firmware update - " |
| 224 | "images are identical\n"); |
stroese | e72a7b7 | 2005-03-16 20:58:31 +0000 | [diff] [blame] | 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 229 | /* unprotect the address range */ |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 230 | if (((au_image[i].type & AU_FLAGMASK) == AU_PROTECT) || |
| 231 | (au_image[i].type == AU_FIRMWARE)) { |
| 232 | flash_sect_protect (0, start, end); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /* |
| 236 | * erase the address range. |
| 237 | */ |
| 238 | if (au_image[i].type != AU_NAND) { |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 239 | printf ("Updating NOR FLASH with image %s\n", |
| 240 | au_image[i].name); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 241 | debug ("flash_sect_erase(%lx, %lx);\n", start, end); |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 242 | flash_sect_erase (start, end); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 243 | } else { |
Jean-Christophe PLAGNIOL-VILLARD | 719bb5f | 2008-08-13 01:40:43 +0200 | [diff] [blame] | 244 | #if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_LEGACY) |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 245 | printf ("Updating NAND FLASH with image %s\n", |
| 246 | au_image[i].name); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 247 | debug ("nand_legacy_erase(%lx, %lx);\n", start, end); |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 248 | rc = nand_legacy_erase (nand_dev_desc, start, |
| 249 | end - start + 1, 0); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 250 | debug ("nand_legacy_erase returned %x\n", rc); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 251 | #endif |
| 252 | } |
| 253 | |
| 254 | udelay(10000); |
| 255 | |
| 256 | /* strip the header - except for the kernel and ramdisk */ |
| 257 | if (au_image[i].type != AU_FIRMWARE) { |
| 258 | addr = (char *)hdr; |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 259 | off = image_get_header_size (); |
| 260 | nbytes = image_get_image_size (hdr); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 261 | } else { |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 262 | addr = (char *)((char *)hdr + image_get_header_size ()); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 263 | off = 0; |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 264 | nbytes = image_get_data_size (hdr); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* |
| 268 | * copy the data from RAM to FLASH |
| 269 | */ |
| 270 | if (au_image[i].type != AU_NAND) { |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 271 | debug ("flash_write(%p, %lx, %x)\n", |
| 272 | addr, start, nbytes); |
| 273 | rc = flash_write ((char *)addr, start, |
| 274 | (nbytes + 1) & ~1); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 275 | } else { |
Jean-Christophe PLAGNIOL-VILLARD | 719bb5f | 2008-08-13 01:40:43 +0200 | [diff] [blame] | 276 | #if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_LEGACY) |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 277 | debug ("nand_legacy_rw(%p, %lx, %x)\n", |
| 278 | addr, start, nbytes); |
| 279 | rc = nand_legacy_rw (nand_dev_desc, |
| 280 | NANDRW_WRITE | NANDRW_JFFS2, |
| 281 | start, nbytes, (size_t *)&total, |
| 282 | (uchar *)addr); |
| 283 | debug ("nand_legacy_rw: ret=%x total=%d nbytes=%d\n", |
| 284 | rc, total, nbytes); |
Matthias Fuchs | c0d6f11 | 2007-07-09 10:10:04 +0200 | [diff] [blame] | 285 | #else |
| 286 | rc = -1; |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 287 | #endif |
| 288 | } |
| 289 | if (rc != 0) { |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 290 | printf ("Flashing failed due to error %d\n", rc); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * check the dcrc of the copy |
| 296 | */ |
| 297 | if (au_image[i].type != AU_NAND) { |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 298 | rc = crc32 (0, (uchar *)(start + off), |
| 299 | image_get_data_size (hdr)); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 300 | } else { |
Jean-Christophe PLAGNIOL-VILLARD | 719bb5f | 2008-08-13 01:40:43 +0200 | [diff] [blame] | 301 | #if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_LEGACY) |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 302 | rc = nand_legacy_rw (nand_dev_desc, |
| 303 | NANDRW_READ | NANDRW_JFFS2 | |
| 304 | NANDRW_JFFS2_SKIP, |
| 305 | start, nbytes, (size_t *)&total, |
| 306 | (uchar *)addr); |
| 307 | rc = crc32 (0, (uchar *)(addr + off), |
| 308 | image_get_data_size (hdr)); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 309 | #endif |
| 310 | } |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 311 | if (rc != image_get_dcrc (hdr)) { |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 312 | printf ("Image %s Bad Data Checksum After COPY\n", |
| 313 | au_image[i].name); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | /* protect the address range */ |
| 318 | /* this assumes that ONLY the firmware is protected! */ |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 319 | if (((au_image[i].type & AU_FLAGMASK) == AU_PROTECT) || |
| 320 | (au_image[i].type == AU_FIRMWARE)) { |
| 321 | flash_sect_protect (1, start, end); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | break; |
| 325 | |
| 326 | default: |
| 327 | printf("Wrong image type selected!\n"); |
| 328 | } |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 333 | static void process_macros (const char *input, char *output) |
| 334 | { |
| 335 | char c, prev; |
| 336 | const char *varname_start = NULL; |
| 337 | int inputcnt = strlen (input); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 338 | int outputcnt = CONFIG_SYS_CBSIZE; |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 339 | int state = 0; /* 0 = waiting for '$' */ |
| 340 | /* 1 = waiting for '(' or '{' */ |
| 341 | /* 2 = waiting for ')' or '}' */ |
| 342 | /* 3 = waiting for ''' */ |
| 343 | #ifdef DEBUG_PARSER |
| 344 | char *output_start = output; |
| 345 | |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 346 | printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", |
| 347 | strlen(input), input); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 348 | #endif |
| 349 | |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 350 | prev = '\0'; /* previous character */ |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 351 | |
| 352 | while (inputcnt && outputcnt) { |
| 353 | c = *input++; |
| 354 | inputcnt--; |
| 355 | |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 356 | if (state != 3) { |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 357 | /* remove one level of escape characters */ |
| 358 | if ((c == '\\') && (prev != '\\')) { |
| 359 | if (inputcnt-- == 0) |
| 360 | break; |
| 361 | prev = c; |
| 362 | c = *input++; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | switch (state) { |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 367 | case 0: /* Waiting for (unescaped) $ */ |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 368 | if ((c == '\'') && (prev != '\\')) { |
| 369 | state = 3; |
| 370 | break; |
| 371 | } |
| 372 | if ((c == '$') && (prev != '\\')) { |
| 373 | state++; |
| 374 | } else { |
| 375 | *(output++) = c; |
| 376 | outputcnt--; |
| 377 | } |
| 378 | break; |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 379 | case 1: /* Waiting for ( */ |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 380 | if (c == '(' || c == '{') { |
| 381 | state++; |
| 382 | varname_start = input; |
| 383 | } else { |
| 384 | state = 0; |
| 385 | *(output++) = '$'; |
| 386 | outputcnt--; |
| 387 | |
| 388 | if (outputcnt) { |
| 389 | *(output++) = c; |
| 390 | outputcnt--; |
| 391 | } |
| 392 | } |
| 393 | break; |
| 394 | case 2: /* Waiting for ) */ |
| 395 | if (c == ')' || c == '}') { |
| 396 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 397 | char envname[CONFIG_SYS_CBSIZE], *envval; |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 398 | /* Varname # of chars */ |
| 399 | int envcnt = input - varname_start - 1; |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 400 | |
| 401 | /* Get the varname */ |
| 402 | for (i = 0; i < envcnt; i++) { |
| 403 | envname[i] = varname_start[i]; |
| 404 | } |
| 405 | envname[i] = 0; |
| 406 | |
| 407 | /* Get its value */ |
| 408 | envval = getenv (envname); |
| 409 | |
| 410 | /* Copy into the line if it exists */ |
| 411 | if (envval != NULL) |
| 412 | while ((*envval) && outputcnt) { |
| 413 | *(output++) = *(envval++); |
| 414 | outputcnt--; |
| 415 | } |
| 416 | /* Look for another '$' */ |
| 417 | state = 0; |
| 418 | } |
| 419 | break; |
| 420 | case 3: /* Waiting for ' */ |
| 421 | if ((c == '\'') && (prev != '\\')) { |
| 422 | state = 0; |
| 423 | } else { |
| 424 | *(output++) = c; |
| 425 | outputcnt--; |
| 426 | } |
| 427 | break; |
| 428 | } |
| 429 | prev = c; |
| 430 | } |
| 431 | |
| 432 | if (outputcnt) |
| 433 | *output = 0; |
| 434 | |
| 435 | #ifdef DEBUG_PARSER |
| 436 | printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n", |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 437 | strlen (output_start), output_start); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 438 | #endif |
| 439 | } |
| 440 | |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 441 | /* |
| 442 | * this is called from board_init() after the hardware has been set up |
| 443 | * and is usable. That seems like a good time to do this. |
| 444 | * Right now the return value is ignored. |
| 445 | */ |
| 446 | int do_auto_update(void) |
| 447 | { |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 448 | block_dev_desc_t *stor_dev = NULL; |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 449 | long sz; |
| 450 | int i, res, cnt, old_ctrlc, got_ctrlc; |
| 451 | char buffer[32]; |
| 452 | char str[80]; |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 453 | int n; |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 454 | |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 455 | if (ide_dev_desc[0].type != DEV_TYPE_UNKNOWN) { |
| 456 | stor_dev = get_dev ("ide", 0); |
| 457 | if (stor_dev == NULL) { |
| 458 | debug ("ide: unknown device\n"); |
| 459 | return -1; |
| 460 | } |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 463 | if (fat_register_device (stor_dev, 1) != 0) { |
| 464 | debug ("Unable to register ide disk 0:1\n"); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 465 | return -1; |
| 466 | } |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 467 | |
| 468 | /* |
| 469 | * Check if magic file is present |
| 470 | */ |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 471 | if ((n = do_fat_read (AU_MAGIC_FILE, buffer, |
| 472 | sizeof(buffer), LS_NO)) <= 0) { |
| 473 | debug ("No auto_update magic file (n=%d)\n", n); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | #ifdef CONFIG_AUTO_UPDATE_SHOW |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 478 | board_auto_update_show (1); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 479 | #endif |
| 480 | puts("\nAutoUpdate Disk detected! Trying to update system...\n"); |
| 481 | |
| 482 | /* make sure that we see CTRL-C and save the old state */ |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 483 | old_ctrlc = disable_ctrlc (0); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 484 | |
| 485 | /* just loop thru all the possible files */ |
| 486 | for (i = 0; i < N_AU_IMAGES; i++) { |
| 487 | /* |
| 488 | * Try to expand the environment var in the fname |
| 489 | */ |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 490 | process_macros (au_image[i].name, str); |
| 491 | strcpy (au_image[i].name, str); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 492 | |
| 493 | printf("Reading %s ...", au_image[i].name); |
| 494 | /* just read the header */ |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 495 | sz = do_fat_read (au_image[i].name, LOAD_ADDR, |
| 496 | image_get_header_size (), LS_NO); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 497 | debug ("read %s sz %ld hdr %d\n", |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 498 | au_image[i].name, sz, image_get_header_size ()); |
| 499 | if (sz <= 0 || sz < image_get_header_size ()) { |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 500 | puts(" not found\n"); |
| 501 | continue; |
| 502 | } |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 503 | if (au_check_header_valid (i, sz) < 0) { |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 504 | puts(" header not valid\n"); |
| 505 | continue; |
| 506 | } |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 507 | sz = do_fat_read (au_image[i].name, LOAD_ADDR, |
| 508 | MAX_LOADSZ, LS_NO); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 509 | debug ("read %s sz %ld hdr %d\n", |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 510 | au_image[i].name, sz, image_get_header_size ()); |
| 511 | if (sz <= 0 || sz <= image_get_header_size ()) { |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 512 | puts(" not found\n"); |
| 513 | continue; |
| 514 | } |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 515 | if (au_check_cksum_valid (i, sz) < 0) { |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 516 | puts(" checksum not valid\n"); |
| 517 | continue; |
| 518 | } |
| 519 | puts(" done\n"); |
| 520 | |
| 521 | do { |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 522 | res = au_do_update (i, sz); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 523 | /* let the user break out of the loop */ |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 524 | if (ctrlc() || had_ctrlc ()) { |
| 525 | clear_ctrlc (); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 526 | if (res < 0) |
| 527 | got_ctrlc = 1; |
| 528 | break; |
| 529 | } |
| 530 | cnt++; |
| 531 | } while (res < 0); |
| 532 | } |
| 533 | |
| 534 | /* restore the old state */ |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 535 | disable_ctrlc (old_ctrlc); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 536 | |
| 537 | puts("AutoUpdate finished\n\n"); |
| 538 | #ifdef CONFIG_AUTO_UPDATE_SHOW |
Matthias Fuchs | 17dc761 | 2008-04-21 14:42:06 +0200 | [diff] [blame] | 539 | board_auto_update_show (0); |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 540 | #endif |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 545 | int auto_update(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 546 | { |
| 547 | do_auto_update(); |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | U_BOOT_CMD( |
| 552 | autoupd, 1, 1, auto_update, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 553 | "Automatically update images", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 554 | "" |
stroese | 56b9e4f | 2004-12-16 18:43:13 +0000 | [diff] [blame] | 555 | ); |
| 556 | #endif /* CONFIG_AUTO_UPDATE */ |