1 // SPDX-License-Identifier: GPL-2.0-only
3 * BCM47XX MTD partitioning
5 * Copyright © 2012 Rafał Miłecki <zajec5@gmail.com>
8 #include <linux/bcm47xx_nvram.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/mtd/mtd.h>
13 #include <linux/mtd/partitions.h>
15 #include <uapi/linux/magic.h>
18 * NAND flash on Netgear R6250 was verified to contain 15 partitions.
19 * This will result in allocating too big array for some old devices, but the
20 * memory will be freed soon anyway (see mtd_device_parse_register).
22 #define BCM47XXPART_MAX_PARTS 20
25 * Amount of bytes we read when analyzing each block of flash memory.
26 * Set it big enough to allow detecting partition and reading important data.
28 #define BCM47XXPART_BYTES_TO_READ 0x4e8
31 #define BOARD_DATA_MAGIC 0x5246504D /* MPFR */
32 #define BOARD_DATA_MAGIC2 0xBD0D0BBD
33 #define CFE_MAGIC 0x43464531 /* 1EFC */
34 #define FACTORY_MAGIC 0x59544346 /* FCTY */
35 #define NVRAM_HEADER 0x48534C46 /* FLSH */
36 #define POT_MAGIC1 0x54544f50 /* POTT */
37 #define POT_MAGIC2 0x504f /* OP */
38 #define ML_MAGIC1 0x39685a42
39 #define ML_MAGIC2 0x26594131
40 #define TRX_MAGIC 0x30524448
41 #define SHSQ_MAGIC 0x71736873 /* shsq (weird ZTE H218N endianness) */
43 static const char * const trx_types[] = { "trx", NULL };
54 static void bcm47xxpart_add_part(struct mtd_partition *part, const char *name,
55 u64 offset, uint32_t mask_flags)
58 part->offset = offset;
59 part->mask_flags = mask_flags;
63 * bcm47xxpart_bootpartition - gets index of TRX partition used by bootloader
65 * Some devices may have more than one TRX partition. In such case one of them
66 * is the main one and another a failsafe one. Bootloader may fallback to the
67 * failsafe firmware if it detects corruption of the main image.
69 * This function provides info about currently used TRX partition. It's the one
70 * containing kernel started by the bootloader.
72 static int bcm47xxpart_bootpartition(void)
77 /* Check CFE environment variable */
78 if (bcm47xx_nvram_getenv("bootpartition", buf, sizeof(buf)) > 0) {
79 if (!kstrtoint(buf, 0, &bootpartition))
86 static int bcm47xxpart_parse(struct mtd_info *master,
87 const struct mtd_partition **pparts,
88 struct mtd_part_parser_data *data)
90 struct mtd_partition *parts;
91 uint8_t i, curr_part = 0;
95 uint32_t blocksize = master->erasesize;
96 int trx_parts[2]; /* Array with indexes of TRX partitions */
97 int trx_num = 0; /* Number of found TRX partitions */
98 int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
102 * Some really old flashes (like AT45DB*) had smaller erasesize-s, but
103 * partitions were aligned to at least 0x1000 anyway.
105 if (blocksize < 0x1000)
109 parts = kcalloc(BCM47XXPART_MAX_PARTS, sizeof(struct mtd_partition),
114 buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
120 /* Parse block by block looking for magics */
121 for (offset = 0; offset <= master->size - blocksize;
122 offset += blocksize) {
123 /* Nothing more in higher memory on BCM47XX (MIPS) */
124 if (IS_ENABLED(CONFIG_BCM47XX) && offset >= 0x2000000)
127 if (curr_part >= BCM47XXPART_MAX_PARTS) {
128 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
132 /* Read beginning of the block */
133 err = mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
134 &bytes_read, (uint8_t *)buf);
135 if (err && !mtd_is_bitflip(err)) {
136 pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
141 /* Magic or small NVRAM at 0x400 */
142 if ((buf[0x4e0 / 4] == CFE_MAGIC && buf[0x4e4 / 4] == CFE_MAGIC) ||
143 (buf[0x400 / 4] == NVRAM_HEADER)) {
144 bcm47xxpart_add_part(&parts[curr_part++], "boot",
145 offset, MTD_WRITEABLE);
150 * board_data starts with board_id which differs across boards,
151 * but we can use 'MPFR' (hopefully) magic at 0x100
153 if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
154 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
155 offset, MTD_WRITEABLE);
159 /* Found on Huawei E970 */
160 if (buf[0x000 / 4] == FACTORY_MAGIC) {
161 bcm47xxpart_add_part(&parts[curr_part++], "factory",
162 offset, MTD_WRITEABLE);
167 if (buf[0x000 / 4] == POT_MAGIC1 &&
168 (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
169 bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
175 if (buf[0x010 / 4] == ML_MAGIC1 &&
176 buf[0x014 / 4] == ML_MAGIC2) {
177 bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
183 if (buf[0x000 / 4] == TRX_MAGIC) {
184 struct trx_header *trx;
185 uint32_t last_subpart;
188 if (trx_num >= ARRAY_SIZE(trx_parts))
189 pr_warn("No enough space to store another TRX found at 0x%X\n",
192 trx_parts[trx_num++] = curr_part;
193 bcm47xxpart_add_part(&parts[curr_part++], "firmware",
197 * Try to find TRX size. The "length" field isn't fully
198 * reliable as it could be decreased to make CRC32 cover
199 * only part of TRX data. It's commonly used as checksum
200 * can't cover e.g. ever-changing rootfs partition.
201 * Use offsets as helpers for assuming min TRX size.
203 trx = (struct trx_header *)buf;
204 last_subpart = max3(trx->offset[0], trx->offset[1],
206 trx_size = max(trx->length, last_subpart + blocksize);
209 * Skip the TRX data. Decrease offset by block size as
210 * the next loop iteration will increase it.
212 offset += roundup(trx_size, blocksize) - blocksize;
216 /* Squashfs on devices not using TRX */
217 if (le32_to_cpu(buf[0x000 / 4]) == SQUASHFS_MAGIC ||
218 buf[0x000 / 4] == SHSQ_MAGIC) {
219 bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
225 * New (ARM?) devices may have NVRAM in some middle block. Last
226 * block will be checked later, so skip it.
228 if (offset != master->size - blocksize &&
229 buf[0x000 / 4] == NVRAM_HEADER) {
230 bcm47xxpart_add_part(&parts[curr_part++], "nvram",
235 /* Read middle of the block */
236 err = mtd_read(master, offset + (blocksize / 2), 0x4, &bytes_read,
238 if (err && !mtd_is_bitflip(err)) {
239 pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
240 offset + (blocksize / 2), err);
244 /* Some devices (ex. WNDR3700v3) don't have a standard 'MPFR' */
245 if (buf[0x000 / 4] == BOARD_DATA_MAGIC2) {
246 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
247 offset, MTD_WRITEABLE);
252 /* Look for NVRAM at the end of the last block. */
253 for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
254 if (curr_part >= BCM47XXPART_MAX_PARTS) {
255 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
259 offset = master->size - possible_nvram_sizes[i];
260 err = mtd_read(master, offset, 0x4, &bytes_read,
262 if (err && !mtd_is_bitflip(err)) {
263 pr_err("mtd_read error while reading (offset 0x%X): %d\n",
269 if (buf[0] == NVRAM_HEADER) {
270 bcm47xxpart_add_part(&parts[curr_part++], "nvram",
271 master->size - blocksize, 0);
279 * Assume that partitions end at the beginning of the one they are
282 for (i = 0; i < curr_part; i++) {
283 u64 next_part_offset = (i < curr_part - 1) ?
284 parts[i + 1].offset : master->size;
286 parts[i].size = next_part_offset - parts[i].offset;
289 /* If there was TRX parse it now */
290 for (i = 0; i < trx_num; i++) {
291 struct mtd_partition *trx = &parts[trx_parts[i]];
293 if (i == bcm47xxpart_bootpartition())
294 trx->types = trx_types;
296 trx->name = "failsafe";
303 static const struct of_device_id bcm47xxpart_of_match_table[] = {
304 { .compatible = "brcm,bcm947xx-cfe-partitions" },
307 MODULE_DEVICE_TABLE(of, bcm47xxpart_of_match_table);
309 static struct mtd_part_parser bcm47xxpart_mtd_parser = {
310 .parse_fn = bcm47xxpart_parse,
311 .name = "bcm47xxpart",
312 .of_match_table = bcm47xxpart_of_match_table,
314 module_mtd_part_parser(bcm47xxpart_mtd_parser);
316 MODULE_LICENSE("GPL");
317 MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");