generic: Convert incorrect generic/5.10 patches
[openwrt/staging/hauke.git] / target / linux / generic / pending-5.10 / 400-mtd-mtdsplit-support.patch
1 From: Gabor Juhos <juhosg@openwrt.org>
2 Subject: mtd: Add new Kconfig option for firmware partition split
3
4 Add a new kernel config option for generic firmware partition
5 split support and change the uImage split support to depend on
6 the new option. Aslo rename the MTD_UIMAGE_SPLIT_NAME option to
7 MTD_SPLIT_FIRMWARE_NAME to make it more generic.
8
9 The patch is in preparation for multiple firmware format
10 support.
11
12 Submitted-by: Gabor Juhos <juhosg@openwrt.org>
13
14 SVN-Revision: 38002
15 ---
16 drivers/mtd/Kconfig | 19 +
17 drivers/mtd/mtdpart.c | 144 +++++++++++++-----
18 include/linux/mtd/partitions.h | 7 +
19 drivers/mtd/Makefile | 2 +
20 include/linux/mtd/mtd.h | 25 +
21 5 files changed, 171 insertions(+), 25 deletions(-)
22
23 --- a/drivers/mtd/Kconfig
24 +++ b/drivers/mtd/Kconfig
25 @@ -12,6 +12,25 @@ menuconfig MTD
26
27 if MTD
28
29 +menu "OpenWrt specific MTD options"
30 +
31 +config MTD_ROOTFS_ROOT_DEV
32 + bool "Automatically set 'rootfs' partition to be root filesystem"
33 + default y
34 +
35 +config MTD_SPLIT_FIRMWARE
36 + bool "Automatically split firmware partition for kernel+rootfs"
37 + default y
38 +
39 +config MTD_SPLIT_FIRMWARE_NAME
40 + string "Firmware partition name"
41 + depends on MTD_SPLIT_FIRMWARE
42 + default "firmware"
43 +
44 +source "drivers/mtd/mtdsplit/Kconfig"
45 +
46 +endmenu
47 +
48 config MTD_TESTS
49 tristate "MTD tests support (DANGEROUS)"
50 depends on m
51 --- a/drivers/mtd/mtdpart.c
52 +++ b/drivers/mtd/mtdpart.c
53 @@ -15,11 +15,13 @@
54 #include <linux/kmod.h>
55 #include <linux/mtd/mtd.h>
56 #include <linux/mtd/partitions.h>
57 +#include <linux/magic.h>
58 #include <linux/err.h>
59 #include <linux/of.h>
60 #include <linux/of_platform.h>
61
62 #include "mtdcore.h"
63 +#include "mtdsplit/mtdsplit.h"
64
65 /*
66 * MTD methods which simply translate the effective address and pass through
67 @@ -237,6 +239,146 @@ static int mtd_add_partition_attrs(struc
68 return ret;
69 }
70
71 +static DEFINE_SPINLOCK(part_parser_lock);
72 +static LIST_HEAD(part_parsers);
73 +
74 +static struct mtd_part_parser *mtd_part_parser_get(const char *name)
75 +{
76 + struct mtd_part_parser *p, *ret = NULL;
77 +
78 + spin_lock(&part_parser_lock);
79 +
80 + list_for_each_entry(p, &part_parsers, list)
81 + if (!strcmp(p->name, name) && try_module_get(p->owner)) {
82 + ret = p;
83 + break;
84 + }
85 +
86 + spin_unlock(&part_parser_lock);
87 +
88 + return ret;
89 +}
90 +
91 +static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
92 +{
93 + module_put(p->owner);
94 +}
95 +
96 +static struct mtd_part_parser *
97 +get_partition_parser_by_type(enum mtd_parser_type type,
98 + struct mtd_part_parser *start)
99 +{
100 + struct mtd_part_parser *p, *ret = NULL;
101 +
102 + spin_lock(&part_parser_lock);
103 +
104 + p = list_prepare_entry(start, &part_parsers, list);
105 + if (start)
106 + mtd_part_parser_put(start);
107 +
108 + list_for_each_entry_continue(p, &part_parsers, list) {
109 + if (p->type == type && try_module_get(p->owner)) {
110 + ret = p;
111 + break;
112 + }
113 + }
114 +
115 + spin_unlock(&part_parser_lock);
116 +
117 + return ret;
118 +}
119 +
120 +static int parse_mtd_partitions_by_type(struct mtd_info *master,
121 + enum mtd_parser_type type,
122 + const struct mtd_partition **pparts,
123 + struct mtd_part_parser_data *data)
124 +{
125 + struct mtd_part_parser *prev = NULL;
126 + int ret = 0;
127 +
128 + while (1) {
129 + struct mtd_part_parser *parser;
130 +
131 + parser = get_partition_parser_by_type(type, prev);
132 + if (!parser)
133 + break;
134 +
135 + ret = (*parser->parse_fn)(master, pparts, data);
136 +
137 + if (ret > 0) {
138 + mtd_part_parser_put(parser);
139 + printk(KERN_NOTICE
140 + "%d %s partitions found on MTD device %s\n",
141 + ret, parser->name, master->name);
142 + break;
143 + }
144 +
145 + prev = parser;
146 + }
147 +
148 + return ret;
149 +}
150 +
151 +static int
152 +run_parsers_by_type(struct mtd_info *child, enum mtd_parser_type type)
153 +{
154 + struct mtd_partition *parts;
155 + int nr_parts;
156 + int i;
157 +
158 + nr_parts = parse_mtd_partitions_by_type(child, type, (const struct mtd_partition **)&parts,
159 + NULL);
160 + if (nr_parts <= 0)
161 + return nr_parts;
162 +
163 + if (WARN_ON(!parts))
164 + return 0;
165 +
166 + for (i = 0; i < nr_parts; i++) {
167 + /* adjust partition offsets */
168 + parts[i].offset += child->part.offset;
169 +
170 + mtd_add_partition(child->parent,
171 + parts[i].name,
172 + parts[i].offset,
173 + parts[i].size);
174 + }
175 +
176 + kfree(parts);
177 +
178 + return nr_parts;
179 +}
180 +
181 +#ifdef CONFIG_MTD_SPLIT_FIRMWARE_NAME
182 +#define SPLIT_FIRMWARE_NAME CONFIG_MTD_SPLIT_FIRMWARE_NAME
183 +#else
184 +#define SPLIT_FIRMWARE_NAME "unused"
185 +#endif
186 +
187 +static void split_firmware(struct mtd_info *master, struct mtd_info *part)
188 +{
189 + run_parsers_by_type(part, MTD_PARSER_TYPE_FIRMWARE);
190 +}
191 +
192 +static void mtd_partition_split(struct mtd_info *master, struct mtd_info *part)
193 +{
194 + static int rootfs_found = 0;
195 +
196 + if (rootfs_found)
197 + return;
198 +
199 + if (!strcmp(part->name, "rootfs")) {
200 + run_parsers_by_type(part, MTD_PARSER_TYPE_ROOTFS);
201 +
202 + rootfs_found = 1;
203 + }
204 +
205 + if (IS_ENABLED(CONFIG_MTD_SPLIT_FIRMWARE) &&
206 + !strcmp(part->name, SPLIT_FIRMWARE_NAME) &&
207 + !of_find_property(mtd_get_of_node(part), "compatible", NULL))
208 + split_firmware(master, part);
209 +}
210 +
211 int mtd_add_partition(struct mtd_info *parent, const char *name,
212 long long offset, long long length)
213 {
214 @@ -275,6 +417,7 @@ int mtd_add_partition(struct mtd_info *p
215 if (ret)
216 goto err_remove_part;
217
218 + mtd_partition_split(parent, child);
219 mtd_add_partition_attrs(child);
220
221 return 0;
222 @@ -423,6 +566,7 @@ int add_mtd_partitions(struct mtd_info *
223 goto err_del_partitions;
224 }
225
226 + mtd_partition_split(master, child);
227 mtd_add_partition_attrs(child);
228
229 /* Look for subpartitions */
230 @@ -439,31 +583,6 @@ err_del_partitions:
231 return ret;
232 }
233
234 -static DEFINE_SPINLOCK(part_parser_lock);
235 -static LIST_HEAD(part_parsers);
236 -
237 -static struct mtd_part_parser *mtd_part_parser_get(const char *name)
238 -{
239 - struct mtd_part_parser *p, *ret = NULL;
240 -
241 - spin_lock(&part_parser_lock);
242 -
243 - list_for_each_entry(p, &part_parsers, list)
244 - if (!strcmp(p->name, name) && try_module_get(p->owner)) {
245 - ret = p;
246 - break;
247 - }
248 -
249 - spin_unlock(&part_parser_lock);
250 -
251 - return ret;
252 -}
253 -
254 -static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
255 -{
256 - module_put(p->owner);
257 -}
258 -
259 /*
260 * Many partition parsers just expected the core to kfree() all their data in
261 * one chunk. Do that by default.
262 --- a/include/linux/mtd/partitions.h
263 +++ b/include/linux/mtd/partitions.h
264 @@ -75,6 +75,12 @@ struct mtd_part_parser_data {
265 * Functions dealing with the various ways of partitioning the space
266 */
267
268 +enum mtd_parser_type {
269 + MTD_PARSER_TYPE_DEVICE = 0,
270 + MTD_PARSER_TYPE_ROOTFS,
271 + MTD_PARSER_TYPE_FIRMWARE,
272 +};
273 +
274 struct mtd_part_parser {
275 struct list_head list;
276 struct module *owner;
277 @@ -83,6 +89,7 @@ struct mtd_part_parser {
278 int (*parse_fn)(struct mtd_info *, const struct mtd_partition **,
279 struct mtd_part_parser_data *);
280 void (*cleanup)(const struct mtd_partition *pparts, int nr_parts);
281 + enum mtd_parser_type type;
282 };
283
284 /* Container for passing around a set of parsed partitions */
285 --- a/drivers/mtd/Makefile
286 +++ b/drivers/mtd/Makefile
287 @@ -9,6 +9,8 @@ mtd-y := mtdcore.o mtdsuper.o mtdconc
288
289 obj-y += parsers/
290
291 +obj-$(CONFIG_MTD_SPLIT) += mtdsplit/
292 +
293 # 'Users' - code which presents functionality to userspace.
294 obj-$(CONFIG_MTD_BLKDEVS) += mtd_blkdevs.o
295 obj-$(CONFIG_MTD_BLOCK) += mtdblock.o
296 --- a/include/linux/mtd/mtd.h
297 +++ b/include/linux/mtd/mtd.h
298 @@ -606,6 +606,24 @@ static inline void mtd_align_erase_req(s
299 req->len += mtd->erasesize - mod;
300 }
301
302 +static inline uint64_t mtd_roundup_to_eb(uint64_t sz, struct mtd_info *mtd)
303 +{
304 + if (mtd_mod_by_eb(sz, mtd) == 0)
305 + return sz;
306 +
307 + /* Round up to next erase block */
308 + return (mtd_div_by_eb(sz, mtd) + 1) * mtd->erasesize;
309 +}
310 +
311 +static inline uint64_t mtd_rounddown_to_eb(uint64_t sz, struct mtd_info *mtd)
312 +{
313 + if (mtd_mod_by_eb(sz, mtd) == 0)
314 + return sz;
315 +
316 + /* Round down to the start of the current erase block */
317 + return (mtd_div_by_eb(sz, mtd)) * mtd->erasesize;
318 +}
319 +
320 static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
321 {
322 if (mtd->writesize_shift)
323 @@ -679,6 +697,13 @@ extern struct mtd_info *of_get_mtd_devic
324 extern struct mtd_info *get_mtd_device_nm(const char *name);
325 extern void put_mtd_device(struct mtd_info *mtd);
326
327 +static inline uint64_t mtdpart_get_offset(const struct mtd_info *mtd)
328 +{
329 + if (!mtd_is_partition(mtd))
330 + return 0;
331 +
332 + return mtd->part.offset;
333 +}
334
335 struct mtd_notifier {
336 void (*add)(struct mtd_info *mtd);