1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -23,6 +23,23 @@ config MTD_TESTS
4 WARNING: some of the tests will ERASE entire MTD device which they
5 test. Do not use these tests unless you really know what you do.
7 +config MTD_ROOTFS_ROOT_DEV
8 + bool "Automatically set 'rootfs' partition to be root filesystem"
11 +config MTD_ROOTFS_SPLIT
12 + bool "Automatically split 'rootfs' partition for squashfs"
15 +config MTD_UIMAGE_SPLIT
16 + bool "Automatically split off rootfs from a kernel partition containing a uImage"
19 +config MTD_UIMAGE_SPLIT_NAME
20 + string "uImage partition name"
21 + depends on MTD_UIMAGE_SPLIT
24 config MTD_REDBOOT_PARTS
25 tristate "RedBoot partition table parsing"
27 --- a/drivers/mtd/mtdpart.c
28 +++ b/drivers/mtd/mtdpart.c
30 #include <linux/kmod.h>
31 #include <linux/mtd/mtd.h>
32 #include <linux/mtd/partitions.h>
33 +#include <linux/root_dev.h>
34 +#include <linux/magic.h>
35 #include <linux/err.h>
38 @@ -45,12 +47,14 @@ struct mtd_part {
39 struct list_head list;
42 +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part);
45 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
46 * the pointer to that structure with this macro.
48 #define PART(x) ((struct mtd_part *)(x))
50 +#define IS_PART(mtd) (mtd->_read == part_read)
53 * MTD methods which simply translate the effective address and pass through
54 @@ -533,8 +537,10 @@ out_register:
58 -int mtd_add_partition(struct mtd_info *master, char *name,
59 - long long offset, long long length)
62 +__mtd_add_partition(struct mtd_info *master, char *name,
63 + long long offset, long long length, bool dup_check)
65 struct mtd_partition part;
66 struct mtd_part *p, *new;
67 @@ -566,21 +572,24 @@ int mtd_add_partition(struct mtd_info *m
68 end = offset + length;
70 mutex_lock(&mtd_partitions_mutex);
71 - list_for_each_entry(p, &mtd_partitions, list)
72 - if (p->master == master) {
73 - if ((start >= p->offset) &&
74 - (start < (p->offset + p->mtd.size)))
77 - if ((end >= p->offset) &&
78 - (end < (p->offset + p->mtd.size)))
82 + list_for_each_entry(p, &mtd_partitions, list)
83 + if (p->master == master) {
84 + if ((start >= p->offset) &&
85 + (start < (p->offset + p->mtd.size)))
88 + if ((end >= p->offset) &&
89 + (end < (p->offset + p->mtd.size)))
94 list_add(&new->list, &mtd_partitions);
95 mutex_unlock(&mtd_partitions_mutex);
97 add_mtd_device(&new->mtd);
98 + mtd_partition_split(master, new);
102 @@ -590,6 +599,12 @@ err_inv:
104 EXPORT_SYMBOL_GPL(mtd_add_partition);
106 +int mtd_add_partition(struct mtd_info *master, char *name,
107 + long long offset, long long length)
109 + return __mtd_add_partition(master, name, offset, length, true);
112 int mtd_del_partition(struct mtd_info *master, int partno)
114 struct mtd_part *slave, *next;
115 @@ -613,6 +628,149 @@ int mtd_del_partition(struct mtd_info *m
117 EXPORT_SYMBOL_GPL(mtd_del_partition);
119 +static inline unsigned long
120 +mtd_pad_erasesize(struct mtd_info *mtd, int offset, int len)
122 + unsigned long mask = mtd->erasesize - 1;
124 + len += offset & mask;
125 + len = (len + mask) & ~mask;
126 + len -= offset & mask;
130 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
131 +#define ROOTFS_SPLIT_NAME "rootfs_data"
132 +#define ROOTFS_REMOVED_NAME "<removed>"
134 +struct squashfs_super_block {
141 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
143 + struct squashfs_super_block sb;
146 + ret = mtd_read(master, offset, sizeof(sb), &len, (void *) &sb);
147 + if (ret || (len != sizeof(sb))) {
148 + printk(KERN_ALERT "split_squashfs: error occured while reading "
149 + "from \"%s\"\n", master->name);
153 + if (SQUASHFS_MAGIC != le32_to_cpu(sb.s_magic) ) {
154 + printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
160 + if (le64_to_cpu((sb.bytes_used)) <= 0) {
161 + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
167 + len = (u32) le64_to_cpu(sb.bytes_used);
168 + len = mtd_pad_erasesize(master, offset, len);
169 + *split_offset = offset + len;
174 +static void split_rootfs_data(struct mtd_info *master, struct mtd_part *part)
176 + unsigned int split_offset = 0;
177 + unsigned int split_size;
180 + ret = split_squashfs(master, part->offset, &split_offset);
184 + if (split_offset <= 0)
187 + split_size = part->mtd.size - (split_offset - part->offset);
188 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=0x%x, len=0x%x\n",
189 + ROOTFS_SPLIT_NAME, split_offset, split_size);
191 + __mtd_add_partition(master, ROOTFS_SPLIT_NAME, split_offset,
192 + split_size, false);
194 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
196 +#ifdef CONFIG_MTD_UIMAGE_SPLIT
197 +#define UBOOT_MAGIC 0x27051956
199 +static void split_uimage(struct mtd_info *master, struct mtd_part *part)
208 + if (strcmp(part->mtd.name, CONFIG_MTD_UIMAGE_SPLIT_NAME) != 0)
211 + if (mtd_read(master, part->offset, sizeof(hdr), &len, (void *) &hdr))
214 + if (len != sizeof(hdr) || hdr.magic != cpu_to_be32(UBOOT_MAGIC))
217 + len = be32_to_cpu(hdr.size) + 0x40;
218 + len = mtd_pad_erasesize(master, part->offset, len);
219 + if (len + master->erasesize > part->mtd.size)
222 + __mtd_add_partition(master, "rootfs", part->offset + len,
223 + part->mtd.size - len, false);
227 +void __weak arch_split_mtd_part(struct mtd_info *master, const char *name,
228 + int offset, int size)
233 +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part)
235 + static int rootfs_found = 0;
240 + if (!strcmp(part->mtd.name, "rootfs")) {
243 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
244 + if (ROOT_DEV == 0) {
245 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
246 + "set to be root filesystem\n");
247 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, part->mtd.index);
250 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
251 + split_rootfs_data(master, part);
255 +#ifdef CONFIG_MTD_UIMAGE_SPLIT
256 + split_uimage(master, part);
259 + arch_split_mtd_part(master, part->mtd.name, part->offset,
263 * This function, given a master MTD object and a partition table, creates
264 * and registers slave MTD objects which are bound to the master according to
265 @@ -642,6 +800,7 @@ int add_mtd_partitions(struct mtd_info *
266 mutex_unlock(&mtd_partitions_mutex);
268 add_mtd_device(&slave->mtd);
269 + mtd_partition_split(master, slave);
271 cur_offset = slave->offset + slave->mtd.size;
273 --- a/include/linux/mtd/partitions.h
274 +++ b/include/linux/mtd/partitions.h
275 @@ -84,5 +84,7 @@ int mtd_add_partition(struct mtd_info *m
276 long long offset, long long length);
277 int mtd_del_partition(struct mtd_info *master, int partno);
278 uint64_t mtd_get_device_size(const struct mtd_info *mtd);
279 +extern void __weak arch_split_mtd_part(struct mtd_info *master,
280 + const char *name, int offset, int size);