]> git.ipfire.org Git - people/ms/u-boot.git/blob - board/mcc200/auto_update.c
kconfig: zynq: Add ZYBO board
[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 * SPDX-License-Identifier: GPL-2.0+
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>
13 #include <part.h>
14
15 #ifdef CONFIG_AUTO_UPDATE
16
17 #ifndef CONFIG_USB_OHCI
18 #error "must define CONFIG_USB_OHCI"
19 #endif
20
21 #ifndef CONFIG_USB_STORAGE
22 #error "must define CONFIG_USB_STORAGE"
23 #endif
24
25 #ifndef CONFIG_SYS_HUSH_PARSER
26 #error "must define CONFIG_SYS_HUSH_PARSER"
27 #endif
28
29 #if !defined(CONFIG_CMD_FAT)
30 #error "must define CONFIG_CMD_FAT"
31 #endif
32
33 #undef AU_DEBUG
34
35 #undef debug
36 #ifdef AU_DEBUG
37 #define debug(fmt,args...) printf (fmt ,##args)
38 #else
39 #define debug(fmt,args...)
40 #endif /* AU_DEBUG */
41
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"
46
47 struct flash_layout {
48 long start;
49 long end;
50 };
51
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
59
60 static int au_usb_stor_curr_dev; /* current device */
61
62 /* index of each file in the following arrays */
63 #define IDX_FIRMWARE 0
64 #define IDX_KERNEL 1
65 #define IDX_ROOTFS 2
66
67 /* max. number of files which could interest us */
68 #define AU_MAXFILES 3
69
70 /* pointers to file names */
71 char *aufile[AU_MAXFILES] = {
72 AU_FIRMWARE,
73 AU_KERNEL,
74 AU_ROOTFS
75 };
76
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,
82 };
83
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, },
89 };
90
91 ulong totsize;
92
93 /* where to load files into memory */
94 #define LOAD_ADDR ((unsigned char *)0x00200000)
95
96 /* the root file system is the largest image */
97 #define MAX_LOADSZ ausize[IDX_ROOTFS]
98
99 /*i2c address of the keypad status*/
100 #define I2C_PSOC_KEYPAD_ADDR 0x53
101
102 /* keypad mask */
103 #define KEYPAD_ROW 2
104 #define KEYPAD_COL 2
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)
107
108 /* externals */
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);
121 #endif
122
123 int au_check_cksum_valid(int idx, long nbytes)
124 {
125 image_header_t *hdr;
126
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");
131 return -1;
132 }
133 #endif
134
135 if (nbytes != image_get_image_size (hdr)) {
136 printf ("Image %s bad total SIZE\n", aufile[idx]);
137 return -1;
138 }
139 /* check the data CRC */
140 if (!image_check_dcrc (hdr)) {
141 printf ("Image %s bad data checksum\n", aufile[idx]);
142 return -1;
143 }
144 return 0;
145 }
146
147 int au_check_header_valid(int idx, long nbytes)
148 {
149 image_header_t *hdr;
150 unsigned long checksum, fsize;
151
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");
156 return -1;
157 }
158 #endif
159
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);
167 #endif
168 if (nbytes < image_get_header_size ()) {
169 printf ("Image %s bad header SIZE\n", aufile[idx]);
170 ausize[idx] = 0;
171 return -1;
172 }
173 if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) {
174 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
175 ausize[idx] = 0;
176 return -1;
177 }
178 /* check the hdr CRC */
179 if (!image_check_hcrc (hdr)) {
180 printf ("Image %s bad header checksum\n", aufile[idx]);
181 ausize[idx] = 0;
182 return -1;
183 }
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]);
187 ausize[idx] = 0;
188 return -1;
189 }
190 if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) {
191 printf ("Image %s wrong type\n", aufile[idx]);
192 ausize[idx] = 0;
193 return -1;
194 }
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]);
199 ausize[idx] = 0;
200 return -1;
201 }
202 /* recycle checksum */
203 checksum = image_get_data_size (hdr);
204
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 ();
209
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]);
213 ausize[idx] = 0;
214 return -1;
215 }
216 /* Update with the real filesize */
217 ausize[idx] = fsize;
218
219 return checksum; /* return size to be written to flash */
220 }
221
222 int au_do_update(int idx, long sz)
223 {
224 image_header_t *hdr;
225 char *addr;
226 long start, end;
227 int off, rc;
228 uint nbytes;
229
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");
234 return -1;
235 }
236 #endif
237
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;
244 addr += 8;
245 run_command_list(addr, -1, 0);
246 return 0;
247 }
248
249 start = aufl_layout[idx].start;
250 end = aufl_layout[idx].end;
251
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;
260 #endif
261 flash_sect_protect(0, start, end);
262 }
263
264 /*
265 * erase the address range.
266 */
267 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
268 flash_sect_erase(start, end);
269 mdelay(100);
270 #ifdef CONFIG_PROGRESSBAR
271 show_progress(end - start, totsize);
272 #endif
273
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)) {
277 addr = (char *)hdr;
278 off = image_get_header_size ();
279 nbytes = image_get_image_size (hdr);
280 } else {
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;
286 #endif
287 off = 0;
288 nbytes = image_get_data_size (hdr);
289 }
290
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);
294 if (rc != 0) {
295 printf("Flashing failed due to error %d\n", rc);
296 return -1;
297 }
298
299 #ifdef CONFIG_PROGRESSBAR
300 show_progress(nbytes, totsize);
301 #endif
302
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]);
307 return -1;
308 }
309
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);
314 return 0;
315 }
316
317 /*
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.
321 */
322 int do_auto_update(void)
323 {
324 block_dev_desc_t *stor_dev;
325 long sz;
326 int i, res = 0, cnt, old_ctrlc;
327 char *env;
328 long start, end;
329
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};
332
333 /*
334 * Read keypad status
335 */
336 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
337 mdelay(500);
338 i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
339
340 /*
341 * Check keypad
342 */
343 if ( !(keypad_status1[1] & KEYPAD_MASK_LO) ||
344 (keypad_status1[1] != keypad_status2[1])) {
345 return 0;
346 }
347
348 #endif
349 au_usb_stor_curr_dev = -1;
350 /* start USB */
351 if (usb_stop() < 0) {
352 debug ("usb_stop failed\n");
353 return -1;
354 }
355 if (usb_init() < 0) {
356 debug ("usb_init failed\n");
357 return -1;
358 }
359 /*
360 * check whether a storage device is attached (assume that it's
361 * a USB memory stick, since nothing else should be attached).
362 */
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");
366 res = -1;
367 goto xit;
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 res = -1;
374 goto xit;
375 }
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);
379 res = -1;
380 goto xit;
381 }
382 if (file_fat_detectfs() != 0) {
383 debug ("file_fat_detectfs failed\n");
384 }
385
386 /*
387 * now check whether start and end are defined using environment
388 * variables.
389 */
390 start = -1;
391 end = 0;
392 env = getenv("firmware_st");
393 if (env != NULL)
394 start = simple_strtoul(env, NULL, 16);
395 env = getenv("firmware_nd");
396 if (env != NULL)
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;
402 }
403 start = -1;
404 end = 0;
405 env = getenv("kernel_st");
406 if (env != NULL)
407 start = simple_strtoul(env, NULL, 16);
408 env = getenv("kernel_nd");
409 if (env != NULL)
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;
415 }
416 start = -1;
417 end = 0;
418 env = getenv("rootfs_st");
419 if (env != NULL)
420 start = simple_strtoul(env, NULL, 16);
421 env = getenv("rootfs_nd");
422 if (env != NULL)
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;
428 }
429
430 /* make certain that HUSH is runnable */
431 u_boot_hush_start();
432 /* make sure that we see CTRL-C and save the old state */
433 old_ctrlc = disable_ctrlc(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, 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]);
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, image_get_header_size ());
472
473 if (sz != ausize[i]) {
474 printf ("%s: size %ld read %ld?\n", aufile[i], ausize[i], sz);
475 continue;
476 }
477
478 if (sz <= 0 || sz <= image_get_header_size ()) {
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 do {
490 res = au_do_update(i, sz);
491 /* let the user break out of the loop */
492 if (ctrlc() || had_ctrlc()) {
493 clear_ctrlc();
494 break;
495 }
496 cnt++;
497 #ifdef AU_TEST_ONLY
498 } while (res < 0 && cnt < (AU_MAXFILES + 1));
499 if (cnt < (AU_MAXFILES + 1))
500 #else
501 } while (res < 0);
502 #endif
503 }
504
505 /* restore the old state */
506 disable_ctrlc(old_ctrlc);
507 #ifdef CONFIG_PROGRESSBAR
508 if (totsize) {
509 if (!res) {
510 lcd_puts("\n Update completed\n");
511 } else {
512 lcd_puts("\n Update error\n");
513 }
514 lcd_enable();
515 }
516 #endif
517 xit:
518 usb_stop();
519 return res;
520 }
521 #endif /* CONFIG_AUTO_UPDATE */