mtd: bcm47part driver for BCM47XX chipsets
PATCH v2 1/1 mtd: bcm47part driver for BCM47XX chipsets
This driver provides parser detecting partitions on BCM47XX flash memories. It has many differences in comparision to older BCM63XX, like:
1) Different CFE with no more trivial MAGICs
2) More partitions types (board_data, ML, POT)
3) Supporting more than 1 flash on a device
which resulted in decision of writing new parser.
It uses generic mtd interface and was successfully tested with Netgear WNDR4500 router which has 2 flash memories: serial one and NAND one.
---
V2: 1) Add support for more partitinos (ML and POT)
2) Optimize: don't scan whole flash (like up to 128 MiB)
3) Optimize: don't scan TRX partitions after detecting header
--- drivers/mtd/Kconfig | 4 + drivers/mtd/Makefile | 1 + drivers/mtd/bcm47part.c | 193 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+), 0 deletions(-) create mode 100644 drivers/mtd/bcm47part.c
drivers/mtd/Kconfig +4 -0
@@ -148,6 +148,10 @@ config MTD_BCM63XX_PARTS This provides partions parsing for BCM63xx devices with CFE bootloaders. +config MTD_BCM47_PARTS + tristate "BCM47XX partitioning support" + depends on BCM47XX + comment "User Modules And Translation Layers" config MTD_CHAR
drivers/mtd/Makefile +1 -0
@@ -12,6 +12,7 @@ obj-$(CONFIG_MTD_CMDLINE_PARTS) += cmdlinepart.o obj-$(CONFIG_MTD_AFS_PARTS) += afs.o obj-$(CONFIG_MTD_AR7_PARTS) += ar7part.o obj-$(CONFIG_MTD_BCM63XX_PARTS) += bcm63xxpart.o +obj-$(CONFIG_MTD_BCM47_PARTS) += bcm47part.o # 'Users' - code which presents functionality to userspace. obj-$(CONFIG_MTD_CHAR) += mtdchar.o
drivers/mtd/bcm47part.c new file +193 -0
193 lines not on this page · raw message
Signature · 1 line
1.7.7
drivers/mtd/Kconfig config MTD_BCM63XX_PARTS · line 153
Quoted 3 lines · Rafał Miłecki
drivers/mtd/Kconfig +3
-0+config MTD_BCM47_PARTS + tristate "BCM47XX partitioning support" + depends on BCM47XX
You may want to change "bcm47" to "bcm47xx", for consistency with the other bcm47xx/bcm63xx references found throughout the tree.
Quoted 1 line
+ u8 *buf;
[...]
Quoted 1 line
+ buf =
kzalloc(sizeof(*buf) * BCM47PART_BYTES_TO_READ, GFP_KERNEL);
sizeof(u8) probably isn't needed.
Quoted 33 lines
drivers/mtd/bcm47part.c new file +33
-0+/* CFE has small NVRAM at 0x400 */ + fourcc = (u32 *)&buf[0x400 ]; +if (*fourcc ==NVRAM_HEADER ) { + parts[curr_part].name ="boot" ; + parts[curr_part].mask_flags =MTD_WRITEABLE ; + parts[curr_part].offset = offset; + curr_part++; + } + +/* Standard NVRAM */ + fourcc = (u32 *)&buf[0x000 ]; +if (*fourcc ==NVRAM_HEADER ) { + parts[curr_part].name ="nvram" ; + parts[curr_part].mask_flags =MTD_WRITEABLE ; + parts[curr_part].offset = offset; + curr_part++; + } + +/* board_data starts with board_id which differs across boards, +* but we can use 'MPFR' (hopefully) magic at 0x100 */ + fourcc = (u32 *)&buf[0x100 ]; +if (*fourcc ==0x5246504D ) {/* MPFR */ + parts[curr_part].name ="board_data" ; + parts[curr_part].mask_flags =MTD_WRITEABLE ; + parts[curr_part].offset = offset; + curr_part++; + } + +/* POT(TOP) */ + fourcc = (u32 *)&buf[0x000 ]; + fourcc2 = (u32 *)&buf[0x004 ]; +if (*fourcc ==0x54544f50 &&/* POTT */ + (*fourcc2 &0xFFFF ) ==0x504f ) {/* OP */
drivers/mtd/bcm47part.c line 112
Quoted 10 lines
drivers/mtd/bcm47part.c new file +10
-0+ parts[curr_part].name ="POT" ; + parts[curr_part].mask_flags =MTD_WRITEABLE ; + parts[curr_part].offset = offset; + curr_part++; + } + +/* ML */ + fourcc = (u32 *)&buf[0x010 ]; + fourcc2 = (u32 *)&buf[0x014 ]; +if (*fourcc ==0x39685a42 && *fourcc2 ==0x26594131 ) {
I would suggest using symbolic names for the constants, even if it's just something like ML_MAGIC1.
drivers/mtd/bcm47part.c line 117
Quoted 5 lines
drivers/mtd/bcm47part.c new file +5
-0+ parts[curr_part].name ="ML" ; + parts[curr_part].mask_flags =MTD_WRITEABLE ; + parts[curr_part].offset = offset; + curr_part++; + }
Are all of these cases mutually exclusive, given that they use the same offset?
It would be nice to reduce the amount of duplicated code if possible. Maybe something as simple as:
char *name = NULL;
if (some_condition)
name = "POT";
else if (some_other_condition)
name = "ML";[...]
if (name) {
parts[curr_part].name = name;
parts[curr_part].mask_flags = MTD_WRITEABLE;
parts[curr_part].offset = offset;
curr_part++;
}Can the contents of buf[] be represented in a struct?
Thanks for comments!
2012/8/26 Kevin Cernekee <[email protected]>:
Quoted 7 lines · Rafał Miłecki
drivers/mtd/Kconfig config MTD_BCM63XX_PARTS · line 153
Quoted 3 lines
drivers/mtd/Kconfig +3
-0+config MTD_BCM47_PARTS + tristate "BCM47XX partitioning support" + depends on BCM47XXYou may want to change "bcm47" to "bcm47xx", for consistency with the other bcm47xx/bcm63xx references found throughout the tree.
Personally I prefer that "bcm47" over "bcm47xx". 2 minor advantages for me:
1) It's shorted but still unique
2) We have chars after digits in it's name, it's easier to read than "xxpart" or "xxnflash"
David: AFAIK you're the maintainer of mtd? Could you share your opinion?
I won't argue if you guys prefer "bcm47xx" :)
Quoted 5 lines
Quoted 1 line
+ u8 *buf;
[...]
Quoted 1 line
+ buf =
kzalloc(sizeof(*buf) * BCM47PART_BYTES_TO_READ, GFP_KERNEL);sizeof(u8) probably isn't needed.
I hoped it will make reading the code easier. Multiplying should be optimized by compiler. But can drop this if you think it's not worth it.
Quoted 38 lines
Quoted 38 lines
drivers/mtd/bcm47part.c new file +38
-0+/* CFE has small NVRAM at 0x400 */ + fourcc = (u32 *)&buf[0x400 ]; +if (*fourcc ==NVRAM_HEADER ) { + parts[curr_part].name ="boot" ; + parts[curr_part].mask_flags =MTD_WRITEABLE ; + parts[curr_part].offset = offset; + curr_part++; + } + +/* Standard NVRAM */ + fourcc = (u32 *)&buf[0x000 ]; +if (*fourcc ==NVRAM_HEADER ) { + parts[curr_part].name ="nvram" ; + parts[curr_part].mask_flags =MTD_WRITEABLE ; + parts[curr_part].offset = offset; + curr_part++; + } + +/* board_data starts with board_id which differs across boards, +* but we can use 'MPFR' (hopefully) magic at 0x100 */ + fourcc = (u32 *)&buf[0x100 ]; +if (*fourcc ==0x5246504D ) {/* MPFR */ + parts[curr_part].name ="board_data" ; + parts[curr_part].mask_flags =MTD_WRITEABLE ; + parts[curr_part].offset = offset; + curr_part++; + } + +/* POT(TOP) */ + fourcc = (u32 *)&buf[0x000 ]; + fourcc2 = (u32 *)&buf[0x004 ]; +if (*fourcc ==0x54544f50 &&/* POTT */ + (*fourcc2 &0xFFFF ) ==0x504f ) {/* OP */ + parts[curr_part].name ="POT" ; + parts[curr_part].mask_flags =MTD_WRITEABLE ; + parts[curr_part].offset = offset; + curr_part++; + }
Quoted 8 lines
drivers/mtd/bcm47part.c line 112
Quoted 5 lines
drivers/mtd/bcm47part.c new file +5
-0+ +/* ML */ + fourcc = (u32 *)&buf[0x010 ]; + fourcc2 = (u32 *)&buf[0x014 ]; +if (*fourcc ==0x39685a42 && *fourcc2 ==0x26594131 ) {I would suggest using symbolic names for the constants, even if it's just something like
ML_MAGIC1.
Agree.
Quoted 7 lines
drivers/mtd/bcm47part.c line 117
Quoted 5 lines
drivers/mtd/bcm47part.c new file +5
-0+ parts[curr_part].name ="ML" ; + parts[curr_part].mask_flags =MTD_WRITEABLE ; + parts[curr_part].offset = offset; + curr_part++; + }Are all of these cases mutually exclusive, given that they use the same offset?
Yes.
Quoted 8 lines
It would be nice to reduce the amount of duplicated code if possible. Maybe something as simple as:
char *name = NULL;
if (some_condition)name = "POT"; else if (some_other_condition) name = "ML";
Quoted 8 lines
[...]
if (name) { parts[curr_part].name = name; parts[curr_part].mask_flags = MTD_WRITEABLE; parts[curr_part].offset = offset; curr_part++; }
Ouch, you just reminded me I've forgotten to drop mask_flags in few places. Masking MTD_WRITEABLE is needed for only ~half of our cases. So if we follow your way now, we have to pass two arguments to the generic partition handler: "name" and "mask_flags". But maybe I can use some additional function for that? It should de-duplicate some code and should work even for code inside "trx" parser.
Quoted 1 line
Can the contents of buf[] be represented in a struct?
Theoretically yes, but I think it doesn't make much sense. It's for detecting type of partition, so it wouldn't have nice struct. It usually would have only one valid field per type. So it would be mostly like
struct partitions_ids {
union {
u32 possible_nvram_fourcc;
u32 possible_pot_fourcc;
u32 possible_trx_fourcc;
},
u32 possible_pot_fourcc2;
u32 pad[2]
u32 possible_ml_fourcc;
u32 possible_ml_fourcc2;
}I really don't like idea of such a struct.
Signature · 1 line
Rafał