1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -49,6 +49,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/mtd/mtd.h>
24 #include <linux/mtd/partitions.h>
25 #include <linux/mtd/compatmac.h>
26 +#include <linux/squashfs_fs.h>
27 +#include <linux/root_dev.h>
29 /* Our partition linked list */
30 static LIST_HEAD(mtd_partitions);
31 @@ -308,6 +310,266 @@ int del_mtd_partitions(struct mtd_info *
35 +static u_int32_t cur_offset = 0;
36 +static int add_one_partition(struct mtd_info *master, const struct mtd_partition *part,
37 + int i, struct mtd_part **slp)
39 + struct mtd_part *slave;
41 + /* allocate the partition structure */
42 + slave = kzalloc (sizeof(*slave), GFP_KERNEL);
44 + printk ("memory allocation error while creating partitions for \"%s\"\n",
46 + del_mtd_partitions(master);
49 + list_add(&slave->list, &mtd_partitions);
51 + /* set up the MTD object for this partition */
52 + slave->mtd.type = master->type;
53 + slave->mtd.flags = master->flags & ~part->mask_flags;
54 + slave->mtd.size = part->size;
55 + slave->mtd.writesize = master->writesize;
56 + slave->mtd.oobsize = master->oobsize;
57 + slave->mtd.oobavail = master->oobavail;
58 + slave->mtd.subpage_sft = master->subpage_sft;
60 + slave->mtd.name = part->name;
61 + slave->mtd.bank_size = master->bank_size;
62 + slave->mtd.owner = master->owner;
64 + slave->mtd.read = part_read;
65 + slave->mtd.write = part_write;
67 + if(master->point && master->unpoint){
68 + slave->mtd.point = part_point;
69 + slave->mtd.unpoint = part_unpoint;
72 + if (master->read_oob)
73 + slave->mtd.read_oob = part_read_oob;
74 + if (master->write_oob)
75 + slave->mtd.write_oob = part_write_oob;
76 + if(master->read_user_prot_reg)
77 + slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
78 + if(master->read_fact_prot_reg)
79 + slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
80 + if(master->write_user_prot_reg)
81 + slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
82 + if(master->lock_user_prot_reg)
83 + slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
84 + if(master->get_user_prot_info)
85 + slave->mtd.get_user_prot_info = part_get_user_prot_info;
86 + if(master->get_fact_prot_info)
87 + slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
89 + slave->mtd.sync = part_sync;
90 + if (!i && master->suspend && master->resume) {
91 + slave->mtd.suspend = part_suspend;
92 + slave->mtd.resume = part_resume;
95 + slave->mtd.writev = part_writev;
97 + slave->mtd.lock = part_lock;
99 + slave->mtd.unlock = part_unlock;
100 + if (master->block_isbad)
101 + slave->mtd.block_isbad = part_block_isbad;
102 + if (master->block_markbad)
103 + slave->mtd.block_markbad = part_block_markbad;
104 + slave->mtd.erase = part_erase;
105 + slave->master = master;
106 + slave->offset = part->offset;
109 + if (slave->offset == MTDPART_OFS_APPEND)
110 + slave->offset = cur_offset;
111 + if (slave->offset == MTDPART_OFS_NXTBLK) {
112 + slave->offset = cur_offset;
113 + if ((cur_offset % master->erasesize) != 0) {
114 + /* Round up to next erasesize */
115 + slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
116 + printk(KERN_NOTICE "Moving partition %d: "
117 + "0x%08x -> 0x%08x\n", i,
118 + cur_offset, slave->offset);
121 + if (slave->mtd.size == MTDPART_SIZ_FULL)
122 + slave->mtd.size = master->size - slave->offset;
123 + cur_offset = slave->offset + slave->mtd.size;
125 + printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
126 + slave->offset + slave->mtd.size, slave->mtd.name);
128 + /* let's do some sanity checks */
129 + if (slave->offset >= master->size) {
130 + /* let's register it anyway to preserve ordering */
132 + slave->mtd.size = 0;
133 + printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
136 + if (slave->offset + slave->mtd.size > master->size) {
137 + slave->mtd.size = master->size - slave->offset;
138 + printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
139 + part->name, master->name, slave->mtd.size);
141 + if (master->numeraseregions>1) {
142 + /* Deal with variable erase size stuff */
144 + struct mtd_erase_region_info *regions = master->eraseregions;
146 + /* Find the first erase regions which is part of this partition. */
147 + for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
150 + for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
151 + if (slave->mtd.erasesize < regions[i].erasesize) {
152 + slave->mtd.erasesize = regions[i].erasesize;
156 + /* Single erase size */
157 + slave->mtd.erasesize = master->erasesize;
160 + if ((slave->mtd.flags & MTD_WRITEABLE) &&
161 + (slave->offset % slave->mtd.erasesize)) {
162 + /* Doesn't start on a boundary of major erase size */
163 + /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
164 + slave->mtd.flags &= ~MTD_WRITEABLE;
165 + printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
168 + if ((slave->mtd.flags & MTD_WRITEABLE) &&
169 + (slave->mtd.size % slave->mtd.erasesize)) {
170 + slave->mtd.flags &= ~MTD_WRITEABLE;
171 + printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
175 + slave->mtd.ecclayout = master->ecclayout;
176 + if (master->block_isbad) {
179 + while(offs < slave->mtd.size) {
180 + if (master->block_isbad(master,
181 + offs + slave->offset))
182 + slave->mtd.ecc_stats.badblocks++;
183 + offs += slave->mtd.erasesize;
188 + { /* store the object pointer (caller may or may not register it */
189 + *part->mtdp = &slave->mtd;
190 + slave->registered = 0;
194 + /* register our partition */
195 + add_mtd_device(&slave->mtd);
196 + slave->registered = 1;
205 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
206 +#define ROOTFS_SPLIT_NAME "rootfs_data"
207 +static int split_squashfs(struct mtd_info *master, struct mtd_partition *old,
208 + struct mtd_partition **new)
210 + struct mtd_partition *part = NULL;
213 + struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
216 + ret = master->read(master, old->offset, sizeof(*sb), &len, buf);
218 + printk(KERN_ALERT "split_squashfs: error occured while reading "
219 + "from \"%s\"\n", master->name);
223 + if (len != sizeof(*sb)) {
224 + printk(KERN_ALERT "split_squashfs: unable to read superblock "
225 + "from \"%s\"\n", master->name);
230 + if (*((u32 *) buf) != SQUASHFS_MAGIC) {
231 + printk(KERN_ALERT "split_squasfs: no squashfs found in \"%s\"\n",
237 + if (sb->bytes_used <= 0) {
238 + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
244 + part = kmalloc(sizeof(*part)+sizeof(ROOTFS_SPLIT_NAME)+1, GFP_KERNEL);
245 + if (part == NULL) {
246 + printk(KERN_INFO "split_squashfs: no memory for partition \"%s\"\n",
247 + ROOTFS_SPLIT_NAME);
252 + memcpy(part, old, sizeof(*part));
253 + part->name = (unsigned char *)&part[1];
254 + strcpy(part->name, ROOTFS_SPLIT_NAME);
256 + len = (u32) sb->bytes_used;
257 + len += (part->offset & 0x000fffff);
258 + len += (master->erasesize - 1);
259 + len &= ~(master->erasesize - 1);
260 + len -= (part->offset & 0x000fffff);
261 + part->offset += len;
271 +static int split_rootfs_data(struct mtd_info *master, struct mtd_partition *part,
274 + struct mtd_partition *dpart;
277 + ret = split_squashfs(master, part, &dpart);
284 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%X, len=%X \n",
285 + ROOTFS_SPLIT_NAME, dpart->offset, dpart->size);
287 + ret = add_one_partition(master, dpart, index, NULL);
293 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
296 * This function, given a master MTD object and a partition table, creates
297 * and registers slave MTD objects which are bound to the master according to
298 @@ -320,169 +582,31 @@ int add_mtd_partitions(struct mtd_info *
301 struct mtd_part *slave;
302 - u_int32_t cur_offset = 0;
304 + struct mtd_partition *part;
307 printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
309 - for (i = 0; i < nbparts; i++) {
311 - /* allocate the partition structure */
312 - slave = kzalloc (sizeof(*slave), GFP_KERNEL);
314 - printk ("memory allocation error while creating partitions for \"%s\"\n",
316 - del_mtd_partitions(master);
319 - list_add(&slave->list, &mtd_partitions);
321 - /* set up the MTD object for this partition */
322 - slave->mtd.type = master->type;
323 - slave->mtd.flags = master->flags & ~parts[i].mask_flags;
324 - slave->mtd.size = parts[i].size;
325 - slave->mtd.writesize = master->writesize;
326 - slave->mtd.oobsize = master->oobsize;
327 - slave->mtd.oobavail = master->oobavail;
328 - slave->mtd.subpage_sft = master->subpage_sft;
330 - slave->mtd.name = parts[i].name;
331 - slave->mtd.bank_size = master->bank_size;
332 - slave->mtd.owner = master->owner;
334 - slave->mtd.read = part_read;
335 - slave->mtd.write = part_write;
337 - if(master->point && master->unpoint){
338 - slave->mtd.point = part_point;
339 - slave->mtd.unpoint = part_unpoint;
342 - if (master->read_oob)
343 - slave->mtd.read_oob = part_read_oob;
344 - if (master->write_oob)
345 - slave->mtd.write_oob = part_write_oob;
346 - if(master->read_user_prot_reg)
347 - slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
348 - if(master->read_fact_prot_reg)
349 - slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
350 - if(master->write_user_prot_reg)
351 - slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
352 - if(master->lock_user_prot_reg)
353 - slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
354 - if(master->get_user_prot_info)
355 - slave->mtd.get_user_prot_info = part_get_user_prot_info;
356 - if(master->get_fact_prot_info)
357 - slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
359 - slave->mtd.sync = part_sync;
360 - if (!i && master->suspend && master->resume) {
361 - slave->mtd.suspend = part_suspend;
362 - slave->mtd.resume = part_resume;
364 - if (master->writev)
365 - slave->mtd.writev = part_writev;
367 - slave->mtd.lock = part_lock;
368 - if (master->unlock)
369 - slave->mtd.unlock = part_unlock;
370 - if (master->block_isbad)
371 - slave->mtd.block_isbad = part_block_isbad;
372 - if (master->block_markbad)
373 - slave->mtd.block_markbad = part_block_markbad;
374 - slave->mtd.erase = part_erase;
375 - slave->master = master;
376 - slave->offset = parts[i].offset;
379 - if (slave->offset == MTDPART_OFS_APPEND)
380 - slave->offset = cur_offset;
381 - if (slave->offset == MTDPART_OFS_NXTBLK) {
382 - slave->offset = cur_offset;
383 - if ((cur_offset % master->erasesize) != 0) {
384 - /* Round up to next erasesize */
385 - slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
386 - printk(KERN_NOTICE "Moving partition %d: "
387 - "0x%08x -> 0x%08x\n", i,
388 - cur_offset, slave->offset);
391 - if (slave->mtd.size == MTDPART_SIZ_FULL)
392 - slave->mtd.size = master->size - slave->offset;
393 - cur_offset = slave->offset + slave->mtd.size;
395 - printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
396 - slave->offset + slave->mtd.size, slave->mtd.name);
398 - /* let's do some sanity checks */
399 - if (slave->offset >= master->size) {
400 - /* let's register it anyway to preserve ordering */
402 - slave->mtd.size = 0;
403 - printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
406 - if (slave->offset + slave->mtd.size > master->size) {
407 - slave->mtd.size = master->size - slave->offset;
408 - printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
409 - parts[i].name, master->name, slave->mtd.size);
411 - if (master->numeraseregions>1) {
412 - /* Deal with variable erase size stuff */
414 - struct mtd_erase_region_info *regions = master->eraseregions;
416 - /* Find the first erase regions which is part of this partition. */
417 - for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
420 - for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
421 - if (slave->mtd.erasesize < regions[i].erasesize) {
422 - slave->mtd.erasesize = regions[i].erasesize;
426 - /* Single erase size */
427 - slave->mtd.erasesize = master->erasesize;
430 - if ((slave->mtd.flags & MTD_WRITEABLE) &&
431 - (slave->offset % slave->mtd.erasesize)) {
432 - /* Doesn't start on a boundary of major erase size */
433 - /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
434 - slave->mtd.flags &= ~MTD_WRITEABLE;
435 - printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
438 - if ((slave->mtd.flags & MTD_WRITEABLE) &&
439 - (slave->mtd.size % slave->mtd.erasesize)) {
440 - slave->mtd.flags &= ~MTD_WRITEABLE;
441 - printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
445 - slave->mtd.ecclayout = master->ecclayout;
446 - if (master->block_isbad) {
449 - while(offs < slave->mtd.size) {
450 - if (master->block_isbad(master,
451 - offs + slave->offset))
452 - slave->mtd.ecc_stats.badblocks++;
453 - offs += slave->mtd.erasesize;
454 + for (i = 0, j = 0; i < nbparts; i++) {
455 + part = (struct mtd_partition *) &parts[i];
456 + ret = add_one_partition(master, part, j, &slave);
461 + if (strcmp(part->name, "rootfs") == 0 && slave->registered) {
462 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
463 + if (ROOT_DEV == 0) {
464 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
465 + "set to be root filesystem\n");
466 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
471 - { /* store the object pointer (caller may or may not register it */
472 - *parts[i].mtdp = &slave->mtd;
473 - slave->registered = 0;
477 - /* register our partition */
478 - add_mtd_device(&slave->mtd);
479 - slave->registered = 1;
481 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
482 + ret = split_rootfs_data(master, part, j);