]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/trab/auto_update.c
G2000 board support added
[karo-tx-uboot.git] / board / trab / auto_update.c
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:
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.
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 struct flash_layout
91 {
92         long start;
93         long end;
94 };
95
96 /* layout of the FLASH. ST = start address, ND = end address. */
97 #ifndef CONFIG_FLASH_8MB                        /* 16 MB Flash, 32 MB RAM */
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, 32 MB RAM */
109 #define AU_FL_FIRMWARE_ST       0x00000000
110 #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
116 #define AU_FL_DISK_ND           0x007DFFFF
117 #define AU_FL_VFD_ST            0x007E0000
118 #define AU_FL_VFD_ND            0x007FFFFF
119 #endif  /* CONFIG_FLASH_8MB */
120
121 /* a structure with the offsets to values in the EEPROM */
122 struct eeprom_layout
123 {
124         int time;
125         int size;
126         int dcrc;
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
149 static int au_usb_stor_curr_dev; /* current device */
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
159 /* max. number of files which could interest us */
160 #define AU_MAXFILES 7
161 /* pointers to file names */
162 char *aufile[AU_MAXFILES];
163 /* sizes of flash areas for each file */
164 long ausize[AU_MAXFILES];
165 /* offsets into the EEEPROM */
166 struct 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         };
175 /* array of flash areas start and end addresses */
176 struct 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
185 /* where to load files into memory */
186 #define LOAD_ADDR ((unsigned char *)0x0C100000)
187 /* the app is the largest image */
188 #define MAX_LOADSZ ausize[IDX_APP]
189
190 /* externals */
191 extern int fat_register_device(block_dev_desc_t *, int);
192 extern int file_fat_detectfs(void);
193 extern long file_fat_read(const char *, void *, unsigned long);
194 extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
195 extern int i2c_write (uchar, uint, int , uchar* , int);
196 #ifdef CONFIG_VFD
197 extern int trab_vfd (ulong);
198 extern int transfer_pic(unsigned char, unsigned char *, int, int);
199 #endif
200 extern int flash_sect_erase(ulong, ulong);
201 extern int flash_sect_protect (int, ulong, ulong);
202 extern int flash_write (uchar *, ulong, ulong);
203 /* change char* to void* to shutup the compiler */
204 extern int i2c_write_multiple (uchar, uint, int, void *, int);
205 extern int i2c_read_multiple (uchar, uint, int, void *, int);
206 extern block_dev_desc_t *get_dev (char*, int);
207 extern int u_boot_hush_start(void);
208
209 int
210 au_check_cksum_valid(int idx, long nbytes)
211 {
212         image_header_t *hdr;
213         unsigned long checksum;
214
215         hdr = (image_header_t *)LOAD_ADDR;
216
217         if (nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size)))
218         {
219                 printf ("Image %s bad total SIZE\n", aufile[idx]);
220                 return -1;
221         }
222         /* check the data CRC */
223         checksum = ntohl(hdr->ih_dcrc);
224
225         if (crc32 (0, (char *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size))
226                 != checksum)
227         {
228                 printf ("Image %s bad data checksum\n", aufile[idx]);
229                 return -1;
230         }
231         return 0;
232 }
233
234 int
235 au_check_header_valid(int idx, long nbytes)
236 {
237         image_header_t *hdr;
238         unsigned long checksum;
239         unsigned char buf[4];
240
241         hdr = (image_header_t *)LOAD_ADDR;
242         /* check the easy ones first */
243 #undef CHECK_VALID_DEBUG
244 #ifdef CHECK_VALID_DEBUG
245         printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
246         printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_ARM);
247         printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
248         printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
249 #endif
250         if (nbytes < sizeof(*hdr))
251         {
252                 printf ("Image %s bad header SIZE\n", aufile[idx]);
253                 return -1;
254         }
255         if (ntohl(hdr->ih_magic) != IH_MAGIC || hdr->ih_arch != IH_CPU_ARM)
256         {
257                 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
258                 return -1;
259         }
260         /* check the hdr CRC */
261         checksum = ntohl(hdr->ih_hcrc);
262         hdr->ih_hcrc = 0;
263
264         if (crc32 (0, (char *)hdr, sizeof(*hdr)) != checksum) {
265                 printf ("Image %s bad header checksum\n", aufile[idx]);
266                 return -1;
267         }
268         hdr->ih_hcrc = htonl(checksum);
269         /* check the type - could do this all in one gigantic if() */
270         if ((idx == IDX_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
271                 printf ("Image %s wrong type\n", aufile[idx]);
272                 return -1;
273         }
274         if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
275                 printf ("Image %s wrong type\n", aufile[idx]);
276                 return -1;
277         }
278         if ((idx == IDX_DISK) && (hdr->ih_type != IH_TYPE_FILESYSTEM)) {
279                 printf ("Image %s wrong type\n", aufile[idx]);
280                 return -1;
281         }
282         if ((idx == IDX_APP) && (hdr->ih_type != IH_TYPE_RAMDISK)
283             && (hdr->ih_type != IH_TYPE_FILESYSTEM)) {
284                 printf ("Image %s wrong type\n", aufile[idx]);
285                 return -1;
286         }
287         if ((idx == IDX_PREPARE || idx == IDX_PREINST || idx == IDX_POSTINST)
288                 && (hdr->ih_type != IH_TYPE_SCRIPT))
289         {
290                 printf ("Image %s wrong type\n", aufile[idx]);
291                 return -1;
292         }
293         /* special case for prepare.img */
294         if (idx == IDX_PREPARE)
295                 return 0;
296         /* recycle checksum */
297         checksum = ntohl(hdr->ih_size);
298         /* for kernel and app the image header must also fit into flash */
299         if (idx != IDX_DISK)
300                 checksum += sizeof(*hdr);
301         /* check the size does not exceed space in flash. HUSH scripts */
302         /* all have ausize[] set to 0 */
303         if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
304                 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
305                 return -1;
306         }
307         /* check the time stamp from the EEPROM */
308         /* read it in */
309         i2c_read_multiple(0x54, auee_off[idx].time, 1, buf, sizeof(buf));
310 #ifdef CHECK_VALID_DEBUG
311         printf ("buf[0] %#x buf[1] %#x buf[2] %#x buf[3] %#x "
312                 "as int %#x time %#x\n",
313                 buf[0], buf[1], buf[2], buf[3],
314                 *((unsigned int *)buf), ntohl(hdr->ih_time));
315 #endif
316         /* check it */
317         if (*((unsigned int *)buf) >= ntohl(hdr->ih_time)) {
318                 printf ("Image %s is too old\n", aufile[idx]);
319                 return -1;
320         }
321
322         return 0;
323 }
324
325 /* power control defines */
326 #define CPLD_VFD_BK ((volatile char *)0x04038002)
327 #define POWER_OFF (1 << 1)
328
329 int
330 au_do_update(int idx, long sz)
331 {
332         image_header_t *hdr;
333         char *addr;
334         long start, end;
335         int off, rc;
336         uint nbytes;
337
338         hdr = (image_header_t *)LOAD_ADDR;
339
340         /* disable the power switch */
341         *CPLD_VFD_BK |= POWER_OFF;
342
343         /* execute a script */
344         if (hdr->ih_type == IH_TYPE_SCRIPT) {
345                 addr = (char *)((char *)hdr + sizeof(*hdr));
346                 /* stick a NULL at the end of the script, otherwise */
347                 /* parse_string_outer() runs off the end. */
348                 addr[ntohl(hdr->ih_size)] = 0;
349                 addr += 8;
350                 parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
351                 return 0;
352         }
353
354         start = aufl_layout[FIDX_TO_LIDX(idx)].start;
355         end = aufl_layout[FIDX_TO_LIDX(idx)].end;
356
357         /* unprotect the address range */
358         /* this assumes that ONLY the firmware is protected! */
359         if (idx == IDX_FIRMWARE) {
360 #undef AU_UPDATE_TEST
361 #ifdef AU_UPDATE_TEST
362                 /* erase it where Linux goes */
363                 start = aufl_layout[1].start;
364                 end = aufl_layout[1].end;
365 #endif
366                 flash_sect_protect(0, start, end);
367         }
368
369         /*
370          * erase the address range.
371          */
372         debug ("flash_sect_erase(%lx, %lx);\n", start, end);
373         flash_sect_erase(start, end);
374         wait_ms(100);
375         /* strip the header - except for the kernel and ramdisk */
376         if (hdr->ih_type == IH_TYPE_KERNEL || hdr->ih_type == IH_TYPE_RAMDISK) {
377                 addr = (char *)hdr;
378                 off = sizeof(*hdr);
379                 nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
380         } else {
381                 addr = (char *)((char *)hdr + sizeof(*hdr));
382 #ifdef AU_UPDATE_TEST
383                 /* copy it to where Linux goes */
384                 if (idx == IDX_FIRMWARE)
385                         start = aufl_layout[1].start;
386 #endif
387                 off = 0;
388                 nbytes = ntohl(hdr->ih_size);
389         }
390
391         /* copy the data from RAM to FLASH */
392         debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
393         rc = flash_write(addr, start, nbytes);
394         if (rc != 0) {
395                 printf("Flashing failed due to error %d\n", rc);
396                 return -1;
397         }
398
399         /* check the dcrc of the copy */
400         if (crc32 (0, (char *)(start + off), ntohl(hdr->ih_size)) != ntohl(hdr->ih_dcrc)) {
401                 printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
402                 return -1;
403         }
404
405         /* protect the address range */
406         /* this assumes that ONLY the firmware is protected! */
407         if (idx == IDX_FIRMWARE)
408                 flash_sect_protect(1, start, end);
409         return 0;
410 }
411
412 int
413 au_update_eeprom(int idx)
414 {
415         image_header_t *hdr;
416         int off;
417         uint32_t val;
418
419         hdr = (image_header_t *)LOAD_ADDR;
420         /* write the time field into EEPROM */
421         off = auee_off[idx].time;
422         val = ntohl(hdr->ih_time);
423         i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
424         /* write the size field into EEPROM */
425         off = auee_off[idx].size;
426         val = ntohl(hdr->ih_size);
427         i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
428         /* write the dcrc field into EEPROM */
429         off = auee_off[idx].dcrc;
430         val = ntohl(hdr->ih_dcrc);
431         i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
432         /* enable the power switch */
433         *CPLD_VFD_BK &= ~POWER_OFF;
434         return 0;
435 }
436
437 /*
438  * this is called from board_init() after the hardware has been set up
439  * and is usable. That seems like a good time to do this.
440  * Right now the return value is ignored.
441  */
442 int
443 do_auto_update(void)
444 {
445         block_dev_desc_t *stor_dev;
446         long sz;
447         int i, res, bitmap_first, cnt, old_ctrlc, got_ctrlc;
448         char *env;
449         long start, end;
450
451 #undef ERASE_EEPROM
452 #ifdef ERASE_EEPROM
453         int arr[18];
454         memset(arr, 0, sizeof(arr));
455         i2c_write_multiple(0x54, 64, 1, arr, sizeof(arr));
456 #endif
457         au_usb_stor_curr_dev = -1;
458         /* start USB */
459         if (usb_stop() < 0) {
460                 debug ("usb_stop failed\n");
461                 return -1;
462         }
463         if (usb_init() < 0) {
464                 debug ("usb_init failed\n");
465                 return -1;
466         }
467         /*
468          * check whether a storage device is attached (assume that it's
469          * a USB memory stick, since nothing else should be attached).
470          */
471         au_usb_stor_curr_dev = usb_stor_scan(0);
472         if (au_usb_stor_curr_dev == -1) {
473                 debug ("No device found. Not initialized?\n");
474                 return -1;
475         }
476         /* check whether it has a partition table */
477         stor_dev = get_dev("usb", 0);
478         if (stor_dev == NULL) {
479                 debug ("uknown device type\n");
480                 return -1;
481         }
482         if (fat_register_device(stor_dev, 1) != 0) {
483                 debug ("Unable to use USB %d:%d for fatls\n",
484                         au_usb_stor_curr_dev, 1);
485                 return -1;
486         }
487         if (file_fat_detectfs() != 0) {
488                 debug ("file_fat_detectfs failed\n");
489         }
490
491         /* initialize the array of file names */
492         memset(aufile, 0, sizeof(aufile));
493         aufile[IDX_PREPARE] = AU_PREPARE;
494         aufile[IDX_PREINST] = AU_PREINST;
495         aufile[IDX_FIRMWARE] = AU_FIRMWARE;
496         aufile[IDX_KERNEL] = AU_KERNEL;
497         aufile[IDX_APP] = AU_APP;
498         aufile[IDX_DISK] = AU_DISK;
499         aufile[IDX_POSTINST] = AU_POSTINST;
500         /* initialize the array of flash sizes */
501         memset(ausize, 0, sizeof(ausize));
502         ausize[IDX_FIRMWARE] = (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST;
503         ausize[IDX_KERNEL] = (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST;
504         ausize[IDX_APP] = (AU_FL_APP_ND + 1) - AU_FL_APP_ST;
505         ausize[IDX_DISK] = (AU_FL_DISK_ND + 1) - AU_FL_DISK_ST;
506         /*
507          * now check whether start and end are defined using environment
508          * variables.
509          */
510         start = -1;
511         end = 0;
512         env = getenv("firmware_st");
513         if (env != NULL)
514                 start = simple_strtoul(env, NULL, 16);
515         env = getenv("firmware_nd");
516         if (env != NULL)
517                 end = simple_strtoul(env, NULL, 16);
518         if (start >= 0 && end && end > start) {
519                 ausize[IDX_FIRMWARE] = (end + 1) - start;
520                 aufl_layout[0].start = start;
521                 aufl_layout[0].end = end;
522         }
523         start = -1;
524         end = 0;
525         env = getenv("kernel_st");
526         if (env != NULL)
527                 start = simple_strtoul(env, NULL, 16);
528         env = getenv("kernel_nd");
529         if (env != NULL)
530                 end = simple_strtoul(env, NULL, 16);
531         if (start >= 0 && end && end > start) {
532                 ausize[IDX_KERNEL] = (end + 1) - start;
533                 aufl_layout[1].start = start;
534                 aufl_layout[1].end = end;
535         }
536         start = -1;
537         end = 0;
538         env = getenv("app_st");
539         if (env != NULL)
540                 start = simple_strtoul(env, NULL, 16);
541         env = getenv("app_nd");
542         if (env != NULL)
543                 end = simple_strtoul(env, NULL, 16);
544         if (start >= 0 && end && end > start) {
545                 ausize[IDX_APP] = (end + 1) - start;
546                 aufl_layout[2].start = start;
547                 aufl_layout[2].end = end;
548         }
549         start = -1;
550         end = 0;
551         env = getenv("disk_st");
552         if (env != NULL)
553                 start = simple_strtoul(env, NULL, 16);
554         env = getenv("disk_nd");
555         if (env != NULL)
556                 end = simple_strtoul(env, NULL, 16);
557         if (start >= 0 && end && end > start) {
558                 ausize[IDX_DISK] = (end + 1) - start;
559                 aufl_layout[3].start = start;
560                 aufl_layout[3].end = end;
561         }
562         /* make certain that HUSH is runnable */
563         u_boot_hush_start();
564         /* make sure that we see CTRL-C and save the old state */
565         old_ctrlc = disable_ctrlc(0);
566
567         bitmap_first = 0;
568         /* just loop thru all the possible files */
569         for (i = 0; i < AU_MAXFILES; i++) {
570                 /* just read the header */
571                 sz = file_fat_read(aufile[i], LOAD_ADDR, sizeof(image_header_t));
572                 debug ("read %s sz %ld hdr %d\n",
573                         aufile[i], sz, sizeof(image_header_t));
574                 if (sz <= 0 || sz < sizeof(image_header_t)) {
575                         debug ("%s not found\n", aufile[i]);
576                         continue;
577                 }
578                 if (au_check_header_valid(i, sz) < 0) {
579                         debug ("%s header not valid\n", aufile[i]);
580                         continue;
581                 }
582                 sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
583                 debug ("read %s sz %ld hdr %d\n",
584                         aufile[i], sz, sizeof(image_header_t));
585                 if (sz <= 0 || sz <= sizeof(image_header_t)) {
586                         debug ("%s not found\n", aufile[i]);
587                         continue;
588                 }
589                 if (au_check_cksum_valid(i, sz) < 0) {
590                         debug ("%s checksum not valid\n", aufile[i]);
591                         continue;
592                 }
593 #ifdef CONFIG_VFD
594                 /* now that we have a valid file we can display the */
595                 /* bitmap. */
596                 if (bitmap_first == 0) {
597                         env = getenv("bitmap2");
598                         if (env == NULL) {
599                                 trab_vfd(0);
600                         } else {
601                                 /* not so simple - bitmap2 is supposed to */
602                                 /* contain the address of the bitmap */
603                                 env = (char *)simple_strtoul(env, NULL, 16);
604 /* NOTE: these are taken from vfd_logo.h. If that file changes then */
605 /* these defines MUST also be updated! These may be wrong for bitmap2. */
606 #define VFD_LOGO_WIDTH 112
607 #define VFD_LOGO_HEIGHT 72
608                                 /* must call transfer_pic directly */
609                                 transfer_pic(3, env, VFD_LOGO_HEIGHT, VFD_LOGO_WIDTH);
610                         }
611                         bitmap_first = 1;
612                 }
613 #endif
614                 /* this is really not a good idea, but it's what the */
615                 /* customer wants. */
616                 cnt = 0;
617                 got_ctrlc = 0;
618                 do {
619                         res = au_do_update(i, sz);
620                         /* let the user break out of the loop */
621                         if (ctrlc() || had_ctrlc()) {
622                                 clear_ctrlc();
623                                 if (res < 0)
624                                         got_ctrlc = 1;
625                                 break;
626                         }
627                         cnt++;
628 #ifdef AU_TEST_ONLY
629                 } while (res < 0 && cnt < 3);
630                 if (cnt < 3)
631 #else
632                 } while (res < 0);
633 #endif
634                 /*
635                  * it doesn't make sense to update the EEPROM if the
636                  * update was interrupted by the user due to errors.
637                  */
638                 if (got_ctrlc == 0)
639                         au_update_eeprom(i);
640                 else
641                         /* enable the power switch */
642                         *CPLD_VFD_BK &= ~POWER_OFF;
643         }
644         usb_stop();
645         /* restore the old state */
646         disable_ctrlc(old_ctrlc);
647         return 0;
648 }
649 #endif /* CONFIG_AUTO_UPDATE */