1 Index: linux-3.14.27/drivers/mtd/Kconfig
2 ===================================================================
3 --- linux-3.14.27.orig/drivers/mtd/Kconfig
4 +++ linux-3.14.27/drivers/mtd/Kconfig
5 @@ -52,6 +52,14 @@ config MTD_TESTS
6 WARNING: some of the tests will ERASE entire MTD device which they
7 test. Do not use these tests unless you really know what you do.
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_REDBOOT_PARTS
18 tristate "RedBoot partition table parsing"
20 Index: linux-3.14.27/drivers/mtd/mtdpart.c
21 ===================================================================
22 --- linux-3.14.27.orig/drivers/mtd/mtdpart.c
23 +++ linux-3.14.27/drivers/mtd/mtdpart.c
25 #include <linux/kmod.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/partitions.h>
28 +#include <linux/root_dev.h>
29 +#include <linux/magic.h>
30 #include <linux/err.h>
33 @@ -52,7 +54,7 @@ struct mtd_part {
34 * the pointer to that structure with this macro.
36 #define PART(x) ((struct mtd_part *)(x))
38 +#define IS_PART(mtd) (mtd->_read == part_read)
41 * MTD methods which simply translate the effective address and pass through
42 @@ -695,6 +697,144 @@ int mtd_del_partition(struct mtd_info *m
44 EXPORT_SYMBOL_GPL(mtd_del_partition);
46 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
47 +#define ROOTFS_SPLIT_NAME "rootfs_data"
48 +#define ROOTFS_REMOVED_NAME "<removed>"
50 +struct squashfs_super_block {
57 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
59 + struct squashfs_super_block sb;
62 + ret = mtd_read(master, offset, sizeof(sb), &len, (void *) &sb);
63 + if (ret || (len != sizeof(sb))) {
64 + printk(KERN_ALERT "split_squashfs: error occured while reading "
65 + "from \"%s\"\n", master->name);
69 + if (SQUASHFS_MAGIC != le32_to_cpu(sb.s_magic) ) {
70 + printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
76 + if (le64_to_cpu((sb.bytes_used)) <= 0) {
77 + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
83 + len = (u32) le64_to_cpu(sb.bytes_used);
84 + len += (offset & 0x000fffff);
85 + len += (master->erasesize - 1);
86 + len &= ~(master->erasesize - 1);
87 + len -= (offset & 0x000fffff);
88 + *split_offset = offset + len;
93 +static int split_rootfs_data(struct mtd_info *master, struct mtd_info *rpart, const struct mtd_partition *part)
95 + struct mtd_partition dpart;
96 + struct mtd_part *slave = NULL;
97 + struct mtd_part *spart;
98 + int ret, split_offset = 0;
100 + spart = PART(rpart);
101 + ret = split_squashfs(master, spart->offset, &split_offset);
105 + if (split_offset <= 0)
108 + memcpy(&dpart, part, sizeof(dpart));
109 + dpart.name = ROOTFS_SPLIT_NAME;
111 + dpart.size = rpart->size - (split_offset - spart->offset);
112 + dpart.offset = split_offset;
114 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%llX, len=%llX \n",
115 + ROOTFS_SPLIT_NAME, dpart.offset, dpart.size);
117 + slave = allocate_partition(master, &dpart, 0, split_offset);
119 + return PTR_ERR(slave);
120 + mutex_lock(&mtd_partitions_mutex);
121 + list_add(&slave->list, &mtd_partitions);
122 + mutex_unlock(&mtd_partitions_mutex);
124 + add_mtd_device(&slave->mtd);
126 + rpart->split = &slave->mtd;
131 +static int refresh_rootfs_split(struct mtd_info *mtd)
133 + struct mtd_partition tpart;
134 + struct mtd_part *part;
142 + /* check for the new squashfs offset first */
143 + ret = split_squashfs(part->master, part->offset, &offset);
147 + if ((offset > 0) && !mtd->split) {
148 + printk(KERN_INFO "%s: creating new split partition for \"%s\"\n", __func__, mtd->name);
149 + /* if we don't have a rootfs split partition, create a new one */
150 + tpart.name = (char *) mtd->name;
151 + tpart.size = mtd->size;
152 + tpart.offset = part->offset;
154 + return split_rootfs_data(part->master, &part->mtd, &tpart);
155 + } else if ((offset > 0) && mtd->split) {
156 + /* update the offsets of the existing partition */
157 + size = mtd->size + part->offset - offset;
159 + part = PART(mtd->split);
160 + part->offset = offset;
161 + part->mtd.size = size;
162 + printk(KERN_INFO "%s: %s partition \"" ROOTFS_SPLIT_NAME "\", offset: 0x%06x (0x%06x)\n",
163 + __func__, (!strcmp(part->mtd.name, ROOTFS_SPLIT_NAME) ? "updating" : "creating"),
164 + (u32) part->offset, (u32) part->mtd.size);
165 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
166 + strcpy(name, ROOTFS_SPLIT_NAME);
167 + part->mtd.name = name;
168 + } else if ((offset <= 0) && mtd->split) {
169 + printk(KERN_INFO "%s: removing partition \"%s\"\n", __func__, mtd->split->name);
171 + /* mark existing partition as removed */
172 + part = PART(mtd->split);
173 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
174 + strcpy(name, ROOTFS_REMOVED_NAME);
175 + part->mtd.name = name;
177 + part->mtd.size = 0;
182 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
185 * This function, given a master MTD object and a partition table, creates
186 * and registers slave MTD objects which are bound to the master according to
187 @@ -711,6 +851,9 @@ int add_mtd_partitions(struct mtd_info *
188 struct mtd_part *slave;
189 uint64_t cur_offset = 0;
191 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
195 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
197 @@ -725,12 +868,53 @@ int add_mtd_partitions(struct mtd_info *
199 add_mtd_device(&slave->mtd);
201 + if (!strcmp(parts[i].name, "rootfs")) {
202 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
203 + if (ROOT_DEV == 0) {
204 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
205 + "set to be root filesystem\n");
206 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
209 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
210 + ret = split_rootfs_data(master, &slave->mtd, &parts[i]);
216 cur_offset = slave->offset + slave->mtd.size;
222 +int mtd_device_refresh(struct mtd_info *mtd)
226 + if (IS_PART(mtd)) {
227 + struct mtd_part *part;
228 + struct mtd_info *master;
231 + master = part->master;
232 + if (master->refresh_device)
233 + ret = master->refresh_device(master);
236 + if (!ret && mtd->refresh_device)
237 + ret = mtd->refresh_device(mtd);
239 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
240 + if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
241 + refresh_rootfs_split(mtd);
246 +EXPORT_SYMBOL_GPL(mtd_device_refresh);
248 static DEFINE_SPINLOCK(part_parser_lock);
249 static LIST_HEAD(part_parsers);
251 Index: linux-3.14.27/drivers/mtd/mtdchar.c
252 ===================================================================
253 --- linux-3.14.27.orig/drivers/mtd/mtdchar.c
254 +++ linux-3.14.27/drivers/mtd/mtdchar.c
255 @@ -993,6 +993,12 @@ static int mtdchar_ioctl(struct file *fi
261 + ret = mtd_device_refresh(mtd);
268 Index: linux-3.14.27/include/linux/mtd/mtd.h
269 ===================================================================
270 --- linux-3.14.27.orig/include/linux/mtd/mtd.h
271 +++ linux-3.14.27/include/linux/mtd/mtd.h
272 @@ -115,6 +115,7 @@ struct nand_ecclayout {
274 struct module; /* only needed for owner field in mtd_info */
280 @@ -230,6 +231,9 @@ struct mtd_info {
281 int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
282 int (*_suspend) (struct mtd_info *mtd);
283 void (*_resume) (struct mtd_info *mtd);
284 + int (*refresh_device)(struct mtd_info *mtd);
285 + struct mtd_info *split;
288 * If the driver is something smart, like UBI, it may need to maintain
289 * its own reference counting. The below functions are only for driver.
290 @@ -395,6 +399,7 @@ extern int mtd_device_parse_register(str
292 #define mtd_device_register(master, parts, nr_parts) \
293 mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
294 +extern int mtd_device_refresh(struct mtd_info *master);
295 extern int mtd_device_unregister(struct mtd_info *master);
296 extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
297 extern int __get_mtd_device(struct mtd_info *mtd);
298 Index: linux-3.14.27/include/linux/mtd/partitions.h
299 ===================================================================
300 --- linux-3.14.27.orig/include/linux/mtd/partitions.h
301 +++ linux-3.14.27/include/linux/mtd/partitions.h
306 +struct mtd_partition;
307 struct mtd_partition {
308 const char *name; /* identifier string */
309 uint64_t size; /* partition size */
310 uint64_t offset; /* offset within the master MTD space */
311 uint32_t mask_flags; /* master MTD flags to mask out for this partition */
312 struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only) */
313 + int (*refresh_partition)(struct mtd_info *);
316 #define MTDPART_OFS_RETAIN (-3)
317 Index: linux-3.14.27/include/uapi/mtd/mtd-abi.h
318 ===================================================================
319 --- linux-3.14.27.orig/include/uapi/mtd/mtd-abi.h
320 +++ linux-3.14.27/include/uapi/mtd/mtd-abi.h
321 @@ -202,6 +202,7 @@ struct otp_info {
322 * without OOB, e.g., NOR flash.
324 #define MEMWRITE _IOWR('M', 24, struct mtd_write_req)
325 +#define MTDREFRESH _IO('M', 50)
328 * Obsolete legacy interface. Keep it in order not to break userspace