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 @@ -236,6 +238,146 @@ 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 (!strcmp(part->name, "rootfs")) {
202 + run_parsers_by_type(part, MTD_PARSER_TYPE_ROOTFS);
207 + if (IS_ENABLED(CONFIG_MTD_SPLIT_FIRMWARE) &&
208 + !strcmp(part->name, SPLIT_FIRMWARE_NAME) &&
209 + !of_find_property(mtd_get_of_node(part), "compatible", NULL))
210 + split_firmware(master, part);
213 int mtd_add_partition(struct mtd_info *parent, const char *name,
214 long long offset, long long length)
216 @@ -274,6 +416,7 @@ int mtd_add_partition(struct mtd_info *p
218 goto err_remove_part;
220 + mtd_partition_split(parent, child);
221 mtd_add_partition_attrs(child);
224 @@ -422,6 +565,7 @@ int add_mtd_partitions(struct mtd_info *
225 goto err_del_partitions;
228 + mtd_partition_split(master, child);
229 mtd_add_partition_attrs(child);
231 /* Look for subpartitions */
232 @@ -438,31 +582,6 @@ err_del_partitions:
236 -static DEFINE_SPINLOCK(part_parser_lock);
237 -static LIST_HEAD(part_parsers);
239 -static struct mtd_part_parser *mtd_part_parser_get(const char *name)
241 - struct mtd_part_parser *p, *ret = NULL;
243 - spin_lock(&part_parser_lock);
245 - list_for_each_entry(p, &part_parsers, list)
246 - if (!strcmp(p->name, name) && try_module_get(p->owner)) {
251 - spin_unlock(&part_parser_lock);
256 -static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
258 - module_put(p->owner);
262 * Many partition parsers just expected the core to kfree() all their data in
263 * one chunk. Do that by default.
264 --- a/include/linux/mtd/mtd.h
265 +++ b/include/linux/mtd/mtd.h
266 @@ -613,6 +613,24 @@ static inline void mtd_align_erase_req(s
267 req->len += mtd->erasesize - mod;
270 +static inline uint64_t mtd_roundup_to_eb(uint64_t sz, struct mtd_info *mtd)
272 + if (mtd_mod_by_eb(sz, mtd) == 0)
275 + /* Round up to next erase block */
276 + return (mtd_div_by_eb(sz, mtd) + 1) * mtd->erasesize;
279 +static inline uint64_t mtd_rounddown_to_eb(uint64_t sz, struct mtd_info *mtd)
281 + if (mtd_mod_by_eb(sz, mtd) == 0)
284 + /* Round down to the start of the current erase block */
285 + return (mtd_div_by_eb(sz, mtd)) * mtd->erasesize;
288 static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
290 if (mtd->writesize_shift)
291 @@ -686,6 +704,13 @@ extern struct mtd_info *of_get_mtd_devic
292 extern struct mtd_info *get_mtd_device_nm(const char *name);
293 extern void put_mtd_device(struct mtd_info *mtd);
295 +static inline uint64_t mtdpart_get_offset(const struct mtd_info *mtd)
297 + if (!mtd_is_partition(mtd))
300 + return mtd->part.offset;
303 struct mtd_notifier {
304 void (*add)(struct mtd_info *mtd);
305 --- a/include/linux/mtd/partitions.h
306 +++ b/include/linux/mtd/partitions.h
307 @@ -75,6 +75,12 @@ struct mtd_part_parser_data {
308 * Functions dealing with the various ways of partitioning the space
311 +enum mtd_parser_type {
312 + MTD_PARSER_TYPE_DEVICE = 0,
313 + MTD_PARSER_TYPE_ROOTFS,
314 + MTD_PARSER_TYPE_FIRMWARE,
317 struct mtd_part_parser {
318 struct list_head list;
319 struct module *owner;
320 @@ -83,6 +89,7 @@ struct mtd_part_parser {
321 int (*parse_fn)(struct mtd_info *, const struct mtd_partition **,
322 struct mtd_part_parser_data *);
323 void (*cleanup)(const struct mtd_partition *pparts, int nr_parts);
324 + enum mtd_parser_type type;
327 /* Container for passing around a set of parsed partitions */