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
)
325 static struct mtd_part
*allocate_partition(struct mtd_info
*parent
,
326 const struct mtd_partition
*part
, int partno
,
329 int wr_alignment
= (parent
->flags
& MTD_NO_ERASE
) ? parent
->writesize
:
331 struct mtd_part
*slave
;
336 /* allocate the partition structure */
337 slave
= kzalloc(sizeof(*slave
), GFP_KERNEL
);
338 name
= kstrdup(part
->name
, GFP_KERNEL
);
339 if (!name
|| !slave
) {
340 printk(KERN_ERR
"memory allocation error while creating partitions for \"%s
\"\n",
344 return ERR_PTR(-ENOMEM);
347 /* set up the MTD object for this partition */
348 slave->mtd.type = parent->type;
349 slave->mtd.flags = parent->flags & ~part->mask_flags;
350 slave->mtd.size = part->size;
351 slave->mtd.writesize = parent->writesize;
352 slave->mtd.writebufsize = parent->writebufsize;
353 slave->mtd.oobsize = parent->oobsize;
354 slave->mtd.oobavail = parent->oobavail;
355 slave->mtd.subpage_sft = parent->subpage_sft;
356 slave->mtd.pairing = parent->pairing;
358 slave->mtd.name = name;
359 slave->mtd.owner = parent->owner;
361 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
362 * concern for showing the same data in multiple partitions.
363 * However, it is very useful to have the master node present,
364 * so the MTD_PARTITIONED_MASTER option allows that. The master
365 * will have device nodes etc only if this is set, so make the
366 * parent conditional on that option. Note, this is a way to
367 * distinguish between the master and the partition in sysfs.
369 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
372 slave->mtd.dev.of_node = part->of_node;
375 slave->mtd._read = part_read;
377 slave->mtd._write = part_write;
379 if (parent->_panic_write)
380 slave->mtd._panic_write = part_panic_write;
382 if (parent->_point && parent->_unpoint) {
383 slave->mtd._point = part_point;
384 slave->mtd._unpoint = part_unpoint;
387 if (parent->_read_oob)
388 slave->mtd._read_oob = part_read_oob;
389 if (parent->_write_oob)
390 slave->mtd._write_oob = part_write_oob;
391 if (parent->_read_user_prot_reg)
392 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
393 if (parent->_read_fact_prot_reg)
394 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
395 if (parent->_write_user_prot_reg)
396 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
397 if (parent->_lock_user_prot_reg)
398 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
399 if (parent->_get_user_prot_info)
400 slave->mtd._get_user_prot_info = part_get_user_prot_info;
401 if (parent->_get_fact_prot_info)
402 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
404 slave->mtd._sync = part_sync;
405 if (!partno && !parent->dev.class && parent->_suspend &&
407 slave->mtd._suspend = part_suspend;
408 slave->mtd._resume = part_resume;
411 slave->mtd._writev = part_writev;
413 slave->mtd._lock = part_lock;
415 slave->mtd._unlock = part_unlock;
416 if (parent->_is_locked)
417 slave->mtd._is_locked = part_is_locked;
418 if (parent->_block_isreserved)
419 slave->mtd._block_isreserved = part_block_isreserved;
420 if (parent->_block_isbad)
421 slave->mtd._block_isbad = part_block_isbad;
422 if (parent->_block_markbad)
423 slave->mtd._block_markbad = part_block_markbad;
424 if (parent->_max_bad_blocks)
425 slave->mtd._max_bad_blocks = part_max_bad_blocks;
427 if (parent->_get_device)
428 slave->mtd._get_device = part_get_device;
429 if (parent->_put_device)
430 slave->mtd._put_device = part_put_device;
432 slave->mtd._erase = part_erase;
433 slave->parent = parent;
434 slave->offset = part->offset;
436 if (slave->offset == MTDPART_OFS_APPEND)
437 slave->offset = cur_offset;
438 if (slave->offset == MTDPART_OFS_NXTBLK) {
440 slave->offset = cur_offset;
441 remainder = do_div(tmp, wr_alignment);
443 slave->offset += wr_alignment - remainder;
444 printk(KERN_NOTICE "Moving partition
%d
: "
445 "0x
%012llx
-> 0x
%012llx
\n", partno,
446 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
449 if (slave->offset == MTDPART_OFS_RETAIN) {
450 slave->offset = cur_offset;
451 if (parent->size - slave->offset >= slave->mtd.size) {
452 slave->mtd.size = parent->size - slave->offset
455 printk(KERN_ERR "mtd partition
\"%s
\" doesn
't have enough space: %#llx < %#llx, disabled\n",
456 part->name, parent->size - slave->offset,
458 /* register to preserve ordering */
462 if (slave->mtd.size == MTDPART_SIZ_FULL)
463 slave->mtd.size = parent->size - slave->offset;
465 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
466 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
468 /* let's
do some sanity checks */
469 if (slave
->offset
>= parent
->size
) {
470 /* let's register it anyway to preserve ordering */
474 /* Initialize ->erasesize to make add_mtd_device() happy. */
475 slave
->mtd
.erasesize
= parent
->erasesize
;
477 printk(KERN_ERR
"mtd: partition \"%s
\" is out of reach
-- disabled
\n",
481 if (slave->offset + slave->mtd.size > parent->size) {
482 slave->mtd.size = parent->size - slave->offset;
483 printk(KERN_WARNING"mtd
: partition
\"%s
\" extends beyond the end of device
\"%s
\" -- size truncated to
%#llx\n",
484 part->name, parent->name, (unsigned long long)slave->mtd.size);
486 if (parent->numeraseregions > 1) {
487 /* Deal with variable erase size stuff */
488 int i, max = parent->numeraseregions;
489 u64 end = slave->offset + slave->mtd.size;
490 struct mtd_erase_region_info *regions = parent->eraseregions;
492 /* Find the first erase regions which is part of this
494 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
496 /* The loop searched for the region _behind_ the first one */
500 /* Pick biggest erasesize */
501 for (; i < max && regions[i].offset < end; i++) {
502 if (slave->mtd.erasesize < regions[i].erasesize) {
503 slave->mtd.erasesize = regions[i].erasesize;
506 BUG_ON(slave->mtd.erasesize == 0);
508 /* Single erase size */
509 slave->mtd.erasesize = parent->erasesize;
513 * Slave erasesize might differ from the master one if the master
514 * exposes several regions with different erasesize. Adjust
515 * wr_alignment accordingly.
517 if (!(slave->mtd.flags & MTD_NO_ERASE))
518 wr_alignment = slave->mtd.erasesize;
521 remainder = do_div(tmp, wr_alignment);
522 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
523 /* Doesn't start on a boundary of major erase size */
524 /* FIXME: Let it be writable if it is on a boundary of
525 * _minor_ erase size though */
526 slave->mtd.flags &= ~MTD_WRITEABLE;
527 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
531 tmp = slave->mtd.size;
532 remainder = do_div(tmp, wr_alignment);
533 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
534 slave->mtd.flags &= ~MTD_WRITEABLE;
535 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
539 mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
540 slave->mtd.ecc_step_size = parent->ecc_step_size;
541 slave->mtd.ecc_strength = parent->ecc_strength;
542 slave->mtd.bitflip_threshold = parent->bitflip_threshold;
544 if (parent->_block_isbad) {
547 while (offs < slave->mtd.size) {
548 if (mtd_block_isreserved(parent, offs + slave->offset))
549 slave->mtd.ecc_stats.bbtblocks++;
550 else if (mtd_block_isbad(parent, offs + slave->offset))
551 slave->mtd.ecc_stats.badblocks++;
552 offs += slave->mtd.erasesize;
560 static ssize_t mtd_partition_offset_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
563 struct mtd_info *mtd = dev_get_drvdata(dev);
564 struct mtd_part *part = mtd_to_part(mtd);
565 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
568 static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
570 static const struct attribute *mtd_partition_attrs[] = {
571 &dev_attr_offset.attr,
575 static int mtd_add_partition_attrs(struct mtd_part *new)
577 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
580 "mtd: failed to create partition attrs, err=%d\n", ret);
584 int mtd_add_partition(struct mtd_info *parent, const char *name,
585 long long offset, long long length)
587 struct mtd_partition part;
588 struct mtd_part *new;
591 /* the direct offset is expected */
592 if (offset == MTDPART_OFS_APPEND ||
593 offset == MTDPART_OFS_NXTBLK)
596 if (length == MTDPART_SIZ_FULL)
597 length = parent->size - offset;
602 memset(&part, 0, sizeof(part));
605 part.offset = offset;
607 new = allocate_partition(parent, &part, -1, offset);
611 mutex_lock(&mtd_partitions_mutex);
612 list_add(&new->list, &mtd_partitions);
613 mutex_unlock(&mtd_partitions_mutex);
615 ret = add_mtd_device(&new->mtd);
617 goto err_remove_part;
619 mtd_add_partition_attrs(new);
624 mutex_lock(&mtd_partitions_mutex);
625 list_del(&new->list);
626 mutex_unlock(&mtd_partitions_mutex);
632 EXPORT_SYMBOL_GPL(mtd_add_partition);
635 * __mtd_del_partition - delete MTD partition
637 * @priv: internal MTD struct for partition to be deleted
639 * This function must be called with the partitions mutex locked.
641 static int __mtd_del_partition(struct mtd_part *priv)
643 struct mtd_part *child, *next;
646 list_for_each_entry_safe(child, next, &mtd_partitions, list) {
647 if (child->parent == &priv->mtd) {
648 err = __mtd_del_partition(child);
654 sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs);
656 err = del_mtd_device(&priv->mtd);
660 list_del(&priv->list);
661 free_partition(priv);
667 * This function unregisters and destroy all slave MTD objects which are
668 * attached to the given MTD object.
670 int del_mtd_partitions(struct mtd_info *mtd)
672 struct mtd_part *slave, *next;
675 mutex_lock(&mtd_partitions_mutex);
676 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
677 if (slave->parent == mtd) {
678 ret = __mtd_del_partition(slave);
682 mutex_unlock(&mtd_partitions_mutex);
687 int mtd_del_partition(struct mtd_info *mtd, int partno)
689 struct mtd_part *slave, *next;
692 mutex_lock(&mtd_partitions_mutex);
693 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
694 if ((slave->parent == mtd) &&
695 (slave->mtd.index == partno)) {
696 ret = __mtd_del_partition(slave);
699 mutex_unlock(&mtd_partitions_mutex);
703 EXPORT_SYMBOL_GPL(mtd_del_partition);
706 * This function, given a master MTD object and a partition table, creates
707 * and registers slave MTD objects which are bound to the master according to
708 * the partition definitions.
710 * For historical reasons, this function's caller only registers the master
711 * if the MTD_PARTITIONED_MASTER config option is set.
714 int add_mtd_partitions(struct mtd_info *master,
715 const struct mtd_partition *parts,
718 struct mtd_part *slave;
719 uint64_t cur_offset = 0;
722 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
724 for (i = 0; i < nbparts; i++) {
725 slave = allocate_partition(master, parts + i, i, cur_offset);
727 ret = PTR_ERR(slave);
728 goto err_del_partitions;
731 mutex_lock(&mtd_partitions_mutex);
732 list_add(&slave->list, &mtd_partitions);
733 mutex_unlock(&mtd_partitions_mutex);
735 ret = add_mtd_device(&slave->mtd);
737 mutex_lock(&mtd_partitions_mutex);
738 list_del(&slave->list);
739 mutex_unlock(&mtd_partitions_mutex);
741 free_partition(slave);
742 goto err_del_partitions;
745 mtd_add_partition_attrs(slave);
746 /* Look for subpartitions */
747 parse_mtd_partitions(&slave->mtd, parts[i].types, NULL);
749 cur_offset = slave->offset + slave->mtd.size;
755 del_mtd_partitions(master);
760 static DEFINE_SPINLOCK(part_parser_lock);
761 static LIST_HEAD(part_parsers);
763 static struct mtd_part_parser *mtd_part_parser_get(const char *name)
765 struct mtd_part_parser *p, *ret = NULL;
767 spin_lock(&part_parser_lock);
769 list_for_each_entry(p, &part_parsers, list)
770 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
775 spin_unlock(&part_parser_lock);
780 static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
782 module_put(p->owner);
786 * Many partition parsers just expected the core to kfree() all their data in
787 * one chunk. Do that by default.
789 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
795 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
800 p->cleanup = &mtd_part_parser_cleanup_default;
802 spin_lock(&part_parser_lock);
803 list_add(&p->list, &part_parsers);
804 spin_unlock(&part_parser_lock);
808 EXPORT_SYMBOL_GPL(__register_mtd_parser);
810 void deregister_mtd_parser(struct mtd_part_parser *p)
812 spin_lock(&part_parser_lock);
814 spin_unlock(&part_parser_lock);
816 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
819 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
820 * are changing this array!
822 static const char * const default_mtd_part_types[] = {
828 /* Check DT only when looking for subpartitions. */
829 static const char * const default_subpartition_types[] = {
834 static int mtd_part_do_parse(struct mtd_part_parser *parser,
835 struct mtd_info *master,
836 struct mtd_partitions *pparts,
837 struct mtd_part_parser_data *data)
841 ret = (*parser->parse_fn)(master, &pparts->parts, data);
842 pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
846 pr_notice("%d %s partitions found on MTD device %s\n", ret,
847 parser->name, master->name);
849 pparts->nr_parts = ret;
850 pparts->parser = parser;
856 * mtd_part_get_compatible_parser - find MTD parser by a compatible string
858 * @compat: compatible string describing partitions in a device tree
860 * MTD parsers can specify supported partitions by providing a table of
861 * compatibility strings. This function finds a parser that advertises support
862 * for a passed value of "compatible".
864 static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
866 struct mtd_part_parser *p, *ret = NULL;
868 spin_lock(&part_parser_lock);
870 list_for_each_entry(p, &part_parsers, list) {
871 const struct of_device_id *matches;
873 matches = p->of_match_table;
877 for (; matches->compatible[0]; matches++) {
878 if (!strcmp(matches->compatible, compat) &&
879 try_module_get(p->owner)) {
889 spin_unlock(&part_parser_lock);
894 static int mtd_part_of_parse(struct mtd_info *master,
895 struct mtd_partitions *pparts)
897 struct mtd_part_parser *parser;
898 struct device_node *np;
899 struct property *prop;
901 const char *fixed = "fixed-partitions";
904 np = mtd_get_of_node(master);
905 if (mtd_is_partition(master))
908 np = of_get_child_by_name(np, "partitions");
910 of_property_for_each_string(np, "compatible", prop, compat) {
911 parser = mtd_part_get_compatible_parser(compat);
914 ret = mtd_part_do_parse(parser, master, pparts, NULL);
919 mtd_part_parser_put(parser);
926 * For backward compatibility we have to try the "fixed-partitions"
927 * parser. It supports old DT format with partitions specified as a
928 * direct subnodes of a flash device DT node without any compatibility
929 * specified we could match.
931 parser = mtd_part_parser_get(fixed);
932 if (!parser && !request_module("%s", fixed))
933 parser = mtd_part_parser_get(fixed);
935 ret = mtd_part_do_parse(parser, master, pparts, NULL);
938 mtd_part_parser_put(parser);
947 * parse_mtd_partitions - parse and register MTD partitions
949 * @master: the master partition (describes whole MTD device)
950 * @types: names of partition parsers to try or %NULL
951 * @data: MTD partition parser-specific data
953 * This function tries to find & register partitions on MTD device @master. It
954 * uses MTD partition parsers, specified in @types. However, if @types is %NULL,
955 * then the default list of parsers is used. The default list contains only the
956 * "cmdlinepart" and "ofpart" parsers ATM.
957 * Note: If there are more then one parser in @types, the kernel only takes the
958 * partitions parsed out by the first parser.
960 * This function may return:
961 * o a negative error code in case of failure
962 * o number of found partitions otherwise
964 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
965 struct mtd_part_parser_data *data)
967 struct mtd_partitions pparts = { };
968 struct mtd_part_parser *parser;
972 types = mtd_is_partition(master) ? default_subpartition_types :
973 default_mtd_part_types;
975 for ( ; *types; types++) {
977 * ofpart is a special type that means OF partitioning info
978 * should be used. It requires a bit different logic so it is
979 * handled in a separated function.
981 if (!strcmp(*types, "ofpart")) {
982 ret = mtd_part_of_parse(master, &pparts);
984 pr_debug("%s: parsing partitions %s\n", master->name,
986 parser = mtd_part_parser_get(*types);
987 if (!parser && !request_module("%s", *types))
988 parser = mtd_part_parser_get(*types);
989 pr_debug("%s: got parser %s\n", master->name,
990 parser ? parser->name : NULL);
993 ret = mtd_part_do_parse(parser, master, &pparts, data);
995 mtd_part_parser_put(parser);
997 /* Found partitions! */
999 err = add_mtd_partitions(master, pparts.parts,
1001 mtd_part_parser_cleanup(&pparts);
1002 return err ? err : pparts.nr_parts;
1005 * Stash the first error we see; only report it if no parser
1008 if (ret < 0 && !err)
1014 void mtd_part_parser_cleanup(struct mtd_partitions *parts)
1016 const struct mtd_part_parser *parser;
1021 parser = parts->parser;
1023 if (parser->cleanup)
1024 parser->cleanup(parts->parts, parts->nr_parts);
1026 mtd_part_parser_put(parser);
1030 int mtd_is_partition(const struct mtd_info *mtd)
1032 struct mtd_part *part;
1035 mutex_lock(&mtd_partitions_mutex);
1036 list_for_each_entry(part, &mtd_partitions, list)
1037 if (&part->mtd == mtd) {
1041 mutex_unlock(&mtd_partitions_mutex);
1045 EXPORT_SYMBOL_GPL(mtd_is_partition);
1047 /* Returns the size of the entire flash chip */
1048 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
1050 if (!mtd_is_partition(mtd))
1053 return mtd_get_device_size(mtd_to_part(mtd)->parent);
1055 EXPORT_SYMBOL_GPL(mtd_get_device_size);