kernel: split out mtd hack for CONFIG_FIT_PARTITION + rootfs
[openwrt/openwrt.git] / target / linux / generic / hack-5.10 / 422-drivers-mtd-parsers-add-nvmem-support-to-cmdlinepart.patch
1 From 6fa9e3678eb002246df1280322b6a024853950a5 Mon Sep 17 00:00:00 2001
2 From: Ansuel Smith <ansuelsmth@gmail.com>
3 Date: Mon, 11 Oct 2021 00:53:14 +0200
4 Subject: [PATCH] drivers: mtd: parsers: add nvmem support to cmdlinepart
5
6 Assuming cmdlinepart is only one level deep partition scheme and that
7 static partition are also defined in DTS, we can assign an of_node for
8 partition declared from bootargs. cmdlinepart have priority than
9 fiexed-partition parser so in this specific case the parser doesn't
10 assign an of_node. Fix this by searching a defined of_node using a
11 similar fixed_partition parser and if a partition is found with the same
12 label, check that it has the same offset and size and return the DT
13 of_node to correctly use NVMEM cells.
14
15 Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
16 ---
17 drivers/mtd/parsers/cmdlinepart.c | 71 +++++++++++++++++++++++++++++++
18 1 file changed, 71 insertions(+)
19
20 --- a/drivers/mtd/parsers/cmdlinepart.c
21 +++ b/drivers/mtd/parsers/cmdlinepart.c
22 @@ -43,6 +43,7 @@
23 #include <linux/mtd/partitions.h>
24 #include <linux/module.h>
25 #include <linux/err.h>
26 +#include <linux/of.h>
27
28 /* debug macro */
29 #if 0
30 @@ -323,6 +324,68 @@ static int mtdpart_setup_real(char *s)
31 return 0;
32 }
33
34 +static int search_fixed_partition(struct mtd_info *master,
35 + struct mtd_partition *target_part,
36 + struct mtd_partition *fixed_part)
37 +{
38 + struct device_node *mtd_node;
39 + struct device_node *ofpart_node;
40 + struct device_node *pp;
41 + struct mtd_partition part;
42 + const char *partname;
43 +
44 + mtd_node = mtd_get_of_node(master);
45 + if (!mtd_node)
46 + return -EINVAL;
47 +
48 + ofpart_node = of_get_child_by_name(mtd_node, "partitions");
49 +
50 + for_each_child_of_node(ofpart_node, pp) {
51 + const __be32 *reg;
52 + int len;
53 + int a_cells, s_cells;
54 +
55 + reg = of_get_property(pp, "reg", &len);
56 + if (!reg) {
57 + pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
58 + master->name, pp,
59 + mtd_node);
60 + continue;
61 + }
62 +
63 + a_cells = of_n_addr_cells(pp);
64 + s_cells = of_n_size_cells(pp);
65 + if (len / 4 != a_cells + s_cells) {
66 + pr_debug("%s: ofpart partition %pOF (%pOF) error parsing reg property.\n",
67 + master->name, pp,
68 + mtd_node);
69 + continue;
70 + }
71 +
72 + part.offset = of_read_number(reg, a_cells);
73 + part.size = of_read_number(reg + a_cells, s_cells);
74 + part.of_node = pp;
75 +
76 + partname = of_get_property(pp, "label", &len);
77 + if (!partname)
78 + partname = of_get_property(pp, "name", &len);
79 + part.name = partname;
80 +
81 + if (!strncmp(target_part->name, part.name, len)) {
82 + if (part.offset != target_part->offset)
83 + return -EINVAL;
84 +
85 + if (part.size != target_part->size)
86 + return -EINVAL;
87 +
88 + memcpy(fixed_part, &part, sizeof(struct mtd_partition));
89 + return 0;
90 + }
91 + }
92 +
93 + return -EINVAL;
94 +}
95 +
96 /*
97 * Main function to be called from the MTD mapping driver/device to
98 * obtain the partitioning information. At this point the command line
99 @@ -338,6 +401,7 @@ static int parse_cmdline_partitions(stru
100 int i, err;
101 struct cmdline_mtd_partition *part;
102 const char *mtd_id = master->name;
103 + struct mtd_partition fixed_part;
104
105 /* parse command line */
106 if (!cmdline_parsed) {
107 @@ -382,6 +446,13 @@ static int parse_cmdline_partitions(stru
108 sizeof(*part->parts) * (part->num_parts - i));
109 i--;
110 }
111 +
112 + err = search_fixed_partition(master, &part->parts[i], &fixed_part);
113 + if (!err) {
114 + part->parts[i].of_node = fixed_part.of_node;
115 + pr_info("Found partition defined in DT for %s. Assigning OF node to support nvmem.",
116 + part->parts[i].name);
117 + }
118 }
119
120 *pparts = kmemdup(part->parts, sizeof(*part->parts) * part->num_parts,