Use myloader parser in the adm5120 mtd driver. Do not use EXPORT_SYMBOL in myloader...
[librecmc/librecmc.git] / target / linux / adm5120-2.6 / files / drivers / mtd / maps / adm5120_mtd.c
1 /*
2  *  Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
3  *  Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
4  *  Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
5  *
6  *  original functions for finding root filesystem from Mike Baker 
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  *
13  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
14  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
15  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
16  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
17  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
19  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
21  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  *  You should have received a copy of the  GNU General Public License along
25  *  with this program; if not, write  to the Free Software Foundation, Inc.,
26  *  675 Mass Ave, Cambridge, MA 02139, USA.
27  * 
28  *  Copyright 2001-2003, Broadcom Corporation
29  *  All Rights Reserved.
30  * 
31  *  THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
32  *  KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
33  *  SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
34  *  FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
35  *
36  *  Flash mapping for ADM5120 boards
37  */
38
39 #include <linux/init.h>
40 #include <linux/module.h>
41 #include <linux/types.h>
42 #include <linux/kernel.h>
43 #include <linux/wait.h>
44 #include <linux/mtd/mtd.h>
45 #include <linux/mtd/map.h>
46 #ifdef CONFIG_MTD_PARTITIONS
47 #include <linux/mtd/partitions.h>
48 #endif
49 #include <linux/squashfs_fs.h>
50 #include <linux/jffs2.h>
51 #include <linux/crc32.h>
52 #include <asm/io.h>
53 #include <asm/mach-adm5120/myloader.h>
54 #include <asm/mach-adm5120/adm5120_info.h>
55
56 extern int parse_myloader_partitions(struct mtd_info *master,
57                         struct mtd_partition **pparts,
58                         unsigned long origin);
59
60 #define TRX_MAGIC       0x30524448      /* "HDR0" */
61 #define TRX_VERSION     1
62 #define TRX_MAX_LEN     0x3A0000
63 #define TRX_NO_HEADER   1               /* Do not write TRX header */   
64 #define TRX_GZ_FILES    0x2     /* Contains up to TRX_MAX_OFFSET individual gzip files */
65 #define TRX_MAX_OFFSET  3
66
67 struct trx_header {
68         u32 magic;              /* "HDR0" */
69         u32 len;                /* Length of file including header */
70         u32 crc32;              /* 32-bit CRC from flag_version to end of file */
71         u32 flag_version;       /* 0:15 flags, 16:31 version */
72         u32 offsets[TRX_MAX_OFFSET];    /* Offsets of partitions from start of header */
73 };
74
75 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
76 #define NVRAM_SPACE 0x8000
77 #define WINDOW_ADDR 0x1fc00000
78 #define WINDOW_SIZE 0x400000
79 #define BUSWIDTH 2
80
81 static struct mtd_info *adm5120_mtd;
82
83 static struct map_info adm5120_map = {
84         name: "adm5120 physically mapped flash",
85         size: WINDOW_SIZE,
86         bankwidth: BUSWIDTH,
87         phys: WINDOW_ADDR,
88 };
89
90 #ifdef CONFIG_MTD_PARTITIONS
91
92 static struct mtd_partition adm5120_cfe_parts[] = {
93         { name: "cfe",  offset: 0, size: 0, mask_flags: MTD_WRITEABLE, },
94         { name: "linux", offset: 0, size: 0, },
95         { name: "rootfs", offset: 0, size: 0, },
96         { name: "nvram", offset: 0, size: 0, },
97         { name: "OpenWrt", offset: 0, size: 0, },
98         { name: NULL, },
99 };
100
101 static int __init
102 find_cfe_size(struct mtd_info *mtd, size_t size)
103 {
104         struct trx_header *trx;
105         unsigned char buf[512];
106         int off;
107         size_t len;
108         int blocksize;
109
110         trx = (struct trx_header *) buf;
111
112         blocksize = mtd->erasesize;
113         if (blocksize < 0x10000)
114                 blocksize = 0x10000;
115
116         for (off = (128*1024); off < size; off += blocksize) {
117                 memset(buf, 0xe5, sizeof(buf));
118
119                 /*
120                  * Read into buffer 
121                  */
122                 if (mtd->read(mtd, off, sizeof(buf), &len, buf) ||
123                     len != sizeof(buf))
124                         continue;
125
126                 /* found a TRX header */
127                 if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
128                         goto found;
129                 }
130         }
131
132         printk(KERN_NOTICE
133                "%s: Couldn't find bootloader size\n",
134                mtd->name);
135         return -1;
136
137  found:
138         printk(KERN_NOTICE "bootloader size: %d\n", off);
139         return off;
140
141 }
142
143 /*
144  * Copied from mtdblock.c
145  *
146  * Cache stuff...
147  * 
148  * Since typical flash erasable sectors are much larger than what Linux's
149  * buffer cache can handle, we must implement read-modify-write on flash
150  * sectors for each block write requests.  To avoid over-erasing flash sectors
151  * and to speed things up, we locally cache a whole flash sector while it is
152  * being written to until a different sector is required.
153  */
154
155 static void erase_callback(struct erase_info *done)
156 {
157         wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
158         wake_up(wait_q);
159 }
160
161 static int erase_write (struct mtd_info *mtd, unsigned long pos, 
162                         int len, const char *buf)
163 {
164         struct erase_info erase;
165         DECLARE_WAITQUEUE(wait, current);
166         wait_queue_head_t wait_q;
167         size_t retlen;
168         int ret;
169
170         /*
171          * First, let's erase the flash block.
172          */
173
174         init_waitqueue_head(&wait_q);
175         erase.mtd = mtd;
176         erase.callback = erase_callback;
177         erase.addr = pos;
178         erase.len = len;
179         erase.priv = (u_long)&wait_q;
180
181         set_current_state(TASK_INTERRUPTIBLE);
182         add_wait_queue(&wait_q, &wait);
183
184         ret = mtd->erase(mtd, &erase);
185         if (ret) {
186                 set_current_state(TASK_RUNNING);
187                 remove_wait_queue(&wait_q, &wait);
188                 printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
189                                      "on \"%s\" failed\n",
190                         pos, len, mtd->name);
191                 return ret;
192         }
193
194         schedule();  /* Wait for erase to finish. */
195         remove_wait_queue(&wait_q, &wait);
196
197         /*
198          * Next, writhe data to flash.
199          */
200
201         ret = mtd->write (mtd, pos, len, &retlen, buf);
202         if (ret)
203                 return ret;
204         if (retlen != len)
205                 return -EIO;
206         return 0;
207 }
208
209
210
211
212 static int __init
213 find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
214 {
215         struct trx_header trx, *trx2;
216         unsigned char buf[512], *block;
217         int off, blocksize;
218         u32 i, crc = ~0;
219         size_t len;
220         struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
221
222         blocksize = mtd->erasesize;
223         if (blocksize < 0x10000)
224                 blocksize = 0x10000;
225
226         for (off = (128*1024); off < size; off += blocksize) {
227                 memset(&trx, 0xe5, sizeof(trx));
228
229                 /*
230                  * Read into buffer 
231                  */
232                 if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
233                     len != sizeof(trx))
234                         continue;
235
236                 /* found a TRX header */
237                 if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
238                         part->offset = le32_to_cpu(trx.offsets[2]) ? : 
239                                 le32_to_cpu(trx.offsets[1]);
240                         part->size = le32_to_cpu(trx.len); 
241
242                         part->size -= part->offset;
243                         part->offset += off;
244
245                         goto found;
246                 }
247         }
248
249         printk(KERN_NOTICE
250                "%s: Couldn't find root filesystem\n",
251                mtd->name);
252         return -1;
253
254  found:
255         if (part->size == 0)
256                 return 0;
257         
258         if (mtd->read(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
259                 return 0;
260
261         if (*((__u32 *) buf) == SQUASHFS_MAGIC) {
262                 printk(KERN_INFO "%s: Filesystem type: squashfs, size=0x%x\n", mtd->name, (u32) sb->bytes_used);
263
264                 /* Update the squashfs partition size based on the superblock info */
265                 part->size = sb->bytes_used;
266                 len = part->offset + part->size;
267                 len +=  (mtd->erasesize - 1);
268                 len &= ~(mtd->erasesize - 1);
269                 part->size = len - part->offset;
270         } else if (*((__u16 *) buf) == JFFS2_MAGIC_BITMASK) {
271                 printk(KERN_INFO "%s: Filesystem type: jffs2\n", mtd->name);
272
273                 /* Move the squashfs outside of the trx */
274                 part->size = 0;
275         } else {
276                 printk(KERN_INFO "%s: Filesystem type: unknown\n", mtd->name);
277                 return 0;
278         }
279
280         if (trx.len != part->offset + part->size - off) {
281                 /* Update the trx offsets and length */
282                 trx.len = part->offset + part->size - off;
283         
284                 /* Update the trx crc32 */
285                 for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
286                         if (mtd->read(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
287                                 return 0;
288                         crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
289                 }
290                 trx.crc32 = crc;
291
292                 /* read first eraseblock from the trx */
293                 block = kmalloc(mtd->erasesize, GFP_KERNEL);
294                 trx2 = (struct trx_header *) block;
295                 if (mtd->read(mtd, off, mtd->erasesize, &len, block) || len != mtd->erasesize) {
296                         printk("Error accessing the first trx eraseblock\n");
297                         return 0;
298                 }
299                 
300                 printk("Updating TRX offsets and length:\n");
301                 printk("old trx = [0x%08x, 0x%08x, 0x%08x], len=0x%08x crc32=0x%08x\n", trx2->offsets[0], trx2->offsets[1], trx2->offsets[2], trx2->len, trx2->crc32);
302                 printk("new trx = [0x%08x, 0x%08x, 0x%08x], len=0x%08x crc32=0x%08x\n",   trx.offsets[0],   trx.offsets[1],   trx.offsets[2],   trx.len, trx.crc32);
303
304                 /* Write updated trx header to the flash */
305                 memcpy(block, &trx, sizeof(trx));
306                 if (mtd->unlock)
307                         mtd->unlock(mtd, off, mtd->erasesize);
308                 erase_write(mtd, off, mtd->erasesize, block);
309                 if (mtd->sync)
310                         mtd->sync(mtd);
311                 kfree(block);
312                 printk("Done\n");
313         }
314         
315         return part->size;
316 }
317
318 struct mtd_partition * __init
319 init_mtd_partitions(struct mtd_info *mtd, size_t size)
320 {
321         int cfe_size;
322
323         if ((cfe_size = find_cfe_size(mtd,size)) < 0)
324                 return NULL;
325
326         /* boot loader */
327         adm5120_cfe_parts[0].offset = 0;
328         adm5120_cfe_parts[0].size   = cfe_size;
329
330         /* nvram */
331         if (cfe_size != 384 * 1024) {
332                 adm5120_cfe_parts[3].offset = size - ROUNDUP(NVRAM_SPACE, mtd->erasesize);
333                 adm5120_cfe_parts[3].size   = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
334         } else {
335                 /* nvram (old 128kb config partition on netgear wgt634u) */
336                 adm5120_cfe_parts[3].offset = adm5120_cfe_parts[0].size;
337                 adm5120_cfe_parts[3].size   = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
338         }
339
340         /* linux (kernel and rootfs) */
341         if (cfe_size != 384 * 1024) {
342                 adm5120_cfe_parts[1].offset = adm5120_cfe_parts[0].size;
343                 adm5120_cfe_parts[1].size   = adm5120_cfe_parts[3].offset - 
344                         adm5120_cfe_parts[1].offset;
345         } else {
346                 /* do not count the elf loader, which is on one block */
347                 adm5120_cfe_parts[1].offset = adm5120_cfe_parts[0].size + 
348                         adm5120_cfe_parts[3].size + mtd->erasesize;
349                 adm5120_cfe_parts[1].size   = size - 
350                         adm5120_cfe_parts[0].size - 
351                         (2*adm5120_cfe_parts[3].size) - 
352                         mtd->erasesize;
353         }
354
355         /* find and size rootfs */
356         if (find_root(mtd,size,&adm5120_cfe_parts[2])==0) {
357                 /* entirely jffs2 */
358                 adm5120_cfe_parts[4].name = NULL;
359                 adm5120_cfe_parts[2].size = size - adm5120_cfe_parts[2].offset - 
360                                 adm5120_cfe_parts[3].size;
361         } else {
362                 /* legacy setup */
363                 /* calculate leftover flash, and assign it to the jffs2 partition */
364                 if (cfe_size != 384 * 1024) {
365                         adm5120_cfe_parts[4].offset = adm5120_cfe_parts[2].offset + 
366                                 adm5120_cfe_parts[2].size;
367                         if ((adm5120_cfe_parts[4].offset % mtd->erasesize) > 0) {
368                                 adm5120_cfe_parts[4].offset += mtd->erasesize - 
369                                         (adm5120_cfe_parts[4].offset % mtd->erasesize);
370                         }
371                         adm5120_cfe_parts[4].size = adm5120_cfe_parts[3].offset - 
372                                 adm5120_cfe_parts[4].offset;
373                 } else {
374                         adm5120_cfe_parts[4].offset = adm5120_cfe_parts[2].offset + 
375                                 adm5120_cfe_parts[2].size;
376                         if ((adm5120_cfe_parts[4].offset % mtd->erasesize) > 0) {
377                                 adm5120_cfe_parts[4].offset += mtd->erasesize - 
378                                         (adm5120_cfe_parts[4].offset % mtd->erasesize);
379                         }
380                         adm5120_cfe_parts[4].size = size - adm5120_cfe_parts[3].size - 
381                                 adm5120_cfe_parts[4].offset;
382                 }
383         }
384
385         return adm5120_cfe_parts;
386 }
387 #endif
388
389 int __init init_adm5120_map(void)
390 {
391         size_t size;
392         int ret = 0;
393 #if defined (CONFIG_MTD_PARTITIONS) || (CONFIG_MTD_MYLOADER_PARTS)
394         struct mtd_partition *parts;
395         int i, parsed_nr_parts = 0;
396 #endif
397         printk("adm5120 : flash init : 0x%08x 0x%08x\n", WINDOW_ADDR, WINDOW_SIZE);
398         adm5120_map.virt = ioremap_nocache(WINDOW_ADDR, WINDOW_SIZE);
399
400         if (!adm5120_map.virt) {
401                 printk("Failed to ioremap\n");
402                 return -EIO;
403         }
404         simple_map_init(&adm5120_map);
405         
406         if (!(adm5120_mtd = do_map_probe("cfi_probe", &adm5120_map))) {
407                 printk("Failed to do_map_probe\n");
408                 iounmap((void *)adm5120_map.virt);
409                 return -ENXIO;
410         }
411
412         adm5120_mtd->owner = THIS_MODULE;
413
414         size = adm5120_mtd->size;
415
416         printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, WINDOW_ADDR);
417
418 #ifdef CONFIG_MTD_PARTITIONS
419
420         if (adm5120_info.boot_loader == BOOT_LOADER_CFE)
421         {
422                 printk(KERN_NOTICE "adm5120 : using CFE flash mapping\n");
423                 parts = init_mtd_partitions(adm5120_mtd, size);
424         
425                 for (i = 0; parts[i].name; i++);
426                         ret = add_mtd_partitions(adm5120_mtd, parts, i);
427         
428                 if (ret) {
429                         printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
430                         goto fail;
431                 }
432         }
433 #endif
434 #ifdef CONFIG_MTD_MYLOADER_PARTS
435         if (adm5120_info.boot_loader == BOOT_LOADER_MYLOADER)
436         {
437                 printk(KERN_NOTICE "adm5120 : using MyLoader flash mapping\n");
438                 char *part_type;
439                 
440                 if (parsed_nr_parts == 0) {
441                         ret = parse_myloader_partitions(adm5120_mtd, &parts, 0);
442
443                         if (ret  > 0) {
444                                 part_type ="MyLoader";
445                                 parsed_nr_parts = ret;
446                         }
447                 }
448                 ret = add_mtd_partitions(adm5120_mtd, parts, parsed_nr_parts);
449
450                 if (ret) {
451                         printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
452                         goto fail;
453                 }
454         }
455 #endif
456         return 0;
457
458  fail:
459         if (adm5120_mtd)
460                 map_destroy(adm5120_mtd);
461         if (adm5120_map.virt)
462                 iounmap((void *)adm5120_map.virt);
463         adm5120_map.virt = 0;
464         return ret;
465 }
466
467 void __exit cleanup_adm5120_map(void)
468 {
469 #ifdef CONFIG_MTD_PARTITIONS
470         del_mtd_partitions(adm5120_mtd);
471 #endif
472         map_destroy(adm5120_mtd);
473         iounmap((void *)adm5120_map.virt);
474 }
475
476 module_init(init_adm5120_map);
477 module_exit(cleanup_adm5120_map);