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 <asm/mach-bcm47xx/nvram.h>
19 /* 10 parts were found on sflash on Netgear WNDR4500 */
20 #define BCM47XXPART_MAX_PARTS 12
23 * Amount of bytes we read when analyzing each block of flash memory.
24 * Set it big enough to allow detecting partition and reading important data.
26 #define BCM47XXPART_BYTES_TO_READ 0x404
29 #define BOARD_DATA_MAGIC 0x5246504D /* MPFR */
30 #define POT_MAGIC1 0x54544f50 /* POTT */
31 #define POT_MAGIC2 0x504f /* OP */
32 #define ML_MAGIC1 0x39685a42
33 #define ML_MAGIC2 0x26594131
34 #define TRX_MAGIC 0x30524448
45 static void bcm47xxpart_add_part(struct mtd_partition
*part
, char *name
,
46 u64 offset
, uint32_t mask_flags
)
49 part
->offset
= offset
;
50 part
->mask_flags
= mask_flags
;
53 static int bcm47xxpart_parse(struct mtd_info
*master
,
54 struct mtd_partition
**pparts
,
55 struct mtd_part_parser_data
*data
)
57 struct mtd_partition
*parts
;
58 uint8_t i
, curr_part
= 0;
62 uint32_t blocksize
= 0x10000;
63 struct trx_header
*trx
;
66 parts
= kzalloc(sizeof(struct mtd_partition
) * BCM47XXPART_MAX_PARTS
,
68 buf
= kzalloc(BCM47XXPART_BYTES_TO_READ
, GFP_KERNEL
);
70 /* Parse block by block looking for magics */
71 for (offset
= 0; offset
<= master
->size
- blocksize
;
72 offset
+= blocksize
) {
73 /* Nothing more in higher memory */
74 if (offset
>= 0x2000000)
77 if (curr_part
> BCM47XXPART_MAX_PARTS
) {
78 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
82 /* Read beginning of the block */
83 if (mtd_read(master
, offset
, BCM47XXPART_BYTES_TO_READ
,
84 &bytes_read
, (uint8_t *)buf
) < 0) {
85 pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
90 /* CFE has small NVRAM at 0x400 */
91 if (buf
[0x400 / 4] == NVRAM_HEADER
) {
92 bcm47xxpart_add_part(&parts
[curr_part
++], "boot",
93 offset
, MTD_WRITEABLE
);
98 if (buf
[0x000 / 4] == NVRAM_HEADER
) {
99 bcm47xxpart_add_part(&parts
[curr_part
++], "nvram",
105 * board_data starts with board_id which differs across boards,
106 * but we can use 'MPFR' (hopefully) magic at 0x100
108 if (buf
[0x100 / 4] == BOARD_DATA_MAGIC
) {
109 bcm47xxpart_add_part(&parts
[curr_part
++], "board_data",
110 offset
, MTD_WRITEABLE
);
115 if (buf
[0x000 / 4] == POT_MAGIC1
&&
116 (buf
[0x004 / 4] & 0xFFFF) == POT_MAGIC2
) {
117 bcm47xxpart_add_part(&parts
[curr_part
++], "POT", offset
,
123 if (buf
[0x010 / 4] == ML_MAGIC1
&&
124 buf
[0x014 / 4] == ML_MAGIC2
) {
125 bcm47xxpart_add_part(&parts
[curr_part
++], "ML", offset
,
131 if (buf
[0x000 / 4] == TRX_MAGIC
) {
132 trx
= (struct trx_header
*)buf
;
135 /* We have LZMA loader if offset[2] points to sth */
136 if (trx
->offset
[2]) {
137 bcm47xxpart_add_part(&parts
[curr_part
++],
139 offset
+ trx
->offset
[i
],
144 bcm47xxpart_add_part(&parts
[curr_part
++], "linux",
145 offset
+ trx
->offset
[i
], 0);
149 * Pure rootfs size is known and can be calculated as:
150 * trx->length - trx->offset[i]. We don't fill it as
151 * we want to have jffs2 (overlay) in the same mtd.
153 bcm47xxpart_add_part(&parts
[curr_part
++], "rootfs",
154 offset
+ trx
->offset
[i
], 0);
158 * We have whole TRX scanned, skip to the next part. Use
159 * roundown (not roundup), as the loop will increase
160 * offset in next step.
162 offset
= rounddown(offset
+ trx
->length
, blocksize
);
169 * Assume that partitions end at the beginning of the one they are
172 for (i
= 0; i
< curr_part
- 1; i
++)
173 parts
[i
].size
= parts
[i
+ 1].offset
- parts
[i
].offset
;
175 parts
[curr_part
- 1].size
=
176 master
->size
- parts
[curr_part
- 1].offset
;
182 static struct mtd_part_parser bcm47xxpart_mtd_parser
= {
183 .owner
= THIS_MODULE
,
184 .parse_fn
= bcm47xxpart_parse
,
185 .name
= "bcm47xxpart",
188 static int __init
bcm47xxpart_init(void)
190 return register_mtd_parser(&bcm47xxpart_mtd_parser
);
193 static void __exit
bcm47xxpart_exit(void)
195 deregister_mtd_parser(&bcm47xxpart_mtd_parser
);
198 module_init(bcm47xxpart_init
);
199 module_exit(bcm47xxpart_exit
);
201 MODULE_LICENSE("GPL");
202 MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");