blob: 33aeb46d2647f3da33ff424bd52af8e87516ee76 [file] [log] [blame]
stroese56b9e4f2004-12-16 18:43:13 +00001/*
2 * (C) Copyright 2003-2004
Detlev Zundelf1b3f2b2009-05-13 10:54:10 +02003 * Gary Jennejohn, DENX Software Engineering, garyj@denx.de.
stroese56b9e4f2004-12-16 18:43:13 +00004 * 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 Sieka582f1a32006-03-05 18:57:33 +010026
stroese56b9e4f2004-12-16 18:43:13 +000027#include <command.h>
28#include <image.h>
29#include <asm/byteorder.h>
Jean-Christophe PLAGNIOL-VILLARD719bb5f2008-08-13 01:40:43 +020030#if defined(CONFIG_NAND_LEGACY)
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010031#include <linux/mtd/nand_legacy.h>
Matthias Fuchsc0d6f112007-07-09 10:10:04 +020032#endif
stroese56b9e4f2004-12-16 18:43:13 +000033#include <fat.h>
Grant Likelyffc2dd72007-02-20 09:04:34 +010034#include <part.h>
stroese56b9e4f2004-12-16 18:43:13 +000035
36#include "auto_update.h"
37
38#ifdef CONFIG_AUTO_UPDATE
39
Jon Loeliger4ed9ed62007-07-09 18:24:55 -050040#if !defined(CONFIG_CMD_FAT)
Jon Loeliger13f75992007-07-10 10:39:10 -050041#error "must define CONFIG_CMD_FAT"
stroese56b9e4f2004-12-16 18:43:13 +000042#endif
43
44extern au_image_t au_image[];
45extern int N_AU_IMAGES;
46
Matthias Fuchs17dc7612008-04-21 14:42:06 +020047/* where to load files into memory */
48#define LOAD_ADDR ((unsigned char *)0x100000)
49#define MAX_LOADSZ 0x1c00000
stroese56b9e4f2004-12-16 18:43:13 +000050
51/* externals */
52extern int fat_register_device(block_dev_desc_t *, int);
53extern int file_fat_detectfs(void);
54extern long file_fat_read(const char *, void *, unsigned long);
Matthias Fuchs17dc7612008-04-21 14:42:06 +020055long do_fat_read (const char *filename, void *buffer,
56 unsigned long maxsize, int dols);
stroese56b9e4f2004-12-16 18:43:13 +000057extern int flash_sect_erase(ulong, ulong);
58extern int flash_sect_protect (int, ulong, ulong);
Stefan Roesebcb3b272005-10-20 16:39:16 +020059extern int flash_write (char *, ulong, ulong);
stroese56b9e4f2004-12-16 18:43:13 +000060
Jean-Christophe PLAGNIOL-VILLARD719bb5f2008-08-13 01:40:43 +020061#if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_LEGACY)
stroese56b9e4f2004-12-16 18:43:13 +000062/* 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
67extern struct nand_chip nand_dev_desc[];
Matthias Fuchs17dc7612008-04-21 14:42:06 +020068extern int nand_legacy_rw(struct nand_chip* nand, int cmd,
69 size_t start, size_t len,
70 size_t * retlen, u_char * buf);
71extern int nand_legacy_erase(struct nand_chip* nand, size_t ofs,
72 size_t len, int clean);
Jon Loeliger4ed9ed62007-07-09 18:24:55 -050073#endif
stroese56b9e4f2004-12-16 18:43:13 +000074
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020075extern block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
stroese56b9e4f2004-12-16 18:43:13 +000076
stroese56b9e4f2004-12-16 18:43:13 +000077int au_check_cksum_valid(int i, long nbytes)
78{
79 image_header_t *hdr;
stroese56b9e4f2004-12-16 18:43:13 +000080
81 hdr = (image_header_t *)LOAD_ADDR;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010082#if defined(CONFIG_FIT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +010083 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010084 puts ("Non legacy image format not supported\n");
85 return -1;
86 }
87#endif
stroese56b9e4f2004-12-16 18:43:13 +000088
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010089 if ((au_image[i].type == AU_FIRMWARE) &&
90 (au_image[i].size != image_get_data_size (hdr))) {
stroese56b9e4f2004-12-16 18:43:13 +000091 printf ("Image %s has wrong size\n", au_image[i].name);
92 return -1;
93 }
94
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010095 if (nbytes != (image_get_image_size (hdr))) {
stroese56b9e4f2004-12-16 18:43:13 +000096 printf ("Image %s bad total SIZE\n", au_image[i].name);
97 return -1;
98 }
stroese56b9e4f2004-12-16 18:43:13 +000099
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100100 /* check the data CRC */
101 if (!image_check_dcrc (hdr)) {
stroese56b9e4f2004-12-16 18:43:13 +0000102 printf ("Image %s bad data checksum\n", au_image[i].name);
103 return -1;
104 }
105 return 0;
106}
107
stroese56b9e4f2004-12-16 18:43:13 +0000108int 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 Balakowiczdbdd16a2008-02-04 08:28:09 +0100114#if defined(CONFIG_FIT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100115 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100116 puts ("Non legacy image format not supported\n");
117 return -1;
118 }
119#endif
120
stroese56b9e4f2004-12-16 18:43:13 +0000121 /* check the easy ones first */
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200122 if (nbytes < image_get_header_size ()) {
stroese56b9e4f2004-12-16 18:43:13 +0000123 printf ("Image %s bad header SIZE\n", au_image[i].name);
124 return -1;
125 }
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200126 if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) {
stroese56b9e4f2004-12-16 18:43:13 +0000127 printf ("Image %s bad MAGIC or ARCH\n", au_image[i].name);
128 return -1;
129 }
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100130 if (!image_check_hcrc (hdr)) {
stroese56b9e4f2004-12-16 18:43:13 +0000131 printf ("Image %s bad header checksum\n", au_image[i].name);
132 return -1;
133 }
stroese56b9e4f2004-12-16 18:43:13 +0000134
135 /* check the type - could do this all in one gigantic if() */
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200136 if (((au_image[i].type & AU_TYPEMASK) == AU_FIRMWARE) &&
137 !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
stroese56b9e4f2004-12-16 18:43:13 +0000138 printf ("Image %s wrong type\n", au_image[i].name);
139 return -1;
140 }
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200141 if (((au_image[i].type & AU_TYPEMASK) == AU_SCRIPT) &&
142 !image_check_type (hdr, IH_TYPE_SCRIPT)) {
stroese56b9e4f2004-12-16 18:43:13 +0000143 printf ("Image %s wrong type\n", au_image[i].name);
144 return -1;
145 }
146
147 /* recycle checksum */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100148 checksum = image_get_data_size (hdr);
stroese56b9e4f2004-12-16 18:43:13 +0000149
stroese56b9e4f2004-12-16 18:43:13 +0000150 return 0;
151}
152
stroese56b9e4f2004-12-16 18:43:13 +0000153int 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-VILLARD719bb5f2008-08-13 01:40:43 +0200161#if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_LEGACY)
stroese56b9e4f2004-12-16 18:43:13 +0000162 int total;
163#endif
164
165 hdr = (image_header_t *)LOAD_ADDR;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100166#if defined(CONFIG_FIT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100167 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100168 puts ("Non legacy image format not supported\n");
169 return -1;
170 }
171#endif
stroese56b9e4f2004-12-16 18:43:13 +0000172
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200173 switch (au_image[i].type & AU_TYPEMASK) {
stroese56b9e4f2004-12-16 18:43:13 +0000174 case AU_SCRIPT:
175 printf("Executing script %s\n", au_image[i].name);
176
177 /* execute a script */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100178 if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
179 addr = (char *)((char *)hdr + image_get_header_size ());
stroese56b9e4f2004-12-16 18:43:13 +0000180 /* stick a NULL at the end of the script, otherwise */
181 /* parse_string_outer() runs off the end. */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100182 addr[image_get_data_size (hdr)] = 0;
stroese56b9e4f2004-12-16 18:43:13 +0000183 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
stroesee72a7b72005-03-16 20:58:31 +0000208 /*
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 Fuchs17dc7612008-04-21 14:42:06 +0200213 char *new = (char *)((char *)hdr +
214 image_get_header_size ());
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100215 nbytes = image_get_data_size (hdr);
stroesee72a7b72005-03-16 20:58:31 +0000216
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200217 while (--nbytes) {
stroesee72a7b72005-03-16 20:58:31 +0000218 if (*orig++ != *new++) {
219 break;
220 }
221 }
222 if (!nbytes) {
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200223 printf ("Skipping firmware update - "
224 "images are identical\n");
stroesee72a7b72005-03-16 20:58:31 +0000225 break;
226 }
227 }
228
stroese56b9e4f2004-12-16 18:43:13 +0000229 /* unprotect the address range */
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200230 if (((au_image[i].type & AU_FLAGMASK) == AU_PROTECT) ||
231 (au_image[i].type == AU_FIRMWARE)) {
232 flash_sect_protect (0, start, end);
stroese56b9e4f2004-12-16 18:43:13 +0000233 }
234
235 /*
236 * erase the address range.
237 */
238 if (au_image[i].type != AU_NAND) {
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200239 printf ("Updating NOR FLASH with image %s\n",
240 au_image[i].name);
stroese56b9e4f2004-12-16 18:43:13 +0000241 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200242 flash_sect_erase (start, end);
stroese56b9e4f2004-12-16 18:43:13 +0000243 } else {
Jean-Christophe PLAGNIOL-VILLARD719bb5f2008-08-13 01:40:43 +0200244#if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_LEGACY)
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200245 printf ("Updating NAND FLASH with image %s\n",
246 au_image[i].name);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100247 debug ("nand_legacy_erase(%lx, %lx);\n", start, end);
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200248 rc = nand_legacy_erase (nand_dev_desc, start,
249 end - start + 1, 0);
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100250 debug ("nand_legacy_erase returned %x\n", rc);
stroese56b9e4f2004-12-16 18:43:13 +0000251#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 Balakowicz41d71ed2008-01-08 18:14:09 +0100259 off = image_get_header_size ();
260 nbytes = image_get_image_size (hdr);
stroese56b9e4f2004-12-16 18:43:13 +0000261 } else {
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100262 addr = (char *)((char *)hdr + image_get_header_size ());
stroese56b9e4f2004-12-16 18:43:13 +0000263 off = 0;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100264 nbytes = image_get_data_size (hdr);
stroese56b9e4f2004-12-16 18:43:13 +0000265 }
266
267 /*
268 * copy the data from RAM to FLASH
269 */
270 if (au_image[i].type != AU_NAND) {
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200271 debug ("flash_write(%p, %lx, %x)\n",
272 addr, start, nbytes);
273 rc = flash_write ((char *)addr, start,
274 (nbytes + 1) & ~1);
stroese56b9e4f2004-12-16 18:43:13 +0000275 } else {
Jean-Christophe PLAGNIOL-VILLARD719bb5f2008-08-13 01:40:43 +0200276#if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_LEGACY)
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200277 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 Fuchsc0d6f112007-07-09 10:10:04 +0200285#else
286 rc = -1;
stroese56b9e4f2004-12-16 18:43:13 +0000287#endif
288 }
289 if (rc != 0) {
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200290 printf ("Flashing failed due to error %d\n", rc);
stroese56b9e4f2004-12-16 18:43:13 +0000291 return -1;
292 }
293
294 /*
295 * check the dcrc of the copy
296 */
297 if (au_image[i].type != AU_NAND) {
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200298 rc = crc32 (0, (uchar *)(start + off),
299 image_get_data_size (hdr));
stroese56b9e4f2004-12-16 18:43:13 +0000300 } else {
Jean-Christophe PLAGNIOL-VILLARD719bb5f2008-08-13 01:40:43 +0200301#if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_LEGACY)
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200302 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));
stroese56b9e4f2004-12-16 18:43:13 +0000309#endif
310 }
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100311 if (rc != image_get_dcrc (hdr)) {
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200312 printf ("Image %s Bad Data Checksum After COPY\n",
313 au_image[i].name);
stroese56b9e4f2004-12-16 18:43:13 +0000314 return -1;
315 }
316
317 /* protect the address range */
318 /* this assumes that ONLY the firmware is protected! */
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200319 if (((au_image[i].type & AU_FLAGMASK) == AU_PROTECT) ||
320 (au_image[i].type == AU_FIRMWARE)) {
321 flash_sect_protect (1, start, end);
stroese56b9e4f2004-12-16 18:43:13 +0000322 }
323
324 break;
325
326 default:
327 printf("Wrong image type selected!\n");
328 }
329
330 return 0;
331}
332
stroese56b9e4f2004-12-16 18:43:13 +0000333static 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-VILLARD03836942008-10-16 15:01:15 +0200338 int outputcnt = CONFIG_SYS_CBSIZE;
stroese56b9e4f2004-12-16 18:43:13 +0000339 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 Fuchs17dc7612008-04-21 14:42:06 +0200346 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n",
347 strlen(input), input);
stroese56b9e4f2004-12-16 18:43:13 +0000348#endif
349
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200350 prev = '\0'; /* previous character */
stroese56b9e4f2004-12-16 18:43:13 +0000351
352 while (inputcnt && outputcnt) {
353 c = *input++;
354 inputcnt--;
355
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200356 if (state != 3) {
stroese56b9e4f2004-12-16 18:43:13 +0000357 /* 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 Fuchs17dc7612008-04-21 14:42:06 +0200367 case 0: /* Waiting for (unescaped) $ */
stroese56b9e4f2004-12-16 18:43:13 +0000368 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 Fuchs17dc7612008-04-21 14:42:06 +0200379 case 1: /* Waiting for ( */
stroese56b9e4f2004-12-16 18:43:13 +0000380 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-VILLARD03836942008-10-16 15:01:15 +0200397 char envname[CONFIG_SYS_CBSIZE], *envval;
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200398 /* Varname # of chars */
399 int envcnt = input - varname_start - 1;
stroese56b9e4f2004-12-16 18:43:13 +0000400
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 Fuchs17dc7612008-04-21 14:42:06 +0200437 strlen (output_start), output_start);
stroese56b9e4f2004-12-16 18:43:13 +0000438#endif
439}
440
stroese56b9e4f2004-12-16 18:43:13 +0000441/*
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 */
446int do_auto_update(void)
447{
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200448 block_dev_desc_t *stor_dev = NULL;
stroese56b9e4f2004-12-16 18:43:13 +0000449 long sz;
450 int i, res, cnt, old_ctrlc, got_ctrlc;
451 char buffer[32];
452 char str[80];
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200453 int n;
stroese56b9e4f2004-12-16 18:43:13 +0000454
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200455 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 }
stroese56b9e4f2004-12-16 18:43:13 +0000461 }
462
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200463 if (fat_register_device (stor_dev, 1) != 0) {
464 debug ("Unable to register ide disk 0:1\n");
stroese56b9e4f2004-12-16 18:43:13 +0000465 return -1;
466 }
stroese56b9e4f2004-12-16 18:43:13 +0000467
468 /*
469 * Check if magic file is present
470 */
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200471 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);
stroese56b9e4f2004-12-16 18:43:13 +0000474 return -1;
475 }
476
477#ifdef CONFIG_AUTO_UPDATE_SHOW
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200478 board_auto_update_show (1);
stroese56b9e4f2004-12-16 18:43:13 +0000479#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 Fuchs17dc7612008-04-21 14:42:06 +0200483 old_ctrlc = disable_ctrlc (0);
stroese56b9e4f2004-12-16 18:43:13 +0000484
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 Fuchs17dc7612008-04-21 14:42:06 +0200490 process_macros (au_image[i].name, str);
491 strcpy (au_image[i].name, str);
stroese56b9e4f2004-12-16 18:43:13 +0000492
493 printf("Reading %s ...", au_image[i].name);
494 /* just read the header */
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200495 sz = do_fat_read (au_image[i].name, LOAD_ADDR,
496 image_get_header_size (), LS_NO);
stroese56b9e4f2004-12-16 18:43:13 +0000497 debug ("read %s sz %ld hdr %d\n",
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100498 au_image[i].name, sz, image_get_header_size ());
499 if (sz <= 0 || sz < image_get_header_size ()) {
stroese56b9e4f2004-12-16 18:43:13 +0000500 puts(" not found\n");
501 continue;
502 }
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200503 if (au_check_header_valid (i, sz) < 0) {
stroese56b9e4f2004-12-16 18:43:13 +0000504 puts(" header not valid\n");
505 continue;
506 }
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200507 sz = do_fat_read (au_image[i].name, LOAD_ADDR,
508 MAX_LOADSZ, LS_NO);
stroese56b9e4f2004-12-16 18:43:13 +0000509 debug ("read %s sz %ld hdr %d\n",
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100510 au_image[i].name, sz, image_get_header_size ());
511 if (sz <= 0 || sz <= image_get_header_size ()) {
stroese56b9e4f2004-12-16 18:43:13 +0000512 puts(" not found\n");
513 continue;
514 }
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200515 if (au_check_cksum_valid (i, sz) < 0) {
stroese56b9e4f2004-12-16 18:43:13 +0000516 puts(" checksum not valid\n");
517 continue;
518 }
519 puts(" done\n");
520
521 do {
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200522 res = au_do_update (i, sz);
stroese56b9e4f2004-12-16 18:43:13 +0000523 /* let the user break out of the loop */
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200524 if (ctrlc() || had_ctrlc ()) {
525 clear_ctrlc ();
stroese56b9e4f2004-12-16 18:43:13 +0000526 if (res < 0)
527 got_ctrlc = 1;
528 break;
529 }
530 cnt++;
531 } while (res < 0);
532 }
533
534 /* restore the old state */
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200535 disable_ctrlc (old_ctrlc);
stroese56b9e4f2004-12-16 18:43:13 +0000536
537 puts("AutoUpdate finished\n\n");
538#ifdef CONFIG_AUTO_UPDATE_SHOW
Matthias Fuchs17dc7612008-04-21 14:42:06 +0200539 board_auto_update_show (0);
stroese56b9e4f2004-12-16 18:43:13 +0000540#endif
541
542 return 0;
543}
544
stroese56b9e4f2004-12-16 18:43:13 +0000545int auto_update(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
546{
547 do_auto_update();
548
549 return 0;
550}
551U_BOOT_CMD(
552 autoupd, 1, 1, auto_update,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600553 "Automatically update images",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200554 ""
stroese56b9e4f2004-12-16 18:43:13 +0000555);
556#endif /* CONFIG_AUTO_UPDATE */