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,
27 #if (CONFIG_COMMANDS & CFG_CMD_NAND) && !defined(CFG_NAND_LEGACY)
28 #warning CFG_NAND_LEGACY not defined in a file using the legacy NAND support!
33 #include <asm/byteorder.h>
34 #include <linux/mtd/nand_legacy.h>
38 #include "auto_update.h"
40 #ifdef CONFIG_AUTO_UPDATE
42 #if !(CONFIG_COMMANDS & CFG_CMD_FAT)
43 #error "must define CFG_CMD_FAT"
46 extern au_image_t au_image[];
47 extern int N_AU_IMAGES;
54 #define debug(fmt,args...) printf (fmt ,##args)
56 #define debug(fmt,args...)
60 #define LOAD_ADDR ((unsigned char *)0x100000) /* where to load files into memory */
61 #define MAX_LOADSZ 0x1e00000
64 extern int fat_register_device(block_dev_desc_t *, int);
65 extern int file_fat_detectfs(void);
66 extern long file_fat_read(const char *, void *, unsigned long);
67 long do_fat_read (const char *filename, void *buffer, unsigned long maxsize, int dols);
69 extern int trab_vfd (ulong);
70 extern int transfer_pic(unsigned char, unsigned char *, int, int);
72 extern int flash_sect_erase(ulong, ulong);
73 extern int flash_sect_protect (int, ulong, ulong);
74 extern int flash_write (char *, ulong, ulong);
76 #if (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
77 /* references to names in cmd_nand.c */
78 #define NANDRW_READ 0x01
79 #define NANDRW_WRITE 0x00
80 #define NANDRW_JFFS2 0x02
81 #define NANDRW_JFFS2_SKIP 0x04
82 extern struct nand_chip nand_dev_desc[];
83 extern int nand_legacy_rw(struct nand_chip* nand, int cmd, size_t start, size_t len,
84 size_t * retlen, u_char * buf);
85 extern int nand_legacy_erase(struct nand_chip* nand, size_t ofs, size_t len, int clean);
86 #endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY) */
88 extern block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
91 int au_check_cksum_valid(int i, long nbytes)
94 unsigned long checksum;
96 hdr = (image_header_t *)LOAD_ADDR;
98 if ((au_image[i].type == AU_FIRMWARE) && (au_image[i].size != ntohl(hdr->ih_size))) {
99 printf ("Image %s has wrong size\n", au_image[i].name);
103 if (nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size))) {
104 printf ("Image %s bad total SIZE\n", au_image[i].name);
107 /* check the data CRC */
108 checksum = ntohl(hdr->ih_dcrc);
110 if (crc32 (0, (uchar *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size))
112 printf ("Image %s bad data checksum\n", au_image[i].name);
119 int au_check_header_valid(int i, long nbytes)
122 unsigned long checksum;
124 hdr = (image_header_t *)LOAD_ADDR;
125 /* check the easy ones first */
126 #undef CHECK_VALID_DEBUG
127 #ifdef CHECK_VALID_DEBUG
128 printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
129 printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_PPC);
130 printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
131 printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
133 if (nbytes < sizeof(*hdr))
135 printf ("Image %s bad header SIZE\n", au_image[i].name);
138 if (ntohl(hdr->ih_magic) != IH_MAGIC || hdr->ih_arch != IH_CPU_PPC)
140 printf ("Image %s bad MAGIC or ARCH\n", au_image[i].name);
143 /* check the hdr CRC */
144 checksum = ntohl(hdr->ih_hcrc);
147 if (crc32 (0, (uchar *)hdr, sizeof(*hdr)) != checksum) {
148 printf ("Image %s bad header checksum\n", au_image[i].name);
151 hdr->ih_hcrc = htonl(checksum);
153 /* check the type - could do this all in one gigantic if() */
154 if ((au_image[i].type == AU_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
155 printf ("Image %s wrong type\n", au_image[i].name);
158 if ((au_image[i].type == AU_SCRIPT) && (hdr->ih_type != IH_TYPE_SCRIPT)) {
159 printf ("Image %s wrong type\n", au_image[i].name);
163 /* recycle checksum */
164 checksum = ntohl(hdr->ih_size);
166 #if 0 /* test-only */
167 /* for kernel and app the image header must also fit into flash */
169 checksum += sizeof(*hdr);
170 /* check the size does not exceed space in flash. HUSH scripts */
171 /* all have ausize[] set to 0 */
172 if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
173 printf ("Image %s is bigger than FLASH\n", au_image[i].name);
182 int au_do_update(int i, long sz)
190 #if (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
194 hdr = (image_header_t *)LOAD_ADDR;
196 switch (au_image[i].type) {
198 printf("Executing script %s\n", au_image[i].name);
200 /* execute a script */
201 if (hdr->ih_type == IH_TYPE_SCRIPT) {
202 addr = (char *)((char *)hdr + sizeof(*hdr));
203 /* stick a NULL at the end of the script, otherwise */
204 /* parse_string_outer() runs off the end. */
205 addr[ntohl(hdr->ih_size)] = 0;
209 * Replace cr/lf with ;
212 while (addr[k] != 0) {
213 if ((addr[k] == 10) || (addr[k] == 13)) {
219 run_command(addr, 0);
228 start = au_image[i].start;
229 end = au_image[i].start + au_image[i].size - 1;
232 * do not update firmware when image is already in flash.
234 if (au_image[i].type == AU_FIRMWARE) {
235 char *orig = (char*)start;
236 char *new = (char *)((char *)hdr + sizeof(*hdr));
237 nbytes = ntohl(hdr->ih_size);
240 if (*orig++ != *new++) {
245 printf("Skipping firmware update - images are identical\n");
250 /* unprotect the address range */
251 /* this assumes that ONLY the firmware is protected! */
252 if (au_image[i].type == AU_FIRMWARE) {
253 flash_sect_protect(0, start, end);
257 * erase the address range.
259 if (au_image[i].type != AU_NAND) {
260 printf("Updating NOR FLASH with image %s\n", au_image[i].name);
261 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
262 flash_sect_erase(start, end);
264 #if (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
265 printf("Updating NAND FLASH with image %s\n", au_image[i].name);
266 debug ("nand_legacy_erase(%lx, %lx);\n", start, end);
267 rc = nand_legacy_erase (nand_dev_desc, start, end - start + 1, 0);
268 debug ("nand_legacy_erase returned %x\n", rc);
274 /* strip the header - except for the kernel and ramdisk */
275 if (au_image[i].type != AU_FIRMWARE) {
278 nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
280 addr = (char *)((char *)hdr + sizeof(*hdr));
282 nbytes = ntohl(hdr->ih_size);
286 * copy the data from RAM to FLASH
288 if (au_image[i].type != AU_NAND) {
289 debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
290 rc = flash_write((char *)addr, start, nbytes);
292 #if (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
293 debug ("nand_legacy_rw(%p, %lx %x)\n", addr, start, nbytes);
294 rc = nand_legacy_rw(nand_dev_desc, NANDRW_WRITE | NANDRW_JFFS2,
295 start, nbytes, (size_t *)&total, (uchar *)addr);
296 debug ("nand_legacy_rw: ret=%x total=%d nbytes=%d\n", rc, total, nbytes);
300 printf("Flashing failed due to error %d\n", rc);
305 * check the dcrc of the copy
307 if (au_image[i].type != AU_NAND) {
308 rc = crc32 (0, (uchar *)(start + off), ntohl(hdr->ih_size));
310 #if (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
311 rc = nand_legacy_rw(nand_dev_desc, NANDRW_READ | NANDRW_JFFS2 | NANDRW_JFFS2_SKIP,
312 start, nbytes, (size_t *)&total, (uchar *)addr);
313 rc = crc32 (0, (uchar *)(addr + off), ntohl(hdr->ih_size));
316 if (rc != ntohl(hdr->ih_dcrc)) {
317 printf ("Image %s Bad Data Checksum After COPY\n", au_image[i].name);
321 /* protect the address range */
322 /* this assumes that ONLY the firmware is protected! */
323 if (au_image[i].type == AU_FIRMWARE) {
324 flash_sect_protect(1, start, end);
330 printf("Wrong image type selected!\n");
337 static void process_macros (const char *input, char *output)
340 const char *varname_start = NULL;
341 int inputcnt = strlen (input);
342 int outputcnt = CFG_CBSIZE;
343 int state = 0; /* 0 = waiting for '$' */
344 /* 1 = waiting for '(' or '{' */
345 /* 2 = waiting for ')' or '}' */
346 /* 3 = waiting for ''' */
348 char *output_start = output;
350 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
353 prev = '\0'; /* previous character */
355 while (inputcnt && outputcnt) {
360 /* remove one level of escape characters */
361 if ((c == '\\') && (prev != '\\')) {
370 case 0: /* Waiting for (unescaped) $ */
371 if ((c == '\'') && (prev != '\\')) {
375 if ((c == '$') && (prev != '\\')) {
382 case 1: /* Waiting for ( */
383 if (c == '(' || c == '{') {
385 varname_start = input;
397 case 2: /* Waiting for ) */
398 if (c == ')' || c == '}') {
400 char envname[CFG_CBSIZE], *envval;
401 int envcnt = input-varname_start-1; /* Varname # of chars */
403 /* Get the varname */
404 for (i = 0; i < envcnt; i++) {
405 envname[i] = varname_start[i];
410 envval = getenv (envname);
412 /* Copy into the line if it exists */
414 while ((*envval) && outputcnt) {
415 *(output++) = *(envval++);
418 /* Look for another '$' */
422 case 3: /* Waiting for ' */
423 if ((c == '\'') && (prev != '\\')) {
438 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
439 strlen(output_start), output_start);
445 * this is called from board_init() after the hardware has been set up
446 * and is usable. That seems like a good time to do this.
447 * Right now the return value is ignored.
449 int do_auto_update(void)
451 block_dev_desc_t *stor_dev;
453 int i, res, cnt, old_ctrlc, got_ctrlc;
458 * Check whether a CompactFlash is inserted
460 if (ide_dev_desc[0].type == DEV_TYPE_UNKNOWN) {
461 return -1; /* no disk detected! */
464 /* check whether it has a partition table */
465 stor_dev = get_dev("ide", 0);
466 if (stor_dev == NULL) {
467 debug ("Uknown device type\n");
470 if (fat_register_device(stor_dev, 1) != 0) {
471 debug ("Unable to register ide disk 0:1 for fatls\n");
476 * Check if magic file is present
478 if (do_fat_read(AU_MAGIC_FILE, buffer, sizeof(buffer), LS_NO) <= 0) {
482 #ifdef CONFIG_AUTO_UPDATE_SHOW
483 board_auto_update_show(1);
485 puts("\nAutoUpdate Disk detected! Trying to update system...\n");
487 /* make sure that we see CTRL-C and save the old state */
488 old_ctrlc = disable_ctrlc(0);
490 /* just loop thru all the possible files */
491 for (i = 0; i < N_AU_IMAGES; i++) {
493 * Try to expand the environment var in the fname
495 process_macros(au_image[i].name, str);
496 strcpy(au_image[i].name, str);
498 printf("Reading %s ...", au_image[i].name);
499 /* just read the header */
500 sz = do_fat_read(au_image[i].name, LOAD_ADDR, sizeof(image_header_t), LS_NO);
501 debug ("read %s sz %ld hdr %d\n",
502 au_image[i].name, sz, sizeof(image_header_t));
503 if (sz <= 0 || sz < sizeof(image_header_t)) {
504 puts(" not found\n");
507 if (au_check_header_valid(i, sz) < 0) {
508 puts(" header not valid\n");
511 sz = do_fat_read(au_image[i].name, LOAD_ADDR, MAX_LOADSZ, LS_NO);
512 debug ("read %s sz %ld hdr %d\n",
513 au_image[i].name, sz, sizeof(image_header_t));
514 if (sz <= 0 || sz <= sizeof(image_header_t)) {
515 puts(" not found\n");
518 if (au_check_cksum_valid(i, sz) < 0) {
519 puts(" checksum not valid\n");
525 res = au_do_update(i, sz);
526 /* let the user break out of the loop */
527 if (ctrlc() || had_ctrlc()) {
537 /* restore the old state */
538 disable_ctrlc(old_ctrlc);
540 puts("AutoUpdate finished\n\n");
541 #ifdef CONFIG_AUTO_UPDATE_SHOW
542 board_auto_update_show(0);
549 int auto_update(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
556 autoupd, 1, 1, auto_update,
557 "autoupd - Automatically update images\n",
560 #endif /* CONFIG_AUTO_UPDATE */