]> git.ipfire.org Git - people/ms/u-boot.git/blame - board/trab/auto_update.c
[new uImage] Fix erroneous use of image_get_magic() in fdc/usb cmds
[people/ms/u-boot.git] / board / trab / auto_update.c
CommitLineData
f54ebdfa
WD
1/*
2 * (C) Copyright 2003
3 * Gary Jennejohn, DENX Software Engineering, gj@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <command.h>
26#include <malloc.h>
27#include <image.h>
28#include <asm/byteorder.h>
29#include <usb.h>
30
31#ifdef CFG_HUSH_PARSER
32#include <hush.h>
33#endif
34
35#ifdef CONFIG_AUTO_UPDATE
36
7b59b3c7 37#ifndef CONFIG_USB_OHCI_NEW
f54ebdfa
WD
38#error "must define CONFIG_USB_OHCI"
39#endif
40
41#ifndef CONFIG_USB_STORAGE
42#error "must define CONFIG_USB_STORAGE"
43#endif
44
45#ifndef CFG_HUSH_PARSER
46#error "must define CFG_HUSH_PARSER"
47#endif
48
ab3abcba 49#if !defined(CONFIG_CMD_FAT)
d39b5741 50#error "must define CONFIG_CMD_FAT"
f54ebdfa
WD
51#endif
52
53/*
54 * Check whether a USB memory stick is plugged in.
55 * If one is found:
b0639ca3
WD
56 * 1) if prepare.img ist found load it into memory. If it is
57 * valid then run it.
58 * 2) if preinst.img is found load it into memory. If it is
59 * valid then run it. Update the EEPROM.
c786f427 60 * 3) if firmw_01.img is found load it into memory. If it is valid,
b0639ca3 61 * burn it into FLASH and update the EEPROM.
c786f427 62 * 4) if kernl_01.img is found load it into memory. If it is valid,
b0639ca3
WD
63 * burn it into FLASH and update the EEPROM.
64 * 5) if app.img is found load it into memory. If it is valid,
65 * burn it into FLASH and update the EEPROM.
66 * 6) if disk.img is found load it into memory. If it is valid,
67 * burn it into FLASH and update the EEPROM.
68 * 7) if postinst.img is found load it into memory. If it is
69 * valid then run it. Update the EEPROM.
f54ebdfa
WD
70 */
71
72#undef AU_DEBUG
73
74#undef debug
75#ifdef AU_DEBUG
76#define debug(fmt,args...) printf (fmt ,##args)
77#else
78#define debug(fmt,args...)
79#endif /* AU_DEBUG */
80
81/* possible names of files on the USB stick. */
82#define AU_PREPARE "prepare.img"
83#define AU_PREINST "preinst.img"
c786f427
WD
84#define AU_FIRMWARE "firmw_01.img"
85#define AU_KERNEL "kernl_01.img"
f54ebdfa
WD
86#define AU_APP "app.img"
87#define AU_DISK "disk.img"
88#define AU_POSTINST "postinst.img"
89
b0639ca3
WD
90struct flash_layout
91{
92 long start;
93 long end;
94};
95
f54ebdfa 96/* layout of the FLASH. ST = start address, ND = end address. */
b0639ca3 97#ifndef CONFIG_FLASH_8MB /* 16 MB Flash, 32 MB RAM */
f54ebdfa
WD
98#define AU_FL_FIRMWARE_ST 0x00000000
99#define AU_FL_FIRMWARE_ND 0x0009FFFF
100#define AU_FL_VFD_ST 0x000A0000
101#define AU_FL_VFD_ND 0x000BFFFF
102#define AU_FL_KERNEL_ST 0x000C0000
103#define AU_FL_KERNEL_ND 0x001BFFFF
104#define AU_FL_APP_ST 0x001C0000
105#define AU_FL_APP_ND 0x005BFFFF
106#define AU_FL_DISK_ST 0x005C0000
107#define AU_FL_DISK_ND 0x00FFFFFF
8a42eac7 108#else /* 8 MB Flash, 32 MB RAM */
f54ebdfa 109#define AU_FL_FIRMWARE_ST 0x00000000
8a42eac7 110#define AU_FL_FIRMWARE_ND 0x0005FFFF
111#define AU_FL_KERNEL_ST 0x00060000
112#define AU_FL_KERNEL_ND 0x0013FFFF
113#define AU_FL_APP_ST 0x00140000
114#define AU_FL_APP_ND 0x0067FFFF
115#define AU_FL_DISK_ST 0x00680000
f54ebdfa
WD
116#define AU_FL_DISK_ND 0x007DFFFF
117#define AU_FL_VFD_ST 0x007E0000
118#define AU_FL_VFD_ND 0x007FFFFF
b0639ca3 119#endif /* CONFIG_FLASH_8MB */
f54ebdfa
WD
120
121/* a structure with the offsets to values in the EEPROM */
122struct eeprom_layout
123{
b0639ca3
WD
124 int time;
125 int size;
126 int dcrc;
f54ebdfa
WD
127};
128
129/* layout of the EEPROM - offset from the start. All entries are 32 bit. */
130#define AU_EEPROM_TIME_PREINST 64
131#define AU_EEPROM_SIZE_PREINST 68
132#define AU_EEPROM_DCRC_PREINST 72
133#define AU_EEPROM_TIME_FIRMWARE 76
134#define AU_EEPROM_SIZE_FIRMWARE 80
135#define AU_EEPROM_DCRC_FIRMWARE 84
136#define AU_EEPROM_TIME_KERNEL 88
137#define AU_EEPROM_SIZE_KERNEL 92
138#define AU_EEPROM_DCRC_KERNEL 96
139#define AU_EEPROM_TIME_APP 100
140#define AU_EEPROM_SIZE_APP 104
141#define AU_EEPROM_DCRC_APP 108
142#define AU_EEPROM_TIME_DISK 112
143#define AU_EEPROM_SIZE_DISK 116
144#define AU_EEPROM_DCRC_DISK 120
145#define AU_EEPROM_TIME_POSTINST 124
146#define AU_EEPROM_SIZE_POSTINST 128
147#define AU_EEPROM_DCRC_POSTINST 132
148
149static int au_usb_stor_curr_dev; /* current device */
b0639ca3
WD
150
151/* index of each file in the following arrays */
152#define IDX_PREPARE 0
153#define IDX_PREINST 1
154#define IDX_FIRMWARE 2
155#define IDX_KERNEL 3
156#define IDX_APP 4
157#define IDX_DISK 5
158#define IDX_POSTINST 6
f54ebdfa
WD
159/* max. number of files which could interest us */
160#define AU_MAXFILES 7
161/* pointers to file names */
162char *aufile[AU_MAXFILES];
163/* sizes of flash areas for each file */
164long ausize[AU_MAXFILES];
165/* offsets into the EEEPROM */
166struct eeprom_layout auee_off[AU_MAXFILES] = { \
167 {0}, \
168 {AU_EEPROM_TIME_PREINST, AU_EEPROM_SIZE_PREINST, AU_EEPROM_DCRC_PREINST,}, \
169 {AU_EEPROM_TIME_FIRMWARE, AU_EEPROM_SIZE_FIRMWARE, AU_EEPROM_DCRC_FIRMWARE,}, \
170 {AU_EEPROM_TIME_KERNEL, AU_EEPROM_SIZE_KERNEL, AU_EEPROM_DCRC_KERNEL,}, \
171 {AU_EEPROM_TIME_APP, AU_EEPROM_SIZE_APP, AU_EEPROM_DCRC_APP,}, \
172 {AU_EEPROM_TIME_DISK, AU_EEPROM_SIZE_DISK, AU_EEPROM_DCRC_DISK,}, \
173 {AU_EEPROM_TIME_POSTINST, AU_EEPROM_SIZE_POSTINST, AU_EEPROM_DCRC_POSTINST,} \
174 };
b0639ca3
WD
175/* array of flash areas start and end addresses */
176struct flash_layout aufl_layout[AU_MAXFILES - 3] = { \
177 {AU_FL_FIRMWARE_ST, AU_FL_FIRMWARE_ND,}, \
178 {AU_FL_KERNEL_ST, AU_FL_KERNEL_ND,}, \
179 {AU_FL_APP_ST, AU_FL_APP_ND,}, \
180 {AU_FL_DISK_ST, AU_FL_DISK_ND,}, \
181};
182/* convert the index into aufile[] to an index into aufl_layout[] */
183#define FIDX_TO_LIDX(idx) ((idx) - 2)
184
f54ebdfa 185/* where to load files into memory */
a0ff7f2e 186#define LOAD_ADDR ((unsigned char *)0x0C100000)
8a42eac7 187/* the app is the largest image */
188#define MAX_LOADSZ ausize[IDX_APP]
f54ebdfa
WD
189
190/* externals */
191extern int fat_register_device(block_dev_desc_t *, int);
192extern int file_fat_detectfs(void);
193extern long file_fat_read(const char *, void *, unsigned long);
194extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
195extern int i2c_write (uchar, uint, int , uchar* , int);
196#ifdef CONFIG_VFD
197extern int trab_vfd (ulong);
198extern int transfer_pic(unsigned char, unsigned char *, int, int);
199#endif
a0ff7f2e
WD
200extern int flash_sect_erase(ulong, ulong);
201extern int flash_sect_protect (int, ulong, ulong);
170d5e6d 202extern int flash_write (char *, ulong, ulong);
b0639ca3
WD
203/* change char* to void* to shutup the compiler */
204extern int i2c_write_multiple (uchar, uint, int, void *, int);
205extern int i2c_read_multiple (uchar, uint, int, void *, int);
2d5b561e 206extern int u_boot_hush_start(void);
f54ebdfa
WD
207
208int
fa1399ed
WD
209au_check_cksum_valid(int idx, long nbytes)
210{
211 image_header_t *hdr;
fa1399ed
WD
212
213 hdr = (image_header_t *)LOAD_ADDR;
214
b97a2a0a 215 if (nbytes != image_get_image_size (hdr))
fa1399ed
WD
216 {
217 printf ("Image %s bad total SIZE\n", aufile[idx]);
218 return -1;
219 }
220 /* check the data CRC */
b97a2a0a 221 if (!image_check_dcrc (hdr)) {
fa1399ed
WD
222 {
223 printf ("Image %s bad data checksum\n", aufile[idx]);
224 return -1;
225 }
226 return 0;
227}
228
229int
230au_check_header_valid(int idx, long nbytes)
f54ebdfa
WD
231{
232 image_header_t *hdr;
233 unsigned long checksum;
234 unsigned char buf[4];
235
236 hdr = (image_header_t *)LOAD_ADDR;
237 /* check the easy ones first */
b0639ca3 238#undef CHECK_VALID_DEBUG
f54ebdfa 239#ifdef CHECK_VALID_DEBUG
b97a2a0a
MB
240 printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC);
241 printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_ARM);
242 printf("size %#x %#lx ", image_get_data_size (hdr), nbytes);
243 printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL);
f54ebdfa 244#endif
b97a2a0a 245 if (nbytes < image_get_header_size ())
80369866 246 {
fa1399ed
WD
247 printf ("Image %s bad header SIZE\n", aufile[idx]);
248 return -1;
249 }
b97a2a0a 250 if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_ARM))
fa1399ed
WD
251 {
252 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
80369866 253 return -1;
f54ebdfa
WD
254 }
255 /* check the hdr CRC */
b97a2a0a 256 if (!image_check_hcrc (hdr)) {
80369866
WD
257 printf ("Image %s bad header checksum\n", aufile[idx]);
258 return -1;
f54ebdfa 259 }
f54ebdfa 260 /* check the type - could do this all in one gigantic if() */
b97a2a0a 261 if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
80369866
WD
262 printf ("Image %s wrong type\n", aufile[idx]);
263 return -1;
f54ebdfa 264 }
b97a2a0a 265 if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) {
80369866
WD
266 printf ("Image %s wrong type\n", aufile[idx]);
267 return -1;
f54ebdfa 268 }
b97a2a0a 269 if ((idx == IDX_DISK) && !image_check_type (hdr, IH_TYPE_FILESYSTEM)) {
d9a405aa
WD
270 printf ("Image %s wrong type\n", aufile[idx]);
271 return -1;
272 }
b97a2a0a
MB
273 if ((idx == IDX_APP) && !image_check_type (hdr, IH_TYPE_RAMDISK)
274 && !image_check_type (hdr, FILESYSTEM)) {
80369866
WD
275 printf ("Image %s wrong type\n", aufile[idx]);
276 return -1;
f54ebdfa
WD
277 }
278 if ((idx == IDX_PREPARE || idx == IDX_PREINST || idx == IDX_POSTINST)
b97a2a0a 279 && !image_check_type (hdr, IH_TYPE_SCRIPT))
f54ebdfa 280 {
80369866
WD
281 printf ("Image %s wrong type\n", aufile[idx]);
282 return -1;
f54ebdfa
WD
283 }
284 /* special case for prepare.img */
285 if (idx == IDX_PREPARE)
286 return 0;
d9a405aa 287 /* recycle checksum */
b97a2a0a 288 checksum = image_get_data_size (hdr);
d9a405aa 289 /* for kernel and app the image header must also fit into flash */
8b4c9e7c 290 if ((idx != IDX_DISK) && (idx != IDX_FIRMWARE))
b97a2a0a 291 checksum += image_get_header_size ();
d9a405aa
WD
292 /* check the size does not exceed space in flash. HUSH scripts */
293 /* all have ausize[] set to 0 */
294 if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
80369866
WD
295 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
296 return -1;
f54ebdfa
WD
297 }
298 /* check the time stamp from the EEPROM */
299 /* read it in */
300 i2c_read_multiple(0x54, auee_off[idx].time, 1, buf, sizeof(buf));
301#ifdef CHECK_VALID_DEBUG
80369866
WD
302 printf ("buf[0] %#x buf[1] %#x buf[2] %#x buf[3] %#x "
303 "as int %#x time %#x\n",
304 buf[0], buf[1], buf[2], buf[3],
b97a2a0a 305 *((unsigned int *)buf), image_get_time (hdr));
f54ebdfa
WD
306#endif
307 /* check it */
b97a2a0a 308 if (*((unsigned int *)buf) >= image_get_time (hdr)) {
80369866
WD
309 printf ("Image %s is too old\n", aufile[idx]);
310 return -1;
f54ebdfa
WD
311 }
312
313 return 0;
314}
315
316/* power control defines */
317#define CPLD_VFD_BK ((volatile char *)0x04038002)
318#define POWER_OFF (1 << 1)
319
320int
a0ff7f2e 321au_do_update(int idx, long sz)
f54ebdfa
WD
322{
323 image_header_t *hdr;
324 char *addr;
b0639ca3 325 long start, end;
a0ff7f2e 326 int off, rc;
b0639ca3 327 uint nbytes;
f54ebdfa
WD
328
329 hdr = (image_header_t *)LOAD_ADDR;
b0639ca3 330
f54ebdfa
WD
331 /* disable the power switch */
332 *CPLD_VFD_BK |= POWER_OFF;
b0639ca3 333
f54ebdfa 334 /* execute a script */
b97a2a0a
MB
335 if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
336 addr = (char *)((char *)hdr + image_get_header_size ());
fbe4b5cb
WD
337 /* stick a NULL at the end of the script, otherwise */
338 /* parse_string_outer() runs off the end. */
b97a2a0a 339 addr[image_get_data_size (hdr)] = 0;
fbe4b5cb
WD
340 addr += 8;
341 parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
f54ebdfa
WD
342 return 0;
343 }
b0639ca3
WD
344
345 start = aufl_layout[FIDX_TO_LIDX(idx)].start;
346 end = aufl_layout[FIDX_TO_LIDX(idx)].end;
347
348 /* unprotect the address range */
349 /* this assumes that ONLY the firmware is protected! */
350 if (idx == IDX_FIRMWARE) {
351#undef AU_UPDATE_TEST
352#ifdef AU_UPDATE_TEST
353 /* erase it where Linux goes */
354 start = aufl_layout[1].start;
355 end = aufl_layout[1].end;
356#endif
a0ff7f2e 357 flash_sect_protect(0, start, end);
b0639ca3
WD
358 }
359
80369866 360 /*
a0ff7f2e 361 * erase the address range.
80369866 362 */
a0ff7f2e
WD
363 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
364 flash_sect_erase(start, end);
80369866 365 wait_ms(100);
3d1e8a9d 366 /* strip the header - except for the kernel and ramdisk */
b97a2a0a
MB
367 if (image_check_type (hdr, IH_TYPE_KERNEL) ||
368 image_check_type (hdr, IH_TYPE_RAMDISK)) {
3d1e8a9d 369 addr = (char *)hdr;
b97a2a0a
MB
370 off = image_get_header_size ();
371 nbytes = image_get_image_size (hdr);
3d1e8a9d 372 } else {
b97a2a0a 373 addr = (char *)((char *)hdr + image_get_header_size ());
b0639ca3
WD
374#ifdef AU_UPDATE_TEST
375 /* copy it to where Linux goes */
376 if (idx == IDX_FIRMWARE)
377 start = aufl_layout[1].start;
378#endif
379 off = 0;
b97a2a0a 380 nbytes = image_get_data_size (hdr);
b0639ca3
WD
381 }
382
383 /* copy the data from RAM to FLASH */
a0ff7f2e
WD
384 debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
385 rc = flash_write(addr, start, nbytes);
386 if (rc != 0) {
387 printf("Flashing failed due to error %d\n", rc);
388 return -1;
389 }
b0639ca3
WD
390
391 /* check the dcrc of the copy */
b97a2a0a
MB
392 if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) !=
393 image_get_dcrc (hdr)) {
80369866
WD
394 printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
395 return -1;
b0639ca3
WD
396 }
397
398 /* protect the address range */
399 /* this assumes that ONLY the firmware is protected! */
a0ff7f2e
WD
400 if (idx == IDX_FIRMWARE)
401 flash_sect_protect(1, start, end);
f54ebdfa
WD
402 return 0;
403}
404
405int
406au_update_eeprom(int idx)
407{
408 image_header_t *hdr;
b0639ca3
WD
409 int off;
410 uint32_t val;
f54ebdfa 411
151ab83a
WD
412 /* special case for prepare.img */
413 if (idx == IDX_PREPARE) {
414 /* enable the power switch */
415 *CPLD_VFD_BK &= ~POWER_OFF;
416 return 0;
417 }
418
f54ebdfa 419 hdr = (image_header_t *)LOAD_ADDR;
b0639ca3
WD
420 /* write the time field into EEPROM */
421 off = auee_off[idx].time;
b97a2a0a 422 val = image_get_time (hdr);
b0639ca3
WD
423 i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
424 /* write the size field into EEPROM */
425 off = auee_off[idx].size;
b97a2a0a 426 val = image_get_data_size (hdr);
b0639ca3
WD
427 i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
428 /* write the dcrc field into EEPROM */
429 off = auee_off[idx].dcrc;
b97a2a0a 430 val = image_get_dcrc (hdr);
b0639ca3 431 i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
f54ebdfa
WD
432 /* enable the power switch */
433 *CPLD_VFD_BK &= ~POWER_OFF;
434 return 0;
435}
436
437/*
438 * this is called from board_init() after the hardware has been set up
439 * and is usable. That seems like a good time to do this.
440 * Right now the return value is ignored.
441 */
442int
443do_auto_update(void)
444{
445 block_dev_desc_t *stor_dev;
446 long sz;
a4d2636f 447 int i, res = 0, bitmap_first, cnt, old_ctrlc, got_ctrlc;
f54ebdfa 448 char *env;
80369866 449 long start, end;
f54ebdfa 450
b0639ca3 451#undef ERASE_EEPROM
f54ebdfa
WD
452#ifdef ERASE_EEPROM
453 int arr[18];
454 memset(arr, 0, sizeof(arr));
455 i2c_write_multiple(0x54, 64, 1, arr, sizeof(arr));
456#endif
457 au_usb_stor_curr_dev = -1;
458 /* start USB */
459 if (usb_stop() < 0) {
460 debug ("usb_stop failed\n");
461 return -1;
462 }
463 if (usb_init() < 0) {
464 debug ("usb_init failed\n");
465 return -1;
466 }
467 /*
468 * check whether a storage device is attached (assume that it's
469 * a USB memory stick, since nothing else should be attached).
470 */
9fd5e31f 471 au_usb_stor_curr_dev = usb_stor_scan(0);
f54ebdfa
WD
472 if (au_usb_stor_curr_dev == -1) {
473 debug ("No device found. Not initialized?\n");
a4d2636f
WD
474 res = -1;
475 goto xit;
f54ebdfa
WD
476 }
477 /* check whether it has a partition table */
80369866
WD
478 stor_dev = get_dev("usb", 0);
479 if (stor_dev == NULL) {
f54ebdfa 480 debug ("uknown device type\n");
a4d2636f
WD
481 res = -1;
482 goto xit;
f54ebdfa
WD
483 }
484 if (fat_register_device(stor_dev, 1) != 0) {
485 debug ("Unable to use USB %d:%d for fatls\n",
486 au_usb_stor_curr_dev, 1);
a4d2636f
WD
487 res = -1;
488 goto xit;
f54ebdfa
WD
489 }
490 if (file_fat_detectfs() != 0) {
491 debug ("file_fat_detectfs failed\n");
492 }
b0639ca3 493
f54ebdfa
WD
494 /* initialize the array of file names */
495 memset(aufile, 0, sizeof(aufile));
496 aufile[IDX_PREPARE] = AU_PREPARE;
497 aufile[IDX_PREINST] = AU_PREINST;
498 aufile[IDX_FIRMWARE] = AU_FIRMWARE;
499 aufile[IDX_KERNEL] = AU_KERNEL;
500 aufile[IDX_APP] = AU_APP;
501 aufile[IDX_DISK] = AU_DISK;
502 aufile[IDX_POSTINST] = AU_POSTINST;
503 /* initialize the array of flash sizes */
504 memset(ausize, 0, sizeof(ausize));
505 ausize[IDX_FIRMWARE] = (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST;
506 ausize[IDX_KERNEL] = (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST;
507 ausize[IDX_APP] = (AU_FL_APP_ND + 1) - AU_FL_APP_ST;
508 ausize[IDX_DISK] = (AU_FL_DISK_ND + 1) - AU_FL_DISK_ST;
80369866
WD
509 /*
510 * now check whether start and end are defined using environment
511 * variables.
512 */
1d70468b
WD
513 start = -1;
514 end = 0;
80369866
WD
515 env = getenv("firmware_st");
516 if (env != NULL)
517 start = simple_strtoul(env, NULL, 16);
518 env = getenv("firmware_nd");
519 if (env != NULL)
520 end = simple_strtoul(env, NULL, 16);
fbe4b5cb 521 if (start >= 0 && end && end > start) {
80369866 522 ausize[IDX_FIRMWARE] = (end + 1) - start;
fbe4b5cb
WD
523 aufl_layout[0].start = start;
524 aufl_layout[0].end = end;
525 }
1d70468b
WD
526 start = -1;
527 end = 0;
80369866
WD
528 env = getenv("kernel_st");
529 if (env != NULL)
530 start = simple_strtoul(env, NULL, 16);
531 env = getenv("kernel_nd");
532 if (env != NULL)
533 end = simple_strtoul(env, NULL, 16);
fbe4b5cb 534 if (start >= 0 && end && end > start) {
80369866 535 ausize[IDX_KERNEL] = (end + 1) - start;
fbe4b5cb
WD
536 aufl_layout[1].start = start;
537 aufl_layout[1].end = end;
538 }
1d70468b
WD
539 start = -1;
540 end = 0;
80369866
WD
541 env = getenv("app_st");
542 if (env != NULL)
543 start = simple_strtoul(env, NULL, 16);
544 env = getenv("app_nd");
545 if (env != NULL)
546 end = simple_strtoul(env, NULL, 16);
fbe4b5cb 547 if (start >= 0 && end && end > start) {
80369866 548 ausize[IDX_APP] = (end + 1) - start;
fbe4b5cb
WD
549 aufl_layout[2].start = start;
550 aufl_layout[2].end = end;
551 }
1d70468b
WD
552 start = -1;
553 end = 0;
80369866
WD
554 env = getenv("disk_st");
555 if (env != NULL)
556 start = simple_strtoul(env, NULL, 16);
557 env = getenv("disk_nd");
558 if (env != NULL)
559 end = simple_strtoul(env, NULL, 16);
fbe4b5cb 560 if (start >= 0 && end && end > start) {
80369866 561 ausize[IDX_DISK] = (end + 1) - start;
fbe4b5cb
WD
562 aufl_layout[3].start = start;
563 aufl_layout[3].end = end;
564 }
2d5b561e
WD
565 /* make certain that HUSH is runnable */
566 u_boot_hush_start();
80369866
WD
567 /* make sure that we see CTRL-C and save the old state */
568 old_ctrlc = disable_ctrlc(0);
569
f54ebdfa
WD
570 bitmap_first = 0;
571 /* just loop thru all the possible files */
572 for (i = 0; i < AU_MAXFILES; i++) {
fa1399ed 573 /* just read the header */
b97a2a0a 574 sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ());
fa1399ed 575 debug ("read %s sz %ld hdr %d\n",
b97a2a0a
MB
576 aufile[i], sz, image_get_header_size ());
577 if (sz <= 0 || sz < image_get_header_size ()) {
fa1399ed
WD
578 debug ("%s not found\n", aufile[i]);
579 continue;
580 }
581 if (au_check_header_valid(i, sz) < 0) {
582 debug ("%s header not valid\n", aufile[i]);
583 continue;
584 }
f54ebdfa
WD
585 sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
586 debug ("read %s sz %ld hdr %d\n",
b97a2a0a
MB
587 aufile[i], sz, image_get_header_size ());
588 if (sz <= 0 || sz <= image_get_header_size ()) {
f54ebdfa
WD
589 debug ("%s not found\n", aufile[i]);
590 continue;
591 }
fa1399ed
WD
592 if (au_check_cksum_valid(i, sz) < 0) {
593 debug ("%s checksum not valid\n", aufile[i]);
f54ebdfa
WD
594 continue;
595 }
596#ifdef CONFIG_VFD
597 /* now that we have a valid file we can display the */
598 /* bitmap. */
599 if (bitmap_first == 0) {
600 env = getenv("bitmap2");
601 if (env == NULL) {
602 trab_vfd(0);
603 } else {
604 /* not so simple - bitmap2 is supposed to */
605 /* contain the address of the bitmap */
606 env = (char *)simple_strtoul(env, NULL, 16);
607/* NOTE: these are taken from vfd_logo.h. If that file changes then */
608/* these defines MUST also be updated! These may be wrong for bitmap2. */
609#define VFD_LOGO_WIDTH 112
610#define VFD_LOGO_HEIGHT 72
611 /* must call transfer_pic directly */
a63c31cf
WD
612 transfer_pic(3, (unsigned char *)env,
613 VFD_LOGO_HEIGHT, VFD_LOGO_WIDTH);
f54ebdfa
WD
614 }
615 bitmap_first = 1;
616 }
617#endif
618 /* this is really not a good idea, but it's what the */
619 /* customer wants. */
b0639ca3 620 cnt = 0;
80369866 621 got_ctrlc = 0;
f54ebdfa 622 do {
a0ff7f2e 623 res = au_do_update(i, sz);
80369866
WD
624 /* let the user break out of the loop */
625 if (ctrlc() || had_ctrlc()) {
626 clear_ctrlc();
627 if (res < 0)
628 got_ctrlc = 1;
629 break;
630 }
b0639ca3 631 cnt++;
80369866 632#ifdef AU_TEST_ONLY
b0639ca3
WD
633 } while (res < 0 && cnt < 3);
634 if (cnt < 3)
635#else
f54ebdfa 636 } while (res < 0);
b0639ca3 637#endif
80369866
WD
638 /*
639 * it doesn't make sense to update the EEPROM if the
640 * update was interrupted by the user due to errors.
641 */
642 if (got_ctrlc == 0)
643 au_update_eeprom(i);
8a42eac7 644 else
645 /* enable the power switch */
646 *CPLD_VFD_BK &= ~POWER_OFF;
f54ebdfa 647 }
80369866
WD
648 /* restore the old state */
649 disable_ctrlc(old_ctrlc);
a4d2636f
WD
650xit:
651 usb_stop();
652 return res;
f54ebdfa
WD
653}
654#endif /* CONFIG_AUTO_UPDATE */