1 From 1934b89b42976a083c2ad8d8fdc526ab5b05c5e4 Mon Sep 17 00:00:00 2001
2 From: Hauke Mehrtens <hauke@hauke-m.de>
3 Date: Sun, 17 Jul 2011 14:54:11 +0200
4 Subject: [PATCH 17/26] mtd: bcm47xx: add bcm47xx part parser
7 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
9 drivers/mtd/Kconfig | 7 +
10 drivers/mtd/Makefile | 1 +
11 drivers/mtd/bcm47xxpart.c | 536 +++++++++++++++++++++++++++++++++++++++++++++
12 3 files changed, 544 insertions(+), 0 deletions(-)
13 create mode 100644 drivers/mtd/bcm47xxpart.c
15 --- a/drivers/mtd/Kconfig
16 +++ b/drivers/mtd/Kconfig
17 @@ -173,6 +173,13 @@ config MTD_MYLOADER_PARTS
18 You will still need the parsing functions to be called by the driver
19 for your particular device. It won't happen automatically.
21 +config MTD_BCM47XX_PARTS
22 + tristate "BCM47XX partitioning support"
26 + bcm47XX partitioning support
28 comment "User Modules And Translation Layers"
31 --- a/drivers/mtd/Makefile
32 +++ b/drivers/mtd/Makefile
33 @@ -12,6 +12,7 @@ obj-$(CONFIG_MTD_CMDLINE_PARTS) += cmdli
34 obj-$(CONFIG_MTD_AFS_PARTS) += afs.o
35 obj-$(CONFIG_MTD_AR7_PARTS) += ar7part.o
36 obj-$(CONFIG_MTD_MYLOADER_PARTS) += myloader.o
37 +obj-$(CONFIG_MTD_BCM47XX_PARTS) += bcm47xxpart.o
39 # 'Users' - code which presents functionality to userspace.
40 obj-$(CONFIG_MTD_CHAR) += mtdchar.o
42 +++ b/drivers/mtd/bcm47xxpart.c
45 + * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
46 + * Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
47 + * Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
49 + * original functions for finding root filesystem from Mike Baker
51 + * This program is free software; you can redistribute it and/or modify it
52 + * under the terms of the GNU General Public License as published by the
53 + * Free Software Foundation; either version 2 of the License, or (at your
54 + * option) any later version.
56 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
57 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
58 + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
59 + * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
60 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
61 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
62 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
63 + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
64 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
65 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 + * You should have received a copy of the GNU General Public License along
68 + * with this program; if not, write to the Free Software Foundation, Inc.,
69 + * 675 Mass Ave, Cambridge, MA 02139, USA.
71 + * Copyright 2001-2003, Broadcom Corporation
72 + * All Rights Reserved.
74 + * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
75 + * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
76 + * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
77 + * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
79 + * Flash mapping for BCM947XX boards
82 +#define pr_fmt(fmt) "bcm47xx_part: " fmt
83 +#include <linux/init.h>
84 +#include <linux/module.h>
85 +#include <linux/types.h>
86 +#include <linux/kernel.h>
87 +#include <linux/sched.h>
88 +#include <linux/wait.h>
89 +#include <linux/mtd/mtd.h>
90 +#include <linux/mtd/partitions.h>
91 +#include <linux/crc32.h>
92 +#include <linux/io.h>
93 +#include <asm/mach-bcm47xx/nvram.h>
94 +#include <asm/mach-bcm47xx/bcm47xx.h>
95 +#include <asm/fw/cfe/cfe_api.h>
98 +#define TRX_MAGIC 0x30524448 /* "HDR0" */
99 +#define TRX_VERSION 1
100 +#define TRX_MAX_LEN 0x3A0000
101 +#define TRX_NO_HEADER 1 /* Do not write TRX header */
102 +#define TRX_GZ_FILES 0x2 /* Contains up to TRX_MAX_OFFSET individual gzip files */
103 +#define TRX_MAX_OFFSET 3
106 + u32 magic; /* "HDR0" */
107 + u32 len; /* Length of file including header */
108 + u32 crc32; /* 32-bit CRC from flag_version to end of file */
109 + u32 flag_version; /* 0:15 flags, 16:31 version */
110 + u32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */
113 +/* for Edimax Print servers which use an additional header
114 + * then the firmware on flash looks like :
115 + * EDIMAX HEADER | TRX HEADER
116 + * As this header is 12 bytes long we have to handle it
117 + * and skip it to find the TRX header
119 +#define EDIMAX_PS_HEADER_MAGIC 0x36315350 /* "PS16" */
120 +#define EDIMAX_PS_HEADER_LEN 0xc /* 12 bytes long for edimax header */
122 +#define NVRAM_SPACE 0x8000
124 +#define ROUTER_NETGEAR_WGR614L 1
125 +#define ROUTER_NETGEAR_WNR834B 2
126 +#define ROUTER_NETGEAR_WNDR3300 3
127 +#define ROUTER_NETGEAR_WNR3500L 4
128 +#define ROUTER_SIMPLETECH_SIMPLESHARE 5
130 +static struct mtd_partition bcm47xx_parts[] = {
131 + { name: "cfe", offset:0, size:0, mask_flags:MTD_WRITEABLE, },
132 + { name: "linux", offset:0, size:0, },
133 + { name: "rootfs", offset:0, size:0, },
134 + { name: "nvram", offset:0, size:0, },
135 + { name: NULL, }, /* Used to create custom partitons with the function get_router() */
140 +find_cfe_size(struct mtd_info *mtd)
142 + struct trx_header *trx;
143 + unsigned char buf[512];
148 + trx = (struct trx_header *) buf;
150 + blocksize = mtd->erasesize;
151 + if (blocksize < 0x10000)
152 + blocksize = 0x10000;
154 + for (off = (128*1024); off < mtd->size; off += blocksize) {
155 + memset(buf, 0xe5, sizeof(buf));
160 + if (mtd->read(mtd, off, sizeof(buf), &len, buf) ||
161 + len != sizeof(buf))
164 + if (le32_to_cpu(trx->magic) == EDIMAX_PS_HEADER_MAGIC) {
165 + if (mtd->read(mtd, off + EDIMAX_PS_HEADER_LEN,
166 + sizeof(buf), &len, buf) || len != sizeof(buf)) {
169 + pr_notice("Found edimax header\n");
173 + /* found a TRX header */
174 + if (le32_to_cpu(trx->magic) == TRX_MAGIC)
178 + pr_notice("%s: Couldn't find bootloader size\n", mtd->name);
182 + pr_notice("bootloader size: %d\n", off);
188 + * Copied from mtdblock.c
192 + * Since typical flash erasable sectors are much larger than what Linux's
193 + * buffer cache can handle, we must implement read-modify-write on flash
194 + * sectors for each block write requests. To avoid over-erasing flash sectors
195 + * and to speed things up, we locally cache a whole flash sector while it is
196 + * being written to until a different sector is required.
199 +static void erase_callback(struct erase_info *done)
201 + wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
205 +static int erase_write(struct mtd_info *mtd, unsigned long pos,
206 + int len, const char *buf)
208 + struct erase_info erase;
209 + DECLARE_WAITQUEUE(wait, current);
210 + wait_queue_head_t wait_q;
215 + * First, let's erase the flash block.
218 + init_waitqueue_head(&wait_q);
220 + erase.callback = erase_callback;
223 + erase.priv = (u_long)&wait_q;
225 + set_current_state(TASK_INTERRUPTIBLE);
226 + add_wait_queue(&wait_q, &wait);
228 + ret = mtd->erase(mtd, &erase);
230 + set_current_state(TASK_RUNNING);
231 + remove_wait_queue(&wait_q, &wait);
232 + pr_warn("erase of region [0x%lx, 0x%x] on \"%s\" failed\n",
233 + pos, len, mtd->name);
237 + schedule(); /* Wait for erase to finish. */
238 + remove_wait_queue(&wait_q, &wait);
241 + * Next, write data to flash.
244 + ret = mtd->write(mtd, pos, len, &retlen, buf);
254 +find_dual_image_off(struct mtd_info *mtd)
256 + struct trx_header trx;
257 + int off, blocksize;
260 + blocksize = mtd->erasesize;
261 + if (blocksize < 0x10000)
262 + blocksize = 0x10000;
264 + for (off = (128*1024); off < mtd->size; off += blocksize) {
265 + memset(&trx, 0xe5, sizeof(trx));
269 + if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
270 + len != sizeof(trx))
272 + /* found last TRX header */
273 + if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
274 + if (le32_to_cpu(trx.flag_version >> 16) == 2) {
275 + pr_notice("dual image TRX header found\n");
276 + return mtd->size / 2;
287 +find_root(struct mtd_info *mtd, struct mtd_partition *part)
289 + struct trx_header trx, *trx2;
290 + unsigned char buf[512], *block;
291 + int off, blocksize, trxoff = 0;
294 + bool edimax = false;
296 + blocksize = mtd->erasesize;
297 + if (blocksize < 0x10000)
298 + blocksize = 0x10000;
300 + for (off = (128*1024); off < mtd->size; off += blocksize) {
301 + memset(&trx, 0xe5, sizeof(trx));
306 + if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
307 + len != sizeof(trx))
310 + /* found an edimax header */
311 + if (le32_to_cpu(trx.magic) == EDIMAX_PS_HEADER_MAGIC) {
312 + /* read the correct trx header */
313 + if (mtd->read(mtd, off + EDIMAX_PS_HEADER_LEN,
314 + sizeof(trx), &len, (char *) &trx) ||
315 + len != sizeof(trx)) {
318 + pr_notice("Found an edimax ps header\n");
323 + /* found a TRX header */
324 + if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
325 + part->offset = le32_to_cpu(trx.offsets[2]) ? :
326 + le32_to_cpu(trx.offsets[1]);
327 + part->size = le32_to_cpu(trx.len);
329 + part->size -= part->offset;
330 + part->offset += off;
332 + off += EDIMAX_PS_HEADER_LEN;
333 + trxoff = EDIMAX_PS_HEADER_LEN;
340 + pr_warn("%s: Couldn't find root filesystem\n",
345 + pr_notice("TRX offset : %x\n", trxoff);
346 + if (part->size == 0)
349 + if (mtd->read(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
352 + /* Move the fs outside of the trx */
355 + if (trx.len != part->offset + part->size - off) {
356 + /* Update the trx offsets and length */
357 + trx.len = part->offset + part->size - off;
359 + /* Update the trx crc32 */
360 + for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
361 + if (mtd->read(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
363 + crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
367 + /* read first eraseblock from the trx */
368 + block = kmalloc(mtd->erasesize, GFP_KERNEL);
369 + trx2 = (struct trx_header *) block;
370 + if (mtd->read(mtd, off - trxoff, mtd->erasesize, &len, block) || len != mtd->erasesize) {
371 + pr_err("Error accessing the first trx eraseblock\n");
375 + pr_notice("Updating TRX offsets and length:\n");
376 + 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);
377 + 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);
379 + /* Write updated trx header to the flash */
380 + memcpy(block + trxoff, &trx, sizeof(trx));
382 + mtd->unlock(mtd, off - trxoff, mtd->erasesize);
383 + erase_write(mtd, off - trxoff, mtd->erasesize, block);
387 + pr_notice("Done\n");
393 +static int get_router(void)
399 + u32 boardflags = 0;
400 + u16 sdram_init = 0;
404 + if (nvram_getenv("boardnum", buf, sizeof(buf)) >= 0)
405 + boardnum = simple_strtoul(buf, NULL, 0);
406 + if (nvram_getenv("boardtype", buf, sizeof(buf)) >= 0)
407 + boardtype = simple_strtoul(buf, NULL, 0);
408 + if (nvram_getenv("boardrev", buf, sizeof(buf)) >= 0)
409 + boardrev = simple_strtoul(buf, NULL, 0);
410 + if (nvram_getenv("boardflags", buf, sizeof(buf)) >= 0)
411 + boardflags = simple_strtoul(buf, NULL, 0);
412 + if (nvram_getenv("sdram_init", buf, sizeof(buf)) >= 0)
413 + sdram_init = simple_strtoul(buf, NULL, 0);
414 + if (nvram_getenv("cardbus", buf, sizeof(buf)) >= 0)
415 + cardbus = simple_strtoul(buf, NULL, 0);
416 + if (nvram_getenv("st_rev", buf, sizeof(buf)) >= 0)
417 + strev = simple_strtoul(buf, NULL, 0);
419 + if ((boardnum == 8 || boardnum == 01)
420 + && boardtype == 0x0472 && cardbus == 1) {
421 + /* Netgear WNR834B, Netgear WNR834Bv2 */
422 + return ROUTER_NETGEAR_WNR834B;
425 + if (boardnum == 01 && boardtype == 0x0472 && boardrev == 0x23) {
426 + /* Netgear WNDR-3300 */
427 + return ROUTER_NETGEAR_WNDR3300;
430 + if ((boardnum == 83258 || boardnum == 01)
431 + && boardtype == 0x048e
432 + && (boardrev == 0x11 || boardrev == 0x10)
433 + && boardflags == 0x750
434 + && sdram_init == 0x000A) {
435 + /* Netgear WGR614v8/L/WW 16MB ram, cfe v1.3 or v1.5 */
436 + return ROUTER_NETGEAR_WGR614L;
439 + if ((boardnum == 1 || boardnum == 3500)
440 + && boardtype == 0x04CF
441 + && (boardrev == 0x1213 || boardrev == 02)) {
442 + /* Netgear WNR3500v2/U/L */
443 + return ROUTER_NETGEAR_WNR3500L;
446 + if (boardtype == 0x042f
447 + && boardrev == 0x10
449 + && strev == 0x11) {
450 + /* Simpletech Simpleshare */
451 + return ROUTER_SIMPLETECH_SIMPLESHARE;
457 +static int parse_bcm47xx_partitions(struct mtd_info *mtd,
458 + struct mtd_partition **pparts,
459 +// struct mtd_part_parser_data *data)
460 + unsigned long data)
463 + int dual_image_offset = 0;
464 + /* e.g Netgear 0x003e0000-0x003f0000 : "board_data", we exclude this
465 + * part from our mapping to prevent overwriting len/checksum on e.g.
466 + * Netgear WGR614v8/L/WW
468 + int custom_data_size = 0;
470 + cfe_size = find_cfe_size(mtd);
475 + bcm47xx_parts[0].offset = 0;
476 + bcm47xx_parts[0].size = cfe_size;
479 + if (cfe_size != 384 * 1024) {
481 + switch (get_router()) {
482 + case ROUTER_NETGEAR_WGR614L:
483 + case ROUTER_NETGEAR_WNR834B:
484 + case ROUTER_NETGEAR_WNDR3300:
485 + case ROUTER_NETGEAR_WNR3500L:
486 + /* Netgear: checksum is @ 0x003AFFF8 for 4M flash or checksum
487 + * is @ 0x007AFFF8 for 8M flash
489 + custom_data_size = mtd->erasesize;
491 + bcm47xx_parts[3].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize);
492 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
494 + /* Place CFE board_data into a partition */
495 + bcm47xx_parts[4].name = "board_data";
496 + bcm47xx_parts[4].offset = bcm47xx_parts[3].offset - custom_data_size;
497 + bcm47xx_parts[4].size = custom_data_size;
500 + case ROUTER_SIMPLETECH_SIMPLESHARE:
501 + /* Fixup Simpletech Simple share nvram */
503 + pr_notice("Setting up simpletech nvram\n");
504 + custom_data_size = mtd->erasesize;
506 + bcm47xx_parts[3].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize) * 2;
507 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
509 + /* Place backup nvram into a partition */
510 + bcm47xx_parts[4].name = "nvram_copy";
511 + bcm47xx_parts[4].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize);
512 + bcm47xx_parts[4].size = roundup(NVRAM_SPACE, mtd->erasesize);
516 + bcm47xx_parts[3].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize);
517 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
521 + /* nvram (old 128kb config partition on netgear wgt634u) */
522 + bcm47xx_parts[3].offset = bcm47xx_parts[0].size;
523 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
526 + /* dual image offset*/
527 + pr_notice("Looking for dual image\n");
528 + dual_image_offset = find_dual_image_off(mtd);
529 + /* linux (kernel and rootfs) */
530 + if (cfe_size != 384 * 1024) {
531 + if (get_router() == ROUTER_SIMPLETECH_SIMPLESHARE) {
532 + bcm47xx_parts[1].offset = bcm47xx_parts[0].size;
533 + bcm47xx_parts[1].size = bcm47xx_parts[4].offset - dual_image_offset -
534 + bcm47xx_parts[1].offset - custom_data_size;
536 + bcm47xx_parts[1].offset = bcm47xx_parts[0].size;
537 + bcm47xx_parts[1].size = bcm47xx_parts[3].offset - dual_image_offset -
538 + bcm47xx_parts[1].offset - custom_data_size;
541 + /* do not count the elf loader, which is on one block */
542 + bcm47xx_parts[1].offset = bcm47xx_parts[0].size +
543 + bcm47xx_parts[3].size + mtd->erasesize;
544 + bcm47xx_parts[1].size = mtd->size -
545 + bcm47xx_parts[0].size -
546 + (2*bcm47xx_parts[3].size) -
547 + mtd->erasesize - custom_data_size;
550 + /* find and size rootfs */
551 + find_root(mtd, &bcm47xx_parts[2]);
552 + bcm47xx_parts[2].size = mtd->size - dual_image_offset -
553 + bcm47xx_parts[2].offset -
554 + bcm47xx_parts[3].size - custom_data_size;
555 + *pparts = bcm47xx_parts;
556 + return bcm47xx_parts[4].name == NULL ? 4 : 5;
559 +static struct mtd_part_parser bcm47xx_parser = {
560 + .owner = THIS_MODULE,
561 + .parse_fn = parse_bcm47xx_partitions,
565 +static int __init bcm47xx_parser_init(void)
567 + return register_mtd_parser(&bcm47xx_parser);
570 +static void __exit bcm47xx_parser_exit(void)
572 + deregister_mtd_parser(&bcm47xx_parser);
575 +module_init(bcm47xx_parser_init);
576 +module_exit(bcm47xx_parser_exit);
578 +MODULE_LICENSE("GPL");
579 +MODULE_DESCRIPTION("Parsing code for flash partitions on bcm47xx SoCs");