Merge branch 'master' of /home/git/u-boot
[platform/kernel/u-boot.git] / board / esd / common / auto_update.c
1 /*
2  * (C) Copyright 2003-2004
3  * Gary Jennejohn, DENX Software Engineering, gj@denx.de.
4  * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24
25 #include <common.h>
26
27 #include <command.h>
28 #include <image.h>
29 #include <asm/byteorder.h>
30 #if defined(CFG_NAND_LEGACY)
31 #include <linux/mtd/nand_legacy.h>
32 #endif
33 #include <fat.h>
34 #include <part.h>
35
36 #include "auto_update.h"
37
38 #ifdef CONFIG_AUTO_UPDATE
39
40 #if !defined(CONFIG_CMD_FAT)
41 #error "must define CONFIG_CMD_FAT"
42 #endif
43
44 extern au_image_t au_image[];
45 extern int N_AU_IMAGES;
46
47 #define AU_DEBUG
48 #undef AU_DEBUG
49
50 #undef debug
51 #ifdef  AU_DEBUG
52 #define debug(fmt,args...)      printf (fmt ,##args)
53 #else
54 #define debug(fmt,args...)
55 #endif  /* AU_DEBUG */
56
57
58 #define LOAD_ADDR ((unsigned char *)0x100000)   /* where to load files into memory */
59 #define MAX_LOADSZ 0x1e00000
60
61 /* externals */
62 extern int fat_register_device(block_dev_desc_t *, int);
63 extern int file_fat_detectfs(void);
64 extern long file_fat_read(const char *, void *, unsigned long);
65 long do_fat_read (const char *filename, void *buffer, unsigned long maxsize, int dols);
66 #ifdef CONFIG_VFD
67 extern int trab_vfd (ulong);
68 extern int transfer_pic(unsigned char, unsigned char *, int, int);
69 #endif
70 extern int flash_sect_erase(ulong, ulong);
71 extern int flash_sect_protect (int, ulong, ulong);
72 extern int flash_write (char *, ulong, ulong);
73
74 #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
75 /* references to names in cmd_nand.c */
76 #define NANDRW_READ     0x01
77 #define NANDRW_WRITE    0x00
78 #define NANDRW_JFFS2    0x02
79 #define NANDRW_JFFS2_SKIP       0x04
80 extern struct nand_chip nand_dev_desc[];
81 extern int nand_legacy_rw(struct nand_chip* nand, int cmd, size_t start, size_t len,
82                    size_t * retlen, u_char * buf);
83 extern int nand_legacy_erase(struct nand_chip* nand, size_t ofs, size_t len, int clean);
84 #endif
85
86 extern block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
87
88
89 int au_check_cksum_valid(int i, long nbytes)
90 {
91         image_header_t *hdr;
92
93         hdr = (image_header_t *)LOAD_ADDR;
94
95         if ((au_image[i].type == AU_FIRMWARE) &&
96             (au_image[i].size != image_get_data_size (hdr))) {
97                 printf ("Image %s has wrong size\n", au_image[i].name);
98                 return -1;
99         }
100
101         if (nbytes != (image_get_image_size (hdr))) {
102                 printf ("Image %s bad total SIZE\n", au_image[i].name);
103                 return -1;
104         }
105
106         /* check the data CRC */
107         if (!image_check_dcrc (hdr)) {
108                 printf ("Image %s bad data checksum\n", au_image[i].name);
109                 return -1;
110         }
111         return 0;
112 }
113
114
115 int au_check_header_valid(int i, long nbytes)
116 {
117         image_header_t *hdr;
118         unsigned long checksum;
119
120         hdr = (image_header_t *)LOAD_ADDR;
121         /* check the easy ones first */
122 #undef CHECK_VALID_DEBUG
123 #ifdef CHECK_VALID_DEBUG
124         printf("magic %#x %#x ", image_get_magic (hdr), IH_MAGIC);
125         printf("arch %#x %#x ", image_get_arch (hdr), IH_ARCH_PPC);
126         printf("size %#x %#lx ", image_get_data_size (hdr), nbytes);
127         printf("type %#x %#x ", image_get_type (hdr), IH_TYPE_KERNEL);
128 #endif
129         if (nbytes < image_get_header_size ())
130         {
131                 printf ("Image %s bad header SIZE\n", au_image[i].name);
132                 return -1;
133         }
134         if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC))
135         {
136                 printf ("Image %s bad MAGIC or ARCH\n", au_image[i].name);
137                 return -1;
138         }
139         if (!image_check_hcrc (hdr)) {
140                 printf ("Image %s bad header checksum\n", au_image[i].name);
141                 return -1;
142         }
143
144         /* check the type - could do this all in one gigantic if() */
145         if ((au_image[i].type == AU_FIRMWARE) && !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
146                 printf ("Image %s wrong type\n", au_image[i].name);
147                 return -1;
148         }
149         if ((au_image[i].type == AU_SCRIPT) && !image_check_type (hdr, IH_TYPE_SCRIPT)) {
150                 printf ("Image %s wrong type\n", au_image[i].name);
151                 return -1;
152         }
153
154         /* recycle checksum */
155         checksum = image_get_data_size (hdr);
156
157 #if 0 /* test-only */
158         /* for kernel and app the image header must also fit into flash */
159         if (idx != IDX_DISK)
160                 checksum += image_get_header_size ();
161         /* check the size does not exceed space in flash. HUSH scripts */
162         /* all have ausize[] set to 0 */
163         if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
164                 printf ("Image %s is bigger than FLASH\n", au_image[i].name);
165                 return -1;
166         }
167 #endif
168
169         return 0;
170 }
171
172
173 int au_do_update(int i, long sz)
174 {
175         image_header_t *hdr;
176         char *addr;
177         long start, end;
178         int off, rc;
179         uint nbytes;
180         int k;
181 #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
182         int total;
183 #endif
184
185         hdr = (image_header_t *)LOAD_ADDR;
186
187         switch (au_image[i].type) {
188         case AU_SCRIPT:
189                 printf("Executing script %s\n", au_image[i].name);
190
191                 /* execute a script */
192                 if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
193                         addr = (char *)((char *)hdr + image_get_header_size ());
194                         /* stick a NULL at the end of the script, otherwise */
195                         /* parse_string_outer() runs off the end. */
196                         addr[image_get_data_size (hdr)] = 0;
197                         addr += 8;
198
199                         /*
200                          * Replace cr/lf with ;
201                          */
202                         k = 0;
203                         while (addr[k] != 0) {
204                                 if ((addr[k] == 10) || (addr[k] == 13)) {
205                                         addr[k] = ';';
206                                 }
207                                 k++;
208                         }
209
210                         run_command(addr, 0);
211                         return 0;
212                 }
213
214                 break;
215
216         case AU_FIRMWARE:
217         case AU_NOR:
218         case AU_NAND:
219                 start = au_image[i].start;
220                 end = au_image[i].start + au_image[i].size - 1;
221
222                 /*
223                  * do not update firmware when image is already in flash.
224                  */
225                 if (au_image[i].type == AU_FIRMWARE) {
226                         char *orig = (char*)start;
227                         char *new  = (char *)((char *)hdr + image_get_header_size ());
228                         nbytes = image_get_data_size (hdr);
229
230                         while(--nbytes) {
231                                 if (*orig++ != *new++) {
232                                         break;
233                                 }
234                         }
235                         if (!nbytes) {
236                                 printf("Skipping firmware update - images are identical\n");
237                                 break;
238                         }
239                 }
240
241                 /* unprotect the address range */
242                 /* this assumes that ONLY the firmware is protected! */
243                 if (au_image[i].type == AU_FIRMWARE) {
244                         flash_sect_protect(0, start, end);
245                 }
246
247                 /*
248                  * erase the address range.
249                  */
250                 if (au_image[i].type != AU_NAND) {
251                         printf("Updating NOR FLASH with image %s\n", au_image[i].name);
252                         debug ("flash_sect_erase(%lx, %lx);\n", start, end);
253                         flash_sect_erase(start, end);
254                 } else {
255 #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
256                         printf("Updating NAND FLASH with image %s\n", au_image[i].name);
257                         debug ("nand_legacy_erase(%lx, %lx);\n", start, end);
258                         rc = nand_legacy_erase (nand_dev_desc, start, end - start + 1, 0);
259                         debug ("nand_legacy_erase returned %x\n", rc);
260 #endif
261                 }
262
263                 udelay(10000);
264
265                 /* strip the header - except for the kernel and ramdisk */
266                 if (au_image[i].type != AU_FIRMWARE) {
267                         addr = (char *)hdr;
268                         off = image_get_header_size ();
269                         nbytes = image_get_image_size (hdr);
270                 } else {
271                         addr = (char *)((char *)hdr + image_get_header_size ());
272                         off = 0;
273                         nbytes = image_get_data_size (hdr);
274                 }
275
276                 /*
277                  * copy the data from RAM to FLASH
278                  */
279                 if (au_image[i].type != AU_NAND) {
280                         debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
281                         rc = flash_write((char *)addr, start, nbytes);
282                 } else {
283 #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
284                         debug ("nand_legacy_rw(%p, %lx %x)\n", addr, start, nbytes);
285                         rc = nand_legacy_rw(nand_dev_desc, NANDRW_WRITE | NANDRW_JFFS2,
286                                      start, nbytes, (size_t *)&total, (uchar *)addr);
287                         debug ("nand_legacy_rw: ret=%x total=%d nbytes=%d\n", rc, total, nbytes);
288 #else
289                         rc = -1;
290 #endif
291                 }
292                 if (rc != 0) {
293                         printf("Flashing failed due to error %d\n", rc);
294                         return -1;
295                 }
296
297                 /*
298                  * check the dcrc of the copy
299                  */
300                 if (au_image[i].type != AU_NAND) {
301                         rc = crc32 (0, (uchar *)(start + off), image_get_data_size (hdr));
302                 } else {
303 #if defined(CONFIG_CMD_NAND) && defined(CFG_NAND_LEGACY)
304                         rc = nand_legacy_rw(nand_dev_desc, NANDRW_READ | NANDRW_JFFS2 | NANDRW_JFFS2_SKIP,
305                                      start, nbytes, (size_t *)&total, (uchar *)addr);
306                         rc = crc32 (0, (uchar *)(addr + off), image_get_data_size (hdr));
307 #endif
308                 }
309                 if (rc != image_get_dcrc (hdr)) {
310                         printf ("Image %s Bad Data Checksum After COPY\n", au_image[i].name);
311                         return -1;
312                 }
313
314                 /* protect the address range */
315                 /* this assumes that ONLY the firmware is protected! */
316                 if (au_image[i].type == AU_FIRMWARE) {
317                         flash_sect_protect(1, start, end);
318                 }
319
320                 break;
321
322         default:
323                 printf("Wrong image type selected!\n");
324         }
325
326         return 0;
327 }
328
329
330 static void process_macros (const char *input, char *output)
331 {
332         char c, prev;
333         const char *varname_start = NULL;
334         int inputcnt  = strlen (input);
335         int outputcnt = CFG_CBSIZE;
336         int state = 0;  /* 0 = waiting for '$'  */
337                         /* 1 = waiting for '(' or '{' */
338                         /* 2 = waiting for ')' or '}' */
339                         /* 3 = waiting for '''  */
340 #ifdef DEBUG_PARSER
341         char *output_start = output;
342
343         printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
344 #endif
345
346         prev = '\0';                    /* previous character   */
347
348         while (inputcnt && outputcnt) {
349             c = *input++;
350             inputcnt--;
351
352             if (state!=3) {
353             /* remove one level of escape characters */
354             if ((c == '\\') && (prev != '\\')) {
355                 if (inputcnt-- == 0)
356                         break;
357                 prev = c;
358                 c = *input++;
359             }
360             }
361
362             switch (state) {
363             case 0:                     /* Waiting for (unescaped) $    */
364                 if ((c == '\'') && (prev != '\\')) {
365                         state = 3;
366                         break;
367                 }
368                 if ((c == '$') && (prev != '\\')) {
369                         state++;
370                 } else {
371                         *(output++) = c;
372                         outputcnt--;
373                 }
374                 break;
375             case 1:                     /* Waiting for (        */
376                 if (c == '(' || c == '{') {
377                         state++;
378                         varname_start = input;
379                 } else {
380                         state = 0;
381                         *(output++) = '$';
382                         outputcnt--;
383
384                         if (outputcnt) {
385                                 *(output++) = c;
386                                 outputcnt--;
387                         }
388                 }
389                 break;
390             case 2:                     /* Waiting for )        */
391                 if (c == ')' || c == '}') {
392                         int i;
393                         char envname[CFG_CBSIZE], *envval;
394                         int envcnt = input-varname_start-1; /* Varname # of chars */
395
396                         /* Get the varname */
397                         for (i = 0; i < envcnt; i++) {
398                                 envname[i] = varname_start[i];
399                         }
400                         envname[i] = 0;
401
402                         /* Get its value */
403                         envval = getenv (envname);
404
405                         /* Copy into the line if it exists */
406                         if (envval != NULL)
407                                 while ((*envval) && outputcnt) {
408                                         *(output++) = *(envval++);
409                                         outputcnt--;
410                                 }
411                         /* Look for another '$' */
412                         state = 0;
413                 }
414                 break;
415             case 3:                     /* Waiting for '        */
416                 if ((c == '\'') && (prev != '\\')) {
417                         state = 0;
418                 } else {
419                         *(output++) = c;
420                         outputcnt--;
421                 }
422                 break;
423             }
424             prev = c;
425         }
426
427         if (outputcnt)
428                 *output = 0;
429
430 #ifdef DEBUG_PARSER
431         printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
432                 strlen(output_start), output_start);
433 #endif
434 }
435
436
437 /*
438  * this is called from board_init() after the hardware has been set up
439  * and is usable. That seems like a good time to do this.
440  * Right now the return value is ignored.
441  */
442 int do_auto_update(void)
443 {
444         block_dev_desc_t *stor_dev;
445         long sz;
446         int i, res, cnt, old_ctrlc, got_ctrlc;
447         char buffer[32];
448         char str[80];
449
450         /*
451          * Check whether a CompactFlash is inserted
452          */
453         if (ide_dev_desc[0].type == DEV_TYPE_UNKNOWN) {
454                 return -1;       /* no disk detected! */
455         }
456
457         /* check whether it has a partition table */
458         stor_dev = get_dev("ide", 0);
459         if (stor_dev == NULL) {
460                 debug ("Uknown device type\n");
461                 return -1;
462         }
463         if (fat_register_device(stor_dev, 1) != 0) {
464                 debug ("Unable to register ide disk 0:1 for fatls\n");
465                 return -1;
466         }
467
468         /*
469          * Check if magic file is present
470          */
471         if (do_fat_read(AU_MAGIC_FILE, buffer, sizeof(buffer), LS_NO) <= 0) {
472                 return -1;
473         }
474
475 #ifdef CONFIG_AUTO_UPDATE_SHOW
476         board_auto_update_show(1);
477 #endif
478         puts("\nAutoUpdate Disk detected! Trying to update system...\n");
479
480         /* make sure that we see CTRL-C and save the old state */
481         old_ctrlc = disable_ctrlc(0);
482
483         /* just loop thru all the possible files */
484         for (i = 0; i < N_AU_IMAGES; i++) {
485                 /*
486                  * Try to expand the environment var in the fname
487                  */
488                 process_macros(au_image[i].name, str);
489                 strcpy(au_image[i].name, str);
490
491                 printf("Reading %s ...", au_image[i].name);
492                 /* just read the header */
493                 sz = do_fat_read(au_image[i].name, LOAD_ADDR, image_get_header_size (), LS_NO);
494                 debug ("read %s sz %ld hdr %d\n",
495                         au_image[i].name, sz, image_get_header_size ());
496                 if (sz <= 0 || sz < image_get_header_size ()) {
497                         puts(" not found\n");
498                         continue;
499                 }
500                 if (au_check_header_valid(i, sz) < 0) {
501                         puts(" header not valid\n");
502                         continue;
503                 }
504                 sz = do_fat_read(au_image[i].name, LOAD_ADDR, MAX_LOADSZ, LS_NO);
505                 debug ("read %s sz %ld hdr %d\n",
506                         au_image[i].name, sz, image_get_header_size ());
507                 if (sz <= 0 || sz <= image_get_header_size ()) {
508                         puts(" not found\n");
509                         continue;
510                 }
511                 if (au_check_cksum_valid(i, sz) < 0) {
512                         puts(" checksum not valid\n");
513                         continue;
514                 }
515                 puts(" done\n");
516
517                 do {
518                         res = au_do_update(i, sz);
519                         /* let the user break out of the loop */
520                         if (ctrlc() || had_ctrlc()) {
521                                 clear_ctrlc();
522                                 if (res < 0)
523                                         got_ctrlc = 1;
524                                 break;
525                         }
526                         cnt++;
527                 } while (res < 0);
528         }
529
530         /* restore the old state */
531         disable_ctrlc(old_ctrlc);
532
533         puts("AutoUpdate finished\n\n");
534 #ifdef CONFIG_AUTO_UPDATE_SHOW
535         board_auto_update_show(0);
536 #endif
537
538         return 0;
539 }
540
541
542 int auto_update(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
543 {
544         do_auto_update();
545
546         return 0;
547 }
548 U_BOOT_CMD(
549         autoupd,        1,      1,      auto_update,
550         "autoupd - Automatically update images\n",
551         NULL
552 );
553 #endif /* CONFIG_AUTO_UPDATE */