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);
365 + pr_notice("Done\n");
371 +static int get_router(void)
377 + u32 boardflags = 0;
378 + u16 sdram_init = 0;
382 + if (nvram_getenv("boardnum", buf, sizeof(buf)) >= 0)
383 + boardnum = simple_strtoul(buf, NULL, 0);
384 + if (nvram_getenv("boardtype", buf, sizeof(buf)) >= 0)
385 + boardtype = simple_strtoul(buf, NULL, 0);
386 + if (nvram_getenv("boardrev", buf, sizeof(buf)) >= 0)
387 + boardrev = simple_strtoul(buf, NULL, 0);
388 + if (nvram_getenv("boardflags", buf, sizeof(buf)) >= 0)
389 + boardflags = simple_strtoul(buf, NULL, 0);
390 + if (nvram_getenv("sdram_init", buf, sizeof(buf)) >= 0)
391 + sdram_init = simple_strtoul(buf, NULL, 0);
392 + if (nvram_getenv("cardbus", buf, sizeof(buf)) >= 0)
393 + cardbus = simple_strtoul(buf, NULL, 0);
394 + if (nvram_getenv("st_rev", buf, sizeof(buf)) >= 0)
395 + strev = simple_strtoul(buf, NULL, 0);
397 + if ((boardnum == 8 || boardnum == 01)
398 + && boardtype == 0x0472 && cardbus == 1) {
399 + /* Netgear WNR834B, Netgear WNR834Bv2 */
400 + return ROUTER_NETGEAR_WNR834B;
403 + if (boardnum == 01 && boardtype == 0x0472 && boardrev == 0x23) {
404 + /* Netgear WNDR-3300 */
405 + return ROUTER_NETGEAR_WNDR3300;
408 + if ((boardnum == 83258 || boardnum == 01)
409 + && boardtype == 0x048e
410 + && (boardrev == 0x11 || boardrev == 0x10)
411 + && boardflags == 0x750
412 + && sdram_init == 0x000A) {
413 + /* Netgear WGR614v8/L/WW 16MB ram, cfe v1.3 or v1.5 */
414 + return ROUTER_NETGEAR_WGR614L;
417 + if ((boardnum == 1 || boardnum == 3500)
418 + && boardtype == 0x04CF
419 + && (boardrev == 0x1213 || boardrev == 02)) {
420 + /* Netgear WNR3500v2/U/L */
421 + return ROUTER_NETGEAR_WNR3500L;
424 + if (boardnum == 1 && boardtype == 0xb4cf && boardrev == 0x1100) {
425 + /* Netgear WNDR3400 */
426 + return ROUTER_NETGEAR_WNDR3400;
429 + if (boardtype == 0x042f
430 + && boardrev == 0x10
432 + && strev == 0x11) {
433 + /* Simpletech Simpleshare */
434 + return ROUTER_SIMPLETECH_SIMPLESHARE;
440 +static int parse_bcm47xx_partitions(struct mtd_info *mtd,
441 + struct mtd_partition **pparts,
442 + struct mtd_part_parser_data *data)
445 + int dual_image_offset = 0;
446 + /* e.g Netgear 0x003e0000-0x003f0000 : "board_data", we exclude this
447 + * part from our mapping to prevent overwriting len/checksum on e.g.
448 + * Netgear WGR614v8/L/WW
450 + int custom_data_size = 0;
451 + struct mtd_partition *bcm47xx_parts;
453 + cfe_size = find_cfe_size(mtd);
457 + bcm47xx_parts = kzalloc(sizeof(struct mtd_partition) * 6, GFP_KERNEL);
459 + bcm47xx_parts[0].name = "cfe";
460 + bcm47xx_parts[1].name = "linux";
461 + bcm47xx_parts[2].name = "rootfs";
462 + bcm47xx_parts[3].name = "nvram";
465 + bcm47xx_parts[0].mask_flags = MTD_WRITEABLE;
466 + bcm47xx_parts[0].offset = 0;
467 + bcm47xx_parts[0].size = cfe_size;
470 + if (cfe_size != 384 * 1024) {
472 + switch (get_router()) {
473 + case ROUTER_NETGEAR_WGR614L:
474 + case ROUTER_NETGEAR_WNR834B:
475 + case ROUTER_NETGEAR_WNDR3300:
476 + case ROUTER_NETGEAR_WNR3500L:
477 + case ROUTER_NETGEAR_WNDR3400:
478 + /* Netgear: checksum is @ 0x003AFFF8 for 4M flash or checksum
479 + * is @ 0x007AFFF8 for 8M flash
481 + custom_data_size = mtd->erasesize;
483 + bcm47xx_parts[3].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize);
484 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
486 + /* Place CFE board_data into a partition */
487 + bcm47xx_parts[4].name = "board_data";
488 + bcm47xx_parts[4].offset = bcm47xx_parts[3].offset - custom_data_size;
489 + bcm47xx_parts[4].size = custom_data_size;
492 + case ROUTER_SIMPLETECH_SIMPLESHARE:
493 + /* Fixup Simpletech Simple share nvram */
495 + pr_notice("Setting up simpletech nvram\n");
496 + custom_data_size = mtd->erasesize;
498 + bcm47xx_parts[3].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize) * 2;
499 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
501 + /* Place backup nvram into a partition */
502 + bcm47xx_parts[4].name = "nvram_copy";
503 + bcm47xx_parts[4].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize);
504 + bcm47xx_parts[4].size = roundup(NVRAM_SPACE, mtd->erasesize);
508 + bcm47xx_parts[3].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize);
509 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
513 + /* nvram (old 128kb config partition on netgear wgt634u) */
514 + bcm47xx_parts[3].offset = bcm47xx_parts[0].size;
515 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
518 + /* dual image offset*/
519 + pr_notice("Looking for dual image\n");
520 + dual_image_offset = find_dual_image_off(mtd);
521 + /* linux (kernel and rootfs) */
522 + if (cfe_size != 384 * 1024) {
523 + if (get_router() == ROUTER_SIMPLETECH_SIMPLESHARE) {
524 + bcm47xx_parts[1].offset = bcm47xx_parts[0].size;
525 + bcm47xx_parts[1].size = bcm47xx_parts[4].offset - dual_image_offset -
526 + bcm47xx_parts[1].offset - custom_data_size;
528 + bcm47xx_parts[1].offset = bcm47xx_parts[0].size;
529 + bcm47xx_parts[1].size = bcm47xx_parts[3].offset - dual_image_offset -
530 + bcm47xx_parts[1].offset - custom_data_size;
533 + /* do not count the elf loader, which is on one block */
534 + bcm47xx_parts[1].offset = bcm47xx_parts[0].size +
535 + bcm47xx_parts[3].size + mtd->erasesize;
536 + bcm47xx_parts[1].size = mtd->size -
537 + bcm47xx_parts[0].size -
538 + (2*bcm47xx_parts[3].size) -
539 + mtd->erasesize - custom_data_size;
542 + /* find and size rootfs */
543 + find_root(mtd, &bcm47xx_parts[2]);
544 + bcm47xx_parts[2].size = mtd->size - dual_image_offset -
545 + bcm47xx_parts[2].offset -
546 + bcm47xx_parts[3].size - custom_data_size;
547 + *pparts = bcm47xx_parts;
548 + return bcm47xx_parts[4].name == NULL ? 4 : 5;
551 +static struct mtd_part_parser bcm47xx_parser = {
552 + .owner = THIS_MODULE,
553 + .parse_fn = parse_bcm47xx_partitions,
557 +static int __init bcm47xx_parser_init(void)
559 + return register_mtd_parser(&bcm47xx_parser);
562 +static void __exit bcm47xx_parser_exit(void)
564 + deregister_mtd_parser(&bcm47xx_parser);
567 +module_init(bcm47xx_parser_init);
568 +module_exit(bcm47xx_parser_exit);
570 +MODULE_LICENSE("GPL");
571 +MODULE_DESCRIPTION("Parsing code for flash partitions on bcm47xx SoCs");