blob: 2f622b0846e103f5b3036322269e5577ca451427 [file] [log] [blame]
Andrei Safronov82336242006-12-08 16:23:08 +01001/*
2 * (C) Copyright 2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Andrei Safronov82336242006-12-08 16:23:08 +01006 */
7#include <common.h>
8#include <command.h>
9#include <malloc.h>
10#include <image.h>
11#include <asm/byteorder.h>
12#include <usb.h>
Grant Likelyffc2dd72007-02-20 09:04:34 +010013#include <part.h>
Andrei Safronov82336242006-12-08 16:23:08 +010014
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020015#ifdef CONFIG_SYS_HUSH_PARSER
Andrei Safronov82336242006-12-08 16:23:08 +010016#include <hush.h>
17#endif
18
19
20#ifdef CONFIG_AUTO_UPDATE
21
22#ifndef CONFIG_USB_OHCI
23#error "must define CONFIG_USB_OHCI"
24#endif
25
26#ifndef CONFIG_USB_STORAGE
27#error "must define CONFIG_USB_STORAGE"
28#endif
29
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020030#ifndef CONFIG_SYS_HUSH_PARSER
31#error "must define CONFIG_SYS_HUSH_PARSER"
Andrei Safronov82336242006-12-08 16:23:08 +010032#endif
33
Jon Loeliger145318c2007-07-09 18:38:39 -050034#if !defined(CONFIG_CMD_FAT)
Jon Loeliger761ea742007-07-10 10:48:22 -050035#error "must define CONFIG_CMD_FAT"
Andrei Safronov82336242006-12-08 16:23:08 +010036#endif
37
Andrei Safronov82336242006-12-08 16:23:08 +010038#undef AU_DEBUG
39
40#undef debug
41#ifdef AU_DEBUG
42#define debug(fmt,args...) printf (fmt ,##args)
43#else
44#define debug(fmt,args...)
45#endif /* AU_DEBUG */
46
47/* possible names of files on the USB stick. */
48#define AU_FIRMWARE "u-boot.img"
49#define AU_KERNEL "kernel.img"
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030050#define AU_ROOTFS "rootfs.img"
Andrei Safronov82336242006-12-08 16:23:08 +010051
Wolfgang Denk8e59e1f2006-12-22 10:30:26 +010052struct flash_layout {
Andrei Safronov82336242006-12-08 16:23:08 +010053 long start;
54 long end;
55};
56
57/* layout of the FLASH. ST = start address, ND = end address. */
58#define AU_FL_FIRMWARE_ST 0xfC000000
59#define AU_FL_FIRMWARE_ND 0xfC03FFFF
60#define AU_FL_KERNEL_ST 0xfC0C0000
61#define AU_FL_KERNEL_ND 0xfC1BFFFF
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030062#define AU_FL_ROOTFS_ST 0xFC1C0000
63#define AU_FL_ROOTFS_ND 0xFCFBFFFF
Andrei Safronov82336242006-12-08 16:23:08 +010064
65static int au_usb_stor_curr_dev; /* current device */
66
67/* index of each file in the following arrays */
68#define IDX_FIRMWARE 0
69#define IDX_KERNEL 1
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030070#define IDX_ROOTFS 2
Andrei Safronov82336242006-12-08 16:23:08 +010071
72/* max. number of files which could interest us */
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030073#define AU_MAXFILES 3
Andrei Safronov82336242006-12-08 16:23:08 +010074
75/* pointers to file names */
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030076char *aufile[AU_MAXFILES] = {
77 AU_FIRMWARE,
78 AU_KERNEL,
79 AU_ROOTFS
80};
Andrei Safronov82336242006-12-08 16:23:08 +010081
82/* sizes of flash areas for each file */
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030083long ausize[AU_MAXFILES] = {
84 (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST,
Wolfgang Denkf4da2ef2007-02-19 23:09:51 +010085 (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST,
86 (AU_FL_ROOTFS_ND + 1) - AU_FL_ROOTFS_ST,
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030087};
Andrei Safronov82336242006-12-08 16:23:08 +010088
89/* array of flash areas start and end addresses */
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030090struct flash_layout aufl_layout[AU_MAXFILES] = {
Wolfgang Denkf4da2ef2007-02-19 23:09:51 +010091 { AU_FL_FIRMWARE_ST, AU_FL_FIRMWARE_ND, },
92 { AU_FL_KERNEL_ST, AU_FL_KERNEL_ND, },
93 { AU_FL_ROOTFS_ST, AU_FL_ROOTFS_ND, },
Andrei Safronov82336242006-12-08 16:23:08 +010094};
95
Sergei Poselenov91904cd2007-02-27 12:40:16 +030096ulong totsize;
97
Andrei Safronov82336242006-12-08 16:23:08 +010098/* where to load files into memory */
99#define LOAD_ADDR ((unsigned char *)0x00200000)
100
Wolfgang Denkf4da2ef2007-02-19 23:09:51 +0100101/* the root file system is the largest image */
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +0300102#define MAX_LOADSZ ausize[IDX_ROOTFS]
Andrei Safronov82336242006-12-08 16:23:08 +0100103
104/*i2c address of the keypad status*/
105#define I2C_PSOC_KEYPAD_ADDR 0x53
106
107/* keypad mask */
Wolfgang Denk299d6102007-01-10 01:28:39 +0100108#define KEYPAD_ROW 2
109#define KEYPAD_COL 2
110#define KEYPAD_MASK_LO ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))&0xFF)
111#define KEYPAD_MASK_HI ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))>>8)
Andrei Safronov82336242006-12-08 16:23:08 +0100112
113/* externals */
114extern int fat_register_device(block_dev_desc_t *, int);
115extern int file_fat_detectfs(void);
116extern long file_fat_read(const char *, void *, unsigned long);
117extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
118extern int flash_sect_erase(ulong, ulong);
119extern int flash_sect_protect (int, ulong, ulong);
120extern int flash_write (char *, ulong, ulong);
Andrei Safronov82336242006-12-08 16:23:08 +0100121extern int u_boot_hush_start(void);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300122#ifdef CONFIG_PROGRESSBAR
123extern void show_progress(int, int);
124extern void lcd_puts (char *);
125extern void lcd_enable(void);
126#endif
Andrei Safronov82336242006-12-08 16:23:08 +0100127
Wolfgang Denk8e59e1f2006-12-22 10:30:26 +0100128int au_check_cksum_valid(int idx, long nbytes)
Andrei Safronov82336242006-12-08 16:23:08 +0100129{
130 image_header_t *hdr;
Andrei Safronov82336242006-12-08 16:23:08 +0100131
132 hdr = (image_header_t *)LOAD_ADDR;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100133#if defined(CONFIG_FIT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100134 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100135 puts ("Non legacy image format not supported\n");
136 return -1;
137 }
138#endif
Andrei Safronov82336242006-12-08 16:23:08 +0100139
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100140 if (nbytes != image_get_image_size (hdr)) {
Andrei Safronov82336242006-12-08 16:23:08 +0100141 printf ("Image %s bad total SIZE\n", aufile[idx]);
142 return -1;
143 }
144 /* check the data CRC */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100145 if (!image_check_dcrc (hdr)) {
Andrei Safronov82336242006-12-08 16:23:08 +0100146 printf ("Image %s bad data checksum\n", aufile[idx]);
147 return -1;
148 }
149 return 0;
150}
151
Wolfgang Denk8e59e1f2006-12-22 10:30:26 +0100152int au_check_header_valid(int idx, long nbytes)
Andrei Safronov82336242006-12-08 16:23:08 +0100153{
154 image_header_t *hdr;
Sergei Poselenov854fe362007-02-27 20:15:30 +0300155 unsigned long checksum, fsize;
Andrei Safronov82336242006-12-08 16:23:08 +0100156
157 hdr = (image_header_t *)LOAD_ADDR;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100158#if defined(CONFIG_FIT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100159 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100160 puts ("Non legacy image format not supported\n");
161 return -1;
162 }
163#endif
164
Andrei Safronov82336242006-12-08 16:23:08 +0100165 /* check the easy ones first */
166#undef CHECK_VALID_DEBUG
167#ifdef CHECK_VALID_DEBUG
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100168 printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC);
169 printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_ARM);
170 printf("size %#x %#lx ", image_get_data_size (hdr), nbytes);
171 printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL);
Andrei Safronov82336242006-12-08 16:23:08 +0100172#endif
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100173 if (nbytes < image_get_header_size ()) {
Andrei Safronov82336242006-12-08 16:23:08 +0100174 printf ("Image %s bad header SIZE\n", aufile[idx]);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300175 ausize[idx] = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100176 return -1;
177 }
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100178 if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) {
Andrei Safronov82336242006-12-08 16:23:08 +0100179 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300180 ausize[idx] = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100181 return -1;
182 }
183 /* check the hdr CRC */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100184 if (!image_check_hcrc (hdr)) {
Andrei Safronov82336242006-12-08 16:23:08 +0100185 printf ("Image %s bad header checksum\n", aufile[idx]);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300186 ausize[idx] = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100187 return -1;
188 }
Andrei Safronov82336242006-12-08 16:23:08 +0100189 /* check the type - could do this all in one gigantic if() */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100190 if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
Andrei Safronov82336242006-12-08 16:23:08 +0100191 printf ("Image %s wrong type\n", aufile[idx]);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300192 ausize[idx] = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100193 return -1;
194 }
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100195 if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) {
Andrei Safronov82336242006-12-08 16:23:08 +0100196 printf ("Image %s wrong type\n", aufile[idx]);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300197 ausize[idx] = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100198 return -1;
199 }
Wolfgang Denkf4da2ef2007-02-19 23:09:51 +0100200 if ((idx == IDX_ROOTFS) &&
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100201 (!image_check_type (hdr, IH_TYPE_RAMDISK) &&
202 !image_check_type (hdr, IH_TYPE_FILESYSTEM))) {
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +0300203 printf ("Image %s wrong type\n", aufile[idx]);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300204 ausize[idx] = 0;
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +0300205 return -1;
206 }
Andrei Safronov82336242006-12-08 16:23:08 +0100207 /* recycle checksum */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100208 checksum = image_get_data_size (hdr);
Sergei Poselenov854fe362007-02-27 20:15:30 +0300209
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100210 fsize = checksum + image_get_header_size ();
Sergei Poselenov854fe362007-02-27 20:15:30 +0300211 /* for kernel and ramdisk the image header must also fit into flash */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100212 if (idx == IDX_KERNEL || image_check_type (hdr, IH_TYPE_RAMDISK))
213 checksum += image_get_header_size ();
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300214
Andrei Safronov82336242006-12-08 16:23:08 +0100215 /* check the size does not exceed space in flash. HUSH scripts */
Andrei Safronov82336242006-12-08 16:23:08 +0100216 if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
217 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300218 ausize[idx] = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100219 return -1;
220 }
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300221 /* Update with the real filesize */
Sergei Poselenov854fe362007-02-27 20:15:30 +0300222 ausize[idx] = fsize;
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300223
224 return checksum; /* return size to be written to flash */
Andrei Safronov82336242006-12-08 16:23:08 +0100225}
226
Wolfgang Denk8e59e1f2006-12-22 10:30:26 +0100227int au_do_update(int idx, long sz)
Andrei Safronov82336242006-12-08 16:23:08 +0100228{
229 image_header_t *hdr;
230 char *addr;
231 long start, end;
232 int off, rc;
233 uint nbytes;
234
235 hdr = (image_header_t *)LOAD_ADDR;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100236#if defined(CONFIG_FIT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100237 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100238 puts ("Non legacy image format not supported\n");
239 return -1;
240 }
241#endif
Andrei Safronov82336242006-12-08 16:23:08 +0100242
243 /* execute a script */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100244 if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
245 addr = (char *)((char *)hdr + image_get_header_size ());
Andrei Safronov82336242006-12-08 16:23:08 +0100246 /* stick a NULL at the end of the script, otherwise */
247 /* parse_string_outer() runs off the end. */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100248 addr[image_get_data_size (hdr)] = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100249 addr += 8;
250 parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
251 return 0;
252 }
253
254 start = aufl_layout[idx].start;
255 end = aufl_layout[idx].end;
256
257 /* unprotect the address range */
258 /* this assumes that ONLY the firmware is protected! */
259 if (idx == IDX_FIRMWARE) {
260#undef AU_UPDATE_TEST
261#ifdef AU_UPDATE_TEST
262 /* erase it where Linux goes */
263 start = aufl_layout[1].start;
264 end = aufl_layout[1].end;
265#endif
266 flash_sect_protect(0, start, end);
267 }
268
269 /*
270 * erase the address range.
271 */
272 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
273 flash_sect_erase(start, end);
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000274 mdelay(100);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300275#ifdef CONFIG_PROGRESSBAR
276 show_progress(end - start, totsize);
277#endif
278
Andrei Safronov82336242006-12-08 16:23:08 +0100279 /* strip the header - except for the kernel and ramdisk */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100280 if (image_check_type (hdr, IH_TYPE_KERNEL) ||
281 image_check_type (hdr, IH_TYPE_RAMDISK)) {
Andrei Safronov82336242006-12-08 16:23:08 +0100282 addr = (char *)hdr;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100283 off = image_get_header_size ();
284 nbytes = image_get_image_size (hdr);
Andrei Safronov82336242006-12-08 16:23:08 +0100285 } else {
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100286 addr = (char *)((char *)hdr + image_get_header_size ());
Andrei Safronov82336242006-12-08 16:23:08 +0100287#ifdef AU_UPDATE_TEST
288 /* copy it to where Linux goes */
289 if (idx == IDX_FIRMWARE)
290 start = aufl_layout[1].start;
291#endif
292 off = 0;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100293 nbytes = image_get_data_size (hdr);
Andrei Safronov82336242006-12-08 16:23:08 +0100294 }
295
296 /* copy the data from RAM to FLASH */
297 debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
298 rc = flash_write(addr, start, nbytes);
299 if (rc != 0) {
300 printf("Flashing failed due to error %d\n", rc);
301 return -1;
302 }
303
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300304#ifdef CONFIG_PROGRESSBAR
305 show_progress(nbytes, totsize);
306#endif
307
Wolfgang Denkf4da2ef2007-02-19 23:09:51 +0100308 /* check the data CRC of the copy */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100309 if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) !=
310 image_get_dcrc (hdr)) {
Wolfgang Denkf4da2ef2007-02-19 23:09:51 +0100311 printf ("Image %s Bad Data Checksum after COPY\n", aufile[idx]);
Andrei Safronov82336242006-12-08 16:23:08 +0100312 return -1;
313 }
314
315 /* protect the address range */
316 /* this assumes that ONLY the firmware is protected! */
317 if (idx == IDX_FIRMWARE)
318 flash_sect_protect(1, start, end);
319 return 0;
320}
321
Andrei Safronov82336242006-12-08 16:23:08 +0100322/*
323 * this is called from board_init() after the hardware has been set up
324 * and is usable. That seems like a good time to do this.
325 * Right now the return value is ignored.
326 */
Wolfgang Denk8e59e1f2006-12-22 10:30:26 +0100327int do_auto_update(void)
Andrei Safronov82336242006-12-08 16:23:08 +0100328{
329 block_dev_desc_t *stor_dev;
330 long sz;
Wolfgang Denk8af49192011-11-04 15:55:11 +0000331 int i, res = 0, cnt, old_ctrlc;
Andrei Safronov82336242006-12-08 16:23:08 +0100332 char *env;
333 long start, end;
Wolfgang Denk036384c2007-08-12 15:11:38 +0200334
335#if 0 /* disable key-press detection to speed up boot-up time */
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +0300336 uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
Andrei Safronov82336242006-12-08 16:23:08 +0100337
338 /*
339 * Read keypad status
340 */
341 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000342 mdelay(500);
Andrei Safronov82336242006-12-08 16:23:08 +0100343 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
344
345 /*
346 * Check keypad
347 */
Andrei Safronov82336242006-12-08 16:23:08 +0100348 if ( !(keypad_status1[1] & KEYPAD_MASK_LO) ||
349 (keypad_status1[1] != keypad_status2[1])) {
350 return 0;
351 }
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300352
Wolfgang Denk036384c2007-08-12 15:11:38 +0200353#endif
Andrei Safronov82336242006-12-08 16:23:08 +0100354 au_usb_stor_curr_dev = -1;
355 /* start USB */
356 if (usb_stop() < 0) {
357 debug ("usb_stop failed\n");
358 return -1;
359 }
360 if (usb_init() < 0) {
361 debug ("usb_init failed\n");
362 return -1;
363 }
364 /*
365 * check whether a storage device is attached (assume that it's
366 * a USB memory stick, since nothing else should be attached).
367 */
368 au_usb_stor_curr_dev = usb_stor_scan(0);
369 if (au_usb_stor_curr_dev == -1) {
370 debug ("No device found. Not initialized?\n");
Wolfgang Denk036384c2007-08-12 15:11:38 +0200371 res = -1;
372 goto xit;
Andrei Safronov82336242006-12-08 16:23:08 +0100373 }
374 /* check whether it has a partition table */
375 stor_dev = get_dev("usb", 0);
376 if (stor_dev == NULL) {
377 debug ("uknown device type\n");
Wolfgang Denk036384c2007-08-12 15:11:38 +0200378 res = -1;
379 goto xit;
Andrei Safronov82336242006-12-08 16:23:08 +0100380 }
381 if (fat_register_device(stor_dev, 1) != 0) {
382 debug ("Unable to use USB %d:%d for fatls\n",
383 au_usb_stor_curr_dev, 1);
Wolfgang Denk036384c2007-08-12 15:11:38 +0200384 res = -1;
385 goto xit;
Andrei Safronov82336242006-12-08 16:23:08 +0100386 }
387 if (file_fat_detectfs() != 0) {
388 debug ("file_fat_detectfs failed\n");
389 }
390
Andrei Safronov82336242006-12-08 16:23:08 +0100391 /*
392 * now check whether start and end are defined using environment
393 * variables.
394 */
395 start = -1;
396 end = 0;
397 env = getenv("firmware_st");
398 if (env != NULL)
399 start = simple_strtoul(env, NULL, 16);
400 env = getenv("firmware_nd");
401 if (env != NULL)
402 end = simple_strtoul(env, NULL, 16);
403 if (start >= 0 && end && end > start) {
404 ausize[IDX_FIRMWARE] = (end + 1) - start;
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +0300405 aufl_layout[IDX_FIRMWARE].start = start;
406 aufl_layout[IDX_FIRMWARE].end = end;
Andrei Safronov82336242006-12-08 16:23:08 +0100407 }
408 start = -1;
409 end = 0;
410 env = getenv("kernel_st");
411 if (env != NULL)
412 start = simple_strtoul(env, NULL, 16);
413 env = getenv("kernel_nd");
414 if (env != NULL)
415 end = simple_strtoul(env, NULL, 16);
416 if (start >= 0 && end && end > start) {
417 ausize[IDX_KERNEL] = (end + 1) - start;
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +0300418 aufl_layout[IDX_KERNEL].start = start;
419 aufl_layout[IDX_KERNEL].end = end;
Andrei Safronov82336242006-12-08 16:23:08 +0100420 }
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +0300421 start = -1;
422 end = 0;
423 env = getenv("rootfs_st");
424 if (env != NULL)
425 start = simple_strtoul(env, NULL, 16);
426 env = getenv("rootfs_nd");
427 if (env != NULL)
428 end = simple_strtoul(env, NULL, 16);
429 if (start >= 0 && end && end > start) {
430 ausize[IDX_ROOTFS] = (end + 1) - start;
431 aufl_layout[IDX_ROOTFS].start = start;
432 aufl_layout[IDX_ROOTFS].end = end;
433 }
434
Andrei Safronov82336242006-12-08 16:23:08 +0100435 /* make certain that HUSH is runnable */
436 u_boot_hush_start();
437 /* make sure that we see CTRL-C and save the old state */
438 old_ctrlc = disable_ctrlc(0);
439
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100440 /* validate the images first */
Andrei Safronov82336242006-12-08 16:23:08 +0100441 for (i = 0; i < AU_MAXFILES; i++) {
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300442 ulong imsize;
Andrei Safronov82336242006-12-08 16:23:08 +0100443 /* just read the header */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100444 sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ());
Andrei Safronov82336242006-12-08 16:23:08 +0100445 debug ("read %s sz %ld hdr %d\n",
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100446 aufile[i], sz, image_get_header_size ());
447 if (sz <= 0 || sz < image_get_header_size ()) {
Andrei Safronov82336242006-12-08 16:23:08 +0100448 debug ("%s not found\n", aufile[i]);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300449 ausize[i] = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100450 continue;
451 }
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300452 /* au_check_header_valid() updates ausize[] */
453 if ((imsize = au_check_header_valid(i, sz)) < 0) {
Andrei Safronov82336242006-12-08 16:23:08 +0100454 debug ("%s header not valid\n", aufile[i]);
455 continue;
456 }
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300457 /* totsize accounts for image size and flash erase size */
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100458 totsize += (imsize + (aufl_layout[i].end - aufl_layout[i].start));
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300459 }
460
461#ifdef CONFIG_PROGRESSBAR
462 if (totsize) {
463 lcd_puts(" Update in progress\n");
464 lcd_enable();
465 }
466#endif
467
468 /* just loop thru all the possible files */
469 for (i = 0; i < AU_MAXFILES && totsize; i++) {
470 if (!ausize[i]) {
471 continue;
472 }
473 sz = file_fat_read(aufile[i], LOAD_ADDR, ausize[i]);
474
Andrei Safronov82336242006-12-08 16:23:08 +0100475 debug ("read %s sz %ld hdr %d\n",
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100476 aufile[i], sz, image_get_header_size ());
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300477
478 if (sz != ausize[i]) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +0200479 printf ("%s: size %ld read %ld?\n", aufile[i], ausize[i], sz);
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100480 continue;
481 }
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300482
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100483 if (sz <= 0 || sz <= image_get_header_size ()) {
Andrei Safronov82336242006-12-08 16:23:08 +0100484 debug ("%s not found\n", aufile[i]);
485 continue;
486 }
487 if (au_check_cksum_valid(i, sz) < 0) {
488 debug ("%s checksum not valid\n", aufile[i]);
489 continue;
490 }
491 /* this is really not a good idea, but it's what the */
492 /* customer wants. */
493 cnt = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100494 do {
495 res = au_do_update(i, sz);
496 /* let the user break out of the loop */
497 if (ctrlc() || had_ctrlc()) {
498 clear_ctrlc();
Andrei Safronov82336242006-12-08 16:23:08 +0100499 break;
500 }
501 cnt++;
502#ifdef AU_TEST_ONLY
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +0300503 } while (res < 0 && cnt < (AU_MAXFILES + 1));
504 if (cnt < (AU_MAXFILES + 1))
Andrei Safronov82336242006-12-08 16:23:08 +0100505#else
506 } while (res < 0);
507#endif
508 }
Wolfgang Denk036384c2007-08-12 15:11:38 +0200509
Andrei Safronov82336242006-12-08 16:23:08 +0100510 /* restore the old state */
511 disable_ctrlc(old_ctrlc);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300512#ifdef CONFIG_PROGRESSBAR
513 if (totsize) {
514 if (!res) {
515 lcd_puts("\n Update completed\n");
516 } else {
517 lcd_puts("\n Update error\n");
518 }
519 lcd_enable();
520 }
521#endif
Wolfgang Denk036384c2007-08-12 15:11:38 +0200522 xit:
523 usb_stop();
524 return res;
Andrei Safronov82336242006-12-08 16:23:08 +0100525}
526#endif /* CONFIG_AUTO_UPDATE */