blob: 12091fd7821406679e5df1ca2b1e7a3a7524cb6d [file] [log] [blame]
Andrei Safronovcdb97a62006-12-08 16:23:08 +01001/*
2 * (C) Copyright 2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
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.
9 *
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.
14 *
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,
18 * MA 02111-1307 USA
19 */
20#include <common.h>
21#include <command.h>
22#include <malloc.h>
23#include <image.h>
24#include <asm/byteorder.h>
25#include <usb.h>
26
27#ifdef CFG_HUSH_PARSER
28#include <hush.h>
29#endif
30
31
32#ifdef CONFIG_AUTO_UPDATE
33
34#ifndef CONFIG_USB_OHCI
35#error "must define CONFIG_USB_OHCI"
36#endif
37
38#ifndef CONFIG_USB_STORAGE
39#error "must define CONFIG_USB_STORAGE"
40#endif
41
42#ifndef CFG_HUSH_PARSER
43#error "must define CFG_HUSH_PARSER"
44#endif
45
46#if !(CONFIG_COMMANDS & CFG_CMD_FAT)
47#error "must define CFG_CMD_FAT"
48#endif
49
Andrei Safronovcdb97a62006-12-08 16:23:08 +010050#undef AU_DEBUG
51
52#undef debug
53#ifdef AU_DEBUG
54#define debug(fmt,args...) printf (fmt ,##args)
55#else
56#define debug(fmt,args...)
57#endif /* AU_DEBUG */
58
59/* possible names of files on the USB stick. */
60#define AU_FIRMWARE "u-boot.img"
61#define AU_KERNEL "kernel.img"
Sergei Poselenov489c6962007-02-14 14:30:28 +030062#define AU_ROOTFS "rootfs.img"
Andrei Safronovcdb97a62006-12-08 16:23:08 +010063
Wolfgang Denk82e52362006-12-22 10:30:26 +010064struct flash_layout {
Andrei Safronovcdb97a62006-12-08 16:23:08 +010065 long start;
66 long end;
67};
68
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
Sergei Poselenov489c6962007-02-14 14:30:28 +030074#define AU_FL_ROOTFS_ST 0xFC1C0000
75#define AU_FL_ROOTFS_ND 0xFCFBFFFF
Andrei Safronovcdb97a62006-12-08 16:23:08 +010076
77static int au_usb_stor_curr_dev; /* current device */
78
79/* index of each file in the following arrays */
80#define IDX_FIRMWARE 0
81#define IDX_KERNEL 1
Sergei Poselenov489c6962007-02-14 14:30:28 +030082#define IDX_ROOTFS 2
Andrei Safronovcdb97a62006-12-08 16:23:08 +010083
84/* max. number of files which could interest us */
Sergei Poselenov489c6962007-02-14 14:30:28 +030085#define AU_MAXFILES 3
Andrei Safronovcdb97a62006-12-08 16:23:08 +010086
87/* pointers to file names */
Sergei Poselenov489c6962007-02-14 14:30:28 +030088char *aufile[AU_MAXFILES] = {
89 AU_FIRMWARE,
90 AU_KERNEL,
91 AU_ROOTFS
92};
Andrei Safronovcdb97a62006-12-08 16:23:08 +010093
94/* sizes of flash areas for each file */
Sergei Poselenov489c6962007-02-14 14:30:28 +030095long 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
99};
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100100
101/* array of flash areas start and end addresses */
Sergei Poselenov489c6962007-02-14 14:30:28 +0300102struct 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,}
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100106};
107
108/* where to load files into memory */
109#define LOAD_ADDR ((unsigned char *)0x00200000)
110
111/* the app is the largest image */
Sergei Poselenov489c6962007-02-14 14:30:28 +0300112#define MAX_LOADSZ ausize[IDX_ROOTFS]
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100113
114/*i2c address of the keypad status*/
115#define I2C_PSOC_KEYPAD_ADDR 0x53
116
117/* keypad mask */
Wolfgang Denk787fa152007-01-10 01:28:39 +0100118#define KEYPAD_ROW 2
119#define KEYPAD_COL 2
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)
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100122
123/* externals */
124extern int fat_register_device(block_dev_desc_t *, int);
125extern int file_fat_detectfs(void);
126extern long file_fat_read(const char *, void *, unsigned long);
127extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
128extern int flash_sect_erase(ulong, ulong);
129extern int flash_sect_protect (int, ulong, ulong);
130extern int flash_write (char *, ulong, ulong);
131/* change char* to void* to shutup the compiler */
132extern block_dev_desc_t *get_dev (char*, int);
133extern int u_boot_hush_start(void);
134
Wolfgang Denk82e52362006-12-22 10:30:26 +0100135int au_check_cksum_valid(int idx, long nbytes)
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100136{
137 image_header_t *hdr;
138 unsigned long checksum;
139
140 hdr = (image_header_t *)LOAD_ADDR;
141
Wolfgang Denk82e52362006-12-22 10:30:26 +0100142 if (nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size))) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100143 printf ("Image %s bad total SIZE\n", aufile[idx]);
144 return -1;
145 }
146 /* check the data CRC */
147 checksum = ntohl(hdr->ih_dcrc);
148
Wolfgang Denk82e52362006-12-22 10:30:26 +0100149 if (crc32 (0, (uchar *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size)) != checksum) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100150 printf ("Image %s bad data checksum\n", aufile[idx]);
151 return -1;
152 }
153 return 0;
154}
155
Wolfgang Denk82e52362006-12-22 10:30:26 +0100156int au_check_header_valid(int idx, long nbytes)
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100157{
158 image_header_t *hdr;
159 unsigned long checksum;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100160
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);
169#endif
Wolfgang Denk82e52362006-12-22 10:30:26 +0100170 if (nbytes < sizeof(*hdr)) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100171 printf ("Image %s bad header SIZE\n", aufile[idx]);
172 return -1;
173 }
Wolfgang Denk82e52362006-12-22 10:30:26 +0100174 if (ntohl(hdr->ih_magic) != IH_MAGIC || hdr->ih_arch != IH_CPU_PPC) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100175 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
176 return -1;
177 }
178 /* check the hdr CRC */
179 checksum = ntohl(hdr->ih_hcrc);
180 hdr->ih_hcrc = 0;
181
182 if (crc32 (0, (uchar *)hdr, sizeof(*hdr)) != checksum) {
183 printf ("Image %s bad header checksum\n", aufile[idx]);
184 return -1;
185 }
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]);
190 return -1;
191 }
192 if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
193 printf ("Image %s wrong type\n", aufile[idx]);
194 return -1;
195 }
Sergei Poselenov489c6962007-02-14 14:30:28 +0300196 if ((idx == IDX_ROOTFS) && (hdr->ih_type != IH_TYPE_RAMDISK)) {
197 printf ("Image %s wrong type\n", aufile[idx]);
198 return -1;
199 }
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100200 /* recycle checksum */
201 checksum = ntohl(hdr->ih_size);
202 /* for kernel and app the image header must also fit into flash */
203 if (idx != IDX_FIRMWARE)
204 checksum += sizeof(*hdr);
205 /* check the size does not exceed space in flash. HUSH scripts */
206 /* all have ausize[] set to 0 */
207 if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
208 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
209 return -1;
210 }
211 return 0;
212}
213
Wolfgang Denk82e52362006-12-22 10:30:26 +0100214int au_do_update(int idx, long sz)
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100215{
216 image_header_t *hdr;
217 char *addr;
218 long start, end;
219 int off, rc;
220 uint nbytes;
221
222 hdr = (image_header_t *)LOAD_ADDR;
223
224 /* execute a script */
225 if (hdr->ih_type == IH_TYPE_SCRIPT) {
226 addr = (char *)((char *)hdr + sizeof(*hdr));
227 /* stick a NULL at the end of the script, otherwise */
228 /* parse_string_outer() runs off the end. */
229 addr[ntohl(hdr->ih_size)] = 0;
230 addr += 8;
231 parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
232 return 0;
233 }
234
235 start = aufl_layout[idx].start;
236 end = aufl_layout[idx].end;
237
238 /* unprotect the address range */
239 /* this assumes that ONLY the firmware is protected! */
240 if (idx == IDX_FIRMWARE) {
241#undef AU_UPDATE_TEST
242#ifdef AU_UPDATE_TEST
243 /* erase it where Linux goes */
244 start = aufl_layout[1].start;
245 end = aufl_layout[1].end;
246#endif
247 flash_sect_protect(0, start, end);
248 }
249
250 /*
251 * erase the address range.
252 */
253 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
254 flash_sect_erase(start, end);
255 wait_ms(100);
256 /* strip the header - except for the kernel and ramdisk */
Sergei Poselenov489c6962007-02-14 14:30:28 +0300257 if (hdr->ih_type == IH_TYPE_KERNEL || hdr->ih_type == IH_TYPE_RAMDISK) {
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100258 addr = (char *)hdr;
259 off = sizeof(*hdr);
260 nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
261 } else {
262 addr = (char *)((char *)hdr + sizeof(*hdr));
263#ifdef AU_UPDATE_TEST
264 /* copy it to where Linux goes */
265 if (idx == IDX_FIRMWARE)
266 start = aufl_layout[1].start;
267#endif
268 off = 0;
269 nbytes = ntohl(hdr->ih_size);
270 }
271
272 /* copy the data from RAM to FLASH */
273 debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
274 rc = flash_write(addr, start, nbytes);
275 if (rc != 0) {
276 printf("Flashing failed due to error %d\n", rc);
277 return -1;
278 }
279
280 /* check the dcrc of the copy */
281 if (crc32 (0, (uchar *)(start + off), ntohl(hdr->ih_size)) != ntohl(hdr->ih_dcrc)) {
282 printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
283 return -1;
284 }
285
286 /* protect the address range */
287 /* this assumes that ONLY the firmware is protected! */
288 if (idx == IDX_FIRMWARE)
289 flash_sect_protect(1, start, end);
290 return 0;
291}
292
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100293/*
294 * this is called from board_init() after the hardware has been set up
295 * and is usable. That seems like a good time to do this.
296 * Right now the return value is ignored.
297 */
Wolfgang Denk82e52362006-12-22 10:30:26 +0100298int do_auto_update(void)
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100299{
300 block_dev_desc_t *stor_dev;
301 long sz;
302 int i, res, bitmap_first, cnt, old_ctrlc, got_ctrlc;
303 char *env;
304 long start, end;
Sergei Poselenov489c6962007-02-14 14:30:28 +0300305 uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100306
307 /*
308 * Read keypad status
309 */
310 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
311 wait_ms(500);
312 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
313
314 /*
315 * Check keypad
316 */
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100317 if ( !(keypad_status1[1] & KEYPAD_MASK_LO) ||
318 (keypad_status1[1] != keypad_status2[1])) {
319 return 0;
320 }
321 au_usb_stor_curr_dev = -1;
322 /* start USB */
323 if (usb_stop() < 0) {
324 debug ("usb_stop failed\n");
325 return -1;
326 }
327 if (usb_init() < 0) {
328 debug ("usb_init failed\n");
329 return -1;
330 }
331 /*
332 * check whether a storage device is attached (assume that it's
333 * a USB memory stick, since nothing else should be attached).
334 */
335 au_usb_stor_curr_dev = usb_stor_scan(0);
336 if (au_usb_stor_curr_dev == -1) {
337 debug ("No device found. Not initialized?\n");
338 return -1;
339 }
340 /* check whether it has a partition table */
341 stor_dev = get_dev("usb", 0);
342 if (stor_dev == NULL) {
343 debug ("uknown device type\n");
344 return -1;
345 }
346 if (fat_register_device(stor_dev, 1) != 0) {
347 debug ("Unable to use USB %d:%d for fatls\n",
348 au_usb_stor_curr_dev, 1);
349 return -1;
350 }
351 if (file_fat_detectfs() != 0) {
352 debug ("file_fat_detectfs failed\n");
353 }
354
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100355 /*
356 * now check whether start and end are defined using environment
357 * variables.
358 */
359 start = -1;
360 end = 0;
361 env = getenv("firmware_st");
362 if (env != NULL)
363 start = simple_strtoul(env, NULL, 16);
364 env = getenv("firmware_nd");
365 if (env != NULL)
366 end = simple_strtoul(env, NULL, 16);
367 if (start >= 0 && end && end > start) {
368 ausize[IDX_FIRMWARE] = (end + 1) - start;
Sergei Poselenov489c6962007-02-14 14:30:28 +0300369 aufl_layout[IDX_FIRMWARE].start = start;
370 aufl_layout[IDX_FIRMWARE].end = end;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100371 }
372 start = -1;
373 end = 0;
374 env = getenv("kernel_st");
375 if (env != NULL)
376 start = simple_strtoul(env, NULL, 16);
377 env = getenv("kernel_nd");
378 if (env != NULL)
379 end = simple_strtoul(env, NULL, 16);
380 if (start >= 0 && end && end > start) {
381 ausize[IDX_KERNEL] = (end + 1) - start;
Sergei Poselenov489c6962007-02-14 14:30:28 +0300382 aufl_layout[IDX_KERNEL].start = start;
383 aufl_layout[IDX_KERNEL].end = end;
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100384 }
Sergei Poselenov489c6962007-02-14 14:30:28 +0300385 start = -1;
386 end = 0;
387 env = getenv("rootfs_st");
388 if (env != NULL)
389 start = simple_strtoul(env, NULL, 16);
390 env = getenv("rootfs_nd");
391 if (env != NULL)
392 end = simple_strtoul(env, NULL, 16);
393 if (start >= 0 && end && end > start) {
394 ausize[IDX_ROOTFS] = (end + 1) - start;
395 aufl_layout[IDX_ROOTFS].start = start;
396 aufl_layout[IDX_ROOTFS].end = end;
397 }
398
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100399 /* make certain that HUSH is runnable */
400 u_boot_hush_start();
401 /* make sure that we see CTRL-C and save the old state */
402 old_ctrlc = disable_ctrlc(0);
403
404 bitmap_first = 0;
405 /* just loop thru all the possible files */
406 for (i = 0; i < AU_MAXFILES; i++) {
407 /* just read the header */
408 sz = file_fat_read(aufile[i], LOAD_ADDR, sizeof(image_header_t));
409 debug ("read %s sz %ld hdr %d\n",
410 aufile[i], sz, sizeof(image_header_t));
411 if (sz <= 0 || sz < sizeof(image_header_t)) {
412 debug ("%s not found\n", aufile[i]);
413 continue;
414 }
415 if (au_check_header_valid(i, sz) < 0) {
416 debug ("%s header not valid\n", aufile[i]);
417 continue;
418 }
419 sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
420 debug ("read %s sz %ld hdr %d\n",
421 aufile[i], sz, sizeof(image_header_t));
422 if (sz <= 0 || sz <= sizeof(image_header_t)) {
423 debug ("%s not found\n", aufile[i]);
424 continue;
425 }
426 if (au_check_cksum_valid(i, sz) < 0) {
427 debug ("%s checksum not valid\n", aufile[i]);
428 continue;
429 }
430 /* this is really not a good idea, but it's what the */
431 /* customer wants. */
432 cnt = 0;
433 got_ctrlc = 0;
434 do {
435 res = au_do_update(i, sz);
436 /* let the user break out of the loop */
437 if (ctrlc() || had_ctrlc()) {
438 clear_ctrlc();
439 if (res < 0)
440 got_ctrlc = 1;
441 break;
442 }
443 cnt++;
444#ifdef AU_TEST_ONLY
Sergei Poselenov489c6962007-02-14 14:30:28 +0300445 } while (res < 0 && cnt < (AU_MAXFILES + 1));
446 if (cnt < (AU_MAXFILES + 1))
Andrei Safronovcdb97a62006-12-08 16:23:08 +0100447#else
448 } while (res < 0);
449#endif
450 }
451 usb_stop();
452 /* restore the old state */
453 disable_ctrlc(old_ctrlc);
454 return 0;
455}
456#endif /* CONFIG_AUTO_UPDATE */