2 * Simple MTD partitioning layer
4 * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
5 * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
6 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/list.h>
29 #include <linux/kmod.h>
30 #include <linux/mtd/mtd.h>
31 #include <linux/mtd/partitions.h>
32 #include <linux/err.h>
34 /* Our partition linked list */
35 static LIST_HEAD(mtd_partitions
);
36 static DEFINE_MUTEX(mtd_partitions_mutex
);
38 /* Our partition node structure */
41 struct mtd_info
*master
;
43 struct list_head list
;
47 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
48 * the pointer to that structure with this macro.
50 #define PART(x) ((struct mtd_part *)(x))
54 * MTD methods which simply translate the effective address and pass through
55 * to the _real_ device.
58 static int part_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
59 size_t *retlen
, u_char
*buf
)
61 struct mtd_part
*part
= PART(mtd
);
62 struct mtd_ecc_stats stats
;
65 stats
= part
->master
->ecc_stats
;
67 if (from
>= mtd
->size
)
69 else if (from
+ len
> mtd
->size
)
70 len
= mtd
->size
- from
;
71 res
= part
->master
->read(part
->master
, from
+ part
->offset
,
75 mtd
->ecc_stats
.corrected
+= part
->master
->ecc_stats
.corrected
- stats
.corrected
;
77 mtd
->ecc_stats
.failed
+= part
->master
->ecc_stats
.failed
- stats
.failed
;
82 static int part_point(struct mtd_info
*mtd
, loff_t from
, size_t len
,
83 size_t *retlen
, void **virt
, resource_size_t
*phys
)
85 struct mtd_part
*part
= PART(mtd
);
86 if (from
>= mtd
->size
)
88 else if (from
+ len
> mtd
->size
)
89 len
= mtd
->size
- from
;
90 return part
->master
->point (part
->master
, from
+ part
->offset
,
91 len
, retlen
, virt
, phys
);
94 static void part_unpoint(struct mtd_info
*mtd
, loff_t from
, size_t len
)
96 struct mtd_part
*part
= PART(mtd
);
98 part
->master
->unpoint(part
->master
, from
+ part
->offset
, len
);
101 static unsigned long part_get_unmapped_area(struct mtd_info
*mtd
,
103 unsigned long offset
,
106 struct mtd_part
*part
= PART(mtd
);
108 offset
+= part
->offset
;
109 return part
->master
->get_unmapped_area(part
->master
, len
, offset
,
113 static int part_read_oob(struct mtd_info
*mtd
, loff_t from
,
114 struct mtd_oob_ops
*ops
)
116 struct mtd_part
*part
= PART(mtd
);
119 if (from
>= mtd
->size
)
121 if (ops
->datbuf
&& from
+ ops
->len
> mtd
->size
)
123 res
= part
->master
->read_oob(part
->master
, from
+ part
->offset
, ops
);
127 mtd
->ecc_stats
.corrected
++;
129 mtd
->ecc_stats
.failed
++;
134 static int part_read_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
135 size_t len
, size_t *retlen
, u_char
*buf
)
137 struct mtd_part
*part
= PART(mtd
);
138 return part
->master
->read_user_prot_reg(part
->master
, from
,
142 static int part_get_user_prot_info(struct mtd_info
*mtd
,
143 struct otp_info
*buf
, size_t len
)
145 struct mtd_part
*part
= PART(mtd
);
146 return part
->master
->get_user_prot_info(part
->master
, buf
, len
);
149 static int part_read_fact_prot_reg(struct mtd_info
*mtd
, loff_t from
,
150 size_t len
, size_t *retlen
, u_char
*buf
)
152 struct mtd_part
*part
= PART(mtd
);
153 return part
->master
->read_fact_prot_reg(part
->master
, from
,
157 static int part_get_fact_prot_info(struct mtd_info
*mtd
, struct otp_info
*buf
,
160 struct mtd_part
*part
= PART(mtd
);
161 return part
->master
->get_fact_prot_info(part
->master
, buf
, len
);
164 static int part_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
165 size_t *retlen
, const u_char
*buf
)
167 struct mtd_part
*part
= PART(mtd
);
168 if (!(mtd
->flags
& MTD_WRITEABLE
))
172 else if (to
+ len
> mtd
->size
)
173 len
= mtd
->size
- to
;
174 return part
->master
->write(part
->master
, to
+ part
->offset
,
178 static int part_panic_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
179 size_t *retlen
, const u_char
*buf
)
181 struct mtd_part
*part
= PART(mtd
);
182 if (!(mtd
->flags
& MTD_WRITEABLE
))
186 else if (to
+ len
> mtd
->size
)
187 len
= mtd
->size
- to
;
188 return part
->master
->panic_write(part
->master
, to
+ part
->offset
,
192 static int part_write_oob(struct mtd_info
*mtd
, loff_t to
,
193 struct mtd_oob_ops
*ops
)
195 struct mtd_part
*part
= PART(mtd
);
197 if (!(mtd
->flags
& MTD_WRITEABLE
))
202 if (ops
->datbuf
&& to
+ ops
->len
> mtd
->size
)
204 return part
->master
->write_oob(part
->master
, to
+ part
->offset
, ops
);
207 static int part_write_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
208 size_t len
, size_t *retlen
, u_char
*buf
)
210 struct mtd_part
*part
= PART(mtd
);
211 return part
->master
->write_user_prot_reg(part
->master
, from
,
215 static int part_lock_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
218 struct mtd_part
*part
= PART(mtd
);
219 return part
->master
->lock_user_prot_reg(part
->master
, from
, len
);
222 static int part_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
223 unsigned long count
, loff_t to
, size_t *retlen
)
225 struct mtd_part
*part
= PART(mtd
);
226 if (!(mtd
->flags
& MTD_WRITEABLE
))
228 return part
->master
->writev(part
->master
, vecs
, count
,
229 to
+ part
->offset
, retlen
);
232 static int part_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
234 struct mtd_part
*part
= PART(mtd
);
236 if (!(mtd
->flags
& MTD_WRITEABLE
))
238 if (instr
->addr
>= mtd
->size
)
240 instr
->addr
+= part
->offset
;
241 ret
= part
->master
->erase(part
->master
, instr
);
243 if (instr
->fail_addr
!= MTD_FAIL_ADDR_UNKNOWN
)
244 instr
->fail_addr
-= part
->offset
;
245 instr
->addr
-= part
->offset
;
250 void mtd_erase_callback(struct erase_info
*instr
)
252 if (instr
->mtd
->erase
== part_erase
) {
253 struct mtd_part
*part
= PART(instr
->mtd
);
255 if (instr
->fail_addr
!= MTD_FAIL_ADDR_UNKNOWN
)
256 instr
->fail_addr
-= part
->offset
;
257 instr
->addr
-= part
->offset
;
260 instr
->callback(instr
);
262 EXPORT_SYMBOL_GPL(mtd_erase_callback
);
264 static int part_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
266 struct mtd_part
*part
= PART(mtd
);
267 if ((len
+ ofs
) > mtd
->size
)
269 return part
->master
->lock(part
->master
, ofs
+ part
->offset
, len
);
272 static int part_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
274 struct mtd_part
*part
= PART(mtd
);
275 if ((len
+ ofs
) > mtd
->size
)
277 return part
->master
->unlock(part
->master
, ofs
+ part
->offset
, len
);
280 static int part_is_locked(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
282 struct mtd_part
*part
= PART(mtd
);
283 if ((len
+ ofs
) > mtd
->size
)
285 return part
->master
->is_locked(part
->master
, ofs
+ part
->offset
, len
);
288 static void part_sync(struct mtd_info
*mtd
)
290 struct mtd_part
*part
= PART(mtd
);
291 part
->master
->sync(part
->master
);
294 static int part_suspend(struct mtd_info
*mtd
)
296 struct mtd_part
*part
= PART(mtd
);
297 return part
->master
->suspend(part
->master
);
300 static void part_resume(struct mtd_info
*mtd
)
302 struct mtd_part
*part
= PART(mtd
);
303 part
->master
->resume(part
->master
);
306 static int part_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
308 struct mtd_part
*part
= PART(mtd
);
309 if (ofs
>= mtd
->size
)
312 return part
->master
->block_isbad(part
->master
, ofs
);
315 static int part_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
317 struct mtd_part
*part
= PART(mtd
);
320 if (!(mtd
->flags
& MTD_WRITEABLE
))
322 if (ofs
>= mtd
->size
)
325 res
= part
->master
->block_markbad(part
->master
, ofs
);
327 mtd
->ecc_stats
.badblocks
++;
331 static inline void free_partition(struct mtd_part
*p
)
338 * This function unregisters and destroy all slave MTD objects which are
339 * attached to the given master MTD object.
342 int del_mtd_partitions(struct mtd_info
*master
)
344 struct mtd_part
*slave
, *next
;
347 mutex_lock(&mtd_partitions_mutex
);
348 list_for_each_entry_safe(slave
, next
, &mtd_partitions
, list
)
349 if (slave
->master
== master
) {
350 ret
= del_mtd_device(&slave
->mtd
);
355 list_del(&slave
->list
);
356 free_partition(slave
);
358 mutex_unlock(&mtd_partitions_mutex
);
362 EXPORT_SYMBOL(del_mtd_partitions
);
364 static struct mtd_part
*allocate_partition(struct mtd_info
*master
,
365 const struct mtd_partition
*part
, int partno
,
368 struct mtd_part
*slave
;
371 /* allocate the partition structure */
372 slave
= kzalloc(sizeof(*slave
), GFP_KERNEL
);
373 name
= kstrdup(part
->name
, GFP_KERNEL
);
374 if (!name
|| !slave
) {
375 printk(KERN_ERR
"memory allocation error while creating partitions for \"%s
\"\n",
379 return ERR_PTR(-ENOMEM);
382 /* set up the MTD object for this partition */
383 slave->mtd.type = master->type;
384 slave->mtd.flags = master->flags & ~part->mask_flags;
385 slave->mtd.size = part->size;
386 slave->mtd.writesize = master->writesize;
387 slave->mtd.oobsize = master->oobsize;
388 slave->mtd.oobavail = master->oobavail;
389 slave->mtd.subpage_sft = master->subpage_sft;
391 slave->mtd.name = name;
392 slave->mtd.owner = master->owner;
393 slave->mtd.backing_dev_info = master->backing_dev_info;
395 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
396 * to have the same data be in two different partitions.
398 slave->mtd.dev.parent = master->dev.parent;
400 slave->mtd.read = part_read;
401 slave->mtd.write = part_write;
403 if (master->panic_write)
404 slave->mtd.panic_write = part_panic_write;
406 if (master->point && master->unpoint) {
407 slave->mtd.point = part_point;
408 slave->mtd.unpoint = part_unpoint;
411 if (master->get_unmapped_area)
412 slave->mtd.get_unmapped_area = part_get_unmapped_area;
413 if (master->read_oob)
414 slave->mtd.read_oob = part_read_oob;
415 if (master->write_oob)
416 slave->mtd.write_oob = part_write_oob;
417 if (master->read_user_prot_reg)
418 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
419 if (master->read_fact_prot_reg)
420 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
421 if (master->write_user_prot_reg)
422 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
423 if (master->lock_user_prot_reg)
424 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
425 if (master->get_user_prot_info)
426 slave->mtd.get_user_prot_info = part_get_user_prot_info;
427 if (master->get_fact_prot_info)
428 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
430 slave->mtd.sync = part_sync;
431 if (!partno && !master->dev.class && master->suspend && master->resume) {
432 slave->mtd.suspend = part_suspend;
433 slave->mtd.resume = part_resume;
436 slave->mtd.writev = part_writev;
438 slave->mtd.lock = part_lock;
440 slave->mtd.unlock = part_unlock;
441 if (master->is_locked)
442 slave->mtd.is_locked = part_is_locked;
443 if (master->block_isbad)
444 slave->mtd.block_isbad = part_block_isbad;
445 if (master->block_markbad)
446 slave->mtd.block_markbad = part_block_markbad;
447 slave->mtd.erase = part_erase;
448 slave->master = master;
449 slave->offset = part->offset;
451 if (slave->offset == MTDPART_OFS_APPEND)
452 slave->offset = cur_offset;
453 if (slave->offset == MTDPART_OFS_NXTBLK) {
454 slave->offset = cur_offset;
455 if (mtd_mod_by_eb(cur_offset, master) != 0) {
456 /* Round up to next erasesize */
457 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
458 printk(KERN_NOTICE "Moving partition
%d
: "
459 "0x
%012llx
-> 0x
%012llx
\n", partno,
460 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
463 if (slave->mtd.size == MTDPART_SIZ_FULL)
464 slave->mtd.size = master->size - slave->offset;
466 printk(KERN_NOTICE "0x
%012llx
-0x
%012llx
: \"%s
\"\n", (unsigned long long)slave->offset,
467 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
469 /* let's do some sanity checks */
470 if (slave->offset >= master->size) {
471 /* let's register it anyway to preserve ordering */
474 printk(KERN_ERR"mtd
: partition
\"%s
\" is out of reach
-- disabled
\n",
478 if (slave->offset + slave->mtd.size > master->size) {
479 slave->mtd.size = master->size - slave->offset;
480 printk(KERN_WARNING"mtd
: partition
\"%s
\" extends beyond the end of device
\"%s
\" -- size truncated to
%#llx\n",
481 part->name, master->name, (unsigned long long)slave->mtd.size);
483 if (master->numeraseregions > 1) {
484 /* Deal with variable erase size stuff */
485 int i, max = master->numeraseregions;
486 u64 end = slave->offset + slave->mtd.size;
487 struct mtd_erase_region_info *regions = master->eraseregions;
489 /* Find the first erase regions which is part of this
491 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
493 /* The loop searched for the region _behind_ the first one */
497 /* Pick biggest erasesize */
498 for (; i < max && regions[i].offset < end; i++) {
499 if (slave->mtd.erasesize < regions[i].erasesize) {
500 slave->mtd.erasesize = regions[i].erasesize;
503 BUG_ON(slave->mtd.erasesize == 0);
505 /* Single erase size */
506 slave->mtd.erasesize = master->erasesize;
509 if ((slave->mtd.flags & MTD_WRITEABLE) &&
510 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
511 /* Doesn't start on a boundary of major erase size */
512 /* FIXME: Let it be writable if it is on a boundary of
513 * _minor_ erase size though */
514 slave->mtd.flags &= ~MTD_WRITEABLE;
515 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
518 if ((slave->mtd.flags & MTD_WRITEABLE) &&
519 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
520 slave->mtd.flags &= ~MTD_WRITEABLE;
521 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
525 slave->mtd.ecclayout = master->ecclayout;
526 if (master->block_isbad) {
529 while (offs < slave->mtd.size) {
530 if (master->block_isbad(master,
531 offs + slave->offset))
532 slave->mtd.ecc_stats.badblocks++;
533 offs += slave->mtd.erasesize;
541 int mtd_add_partition(struct mtd_info *master, char *name,
542 long long offset, long long length)
544 struct mtd_partition part;
545 struct mtd_part *p, *new;
549 /* the direct offset is expected */
550 if (offset == MTDPART_OFS_APPEND ||
551 offset == MTDPART_OFS_NXTBLK)
554 if (length == MTDPART_SIZ_FULL)
555 length = master->size - offset;
562 part.offset = offset;
564 part.ecclayout = NULL;
566 new = allocate_partition(master, &part, -1, offset);
571 end = offset + length;
573 mutex_lock(&mtd_partitions_mutex);
574 list_for_each_entry(p, &mtd_partitions, list)
575 if (p->master == master) {
576 if ((start >= p->offset) &&
577 (start < (p->offset + p->mtd.size)))
580 if ((end >= p->offset) &&
581 (end < (p->offset + p->mtd.size)))
585 list_add(&new->list, &mtd_partitions);
586 mutex_unlock(&mtd_partitions_mutex);
588 add_mtd_device(&new->mtd);
592 mutex_unlock(&mtd_partitions_mutex);
596 EXPORT_SYMBOL_GPL(mtd_add_partition);
598 int mtd_del_partition(struct mtd_info *master, int partno)
600 struct mtd_part *slave, *next;
603 mutex_lock(&mtd_partitions_mutex);
604 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
605 if ((slave->master == master) &&
606 (slave->mtd.index == partno)) {
607 ret = del_mtd_device(&slave->mtd);
611 list_del(&slave->list);
612 free_partition(slave);
615 mutex_unlock(&mtd_partitions_mutex);
619 EXPORT_SYMBOL_GPL(mtd_del_partition);
622 * This function, given a master MTD object and a partition table, creates
623 * and registers slave MTD objects which are bound to the master according to
624 * the partition definitions.
626 * We don't register the master, or expect the caller to have done so,
627 * for reasons of data integrity.
630 int add_mtd_partitions(struct mtd_info *master,
631 const struct mtd_partition *parts,
634 struct mtd_part *slave;
635 uint64_t cur_offset = 0;
638 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
640 for (i = 0; i < nbparts; i++) {
641 slave = allocate_partition(master, parts + i, i, cur_offset);
643 return PTR_ERR(slave);
645 mutex_lock(&mtd_partitions_mutex);
646 list_add(&slave->list, &mtd_partitions);
647 mutex_unlock(&mtd_partitions_mutex);
649 add_mtd_device(&slave->mtd);
651 cur_offset = slave->offset + slave->mtd.size;
656 EXPORT_SYMBOL(add_mtd_partitions);
658 static DEFINE_SPINLOCK(part_parser_lock);
659 static LIST_HEAD(part_parsers);
661 static struct mtd_part_parser *get_partition_parser(const char *name)
663 struct mtd_part_parser *p, *ret = NULL;
665 spin_lock(&part_parser_lock);
667 list_for_each_entry(p, &part_parsers, list)
668 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
673 spin_unlock(&part_parser_lock);
678 int register_mtd_parser(struct mtd_part_parser *p)
680 spin_lock(&part_parser_lock);
681 list_add(&p->list, &part_parsers);
682 spin_unlock(&part_parser_lock);
686 EXPORT_SYMBOL_GPL(register_mtd_parser);
688 int deregister_mtd_parser(struct mtd_part_parser *p)
690 spin_lock(&part_parser_lock);
692 spin_unlock(&part_parser_lock);
695 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
697 int parse_mtd_partitions(struct mtd_info *master, const char **types,
698 struct mtd_partition **pparts, unsigned long origin)
700 struct mtd_part_parser *parser;
703 for ( ; ret <= 0 && *types; types++) {
704 parser = get_partition_parser(*types);
705 if (!parser && !request_module("%s", *types))
706 parser = get_partition_parser(*types);
708 printk(KERN_NOTICE "%s partition parsing not available\n",
712 ret = (*parser->parse_fn)(master, pparts, origin);
714 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
715 ret, parser->name, master->name);
717 put_partition_parser(parser);
721 EXPORT_SYMBOL_GPL(parse_mtd_partitions);
723 int mtd_is_master(struct mtd_info *mtd)
725 struct mtd_part *part;
728 mutex_lock(&mtd_partitions_mutex);
729 list_for_each_entry(part, &mtd_partitions, list)
730 if (&part->mtd == mtd) {
734 mutex_unlock(&mtd_partitions_mutex);
738 EXPORT_SYMBOL_GPL(mtd_is_master);