3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 #include <asm/byteorder.h>
28 #ifdef CONFIG_SYS_HUSH_PARSER
33 #ifdef CONFIG_AUTO_UPDATE
35 #ifndef CONFIG_USB_OHCI
36 #error "must define CONFIG_USB_OHCI"
39 #ifndef CONFIG_USB_STORAGE
40 #error "must define CONFIG_USB_STORAGE"
43 #ifndef CONFIG_SYS_HUSH_PARSER
44 #error "must define CONFIG_SYS_HUSH_PARSER"
47 #if !defined(CONFIG_CMD_FAT)
48 #error "must define CONFIG_CMD_FAT"
55 #define debug(fmt,args...) printf (fmt ,##args)
57 #define debug(fmt,args...)
60 /* possible names of files on the USB stick. */
61 #define AU_FIRMWARE "u-boot.img"
62 #define AU_KERNEL "kernel.img"
63 #define AU_ROOTFS "rootfs.img"
70 /* layout of the FLASH. ST = start address, ND = end address. */
71 #define AU_FL_FIRMWARE_ST 0xfC000000
72 #define AU_FL_FIRMWARE_ND 0xfC03FFFF
73 #define AU_FL_KERNEL_ST 0xfC0C0000
74 #define AU_FL_KERNEL_ND 0xfC1BFFFF
75 #define AU_FL_ROOTFS_ST 0xFC1C0000
76 #define AU_FL_ROOTFS_ND 0xFCFBFFFF
78 static int au_usb_stor_curr_dev; /* current device */
80 /* index of each file in the following arrays */
81 #define IDX_FIRMWARE 0
85 /* max. number of files which could interest us */
88 /* pointers to file names */
89 char *aufile[AU_MAXFILES] = {
95 /* sizes of flash areas for each file */
96 long ausize[AU_MAXFILES] = {
97 (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST,
98 (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST,
99 (AU_FL_ROOTFS_ND + 1) - AU_FL_ROOTFS_ST,
102 /* array of flash areas start and end addresses */
103 struct flash_layout aufl_layout[AU_MAXFILES] = {
104 { AU_FL_FIRMWARE_ST, AU_FL_FIRMWARE_ND, },
105 { AU_FL_KERNEL_ST, AU_FL_KERNEL_ND, },
106 { AU_FL_ROOTFS_ST, AU_FL_ROOTFS_ND, },
111 /* where to load files into memory */
112 #define LOAD_ADDR ((unsigned char *)0x00200000)
114 /* the root file system is the largest image */
115 #define MAX_LOADSZ ausize[IDX_ROOTFS]
117 /*i2c address of the keypad status*/
118 #define I2C_PSOC_KEYPAD_ADDR 0x53
123 #define KEYPAD_MASK_LO ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))&0xFF)
124 #define KEYPAD_MASK_HI ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))>>8)
127 extern int fat_register_device(block_dev_desc_t *, int);
128 extern int file_fat_detectfs(void);
129 extern long file_fat_read(const char *, void *, unsigned long);
130 extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
131 extern int flash_sect_erase(ulong, ulong);
132 extern int flash_sect_protect (int, ulong, ulong);
133 extern int flash_write (char *, ulong, ulong);
134 extern int u_boot_hush_start(void);
135 #ifdef CONFIG_PROGRESSBAR
136 extern void show_progress(int, int);
137 extern void lcd_puts (char *);
138 extern void lcd_enable(void);
141 int au_check_cksum_valid(int idx, long nbytes)
145 hdr = (image_header_t *)LOAD_ADDR;
146 #if defined(CONFIG_FIT)
147 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
148 puts ("Non legacy image format not supported\n");
153 if (nbytes != image_get_image_size (hdr)) {
154 printf ("Image %s bad total SIZE\n", aufile[idx]);
157 /* check the data CRC */
158 if (!image_check_dcrc (hdr)) {
159 printf ("Image %s bad data checksum\n", aufile[idx]);
165 int au_check_header_valid(int idx, long nbytes)
168 unsigned long checksum, fsize;
170 hdr = (image_header_t *)LOAD_ADDR;
171 #if defined(CONFIG_FIT)
172 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
173 puts ("Non legacy image format not supported\n");
178 /* check the easy ones first */
179 #undef CHECK_VALID_DEBUG
180 #ifdef CHECK_VALID_DEBUG
181 printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC);
182 printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_ARM);
183 printf("size %#x %#lx ", image_get_data_size (hdr), nbytes);
184 printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL);
186 if (nbytes < image_get_header_size ()) {
187 printf ("Image %s bad header SIZE\n", aufile[idx]);
191 if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) {
192 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
196 /* check the hdr CRC */
197 if (!image_check_hcrc (hdr)) {
198 printf ("Image %s bad header checksum\n", aufile[idx]);
202 /* check the type - could do this all in one gigantic if() */
203 if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
204 printf ("Image %s wrong type\n", aufile[idx]);
208 if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) {
209 printf ("Image %s wrong type\n", aufile[idx]);
213 if ((idx == IDX_ROOTFS) &&
214 (!image_check_type (hdr, IH_TYPE_RAMDISK) &&
215 !image_check_type (hdr, IH_TYPE_FILESYSTEM))) {
216 printf ("Image %s wrong type\n", aufile[idx]);
220 /* recycle checksum */
221 checksum = image_get_data_size (hdr);
223 fsize = checksum + image_get_header_size ();
224 /* for kernel and ramdisk the image header must also fit into flash */
225 if (idx == IDX_KERNEL || image_check_type (hdr, IH_TYPE_RAMDISK))
226 checksum += image_get_header_size ();
228 /* check the size does not exceed space in flash. HUSH scripts */
229 if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
230 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
234 /* Update with the real filesize */
237 return checksum; /* return size to be written to flash */
240 int au_do_update(int idx, long sz)
248 hdr = (image_header_t *)LOAD_ADDR;
249 #if defined(CONFIG_FIT)
250 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
251 puts ("Non legacy image format not supported\n");
256 /* execute a script */
257 if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
258 addr = (char *)((char *)hdr + image_get_header_size ());
259 /* stick a NULL at the end of the script, otherwise */
260 /* parse_string_outer() runs off the end. */
261 addr[image_get_data_size (hdr)] = 0;
263 parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
267 start = aufl_layout[idx].start;
268 end = aufl_layout[idx].end;
270 /* unprotect the address range */
271 /* this assumes that ONLY the firmware is protected! */
272 if (idx == IDX_FIRMWARE) {
273 #undef AU_UPDATE_TEST
274 #ifdef AU_UPDATE_TEST
275 /* erase it where Linux goes */
276 start = aufl_layout[1].start;
277 end = aufl_layout[1].end;
279 flash_sect_protect(0, start, end);
283 * erase the address range.
285 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
286 flash_sect_erase(start, end);
288 #ifdef CONFIG_PROGRESSBAR
289 show_progress(end - start, totsize);
292 /* strip the header - except for the kernel and ramdisk */
293 if (image_check_type (hdr, IH_TYPE_KERNEL) ||
294 image_check_type (hdr, IH_TYPE_RAMDISK)) {
296 off = image_get_header_size ();
297 nbytes = image_get_image_size (hdr);
299 addr = (char *)((char *)hdr + image_get_header_size ());
300 #ifdef AU_UPDATE_TEST
301 /* copy it to where Linux goes */
302 if (idx == IDX_FIRMWARE)
303 start = aufl_layout[1].start;
306 nbytes = image_get_data_size (hdr);
309 /* copy the data from RAM to FLASH */
310 debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
311 rc = flash_write(addr, start, nbytes);
313 printf("Flashing failed due to error %d\n", rc);
317 #ifdef CONFIG_PROGRESSBAR
318 show_progress(nbytes, totsize);
321 /* check the data CRC of the copy */
322 if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) !=
323 image_get_dcrc (hdr)) {
324 printf ("Image %s Bad Data Checksum after COPY\n", aufile[idx]);
328 /* protect the address range */
329 /* this assumes that ONLY the firmware is protected! */
330 if (idx == IDX_FIRMWARE)
331 flash_sect_protect(1, start, end);
336 * this is called from board_init() after the hardware has been set up
337 * and is usable. That seems like a good time to do this.
338 * Right now the return value is ignored.
340 int do_auto_update(void)
342 block_dev_desc_t *stor_dev;
344 int i, res = 0, cnt, old_ctrlc;
348 #if 0 /* disable key-press detection to speed up boot-up time */
349 uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
354 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
356 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
361 if ( !(keypad_status1[1] & KEYPAD_MASK_LO) ||
362 (keypad_status1[1] != keypad_status2[1])) {
367 au_usb_stor_curr_dev = -1;
369 if (usb_stop() < 0) {
370 debug ("usb_stop failed\n");
373 if (usb_init() < 0) {
374 debug ("usb_init failed\n");
378 * check whether a storage device is attached (assume that it's
379 * a USB memory stick, since nothing else should be attached).
381 au_usb_stor_curr_dev = usb_stor_scan(0);
382 if (au_usb_stor_curr_dev == -1) {
383 debug ("No device found. Not initialized?\n");
387 /* check whether it has a partition table */
388 stor_dev = get_dev("usb", 0);
389 if (stor_dev == NULL) {
390 debug ("uknown device type\n");
394 if (fat_register_device(stor_dev, 1) != 0) {
395 debug ("Unable to use USB %d:%d for fatls\n",
396 au_usb_stor_curr_dev, 1);
400 if (file_fat_detectfs() != 0) {
401 debug ("file_fat_detectfs failed\n");
405 * now check whether start and end are defined using environment
410 env = getenv("firmware_st");
412 start = simple_strtoul(env, NULL, 16);
413 env = getenv("firmware_nd");
415 end = simple_strtoul(env, NULL, 16);
416 if (start >= 0 && end && end > start) {
417 ausize[IDX_FIRMWARE] = (end + 1) - start;
418 aufl_layout[IDX_FIRMWARE].start = start;
419 aufl_layout[IDX_FIRMWARE].end = end;
423 env = getenv("kernel_st");
425 start = simple_strtoul(env, NULL, 16);
426 env = getenv("kernel_nd");
428 end = simple_strtoul(env, NULL, 16);
429 if (start >= 0 && end && end > start) {
430 ausize[IDX_KERNEL] = (end + 1) - start;
431 aufl_layout[IDX_KERNEL].start = start;
432 aufl_layout[IDX_KERNEL].end = end;
436 env = getenv("rootfs_st");
438 start = simple_strtoul(env, NULL, 16);
439 env = getenv("rootfs_nd");
441 end = simple_strtoul(env, NULL, 16);
442 if (start >= 0 && end && end > start) {
443 ausize[IDX_ROOTFS] = (end + 1) - start;
444 aufl_layout[IDX_ROOTFS].start = start;
445 aufl_layout[IDX_ROOTFS].end = end;
448 /* make certain that HUSH is runnable */
450 /* make sure that we see CTRL-C and save the old state */
451 old_ctrlc = disable_ctrlc(0);
453 /* validate the images first */
454 for (i = 0; i < AU_MAXFILES; i++) {
456 /* just read the header */
457 sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ());
458 debug ("read %s sz %ld hdr %d\n",
459 aufile[i], sz, image_get_header_size ());
460 if (sz <= 0 || sz < image_get_header_size ()) {
461 debug ("%s not found\n", aufile[i]);
465 /* au_check_header_valid() updates ausize[] */
466 if ((imsize = au_check_header_valid(i, sz)) < 0) {
467 debug ("%s header not valid\n", aufile[i]);
470 /* totsize accounts for image size and flash erase size */
471 totsize += (imsize + (aufl_layout[i].end - aufl_layout[i].start));
474 #ifdef CONFIG_PROGRESSBAR
476 lcd_puts(" Update in progress\n");
481 /* just loop thru all the possible files */
482 for (i = 0; i < AU_MAXFILES && totsize; i++) {
486 sz = file_fat_read(aufile[i], LOAD_ADDR, ausize[i]);
488 debug ("read %s sz %ld hdr %d\n",
489 aufile[i], sz, image_get_header_size ());
491 if (sz != ausize[i]) {
492 printf ("%s: size %ld read %ld?\n", aufile[i], ausize[i], sz);
496 if (sz <= 0 || sz <= image_get_header_size ()) {
497 debug ("%s not found\n", aufile[i]);
500 if (au_check_cksum_valid(i, sz) < 0) {
501 debug ("%s checksum not valid\n", aufile[i]);
504 /* this is really not a good idea, but it's what the */
505 /* customer wants. */
508 res = au_do_update(i, sz);
509 /* let the user break out of the loop */
510 if (ctrlc() || had_ctrlc()) {
516 } while (res < 0 && cnt < (AU_MAXFILES + 1));
517 if (cnt < (AU_MAXFILES + 1))
523 /* restore the old state */
524 disable_ctrlc(old_ctrlc);
525 #ifdef CONFIG_PROGRESSBAR
528 lcd_puts("\n Update completed\n");
530 lcd_puts("\n Update error\n");
539 #endif /* CONFIG_AUTO_UPDATE */