]> git.sur5r.net Git - u-boot/blob - board/mcc200/auto_update.c
Merge branch 'master' into hpc2
[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
27 #ifdef CFG_HUSH_PARSER
28 #include <hush.h>
29 #endif
30
31
32 #ifdef CONFIG_AUTO_UPDATE
33
34 #ifndef CONFIG_USB_OHCI
35 #error "must define CONFIG_USB_OHCI"
36 #endif
37
38 #ifndef CONFIG_USB_STORAGE
39 #error "must define CONFIG_USB_STORAGE"
40 #endif
41
42 #ifndef CFG_HUSH_PARSER
43 #error "must define CFG_HUSH_PARSER"
44 #endif
45
46 #if !(CONFIG_COMMANDS & CFG_CMD_FAT)
47 #error "must define CFG_CMD_FAT"
48 #endif
49
50 /*
51  * Check whether a USB memory stick is plugged in.
52  * If one is found:
53  *      1) if prepare.img ist found load it into memory. If it is
54  *              valid then run it.
55  *      2) if preinst.img is found load it into memory. If it is
56  *              valid then run it. Update the EEPROM.
57  *      3) if firmw_01.img is found load it into memory. If it is valid,
58  *              burn it into FLASH and update the EEPROM.
59  *      4) if kernl_01.img is found load it into memory. If it is valid,
60  *              burn it into FLASH and update the EEPROM.
61  *      5) if app.img is found load it into memory. If it is valid,
62  *              burn it into FLASH and update the EEPROM.
63  *      6) if disk.img is found load it into memory. If it is valid,
64  *              burn it into FLASH and update the EEPROM.
65  *      7) if postinst.img is found load it into memory. If it is
66  *              valid then run it. Update the EEPROM.
67  */
68
69 #undef AU_DEBUG
70
71 #undef debug
72 #ifdef  AU_DEBUG
73 #define debug(fmt,args...)      printf (fmt ,##args)
74 #else
75 #define debug(fmt,args...)
76 #endif  /* AU_DEBUG */
77
78 /* possible names of files on the USB stick. */
79 #define AU_FIRMWARE     "u-boot.img"
80 #define AU_KERNEL       "kernel.img"
81
82 struct flash_layout {
83         long start;
84         long end;
85 };
86
87 /* layout of the FLASH. ST = start address, ND = end address. */
88 #define AU_FL_FIRMWARE_ST       0xfC000000
89 #define AU_FL_FIRMWARE_ND       0xfC03FFFF
90 #define AU_FL_KERNEL_ST         0xfC0C0000
91 #define AU_FL_KERNEL_ND         0xfC1BFFFF
92
93 static int au_usb_stor_curr_dev; /* current device */
94
95 /* index of each file in the following arrays */
96 #define IDX_FIRMWARE    0
97 #define IDX_KERNEL      1
98
99 /* max. number of files which could interest us */
100 #define AU_MAXFILES 2
101
102 /* pointers to file names */
103 char *aufile[AU_MAXFILES];
104
105 /* sizes of flash areas for each file */
106 long ausize[AU_MAXFILES];
107
108 /* array of flash areas start and end addresses */
109 struct flash_layout aufl_layout[AU_MAXFILES] = { \
110         {AU_FL_FIRMWARE_ST, AU_FL_FIRMWARE_ND,}, \
111         {AU_FL_KERNEL_ST, AU_FL_KERNEL_ND,}, \
112 };
113
114 /* where to load files into memory */
115 #define LOAD_ADDR ((unsigned char *)0x00200000)
116
117 /* the app is the largest image */
118 #define MAX_LOADSZ ausize[IDX_KERNEL]
119
120 /*i2c address of the keypad status*/
121 #define I2C_PSOC_KEYPAD_ADDR    0x53
122
123 /* keypad mask */
124 #define KEYPAD_ROW      3
125 #define KEYPAD_COL      3
126 #define KEYPAD_MASK_LO  ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*4-4)))&0xFF)
127 #define KEYPAD_MASK_HI  ((1<<(KEYPAD_COL-1+(KEYPAD_ROW*4-4)))>>8)
128
129 /* externals */
130 extern int fat_register_device(block_dev_desc_t *, int);
131 extern int file_fat_detectfs(void);
132 extern long file_fat_read(const char *, void *, unsigned long);
133 extern int i2c_read (unsigned char, unsigned int, int , unsigned char* , int);
134 extern int flash_sect_erase(ulong, ulong);
135 extern int flash_sect_protect (int, ulong, ulong);
136 extern int flash_write (char *, ulong, ulong);
137 /* change char* to void* to shutup the compiler */
138 extern block_dev_desc_t *get_dev (char*, int);
139 extern int u_boot_hush_start(void);
140
141 int au_check_cksum_valid(int idx, long nbytes)
142 {
143         image_header_t *hdr;
144         unsigned long checksum;
145
146         hdr = (image_header_t *)LOAD_ADDR;
147
148         if (nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size))) {
149                 printf ("Image %s bad total SIZE\n", aufile[idx]);
150                 return -1;
151         }
152         /* check the data CRC */
153         checksum = ntohl(hdr->ih_dcrc);
154
155         if (crc32 (0, (uchar *)(LOAD_ADDR + sizeof(*hdr)), ntohl(hdr->ih_size)) != checksum) {
156                 printf ("Image %s bad data checksum\n", aufile[idx]);
157                 return -1;
158         }
159         return 0;
160 }
161
162 int au_check_header_valid(int idx, long nbytes)
163 {
164         image_header_t *hdr;
165         unsigned long checksum;
166         unsigned char buf[4];
167
168         hdr = (image_header_t *)LOAD_ADDR;
169         /* check the easy ones first */
170 #undef CHECK_VALID_DEBUG
171 #ifdef CHECK_VALID_DEBUG
172         printf("magic %#x %#x ", ntohl(hdr->ih_magic), IH_MAGIC);
173         printf("arch %#x %#x ", hdr->ih_arch, IH_CPU_ARM);
174         printf("size %#x %#lx ", ntohl(hdr->ih_size), nbytes);
175         printf("type %#x %#x ", hdr->ih_type, IH_TYPE_KERNEL);
176 #endif
177         if (nbytes < sizeof(*hdr)) {
178                 printf ("Image %s bad header SIZE\n", aufile[idx]);
179                 return -1;
180         }
181         if (ntohl(hdr->ih_magic) != IH_MAGIC || hdr->ih_arch != IH_CPU_PPC) {
182                 printf ("Image %s bad MAGIC or ARCH\n", aufile[idx]);
183                 return -1;
184         }
185         /* check the hdr CRC */
186         checksum = ntohl(hdr->ih_hcrc);
187         hdr->ih_hcrc = 0;
188
189         if (crc32 (0, (uchar *)hdr, sizeof(*hdr)) != checksum) {
190                 printf ("Image %s bad header checksum\n", aufile[idx]);
191                 return -1;
192         }
193         hdr->ih_hcrc = htonl(checksum);
194         /* check the type - could do this all in one gigantic if() */
195         if ((idx == IDX_FIRMWARE) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
196                 printf ("Image %s wrong type\n", aufile[idx]);
197                 return -1;
198         }
199         if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
200                 printf ("Image %s wrong type\n", aufile[idx]);
201                 return -1;
202         }
203         /* recycle checksum */
204         checksum = ntohl(hdr->ih_size);
205         /* for kernel and app the image header must also fit into flash */
206         if (idx != IDX_FIRMWARE)
207                 checksum += sizeof(*hdr);
208         /* check the size does not exceed space in flash. HUSH scripts */
209         /* all have ausize[] set to 0 */
210         if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
211                 printf ("Image %s is bigger than FLASH\n", aufile[idx]);
212                 return -1;
213         }
214         return 0;
215 }
216
217 int au_do_update(int idx, long sz)
218 {
219         image_header_t *hdr;
220         char *addr;
221         long start, end;
222         int off, rc;
223         uint nbytes;
224
225         hdr = (image_header_t *)LOAD_ADDR;
226
227         /* execute a script */
228         if (hdr->ih_type == IH_TYPE_SCRIPT) {
229                 addr = (char *)((char *)hdr + sizeof(*hdr));
230                 /* stick a NULL at the end of the script, otherwise */
231                 /* parse_string_outer() runs off the end. */
232                 addr[ntohl(hdr->ih_size)] = 0;
233                 addr += 8;
234                 parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
235                 return 0;
236         }
237
238         start = aufl_layout[idx].start;
239         end = aufl_layout[idx].end;
240
241         /* unprotect the address range */
242         /* this assumes that ONLY the firmware is protected! */
243         if (idx == IDX_FIRMWARE) {
244 #undef AU_UPDATE_TEST
245 #ifdef AU_UPDATE_TEST
246                 /* erase it where Linux goes */
247                 start = aufl_layout[1].start;
248                 end = aufl_layout[1].end;
249 #endif
250                 flash_sect_protect(0, start, end);
251         }
252
253         /*
254          * erase the address range.
255          */
256         debug ("flash_sect_erase(%lx, %lx);\n", start, end);
257         flash_sect_erase(start, end);
258         wait_ms(100);
259         /* strip the header - except for the kernel and ramdisk */
260         if (hdr->ih_type == IH_TYPE_KERNEL) {
261                 addr = (char *)hdr;
262                 off = sizeof(*hdr);
263                 nbytes = sizeof(*hdr) + ntohl(hdr->ih_size);
264         } else {
265                 addr = (char *)((char *)hdr + sizeof(*hdr));
266 #ifdef AU_UPDATE_TEST
267                 /* copy it to where Linux goes */
268                 if (idx == IDX_FIRMWARE)
269                         start = aufl_layout[1].start;
270 #endif
271                 off = 0;
272                 nbytes = ntohl(hdr->ih_size);
273         }
274
275         /* copy the data from RAM to FLASH */
276         debug ("flash_write(%p, %lx %x)\n", addr, start, nbytes);
277         rc = flash_write(addr, start, nbytes);
278         if (rc != 0) {
279                 printf("Flashing failed due to error %d\n", rc);
280                 return -1;
281         }
282
283         /* check the dcrc of the copy */
284         if (crc32 (0, (uchar *)(start + off), ntohl(hdr->ih_size)) != ntohl(hdr->ih_dcrc)) {
285                 printf ("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
286                 return -1;
287         }
288
289         /* protect the address range */
290         /* this assumes that ONLY the firmware is protected! */
291         if (idx == IDX_FIRMWARE)
292                 flash_sect_protect(1, start, end);
293         return 0;
294 }
295
296 /*
297  * this is called from board_init() after the hardware has been set up
298  * and is usable. That seems like a good time to do this.
299  * Right now the return value is ignored.
300  */
301 int do_auto_update(void)
302 {
303         block_dev_desc_t *stor_dev;
304         long sz;
305         int i, res, bitmap_first, cnt, old_ctrlc, got_ctrlc;
306         char *env;
307         long start, end;
308         char keypad_status1[2] = {0,0}, keypad_status2[2] = {0,0};
309
310         /*
311          * Read keypad status
312          */
313         i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status1, 2);
314         wait_ms(500);
315         i2c_read(I2C_PSOC_KEYPAD_ADDR, 0, 0, keypad_status2, 2);
316
317         /*
318          * Check keypad
319          */
320         if ( !(keypad_status1[0] & KEYPAD_MASK_HI) ||
321               (keypad_status1[0] != keypad_status2[0])) {
322                 return 0;
323         }
324         if ( !(keypad_status1[1] & KEYPAD_MASK_LO) ||
325               (keypad_status1[1] != keypad_status2[1])) {
326                 return 0;
327         }
328         au_usb_stor_curr_dev = -1;
329         /* start USB */
330         if (usb_stop() < 0) {
331                 debug ("usb_stop failed\n");
332                 return -1;
333         }
334         if (usb_init() < 0) {
335                 debug ("usb_init failed\n");
336                 return -1;
337         }
338         /*
339          * check whether a storage device is attached (assume that it's
340          * a USB memory stick, since nothing else should be attached).
341          */
342         au_usb_stor_curr_dev = usb_stor_scan(0);
343         if (au_usb_stor_curr_dev == -1) {
344                 debug ("No device found. Not initialized?\n");
345                 return -1;
346         }
347         /* check whether it has a partition table */
348         stor_dev = get_dev("usb", 0);
349         if (stor_dev == NULL) {
350                 debug ("uknown device type\n");
351                 return -1;
352         }
353         if (fat_register_device(stor_dev, 1) != 0) {
354                 debug ("Unable to use USB %d:%d for fatls\n",
355                         au_usb_stor_curr_dev, 1);
356                 return -1;
357         }
358         if (file_fat_detectfs() != 0) {
359                 debug ("file_fat_detectfs failed\n");
360         }
361
362         /* initialize the array of file names */
363         memset(aufile, 0, sizeof(aufile));
364         aufile[IDX_FIRMWARE] = AU_FIRMWARE;
365         aufile[IDX_KERNEL] = AU_KERNEL;
366         /* initialize the array of flash sizes */
367         memset(ausize, 0, sizeof(ausize));
368         ausize[IDX_FIRMWARE] = (AU_FL_FIRMWARE_ND + 1) - AU_FL_FIRMWARE_ST;
369         ausize[IDX_KERNEL] = (AU_FL_KERNEL_ND + 1) - AU_FL_KERNEL_ST;
370         /*
371          * now check whether start and end are defined using environment
372          * variables.
373          */
374         start = -1;
375         end = 0;
376         env = getenv("firmware_st");
377         if (env != NULL)
378                 start = simple_strtoul(env, NULL, 16);
379         env = getenv("firmware_nd");
380         if (env != NULL)
381                 end = simple_strtoul(env, NULL, 16);
382         if (start >= 0 && end && end > start) {
383                 ausize[IDX_FIRMWARE] = (end + 1) - start;
384                 aufl_layout[0].start = start;
385                 aufl_layout[0].end = end;
386         }
387         start = -1;
388         end = 0;
389         env = getenv("kernel_st");
390         if (env != NULL)
391                 start = simple_strtoul(env, NULL, 16);
392         env = getenv("kernel_nd");
393         if (env != NULL)
394                 end = simple_strtoul(env, NULL, 16);
395         if (start >= 0 && end && end > start) {
396                 ausize[IDX_KERNEL] = (end + 1) - start;
397                 aufl_layout[1].start = start;
398                 aufl_layout[1].end = end;
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 < 3);
447                 if (cnt < 3)
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 */