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/bcm47xx_nvram.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/partitions.h>
19 #include <uapi/linux/magic.h>
22 * NAND flash on Netgear R6250 was verified to contain 15 partitions.
23 * This will result in allocating too big array for some old devices, but the
24 * memory will be freed soon anyway (see mtd_device_parse_register).
26 #define BCM47XXPART_MAX_PARTS 20
29 * Amount of bytes we read when analyzing each block of flash memory.
30 * Set it big enough to allow detecting partition and reading important data.
32 #define BCM47XXPART_BYTES_TO_READ 0x4e8
35 #define BOARD_DATA_MAGIC 0x5246504D /* MPFR */
36 #define BOARD_DATA_MAGIC2 0xBD0D0BBD
37 #define CFE_MAGIC 0x43464531 /* 1EFC */
38 #define FACTORY_MAGIC 0x59544346 /* FCTY */
39 #define NVRAM_HEADER 0x48534C46 /* FLSH */
40 #define POT_MAGIC1 0x54544f50 /* POTT */
41 #define POT_MAGIC2 0x504f /* OP */
42 #define ML_MAGIC1 0x39685a42
43 #define ML_MAGIC2 0x26594131
44 #define TRX_MAGIC 0x30524448
45 #define SHSQ_MAGIC 0x71736873 /* shsq (weird ZTE H218N endianness) */
46 #define UBI_EC_MAGIC 0x23494255 /* UBI# */
57 static void bcm47xxpart_add_part(struct mtd_partition *part, const char *name,
58 u64 offset, uint32_t mask_flags)
61 part->offset = offset;
62 part->mask_flags = mask_flags;
65 static const char *bcm47xxpart_trx_data_part_name(struct mtd_info *master,
72 err = mtd_read(master, offset, sizeof(buf), &bytes_read,
74 if (err && !mtd_is_bitflip(err)) {
75 pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
80 if (buf == UBI_EC_MAGIC)
87 static int bcm47xxpart_parse_trx(struct mtd_info *master,
88 struct mtd_partition *trx,
89 struct mtd_partition *parts,
92 struct trx_header header;
98 pr_warn("No enough space to add TRX partitions!\n");
102 err = mtd_read(master, trx->offset, sizeof(header), &bytes_read,
104 if (err && !mtd_is_bitflip(err)) {
105 pr_err("mtd_read error while reading TRX header: %d\n", err);
111 /* We have LZMA loader if offset[2] points to sth */
112 if (header.offset[2]) {
113 bcm47xxpart_add_part(&parts[curr_part++], "loader",
114 trx->offset + header.offset[i], 0);
118 if (header.offset[i]) {
119 bcm47xxpart_add_part(&parts[curr_part++], "linux",
120 trx->offset + header.offset[i], 0);
124 if (header.offset[i]) {
125 size_t offset = trx->offset + header.offset[i];
126 const char *name = bcm47xxpart_trx_data_part_name(master,
129 bcm47xxpart_add_part(&parts[curr_part++], name, offset, 0);
134 * Assume that every partition ends at the beginning of the one it is
137 for (i = 0; i < curr_part; i++) {
138 u64 next_part_offset = (i < curr_part - 1) ?
139 parts[i + 1].offset :
140 trx->offset + trx->size;
142 parts[i].size = next_part_offset - parts[i].offset;
149 * bcm47xxpart_bootpartition - gets index of TRX partition used by bootloader
151 * Some devices may have more than one TRX partition. In such case one of them
152 * is the main one and another a failsafe one. Bootloader may fallback to the
153 * failsafe firmware if it detects corruption of the main image.
155 * This function provides info about currently used TRX partition. It's the one
156 * containing kernel started by the bootloader.
158 static int bcm47xxpart_bootpartition(void)
163 /* Check CFE environment variable */
164 if (bcm47xx_nvram_getenv("bootpartition", buf, sizeof(buf)) > 0) {
165 if (!kstrtoint(buf, 0, &bootpartition))
166 return bootpartition;
172 static int bcm47xxpart_parse(struct mtd_info *master,
173 const struct mtd_partition **pparts,
174 struct mtd_part_parser_data *data)
176 struct mtd_partition *parts;
177 uint8_t i, curr_part = 0;
181 uint32_t blocksize = master->erasesize;
182 int trx_parts[2]; /* Array with indexes of TRX partitions */
183 int trx_num = 0; /* Number of found TRX partitions */
184 int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
188 * Some really old flashes (like AT45DB*) had smaller erasesize-s, but
189 * partitions were aligned to at least 0x1000 anyway.
191 if (blocksize < 0x1000)
195 parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
200 buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
206 /* Parse block by block looking for magics */
207 for (offset = 0; offset <= master->size - blocksize;
208 offset += blocksize) {
209 /* Nothing more in higher memory on BCM47XX (MIPS) */
210 if (IS_ENABLED(CONFIG_BCM47XX) && offset >= 0x2000000)
213 if (curr_part >= BCM47XXPART_MAX_PARTS) {
214 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
218 /* Read beginning of the block */
219 err = mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
220 &bytes_read, (uint8_t *)buf);
221 if (err && !mtd_is_bitflip(err)) {
222 pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
227 /* Magic or small NVRAM at 0x400 */
228 if ((buf[0x4e0 / 4] == CFE_MAGIC && buf[0x4e4 / 4] == CFE_MAGIC) ||
229 (buf[0x400 / 4] == NVRAM_HEADER)) {
230 bcm47xxpart_add_part(&parts[curr_part++], "boot",
231 offset, MTD_WRITEABLE);
236 * board_data starts with board_id which differs across boards,
237 * but we can use 'MPFR' (hopefully) magic at 0x100
239 if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
240 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
241 offset, MTD_WRITEABLE);
245 /* Found on Huawei E970 */
246 if (buf[0x000 / 4] == FACTORY_MAGIC) {
247 bcm47xxpart_add_part(&parts[curr_part++], "factory",
248 offset, MTD_WRITEABLE);
253 if (buf[0x000 / 4] == POT_MAGIC1 &&
254 (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
255 bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
261 if (buf[0x010 / 4] == ML_MAGIC1 &&
262 buf[0x014 / 4] == ML_MAGIC2) {
263 bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
269 if (buf[0x000 / 4] == TRX_MAGIC) {
270 struct trx_header *trx;
272 if (trx_num >= ARRAY_SIZE(trx_parts))
273 pr_warn("No enough space to store another TRX found at 0x%X\n",
276 trx_parts[trx_num++] = curr_part;
277 bcm47xxpart_add_part(&parts[curr_part++], "firmware",
280 /* Jump to the end of TRX */
281 trx = (struct trx_header *)buf;
282 offset = roundup(offset + trx->length, blocksize);
283 /* Next loop iteration will increase the offset */
288 /* Squashfs on devices not using TRX */
289 if (le32_to_cpu(buf[0x000 / 4]) == SQUASHFS_MAGIC ||
290 buf[0x000 / 4] == SHSQ_MAGIC) {
291 bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
297 * New (ARM?) devices may have NVRAM in some middle block. Last
298 * block will be checked later, so skip it.
300 if (offset != master->size - blocksize &&
301 buf[0x000 / 4] == NVRAM_HEADER) {
302 bcm47xxpart_add_part(&parts[curr_part++], "nvram",
307 /* Read middle of the block */
308 err = mtd_read(master, offset + 0x8000, 0x4, &bytes_read,
310 if (err && !mtd_is_bitflip(err)) {
311 pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
316 /* Some devices (ex. WNDR3700v3) don't have a standard 'MPFR' */
317 if (buf[0x000 / 4] == BOARD_DATA_MAGIC2) {
318 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
319 offset, MTD_WRITEABLE);
324 /* Look for NVRAM at the end of the last block. */
325 for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
326 if (curr_part >= BCM47XXPART_MAX_PARTS) {
327 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
331 offset = master->size - possible_nvram_sizes[i];
332 err = mtd_read(master, offset, 0x4, &bytes_read,
334 if (err && !mtd_is_bitflip(err)) {
335 pr_err("mtd_read error while reading (offset 0x%X): %d\n",
341 if (buf[0] == NVRAM_HEADER) {
342 bcm47xxpart_add_part(&parts[curr_part++], "nvram",
343 master->size - blocksize, 0);
351 * Assume that partitions end at the beginning of the one they are
354 for (i = 0; i < curr_part; i++) {
355 u64 next_part_offset = (i < curr_part - 1) ?
356 parts[i + 1].offset : master->size;
358 parts[i].size = next_part_offset - parts[i].offset;
361 /* If there was TRX parse it now */
362 for (i = 0; i < trx_num; i++) {
363 struct mtd_partition *trx = &parts[trx_parts[i]];
365 if (i == bcm47xxpart_bootpartition()) {
368 num_parts = bcm47xxpart_parse_trx(master, trx,
370 BCM47XXPART_MAX_PARTS - curr_part);
372 curr_part += num_parts;
374 trx->name = "failsafe";
382 static struct mtd_part_parser bcm47xxpart_mtd_parser = {
383 .parse_fn = bcm47xxpart_parse,
384 .name = "bcm47xxpart",
386 module_mtd_part_parser(bcm47xxpart_mtd_parser);
388 MODULE_LICENSE("GPL");
389 MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");