Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2006 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
| 9 | #include <malloc.h> |
| 10 | #include <image.h> |
| 11 | #include <asm/byteorder.h> |
| 12 | #include <usb.h> |
Grant Likely | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 13 | #include <part.h> |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 14 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 15 | #ifdef CONFIG_SYS_HUSH_PARSER |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 16 | #include <hush.h> |
| 17 | #endif |
| 18 | |
| 19 | |
| 20 | #ifdef CONFIG_AUTO_UPDATE |
| 21 | |
| 22 | #ifndef CONFIG_USB_OHCI |
| 23 | #error "must define CONFIG_USB_OHCI" |
| 24 | #endif |
| 25 | |
| 26 | #ifndef CONFIG_USB_STORAGE |
| 27 | #error "must define CONFIG_USB_STORAGE" |
| 28 | #endif |
| 29 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 30 | #ifndef CONFIG_SYS_HUSH_PARSER |
| 31 | #error "must define CONFIG_SYS_HUSH_PARSER" |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 32 | #endif |
| 33 | |
Jon Loeliger | 3fe0010 | 2007-07-09 18:38:39 -0500 | [diff] [blame] | 34 | #if !defined(CONFIG_CMD_FAT) |
Jon Loeliger | d39b574 | 2007-07-10 10:48:22 -0500 | [diff] [blame] | 35 | #error "must define CONFIG_CMD_FAT" |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 36 | #endif |
| 37 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 38 | #undef AU_DEBUG |
| 39 | |
| 40 | #undef debug |
| 41 | #ifdef AU_DEBUG |
| 42 | #define debug(fmt,args...) printf (fmt ,##args) |
| 43 | #else |
| 44 | #define debug(fmt,args...) |
| 45 | #endif /* AU_DEBUG */ |
| 46 | |
| 47 | /* possible names of files on the USB stick. */ |
| 48 | #define AU_FIRMWARE "u-boot.img" |
| 49 | #define AU_KERNEL "kernel.img" |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 50 | #define AU_ROOTFS "rootfs.img" |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 51 | |
Wolfgang Denk | 82e5236 | 2006-12-22 10:30:26 +0100 | [diff] [blame] | 52 | struct flash_layout { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 53 | long start; |
| 54 | long end; |
| 55 | }; |
| 56 | |
| 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 |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 62 | #define AU_FL_ROOTFS_ST 0xFC1C0000 |
| 63 | #define AU_FL_ROOTFS_ND 0xFCFBFFFF |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 64 | |
| 65 | static int au_usb_stor_curr_dev; /* current device */ |
| 66 | |
| 67 | /* index of each file in the following arrays */ |
| 68 | #define IDX_FIRMWARE 0 |
| 69 | #define IDX_KERNEL 1 |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 70 | #define IDX_ROOTFS 2 |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 71 | |
| 72 | /* max. number of files which could interest us */ |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 73 | #define AU_MAXFILES 3 |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 74 | |
| 75 | /* pointers to file names */ |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 76 | char *aufile[AU_MAXFILES] = { |
| 77 | AU_FIRMWARE, |
| 78 | AU_KERNEL, |
| 79 | AU_ROOTFS |
| 80 | }; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 81 | |
| 82 | /* sizes of flash areas for each file */ |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 83 | long ausize[AU_MAXFILES] = { |
| 84 | (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST, |
Wolfgang Denk | f5fcc3c | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 85 | (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST, |
| 86 | (AU_FL_ROOTFS_ND + 1) - AU_FL_ROOTFS_ST, |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 87 | }; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 88 | |
| 89 | /* array of flash areas start and end addresses */ |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 90 | struct flash_layout aufl_layout[AU_MAXFILES] = { |
Wolfgang Denk | f5fcc3c | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 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, }, |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 96 | ulong totsize; |
| 97 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 98 | /* where to load files into memory */ |
| 99 | #define LOAD_ADDR ((unsigned char *)0x00200000) |
| 100 | |
Wolfgang Denk | f5fcc3c | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 101 | /* the root file system is the largest image */ |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 102 | #define MAX_LOADSZ ausize[IDX_ROOTFS] |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 103 | |
| 104 | /*i2c address of the keypad status*/ |
| 105 | #define I2C_PSOC_KEYPAD_ADDR 0x53 |
| 106 | |
| 107 | /* keypad mask */ |
Wolfgang Denk | 787fa15 | 2007-01-10 01:28:39 +0100 | [diff] [blame] | 108 | #define KEYPAD_ROW 2 |
| 109 | #define KEYPAD_COL 2 |
| 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) |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 112 | |
| 113 | /* externals */ |
| 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); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 121 | extern int u_boot_hush_start(void); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 122 | #ifdef CONFIG_PROGRESSBAR |
| 123 | extern void show_progress(int, int); |
| 124 | extern void lcd_puts (char *); |
| 125 | extern void lcd_enable(void); |
| 126 | #endif |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 127 | |
Wolfgang Denk | 82e5236 | 2006-12-22 10:30:26 +0100 | [diff] [blame] | 128 | int au_check_cksum_valid(int idx, long nbytes) |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 129 | { |
| 130 | image_header_t *hdr; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 131 | |
| 132 | hdr = (image_header_t *)LOAD_ADDR; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 133 | #if defined(CONFIG_FIT) |
Marian Balakowicz | 9a4daad | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 134 | if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 135 | puts ("Non legacy image format not supported\n"); |
| 136 | return -1; |
| 137 | } |
| 138 | #endif |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 139 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 140 | if (nbytes != image_get_image_size (hdr)) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 141 | printf ("Image %s bad total SIZE\n", aufile[idx]); |
| 142 | return -1; |
| 143 | } |
| 144 | /* check the data CRC */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 145 | if (!image_check_dcrc (hdr)) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 146 | printf ("Image %s bad data checksum\n", aufile[idx]); |
| 147 | return -1; |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | |
Wolfgang Denk | 82e5236 | 2006-12-22 10:30:26 +0100 | [diff] [blame] | 152 | int au_check_header_valid(int idx, long nbytes) |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 153 | { |
| 154 | image_header_t *hdr; |
Sergei Poselenov | e344568 | 2007-02-27 20:15:30 +0300 | [diff] [blame] | 155 | unsigned long checksum, fsize; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 156 | |
| 157 | hdr = (image_header_t *)LOAD_ADDR; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 158 | #if defined(CONFIG_FIT) |
Marian Balakowicz | 9a4daad | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 159 | if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 160 | puts ("Non legacy image format not supported\n"); |
| 161 | return -1; |
| 162 | } |
| 163 | #endif |
| 164 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 165 | /* check the easy ones first */ |
| 166 | #undef CHECK_VALID_DEBUG |
| 167 | #ifdef CHECK_VALID_DEBUG |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 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); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 172 | #endif |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 173 | if (nbytes < image_get_header_size ()) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 174 | printf ("Image %s bad header SIZE\n", aufile[idx]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 175 | ausize[idx] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 176 | return -1; |
| 177 | } |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 178 | if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 179 | printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 180 | ausize[idx] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 181 | return -1; |
| 182 | } |
| 183 | /* check the hdr CRC */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 184 | if (!image_check_hcrc (hdr)) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 185 | printf ("Image %s bad header checksum\n", aufile[idx]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 186 | ausize[idx] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 187 | return -1; |
| 188 | } |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 189 | /* check the type - could do this all in one gigantic if() */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 190 | if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 191 | printf ("Image %s wrong type\n", aufile[idx]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 192 | ausize[idx] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 193 | return -1; |
| 194 | } |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 195 | if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 196 | printf ("Image %s wrong type\n", aufile[idx]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 197 | ausize[idx] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 198 | return -1; |
| 199 | } |
Wolfgang Denk | f5fcc3c | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 200 | if ((idx == IDX_ROOTFS) && |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 201 | (!image_check_type (hdr, IH_TYPE_RAMDISK) && |
| 202 | !image_check_type (hdr, IH_TYPE_FILESYSTEM))) { |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 203 | printf ("Image %s wrong type\n", aufile[idx]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 204 | ausize[idx] = 0; |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 205 | return -1; |
| 206 | } |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 207 | /* recycle checksum */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 208 | checksum = image_get_data_size (hdr); |
Sergei Poselenov | e344568 | 2007-02-27 20:15:30 +0300 | [diff] [blame] | 209 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 210 | fsize = checksum + image_get_header_size (); |
Sergei Poselenov | e344568 | 2007-02-27 20:15:30 +0300 | [diff] [blame] | 211 | /* for kernel and ramdisk the image header must also fit into flash */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 212 | if (idx == IDX_KERNEL || image_check_type (hdr, IH_TYPE_RAMDISK)) |
| 213 | checksum += image_get_header_size (); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 214 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 215 | /* check the size does not exceed space in flash. HUSH scripts */ |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 216 | if ((ausize[idx] != 0) && (ausize[idx] < checksum)) { |
| 217 | printf ("Image %s is bigger than FLASH\n", aufile[idx]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 218 | ausize[idx] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 219 | return -1; |
| 220 | } |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 221 | /* Update with the real filesize */ |
Sergei Poselenov | e344568 | 2007-02-27 20:15:30 +0300 | [diff] [blame] | 222 | ausize[idx] = fsize; |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 223 | |
| 224 | return checksum; /* return size to be written to flash */ |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Wolfgang Denk | 82e5236 | 2006-12-22 10:30:26 +0100 | [diff] [blame] | 227 | int au_do_update(int idx, long sz) |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 228 | { |
| 229 | image_header_t *hdr; |
| 230 | char *addr; |
| 231 | long start, end; |
| 232 | int off, rc; |
| 233 | uint nbytes; |
| 234 | |
| 235 | hdr = (image_header_t *)LOAD_ADDR; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 236 | #if defined(CONFIG_FIT) |
Marian Balakowicz | 9a4daad | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 237 | if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) { |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 238 | puts ("Non legacy image format not supported\n"); |
| 239 | return -1; |
| 240 | } |
| 241 | #endif |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 242 | |
| 243 | /* execute a script */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 244 | if (image_check_type (hdr, IH_TYPE_SCRIPT)) { |
| 245 | addr = (char *)((char *)hdr + image_get_header_size ()); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 246 | /* stick a NULL at the end of the script, otherwise */ |
| 247 | /* parse_string_outer() runs off the end. */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 248 | addr[image_get_data_size (hdr)] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 249 | addr += 8; |
| 250 | parse_string_outer(addr, FLAG_PARSE_SEMICOLON); |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | start = aufl_layout[idx].start; |
| 255 | end = aufl_layout[idx].end; |
| 256 | |
| 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; |
| 265 | #endif |
| 266 | flash_sect_protect(0, start, end); |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * erase the address range. |
| 271 | */ |
| 272 | debug ("flash_sect_erase(%lx, %lx);\n", start, end); |
| 273 | flash_sect_erase(start, end); |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 274 | mdelay(100); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 275 | #ifdef CONFIG_PROGRESSBAR |
| 276 | show_progress(end - start, totsize); |
| 277 | #endif |
| 278 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 279 | /* strip the header - except for the kernel and ramdisk */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 280 | if (image_check_type (hdr, IH_TYPE_KERNEL) || |
| 281 | image_check_type (hdr, IH_TYPE_RAMDISK)) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 282 | addr = (char *)hdr; |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 283 | off = image_get_header_size (); |
| 284 | nbytes = image_get_image_size (hdr); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 285 | } else { |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 286 | addr = (char *)((char *)hdr + image_get_header_size ()); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 287 | #ifdef AU_UPDATE_TEST |
| 288 | /* copy it to where Linux goes */ |
| 289 | if (idx == IDX_FIRMWARE) |
| 290 | start = aufl_layout[1].start; |
| 291 | #endif |
| 292 | off = 0; |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 293 | nbytes = image_get_data_size (hdr); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 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); |
| 299 | if (rc != 0) { |
| 300 | printf("Flashing failed due to error %d\n", rc); |
| 301 | return -1; |
| 302 | } |
| 303 | |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 304 | #ifdef CONFIG_PROGRESSBAR |
| 305 | show_progress(nbytes, totsize); |
| 306 | #endif |
| 307 | |
Wolfgang Denk | f5fcc3c | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 308 | /* check the data CRC of the copy */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 309 | if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) != |
| 310 | image_get_dcrc (hdr)) { |
Wolfgang Denk | f5fcc3c | 2007-02-19 23:09:51 +0100 | [diff] [blame] | 311 | printf ("Image %s Bad Data Checksum after COPY\n", aufile[idx]); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 312 | return -1; |
| 313 | } |
| 314 | |
| 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); |
| 319 | return 0; |
| 320 | } |
| 321 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 322 | /* |
| 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. |
| 326 | */ |
Wolfgang Denk | 82e5236 | 2006-12-22 10:30:26 +0100 | [diff] [blame] | 327 | int do_auto_update(void) |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 328 | { |
| 329 | block_dev_desc_t *stor_dev; |
| 330 | long sz; |
Wolfgang Denk | 55b9788 | 2011-11-04 15:55:11 +0000 | [diff] [blame] | 331 | int i, res = 0, cnt, old_ctrlc; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 332 | char *env; |
| 333 | long start, end; |
Wolfgang Denk | a4d2636 | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 334 | |
| 335 | #if 0 /* disable key-press detection to speed up boot-up time */ |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 336 | uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0}; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 337 | |
| 338 | /* |
| 339 | * Read keypad status |
| 340 | */ |
| 341 | i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2); |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 342 | mdelay(500); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 343 | i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2); |
| 344 | |
| 345 | /* |
| 346 | * Check keypad |
| 347 | */ |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 348 | if ( !(keypad_status1[1] & KEYPAD_MASK_LO) || |
| 349 | (keypad_status1[1] != keypad_status2[1])) { |
| 350 | return 0; |
| 351 | } |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 352 | |
Wolfgang Denk | a4d2636 | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 353 | #endif |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 354 | au_usb_stor_curr_dev = -1; |
| 355 | /* start USB */ |
| 356 | if (usb_stop() < 0) { |
| 357 | debug ("usb_stop failed\n"); |
| 358 | return -1; |
| 359 | } |
| 360 | if (usb_init() < 0) { |
| 361 | debug ("usb_init failed\n"); |
| 362 | return -1; |
| 363 | } |
| 364 | /* |
| 365 | * check whether a storage device is attached (assume that it's |
| 366 | * a USB memory stick, since nothing else should be attached). |
| 367 | */ |
| 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"); |
Wolfgang Denk | a4d2636 | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 371 | res = -1; |
| 372 | goto xit; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 373 | } |
| 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"); |
Wolfgang Denk | a4d2636 | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 378 | res = -1; |
| 379 | goto xit; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 380 | } |
| 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); |
Wolfgang Denk | a4d2636 | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 384 | res = -1; |
| 385 | goto xit; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 386 | } |
| 387 | if (file_fat_detectfs() != 0) { |
| 388 | debug ("file_fat_detectfs failed\n"); |
| 389 | } |
| 390 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 391 | /* |
| 392 | * now check whether start and end are defined using environment |
| 393 | * variables. |
| 394 | */ |
| 395 | start = -1; |
| 396 | end = 0; |
| 397 | env = getenv("firmware_st"); |
| 398 | if (env != NULL) |
| 399 | start = simple_strtoul(env, NULL, 16); |
| 400 | env = getenv("firmware_nd"); |
| 401 | if (env != NULL) |
| 402 | end = simple_strtoul(env, NULL, 16); |
| 403 | if (start >= 0 && end && end > start) { |
| 404 | ausize[IDX_FIRMWARE] = (end + 1) - start; |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 405 | aufl_layout[IDX_FIRMWARE].start = start; |
| 406 | aufl_layout[IDX_FIRMWARE].end = end; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 407 | } |
| 408 | start = -1; |
| 409 | end = 0; |
| 410 | env = getenv("kernel_st"); |
| 411 | if (env != NULL) |
| 412 | start = simple_strtoul(env, NULL, 16); |
| 413 | env = getenv("kernel_nd"); |
| 414 | if (env != NULL) |
| 415 | end = simple_strtoul(env, NULL, 16); |
| 416 | if (start >= 0 && end && end > start) { |
| 417 | ausize[IDX_KERNEL] = (end + 1) - start; |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 418 | aufl_layout[IDX_KERNEL].start = start; |
| 419 | aufl_layout[IDX_KERNEL].end = end; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 420 | } |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 421 | start = -1; |
| 422 | end = 0; |
| 423 | env = getenv("rootfs_st"); |
| 424 | if (env != NULL) |
| 425 | start = simple_strtoul(env, NULL, 16); |
| 426 | env = getenv("rootfs_nd"); |
| 427 | if (env != NULL) |
| 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; |
| 433 | } |
| 434 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 435 | /* make certain that HUSH is runnable */ |
| 436 | u_boot_hush_start(); |
| 437 | /* make sure that we see CTRL-C and save the old state */ |
| 438 | old_ctrlc = disable_ctrlc(0); |
| 439 | |
Wolfgang Denk | 7435711 | 2007-02-27 14:26:04 +0100 | [diff] [blame] | 440 | /* validate the images first */ |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 441 | for (i = 0; i < AU_MAXFILES; i++) { |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 442 | ulong imsize; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 443 | /* just read the header */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 444 | sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ()); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 445 | debug ("read %s sz %ld hdr %d\n", |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 446 | aufile[i], sz, image_get_header_size ()); |
| 447 | if (sz <= 0 || sz < image_get_header_size ()) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 448 | debug ("%s not found\n", aufile[i]); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 449 | ausize[i] = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 450 | continue; |
| 451 | } |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 452 | /* au_check_header_valid() updates ausize[] */ |
| 453 | if ((imsize = au_check_header_valid(i, sz)) < 0) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 454 | debug ("%s header not valid\n", aufile[i]); |
| 455 | continue; |
| 456 | } |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 457 | /* totsize accounts for image size and flash erase size */ |
Wolfgang Denk | 7435711 | 2007-02-27 14:26:04 +0100 | [diff] [blame] | 458 | totsize += (imsize + (aufl_layout[i].end - aufl_layout[i].start)); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | #ifdef CONFIG_PROGRESSBAR |
| 462 | if (totsize) { |
| 463 | lcd_puts(" Update in progress\n"); |
| 464 | lcd_enable(); |
| 465 | } |
| 466 | #endif |
| 467 | |
| 468 | /* just loop thru all the possible files */ |
| 469 | for (i = 0; i < AU_MAXFILES && totsize; i++) { |
| 470 | if (!ausize[i]) { |
| 471 | continue; |
| 472 | } |
| 473 | sz = file_fat_read(aufile[i], LOAD_ADDR, ausize[i]); |
| 474 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 475 | debug ("read %s sz %ld hdr %d\n", |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 476 | aufile[i], sz, image_get_header_size ()); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 477 | |
| 478 | if (sz != ausize[i]) { |
Wolfgang Denk | 9b55a25 | 2008-07-11 01:16:00 +0200 | [diff] [blame] | 479 | printf ("%s: size %ld read %ld?\n", aufile[i], ausize[i], sz); |
Wolfgang Denk | 7435711 | 2007-02-27 14:26:04 +0100 | [diff] [blame] | 480 | continue; |
| 481 | } |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 482 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 483 | if (sz <= 0 || sz <= image_get_header_size ()) { |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 484 | debug ("%s not found\n", aufile[i]); |
| 485 | continue; |
| 486 | } |
| 487 | if (au_check_cksum_valid(i, sz) < 0) { |
| 488 | debug ("%s checksum not valid\n", aufile[i]); |
| 489 | continue; |
| 490 | } |
| 491 | /* this is really not a good idea, but it's what the */ |
| 492 | /* customer wants. */ |
| 493 | cnt = 0; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 494 | do { |
| 495 | res = au_do_update(i, sz); |
| 496 | /* let the user break out of the loop */ |
| 497 | if (ctrlc() || had_ctrlc()) { |
| 498 | clear_ctrlc(); |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 499 | break; |
| 500 | } |
| 501 | cnt++; |
| 502 | #ifdef AU_TEST_ONLY |
Sergei Poselenov | 489c696 | 2007-02-14 14:30:28 +0300 | [diff] [blame] | 503 | } while (res < 0 && cnt < (AU_MAXFILES + 1)); |
| 504 | if (cnt < (AU_MAXFILES + 1)) |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 505 | #else |
| 506 | } while (res < 0); |
| 507 | #endif |
| 508 | } |
Wolfgang Denk | a4d2636 | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 509 | |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 510 | /* restore the old state */ |
| 511 | disable_ctrlc(old_ctrlc); |
Sergei Poselenov | 638dd14 | 2007-02-27 12:40:16 +0300 | [diff] [blame] | 512 | #ifdef CONFIG_PROGRESSBAR |
| 513 | if (totsize) { |
| 514 | if (!res) { |
| 515 | lcd_puts("\n Update completed\n"); |
| 516 | } else { |
| 517 | lcd_puts("\n Update error\n"); |
| 518 | } |
| 519 | lcd_enable(); |
| 520 | } |
| 521 | #endif |
Wolfgang Denk | a4d2636 | 2007-08-12 15:11:38 +0200 | [diff] [blame] | 522 | xit: |
| 523 | usb_stop(); |
| 524 | return res; |
Andrei Safronov | cdb97a6 | 2006-12-08 16:23:08 +0100 | [diff] [blame] | 525 | } |
| 526 | #endif /* CONFIG_AUTO_UPDATE */ |