blob: dd62d0948372d36983bfbbc35d9239727dd963af [file] [log] [blame]
wdenkf54ebdf2003-09-17 15:10:32 +00001/*
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
37#ifndef CONFIG_USB_OHCI
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
49#if !(CONFIG_COMMANDS & CFG_CMD_FAT)
50#error "must define CFG_CMD_FAT"
51#endif
52
53/*
54 * Check whether a USB memory stick is plugged in.
55 * If one is found:
wdenkb0639ca2003-09-17 22:48:07 +000056 * 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.
60 * 3) if firmware.img is found load it into memory. If it is valid,
61 * burn it into FLASH and update the EEPROM.
62 * 4) if kernel.img is found load it into memory. If it is valid,
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.
wdenkf54ebdf2003-09-17 15:10:32 +000070 */
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"
84#define AU_FIRMWARE "firmware.img"
85#define AU_KERNEL "kernel.img"
86#define AU_APP "app.img"
87#define AU_DISK "disk.img"
88#define AU_POSTINST "postinst.img"
89
wdenkb0639ca2003-09-17 22:48:07 +000090struct flash_layout
91{
92 long start;
93 long end;
94};
95
wdenkf54ebdf2003-09-17 15:10:32 +000096/* layout of the FLASH. ST = start address, ND = end address. */
wdenkb0639ca2003-09-17 22:48:07 +000097#ifndef CONFIG_FLASH_8MB /* 16 MB Flash, 32 MB RAM */
wdenkf54ebdf2003-09-17 15:10:32 +000098#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
dzu8a42eac2003-09-29 21:55:54 +0000108#else /* 8 MB Flash, 32 MB RAM */
wdenkf54ebdf2003-09-17 15:10:32 +0000109#define AU_FL_FIRMWARE_ST 0x00000000
dzu8a42eac2003-09-29 21:55:54 +0000110#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
wdenkf54ebdf2003-09-17 15:10:32 +0000116#define AU_FL_DISK_ND 0x007DFFFF
117#define AU_FL_VFD_ST 0x007E0000
118#define AU_FL_VFD_ND 0x007FFFFF
wdenkb0639ca2003-09-17 22:48:07 +0000119#endif /* CONFIG_FLASH_8MB */
wdenkf54ebdf2003-09-17 15:10:32 +0000120
121/* a structure with the offsets to values in the EEPROM */
122struct eeprom_layout
123{
wdenkb0639ca2003-09-17 22:48:07 +0000124 int time;
125 int size;
126 int dcrc;
wdenkf54ebdf2003-09-17 15:10:32 +0000127};
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 */
wdenkb0639ca2003-09-17 22:48:07 +0000150
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
wdenkf54ebdf2003-09-17 15:10:32 +0000159/* 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 };
wdenkb0639ca2003-09-17 22:48:07 +0000175/* 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
wdenkf54ebdf2003-09-17 15:10:32 +0000185/* where to load files into memory */
wdenka0ff7f22003-10-09 13:16:55 +0000186#define LOAD_ADDR ((unsigned char *)0x0C100000)
dzu8a42eac2003-09-29 21:55:54 +0000187/* the app is the largest image */
188#define MAX_LOADSZ ausize[IDX_APP]
wdenkf54ebdf2003-09-17 15:10:32 +0000189
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
wdenka0ff7f22003-10-09 13:16:55 +0000200extern int flash_sect_erase(ulong, ulong);
201extern int flash_sect_protect (int, ulong, ulong);
202extern int flash_write (uchar *, ulong, ulong);
wdenkb0639ca2003-09-17 22:48:07 +0000203/* 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);
wdenk80369862003-09-18 18:55:25 +0000206extern block_dev_desc_t *get_dev (char*, int);
wdenk2d5b5612003-10-14 19:43:55 +0000207extern int u_boot_hush_start(void);
wdenkf54ebdf2003-09-17 15:10:32 +0000208
209int
210au_check_valid(int idx, long nbytes)
211{
212 image_header_t *hdr;
213 unsigned long checksum;
214 unsigned char buf[4];
215
216 hdr = (image_header_t *)LOAD_ADDR;
217 /* check the easy ones first */
wdenkb0639ca2003-09-17 22:48:07 +0000218#undef CHECK_VALID_DEBUG
wdenkf54ebdf2003-09-17 15:10:32 +0000219#ifdef CHECK_VALID_DEBUG
220 printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
221 printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_ARM);
222 printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
223 printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
224#endif
225 if (ntohl(hdr->ih_magic) != IH_MAGIC ||
wdenk80369862003-09-18 18:55:25 +0000226 hdr->ih_arch != IH_CPU_ARM ||
dzu8a42eac2003-09-29 21:55:54 +0000227 nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size)))
wdenk80369862003-09-18 18:55:25 +0000228 {
229 printf ("Image %s bad MAGIC or ARCH or SIZE\n", aufile[idx]);
230 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000231 }
232 /* check the hdr CRC */
233 checksum = ntohl(hdr->ih_hcrc);
234 hdr->ih_hcrc = 0;
235
236 if (crc32 (0, (char *)hdr, sizeof(*hdr)) != checksum) {
wdenk80369862003-09-18 18:55:25 +0000237 printf ("Image %s bad header checksum\n", aufile[idx]);
238 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000239 }
wdenk80369862003-09-18 18:55:25 +0000240 hdr->ih_hcrc = htonl(checksum);
wdenkf54ebdf2003-09-17 15:10:32 +0000241 /* check the data CRC */
242 checksum = ntohl(hdr->ih_dcrc);
243
244 if (crc32 (0, (char *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size))
245 != checksum)
246 {
wdenk80369862003-09-18 18:55:25 +0000247 printf ("Image %s bad data checksum\n", aufile[idx]);
248 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000249 }
250 /* check the type - could do this all in one gigantic if() */
251 if ((idx == IDX_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
wdenk80369862003-09-18 18:55:25 +0000252 printf ("Image %s wrong type\n", aufile[idx]);
253 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000254 }
255 if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
wdenk80369862003-09-18 18:55:25 +0000256 printf ("Image %s wrong type\n", aufile[idx]);
257 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000258 }
wdenkd9a405a2003-10-07 20:01:55 +0000259 if ((idx == IDX_DISK) && (hdr->ih_type != IH_TYPE_FILESYSTEM)) {
260 printf ("Image %s wrong type\n", aufile[idx]);
261 return -1;
262 }
263 if ((idx == IDX_APP) && (hdr->ih_type != IH_TYPE_RAMDISK)) {
wdenk80369862003-09-18 18:55:25 +0000264 printf ("Image %s wrong type\n", aufile[idx]);
265 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000266 }
267 if ((idx == IDX_PREPARE || idx == IDX_PREINST || idx == IDX_POSTINST)
268 && (hdr->ih_type != IH_TYPE_SCRIPT))
269 {
wdenk80369862003-09-18 18:55:25 +0000270 printf ("Image %s wrong type\n", aufile[idx]);
271 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000272 }
273 /* special case for prepare.img */
274 if (idx == IDX_PREPARE)
275 return 0;
wdenkd9a405a2003-10-07 20:01:55 +0000276 /* recycle checksum */
277 checksum = ntohl(hdr->ih_size);
278 /* for kernel and app the image header must also fit into flash */
279 if (idx != IDX_DISK)
280 checksum += sizeof(*hdr);
281 /* check the size does not exceed space in flash. HUSH scripts */
282 /* all have ausize[] set to 0 */
283 if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
wdenk80369862003-09-18 18:55:25 +0000284 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
285 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000286 }
287 /* check the time stamp from the EEPROM */
288 /* read it in */
289 i2c_read_multiple(0x54, auee_off[idx].time, 1, buf, sizeof(buf));
290#ifdef CHECK_VALID_DEBUG
wdenk80369862003-09-18 18:55:25 +0000291 printf ("buf[0] %#x buf[1] %#x buf[2] %#x buf[3] %#x "
292 "as int %#x time %#x\n",
293 buf[0], buf[1], buf[2], buf[3],
294 *((unsigned int *)buf), ntohl(hdr->ih_time));
wdenkf54ebdf2003-09-17 15:10:32 +0000295#endif
296 /* check it */
297 if (*((unsigned int *)buf) >= ntohl(hdr->ih_time)) {
wdenk80369862003-09-18 18:55:25 +0000298 printf ("Image %s is too old\n", aufile[idx]);
299 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000300 }
301
302 return 0;
303}
304
305/* power control defines */
306#define CPLD_VFD_BK ((volatile char *)0x04038002)
307#define POWER_OFF (1 << 1)
308
309int
wdenka0ff7f22003-10-09 13:16:55 +0000310au_do_update(int idx, long sz)
wdenkf54ebdf2003-09-17 15:10:32 +0000311{
312 image_header_t *hdr;
313 char *addr;
wdenkb0639ca2003-09-17 22:48:07 +0000314 long start, end;
wdenka0ff7f22003-10-09 13:16:55 +0000315 int off, rc;
wdenkb0639ca2003-09-17 22:48:07 +0000316 uint nbytes;
wdenkf54ebdf2003-09-17 15:10:32 +0000317
318 hdr = (image_header_t *)LOAD_ADDR;
wdenkb0639ca2003-09-17 22:48:07 +0000319
wdenkf54ebdf2003-09-17 15:10:32 +0000320 /* disable the power switch */
321 *CPLD_VFD_BK |= POWER_OFF;
wdenkb0639ca2003-09-17 22:48:07 +0000322
wdenkf54ebdf2003-09-17 15:10:32 +0000323 /* execute a script */
324 if (hdr->ih_type == IH_TYPE_SCRIPT) {
wdenkfbe4b5c2003-10-06 21:55:32 +0000325 addr = (char *)((char *)hdr + sizeof(*hdr));
326 /* stick a NULL at the end of the script, otherwise */
327 /* parse_string_outer() runs off the end. */
328 addr[ntohl(hdr->ih_size)] = 0;
329 addr += 8;
330 parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
wdenkf54ebdf2003-09-17 15:10:32 +0000331 return 0;
332 }
wdenkb0639ca2003-09-17 22:48:07 +0000333
334 start = aufl_layout[FIDX_TO_LIDX(idx)].start;
335 end = aufl_layout[FIDX_TO_LIDX(idx)].end;
336
337 /* unprotect the address range */
338 /* this assumes that ONLY the firmware is protected! */
339 if (idx == IDX_FIRMWARE) {
340#undef AU_UPDATE_TEST
341#ifdef AU_UPDATE_TEST
342 /* erase it where Linux goes */
343 start = aufl_layout[1].start;
344 end = aufl_layout[1].end;
345#endif
wdenka0ff7f22003-10-09 13:16:55 +0000346 flash_sect_protect(0, start, end);
wdenkb0639ca2003-09-17 22:48:07 +0000347 }
348
wdenk80369862003-09-18 18:55:25 +0000349 /*
wdenka0ff7f22003-10-09 13:16:55 +0000350 * erase the address range.
wdenk80369862003-09-18 18:55:25 +0000351 */
wdenka0ff7f22003-10-09 13:16:55 +0000352 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
353 flash_sect_erase(start, end);
wdenk80369862003-09-18 18:55:25 +0000354 wait_ms(100);
wdenk3d1e8a92003-10-16 12:53:35 +0000355 /* strip the header - except for the kernel and ramdisk */
356 if (hdr->ih_type == IH_TYPE_KERNEL || hdr->ih_type == IH_TYPE_RAMDISK) {
357 addr = (char *)hdr;
358 off = sizeof(*hdr);
359 nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
360 } else {
wdenkb0639ca2003-09-17 22:48:07 +0000361 addr = (char *)((char *)hdr + sizeof(*hdr));
362#ifdef AU_UPDATE_TEST
363 /* copy it to where Linux goes */
364 if (idx == IDX_FIRMWARE)
365 start = aufl_layout[1].start;
366#endif
367 off = 0;
368 nbytes = ntohl(hdr->ih_size);
wdenkb0639ca2003-09-17 22:48:07 +0000369 }
370
371 /* copy the data from RAM to FLASH */
wdenka0ff7f22003-10-09 13:16:55 +0000372 debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
373 rc = flash_write(addr, start, nbytes);
374 if (rc != 0) {
375 printf("Flashing failed due to error %d\n", rc);
376 return -1;
377 }
wdenkb0639ca2003-09-17 22:48:07 +0000378
379 /* check the dcrc of the copy */
wdenk80369862003-09-18 18:55:25 +0000380 if (crc32 (0, (char *)(start + off), ntohl(hdr->ih_size)) != ntohl(hdr->ih_dcrc)) {
381 printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
382 return -1;
wdenkb0639ca2003-09-17 22:48:07 +0000383 }
384
385 /* protect the address range */
386 /* this assumes that ONLY the firmware is protected! */
wdenka0ff7f22003-10-09 13:16:55 +0000387 if (idx == IDX_FIRMWARE)
388 flash_sect_protect(1, start, end);
wdenkf54ebdf2003-09-17 15:10:32 +0000389 return 0;
390}
391
392int
393au_update_eeprom(int idx)
394{
395 image_header_t *hdr;
wdenkb0639ca2003-09-17 22:48:07 +0000396 int off;
397 uint32_t val;
wdenkf54ebdf2003-09-17 15:10:32 +0000398
399 hdr = (image_header_t *)LOAD_ADDR;
wdenkb0639ca2003-09-17 22:48:07 +0000400 /* write the time field into EEPROM */
401 off = auee_off[idx].time;
402 val = ntohl(hdr->ih_time);
403 i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
404 /* write the size field into EEPROM */
405 off = auee_off[idx].size;
406 val = ntohl(hdr->ih_size);
407 i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
408 /* write the dcrc field into EEPROM */
409 off = auee_off[idx].dcrc;
410 val = ntohl(hdr->ih_dcrc);
411 i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
wdenkf54ebdf2003-09-17 15:10:32 +0000412 /* enable the power switch */
413 *CPLD_VFD_BK &= ~POWER_OFF;
414 return 0;
415}
416
417/*
418 * this is called from board_init() after the hardware has been set up
419 * and is usable. That seems like a good time to do this.
420 * Right now the return value is ignored.
421 */
422int
423do_auto_update(void)
424{
425 block_dev_desc_t *stor_dev;
426 long sz;
wdenk80369862003-09-18 18:55:25 +0000427 int i, res, bitmap_first, cnt, old_ctrlc, got_ctrlc;
wdenkf54ebdf2003-09-17 15:10:32 +0000428 char *env;
wdenk80369862003-09-18 18:55:25 +0000429 long start, end;
wdenkf54ebdf2003-09-17 15:10:32 +0000430
wdenkb0639ca2003-09-17 22:48:07 +0000431#undef ERASE_EEPROM
wdenkf54ebdf2003-09-17 15:10:32 +0000432#ifdef ERASE_EEPROM
433 int arr[18];
434 memset(arr, 0, sizeof(arr));
435 i2c_write_multiple(0x54, 64, 1, arr, sizeof(arr));
436#endif
437 au_usb_stor_curr_dev = -1;
438 /* start USB */
439 if (usb_stop() < 0) {
440 debug ("usb_stop failed\n");
441 return -1;
442 }
443 if (usb_init() < 0) {
444 debug ("usb_init failed\n");
445 return -1;
446 }
447 /*
448 * check whether a storage device is attached (assume that it's
449 * a USB memory stick, since nothing else should be attached).
450 */
451 au_usb_stor_curr_dev = usb_stor_scan(1);
452 if (au_usb_stor_curr_dev == -1) {
453 debug ("No device found. Not initialized?\n");
454 return -1;
455 }
456 /* check whether it has a partition table */
wdenk80369862003-09-18 18:55:25 +0000457 stor_dev = get_dev("usb", 0);
458 if (stor_dev == NULL) {
wdenkf54ebdf2003-09-17 15:10:32 +0000459 debug ("uknown device type\n");
460 return -1;
461 }
462 if (fat_register_device(stor_dev, 1) != 0) {
463 debug ("Unable to use USB %d:%d for fatls\n",
464 au_usb_stor_curr_dev, 1);
465 return -1;
466 }
467 if (file_fat_detectfs() != 0) {
468 debug ("file_fat_detectfs failed\n");
469 }
wdenkb0639ca2003-09-17 22:48:07 +0000470
wdenkf54ebdf2003-09-17 15:10:32 +0000471 /* initialize the array of file names */
472 memset(aufile, 0, sizeof(aufile));
473 aufile[IDX_PREPARE] = AU_PREPARE;
474 aufile[IDX_PREINST] = AU_PREINST;
475 aufile[IDX_FIRMWARE] = AU_FIRMWARE;
476 aufile[IDX_KERNEL] = AU_KERNEL;
477 aufile[IDX_APP] = AU_APP;
478 aufile[IDX_DISK] = AU_DISK;
479 aufile[IDX_POSTINST] = AU_POSTINST;
480 /* initialize the array of flash sizes */
481 memset(ausize, 0, sizeof(ausize));
482 ausize[IDX_FIRMWARE] = (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST;
483 ausize[IDX_KERNEL] = (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST;
484 ausize[IDX_APP] = (AU_FL_APP_ND + 1) - AU_FL_APP_ST;
485 ausize[IDX_DISK] = (AU_FL_DISK_ND + 1) - AU_FL_DISK_ST;
wdenk80369862003-09-18 18:55:25 +0000486 /*
487 * now check whether start and end are defined using environment
488 * variables.
489 */
wdenk1d704682003-09-19 08:29:25 +0000490 start = -1;
491 end = 0;
wdenk80369862003-09-18 18:55:25 +0000492 env = getenv("firmware_st");
493 if (env != NULL)
494 start = simple_strtoul(env, NULL, 16);
495 env = getenv("firmware_nd");
496 if (env != NULL)
497 end = simple_strtoul(env, NULL, 16);
wdenkfbe4b5c2003-10-06 21:55:32 +0000498 if (start >= 0 && end && end > start) {
wdenk80369862003-09-18 18:55:25 +0000499 ausize[IDX_FIRMWARE] = (end + 1) - start;
wdenkfbe4b5c2003-10-06 21:55:32 +0000500 aufl_layout[0].start = start;
501 aufl_layout[0].end = end;
502 }
wdenk1d704682003-09-19 08:29:25 +0000503 start = -1;
504 end = 0;
wdenk80369862003-09-18 18:55:25 +0000505 env = getenv("kernel_st");
506 if (env != NULL)
507 start = simple_strtoul(env, NULL, 16);
508 env = getenv("kernel_nd");
509 if (env != NULL)
510 end = simple_strtoul(env, NULL, 16);
wdenkfbe4b5c2003-10-06 21:55:32 +0000511 if (start >= 0 && end && end > start) {
wdenk80369862003-09-18 18:55:25 +0000512 ausize[IDX_KERNEL] = (end + 1) - start;
wdenkfbe4b5c2003-10-06 21:55:32 +0000513 aufl_layout[1].start = start;
514 aufl_layout[1].end = end;
515 }
wdenk1d704682003-09-19 08:29:25 +0000516 start = -1;
517 end = 0;
wdenk80369862003-09-18 18:55:25 +0000518 env = getenv("app_st");
519 if (env != NULL)
520 start = simple_strtoul(env, NULL, 16);
521 env = getenv("app_nd");
522 if (env != NULL)
523 end = simple_strtoul(env, NULL, 16);
wdenkfbe4b5c2003-10-06 21:55:32 +0000524 if (start >= 0 && end && end > start) {
wdenk80369862003-09-18 18:55:25 +0000525 ausize[IDX_APP] = (end + 1) - start;
wdenkfbe4b5c2003-10-06 21:55:32 +0000526 aufl_layout[2].start = start;
527 aufl_layout[2].end = end;
528 }
wdenk1d704682003-09-19 08:29:25 +0000529 start = -1;
530 end = 0;
wdenk80369862003-09-18 18:55:25 +0000531 env = getenv("disk_st");
532 if (env != NULL)
533 start = simple_strtoul(env, NULL, 16);
534 env = getenv("disk_nd");
535 if (env != NULL)
536 end = simple_strtoul(env, NULL, 16);
wdenkfbe4b5c2003-10-06 21:55:32 +0000537 if (start >= 0 && end && end > start) {
wdenk80369862003-09-18 18:55:25 +0000538 ausize[IDX_DISK] = (end + 1) - start;
wdenkfbe4b5c2003-10-06 21:55:32 +0000539 aufl_layout[3].start = start;
540 aufl_layout[3].end = end;
541 }
wdenk2d5b5612003-10-14 19:43:55 +0000542 /* make certain that HUSH is runnable */
543 u_boot_hush_start();
wdenk80369862003-09-18 18:55:25 +0000544 /* make sure that we see CTRL-C and save the old state */
545 old_ctrlc = disable_ctrlc(0);
546
wdenkf54ebdf2003-09-17 15:10:32 +0000547 bitmap_first = 0;
548 /* just loop thru all the possible files */
549 for (i = 0; i < AU_MAXFILES; i++) {
550 sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
551 debug ("read %s sz %ld hdr %d\n",
552 aufile[i], sz, sizeof(image_header_t));
553 if (sz <= 0 || sz <= sizeof(image_header_t)) {
554 debug ("%s not found\n", aufile[i]);
555 continue;
556 }
557 if (au_check_valid(i, sz) < 0) {
558 debug ("%s not valid\n", aufile[i]);
559 continue;
560 }
561#ifdef CONFIG_VFD
562 /* now that we have a valid file we can display the */
563 /* bitmap. */
564 if (bitmap_first == 0) {
565 env = getenv("bitmap2");
566 if (env == NULL) {
567 trab_vfd(0);
568 } else {
569 /* not so simple - bitmap2 is supposed to */
570 /* contain the address of the bitmap */
571 env = (char *)simple_strtoul(env, NULL, 16);
572/* NOTE: these are taken from vfd_logo.h. If that file changes then */
573/* these defines MUST also be updated! These may be wrong for bitmap2. */
574#define VFD_LOGO_WIDTH 112
575#define VFD_LOGO_HEIGHT 72
576 /* must call transfer_pic directly */
577 transfer_pic(3, env, VFD_LOGO_HEIGHT, VFD_LOGO_WIDTH);
578 }
579 bitmap_first = 1;
580 }
581#endif
582 /* this is really not a good idea, but it's what the */
583 /* customer wants. */
wdenkb0639ca2003-09-17 22:48:07 +0000584 cnt = 0;
wdenk80369862003-09-18 18:55:25 +0000585 got_ctrlc = 0;
wdenkf54ebdf2003-09-17 15:10:32 +0000586 do {
wdenka0ff7f22003-10-09 13:16:55 +0000587 res = au_do_update(i, sz);
wdenk80369862003-09-18 18:55:25 +0000588 /* let the user break out of the loop */
589 if (ctrlc() || had_ctrlc()) {
590 clear_ctrlc();
591 if (res < 0)
592 got_ctrlc = 1;
593 break;
594 }
wdenkb0639ca2003-09-17 22:48:07 +0000595 cnt++;
wdenk80369862003-09-18 18:55:25 +0000596#ifdef AU_TEST_ONLY
wdenkb0639ca2003-09-17 22:48:07 +0000597 } while (res < 0 && cnt < 3);
598 if (cnt < 3)
599#else
wdenkf54ebdf2003-09-17 15:10:32 +0000600 } while (res < 0);
wdenkb0639ca2003-09-17 22:48:07 +0000601#endif
wdenk80369862003-09-18 18:55:25 +0000602 /*
603 * it doesn't make sense to update the EEPROM if the
604 * update was interrupted by the user due to errors.
605 */
606 if (got_ctrlc == 0)
607 au_update_eeprom(i);
dzu8a42eac2003-09-29 21:55:54 +0000608 else
609 /* enable the power switch */
610 *CPLD_VFD_BK &= ~POWER_OFF;
wdenkf54ebdf2003-09-17 15:10:32 +0000611 }
wdenkb0639ca2003-09-17 22:48:07 +0000612 usb_stop();
wdenk80369862003-09-18 18:55:25 +0000613 /* restore the old state */
614 disable_ctrlc(old_ctrlc);
wdenkf54ebdf2003-09-17 15:10:32 +0000615 return 0;
616}
617#endif /* CONFIG_AUTO_UPDATE */