]> git.sur5r.net Git - u-boot/blob - board/mcc200/auto_update.c
Merge with git+ssh://sr@pollux.denx.org/home/sr/git/u-boot/denx-merge-sr
[u-boot] / board / mcc200 / auto_update.c
1 /*
2  * (C) Copyright 2006
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18  * MA 02111-1307 USA
19  */
20 #include <common.h>
21 #include <command.h>
22 #include <malloc.h>
23 #include <image.h>
24 #include <asm/byteorder.h>
25 #include <usb.h>
26 #include <part.h>
27
28 #ifdef CFG_HUSH_PARSER
29 #include <hush.h>
30 #endif
31
32
33 #ifdef CONFIG_AUTO_UPDATE
34
35 #ifndef CONFIG_USB_OHCI
36 #error "must define CONFIG_USB_OHCI"
37 #endif
38
39 #ifndef CONFIG_USB_STORAGE
40 #error "must define CONFIG_USB_STORAGE"
41 #endif
42
43 #ifndef CFG_HUSH_PARSER
44 #error "must define CFG_HUSH_PARSER"
45 #endif
46
47 #if !(CONFIG_COMMANDS & CFG_CMD_FAT)
48 #error "must define CFG_CMD_FAT"
49 #endif
50
51 #undef AU_DEBUG
52
53 #undef debug
54 #ifdef  AU_DEBUG
55 #define debug(fmt,args...)      printf (fmt ,##args)
56 #else
57 #define debug(fmt,args...)
58 #endif  /* AU_DEBUG */
59
60 /* possible names of files on the USB stick. */
61 #define AU_FIRMWARE     "u-boot.img"
62 #define AU_KERNEL       "kernel.img"
63 #define AU_ROOTFS       "rootfs.img"
64
65 struct flash_layout {
66         long start;
67         long end;
68 };
69
70 /* layout of the FLASH. ST = start address, ND = end address. */
71 #define AU_FL_FIRMWARE_ST       0xfC000000
72 #define AU_FL_FIRMWARE_ND       0xfC03FFFF
73 #define AU_FL_KERNEL_ST         0xfC0C0000
74 #define AU_FL_KERNEL_ND         0xfC1BFFFF
75 #define AU_FL_ROOTFS_ST         0xFC1C0000
76 #define AU_FL_ROOTFS_ND         0xFCFBFFFF
77
78 static int au_usb_stor_curr_dev; /* current device */
79
80 /* index of each file in the following arrays */
81 #define IDX_FIRMWARE    0
82 #define IDX_KERNEL      1
83 #define IDX_ROOTFS      2
84
85 /* max. number of files which could interest us */
86 #define AU_MAXFILES 3
87
88 /* pointers to file names */
89 char *aufile[AU_MAXFILES] = {
90         AU_FIRMWARE,
91         AU_KERNEL,
92         AU_ROOTFS
93 };
94
95 /* sizes of flash areas for each file */
96 long ausize[AU_MAXFILES] = {
97         (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST,
98         (AU_FL_KERNEL_ND   + 1) - AU_FL_KERNEL_ST,
99         (AU_FL_ROOTFS_ND   + 1) - AU_FL_ROOTFS_ST,
100 };
101
102 /* array of flash areas start and end addresses */
103 struct flash_layout aufl_layout[AU_MAXFILES] = {
104         { AU_FL_FIRMWARE_ST,    AU_FL_FIRMWARE_ND, },
105         { AU_FL_KERNEL_ST,      AU_FL_KERNEL_ND,   },
106         { AU_FL_ROOTFS_ST,      AU_FL_ROOTFS_ND,   },
107 };
108
109 /* where to load files into memory */
110 #define LOAD_ADDR ((unsigned char *)0x00200000)
111
112 /* the root file system is the largest image */
113 #define MAX_LOADSZ ausize[IDX_ROOTFS]
114
115 /*i2c address of the keypad status*/
116 #define I2C_PSOC_KEYPAD_ADDR    0x53
117
118 /* keypad mask */
119 #define KEYPAD_ROW      2
120 #define KEYPAD_COL      2
121 #define KEYPAD_MASK_LO  ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))&0xFF)
122 #define KEYPAD_MASK_HI  ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*3-3)))>>8)
123
124 /* externals */
125 extern int fat_register_device(block_dev_desc_t *, int);
126 extern int file_fat_detectfs(void);
127 extern long file_fat_read(const char *, void *, unsigned long);
128 extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
129 extern int flash_sect_erase(ulong, ulong);
130 extern int flash_sect_protect (int, ulong, ulong);
131 extern int flash_write (char *, ulong, ulong);
132 extern int u_boot_hush_start(void);
133
134 int au_check_cksum_valid(int idx, long nbytes)
135 {
136         image_header_t *hdr;
137         unsigned long checksum;
138
139         hdr = (image_header_t *)LOAD_ADDR;
140
141         if (nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size))) {
142                 printf ("Image %s bad total SIZE\n", aufile[idx]);
143                 return -1;
144         }
145         /* check the data CRC */
146         checksum = ntohl(hdr->ih_dcrc);
147
148         if (crc32 (0, (uchar *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size)) != checksum) {
149                 printf ("Image %s bad data checksum\n", aufile[idx]);
150                 return -1;
151         }
152         return 0;
153 }
154
155 int au_check_header_valid(int idx, long nbytes)
156 {
157         image_header_t *hdr;
158         unsigned long checksum;
159
160         hdr = (image_header_t *)LOAD_ADDR;
161         /* check the easy ones first */
162 #undef CHECK_VALID_DEBUG
163 #ifdef CHECK_VALID_DEBUG
164         printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
165         printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_ARM);
166         printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
167         printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
168 #endif
169         if (nbytes < sizeof(*hdr)) {
170                 printf ("Image %s bad header SIZE\n", aufile[idx]);
171                 return -1;
172         }
173         if (ntohl(hdr->ih_magic) != IH_MAGIC || hdr->ih_arch != IH_CPU_PPC) {
174                 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
175                 return -1;
176         }
177         /* check the hdr CRC */
178         checksum = ntohl(hdr->ih_hcrc);
179         hdr->ih_hcrc = 0;
180
181         if (crc32 (0, (uchar *)hdr, sizeof(*hdr)) != checksum) {
182                 printf ("Image %s bad header checksum\n", aufile[idx]);
183                 return -1;
184         }
185         hdr->ih_hcrc = htonl(checksum);
186         /* check the type - could do this all in one gigantic if() */
187         if ((idx == IDX_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
188                 printf ("Image %s wrong type\n", aufile[idx]);
189                 return -1;
190         }
191         if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
192                 printf ("Image %s wrong type\n", aufile[idx]);
193                 return -1;
194         }
195         if ((idx == IDX_ROOTFS) &&
196                 ( (hdr->ih_type != IH_TYPE_RAMDISK) || (hdr->ih_type != IH_TYPE_FILESYSTEM) )
197            ) {
198                 printf ("Image %s wrong type\n", aufile[idx]);
199                 return -1;
200         }
201         /* recycle checksum */
202         checksum = ntohl(hdr->ih_size);
203         /* for kernel and app the image header must also fit into flash */
204         if (idx != IDX_FIRMWARE)
205                 checksum += sizeof(*hdr);
206         /* check the size does not exceed space in flash. HUSH scripts */
207         /* all have ausize[] set to 0 */
208         if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
209                 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
210                 return -1;
211         }
212         return 0;
213 }
214
215 int au_do_update(int idx, long sz)
216 {
217         image_header_t *hdr;
218         char *addr;
219         long start, end;
220         int off, rc;
221         uint nbytes;
222
223         hdr = (image_header_t *)LOAD_ADDR;
224
225         /* execute a script */
226         if (hdr->ih_type == IH_TYPE_SCRIPT) {
227                 addr = (char *)((char *)hdr + sizeof(*hdr));
228                 /* stick a NULL at the end of the script, otherwise */
229                 /* parse_string_outer() runs off the end. */
230                 addr[ntohl(hdr->ih_size)] = 0;
231                 addr += 8;
232                 parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
233                 return 0;
234         }
235
236         start = aufl_layout[idx].start;
237         end = aufl_layout[idx].end;
238
239         /* unprotect the address range */
240         /* this assumes that ONLY the firmware is protected! */
241         if (idx == IDX_FIRMWARE) {
242 #undef AU_UPDATE_TEST
243 #ifdef AU_UPDATE_TEST
244                 /* erase it where Linux goes */
245                 start = aufl_layout[1].start;
246                 end = aufl_layout[1].end;
247 #endif
248                 flash_sect_protect(0, start, end);
249         }
250
251         /*
252          * erase the address range.
253          */
254         debug ("flash_sect_erase(%lx, %lx);\n", start, end);
255         flash_sect_erase(start, end);
256         wait_ms(100);
257         /* strip the header - except for the kernel and ramdisk */
258         if (hdr->ih_type == IH_TYPE_KERNEL || hdr->ih_type == IH_TYPE_RAMDISK) {
259                 addr = (char *)hdr;
260                 off = sizeof(*hdr);
261                 nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
262         } else {
263                 addr = (char *)((char *)hdr + sizeof(*hdr));
264 #ifdef AU_UPDATE_TEST
265                 /* copy it to where Linux goes */
266                 if (idx == IDX_FIRMWARE)
267                         start = aufl_layout[1].start;
268 #endif
269                 off = 0;
270                 nbytes = ntohl(hdr->ih_size);
271         }
272
273         /* copy the data from RAM to FLASH */
274         debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
275         rc = flash_write(addr, start, nbytes);
276         if (rc != 0) {
277                 printf("Flashing failed due to error %d\n", rc);
278                 return -1;
279         }
280
281         /* check the data CRC of the copy */
282         if (crc32 (0, (uchar *)(start + off), ntohl(hdr->ih_size)) != ntohl(hdr->ih_dcrc)) {
283                 printf ("Image %s Bad Data Checksum after COPY\n", aufile[idx]);
284                 return -1;
285         }
286
287         /* protect the address range */
288         /* this assumes that ONLY the firmware is protected! */
289         if (idx == IDX_FIRMWARE)
290                 flash_sect_protect(1, start, end);
291         return 0;
292 }
293
294 /*
295  * this is called from board_init() after the hardware has been set up
296  * and is usable. That seems like a good time to do this.
297  * Right now the return value is ignored.
298  */
299 int do_auto_update(void)
300 {
301         block_dev_desc_t *stor_dev;
302         long sz;
303         int i, res, bitmap_first, cnt, old_ctrlc, got_ctrlc;
304         char *env;
305         long start, end;
306         uchar keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
307
308         /*
309          * Read keypad status
310          */
311         i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
312         wait_ms(500);
313         i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
314
315         /*
316          * Check keypad
317          */
318         if ( !(keypad_status1[1] & KEYPAD_MASK_LO) ||
319               (keypad_status1[1] != keypad_status2[1])) {
320                 return 0;
321         }
322         au_usb_stor_curr_dev = -1;
323         /* start USB */
324         if (usb_stop() < 0) {
325                 debug ("usb_stop failed\n");
326                 return -1;
327         }
328         if (usb_init() < 0) {
329                 debug ("usb_init failed\n");
330                 return -1;
331         }
332         /*
333          * check whether a storage device is attached (assume that it's
334          * a USB memory stick, since nothing else should be attached).
335          */
336         au_usb_stor_curr_dev = usb_stor_scan(0);
337         if (au_usb_stor_curr_dev == -1) {
338                 debug ("No device found. Not initialized?\n");
339                 return -1;
340         }
341         /* check whether it has a partition table */
342         stor_dev = get_dev("usb", 0);
343         if (stor_dev == NULL) {
344                 debug ("uknown device type\n");
345                 return -1;
346         }
347         if (fat_register_device(stor_dev, 1) != 0) {
348                 debug ("Unable to use USB %d:%d for fatls\n",
349                         au_usb_stor_curr_dev, 1);
350                 return -1;
351         }
352         if (file_fat_detectfs() != 0) {
353                 debug ("file_fat_detectfs failed\n");
354         }
355
356         /*
357          * now check whether start and end are defined using environment
358          * variables.
359          */
360         start = -1;
361         end = 0;
362         env = getenv("firmware_st");
363         if (env != NULL)
364                 start = simple_strtoul(env, NULL, 16);
365         env = getenv("firmware_nd");
366         if (env != NULL)
367                 end = simple_strtoul(env, NULL, 16);
368         if (start >= 0 && end && end > start) {
369                 ausize[IDX_FIRMWARE] = (end + 1) - start;
370                 aufl_layout[IDX_FIRMWARE].start = start;
371                 aufl_layout[IDX_FIRMWARE].end = end;
372         }
373         start = -1;
374         end = 0;
375         env = getenv("kernel_st");
376         if (env != NULL)
377                 start = simple_strtoul(env, NULL, 16);
378         env = getenv("kernel_nd");
379         if (env != NULL)
380                 end = simple_strtoul(env, NULL, 16);
381         if (start >= 0 && end && end > start) {
382                 ausize[IDX_KERNEL] = (end + 1) - start;
383                 aufl_layout[IDX_KERNEL].start = start;
384                 aufl_layout[IDX_KERNEL].end = end;
385         }
386         start = -1;
387         end = 0;
388         env = getenv("rootfs_st");
389         if (env != NULL)
390                 start = simple_strtoul(env, NULL, 16);
391         env = getenv("rootfs_nd");
392         if (env != NULL)
393                 end = simple_strtoul(env, NULL, 16);
394         if (start >= 0 && end && end > start) {
395                 ausize[IDX_ROOTFS] = (end + 1) - start;
396                 aufl_layout[IDX_ROOTFS].start = start;
397                 aufl_layout[IDX_ROOTFS].end = end;
398         }
399
400         /* make certain that HUSH is runnable */
401         u_boot_hush_start();
402         /* make sure that we see CTRL-C and save the old state */
403         old_ctrlc = disable_ctrlc(0);
404
405         bitmap_first = 0;
406         /* just loop thru all the possible files */
407         for (i = 0; i < AU_MAXFILES; i++) {
408                 /* just read the header */
409                 sz = file_fat_read(aufile[i], LOAD_ADDR, sizeof(image_header_t));
410                 debug ("read %s sz %ld hdr %d\n",
411                         aufile[i], sz, sizeof(image_header_t));
412                 if (sz <= 0 || sz < sizeof(image_header_t)) {
413                         debug ("%s not found\n", aufile[i]);
414                         continue;
415                 }
416                 if (au_check_header_valid(i, sz) < 0) {
417                         debug ("%s header not valid\n", aufile[i]);
418                         continue;
419                 }
420                 sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
421                 debug ("read %s sz %ld hdr %d\n",
422                         aufile[i], sz, sizeof(image_header_t));
423                 if (sz <= 0 || sz <= sizeof(image_header_t)) {
424                         debug ("%s not found\n", aufile[i]);
425                         continue;
426                 }
427                 if (au_check_cksum_valid(i, sz) < 0) {
428                         debug ("%s checksum not valid\n", aufile[i]);
429                         continue;
430                 }
431                 /* this is really not a good idea, but it's what the */
432                 /* customer wants. */
433                 cnt = 0;
434                 got_ctrlc = 0;
435                 do {
436                         res = au_do_update(i, sz);
437                         /* let the user break out of the loop */
438                         if (ctrlc() || had_ctrlc()) {
439                                 clear_ctrlc();
440                                 if (res < 0)
441                                         got_ctrlc = 1;
442                                 break;
443                         }
444                         cnt++;
445 #ifdef AU_TEST_ONLY
446                 } while (res < 0 && cnt < (AU_MAXFILES + 1));
447                 if (cnt < (AU_MAXFILES + 1))
448 #else
449                 } while (res < 0);
450 #endif
451         }
452         usb_stop();
453         /* restore the old state */
454         disable_ctrlc(old_ctrlc);
455         return 0;
456 }
457 #endif /* CONFIG_AUTO_UPDATE */