]>
git.ipfire.org Git - people/ms/u-boot.git/blob - board/mcc200/auto_update.c
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <asm/byteorder.h>
15 #ifdef CONFIG_AUTO_UPDATE
17 #ifndef CONFIG_USB_OHCI
18 #error "must define CONFIG_USB_OHCI"
21 #ifndef CONFIG_USB_STORAGE
22 #error "must define CONFIG_USB_STORAGE"
25 #ifndef CONFIG_SYS_HUSH_PARSER
26 #error "must define CONFIG_SYS_HUSH_PARSER"
29 #if !defined(CONFIG_CMD_FAT)
30 #error "must define CONFIG_CMD_FAT"
37 #define debug(fmt,args...) printf (fmt ,##args)
39 #define debug(fmt,args...)
42 /* possible names of files on the USB stick. */
43 #define AU_FIRMWARE "u-boot.img"
44 #define AU_KERNEL "kernel.img"
45 #define AU_ROOTFS "rootfs.img"
52 /* layout of the FLASH. ST = start address, ND = end address. */
53 #define AU_FL_FIRMWARE_ST 0xfC000000
54 #define AU_FL_FIRMWARE_ND 0xfC03FFFF
55 #define AU_FL_KERNEL_ST 0xfC0C0000
56 #define AU_FL_KERNEL_ND 0xfC1BFFFF
57 #define AU_FL_ROOTFS_ST 0xFC1C0000
58 #define AU_FL_ROOTFS_ND 0xFCFBFFFF
60 static int au_usb_stor_curr_dev
; /* current device */
62 /* index of each file in the following arrays */
63 #define IDX_FIRMWARE 0
67 /* max. number of files which could interest us */
70 /* pointers to file names */
71 char *aufile
[AU_MAXFILES
] = {
77 /* sizes of flash areas for each file */
78 long ausize
[AU_MAXFILES
] = {
79 (AU_FL_FIRMWARE_ND
+ 1) - AU_FL_FIRMWARE_ST
,
80 (AU_FL_KERNEL_ND
+ 1) - AU_FL_KERNEL_ST
,
81 (AU_FL_ROOTFS_ND
+ 1) - AU_FL_ROOTFS_ST
,
84 /* array of flash areas start and end addresses */
85 struct flash_layout aufl_layout
[AU_MAXFILES
] = {
86 { AU_FL_FIRMWARE_ST
, AU_FL_FIRMWARE_ND
, },
87 { AU_FL_KERNEL_ST
, AU_FL_KERNEL_ND
, },
88 { AU_FL_ROOTFS_ST
, AU_FL_ROOTFS_ND
, },
93 /* where to load files into memory */
94 #define LOAD_ADDR ((unsigned char *)0x00200000)
96 /* the root file system is the largest image */
97 #define MAX_LOADSZ ausize[IDX_ROOTFS]
99 /*i2c address of the keypad status*/
100 #define I2C_PSOC_KEYPAD_ADDR 0x53
105 #define KEYPAD_MASK_LO ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))&0xFF)
106 #define KEYPAD_MASK_HI ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))>>8)
109 extern int fat_register_device(block_dev_desc_t
*, int);
110 extern int file_fat_detectfs(void);
111 extern long file_fat_read(const char *, void *, unsigned long);
112 extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
113 extern int flash_sect_erase(ulong
, ulong
);
114 extern int flash_sect_protect (int, ulong
, ulong
);
115 extern int flash_write (char *, ulong
, ulong
);
116 extern int u_boot_hush_start(void);
117 #ifdef CONFIG_PROGRESSBAR
118 extern void show_progress(int, int);
119 extern void lcd_puts (char *);
120 extern void lcd_enable(void);
123 int au_check_cksum_valid(int idx
, long nbytes
)
127 hdr
= (image_header_t
*)LOAD_ADDR
;
128 #if defined(CONFIG_FIT)
129 if (genimg_get_format ((void *)hdr
) != IMAGE_FORMAT_LEGACY
) {
130 puts ("Non legacy image format not supported\n");
135 if (nbytes
!= image_get_image_size (hdr
)) {
136 printf ("Image %s bad total SIZE\n", aufile
[idx
]);
139 /* check the data CRC */
140 if (!image_check_dcrc (hdr
)) {
141 printf ("Image %s bad data checksum\n", aufile
[idx
]);
147 int au_check_header_valid(int idx
, long nbytes
)
150 unsigned long checksum
, fsize
;
152 hdr
= (image_header_t
*)LOAD_ADDR
;
153 #if defined(CONFIG_FIT)
154 if (genimg_get_format ((void *)hdr
) != IMAGE_FORMAT_LEGACY
) {
155 puts ("Non legacy image format not supported\n");
160 /* check the easy ones first */
161 #undef CHECK_VALID_DEBUG
162 #ifdef CHECK_VALID_DEBUG
163 printf("magic %#x %#x ", image_get_magic (hdr
), IH_MAGIC
);
164 printf("arch %#x %#x ", image_get_arch (hdr
), IH_ARCH_ARM
);
165 printf("size %#x %#lx ", image_get_data_size (hdr
), nbytes
);
166 printf("type %#x %#x ", image_get_type (hdr
), IH_TYPE_KERNEL
);
168 if (nbytes
< image_get_header_size ()) {
169 printf ("Image %s bad header SIZE\n", aufile
[idx
]);
173 if (!image_check_magic (hdr
) || !image_check_arch (hdr
, IH_ARCH_PPC
)) {
174 printf ("Image %s bad MAGIC or ARCH\n", aufile
[idx
]);
178 /* check the hdr CRC */
179 if (!image_check_hcrc (hdr
)) {
180 printf ("Image %s bad header checksum\n", aufile
[idx
]);
184 /* check the type - could do this all in one gigantic if() */
185 if ((idx
== IDX_FIRMWARE
) && !image_check_type (hdr
, IH_TYPE_FIRMWARE
)) {
186 printf ("Image %s wrong type\n", aufile
[idx
]);
190 if ((idx
== IDX_KERNEL
) && !image_check_type (hdr
, IH_TYPE_KERNEL
)) {
191 printf ("Image %s wrong type\n", aufile
[idx
]);
195 if ((idx
== IDX_ROOTFS
) &&
196 (!image_check_type (hdr
, IH_TYPE_RAMDISK
) &&
197 !image_check_type (hdr
, IH_TYPE_FILESYSTEM
))) {
198 printf ("Image %s wrong type\n", aufile
[idx
]);
202 /* recycle checksum */
203 checksum
= image_get_data_size (hdr
);
205 fsize
= checksum
+ image_get_header_size ();
206 /* for kernel and ramdisk the image header must also fit into flash */
207 if (idx
== IDX_KERNEL
|| image_check_type (hdr
, IH_TYPE_RAMDISK
))
208 checksum
+= image_get_header_size ();
210 /* check the size does not exceed space in flash. HUSH scripts */
211 if ((ausize
[idx
] != 0) && (ausize
[idx
] < checksum
)) {
212 printf ("Image %s is bigger than FLASH\n", aufile
[idx
]);
216 /* Update with the real filesize */
219 return checksum
; /* return size to be written to flash */
222 int au_do_update(int idx
, long sz
)
230 hdr
= (image_header_t
*)LOAD_ADDR
;
231 #if defined(CONFIG_FIT)
232 if (genimg_get_format ((void *)hdr
) != IMAGE_FORMAT_LEGACY
) {
233 puts ("Non legacy image format not supported\n");
238 /* execute a script */
239 if (image_check_type (hdr
, IH_TYPE_SCRIPT
)) {
240 addr
= (char *)((char *)hdr
+ image_get_header_size ());
241 /* stick a NULL at the end of the script, otherwise */
242 /* parse_string_outer() runs off the end. */
243 addr
[image_get_data_size (hdr
)] = 0;
245 run_command_list(addr
, -1, 0);
249 start
= aufl_layout
[idx
].start
;
250 end
= aufl_layout
[idx
].end
;
252 /* unprotect the address range */
253 /* this assumes that ONLY the firmware is protected! */
254 if (idx
== IDX_FIRMWARE
) {
255 #undef AU_UPDATE_TEST
256 #ifdef AU_UPDATE_TEST
257 /* erase it where Linux goes */
258 start
= aufl_layout
[1].start
;
259 end
= aufl_layout
[1].end
;
261 flash_sect_protect(0, start
, end
);
265 * erase the address range.
267 debug ("flash_sect_erase(%lx, %lx);\n", start
, end
);
268 flash_sect_erase(start
, end
);
270 #ifdef CONFIG_PROGRESSBAR
271 show_progress(end
- start
, totsize
);
274 /* strip the header - except for the kernel and ramdisk */
275 if (image_check_type (hdr
, IH_TYPE_KERNEL
) ||
276 image_check_type (hdr
, IH_TYPE_RAMDISK
)) {
278 off
= image_get_header_size ();
279 nbytes
= image_get_image_size (hdr
);
281 addr
= (char *)((char *)hdr
+ image_get_header_size ());
282 #ifdef AU_UPDATE_TEST
283 /* copy it to where Linux goes */
284 if (idx
== IDX_FIRMWARE
)
285 start
= aufl_layout
[1].start
;
288 nbytes
= image_get_data_size (hdr
);
291 /* copy the data from RAM to FLASH */
292 debug ("flash_write(%p, %lx %x)\n", addr
, start
, nbytes
);
293 rc
= flash_write(addr
, start
, nbytes
);
295 printf("Flashing failed due to error %d\n", rc
);
299 #ifdef CONFIG_PROGRESSBAR
300 show_progress(nbytes
, totsize
);
303 /* check the data CRC of the copy */
304 if (crc32 (0, (uchar
*)(start
+ off
), image_get_data_size (hdr
)) !=
305 image_get_dcrc (hdr
)) {
306 printf ("Image %s Bad Data Checksum after COPY\n", aufile
[idx
]);
310 /* protect the address range */
311 /* this assumes that ONLY the firmware is protected! */
312 if (idx
== IDX_FIRMWARE
)
313 flash_sect_protect(1, start
, end
);
318 * this is called from board_init() after the hardware has been set up
319 * and is usable. That seems like a good time to do this.
320 * Right now the return value is ignored.
322 int do_auto_update(void)
324 block_dev_desc_t
*stor_dev
;
326 int i
, res
= 0, cnt
, old_ctrlc
;
330 #if 0 /* disable key-press detection to speed up boot-up time */
331 uchar keypad_status1
[2] = {0,0}, keypad_status2
[2] = {0,0};
336 i2c_read(I2C_PSOC_KEYPAD_ADDR
, 0, 0, keypad_status1
, 2);
338 i2c_read(I2C_PSOC_KEYPAD_ADDR
, 0, 0, keypad_status2
, 2);
343 if ( !(keypad_status1
[1] & KEYPAD_MASK_LO
) ||
344 (keypad_status1
[1] != keypad_status2
[1])) {
349 au_usb_stor_curr_dev
= -1;
351 if (usb_stop() < 0) {
352 debug ("usb_stop failed\n");
355 if (usb_init() < 0) {
356 debug ("usb_init failed\n");
360 * check whether a storage device is attached (assume that it's
361 * a USB memory stick, since nothing else should be attached).
363 au_usb_stor_curr_dev
= usb_stor_scan(0);
364 if (au_usb_stor_curr_dev
== -1) {
365 debug ("No device found. Not initialized?\n");
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");
376 if (fat_register_device(stor_dev
, 1) != 0) {
377 debug ("Unable to use USB %d:%d for fatls\n",
378 au_usb_stor_curr_dev
, 1);
382 if (file_fat_detectfs() != 0) {
383 debug ("file_fat_detectfs failed\n");
387 * now check whether start and end are defined using environment
392 env
= getenv("firmware_st");
394 start
= simple_strtoul(env
, NULL
, 16);
395 env
= getenv("firmware_nd");
397 end
= simple_strtoul(env
, NULL
, 16);
398 if (start
>= 0 && end
&& end
> start
) {
399 ausize
[IDX_FIRMWARE
] = (end
+ 1) - start
;
400 aufl_layout
[IDX_FIRMWARE
].start
= start
;
401 aufl_layout
[IDX_FIRMWARE
].end
= end
;
405 env
= getenv("kernel_st");
407 start
= simple_strtoul(env
, NULL
, 16);
408 env
= getenv("kernel_nd");
410 end
= simple_strtoul(env
, NULL
, 16);
411 if (start
>= 0 && end
&& end
> start
) {
412 ausize
[IDX_KERNEL
] = (end
+ 1) - start
;
413 aufl_layout
[IDX_KERNEL
].start
= start
;
414 aufl_layout
[IDX_KERNEL
].end
= end
;
418 env
= getenv("rootfs_st");
420 start
= simple_strtoul(env
, NULL
, 16);
421 env
= getenv("rootfs_nd");
423 end
= simple_strtoul(env
, NULL
, 16);
424 if (start
>= 0 && end
&& end
> start
) {
425 ausize
[IDX_ROOTFS
] = (end
+ 1) - start
;
426 aufl_layout
[IDX_ROOTFS
].start
= start
;
427 aufl_layout
[IDX_ROOTFS
].end
= end
;
430 /* make certain that HUSH is runnable */
432 /* make sure that we see CTRL-C and save the old state */
433 old_ctrlc
= disable_ctrlc(0);
435 /* validate the images first */
436 for (i
= 0; i
< AU_MAXFILES
; i
++) {
438 /* just read the header */
439 sz
= file_fat_read(aufile
[i
], LOAD_ADDR
, image_get_header_size ());
440 debug ("read %s sz %ld hdr %d\n",
441 aufile
[i
], sz
, image_get_header_size ());
442 if (sz
<= 0 || sz
< image_get_header_size ()) {
443 debug ("%s not found\n", aufile
[i
]);
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
]);
452 /* totsize accounts for image size and flash erase size */
453 totsize
+= (imsize
+ (aufl_layout
[i
].end
- aufl_layout
[i
].start
));
456 #ifdef CONFIG_PROGRESSBAR
458 lcd_puts(" Update in progress\n");
463 /* just loop thru all the possible files */
464 for (i
= 0; i
< AU_MAXFILES
&& totsize
; i
++) {
468 sz
= file_fat_read(aufile
[i
], LOAD_ADDR
, ausize
[i
]);
470 debug ("read %s sz %ld hdr %d\n",
471 aufile
[i
], sz
, image_get_header_size ());
473 if (sz
!= ausize
[i
]) {
474 printf ("%s: size %ld read %ld?\n", aufile
[i
], ausize
[i
], sz
);
478 if (sz
<= 0 || sz
<= image_get_header_size ()) {
479 debug ("%s not found\n", aufile
[i
]);
482 if (au_check_cksum_valid(i
, sz
) < 0) {
483 debug ("%s checksum not valid\n", aufile
[i
]);
486 /* this is really not a good idea, but it's what the */
487 /* customer wants. */
490 res
= au_do_update(i
, sz
);
491 /* let the user break out of the loop */
492 if (ctrlc() || had_ctrlc()) {
498 } while (res
< 0 && cnt
< (AU_MAXFILES
+ 1));
499 if (cnt
< (AU_MAXFILES
+ 1))
505 /* restore the old state */
506 disable_ctrlc(old_ctrlc
);
507 #ifdef CONFIG_PROGRESSBAR
510 lcd_puts("\n Update completed\n");
512 lcd_puts("\n Update error\n");
521 #endif /* CONFIG_AUTO_UPDATE */