Skip to contentSkip to conversation

mtd: add support for partition parsers

PATCH Superseded Rafał Miłecki 2 participants 4 messages Jul 19, 2016 – Sep 19, 2016 Memory technology devices (MTD)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 mtd: add support for partition parsers

This extends MTD subsystem by adding a support for partition parsers that should be used for creating subpartitions. There are some types of partitions that require splitting, like firmware containers. It's common some home routers that a single firmware image gets flashed to predefined partition and then we need to parse it dynamically.

The reason for having such parsers is to share code. Right now we have e.g. TRX parsing implemented in bcm47xxpart but this could be used in more cases (e.g. on ramips which doesn't use bcm47xxpart or with DT).

This implementation requires marking partition as requiring parsing with a specific parser. This can be used right away with some parsers like bcm47xxpart, in future we will hopefully also find a solution for doing it with ofpart ("fixed-partitions").

---
One thing I'm not proud of in this patch is struct mtd_partition casting in order to be able to modify partition offset. It's marked as const so it was the only way I could think of. Any better ideas? Currently parser gets a single MTD and it doesn't have to deal with its offset, which I kind of like as it simplifies things.

---
 drivers/mtd/mtdpart.c          | 34 +++++++++++++++++++++++++++++++++-
 include/linux/mtd/partitions.h |  5 +++++
 2 files changed, 38 insertions(+), 1 deletion(-)
drivers/mtd/mtdpart.c +33 -1
index 1f13e32..0203e21 100644
@@ -663,6 +663,34 @@ int mtd_del_partition(struct mtd_info *master, int partno)
 }
 EXPORT_SYMBOL_GPL(mtd_del_partition);
 
+static int mtd_parse_part(struct mtd_part *slave, const char *parser)
+{
+	static const char *probes[2] = { NULL, NULL };
+	struct mtd_partitions parsed = { 0 };
+	int i;
+	int err;
+
+	probes[0] = parser;
+	err = parse_mtd_partitions(&slave->mtd, probes, &parsed, NULL);
+
+	if (err)
+		return err;
+	else if (!parsed.nr_parts)
+		return -ENOENT;
+
+	for (i = 0; i < parsed.nr_parts; i++) {
+		struct mtd_partition *part;
+
+		part = (struct mtd_partition *)&parsed.parts[i];
+		part->offset += slave->offset;
+	}
+	err = add_mtd_partitions(slave->master, parsed.parts, parsed.nr_parts);
+
+	if (parsed.parser)
+		kfree(parsed.parts);
+	return err;
+}
+
 /*
  * This function, given a master MTD object and a partition table, creates
  * and registers slave MTD objects which are bound to the master according to
@@ -683,7 +711,9 @@ int add_mtd_partitions(struct mtd_info *master,
 	printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
 
 	for (i = 0; i < nbparts; i++) {
-		slave = allocate_partition(master, parts + i, i, cur_offset);
+		const struct mtd_partition *part = parts + i;
+
+		slave = allocate_partition(master, part, i, cur_offset);
 		if (IS_ERR(slave)) {
 			del_mtd_partitions(master);
 			return PTR_ERR(slave);
@@ -695,6 +725,8 @@ int add_mtd_partitions(struct mtd_info *master,
 
 		add_mtd_device(&slave->mtd);
 		mtd_add_partition_attrs(slave);
+		if (part->parser)
+			mtd_parse_part(slave, part->parser);
 
 		cur_offset = slave->offset + slave->mtd.size;
 	}
include/linux/mtd/partitions.h +5 -0
index 70736e1..c6da77f9 100644
@@ -20,6 +20,10 @@
  *
  * For each partition, these fields are available:
  * name: string that will be used to label the partition's MTD device.
+ * parser: some partitions may require specific handling like splitting them
+ *	into subpartitions (e.g. firmware partition may contain partitions
+ *	table, kernel image and rootfs). This field may contain a name of parser
+ *	than can handle specific type.
  * size: the partition size; if defined as MTDPART_SIZ_FULL, the partition
  * 	will extend to the end of the master MTD device.
  * offset: absolute starting position within the master MTD device; if
@@ -38,6 +42,7 @@
 
 struct mtd_partition {
 	const char *name;		/* identifier string */
+	const char *parser;		/* parser name */
 	uint64_t size;			/* partition size */
 	uint64_t offset;		/* offset within the master MTD space */
 	uint32_t mask_flags;		/* master MTD flags to mask out for this partition */
Signature · 1 line
1.8.4.5
#1 lore raw

PATCH 2/2 mtd: extract TRX parser out of bcm47xxpart into separated module

This makes TRX parsing code reusable with other platforms and parsers. There is still some place for improvement (e.g. working on some smarter method of checking rootfs format), for now we simply move the code as it is.

---
 drivers/mtd/Kconfig              |   4 ++
 drivers/mtd/Makefile             |   1 +
 drivers/mtd/bcm47xxpart.c        |  63 +--------------------
 drivers/mtd/parsers/Kconfig      |   8 +++
 drivers/mtd/parsers/Makefile     |   1 +
 drivers/mtd/parsers/parser_trx.c | 119 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 136 insertions(+), 60 deletions(-)
 create mode 100644 drivers/mtd/parsers/Kconfig
 create mode 100644 drivers/mtd/parsers/Makefile
 create mode 100644 drivers/mtd/parsers/parser_trx.c
drivers/mtd/Kconfig +4 -0
index e83a279..5a2d717 100644
@@ -155,6 +155,10 @@ config MTD_BCM47XX_PARTS
 	  This provides partitions parser for devices based on BCM47xx
 	  boards.
 
