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(CONFIG_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
;
47 /* where to load files into memory */
48 #define LOAD_ADDR ((unsigned char *)0x100000)
49 #define MAX_LOADSZ 0x1c00000
52 extern int fat_register_device(block_dev_desc_t
*, int);
53 extern int file_fat_detectfs(void);
54 extern long file_fat_read(const char *, void *, unsigned long);
55 long do_fat_read (const char *filename
, void *buffer
,
56 unsigned long maxsize
, int dols
);
57 extern int flash_sect_erase(ulong
, ulong
);
58 extern int flash_sect_protect (int, ulong
, ulong
);
59 extern int flash_write (char *, ulong
, ulong
);
61 #if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_LEGACY)
62 /* 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
67 extern struct nand_chip nand_dev_desc
[];
68 extern int nand_legacy_rw(struct nand_chip
* nand
, int cmd
,
69 size_t start
, size_t len
,
70 size_t * retlen
, u_char
* buf
);
71 extern int nand_legacy_erase(struct nand_chip
* nand
, size_t ofs
,
72 size_t len
, int clean
);
75 extern block_dev_desc_t ide_dev_desc
[CFG_IDE_MAXDEVICE
];
77 int au_check_cksum_valid(int i
, long nbytes
)
81 hdr
= (image_header_t
*)LOAD_ADDR
;
82 #if defined(CONFIG_FIT)
83 if (genimg_get_format ((void *)hdr
) != IMAGE_FORMAT_LEGACY
) {
84 puts ("Non legacy image format not supported\n");
89 if ((au_image
[i
].type
== AU_FIRMWARE
) &&
90 (au_image
[i
].size
!= image_get_data_size (hdr
))) {
91 printf ("Image %s has wrong size\n", au_image
[i
].name
);
95 if (nbytes
!= (image_get_image_size (hdr
))) {
96 printf ("Image %s bad total SIZE\n", au_image
[i
].name
);
100 /* check the data CRC */
101 if (!image_check_dcrc (hdr
)) {
102 printf ("Image %s bad data checksum\n", au_image
[i
].name
);
108 int au_check_header_valid(int i
, long nbytes
)
111 unsigned long checksum
;
113 hdr
= (image_header_t
*)LOAD_ADDR
;
114 #if defined(CONFIG_FIT)
115 if (genimg_get_format ((void *)hdr
) != IMAGE_FORMAT_LEGACY
) {
116 puts ("Non legacy image format not supported\n");
121 /* check the easy ones first */
122 if (nbytes
< image_get_header_size ()) {
123 printf ("Image %s bad header SIZE\n", au_image
[i
].name
);
126 if (!image_check_magic (hdr
) || !image_check_arch (hdr
, IH_ARCH_PPC
)) {
127 printf ("Image %s bad MAGIC or ARCH\n", au_image
[i
].name
);
130 if (!image_check_hcrc (hdr
)) {
131 printf ("Image %s bad header checksum\n", au_image
[i
].name
);
135 /* check the type - could do this all in one gigantic if() */
136 if (((au_image
[i
].type
& AU_TYPEMASK
) == AU_FIRMWARE
) &&
137 !image_check_type (hdr
, IH_TYPE_FIRMWARE
)) {
138 printf ("Image %s wrong type\n", au_image
[i
].name
);
141 if (((au_image
[i
].type
& AU_TYPEMASK
) == AU_SCRIPT
) &&
142 !image_check_type (hdr
, IH_TYPE_SCRIPT
)) {
143 printf ("Image %s wrong type\n", au_image
[i
].name
);
147 /* recycle checksum */
148 checksum
= image_get_data_size (hdr
);
153 int au_do_update(int i
, long sz
)
161 #if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_LEGACY)
165 hdr
= (image_header_t
*)LOAD_ADDR
;
166 #if defined(CONFIG_FIT)
167 if (genimg_get_format ((void *)hdr
) != IMAGE_FORMAT_LEGACY
) {
168 puts ("Non legacy image format not supported\n");
173 switch (au_image
[i
].type
& AU_TYPEMASK
) {
175 printf("Executing script %s\n", au_image
[i
].name
);
177 /* execute a script */
178 if (image_check_type (hdr
, IH_TYPE_SCRIPT
)) {
179 addr
= (char *)((char *)hdr
+ image_get_header_size ());
180 /* stick a NULL at the end of the script, otherwise */
181 /* parse_string_outer() runs off the end. */
182 addr
[image_get_data_size (hdr
)] = 0;
186 * Replace cr/lf with ;
189 while (addr
[k
] != 0) {
190 if ((addr
[k
] == 10) || (addr
[k
] == 13)) {
196 run_command(addr
, 0);
205 start
= au_image
[i
].start
;
206 end
= au_image
[i
].start
+ au_image
[i
].size
- 1;
209 * do not update firmware when image is already in flash.
211 if (au_image
[i
].type
== AU_FIRMWARE
) {
212 char *orig
= (char*)start
;
213 char *new = (char *)((char *)hdr
+
214 image_get_header_size ());
215 nbytes
= image_get_data_size (hdr
);
218 if (*orig
++ != *new++) {
223 printf ("Skipping firmware update - "
224 "images are identical\n");
229 /* unprotect the address range */
230 if (((au_image
[i
].type
& AU_FLAGMASK
) == AU_PROTECT
) ||
231 (au_image
[i
].type
== AU_FIRMWARE
)) {
232 flash_sect_protect (0, start
, end
);
236 * erase the address range.
238 if (au_image
[i
].type
!= AU_NAND
) {
239 printf ("Updating NOR FLASH with image %s\n",
241 debug ("flash_sect_erase(%lx, %lx);\n", start
, end
);
242 flash_sect_erase (start
, end
);
244 #if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_LEGACY)
245 printf ("Updating NAND FLASH with image %s\n",
247 debug ("nand_legacy_erase(%lx, %lx);\n", start
, end
);
248 rc
= nand_legacy_erase (nand_dev_desc
, start
,
250 debug ("nand_legacy_erase returned %x\n", rc
);
256 /* strip the header - except for the kernel and ramdisk */
257 if (au_image
[i
].type
!= AU_FIRMWARE
) {
259 off
= image_get_header_size ();
260 nbytes
= image_get_image_size (hdr
);
262 addr
= (char *)((char *)hdr
+ image_get_header_size ());
264 nbytes
= image_get_data_size (hdr
);
268 * copy the data from RAM to FLASH
270 if (au_image
[i
].type
!= AU_NAND
) {
271 debug ("flash_write(%p, %lx, %x)\n",
272 addr
, start
, nbytes
);
273 rc
= flash_write ((char *)addr
, start
,
276 #if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_LEGACY)
277 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
,
283 debug ("nand_legacy_rw: ret=%x total=%d nbytes=%d\n",
290 printf ("Flashing failed due to error %d\n", rc
);
295 * check the dcrc of the copy
297 if (au_image
[i
].type
!= AU_NAND
) {
298 rc
= crc32 (0, (uchar
*)(start
+ off
),
299 image_get_data_size (hdr
));
301 #if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_LEGACY)
302 rc
= nand_legacy_rw (nand_dev_desc
,
303 NANDRW_READ
| NANDRW_JFFS2
|
305 start
, nbytes
, (size_t *)&total
,
307 rc
= crc32 (0, (uchar
*)(addr
+ off
),
308 image_get_data_size (hdr
));
311 if (rc
!= image_get_dcrc (hdr
)) {
312 printf ("Image %s Bad Data Checksum After COPY\n",
317 /* protect the address range */
318 /* this assumes that ONLY the firmware is protected! */
319 if (((au_image
[i
].type
& AU_FLAGMASK
) == AU_PROTECT
) ||
320 (au_image
[i
].type
== AU_FIRMWARE
)) {
321 flash_sect_protect (1, start
, end
);
327 printf("Wrong image type selected!\n");
333 static void process_macros (const char *input
, char *output
)
336 const char *varname_start
= NULL
;
337 int inputcnt
= strlen (input
);
338 int outputcnt
= CFG_CBSIZE
;
339 int state
= 0; /* 0 = waiting for '$' */
340 /* 1 = waiting for '(' or '{' */
341 /* 2 = waiting for ')' or '}' */
342 /* 3 = waiting for ''' */
344 char *output_start
= output
;
346 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n",
347 strlen(input
), input
);
350 prev
= '\0'; /* previous character */
352 while (inputcnt
&& outputcnt
) {
357 /* remove one level of escape characters */
358 if ((c
== '\\') && (prev
!= '\\')) {
367 case 0: /* Waiting for (unescaped) $ */
368 if ((c
== '\'') && (prev
!= '\\')) {
372 if ((c
== '$') && (prev
!= '\\')) {
379 case 1: /* Waiting for ( */
380 if (c
== '(' || c
== '{') {
382 varname_start
= input
;
394 case 2: /* Waiting for ) */
395 if (c
== ')' || c
== '}') {
397 char envname
[CFG_CBSIZE
], *envval
;
398 /* Varname # of chars */
399 int envcnt
= input
- varname_start
- 1;
401 /* Get the varname */
402 for (i
= 0; i
< envcnt
; i
++) {
403 envname
[i
] = varname_start
[i
];
408 envval
= getenv (envname
);
410 /* Copy into the line if it exists */
412 while ((*envval
) && outputcnt
) {
413 *(output
++) = *(envval
++);
416 /* Look for another '$' */
420 case 3: /* Waiting for ' */
421 if ((c
== '\'') && (prev
!= '\\')) {
436 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
437 strlen (output_start
), output_start
);
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.
446 int do_auto_update(void)
448 block_dev_desc_t
*stor_dev
= NULL
;
450 int i
, res
, cnt
, old_ctrlc
, got_ctrlc
;
455 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");
463 if (fat_register_device (stor_dev
, 1) != 0) {
464 debug ("Unable to register ide disk 0:1\n");
469 * Check if magic file is present
471 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
);
477 #ifdef CONFIG_AUTO_UPDATE_SHOW
478 board_auto_update_show (1);
480 puts("\nAutoUpdate Disk detected! Trying to update system...\n");
482 /* make sure that we see CTRL-C and save the old state */
483 old_ctrlc
= disable_ctrlc (0);
485 /* just loop thru all the possible files */
486 for (i
= 0; i
< N_AU_IMAGES
; i
++) {
488 * Try to expand the environment var in the fname
490 process_macros (au_image
[i
].name
, str
);
491 strcpy (au_image
[i
].name
, str
);
493 printf("Reading %s ...", au_image
[i
].name
);
494 /* just read the header */
495 sz
= do_fat_read (au_image
[i
].name
, LOAD_ADDR
,
496 image_get_header_size (), LS_NO
);
497 debug ("read %s sz %ld hdr %d\n",
498 au_image
[i
].name
, sz
, image_get_header_size ());
499 if (sz
<= 0 || sz
< image_get_header_size ()) {
500 puts(" not found\n");
503 if (au_check_header_valid (i
, sz
) < 0) {
504 puts(" header not valid\n");
507 sz
= do_fat_read (au_image
[i
].name
, LOAD_ADDR
,
509 debug ("read %s sz %ld hdr %d\n",
510 au_image
[i
].name
, sz
, image_get_header_size ());
511 if (sz
<= 0 || sz
<= image_get_header_size ()) {
512 puts(" not found\n");
515 if (au_check_cksum_valid (i
, sz
) < 0) {
516 puts(" checksum not valid\n");
522 res
= au_do_update (i
, sz
);
523 /* let the user break out of the loop */
524 if (ctrlc() || had_ctrlc ()) {
534 /* restore the old state */
535 disable_ctrlc (old_ctrlc
);
537 puts("AutoUpdate finished\n\n");
538 #ifdef CONFIG_AUTO_UPDATE_SHOW
539 board_auto_update_show (0);
545 int auto_update(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
552 autoupd
, 1, 1, auto_update
,
553 "autoupd - Automatically update images\n",
556 #endif /* CONFIG_AUTO_UPDATE */