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.
7 +config MTD_BCM47XX_PARTS
8 + tristate "BCM47XX partitioning support"
12 + bcm47XX partitioning support
14 comment "User Modules And Translation Layers"
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
25 # 'Users' - code which presents functionality to userspace.
26 obj-$(CONFIG_MTD_CHAR) += mtdchar.o
28 +++ b/drivers/mtd/bcm47xxpart.c
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)
35 + * original functions for finding root filesystem from Mike Baker
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.
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.
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.
57 + * Copyright 2001-2003, Broadcom Corporation
58 + * All Rights Reserved.
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.
65 + * Flash mapping for BCM947XX boards
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>
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
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 */
99 +/* for Edimax Print servers which use an additional header
100 + * then the firmware on flash looks like :
101 + * EDIMAX HEADER | TRX HEADER
102 + * As this header is 12 bytes long we have to handle it
103 + * and skip it to find the TRX header
105 +#define EDIMAX_PS_HEADER_MAGIC 0x36315350 /* "PS16" */
106 +#define EDIMAX_PS_HEADER_LEN 0xc /* 12 bytes long for edimax header */
108 +#define NVRAM_SPACE 0x8000
110 +#define ROUTER_NETGEAR_WGR614L 1
111 +#define ROUTER_NETGEAR_WNR834B 2
112 +#define ROUTER_NETGEAR_WNDR3300 3
113 +#define ROUTER_NETGEAR_WNR3500L 4
114 +#define ROUTER_SIMPLETECH_SIMPLESHARE 5
115 +#define ROUTER_NETGEAR_WNDR3400 6
118 +find_cfe_size(struct mtd_info *mtd)
120 + struct trx_header *trx;
121 + unsigned char buf[512];
126 + trx = (struct trx_header *) buf;
128 + blocksize = mtd->erasesize;
129 + if (blocksize < 0x10000)
130 + blocksize = 0x10000;
132 + for (off = (128*1024); off < mtd->size; off += blocksize) {
133 + memset(buf, 0xe5, sizeof(buf));
138 + if (mtd_read(mtd, off, sizeof(buf), &len, buf) ||
139 + len != sizeof(buf))
142 + if (le32_to_cpu(trx->magic) == EDIMAX_PS_HEADER_MAGIC) {
143 + if (mtd_read(mtd, off + EDIMAX_PS_HEADER_LEN,
144 + sizeof(buf), &len, buf) || len != sizeof(buf)) {
147 + pr_notice("Found edimax header\n");
151 + /* found a TRX header */
152 + if (le32_to_cpu(trx->magic) == TRX_MAGIC)
156 + pr_notice("%s: Couldn't find bootloader size\n", mtd->name);
160 + pr_notice("bootloader size: %d\n", off);
166 + * Copied from mtdblock.c
170 + * Since typical flash erasable sectors are much larger than what Linux's
171 + * buffer cache can handle, we must implement read-modify-write on flash
172 + * sectors for each block write requests. To avoid over-erasing flash sectors
173 + * and to speed things up, we locally cache a whole flash sector while it is
174 + * being written to until a different sector is required.
177 +static void erase_callback(struct erase_info *done)
179 + wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
183 +static int erase_write(struct mtd_info *mtd, unsigned long pos,
184 + int len, const char *buf)
186 + struct erase_info erase;
187 + DECLARE_WAITQUEUE(wait, current);
188 + wait_queue_head_t wait_q;
193 + * First, let's erase the flash block.
196 + init_waitqueue_head(&wait_q);
198 + erase.callback = erase_callback;
201 + erase.priv = (u_long)&wait_q;
203 + set_current_state(TASK_INTERRUPTIBLE);
204 + add_wait_queue(&wait_q, &wait);
206 + ret = mtd_erase(mtd, &erase);
208 + set_current_state(TASK_RUNNING);
209 + remove_wait_queue(&wait_q, &wait);
210 + pr_warn("erase of region [0x%lx, 0x%x] on \"%s\" failed\n",
211 + pos, len, mtd->name);
215 + schedule(); /* Wait for erase to finish. */
216 + remove_wait_queue(&wait_q, &wait);
219 + * Next, write data to flash.
222 + ret = mtd_write(mtd, pos, len, &retlen, buf);
232 +find_dual_image_off(struct mtd_info *mtd)
234 + struct trx_header trx;
235 + int off, blocksize;
238 + blocksize = mtd->erasesize;
239 + if (blocksize < 0x10000)
240 + blocksize = 0x10000;
242 + for (off = (128*1024); off < mtd->size; off += blocksize) {
243 + memset(&trx, 0xe5, sizeof(trx));
247 + if (mtd_read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
248 + len != sizeof(trx))
250 + /* found last TRX header */
251 + if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
252 + if (le32_to_cpu(trx.flag_version >> 16) == 2) {
253 + pr_notice("dual image TRX header found\n");
254 + return mtd->size / 2;
265 +find_root(struct mtd_info *mtd, struct mtd_partition *part)
267 + struct trx_header trx, *trx2;
268 + unsigned char buf[512], *block;
269 + int off, blocksize, trxoff = 0;
272 + bool edimax = false;
274 + blocksize = mtd->erasesize;
275 + if (blocksize < 0x10000)
276 + blocksize = 0x10000;
278 + for (off = (128*1024); off < mtd->size; off += blocksize) {
279 + memset(&trx, 0xe5, sizeof(trx));
284 + if (mtd_read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
285 + len != sizeof(trx))
288 + /* found an edimax header */
289 + if (le32_to_cpu(trx.magic) == EDIMAX_PS_HEADER_MAGIC) {
290 + /* read the correct trx header */
291 + if (mtd_read(mtd, off + EDIMAX_PS_HEADER_LEN,
292 + sizeof(trx), &len, (char *) &trx) ||
293 + len != sizeof(trx)) {
296 + pr_notice("Found an edimax ps header\n");
301 + /* found a TRX header */
302 + if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
303 + part->offset = le32_to_cpu(trx.offsets[2]) ? :
304 + le32_to_cpu(trx.offsets[1]);
305 + part->size = le32_to_cpu(trx.len);
307 + part->size -= part->offset;
308 + part->offset += off;
310 + off += EDIMAX_PS_HEADER_LEN;
311 + trxoff = EDIMAX_PS_HEADER_LEN;
318 + pr_warn("%s: Couldn't find root filesystem\n",
323 + pr_notice("TRX offset : %x\n", trxoff);
324 + if (part->size == 0)
327 + if (mtd_read(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
330 + /* Move the fs outside of the trx */
333 + if (trx.len != part->offset + part->size - off) {
334 + /* Update the trx offsets and length */
335 + trx.len = part->offset + part->size - off;
337 + /* Update the trx crc32 */
338 + for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
339 + if (mtd_read(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
341 + crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
345 + /* read first eraseblock from the trx */
346 + block = kmalloc(mtd->erasesize, GFP_KERNEL);
347 + trx2 = (struct trx_header *) block;
348 + if (mtd_read(mtd, off - trxoff, mtd->erasesize, &len, block) || len != mtd->erasesize) {
349 + pr_err("Error accessing the first trx eraseblock\n");
353 + pr_notice("Updating TRX offsets and length:\n");
354 + 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);
355 + 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);
357 + /* Write updated trx header to the flash */
358 + memcpy(block + trxoff, &trx, sizeof(trx));
360 + mtd->_unlock(mtd, off - trxoff, mtd->erasesize);
361 + erase_write(mtd, off - trxoff, mtd->erasesize, block);
364 + pr_notice("Done\n");
370 +static int get_router(void)
376 + u32 boardflags = 0;
377 + u16 sdram_init = 0;
381 + if (nvram_getenv("boardnum", buf, sizeof(buf)) >= 0)
382 + boardnum = simple_strtoul(buf, NULL, 0);
383 + if (nvram_getenv("boardtype", buf, sizeof(buf)) >= 0)
384 + boardtype = simple_strtoul(buf, NULL, 0);
385 + if (nvram_getenv("boardrev", buf, sizeof(buf)) >= 0)
386 + boardrev = simple_strtoul(buf, NULL, 0);
387 + if (nvram_getenv("boardflags", buf, sizeof(buf)) >= 0)
388 + boardflags = simple_strtoul(buf, NULL, 0);
389 + if (nvram_getenv("sdram_init", buf, sizeof(buf)) >= 0)
390 + sdram_init = simple_strtoul(buf, NULL, 0);
391 + if (nvram_getenv("cardbus", buf, sizeof(buf)) >= 0)
392 + cardbus = simple_strtoul(buf, NULL, 0);
393 + if (nvram_getenv("st_rev", buf, sizeof(buf)) >= 0)
394 + strev = simple_strtoul(buf, NULL, 0);
396 + if ((boardnum == 8 || boardnum == 01)
397 + && boardtype == 0x0472 && cardbus == 1) {
398 + /* Netgear WNR834B, Netgear WNR834Bv2 */
399 + return ROUTER_NETGEAR_WNR834B;
402 + if (boardnum == 01 && boardtype == 0x0472 && boardrev == 0x23) {
403 + /* Netgear WNDR-3300 */
404 + return ROUTER_NETGEAR_WNDR3300;
407 + if ((boardnum == 83258 || boardnum == 01)
408 + && boardtype == 0x048e
409 + && (boardrev == 0x11 || boardrev == 0x10)
410 + && boardflags == 0x750
411 + && sdram_init == 0x000A) {
412 + /* Netgear WGR614v8/L/WW 16MB ram, cfe v1.3 or v1.5 */
413 + return ROUTER_NETGEAR_WGR614L;
416 + if ((boardnum == 1 || boardnum == 3500)
417 + && boardtype == 0x04CF
418 + && (boardrev == 0x1213 || boardrev == 02)) {
419 + /* Netgear WNR3500v2/U/L */
420 + return ROUTER_NETGEAR_WNR3500L;
423 + if (boardnum == 1 && boardtype == 0xb4cf && boardrev == 0x1100) {
424 + /* Netgear WNDR3400 */
425 + return ROUTER_NETGEAR_WNDR3400;
428 + if (boardtype == 0x042f
429 + && boardrev == 0x10
431 + && strev == 0x11) {
432 + /* Simpletech Simpleshare */
433 + return ROUTER_SIMPLETECH_SIMPLESHARE;
439 +static int parse_bcm47xx_partitions(struct mtd_info *mtd,
440 + struct mtd_partition **pparts,
441 + struct mtd_part_parser_data *data)
444 + int dual_image_offset = 0;
445 + /* e.g Netgear 0x003e0000-0x003f0000 : "board_data", we exclude this
446 + * part from our mapping to prevent overwriting len/checksum on e.g.
447 + * Netgear WGR614v8/L/WW
449 + int custom_data_size = 0;
450 + struct mtd_partition *bcm47xx_parts;
452 + cfe_size = find_cfe_size(mtd);
456 + bcm47xx_parts = kzalloc(sizeof(struct mtd_partition) * 6, GFP_KERNEL);
458 + bcm47xx_parts[0].name = "cfe";
459 + bcm47xx_parts[1].name = "linux";
460 + bcm47xx_parts[2].name = "rootfs";
461 + bcm47xx_parts[3].name = "nvram";
464 + bcm47xx_parts[0].mask_flags = MTD_WRITEABLE;
465 + bcm47xx_parts[0].offset = 0;
466 + bcm47xx_parts[0].size = cfe_size;
469 + if (cfe_size != 384 * 1024) {
471 + switch (get_router()) {
472 + case ROUTER_NETGEAR_WGR614L:
473 + case ROUTER_NETGEAR_WNR834B:
474 + case ROUTER_NETGEAR_WNDR3300:
475 + case ROUTER_NETGEAR_WNR3500L:
476 + case ROUTER_NETGEAR_WNDR3400:
477 + /* Netgear: checksum is @ 0x003AFFF8 for 4M flash or checksum
478 + * is @ 0x007AFFF8 for 8M flash
480 + custom_data_size = mtd->erasesize;
482 + bcm47xx_parts[3].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize);
483 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
485 + /* Place CFE board_data into a partition */
486 + bcm47xx_parts[4].name = "board_data";
487 + bcm47xx_parts[4].offset = bcm47xx_parts[3].offset - custom_data_size;
488 + bcm47xx_parts[4].size = custom_data_size;
491 + case ROUTER_SIMPLETECH_SIMPLESHARE:
492 + /* Fixup Simpletech Simple share nvram */
494 + pr_notice("Setting up simpletech nvram\n");
495 + custom_data_size = mtd->erasesize;
497 + bcm47xx_parts[3].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize) * 2;
498 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
500 + /* Place backup nvram into a partition */
501 + bcm47xx_parts[4].name = "nvram_copy";
502 + bcm47xx_parts[4].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize);
503 + bcm47xx_parts[4].size = roundup(NVRAM_SPACE, mtd->erasesize);
507 + bcm47xx_parts[3].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize);
508 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
512 + /* nvram (old 128kb config partition on netgear wgt634u) */
513 + bcm47xx_parts[3].offset = bcm47xx_parts[0].size;
514 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
517 + /* dual image offset*/
518 + pr_notice("Looking for dual image\n");
519 + dual_image_offset = find_dual_image_off(mtd);
520 + /* linux (kernel and rootfs) */
521 + if (cfe_size != 384 * 1024) {
522 + if (get_router() == ROUTER_SIMPLETECH_SIMPLESHARE) {
523 + bcm47xx_parts[1].offset = bcm47xx_parts[0].size;
524 + bcm47xx_parts[1].size = bcm47xx_parts[4].offset - dual_image_offset -
525 + bcm47xx_parts[1].offset - custom_data_size;
527 + bcm47xx_parts[1].offset = bcm47xx_parts[0].size;
528 + bcm47xx_parts[1].size = bcm47xx_parts[3].offset - dual_image_offset -
529 + bcm47xx_parts[1].offset - custom_data_size;
532 + /* do not count the elf loader, which is on one block */
533 + bcm47xx_parts[1].offset = bcm47xx_parts[0].size +
534 + bcm47xx_parts[3].size + mtd->erasesize;
535 + bcm47xx_parts[1].size = mtd->size -
536 + bcm47xx_parts[0].size -
537 + (2*bcm47xx_parts[3].size) -
538 + mtd->erasesize - custom_data_size;
541 + /* find and size rootfs */
542 + find_root(mtd, &bcm47xx_parts[2]);
543 + bcm47xx_parts[2].size = mtd->size - dual_image_offset -
544 + bcm47xx_parts[2].offset -
545 + bcm47xx_parts[3].size - custom_data_size;
546 + *pparts = bcm47xx_parts;
547 + return bcm47xx_parts[4].name == NULL ? 4 : 5;
550 +static struct mtd_part_parser bcm47xx_parser = {
551 + .owner = THIS_MODULE,
552 + .parse_fn = parse_bcm47xx_partitions,
556 +static int __init bcm47xx_parser_init(void)
558 + return register_mtd_parser(&bcm47xx_parser);
561 +static void __exit bcm47xx_parser_exit(void)
563 + deregister_mtd_parser(&bcm47xx_parser);
566 +module_init(bcm47xx_parser_init);
567 +module_exit(bcm47xx_parser_exit);
569 +MODULE_LICENSE("GPL");
570 +MODULE_DESCRIPTION("Parsing code for flash partitions on bcm47xx SoCs");