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