blob: 8f6753592b6fb08f6543bfdcdc03386f4cd22681 [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
Markus Klotzbuecher7b59b3c2006-11-27 11:44:58 +010037#ifndef CONFIG_USB_OHCI_NEW
wdenkf54ebdf2003-09-17 15:10:32 +000038#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
Jon Loeligerab3abcb2007-07-09 18:45:16 -050049#if !defined(CONFIG_CMD_FAT)
Jon Loeligerd39b5742007-07-10 10:48:22 -050050#error "must define CONFIG_CMD_FAT"
wdenkf54ebdf2003-09-17 15:10:32 +000051#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.
Wolfgang Denkc786f422006-07-19 14:40:43 +020060 * 3) if firmw_01.img is found load it into memory. If it is valid,
wdenkb0639ca2003-09-17 22:48:07 +000061 * burn it into FLASH and update the EEPROM.
Wolfgang Denkc786f422006-07-19 14:40:43 +020062 * 4) if kernl_01.img is found load it into memory. If it is valid,
wdenkb0639ca2003-09-17 22:48:07 +000063 * 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"
Wolfgang Denkc786f422006-07-19 14:40:43 +020084#define AU_FIRMWARE "firmw_01.img"
85#define AU_KERNEL "kernl_01.img"
wdenkf54ebdf2003-09-17 15:10:32 +000086#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);
Marian Balakowicz170d5e62005-10-28 15:29:43 +0200202extern int flash_write (char *, 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);
wdenk2d5b5612003-10-14 19:43:55 +0000206extern int u_boot_hush_start(void);
wdenkf54ebdf2003-09-17 15:10:32 +0000207
208int
wdenkfa1399e2003-12-06 11:20:01 +0000209au_check_cksum_valid(int idx, long nbytes)
210{
211 image_header_t *hdr;
wdenkfa1399e2003-12-06 11:20:01 +0000212
213 hdr = (image_header_t *)LOAD_ADDR;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100214#if defined(CONFIG_FIT)
215 if (gen_image_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
216 puts ("Non legacy image format not supported\n");
217 return -1;
218 }
219#endif
wdenkfa1399e2003-12-06 11:20:01 +0000220
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100221 if (nbytes != image_get_image_size (hdr))
wdenkfa1399e2003-12-06 11:20:01 +0000222 {
223 printf ("Image %s bad total SIZE\n", aufile[idx]);
224 return -1;
225 }
226 /* check the data CRC */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100227 if (!image_check_dcrc (hdr)) {
wdenkfa1399e2003-12-06 11:20:01 +0000228 {
229 printf ("Image %s bad data checksum\n", aufile[idx]);
230 return -1;
231 }
232 return 0;
233}
234
235int
236au_check_header_valid(int idx, long nbytes)
wdenkf54ebdf2003-09-17 15:10:32 +0000237{
238 image_header_t *hdr;
239 unsigned long checksum;
240 unsigned char buf[4];
241
242 hdr = (image_header_t *)LOAD_ADDR;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100243#if defined(CONFIG_FIT)
244 if (gen_image_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
245 puts ("Non legacy image format not supported\n");
246 return -1;
247 }
248#endif
249
wdenkf54ebdf2003-09-17 15:10:32 +0000250 /* check the easy ones first */
wdenkb0639ca2003-09-17 22:48:07 +0000251#undef CHECK_VALID_DEBUG
wdenkf54ebdf2003-09-17 15:10:32 +0000252#ifdef CHECK_VALID_DEBUG
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100253 printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC);
254 printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_ARM);
255 printf("size %#x %#lx ", image_get_data_size (hdr), nbytes);
256 printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL);
wdenkf54ebdf2003-09-17 15:10:32 +0000257#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100258 if (nbytes < image_get_header_size ())
wdenk80369862003-09-18 18:55:25 +0000259 {
wdenkfa1399e2003-12-06 11:20:01 +0000260 printf ("Image %s bad header SIZE\n", aufile[idx]);
261 return -1;
262 }
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100263 if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_ARM))
wdenkfa1399e2003-12-06 11:20:01 +0000264 {
265 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
wdenk80369862003-09-18 18:55:25 +0000266 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000267 }
268 /* check the hdr CRC */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100269 if (!image_check_hcrc (hdr)) {
wdenk80369862003-09-18 18:55:25 +0000270 printf ("Image %s bad header checksum\n", aufile[idx]);
271 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000272 }
wdenkf54ebdf2003-09-17 15:10:32 +0000273 /* check the type - could do this all in one gigantic if() */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100274 if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
wdenk80369862003-09-18 18:55:25 +0000275 printf ("Image %s wrong type\n", aufile[idx]);
276 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000277 }
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100278 if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) {
wdenk80369862003-09-18 18:55:25 +0000279 printf ("Image %s wrong type\n", aufile[idx]);
280 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000281 }
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100282 if ((idx == IDX_DISK) && !image_check_type (hdr, IH_TYPE_FILESYSTEM)) {
wdenkd9a405a2003-10-07 20:01:55 +0000283 printf ("Image %s wrong type\n", aufile[idx]);
284 return -1;
285 }
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100286 if ((idx == IDX_APP) && !image_check_type (hdr, IH_TYPE_RAMDISK)
287 && !image_check_type (hdr, FILESYSTEM)) {
wdenk80369862003-09-18 18:55:25 +0000288 printf ("Image %s wrong type\n", aufile[idx]);
289 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000290 }
291 if ((idx == IDX_PREPARE || idx == IDX_PREINST || idx == IDX_POSTINST)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100292 && !image_check_type (hdr, IH_TYPE_SCRIPT))
wdenkf54ebdf2003-09-17 15:10:32 +0000293 {
wdenk80369862003-09-18 18:55:25 +0000294 printf ("Image %s wrong type\n", aufile[idx]);
295 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000296 }
297 /* special case for prepare.img */
298 if (idx == IDX_PREPARE)
299 return 0;
wdenkd9a405a2003-10-07 20:01:55 +0000300 /* recycle checksum */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100301 checksum = image_get_data_size (hdr);
wdenkd9a405a2003-10-07 20:01:55 +0000302 /* for kernel and app the image header must also fit into flash */
Wolfgang Denk8b4c9e72005-09-21 15:31:25 +0200303 if ((idx != IDX_DISK) && (idx != IDX_FIRMWARE))
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100304 checksum += image_get_header_size ();
wdenkd9a405a2003-10-07 20:01:55 +0000305 /* check the size does not exceed space in flash. HUSH scripts */
306 /* all have ausize[] set to 0 */
307 if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
wdenk80369862003-09-18 18:55:25 +0000308 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
309 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000310 }
311 /* check the time stamp from the EEPROM */
312 /* read it in */
313 i2c_read_multiple(0x54, auee_off[idx].time, 1, buf, sizeof(buf));
314#ifdef CHECK_VALID_DEBUG
wdenk80369862003-09-18 18:55:25 +0000315 printf ("buf[0] %#x buf[1] %#x buf[2] %#x buf[3] %#x "
316 "as int %#x time %#x\n",
317 buf[0], buf[1], buf[2], buf[3],
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100318 *((unsigned int *)buf), image_get_time (hdr));
wdenkf54ebdf2003-09-17 15:10:32 +0000319#endif
320 /* check it */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100321 if (*((unsigned int *)buf) >= image_get_time (hdr)) {
wdenk80369862003-09-18 18:55:25 +0000322 printf ("Image %s is too old\n", aufile[idx]);
323 return -1;
wdenkf54ebdf2003-09-17 15:10:32 +0000324 }
325
326 return 0;
327}
328
329/* power control defines */
330#define CPLD_VFD_BK ((volatile char *)0x04038002)
331#define POWER_OFF (1 << 1)
332
333int
wdenka0ff7f22003-10-09 13:16:55 +0000334au_do_update(int idx, long sz)
wdenkf54ebdf2003-09-17 15:10:32 +0000335{
336 image_header_t *hdr;
337 char *addr;
wdenkb0639ca2003-09-17 22:48:07 +0000338 long start, end;
wdenka0ff7f22003-10-09 13:16:55 +0000339 int off, rc;
wdenkb0639ca2003-09-17 22:48:07 +0000340 uint nbytes;
wdenkf54ebdf2003-09-17 15:10:32 +0000341
342 hdr = (image_header_t *)LOAD_ADDR;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100343#if defined(CONFIG_FIT)
344 if (gen_image_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
345 puts ("Non legacy image format not supported\n");
346 return -1;
347 }
348#endif
wdenkb0639ca2003-09-17 22:48:07 +0000349
wdenkf54ebdf2003-09-17 15:10:32 +0000350 /* disable the power switch */
351 *CPLD_VFD_BK |= POWER_OFF;
wdenkb0639ca2003-09-17 22:48:07 +0000352
wdenkf54ebdf2003-09-17 15:10:32 +0000353 /* execute a script */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100354 if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
355 addr = (char *)((char *)hdr + image_get_header_size ());
wdenkfbe4b5c2003-10-06 21:55:32 +0000356 /* stick a NULL at the end of the script, otherwise */
357 /* parse_string_outer() runs off the end. */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100358 addr[image_get_data_size (hdr)] = 0;
wdenkfbe4b5c2003-10-06 21:55:32 +0000359 addr += 8;
360 parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
wdenkf54ebdf2003-09-17 15:10:32 +0000361 return 0;
362 }
wdenkb0639ca2003-09-17 22:48:07 +0000363
364 start = aufl_layout[FIDX_TO_LIDX(idx)].start;
365 end = aufl_layout[FIDX_TO_LIDX(idx)].end;
366
367 /* unprotect the address range */
368 /* this assumes that ONLY the firmware is protected! */
369 if (idx == IDX_FIRMWARE) {
370#undef AU_UPDATE_TEST
371#ifdef AU_UPDATE_TEST
372 /* erase it where Linux goes */
373 start = aufl_layout[1].start;
374 end = aufl_layout[1].end;
375#endif
wdenka0ff7f22003-10-09 13:16:55 +0000376 flash_sect_protect(0, start, end);
wdenkb0639ca2003-09-17 22:48:07 +0000377 }
378
wdenk80369862003-09-18 18:55:25 +0000379 /*
wdenka0ff7f22003-10-09 13:16:55 +0000380 * erase the address range.
wdenk80369862003-09-18 18:55:25 +0000381 */
wdenka0ff7f22003-10-09 13:16:55 +0000382 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
383 flash_sect_erase(start, end);
wdenk80369862003-09-18 18:55:25 +0000384 wait_ms(100);
wdenk3d1e8a92003-10-16 12:53:35 +0000385 /* strip the header - except for the kernel and ramdisk */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100386 if (image_check_type (hdr, IH_TYPE_KERNEL) ||
387 image_check_type (hdr, IH_TYPE_RAMDISK)) {
wdenk3d1e8a92003-10-16 12:53:35 +0000388 addr = (char *)hdr;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100389 off = image_get_header_size ();
390 nbytes = image_get_image_size (hdr);
wdenk3d1e8a92003-10-16 12:53:35 +0000391 } else {
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100392 addr = (char *)((char *)hdr + image_get_header_size ());
wdenkb0639ca2003-09-17 22:48:07 +0000393#ifdef AU_UPDATE_TEST
394 /* copy it to where Linux goes */
395 if (idx == IDX_FIRMWARE)
396 start = aufl_layout[1].start;
397#endif
398 off = 0;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100399 nbytes = image_get_data_size (hdr);
wdenkb0639ca2003-09-17 22:48:07 +0000400 }
401
402 /* copy the data from RAM to FLASH */
wdenka0ff7f22003-10-09 13:16:55 +0000403 debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
404 rc = flash_write(addr, start, nbytes);
405 if (rc != 0) {
406 printf("Flashing failed due to error %d\n", rc);
407 return -1;
408 }
wdenkb0639ca2003-09-17 22:48:07 +0000409
410 /* check the dcrc of the copy */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100411 if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) !=
412 image_get_dcrc (hdr)) {
wdenk80369862003-09-18 18:55:25 +0000413 printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
414 return -1;
wdenkb0639ca2003-09-17 22:48:07 +0000415 }
416
417 /* protect the address range */
418 /* this assumes that ONLY the firmware is protected! */
wdenka0ff7f22003-10-09 13:16:55 +0000419 if (idx == IDX_FIRMWARE)
420 flash_sect_protect(1, start, end);
wdenkf54ebdf2003-09-17 15:10:32 +0000421 return 0;
422}
423
424int
425au_update_eeprom(int idx)
426{
427 image_header_t *hdr;
wdenkb0639ca2003-09-17 22:48:07 +0000428 int off;
429 uint32_t val;
wdenkf54ebdf2003-09-17 15:10:32 +0000430
wdenk151ab832005-02-24 22:44:16 +0000431 /* special case for prepare.img */
432 if (idx == IDX_PREPARE) {
433 /* enable the power switch */
434 *CPLD_VFD_BK &= ~POWER_OFF;
435 return 0;
436 }
437
wdenkf54ebdf2003-09-17 15:10:32 +0000438 hdr = (image_header_t *)LOAD_ADDR;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100439#if defined(CONFIG_FIT)
440 if (gen_image_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
441 puts ("Non legacy image format not supported\n");
442 return -1;
443 }
444#endif
445
wdenkb0639ca2003-09-17 22:48:07 +0000446 /* write the time field into EEPROM */
447 off = auee_off[idx].time;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100448 val = image_get_time (hdr);
wdenkb0639ca2003-09-17 22:48:07 +0000449 i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
450 /* write the size field into EEPROM */
451 off = auee_off[idx].size;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100452 val = image_get_data_size (hdr);
wdenkb0639ca2003-09-17 22:48:07 +0000453 i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
454 /* write the dcrc field into EEPROM */
455 off = auee_off[idx].dcrc;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100456 val = image_get_dcrc (hdr);
wdenkb0639ca2003-09-17 22:48:07 +0000457 i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
wdenkf54ebdf2003-09-17 15:10:32 +0000458 /* enable the power switch */
459 *CPLD_VFD_BK &= ~POWER_OFF;
460 return 0;
461}
462
463/*
464 * this is called from board_init() after the hardware has been set up
465 * and is usable. That seems like a good time to do this.
466 * Right now the return value is ignored.
467 */
468int
469do_auto_update(void)
470{
471 block_dev_desc_t *stor_dev;
472 long sz;
Wolfgang Denka4d26362007-08-12 15:11:38 +0200473 int i, res = 0, bitmap_first, cnt, old_ctrlc, got_ctrlc;
wdenkf54ebdf2003-09-17 15:10:32 +0000474 char *env;
wdenk80369862003-09-18 18:55:25 +0000475 long start, end;
wdenkf54ebdf2003-09-17 15:10:32 +0000476
wdenkb0639ca2003-09-17 22:48:07 +0000477#undef ERASE_EEPROM
wdenkf54ebdf2003-09-17 15:10:32 +0000478#ifdef ERASE_EEPROM
479 int arr[18];
480 memset(arr, 0, sizeof(arr));
481 i2c_write_multiple(0x54, 64, 1, arr, sizeof(arr));
482#endif
483 au_usb_stor_curr_dev = -1;
484 /* start USB */
485 if (usb_stop() < 0) {
486 debug ("usb_stop failed\n");
487 return -1;
488 }
489 if (usb_init() < 0) {
490 debug ("usb_init failed\n");
491 return -1;
492 }
493 /*
494 * check whether a storage device is attached (assume that it's
495 * a USB memory stick, since nothing else should be attached).
496 */
wdenk9fd5e312003-12-07 23:55:12 +0000497 au_usb_stor_curr_dev = usb_stor_scan(0);
wdenkf54ebdf2003-09-17 15:10:32 +0000498 if (au_usb_stor_curr_dev == -1) {
499 debug ("No device found. Not initialized?\n");
Wolfgang Denka4d26362007-08-12 15:11:38 +0200500 res = -1;
501 goto xit;
wdenkf54ebdf2003-09-17 15:10:32 +0000502 }
503 /* check whether it has a partition table */
wdenk80369862003-09-18 18:55:25 +0000504 stor_dev = get_dev("usb", 0);
505 if (stor_dev == NULL) {
wdenkf54ebdf2003-09-17 15:10:32 +0000506 debug ("uknown device type\n");
Wolfgang Denka4d26362007-08-12 15:11:38 +0200507 res = -1;
508 goto xit;
wdenkf54ebdf2003-09-17 15:10:32 +0000509 }
510 if (fat_register_device(stor_dev, 1) != 0) {
511 debug ("Unable to use USB %d:%d for fatls\n",
512 au_usb_stor_curr_dev, 1);
Wolfgang Denka4d26362007-08-12 15:11:38 +0200513 res = -1;
514 goto xit;
wdenkf54ebdf2003-09-17 15:10:32 +0000515 }
516 if (file_fat_detectfs() != 0) {
517 debug ("file_fat_detectfs failed\n");
518 }
wdenkb0639ca2003-09-17 22:48:07 +0000519
wdenkf54ebdf2003-09-17 15:10:32 +0000520 /* initialize the array of file names */
521 memset(aufile, 0, sizeof(aufile));
522 aufile[IDX_PREPARE] = AU_PREPARE;
523 aufile[IDX_PREINST] = AU_PREINST;
524 aufile[IDX_FIRMWARE] = AU_FIRMWARE;
525 aufile[IDX_KERNEL] = AU_KERNEL;
526 aufile[IDX_APP] = AU_APP;
527 aufile[IDX_DISK] = AU_DISK;
528 aufile[IDX_POSTINST] = AU_POSTINST;
529 /* initialize the array of flash sizes */
530 memset(ausize, 0, sizeof(ausize));
531 ausize[IDX_FIRMWARE] = (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST;
532 ausize[IDX_KERNEL] = (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST;
533 ausize[IDX_APP] = (AU_FL_APP_ND + 1) - AU_FL_APP_ST;
534 ausize[IDX_DISK] = (AU_FL_DISK_ND + 1) - AU_FL_DISK_ST;
wdenk80369862003-09-18 18:55:25 +0000535 /*
536 * now check whether start and end are defined using environment
537 * variables.
538 */
wdenk1d704682003-09-19 08:29:25 +0000539 start = -1;
540 end = 0;
wdenk80369862003-09-18 18:55:25 +0000541 env = getenv("firmware_st");
542 if (env != NULL)
543 start = simple_strtoul(env, NULL, 16);
544 env = getenv("firmware_nd");
545 if (env != NULL)
546 end = simple_strtoul(env, NULL, 16);
wdenkfbe4b5c2003-10-06 21:55:32 +0000547 if (start >= 0 && end && end > start) {
wdenk80369862003-09-18 18:55:25 +0000548 ausize[IDX_FIRMWARE] = (end + 1) - start;
wdenkfbe4b5c2003-10-06 21:55:32 +0000549 aufl_layout[0].start = start;
550 aufl_layout[0].end = end;
551 }
wdenk1d704682003-09-19 08:29:25 +0000552 start = -1;
553 end = 0;
wdenk80369862003-09-18 18:55:25 +0000554 env = getenv("kernel_st");
555 if (env != NULL)
556 start = simple_strtoul(env, NULL, 16);
557 env = getenv("kernel_nd");
558 if (env != NULL)
559 end = simple_strtoul(env, NULL, 16);
wdenkfbe4b5c2003-10-06 21:55:32 +0000560 if (start >= 0 && end && end > start) {
wdenk80369862003-09-18 18:55:25 +0000561 ausize[IDX_KERNEL] = (end + 1) - start;
wdenkfbe4b5c2003-10-06 21:55:32 +0000562 aufl_layout[1].start = start;
563 aufl_layout[1].end = end;
564 }
wdenk1d704682003-09-19 08:29:25 +0000565 start = -1;
566 end = 0;
wdenk80369862003-09-18 18:55:25 +0000567 env = getenv("app_st");
568 if (env != NULL)
569 start = simple_strtoul(env, NULL, 16);
570 env = getenv("app_nd");
571 if (env != NULL)
572 end = simple_strtoul(env, NULL, 16);
wdenkfbe4b5c2003-10-06 21:55:32 +0000573 if (start >= 0 && end && end > start) {
wdenk80369862003-09-18 18:55:25 +0000574 ausize[IDX_APP] = (end + 1) - start;
wdenkfbe4b5c2003-10-06 21:55:32 +0000575 aufl_layout[2].start = start;
576 aufl_layout[2].end = end;
577 }
wdenk1d704682003-09-19 08:29:25 +0000578 start = -1;
579 end = 0;
wdenk80369862003-09-18 18:55:25 +0000580 env = getenv("disk_st");
581 if (env != NULL)
582 start = simple_strtoul(env, NULL, 16);
583 env = getenv("disk_nd");
584 if (env != NULL)
585 end = simple_strtoul(env, NULL, 16);
wdenkfbe4b5c2003-10-06 21:55:32 +0000586 if (start >= 0 && end && end > start) {
wdenk80369862003-09-18 18:55:25 +0000587 ausize[IDX_DISK] = (end + 1) - start;
wdenkfbe4b5c2003-10-06 21:55:32 +0000588 aufl_layout[3].start = start;
589 aufl_layout[3].end = end;
590 }
wdenk2d5b5612003-10-14 19:43:55 +0000591 /* make certain that HUSH is runnable */
592 u_boot_hush_start();
wdenk80369862003-09-18 18:55:25 +0000593 /* make sure that we see CTRL-C and save the old state */
594 old_ctrlc = disable_ctrlc(0);
595
wdenkf54ebdf2003-09-17 15:10:32 +0000596 bitmap_first = 0;
597 /* just loop thru all the possible files */
598 for (i = 0; i < AU_MAXFILES; i++) {
wdenkfa1399e2003-12-06 11:20:01 +0000599 /* just read the header */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100600 sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ());
wdenkfa1399e2003-12-06 11:20:01 +0000601 debug ("read %s sz %ld hdr %d\n",
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100602 aufile[i], sz, image_get_header_size ());
603 if (sz <= 0 || sz < image_get_header_size ()) {
wdenkfa1399e2003-12-06 11:20:01 +0000604 debug ("%s not found\n", aufile[i]);
605 continue;
606 }
607 if (au_check_header_valid(i, sz) < 0) {
608 debug ("%s header not valid\n", aufile[i]);
609 continue;
610 }
wdenkf54ebdf2003-09-17 15:10:32 +0000611 sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
612 debug ("read %s sz %ld hdr %d\n",
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100613 aufile[i], sz, image_get_header_size ());
614 if (sz <= 0 || sz <= image_get_header_size ()) {
wdenkf54ebdf2003-09-17 15:10:32 +0000615 debug ("%s not found\n", aufile[i]);
616 continue;
617 }
wdenkfa1399e2003-12-06 11:20:01 +0000618 if (au_check_cksum_valid(i, sz) < 0) {
619 debug ("%s checksum not valid\n", aufile[i]);
wdenkf54ebdf2003-09-17 15:10:32 +0000620 continue;
621 }
622#ifdef CONFIG_VFD
623 /* now that we have a valid file we can display the */
624 /* bitmap. */
625 if (bitmap_first == 0) {
626 env = getenv("bitmap2");
627 if (env == NULL) {
628 trab_vfd(0);
629 } else {
630 /* not so simple - bitmap2 is supposed to */
631 /* contain the address of the bitmap */
632 env = (char *)simple_strtoul(env, NULL, 16);
633/* NOTE: these are taken from vfd_logo.h. If that file changes then */
634/* these defines MUST also be updated! These may be wrong for bitmap2. */
635#define VFD_LOGO_WIDTH 112
636#define VFD_LOGO_HEIGHT 72
637 /* must call transfer_pic directly */
Wolfgang Denka63c31c2006-06-26 10:54:52 +0200638 transfer_pic(3, (unsigned char *)env,
639 VFD_LOGO_HEIGHT, VFD_LOGO_WIDTH);
wdenkf54ebdf2003-09-17 15:10:32 +0000640 }
641 bitmap_first = 1;
642 }
643#endif
644 /* this is really not a good idea, but it's what the */
645 /* customer wants. */
wdenkb0639ca2003-09-17 22:48:07 +0000646 cnt = 0;
wdenk80369862003-09-18 18:55:25 +0000647 got_ctrlc = 0;
wdenkf54ebdf2003-09-17 15:10:32 +0000648 do {
wdenka0ff7f22003-10-09 13:16:55 +0000649 res = au_do_update(i, sz);
wdenk80369862003-09-18 18:55:25 +0000650 /* let the user break out of the loop */
651 if (ctrlc() || had_ctrlc()) {
652 clear_ctrlc();
653 if (res < 0)
654 got_ctrlc = 1;
655 break;
656 }
wdenkb0639ca2003-09-17 22:48:07 +0000657 cnt++;
wdenk80369862003-09-18 18:55:25 +0000658#ifdef AU_TEST_ONLY
wdenkb0639ca2003-09-17 22:48:07 +0000659 } while (res < 0 && cnt < 3);
660 if (cnt < 3)
661#else
wdenkf54ebdf2003-09-17 15:10:32 +0000662 } while (res < 0);
wdenkb0639ca2003-09-17 22:48:07 +0000663#endif
wdenk80369862003-09-18 18:55:25 +0000664 /*
665 * it doesn't make sense to update the EEPROM if the
666 * update was interrupted by the user due to errors.
667 */
668 if (got_ctrlc == 0)
669 au_update_eeprom(i);
dzu8a42eac2003-09-29 21:55:54 +0000670 else
671 /* enable the power switch */
672 *CPLD_VFD_BK &= ~POWER_OFF;
wdenkf54ebdf2003-09-17 15:10:32 +0000673 }
wdenk80369862003-09-18 18:55:25 +0000674 /* restore the old state */
675 disable_ctrlc(old_ctrlc);
Wolfgang Denka4d26362007-08-12 15:11:38 +0200676xit:
677 usb_stop();
678 return res;
wdenkf54ebdf2003-09-17 15:10:32 +0000679}
680#endif /* CONFIG_AUTO_UPDATE */