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
6 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #include <asm/byteorder.h>
30 #if defined(CFG_NAND_LEGACY)
31 #include <linux/mtd/nand_legacy.h>
36 #include "auto_update.h"
38 #ifdef CONFIG_AUTO_UPDATE
40 #if !defined(CONFIG_CMD_FAT)
41 #error "must define CONFIG_CMD_FAT"
44 extern au_image_t au_image[];
45 extern int N_AU_IMAGES;
52 #define debug(fmt,args...) printf (fmt ,##args)
54 #define debug(fmt,args...)
58 #define LOAD_ADDR ((unsigned char *)0x100000) /* where to load files into memory */
59 #define MAX_LOADSZ 0x1e00000
62 extern int fat_register_device(block_dev_desc_t *, int);
63 extern int file_fat_detectfs(void);
64 extern long file_fat_read(const char *, void *, unsigned long);
65 long do_fat_read (const char *filename, void *buffer, unsigned long maxsize, int dols);
67 extern int trab_vfd (ulong);
68 extern int transfer_pic(unsigned char, unsigned char *, int, int);
70 extern int flash_sect_erase(ulong, ulong);
71 extern int flash_sect_protect (int, ulong, ulong);
72 extern int flash_write (char *, ulong, ulong);
74 #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
75 /* 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
80 extern struct nand_chip nand_dev_desc[];
81 extern int nand_legacy_rw(struct nand_chip* nand, int cmd, size_t start, size_t len,
82 size_t * retlen, u_char * buf);
83 extern int nand_legacy_erase(struct nand_chip* nand, size_t ofs, size_t len, int clean);
86 extern block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
89 int au_check_cksum_valid(int i, long nbytes)
93 hdr = (image_header_t *)LOAD_ADDR;
94 #if defined(CONFIG_FIT)
95 if (gen_image_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
96 puts ("Non legacy image format not supported\n");
101 if ((au_image[i].type == AU_FIRMWARE) &&
102 (au_image[i].size != image_get_data_size (hdr))) {
103 printf ("Image %s has wrong size\n", au_image[i].name);
107 if (nbytes != (image_get_image_size (hdr))) {
108 printf ("Image %s bad total SIZE\n", au_image[i].name);
112 /* check the data CRC */
113 if (!image_check_dcrc (hdr)) {
114 printf ("Image %s bad data checksum\n", au_image[i].name);
121 int au_check_header_valid(int i, long nbytes)
124 unsigned long checksum;
126 hdr = (image_header_t *)LOAD_ADDR;
127 #if defined(CONFIG_FIT)
128 if (gen_image_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
129 puts ("Non legacy image format not supported\n");
134 /* check the easy ones first */
135 #undef CHECK_VALID_DEBUG
136 #ifdef CHECK_VALID_DEBUG
137 printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC);
138 printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_PPC);
139 printf("size %#x %#lx ", image_get_data_size (hdr), nbytes);
140 printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL);
142 if (nbytes < image_get_header_size ())
144 printf ("Image %s bad header SIZE\n", au_image[i].name);
147 if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC))
149 printf ("Image %s bad MAGIC or ARCH\n", au_image[i].name);
152 if (!image_check_hcrc (hdr)) {
153 printf ("Image %s bad header checksum\n", au_image[i].name);
157 /* check the type - could do this all in one gigantic if() */
158 if ((au_image[i].type == AU_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
159 printf ("Image %s wrong type\n", au_image[i].name);
162 if ((au_image[i].type == AU_SCRIPT) && !image_check_type (hdr, IH_TYPE_SCRIPT)) {
163 printf ("Image %s wrong type\n", au_image[i].name);
167 /* recycle checksum */
168 checksum = image_get_data_size (hdr);
170 #if 0 /* test-only */
171 /* for kernel and app the image header must also fit into flash */
173 checksum += image_get_header_size ();
174 /* check the size does not exceed space in flash. HUSH scripts */
175 /* all have ausize[] set to 0 */
176 if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
177 printf ("Image %s is bigger than FLASH\n", au_image[i].name);
186 int au_do_update(int i, long sz)
194 #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
198 hdr = (image_header_t *)LOAD_ADDR;
199 #if defined(CONFIG_FIT)
200 if (gen_image_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
201 puts ("Non legacy image format not supported\n");
206 switch (au_image[i].type) {
208 printf("Executing script %s\n", au_image[i].name);
210 /* execute a script */
211 if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
212 addr = (char *)((char *)hdr + image_get_header_size ());
213 /* stick a NULL at the end of the script, otherwise */
214 /* parse_string_outer() runs off the end. */
215 addr[image_get_data_size (hdr)] = 0;
219 * Replace cr/lf with ;
222 while (addr[k] != 0) {
223 if ((addr[k] == 10) || (addr[k] == 13)) {
229 run_command(addr, 0);
238 start = au_image[i].start;
239 end = au_image[i].start + au_image[i].size - 1;
242 * do not update firmware when image is already in flash.
244 if (au_image[i].type == AU_FIRMWARE) {
245 char *orig = (char*)start;
246 char *new = (char *)((char *)hdr + image_get_header_size ());
247 nbytes = image_get_data_size (hdr);
250 if (*orig++ != *new++) {
255 printf("Skipping firmware update - images are identical\n");
260 /* unprotect the address range */
261 /* this assumes that ONLY the firmware is protected! */
262 if (au_image[i].type == AU_FIRMWARE) {
263 flash_sect_protect(0, start, end);
267 * erase the address range.
269 if (au_image[i].type != AU_NAND) {
270 printf("Updating NOR FLASH with image %s\n", au_image[i].name);
271 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
272 flash_sect_erase(start, end);
274 #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
275 printf("Updating NAND FLASH with image %s\n", au_image[i].name);
276 debug ("nand_legacy_erase(%lx, %lx);\n", start, end);
277 rc = nand_legacy_erase (nand_dev_desc, start, end - start + 1, 0);
278 debug ("nand_legacy_erase returned %x\n", rc);
284 /* strip the header - except for the kernel and ramdisk */
285 if (au_image[i].type != AU_FIRMWARE) {
287 off = image_get_header_size ();
288 nbytes = image_get_image_size (hdr);
290 addr = (char *)((char *)hdr + image_get_header_size ());
292 nbytes = image_get_data_size (hdr);
296 * copy the data from RAM to FLASH
298 if (au_image[i].type != AU_NAND) {
299 debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
300 rc = flash_write((char *)addr, start, nbytes);
302 #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
303 debug ("nand_legacy_rw(%p, %lx %x)\n", addr, start, nbytes);
304 rc = nand_legacy_rw(nand_dev_desc, NANDRW_WRITE | NANDRW_JFFS2,
305 start, nbytes, (size_t *)&total, (uchar *)addr);
306 debug ("nand_legacy_rw: ret=%x total=%d nbytes=%d\n", rc, total, nbytes);
312 printf("Flashing failed due to error %d\n", rc);
317 * check the dcrc of the copy
319 if (au_image[i].type != AU_NAND) {
320 rc = crc32 (0, (uchar *)(start + off), image_get_data_size (hdr));
322 #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
323 rc = nand_legacy_rw(nand_dev_desc, NANDRW_READ | NANDRW_JFFS2 | NANDRW_JFFS2_SKIP,
324 start, nbytes, (size_t *)&total, (uchar *)addr);
325 rc = crc32 (0, (uchar *)(addr + off), image_get_data_size (hdr));
328 if (rc != image_get_dcrc (hdr)) {
329 printf ("Image %s Bad Data Checksum After COPY\n", au_image[i].name);
333 /* protect the address range */
334 /* this assumes that ONLY the firmware is protected! */
335 if (au_image[i].type == AU_FIRMWARE) {
336 flash_sect_protect(1, start, end);
342 printf("Wrong image type selected!\n");
349 static void process_macros (const char *input, char *output)
352 const char *varname_start = NULL;
353 int inputcnt = strlen (input);
354 int outputcnt = CFG_CBSIZE;
355 int state = 0; /* 0 = waiting for '$' */
356 /* 1 = waiting for '(' or '{' */
357 /* 2 = waiting for ')' or '}' */
358 /* 3 = waiting for ''' */
360 char *output_start = output;
362 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
365 prev = '\0'; /* previous character */
367 while (inputcnt && outputcnt) {
372 /* remove one level of escape characters */
373 if ((c == '\\') && (prev != '\\')) {
382 case 0: /* Waiting for (unescaped) $ */
383 if ((c == '\'') && (prev != '\\')) {
387 if ((c == '$') && (prev != '\\')) {
394 case 1: /* Waiting for ( */
395 if (c == '(' || c == '{') {
397 varname_start = input;
409 case 2: /* Waiting for ) */
410 if (c == ')' || c == '}') {
412 char envname[CFG_CBSIZE], *envval;
413 int envcnt = input-varname_start-1; /* Varname # of chars */
415 /* Get the varname */
416 for (i = 0; i < envcnt; i++) {
417 envname[i] = varname_start[i];
422 envval = getenv (envname);
424 /* Copy into the line if it exists */
426 while ((*envval) && outputcnt) {
427 *(output++) = *(envval++);
430 /* Look for another '$' */
434 case 3: /* Waiting for ' */
435 if ((c == '\'') && (prev != '\\')) {
450 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
451 strlen(output_start), output_start);
457 * this is called from board_init() after the hardware has been set up
458 * and is usable. That seems like a good time to do this.
459 * Right now the return value is ignored.
461 int do_auto_update(void)
463 block_dev_desc_t *stor_dev;
465 int i, res, cnt, old_ctrlc, got_ctrlc;
470 * Check whether a CompactFlash is inserted
472 if (ide_dev_desc[0].type == DEV_TYPE_UNKNOWN) {
473 return -1; /* no disk detected! */
476 /* check whether it has a partition table */
477 stor_dev = get_dev("ide", 0);
478 if (stor_dev == NULL) {
479 debug ("Uknown device type\n");
482 if (fat_register_device(stor_dev, 1) != 0) {
483 debug ("Unable to register ide disk 0:1 for fatls\n");
488 * Check if magic file is present
490 if (do_fat_read(AU_MAGIC_FILE, buffer, sizeof(buffer), LS_NO) <= 0) {
494 #ifdef CONFIG_AUTO_UPDATE_SHOW
495 board_auto_update_show(1);
497 puts("\nAutoUpdate Disk detected! Trying to update system...\n");
499 /* make sure that we see CTRL-C and save the old state */
500 old_ctrlc = disable_ctrlc(0);
502 /* just loop thru all the possible files */
503 for (i = 0; i < N_AU_IMAGES; i++) {
505 * Try to expand the environment var in the fname
507 process_macros(au_image[i].name, str);
508 strcpy(au_image[i].name, str);
510 printf("Reading %s ...", au_image[i].name);
511 /* just read the header */
512 sz = do_fat_read(au_image[i].name, LOAD_ADDR, image_get_header_size (), LS_NO);
513 debug ("read %s sz %ld hdr %d\n",
514 au_image[i].name, sz, image_get_header_size ());
515 if (sz <= 0 || sz < image_get_header_size ()) {
516 puts(" not found\n");
519 if (au_check_header_valid(i, sz) < 0) {
520 puts(" header not valid\n");
523 sz = do_fat_read(au_image[i].name, LOAD_ADDR, MAX_LOADSZ, LS_NO);
524 debug ("read %s sz %ld hdr %d\n",
525 au_image[i].name, sz, image_get_header_size ());
526 if (sz <= 0 || sz <= image_get_header_size ()) {
527 puts(" not found\n");
530 if (au_check_cksum_valid(i, sz) < 0) {
531 puts(" checksum not valid\n");
537 res = au_do_update(i, sz);
538 /* let the user break out of the loop */
539 if (ctrlc() || had_ctrlc()) {
549 /* restore the old state */
550 disable_ctrlc(old_ctrlc);
552 puts("AutoUpdate finished\n\n");
553 #ifdef CONFIG_AUTO_UPDATE_SHOW
554 board_auto_update_show(0);
561 int auto_update(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
568 autoupd, 1, 1, auto_update,
569 "autoupd - Automatically update images\n",
572 #endif /* CONFIG_AUTO_UPDATE */