1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -12,6 +12,27 @@ 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"
17 +config MTD_UIMAGE_SPLIT
18 + bool "Automatically split off rootfs from a kernel partition containing a uImage"
21 +config MTD_UIMAGE_SPLIT_NAME
22 + string "uImage partition name"
23 + depends on MTD_UIMAGE_SPLIT
29 tristate "MTD tests support (DANGEROUS)"
31 --- a/drivers/mtd/mtdpart.c
32 +++ b/drivers/mtd/mtdpart.c
34 #include <linux/kmod.h>
35 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/partitions.h>
37 +#include <linux/root_dev.h>
38 +#include <linux/magic.h>
39 #include <linux/err.h>
42 @@ -45,13 +47,14 @@ struct mtd_part {
43 struct list_head list;
46 +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part);
49 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
50 * the pointer to that structure with this macro.
52 #define PART(x) ((struct mtd_part *)(x))
56 * MTD methods which simply translate the effective address and pass through
57 * to the _real_ device.
58 @@ -533,8 +536,10 @@ out_register:
62 -int mtd_add_partition(struct mtd_info *master, char *name,
63 - long long offset, long long length)
66 +__mtd_add_partition(struct mtd_info *master, char *name,
67 + long long offset, long long length, bool dup_check)
69 struct mtd_partition part;
70 struct mtd_part *p, *new;
71 @@ -566,21 +571,24 @@ int mtd_add_partition(struct mtd_info *m
72 end = offset + length;
74 mutex_lock(&mtd_partitions_mutex);
75 - list_for_each_entry(p, &mtd_partitions, list)
76 - if (p->master == master) {
77 - if ((start >= p->offset) &&
78 - (start < (p->offset + p->mtd.size)))
81 - if ((end >= p->offset) &&
82 - (end < (p->offset + p->mtd.size)))
86 + list_for_each_entry(p, &mtd_partitions, list)
87 + if (p->master == master) {
88 + if ((start >= p->offset) &&
89 + (start < (p->offset + p->mtd.size)))
92 + if ((end >= p->offset) &&
93 + (end < (p->offset + p->mtd.size)))
98 list_add(&new->list, &mtd_partitions);
99 mutex_unlock(&mtd_partitions_mutex);
101 add_mtd_device(&new->mtd);
102 + mtd_partition_split(master, new);
106 @@ -590,6 +598,12 @@ err_inv:
108 EXPORT_SYMBOL_GPL(mtd_add_partition);
110 +int mtd_add_partition(struct mtd_info *master, char *name,
111 + long long offset, long long length)
113 + return __mtd_add_partition(master, name, offset, length, true);
116 int mtd_del_partition(struct mtd_info *master, int partno)
118 struct mtd_part *slave, *next;
119 @@ -613,6 +627,148 @@ int mtd_del_partition(struct mtd_info *m
121 EXPORT_SYMBOL_GPL(mtd_del_partition);
123 +static inline unsigned long
124 +mtd_pad_erasesize(struct mtd_info *mtd, int offset, int len)
126 + unsigned long mask = mtd->erasesize - 1;
128 + len += offset & mask;
129 + len = (len + mask) & ~mask;
130 + len -= offset & mask;
134 +#define ROOTFS_SPLIT_NAME "rootfs_data"
136 +struct squashfs_super_block {
143 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
145 + struct squashfs_super_block sb;
148 + ret = mtd_read(master, offset, sizeof(sb), &len, (void *) &sb);
149 + if (ret || (len != sizeof(sb))) {
150 + printk(KERN_ALERT "split_squashfs: error occured while reading "
151 + "from \"%s\"\n", master->name);
155 + if (SQUASHFS_MAGIC != le32_to_cpu(sb.s_magic) ) {
156 + printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
162 + if (le64_to_cpu((sb.bytes_used)) <= 0) {
163 + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
169 + len = (u32) le64_to_cpu(sb.bytes_used);
170 + len = mtd_pad_erasesize(master, offset, len);
171 + *split_offset = offset + len;
176 +static void split_rootfs_data(struct mtd_info *master, struct mtd_part *part)
178 + unsigned int split_offset = 0;
179 + unsigned int split_size;
182 + ret = split_squashfs(master, part->offset, &split_offset);
186 + if (split_offset <= 0)
189 + split_size = part->mtd.size - (split_offset - part->offset);
190 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=0x%x, len=0x%x\n",
191 + ROOTFS_SPLIT_NAME, split_offset, split_size);
193 + __mtd_add_partition(master, ROOTFS_SPLIT_NAME, split_offset,
194 + split_size, false);
197 +#define UBOOT_MAGIC 0x27051956
199 +#ifdef CONFIG_MTD_UIMAGE_SPLIT_NAME
200 +#define UIMAGE_SPLIT_NAME CONFIG_MTD_UIMAGE_SPLIT_NAME
202 +#define UIMAGE_SPLIT_NAME "unused"
205 +static void split_uimage(struct mtd_info *master, struct mtd_part *part)
214 + if (strcmp(part->mtd.name, UIMAGE_SPLIT_NAME) != 0)
217 + if (mtd_read(master, part->offset, sizeof(hdr), &len, (void *) &hdr))
220 + if (len != sizeof(hdr) || hdr.magic != cpu_to_be32(UBOOT_MAGIC))
223 + len = be32_to_cpu(hdr.size) + 0x40;
224 + len = mtd_pad_erasesize(master, part->offset, len);
225 + if (len + master->erasesize > part->mtd.size)
228 + __mtd_add_partition(master, "rootfs", part->offset + len,
229 + part->mtd.size - len, false);
232 +void __weak arch_split_mtd_part(struct mtd_info *master, const char *name,
233 + int offset, int size)
238 +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part)
240 + static int rootfs_found = 0;
245 + if (!strcmp(part->mtd.name, "rootfs")) {
248 + if (config_enabled(CONFIG_MTD_ROOTFS_ROOT_DEV) &&
250 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
251 + "set to be root filesystem\n");
252 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, part->mtd.index);
255 + if (config_enabled(CONFIG_MTD_ROOTFS_SPLIT))
256 + split_rootfs_data(master, part);
259 + if (config_enabled(CONFIG_MTD_UIMAGE_SPLIT))
260 + split_uimage(master, part);
262 + arch_split_mtd_part(master, part->mtd.name, part->offset,
266 * This function, given a master MTD object and a partition table, creates
267 * and registers slave MTD objects which are bound to the master according to
268 @@ -642,6 +798,7 @@ int add_mtd_partitions(struct mtd_info *
269 mutex_unlock(&mtd_partitions_mutex);
271 add_mtd_device(&slave->mtd);
272 + mtd_partition_split(master, slave);
274 cur_offset = slave->offset + slave->mtd.size;
276 --- a/include/linux/mtd/partitions.h
277 +++ b/include/linux/mtd/partitions.h
278 @@ -84,5 +84,7 @@ int mtd_add_partition(struct mtd_info *m
279 long long offset, long long length);
280 int mtd_del_partition(struct mtd_info *master, int partno);
281 uint64_t mtd_get_device_size(const struct mtd_info *mtd);
282 +extern void __weak arch_split_mtd_part(struct mtd_info *master,
283 + const char *name, int offset, int size);