0c66603f2e10765cdb543eac5a6865b95d666a15
[openwrt.git] / target / linux / brcm47xx / patches-3.3 / 050-mtd-add-bcm47xx-part-parser.patch
1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -172,6 +172,13 @@ config MTD_MYLOADER_PARTS
4           You will still need the parsing functions to be called by the driver
5           for your particular device. It won't happen automatically.
6  
7 +config MTD_BCM47XX_PARTS
8 +       tristate "BCM47XX partitioning support"
9 +       default y
10 +       depends on BCM47XX
11 +       ---help---
12 +         bcm47XX partitioning support
13 +
14  comment "User Modules And Translation Layers"
15  
16  config MTD_CHAR
17 --- a/drivers/mtd/Makefile
18 +++ b/drivers/mtd/Makefile
19 @@ -13,6 +13,7 @@ obj-$(CONFIG_MTD_AFS_PARTS)   += afs.o
20  obj-$(CONFIG_MTD_AR7_PARTS)    += ar7part.o
21  obj-$(CONFIG_MTD_BCM63XX_PARTS)        += bcm63xxpart.o
22  obj-$(CONFIG_MTD_MYLOADER_PARTS) += myloader.o
23 +obj-$(CONFIG_MTD_BCM47XX_PARTS)        += bcm47xxpart.o
24  
25  # 'Users' - code which presents functionality to userspace.
26  obj-$(CONFIG_MTD_CHAR)         += mtdchar.o
27 --- /dev/null
28 +++ b/drivers/mtd/bcm47xxpart.c
29 @@ -0,0 +1,548 @@
30 +/*
31 + *  Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
32 + *  Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
33 + *  Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
34 + *
35 + *  original functions for finding root filesystem from Mike Baker
36 + *
37 + *  This program is free software; you can redistribute  it and/or modify it
38 + *  under  the terms of  the GNU General  Public License as published by the
39 + *  Free Software Foundation;  either version 2 of the  License, or (at your
40 + *  option) any later version.
41 + *
42 + *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
43 + *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
44 + *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
45 + *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
46 + *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 + *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
48 + *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
49 + *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
50 + *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
51 + *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 + *
53 + *  You should have received a copy of the  GNU General Public License along
54 + *  with this program; if not, write  to the Free Software Foundation, Inc.,
55 + *  675 Mass Ave, Cambridge, MA 02139, USA.
56 + *
57 + *  Copyright 2001-2003, Broadcom Corporation
58 + *  All Rights Reserved.
59 + *
60 + *  THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
61 + *  KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
62 + *  SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
63 + *  FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
64 + *
65 + *  Flash mapping for BCM947XX boards
66 + */
67 +
68 +#define pr_fmt(fmt) "bcm47xx_part: " fmt
69 +#include <linux/init.h>
70 +#include <linux/module.h>
71 +#include <linux/types.h>
72 +#include <linux/kernel.h>
73 +#include <linux/sched.h>
74 +#include <linux/wait.h>
75 +#include <linux/mtd/mtd.h>
76 +#include <linux/mtd/partitions.h>
77 +#include <linux/crc32.h>
78 +#include <linux/io.h>
79 +#include <asm/mach-bcm47xx/nvram.h>
80 +#include <asm/mach-bcm47xx/bcm47xx.h>
81 +#include <asm/fw/cfe/cfe_api.h>
82 +
83 +
84 +#define TRX_MAGIC      0x30524448      /* "HDR0" */
85 +#define TRX_VERSION    1
86 +#define TRX_MAX_LEN    0x3A0000
87 +#define TRX_NO_HEADER  1               /* Do not write TRX header */
88 +#define TRX_GZ_FILES   0x2     /* Contains up to TRX_MAX_OFFSET individual gzip files */
89 +#define TRX_MAX_OFFSET 3
90 +
91 +struct trx_header {
92 +       u32 magic;              /* "HDR0" */
93 +       u32 len;                /* Length of file including header */
94 +       u32 crc32;              /* 32-bit CRC from flag_version to end of file */
95 +       u32 flag_version;       /* 0:15 flags, 16:31 version */
96 +       u32 offsets[TRX_MAX_OFFSET];    /* Offsets of partitions from start of header */
97 +};
98 +
99 +/* for WGT634U search the ELF-header of
100 + * the kernel decompressor instead of TRX
101 + */
102 +#define WGT634U_ELF_MAGIC      0x464c457f /*  ".ELF"  */
103 +
104 +/* for Edimax Print servers which use an additional header
105 + * then the firmware on flash looks like :
106 + * EDIMAX HEADER | TRX HEADER
107 + * As this header is 12 bytes long we have to handle it
108 + * and skip it to find the TRX header
109 + */
110 +#define EDIMAX_PS_HEADER_MAGIC 0x36315350 /*  "PS16"  */
111 +#define EDIMAX_PS_HEADER_LEN   0xc /* 12 bytes long for edimax header */
112 +
113 +#define NVRAM_SPACE 0x8000
114 +
115 +#define ROUTER_NETGEAR_WGR614L         1
116 +#define ROUTER_NETGEAR_WNR834B         2
117 +#define ROUTER_NETGEAR_WNDR3300                3
118 +#define ROUTER_NETGEAR_WNR3500L                4
119 +#define ROUTER_SIMPLETECH_SIMPLESHARE  5
120 +#define ROUTER_NETGEAR_WNDR3400                6
121 +
122 +static int
123 +find_cfe_size(struct mtd_info *mtd)
124 +{
125 +       struct trx_header *trx;
126 +       unsigned char buf[512];
127 +       int off;
128 +       size_t len;
129 +       int blocksize;
130 +
131 +       trx = (struct trx_header *) buf;
132 +
133 +       blocksize = mtd->erasesize;
134 +       if (blocksize < 0x10000)
135 +               blocksize = 0x10000;
136 +
137 +       for (off = (128*1024); off < mtd->size; off += blocksize) {
138 +               memset(buf, 0xe5, sizeof(buf));
139 +
140 +               /*
141 +                * Read into buffer
142 +                */
143 +               if (mtd->read(mtd, off, sizeof(buf), &len, buf) ||
144 +                   len != sizeof(buf))
145 +                       continue;
146 +
147 +               if (le32_to_cpu(trx->magic) == EDIMAX_PS_HEADER_MAGIC) {
148 +                       if (mtd->read(mtd, off + EDIMAX_PS_HEADER_LEN,
149 +                           sizeof(buf), &len, buf) || len != sizeof(buf)) {
150 +                               continue;
151 +                       } else {
152 +                               pr_notice("Found edimax header\n");
153 +                       }
154 +               }
155 +
156 +               /* found a TRX header */
157 +               if (le32_to_cpu(trx->magic) == TRX_MAGIC ||
158 +                   le32_to_cpu(trx->magic) == WGT634U_ELF_MAGIC)
159 +                       goto found;
160 +       }
161 +
162 +       pr_notice("%s: Couldn't find bootloader size\n", mtd->name);
163 +       return -1;
164 +
165 + found:
166 +       pr_notice("bootloader size: %d\n", off);
167 +       return off;
168 +
169 +}
170 +
171 +/*
172 + * Copied from mtdblock.c
173 + *
174 + * Cache stuff...
175 + *
176 + * Since typical flash erasable sectors are much larger than what Linux's
177 + * buffer cache can handle, we must implement read-modify-write on flash
178 + * sectors for each block write requests.  To avoid over-erasing flash sectors
179 + * and to speed things up, we locally cache a whole flash sector while it is
180 + * being written to until a different sector is required.
181 + */
182 +
183 +static void erase_callback(struct erase_info *done)
184 +{
185 +       wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
186 +       wake_up(wait_q);
187 +}
188 +
189 +static int erase_write(struct mtd_info *mtd, unsigned long pos,
190 +                       int len, const char *buf)
191 +{
192 +       struct erase_info erase;
193 +       DECLARE_WAITQUEUE(wait, current);
194 +       wait_queue_head_t wait_q;
195 +       size_t retlen;
196 +       int ret;
197 +
198 +       /*
199 +        * First, let's erase the flash block.
200 +        */
201 +
202 +       init_waitqueue_head(&wait_q);
203 +       erase.mtd = mtd;
204 +       erase.callback = erase_callback;
205 +       erase.addr = pos;
206 +       erase.len = len;
207 +       erase.priv = (u_long)&wait_q;
208 +
209 +       set_current_state(TASK_INTERRUPTIBLE);
210 +       add_wait_queue(&wait_q, &wait);
211 +
212 +       ret = mtd->erase(mtd, &erase);
213 +       if (ret) {
214 +               set_current_state(TASK_RUNNING);
215 +               remove_wait_queue(&wait_q, &wait);
216 +               pr_warn("erase of region [0x%lx, 0x%x] on \"%s\" failed\n",
217 +                       pos, len, mtd->name);
218 +               return ret;
219 +       }
220 +
221 +       schedule();  /* Wait for erase to finish. */
222 +       remove_wait_queue(&wait_q, &wait);
223 +
224 +       /*
225 +        * Next, write data to flash.
226 +        */
227 +
228 +       ret = mtd->write(mtd, pos, len, &retlen, buf);
229 +       if (ret)
230 +               return ret;
231 +       if (retlen != len)
232 +               return -EIO;
233 +       return 0;
234 +}
235 +
236 +
237 +static int
238 +find_dual_image_off(struct mtd_info *mtd)
239 +{
240 +       struct trx_header trx;
241 +       int off, blocksize;
242 +       size_t len;
243 +
244 +       blocksize = mtd->erasesize;
245 +       if (blocksize < 0x10000)
246 +               blocksize = 0x10000;
247 +
248 +       for (off = (128*1024); off < mtd->size; off += blocksize) {
249 +               memset(&trx, 0xe5, sizeof(trx));
250 +               /*
251 +               * Read into buffer
252 +               */
253 +               if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
254 +                   len != sizeof(trx))
255 +                       continue;
256 +               /* found last TRX header */
257 +               if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
258 +                       if (le32_to_cpu(trx.flag_version >> 16) == 2) {
259 +                               pr_notice("dual image TRX header found\n");
260 +                               return mtd->size / 2;
261 +                       } else {
262 +                               return 0;
263 +                       }
264 +               }
265 +       }
266 +       return 0;
267 +}
268 +
269 +
270 +static int
271 +find_root(struct mtd_info *mtd, struct mtd_partition *part)
272 +{
273 +       struct trx_header trx, *trx2;
274 +       unsigned char buf[512], *block;
275 +       int off, blocksize, trxoff = 0;
276 +       u32 i, crc = ~0;
277 +       size_t len;
278 +       bool edimax = false;
279 +
280 +       blocksize = mtd->erasesize;
281 +       if (blocksize < 0x10000)
282 +               blocksize = 0x10000;
283 +
284 +       for (off = (128*1024); off < mtd->size; off += blocksize) {
285 +               memset(&trx, 0xe5, sizeof(trx));
286 +
287 +               /*
288 +                * Read into buffer
289 +                */
290 +               if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
291 +                   len != sizeof(trx))
292 +                       continue;
293 +
294 +               /* found an edimax header */
295 +               if (le32_to_cpu(trx.magic) == EDIMAX_PS_HEADER_MAGIC) {
296 +                       /* read the correct trx header */
297 +                       if (mtd->read(mtd, off + EDIMAX_PS_HEADER_LEN,
298 +                           sizeof(trx), &len, (char *) &trx) ||
299 +                           len != sizeof(trx)) {
300 +                               continue;
301 +                       } else {
302 +                               pr_notice("Found an edimax ps header\n");
303 +                               edimax = true;
304 +                       }
305 +               }
306 +
307 +               /* found a TRX header */
308 +               if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
309 +                       part->offset = le32_to_cpu(trx.offsets[2]) ? :
310 +                               le32_to_cpu(trx.offsets[1]);
311 +                       part->size = le32_to_cpu(trx.len);
312 +
313 +                       part->size -= part->offset;
314 +                       part->offset += off;
315 +                       if (edimax) {
316 +                               off += EDIMAX_PS_HEADER_LEN;
317 +                               trxoff = EDIMAX_PS_HEADER_LEN;
318 +                       }
319 +
320 +                       goto found;
321 +               }
322 +       }
323 +
324 +       pr_warn("%s: Couldn't find root filesystem\n",
325 +              mtd->name);
326 +       return -1;
327 +
328 + found:
329 +       pr_notice("TRX offset : %x\n", trxoff);
330 +       if (part->size == 0)
331 +               return 0;
332 +
333 +       if (mtd->read(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
334 +               return 0;
335 +
336 +       /* Move the fs outside of the trx */
337 +       part->size = 0;
338 +
339 +       if (trx.len != part->offset + part->size - off) {
340 +               /* Update the trx offsets and length */
341 +               trx.len = part->offset + part->size - off;
342 +
343 +               /* Update the trx crc32 */
344 +               for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
345 +                       if (mtd->read(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
346 +                               return 0;
347 +                       crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
348 +               }
349 +               trx.crc32 = crc;
350 +
351 +               /* read first eraseblock from the trx */
352 +               block = kmalloc(mtd->erasesize, GFP_KERNEL);
353 +               trx2 = (struct trx_header *) block;
354 +               if (mtd->read(mtd, off - trxoff, mtd->erasesize, &len, block) || len != mtd->erasesize) {
355 +                       pr_err("Error accessing the first trx eraseblock\n");
356 +                       return 0;
357 +               }
358 +
359 +               pr_notice("Updating TRX offsets and length:\n");
360 +               pr_notice("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);
361 +               pr_notice("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);
362 +
363 +               /* Write updated trx header to the flash */
364 +               memcpy(block + trxoff, &trx, sizeof(trx));
365 +               if (mtd->unlock)
366 +                       mtd->unlock(mtd, off - trxoff, mtd->erasesize);
367 +               erase_write(mtd, off - trxoff, mtd->erasesize, block);
368 +               if (mtd->sync)
369 +                       mtd->sync(mtd);
370 +               kfree(block);
371 +               pr_notice("Done\n");
372 +       }
373 +
374 +       return part->size;
375 +}
376 +
377 +static int get_router(void)
378 +{
379 +       char buf[20];
380 +       u32 boardnum = 0;
381 +       u16 boardtype = 0;
382 +       u16 boardrev = 0;
383 +       u32 boardflags = 0;
384 +       u16 sdram_init = 0;
385 +       u16 cardbus = 0;
386 +       u16 strev = 0;
387 +
388 +       if (nvram_getenv("boardnum", buf, sizeof(buf)) >= 0)
389 +               boardnum = simple_strtoul(buf, NULL, 0);
390 +       if (nvram_getenv("boardtype", buf, sizeof(buf)) >= 0)
391 +               boardtype = simple_strtoul(buf, NULL, 0);
392 +       if (nvram_getenv("boardrev", buf, sizeof(buf)) >= 0)
393 +               boardrev = simple_strtoul(buf, NULL, 0);
394 +       if (nvram_getenv("boardflags", buf, sizeof(buf)) >= 0)
395 +               boardflags = simple_strtoul(buf, NULL, 0);
396 +       if (nvram_getenv("sdram_init", buf, sizeof(buf)) >= 0)
397 +               sdram_init = simple_strtoul(buf, NULL, 0);
398 +       if (nvram_getenv("cardbus", buf, sizeof(buf)) >= 0)
399 +               cardbus = simple_strtoul(buf, NULL, 0);
400 +       if (nvram_getenv("st_rev", buf, sizeof(buf)) >= 0)
401 +               strev = simple_strtoul(buf, NULL, 0);
402 +
403 +       if ((boardnum == 8 || boardnum == 01)
404 +         && boardtype == 0x0472 && cardbus == 1) {
405 +               /* Netgear WNR834B, Netgear WNR834Bv2 */
406 +               return ROUTER_NETGEAR_WNR834B;
407 +       }
408 +
409 +       if (boardnum == 01 && boardtype == 0x0472 && boardrev == 0x23) {
410 +               /* Netgear WNDR-3300 */
411 +               return ROUTER_NETGEAR_WNDR3300;
412 +       }
413 +
414 +       if ((boardnum == 83258 || boardnum == 01)
415 +         && boardtype == 0x048e
416 +         && (boardrev == 0x11 || boardrev == 0x10)
417 +         && boardflags == 0x750
418 +         && sdram_init == 0x000A) {
419 +               /* Netgear WGR614v8/L/WW 16MB ram, cfe v1.3 or v1.5 */
420 +               return ROUTER_NETGEAR_WGR614L;
421 +       }
422 +
423 +       if ((boardnum == 1 || boardnum == 3500)
424 +         && boardtype == 0x04CF
425 +         && (boardrev == 0x1213 || boardrev == 02)) {
426 +               /* Netgear WNR3500v2/U/L */
427 +               return ROUTER_NETGEAR_WNR3500L;
428 +       }
429 +
430 +       if (boardnum == 1 && boardtype == 0xb4cf && boardrev == 0x1100) {
431 +               /* Netgear WNDR3400 */
432 +               return ROUTER_NETGEAR_WNDR3400;
433 +       }
434 +
435 +       if (boardtype == 0x042f
436 +         && boardrev == 0x10
437 +         && boardflags == 0
438 +         && strev == 0x11) {
439 +               /* Simpletech Simpleshare */
440 +               return ROUTER_SIMPLETECH_SIMPLESHARE;
441 +       }
442 +
443 +       return 0;
444 +}
445 +
446 +static int parse_bcm47xx_partitions(struct mtd_info *mtd,
447 +                                   struct mtd_partition **pparts,
448 +                                   struct mtd_part_parser_data *data)
449 +{
450 +       int cfe_size;
451 +       int dual_image_offset = 0;
452 +       /* e.g Netgear 0x003e0000-0x003f0000 : "board_data", we exclude this
453 +        * part from our mapping to prevent overwriting len/checksum on e.g.
454 +        * Netgear WGR614v8/L/WW
455 +        */
456 +       int custom_data_size = 0;
457 +       struct mtd_partition *bcm47xx_parts;
458 +
459 +       cfe_size = find_cfe_size(mtd);
460 +       if (cfe_size < 0)
461 +               return 0;
462 +
463 +       bcm47xx_parts = kzalloc(sizeof(struct mtd_partition) * 6, GFP_KERNEL);
464 +
465 +       bcm47xx_parts[0].name = "cfe";
466 +       bcm47xx_parts[1].name = "linux";
467 +       bcm47xx_parts[2].name = "rootfs";
468 +       bcm47xx_parts[3].name = "nvram";
469 +
470 +       /* boot loader */
471 +       bcm47xx_parts[0].mask_flags = MTD_WRITEABLE;
472 +       bcm47xx_parts[0].offset = 0;
473 +       bcm47xx_parts[0].size   = cfe_size;
474 +
475 +       /* nvram */
476 +       if (cfe_size != 384 * 1024) {
477 +
478 +               switch (get_router()) {
479 +               case ROUTER_NETGEAR_WGR614L:
480 +               case ROUTER_NETGEAR_WNR834B:
481 +               case ROUTER_NETGEAR_WNDR3300:
482 +               case ROUTER_NETGEAR_WNR3500L:
483 +               case ROUTER_NETGEAR_WNDR3400:
484 +                       /* Netgear: checksum is @ 0x003AFFF8 for 4M flash or checksum
485 +                        * is @ 0x007AFFF8 for 8M flash
486 +                        */
487 +                       custom_data_size = mtd->erasesize;
488 +
489 +                       bcm47xx_parts[3].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize);
490 +                       bcm47xx_parts[3].size   = roundup(NVRAM_SPACE, mtd->erasesize);
491 +
492 +                       /* Place CFE board_data into a partition */
493 +                       bcm47xx_parts[4].name = "board_data";
494 +                       bcm47xx_parts[4].offset = bcm47xx_parts[3].offset - custom_data_size;
495 +                       bcm47xx_parts[4].size   = custom_data_size;
496 +                       break;
497 +
498 +               case ROUTER_SIMPLETECH_SIMPLESHARE:
499 +                       /* Fixup Simpletech Simple share nvram  */
500 +
501 +                       pr_notice("Setting up simpletech nvram\n");
502 +                       custom_data_size = mtd->erasesize;
503 +
504 +                       bcm47xx_parts[3].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize) * 2;
505 +                       bcm47xx_parts[3].size   = roundup(NVRAM_SPACE, mtd->erasesize);
506 +
507 +                       /* Place backup nvram into a partition */
508 +                       bcm47xx_parts[4].name = "nvram_copy";
509 +                       bcm47xx_parts[4].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize);
510 +                       bcm47xx_parts[4].size   = roundup(NVRAM_SPACE, mtd->erasesize);
511 +                       break;
512 +
513 +               default:
514 +                       bcm47xx_parts[3].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize);
515 +                       bcm47xx_parts[3].size   = roundup(NVRAM_SPACE, mtd->erasesize);
516 +               }
517 +
518 +       } else {
519 +               /* nvram (old 128kb config partition on netgear wgt634u) */
520 +               bcm47xx_parts[3].offset = bcm47xx_parts[0].size;
521 +               bcm47xx_parts[3].size   = roundup(NVRAM_SPACE, mtd->erasesize);
522 +       }
523 +
524 +       /* dual image offset*/
525 +       pr_notice("Looking for dual image\n");
526 +       dual_image_offset = find_dual_image_off(mtd);
527 +       /* linux (kernel and rootfs) */
528 +       if (cfe_size != 384 * 1024) {
529 +               if (get_router() == ROUTER_SIMPLETECH_SIMPLESHARE) {
530 +                       bcm47xx_parts[1].offset = bcm47xx_parts[0].size;
531 +                       bcm47xx_parts[1].size   = bcm47xx_parts[4].offset - dual_image_offset -
532 +                               bcm47xx_parts[1].offset - custom_data_size;
533 +               } else {
534 +                       bcm47xx_parts[1].offset = bcm47xx_parts[0].size;
535 +                       bcm47xx_parts[1].size   = bcm47xx_parts[3].offset - dual_image_offset -
536 +                               bcm47xx_parts[1].offset - custom_data_size;
537 +               }
538 +       } else {
539 +               /* do not count the elf loader, which is on one block */
540 +               bcm47xx_parts[1].offset = bcm47xx_parts[0].size +
541 +                       bcm47xx_parts[3].size + mtd->erasesize;
542 +               bcm47xx_parts[1].size   = mtd->size -
543 +                       bcm47xx_parts[0].size -
544 +                       (2*bcm47xx_parts[3].size) -
545 +                       mtd->erasesize - custom_data_size;
546 +       }
547 +
548 +       /* find and size rootfs */
549 +       find_root(mtd, &bcm47xx_parts[2]);
550 +       bcm47xx_parts[2].size = mtd->size - dual_image_offset -
551 +                               bcm47xx_parts[2].offset -
552 +                               bcm47xx_parts[3].size - custom_data_size;
553 +       *pparts = bcm47xx_parts;
554 +       return bcm47xx_parts[4].name == NULL ? 4 : 5;
555 +}
556 +
557 +static struct mtd_part_parser bcm47xx_parser = {
558 +       .owner = THIS_MODULE,
559 +       .parse_fn = parse_bcm47xx_partitions,
560 +       .name = "bcm47xx",
561 +};
562 +
563 +static int __init bcm47xx_parser_init(void)
564 +{
565 +       return register_mtd_parser(&bcm47xx_parser);
566 +}
567 +
568 +static void __exit bcm47xx_parser_exit(void)
569 +{
570 +       deregister_mtd_parser(&bcm47xx_parser);
571 +}
572 +
573 +module_init(bcm47xx_parser_init);
574 +module_exit(bcm47xx_parser_exit);
575 +
576 +MODULE_LICENSE("GPL");
577 +MODULE_DESCRIPTION("Parsing code for flash partitions on bcm47xx SoCs");