blob: 43173ce060d56817353595d681bbf531cba1c76a [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
Andrei Safronov82336242006-12-08 16:23:08 +010015#ifdef CONFIG_AUTO_UPDATE
16
17#ifndef CONFIG_USB_OHCI
18#error "must define CONFIG_USB_OHCI"
19#endif
20
21#ifndef CONFIG_USB_STORAGE
22#error "must define CONFIG_USB_STORAGE"
23#endif
24
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020025#ifndef CONFIG_SYS_HUSH_PARSER
26#error "must define CONFIG_SYS_HUSH_PARSER"
Andrei Safronov82336242006-12-08 16:23:08 +010027#endif
28
Jon Loeliger145318c2007-07-09 18:38:39 -050029#if !defined(CONFIG_CMD_FAT)
Jon Loeliger761ea742007-07-10 10:48:22 -050030#error "must define CONFIG_CMD_FAT"
Andrei Safronov82336242006-12-08 16:23:08 +010031#endif
32
Andrei Safronov82336242006-12-08 16:23:08 +010033#undef AU_DEBUG
34
35#undef debug
36#ifdef AU_DEBUG
37#define debug(fmt,args...) printf (fmt ,##args)
38#else
39#define debug(fmt,args...)
40#endif /* AU_DEBUG */
41
42/* possible names of files on the USB stick. */
43#define AU_FIRMWARE "u-boot.img"
44#define AU_KERNEL "kernel.img"
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030045#define AU_ROOTFS "rootfs.img"
Andrei Safronov82336242006-12-08 16:23:08 +010046
Wolfgang Denk8e59e1f2006-12-22 10:30:26 +010047struct flash_layout {
Andrei Safronov82336242006-12-08 16:23:08 +010048 long start;
49 long end;
50};
51
52/* layout of the FLASH. ST = start address, ND = end address. */
53#define AU_FL_FIRMWARE_ST 0xfC000000
54#define AU_FL_FIRMWARE_ND 0xfC03FFFF
55#define AU_FL_KERNEL_ST 0xfC0C0000
56#define AU_FL_KERNEL_ND 0xfC1BFFFF
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030057#define AU_FL_ROOTFS_ST 0xFC1C0000
58#define AU_FL_ROOTFS_ND 0xFCFBFFFF
Andrei Safronov82336242006-12-08 16:23:08 +010059
60static int au_usb_stor_curr_dev; /* current device */
61
62/* index of each file in the following arrays */
63#define IDX_FIRMWARE 0
64#define IDX_KERNEL 1
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030065#define IDX_ROOTFS 2
Andrei Safronov82336242006-12-08 16:23:08 +010066
67/* max. number of files which could interest us */
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030068#define AU_MAXFILES 3
Andrei Safronov82336242006-12-08 16:23:08 +010069
70/* pointers to file names */
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030071char *aufile[AU_MAXFILES] = {
72 AU_FIRMWARE,
73 AU_KERNEL,
74 AU_ROOTFS
75};
Andrei Safronov82336242006-12-08 16:23:08 +010076
77/* sizes of flash areas for each file */
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030078long ausize[AU_MAXFILES] = {
79 (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST,
Wolfgang Denkf4da2ef2007-02-19 23:09:51 +010080 (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST,
81 (AU_FL_ROOTFS_ND + 1) - AU_FL_ROOTFS_ST,
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030082};
Andrei Safronov82336242006-12-08 16:23:08 +010083
84/* array of flash areas start and end addresses */
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030085struct flash_layout aufl_layout[AU_MAXFILES] = {
Wolfgang Denkf4da2ef2007-02-19 23:09:51 +010086 { AU_FL_FIRMWARE_ST, AU_FL_FIRMWARE_ND, },
87 { AU_FL_KERNEL_ST, AU_FL_KERNEL_ND, },
88 { AU_FL_ROOTFS_ST, AU_FL_ROOTFS_ND, },
Andrei Safronov82336242006-12-08 16:23:08 +010089};
90
Sergei Poselenov91904cd2007-02-27 12:40:16 +030091ulong totsize;
92
Andrei Safronov82336242006-12-08 16:23:08 +010093/* where to load files into memory */
94#define LOAD_ADDR ((unsigned char *)0x00200000)
95
Wolfgang Denkf4da2ef2007-02-19 23:09:51 +010096/* the root file system is the largest image */
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +030097#define MAX_LOADSZ ausize[IDX_ROOTFS]
Andrei Safronov82336242006-12-08 16:23:08 +010098
99/*i2c address of the keypad status*/
100#define I2C_PSOC_KEYPAD_ADDR 0x53
101
102/* keypad mask */
Wolfgang Denk299d6102007-01-10 01:28:39 +0100103#define KEYPAD_ROW 2
104#define KEYPAD_COL 2
105#define KEYPAD_MASK_LO ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))&0xFF)
106#define KEYPAD_MASK_HI ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))>>8)
Andrei Safronov82336242006-12-08 16:23:08 +0100107
108/* externals */
109extern int fat_register_device(block_dev_desc_t *, int);
110extern int file_fat_detectfs(void);
111extern long file_fat_read(const char *, void *, unsigned long);
112extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
113extern int flash_sect_erase(ulong, ulong);
114extern int flash_sect_protect (int, ulong, ulong);
115extern int flash_write (char *, ulong, ulong);
Andrei Safronov82336242006-12-08 16:23:08 +0100116extern int u_boot_hush_start(void);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300117#ifdef CONFIG_PROGRESSBAR
118extern void show_progress(int, int);
119extern void lcd_puts (char *);
120extern void lcd_enable(void);
121#endif
Andrei Safronov82336242006-12-08 16:23:08 +0100122
Wolfgang Denk8e59e1f2006-12-22 10:30:26 +0100123int au_check_cksum_valid(int idx, long nbytes)
Andrei Safronov82336242006-12-08 16:23:08 +0100124{
125 image_header_t *hdr;
Andrei Safronov82336242006-12-08 16:23:08 +0100126
127 hdr = (image_header_t *)LOAD_ADDR;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100128#if defined(CONFIG_FIT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100129 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100130 puts ("Non legacy image format not supported\n");
131 return -1;
132 }
133#endif
Andrei Safronov82336242006-12-08 16:23:08 +0100134
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100135 if (nbytes != image_get_image_size (hdr)) {
Andrei Safronov82336242006-12-08 16:23:08 +0100136 printf ("Image %s bad total SIZE\n", aufile[idx]);
137 return -1;
138 }
139 /* check the data CRC */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100140 if (!image_check_dcrc (hdr)) {
Andrei Safronov82336242006-12-08 16:23:08 +0100141 printf ("Image %s bad data checksum\n", aufile[idx]);
142 return -1;
143 }
144 return 0;
145}
146
Wolfgang Denk8e59e1f2006-12-22 10:30:26 +0100147int au_check_header_valid(int idx, long nbytes)
Andrei Safronov82336242006-12-08 16:23:08 +0100148{
149 image_header_t *hdr;
Sergei Poselenov854fe362007-02-27 20:15:30 +0300150 unsigned long checksum, fsize;
Andrei Safronov82336242006-12-08 16:23:08 +0100151
152 hdr = (image_header_t *)LOAD_ADDR;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100153#if defined(CONFIG_FIT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100154 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100155 puts ("Non legacy image format not supported\n");
156 return -1;
157 }
158#endif
159
Andrei Safronov82336242006-12-08 16:23:08 +0100160 /* check the easy ones first */
161#undef CHECK_VALID_DEBUG
162#ifdef CHECK_VALID_DEBUG
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100163 printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC);
164 printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_ARM);
165 printf("size %#x %#lx ", image_get_data_size (hdr), nbytes);
166 printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL);
Andrei Safronov82336242006-12-08 16:23:08 +0100167#endif
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100168 if (nbytes < image_get_header_size ()) {
Andrei Safronov82336242006-12-08 16:23:08 +0100169 printf ("Image %s bad header SIZE\n", aufile[idx]);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300170 ausize[idx] = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100171 return -1;
172 }
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100173 if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) {
Andrei Safronov82336242006-12-08 16:23:08 +0100174 printf ("Image %s bad MAGIC or ARCH\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 }
178 /* check the hdr CRC */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100179 if (!image_check_hcrc (hdr)) {
Andrei Safronov82336242006-12-08 16:23:08 +0100180 printf ("Image %s bad header checksum\n", aufile[idx]);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300181 ausize[idx] = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100182 return -1;
183 }
Andrei Safronov82336242006-12-08 16:23:08 +0100184 /* check the type - could do this all in one gigantic if() */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100185 if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
Andrei Safronov82336242006-12-08 16:23:08 +0100186 printf ("Image %s wrong type\n", aufile[idx]);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300187 ausize[idx] = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100188 return -1;
189 }
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100190 if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) {
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 }
Wolfgang Denkf4da2ef2007-02-19 23:09:51 +0100195 if ((idx == IDX_ROOTFS) &&
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100196 (!image_check_type (hdr, IH_TYPE_RAMDISK) &&
197 !image_check_type (hdr, IH_TYPE_FILESYSTEM))) {
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +0300198 printf ("Image %s wrong type\n", aufile[idx]);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300199 ausize[idx] = 0;
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +0300200 return -1;
201 }
Andrei Safronov82336242006-12-08 16:23:08 +0100202 /* recycle checksum */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100203 checksum = image_get_data_size (hdr);
Sergei Poselenov854fe362007-02-27 20:15:30 +0300204
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100205 fsize = checksum + image_get_header_size ();
Sergei Poselenov854fe362007-02-27 20:15:30 +0300206 /* for kernel and ramdisk the image header must also fit into flash */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100207 if (idx == IDX_KERNEL || image_check_type (hdr, IH_TYPE_RAMDISK))
208 checksum += image_get_header_size ();
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300209
Andrei Safronov82336242006-12-08 16:23:08 +0100210 /* check the size does not exceed space in flash. HUSH scripts */
Andrei Safronov82336242006-12-08 16:23:08 +0100211 if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
212 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300213 ausize[idx] = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100214 return -1;
215 }
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300216 /* Update with the real filesize */
Sergei Poselenov854fe362007-02-27 20:15:30 +0300217 ausize[idx] = fsize;
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300218
219 return checksum; /* return size to be written to flash */
Andrei Safronov82336242006-12-08 16:23:08 +0100220}
221
Wolfgang Denk8e59e1f2006-12-22 10:30:26 +0100222int au_do_update(int idx, long sz)
Andrei Safronov82336242006-12-08 16:23:08 +0100223{
224 image_header_t *hdr;
225 char *addr;
226 long start, end;
227 int off, rc;
228 uint nbytes;
229
230 hdr = (image_header_t *)LOAD_ADDR;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100231#if defined(CONFIG_FIT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100232 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100233 puts ("Non legacy image format not supported\n");
234 return -1;
235 }
236#endif
Andrei Safronov82336242006-12-08 16:23:08 +0100237
238 /* execute a script */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100239 if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
240 addr = (char *)((char *)hdr + image_get_header_size ());
Andrei Safronov82336242006-12-08 16:23:08 +0100241 /* stick a NULL at the end of the script, otherwise */
242 /* parse_string_outer() runs off the end. */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100243 addr[image_get_data_size (hdr)] = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100244 addr += 8;
Simon Glass794cf6a2014-04-10 20:01:23 -0600245 run_command_list(addr, -1, 0);
Andrei Safronov82336242006-12-08 16:23:08 +0100246 return 0;
247 }
248
249 start = aufl_layout[idx].start;
250 end = aufl_layout[idx].end;
251
252 /* unprotect the address range */
253 /* this assumes that ONLY the firmware is protected! */
254 if (idx == IDX_FIRMWARE) {
255#undef AU_UPDATE_TEST
256#ifdef AU_UPDATE_TEST
257 /* erase it where Linux goes */
258 start = aufl_layout[1].start;
259 end = aufl_layout[1].end;
260#endif
261 flash_sect_protect(0, start, end);
262 }
263
264 /*
265 * erase the address range.
266 */
267 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
268 flash_sect_erase(start, end);
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000269 mdelay(100);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300270#ifdef CONFIG_PROGRESSBAR
271 show_progress(end - start, totsize);
272#endif
273
Andrei Safronov82336242006-12-08 16:23:08 +0100274 /* strip the header - except for the kernel and ramdisk */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100275 if (image_check_type (hdr, IH_TYPE_KERNEL) ||
276 image_check_type (hdr, IH_TYPE_RAMDISK)) {
Andrei Safronov82336242006-12-08 16:23:08 +0100277 addr = (char *)hdr;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100278 off = image_get_header_size ();
279 nbytes = image_get_image_size (hdr);
Andrei Safronov82336242006-12-08 16:23:08 +0100280 } else {
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100281 addr = (char *)((char *)hdr + image_get_header_size ());
Andrei Safronov82336242006-12-08 16:23:08 +0100282#ifdef AU_UPDATE_TEST
283 /* copy it to where Linux goes */
284 if (idx == IDX_FIRMWARE)
285 start = aufl_layout[1].start;
286#endif
287 off = 0;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100288 nbytes = image_get_data_size (hdr);
Andrei Safronov82336242006-12-08 16:23:08 +0100289 }
290
291 /* copy the data from RAM to FLASH */
292 debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
293 rc = flash_write(addr, start, nbytes);
294 if (rc != 0) {
295 printf("Flashing failed due to error %d\n", rc);
296 return -1;
297 }
298
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300299#ifdef CONFIG_PROGRESSBAR
300 show_progress(nbytes, totsize);
301#endif
302
Wolfgang Denkf4da2ef2007-02-19 23:09:51 +0100303 /* check the data CRC of the copy */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100304 if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) !=
305 image_get_dcrc (hdr)) {
Wolfgang Denkf4da2ef2007-02-19 23:09:51 +0100306 printf ("Image %s Bad Data Checksum after COPY\n", aufile[idx]);
Andrei Safronov82336242006-12-08 16:23:08 +0100307 return -1;
308 }
309
310 /* protect the address range */
311 /* this assumes that ONLY the firmware is protected! */
312 if (idx == IDX_FIRMWARE)
313 flash_sect_protect(1, start, end);
314 return 0;
315}
316
Andrei Safronov82336242006-12-08 16:23:08 +0100317/*
318 * this is called from board_init() after the hardware has been set up
319 * and is usable. That seems like a good time to do this.
320 * Right now the return value is ignored.
321 */
Wolfgang Denk8e59e1f2006-12-22 10:30:26 +0100322int do_auto_update(void)
Andrei Safronov82336242006-12-08 16:23:08 +0100323{
324 block_dev_desc_t *stor_dev;
325 long sz;
Wolfgang Denk8af49192011-11-04 15:55:11 +0000326 int i, res = 0, cnt, old_ctrlc;
Andrei Safronov82336242006-12-08 16:23:08 +0100327 char *env;
328 long start, end;
Wolfgang Denk036384c2007-08-12 15:11:38 +0200329
330#if 0 /* disable key-press detection to speed up boot-up time */
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +0300331 uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
Andrei Safronov82336242006-12-08 16:23:08 +0100332
333 /*
334 * Read keypad status
335 */
336 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000337 mdelay(500);
Andrei Safronov82336242006-12-08 16:23:08 +0100338 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
339
340 /*
341 * Check keypad
342 */
Andrei Safronov82336242006-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 Poselenov91904cd2007-02-27 12:40:16 +0300347
Wolfgang Denk036384c2007-08-12 15:11:38 +0200348#endif
Andrei Safronov82336242006-12-08 16:23:08 +0100349 au_usb_stor_curr_dev = -1;
350 /* start USB */
351 if (usb_stop() < 0) {
352 debug ("usb_stop failed\n");
353 return -1;
354 }
355 if (usb_init() < 0) {
356 debug ("usb_init failed\n");
357 return -1;
358 }
359 /*
360 * check whether a storage device is attached (assume that it's
361 * a USB memory stick, since nothing else should be attached).
362 */
363 au_usb_stor_curr_dev = usb_stor_scan(0);
364 if (au_usb_stor_curr_dev == -1) {
365 debug ("No device found. Not initialized?\n");
Wolfgang Denk036384c2007-08-12 15:11:38 +0200366 res = -1;
367 goto xit;
Andrei Safronov82336242006-12-08 16:23:08 +0100368 }
369 /* check whether it has a partition table */
370 stor_dev = get_dev("usb", 0);
371 if (stor_dev == NULL) {
372 debug ("uknown device type\n");
Wolfgang Denk036384c2007-08-12 15:11:38 +0200373 res = -1;
374 goto xit;
Andrei Safronov82336242006-12-08 16:23:08 +0100375 }
376 if (fat_register_device(stor_dev, 1) != 0) {
377 debug ("Unable to use USB %d:%d for fatls\n",
378 au_usb_stor_curr_dev, 1);
Wolfgang Denk036384c2007-08-12 15:11:38 +0200379 res = -1;
380 goto xit;
Andrei Safronov82336242006-12-08 16:23:08 +0100381 }
382 if (file_fat_detectfs() != 0) {
383 debug ("file_fat_detectfs failed\n");
384 }
385
Andrei Safronov82336242006-12-08 16:23:08 +0100386 /*
387 * now check whether start and end are defined using environment
388 * variables.
389 */
390 start = -1;
391 end = 0;
392 env = getenv("firmware_st");
393 if (env != NULL)
394 start = simple_strtoul(env, NULL, 16);
395 env = getenv("firmware_nd");
396 if (env != NULL)
397 end = simple_strtoul(env, NULL, 16);
398 if (start >= 0 && end && end > start) {
399 ausize[IDX_FIRMWARE] = (end + 1) - start;
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +0300400 aufl_layout[IDX_FIRMWARE].start = start;
401 aufl_layout[IDX_FIRMWARE].end = end;
Andrei Safronov82336242006-12-08 16:23:08 +0100402 }
403 start = -1;
404 end = 0;
405 env = getenv("kernel_st");
406 if (env != NULL)
407 start = simple_strtoul(env, NULL, 16);
408 env = getenv("kernel_nd");
409 if (env != NULL)
410 end = simple_strtoul(env, NULL, 16);
411 if (start >= 0 && end && end > start) {
412 ausize[IDX_KERNEL] = (end + 1) - start;
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +0300413 aufl_layout[IDX_KERNEL].start = start;
414 aufl_layout[IDX_KERNEL].end = end;
Andrei Safronov82336242006-12-08 16:23:08 +0100415 }
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +0300416 start = -1;
417 end = 0;
418 env = getenv("rootfs_st");
419 if (env != NULL)
420 start = simple_strtoul(env, NULL, 16);
421 env = getenv("rootfs_nd");
422 if (env != NULL)
423 end = simple_strtoul(env, NULL, 16);
424 if (start >= 0 && end && end > start) {
425 ausize[IDX_ROOTFS] = (end + 1) - start;
426 aufl_layout[IDX_ROOTFS].start = start;
427 aufl_layout[IDX_ROOTFS].end = end;
428 }
429
Andrei Safronov82336242006-12-08 16:23:08 +0100430 /* make certain that HUSH is runnable */
431 u_boot_hush_start();
432 /* make sure that we see CTRL-C and save the old state */
433 old_ctrlc = disable_ctrlc(0);
434
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100435 /* validate the images first */
Andrei Safronov82336242006-12-08 16:23:08 +0100436 for (i = 0; i < AU_MAXFILES; i++) {
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300437 ulong imsize;
Andrei Safronov82336242006-12-08 16:23:08 +0100438 /* just read the header */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100439 sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ());
Andrei Safronov82336242006-12-08 16:23:08 +0100440 debug ("read %s sz %ld hdr %d\n",
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100441 aufile[i], sz, image_get_header_size ());
442 if (sz <= 0 || sz < image_get_header_size ()) {
Andrei Safronov82336242006-12-08 16:23:08 +0100443 debug ("%s not found\n", aufile[i]);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300444 ausize[i] = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100445 continue;
446 }
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300447 /* au_check_header_valid() updates ausize[] */
448 if ((imsize = au_check_header_valid(i, sz)) < 0) {
Andrei Safronov82336242006-12-08 16:23:08 +0100449 debug ("%s header not valid\n", aufile[i]);
450 continue;
451 }
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300452 /* totsize accounts for image size and flash erase size */
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100453 totsize += (imsize + (aufl_layout[i].end - aufl_layout[i].start));
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300454 }
455
456#ifdef CONFIG_PROGRESSBAR
457 if (totsize) {
458 lcd_puts(" Update in progress\n");
459 lcd_enable();
460 }
461#endif
462
463 /* just loop thru all the possible files */
464 for (i = 0; i < AU_MAXFILES && totsize; i++) {
465 if (!ausize[i]) {
466 continue;
467 }
468 sz = file_fat_read(aufile[i], LOAD_ADDR, ausize[i]);
469
Andrei Safronov82336242006-12-08 16:23:08 +0100470 debug ("read %s sz %ld hdr %d\n",
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100471 aufile[i], sz, image_get_header_size ());
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300472
473 if (sz != ausize[i]) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +0200474 printf ("%s: size %ld read %ld?\n", aufile[i], ausize[i], sz);
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100475 continue;
476 }
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300477
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100478 if (sz <= 0 || sz <= image_get_header_size ()) {
Andrei Safronov82336242006-12-08 16:23:08 +0100479 debug ("%s not found\n", aufile[i]);
480 continue;
481 }
482 if (au_check_cksum_valid(i, sz) < 0) {
483 debug ("%s checksum not valid\n", aufile[i]);
484 continue;
485 }
486 /* this is really not a good idea, but it's what the */
487 /* customer wants. */
488 cnt = 0;
Andrei Safronov82336242006-12-08 16:23:08 +0100489 do {
490 res = au_do_update(i, sz);
491 /* let the user break out of the loop */
492 if (ctrlc() || had_ctrlc()) {
493 clear_ctrlc();
Andrei Safronov82336242006-12-08 16:23:08 +0100494 break;
495 }
496 cnt++;
497#ifdef AU_TEST_ONLY
Sergei Poselenov8ceb08f2007-02-14 14:30:28 +0300498 } while (res < 0 && cnt < (AU_MAXFILES + 1));
499 if (cnt < (AU_MAXFILES + 1))
Andrei Safronov82336242006-12-08 16:23:08 +0100500#else
501 } while (res < 0);
502#endif
503 }
Wolfgang Denk036384c2007-08-12 15:11:38 +0200504
Andrei Safronov82336242006-12-08 16:23:08 +0100505 /* restore the old state */
506 disable_ctrlc(old_ctrlc);
Sergei Poselenov91904cd2007-02-27 12:40:16 +0300507#ifdef CONFIG_PROGRESSBAR
508 if (totsize) {
509 if (!res) {
510 lcd_puts("\n Update completed\n");
511 } else {
512 lcd_puts("\n Update error\n");
513 }
514 lcd_enable();
515 }
516#endif
Wolfgang Denk036384c2007-08-12 15:11:38 +0200517 xit:
518 usb_stop();
519 return res;
Andrei Safronov82336242006-12-08 16:23:08 +0100520}
521#endif /* CONFIG_AUTO_UPDATE */