]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/mcc200/auto_update.c
2f622b0846e103f5b3036322269e5577ca451427
[karo-tx-uboot.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_SYS_HUSH_PARSER
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
30 #ifndef CONFIG_SYS_HUSH_PARSER
31 #error "must define CONFIG_SYS_HUSH_PARSER"
32 #endif
33
34 #if !defined(CONFIG_CMD_FAT)
35 #error "must define CONFIG_CMD_FAT"
36 #endif
37
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"
50 #define AU_ROOTFS       "rootfs.img"
51
52 struct flash_layout {
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
62 #define AU_FL_ROOTFS_ST         0xFC1C0000
63 #define AU_FL_ROOTFS_ND         0xFCFBFFFF
64
65 static 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
70 #define IDX_ROOTFS      2
71
72 /* max. number of files which could interest us */
73 #define AU_MAXFILES 3
74
75 /* pointers to file names */
76 char *aufile[AU_MAXFILES] = {
77         AU_FIRMWARE,
78         AU_KERNEL,
79         AU_ROOTFS
80 };
81
82 /* sizes of flash areas for each file */
83 long ausize[AU_MAXFILES] = {
84         (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST,
85         (AU_FL_KERNEL_ND   + 1) - AU_FL_KERNEL_ST,
86         (AU_FL_ROOTFS_ND   + 1) - AU_FL_ROOTFS_ST,
87 };
88
89 /* array of flash areas start and end addresses */
90 struct flash_layout aufl_layout[AU_MAXFILES] = {
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,   },
94 };
95
96 ulong totsize;
97
98 /* where to load files into memory */
99 #define LOAD_ADDR ((unsigned char *)0x00200000)
100
101 /* the root file system is the largest image */
102 #define MAX_LOADSZ ausize[IDX_ROOTFS]
103
104 /*i2c address of the keypad status*/
105 #define I2C_PSOC_KEYPAD_ADDR    0x53
106
107 /* keypad mask */
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)
112
113 /* externals */
114 extern int fat_register_device(block_dev_desc_t *, int);
115 extern int file_fat_detectfs(void);
116 extern long file_fat_read(const char *, void *, unsigned long);
117 extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
118 extern int flash_sect_erase(ulong, ulong);
119 extern int flash_sect_protect (int, ulong, ulong);
120 extern int flash_write (char *, ulong, ulong);
121 extern int u_boot_hush_start(void);
122 #ifdef CONFIG_PROGRESSBAR
123 extern void show_progress(int, int);
124 extern void lcd_puts (char *);
125 extern void lcd_enable(void);
126 #endif
127
128 int au_check_cksum_valid(int idx, long nbytes)
129 {
130         image_header_t *hdr;
131
132         hdr = (image_header_t *)LOAD_ADDR;
133 #if defined(CONFIG_FIT)
134         if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
135                 puts ("Non legacy image format not supported\n");
136                 return -1;
137         }
138 #endif
139
140         if (nbytes != image_get_image_size (hdr)) {
141                 printf ("Image %s bad total SIZE\n", aufile[idx]);
142                 return -1;
143         }
144         /* check the data CRC */
145         if (!image_check_dcrc (hdr)) {
146                 printf ("Image %s bad data checksum\n", aufile[idx]);
147                 return -1;
148         }
149         return 0;
150 }
151
152 int au_check_header_valid(int idx, long nbytes)
153 {
154         image_header_t *hdr;
155         unsigned long checksum, fsize;
156
157         hdr = (image_header_t *)LOAD_ADDR;
158 #if defined(CONFIG_FIT)
159         if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
160                 puts ("Non legacy image format not supported\n");
161                 return -1;
162         }
163 #endif
164
165         /* check the easy ones first */
166 #undef CHECK_VALID_DEBUG
167 #ifdef CHECK_VALID_DEBUG
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);
172 #endif
173         if (nbytes < image_get_header_size ()) {
174                 printf ("Image %s bad header SIZE\n", aufile[idx]);
175                 ausize[idx] = 0;
176                 return -1;
177         }
178         if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) {
179                 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
180                 ausize[idx] = 0;
181                 return -1;
182         }
183         /* check the hdr CRC */
184         if (!image_check_hcrc (hdr)) {
185                 printf ("Image %s bad header checksum\n", aufile[idx]);
186                 ausize[idx] = 0;
187                 return -1;
188         }
189         /* check the type - could do this all in one gigantic if() */
190         if ((idx == IDX_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
191                 printf ("Image %s wrong type\n", aufile[idx]);
192                 ausize[idx] = 0;
193                 return -1;
194         }
195         if ((idx == IDX_KERNEL) && !image_check_type (hdr, IH_TYPE_KERNEL)) {
196                 printf ("Image %s wrong type\n", aufile[idx]);
197                 ausize[idx] = 0;
198                 return -1;
199         }
200         if ((idx == IDX_ROOTFS) &&
201                         (!image_check_type (hdr, IH_TYPE_RAMDISK) &&
202                         !image_check_type (hdr, IH_TYPE_FILESYSTEM))) {
203                 printf ("Image %s wrong type\n", aufile[idx]);
204                 ausize[idx] = 0;
205                 return -1;
206         }
207         /* recycle checksum */
208         checksum = image_get_data_size (hdr);
209
210         fsize = checksum + image_get_header_size ();
211         /* for kernel and ramdisk the image header must also fit into flash */
212         if (idx == IDX_KERNEL || image_check_type (hdr, IH_TYPE_RAMDISK))
213                 checksum += image_get_header_size ();
214
215         /* check the size does not exceed space in flash. HUSH scripts */
216         if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
217                 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
218                 ausize[idx] = 0;
219                 return -1;
220         }
221         /* Update with the real filesize */
222         ausize[idx] = fsize;
223
224         return checksum; /* return size to be written to flash */
225 }
226
227 int au_do_update(int idx, long sz)
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;
236 #if defined(CONFIG_FIT)
237         if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
238                 puts ("Non legacy image format not supported\n");
239                 return -1;
240         }
241 #endif
242
243         /* execute a script */
244         if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
245                 addr = (char *)((char *)hdr + image_get_header_size ());
246                 /* stick a NULL at the end of the script, otherwise */
247                 /* parse_string_outer() runs off the end. */
248                 addr[image_get_data_size (hdr)] = 0;
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);
274         mdelay(100);
275 #ifdef CONFIG_PROGRESSBAR
276         show_progress(end - start, totsize);
277 #endif
278
279         /* strip the header - except for the kernel and ramdisk */
280         if (image_check_type (hdr, IH_TYPE_KERNEL) ||
281                         image_check_type (hdr, IH_TYPE_RAMDISK)) {
282                 addr = (char *)hdr;
283                 off = image_get_header_size ();
284                 nbytes = image_get_image_size (hdr);
285         } else {
286                 addr = (char *)((char *)hdr + image_get_header_size ());
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 = image_get_data_size (hdr);
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), image_get_data_size (hdr)) !=
310             image_get_dcrc (hdr)) {
311                 printf ("Image %s Bad Data Checksum after COPY\n", aufile[idx]);
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
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  */
327 int do_auto_update(void)
328 {
329         block_dev_desc_t *stor_dev;
330         long sz;
331         int i, res = 0, cnt, old_ctrlc;
332         char *env;
333         long start, end;
334
335 #if 0 /* disable key-press detection to speed up boot-up time */
336         uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
337
338         /*
339          * Read keypad status
340          */
341         i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
342         mdelay(500);
343         i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
344
345         /*
346          * Check keypad
347          */
348         if ( !(keypad_status1[1] & KEYPAD_MASK_LO) ||
349               (keypad_status1[1] != keypad_status2[1])) {
350                 return 0;
351         }
352
353 #endif
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");
371                 res = -1;
372                 goto xit;
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");
378                 res = -1;
379                 goto xit;
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);
384                 res = -1;
385                 goto xit;
386         }
387         if (file_fat_detectfs() != 0) {
388                 debug ("file_fat_detectfs failed\n");
389         }
390
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;
405                 aufl_layout[IDX_FIRMWARE].start = start;
406                 aufl_layout[IDX_FIRMWARE].end = end;
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;
418                 aufl_layout[IDX_KERNEL].start = start;
419                 aufl_layout[IDX_KERNEL].end = end;
420         }
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
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
440         /* validate the images first */
441         for (i = 0; i < AU_MAXFILES; i++) {
442                 ulong imsize;
443                 /* just read the header */
444                 sz = file_fat_read(aufile[i], LOAD_ADDR, image_get_header_size ());
445                 debug ("read %s sz %ld hdr %d\n",
446                         aufile[i], sz, image_get_header_size ());
447                 if (sz <= 0 || sz < image_get_header_size ()) {
448                         debug ("%s not found\n", aufile[i]);
449                         ausize[i] = 0;
450                         continue;
451                 }
452                 /* au_check_header_valid() updates ausize[] */
453                 if ((imsize = au_check_header_valid(i, sz)) < 0) {
454                         debug ("%s header not valid\n", aufile[i]);
455                         continue;
456                 }
457                 /* totsize accounts for image size and flash erase size */
458                 totsize += (imsize + (aufl_layout[i].end - aufl_layout[i].start));
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
475                 debug ("read %s sz %ld hdr %d\n",
476                         aufile[i], sz, image_get_header_size ());
477
478                 if (sz != ausize[i]) {
479                         printf ("%s: size %ld read %ld?\n", aufile[i], ausize[i], sz);
480                         continue;
481                 }
482
483                 if (sz <= 0 || sz <= image_get_header_size ()) {
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;
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();
499                                 break;
500                         }
501                         cnt++;
502 #ifdef AU_TEST_ONLY
503                 } while (res < 0 && cnt < (AU_MAXFILES + 1));
504                 if (cnt < (AU_MAXFILES + 1))
505 #else
506                 } while (res < 0);
507 #endif
508         }
509
510         /* restore the old state */
511         disable_ctrlc(old_ctrlc);
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
522  xit:
523         usb_stop();
524         return res;
525 }
526 #endif /* CONFIG_AUTO_UPDATE */