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