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>
37 /* Our partition linked list */
38 static LIST_HEAD(mtd_partitions
);
39 static DEFINE_MUTEX(mtd_partitions_mutex
);
42 * struct mtd_part - our partition node structure
44 * @mtd: struct holding partition details
45 * @parent: parent mtd - flash device or another partition
46 * @offset: partition offset relative to the *flash device*
50 struct mtd_info
*parent
;
52 struct list_head list
;
56 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
57 * the pointer to that structure.
59 static inline struct mtd_part
*mtd_to_part(const struct mtd_info
*mtd
)
61 return container_of(mtd
, struct mtd_part
, mtd
);
66 * MTD methods which simply translate the effective address and pass through
67 * to the _real_ device.
70 static int part_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
71 size_t *retlen
, u_char
*buf
)
73 struct mtd_part
*part
= mtd_to_part(mtd
);
74 struct mtd_ecc_stats stats
;
77 stats
= part
->parent
->ecc_stats
;
78 res
= part
->parent
->_read(part
->parent
, from
+ part
->offset
, len
,
80 if (unlikely(mtd_is_eccerr(res
)))
81 mtd
->ecc_stats
.failed
+=
82 part
->parent
->ecc_stats
.failed
- stats
.failed
;
84 mtd
->ecc_stats
.corrected
+=
85 part
->parent
->ecc_stats
.corrected
- stats
.corrected
;
89 static int part_point(struct mtd_info
*mtd
, loff_t from
, size_t len
,
90 size_t *retlen
, void **virt
, resource_size_t
*phys
)
92 struct mtd_part
*part
= mtd_to_part(mtd
);
94 return part
->parent
->_point(part
->parent
, from
+ part
->offset
, len
,
98 static int part_unpoint(struct mtd_info
*mtd
, loff_t from
, size_t len
)
100 struct mtd_part
*part
= mtd_to_part(mtd
);
102 return part
->parent
->_unpoint(part
->parent
, from
+ part
->offset
, len
);
105 static int part_read_oob(struct mtd_info
*mtd
, loff_t from
,
106 struct mtd_oob_ops
*ops
)
108 struct mtd_part
*part
= mtd_to_part(mtd
);
109 struct mtd_ecc_stats stats
;
112 stats
= part
->parent
->ecc_stats
;
113 res
= part
->parent
->_read_oob(part
->parent
, from
+ part
->offset
, ops
);
114 if (unlikely(mtd_is_eccerr(res
)))
115 mtd
->ecc_stats
.failed
+=
116 part
->parent
->ecc_stats
.failed
- stats
.failed
;
118 mtd
->ecc_stats
.corrected
+=
119 part
->parent
->ecc_stats
.corrected
- stats
.corrected
;
123 static int part_read_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
124 size_t len
, size_t *retlen
, u_char
*buf
)
126 struct mtd_part
*part
= mtd_to_part(mtd
);
127 return part
->parent
->_read_user_prot_reg(part
->parent
, from
, len
,
131 static int part_get_user_prot_info(struct mtd_info
*mtd
, size_t len
,
132 size_t *retlen
, struct otp_info
*buf
)
134 struct mtd_part
*part
= mtd_to_part(mtd
);
135 return part
->parent
->_get_user_prot_info(part
->parent
, len
, retlen
,
139 static int part_read_fact_prot_reg(struct mtd_info
*mtd
, loff_t from
,
140 size_t len
, size_t *retlen
, u_char
*buf
)
142 struct mtd_part
*part
= mtd_to_part(mtd
);
143 return part
->parent
->_read_fact_prot_reg(part
->parent
, from
, len
,
147 static int part_get_fact_prot_info(struct mtd_info
*mtd
, size_t len
,
148 size_t *retlen
, struct otp_info
*buf
)
150 struct mtd_part
*part
= mtd_to_part(mtd
);
151 return part
->parent
->_get_fact_prot_info(part
->parent
, len
, retlen
,
155 static int part_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
156 size_t *retlen
, const u_char
*buf
)
158 struct mtd_part
*part
= mtd_to_part(mtd
);
159 return part
->parent
->_write(part
->parent
, to
+ part
->offset
, len
,
163 static int part_panic_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
164 size_t *retlen
, const u_char
*buf
)
166 struct mtd_part
*part
= mtd_to_part(mtd
);
167 return part
->parent
->_panic_write(part
->parent
, to
+ part
->offset
, len
,
171 static int part_write_oob(struct mtd_info
*mtd
, loff_t to
,
172 struct mtd_oob_ops
*ops
)
174 struct mtd_part
*part
= mtd_to_part(mtd
);
176 return part
->parent
->_write_oob(part
->parent
, to
+ part
->offset
, ops
);
179 static int part_write_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
180 size_t len
, size_t *retlen
, u_char
*buf
)
182 struct mtd_part
*part
= mtd_to_part(mtd
);
183 return part
->parent
->_write_user_prot_reg(part
->parent
, from
, len
,
187 static int part_lock_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
190 struct mtd_part
*part
= mtd_to_part(mtd
);
191 return part
->parent
->_lock_user_prot_reg(part
->parent
, from
, len
);
194 static int part_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
195 unsigned long count
, loff_t to
, size_t *retlen
)
197 struct mtd_part
*part
= mtd_to_part(mtd
);
198 return part
->parent
->_writev(part
->parent
, vecs
, count
,
199 to
+ part
->offset
, retlen
);
202 static int part_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
204 struct mtd_part
*part
= mtd_to_part(mtd
);
207 instr
->addr
+= part
->offset
;
208 ret
= part
->parent
->_erase(part
->parent
, instr
);
209 if (instr
->fail_addr
!= MTD_FAIL_ADDR_UNKNOWN
)
210 instr
->fail_addr
-= part
->offset
;
211 instr
->addr
-= part
->offset
;
216 static int part_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
218 struct mtd_part
*part
= mtd_to_part(mtd
);
219 return part
->parent
->_lock(part
->parent
, ofs
+ part
->offset
, len
);
222 static int part_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
224 struct mtd_part
*part
= mtd_to_part(mtd
);
225 return part
->parent
->_unlock(part
->parent
, ofs
+ part
->offset
, len
);
228 static int part_is_locked(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
230 struct mtd_part
*part
= mtd_to_part(mtd
);
231 return part
->parent
->_is_locked(part
->parent
, ofs
+ part
->offset
, len
);
234 static void part_sync(struct mtd_info
*mtd
)
236 struct mtd_part
*part
= mtd_to_part(mtd
);
237 part
->parent
->_sync(part
->parent
);
240 static int part_suspend(struct mtd_info
*mtd
)
242 struct mtd_part
*part
= mtd_to_part(mtd
);
243 return part
->parent
->_suspend(part
->parent
);
246 static void part_resume(struct mtd_info
*mtd
)
248 struct mtd_part
*part
= mtd_to_part(mtd
);
249 part
->parent
->_resume(part
->parent
);
252 static int part_block_isreserved(struct mtd_info
*mtd
, loff_t ofs
)
254 struct mtd_part
*part
= mtd_to_part(mtd
);
256 return part
->parent
->_block_isreserved(part
->parent
, ofs
);
259 static int part_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
261 struct mtd_part
*part
= mtd_to_part(mtd
);
263 return part
->parent
->_block_isbad(part
->parent
, ofs
);
266 static int part_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
268 struct mtd_part
*part
= mtd_to_part(mtd
);
272 res
= part
->parent
->_block_markbad(part
->parent
, ofs
);
274 mtd
->ecc_stats
.badblocks
++;
278 static int part_get_device(struct mtd_info
*mtd
)
280 struct mtd_part
*part
= mtd_to_part(mtd
);
281 return part
->parent
->_get_device(part
->parent
);
284 static void part_put_device(struct mtd_info
*mtd
)
286 struct mtd_part
*part
= mtd_to_part(mtd
);
287 part
->parent
->_put_device(part
->parent
);
290 static int part_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
291 struct mtd_oob_region
*oobregion
)
293 struct mtd_part
*part
= mtd_to_part(mtd
);
295 return mtd_ooblayout_ecc(part
->parent
, section
, oobregion
);
298 static int part_ooblayout_free(struct mtd_info
*mtd
, int section
,
299 struct mtd_oob_region
*oobregion
)
301 struct mtd_part
*part
= mtd_to_part(mtd
);
303 return mtd_ooblayout_free(part
->parent
, section
, oobregion
);
306 static const struct mtd_ooblayout_ops part_ooblayout_ops
= {
307 .ecc
= part_ooblayout_ecc
,
308 .free
= part_ooblayout_free
,
311 static int part_max_bad_blocks(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
313 struct mtd_part
*part
= mtd_to_part(mtd
);
315 return part
->parent
->_max_bad_blocks(part
->parent
,
316 ofs
+ part
->offset
, len
);
319 static inline void free_partition(struct mtd_part
*p
)
326 * mtd_parse_part - parse MTD partition looking for subpartitions
328 * @slave: part that is supposed to be a container and should be parsed
329 * @types: NULL-terminated array with names of partition parsers to try
331 * Some partitions are kind of containers with extra subpartitions (volumes).
332 * There can be various formats of such containers. This function tries to use
333 * specified parsers to analyze given partition and registers found
334 * subpartitions on success.
336 static int mtd_parse_part(struct mtd_part
*slave
, const char *const *types
)
338 return parse_mtd_partitions(&slave
->mtd
, types
, NULL
);
341 static struct mtd_part
*allocate_partition(struct mtd_info
*parent
,
342 const struct mtd_partition
*part
, int partno
,
345 int wr_alignment
= (parent
->flags
& MTD_NO_ERASE
) ? parent
->writesize
:
347 struct mtd_part
*slave
;
352 /* allocate the partition structure */
353 slave
= kzalloc(sizeof(*slave
), GFP_KERNEL
);
354 name
= kstrdup(part
->name
, GFP_KERNEL
);
355 if (!name
|| !slave
) {
356 printk(KERN_ERR
"memory allocation error while creating partitions for \"%s
\"\n",
360 return ERR_PTR(-ENOMEM);
363 /* set up the MTD object for this partition */
364 slave->mtd.type = parent->type;
365 slave->mtd.flags = parent->flags & ~part->mask_flags;
366 slave->mtd.size = part->size;
367 slave->mtd.writesize = parent->writesize;
368 slave->mtd.writebufsize = parent->writebufsize;
369 slave->mtd.oobsize = parent->oobsize;
370 slave->mtd.oobavail = parent->oobavail;
371 slave->mtd.subpage_sft = parent->subpage_sft;
372 slave->mtd.pairing = parent->pairing;
374 slave->mtd.name = name;
375 slave->mtd.owner = parent->owner;
377 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
378 * concern for showing the same data in multiple partitions.
379 * However, it is very useful to have the master node present,
380 * so the MTD_PARTITIONED_MASTER option allows that. The master
381 * will have device nodes etc only if this is set, so make the
382 * parent conditional on that option. Note, this is a way to
383 * distinguish between the master and the partition in sysfs.
385 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
388 slave->mtd.dev.of_node = part->of_node;
391 slave->mtd._read = part_read;
393 slave->mtd._write = part_write;
395 if (parent->_panic_write)
396 slave->mtd._panic_write = part_panic_write;
398 if (parent->_point && parent->_unpoint) {
399 slave->mtd._point = part_point;
400 slave->mtd._unpoint = part_unpoint;
403 if (parent->_read_oob)
404 slave->mtd._read_oob = part_read_oob;
405 if (parent->_write_oob)
406 slave->mtd._write_oob = part_write_oob;
407 if (parent->_read_user_prot_reg)
408 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
409 if (parent->_read_fact_prot_reg)
410 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
411 if (parent->_write_user_prot_reg)
412 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
413 if (parent->_lock_user_prot_reg)
414 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
415 if (parent->_get_user_prot_info)
416 slave->mtd._get_user_prot_info = part_get_user_prot_info;
417 if (parent->_get_fact_prot_info)
418 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
420 slave->mtd._sync = part_sync;
421 if (!partno && !parent->dev.class && parent->_suspend &&
423 slave->mtd._suspend = part_suspend;
424 slave->mtd._resume = part_resume;
427 slave->mtd._writev = part_writev;
429 slave->mtd._lock = part_lock;
431 slave->mtd._unlock = part_unlock;
432 if (parent->_is_locked)
433 slave->mtd._is_locked = part_is_locked;
434 if (parent->_block_isreserved)
435 slave->mtd._block_isreserved = part_block_isreserved;
436 if (parent->_block_isbad)
437 slave->mtd._block_isbad = part_block_isbad;
438 if (parent->_block_markbad)
439 slave->mtd._block_markbad = part_block_markbad;
440 if (parent->_max_bad_blocks)
441 slave->mtd._max_bad_blocks = part_max_bad_blocks;
443 if (parent->_get_device)
444 slave->mtd._get_device = part_get_device;
445 if (parent->_put_device)
446 slave->mtd._put_device = part_put_device;
448 slave->mtd._erase = part_erase;
449 slave->parent = parent;
450 slave->offset = part->offset;
452 if (slave->offset == MTDPART_OFS_APPEND)
453 slave->offset = cur_offset;
454 if (slave->offset == MTDPART_OFS_NXTBLK) {
456 slave->offset = cur_offset;
457 remainder = do_div(tmp, wr_alignment);
459 slave->offset += wr_alignment - remainder;
460 printk(KERN_NOTICE "Moving partition
%d
: "
461 "0x
%012llx
-> 0x
%012llx
\n", partno,
462 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
465 if (slave->offset == MTDPART_OFS_RETAIN) {
466 slave->offset = cur_offset;
467 if (parent->size - slave->offset >= slave->mtd.size) {
468 slave->mtd.size = parent->size - slave->offset
471 printk(KERN_ERR "mtd partition
\"%s
\" doesn
't have enough space: %#llx < %#llx, disabled\n",
472 part->name, parent->size - slave->offset,
474 /* register to preserve ordering */
478 if (slave->mtd.size == MTDPART_SIZ_FULL)
479 slave->mtd.size = parent->size - slave->offset;
481 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
482 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
484 /* let's
do some sanity checks */
485 if (slave
->offset
>= parent
->size
) {
486 /* let's register it anyway to preserve ordering */
489 printk(KERN_ERR
"mtd: partition \"%s
\" is out of reach
-- disabled
\n",
493 if (slave->offset + slave->mtd.size > parent->size) {
494 slave->mtd.size = parent->size - slave->offset;
495 printk(KERN_WARNING"mtd
: partition
\"%s
\" extends beyond the end of device
\"%s
\" -- size truncated to
%#llx\n",
496 part->name, parent->name, (unsigned long long)slave->mtd.size);
498 if (parent->numeraseregions > 1) {
499 /* Deal with variable erase size stuff */
500 int i, max = parent->numeraseregions;
501 u64 end = slave->offset + slave->mtd.size;
502 struct mtd_erase_region_info *regions = parent->eraseregions;
504 /* Find the first erase regions which is part of this
506 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
508 /* The loop searched for the region _behind_ the first one */
512 /* Pick biggest erasesize */
513 for (; i < max && regions[i].offset < end; i++) {
514 if (slave->mtd.erasesize < regions[i].erasesize) {
515 slave->mtd.erasesize = regions[i].erasesize;
518 BUG_ON(slave->mtd.erasesize == 0);
520 /* Single erase size */
521 slave->mtd.erasesize = parent->erasesize;
525 * Slave erasesize might differ from the master one if the master
526 * exposes several regions with different erasesize. Adjust
527 * wr_alignment accordingly.
529 if (!(slave->mtd.flags & MTD_NO_ERASE))
530 wr_alignment = slave->mtd.erasesize;
533 remainder = do_div(tmp, wr_alignment);
534 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
535 /* Doesn't start on a boundary of major erase size */
536 /* FIXME: Let it be writable if it is on a boundary of
537 * _minor_ erase size though */
538 slave->mtd.flags &= ~MTD_WRITEABLE;
539 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
543 tmp = slave->mtd.size;
544 remainder = do_div(tmp, wr_alignment);
545 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
546 slave->mtd.flags &= ~MTD_WRITEABLE;
547 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
551 mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
552 slave->mtd.ecc_step_size = parent->ecc_step_size;
553 slave->mtd.ecc_strength = parent->ecc_strength;
554 slave->mtd.bitflip_threshold = parent->bitflip_threshold;
556 if (parent->_block_isbad) {
559 while (offs < slave->mtd.size) {
560 if (mtd_block_isreserved(parent, offs + slave->offset))
561 slave->mtd.ecc_stats.bbtblocks++;
562 else if (mtd_block_isbad(parent, offs + slave->offset))
563 slave->mtd.ecc_stats.badblocks++;
564 offs += slave->mtd.erasesize;
572 static ssize_t mtd_partition_offset_show(struct device *dev,
573 struct device_attribute *attr, char *buf)
575 struct mtd_info *mtd = dev_get_drvdata(dev);
576 struct mtd_part *part = mtd_to_part(mtd);
577 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
580 static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
582 static const struct attribute *mtd_partition_attrs[] = {
583 &dev_attr_offset.attr,
587 static int mtd_add_partition_attrs(struct mtd_part *new)
589 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
592 "mtd: failed to create partition attrs, err=%d\n", ret);
596 int mtd_add_partition(struct mtd_info *parent, const char *name,
597 long long offset, long long length)
599 struct mtd_partition part;
600 struct mtd_part *new;
603 /* the direct offset is expected */
604 if (offset == MTDPART_OFS_APPEND ||
605 offset == MTDPART_OFS_NXTBLK)
608 if (length == MTDPART_SIZ_FULL)
609 length = parent->size - offset;
614 memset(&part, 0, sizeof(part));
617 part.offset = offset;
619 new = allocate_partition(parent, &part, -1, offset);
623 mutex_lock(&mtd_partitions_mutex);
624 list_add(&new->list, &mtd_partitions);
625 mutex_unlock(&mtd_partitions_mutex);
627 add_mtd_device(&new->mtd);
629 mtd_add_partition_attrs(new);
633 EXPORT_SYMBOL_GPL(mtd_add_partition);
636 * __mtd_del_partition - delete MTD partition
638 * @priv: internal MTD struct for partition to be deleted
640 * This function must be called with the partitions mutex locked.
642 static int __mtd_del_partition(struct mtd_part *priv)
644 struct mtd_part *child, *next;
647 list_for_each_entry_safe(child, next, &mtd_partitions, list) {
648 if (child->parent == &priv->mtd) {
649 err = __mtd_del_partition(child);
655 sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs);
657 err = del_mtd_device(&priv->mtd);
661 list_del(&priv->list);
662 free_partition(priv);
668 * This function unregisters and destroy all slave MTD objects which are
669 * attached to the given MTD object.
671 int del_mtd_partitions(struct mtd_info *mtd)
673 struct mtd_part *slave, *next;
676 mutex_lock(&mtd_partitions_mutex);
677 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
678 if (slave->parent == mtd) {
679 ret = __mtd_del_partition(slave);
683 mutex_unlock(&mtd_partitions_mutex);
688 int mtd_del_partition(struct mtd_info *mtd, int partno)
690 struct mtd_part *slave, *next;
693 mutex_lock(&mtd_partitions_mutex);
694 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
695 if ((slave->parent == mtd) &&
696 (slave->mtd.index == partno)) {
697 ret = __mtd_del_partition(slave);
700 mutex_unlock(&mtd_partitions_mutex);
704 EXPORT_SYMBOL_GPL(mtd_del_partition);
707 * This function, given a master MTD object and a partition table, creates
708 * and registers slave MTD objects which are bound to the master according to
709 * the partition definitions.
711 * For historical reasons, this function's caller only registers the master
712 * if the MTD_PARTITIONED_MASTER config option is set.
715 int add_mtd_partitions(struct mtd_info *master,
716 const struct mtd_partition *parts,
719 struct mtd_part *slave;
720 uint64_t cur_offset = 0;
723 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
725 for (i = 0; i < nbparts; i++) {
726 slave = allocate_partition(master, parts + i, i, cur_offset);
728 del_mtd_partitions(master);
729 return PTR_ERR(slave);
732 mutex_lock(&mtd_partitions_mutex);
733 list_add(&slave->list, &mtd_partitions);
734 mutex_unlock(&mtd_partitions_mutex);
736 add_mtd_device(&slave->mtd);
737 mtd_add_partition_attrs(slave);
739 mtd_parse_part(slave, parts[i].types);
741 cur_offset = slave->offset + slave->mtd.size;
747 static DEFINE_SPINLOCK(part_parser_lock);
748 static LIST_HEAD(part_parsers);
750 static struct mtd_part_parser *mtd_part_parser_get(const char *name)
752 struct mtd_part_parser *p, *ret = NULL;
754 spin_lock(&part_parser_lock);
756 list_for_each_entry(p, &part_parsers, list)
757 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
762 spin_unlock(&part_parser_lock);
767 static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
769 module_put(p->owner);
773 * Many partition parsers just expected the core to kfree() all their data in
774 * one chunk. Do that by default.
776 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
782 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
787 p->cleanup = &mtd_part_parser_cleanup_default;
789 spin_lock(&part_parser_lock);
790 list_add(&p->list, &part_parsers);
791 spin_unlock(&part_parser_lock);
795 EXPORT_SYMBOL_GPL(__register_mtd_parser);
797 void deregister_mtd_parser(struct mtd_part_parser *p)
799 spin_lock(&part_parser_lock);
801 spin_unlock(&part_parser_lock);
803 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
806 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
807 * are changing this array!
809 static const char * const default_mtd_part_types[] = {
815 static int mtd_part_do_parse(struct mtd_part_parser *parser,
816 struct mtd_info *master,
817 struct mtd_partitions *pparts,
818 struct mtd_part_parser_data *data)
822 ret = (*parser->parse_fn)(master, &pparts->parts, data);
823 pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
827 pr_notice("%d %s partitions found on MTD device %s\n", ret,
828 parser->name, master->name);
830 pparts->nr_parts = ret;
831 pparts->parser = parser;
837 * mtd_part_get_compatible_parser - find MTD parser by a compatible string
839 * @compat: compatible string describing partitions in a device tree
841 * MTD parsers can specify supported partitions by providing a table of
842 * compatibility strings. This function finds a parser that advertises support
843 * for a passed value of "compatible".
845 static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
847 struct mtd_part_parser *p, *ret = NULL;
849 spin_lock(&part_parser_lock);
851 list_for_each_entry(p, &part_parsers, list) {
852 const struct of_device_id *matches;
854 matches = p->of_match_table;
858 for (; matches->compatible[0]; matches++) {
859 if (!strcmp(matches->compatible, compat) &&
860 try_module_get(p->owner)) {
870 spin_unlock(&part_parser_lock);
875 static int mtd_part_of_parse(struct mtd_info *master,
876 struct mtd_partitions *pparts)
878 struct mtd_part_parser *parser;
879 struct device_node *np;
880 struct property *prop;
882 const char *fixed = "fixed-partitions";
885 np = of_get_child_by_name(mtd_get_of_node(master), "partitions");
886 of_property_for_each_string(np, "compatible", prop, compat) {
887 parser = mtd_part_get_compatible_parser(compat);
890 ret = mtd_part_do_parse(parser, master, pparts, NULL);
895 mtd_part_parser_put(parser);
902 * For backward compatibility we have to try the "fixed-partitions"
903 * parser. It supports old DT format with partitions specified as a
904 * direct subnodes of a flash device DT node without any compatibility
905 * specified we could match.
907 parser = mtd_part_parser_get(fixed);
908 if (!parser && !request_module("%s", fixed))
909 parser = mtd_part_parser_get(fixed);
911 ret = mtd_part_do_parse(parser, master, pparts, NULL);
914 mtd_part_parser_put(parser);
923 * parse_mtd_partitions - parse and register MTD partitions
925 * @master: the master partition (describes whole MTD device)
926 * @types: names of partition parsers to try or %NULL
927 * @data: MTD partition parser-specific data
929 * This function tries to find & register partitions on MTD device @master. It
930 * uses MTD partition parsers, specified in @types. However, if @types is %NULL,
931 * then the default list of parsers is used. The default list contains only the
932 * "cmdlinepart" and "ofpart" parsers ATM.
933 * Note: If there are more then one parser in @types, the kernel only takes the
934 * partitions parsed out by the first parser.
936 * This function may return:
937 * o a negative error code in case of failure
938 * o number of found partitions otherwise
940 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
941 struct mtd_part_parser_data *data)
943 struct mtd_partitions pparts = { };
944 struct mtd_part_parser *parser;
948 types = default_mtd_part_types;
950 for ( ; *types; types++) {
952 * ofpart is a special type that means OF partitioning info
953 * should be used. It requires a bit different logic so it is
954 * handled in a separated function.
956 if (!strcmp(*types, "ofpart")) {
957 ret = mtd_part_of_parse(master, &pparts);
959 pr_debug("%s: parsing partitions %s\n", master->name,
961 parser = mtd_part_parser_get(*types);
962 if (!parser && !request_module("%s", *types))
963 parser = mtd_part_parser_get(*types);
964 pr_debug("%s: got parser %s\n", master->name,
965 parser ? parser->name : NULL);
968 ret = mtd_part_do_parse(parser, master, &pparts, data);
970 mtd_part_parser_put(parser);
972 /* Found partitions! */
974 err = add_mtd_partitions(master, pparts.parts,
976 mtd_part_parser_cleanup(&pparts);
977 return err ? err : pparts.nr_parts;
980 * Stash the first error we see; only report it if no parser
989 void mtd_part_parser_cleanup(struct mtd_partitions *parts)
991 const struct mtd_part_parser *parser;
996 parser = parts->parser;
999 parser->cleanup(parts->parts, parts->nr_parts);
1001 mtd_part_parser_put(parser);
1005 int mtd_is_partition(const struct mtd_info *mtd)
1007 struct mtd_part *part;
1010 mutex_lock(&mtd_partitions_mutex);
1011 list_for_each_entry(part, &mtd_partitions, list)
1012 if (&part->mtd == mtd) {
1016 mutex_unlock(&mtd_partitions_mutex);
1020 EXPORT_SYMBOL_GPL(mtd_is_partition);
1022 /* Returns the size of the entire flash chip */
1023 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
1025 if (!mtd_is_partition(mtd))
1028 return mtd_get_device_size(mtd_to_part(mtd)->parent);
1030 EXPORT_SYMBOL_GPL(mtd_get_device_size);