1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -12,6 +12,25 @@ menuconfig MTD
7 +menu "OpenWrt specific MTD options"
9 +config MTD_ROOTFS_ROOT_DEV
10 + bool "Automatically set 'rootfs' partition to be root filesystem"
13 +config MTD_SPLIT_FIRMWARE
14 + bool "Automatically split firmware partition for kernel+rootfs"
17 +config MTD_SPLIT_FIRMWARE_NAME
18 + string "Firmware partition name"
19 + depends on MTD_SPLIT_FIRMWARE
22 +source "drivers/mtd/mtdsplit/Kconfig"
27 tristate "MTD tests support (DANGEROUS)"
29 --- a/drivers/mtd/mtdpart.c
30 +++ b/drivers/mtd/mtdpart.c
32 #include <linux/kmod.h>
33 #include <linux/mtd/mtd.h>
34 #include <linux/mtd/partitions.h>
35 +#include <linux/magic.h>
36 #include <linux/err.h>
40 +#include "mtdsplit/mtdsplit.h"
43 * MTD methods which simply translate the effective address and pass through
44 @@ -236,6 +238,146 @@ static int mtd_add_partition_attrs(struc
48 +static DEFINE_SPINLOCK(part_parser_lock);
49 +static LIST_HEAD(part_parsers);
51 +static struct mtd_part_parser *mtd_part_parser_get(const char *name)
53 + struct mtd_part_parser *p, *ret = NULL;
55 + spin_lock(&part_parser_lock);
57 + list_for_each_entry(p, &part_parsers, list)
58 + if (!strcmp(p->name, name) && try_module_get(p->owner)) {
63 + spin_unlock(&part_parser_lock);
68 +static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
70 + module_put(p->owner);
73 +static struct mtd_part_parser *
74 +get_partition_parser_by_type(enum mtd_parser_type type,
75 + struct mtd_part_parser *start)
77 + struct mtd_part_parser *p, *ret = NULL;
79 + spin_lock(&part_parser_lock);
81 + p = list_prepare_entry(start, &part_parsers, list);
83 + mtd_part_parser_put(start);
85 + list_for_each_entry_continue(p, &part_parsers, list) {
86 + if (p->type == type && try_module_get(p->owner)) {
92 + spin_unlock(&part_parser_lock);
97 +static int parse_mtd_partitions_by_type(struct mtd_info *master,
98 + enum mtd_parser_type type,
99 + const struct mtd_partition **pparts,
100 + struct mtd_part_parser_data *data)
102 + struct mtd_part_parser *prev = NULL;
106 + struct mtd_part_parser *parser;
108 + parser = get_partition_parser_by_type(type, prev);
112 + ret = (*parser->parse_fn)(master, pparts, data);
115 + mtd_part_parser_put(parser);
117 + "%d %s partitions found on MTD device %s\n",
118 + ret, parser->name, master->name);
129 +run_parsers_by_type(struct mtd_info *child, enum mtd_parser_type type)
131 + struct mtd_partition *parts;
135 + nr_parts = parse_mtd_partitions_by_type(child, type, (const struct mtd_partition **)&parts,
140 + if (WARN_ON(!parts))
143 + for (i = 0; i < nr_parts; i++) {
144 + /* adjust partition offsets */
145 + parts[i].offset += child->part.offset;
147 + mtd_add_partition(child->parent,
158 +#ifdef CONFIG_MTD_SPLIT_FIRMWARE_NAME
159 +#define SPLIT_FIRMWARE_NAME CONFIG_MTD_SPLIT_FIRMWARE_NAME
161 +#define SPLIT_FIRMWARE_NAME "unused"
164 +static void split_firmware(struct mtd_info *master, struct mtd_info *part)
166 + run_parsers_by_type(part, MTD_PARSER_TYPE_FIRMWARE);
169 +static void mtd_partition_split(struct mtd_info *master, struct mtd_info *part)
171 + static int rootfs_found = 0;
176 + if (!strcmp(part->name, "rootfs")) {
177 + run_parsers_by_type(part, MTD_PARSER_TYPE_ROOTFS);
182 + if (IS_ENABLED(CONFIG_MTD_SPLIT_FIRMWARE) &&
183 + !strcmp(part->name, SPLIT_FIRMWARE_NAME) &&
184 + !of_find_property(mtd_get_of_node(part), "compatible", NULL))
185 + split_firmware(master, part);
188 int mtd_add_partition(struct mtd_info *parent, const char *name,
189 long long offset, long long length)
191 @@ -274,6 +416,7 @@ int mtd_add_partition(struct mtd_info *p
193 goto err_remove_part;
195 + mtd_partition_split(parent, child);
196 mtd_add_partition_attrs(child);
199 @@ -422,6 +565,7 @@ int add_mtd_partitions(struct mtd_info *
200 goto err_del_partitions;
203 + mtd_partition_split(master, child);
204 mtd_add_partition_attrs(child);
206 /* Look for subpartitions */
207 @@ -438,31 +582,6 @@ err_del_partitions:
211 -static DEFINE_SPINLOCK(part_parser_lock);
212 -static LIST_HEAD(part_parsers);
214 -static struct mtd_part_parser *mtd_part_parser_get(const char *name)
216 - struct mtd_part_parser *p, *ret = NULL;
218 - spin_lock(&part_parser_lock);
220 - list_for_each_entry(p, &part_parsers, list)
221 - if (!strcmp(p->name, name) && try_module_get(p->owner)) {
226 - spin_unlock(&part_parser_lock);
231 -static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
233 - module_put(p->owner);
237 * Many partition parsers just expected the core to kfree() all their data in
238 * one chunk. Do that by default.
239 --- a/include/linux/mtd/partitions.h
240 +++ b/include/linux/mtd/partitions.h
241 @@ -75,6 +75,12 @@ struct mtd_part_parser_data {
242 * Functions dealing with the various ways of partitioning the space
245 +enum mtd_parser_type {
246 + MTD_PARSER_TYPE_DEVICE = 0,
247 + MTD_PARSER_TYPE_ROOTFS,
248 + MTD_PARSER_TYPE_FIRMWARE,
251 struct mtd_part_parser {
252 struct list_head list;
253 struct module *owner;
254 @@ -83,6 +89,7 @@ struct mtd_part_parser {
255 int (*parse_fn)(struct mtd_info *, const struct mtd_partition **,
256 struct mtd_part_parser_data *);
257 void (*cleanup)(const struct mtd_partition *pparts, int nr_parts);
258 + enum mtd_parser_type type;
261 /* Container for passing around a set of parsed partitions */
262 --- a/drivers/mtd/Makefile
263 +++ b/drivers/mtd/Makefile
264 @@ -9,6 +9,8 @@ mtd-y := mtdcore.o mtdsuper.o mtdconc
268 +obj-$(CONFIG_MTD_SPLIT) += mtdsplit/
270 # 'Users' - code which presents functionality to userspace.
271 obj-$(CONFIG_MTD_BLKDEVS) += mtd_blkdevs.o
272 obj-$(CONFIG_MTD_BLOCK) += mtdblock.o
273 --- a/include/linux/mtd/mtd.h
274 +++ b/include/linux/mtd/mtd.h
275 @@ -608,6 +608,24 @@ static inline void mtd_align_erase_req(s
276 req->len += mtd->erasesize - mod;
279 +static inline uint64_t mtd_roundup_to_eb(uint64_t sz, struct mtd_info *mtd)
281 + if (mtd_mod_by_eb(sz, mtd) == 0)
284 + /* Round up to next erase block */
285 + return (mtd_div_by_eb(sz, mtd) + 1) * mtd->erasesize;
288 +static inline uint64_t mtd_rounddown_to_eb(uint64_t sz, struct mtd_info *mtd)
290 + if (mtd_mod_by_eb(sz, mtd) == 0)
293 + /* Round down to the start of the current erase block */
294 + return (mtd_div_by_eb(sz, mtd)) * mtd->erasesize;
297 static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
299 if (mtd->writesize_shift)
300 @@ -680,6 +698,13 @@ extern void __put_mtd_device(struct mtd_
301 extern struct mtd_info *get_mtd_device_nm(const char *name);
302 extern void put_mtd_device(struct mtd_info *mtd);
304 +static inline uint64_t mtdpart_get_offset(const struct mtd_info *mtd)
306 + if (!mtd_is_partition(mtd))
309 + return mtd->part.offset;
312 struct mtd_notifier {
313 void (*add)(struct mtd_info *mtd);