]> git.ipfire.org Git - people/ms/u-boot.git/blob - board/mcc200/auto_update.c
1df63174a5b36a54e4ac4e9aa0a72169528dc110
[people/ms/u-boot.git] / board / mcc200 / auto_update.c
1 /*
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 #include <part.h>
27
28 #ifdef CFG_HUSH_PARSER
29 #include <hush.h>
30 #endif
31
32
33 #ifdef CONFIG_AUTO_UPDATE
34
35 #ifndef CONFIG_USB_OHCI
36 #error "must define CONFIG_USB_OHCI"
37 #endif
38
39 #ifndef CONFIG_USB_STORAGE
40 #error "must define CONFIG_USB_STORAGE"
41 #endif
42
43 #ifndef CFG_HUSH_PARSER
44 #error "must define CFG_HUSH_PARSER"
45 #endif
46
47 #if !defined(CONFIG_CMD_FAT)
48 #error "must define CFG_CMD_FAT"
49 #endif
50
51 #undef AU_DEBUG
52
53 #undef debug
54 #ifdef AU_DEBUG
55 #define debug(fmt,args...) printf (fmt ,##args)
56 #else
57 #define debug(fmt,args...)
58 #endif /* AU_DEBUG */
59
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"
64
65 struct flash_layout {
66 long start;
67 long end;
68 };
69
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
77
78 static int au_usb_stor_curr_dev; /* current device */
79
80 /* index of each file in the following arrays */
81 #define IDX_FIRMWARE 0
82 #define IDX_KERNEL 1
83 #define IDX_ROOTFS 2
84
85 /* max. number of files which could interest us */
86 #define AU_MAXFILES 3
87
88 /* pointers to file names */
89 char *aufile[AU_MAXFILES] = {
90 AU_FIRMWARE,
91 AU_KERNEL,
92 AU_ROOTFS
93 };
94
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,
100 };
101
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, },
107 };
108
109 ulong totsize;
110
111 /* where to load files into memory */
112 #define LOAD_ADDR ((unsigned char *)0x00200000)
113
114 /* the root file system is the largest image */
115 #define MAX_LOADSZ ausize[IDX_ROOTFS]
116
117 /*i2c address of the keypad status*/
118 #define I2C_PSOC_KEYPAD_ADDR 0x53
119
120 /* keypad mask */
121 #define KEYPAD_ROW 2
122 #define KEYPAD_COL 2
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)
125
126 /* externals */
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);
139 #endif
140
141 int au_check_cksum_valid(int idx, long nbytes)
142 {
143 image_header_t *hdr;
144 unsigned long checksum;
145
146 hdr = (image_header_t *)LOAD_ADDR;
147
148 if (nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size))) {
149 printf ("Image %s bad total SIZE\n", aufile[idx]);
150 return -1;
151 }
152 /* check the data CRC */
153 checksum = ntohl(hdr->ih_dcrc);
154
155 if (crc32 (0, (uchar *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size)) != checksum) {
156 printf ("Image %s bad data checksum\n", aufile[idx]);
157 return -1;
158 }
159 return 0;
160 }
161
162 int au_check_header_valid(int idx, long nbytes)
163 {
164 image_header_t *hdr;
165 unsigned long checksum, fsize;
166
167 hdr = (image_header_t *)LOAD_ADDR;
168 /* check the easy ones first */
169 #undef CHECK_VALID_DEBUG
170 #ifdef CHECK_VALID_DEBUG
171 printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
172 printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_ARM);
173 printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
174 printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
175 #endif
176 if (nbytes < sizeof(*hdr)) {
177 printf ("Image %s bad header SIZE\n", aufile[idx]);
178 ausize[idx] = 0;
179 return -1;
180 }
181 if (ntohl(hdr->ih_magic) != IH_MAGIC || hdr->ih_arch != IH_CPU_PPC) {
182 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
183 ausize[idx] = 0;
184 return -1;
185 }
186 /* check the hdr CRC */
187 checksum = ntohl(hdr->ih_hcrc);
188 hdr->ih_hcrc = 0;
189
190 if (crc32 (0, (uchar *)hdr, sizeof(*hdr)) != checksum) {
191 printf ("Image %s bad header checksum\n", aufile[idx]);
192 ausize[idx] = 0;
193 return -1;
194 }
195 hdr->ih_hcrc = htonl(checksum);
196 /* check the type - could do this all in one gigantic if() */
197 if ((idx == IDX_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
198 printf ("Image %s wrong type\n", aufile[idx]);
199 ausize[idx] = 0;
200 return -1;
201 }
202 if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
203 printf ("Image %s wrong type\n", aufile[idx]);
204 ausize[idx] = 0;
205 return -1;
206 }
207 if ((idx == IDX_ROOTFS) &&
208 ( (hdr->ih_type != IH_TYPE_RAMDISK) && (hdr->ih_type != IH_TYPE_FILESYSTEM) )
209 ) {
210 printf ("Image %s wrong type\n", aufile[idx]);
211 ausize[idx] = 0;
212 return -1;
213 }
214 /* recycle checksum */
215 checksum = ntohl(hdr->ih_size);
216
217 fsize = checksum + sizeof(*hdr);
218 /* for kernel and ramdisk the image header must also fit into flash */
219 if (idx == IDX_KERNEL || hdr->ih_type == IH_TYPE_RAMDISK)
220 checksum += sizeof(*hdr);
221
222 /* check the size does not exceed space in flash. HUSH scripts */
223 if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
224 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
225 ausize[idx] = 0;
226 return -1;
227 }
228 /* Update with the real filesize */
229 ausize[idx] = fsize;
230
231 return checksum; /* return size to be written to flash */
232 }
233
234 int au_do_update(int idx, long sz)
235 {
236 image_header_t *hdr;
237 char *addr;
238 long start, end;
239 int off, rc;
240 uint nbytes;
241
242 hdr = (image_header_t *)LOAD_ADDR;
243
244 /* execute a script */
245 if (hdr->ih_type == IH_TYPE_SCRIPT) {
246 addr = (char *)((char *)hdr + sizeof(*hdr));
247 /* stick a NULL at the end of the script, otherwise */
248 /* parse_string_outer() runs off the end. */
249 addr[ntohl(hdr->ih_size)] = 0;
250 addr += 8;
251 parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
252 return 0;
253 }
254
255 start = aufl_layout[idx].start;
256 end = aufl_layout[idx].end;
257
258 /* unprotect the address range */
259 /* this assumes that ONLY the firmware is protected! */
260 if (idx == IDX_FIRMWARE) {
261 #undef AU_UPDATE_TEST
262 #ifdef AU_UPDATE_TEST
263 /* erase it where Linux goes */
264 start = aufl_layout[1].start;
265 end = aufl_layout[1].end;
266 #endif
267 flash_sect_protect(0, start, end);
268 }
269
270 /*
271 * erase the address range.
272 */
273 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
274 flash_sect_erase(start, end);
275 wait_ms(100);
276 #ifdef CONFIG_PROGRESSBAR
277 show_progress(end - start, totsize);
278 #endif
279
280 /* strip the header - except for the kernel and ramdisk */
281 if (hdr->ih_type == IH_TYPE_KERNEL || hdr->ih_type == IH_TYPE_RAMDISK) {
282 addr = (char *)hdr;
283 off = sizeof(*hdr);
284 nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
285 } else {
286 addr = (char *)((char *)hdr + sizeof(*hdr));
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;
293 nbytes = ntohl(hdr->ih_size);
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
304 #ifdef CONFIG_PROGRESSBAR
305 show_progress(nbytes, totsize);
306 #endif
307
308 /* check the data CRC of the copy */
309 if (crc32 (0, (uchar *)(start + off), ntohl(hdr->ih_size)) != ntohl(hdr->ih_dcrc)) {
310 printf ("Image %s Bad Data Checksum after COPY\n", aufile[idx]);
311 return -1;
312 }
313
314 /* protect the address range */
315 /* this assumes that ONLY the firmware is protected! */
316 if (idx == IDX_FIRMWARE)
317 flash_sect_protect(1, start, end);
318 return 0;
319 }
320
321 /*
322 * this is called from board_init() after the hardware has been set up
323 * and is usable. That seems like a good time to do this.
324 * Right now the return value is ignored.
325 */
326 int do_auto_update(void)
327 {
328 block_dev_desc_t *stor_dev;
329 long sz;
330 int i, res = 0, bitmap_first, cnt, old_ctrlc, got_ctrlc;
331 char *env;
332 long start, end;
333 uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
334
335 /*
336 * Read keypad status
337 */
338 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
339 wait_ms(500);
340 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
341
342 /*
343 * Check keypad
344 */
345 if ( !(keypad_status1[1] & KEYPAD_MASK_LO) ||
346 (keypad_status1[1] != keypad_status2[1])) {
347 return 0;
348 }
349
350 au_usb_stor_curr_dev = -1;
351 /* start USB */
352 if (usb_stop() < 0) {
353 debug ("usb_stop failed\n");
354 return -1;
355 }
356 if (usb_init() < 0) {
357 debug ("usb_init failed\n");
358 return -1;
359 }
360 /*
361 * check whether a storage device is attached (assume that it's
362 * a USB memory stick, since nothing else should be attached).
363 */
364 au_usb_stor_curr_dev = usb_stor_scan(0);
365 if (au_usb_stor_curr_dev == -1) {
366 debug ("No device found. Not initialized?\n");
367 return -1;
368 }
369 /* check whether it has a partition table */
370 stor_dev = get_dev("usb", 0);
371 if (stor_dev == NULL) {
372 debug ("uknown device type\n");
373 return -1;
374 }
375 if (fat_register_device(stor_dev, 1) != 0) {
376 debug ("Unable to use USB %d:%d for fatls\n",
377 au_usb_stor_curr_dev, 1);
378 return -1;
379 }
380 if (file_fat_detectfs() != 0) {
381 debug ("file_fat_detectfs failed\n");
382 }
383
384 /*
385 * now check whether start and end are defined using environment
386 * variables.
387 */
388 start = -1;
389 end = 0;
390 env = getenv("firmware_st");
391 if (env != NULL)
392 start = simple_strtoul(env, NULL, 16);
393 env = getenv("firmware_nd");
394 if (env != NULL)
395 end = simple_strtoul(env, NULL, 16);
396 if (start >= 0 && end && end > start) {
397 ausize[IDX_FIRMWARE] = (end + 1) - start;
398 aufl_layout[IDX_FIRMWARE].start = start;
399 aufl_layout[IDX_FIRMWARE].end = end;
400 }
401 start = -1;
402 end = 0;
403 env = getenv("kernel_st");
404 if (env != NULL)
405 start = simple_strtoul(env, NULL, 16);
406 env = getenv("kernel_nd");
407 if (env != NULL)
408 end = simple_strtoul(env, NULL, 16);
409 if (start >= 0 && end && end > start) {
410 ausize[IDX_KERNEL] = (end + 1) - start;
411 aufl_layout[IDX_KERNEL].start = start;
412 aufl_layout[IDX_KERNEL].end = end;
413 }
414 start = -1;
415 end = 0;
416 env = getenv("rootfs_st");
417 if (env != NULL)
418 start = simple_strtoul(env, NULL, 16);
419 env = getenv("rootfs_nd");
420 if (env != NULL)
421 end = simple_strtoul(env, NULL, 16);
422 if (start >= 0 && end && end > start) {
423 ausize[IDX_ROOTFS] = (end + 1) - start;
424 aufl_layout[IDX_ROOTFS].start = start;
425 aufl_layout[IDX_ROOTFS].end = end;
426 }
427
428 /* make certain that HUSH is runnable */
429 u_boot_hush_start();
430 /* make sure that we see CTRL-C and save the old state */
431 old_ctrlc = disable_ctrlc(0);
432
433 bitmap_first = 0;
434
435 /* validate the images first */
436 for (i = 0; i < AU_MAXFILES; i++) {
437 ulong imsize;
438 /* just read the header */
439 sz = file_fat_read(aufile[i], LOAD_ADDR, sizeof(image_header_t));
440 debug ("read %s sz %ld hdr %d\n",
441 aufile[i], sz, sizeof(image_header_t));
442 if (sz <= 0 || sz < sizeof(image_header_t)) {
443 debug ("%s not found\n", aufile[i]);
444 ausize[i] = 0;
445 continue;
446 }
447 /* au_check_header_valid() updates ausize[] */
448 if ((imsize = au_check_header_valid(i, sz)) < 0) {
449 debug ("%s header not valid\n", aufile[i]);
450 continue;
451 }
452 /* totsize accounts for image size and flash erase size */
453 totsize += (imsize + (aufl_layout[i].end - aufl_layout[i].start));
454 }
455
456 #ifdef CONFIG_PROGRESSBAR
457 if (totsize) {
458 lcd_puts(" Update in progress\n");
459 lcd_enable();
460 }
461 #endif
462
463 /* just loop thru all the possible files */
464 for (i = 0; i < AU_MAXFILES && totsize; i++) {
465 if (!ausize[i]) {
466 continue;
467 }
468 sz = file_fat_read(aufile[i], LOAD_ADDR, ausize[i]);
469
470 debug ("read %s sz %ld hdr %d\n",
471 aufile[i], sz, sizeof(image_header_t));
472
473 if (sz != ausize[i]) {
474 printf ("%s: size %d read %d?\n", aufile[i], ausize[i], sz);
475 continue;
476 }
477
478 if (sz <= 0 || sz <= sizeof(image_header_t)) {
479 debug ("%s not found\n", aufile[i]);
480 continue;
481 }
482 if (au_check_cksum_valid(i, sz) < 0) {
483 debug ("%s checksum not valid\n", aufile[i]);
484 continue;
485 }
486 /* this is really not a good idea, but it's what the */
487 /* customer wants. */
488 cnt = 0;
489 got_ctrlc = 0;
490 do {
491 res = au_do_update(i, sz);
492 /* let the user break out of the loop */
493 if (ctrlc() || had_ctrlc()) {
494 clear_ctrlc();
495 if (res < 0)
496 got_ctrlc = 1;
497 break;
498 }
499 cnt++;
500 #ifdef AU_TEST_ONLY
501 } while (res < 0 && cnt < (AU_MAXFILES + 1));
502 if (cnt < (AU_MAXFILES + 1))
503 #else
504 } while (res < 0);
505 #endif
506 }
507 usb_stop();
508 /* restore the old state */
509 disable_ctrlc(old_ctrlc);
510 #ifdef CONFIG_PROGRESSBAR
511 if (totsize) {
512 if (!res) {
513 lcd_puts("\n Update completed\n");
514 } else {
515 lcd_puts("\n Update error\n");
516 }
517 lcd_enable();
518 }
519 #endif
520 return 0;
521 }
522 #endif /* CONFIG_AUTO_UPDATE */