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>
36 /* Our partition linked list */
37 static LIST_HEAD(mtd_partitions);
38 static DEFINE_MUTEX(mtd_partitions_mutex);
40 /* Our partition node structure */
43 struct mtd_info *master;
45 struct list_head list;
49 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
50 * the pointer to that structure with this macro.
52 #define PART(x) ((struct mtd_part *)(x))
56 * MTD methods which simply translate the effective address and pass through
57 * to the _real_ device.
60 static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
61 size_t *retlen, u_char *buf)
63 struct mtd_part *part = PART(mtd);
64 struct mtd_ecc_stats stats;
67 stats = part->master->ecc_stats;
69 if (from >= mtd->size)
71 else if (from + len > mtd->size)
72 len = mtd->size - from;
73 res = part->master->read(part->master, from + part->offset,
76 if (mtd_is_bitflip(res))
77 mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
78 if (mtd_is_eccerr(res))
79 mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
84 static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
85 size_t *retlen, void **virt, resource_size_t *phys)
87 struct mtd_part *part = PART(mtd);
88 if (from >= mtd->size)
90 else if (from + len > mtd->size)
91 len = mtd->size - from;
92 return part->master->point (part->master, from + part->offset,
93 len, retlen, virt, phys);
96 static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
98 struct mtd_part *part = PART(mtd);
100 part->master->unpoint(part->master, from + part->offset, len);
103 static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
105 unsigned long offset,
108 struct mtd_part *part = PART(mtd);
110 offset += part->offset;
111 return part->master->get_unmapped_area(part->master, len, offset,
115 static int part_read_oob(struct mtd_info *mtd, loff_t from,
116 struct mtd_oob_ops *ops)
118 struct mtd_part *part = PART(mtd);
121 if (from >= mtd->size)
123 if (ops->datbuf && from + ops->len > mtd->size)
127 * If OOB is also requested, make sure that we do not read past the end
133 if (ops->mode == MTD_OPS_AUTO_OOB)
137 pages = mtd_div_by_ws(mtd->size, mtd);
138 pages -= mtd_div_by_ws(from, mtd);
139 if (ops->ooboffs + ops->ooblen > pages * len)
143 res = part->master->read_oob(part->master, from + part->offset, ops);
145 if (mtd_is_bitflip(res))
146 mtd->ecc_stats.corrected++;
147 if (mtd_is_eccerr(res))
148 mtd->ecc_stats.failed++;
153 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
154 size_t len, size_t *retlen, u_char *buf)
156 struct mtd_part *part = PART(mtd);
157 return part->master->read_user_prot_reg(part->master, from,
161 static int part_get_user_prot_info(struct mtd_info *mtd,
162 struct otp_info *buf, size_t len)
164 struct mtd_part *part = PART(mtd);
165 return part->master->get_user_prot_info(part->master, buf, len);
168 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
169 size_t len, size_t *retlen, u_char *buf)
171 struct mtd_part *part = PART(mtd);
172 return part->master->read_fact_prot_reg(part->master, from,
176 static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
179 struct mtd_part *part = PART(mtd);
180 return part->master->get_fact_prot_info(part->master, buf, len);
183 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
184 size_t *retlen, const u_char *buf)
186 struct mtd_part *part = PART(mtd);
187 if (!(mtd->flags & MTD_WRITEABLE))
191 else if (to + len > mtd->size)
192 len = mtd->size - to;
193 return part->master->write(part->master, to + part->offset,
197 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
198 size_t *retlen, const u_char *buf)
200 struct mtd_part *part = PART(mtd);
201 if (!(mtd->flags & MTD_WRITEABLE))
205 else if (to + len > mtd->size)
206 len = mtd->size - to;
207 return part->master->panic_write(part->master, to + part->offset,
211 static int part_write_oob(struct mtd_info *mtd, loff_t to,
212 struct mtd_oob_ops *ops)
214 struct mtd_part *part = PART(mtd);
216 if (!(mtd->flags & MTD_WRITEABLE))
221 if (ops->datbuf && to + ops->len > mtd->size)
223 return part->master->write_oob(part->master, to + part->offset, ops);
226 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
227 size_t len, size_t *retlen, u_char *buf)
229 struct mtd_part *part = PART(mtd);
230 return part->master->write_user_prot_reg(part->master, from,
234 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
237 struct mtd_part *part = PART(mtd);
238 return part->master->lock_user_prot_reg(part->master, from, len);
241 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
242 unsigned long count, loff_t to, size_t *retlen)
244 struct mtd_part *part = PART(mtd);
245 if (!(mtd->flags & MTD_WRITEABLE))
247 return part->master->writev(part->master, vecs, count,
248 to + part->offset, retlen);
251 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
253 struct mtd_part *part = PART(mtd);
255 if (!(mtd->flags & MTD_WRITEABLE))
257 if (instr->addr >= mtd->size)
259 instr->addr += part->offset;
260 ret = part->master->erase(part->master, instr);
262 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
263 instr->fail_addr -= part->offset;
264 instr->addr -= part->offset;
269 void mtd_erase_callback(struct erase_info *instr)
271 if (instr->mtd->erase == part_erase) {
272 struct mtd_part *part = PART(instr->mtd);
274 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
275 instr->fail_addr -= part->offset;
276 instr->addr -= part->offset;
279 instr->callback(instr);
281 EXPORT_SYMBOL_GPL(mtd_erase_callback);
283 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
285 struct mtd_part *part = PART(mtd);
286 if ((len + ofs) > mtd->size)
288 return part->master->lock(part->master, ofs + part->offset, len);
291 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
293 struct mtd_part *part = PART(mtd);
294 if ((len + ofs) > mtd->size)
296 return part->master->unlock(part->master, ofs + part->offset, len);
299 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
301 struct mtd_part *part = PART(mtd);
302 if ((len + ofs) > mtd->size)
304 return part->master->is_locked(part->master, ofs + part->offset, len);
307 static void part_sync(struct mtd_info *mtd)
309 struct mtd_part *part = PART(mtd);
310 part->master->sync(part->master);
313 static int part_suspend(struct mtd_info *mtd)
315 struct mtd_part *part = PART(mtd);
316 return part->master->suspend(part->master);
319 static void part_resume(struct mtd_info *mtd)
321 struct mtd_part *part = PART(mtd);
322 part->master->resume(part->master);
325 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
327 struct mtd_part *part = PART(mtd);
328 if (ofs >= mtd->size)
331 return part->master->block_isbad(part->master, ofs);
334 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
336 struct mtd_part *part = PART(mtd);
339 if (!(mtd->flags & MTD_WRITEABLE))
341 if (ofs >= mtd->size)
344 res = part->master->block_markbad(part->master, ofs);
346 mtd->ecc_stats.badblocks++;
350 static inline void free_partition(struct mtd_part *p)
357 * This function unregisters and destroy all slave MTD objects which are
358 * attached to the given master MTD object.
361 int del_mtd_partitions(struct mtd_info *master)
363 struct mtd_part *slave, *next;
366 mutex_lock(&mtd_partitions_mutex);
367 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
368 if (slave->master == master) {
369 ret = del_mtd_device(&slave->mtd);
374 list_del(&slave->list);
375 free_partition(slave);
377 mutex_unlock(&mtd_partitions_mutex);
382 static struct mtd_part *allocate_partition(struct mtd_info *master,
383 const struct mtd_partition *part, int partno,
386 struct mtd_part *slave;
389 /* allocate the partition structure */
390 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
391 name = kstrdup(part->name, GFP_KERNEL);
392 if (!name || !slave) {
393 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
397 return ERR_PTR(-ENOMEM);
400 /* set up the MTD object for this partition */
401 slave->mtd.type = master->type;
402 slave->mtd.flags = master->flags & ~part->mask_flags;
403 slave->mtd.size = part->size;
404 slave->mtd.writesize = master->writesize;
405 slave->mtd.writebufsize = master->writebufsize;
406 slave->mtd.oobsize = master->oobsize;
407 slave->mtd.oobavail = master->oobavail;
408 slave->mtd.subpage_sft = master->subpage_sft;
410 slave->mtd.name = name;
411 slave->mtd.owner = master->owner;
412 slave->mtd.backing_dev_info = master->backing_dev_info;
414 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
415 * to have the same data be in two different partitions.
417 slave->mtd.dev.parent = master->dev.parent;
419 slave->mtd.read = part_read;
420 slave->mtd.write = part_write;
422 if (master->panic_write)
423 slave->mtd.panic_write = part_panic_write;
425 if (master->point && master->unpoint) {
426 slave->mtd.point = part_point;
427 slave->mtd.unpoint = part_unpoint;
430 if (master->get_unmapped_area)
431 slave->mtd.get_unmapped_area = part_get_unmapped_area;
432 if (master->read_oob)
433 slave->mtd.read_oob = part_read_oob;
434 if (master->write_oob)
435 slave->mtd.write_oob = part_write_oob;
436 if (master->read_user_prot_reg)
437 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
438 if (master->read_fact_prot_reg)
439 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
440 if (master->write_user_prot_reg)
441 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
442 if (master->lock_user_prot_reg)
443 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
444 if (master->get_user_prot_info)
445 slave->mtd.get_user_prot_info = part_get_user_prot_info;
446 if (master->get_fact_prot_info)
447 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
449 slave->mtd.sync = part_sync;
450 if (!partno && !master->dev.class && master->suspend && master->resume) {
451 slave->mtd.suspend = part_suspend;
452 slave->mtd.resume = part_resume;
455 slave->mtd.writev = part_writev;
457 slave->mtd.lock = part_lock;
459 slave->mtd.unlock = part_unlock;
460 if (master->is_locked)
461 slave->mtd.is_locked = part_is_locked;
462 if (master->block_isbad)
463 slave->mtd.block_isbad = part_block_isbad;
464 if (master->block_markbad)
465 slave->mtd.block_markbad = part_block_markbad;
466 slave->mtd.erase = part_erase;
467 slave->master = master;
468 slave->offset = part->offset;
470 if (slave->offset == MTDPART_OFS_APPEND)
471 slave->offset = cur_offset;
472 if (slave->offset == MTDPART_OFS_NXTBLK) {
473 slave->offset = cur_offset;
474 if (mtd_mod_by_eb(cur_offset, master) != 0) {
475 /* Round up to next erasesize */
476 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
477 printk(KERN_NOTICE "Moving partition %d: "
478 "0x%012llx -> 0x%012llx\n", partno,
479 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
482 if (slave->offset == MTDPART_OFS_RETAIN) {
483 slave->offset = cur_offset;
484 if (master->size - slave->offset >= slave->mtd.size) {
485 slave->mtd.size = master->size - slave->offset
488 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
489 part->name, master->size - slave->offset,
491 /* register to preserve ordering */
495 if (slave->mtd.size == MTDPART_SIZ_FULL)
496 slave->mtd.size = master->size - slave->offset;
498 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
499 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
501 /* let's do some sanity checks */
502 if (slave->offset >= master->size) {
503 /* let's register it anyway to preserve ordering */
506 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
510 if (slave->offset + slave->mtd.size > master->size) {
511 slave->mtd.size = master->size - slave->offset;
512 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
513 part->name, master->name, (unsigned long long)slave->mtd.size);
515 if (master->numeraseregions > 1) {
516 /* Deal with variable erase size stuff */
517 int i, max = master->numeraseregions;
518 u64 end = slave->offset + slave->mtd.size;
519 struct mtd_erase_region_info *regions = master->eraseregions;
521 /* Find the first erase regions which is part of this
523 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
525 /* The loop searched for the region _behind_ the first one */
529 /* Pick biggest erasesize */
530 for (; i < max && regions[i].offset < end; i++) {
531 if (slave->mtd.erasesize < regions[i].erasesize) {
532 slave->mtd.erasesize = regions[i].erasesize;
535 BUG_ON(slave->mtd.erasesize == 0);
537 /* Single erase size */
538 slave->mtd.erasesize = master->erasesize;
541 if ((slave->mtd.flags & MTD_WRITEABLE) &&
542 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
543 /* Doesn't start on a boundary of major erase size */
544 /* FIXME: Let it be writable if it is on a boundary of
545 * _minor_ erase size though */
546 slave->mtd.flags &= ~MTD_WRITEABLE;
547 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
550 if ((slave->mtd.flags & MTD_WRITEABLE) &&
551 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
552 slave->mtd.flags &= ~MTD_WRITEABLE;
553 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
557 slave->mtd.ecclayout = master->ecclayout;
558 if (master->block_isbad) {
561 while (offs < slave->mtd.size) {
562 if (master->block_isbad(master,
563 offs + slave->offset))
564 slave->mtd.ecc_stats.badblocks++;
565 offs += slave->mtd.erasesize;
573 int mtd_add_partition(struct mtd_info *master, char *name,
574 long long offset, long long length)
576 struct mtd_partition part;
577 struct mtd_part *p, *new;
581 /* the direct offset is expected */
582 if (offset == MTDPART_OFS_APPEND ||
583 offset == MTDPART_OFS_NXTBLK)
586 if (length == MTDPART_SIZ_FULL)
587 length = master->size - offset;
594 part.offset = offset;
596 part.ecclayout = NULL;
598 new = allocate_partition(master, &part, -1, offset);
603 end = offset + length;
605 mutex_lock(&mtd_partitions_mutex);
606 list_for_each_entry(p, &mtd_partitions, list)
607 if (p->master == master) {
608 if ((start >= p->offset) &&
609 (start < (p->offset + p->mtd.size)))
612 if ((end >= p->offset) &&
613 (end < (p->offset + p->mtd.size)))
617 list_add(&new->list, &mtd_partitions);
618 mutex_unlock(&mtd_partitions_mutex);
620 add_mtd_device(&new->mtd);
624 mutex_unlock(&mtd_partitions_mutex);
628 EXPORT_SYMBOL_GPL(mtd_add_partition);
630 int mtd_del_partition(struct mtd_info *master, int partno)
632 struct mtd_part *slave, *next;
635 mutex_lock(&mtd_partitions_mutex);
636 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
637 if ((slave->master == master) &&
638 (slave->mtd.index == partno)) {
639 ret = del_mtd_device(&slave->mtd);
643 list_del(&slave->list);
644 free_partition(slave);
647 mutex_unlock(&mtd_partitions_mutex);
651 EXPORT_SYMBOL_GPL(mtd_del_partition);
654 * This function, given a master MTD object and a partition table, creates
655 * and registers slave MTD objects which are bound to the master according to
656 * the partition definitions.
658 * We don't register the master, or expect the caller to have done so,
659 * for reasons of data integrity.
662 int add_mtd_partitions(struct mtd_info *master,
663 const struct mtd_partition *parts,
666 struct mtd_part *slave;
667 uint64_t cur_offset = 0;
670 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
672 for (i = 0; i < nbparts; i++) {
673 slave = allocate_partition(master, parts + i, i, cur_offset);
675 return PTR_ERR(slave);
677 mutex_lock(&mtd_partitions_mutex);
678 list_add(&slave->list, &mtd_partitions);
679 mutex_unlock(&mtd_partitions_mutex);
681 add_mtd_device(&slave->mtd);
683 cur_offset = slave->offset + slave->mtd.size;
689 static DEFINE_SPINLOCK(part_parser_lock);
690 static LIST_HEAD(part_parsers);
692 static struct mtd_part_parser *get_partition_parser(const char *name)
694 struct mtd_part_parser *p, *ret = NULL;
696 spin_lock(&part_parser_lock);
698 list_for_each_entry(p, &part_parsers, list)
699 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
704 spin_unlock(&part_parser_lock);
709 #define put_partition_parser(p) do { module_put((p)->owner); } while (0)
711 int register_mtd_parser(struct mtd_part_parser *p)
713 spin_lock(&part_parser_lock);
714 list_add(&p->list, &part_parsers);
715 spin_unlock(&part_parser_lock);
719 EXPORT_SYMBOL_GPL(register_mtd_parser);
721 int deregister_mtd_parser(struct mtd_part_parser *p)
723 spin_lock(&part_parser_lock);
725 spin_unlock(&part_parser_lock);
728 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
731 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
732 * are changing this array!
734 static const char *default_mtd_part_types[] = {
741 * parse_mtd_partitions - parse MTD partitions
742 * @master: the master partition (describes whole MTD device)
743 * @types: names of partition parsers to try or %NULL
744 * @pparts: array of partitions found is returned here
745 * @data: MTD partition parser-specific data
747 * This function tries to find partition on MTD device @master. It uses MTD
748 * partition parsers, specified in @types. However, if @types is %NULL, then
749 * the default list of parsers is used. The default list contains only the
750 * "cmdlinepart" and "ofpart" parsers ATM.
752 * This function may return:
753 * o a negative error code in case of failure
754 * o zero if no partitions were found
755 * o a positive number of found partitions, in which case on exit @pparts will
756 * point to an array containing this number of &struct mtd_info objects.
758 int parse_mtd_partitions(struct mtd_info *master, const char **types,
759 struct mtd_partition **pparts,
760 struct mtd_part_parser_data *data)
762 struct mtd_part_parser *parser;
766 types = default_mtd_part_types;
768 for ( ; ret <= 0 && *types; types++) {
769 parser = get_partition_parser(*types);
770 if (!parser && !request_module("%s", *types))
771 parser = get_partition_parser(*types);
774 ret = (*parser->parse_fn)(master, pparts, data);
776 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
777 ret, parser->name, master->name);
779 put_partition_parser(parser);
784 int mtd_is_partition(struct mtd_info *mtd)
786 struct mtd_part *part;
789 mutex_lock(&mtd_partitions_mutex);
790 list_for_each_entry(part, &mtd_partitions, list)
791 if (&part->mtd == mtd) {
795 mutex_unlock(&mtd_partitions_mutex);
799 EXPORT_SYMBOL_GPL(mtd_is_partition);