3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <asm/byteorder.h>
15 #ifdef CONFIG_SYS_HUSH_PARSER
20 #ifdef CONFIG_AUTO_UPDATE
22 #ifndef CONFIG_USB_OHCI
23 #error "must define CONFIG_USB_OHCI"
26 #ifndef CONFIG_USB_STORAGE
27 #error "must define CONFIG_USB_STORAGE"
30 #ifndef CONFIG_SYS_HUSH_PARSER
31 #error "must define CONFIG_SYS_HUSH_PARSER"
34 #if !defined(CONFIG_CMD_FAT)
35 #error "must define CONFIG_CMD_FAT"
42 #define debug(fmt,args...) printf (fmt ,##args)
44 #define debug(fmt,args...)
47 /* possible names of files on the USB stick. */
48 #define AU_FIRMWARE "u-boot.img"
49 #define AU_KERNEL "kernel.img"
50 #define AU_ROOTFS "rootfs.img"
57 /* layout of the FLASH. ST = start address, ND = end address. */
58 #define AU_FL_FIRMWARE_ST 0xfC000000
59 #define AU_FL_FIRMWARE_ND 0xfC03FFFF
60 #define AU_FL_KERNEL_ST 0xfC0C0000
61 #define AU_FL_KERNEL_ND 0xfC1BFFFF
62 #define AU_FL_ROOTFS_ST 0xFC1C0000
63 #define AU_FL_ROOTFS_ND 0xFCFBFFFF
65 static int au_usb_stor_curr_dev; /* current device */
67 /* index of each file in the following arrays */
68 #define IDX_FIRMWARE 0
72 /* max. number of files which could interest us */
75 /* pointers to file names */
76 char *aufile[AU_MAXFILES] = {
82 /* sizes of flash areas for each file */
83 long ausize[AU_MAXFILES] = {
84 (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST,
85 (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST,
86 (AU_FL_ROOTFS_ND + 1) - AU_FL_ROOTFS_ST,
89 /* array of flash areas start and end addresses */
90 struct flash_layout aufl_layout[AU_MAXFILES] = {
91 { AU_FL_FIRMWARE_ST, AU_FL_FIRMWARE_ND, },
92 { AU_FL_KERNEL_ST, AU_FL_KERNEL_ND, },
93 { AU_FL_ROOTFS_ST, AU_FL_ROOTFS_ND, },
98 /* where to load files into memory */
99 #define LOAD_ADDR ((unsigned char *)0x00200000)
101 /* the root file system is the largest image */
102 #define MAX_LOADSZ ausize[IDX_ROOTFS]
104 /*i2c address of the keypad status*/
105 #define I2C_PSOC_KEYPAD_ADDR 0x53
110 #define KEYPAD_MASK_LO ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))&0xFF)
111 #define KEYPAD_MASK_HI ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))>>8)
114 extern int fat_register_device(block_dev_desc_t *, int);
115 extern int file_fat_detectfs(void);
116 extern long file_fat_read(const char *, void *, unsigned long);
117 extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
118 extern int flash_sect_erase(ulong, ulong);
119 extern int flash_sect_protect (int, ulong, ulong);
120 extern int flash_write (char *, ulong, ulong);
121 extern int u_boot_hush_start(void);
122 #ifdef CONFIG_PROGRESSBAR
123 extern void show_progress(int, int);
124 extern void lcd_puts (char *);
125 extern void lcd_enable(void);
128 int au_check_cksum_valid(int idx, long nbytes)
132 hdr = (image_header_t *)LOAD_ADDR;
133 #if defined(CONFIG_FIT)
134 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
135 puts ("Non legacy image format not supported\n");
140 if (nbytes != image_get_image_size (hdr)) {
141 printf ("Image %s bad total SIZE\n", aufile[idx]);
144 /* check the data CRC */
145 if (!image_check_dcrc (hdr)) {
146 printf ("Image %s bad data checksum\n", aufile[idx]);
152 int au_check_header_valid(int idx, long nbytes)
155 unsigned long checksum, fsize;
157 hdr = (image_header_t *)LOAD_ADDR;
158 #if defined(CONFIG_FIT)
159 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
160 puts ("Non legacy image format not supported\n");
165 /* check the easy ones first */
166 #undef CHECK_VALID_DEBUG
167 #ifdef CHECK_VALID_DEBUG
168 printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC);
169 printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_ARM);
170 printf("size %#x %#lx ", image_get_data_size (hdr), nbytes);
171 printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL);
173 if (nbytes < image_get_header_size ()) {
174 printf ("Image %s bad header SIZE\n", aufile[idx]);
178 if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) {
179 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
183 /* check the hdr CRC */
184 if (!image_check_hcrc (hdr)) {
185 printf ("Image %s bad header checksum\n", aufile[idx]);
189 /* check the type - could do this all in one gigantic if() */
190 if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
191 printf ("Image %s wrong type\n", aufile[idx]);
195 if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) {
196 printf ("Image %s wrong type\n", aufile[idx]);
200 if ((idx == IDX_ROOTFS) &&
201 (!image_check_type (hdr, IH_TYPE_RAMDISK) &&
202 !image_check_type (hdr, IH_TYPE_FILESYSTEM))) {
203 printf ("Image %s wrong type\n", aufile[idx]);
207 /* recycle checksum */
208 checksum = image_get_data_size (hdr);
210 fsize = checksum + image_get_header_size ();
211 /* for kernel and ramdisk the image header must also fit into flash */
212 if (idx == IDX_KERNEL || image_check_type (hdr, IH_TYPE_RAMDISK))
213 checksum += image_get_header_size ();
215 /* check the size does not exceed space in flash. HUSH scripts */
216 if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
217 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
221 /* Update with the real filesize */
224 return checksum; /* return size to be written to flash */
227 int au_do_update(int idx, long sz)
235 hdr = (image_header_t *)LOAD_ADDR;
236 #if defined(CONFIG_FIT)
237 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
238 puts ("Non legacy image format not supported\n");
243 /* execute a script */
244 if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
245 addr = (char *)((char *)hdr + image_get_header_size ());
246 /* stick a NULL at the end of the script, otherwise */
247 /* parse_string_outer() runs off the end. */
248 addr[image_get_data_size (hdr)] = 0;
250 parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
254 start = aufl_layout[idx].start;
255 end = aufl_layout[idx].end;
257 /* unprotect the address range */
258 /* this assumes that ONLY the firmware is protected! */
259 if (idx == IDX_FIRMWARE) {
260 #undef AU_UPDATE_TEST
261 #ifdef AU_UPDATE_TEST
262 /* erase it where Linux goes */
263 start = aufl_layout[1].start;
264 end = aufl_layout[1].end;
266 flash_sect_protect(0, start, end);
270 * erase the address range.
272 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
273 flash_sect_erase(start, end);
275 #ifdef CONFIG_PROGRESSBAR
276 show_progress(end - start, totsize);
279 /* strip the header - except for the kernel and ramdisk */
280 if (image_check_type (hdr, IH_TYPE_KERNEL) ||
281 image_check_type (hdr, IH_TYPE_RAMDISK)) {
283 off = image_get_header_size ();
284 nbytes = image_get_image_size (hdr);
286 addr = (char *)((char *)hdr + image_get_header_size ());
287 #ifdef AU_UPDATE_TEST
288 /* copy it to where Linux goes */
289 if (idx == IDX_FIRMWARE)
290 start = aufl_layout[1].start;
293 nbytes = image_get_data_size (hdr);
296 /* copy the data from RAM to FLASH */
297 debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
298 rc = flash_write(addr, start, nbytes);
300 printf("Flashing failed due to error %d\n", rc);
304 #ifdef CONFIG_PROGRESSBAR
305 show_progress(nbytes, totsize);
308 /* check the data CRC of the copy */
309 if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) !=
310 image_get_dcrc (hdr)) {
311 printf ("Image %s Bad Data Checksum after COPY\n", aufile[idx]);
315 /* protect the address range */
316 /* this assumes that ONLY the firmware is protected! */
317 if (idx == IDX_FIRMWARE)
318 flash_sect_protect(1, start, end);
323 * this is called from board_init() after the hardware has been set up
324 * and is usable. That seems like a good time to do this.
325 * Right now the return value is ignored.
327 int do_auto_update(void)
329 block_dev_desc_t *stor_dev;
331 int i, res = 0, cnt, old_ctrlc;
335 #if 0 /* disable key-press detection to speed up boot-up time */
336 uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
341 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
343 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
348 if ( !(keypad_status1[1] & KEYPAD_MASK_LO) ||
349 (keypad_status1[1] != keypad_status2[1])) {
354 au_usb_stor_curr_dev = -1;
356 if (usb_stop() < 0) {
357 debug ("usb_stop failed\n");
360 if (usb_init() < 0) {
361 debug ("usb_init failed\n");
365 * check whether a storage device is attached (assume that it's
366 * a USB memory stick, since nothing else should be attached).
368 au_usb_stor_curr_dev = usb_stor_scan(0);
369 if (au_usb_stor_curr_dev == -1) {
370 debug ("No device found. Not initialized?\n");
374 /* check whether it has a partition table */
375 stor_dev = get_dev("usb", 0);
376 if (stor_dev == NULL) {
377 debug ("uknown device type\n");
381 if (fat_register_device(stor_dev, 1) != 0) {
382 debug ("Unable to use USB %d:%d for fatls\n",
383 au_usb_stor_curr_dev, 1);
387 if (file_fat_detectfs() != 0) {
388 debug ("file_fat_detectfs failed\n");
392 * now check whether start and end are defined using environment
397 env = getenv("firmware_st");
399 start = simple_strtoul(env, NULL, 16);
400 env = getenv("firmware_nd");
402 end = simple_strtoul(env, NULL, 16);
403 if (start >= 0 && end && end > start) {
404 ausize[IDX_FIRMWARE] = (end + 1) - start;
405 aufl_layout[IDX_FIRMWARE].start = start;
406 aufl_layout[IDX_FIRMWARE].end = end;
410 env = getenv("kernel_st");
412 start = simple_strtoul(env, NULL, 16);
413 env = getenv("kernel_nd");
415 end = simple_strtoul(env, NULL, 16);
416 if (start >= 0 && end && end > start) {
417 ausize[IDX_KERNEL] = (end + 1) - start;
418 aufl_layout[IDX_KERNEL].start = start;
419 aufl_layout[IDX_KERNEL].end = end;
423 env = getenv("rootfs_st");
425 start = simple_strtoul(env, NULL, 16);
426 env = getenv("rootfs_nd");
428 end = simple_strtoul(env, NULL, 16);
429 if (start >= 0 && end && end > start) {
430 ausize[IDX_ROOTFS] = (end + 1) - start;
431 aufl_layout[IDX_ROOTFS].start = start;
432 aufl_layout[IDX_ROOTFS].end = end;
435 /* make certain that HUSH is runnable */
437 /* make sure that we see CTRL-C and save the old state */
438 old_ctrlc = disable_ctrlc(0);
440 /* validate the images first */
441 for (i = 0; i < AU_MAXFILES; i++) {
443 /* just read the header */
444 sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ());
445 debug ("read %s sz %ld hdr %d\n",
446 aufile[i], sz, image_get_header_size ());
447 if (sz <= 0 || sz < image_get_header_size ()) {
448 debug ("%s not found\n", aufile[i]);
452 /* au_check_header_valid() updates ausize[] */
453 if ((imsize = au_check_header_valid(i, sz)) < 0) {
454 debug ("%s header not valid\n", aufile[i]);
457 /* totsize accounts for image size and flash erase size */
458 totsize += (imsize + (aufl_layout[i].end - aufl_layout[i].start));
461 #ifdef CONFIG_PROGRESSBAR
463 lcd_puts(" Update in progress\n");
468 /* just loop thru all the possible files */
469 for (i = 0; i < AU_MAXFILES && totsize; i++) {
473 sz = file_fat_read(aufile[i], LOAD_ADDR, ausize[i]);
475 debug ("read %s sz %ld hdr %d\n",
476 aufile[i], sz, image_get_header_size ());
478 if (sz != ausize[i]) {
479 printf ("%s: size %ld read %ld?\n", aufile[i], ausize[i], sz);
483 if (sz <= 0 || sz <= image_get_header_size ()) {
484 debug ("%s not found\n", aufile[i]);
487 if (au_check_cksum_valid(i, sz) < 0) {
488 debug ("%s checksum not valid\n", aufile[i]);
491 /* this is really not a good idea, but it's what the */
492 /* customer wants. */
495 res = au_do_update(i, sz);
496 /* let the user break out of the loop */
497 if (ctrlc() || had_ctrlc()) {
503 } while (res < 0 && cnt < (AU_MAXFILES + 1));
504 if (cnt < (AU_MAXFILES + 1))
510 /* restore the old state */
511 disable_ctrlc(old_ctrlc);
512 #ifdef CONFIG_PROGRESSBAR
515 lcd_puts("\n Update completed\n");
517 lcd_puts("\n Update error\n");
526 #endif /* CONFIG_AUTO_UPDATE */