2 * BCM47XX MTD partitioning
4 * Copyright © 2012 Rafał Miłecki <zajec5@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/mtd/partitions.h>
17 #include <bcm47xx_nvram.h>
19 /* 10 parts were found on sflash on Netgear WNDR4500 */
20 #define BCM47XXPART_MAX_PARTS 12
23 #define BOARD_DATA_MAGIC 0x5246504D /* MPFR */
24 #define POT_MAGIC1 0x54544f50 /* POTT */
25 #define POT_MAGIC2 0x504f /* OP */
26 #define ML_MAGIC1 0x39685a42
27 #define ML_MAGIC2 0x26594131
28 #define TRX_MAGIC 0x30524448
39 static void bcm47xxpart_add_part(struct mtd_partition *part, char *name,
40 u64 offset, uint32_t mask_flags)
43 part->offset = offset;
44 part->mask_flags = mask_flags;
47 static int bcm47xxpart_parse(struct mtd_info *master,
48 struct mtd_partition **pparts,
49 struct mtd_part_parser_data *data)
51 struct mtd_partition *parts;
52 uint8_t i, curr_part = 0;
56 uint32_t blocksize = master->erasesize;
57 struct trx_header *trx;
59 int last_trx_part = -1;
60 int max_bytes_to_read = 0x8004;
62 if (blocksize <= 0x10000)
64 if (blocksize == 0x20000)
65 max_bytes_to_read = 0x18004;
68 parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
70 buf = kzalloc(max_bytes_to_read, GFP_KERNEL);
72 /* Parse block by block looking for magics */
73 for (offset = 0; offset <= master->size - blocksize;
74 offset += blocksize) {
75 /* Nothing more in higher memory */
76 if (offset >= 0x2000000)
79 if (curr_part > BCM47XXPART_MAX_PARTS) {
80 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
84 /* Read beginning of the block */
85 if (mtd_read(master, offset, max_bytes_to_read,
86 &bytes_read, (uint8_t *)buf) < 0) {
87 pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
92 /* CFE has small NVRAM at 0x400 */
93 if (buf[0x400 / 4] == NVRAM_HEADER) {
94 bcm47xxpart_add_part(&parts[curr_part++], "boot",
95 offset, MTD_WRITEABLE);
100 if (buf[0x000 / 4] == NVRAM_HEADER ||
101 buf[0x1000 / 4] == NVRAM_HEADER ||
102 buf[0x8000 / 4] == NVRAM_HEADER ||
103 (blocksize == 0x20000 && (
104 buf[0x10000 / 4] == NVRAM_HEADER ||
105 buf[0x11000 / 4] == NVRAM_HEADER ||
106 buf[0x18000 / 4] == NVRAM_HEADER))) {
107 bcm47xxpart_add_part(&parts[curr_part++], "nvram",
109 offset = rounddown(offset, blocksize);
114 * board_data starts with board_id which differs across boards,
115 * but we can use 'MPFR' (hopefully) magic at 0x100
117 if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
118 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
119 offset, MTD_WRITEABLE);
124 if (buf[0x000 / 4] == POT_MAGIC1 &&
125 (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
126 bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
132 if (buf[0x010 / 4] == ML_MAGIC1 &&
133 buf[0x014 / 4] == ML_MAGIC2) {
134 bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
140 if (buf[0x000 / 4] == TRX_MAGIC) {
141 trx = (struct trx_header *)buf;
143 trx_part = curr_part;
144 bcm47xxpart_add_part(&parts[curr_part++], "firmware",
148 /* We have LZMA loader if offset[2] points to sth */
149 if (trx->offset[2]) {
150 bcm47xxpart_add_part(&parts[curr_part++],
152 offset + trx->offset[i],
157 bcm47xxpart_add_part(&parts[curr_part++], "linux",
158 offset + trx->offset[i], 0);
162 * Pure rootfs size is known and can be calculated as:
163 * trx->length - trx->offset[i]. We don't fill it as
164 * we want to have jffs2 (overlay) in the same mtd.
166 bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
167 offset + trx->offset[i], 0);
170 last_trx_part = curr_part - 1;
173 * We have whole TRX scanned, skip to the next part. Use
174 * roundown (not roundup), as the loop will increase
175 * offset in next step.
177 offset = rounddown(offset + trx->length, blocksize);
184 * Assume that partitions end at the beginning of the one they are
187 for (i = 0; i < curr_part; i++) {
188 u64 next_part_offset = (i < curr_part - 1) ?
189 parts[i + 1].offset : master->size;
191 parts[i].size = next_part_offset - parts[i].offset;
192 if (i == last_trx_part && trx_part >= 0)
193 parts[trx_part].size = next_part_offset -
194 parts[trx_part].offset;
201 static struct mtd_part_parser bcm47xxpart_mtd_parser = {
202 .owner = THIS_MODULE,
203 .parse_fn = bcm47xxpart_parse,
204 .name = "bcm47xxpart",
207 static int __init bcm47xxpart_init(void)
209 return register_mtd_parser(&bcm47xxpart_mtd_parser);
212 static void __exit bcm47xxpart_exit(void)
214 deregister_mtd_parser(&bcm47xxpart_mtd_parser);
217 module_init(bcm47xxpart_init);
218 module_exit(bcm47xxpart_exit);
220 MODULE_LICENSE("GPL");
221 MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");