1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -47,6 +47,16 @@ config MTD_PARTITIONS
7 +config MTD_ROOTFS_ROOT_DEV
8 + bool "Automatically set 'rootfs' partition to be root filesystem"
9 + depends on MTD_PARTITIONS
12 +config MTD_ROOTFS_SPLIT
13 + bool "Automatically split 'rootfs' partition for squashfs"
14 + depends on MTD_PARTITIONS
17 config MTD_REDBOOT_PARTS
18 tristate "RedBoot partition table parsing"
20 --- a/drivers/mtd/mtdpart.c
21 +++ b/drivers/mtd/mtdpart.c
23 #include <linux/kmod.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/partitions.h>
26 +#include <linux/root_dev.h>
27 +#include <linux/magic.h>
28 #include <linux/err.h>
30 /* Our partition linked list */
31 @@ -48,7 +50,7 @@ struct mtd_part {
32 * the pointer to that structure with this macro.
34 #define PART(x) ((struct mtd_part *)(x))
36 +#define IS_PART(mtd) (mtd->read == part_read)
39 * MTD methods which simply translate the effective address and pass through
40 @@ -636,6 +638,155 @@ int mtd_del_partition(struct mtd_info *m
42 EXPORT_SYMBOL_GPL(mtd_del_partition);
44 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
45 +#define ROOTFS_SPLIT_NAME "rootfs_data"
46 +#define ROOTFS_REMOVED_NAME "<removed>"
48 +struct squashfs_super_block {
55 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
57 + struct squashfs_super_block sb;
60 + ret = master->read(master, offset, sizeof(sb), &len, (void *) &sb);
61 + if (ret || (len != sizeof(sb))) {
62 + printk(KERN_ALERT "split_squashfs: error occured while reading "
63 + "from \"%s\"\n", master->name);
67 + if (SQUASHFS_MAGIC != le32_to_cpu(sb.s_magic) ) {
68 + printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
74 + if (le64_to_cpu((sb.bytes_used)) <= 0) {
75 + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
81 + len = (u32) le64_to_cpu(sb.bytes_used);
82 + len += (offset & 0x000fffff);
83 + len += (master->erasesize - 1);
84 + len &= ~(master->erasesize - 1);
85 + len -= (offset & 0x000fffff);
86 + *split_offset = offset + len;
91 +static int split_rootfs_data(struct mtd_info *master, struct mtd_info *rpart, const struct mtd_partition *part)
93 + struct mtd_partition *dpart;
94 + struct mtd_part *slave = NULL;
95 + struct mtd_part *spart;
96 + int ret, split_offset = 0;
98 + spart = PART(rpart);
99 + ret = split_squashfs(master, spart->offset, &split_offset);
103 + if (split_offset <= 0)
106 + dpart = kmalloc(sizeof(*part)+sizeof(ROOTFS_SPLIT_NAME)+1, GFP_KERNEL);
107 + if (dpart == NULL) {
108 + printk(KERN_INFO "split_squashfs: no memory for partition \"%s\"\n",
109 + ROOTFS_SPLIT_NAME);
113 + memcpy(dpart, part, sizeof(*part));
114 + dpart->name = (unsigned char *)&dpart[1];
115 + strcpy(dpart->name, ROOTFS_SPLIT_NAME);
117 + dpart->size = rpart->size - (split_offset - spart->offset);
118 + dpart->offset = split_offset;
123 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%llX, len=%llX \n",
124 + ROOTFS_SPLIT_NAME, dpart->offset, dpart->size);
126 + slave = allocate_partition(master, dpart, 0, split_offset);
128 + return PTR_ERR(slave);
129 + mutex_lock(&mtd_partitions_mutex);
130 + list_add(&slave->list, &mtd_partitions);
131 + mutex_unlock(&mtd_partitions_mutex);
133 + add_mtd_device(&slave->mtd);
135 + rpart->split = &slave->mtd;
140 +static int refresh_rootfs_split(struct mtd_info *mtd)
142 + struct mtd_partition tpart;
143 + struct mtd_part *part;
151 + /* check for the new squashfs offset first */
152 + ret = split_squashfs(part->master, part->offset, &offset);
156 + if ((offset > 0) && !mtd->split) {
157 + printk(KERN_INFO "%s: creating new split partition for \"%s\"\n", __func__, mtd->name);
158 + /* if we don't have a rootfs split partition, create a new one */
159 + tpart.name = (char *) mtd->name;
160 + tpart.size = mtd->size;
161 + tpart.offset = part->offset;
163 + return split_rootfs_data(part->master, &part->mtd, &tpart);
164 + } else if ((offset > 0) && mtd->split) {
165 + /* update the offsets of the existing partition */
166 + size = mtd->size + part->offset - offset;
168 + part = PART(mtd->split);
169 + part->offset = offset;
170 + part->mtd.size = size;
171 + printk(KERN_INFO "%s: %s partition \"" ROOTFS_SPLIT_NAME "\", offset: 0x%06x (0x%06x)\n",
172 + __func__, (!strcmp(part->mtd.name, ROOTFS_SPLIT_NAME) ? "updating" : "creating"),
173 + (u32) part->offset, (u32) part->mtd.size);
174 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
175 + strcpy(name, ROOTFS_SPLIT_NAME);
176 + part->mtd.name = name;
177 + } else if ((offset <= 0) && mtd->split) {
178 + printk(KERN_INFO "%s: removing partition \"%s\"\n", __func__, mtd->split->name);
180 + /* mark existing partition as removed */
181 + part = PART(mtd->split);
182 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
183 + strcpy(name, ROOTFS_REMOVED_NAME);
184 + part->mtd.name = name;
186 + part->mtd.size = 0;
191 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
194 * This function, given a master MTD object and a partition table, creates
195 * and registers slave MTD objects which are bound to the master according to
196 @@ -652,6 +803,9 @@ int add_mtd_partitions(struct mtd_info *
197 struct mtd_part *slave;
198 uint64_t cur_offset = 0;
200 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
204 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
206 @@ -666,6 +820,21 @@ int add_mtd_partitions(struct mtd_info *
208 add_mtd_device(&slave->mtd);
210 + if (!strcmp(parts[i].name, "rootfs")) {
211 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
212 + if (ROOT_DEV == 0) {
213 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
214 + "set to be root filesystem\n");
215 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
218 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
219 + ret = split_rootfs_data(master, &slave->mtd, &parts[i]);
225 cur_offset = slave->offset + slave->mtd.size;
228 @@ -673,6 +842,32 @@ int add_mtd_partitions(struct mtd_info *
230 EXPORT_SYMBOL(add_mtd_partitions);
232 +int refresh_mtd_partitions(struct mtd_info *mtd)
236 + if (IS_PART(mtd)) {
237 + struct mtd_part *part;
238 + struct mtd_info *master;
241 + master = part->master;
242 + if (master->refresh_device)
243 + ret = master->refresh_device(master);
246 + if (!ret && mtd->refresh_device)
247 + ret = mtd->refresh_device(mtd);
249 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
250 + if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
251 + refresh_rootfs_split(mtd);
256 +EXPORT_SYMBOL_GPL(refresh_mtd_partitions);
258 static DEFINE_SPINLOCK(part_parser_lock);
259 static LIST_HEAD(part_parsers);
261 --- a/drivers/mtd/mtdchar.c
262 +++ b/drivers/mtd/mtdchar.c
263 @@ -956,6 +956,13 @@ static int mtd_ioctl(struct file *file,
267 +#ifdef CONFIG_MTD_PARTITIONS
270 + ret = refresh_mtd_partitions(mtd);
277 --- a/include/linux/mtd/mtd.h
278 +++ b/include/linux/mtd/mtd.h
279 @@ -125,6 +125,7 @@ struct nand_ecclayout {
280 struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
287 @@ -277,6 +278,9 @@ struct mtd_info {
291 + int (*refresh_device)(struct mtd_info *mtd);
292 + struct mtd_info *split;
294 /* If the driver is something smart, like UBI, it may need to maintain
295 * its own reference counting. The below functions are only for driver.
296 * The driver may register its callbacks. These callbacks are not
297 --- a/include/linux/mtd/partitions.h
298 +++ b/include/linux/mtd/partitions.h
300 * erasesize aligned (e.g. use MTDPART_OFS_NEXTBLK).
303 +struct mtd_partition;
304 struct mtd_partition {
305 char *name; /* identifier string */
306 uint64_t size; /* partition size */
307 uint64_t offset; /* offset within the master MTD space */
308 uint32_t mask_flags; /* master MTD flags to mask out for this partition */
309 struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only) */
310 + int (*refresh_partition)(struct mtd_info *);
313 #define MTDPART_OFS_NXTBLK (-2)
314 @@ -51,6 +53,7 @@ struct mtd_info;
316 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
317 int del_mtd_partitions(struct mtd_info *);
318 +int refresh_mtd_partitions(struct mtd_info *);
321 * Functions dealing with the various ways of partitioning the space
322 --- a/include/mtd/mtd-abi.h
323 +++ b/include/mtd/mtd-abi.h
324 @@ -127,6 +127,7 @@ struct otp_info {
325 #define MEMWRITEOOB64 _IOWR('M', 21, struct mtd_oob_buf64)
326 #define MEMREADOOB64 _IOWR('M', 22, struct mtd_oob_buf64)
327 #define MEMISLOCKED _IOR('M', 23, struct erase_info_user)
328 +#define MTDREFRESH _IO('M', 50)
331 * Obsolete legacy interface. Keep it in order not to break userspace