blob: 59111852f15b87dc4ccb6b18b9ee5fd7f9576aba [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:
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?)
60 * 3)
61 * 4)
62 * 5)
63 * 6)
64 * 7)
65 * 8)
66 * 9)
67 * 10)
68 * 11)
69 * 12)
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"
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
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 */
114
115/* a structure with the offsets to values in the EEPROM */
116struct eeprom_layout
117{
118 unsigned int time;
119 unsigned int size;
120 unsigned int dcrc;
121};
122
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
142
143static 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 */
147char *aufile[AU_MAXFILES];
148/* sizes of flash areas for each file */
149long ausize[AU_MAXFILES];
150/* offsets into the EEEPROM */
151struct eeprom_layout auee_off[AU_MAXFILES] = { \
152 {0}, \
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,} \
159 };
160/* index of each file in the above array */
161#define IDX_PREPARE 0
162#define IDX_PREINST 1
163#define IDX_FIRMWARE 2
164#define IDX_KERNEL 3
165#define IDX_APP 4
166#define IDX_DISK 5
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]
172
173/* externals */
174extern int fat_register_device(block_dev_desc_t *, int);
175extern int file_fat_detectfs(void);
176extern long file_fat_read(const char *, void *, unsigned long);
177extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
178extern int i2c_write (uchar, uint, int , uchar* , int);
179#ifdef CONFIG_VFD
180extern int trab_vfd (ulong);
181extern int transfer_pic(unsigned char, unsigned char *, int, int);
182#endif
183extern int i2c_write_multiple (uchar, uint, int, uchar *, int);
184extern int i2c_read_multiple (uchar, uint, int, uchar *, int);
185
186
187int
188au_check_valid(int idx, long nbytes)
189{
190 image_header_t *hdr;
191 unsigned long checksum;
192 unsigned char buf[4];
193
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);
201#endif
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]);
206 return -1;
207 }
208 /* check the hdr CRC */
209 checksum = ntohl(hdr->ih_hcrc);
210 hdr->ih_hcrc = 0;
211
212 if (crc32 (0, (char *)hdr, sizeof(*hdr)) != checksum) {
213 printf ("Image %s Bad Header Checksum\n", aufile[idx]);
214 return -1;
215 }
216 /* check the data CRC */
217 checksum = ntohl(hdr->ih_dcrc);
218
219 if (crc32 (0, (char *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size))
220 != checksum)
221 {
222 printf ("Image %s Bad Data Checksum\n", aufile[idx]);
223 return -1;
224 }
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]);
228 return -1;
229 }
230 if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
231 printf ("Image %s Wrong Type\n", aufile[idx]);
232 return -1;
233 }
234 if ((idx == IDX_DISK || idx == IDX_APP)
235 && (hdr->ih_type != IH_TYPE_RAMDISK))
236 {
237 printf ("Image %s Wrong Type\n", aufile[idx]);
238 return -1;
239 }
240 if ((idx == IDX_PREPARE || idx == IDX_PREINST || idx == IDX_POSTINST)
241 && (hdr->ih_type != IH_TYPE_SCRIPT))
242 {
243 printf ("Image %s Wrong Type\n", aufile[idx]);
244 return -1;
245 }
246 /* special case for prepare.img */
247 if (idx == IDX_PREPARE)
248 return 0;
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]);
252 return -1;
253 }
254 /* check the time stamp from the EEPROM */
255 /* read it in */
256 i2c_read_multiple(0x54, auee_off[idx].time, 1, buf, sizeof(buf));
257#ifdef CHECK_VALID_DEBUG
258printf("buf[0] %#x buf[1] %#x buf[2] %#x buf[3] %#x as int %#x time %#x\n",
259buf[0], buf[1], buf[2], buf[3], *((unsigned int *)buf), ntohl(hdr->ih_time));
260#endif
261 /* check it */
262 if (*((unsigned int *)buf) >= ntohl(hdr->ih_time)) {
263 printf ("Image %s is too old\n", aufile[idx]);
264 return -1;
265 }
266
267 return 0;
268}
269
270/* power control defines */
271#define CPLD_VFD_BK ((volatile char *)0x04038002)
272#define POWER_OFF (1 << 1)
273
274int
275au_do_update(int idx)
276{
277 image_header_t *hdr;
278 char *addr;
279
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);
288 return 0;
289 }
290 return 0;
291}
292
293int
294au_update_eeprom(int idx)
295{
296 image_header_t *hdr;
297
298 hdr = (image_header_t *)LOAD_ADDR;
299 /* enable the power switch */
300 *CPLD_VFD_BK &= ~POWER_OFF;
301 return 0;
302}
303
304/*
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.
308 */
309int
310do_auto_update(void)
311{
312 block_dev_desc_t *stor_dev;
313 long sz;
314 int i, res, bitmap_first;
315 char *env;
316
317#ifdef ERASE_EEPROM
318 int arr[18];
319 memset(arr, 0, sizeof(arr));
320 i2c_write_multiple(0x54, 64, 1, arr, sizeof(arr));
321#endif
322 au_usb_stor_curr_dev = -1;
323 /* start USB */
324 if (usb_stop() < 0) {
325 debug ("usb_stop failed\n");
326 return -1;
327 }
328 if (usb_init() < 0) {
329 debug ("usb_init failed\n");
330 return -1;
331 }
332 /*
333 * check whether a storage device is attached (assume that it's
334 * a USB memory stick, since nothing else should be attached).
335 */
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");
339 return -1;
340 }
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");
345 return -1;
346 }
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);
350 return -1;
351 }
352 if (file_fat_detectfs() != 0) {
353 debug ("file_fat_detectfs failed\n");
354 }
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;
370 bitmap_first = 0;
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]);
378 continue;
379 }
380 if (au_check_valid(i, sz) < 0) {
381 debug ("%s not valid\n", aufile[i]);
382 continue;
383 }
384#ifdef CONFIG_VFD
385 /* now that we have a valid file we can display the */
386 /* bitmap. */
387 if (bitmap_first == 0) {
388 env = getenv("bitmap2");
389 if (env == NULL) {
390 trab_vfd(0);
391 } else {
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);
401 }
402 bitmap_first = 1;
403 }
404#endif
405 /* this is really not a good idea, but it's what the */
406 /* customer wants. */
407 do {
408 res = au_do_update(i);
409 } while (res < 0);
410 au_update_eeprom(i);
411 }
412 return 0;
413}
414#endif /* CONFIG_AUTO_UPDATE */