blob: cd6b391b100373563f28b868259ffd454ec6c86b [file] [log] [blame]
Andrei Safronovcdb97a62006-12-08 16:23:08 +01001/*
2 * (C) Copyright 2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 * MA 02111-1307 USA
19 */
20#include <common.h>
21#include <command.h>
22#include <malloc.h>
23#include <image.h>
24#include <asm/byteorder.h>
25#include <usb.h>
Grant Likely735dd972007-02-20 09:04:34 +010026#include <part.h>
Andrei Safronovcdb97a62006-12-08 16:23:08 +010027
28#ifdef CFG_HUSH_PARSER
29#include <hush.h>
30#endif
31
32
33#ifdef CONFIG_AUTO_UPDATE
34
35#ifndef CONFIG_USB_OHCI
36#error "must define CONFIG_USB_OHCI"
37#endif
38
39#ifndef CONFIG_USB_STORAGE
40#error "must define CONFIG_USB_STORAGE"
41#endif
42
43#ifndef CFG_HUSH_PARSER
44#error "must define CFG_HUSH_PARSER"
45#endif
46
47#if !(CONFIG_COMMANDS & CFG_CMD_FAT)
48#error "must define CFG_CMD_FAT"
49#endif
50
Andrei Safronovcdb97a62006-12-08 16:23:08 +010051#undef AU_DEBUG
52
53#undef debug
54#ifdef AU_DEBUG
55#define debug(fmt,args...) printf (fmt ,##args)
56#else
57#define debug(fmt,args...)
58#endif /* AU_DEBUG */
59
60/* possible names of files on the USB stick. */
61#define AU_FIRMWARE "u-boot.img"
62#define AU_KERNEL "kernel.img"
Sergei Poselenov489c6962007-02-14 14:30:28 +030063#define AU_ROOTFS "rootfs.img"
Andrei Safronovcdb97a62006-12-08 16:23:08 +010064
Wolfgang Denk82e52362006-12-22 10:30:26 +010065struct flash_layout {
Andrei Safronovcdb97a62006-12-08 16:23:08 +010066 long start;
67 long end;
68};
69
70/* layout of the FLASH. ST = start address, ND = end address. */
71#define AU_FL_FIRMWARE_ST 0xfC000000
72#define AU_FL_FIRMWARE_ND 0xfC03FFFF
73#define AU_FL_KERNEL_ST 0xfC0C0000
74#define AU_FL_KERNEL_ND 0xfC1BFFFF
Sergei Poselenov489c6962007-02-14 14:30:28 +030075#define AU_FL_ROOTFS_ST 0xFC1C0000
76#define AU_FL_ROOTFS_ND 0xFCFBFFFF
Andrei Safronovcdb97a62006-12-08 16:23:08 +010077
78static int au_usb_stor_curr_dev; /* current device */
79
80/* index of each file in the following arrays */
81#define IDX_FIRMWARE 0
82#define IDX_KERNEL 1
Sergei Poselenov489c6962007-02-14 14:30:28 +030083#define IDX_ROOTFS 2
Andrei Safronovcdb97a62006-12-08 16:23:08 +010084
85/* max. number of files which could interest us */
Sergei Poselenov489c6962007-02-14 14:30:28 +030086#define AU_MAXFILES 3
Andrei Safronovcdb97a62006-12-08 16:23:08 +010087
88/* pointers to file names */
Sergei Poselenov489c6962007-02-14 14:30:28 +030089char *aufile[AU_MAXFILES] = {
90 AU_FIRMWARE,
91 AU_KERNEL,
92 AU_ROOTFS
93};
Andrei Safronovcdb97a62006-12-08 16:23:08 +010094
95/* sizes of flash areas for each file */
Sergei Poselenov489c6962007-02-14 14:30:28 +030096long ausize[AU_MAXFILES] = {
97 (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST,
Wolfgang Denkf5fcc3c2007-02-19 23:09:51 +010098 (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST,
99 (AU_FL_ROOTFS_ND + 1) - AU_FL_ROOTFS_ST,
Sergei Poselenov489c6962007-02-14 14:30:28 +0300100};
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100101
102/* array of flash areas start and end addresses */
Sergei Poselenov489c6962007-02-14 14:30:28 +0300103struct flash_layout aufl_layout[AU_MAXFILES] = {
Wolfgang Denkf5fcc3c2007-02-19 23:09:51 +0100104 { AU_FL_FIRMWARE_ST, AU_FL_FIRMWARE_ND, },
105 { AU_FL_KERNEL_ST, AU_FL_KERNEL_ND, },
106 { AU_FL_ROOTFS_ST, AU_FL_ROOTFS_ND, },
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100107};
108
Sergei Poselenov638dd142007-02-27 12:40:16 +0300109ulong totsize;
110
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100111/* where to load files into memory */
112#define LOAD_ADDR ((unsigned char *)0x00200000)
113
Wolfgang Denkf5fcc3c2007-02-19 23:09:51 +0100114/* the root file system is the largest image */
Sergei Poselenov489c6962007-02-14 14:30:28 +0300115#define MAX_LOADSZ ausize[IDX_ROOTFS]
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100116
117/*i2c address of the keypad status*/
118#define I2C_PSOC_KEYPAD_ADDR 0x53
119
120/* keypad mask */
Wolfgang Denk787fa152007-01-10 01:28:39 +0100121#define KEYPAD_ROW 2
122#define KEYPAD_COL 2
123#define KEYPAD_MASK_LO ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))&0xFF)
124#define KEYPAD_MASK_HI ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))>>8)
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100125
126/* externals */
127extern int fat_register_device(block_dev_desc_t *, int);
128extern int file_fat_detectfs(void);
129extern long file_fat_read(const char *, void *, unsigned long);
130extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
131extern int flash_sect_erase(ulong, ulong);
132extern int flash_sect_protect (int, ulong, ulong);
133extern int flash_write (char *, ulong, ulong);
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100134extern int u_boot_hush_start(void);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300135#ifdef CONFIG_PROGRESSBAR
136extern void show_progress(int, int);
137extern void lcd_puts (char *);
138extern void lcd_enable(void);
139#endif
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100140
Wolfgang Denk82e52362006-12-22 10:30:26 +0100141int au_check_cksum_valid(int idx, long nbytes)
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100142{
143 image_header_t *hdr;
144 unsigned long checksum;
145
146 hdr = (image_header_t *)LOAD_ADDR;
147
Wolfgang Denk82e52362006-12-22 10:30:26 +0100148 if (nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size))) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100149 printf ("Image %s bad total SIZE\n", aufile[idx]);
150 return -1;
151 }
152 /* check the data CRC */
153 checksum = ntohl(hdr->ih_dcrc);
154
Wolfgang Denk82e52362006-12-22 10:30:26 +0100155 if (crc32 (0, (uchar *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size)) != checksum) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100156 printf ("Image %s bad data checksum\n", aufile[idx]);
157 return -1;
158 }
159 return 0;
160}
161
Wolfgang Denk82e52362006-12-22 10:30:26 +0100162int au_check_header_valid(int idx, long nbytes)
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100163{
164 image_header_t *hdr;
165 unsigned long checksum;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100166
167 hdr = (image_header_t *)LOAD_ADDR;
168 /* check the easy ones first */
169#undef CHECK_VALID_DEBUG
170#ifdef CHECK_VALID_DEBUG
171 printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
172 printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_ARM);
173 printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
174 printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
175#endif
Wolfgang Denk82e52362006-12-22 10:30:26 +0100176 if (nbytes < sizeof(*hdr)) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100177 printf ("Image %s bad header SIZE\n", aufile[idx]);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300178 ausize[idx] = 0;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100179 return -1;
180 }
Wolfgang Denk82e52362006-12-22 10:30:26 +0100181 if (ntohl(hdr->ih_magic) != IH_MAGIC || hdr->ih_arch != IH_CPU_PPC) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100182 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300183 ausize[idx] = 0;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100184 return -1;
185 }
186 /* check the hdr CRC */
187 checksum = ntohl(hdr->ih_hcrc);
188 hdr->ih_hcrc = 0;
189
190 if (crc32 (0, (uchar *)hdr, sizeof(*hdr)) != checksum) {
191 printf ("Image %s bad header checksum\n", aufile[idx]);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300192 ausize[idx] = 0;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100193 return -1;
194 }
195 hdr->ih_hcrc = htonl(checksum);
196 /* check the type - could do this all in one gigantic if() */
197 if ((idx == IDX_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
198 printf ("Image %s wrong type\n", aufile[idx]);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300199 ausize[idx] = 0;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100200 return -1;
201 }
202 if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
203 printf ("Image %s wrong type\n", aufile[idx]);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300204 ausize[idx] = 0;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100205 return -1;
206 }
Wolfgang Denkf5fcc3c2007-02-19 23:09:51 +0100207 if ((idx == IDX_ROOTFS) &&
208 ( (hdr->ih_type != IH_TYPE_RAMDISK) || (hdr->ih_type != IH_TYPE_FILESYSTEM) )
209 ) {
Sergei Poselenov489c6962007-02-14 14:30:28 +0300210 printf ("Image %s wrong type\n", aufile[idx]);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300211 ausize[idx] = 0;
Sergei Poselenov489c6962007-02-14 14:30:28 +0300212 return -1;
213 }
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100214 /* recycle checksum */
215 checksum = ntohl(hdr->ih_size);
216 /* for kernel and app the image header must also fit into flash */
217 if (idx != IDX_FIRMWARE)
218 checksum += sizeof(*hdr);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300219
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100220 /* check the size does not exceed space in flash. HUSH scripts */
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100221 if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
222 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300223 ausize[idx] = 0;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100224 return -1;
225 }
Sergei Poselenov638dd142007-02-27 12:40:16 +0300226 /* Update with the real filesize */
227 ausize[idx] = (idx == IDX_FIRMWARE ? checksum + sizeof(*hdr) : checksum);
228
229 return checksum; /* return size to be written to flash */
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100230}
231
Wolfgang Denk82e52362006-12-22 10:30:26 +0100232int au_do_update(int idx, long sz)
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100233{
234 image_header_t *hdr;
235 char *addr;
236 long start, end;
237 int off, rc;
238 uint nbytes;
239
240 hdr = (image_header_t *)LOAD_ADDR;
241
242 /* execute a script */
243 if (hdr->ih_type == IH_TYPE_SCRIPT) {
244 addr = (char *)((char *)hdr + sizeof(*hdr));
245 /* stick a NULL at the end of the script, otherwise */
246 /* parse_string_outer() runs off the end. */
247 addr[ntohl(hdr->ih_size)] = 0;
248 addr += 8;
249 parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
250 return 0;
251 }
252
253 start = aufl_layout[idx].start;
254 end = aufl_layout[idx].end;
255
256 /* unprotect the address range */
257 /* this assumes that ONLY the firmware is protected! */
258 if (idx == IDX_FIRMWARE) {
259#undef AU_UPDATE_TEST
260#ifdef AU_UPDATE_TEST
261 /* erase it where Linux goes */
262 start = aufl_layout[1].start;
263 end = aufl_layout[1].end;
264#endif
265 flash_sect_protect(0, start, end);
266 }
267
268 /*
269 * erase the address range.
270 */
271 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
272 flash_sect_erase(start, end);
273 wait_ms(100);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300274#ifdef CONFIG_PROGRESSBAR
275 show_progress(end - start, totsize);
276#endif
277
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100278 /* strip the header - except for the kernel and ramdisk */
Sergei Poselenov489c6962007-02-14 14:30:28 +0300279 if (hdr->ih_type == IH_TYPE_KERNEL || hdr->ih_type == IH_TYPE_RAMDISK) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100280 addr = (char *)hdr;
281 off = sizeof(*hdr);
282 nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
283 } else {
284 addr = (char *)((char *)hdr + sizeof(*hdr));
285#ifdef AU_UPDATE_TEST
286 /* copy it to where Linux goes */
287 if (idx == IDX_FIRMWARE)
288 start = aufl_layout[1].start;
289#endif
290 off = 0;
291 nbytes = ntohl(hdr->ih_size);
292 }
293
294 /* copy the data from RAM to FLASH */
295 debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
296 rc = flash_write(addr, start, nbytes);
297 if (rc != 0) {
298 printf("Flashing failed due to error %d\n", rc);
299 return -1;
300 }
301
Sergei Poselenov638dd142007-02-27 12:40:16 +0300302#ifdef CONFIG_PROGRESSBAR
303 show_progress(nbytes, totsize);
304#endif
305
Wolfgang Denkf5fcc3c2007-02-19 23:09:51 +0100306 /* check the data CRC of the copy */
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100307 if (crc32 (0, (uchar *)(start + off), ntohl(hdr->ih_size)) != ntohl(hdr->ih_dcrc)) {
Wolfgang Denkf5fcc3c2007-02-19 23:09:51 +0100308 printf ("Image %s Bad Data Checksum after COPY\n", aufile[idx]);
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100309 return -1;
310 }
311
312 /* protect the address range */
313 /* this assumes that ONLY the firmware is protected! */
314 if (idx == IDX_FIRMWARE)
315 flash_sect_protect(1, start, end);
316 return 0;
317}
318
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100319/*
320 * this is called from board_init() after the hardware has been set up
321 * and is usable. That seems like a good time to do this.
322 * Right now the return value is ignored.
323 */
Wolfgang Denk82e52362006-12-22 10:30:26 +0100324int do_auto_update(void)
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100325{
326 block_dev_desc_t *stor_dev;
327 long sz;
Sergei Poselenov638dd142007-02-27 12:40:16 +0300328 int i, res = 0, bitmap_first, cnt, old_ctrlc, got_ctrlc;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100329 char *env;
330 long start, end;
Sergei Poselenov489c6962007-02-14 14:30:28 +0300331 uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100332
333 /*
334 * Read keypad status
335 */
336 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
337 wait_ms(500);
338 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
339
340 /*
341 * Check keypad
342 */
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100343 if ( !(keypad_status1[1] & KEYPAD_MASK_LO) ||
344 (keypad_status1[1] != keypad_status2[1])) {
345 return 0;
346 }
Sergei Poselenov638dd142007-02-27 12:40:16 +0300347
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100348 au_usb_stor_curr_dev = -1;
349 /* start USB */
350 if (usb_stop() < 0) {
351 debug ("usb_stop failed\n");
352 return -1;
353 }
354 if (usb_init() < 0) {
355 debug ("usb_init failed\n");
356 return -1;
357 }
358 /*
359 * check whether a storage device is attached (assume that it's
360 * a USB memory stick, since nothing else should be attached).
361 */
362 au_usb_stor_curr_dev = usb_stor_scan(0);
363 if (au_usb_stor_curr_dev == -1) {
364 debug ("No device found. Not initialized?\n");
365 return -1;
366 }
367 /* check whether it has a partition table */
368 stor_dev = get_dev("usb", 0);
369 if (stor_dev == NULL) {
370 debug ("uknown device type\n");
371 return -1;
372 }
373 if (fat_register_device(stor_dev, 1) != 0) {
374 debug ("Unable to use USB %d:%d for fatls\n",
375 au_usb_stor_curr_dev, 1);
376 return -1;
377 }
378 if (file_fat_detectfs() != 0) {
379 debug ("file_fat_detectfs failed\n");
380 }
381
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100382 /*
383 * now check whether start and end are defined using environment
384 * variables.
385 */
386 start = -1;
387 end = 0;
388 env = getenv("firmware_st");
389 if (env != NULL)
390 start = simple_strtoul(env, NULL, 16);
391 env = getenv("firmware_nd");
392 if (env != NULL)
393 end = simple_strtoul(env, NULL, 16);
394 if (start >= 0 && end && end > start) {
395 ausize[IDX_FIRMWARE] = (end + 1) - start;
Sergei Poselenov489c6962007-02-14 14:30:28 +0300396 aufl_layout[IDX_FIRMWARE].start = start;
397 aufl_layout[IDX_FIRMWARE].end = end;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100398 }
399 start = -1;
400 end = 0;
401 env = getenv("kernel_st");
402 if (env != NULL)
403 start = simple_strtoul(env, NULL, 16);
404 env = getenv("kernel_nd");
405 if (env != NULL)
406 end = simple_strtoul(env, NULL, 16);
407 if (start >= 0 && end && end > start) {
408 ausize[IDX_KERNEL] = (end + 1) - start;
Sergei Poselenov489c6962007-02-14 14:30:28 +0300409 aufl_layout[IDX_KERNEL].start = start;
410 aufl_layout[IDX_KERNEL].end = end;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100411 }
Sergei Poselenov489c6962007-02-14 14:30:28 +0300412 start = -1;
413 end = 0;
414 env = getenv("rootfs_st");
415 if (env != NULL)
416 start = simple_strtoul(env, NULL, 16);
417 env = getenv("rootfs_nd");
418 if (env != NULL)
419 end = simple_strtoul(env, NULL, 16);
420 if (start >= 0 && end && end > start) {
421 ausize[IDX_ROOTFS] = (end + 1) - start;
422 aufl_layout[IDX_ROOTFS].start = start;
423 aufl_layout[IDX_ROOTFS].end = end;
424 }
425
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100426 /* make certain that HUSH is runnable */
427 u_boot_hush_start();
428 /* make sure that we see CTRL-C and save the old state */
429 old_ctrlc = disable_ctrlc(0);
430
431 bitmap_first = 0;
Sergei Poselenov638dd142007-02-27 12:40:16 +0300432
433 /* validate the images first */
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100434 for (i = 0; i < AU_MAXFILES; i++) {
Sergei Poselenov638dd142007-02-27 12:40:16 +0300435 ulong imsize;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100436 /* just read the header */
437 sz = file_fat_read(aufile[i], LOAD_ADDR, sizeof(image_header_t));
438 debug ("read %s sz %ld hdr %d\n",
439 aufile[i], sz, sizeof(image_header_t));
440 if (sz <= 0 || sz < sizeof(image_header_t)) {
441 debug ("%s not found\n", aufile[i]);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300442 ausize[i] = 0;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100443 continue;
444 }
Sergei Poselenov638dd142007-02-27 12:40:16 +0300445 /* au_check_header_valid() updates ausize[] */
446 if ((imsize = au_check_header_valid(i, sz)) < 0) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100447 debug ("%s header not valid\n", aufile[i]);
448 continue;
449 }
Sergei Poselenov638dd142007-02-27 12:40:16 +0300450 /* totsize accounts for image size and flash erase size */
451 totsize += (imsize + (aufl_layout[i].end - aufl_layout[i].start));
452 }
453
454#ifdef CONFIG_PROGRESSBAR
455 if (totsize) {
456 lcd_puts(" Update in progress\n");
457 lcd_enable();
458 }
459#endif
460
461 /* just loop thru all the possible files */
462 for (i = 0; i < AU_MAXFILES && totsize; i++) {
463 if (!ausize[i]) {
464 continue;
465 }
466 sz = file_fat_read(aufile[i], LOAD_ADDR, ausize[i]);
467
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100468 debug ("read %s sz %ld hdr %d\n",
469 aufile[i], sz, sizeof(image_header_t));
Sergei Poselenov638dd142007-02-27 12:40:16 +0300470
471 if (sz != ausize[i]) {
472 printf ("%s: size %d read %d?\n", aufile[i], ausize[i], sz);
473 continue;
474 }
475
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100476 if (sz <= 0 || sz <= sizeof(image_header_t)) {
477 debug ("%s not found\n", aufile[i]);
478 continue;
479 }
480 if (au_check_cksum_valid(i, sz) < 0) {
481 debug ("%s checksum not valid\n", aufile[i]);
482 continue;
483 }
484 /* this is really not a good idea, but it's what the */
485 /* customer wants. */
486 cnt = 0;
487 got_ctrlc = 0;
488 do {
489 res = au_do_update(i, sz);
490 /* let the user break out of the loop */
491 if (ctrlc() || had_ctrlc()) {
492 clear_ctrlc();
493 if (res < 0)
494 got_ctrlc = 1;
495 break;
496 }
497 cnt++;
498#ifdef AU_TEST_ONLY
Sergei Poselenov489c6962007-02-14 14:30:28 +0300499 } while (res < 0 && cnt < (AU_MAXFILES + 1));
500 if (cnt < (AU_MAXFILES + 1))
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100501#else
502 } while (res < 0);
503#endif
504 }
505 usb_stop();
506 /* restore the old state */
507 disable_ctrlc(old_ctrlc);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300508#ifdef CONFIG_PROGRESSBAR
509 if (totsize) {
510 if (!res) {
511 lcd_puts("\n Update completed\n");
512 } else {
513 lcd_puts("\n Update error\n");
514 }
515 lcd_enable();
516 }
517#endif
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100518 return 0;
519}
520#endif /* CONFIG_AUTO_UPDATE */