1 From 39717277d5c87bdb183cf2f258957b44ba99b4df Mon Sep 17 00:00:00 2001
2 From: OpenWrt community <openwrt-devel@lists.openwrt.org>
3 Date: Wed, 13 Jul 2022 11:47:35 +0200
4 Subject: [PATCH] mtd: mtdsplit support
7 drivers/mtd/Kconfig | 19 ++++
8 drivers/mtd/Makefile | 2 +
9 drivers/mtd/mtdpart.c | 169 ++++++++++++++++++++++++++++-----
10 include/linux/mtd/mtd.h | 25 +++++
11 include/linux/mtd/partitions.h | 7 ++
12 5 files changed, 197 insertions(+), 25 deletions(-)
14 --- a/drivers/mtd/Kconfig
15 +++ b/drivers/mtd/Kconfig
16 @@ -12,6 +12,25 @@ menuconfig MTD
20 +menu "OpenWrt specific MTD options"
22 +config MTD_ROOTFS_ROOT_DEV
23 + bool "Automatically set 'rootfs' partition to be root filesystem"
26 +config MTD_SPLIT_FIRMWARE
27 + bool "Automatically split firmware partition for kernel+rootfs"
30 +config MTD_SPLIT_FIRMWARE_NAME
31 + string "Firmware partition name"
32 + depends on MTD_SPLIT_FIRMWARE
35 +source "drivers/mtd/mtdsplit/Kconfig"
40 tristate "MTD tests support (DANGEROUS)"
42 --- a/drivers/mtd/Makefile
43 +++ b/drivers/mtd/Makefile
44 @@ -9,6 +9,8 @@ mtd-y := mtdcore.o mtdsuper.o mtdconc
48 +obj-$(CONFIG_MTD_SPLIT) += mtdsplit/
50 # 'Users' - code which presents functionality to userspace.
51 obj-$(CONFIG_MTD_BLKDEVS) += mtd_blkdevs.o
52 obj-$(CONFIG_MTD_BLOCK) += mtdblock.o
53 --- a/drivers/mtd/mtdpart.c
54 +++ b/drivers/mtd/mtdpart.c
56 #include <linux/kmod.h>
57 #include <linux/mtd/mtd.h>
58 #include <linux/mtd/partitions.h>
59 +#include <linux/magic.h>
60 #include <linux/err.h>
62 #include <linux/of_platform.h>
65 +#include "mtdsplit/mtdsplit.h"
68 * MTD methods which simply translate the effective address and pass through
69 @@ -242,6 +244,147 @@ static int mtd_add_partition_attrs(struc
73 +static DEFINE_SPINLOCK(part_parser_lock);
74 +static LIST_HEAD(part_parsers);
76 +static struct mtd_part_parser *mtd_part_parser_get(const char *name)
78 + struct mtd_part_parser *p, *ret = NULL;
80 + spin_lock(&part_parser_lock);
82 + list_for_each_entry(p, &part_parsers, list)
83 + if (!strcmp(p->name, name) && try_module_get(p->owner)) {
88 + spin_unlock(&part_parser_lock);
93 +static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
95 + module_put(p->owner);
98 +static struct mtd_part_parser *
99 +get_partition_parser_by_type(enum mtd_parser_type type,
100 + struct mtd_part_parser *start)
102 + struct mtd_part_parser *p, *ret = NULL;
104 + spin_lock(&part_parser_lock);
106 + p = list_prepare_entry(start, &part_parsers, list);
108 + mtd_part_parser_put(start);
110 + list_for_each_entry_continue(p, &part_parsers, list) {
111 + if (p->type == type && try_module_get(p->owner)) {
117 + spin_unlock(&part_parser_lock);
122 +static int parse_mtd_partitions_by_type(struct mtd_info *master,
123 + enum mtd_parser_type type,
124 + const struct mtd_partition **pparts,
125 + struct mtd_part_parser_data *data)
127 + struct mtd_part_parser *prev = NULL;
131 + struct mtd_part_parser *parser;
133 + parser = get_partition_parser_by_type(type, prev);
137 + ret = (*parser->parse_fn)(master, pparts, data);
140 + mtd_part_parser_put(parser);
142 + "%d %s partitions found on MTD device %s\n",
143 + ret, parser->name, master->name);
154 +run_parsers_by_type(struct mtd_info *child, enum mtd_parser_type type)
156 + struct mtd_partition *parts;
160 + nr_parts = parse_mtd_partitions_by_type(child, type, (const struct mtd_partition **)&parts,
165 + if (WARN_ON(!parts))
168 + for (i = 0; i < nr_parts; i++) {
169 + /* adjust partition offsets */
170 + parts[i].offset += child->part.offset;
172 + mtd_add_partition(child->parent,
183 +#ifdef CONFIG_MTD_SPLIT_FIRMWARE_NAME
184 +#define SPLIT_FIRMWARE_NAME CONFIG_MTD_SPLIT_FIRMWARE_NAME
186 +#define SPLIT_FIRMWARE_NAME "unused"
189 +static void split_firmware(struct mtd_info *master, struct mtd_info *part)
191 + run_parsers_by_type(part, MTD_PARSER_TYPE_FIRMWARE);
194 +static void mtd_partition_split(struct mtd_info *master, struct mtd_info *part)
196 + static int rootfs_found = 0;
201 + if (of_find_property(mtd_get_of_node(part), "linux,rootfs", NULL) ||
202 + !strcmp(part->name, "rootfs")) {
203 + run_parsers_by_type(part, MTD_PARSER_TYPE_ROOTFS);
208 + if (IS_ENABLED(CONFIG_MTD_SPLIT_FIRMWARE) &&
209 + !strcmp(part->name, SPLIT_FIRMWARE_NAME) &&
210 + !of_find_property(mtd_get_of_node(part), "compatible", NULL))
211 + split_firmware(master, part);
214 int mtd_add_partition(struct mtd_info *parent, const char *name,
215 long long offset, long long length)
217 @@ -280,6 +423,7 @@ int mtd_add_partition(struct mtd_info *p
219 goto err_remove_part;
221 + mtd_partition_split(parent, child);
222 mtd_add_partition_attrs(child);
225 @@ -423,6 +567,7 @@ int add_mtd_partitions(struct mtd_info *
226 goto err_del_partitions;
229 + mtd_partition_split(master, child);
230 mtd_add_partition_attrs(child);
232 /* Look for subpartitions */
233 @@ -439,31 +584,6 @@ err_del_partitions:
237 -static DEFINE_SPINLOCK(part_parser_lock);
238 -static LIST_HEAD(part_parsers);
240 -static struct mtd_part_parser *mtd_part_parser_get(const char *name)
242 - struct mtd_part_parser *p, *ret = NULL;
244 - spin_lock(&part_parser_lock);
246 - list_for_each_entry(p, &part_parsers, list)
247 - if (!strcmp(p->name, name) && try_module_get(p->owner)) {
252 - spin_unlock(&part_parser_lock);
257 -static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
259 - module_put(p->owner);
263 * Many partition parsers just expected the core to kfree() all their data in
264 * one chunk. Do that by default.
265 --- a/include/linux/mtd/mtd.h
266 +++ b/include/linux/mtd/mtd.h
267 @@ -615,6 +615,24 @@ static inline void mtd_align_erase_req(s
268 req->len += mtd->erasesize - mod;
271 +static inline uint64_t mtd_roundup_to_eb(uint64_t sz, struct mtd_info *mtd)
273 + if (mtd_mod_by_eb(sz, mtd) == 0)
276 + /* Round up to next erase block */
277 + return (mtd_div_by_eb(sz, mtd) + 1) * mtd->erasesize;
280 +static inline uint64_t mtd_rounddown_to_eb(uint64_t sz, struct mtd_info *mtd)
282 + if (mtd_mod_by_eb(sz, mtd) == 0)
285 + /* Round down to the start of the current erase block */
286 + return (mtd_div_by_eb(sz, mtd)) * mtd->erasesize;
289 static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
291 if (mtd->writesize_shift)
292 @@ -688,6 +706,13 @@ extern struct mtd_info *of_get_mtd_devic
293 extern struct mtd_info *get_mtd_device_nm(const char *name);
294 extern void put_mtd_device(struct mtd_info *mtd);
296 +static inline uint64_t mtdpart_get_offset(const struct mtd_info *mtd)
298 + if (!mtd_is_partition(mtd))
301 + return mtd->part.offset;
304 struct mtd_notifier {
305 void (*add)(struct mtd_info *mtd);
306 --- a/include/linux/mtd/partitions.h
307 +++ b/include/linux/mtd/partitions.h
308 @@ -75,6 +75,12 @@ struct mtd_part_parser_data {
309 * Functions dealing with the various ways of partitioning the space
312 +enum mtd_parser_type {
313 + MTD_PARSER_TYPE_DEVICE = 0,
314 + MTD_PARSER_TYPE_ROOTFS,
315 + MTD_PARSER_TYPE_FIRMWARE,
318 struct mtd_part_parser {
319 struct list_head list;
320 struct module *owner;
321 @@ -83,6 +89,7 @@ struct mtd_part_parser {
322 int (*parse_fn)(struct mtd_info *, const struct mtd_partition **,
323 struct mtd_part_parser_data *);
324 void (*cleanup)(const struct mtd_partition *pparts, int nr_parts);
325 + enum mtd_parser_type type;
328 /* Container for passing around a set of parsed partitions */