ae13112ef6d0b4a00863ab0970081e481a3ab101
[openwrt/openwrt.git] / target / linux / generic / pending-4.14 / 401-mtd-add-support-for-different-partition-parser-types.patch
1 From: Gabor Juhos <juhosg@openwrt.org>
2 Subject: mtd: add support for different partition parser types
3
4 Signed-off-by: Gabor Juhos <juhosg@openwrt.org>
5 ---
6 drivers/mtd/mtdpart.c | 56 ++++++++++++++++++++++++++++++++++++++++
7 include/linux/mtd/partitions.h | 11 ++++++++
8 2 files changed, 67 insertions(+)
9
10 --- a/drivers/mtd/mtdpart.c
11 +++ b/drivers/mtd/mtdpart.c
12 @@ -56,6 +56,10 @@ struct mtd_part {
13 };
14
15 static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part);
16 +static int parse_mtd_partitions_by_type(struct mtd_info *master,
17 + enum mtd_parser_type type,
18 + const struct mtd_partition **pparts,
19 + struct mtd_part_parser_data *data);
20
21 /*
22 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
23 @@ -762,6 +766,27 @@ int mtd_del_partition(struct mtd_info *m
24 }
25 EXPORT_SYMBOL_GPL(mtd_del_partition);
26
27 +static int
28 +run_parsers_by_type(struct mtd_part *slave, enum mtd_parser_type type)
29 +{
30 + struct mtd_partitions pparts = { };
31 + int err;
32 +
33 + pparts.nr_parts = parse_mtd_partitions_by_type(&slave->mtd, type,
34 + &pparts.parts, NULL);
35 + if (pparts.nr_parts <= 0)
36 + return pparts.nr_parts;
37 +
38 + if (WARN_ON(!pparts.parts))
39 + return 0;
40 +
41 + err = add_mtd_partitions(&slave->mtd, pparts.parts, pparts.nr_parts);
42 +
43 + kfree(pparts.parts);
44 +
45 + return err ? err : pparts.nr_parts;
46 +}
47 +
48 #ifdef CONFIG_MTD_SPLIT_FIRMWARE_NAME
49 #define SPLIT_FIRMWARE_NAME CONFIG_MTD_SPLIT_FIRMWARE_NAME
50 #else
51 @@ -1147,6 +1172,61 @@ void mtd_part_parser_cleanup(struct mtd_
52 }
53 }
54
55 +static struct mtd_part_parser *
56 +get_partition_parser_by_type(enum mtd_parser_type type,
57 + struct mtd_part_parser *start)
58 +{
59 + struct mtd_part_parser *p, *ret = NULL;
60 +
61 + spin_lock(&part_parser_lock);
62 +
63 + p = list_prepare_entry(start, &part_parsers, list);
64 + if (start)
65 + mtd_part_parser_put(start);
66 +
67 + list_for_each_entry_continue(p, &part_parsers, list) {
68 + if (p->type == type && try_module_get(p->owner)) {
69 + ret = p;
70 + break;
71 + }
72 + }
73 +
74 + spin_unlock(&part_parser_lock);
75 +
76 + return ret;
77 +}
78 +
79 +static int parse_mtd_partitions_by_type(struct mtd_info *master,
80 + enum mtd_parser_type type,
81 + const struct mtd_partition **pparts,
82 + struct mtd_part_parser_data *data)
83 +{
84 + struct mtd_part_parser *prev = NULL;
85 + int ret = 0;
86 +
87 + while (1) {
88 + struct mtd_part_parser *parser;
89 +
90 + parser = get_partition_parser_by_type(type, prev);
91 + if (!parser)
92 + break;
93 +
94 + ret = (*parser->parse_fn)(master, pparts, data);
95 +
96 + if (ret > 0) {
97 + mtd_part_parser_put(parser);
98 + printk(KERN_NOTICE
99 + "%d %s partitions found on MTD device %s\n",
100 + ret, parser->name, master->name);
101 + break;
102 + }
103 +
104 + prev = parser;
105 + }
106 +
107 + return ret;
108 +}
109 +
110 int mtd_is_partition(const struct mtd_info *mtd)
111 {
112 struct mtd_part *part;
113 --- a/include/linux/mtd/partitions.h
114 +++ b/include/linux/mtd/partitions.h
115 @@ -73,6 +73,10 @@ struct mtd_part_parser_data {
116 * Functions dealing with the various ways of partitioning the space
117 */
118
119 +enum mtd_parser_type {
120 + MTD_PARSER_TYPE_DEVICE = 0,
121 +};
122 +
123 struct mtd_part_parser {
124 struct list_head list;
125 struct module *owner;
126 @@ -81,6 +85,7 @@ struct mtd_part_parser {
127 int (*parse_fn)(struct mtd_info *, const struct mtd_partition **,
128 struct mtd_part_parser_data *);
129 void (*cleanup)(const struct mtd_partition *pparts, int nr_parts);
130 + enum mtd_parser_type type;
131 };
132
133 /* Container for passing around a set of parsed partitions */