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>
27 #ifdef CFG_HUSH_PARSER
32 #ifdef CONFIG_AUTO_UPDATE
34 #ifndef CONFIG_USB_OHCI
35 #error "must define CONFIG_USB_OHCI"
38 #ifndef CONFIG_USB_STORAGE
39 #error "must define CONFIG_USB_STORAGE"
42 #ifndef CFG_HUSH_PARSER
43 #error "must define CFG_HUSH_PARSER"
46 #if !(CONFIG_COMMANDS & CFG_CMD_FAT)
47 #error "must define CFG_CMD_FAT"
54 #define debug(fmt,args...) printf (fmt ,##args)
56 #define debug(fmt,args...)
59 /* possible names of files on the USB stick. */
60 #define AU_FIRMWARE "u-boot.img"
61 #define AU_KERNEL "kernel.img"
62 #define AU_ROOTFS "rootfs.img"
69 /* layout of the FLASH. ST = start address, ND = end address. */
70 #define AU_FL_FIRMWARE_ST 0xfC000000
71 #define AU_FL_FIRMWARE_ND 0xfC03FFFF
72 #define AU_FL_KERNEL_ST 0xfC0C0000
73 #define AU_FL_KERNEL_ND 0xfC1BFFFF
74 #define AU_FL_ROOTFS_ST 0xFC1C0000
75 #define AU_FL_ROOTFS_ND 0xFCFBFFFF
77 static int au_usb_stor_curr_dev; /* current device */
79 /* index of each file in the following arrays */
80 #define IDX_FIRMWARE 0
84 /* max. number of files which could interest us */
87 /* pointers to file names */
88 char *aufile[AU_MAXFILES] = {
94 /* sizes of flash areas for each file */
95 long ausize[AU_MAXFILES] = {
96 (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST,
97 (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST,
98 (AU_FL_ROOTFS_ND + 1) - AU_FL_ROOTFS_ST,
101 /* array of flash areas start and end addresses */
102 struct flash_layout aufl_layout[AU_MAXFILES] = {
103 { AU_FL_FIRMWARE_ST, AU_FL_FIRMWARE_ND, },
104 { AU_FL_KERNEL_ST, AU_FL_KERNEL_ND, },
105 { AU_FL_ROOTFS_ST, AU_FL_ROOTFS_ND, },
108 /* where to load files into memory */
109 #define LOAD_ADDR ((unsigned char *)0x00200000)
111 /* the root file system is the largest image */
112 #define MAX_LOADSZ ausize[IDX_ROOTFS]
114 /*i2c address of the keypad status*/
115 #define I2C_PSOC_KEYPAD_ADDR 0x53
120 #define KEYPAD_MASK_LO ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))&0xFF)
121 #define KEYPAD_MASK_HI ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))>>8)
124 extern int fat_register_device(block_dev_desc_t *, int);
125 extern int file_fat_detectfs(void);
126 extern long file_fat_read(const char *, void *, unsigned long);
127 extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
128 extern int flash_sect_erase(ulong, ulong);
129 extern int flash_sect_protect (int, ulong, ulong);
130 extern int flash_write (char *, ulong, ulong);
131 /* change char* to void* to shutup the compiler */
132 extern block_dev_desc_t *get_dev (char*, int);
133 extern int u_boot_hush_start(void);
135 int au_check_cksum_valid(int idx, long nbytes)
138 unsigned long checksum;
140 hdr = (image_header_t *)LOAD_ADDR;
142 if (nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size))) {
143 printf ("Image %s bad total SIZE\n", aufile[idx]);
146 /* check the data CRC */
147 checksum = ntohl(hdr->ih_dcrc);
149 if (crc32 (0, (uchar *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size)) != checksum) {
150 printf ("Image %s bad data checksum\n", aufile[idx]);
156 int au_check_header_valid(int idx, long nbytes)
159 unsigned long checksum;
161 hdr = (image_header_t *)LOAD_ADDR;
162 /* check the easy ones first */
163 #undef CHECK_VALID_DEBUG
164 #ifdef CHECK_VALID_DEBUG
165 printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
166 printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_ARM);
167 printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
168 printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
170 if (nbytes < sizeof(*hdr)) {
171 printf ("Image %s bad header SIZE\n", aufile[idx]);
174 if (ntohl(hdr->ih_magic) != IH_MAGIC || hdr->ih_arch != IH_CPU_PPC) {
175 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
178 /* check the hdr CRC */
179 checksum = ntohl(hdr->ih_hcrc);
182 if (crc32 (0, (uchar *)hdr, sizeof(*hdr)) != checksum) {
183 printf ("Image %s bad header checksum\n", aufile[idx]);
186 hdr->ih_hcrc = htonl(checksum);
187 /* check the type - could do this all in one gigantic if() */
188 if ((idx == IDX_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
189 printf ("Image %s wrong type\n", aufile[idx]);
192 if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
193 printf ("Image %s wrong type\n", aufile[idx]);
196 if ((idx == IDX_ROOTFS) &&
197 ( (hdr->ih_type != IH_TYPE_RAMDISK) || (hdr->ih_type != IH_TYPE_FILESYSTEM) )
199 printf ("Image %s wrong type\n", aufile[idx]);
202 /* recycle checksum */
203 checksum = ntohl(hdr->ih_size);
204 /* for kernel and app the image header must also fit into flash */
205 if (idx != IDX_FIRMWARE)
206 checksum += sizeof(*hdr);
207 /* check the size does not exceed space in flash. HUSH scripts */
208 /* all have ausize[] set to 0 */
209 if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
210 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
216 int au_do_update(int idx, long sz)
224 hdr = (image_header_t *)LOAD_ADDR;
226 /* execute a script */
227 if (hdr->ih_type == IH_TYPE_SCRIPT) {
228 addr = (char *)((char *)hdr + sizeof(*hdr));
229 /* stick a NULL at the end of the script, otherwise */
230 /* parse_string_outer() runs off the end. */
231 addr[ntohl(hdr->ih_size)] = 0;
233 parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
237 start = aufl_layout[idx].start;
238 end = aufl_layout[idx].end;
240 /* unprotect the address range */
241 /* this assumes that ONLY the firmware is protected! */
242 if (idx == IDX_FIRMWARE) {
243 #undef AU_UPDATE_TEST
244 #ifdef AU_UPDATE_TEST
245 /* erase it where Linux goes */
246 start = aufl_layout[1].start;
247 end = aufl_layout[1].end;
249 flash_sect_protect(0, start, end);
253 * erase the address range.
255 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
256 flash_sect_erase(start, end);
258 /* strip the header - except for the kernel and ramdisk */
259 if (hdr->ih_type == IH_TYPE_KERNEL || hdr->ih_type == IH_TYPE_RAMDISK) {
262 nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
264 addr = (char *)((char *)hdr + sizeof(*hdr));
265 #ifdef AU_UPDATE_TEST
266 /* copy it to where Linux goes */
267 if (idx == IDX_FIRMWARE)
268 start = aufl_layout[1].start;
271 nbytes = ntohl(hdr->ih_size);
274 /* copy the data from RAM to FLASH */
275 debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
276 rc = flash_write(addr, start, nbytes);
278 printf("Flashing failed due to error %d\n", rc);
282 /* check the data CRC of the copy */
283 if (crc32 (0, (uchar *)(start + off), ntohl(hdr->ih_size)) != ntohl(hdr->ih_dcrc)) {
284 printf ("Image %s Bad Data Checksum after COPY\n", aufile[idx]);
288 /* protect the address range */
289 /* this assumes that ONLY the firmware is protected! */
290 if (idx == IDX_FIRMWARE)
291 flash_sect_protect(1, start, end);
296 * this is called from board_init() after the hardware has been set up
297 * and is usable. That seems like a good time to do this.
298 * Right now the return value is ignored.
300 int do_auto_update(void)
302 block_dev_desc_t *stor_dev;
304 int i, res, bitmap_first, cnt, old_ctrlc, got_ctrlc;
307 uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
312 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
314 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
319 if ( !(keypad_status1[1] & KEYPAD_MASK_LO) ||
320 (keypad_status1[1] != keypad_status2[1])) {
323 au_usb_stor_curr_dev = -1;
325 if (usb_stop() < 0) {
326 debug ("usb_stop failed\n");
329 if (usb_init() < 0) {
330 debug ("usb_init failed\n");
334 * check whether a storage device is attached (assume that it's
335 * a USB memory stick, since nothing else should be attached).
337 au_usb_stor_curr_dev = usb_stor_scan(0);
338 if (au_usb_stor_curr_dev == -1) {
339 debug ("No device found. Not initialized?\n");
342 /* check whether it has a partition table */
343 stor_dev = get_dev("usb", 0);
344 if (stor_dev == NULL) {
345 debug ("uknown device type\n");
348 if (fat_register_device(stor_dev, 1) != 0) {
349 debug ("Unable to use USB %d:%d for fatls\n",
350 au_usb_stor_curr_dev, 1);
353 if (file_fat_detectfs() != 0) {
354 debug ("file_fat_detectfs failed\n");
358 * now check whether start and end are defined using environment
363 env = getenv("firmware_st");
365 start = simple_strtoul(env, NULL, 16);
366 env = getenv("firmware_nd");
368 end = simple_strtoul(env, NULL, 16);
369 if (start >= 0 && end && end > start) {
370 ausize[IDX_FIRMWARE] = (end + 1) - start;
371 aufl_layout[IDX_FIRMWARE].start = start;
372 aufl_layout[IDX_FIRMWARE].end = end;
376 env = getenv("kernel_st");
378 start = simple_strtoul(env, NULL, 16);
379 env = getenv("kernel_nd");
381 end = simple_strtoul(env, NULL, 16);
382 if (start >= 0 && end && end > start) {
383 ausize[IDX_KERNEL] = (end + 1) - start;
384 aufl_layout[IDX_KERNEL].start = start;
385 aufl_layout[IDX_KERNEL].end = end;
389 env = getenv("rootfs_st");
391 start = simple_strtoul(env, NULL, 16);
392 env = getenv("rootfs_nd");
394 end = simple_strtoul(env, NULL, 16);
395 if (start >= 0 && end && end > start) {
396 ausize[IDX_ROOTFS] = (end + 1) - start;
397 aufl_layout[IDX_ROOTFS].start = start;
398 aufl_layout[IDX_ROOTFS].end = end;
401 /* make certain that HUSH is runnable */
403 /* make sure that we see CTRL-C and save the old state */
404 old_ctrlc = disable_ctrlc(0);
407 /* just loop thru all the possible files */
408 for (i = 0; i < AU_MAXFILES; i++) {
409 /* just read the header */
410 sz = file_fat_read(aufile[i], LOAD_ADDR, sizeof(image_header_t));
411 debug ("read %s sz %ld hdr %d\n",
412 aufile[i], sz, sizeof(image_header_t));
413 if (sz <= 0 || sz < sizeof(image_header_t)) {
414 debug ("%s not found\n", aufile[i]);
417 if (au_check_header_valid(i, sz) < 0) {
418 debug ("%s header not valid\n", aufile[i]);
421 sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
422 debug ("read %s sz %ld hdr %d\n",
423 aufile[i], sz, sizeof(image_header_t));
424 if (sz <= 0 || sz <= sizeof(image_header_t)) {
425 debug ("%s not found\n", aufile[i]);
428 if (au_check_cksum_valid(i, sz) < 0) {
429 debug ("%s checksum not valid\n", aufile[i]);
432 /* this is really not a good idea, but it's what the */
433 /* customer wants. */
437 res = au_do_update(i, sz);
438 /* let the user break out of the loop */
439 if (ctrlc() || had_ctrlc()) {
447 } while (res < 0 && cnt < (AU_MAXFILES + 1));
448 if (cnt < (AU_MAXFILES + 1))
454 /* restore the old state */
455 disable_ctrlc(old_ctrlc);
458 #endif /* CONFIG_AUTO_UPDATE */