2012-08-29 23:41:16 -06:00
|
|
|
/*
|
|
|
|
* BCM47XX MTD partitioning
|
|
|
|
*
|
|
|
|
* Copyright © 2012 Rafał Miłecki <zajec5@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-01-10 15:15:25 -07:00
|
|
|
#include <linux/bcm47xx_nvram.h>
|
2012-08-29 23:41:16 -06:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/mtd/mtd.h>
|
|
|
|
#include <linux/mtd/partitions.h>
|
|
|
|
|
2014-12-16 01:50:25 -07:00
|
|
|
#include <uapi/linux/magic.h>
|
|
|
|
|
2014-10-02 03:48:46 -06:00
|
|
|
/*
|
|
|
|
* NAND flash on Netgear R6250 was verified to contain 15 partitions.
|
|
|
|
* This will result in allocating too big array for some old devices, but the
|
|
|
|
* memory will be freed soon anyway (see mtd_device_parse_register).
|
|
|
|
*/
|
|
|
|
#define BCM47XXPART_MAX_PARTS 20
|
2012-08-29 23:41:16 -06:00
|
|
|
|
2013-03-07 01:02:38 -07:00
|
|
|
/*
|
|
|
|
* Amount of bytes we read when analyzing each block of flash memory.
|
|
|
|
* Set it big enough to allow detecting partition and reading important data.
|
|
|
|
*/
|
2013-12-21 11:39:11 -07:00
|
|
|
#define BCM47XXPART_BYTES_TO_READ 0x4e8
|
2013-03-07 01:02:38 -07:00
|
|
|
|
2012-08-29 23:41:16 -06:00
|
|
|
/* Magics */
|
|
|
|
#define BOARD_DATA_MAGIC 0x5246504D /* MPFR */
|
2013-12-21 11:39:12 -07:00
|
|
|
#define BOARD_DATA_MAGIC2 0xBD0D0BBD
|
2013-12-21 11:39:11 -07:00
|
|
|
#define CFE_MAGIC 0x43464531 /* 1EFC */
|
2013-10-21 14:35:34 -06:00
|
|
|
#define FACTORY_MAGIC 0x59544346 /* FCTY */
|
2014-02-28 10:02:01 -07:00
|
|
|
#define NVRAM_HEADER 0x48534C46 /* FLSH */
|
2012-08-29 23:41:16 -06:00
|
|
|
#define POT_MAGIC1 0x54544f50 /* POTT */
|
|
|
|
#define POT_MAGIC2 0x504f /* OP */
|
|
|
|
#define ML_MAGIC1 0x39685a42
|
|
|
|
#define ML_MAGIC2 0x26594131
|
|
|
|
#define TRX_MAGIC 0x30524448
|
2014-12-16 01:50:25 -07:00
|
|
|
#define SHSQ_MAGIC 0x71736873 /* shsq (weird ZTE H218N endianness) */
|
2017-06-21 00:26:47 -06:00
|
|
|
|
|
|
|
static const char * const trx_types[] = { "trx", NULL };
|
2012-08-29 23:41:16 -06:00
|
|
|
|
|
|
|
struct trx_header {
|
|
|
|
uint32_t magic;
|
|
|
|
uint32_t length;
|
|
|
|
uint32_t crc32;
|
|
|
|
uint16_t flags;
|
|
|
|
uint16_t version;
|
|
|
|
uint32_t offset[3];
|
|
|
|
} __packed;
|
|
|
|
|
2014-12-01 10:50:20 -07:00
|
|
|
static void bcm47xxpart_add_part(struct mtd_partition *part, const char *name,
|
2012-08-29 23:41:16 -06:00
|
|
|
u64 offset, uint32_t mask_flags)
|
|
|
|
{
|
|
|
|
part->name = name;
|
|
|
|
part->offset = offset;
|
|
|
|
part->mask_flags = mask_flags;
|
|
|
|
}
|
|
|
|
|
2017-01-10 15:15:25 -07:00
|
|
|
/**
|
|
|
|
* bcm47xxpart_bootpartition - gets index of TRX partition used by bootloader
|
|
|
|
*
|
|
|
|
* Some devices may have more than one TRX partition. In such case one of them
|
|
|
|
* is the main one and another a failsafe one. Bootloader may fallback to the
|
|
|
|
* failsafe firmware if it detects corruption of the main image.
|
|
|
|
*
|
|
|
|
* This function provides info about currently used TRX partition. It's the one
|
|
|
|
* containing kernel started by the bootloader.
|
|
|
|
*/
|
|
|
|
static int bcm47xxpart_bootpartition(void)
|
|
|
|
{
|
|
|
|
char buf[4];
|
|
|
|
int bootpartition;
|
|
|
|
|
|
|
|
/* Check CFE environment variable */
|
|
|
|
if (bcm47xx_nvram_getenv("bootpartition", buf, sizeof(buf)) > 0) {
|
|
|
|
if (!kstrtoint(buf, 0, &bootpartition))
|
|
|
|
return bootpartition;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-29 23:41:16 -06:00
|
|
|
static int bcm47xxpart_parse(struct mtd_info *master,
|
2015-12-04 16:25:14 -07:00
|
|
|
const struct mtd_partition **pparts,
|
2012-08-29 23:41:16 -06:00
|
|
|
struct mtd_part_parser_data *data)
|
|
|
|
{
|
|
|
|
struct mtd_partition *parts;
|
|
|
|
uint8_t i, curr_part = 0;
|
|
|
|
uint32_t *buf;
|
|
|
|
size_t bytes_read;
|
|
|
|
uint32_t offset;
|
2013-01-24 09:39:58 -07:00
|
|
|
uint32_t blocksize = master->erasesize;
|
2017-01-10 15:15:25 -07:00
|
|
|
int trx_parts[2]; /* Array with indexes of TRX partitions */
|
|
|
|
int trx_num = 0; /* Number of found TRX partitions */
|
2013-03-07 01:02:39 -07:00
|
|
|
int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
|
2015-12-06 03:31:38 -07:00
|
|
|
int err;
|
2012-08-29 23:41:16 -06:00
|
|
|
|
2014-12-08 10:45:00 -07:00
|
|
|
/*
|
|
|
|
* Some really old flashes (like AT45DB*) had smaller erasesize-s, but
|
|
|
|
* partitions were aligned to at least 0x1000 anyway.
|
|
|
|
*/
|
|
|
|
if (blocksize < 0x1000)
|
|
|
|
blocksize = 0x1000;
|
2012-08-29 23:41:16 -06:00
|
|
|
|
|
|
|
/* Alloc */
|
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 15:03:40 -06:00
|
|
|
parts = kcalloc(BCM47XXPART_MAX_PARTS, sizeof(struct mtd_partition),
|
2012-08-29 23:41:16 -06:00
|
|
|
GFP_KERNEL);
|
2013-10-13 14:53:49 -06:00
|
|
|
if (!parts)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-03-07 01:02:38 -07:00
|
|
|
buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
|
2013-10-13 14:53:49 -06:00
|
|
|
if (!buf) {
|
|
|
|
kfree(parts);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2012-08-29 23:41:16 -06:00
|
|
|
|
|
|
|
/* Parse block by block looking for magics */
|
|
|
|
for (offset = 0; offset <= master->size - blocksize;
|
|
|
|
offset += blocksize) {
|
2015-12-04 18:09:43 -07:00
|
|
|
/* Nothing more in higher memory on BCM47XX (MIPS) */
|
2016-08-03 14:45:50 -06:00
|
|
|
if (IS_ENABLED(CONFIG_BCM47XX) && offset >= 0x2000000)
|
2012-08-29 23:41:16 -06:00
|
|
|
break;
|
|
|
|
|
2014-02-26 06:02:06 -07:00
|
|
|
if (curr_part >= BCM47XXPART_MAX_PARTS) {
|
2012-08-29 23:41:16 -06:00
|
|
|
pr_warn("Reached maximum number of partitions, scanning stopped!\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read beginning of the block */
|
2015-12-06 03:31:38 -07:00
|
|
|
err = mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
|
|
|
|
&bytes_read, (uint8_t *)buf);
|
|
|
|
if (err && !mtd_is_bitflip(err)) {
|
|
|
|
pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
|
|
|
|
offset, err);
|
2012-08-29 23:41:16 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-12-21 11:39:11 -07:00
|
|
|
/* Magic or small NVRAM at 0x400 */
|
|
|
|
if ((buf[0x4e0 / 4] == CFE_MAGIC && buf[0x4e4 / 4] == CFE_MAGIC) ||
|
|
|
|
(buf[0x400 / 4] == NVRAM_HEADER)) {
|
2012-08-29 23:41:16 -06:00
|
|
|
bcm47xxpart_add_part(&parts[curr_part++], "boot",
|
|
|
|
offset, MTD_WRITEABLE);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* board_data starts with board_id which differs across boards,
|
|
|
|
* but we can use 'MPFR' (hopefully) magic at 0x100
|
|
|
|
*/
|
|
|
|
if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
|
|
|
|
bcm47xxpart_add_part(&parts[curr_part++], "board_data",
|
|
|
|
offset, MTD_WRITEABLE);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-10-21 14:35:34 -06:00
|
|
|
/* Found on Huawei E970 */
|
|
|
|
if (buf[0x000 / 4] == FACTORY_MAGIC) {
|
|
|
|
bcm47xxpart_add_part(&parts[curr_part++], "factory",
|
|
|
|
offset, MTD_WRITEABLE);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-08-29 23:41:16 -06:00
|
|
|
/* POT(TOP) */
|
|
|
|
if (buf[0x000 / 4] == POT_MAGIC1 &&
|
|
|
|
(buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
|
|
|
|
bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
|
|
|
|
MTD_WRITEABLE);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ML */
|
|
|
|
if (buf[0x010 / 4] == ML_MAGIC1 &&
|
|
|
|
buf[0x014 / 4] == ML_MAGIC2) {
|
|
|
|
bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
|
|
|
|
MTD_WRITEABLE);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TRX */
|
|
|
|
if (buf[0x000 / 4] == TRX_MAGIC) {
|
2017-01-10 15:15:24 -07:00
|
|
|
struct trx_header *trx;
|
2018-04-11 23:24:52 -06:00
|
|
|
uint32_t last_subpart;
|
|
|
|
uint32_t trx_size;
|
2012-08-29 23:41:16 -06:00
|
|
|
|
2017-01-10 15:15:25 -07:00
|
|
|
if (trx_num >= ARRAY_SIZE(trx_parts))
|
|
|
|
pr_warn("No enough space to store another TRX found at 0x%X\n",
|
|
|
|
offset);
|
|
|
|
else
|
|
|
|
trx_parts[trx_num++] = curr_part;
|
2013-01-06 08:08:36 -07:00
|
|
|
bcm47xxpart_add_part(&parts[curr_part++], "firmware",
|
|
|
|
offset, 0);
|
|
|
|
|
2018-04-11 23:24:52 -06:00
|
|
|
/*
|
|
|
|
* Try to find TRX size. The "length" field isn't fully
|
|
|
|
* reliable as it could be decreased to make CRC32 cover
|
|
|
|
* only part of TRX data. It's commonly used as checksum
|
|
|
|
* can't cover e.g. ever-changing rootfs partition.
|
|
|
|
* Use offsets as helpers for assuming min TRX size.
|
|
|
|
*/
|
2017-01-10 15:15:24 -07:00
|
|
|
trx = (struct trx_header *)buf;
|
2018-04-11 23:24:52 -06:00
|
|
|
last_subpart = max3(trx->offset[0], trx->offset[1],
|
|
|
|
trx->offset[2]);
|
|
|
|
trx_size = max(trx->length, last_subpart + blocksize);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip the TRX data. Decrease offset by block size as
|
|
|
|
* the next loop iteration will increase it.
|
|
|
|
*/
|
|
|
|
offset += roundup(trx_size, blocksize) - blocksize;
|
2012-08-29 23:41:16 -06:00
|
|
|
continue;
|
|
|
|
}
|
2013-10-21 14:34:37 -06:00
|
|
|
|
|
|
|
/* Squashfs on devices not using TRX */
|
2014-12-16 01:50:25 -07:00
|
|
|
if (le32_to_cpu(buf[0x000 / 4]) == SQUASHFS_MAGIC ||
|
|
|
|
buf[0x000 / 4] == SHSQ_MAGIC) {
|
2013-10-21 14:34:37 -06:00
|
|
|
bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
|
|
|
|
offset, 0);
|
|
|
|
continue;
|
2014-08-18 12:20:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* New (ARM?) devices may have NVRAM in some middle block. Last
|
|
|
|
* block will be checked later, so skip it.
|
|
|
|
*/
|
|
|
|
if (offset != master->size - blocksize &&
|
|
|
|
buf[0x000 / 4] == NVRAM_HEADER) {
|
|
|
|
bcm47xxpart_add_part(&parts[curr_part++], "nvram",
|
|
|
|
offset, 0);
|
|
|
|
continue;
|
2013-10-21 14:34:37 -06:00
|
|
|
}
|
2013-12-21 11:39:12 -07:00
|
|
|
|
|
|
|
/* Read middle of the block */
|
2015-12-06 03:31:38 -07:00
|
|
|
err = mtd_read(master, offset + 0x8000, 0x4, &bytes_read,
|
|
|
|
(uint8_t *)buf);
|
|
|
|
if (err && !mtd_is_bitflip(err)) {
|
|
|
|
pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
|
|
|
|
offset, err);
|
2013-12-21 11:39:12 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Some devices (ex. WNDR3700v3) don't have a standard 'MPFR' */
|
|
|
|
if (buf[0x000 / 4] == BOARD_DATA_MAGIC2) {
|
|
|
|
bcm47xxpart_add_part(&parts[curr_part++], "board_data",
|
|
|
|
offset, MTD_WRITEABLE);
|
|
|
|
continue;
|
|
|
|
}
|
2012-08-29 23:41:16 -06:00
|
|
|
}
|
2013-03-07 01:02:39 -07:00
|
|
|
|
|
|
|
/* Look for NVRAM at the end of the last block. */
|
|
|
|
for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
|
2014-02-26 06:02:06 -07:00
|
|
|
if (curr_part >= BCM47XXPART_MAX_PARTS) {
|
2013-03-07 01:02:39 -07:00
|
|
|
pr_warn("Reached maximum number of partitions, scanning stopped!\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = master->size - possible_nvram_sizes[i];
|
2015-12-06 03:31:38 -07:00
|
|
|
err = mtd_read(master, offset, 0x4, &bytes_read,
|
|
|
|
(uint8_t *)buf);
|
|
|
|
if (err && !mtd_is_bitflip(err)) {
|
|
|
|
pr_err("mtd_read error while reading (offset 0x%X): %d\n",
|
|
|
|
offset, err);
|
2013-03-07 01:02:39 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Standard NVRAM */
|
|
|
|
if (buf[0] == NVRAM_HEADER) {
|
|
|
|
bcm47xxpart_add_part(&parts[curr_part++], "nvram",
|
|
|
|
master->size - blocksize, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-29 23:41:16 -06:00
|
|
|
kfree(buf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assume that partitions end at the beginning of the one they are
|
|
|
|
* followed by.
|
|
|
|
*/
|
2013-01-06 08:08:35 -07:00
|
|
|
for (i = 0; i < curr_part; i++) {
|
|
|
|
u64 next_part_offset = (i < curr_part - 1) ?
|
|
|
|
parts[i + 1].offset : master->size;
|
|
|
|
|
|
|
|
parts[i].size = next_part_offset - parts[i].offset;
|
2017-01-10 15:15:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If there was TRX parse it now */
|
2017-01-10 15:15:25 -07:00
|
|
|
for (i = 0; i < trx_num; i++) {
|
|
|
|
struct mtd_partition *trx = &parts[trx_parts[i]];
|
|
|
|
|
2017-06-21 00:26:47 -06:00
|
|
|
if (i == bcm47xxpart_bootpartition())
|
|
|
|
trx->types = trx_types;
|
|
|
|
else
|
2017-01-10 15:15:25 -07:00
|
|
|
trx->name = "failsafe";
|
2013-01-06 08:08:35 -07:00
|
|
|
}
|
2012-08-29 23:41:16 -06:00
|
|
|
|
|
|
|
*pparts = parts;
|
|
|
|
return curr_part;
|
|
|
|
};
|
|
|
|
|
2018-05-09 02:17:29 -06:00
|
|
|
static const struct of_device_id bcm47xxpart_of_match_table[] = {
|
|
|
|
{ .compatible = "brcm,bcm947xx-cfe-partitions" },
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, bcm47xxpart_of_match_table);
|
|
|
|
|
2012-08-29 23:41:16 -06:00
|
|
|
static struct mtd_part_parser bcm47xxpart_mtd_parser = {
|
|
|
|
.parse_fn = bcm47xxpart_parse,
|
|
|
|
.name = "bcm47xxpart",
|
2018-05-09 02:17:29 -06:00
|
|
|
.of_match_table = bcm47xxpart_of_match_table,
|
2012-08-29 23:41:16 -06:00
|
|
|
};
|
2015-11-11 20:13:30 -07:00
|
|
|
module_mtd_part_parser(bcm47xxpart_mtd_parser);
|
2012-08-29 23:41:16 -06:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");
|