+menu "Partition parsers"
+source "drivers/mtd/parsers/Kconfig"
+endmenu
+
 comment "User Modules And Translation Layers"
 
 #
drivers/mtd/Makefile +1 -0
index 99bb9a1..151d60df 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_MTD_AFS_PARTS)	+= afs.o
 obj-$(CONFIG_MTD_AR7_PARTS)	+= ar7part.o
 obj-$(CONFIG_MTD_BCM63XX_PARTS)	+= bcm63xxpart.o
 obj-$(CONFIG_MTD_BCM47XX_PARTS)	+= bcm47xxpart.o
+obj-y				+= parsers/
 
 # 'Users' - code which presents functionality to userspace.
 obj-$(CONFIG_MTD_BLKDEVS)	+= mtd_blkdevs.o
drivers/mtd/bcm47xxpart.c +3 -60

82 lines not on this page · raw message

drivers/mtd/parsers/Kconfig new file +8 -0
new file mode 100644
index 0000000..3114276
@@ -0,0 +1,8 @@
+config MTD_PARSER_TRX
+	tristate "Parser for TRX format partitions"
+	depends on MTD && (BCM47XX || ARCH_BCM_5301X)
+	help
+	  TRX is a firmware format used by Broadcom on their devices. It
+	  may contain up to 3/4 partitions (depending on the version).
+	  This driver will parse partitions marked as TRX ones and on
+	  success it will at least two partitions: kernel and rootfs.
drivers/mtd/parsers/Makefile new file +1 -0
new file mode 100644
index 0000000..4d9024e
@@ -0,0 +1 @@
+obj-$(CONFIG_MTD_PARSER_TRX)		+= parser_trx.o
drivers/mtd/parsers/parser_trx.c new file +119 -0

119 lines not on this page · raw message

Signature · 1 line
1.8.4.5
#2 lore raw
In reply to Rafał Miłecki

Rafał,

Quoted 15 lines · Rafał Miłecki

This extends MTD subsystem by adding a support for partition parsers that should be used for creating subpartitions. There are some types of partitions that require splitting, like firmware containers. It's common some home routers that a single firmware image gets flashed to predefined partition and then we need to parse it dynamically.

The reason for having such parsers is to share code. Right now we have e.g. TRX parsing implemented in bcm47xxpart but this could be used in more cases (e.g. on ramips which doesn't use bcm47xxpart or with DT).

This implementation requires marking partition as requiring parsing with a specific parser. This can be used right away with some parsers like bcm47xxpart, in future we will hopefully also find a solution for doing it with ofpart ("fixed-partitions").

Quoted 7 lines

---
One thing I'm not proud of in this patch is struct mtd_partition casting in order to be able to modify partition offset. It's marked as const so it was the only way I could think of. Any better ideas? Currently parser gets a single MTD and it doesn't have to deal with its offset, which I kind of like as it simplifies things.

I have to admit, I don't fully understand what you are trying to solve. Why are you introducing a new concept of partition parsing in patch 2/2? We have already one.

Are you unhappy with the way parsers are _requested_?
Currently MTD core tries cmdlinepart and ofpart, drivers can override that. Do you need a way to request a parser via DT or cmdline?

Signature · 2 lines
Thanks,
//richard
#3 lore raw
Quoted 24 lines · Richard Weinberger
Quoted 15 lines · Rafał Miłecki

This extends MTD subsystem by adding a support for partition parsers that should be used for creating subpartitions. There are some types of partitions that require splitting, like firmware containers. It's common some home routers that a single firmware image gets flashed to predefined partition and then we need to parse it dynamically.

The reason for having such parsers is to share code. Right now we have e.g. TRX parsing implemented in bcm47xxpart but this could be used in more cases (e.g. on ramips which doesn't use bcm47xxpart or with DT).

This implementation requires marking partition as requiring parsing with a specific parser. This can be used right away with some parsers like bcm47xxpart, in future we will hopefully also find a solution for doing it with ofpart ("fixed-partitions").

Quoted 7 lines

---
One thing I'm not proud of in this patch is struct mtd_partition casting in order to be able to modify partition offset. It's marked as const so it was the only way I could think of. Any better ideas? Currently parser gets a single MTD and it doesn't have to deal with its offset, which I kind of like as it simplifies things.

Quoted 7 lines

I have to admit, I don't fully understand what you are trying to solve. Why are you introducing a new concept of partition parsing in patch 2/2? We have already one.

Are you unhappy with the way parsers are _requested_?
Currently MTD core tries cmdlinepart and ofpart, drivers can override that. Do you need a way to request a parser via DT or cmdline?

Actually I'm introducing a new concept in 1/2, the later 2/2 just adds a first case using it.

Yes, I need parser(s) that can be requested in a slightly different way. I didn't need to change parsers design (you can see my PATCH 2/2 uses standard struct mtd_part_parser). I just needed a way to call a parser for a specified partition.

I'm working on a standalone TRX parser. Right now I need to request it from bcm47xxpart. I need this as ramips arch will need TRX support as well and I don't want to duplicate TRX support code.

In the future I'd like to use TRX from ofpart as well. I was hoping to work on support for something like this:

partitions {
        compatible = "fixed-partitions";
        #address-cells = <1>;
        #size-cells = <1>;

        partition@0 {
                label = "bootloader";
                reg = <0x00000 0x40000>;
        };

        partition@40000 {
                label = "firmware";
                format = "trx";
                reg = <0x40000 0x1fb0000>;
        };

        partition@1ff0000 {
                label = "nvram";
                reg = <0x1ff0000 0x10000>;
        };
};
Signature · 1 line
Rafał