1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -23,6 +23,14 @@ 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_REDBOOT_PARTS
16 tristate "RedBoot partition table parsing"
18 --- a/drivers/mtd/mtdpart.c
19 +++ b/drivers/mtd/mtdpart.c
21 #include <linux/kmod.h>
22 #include <linux/mtd/mtd.h>
23 #include <linux/mtd/partitions.h>
24 +#include <linux/root_dev.h>
25 +#include <linux/magic.h>
26 #include <linux/err.h>
29 @@ -50,7 +52,7 @@ struct mtd_part {
30 * the pointer to that structure with this macro.
32 #define PART(x) ((struct mtd_part *)(x))
34 +#define IS_PART(mtd) (mtd->_read == part_read)
37 * MTD methods which simply translate the effective address and pass through
38 @@ -613,6 +615,92 @@ int mtd_del_partition(struct mtd_info *m
40 EXPORT_SYMBOL_GPL(mtd_del_partition);
42 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
43 +#define ROOTFS_SPLIT_NAME "rootfs_data"
44 +#define ROOTFS_REMOVED_NAME "<removed>"
46 +struct squashfs_super_block {
53 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
55 + struct squashfs_super_block sb;
58 + ret = mtd_read(master, offset, sizeof(sb), &len, (void *) &sb);
59 + if (ret || (len != sizeof(sb))) {
60 + printk(KERN_ALERT "split_squashfs: error occured while reading "
61 + "from \"%s\"\n", master->name);
65 + if (SQUASHFS_MAGIC != le32_to_cpu(sb.s_magic) ) {
66 + printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
72 + if (le64_to_cpu((sb.bytes_used)) <= 0) {
73 + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
79 + len = (u32) le64_to_cpu(sb.bytes_used);
80 + len += (offset & 0x000fffff);
81 + len += (master->erasesize - 1);
82 + len &= ~(master->erasesize - 1);
83 + len -= (offset & 0x000fffff);
84 + *split_offset = offset + len;
89 +static int split_rootfs_data(struct mtd_info *master, struct mtd_info *rpart, const struct mtd_partition *part)
91 + struct mtd_partition dpart;
92 + struct mtd_part *slave = NULL;
93 + struct mtd_part *spart;
94 + int ret, split_offset = 0;
96 + spart = PART(rpart);
97 + ret = split_squashfs(master, spart->offset, &split_offset);
101 + if (split_offset <= 0)
104 + memcpy(&dpart, part, sizeof(dpart));
105 + dpart.name = ROOTFS_SPLIT_NAME;
107 + dpart.size = rpart->size - (split_offset - spart->offset);
108 + dpart.offset = split_offset;
110 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%llX, len=%llX \n",
111 + ROOTFS_SPLIT_NAME, dpart.offset, dpart.size);
113 + slave = allocate_partition(master, &dpart, 0, split_offset);
115 + return PTR_ERR(slave);
116 + mutex_lock(&mtd_partitions_mutex);
117 + list_add(&slave->list, &mtd_partitions);
118 + mutex_unlock(&mtd_partitions_mutex);
120 + add_mtd_device(&slave->mtd);
122 + rpart->split = &slave->mtd;
126 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
129 * This function, given a master MTD object and a partition table, creates
130 * and registers slave MTD objects which are bound to the master according to
131 @@ -629,6 +717,9 @@ int add_mtd_partitions(struct mtd_info *
132 struct mtd_part *slave;
133 uint64_t cur_offset = 0;
135 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
139 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
141 @@ -643,6 +734,21 @@ int add_mtd_partitions(struct mtd_info *
143 add_mtd_device(&slave->mtd);
145 + if (!strcmp(parts[i].name, "rootfs")) {
146 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
147 + if (ROOT_DEV == 0) {
148 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
149 + "set to be root filesystem\n");
150 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
153 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
154 + ret = split_rootfs_data(master, &slave->mtd, &parts[i]);
160 cur_offset = slave->offset + slave->mtd.size;
163 --- a/include/linux/mtd/mtd.h
164 +++ b/include/linux/mtd/mtd.h
165 @@ -114,6 +114,7 @@ struct nand_ecclayout {
167 struct module; /* only needed for owner field in mtd_info */
173 @@ -226,6 +227,8 @@ struct mtd_info {
174 int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
175 int (*_suspend) (struct mtd_info *mtd);
176 void (*_resume) (struct mtd_info *mtd);
177 + struct mtd_info *split;
180 * If the driver is something smart, like UBI, it may need to maintain
181 * its own reference counting. The below functions are only for driver.