1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -12,6 +12,33 @@ 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_ROOTFS_SPLIT
14 + bool "Automatically split 'rootfs' partition for squashfs"
18 +config MTD_SPLIT_FIRMWARE
19 + bool "Automatically split firmware partition for kernel+rootfs"
22 +config MTD_SPLIT_FIRMWARE_NAME
23 + string "Firmware partition name"
24 + depends on MTD_SPLIT_FIRMWARE
27 +config MTD_UIMAGE_SPLIT
28 + bool "Enable split support for firmware partitions containing a uImage"
29 + depends on MTD_SPLIT_FIRMWARE
35 tristate "MTD tests support (DANGEROUS)"
37 --- a/drivers/mtd/mtdpart.c
38 +++ b/drivers/mtd/mtdpart.c
40 #include <linux/kmod.h>
41 #include <linux/mtd/mtd.h>
42 #include <linux/mtd/partitions.h>
43 +#include <linux/magic.h>
44 #include <linux/err.h>
47 +#include "mtdsplit.h"
49 /* Our partition linked list */
50 static LIST_HEAD(mtd_partitions);
51 @@ -45,13 +47,14 @@ struct mtd_part {
52 struct list_head list;
55 +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part);
58 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
59 * the pointer to that structure with this macro.
61 #define PART(x) ((struct mtd_part *)(x))
65 * MTD methods which simply translate the effective address and pass through
66 * to the _real_ device.
67 @@ -534,8 +537,10 @@ out_register:
71 -int mtd_add_partition(struct mtd_info *master, const char *name,
72 - long long offset, long long length)
75 +__mtd_add_partition(struct mtd_info *master, const char *name,
76 + long long offset, long long length, bool dup_check)
78 struct mtd_partition part;
79 struct mtd_part *p, *new;
80 @@ -567,21 +572,24 @@ int mtd_add_partition(struct mtd_info *m
81 end = offset + length;
83 mutex_lock(&mtd_partitions_mutex);
84 - list_for_each_entry(p, &mtd_partitions, list)
85 - if (p->master == master) {
86 - if ((start >= p->offset) &&
87 - (start < (p->offset + p->mtd.size)))
90 - if ((end >= p->offset) &&
91 - (end < (p->offset + p->mtd.size)))
95 + list_for_each_entry(p, &mtd_partitions, list)
96 + if (p->master == master) {
97 + if ((start >= p->offset) &&
98 + (start < (p->offset + p->mtd.size)))
101 + if ((end >= p->offset) &&
102 + (end < (p->offset + p->mtd.size)))
107 list_add(&new->list, &mtd_partitions);
108 mutex_unlock(&mtd_partitions_mutex);
110 add_mtd_device(&new->mtd);
111 + mtd_partition_split(master, new);
115 @@ -591,6 +599,12 @@ err_inv:
117 EXPORT_SYMBOL_GPL(mtd_add_partition);
119 +int mtd_add_partition(struct mtd_info *master, const char *name,
120 + long long offset, long long length)
122 + return __mtd_add_partition(master, name, offset, length, true);
125 int mtd_del_partition(struct mtd_info *master, int partno)
127 struct mtd_part *slave, *next;
128 @@ -614,6 +628,122 @@ int mtd_del_partition(struct mtd_info *m
130 EXPORT_SYMBOL_GPL(mtd_del_partition);
132 +static inline unsigned long
133 +mtd_pad_erasesize(struct mtd_info *mtd, int offset, int len)
135 + unsigned long mask = mtd->erasesize - 1;
137 + len += offset & mask;
138 + len = (len + mask) & ~mask;
139 + len -= offset & mask;
143 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
145 + size_t squashfs_len;
148 + ret = mtd_get_squashfs_len(master, offset, &squashfs_len);
152 + len = mtd_pad_erasesize(master, offset, squashfs_len);
153 + *split_offset = offset + len;
158 +static void split_rootfs_data(struct mtd_info *master, struct mtd_part *part)
160 + unsigned int split_offset = 0;
161 + unsigned int split_size;
164 + ret = split_squashfs(master, part->offset, &split_offset);
168 + if (split_offset <= 0)
171 + if (config_enabled(CONFIG_MTD_SPLIT_SQUASHFS_ROOT))
172 + pr_err("Dedicated partitioner didn't create \"rootfs_data\" partition, please fill a bug report!\n");
174 + pr_warn("Support for built-in \"rootfs_data\" splitter will be removed, please use CONFIG_MTD_SPLIT_SQUASHFS_ROOT\n");
176 + split_size = part->mtd.size - (split_offset - part->offset);
177 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=0x%x, len=0x%x\n",
178 + ROOTFS_SPLIT_NAME, split_offset, split_size);
180 + __mtd_add_partition(master, ROOTFS_SPLIT_NAME, split_offset,
181 + split_size, false);
184 +#define UBOOT_MAGIC 0x27051956
186 +static void split_uimage(struct mtd_info *master, struct mtd_part *part)
195 + if (mtd_read(master, part->offset, sizeof(hdr), &len, (void *) &hdr))
198 + if (len != sizeof(hdr) || hdr.magic != cpu_to_be32(UBOOT_MAGIC))
201 + len = be32_to_cpu(hdr.size) + 0x40;
202 + len = mtd_pad_erasesize(master, part->offset, len);
203 + if (len + master->erasesize > part->mtd.size)
206 + __mtd_add_partition(master, "rootfs", part->offset + len,
207 + part->mtd.size - len, false);
210 +#ifdef CONFIG_MTD_SPLIT_FIRMWARE_NAME
211 +#define SPLIT_FIRMWARE_NAME CONFIG_MTD_SPLIT_FIRMWARE_NAME
213 +#define SPLIT_FIRMWARE_NAME "unused"
216 +static void split_firmware(struct mtd_info *master, struct mtd_part *part)
218 + if (config_enabled(CONFIG_MTD_UIMAGE_SPLIT))
219 + split_uimage(master, part);
222 +void __weak arch_split_mtd_part(struct mtd_info *master, const char *name,
223 + int offset, int size)
227 +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part)
229 + static int rootfs_found = 0;
234 + if (!strcmp(part->mtd.name, "rootfs")) {
237 + if (config_enabled(CONFIG_MTD_ROOTFS_SPLIT))
238 + split_rootfs_data(master, part);
241 + if (!strcmp(part->mtd.name, SPLIT_FIRMWARE_NAME) &&
242 + config_enabled(CONFIG_MTD_SPLIT_FIRMWARE))
243 + split_firmware(master, part);
245 + arch_split_mtd_part(master, part->mtd.name, part->offset,
249 * This function, given a master MTD object and a partition table, creates
250 * and registers slave MTD objects which are bound to the master according to
251 @@ -643,6 +773,7 @@ int add_mtd_partitions(struct mtd_info *
252 mutex_unlock(&mtd_partitions_mutex);
254 add_mtd_device(&slave->mtd);
255 + mtd_partition_split(master, slave);
257 cur_offset = slave->offset + slave->mtd.size;
259 --- a/include/linux/mtd/partitions.h
260 +++ b/include/linux/mtd/partitions.h
261 @@ -84,5 +84,7 @@ int mtd_add_partition(struct mtd_info *m
262 long long offset, long long length);
263 int mtd_del_partition(struct mtd_info *master, int partno);
264 uint64_t mtd_get_device_size(const struct mtd_info *mtd);
265 +extern void __weak arch_split_mtd_part(struct mtd_info *master,
266 + const char *name, int offset, int size);