1 From: Gabor Juhos <juhosg@openwrt.org>
2 Subject: mtd: Add new Kconfig option for firmware partition split
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.
9 The patch is in preparation for multiple firmware format
12 Submitted-by: Gabor Juhos <juhosg@openwrt.org>
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(-)
23 --- a/drivers/mtd/Kconfig
24 +++ b/drivers/mtd/Kconfig
25 @@ -12,6 +12,25 @@ menuconfig MTD
29 +menu "OpenWrt specific MTD options"
31 +config MTD_ROOTFS_ROOT_DEV
32 + bool "Automatically set 'rootfs' partition to be root filesystem"
35 +config MTD_SPLIT_FIRMWARE
36 + bool "Automatically split firmware partition for kernel+rootfs"
39 +config MTD_SPLIT_FIRMWARE_NAME
40 + string "Firmware partition name"
41 + depends on MTD_SPLIT_FIRMWARE
44 +source "drivers/mtd/mtdsplit/Kconfig"
49 tristate "MTD tests support (DANGEROUS)"
51 --- a/drivers/mtd/mtdpart.c
52 +++ b/drivers/mtd/mtdpart.c
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>
60 #include <linux/of_platform.h>
63 +#include "mtdsplit/mtdsplit.h"
66 * MTD methods which simply translate the effective address and pass through
67 @@ -237,6 +239,146 @@ static int mtd_add_partition_attrs(struc
71 +static DEFINE_SPINLOCK(part_parser_lock);
72 +static LIST_HEAD(part_parsers);
74 +static struct mtd_part_parser *mtd_part_parser_get(const char *name)
76 + struct mtd_part_parser *p, *ret = NULL;
78 + spin_lock(&part_parser_lock);
80 + list_for_each_entry(p, &part_parsers, list)
81 + if (!strcmp(p->name, name) && try_module_get(p->owner)) {
86 + spin_unlock(&part_parser_lock);
91 +static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
93 + module_put(p->owner);
96 +static struct mtd_part_parser *
97 +get_partition_parser_by_type(enum mtd_parser_type type,
98 + struct mtd_part_parser *start)
100 + struct mtd_part_parser *p, *ret = NULL;
102 + spin_lock(&part_parser_lock);
104 + p = list_prepare_entry(start, &part_parsers, list);
106 + mtd_part_parser_put(start);
108 + list_for_each_entry_continue(p, &part_parsers, list) {
109 + if (p->type == type && try_module_get(p->owner)) {
115 + spin_unlock(&part_parser_lock);
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)
125 + struct mtd_part_parser *prev = NULL;
129 + struct mtd_part_parser *parser;
131 + parser = get_partition_parser_by_type(type, prev);
135 + ret = (*parser->parse_fn)(master, pparts, data);
138 + mtd_part_parser_put(parser);
140 + "%d %s partitions found on MTD device %s\n",
141 + ret, parser->name, master->name);
152 +run_parsers_by_type(struct mtd_info *child, enum mtd_parser_type type)
154 + struct mtd_partition *parts;
158 + nr_parts = parse_mtd_partitions_by_type(child, type, (const struct mtd_partition **)&parts,
163 + if (WARN_ON(!parts))
166 + for (i = 0; i < nr_parts; i++) {
167 + /* adjust partition offsets */
168 + parts[i].offset += child->part.offset;
170 + mtd_add_partition(child->parent,
181 +#ifdef CONFIG_MTD_SPLIT_FIRMWARE_NAME
182 +#define SPLIT_FIRMWARE_NAME CONFIG_MTD_SPLIT_FIRMWARE_NAME
184 +#define SPLIT_FIRMWARE_NAME "unused"
187 +static void split_firmware(struct mtd_info *master, struct mtd_info *part)
189 + run_parsers_by_type(part, MTD_PARSER_TYPE_FIRMWARE);
192 +static void mtd_partition_split(struct mtd_info *master, struct mtd_info *part)
194 + static int rootfs_found = 0;
199 + if (!strcmp(part->name, "rootfs")) {
200 + run_parsers_by_type(part, MTD_PARSER_TYPE_ROOTFS);
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);
211 int mtd_add_partition(struct mtd_info *parent, const char *name,
212 long long offset, long long length)
214 @@ -275,6 +417,7 @@ int mtd_add_partition(struct mtd_info *p
216 goto err_remove_part;
218 + mtd_partition_split(parent, child);
219 mtd_add_partition_attrs(child);
222 @@ -423,6 +566,7 @@ int add_mtd_partitions(struct mtd_info *
223 goto err_del_partitions;
226 + mtd_partition_split(master, child);
227 mtd_add_partition_attrs(child);
229 /* Look for subpartitions */
230 @@ -439,31 +583,6 @@ err_del_partitions:
234 -static DEFINE_SPINLOCK(part_parser_lock);
235 -static LIST_HEAD(part_parsers);
237 -static struct mtd_part_parser *mtd_part_parser_get(const char *name)
239 - struct mtd_part_parser *p, *ret = NULL;
241 - spin_lock(&part_parser_lock);
243 - list_for_each_entry(p, &part_parsers, list)
244 - if (!strcmp(p->name, name) && try_module_get(p->owner)) {
249 - spin_unlock(&part_parser_lock);
254 -static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
256 - module_put(p->owner);
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
268 +enum mtd_parser_type {
269 + MTD_PARSER_TYPE_DEVICE = 0,
270 + MTD_PARSER_TYPE_ROOTFS,
271 + MTD_PARSER_TYPE_FIRMWARE,
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;
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
291 +obj-$(CONFIG_MTD_SPLIT) += mtdsplit/
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;
302 +static inline uint64_t mtd_roundup_to_eb(uint64_t sz, struct mtd_info *mtd)
304 + if (mtd_mod_by_eb(sz, mtd) == 0)
307 + /* Round up to next erase block */
308 + return (mtd_div_by_eb(sz, mtd) + 1) * mtd->erasesize;
311 +static inline uint64_t mtd_rounddown_to_eb(uint64_t sz, struct mtd_info *mtd)
313 + if (mtd_mod_by_eb(sz, mtd) == 0)
316 + /* Round down to the start of the current erase block */
317 + return (mtd_div_by_eb(sz, mtd)) * mtd->erasesize;
320 static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
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);
327 +static inline uint64_t mtdpart_get_offset(const struct mtd_info *mtd)
329 + if (!mtd_is_partition(mtd))
332 + return mtd->part.offset;
335 struct mtd_notifier {
336 void (*add)(struct mtd_info *mtd);