3 * Gary Jennejohn, DENX Software Engineering, gj@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
28 #include <asm/byteorder.h>
31 #ifdef CFG_HUSH_PARSER
35 #ifdef CONFIG_AUTO_UPDATE
37 #ifndef CONFIG_USB_OHCI
38 #error "must define CONFIG_USB_OHCI"
41 #ifndef CONFIG_USB_STORAGE
42 #error "must define CONFIG_USB_STORAGE"
45 #ifndef CFG_HUSH_PARSER
46 #error "must define CFG_HUSH_PARSER"
49 #if !(CONFIG_COMMANDS & CFG_CMD_FAT)
50 #error "must define CFG_CMD_FAT"
54 * Check whether a USB memory stick is plugged in.
56 * 1) try to do an MSDOS ls
57 * 2) if preinst.img is found and is not 0 length, load it into
58 * memory and run it (how to check beforehand if it's a
59 * valid HUSH command file?)
76 #define debug(fmt,args...) printf (fmt ,##args)
78 #define debug(fmt,args...)
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"
90 /* layout of the FLASH. ST = start address, ND = end address. */
91 #ifndef CONFIG_OLD_VERSION /* 16 MB Flash, 32 MB RAM */
92 #define AU_FL_FIRMWARE_ST 0x00000000
93 #define AU_FL_FIRMWARE_ND 0x0009FFFF
94 #define AU_FL_VFD_ST 0x000A0000
95 #define AU_FL_VFD_ND 0x000BFFFF
96 #define AU_FL_KERNEL_ST 0x000C0000
97 #define AU_FL_KERNEL_ND 0x001BFFFF
98 #define AU_FL_APP_ST 0x001C0000
99 #define AU_FL_APP_ND 0x005BFFFF
100 #define AU_FL_DISK_ST 0x005C0000
101 #define AU_FL_DISK_ND 0x00FFFFFF
102 #else /* 8 MB Flash, 16 MB RAM */
103 #define AU_FL_FIRMWARE_ST 0x00000000
104 #define AU_FL_FIRMWARE_ND 0x0003FFFF
105 #define AU_FL_KERNEL_ST 0x00040000
106 #define AU_FL_KERNEL_ND 0x0011FFFF
107 #define AU_FL_APP_ST 0x00120000
108 #define AU_FL_APP_ND 0x003FFFFF
109 #define AU_FL_DISK_ST 0x00400000
110 #define AU_FL_DISK_ND 0x007DFFFF
111 #define AU_FL_VFD_ST 0x007E0000
112 #define AU_FL_VFD_ND 0x007FFFFF
113 #endif /* CONFIG_OLD_VERSION */
115 /* a structure with the offsets to values in the EEPROM */
123 /* layout of the EEPROM - offset from the start. All entries are 32 bit. */
124 #define AU_EEPROM_TIME_PREINST 64
125 #define AU_EEPROM_SIZE_PREINST 68
126 #define AU_EEPROM_DCRC_PREINST 72
127 #define AU_EEPROM_TIME_FIRMWARE 76
128 #define AU_EEPROM_SIZE_FIRMWARE 80
129 #define AU_EEPROM_DCRC_FIRMWARE 84
130 #define AU_EEPROM_TIME_KERNEL 88
131 #define AU_EEPROM_SIZE_KERNEL 92
132 #define AU_EEPROM_DCRC_KERNEL 96
133 #define AU_EEPROM_TIME_APP 100
134 #define AU_EEPROM_SIZE_APP 104
135 #define AU_EEPROM_DCRC_APP 108
136 #define AU_EEPROM_TIME_DISK 112
137 #define AU_EEPROM_SIZE_DISK 116
138 #define AU_EEPROM_DCRC_DISK 120
139 #define AU_EEPROM_TIME_POSTINST 124
140 #define AU_EEPROM_SIZE_POSTINST 128
141 #define AU_EEPROM_DCRC_POSTINST 132
143 static int au_usb_stor_curr_dev; /* current device */
144 /* max. number of files which could interest us */
145 #define AU_MAXFILES 7
146 /* pointers to file names */
147 char *aufile[AU_MAXFILES];
148 /* sizes of flash areas for each file */
149 long ausize[AU_MAXFILES];
150 /* offsets into the EEEPROM */
151 struct eeprom_layout auee_off[AU_MAXFILES] = { \
153 {AU_EEPROM_TIME_PREINST, AU_EEPROM_SIZE_PREINST, AU_EEPROM_DCRC_PREINST,}, \
154 {AU_EEPROM_TIME_FIRMWARE, AU_EEPROM_SIZE_FIRMWARE, AU_EEPROM_DCRC_FIRMWARE,}, \
155 {AU_EEPROM_TIME_KERNEL, AU_EEPROM_SIZE_KERNEL, AU_EEPROM_DCRC_KERNEL,}, \
156 {AU_EEPROM_TIME_APP, AU_EEPROM_SIZE_APP, AU_EEPROM_DCRC_APP,}, \
157 {AU_EEPROM_TIME_DISK, AU_EEPROM_SIZE_DISK, AU_EEPROM_DCRC_DISK,}, \
158 {AU_EEPROM_TIME_POSTINST, AU_EEPROM_SIZE_POSTINST, AU_EEPROM_DCRC_POSTINST,} \
160 /* index of each file in the above array */
161 #define IDX_PREPARE 0
162 #define IDX_PREINST 1
163 #define IDX_FIRMWARE 2
167 #define IDX_POSTINST 6
168 /* where to load files into memory */
169 #define LOAD_ADDR ((unsigned char *)0x0c100000)
170 /* the disk is the largest image */
171 #define MAX_LOADSZ ausize[IDX_DISK]
174 extern int fat_register_device(block_dev_desc_t *, int);
175 extern int file_fat_detectfs(void);
176 extern long file_fat_read(const char *, void *, unsigned long);
177 extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
178 extern int i2c_write (uchar, uint, int , uchar* , int);
180 extern int trab_vfd (ulong);
181 extern int transfer_pic(unsigned char, unsigned char *, int, int);
183 extern int i2c_write_multiple (uchar, uint, int, uchar *, int);
184 extern int i2c_read_multiple (uchar, uint, int, uchar *, int);
188 au_check_valid(int idx, long nbytes)
191 unsigned long checksum;
192 unsigned char buf[4];
194 hdr = (image_header_t *)LOAD_ADDR;
195 /* check the easy ones first */
196 #ifdef CHECK_VALID_DEBUG
197 printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
198 printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_ARM);
199 printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
200 printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
202 if (ntohl(hdr->ih_magic) != IH_MAGIC ||
203 hdr->ih_arch != IH_CPU_ARM ||
204 nbytes < ntohl(hdr->ih_size)) {
205 printf ("Image %s Bad MAGIC or ARCH or SIZE\n", aufile[idx]);
208 /* check the hdr CRC */
209 checksum = ntohl(hdr->ih_hcrc);
212 if (crc32 (0, (char *)hdr, sizeof(*hdr)) != checksum) {
213 printf ("Image %s Bad Header Checksum\n", aufile[idx]);
216 /* check the data CRC */
217 checksum = ntohl(hdr->ih_dcrc);
219 if (crc32 (0, (char *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size))
222 printf ("Image %s Bad Data Checksum\n", aufile[idx]);
225 /* check the type - could do this all in one gigantic if() */
226 if ((idx == IDX_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
227 printf ("Image %s Wrong Type\n", aufile[idx]);
230 if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
231 printf ("Image %s Wrong Type\n", aufile[idx]);
234 if ((idx == IDX_DISK || idx == IDX_APP)
235 && (hdr->ih_type != IH_TYPE_RAMDISK))
237 printf ("Image %s Wrong Type\n", aufile[idx]);
240 if ((idx == IDX_PREPARE || idx == IDX_PREINST || idx == IDX_POSTINST)
241 && (hdr->ih_type != IH_TYPE_SCRIPT))
243 printf ("Image %s Wrong Type\n", aufile[idx]);
246 /* special case for prepare.img */
247 if (idx == IDX_PREPARE)
249 /* check the size does not exceed space in flash */
250 if ((ausize[idx] != 0) && (ausize[idx] < ntohl(hdr->ih_size))) {
251 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
254 /* check the time stamp from the EEPROM */
256 i2c_read_multiple(0x54, auee_off[idx].time, 1, buf, sizeof(buf));
257 #ifdef CHECK_VALID_DEBUG
258 printf("buf[0] %#x buf[1] %#x buf[2] %#x buf[3] %#x as int %#x time %#x\n",
259 buf[0], buf[1], buf[2], buf[3], *((unsigned int *)buf), ntohl(hdr->ih_time));
262 if (*((unsigned int *)buf) >= ntohl(hdr->ih_time)) {
263 printf ("Image %s is too old\n", aufile[idx]);
270 /* power control defines */
271 #define CPLD_VFD_BK ((volatile char *)0x04038002)
272 #define POWER_OFF (1 << 1)
275 au_do_update(int idx)
280 hdr = (image_header_t *)LOAD_ADDR;
281 /* disable the power switch */
282 *CPLD_VFD_BK |= POWER_OFF;
283 /* execute a script */
284 if (hdr->ih_type == IH_TYPE_SCRIPT) {
285 addr = (char *)((char *)hdr + sizeof(*hdr) + 8);
286 parse_string_outer(addr,
287 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
294 au_update_eeprom(int idx)
298 hdr = (image_header_t *)LOAD_ADDR;
299 /* enable the power switch */
300 *CPLD_VFD_BK &= ~POWER_OFF;
305 * this is called from board_init() after the hardware has been set up
306 * and is usable. That seems like a good time to do this.
307 * Right now the return value is ignored.
312 block_dev_desc_t *stor_dev;
314 int i, res, bitmap_first;
319 memset(arr, 0, sizeof(arr));
320 i2c_write_multiple(0x54, 64, 1, arr, sizeof(arr));
322 au_usb_stor_curr_dev = -1;
324 if (usb_stop() < 0) {
325 debug ("usb_stop failed\n");
328 if (usb_init() < 0) {
329 debug ("usb_init failed\n");
333 * check whether a storage device is attached (assume that it's
334 * a USB memory stick, since nothing else should be attached).
336 au_usb_stor_curr_dev = usb_stor_scan(1);
337 if (au_usb_stor_curr_dev == -1) {
338 debug ("No device found. Not initialized?\n");
341 /* check whether it has a partition table */
342 stor_dev = usb_stor_get_dev(au_usb_stor_curr_dev);
343 if (stor_dev->type == DEV_TYPE_UNKNOWN) {
344 debug ("uknown device type\n");
347 if (fat_register_device(stor_dev, 1) != 0) {
348 debug ("Unable to use USB %d:%d for fatls\n",
349 au_usb_stor_curr_dev, 1);
352 if (file_fat_detectfs() != 0) {
353 debug ("file_fat_detectfs failed\n");
355 /* initialize the array of file names */
356 memset(aufile, 0, sizeof(aufile));
357 aufile[IDX_PREPARE] = AU_PREPARE;
358 aufile[IDX_PREINST] = AU_PREINST;
359 aufile[IDX_FIRMWARE] = AU_FIRMWARE;
360 aufile[IDX_KERNEL] = AU_KERNEL;
361 aufile[IDX_APP] = AU_APP;
362 aufile[IDX_DISK] = AU_DISK;
363 aufile[IDX_POSTINST] = AU_POSTINST;
364 /* initialize the array of flash sizes */
365 memset(ausize, 0, sizeof(ausize));
366 ausize[IDX_FIRMWARE] = (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST;
367 ausize[IDX_KERNEL] = (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST;
368 ausize[IDX_APP] = (AU_FL_APP_ND + 1) - AU_FL_APP_ST;
369 ausize[IDX_DISK] = (AU_FL_DISK_ND + 1) - AU_FL_DISK_ST;
371 /* just loop thru all the possible files */
372 for (i = 0; i < AU_MAXFILES; i++) {
373 sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
374 debug ("read %s sz %ld hdr %d\n",
375 aufile[i], sz, sizeof(image_header_t));
376 if (sz <= 0 || sz <= sizeof(image_header_t)) {
377 debug ("%s not found\n", aufile[i]);
380 if (au_check_valid(i, sz) < 0) {
381 debug ("%s not valid\n", aufile[i]);
385 /* now that we have a valid file we can display the */
387 if (bitmap_first == 0) {
388 env = getenv("bitmap2");
392 /* not so simple - bitmap2 is supposed to */
393 /* contain the address of the bitmap */
394 env = (char *)simple_strtoul(env, NULL, 16);
395 /* NOTE: these are taken from vfd_logo.h. If that file changes then */
396 /* these defines MUST also be updated! These may be wrong for bitmap2. */
397 #define VFD_LOGO_WIDTH 112
398 #define VFD_LOGO_HEIGHT 72
399 /* must call transfer_pic directly */
400 transfer_pic(3, env, VFD_LOGO_HEIGHT, VFD_LOGO_WIDTH);
405 /* this is really not a good idea, but it's what the */
406 /* customer wants. */
408 res = au_do_update(i);
414 #endif /* CONFIG_AUTO_UPDATE */