1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -53,6 +53,16 @@ config MTD_PARTITIONS
4 devices. Partitioning on NFTL 'devices' is a different - that's the
5 'normal' form of partitioning used on a block device.
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"
19 depends on MTD_PARTITIONS
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 @@ -618,6 +620,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 + strcpy((char *)&dpart[1], ROOTFS_SPLIT_NAME);
115 + dpart->name = (unsigned char *)&dpart[1];
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 @@ -633,7 +784,7 @@ int add_mtd_partitions(struct mtd_info *
198 struct mtd_part *slave;
199 uint64_t cur_offset = 0;
203 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
205 @@ -648,6 +799,21 @@ int add_mtd_partitions(struct mtd_info *
207 add_mtd_device(&slave->mtd);
209 + if (!strcmp(parts[i].name, "rootfs")) {
210 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
211 + if (ROOT_DEV == 0) {
212 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
213 + "set to be root filesystem\n");
214 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
217 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
218 + ret = split_rootfs_data(master, &slave->mtd, &parts[i]);
224 cur_offset = slave->offset + slave->mtd.size;
227 @@ -655,6 +821,32 @@ int add_mtd_partitions(struct mtd_info *
229 EXPORT_SYMBOL(add_mtd_partitions);
231 +int refresh_mtd_partitions(struct mtd_info *mtd)
235 + if (IS_PART(mtd)) {
236 + struct mtd_part *part;
237 + struct mtd_info *master;
240 + master = part->master;
241 + if (master->refresh_device)
242 + ret = master->refresh_device(master);
245 + if (!ret && mtd->refresh_device)
246 + ret = mtd->refresh_device(mtd);
248 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
249 + if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
250 + refresh_rootfs_split(mtd);
255 +EXPORT_SYMBOL_GPL(refresh_mtd_partitions);
257 static DEFINE_SPINLOCK(part_parser_lock);
258 static LIST_HEAD(part_parsers);
260 --- a/drivers/mtd/mtdchar.c
261 +++ b/drivers/mtd/mtdchar.c
262 @@ -841,6 +841,13 @@ static int mtd_ioctl(struct file *file,
266 +#ifdef CONFIG_MTD_PARTITIONS
269 + ret = refresh_mtd_partitions(mtd);
274 case OTPGETREGIONCOUNT:
275 case OTPGETREGIONINFO:
276 --- a/include/linux/mtd/mtd.h
277 +++ b/include/linux/mtd/mtd.h
278 @@ -125,6 +125,7 @@ struct nand_ecclayout {
279 struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
286 @@ -266,6 +267,9 @@ struct mtd_info {
290 + int (*refresh_device)(struct mtd_info *mtd);
291 + struct mtd_info *split;
293 /* If the driver is something smart, like UBI, it may need to maintain
294 * its own reference counting. The below functions are only for driver.
295 * The driver may register its callbacks. These callbacks are not
296 --- a/include/linux/mtd/partitions.h
297 +++ b/include/linux/mtd/partitions.h
299 * erasesize aligned (e.g. use MTDPART_OFS_NEXTBLK).
302 +struct mtd_partition;
303 struct mtd_partition {
304 - char *name; /* identifier string */
305 + const 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', 23)
331 * Obsolete legacy interface. Keep it in order not to break userspace