blob: 8b520c859942f9a95e6cd71b72b2d5307cc20dbe [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
Jon Loeliger3fe00102007-07-09 18:38:39 -050047#if !defined(CONFIG_CMD_FAT)
Jon Loeligerd39b5742007-07-10 10:48:22 -050048#error "must define CONFIG_CMD_FAT"
Andrei Safronovcdb97a62006-12-08 16:23:08 +010049#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;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100144
145 hdr = (image_header_t *)LOAD_ADDR;
146
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100147 if (nbytes != image_get_image_size (hdr)) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100148 printf ("Image %s bad total SIZE\n", aufile[idx]);
149 return -1;
150 }
151 /* check the data CRC */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100152 if (!image_check_dcrc (hdr)) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100153 printf ("Image %s bad data checksum\n", aufile[idx]);
154 return -1;
155 }
156 return 0;
157}
158
Wolfgang Denk82e52362006-12-22 10:30:26 +0100159int au_check_header_valid(int idx, long nbytes)
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100160{
161 image_header_t *hdr;
Sergei Poselenove3445682007-02-27 20:15:30 +0300162 unsigned long checksum, fsize;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100163
164 hdr = (image_header_t *)LOAD_ADDR;
165 /* check the easy ones first */
166#undef CHECK_VALID_DEBUG
167#ifdef CHECK_VALID_DEBUG
Marian Balakowiczb97a2a02008-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 Safronovcdb97a62006-12-08 16:23:08 +0100172#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100173 if (nbytes < image_get_header_size ()) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100174 printf ("Image %s bad header SIZE\n", aufile[idx]);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300175 ausize[idx] = 0;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100176 return -1;
177 }
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100178 if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100179 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300180 ausize[idx] = 0;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100181 return -1;
182 }
183 /* check the hdr CRC */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100184 if (!image_check_hcrc (hdr)) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100185 printf ("Image %s bad header checksum\n", aufile[idx]);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300186 ausize[idx] = 0;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100187 return -1;
188 }
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100189 /* check the type - could do this all in one gigantic if() */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100190 if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100191 printf ("Image %s wrong type\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 }
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100195 if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100196 printf ("Image %s wrong type\n", aufile[idx]);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300197 ausize[idx] = 0;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100198 return -1;
199 }
Wolfgang Denkf5fcc3c2007-02-19 23:09:51 +0100200 if ((idx == IDX_ROOTFS) &&
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100201 (!image_check_type (hdr, IH_TYPE_RAMDISK) &&
202 !image_check_type (hdr, IH_TYPE_FILESYSTEM))) {
Sergei Poselenov489c6962007-02-14 14:30:28 +0300203 printf ("Image %s wrong type\n", aufile[idx]);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300204 ausize[idx] = 0;
Sergei Poselenov489c6962007-02-14 14:30:28 +0300205 return -1;
206 }
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100207 /* recycle checksum */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100208 checksum = image_get_data_size (hdr);
Sergei Poselenove3445682007-02-27 20:15:30 +0300209
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100210 fsize = checksum + image_get_header_size ();
Sergei Poselenove3445682007-02-27 20:15:30 +0300211 /* for kernel and ramdisk the image header must also fit into flash */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100212 if (idx == IDX_KERNEL || image_check_type (hdr, IH_TYPE_RAMDISK))
213 checksum += image_get_header_size ();
Sergei Poselenov638dd142007-02-27 12:40:16 +0300214
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100215 /* check the size does not exceed space in flash. HUSH scripts */
Andrei Safronovcdb97a62006-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 Poselenov638dd142007-02-27 12:40:16 +0300218 ausize[idx] = 0;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100219 return -1;
220 }
Sergei Poselenov638dd142007-02-27 12:40:16 +0300221 /* Update with the real filesize */
Sergei Poselenove3445682007-02-27 20:15:30 +0300222 ausize[idx] = fsize;
Sergei Poselenov638dd142007-02-27 12:40:16 +0300223
224 return checksum; /* return size to be written to flash */
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100225}
226
Wolfgang Denk82e52362006-12-22 10:30:26 +0100227int au_do_update(int idx, long sz)
Andrei Safronovcdb97a62006-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;
236
237 /* execute a script */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100238 if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
239 addr = (char *)((char *)hdr + image_get_header_size ());
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100240 /* stick a NULL at the end of the script, otherwise */
241 /* parse_string_outer() runs off the end. */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100242 addr[image_get_data_size (hdr)] = 0;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100243 addr += 8;
244 parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
245 return 0;
246 }
247
248 start = aufl_layout[idx].start;
249 end = aufl_layout[idx].end;
250
251 /* unprotect the address range */
252 /* this assumes that ONLY the firmware is protected! */
253 if (idx == IDX_FIRMWARE) {
254#undef AU_UPDATE_TEST
255#ifdef AU_UPDATE_TEST
256 /* erase it where Linux goes */
257 start = aufl_layout[1].start;
258 end = aufl_layout[1].end;
259#endif
260 flash_sect_protect(0, start, end);
261 }
262
263 /*
264 * erase the address range.
265 */
266 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
267 flash_sect_erase(start, end);
268 wait_ms(100);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300269#ifdef CONFIG_PROGRESSBAR
270 show_progress(end - start, totsize);
271#endif
272
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100273 /* strip the header - except for the kernel and ramdisk */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100274 if (image_check_type (hdr, IH_TYPE_KERNEL) ||
275 image_check_type (hdr, IH_TYPE_RAMDISK)) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100276 addr = (char *)hdr;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100277 off = image_get_header_size ();
278 nbytes = image_get_image_size (hdr);
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100279 } else {
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100280 addr = (char *)((char *)hdr + image_get_header_size ());
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100281#ifdef AU_UPDATE_TEST
282 /* copy it to where Linux goes */
283 if (idx == IDX_FIRMWARE)
284 start = aufl_layout[1].start;
285#endif
286 off = 0;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100287 nbytes = image_get_data_size (hdr);
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100288 }
289
290 /* copy the data from RAM to FLASH */
291 debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
292 rc = flash_write(addr, start, nbytes);
293 if (rc != 0) {
294 printf("Flashing failed due to error %d\n", rc);
295 return -1;
296 }
297
Sergei Poselenov638dd142007-02-27 12:40:16 +0300298#ifdef CONFIG_PROGRESSBAR
299 show_progress(nbytes, totsize);
300#endif
301
Wolfgang Denkf5fcc3c2007-02-19 23:09:51 +0100302 /* check the data CRC of the copy */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100303 if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) !=
304 image_get_dcrc (hdr)) {
Wolfgang Denkf5fcc3c2007-02-19 23:09:51 +0100305 printf ("Image %s Bad Data Checksum after COPY\n", aufile[idx]);
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100306 return -1;
307 }
308
309 /* protect the address range */
310 /* this assumes that ONLY the firmware is protected! */
311 if (idx == IDX_FIRMWARE)
312 flash_sect_protect(1, start, end);
313 return 0;
314}
315
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100316/*
317 * this is called from board_init() after the hardware has been set up
318 * and is usable. That seems like a good time to do this.
319 * Right now the return value is ignored.
320 */
Wolfgang Denk82e52362006-12-22 10:30:26 +0100321int do_auto_update(void)
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100322{
323 block_dev_desc_t *stor_dev;
324 long sz;
Sergei Poselenov638dd142007-02-27 12:40:16 +0300325 int i, res = 0, bitmap_first, cnt, old_ctrlc, got_ctrlc;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100326 char *env;
327 long start, end;
Wolfgang Denka4d26362007-08-12 15:11:38 +0200328
329#if 0 /* disable key-press detection to speed up boot-up time */
Sergei Poselenov489c6962007-02-14 14:30:28 +0300330 uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100331
332 /*
333 * Read keypad status
334 */
335 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
336 wait_ms(500);
337 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
338
339 /*
340 * Check keypad
341 */
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100342 if ( !(keypad_status1[1] & KEYPAD_MASK_LO) ||
343 (keypad_status1[1] != keypad_status2[1])) {
344 return 0;
345 }
Sergei Poselenov638dd142007-02-27 12:40:16 +0300346
Wolfgang Denka4d26362007-08-12 15:11:38 +0200347#endif
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");
Wolfgang Denka4d26362007-08-12 15:11:38 +0200365 res = -1;
366 goto xit;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100367 }
368 /* check whether it has a partition table */
369 stor_dev = get_dev("usb", 0);
370 if (stor_dev == NULL) {
371 debug ("uknown device type\n");
Wolfgang Denka4d26362007-08-12 15:11:38 +0200372 res = -1;
373 goto xit;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100374 }
375 if (fat_register_device(stor_dev, 1) != 0) {
376 debug ("Unable to use USB %d:%d for fatls\n",
377 au_usb_stor_curr_dev, 1);
Wolfgang Denka4d26362007-08-12 15:11:38 +0200378 res = -1;
379 goto xit;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100380 }
381 if (file_fat_detectfs() != 0) {
382 debug ("file_fat_detectfs failed\n");
383 }
384
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100385 /*
386 * now check whether start and end are defined using environment
387 * variables.
388 */
389 start = -1;
390 end = 0;
391 env = getenv("firmware_st");
392 if (env != NULL)
393 start = simple_strtoul(env, NULL, 16);
394 env = getenv("firmware_nd");
395 if (env != NULL)
396 end = simple_strtoul(env, NULL, 16);
397 if (start >= 0 && end && end > start) {
398 ausize[IDX_FIRMWARE] = (end + 1) - start;
Sergei Poselenov489c6962007-02-14 14:30:28 +0300399 aufl_layout[IDX_FIRMWARE].start = start;
400 aufl_layout[IDX_FIRMWARE].end = end;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100401 }
402 start = -1;
403 end = 0;
404 env = getenv("kernel_st");
405 if (env != NULL)
406 start = simple_strtoul(env, NULL, 16);
407 env = getenv("kernel_nd");
408 if (env != NULL)
409 end = simple_strtoul(env, NULL, 16);
410 if (start >= 0 && end && end > start) {
411 ausize[IDX_KERNEL] = (end + 1) - start;
Sergei Poselenov489c6962007-02-14 14:30:28 +0300412 aufl_layout[IDX_KERNEL].start = start;
413 aufl_layout[IDX_KERNEL].end = end;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100414 }
Sergei Poselenov489c6962007-02-14 14:30:28 +0300415 start = -1;
416 end = 0;
417 env = getenv("rootfs_st");
418 if (env != NULL)
419 start = simple_strtoul(env, NULL, 16);
420 env = getenv("rootfs_nd");
421 if (env != NULL)
422 end = simple_strtoul(env, NULL, 16);
423 if (start >= 0 && end && end > start) {
424 ausize[IDX_ROOTFS] = (end + 1) - start;
425 aufl_layout[IDX_ROOTFS].start = start;
426 aufl_layout[IDX_ROOTFS].end = end;
427 }
428
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100429 /* make certain that HUSH is runnable */
430 u_boot_hush_start();
431 /* make sure that we see CTRL-C and save the old state */
432 old_ctrlc = disable_ctrlc(0);
433
434 bitmap_first = 0;
Sergei Poselenov638dd142007-02-27 12:40:16 +0300435
Wolfgang Denk74357112007-02-27 14:26:04 +0100436 /* validate the images first */
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100437 for (i = 0; i < AU_MAXFILES; i++) {
Sergei Poselenov638dd142007-02-27 12:40:16 +0300438 ulong imsize;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100439 /* just read the header */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100440 sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ());
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100441 debug ("read %s sz %ld hdr %d\n",
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100442 aufile[i], sz, image_get_header_size ());
443 if (sz <= 0 || sz < image_get_header_size ()) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100444 debug ("%s not found\n", aufile[i]);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300445 ausize[i] = 0;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100446 continue;
447 }
Sergei Poselenov638dd142007-02-27 12:40:16 +0300448 /* au_check_header_valid() updates ausize[] */
449 if ((imsize = au_check_header_valid(i, sz)) < 0) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100450 debug ("%s header not valid\n", aufile[i]);
451 continue;
452 }
Sergei Poselenov638dd142007-02-27 12:40:16 +0300453 /* totsize accounts for image size and flash erase size */
Wolfgang Denk74357112007-02-27 14:26:04 +0100454 totsize += (imsize + (aufl_layout[i].end - aufl_layout[i].start));
Sergei Poselenov638dd142007-02-27 12:40:16 +0300455 }
456
457#ifdef CONFIG_PROGRESSBAR
458 if (totsize) {
459 lcd_puts(" Update in progress\n");
460 lcd_enable();
461 }
462#endif
463
464 /* just loop thru all the possible files */
465 for (i = 0; i < AU_MAXFILES && totsize; i++) {
466 if (!ausize[i]) {
467 continue;
468 }
469 sz = file_fat_read(aufile[i], LOAD_ADDR, ausize[i]);
470
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100471 debug ("read %s sz %ld hdr %d\n",
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100472 aufile[i], sz, image_get_header_size ());
Sergei Poselenov638dd142007-02-27 12:40:16 +0300473
474 if (sz != ausize[i]) {
475 printf ("%s: size %d read %d?\n", aufile[i], ausize[i], sz);
Wolfgang Denk74357112007-02-27 14:26:04 +0100476 continue;
477 }
Sergei Poselenov638dd142007-02-27 12:40:16 +0300478
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100479 if (sz <= 0 || sz <= image_get_header_size ()) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100480 debug ("%s not found\n", aufile[i]);
481 continue;
482 }
483 if (au_check_cksum_valid(i, sz) < 0) {
484 debug ("%s checksum not valid\n", aufile[i]);
485 continue;
486 }
487 /* this is really not a good idea, but it's what the */
488 /* customer wants. */
489 cnt = 0;
490 got_ctrlc = 0;
491 do {
492 res = au_do_update(i, sz);
493 /* let the user break out of the loop */
494 if (ctrlc() || had_ctrlc()) {
495 clear_ctrlc();
496 if (res < 0)
497 got_ctrlc = 1;
498 break;
499 }
500 cnt++;
501#ifdef AU_TEST_ONLY
Sergei Poselenov489c6962007-02-14 14:30:28 +0300502 } while (res < 0 && cnt < (AU_MAXFILES + 1));
503 if (cnt < (AU_MAXFILES + 1))
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100504#else
505 } while (res < 0);
506#endif
507 }
Wolfgang Denka4d26362007-08-12 15:11:38 +0200508
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100509 /* restore the old state */
510 disable_ctrlc(old_ctrlc);
Sergei Poselenov638dd142007-02-27 12:40:16 +0300511#ifdef CONFIG_PROGRESSBAR
512 if (totsize) {
513 if (!res) {
514 lcd_puts("\n Update completed\n");
515 } else {
516 lcd_puts("\n Update error\n");
517 }
518 lcd_enable();
519 }
520#endif
Wolfgang Denka4d26362007-08-12 15:11:38 +0200521 xit:
522 usb_stop();
523 return res;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100524}
525#endif /* CONFIG_AUTO_UPDATE */