1 Index: linux/drivers/mtd/Kconfig
2 ===================================================================
3 --- linux.orig/drivers/mtd/Kconfig
4 +++ linux/drivers/mtd/Kconfig
5 @@ -49,6 +49,11 @@ config MTD_PARTITIONS
6 devices. Partitioning on NFTL 'devices' is a different - that's the
7 'normal' form of partitioning used on a block device.
9 +config MTD_SPLIT_ROOTFS
10 + bool "Automatically split rootfs partition for squashfs"
11 + depends on MTD_PARTITIONS
14 config MTD_REDBOOT_PARTS
15 tristate "RedBoot partition table parsing"
16 depends on MTD_PARTITIONS
17 Index: linux/drivers/mtd/mtdpart.c
18 ===================================================================
19 --- linux.orig/drivers/mtd/mtdpart.c
20 +++ linux/drivers/mtd/mtdpart.c
22 #include <linux/mtd/mtd.h>
23 #include <linux/mtd/partitions.h>
24 #include <linux/mtd/compatmac.h>
25 +#include <linux/squashfs_fs.h>
26 +#include <linux/root_dev.h>
28 /* Our partition linked list */
29 static LIST_HEAD(mtd_partitions);
30 @@ -308,6 +310,266 @@ int del_mtd_partitions(struct mtd_info *
34 +static u_int32_t cur_offset = 0;
35 +static int add_one_partition(struct mtd_info *master, const struct mtd_partition *part,
36 + int i, struct mtd_part **slp)
38 + struct mtd_part *slave;
40 + /* allocate the partition structure */
41 + slave = kzalloc (sizeof(*slave), GFP_KERNEL);
43 + printk ("memory allocation error while creating partitions for \"%s\"\n",
45 + del_mtd_partitions(master);
48 + list_add(&slave->list, &mtd_partitions);
50 + /* set up the MTD object for this partition */
51 + slave->mtd.type = master->type;
52 + slave->mtd.flags = master->flags & ~part->mask_flags;
53 + slave->mtd.size = part->size;
54 + slave->mtd.writesize = master->writesize;
55 + slave->mtd.oobsize = master->oobsize;
56 + slave->mtd.oobavail = master->oobavail;
57 + slave->mtd.subpage_sft = master->subpage_sft;
59 + slave->mtd.name = part->name;
60 + slave->mtd.bank_size = master->bank_size;
61 + slave->mtd.owner = master->owner;
63 + slave->mtd.read = part_read;
64 + slave->mtd.write = part_write;
66 + if(master->point && master->unpoint){
67 + slave->mtd.point = part_point;
68 + slave->mtd.unpoint = part_unpoint;
71 + if (master->read_oob)
72 + slave->mtd.read_oob = part_read_oob;
73 + if (master->write_oob)
74 + slave->mtd.write_oob = part_write_oob;
75 + if(master->read_user_prot_reg)
76 + slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
77 + if(master->read_fact_prot_reg)
78 + slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
79 + if(master->write_user_prot_reg)
80 + slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
81 + if(master->lock_user_prot_reg)
82 + slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
83 + if(master->get_user_prot_info)
84 + slave->mtd.get_user_prot_info = part_get_user_prot_info;
85 + if(master->get_fact_prot_info)
86 + slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
88 + slave->mtd.sync = part_sync;
89 + if (!i && master->suspend && master->resume) {
90 + slave->mtd.suspend = part_suspend;
91 + slave->mtd.resume = part_resume;
94 + slave->mtd.writev = part_writev;
96 + slave->mtd.lock = part_lock;
98 + slave->mtd.unlock = part_unlock;
99 + if (master->block_isbad)
100 + slave->mtd.block_isbad = part_block_isbad;
101 + if (master->block_markbad)
102 + slave->mtd.block_markbad = part_block_markbad;
103 + slave->mtd.erase = part_erase;
104 + slave->master = master;
105 + slave->offset = part->offset;
108 + if (slave->offset == MTDPART_OFS_APPEND)
109 + slave->offset = cur_offset;
110 + if (slave->offset == MTDPART_OFS_NXTBLK) {
111 + slave->offset = cur_offset;
112 + if ((cur_offset % master->erasesize) != 0) {
113 + /* Round up to next erasesize */
114 + slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
115 + printk(KERN_NOTICE "Moving partition %d: "
116 + "0x%08x -> 0x%08x\n", i,
117 + cur_offset, slave->offset);
120 + if (slave->mtd.size == MTDPART_SIZ_FULL)
121 + slave->mtd.size = master->size - slave->offset;
122 + cur_offset = slave->offset + slave->mtd.size;
124 + printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
125 + slave->offset + slave->mtd.size, slave->mtd.name);
127 + /* let's do some sanity checks */
128 + if (slave->offset >= master->size) {
129 + /* let's register it anyway to preserve ordering */
131 + slave->mtd.size = 0;
132 + printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
135 + if (slave->offset + slave->mtd.size > master->size) {
136 + slave->mtd.size = master->size - slave->offset;
137 + printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
138 + part->name, master->name, slave->mtd.size);
140 + if (master->numeraseregions>1) {
141 + /* Deal with variable erase size stuff */
143 + struct mtd_erase_region_info *regions = master->eraseregions;
145 + /* Find the first erase regions which is part of this partition. */
146 + for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
149 + for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
150 + if (slave->mtd.erasesize < regions[i].erasesize) {
151 + slave->mtd.erasesize = regions[i].erasesize;
155 + /* Single erase size */
156 + slave->mtd.erasesize = master->erasesize;
159 + if ((slave->mtd.flags & MTD_WRITEABLE) &&
160 + (slave->offset % slave->mtd.erasesize)) {
161 + /* Doesn't start on a boundary of major erase size */
162 + /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
163 + slave->mtd.flags &= ~MTD_WRITEABLE;
164 + printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
167 + if ((slave->mtd.flags & MTD_WRITEABLE) &&
168 + (slave->mtd.size % slave->mtd.erasesize)) {
169 + slave->mtd.flags &= ~MTD_WRITEABLE;
170 + printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
174 + slave->mtd.ecclayout = master->ecclayout;
175 + if (master->block_isbad) {
178 + while(offs < slave->mtd.size) {
179 + if (master->block_isbad(master,
180 + offs + slave->offset))
181 + slave->mtd.ecc_stats.badblocks++;
182 + offs += slave->mtd.erasesize;
187 + { /* store the object pointer (caller may or may not register it */
188 + *part->mtdp = &slave->mtd;
189 + slave->registered = 0;
193 + /* register our partition */
194 + add_mtd_device(&slave->mtd);
195 + slave->registered = 1;
204 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
205 +#define ROOTFS_SPLIT_NAME "rootfs_data"
206 +static int split_squashfs(struct mtd_info *master, struct mtd_partition *old,
207 + struct mtd_partition **new)
209 + struct mtd_partition *part = NULL;
212 + struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
215 + ret = master->read(master, old->offset, sizeof(*sb), &len, buf);
217 + printk(KERN_ALERT "split_squashfs: error occured while reading "
218 + "from \"%s\"\n", master->name);
222 + if (len != sizeof(*sb)) {
223 + printk(KERN_ALERT "split_squashfs: unable to read superblock "
224 + "from \"%s\"\n", master->name);
229 + if (*((u32 *) buf) != SQUASHFS_MAGIC) {
230 + printk(KERN_ALERT "split_squasfs: no squashfs found in \"%s\"\n",
236 + if (sb->bytes_used <= 0) {
237 + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
243 + part = kmalloc(sizeof(*part)+sizeof(ROOTFS_SPLIT_NAME)+1, GFP_KERNEL);
244 + if (part == NULL) {
245 + printk(KERN_INFO "split_squashfs: no memory for partition \"%s\"\n",
246 + ROOTFS_SPLIT_NAME);
251 + part->name = (unsigned char *)&part[1];
252 + memcpy(part, old, sizeof(*part));
253 + strcpy(part->name, ROOTFS_SPLIT_NAME);
255 + len = (u32) sb->bytes_used;
256 + len += (part->offset & 0x000fffff);
257 + len += (master->erasesize - 1);
258 + len &= ~(master->erasesize - 1);
259 + len -= (part->offset & 0x000fffff);
260 + part->offset += len;
270 +static int split_rootfs_data(struct mtd_info *master, struct mtd_partition *part,
273 + struct mtd_partition *dpart;
276 + ret = split_squashfs(master, part, &dpart);
283 + printk(KERN_INFO "mtd: \"%s\" created, ofs=%lX, len=%lX \n",
284 + ROOTFS_SPLIT_NAME, dpart->offset, dpart->size);
286 + ret = add_one_partition(master, dpart, index, NULL);
292 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
295 * This function, given a master MTD object and a partition table, creates
296 * and registers slave MTD objects which are bound to the master according to
297 @@ -320,169 +582,31 @@ int add_mtd_partitions(struct mtd_info *
300 struct mtd_part *slave;
301 - u_int32_t cur_offset = 0;
303 + struct mtd_partition *part;
306 printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
308 - for (i = 0; i < nbparts; i++) {
310 - /* allocate the partition structure */
311 - slave = kzalloc (sizeof(*slave), GFP_KERNEL);
313 - printk ("memory allocation error while creating partitions for \"%s\"\n",
315 - del_mtd_partitions(master);
318 - list_add(&slave->list, &mtd_partitions);
320 - /* set up the MTD object for this partition */
321 - slave->mtd.type = master->type;
322 - slave->mtd.flags = master->flags & ~parts[i].mask_flags;
323 - slave->mtd.size = parts[i].size;
324 - slave->mtd.writesize = master->writesize;
325 - slave->mtd.oobsize = master->oobsize;
326 - slave->mtd.oobavail = master->oobavail;
327 - slave->mtd.subpage_sft = master->subpage_sft;
329 - slave->mtd.name = parts[i].name;
330 - slave->mtd.bank_size = master->bank_size;
331 - slave->mtd.owner = master->owner;
333 - slave->mtd.read = part_read;
334 - slave->mtd.write = part_write;
336 - if(master->point && master->unpoint){
337 - slave->mtd.point = part_point;
338 - slave->mtd.unpoint = part_unpoint;
341 - if (master->read_oob)
342 - slave->mtd.read_oob = part_read_oob;
343 - if (master->write_oob)
344 - slave->mtd.write_oob = part_write_oob;
345 - if(master->read_user_prot_reg)
346 - slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
347 - if(master->read_fact_prot_reg)
348 - slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
349 - if(master->write_user_prot_reg)
350 - slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
351 - if(master->lock_user_prot_reg)
352 - slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
353 - if(master->get_user_prot_info)
354 - slave->mtd.get_user_prot_info = part_get_user_prot_info;
355 - if(master->get_fact_prot_info)
356 - slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
358 - slave->mtd.sync = part_sync;
359 - if (!i && master->suspend && master->resume) {
360 - slave->mtd.suspend = part_suspend;
361 - slave->mtd.resume = part_resume;
363 - if (master->writev)
364 - slave->mtd.writev = part_writev;
366 - slave->mtd.lock = part_lock;
367 - if (master->unlock)
368 - slave->mtd.unlock = part_unlock;
369 - if (master->block_isbad)
370 - slave->mtd.block_isbad = part_block_isbad;
371 - if (master->block_markbad)
372 - slave->mtd.block_markbad = part_block_markbad;
373 - slave->mtd.erase = part_erase;
374 - slave->master = master;
375 - slave->offset = parts[i].offset;
378 - if (slave->offset == MTDPART_OFS_APPEND)
379 - slave->offset = cur_offset;
380 - if (slave->offset == MTDPART_OFS_NXTBLK) {
381 - slave->offset = cur_offset;
382 - if ((cur_offset % master->erasesize) != 0) {
383 - /* Round up to next erasesize */
384 - slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
385 - printk(KERN_NOTICE "Moving partition %d: "
386 - "0x%08x -> 0x%08x\n", i,
387 - cur_offset, slave->offset);
390 - if (slave->mtd.size == MTDPART_SIZ_FULL)
391 - slave->mtd.size = master->size - slave->offset;
392 - cur_offset = slave->offset + slave->mtd.size;
394 - printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
395 - slave->offset + slave->mtd.size, slave->mtd.name);
397 - /* let's do some sanity checks */
398 - if (slave->offset >= master->size) {
399 - /* let's register it anyway to preserve ordering */
401 - slave->mtd.size = 0;
402 - printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
405 - if (slave->offset + slave->mtd.size > master->size) {
406 - slave->mtd.size = master->size - slave->offset;
407 - printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
408 - parts[i].name, master->name, slave->mtd.size);
410 - if (master->numeraseregions>1) {
411 - /* Deal with variable erase size stuff */
413 - struct mtd_erase_region_info *regions = master->eraseregions;
415 - /* Find the first erase regions which is part of this partition. */
416 - for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
419 - for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
420 - if (slave->mtd.erasesize < regions[i].erasesize) {
421 - slave->mtd.erasesize = regions[i].erasesize;
425 - /* Single erase size */
426 - slave->mtd.erasesize = master->erasesize;
429 - if ((slave->mtd.flags & MTD_WRITEABLE) &&
430 - (slave->offset % slave->mtd.erasesize)) {
431 - /* Doesn't start on a boundary of major erase size */
432 - /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
433 - slave->mtd.flags &= ~MTD_WRITEABLE;
434 - printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
437 - if ((slave->mtd.flags & MTD_WRITEABLE) &&
438 - (slave->mtd.size % slave->mtd.erasesize)) {
439 - slave->mtd.flags &= ~MTD_WRITEABLE;
440 - printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
444 - slave->mtd.ecclayout = master->ecclayout;
445 - if (master->block_isbad) {
448 - while(offs < slave->mtd.size) {
449 - if (master->block_isbad(master,
450 - offs + slave->offset))
451 - slave->mtd.ecc_stats.badblocks++;
452 - offs += slave->mtd.erasesize;
453 + for (i = 0, j = 0; i < nbparts; i++) {
454 + part = (struct mtd_partition *) &parts[i];
455 + ret = add_one_partition(master, part, i, &slave);
460 + if (strcmp(part->name, "rootfs") == 0 && slave->registered) {
461 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
462 + if (ROOT_DEV == 0) {
463 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
464 + "set to be root filesystem\n");
465 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
470 - { /* store the object pointer (caller may or may not register it */
471 - *parts[i].mtdp = &slave->mtd;
472 - slave->registered = 0;
476 - /* register our partition */
477 - add_mtd_device(&slave->mtd);
478 - slave->registered = 1;
480 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
481 + ret = split_rootfs_data(master, part, j);