]> git.ipfire.org Git - people/ms/u-boot.git/blame - board/trab/auto_update.c
* Patch by Rune Torgersen, 17 Sep 2003:
[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
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:
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.
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.
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"
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
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
108#else /* 8 MB Flash, 16 MB RAM */
109#define AU_FL_FIRMWARE_ST 0x00000000
110#define AU_FL_FIRMWARE_ND 0x0003FFFF
111#define AU_FL_KERNEL_ST 0x00040000
112#define AU_FL_KERNEL_ND 0x0011FFFF
113#define AU_FL_APP_ST 0x00120000
114#define AU_FL_APP_ND 0x003FFFFF
115#define AU_FL_DISK_ST 0x00400000
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 */
b0639ca3
WD
186#define LOAD_ADDR ((unsigned char *)0x0C100100)
187/* where to build strings in memory - 256 bytes should be enough */
188#define STRING_ADDR ((char *)0x0C100000)
f54ebdfa
WD
189/* the disk is the largest image */
190#define MAX_LOADSZ ausize[IDX_DISK]
191
192/* externals */
193extern int fat_register_device(block_dev_desc_t *, int);
194extern int file_fat_detectfs(void);
195extern long file_fat_read(const char *, void *, unsigned long);
196extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
197extern int i2c_write (uchar, uint, int , uchar* , int);
198#ifdef CONFIG_VFD
199extern int trab_vfd (ulong);
200extern int transfer_pic(unsigned char, unsigned char *, int, int);
201#endif
b0639ca3
WD
202/* change char* to void* to shutup the compiler */
203extern int i2c_write_multiple (uchar, uint, int, void *, int);
204extern int i2c_read_multiple (uchar, uint, int, void *, int);
f54ebdfa
WD
205
206int
207au_check_valid(int idx, long nbytes)
208{
209 image_header_t *hdr;
210 unsigned long checksum;
211 unsigned char buf[4];
212
213 hdr = (image_header_t *)LOAD_ADDR;
214 /* check the easy ones first */
b0639ca3 215#undef CHECK_VALID_DEBUG
f54ebdfa
WD
216#ifdef CHECK_VALID_DEBUG
217 printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
218 printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_ARM);
219 printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
220 printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
221#endif
222 if (ntohl(hdr->ih_magic) != IH_MAGIC ||
223 hdr->ih_arch != IH_CPU_ARM ||
224 nbytes < ntohl(hdr->ih_size)) {
b0639ca3 225 printf ("Image %s bad MAGIC or ARCH or SIZE\n", aufile[idx]);
f54ebdfa
WD
226 return -1;
227 }
228 /* check the hdr CRC */
229 checksum = ntohl(hdr->ih_hcrc);
230 hdr->ih_hcrc = 0;
231
232 if (crc32 (0, (char *)hdr, sizeof(*hdr)) != checksum) {
b0639ca3 233 printf ("Image %s bad header checksum\n", aufile[idx]);
f54ebdfa
WD
234 return -1;
235 }
236 /* check the data CRC */
237 checksum = ntohl(hdr->ih_dcrc);
238
239 if (crc32 (0, (char *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size))
240 != checksum)
241 {
b0639ca3 242 printf ("Image %s bad data checksum\n", aufile[idx]);
f54ebdfa
WD
243 return -1;
244 }
245 /* check the type - could do this all in one gigantic if() */
246 if ((idx == IDX_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
b0639ca3 247 printf ("Image %s wrong type\n", aufile[idx]);
f54ebdfa
WD
248 return -1;
249 }
250 if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
b0639ca3 251 printf ("Image %s wrong type\n", aufile[idx]);
f54ebdfa
WD
252 return -1;
253 }
254 if ((idx == IDX_DISK || idx == IDX_APP)
255 && (hdr->ih_type != IH_TYPE_RAMDISK))
256 {
b0639ca3 257 printf ("Image %s wrong type\n", aufile[idx]);
f54ebdfa
WD
258 return -1;
259 }
260 if ((idx == IDX_PREPARE || idx == IDX_PREINST || idx == IDX_POSTINST)
261 && (hdr->ih_type != IH_TYPE_SCRIPT))
262 {
b0639ca3 263 printf ("Image %s wrong type\n", aufile[idx]);
f54ebdfa
WD
264 return -1;
265 }
266 /* special case for prepare.img */
267 if (idx == IDX_PREPARE)
268 return 0;
269 /* check the size does not exceed space in flash */
270 if ((ausize[idx] != 0) && (ausize[idx] < ntohl(hdr->ih_size))) {
271 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
272 return -1;
273 }
274 /* check the time stamp from the EEPROM */
275 /* read it in */
276 i2c_read_multiple(0x54, auee_off[idx].time, 1, buf, sizeof(buf));
277#ifdef CHECK_VALID_DEBUG
278printf("buf[0] %#x buf[1] %#x buf[2] %#x buf[3] %#x as int %#x time %#x\n",
279buf[0], buf[1], buf[2], buf[3], *((unsigned int *)buf), ntohl(hdr->ih_time));
280#endif
281 /* check it */
282 if (*((unsigned int *)buf) >= ntohl(hdr->ih_time)) {
283 printf ("Image %s is too old\n", aufile[idx]);
284 return -1;
285 }
286
287 return 0;
288}
289
290/* power control defines */
291#define CPLD_VFD_BK ((volatile char *)0x04038002)
292#define POWER_OFF (1 << 1)
293
294int
b0639ca3 295au_do_update(int idx, long sz)
f54ebdfa
WD
296{
297 image_header_t *hdr;
298 char *addr;
b0639ca3
WD
299 long start, end;
300 char *strbuf = STRING_ADDR;
301 int off;
302 uint nbytes;
f54ebdfa
WD
303
304 hdr = (image_header_t *)LOAD_ADDR;
b0639ca3 305
f54ebdfa
WD
306 /* disable the power switch */
307 *CPLD_VFD_BK |= POWER_OFF;
b0639ca3 308
f54ebdfa
WD
309 /* execute a script */
310 if (hdr->ih_type == IH_TYPE_SCRIPT) {
311 addr = (char *)((char *)hdr + sizeof(*hdr) + 8);
312 parse_string_outer(addr,
313 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
314 return 0;
315 }
b0639ca3
WD
316
317 start = aufl_layout[FIDX_TO_LIDX(idx)].start;
318 end = aufl_layout[FIDX_TO_LIDX(idx)].end;
319
320 /* unprotect the address range */
321 /* this assumes that ONLY the firmware is protected! */
322 if (idx == IDX_FIRMWARE) {
323#undef AU_UPDATE_TEST
324#ifdef AU_UPDATE_TEST
325 /* erase it where Linux goes */
326 start = aufl_layout[1].start;
327 end = aufl_layout[1].end;
328#endif
329 debug ("protect off %lx %lx\n", start, end);
330 sprintf(strbuf, "protect off %lx %lx\n", start, end);
331 parse_string_outer(strbuf,
332 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
333 }
334
335 /* erase the address range */
336 debug ("erase %lx %lx\n", start, end);
337 sprintf(strbuf, "erase %lx %lx\n", start, end);
338 parse_string_outer(strbuf,
339 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
340
341 /* strip the header - except for the kernel */
342 if (idx == IDX_FIRMWARE || idx == IDX_DISK || idx == IDX_APP) {
343 addr = (char *)((char *)hdr + sizeof(*hdr));
344#ifdef AU_UPDATE_TEST
345 /* copy it to where Linux goes */
346 if (idx == IDX_FIRMWARE)
347 start = aufl_layout[1].start;
348#endif
349 off = 0;
350 nbytes = ntohl(hdr->ih_size);
351 } else {
352 addr = (char *)hdr;
353 off = sizeof(*hdr);
354 nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
355 }
356
357 /* copy the data from RAM to FLASH */
358 debug ("cp.b %p %lx %x\n", addr, start, nbytes);
359 sprintf(strbuf, "cp.b %p %lx %x\n", addr, start, nbytes);
360 parse_string_outer(strbuf,
361 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
362
363 /* check the dcrc of the copy */
364 if (crc32 (0, (char *)(start + off), ntohl(hdr->ih_size))
365 != ntohl(hdr->ih_dcrc)) {
366 printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
367 return -1;
368 }
369
370 /* protect the address range */
371 /* this assumes that ONLY the firmware is protected! */
372 if (idx == IDX_FIRMWARE) {
373 printf("protect on %lx %lx\n", start, end);
374 sprintf(strbuf, "protect on %lx %lx\n", start, end);
375 parse_string_outer(strbuf,
376 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
377 }
f54ebdfa
WD
378 return 0;
379}
380
381int
382au_update_eeprom(int idx)
383{
384 image_header_t *hdr;
b0639ca3
WD
385 int off;
386 uint32_t val;
f54ebdfa
WD
387
388 hdr = (image_header_t *)LOAD_ADDR;
b0639ca3
WD
389 /* write the time field into EEPROM */
390 off = auee_off[idx].time;
391 val = ntohl(hdr->ih_time);
392 i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
393 /* write the size field into EEPROM */
394 off = auee_off[idx].size;
395 val = ntohl(hdr->ih_size);
396 i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
397 /* write the dcrc field into EEPROM */
398 off = auee_off[idx].dcrc;
399 val = ntohl(hdr->ih_dcrc);
400 i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
f54ebdfa
WD
401 /* enable the power switch */
402 *CPLD_VFD_BK &= ~POWER_OFF;
403 return 0;
404}
405
406/*
407 * this is called from board_init() after the hardware has been set up
408 * and is usable. That seems like a good time to do this.
409 * Right now the return value is ignored.
410 */
411int
412do_auto_update(void)
413{
414 block_dev_desc_t *stor_dev;
415 long sz;
b0639ca3 416 int i, res, bitmap_first, cnt;
f54ebdfa
WD
417 char *env;
418
b0639ca3 419#undef ERASE_EEPROM
f54ebdfa
WD
420#ifdef ERASE_EEPROM
421 int arr[18];
422 memset(arr, 0, sizeof(arr));
423 i2c_write_multiple(0x54, 64, 1, arr, sizeof(arr));
424#endif
425 au_usb_stor_curr_dev = -1;
426 /* start USB */
427 if (usb_stop() < 0) {
428 debug ("usb_stop failed\n");
429 return -1;
430 }
431 if (usb_init() < 0) {
432 debug ("usb_init failed\n");
433 return -1;
434 }
435 /*
436 * check whether a storage device is attached (assume that it's
437 * a USB memory stick, since nothing else should be attached).
438 */
439 au_usb_stor_curr_dev = usb_stor_scan(1);
440 if (au_usb_stor_curr_dev == -1) {
441 debug ("No device found. Not initialized?\n");
442 return -1;
443 }
444 /* check whether it has a partition table */
445 stor_dev = usb_stor_get_dev(au_usb_stor_curr_dev);
446 if (stor_dev->type == DEV_TYPE_UNKNOWN) {
447 debug ("uknown device type\n");
448 return -1;
449 }
450 if (fat_register_device(stor_dev, 1) != 0) {
451 debug ("Unable to use USB %d:%d for fatls\n",
452 au_usb_stor_curr_dev, 1);
453 return -1;
454 }
455 if (file_fat_detectfs() != 0) {
456 debug ("file_fat_detectfs failed\n");
457 }
b0639ca3 458
f54ebdfa
WD
459 /* initialize the array of file names */
460 memset(aufile, 0, sizeof(aufile));
461 aufile[IDX_PREPARE] = AU_PREPARE;
462 aufile[IDX_PREINST] = AU_PREINST;
463 aufile[IDX_FIRMWARE] = AU_FIRMWARE;
464 aufile[IDX_KERNEL] = AU_KERNEL;
465 aufile[IDX_APP] = AU_APP;
466 aufile[IDX_DISK] = AU_DISK;
467 aufile[IDX_POSTINST] = AU_POSTINST;
468 /* initialize the array of flash sizes */
469 memset(ausize, 0, sizeof(ausize));
470 ausize[IDX_FIRMWARE] = (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST;
471 ausize[IDX_KERNEL] = (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST;
472 ausize[IDX_APP] = (AU_FL_APP_ND + 1) - AU_FL_APP_ST;
473 ausize[IDX_DISK] = (AU_FL_DISK_ND + 1) - AU_FL_DISK_ST;
474 bitmap_first = 0;
475 /* just loop thru all the possible files */
476 for (i = 0; i < AU_MAXFILES; i++) {
477 sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
478 debug ("read %s sz %ld hdr %d\n",
479 aufile[i], sz, sizeof(image_header_t));
480 if (sz <= 0 || sz <= sizeof(image_header_t)) {
481 debug ("%s not found\n", aufile[i]);
482 continue;
483 }
484 if (au_check_valid(i, sz) < 0) {
485 debug ("%s not valid\n", aufile[i]);
486 continue;
487 }
488#ifdef CONFIG_VFD
489 /* now that we have a valid file we can display the */
490 /* bitmap. */
491 if (bitmap_first == 0) {
492 env = getenv("bitmap2");
493 if (env == NULL) {
494 trab_vfd(0);
495 } else {
496 /* not so simple - bitmap2 is supposed to */
497 /* contain the address of the bitmap */
498 env = (char *)simple_strtoul(env, NULL, 16);
499/* NOTE: these are taken from vfd_logo.h. If that file changes then */
500/* these defines MUST also be updated! These may be wrong for bitmap2. */
501#define VFD_LOGO_WIDTH 112
502#define VFD_LOGO_HEIGHT 72
503 /* must call transfer_pic directly */
504 transfer_pic(3, env, VFD_LOGO_HEIGHT, VFD_LOGO_WIDTH);
505 }
506 bitmap_first = 1;
507 }
508#endif
509 /* this is really not a good idea, but it's what the */
510 /* customer wants. */
b0639ca3 511 cnt = 0;
f54ebdfa 512 do {
b0639ca3
WD
513 res = au_do_update(i, sz);
514#ifdef AU_TEST_ONLY
515 cnt++;
516 } while (res < 0 && cnt < 3);
517 if (cnt < 3)
518#else
f54ebdfa 519 } while (res < 0);
b0639ca3 520#endif
f54ebdfa
WD
521 au_update_eeprom(i);
522 }
b0639ca3 523 usb_stop();
f54ebdfa
WD
524 return 0;
525}
526#endif /* CONFIG_AUTO_UPDATE */