2012-08-30 05:41:16 +00: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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/mtd/mtd.h>
|
|
|
|
#include <linux/mtd/partitions.h>
|
2012-12-26 19:51:14 +00:00
|
|
|
#include <bcm47xx_nvram.h>
|
2012-08-30 05:41:16 +00:00
|
|
|
|
|
|
|
/* 10 parts were found on sflash on Netgear WNDR4500 */
|
|
|
|
#define BCM47XXPART_MAX_PARTS 12
|
|
|
|
|
|
|
|
/* Magics */
|
|
|
|
#define BOARD_DATA_MAGIC 0x5246504D /* MPFR */
|
|
|
|
#define POT_MAGIC1 0x54544f50 /* POTT */
|
|
|
|
#define POT_MAGIC2 0x504f /* OP */
|
|
|
|
#define ML_MAGIC1 0x39685a42
|
|
|
|
#define ML_MAGIC2 0x26594131
|
|
|
|
#define TRX_MAGIC 0x30524448
|
|
|
|
|
|
|
|
struct trx_header {
|
|
|
|
uint32_t magic;
|
|
|
|
uint32_t length;
|
|
|
|
uint32_t crc32;
|
|
|
|
uint16_t flags;
|
|
|
|
uint16_t version;
|
|
|
|
uint32_t offset[3];
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
static void bcm47xxpart_add_part(struct mtd_partition *part, char *name,
|
|
|
|
u64 offset, uint32_t mask_flags)
|
|
|
|
{
|
|
|
|
part->name = name;
|
|
|
|
part->offset = offset;
|
|
|
|
part->mask_flags = mask_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bcm47xxpart_parse(struct mtd_info *master,
|
|
|
|
struct mtd_partition **pparts,
|
|
|
|
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 16:39:58 +00:00
|
|
|
uint32_t blocksize = master->erasesize;
|
2012-08-30 05:41:16 +00:00
|
|
|
struct trx_header *trx;
|
2013-01-06 15:08:36 +00:00
|
|
|
int trx_part = -1;
|
|
|
|
int last_trx_part = -1;
|
2013-01-24 18:03:37 +00:00
|
|
|
int max_bytes_to_read = 0x8004;
|
2012-08-30 05:41:16 +00:00
|
|
|
|
2013-01-24 16:39:58 +00:00
|
|
|
if (blocksize <= 0x10000)
|
|
|
|
blocksize = 0x10000;
|
2013-01-24 18:03:37 +00:00
|
|
|
if (blocksize == 0x20000)
|
|
|
|
max_bytes_to_read = 0x18004;
|
2012-08-30 05:41:16 +00:00
|
|
|
|
|
|
|
/* Alloc */
|
|
|
|
parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
|
|
|
|
GFP_KERNEL);
|
2013-01-24 18:03:37 +00:00
|
|
|
buf = kzalloc(max_bytes_to_read, GFP_KERNEL);
|
2012-08-30 05:41:16 +00:00
|
|
|
|
|
|
|
/* Parse block by block looking for magics */
|
|
|
|
for (offset = 0; offset <= master->size - blocksize;
|
|
|
|
offset += blocksize) {
|
|
|
|
/* Nothing more in higher memory */
|
|
|
|
if (offset >= 0x2000000)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (curr_part > BCM47XXPART_MAX_PARTS) {
|
|
|
|
pr_warn("Reached maximum number of partitions, scanning stopped!\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read beginning of the block */
|
2013-01-24 18:03:37 +00:00
|
|
|
if (mtd_read(master, offset, max_bytes_to_read,
|
2012-08-30 05:41:16 +00:00
|
|
|
&bytes_read, (uint8_t *)buf) < 0) {
|
|
|
|
pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
|
|
|
|
offset);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CFE has small NVRAM at 0x400 */
|
|
|
|
if (buf[0x400 / 4] == NVRAM_HEADER) {
|
|
|
|
bcm47xxpart_add_part(&parts[curr_part++], "boot",
|
|
|
|
offset, MTD_WRITEABLE);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Standard NVRAM */
|
2013-01-24 18:03:37 +00:00
|
|
|
if (buf[0x000 / 4] == NVRAM_HEADER ||
|
|
|
|
buf[0x1000 / 4] == NVRAM_HEADER ||
|
|
|
|
buf[0x8000 / 4] == NVRAM_HEADER ||
|
|
|
|
(blocksize == 0x20000 && (
|
|
|
|
buf[0x10000 / 4] == NVRAM_HEADER ||
|
|
|
|
buf[0x11000 / 4] == NVRAM_HEADER ||
|
|
|
|
buf[0x18000 / 4] == NVRAM_HEADER))) {
|
2012-08-30 05:41:16 +00:00
|
|
|
bcm47xxpart_add_part(&parts[curr_part++], "nvram",
|
|
|
|
offset, 0);
|
2013-01-24 18:03:37 +00:00
|
|
|
offset = rounddown(offset, blocksize);
|
2012-08-30 05:41:16 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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) {
|
|
|
|
trx = (struct trx_header *)buf;
|
|
|
|
|
2013-01-06 15:08:36 +00:00
|
|
|
trx_part = curr_part;
|
|
|
|
bcm47xxpart_add_part(&parts[curr_part++], "firmware",
|
|
|
|
offset, 0);
|
|
|
|
|
2012-08-30 05:41:16 +00:00
|
|
|
i = 0;
|
|
|
|
/* We have LZMA loader if offset[2] points to sth */
|
|
|
|
if (trx->offset[2]) {
|
|
|
|
bcm47xxpart_add_part(&parts[curr_part++],
|
|
|
|
"loader",
|
|
|
|
offset + trx->offset[i],
|
|
|
|
0);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
bcm47xxpart_add_part(&parts[curr_part++], "linux",
|
|
|
|
offset + trx->offset[i], 0);
|
|
|
|
i++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pure rootfs size is known and can be calculated as:
|
|
|
|
* trx->length - trx->offset[i]. We don't fill it as
|
|
|
|
* we want to have jffs2 (overlay) in the same mtd.
|
|
|
|
*/
|
|
|
|
bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
|
|
|
|
offset + trx->offset[i], 0);
|
|
|
|
i++;
|
|
|
|
|
2013-01-06 15:08:36 +00:00
|
|
|
last_trx_part = curr_part - 1;
|
|
|
|
|
2012-08-30 05:41:16 +00:00
|
|
|
/*
|
|
|
|
* We have whole TRX scanned, skip to the next part. Use
|
|
|
|
* roundown (not roundup), as the loop will increase
|
|
|
|
* offset in next step.
|
|
|
|
*/
|
|
|
|
offset = rounddown(offset + trx->length, blocksize);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kfree(buf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assume that partitions end at the beginning of the one they are
|
|
|
|
* followed by.
|
|
|
|
*/
|
2013-01-06 15:08:35 +00: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;
|
2013-01-06 15:08:36 +00:00
|
|
|
if (i == last_trx_part && trx_part >= 0)
|
|
|
|
parts[trx_part].size = next_part_offset -
|
|
|
|
parts[trx_part].offset;
|
2013-01-06 15:08:35 +00:00
|
|
|
}
|
2012-08-30 05:41:16 +00:00
|
|
|
|
|
|
|
*pparts = parts;
|
|
|
|
return curr_part;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct mtd_part_parser bcm47xxpart_mtd_parser = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.parse_fn = bcm47xxpart_parse,
|
|
|
|
.name = "bcm47xxpart",
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init bcm47xxpart_init(void)
|
|
|
|
{
|
|
|
|
return register_mtd_parser(&bcm47xxpart_mtd_parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit bcm47xxpart_exit(void)
|
|
|
|
{
|
|
|
|
deregister_mtd_parser(&bcm47xxpart_mtd_parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(bcm47xxpart_init);
|
|
|
|
module_exit(bcm47xxpart_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");
|