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 * 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 FACTORY_MAGIC 0x59544346 /* FCTY */
31 #define POT_MAGIC1 0x54544f50 /* POTT */
32 #define POT_MAGIC2 0x504f /* OP */
33 #define ML_MAGIC1 0x39685a42
34 #define ML_MAGIC2 0x26594131
35 #define TRX_MAGIC 0x30524448
36 #define SQSH_MAGIC 0x71736873 /* shsq */
47 static void bcm47xxpart_add_part(struct mtd_partition
*part
, char *name
,
48 u64 offset
, uint32_t mask_flags
)
51 part
->offset
= offset
;
52 part
->mask_flags
= mask_flags
;
55 static int bcm47xxpart_parse(struct mtd_info
*master
,
56 struct mtd_partition
**pparts
,
57 struct mtd_part_parser_data
*data
)
59 struct mtd_partition
*parts
;
60 uint8_t i
, curr_part
= 0;
64 uint32_t blocksize
= master
->erasesize
;
65 struct trx_header
*trx
;
67 int last_trx_part
= -1;
68 int possible_nvram_sizes
[] = { 0x8000, 0xF000, 0x10000, };
70 if (blocksize
<= 0x10000)
74 parts
= kzalloc(sizeof(struct mtd_partition
) * BCM47XXPART_MAX_PARTS
,
79 buf
= kzalloc(BCM47XXPART_BYTES_TO_READ
, GFP_KERNEL
);
85 /* Parse block by block looking for magics */
86 for (offset
= 0; offset
<= master
->size
- blocksize
;
87 offset
+= blocksize
) {
88 /* Nothing more in higher memory */
89 if (offset
>= 0x2000000)
92 if (curr_part
> BCM47XXPART_MAX_PARTS
) {
93 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
97 /* Read beginning of the block */
98 if (mtd_read(master
, offset
, BCM47XXPART_BYTES_TO_READ
,
99 &bytes_read
, (uint8_t *)buf
) < 0) {
100 pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
105 /* CFE has small NVRAM at 0x400 */
106 if (buf
[0x400 / 4] == NVRAM_HEADER
) {
107 bcm47xxpart_add_part(&parts
[curr_part
++], "boot",
108 offset
, MTD_WRITEABLE
);
113 * board_data starts with board_id which differs across boards,
114 * but we can use 'MPFR' (hopefully) magic at 0x100
116 if (buf
[0x100 / 4] == BOARD_DATA_MAGIC
) {
117 bcm47xxpart_add_part(&parts
[curr_part
++], "board_data",
118 offset
, MTD_WRITEABLE
);
122 /* Found on Huawei E970 */
123 if (buf
[0x000 / 4] == FACTORY_MAGIC
) {
124 bcm47xxpart_add_part(&parts
[curr_part
++], "factory",
125 offset
, MTD_WRITEABLE
);
130 if (buf
[0x000 / 4] == POT_MAGIC1
&&
131 (buf
[0x004 / 4] & 0xFFFF) == POT_MAGIC2
) {
132 bcm47xxpart_add_part(&parts
[curr_part
++], "POT", offset
,
138 if (buf
[0x010 / 4] == ML_MAGIC1
&&
139 buf
[0x014 / 4] == ML_MAGIC2
) {
140 bcm47xxpart_add_part(&parts
[curr_part
++], "ML", offset
,
146 if (buf
[0x000 / 4] == TRX_MAGIC
) {
147 trx
= (struct trx_header
*)buf
;
149 trx_part
= curr_part
;
150 bcm47xxpart_add_part(&parts
[curr_part
++], "firmware",
154 /* We have LZMA loader if offset[2] points to sth */
155 if (trx
->offset
[2]) {
156 bcm47xxpart_add_part(&parts
[curr_part
++],
158 offset
+ trx
->offset
[i
],
163 bcm47xxpart_add_part(&parts
[curr_part
++], "linux",
164 offset
+ trx
->offset
[i
], 0);
168 * Pure rootfs size is known and can be calculated as:
169 * trx->length - trx->offset[i]. We don't fill it as
170 * we want to have jffs2 (overlay) in the same mtd.
172 bcm47xxpart_add_part(&parts
[curr_part
++], "rootfs",
173 offset
+ trx
->offset
[i
], 0);
176 last_trx_part
= curr_part
- 1;
179 * We have whole TRX scanned, skip to the next part. Use
180 * roundown (not roundup), as the loop will increase
181 * offset in next step.
183 offset
= rounddown(offset
+ trx
->length
, blocksize
);
187 /* Squashfs on devices not using TRX */
188 if (buf
[0x000 / 4] == SQSH_MAGIC
) {
189 bcm47xxpart_add_part(&parts
[curr_part
++], "rootfs",
195 /* Look for NVRAM at the end of the last block. */
196 for (i
= 0; i
< ARRAY_SIZE(possible_nvram_sizes
); i
++) {
197 if (curr_part
> BCM47XXPART_MAX_PARTS
) {
198 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
202 offset
= master
->size
- possible_nvram_sizes
[i
];
203 if (mtd_read(master
, offset
, 0x4, &bytes_read
,
204 (uint8_t *)buf
) < 0) {
205 pr_err("mtd_read error while reading at offset 0x%X!\n",
211 if (buf
[0] == NVRAM_HEADER
) {
212 bcm47xxpart_add_part(&parts
[curr_part
++], "nvram",
213 master
->size
- blocksize
, 0);
221 * Assume that partitions end at the beginning of the one they are
224 for (i
= 0; i
< curr_part
; i
++) {
225 u64 next_part_offset
= (i
< curr_part
- 1) ?
226 parts
[i
+ 1].offset
: master
->size
;
228 parts
[i
].size
= next_part_offset
- parts
[i
].offset
;
229 if (i
== last_trx_part
&& trx_part
>= 0)
230 parts
[trx_part
].size
= next_part_offset
-
231 parts
[trx_part
].offset
;
238 static struct mtd_part_parser bcm47xxpart_mtd_parser
= {
239 .owner
= THIS_MODULE
,
240 .parse_fn
= bcm47xxpart_parse
,
241 .name
= "bcm47xxpart",
244 static int __init
bcm47xxpart_init(void)
246 return register_mtd_parser(&bcm47xxpart_mtd_parser
);
249 static void __exit
bcm47xxpart_exit(void)
251 deregister_mtd_parser(&bcm47xxpart_mtd_parser
);
254 module_init(bcm47xxpart_init
);
255 module_exit(bcm47xxpart_exit
);
257 MODULE_LICENSE("GPL");
258 MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");