1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <common.h>
25#include <command.h>
26#include <malloc.h>
27#include <image.h>
28#include <asm/byteorder.h>
29#include <usb.h>
30
31#ifdef CONFIG_SYS_HUSH_PARSER
32#include <hush.h>
33#endif
34
35#ifdef CONFIG_AUTO_UPDATE
36
37#ifndef CONFIG_USB_OHCI_NEW
38#error "must define CONFIG_USB_OHCI"
39#endif
40
41#ifndef CONFIG_USB_STORAGE
42#error "must define CONFIG_USB_STORAGE"
43#endif
44
45#ifndef CONFIG_SYS_HUSH_PARSER
46#error "must define CONFIG_SYS_HUSH_PARSER"
47#endif
48
49#if !defined(CONFIG_CMD_FAT)
50#error "must define CONFIG_CMD_FAT"
51#endif
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72#undef AU_DEBUG
73
74#undef debug
75#ifdef AU_DEBUG
76#define debug(fmt,args...) printf (fmt ,##args)
77#else
78#define debug(fmt,args...)
79#endif
80
81
82#define AU_PREPARE "prepare.img"
83#define AU_PREINST "preinst.img"
84#define AU_FIRMWARE "firmw_01.img"
85#define AU_KERNEL "kernl_01.img"
86#define AU_APP "app.img"
87#define AU_DISK "disk.img"
88#define AU_POSTINST "postinst.img"
89
90struct flash_layout
91{
92 long start;
93 long end;
94};
95
96
97#ifndef CONFIG_FLASH_8MB
98#define AU_FL_FIRMWARE_ST 0x00000000
99#define AU_FL_FIRMWARE_ND 0x0009FFFF
100#define AU_FL_VFD_ST 0x000A0000
101#define AU_FL_VFD_ND 0x000BFFFF
102#define AU_FL_KERNEL_ST 0x000C0000
103#define AU_FL_KERNEL_ND 0x001BFFFF
104#define AU_FL_APP_ST 0x001C0000
105#define AU_FL_APP_ND 0x005BFFFF
106#define AU_FL_DISK_ST 0x005C0000
107#define AU_FL_DISK_ND 0x00FFFFFF
108#else
109#define AU_FL_FIRMWARE_ST 0x00000000
110#define AU_FL_FIRMWARE_ND 0x0005FFFF
111#define AU_FL_KERNEL_ST 0x00060000
112#define AU_FL_KERNEL_ND 0x0013FFFF
113#define AU_FL_APP_ST 0x00140000
114#define AU_FL_APP_ND 0x0067FFFF
115#define AU_FL_DISK_ST 0x00680000
116#define AU_FL_DISK_ND 0x007DFFFF
117#define AU_FL_VFD_ST 0x007E0000
118#define AU_FL_VFD_ND 0x007FFFFF
119#endif
120
121
122struct eeprom_layout
123{
124 int time;
125 int size;
126 int dcrc;
127};
128
129
130#define AU_EEPROM_TIME_PREINST 64
131#define AU_EEPROM_SIZE_PREINST 68
132#define AU_EEPROM_DCRC_PREINST 72
133#define AU_EEPROM_TIME_FIRMWARE 76
134#define AU_EEPROM_SIZE_FIRMWARE 80
135#define AU_EEPROM_DCRC_FIRMWARE 84
136#define AU_EEPROM_TIME_KERNEL 88
137#define AU_EEPROM_SIZE_KERNEL 92
138#define AU_EEPROM_DCRC_KERNEL 96
139#define AU_EEPROM_TIME_APP 100
140#define AU_EEPROM_SIZE_APP 104
141#define AU_EEPROM_DCRC_APP 108
142#define AU_EEPROM_TIME_DISK 112
143#define AU_EEPROM_SIZE_DISK 116
144#define AU_EEPROM_DCRC_DISK 120
145#define AU_EEPROM_TIME_POSTINST 124
146#define AU_EEPROM_SIZE_POSTINST 128
147#define AU_EEPROM_DCRC_POSTINST 132
148
149static int au_usb_stor_curr_dev;
150
151
152#define IDX_PREPARE 0
153#define IDX_PREINST 1
154#define IDX_FIRMWARE 2
155#define IDX_KERNEL 3
156#define IDX_APP 4
157#define IDX_DISK 5
158#define IDX_POSTINST 6
159
160#define AU_MAXFILES 7
161
162char *aufile[AU_MAXFILES];
163
164long ausize[AU_MAXFILES];
165
166struct eeprom_layout auee_off[AU_MAXFILES] = { \
167 {0}, \
168 {AU_EEPROM_TIME_PREINST, AU_EEPROM_SIZE_PREINST, AU_EEPROM_DCRC_PREINST,}, \
169 {AU_EEPROM_TIME_FIRMWARE, AU_EEPROM_SIZE_FIRMWARE, AU_EEPROM_DCRC_FIRMWARE,}, \
170 {AU_EEPROM_TIME_KERNEL, AU_EEPROM_SIZE_KERNEL, AU_EEPROM_DCRC_KERNEL,}, \
171 {AU_EEPROM_TIME_APP, AU_EEPROM_SIZE_APP, AU_EEPROM_DCRC_APP,}, \
172 {AU_EEPROM_TIME_DISK, AU_EEPROM_SIZE_DISK, AU_EEPROM_DCRC_DISK,}, \
173 {AU_EEPROM_TIME_POSTINST, AU_EEPROM_SIZE_POSTINST, AU_EEPROM_DCRC_POSTINST,} \
174 };
175
176struct flash_layout aufl_layout[AU_MAXFILES - 3] = { \
177 {AU_FL_FIRMWARE_ST, AU_FL_FIRMWARE_ND,}, \
178 {AU_FL_KERNEL_ST, AU_FL_KERNEL_ND,}, \
179 {AU_FL_APP_ST, AU_FL_APP_ND,}, \
180 {AU_FL_DISK_ST, AU_FL_DISK_ND,}, \
181};
182
183#define FIDX_TO_LIDX(idx) ((idx) - 2)
184
185
186#define LOAD_ADDR ((unsigned char *)0x0C100000)
187
188#define MAX_LOADSZ ausize[IDX_APP]
189
190
191extern int fat_register_device(block_dev_desc_t *, int);
192extern int file_fat_detectfs(void);
193extern long file_fat_read(const char *, void *, unsigned long);
194extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
195extern int i2c_write (uchar, uint, int , uchar* , int);
196#ifdef CONFIG_VFD
197extern int trab_vfd (ulong);
198extern int transfer_pic(unsigned char, unsigned char *, int, int);
199#endif
200extern int flash_sect_erase(ulong, ulong);
201extern int flash_sect_protect (int, ulong, ulong);
202extern int flash_write (char *, ulong, ulong);
203
204extern int i2c_write_multiple (uchar, uint, int, void *, int);
205extern int i2c_read_multiple (uchar, uint, int, void *, int);
206extern int u_boot_hush_start(void);
207
208int
209au_check_cksum_valid(int idx, long nbytes)
210{
211 image_header_t *hdr;
212
213 hdr = (image_header_t *)LOAD_ADDR;
214#if defined(CONFIG_FIT)
215 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
216 puts ("Non legacy image format not supported\n");
217 return -1;
218 }
219#endif
220
221 if (nbytes != image_get_image_size (hdr)) {
222 printf ("Image %s bad total SIZE\n", aufile[idx]);
223 return -1;
224 }
225
226 if (!image_check_dcrc (hdr)) {
227 printf ("Image %s bad data checksum\n", aufile[idx]);
228 return -1;
229 }
230 return 0;
231}
232
233int
234au_check_header_valid(int idx, long nbytes)
235{
236 image_header_t *hdr;
237 unsigned long checksum;
238 unsigned char buf[4];
239
240 hdr = (image_header_t *)LOAD_ADDR;
241#if defined(CONFIG_FIT)
242 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
243 puts ("Non legacy image format not supported\n");
244 return -1;
245 }
246#endif
247
248
249#undef CHECK_VALID_DEBUG
250#ifdef CHECK_VALID_DEBUG
251 printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC);
252 printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_ARM);
253 printf("size %#x %#lx ", image_get_data_size (hdr), nbytes);
254 printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL);
255#endif
256 if (nbytes < image_get_header_size ()) {
257 printf ("Image %s bad header SIZE\n", aufile[idx]);
258 return -1;
259 }
260 if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_ARM)) {
261 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
262 return -1;
263 }
264
265 if (!image_check_hcrc (hdr)) {
266 printf ("Image %s bad header checksum\n", aufile[idx]);
267 return -1;
268 }
269
270 if ((idx == IDX_FIRMWARE) &&
271 !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
272 printf ("Image %s wrong type\n", aufile[idx]);
273 return -1;
274 }
275 if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) {
276 printf ("Image %s wrong type\n", aufile[idx]);
277 return -1;
278 }
279 if ((idx == IDX_DISK) && !image_check_type (hdr, IH_TYPE_FILESYSTEM)) {
280 printf ("Image %s wrong type\n", aufile[idx]);
281 return -1;
282 }
283 if ((idx == IDX_APP) && !image_check_type (hdr, IH_TYPE_RAMDISK)
284 && !image_check_type (hdr, IH_TYPE_FILESYSTEM)) {
285 printf ("Image %s wrong type\n", aufile[idx]);
286 return -1;
287 }
288 if ((idx == IDX_PREPARE || idx == IDX_PREINST || idx == IDX_POSTINST)
289 && !image_check_type (hdr, IH_TYPE_SCRIPT)) {
290 printf ("Image %s wrong type\n", aufile[idx]);
291 return -1;
292 }
293
294 if (idx == IDX_PREPARE)
295 return 0;
296
297 checksum = image_get_data_size (hdr);
298
299 if ((idx != IDX_DISK) && (idx != IDX_FIRMWARE))
300 checksum += image_get_header_size ();
301
302
303 if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
304 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
305 return -1;
306 }
307
308
309 i2c_read_multiple(0x54, auee_off[idx].time, 1, buf, sizeof(buf));
310#ifdef CHECK_VALID_DEBUG
311 printf ("buf[0] %#x buf[1] %#x buf[2] %#x buf[3] %#x "
312 "as int %#x time %#x\n",
313 buf[0], buf[1], buf[2], buf[3],
314 *((unsigned int *)buf), image_get_time (hdr));
315#endif
316
317 if (*((unsigned int *)buf) >= image_get_time (hdr)) {
318 printf ("Image %s is too old\n", aufile[idx]);
319 return -1;
320 }
321
322 return 0;
323}
324
325
326#define CPLD_VFD_BK ((volatile char *)0x04038002)
327#define POWER_OFF (1 << 1)
328
329int
330au_do_update(int idx, long sz)
331{
332 image_header_t *hdr;
333 char *addr;
334 long start, end;
335 int off, rc;
336 uint nbytes;
337
338 hdr = (image_header_t *)LOAD_ADDR;
339#if defined(CONFIG_FIT)
340 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
341 puts ("Non legacy image format not supported\n");
342 return -1;
343 }
344#endif
345
346
347 *CPLD_VFD_BK |= POWER_OFF;
348
349
350 if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
351 addr = (char *)((char *)hdr + image_get_header_size ());
352
353
354 addr[image_get_data_size (hdr)] = 0;
355 addr += 8;
356 parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
357 return 0;
358 }
359
360 start = aufl_layout[FIDX_TO_LIDX(idx)].start;
361 end = aufl_layout[FIDX_TO_LIDX(idx)].end;
362
363
364
365 if (idx == IDX_FIRMWARE) {
366#undef AU_UPDATE_TEST
367#ifdef AU_UPDATE_TEST
368
369 start = aufl_layout[1].start;
370 end = aufl_layout[1].end;
371#endif
372 flash_sect_protect(0, start, end);
373 }
374
375
376
377
378 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
379 flash_sect_erase(start, end);
380 wait_ms(100);
381
382 if (image_check_type (hdr, IH_TYPE_KERNEL) ||
383 image_check_type (hdr, IH_TYPE_RAMDISK)) {
384 addr = (char *)hdr;
385 off = image_get_header_size ();
386 nbytes = image_get_image_size (hdr);
387 } else {
388 addr = (char *)((char *)hdr + image_get_header_size ());
389#ifdef AU_UPDATE_TEST
390
391 if (idx == IDX_FIRMWARE)
392 start = aufl_layout[1].start;
393#endif
394 off = 0;
395 nbytes = image_get_data_size (hdr);
396 }
397
398
399 debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
400 rc = flash_write(addr, start, nbytes);
401 if (rc != 0) {
402 printf("Flashing failed due to error %d\n", rc);
403 return -1;
404 }
405
406
407 if (crc32 (0, (uchar *)(start + off), image_get_data_size (hdr)) !=
408 image_get_dcrc (hdr)) {
409 printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
410 return -1;
411 }
412
413
414
415 if (idx == IDX_FIRMWARE)
416 flash_sect_protect(1, start, end);
417 return 0;
418}
419
420int
421au_update_eeprom(int idx)
422{
423 image_header_t *hdr;
424 int off;
425 uint32_t val;
426
427
428 if (idx == IDX_PREPARE) {
429
430 *CPLD_VFD_BK &= ~POWER_OFF;
431 return 0;
432 }
433
434 hdr = (image_header_t *)LOAD_ADDR;
435#if defined(CONFIG_FIT)
436 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
437 puts ("Non legacy image format not supported\n");
438 return -1;
439 }
440#endif
441
442
443 off = auee_off[idx].time;
444 val = image_get_time (hdr);
445 i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
446
447 off = auee_off[idx].size;
448 val = image_get_data_size (hdr);
449 i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
450
451 off = auee_off[idx].dcrc;
452 val = image_get_dcrc (hdr);
453 i2c_write_multiple(0x54, off, 1, &val, sizeof(val));
454
455 *CPLD_VFD_BK &= ~POWER_OFF;
456 return 0;
457}
458
459
460
461
462
463
464int
465do_auto_update(void)
466{
467 block_dev_desc_t *stor_dev;
468 long sz;
469 int i, res = 0, bitmap_first, cnt, old_ctrlc, got_ctrlc;
470 char *env;
471 long start, end;
472
473#undef ERASE_EEPROM
474#ifdef ERASE_EEPROM
475 int arr[18];
476 memset(arr, 0, sizeof(arr));
477 i2c_write_multiple(0x54, 64, 1, arr, sizeof(arr));
478#endif
479 au_usb_stor_curr_dev = -1;
480
481 if (usb_stop() < 0) {
482 debug ("usb_stop failed\n");
483 return -1;
484 }
485 if (usb_init() < 0) {
486 debug ("usb_init failed\n");
487 return -1;
488 }
489
490
491
492
493 au_usb_stor_curr_dev = usb_stor_scan(0);
494 if (au_usb_stor_curr_dev == -1) {
495 debug ("No device found. Not initialized?\n");
496 res = -1;
497 goto xit;
498 }
499
500 stor_dev = get_dev("usb", 0);
501 if (stor_dev == NULL) {
502 debug ("uknown device type\n");
503 res = -1;
504 goto xit;
505 }
506 if (fat_register_device(stor_dev, 1) != 0) {
507 debug ("Unable to use USB %d:%d for fatls\n",
508 au_usb_stor_curr_dev, 1);
509 res = -1;
510 goto xit;
511 }
512 if (file_fat_detectfs() != 0) {
513 debug ("file_fat_detectfs failed\n");
514 }
515
516
517 memset(aufile, 0, sizeof(aufile));
518 aufile[IDX_PREPARE] = AU_PREPARE;
519 aufile[IDX_PREINST] = AU_PREINST;
520 aufile[IDX_FIRMWARE] = AU_FIRMWARE;
521 aufile[IDX_KERNEL] = AU_KERNEL;
522 aufile[IDX_APP] = AU_APP;
523 aufile[IDX_DISK] = AU_DISK;
524 aufile[IDX_POSTINST] = AU_POSTINST;
525
526 memset(ausize, 0, sizeof(ausize));
527 ausize[IDX_FIRMWARE] = (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST;
528 ausize[IDX_KERNEL] = (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST;
529 ausize[IDX_APP] = (AU_FL_APP_ND + 1) - AU_FL_APP_ST;
530 ausize[IDX_DISK] = (AU_FL_DISK_ND + 1) - AU_FL_DISK_ST;
531
532
533
534
535 start = -1;
536 end = 0;
537 env = getenv("firmware_st");
538 if (env != NULL)
539 start = simple_strtoul(env, NULL, 16);
540 env = getenv("firmware_nd");
541 if (env != NULL)
542 end = simple_strtoul(env, NULL, 16);
543 if (start >= 0 && end && end > start) {
544 ausize[IDX_FIRMWARE] = (end + 1) - start;
545 aufl_layout[0].start = start;
546 aufl_layout[0].end = end;
547 }
548 start = -1;
549 end = 0;
550 env = getenv("kernel_st");
551 if (env != NULL)
552 start = simple_strtoul(env, NULL, 16);
553 env = getenv("kernel_nd");
554 if (env != NULL)
555 end = simple_strtoul(env, NULL, 16);
556 if (start >= 0 && end && end > start) {
557 ausize[IDX_KERNEL] = (end + 1) - start;
558 aufl_layout[1].start = start;
559 aufl_layout[1].end = end;
560 }
561 start = -1;
562 end = 0;
563 env = getenv("app_st");
564 if (env != NULL)
565 start = simple_strtoul(env, NULL, 16);
566 env = getenv("app_nd");
567 if (env != NULL)
568 end = simple_strtoul(env, NULL, 16);
569 if (start >= 0 && end && end > start) {
570 ausize[IDX_APP] = (end + 1) - start;
571 aufl_layout[2].start = start;
572 aufl_layout[2].end = end;
573 }
574 start = -1;
575 end = 0;
576 env = getenv("disk_st");
577 if (env != NULL)
578 start = simple_strtoul(env, NULL, 16);
579 env = getenv("disk_nd");
580 if (env != NULL)
581 end = simple_strtoul(env, NULL, 16);
582 if (start >= 0 && end && end > start) {
583 ausize[IDX_DISK] = (end + 1) - start;
584 aufl_layout[3].start = start;
585 aufl_layout[3].end = end;
586 }
587
588 u_boot_hush_start();
589
590 old_ctrlc = disable_ctrlc(0);
591
592 bitmap_first = 0;
593
594 for (i = 0; i < AU_MAXFILES; i++) {
595
596 sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ());
597 debug ("read %s sz %ld hdr %d\n",
598 aufile[i], sz, image_get_header_size ());
599 if (sz <= 0 || sz < image_get_header_size ()) {
600 debug ("%s not found\n", aufile[i]);
601 continue;
602 }
603 if (au_check_header_valid(i, sz) < 0) {
604 debug ("%s header not valid\n", aufile[i]);
605 continue;
606 }
607 sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
608 debug ("read %s sz %ld hdr %d\n",
609 aufile[i], sz, image_get_header_size ());
610 if (sz <= 0 || sz <= image_get_header_size ()) {
611 debug ("%s not found\n", aufile[i]);
612 continue;
613 }
614 if (au_check_cksum_valid(i, sz) < 0) {
615 debug ("%s checksum not valid\n", aufile[i]);
616 continue;
617 }
618#ifdef CONFIG_VFD
619
620
621 if (bitmap_first == 0) {
622 env = getenv("bitmap2");
623 if (env == NULL) {
624 trab_vfd(0);
625 } else {
626
627
628 env = (char *)simple_strtoul(env, NULL, 16);
629
630
631#define VFD_LOGO_WIDTH 112
632#define VFD_LOGO_HEIGHT 72
633
634 transfer_pic(3, (unsigned char *)env,
635 VFD_LOGO_HEIGHT, VFD_LOGO_WIDTH);
636 }
637 bitmap_first = 1;
638 }
639#endif
640
641
642 cnt = 0;
643 got_ctrlc = 0;
644 do {
645 res = au_do_update(i, sz);
646
647 if (ctrlc() || had_ctrlc()) {
648 clear_ctrlc();
649 if (res < 0)
650 got_ctrlc = 1;
651 break;
652 }
653 cnt++;
654#ifdef AU_TEST_ONLY
655 } while (res < 0 && cnt < 3);
656 if (cnt < 3)
657#else
658 } while (res < 0);
659#endif
660
661
662
663
664 if (got_ctrlc == 0)
665 au_update_eeprom(i);
666 else
667
668 *CPLD_VFD_BK &= ~POWER_OFF;
669 }
670
671 disable_ctrlc(old_ctrlc);
672xit:
673 usb_stop();
674 return res;
675}
676#endif
677