blob: cb8087bee831bc6dd7ddd2f49470df8df981e16e [file] [log] [blame]
stroese0621f6f2004-12-16 18:43:13 +00001/*
2 * (C) Copyright 2003-2004
3 * Gary Jennejohn, DENX Software Engineering, gj@denx.de.
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 Siekaaddb2e12006-03-05 18:57:33 +010026
stroese0621f6f2004-12-16 18:43:13 +000027#include <command.h>
28#include <image.h>
29#include <asm/byteorder.h>
Matthias Fuchse09f7ab2007-07-09 10:10:04 +020030#if defined(CFG_NAND_LEGACY)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010031#include <linux/mtd/nand_legacy.h>
Matthias Fuchse09f7ab2007-07-09 10:10:04 +020032#endif
stroese0621f6f2004-12-16 18:43:13 +000033#include <fat.h>
Grant Likely735dd972007-02-20 09:04:34 +010034#include <part.h>
stroese0621f6f2004-12-16 18:43:13 +000035
36#include "auto_update.h"
37
38#ifdef CONFIG_AUTO_UPDATE
39
Jon Loeligerb9307262007-07-09 18:24:55 -050040#if !defined(CONFIG_CMD_FAT)
Jon Loeliger77a31852007-07-10 10:39:10 -050041#error "must define CONFIG_CMD_FAT"
stroese0621f6f2004-12-16 18:43:13 +000042#endif
43
44extern au_image_t au_image[];
45extern int N_AU_IMAGES;
46
47#define AU_DEBUG
48#undef AU_DEBUG
49
50#undef debug
51#ifdef AU_DEBUG
52#define debug(fmt,args...) printf (fmt ,##args)
53#else
54#define debug(fmt,args...)
55#endif /* AU_DEBUG */
56
57
58#define LOAD_ADDR ((unsigned char *)0x100000) /* where to load files into memory */
59#define MAX_LOADSZ 0x1e00000
60
61/* externals */
62extern int fat_register_device(block_dev_desc_t *, int);
63extern int file_fat_detectfs(void);
64extern long file_fat_read(const char *, void *, unsigned long);
65long do_fat_read (const char *filename, void *buffer, unsigned long maxsize, int dols);
66#ifdef CONFIG_VFD
67extern int trab_vfd (ulong);
68extern int transfer_pic(unsigned char, unsigned char *, int, int);
69#endif
70extern int flash_sect_erase(ulong, ulong);
71extern int flash_sect_protect (int, ulong, ulong);
Stefan Roesea5477752005-10-20 16:39:16 +020072extern int flash_write (char *, ulong, ulong);
stroese0621f6f2004-12-16 18:43:13 +000073
Jon Loeligerb9307262007-07-09 18:24:55 -050074#if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
stroese0621f6f2004-12-16 18:43:13 +000075/* references to names in cmd_nand.c */
76#define NANDRW_READ 0x01
77#define NANDRW_WRITE 0x00
78#define NANDRW_JFFS2 0x02
79#define NANDRW_JFFS2_SKIP 0x04
80extern struct nand_chip nand_dev_desc[];
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010081extern int nand_legacy_rw(struct nand_chip* nand, int cmd, size_t start, size_t len,
stroese0621f6f2004-12-16 18:43:13 +000082 size_t * retlen, u_char * buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010083extern int nand_legacy_erase(struct nand_chip* nand, size_t ofs, size_t len, int clean);
Jon Loeligerb9307262007-07-09 18:24:55 -050084#endif
stroese0621f6f2004-12-16 18:43:13 +000085
86extern block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
87
88
89int au_check_cksum_valid(int i, long nbytes)
90{
91 image_header_t *hdr;
stroese0621f6f2004-12-16 18:43:13 +000092
93 hdr = (image_header_t *)LOAD_ADDR;
94
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010095 if ((au_image[i].type == AU_FIRMWARE) &&
96 (au_image[i].size != image_get_data_size (hdr))) {
stroese0621f6f2004-12-16 18:43:13 +000097 printf ("Image %s has wrong size\n", au_image[i].name);
98 return -1;
99 }
100
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100101 if (nbytes != (image_get_image_size (hdr))) {
stroese0621f6f2004-12-16 18:43:13 +0000102 printf ("Image %s bad total SIZE\n", au_image[i].name);
103 return -1;
104 }
stroese0621f6f2004-12-16 18:43:13 +0000105
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100106 /* check the data CRC */
107 if (!image_check_dcrc (hdr)) {
stroese0621f6f2004-12-16 18:43:13 +0000108 printf ("Image %s bad data checksum\n", au_image[i].name);
109 return -1;
110 }
111 return 0;
112}
113
114
115int au_check_header_valid(int i, long nbytes)
116{
117 image_header_t *hdr;
118 unsigned long checksum;
119
120 hdr = (image_header_t *)LOAD_ADDR;
121 /* check the easy ones first */
122#undef CHECK_VALID_DEBUG
123#ifdef CHECK_VALID_DEBUG
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100124 printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC);
125 printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_PPC);
126 printf("size %#x %#lx ", image_get_data_size (hdr), nbytes);
127 printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL);
stroese0621f6f2004-12-16 18:43:13 +0000128#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100129 if (nbytes < image_get_header_size ())
stroese0621f6f2004-12-16 18:43:13 +0000130 {
131 printf ("Image %s bad header SIZE\n", au_image[i].name);
132 return -1;
133 }
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100134 if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC))
stroese0621f6f2004-12-16 18:43:13 +0000135 {
136 printf ("Image %s bad MAGIC or ARCH\n", au_image[i].name);
137 return -1;
138 }
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100139 if (!image_check_hcrc (hdr)) {
stroese0621f6f2004-12-16 18:43:13 +0000140 printf ("Image %s bad header checksum\n", au_image[i].name);
141 return -1;
142 }
stroese0621f6f2004-12-16 18:43:13 +0000143
144 /* check the type - could do this all in one gigantic if() */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100145 if ((au_image[i].type == AU_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
stroese0621f6f2004-12-16 18:43:13 +0000146 printf ("Image %s wrong type\n", au_image[i].name);
147 return -1;
148 }
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100149 if ((au_image[i].type == AU_SCRIPT) && !image_check_type (hdr, IH_TYPE_SCRIPT)) {
stroese0621f6f2004-12-16 18:43:13 +0000150 printf ("Image %s wrong type\n", au_image[i].name);
151 return -1;
152 }
153
154 /* recycle checksum */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100155 checksum = image_get_data_size (hdr);
stroese0621f6f2004-12-16 18:43:13 +0000156
157#if 0 /* test-only */
158 /* for kernel and app the image header must also fit into flash */
159 if (idx != IDX_DISK)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100160 checksum += image_get_header_size ();
stroese0621f6f2004-12-16 18:43:13 +0000161 /* check the size does not exceed space in flash. HUSH scripts */
162 /* all have ausize[] set to 0 */
163 if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
164 printf ("Image %s is bigger than FLASH\n", au_image[i].name);
165 return -1;
166 }
167#endif
168
169 return 0;
170}
171
172
173int au_do_update(int i, long sz)
174{
175 image_header_t *hdr;
176 char *addr;
177 long start, end;
178 int off, rc;
179 uint nbytes;
180 int k;
Jon Loeligerb9307262007-07-09 18:24:55 -0500181#if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
stroese0621f6f2004-12-16 18:43:13 +0000182 int total;
183#endif
184
185 hdr = (image_header_t *)LOAD_ADDR;
186
187 switch (au_image[i].type) {
188 case AU_SCRIPT:
189 printf("Executing script %s\n", au_image[i].name);
190
191 /* execute a script */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100192 if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
193 addr = (char *)((char *)hdr + image_get_header_size ());
stroese0621f6f2004-12-16 18:43:13 +0000194 /* stick a NULL at the end of the script, otherwise */
195 /* parse_string_outer() runs off the end. */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100196 addr[image_get_data_size (hdr)] = 0;
stroese0621f6f2004-12-16 18:43:13 +0000197 addr += 8;
198
199 /*
200 * Replace cr/lf with ;
201 */
202 k = 0;
203 while (addr[k] != 0) {
204 if ((addr[k] == 10) || (addr[k] == 13)) {
205 addr[k] = ';';
206 }
207 k++;
208 }
209
210 run_command(addr, 0);
211 return 0;
212 }
213
214 break;
215
216 case AU_FIRMWARE:
217 case AU_NOR:
218 case AU_NAND:
219 start = au_image[i].start;
220 end = au_image[i].start + au_image[i].size - 1;
221
stroeseacdcd102005-03-16 20:58:31 +0000222 /*
223 * do not update firmware when image is already in flash.
224 */
225 if (au_image[i].type == AU_FIRMWARE) {
226 char *orig = (char*)start;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100227 char *new = (char *)((char *)hdr + image_get_header_size ());
228 nbytes = image_get_data_size (hdr);
stroeseacdcd102005-03-16 20:58:31 +0000229
230 while(--nbytes) {
231 if (*orig++ != *new++) {
232 break;
233 }
234 }
235 if (!nbytes) {
236 printf("Skipping firmware update - images are identical\n");
237 break;
238 }
239 }
240
stroese0621f6f2004-12-16 18:43:13 +0000241 /* unprotect the address range */
242 /* this assumes that ONLY the firmware is protected! */
243 if (au_image[i].type == AU_FIRMWARE) {
244 flash_sect_protect(0, start, end);
245 }
246
247 /*
248 * erase the address range.
249 */
250 if (au_image[i].type != AU_NAND) {
251 printf("Updating NOR FLASH with image %s\n", au_image[i].name);
252 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
253 flash_sect_erase(start, end);
254 } else {
Jon Loeligerb9307262007-07-09 18:24:55 -0500255#if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
stroese0621f6f2004-12-16 18:43:13 +0000256 printf("Updating NAND FLASH with image %s\n", au_image[i].name);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100257 debug ("nand_legacy_erase(%lx, %lx);\n", start, end);
258 rc = nand_legacy_erase (nand_dev_desc, start, end - start + 1, 0);
259 debug ("nand_legacy_erase returned %x\n", rc);
stroese0621f6f2004-12-16 18:43:13 +0000260#endif
261 }
262
263 udelay(10000);
264
265 /* strip the header - except for the kernel and ramdisk */
266 if (au_image[i].type != AU_FIRMWARE) {
267 addr = (char *)hdr;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100268 off = image_get_header_size ();
269 nbytes = image_get_image_size (hdr);
stroese0621f6f2004-12-16 18:43:13 +0000270 } else {
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100271 addr = (char *)((char *)hdr + image_get_header_size ());
stroese0621f6f2004-12-16 18:43:13 +0000272 off = 0;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100273 nbytes = image_get_data_size (hdr);
stroese0621f6f2004-12-16 18:43:13 +0000274 }
275
276 /*
277 * copy the data from RAM to FLASH
278 */
279 if (au_image[i].type != AU_NAND) {
280 debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
Stefan Roese18c5e642006-01-18 20:06:44 +0100281 rc = flash_write((char *)addr, start, nbytes);
stroese0621f6f2004-12-16 18:43:13 +0000282 } else {
Jon Loeligerb9307262007-07-09 18:24:55 -0500283#if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100284 debug ("nand_legacy_rw(%p, %lx %x)\n", addr, start, nbytes);
285 rc = nand_legacy_rw(nand_dev_desc, NANDRW_WRITE | NANDRW_JFFS2,
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200286 start, nbytes, (size_t *)&total, (uchar *)addr);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100287 debug ("nand_legacy_rw: ret=%x total=%d nbytes=%d\n", rc, total, nbytes);
Matthias Fuchse09f7ab2007-07-09 10:10:04 +0200288#else
289 rc = -1;
stroese0621f6f2004-12-16 18:43:13 +0000290#endif
291 }
292 if (rc != 0) {
293 printf("Flashing failed due to error %d\n", rc);
294 return -1;
295 }
296
297 /*
298 * check the dcrc of the copy
299 */
300 if (au_image[i].type != AU_NAND) {
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100301 rc = crc32 (0, (uchar *)(start + off), image_get_data_size (hdr));
stroese0621f6f2004-12-16 18:43:13 +0000302 } else {
Jon Loeligerb9307262007-07-09 18:24:55 -0500303#if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100304 rc = nand_legacy_rw(nand_dev_desc, NANDRW_READ | NANDRW_JFFS2 | NANDRW_JFFS2_SKIP,
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200305 start, nbytes, (size_t *)&total, (uchar *)addr);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100306 rc = crc32 (0, (uchar *)(addr + off), image_get_data_size (hdr));
stroese0621f6f2004-12-16 18:43:13 +0000307#endif
308 }
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100309 if (rc != image_get_dcrc (hdr)) {
stroese0621f6f2004-12-16 18:43:13 +0000310 printf ("Image %s Bad Data Checksum After COPY\n", au_image[i].name);
311 return -1;
312 }
313
314 /* protect the address range */
315 /* this assumes that ONLY the firmware is protected! */
316 if (au_image[i].type == AU_FIRMWARE) {
317 flash_sect_protect(1, start, end);
318 }
319
320 break;
321
322 default:
323 printf("Wrong image type selected!\n");
324 }
325
326 return 0;
327}
328
329
330static void process_macros (const char *input, char *output)
331{
332 char c, prev;
333 const char *varname_start = NULL;
334 int inputcnt = strlen (input);
335 int outputcnt = CFG_CBSIZE;
336 int state = 0; /* 0 = waiting for '$' */
337 /* 1 = waiting for '(' or '{' */
338 /* 2 = waiting for ')' or '}' */
339 /* 3 = waiting for ''' */
340#ifdef DEBUG_PARSER
341 char *output_start = output;
342
343 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
344#endif
345
346 prev = '\0'; /* previous character */
347
348 while (inputcnt && outputcnt) {
349 c = *input++;
350 inputcnt--;
351
352 if (state!=3) {
353 /* remove one level of escape characters */
354 if ((c == '\\') && (prev != '\\')) {
355 if (inputcnt-- == 0)
356 break;
357 prev = c;
358 c = *input++;
359 }
360 }
361
362 switch (state) {
363 case 0: /* Waiting for (unescaped) $ */
364 if ((c == '\'') && (prev != '\\')) {
365 state = 3;
366 break;
367 }
368 if ((c == '$') && (prev != '\\')) {
369 state++;
370 } else {
371 *(output++) = c;
372 outputcnt--;
373 }
374 break;
375 case 1: /* Waiting for ( */
376 if (c == '(' || c == '{') {
377 state++;
378 varname_start = input;
379 } else {
380 state = 0;
381 *(output++) = '$';
382 outputcnt--;
383
384 if (outputcnt) {
385 *(output++) = c;
386 outputcnt--;
387 }
388 }
389 break;
390 case 2: /* Waiting for ) */
391 if (c == ')' || c == '}') {
392 int i;
393 char envname[CFG_CBSIZE], *envval;
394 int envcnt = input-varname_start-1; /* Varname # of chars */
395
396 /* Get the varname */
397 for (i = 0; i < envcnt; i++) {
398 envname[i] = varname_start[i];
399 }
400 envname[i] = 0;
401
402 /* Get its value */
403 envval = getenv (envname);
404
405 /* Copy into the line if it exists */
406 if (envval != NULL)
407 while ((*envval) && outputcnt) {
408 *(output++) = *(envval++);
409 outputcnt--;
410 }
411 /* Look for another '$' */
412 state = 0;
413 }
414 break;
415 case 3: /* Waiting for ' */
416 if ((c == '\'') && (prev != '\\')) {
417 state = 0;
418 } else {
419 *(output++) = c;
420 outputcnt--;
421 }
422 break;
423 }
424 prev = c;
425 }
426
427 if (outputcnt)
428 *output = 0;
429
430#ifdef DEBUG_PARSER
431 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
432 strlen(output_start), output_start);
433#endif
434}
435
436
437/*
438 * this is called from board_init() after the hardware has been set up
439 * and is usable. That seems like a good time to do this.
440 * Right now the return value is ignored.
441 */
442int do_auto_update(void)
443{
444 block_dev_desc_t *stor_dev;
445 long sz;
446 int i, res, cnt, old_ctrlc, got_ctrlc;
447 char buffer[32];
448 char str[80];
449
450 /*
451 * Check whether a CompactFlash is inserted
452 */
453 if (ide_dev_desc[0].type == DEV_TYPE_UNKNOWN) {
454 return -1; /* no disk detected! */
455 }
456
457 /* check whether it has a partition table */
458 stor_dev = get_dev("ide", 0);
459 if (stor_dev == NULL) {
460 debug ("Uknown device type\n");
461 return -1;
462 }
463 if (fat_register_device(stor_dev, 1) != 0) {
464 debug ("Unable to register ide disk 0:1 for fatls\n");
465 return -1;
466 }
467
468 /*
469 * Check if magic file is present
470 */
471 if (do_fat_read(AU_MAGIC_FILE, buffer, sizeof(buffer), LS_NO) <= 0) {
472 return -1;
473 }
474
475#ifdef CONFIG_AUTO_UPDATE_SHOW
476 board_auto_update_show(1);
477#endif
478 puts("\nAutoUpdate Disk detected! Trying to update system...\n");
479
480 /* make sure that we see CTRL-C and save the old state */
481 old_ctrlc = disable_ctrlc(0);
482
483 /* just loop thru all the possible files */
484 for (i = 0; i < N_AU_IMAGES; i++) {
485 /*
486 * Try to expand the environment var in the fname
487 */
488 process_macros(au_image[i].name, str);
489 strcpy(au_image[i].name, str);
490
491 printf("Reading %s ...", au_image[i].name);
492 /* just read the header */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100493 sz = do_fat_read(au_image[i].name, LOAD_ADDR, image_get_header_size (), LS_NO);
stroese0621f6f2004-12-16 18:43:13 +0000494 debug ("read %s sz %ld hdr %d\n",
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100495 au_image[i].name, sz, image_get_header_size ());
496 if (sz <= 0 || sz < image_get_header_size ()) {
stroese0621f6f2004-12-16 18:43:13 +0000497 puts(" not found\n");
498 continue;
499 }
500 if (au_check_header_valid(i, sz) < 0) {
501 puts(" header not valid\n");
502 continue;
503 }
504 sz = do_fat_read(au_image[i].name, LOAD_ADDR, MAX_LOADSZ, LS_NO);
505 debug ("read %s sz %ld hdr %d\n",
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100506 au_image[i].name, sz, image_get_header_size ());
507 if (sz <= 0 || sz <= image_get_header_size ()) {
stroese0621f6f2004-12-16 18:43:13 +0000508 puts(" not found\n");
509 continue;
510 }
511 if (au_check_cksum_valid(i, sz) < 0) {
512 puts(" checksum not valid\n");
513 continue;
514 }
515 puts(" done\n");
516
517 do {
518 res = au_do_update(i, sz);
519 /* let the user break out of the loop */
520 if (ctrlc() || had_ctrlc()) {
521 clear_ctrlc();
522 if (res < 0)
523 got_ctrlc = 1;
524 break;
525 }
526 cnt++;
527 } while (res < 0);
528 }
529
530 /* restore the old state */
531 disable_ctrlc(old_ctrlc);
532
533 puts("AutoUpdate finished\n\n");
534#ifdef CONFIG_AUTO_UPDATE_SHOW
535 board_auto_update_show(0);
536#endif
537
538 return 0;
539}
540
541
542int auto_update(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
543{
544 do_auto_update();
545
546 return 0;
547}
548U_BOOT_CMD(
549 autoupd, 1, 1, auto_update,
550 "autoupd - Automatically update images\n",
551 NULL
552);
553#endif /* CONFIG_AUTO_UPDATE */