162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * BCM47XX MTD partitioning
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright © 2012 Rafał Miłecki <zajec5@gmail.com>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/bcm47xx_nvram.h>
962306a36Sopenharmony_ci#include <linux/module.h>
1062306a36Sopenharmony_ci#include <linux/kernel.h>
1162306a36Sopenharmony_ci#include <linux/slab.h>
1262306a36Sopenharmony_ci#include <linux/mtd/mtd.h>
1362306a36Sopenharmony_ci#include <linux/mtd/partitions.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include <uapi/linux/magic.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci/*
1862306a36Sopenharmony_ci * NAND flash on Netgear R6250 was verified to contain 15 partitions.
1962306a36Sopenharmony_ci * This will result in allocating too big array for some old devices, but the
2062306a36Sopenharmony_ci * memory will be freed soon anyway (see mtd_device_parse_register).
2162306a36Sopenharmony_ci */
2262306a36Sopenharmony_ci#define BCM47XXPART_MAX_PARTS		20
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/*
2562306a36Sopenharmony_ci * Amount of bytes we read when analyzing each block of flash memory.
2662306a36Sopenharmony_ci * Set it big enough to allow detecting partition and reading important data.
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_ci#define BCM47XXPART_BYTES_TO_READ	0x4e8
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/* Magics */
3162306a36Sopenharmony_ci#define BOARD_DATA_MAGIC		0x5246504D	/* MPFR */
3262306a36Sopenharmony_ci#define BOARD_DATA_MAGIC2		0xBD0D0BBD
3362306a36Sopenharmony_ci#define CFE_MAGIC			0x43464531	/* 1EFC */
3462306a36Sopenharmony_ci#define FACTORY_MAGIC			0x59544346	/* FCTY */
3562306a36Sopenharmony_ci#define NVRAM_HEADER			0x48534C46	/* FLSH */
3662306a36Sopenharmony_ci#define POT_MAGIC1			0x54544f50	/* POTT */
3762306a36Sopenharmony_ci#define POT_MAGIC2			0x504f		/* OP */
3862306a36Sopenharmony_ci#define ML_MAGIC1			0x39685a42
3962306a36Sopenharmony_ci#define ML_MAGIC2			0x26594131
4062306a36Sopenharmony_ci#define TRX_MAGIC			0x30524448
4162306a36Sopenharmony_ci#define SHSQ_MAGIC			0x71736873	/* shsq (weird ZTE H218N endianness) */
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistatic const char * const trx_types[] = { "trx", NULL };
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_cistruct trx_header {
4662306a36Sopenharmony_ci	uint32_t magic;
4762306a36Sopenharmony_ci	uint32_t length;
4862306a36Sopenharmony_ci	uint32_t crc32;
4962306a36Sopenharmony_ci	uint16_t flags;
5062306a36Sopenharmony_ci	uint16_t version;
5162306a36Sopenharmony_ci	uint32_t offset[3];
5262306a36Sopenharmony_ci} __packed;
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_cistatic void bcm47xxpart_add_part(struct mtd_partition *part, const char *name,
5562306a36Sopenharmony_ci				 u64 offset, uint32_t mask_flags)
5662306a36Sopenharmony_ci{
5762306a36Sopenharmony_ci	part->name = name;
5862306a36Sopenharmony_ci	part->offset = offset;
5962306a36Sopenharmony_ci	part->mask_flags = mask_flags;
6062306a36Sopenharmony_ci}
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci/**
6362306a36Sopenharmony_ci * bcm47xxpart_bootpartition - gets index of TRX partition used by bootloader
6462306a36Sopenharmony_ci *
6562306a36Sopenharmony_ci * Some devices may have more than one TRX partition. In such case one of them
6662306a36Sopenharmony_ci * is the main one and another a failsafe one. Bootloader may fallback to the
6762306a36Sopenharmony_ci * failsafe firmware if it detects corruption of the main image.
6862306a36Sopenharmony_ci *
6962306a36Sopenharmony_ci * This function provides info about currently used TRX partition. It's the one
7062306a36Sopenharmony_ci * containing kernel started by the bootloader.
7162306a36Sopenharmony_ci */
7262306a36Sopenharmony_cistatic int bcm47xxpart_bootpartition(void)
7362306a36Sopenharmony_ci{
7462306a36Sopenharmony_ci	char buf[4];
7562306a36Sopenharmony_ci	int bootpartition;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	/* Check CFE environment variable */
7862306a36Sopenharmony_ci	if (bcm47xx_nvram_getenv("bootpartition", buf, sizeof(buf)) > 0) {
7962306a36Sopenharmony_ci		if (!kstrtoint(buf, 0, &bootpartition))
8062306a36Sopenharmony_ci			return bootpartition;
8162306a36Sopenharmony_ci	}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	return 0;
8462306a36Sopenharmony_ci}
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_cistatic int bcm47xxpart_parse(struct mtd_info *master,
8762306a36Sopenharmony_ci			     const struct mtd_partition **pparts,
8862306a36Sopenharmony_ci			     struct mtd_part_parser_data *data)
8962306a36Sopenharmony_ci{
9062306a36Sopenharmony_ci	struct mtd_partition *parts;
9162306a36Sopenharmony_ci	uint8_t i, curr_part = 0;
9262306a36Sopenharmony_ci	uint32_t *buf;
9362306a36Sopenharmony_ci	size_t bytes_read;
9462306a36Sopenharmony_ci	uint32_t offset;
9562306a36Sopenharmony_ci	uint32_t blocksize = master->erasesize;
9662306a36Sopenharmony_ci	int trx_parts[2]; /* Array with indexes of TRX partitions */
9762306a36Sopenharmony_ci	int trx_num = 0; /* Number of found TRX partitions */
9862306a36Sopenharmony_ci	int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
9962306a36Sopenharmony_ci	int err;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	/*
10262306a36Sopenharmony_ci	 * Some really old flashes (like AT45DB*) had smaller erasesize-s, but
10362306a36Sopenharmony_ci	 * partitions were aligned to at least 0x1000 anyway.
10462306a36Sopenharmony_ci	 */
10562306a36Sopenharmony_ci	if (blocksize < 0x1000)
10662306a36Sopenharmony_ci		blocksize = 0x1000;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	/* Alloc */
10962306a36Sopenharmony_ci	parts = kcalloc(BCM47XXPART_MAX_PARTS, sizeof(struct mtd_partition),
11062306a36Sopenharmony_ci			GFP_KERNEL);
11162306a36Sopenharmony_ci	if (!parts)
11262306a36Sopenharmony_ci		return -ENOMEM;
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
11562306a36Sopenharmony_ci	if (!buf) {
11662306a36Sopenharmony_ci		kfree(parts);
11762306a36Sopenharmony_ci		return -ENOMEM;
11862306a36Sopenharmony_ci	}
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	/* Parse block by block looking for magics */
12162306a36Sopenharmony_ci	for (offset = 0; offset <= master->size - blocksize;
12262306a36Sopenharmony_ci	     offset += blocksize) {
12362306a36Sopenharmony_ci		/* Nothing more in higher memory on BCM47XX (MIPS) */
12462306a36Sopenharmony_ci		if (IS_ENABLED(CONFIG_BCM47XX) && offset >= 0x2000000)
12562306a36Sopenharmony_ci			break;
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci		if (curr_part >= BCM47XXPART_MAX_PARTS) {
12862306a36Sopenharmony_ci			pr_warn("Reached maximum number of partitions, scanning stopped!\n");
12962306a36Sopenharmony_ci			break;
13062306a36Sopenharmony_ci		}
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci		/* Read beginning of the block */
13362306a36Sopenharmony_ci		err = mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
13462306a36Sopenharmony_ci			       &bytes_read, (uint8_t *)buf);
13562306a36Sopenharmony_ci		if (err && !mtd_is_bitflip(err)) {
13662306a36Sopenharmony_ci			pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
13762306a36Sopenharmony_ci			       offset, err);
13862306a36Sopenharmony_ci			continue;
13962306a36Sopenharmony_ci		}
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci		/* Magic or small NVRAM at 0x400 */
14262306a36Sopenharmony_ci		if ((buf[0x4e0 / 4] == CFE_MAGIC && buf[0x4e4 / 4] == CFE_MAGIC) ||
14362306a36Sopenharmony_ci		    (buf[0x400 / 4] == NVRAM_HEADER)) {
14462306a36Sopenharmony_ci			bcm47xxpart_add_part(&parts[curr_part++], "boot",
14562306a36Sopenharmony_ci					     offset, MTD_WRITEABLE);
14662306a36Sopenharmony_ci			continue;
14762306a36Sopenharmony_ci		}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci		/*
15062306a36Sopenharmony_ci		 * board_data starts with board_id which differs across boards,
15162306a36Sopenharmony_ci		 * but we can use 'MPFR' (hopefully) magic at 0x100
15262306a36Sopenharmony_ci		 */
15362306a36Sopenharmony_ci		if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
15462306a36Sopenharmony_ci			bcm47xxpart_add_part(&parts[curr_part++], "board_data",
15562306a36Sopenharmony_ci					     offset, MTD_WRITEABLE);
15662306a36Sopenharmony_ci			continue;
15762306a36Sopenharmony_ci		}
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci		/* Found on Huawei E970 */
16062306a36Sopenharmony_ci		if (buf[0x000 / 4] == FACTORY_MAGIC) {
16162306a36Sopenharmony_ci			bcm47xxpart_add_part(&parts[curr_part++], "factory",
16262306a36Sopenharmony_ci					     offset, MTD_WRITEABLE);
16362306a36Sopenharmony_ci			continue;
16462306a36Sopenharmony_ci		}
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci		/* POT(TOP) */
16762306a36Sopenharmony_ci		if (buf[0x000 / 4] == POT_MAGIC1 &&
16862306a36Sopenharmony_ci		    (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
16962306a36Sopenharmony_ci			bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
17062306a36Sopenharmony_ci					     MTD_WRITEABLE);
17162306a36Sopenharmony_ci			continue;
17262306a36Sopenharmony_ci		}
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci		/* ML */
17562306a36Sopenharmony_ci		if (buf[0x010 / 4] == ML_MAGIC1 &&
17662306a36Sopenharmony_ci		    buf[0x014 / 4] == ML_MAGIC2) {
17762306a36Sopenharmony_ci			bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
17862306a36Sopenharmony_ci					     MTD_WRITEABLE);
17962306a36Sopenharmony_ci			continue;
18062306a36Sopenharmony_ci		}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci		/* TRX */
18362306a36Sopenharmony_ci		if (buf[0x000 / 4] == TRX_MAGIC) {
18462306a36Sopenharmony_ci			struct trx_header *trx;
18562306a36Sopenharmony_ci			uint32_t last_subpart;
18662306a36Sopenharmony_ci			uint32_t trx_size;
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci			if (trx_num >= ARRAY_SIZE(trx_parts))
18962306a36Sopenharmony_ci				pr_warn("No enough space to store another TRX found at 0x%X\n",
19062306a36Sopenharmony_ci					offset);
19162306a36Sopenharmony_ci			else
19262306a36Sopenharmony_ci				trx_parts[trx_num++] = curr_part;
19362306a36Sopenharmony_ci			bcm47xxpart_add_part(&parts[curr_part++], "firmware",
19462306a36Sopenharmony_ci					     offset, 0);
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci			/*
19762306a36Sopenharmony_ci			 * Try to find TRX size. The "length" field isn't fully
19862306a36Sopenharmony_ci			 * reliable as it could be decreased to make CRC32 cover
19962306a36Sopenharmony_ci			 * only part of TRX data. It's commonly used as checksum
20062306a36Sopenharmony_ci			 * can't cover e.g. ever-changing rootfs partition.
20162306a36Sopenharmony_ci			 * Use offsets as helpers for assuming min TRX size.
20262306a36Sopenharmony_ci			 */
20362306a36Sopenharmony_ci			trx = (struct trx_header *)buf;
20462306a36Sopenharmony_ci			last_subpart = max3(trx->offset[0], trx->offset[1],
20562306a36Sopenharmony_ci					    trx->offset[2]);
20662306a36Sopenharmony_ci			trx_size = max(trx->length, last_subpart + blocksize);
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci			/*
20962306a36Sopenharmony_ci			 * Skip the TRX data. Decrease offset by block size as
21062306a36Sopenharmony_ci			 * the next loop iteration will increase it.
21162306a36Sopenharmony_ci			 */
21262306a36Sopenharmony_ci			offset += roundup(trx_size, blocksize) - blocksize;
21362306a36Sopenharmony_ci			continue;
21462306a36Sopenharmony_ci		}
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci		/* Squashfs on devices not using TRX */
21762306a36Sopenharmony_ci		if (le32_to_cpu(buf[0x000 / 4]) == SQUASHFS_MAGIC ||
21862306a36Sopenharmony_ci		    buf[0x000 / 4] == SHSQ_MAGIC) {
21962306a36Sopenharmony_ci			bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
22062306a36Sopenharmony_ci					     offset, 0);
22162306a36Sopenharmony_ci			continue;
22262306a36Sopenharmony_ci		}
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci		/*
22562306a36Sopenharmony_ci		 * New (ARM?) devices may have NVRAM in some middle block. Last
22662306a36Sopenharmony_ci		 * block will be checked later, so skip it.
22762306a36Sopenharmony_ci		 */
22862306a36Sopenharmony_ci		if (offset != master->size - blocksize &&
22962306a36Sopenharmony_ci		    buf[0x000 / 4] == NVRAM_HEADER) {
23062306a36Sopenharmony_ci			bcm47xxpart_add_part(&parts[curr_part++], "nvram",
23162306a36Sopenharmony_ci					     offset, 0);
23262306a36Sopenharmony_ci			continue;
23362306a36Sopenharmony_ci		}
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci		/* Read middle of the block */
23662306a36Sopenharmony_ci		err = mtd_read(master, offset + (blocksize / 2), 0x4, &bytes_read,
23762306a36Sopenharmony_ci			       (uint8_t *)buf);
23862306a36Sopenharmony_ci		if (err && !mtd_is_bitflip(err)) {
23962306a36Sopenharmony_ci			pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
24062306a36Sopenharmony_ci			       offset + (blocksize / 2), err);
24162306a36Sopenharmony_ci			continue;
24262306a36Sopenharmony_ci		}
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci		/* Some devices (ex. WNDR3700v3) don't have a standard 'MPFR' */
24562306a36Sopenharmony_ci		if (buf[0x000 / 4] == BOARD_DATA_MAGIC2) {
24662306a36Sopenharmony_ci			bcm47xxpart_add_part(&parts[curr_part++], "board_data",
24762306a36Sopenharmony_ci					     offset, MTD_WRITEABLE);
24862306a36Sopenharmony_ci			continue;
24962306a36Sopenharmony_ci		}
25062306a36Sopenharmony_ci	}
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci	/* Look for NVRAM at the end of the last block. */
25362306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
25462306a36Sopenharmony_ci		if (curr_part >= BCM47XXPART_MAX_PARTS) {
25562306a36Sopenharmony_ci			pr_warn("Reached maximum number of partitions, scanning stopped!\n");
25662306a36Sopenharmony_ci			break;
25762306a36Sopenharmony_ci		}
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci		offset = master->size - possible_nvram_sizes[i];
26062306a36Sopenharmony_ci		err = mtd_read(master, offset, 0x4, &bytes_read,
26162306a36Sopenharmony_ci			       (uint8_t *)buf);
26262306a36Sopenharmony_ci		if (err && !mtd_is_bitflip(err)) {
26362306a36Sopenharmony_ci			pr_err("mtd_read error while reading (offset 0x%X): %d\n",
26462306a36Sopenharmony_ci			       offset, err);
26562306a36Sopenharmony_ci			continue;
26662306a36Sopenharmony_ci		}
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci		/* Standard NVRAM */
26962306a36Sopenharmony_ci		if (buf[0] == NVRAM_HEADER) {
27062306a36Sopenharmony_ci			bcm47xxpart_add_part(&parts[curr_part++], "nvram",
27162306a36Sopenharmony_ci					     master->size - blocksize, 0);
27262306a36Sopenharmony_ci			break;
27362306a36Sopenharmony_ci		}
27462306a36Sopenharmony_ci	}
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci	kfree(buf);
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci	/*
27962306a36Sopenharmony_ci	 * Assume that partitions end at the beginning of the one they are
28062306a36Sopenharmony_ci	 * followed by.
28162306a36Sopenharmony_ci	 */
28262306a36Sopenharmony_ci	for (i = 0; i < curr_part; i++) {
28362306a36Sopenharmony_ci		u64 next_part_offset = (i < curr_part - 1) ?
28462306a36Sopenharmony_ci				       parts[i + 1].offset : master->size;
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci		parts[i].size = next_part_offset - parts[i].offset;
28762306a36Sopenharmony_ci	}
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci	/* If there was TRX parse it now */
29062306a36Sopenharmony_ci	for (i = 0; i < trx_num; i++) {
29162306a36Sopenharmony_ci		struct mtd_partition *trx = &parts[trx_parts[i]];
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci		if (i == bcm47xxpart_bootpartition())
29462306a36Sopenharmony_ci			trx->types = trx_types;
29562306a36Sopenharmony_ci		else
29662306a36Sopenharmony_ci			trx->name = "failsafe";
29762306a36Sopenharmony_ci	}
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci	*pparts = parts;
30062306a36Sopenharmony_ci	return curr_part;
30162306a36Sopenharmony_ci};
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_cistatic const struct of_device_id bcm47xxpart_of_match_table[] = {
30462306a36Sopenharmony_ci	{ .compatible = "brcm,bcm947xx-cfe-partitions" },
30562306a36Sopenharmony_ci	{},
30662306a36Sopenharmony_ci};
30762306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, bcm47xxpart_of_match_table);
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_cistatic struct mtd_part_parser bcm47xxpart_mtd_parser = {
31062306a36Sopenharmony_ci	.parse_fn = bcm47xxpart_parse,
31162306a36Sopenharmony_ci	.name = "bcm47xxpart",
31262306a36Sopenharmony_ci	.of_match_table = bcm47xxpart_of_match_table,
31362306a36Sopenharmony_ci};
31462306a36Sopenharmony_cimodule_mtd_part_parser(bcm47xxpart_mtd_parser);
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ciMODULE_LICENSE("GPL");
31762306a36Sopenharmony_ciMODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");
318