--- /dev/null
+From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
+Date: Thu, 6 May 2021 12:33:58 +0200
+Subject: [PATCH] mtd: parsers: ofpart: fix parsing subpartitions
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+ofpart was recently patched to not scan random partition nodes as
+subpartitions. That change unfortunately broke scanning valid
+subpartitions like:
+
+partitions {
+ compatible = "fixed-partitions";
+ #address-cells = <1>;
+ #size-cells = <1>;
+
+ partition@0 {
+ compatible = "fixed-partitions";
+ label = "bootloader";
+ reg = <0x0 0x100000>;
+
+ partition@0 {
+ label = "config";
+ reg = <0x80000 0x80000>;
+ };
+ };
+};
+
+Fix that regression by adding 1 more code path. We actually need 3
+conditional blocks to support 3 possible cases. This change also makes
+code easier to understand & follow.
+
+Reported-by: David Bauer <mail@david-bauer.net>
+Fixes: 2d751203aacf ("mtd: parsers: ofpart: limit parsing of deprecated DT syntax
+Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
+---
+ drivers/mtd/parsers/ofpart_core.c | 26 ++++++++++++++------------
+ 1 file changed, 14 insertions(+), 12 deletions(-)
+
+--- a/drivers/mtd/parsers/ofpart_core.c
++++ b/drivers/mtd/parsers/ofpart_core.c
+@@ -57,20 +57,22 @@ static int parse_fixed_partitions(struct
+ if (!mtd_node)
+ return 0;
+
+- ofpart_node = of_get_child_by_name(mtd_node, "partitions");
+- if (!ofpart_node && !mtd_is_partition(master)) {
+- /*
+- * We might get here even when ofpart isn't used at all (e.g.,
+- * when using another parser), so don't be louder than
+- * KERN_DEBUG
+- */
+- pr_debug("%s: 'partitions' subnode not found on %pOF. Trying to parse direct subnodes as partitions.\n",
+- master->name, mtd_node);
++ if (!mtd_is_partition(master)) { /* Master */
++ ofpart_node = of_get_child_by_name(mtd_node, "partitions");
++ if (!ofpart_node) {
++ /*
++ * We might get here even when ofpart isn't used at all (e.g.,
++ * when using another parser), so don't be louder than
++ * KERN_DEBUG
++ */
++ pr_debug("%s: 'partitions' subnode not found on %pOF. Trying to parse direct subnodes as partitions.\n",
++ master->name, mtd_node);
++ ofpart_node = mtd_node;
++ dedicated = false;
++ }
++ } else { /* Partition */
+ ofpart_node = mtd_node;
+- dedicated = false;
+ }
+- if (!ofpart_node)
+- return 0;
+
+ of_id = of_match_node(parse_ofpart_match_table, ofpart_node);
+ if (dedicated && !of_id) {
const char *partname;
struct device_node *pp;
int nr_parts, i, ret = 0;
-@@ -131,9 +134,15 @@ static int parse_fixed_partitions(struct
+@@ -133,9 +136,15 @@ static int parse_fixed_partitions(struct
parts[i].size = of_read_number(reg + a_cells, s_cells);
parts[i].of_node = pp;
parts[i].name = partname;
if (of_get_property(pp, "read-only", &len))
-@@ -247,6 +256,18 @@ static int __init ofpart_parser_init(voi
+@@ -249,6 +258,18 @@ static int __init ofpart_parser_init(voi
return 0;
}
/* Pull of_node from the master device node */
mtd_node = mtd_get_of_node(master);
-@@ -93,7 +127,9 @@ static int parse_fixed_partitions(struct
+@@ -95,7 +129,9 @@ static int parse_fixed_partitions(struct
return 0;
parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
return -ENOMEM;
i = 0;
-@@ -142,6 +178,11 @@ static int parse_fixed_partitions(struct
+@@ -144,6 +180,11 @@ static int parse_fixed_partitions(struct
if (of_get_property(pp, "lock", &len))
parts[i].mask_flags |= MTD_POWERUP_LOCK;
i++;
}
-@@ -151,6 +192,11 @@ static int parse_fixed_partitions(struct
+@@ -153,6 +194,11 @@ static int parse_fixed_partitions(struct
if (quirks && quirks->post_parse)
quirks->post_parse(master, parts, nr_parts);
*pparts = parts;
return nr_parts;
-@@ -161,6 +207,7 @@ ofpart_fail:
+@@ -163,6 +209,7 @@ ofpart_fail:
ofpart_none:
of_node_put(pp);
kfree(parts);
struct device_node *pp;
int nr_parts, i, ret = 0;
bool dedicated = true;
-@@ -131,9 +134,13 @@ static int parse_fixed_partitions(struct
+@@ -133,9 +136,13 @@ static int parse_fixed_partitions(struct
parts[i].size = of_read_number(reg + a_cells, s_cells);
parts[i].of_node = pp;
parts[i].name = partname;
if (of_get_property(pp, "read-only", &len))
-@@ -247,6 +254,18 @@ static int __init ofpart_parser_init(voi
+@@ -249,6 +256,18 @@ static int __init ofpart_parser_init(voi
return 0;
}