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>
33 #include <linux/kconfig.h>
37 /* Our partition linked list */
38 static LIST_HEAD(mtd_partitions
);
39 static DEFINE_MUTEX(mtd_partitions_mutex
);
41 /* Our partition node structure */
44 struct mtd_info
*master
;
46 struct list_head list
;
50 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
51 * the pointer to that structure.
53 static inline struct mtd_part
*mtd_to_part(const struct mtd_info
*mtd
)
55 return container_of(mtd
, struct mtd_part
, mtd
);
60 * MTD methods which simply translate the effective address and pass through
61 * to the _real_ device.
64 static int part_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
65 size_t *retlen
, u_char
*buf
)
67 struct mtd_part
*part
= mtd_to_part(mtd
);
68 struct mtd_ecc_stats stats
;
71 stats
= part
->master
->ecc_stats
;
72 res
= part
->master
->_read(part
->master
, from
+ part
->offset
, len
,
74 if (unlikely(mtd_is_eccerr(res
)))
75 mtd
->ecc_stats
.failed
+=
76 part
->master
->ecc_stats
.failed
- stats
.failed
;
78 mtd
->ecc_stats
.corrected
+=
79 part
->master
->ecc_stats
.corrected
- stats
.corrected
;
83 static int part_point(struct mtd_info
*mtd
, loff_t from
, size_t len
,
84 size_t *retlen
, void **virt
, resource_size_t
*phys
)
86 struct mtd_part
*part
= mtd_to_part(mtd
);
88 return part
->master
->_point(part
->master
, from
+ part
->offset
, len
,
92 static int part_unpoint(struct mtd_info
*mtd
, loff_t from
, size_t len
)
94 struct mtd_part
*part
= mtd_to_part(mtd
);
96 return part
->master
->_unpoint(part
->master
, from
+ part
->offset
, len
);
99 static unsigned long part_get_unmapped_area(struct mtd_info
*mtd
,
101 unsigned long offset
,
104 struct mtd_part
*part
= mtd_to_part(mtd
);
106 offset
+= part
->offset
;
107 return part
->master
->_get_unmapped_area(part
->master
, len
, offset
,
111 static int part_read_oob(struct mtd_info
*mtd
, loff_t from
,
112 struct mtd_oob_ops
*ops
)
114 struct mtd_part
*part
= mtd_to_part(mtd
);
117 if (from
>= mtd
->size
)
119 if (ops
->datbuf
&& from
+ ops
->len
> mtd
->size
)
123 * If OOB is also requested, make sure that we do not read past the end
129 len
= mtd_oobavail(mtd
, ops
);
130 pages
= mtd_div_by_ws(mtd
->size
, mtd
);
131 pages
-= mtd_div_by_ws(from
, mtd
);
132 if (ops
->ooboffs
+ ops
->ooblen
> pages
* len
)
136 res
= part
->master
->_read_oob(part
->master
, from
+ part
->offset
, ops
);
138 if (mtd_is_bitflip(res
))
139 mtd
->ecc_stats
.corrected
++;
140 if (mtd_is_eccerr(res
))
141 mtd
->ecc_stats
.failed
++;
146 static int part_read_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
147 size_t len
, size_t *retlen
, u_char
*buf
)
149 struct mtd_part
*part
= mtd_to_part(mtd
);
150 return part
->master
->_read_user_prot_reg(part
->master
, from
, len
,
154 static int part_get_user_prot_info(struct mtd_info
*mtd
, size_t len
,
155 size_t *retlen
, struct otp_info
*buf
)
157 struct mtd_part
*part
= mtd_to_part(mtd
);
158 return part
->master
->_get_user_prot_info(part
->master
, len
, retlen
,
162 static int part_read_fact_prot_reg(struct mtd_info
*mtd
, loff_t from
,
163 size_t len
, size_t *retlen
, u_char
*buf
)
165 struct mtd_part
*part
= mtd_to_part(mtd
);
166 return part
->master
->_read_fact_prot_reg(part
->master
, from
, len
,
170 static int part_get_fact_prot_info(struct mtd_info
*mtd
, size_t len
,
171 size_t *retlen
, struct otp_info
*buf
)
173 struct mtd_part
*part
= mtd_to_part(mtd
);
174 return part
->master
->_get_fact_prot_info(part
->master
, len
, retlen
,
178 static int part_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
179 size_t *retlen
, const u_char
*buf
)
181 struct mtd_part
*part
= mtd_to_part(mtd
);
182 return part
->master
->_write(part
->master
, to
+ part
->offset
, len
,
186 static int part_panic_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
187 size_t *retlen
, const u_char
*buf
)
189 struct mtd_part
*part
= mtd_to_part(mtd
);
190 return part
->master
->_panic_write(part
->master
, to
+ part
->offset
, len
,
194 static int part_write_oob(struct mtd_info
*mtd
, loff_t to
,
195 struct mtd_oob_ops
*ops
)
197 struct mtd_part
*part
= mtd_to_part(mtd
);
201 if (ops
->datbuf
&& to
+ ops
->len
> mtd
->size
)
203 return part
->master
->_write_oob(part
->master
, to
+ part
->offset
, ops
);
206 static int part_write_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
207 size_t len
, size_t *retlen
, u_char
*buf
)
209 struct mtd_part
*part
= mtd_to_part(mtd
);
210 return part
->master
->_write_user_prot_reg(part
->master
, from
, len
,
214 static int part_lock_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
217 struct mtd_part
*part
= mtd_to_part(mtd
);
218 return part
->master
->_lock_user_prot_reg(part
->master
, from
, len
);
221 static int part_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
222 unsigned long count
, loff_t to
, size_t *retlen
)
224 struct mtd_part
*part
= mtd_to_part(mtd
);
225 return part
->master
->_writev(part
->master
, vecs
, count
,
226 to
+ part
->offset
, retlen
);
229 static int part_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
231 struct mtd_part
*part
= mtd_to_part(mtd
);
234 instr
->addr
+= part
->offset
;
235 ret
= part
->master
->_erase(part
->master
, instr
);
237 if (instr
->fail_addr
!= MTD_FAIL_ADDR_UNKNOWN
)
238 instr
->fail_addr
-= part
->offset
;
239 instr
->addr
-= part
->offset
;
244 void mtd_erase_callback(struct erase_info
*instr
)
246 if (instr
->mtd
->_erase
== part_erase
) {
247 struct mtd_part
*part
= mtd_to_part(instr
->mtd
);
249 if (instr
->fail_addr
!= MTD_FAIL_ADDR_UNKNOWN
)
250 instr
->fail_addr
-= part
->offset
;
251 instr
->addr
-= part
->offset
;
254 instr
->callback(instr
);
256 EXPORT_SYMBOL_GPL(mtd_erase_callback
);
258 static int part_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
260 struct mtd_part
*part
= mtd_to_part(mtd
);
261 return part
->master
->_lock(part
->master
, ofs
+ part
->offset
, len
);
264 static int part_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
266 struct mtd_part
*part
= mtd_to_part(mtd
);
267 return part
->master
->_unlock(part
->master
, ofs
+ part
->offset
, len
);
270 static int part_is_locked(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
272 struct mtd_part
*part
= mtd_to_part(mtd
);
273 return part
->master
->_is_locked(part
->master
, ofs
+ part
->offset
, len
);
276 static void part_sync(struct mtd_info
*mtd
)
278 struct mtd_part
*part
= mtd_to_part(mtd
);
279 part
->master
->_sync(part
->master
);
282 static int part_suspend(struct mtd_info
*mtd
)
284 struct mtd_part
*part
= mtd_to_part(mtd
);
285 return part
->master
->_suspend(part
->master
);
288 static void part_resume(struct mtd_info
*mtd
)
290 struct mtd_part
*part
= mtd_to_part(mtd
);
291 part
->master
->_resume(part
->master
);
294 static int part_block_isreserved(struct mtd_info
*mtd
, loff_t ofs
)
296 struct mtd_part
*part
= mtd_to_part(mtd
);
298 return part
->master
->_block_isreserved(part
->master
, ofs
);
301 static int part_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
303 struct mtd_part
*part
= mtd_to_part(mtd
);
305 return part
->master
->_block_isbad(part
->master
, ofs
);
308 static int part_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
310 struct mtd_part
*part
= mtd_to_part(mtd
);
314 res
= part
->master
->_block_markbad(part
->master
, ofs
);
316 mtd
->ecc_stats
.badblocks
++;
320 static int part_get_device(struct mtd_info
*mtd
)
322 struct mtd_part
*part
= mtd_to_part(mtd
);
323 return part
->master
->_get_device(part
->master
);
326 static void part_put_device(struct mtd_info
*mtd
)
328 struct mtd_part
*part
= mtd_to_part(mtd
);
329 part
->master
->_put_device(part
->master
);
332 static int part_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
333 struct mtd_oob_region
*oobregion
)
335 struct mtd_part
*part
= mtd_to_part(mtd
);
337 return mtd_ooblayout_ecc(part
->master
, section
, oobregion
);
340 static int part_ooblayout_free(struct mtd_info
*mtd
, int section
,
341 struct mtd_oob_region
*oobregion
)
343 struct mtd_part
*part
= mtd_to_part(mtd
);
345 return mtd_ooblayout_free(part
->master
, section
, oobregion
);
348 static const struct mtd_ooblayout_ops part_ooblayout_ops
= {
349 .ecc
= part_ooblayout_ecc
,
350 .free
= part_ooblayout_free
,
353 static inline void free_partition(struct mtd_part
*p
)
360 * This function unregisters and destroy all slave MTD objects which are
361 * attached to the given master MTD object.
364 int del_mtd_partitions(struct mtd_info
*master
)
366 struct mtd_part
*slave
, *next
;
369 mutex_lock(&mtd_partitions_mutex
);
370 list_for_each_entry_safe(slave
, next
, &mtd_partitions
, list
)
371 if (slave
->master
== master
) {
372 ret
= del_mtd_device(&slave
->mtd
);
377 list_del(&slave
->list
);
378 free_partition(slave
);
380 mutex_unlock(&mtd_partitions_mutex
);
385 static struct mtd_part
*allocate_partition(struct mtd_info
*master
,
386 const struct mtd_partition
*part
, int partno
,
389 struct mtd_part
*slave
;
392 /* allocate the partition structure */
393 slave
= kzalloc(sizeof(*slave
), GFP_KERNEL
);
394 name
= kstrdup(part
->name
, GFP_KERNEL
);
395 if (!name
|| !slave
) {
396 printk(KERN_ERR
"memory allocation error while creating partitions for \"%s
\"\n",
400 return ERR_PTR(-ENOMEM);
403 /* set up the MTD object for this partition */
404 slave->mtd.type = master->type;
405 slave->mtd.flags = master->flags & ~part->mask_flags;
406 slave->mtd.size = part->size;
407 slave->mtd.writesize = master->writesize;
408 slave->mtd.writebufsize = master->writebufsize;
409 slave->mtd.oobsize = master->oobsize;
410 slave->mtd.oobavail = master->oobavail;
411 slave->mtd.subpage_sft = master->subpage_sft;
412 slave->mtd.pairing = master->pairing;
414 slave->mtd.name = name;
415 slave->mtd.owner = master->owner;
417 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
418 * concern for showing the same data in multiple partitions.
419 * However, it is very useful to have the master node present,
420 * so the MTD_PARTITIONED_MASTER option allows that. The master
421 * will have device nodes etc only if this is set, so make the
422 * parent conditional on that option. Note, this is a way to
423 * distinguish between the master and the partition in sysfs.
425 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) ?
429 slave->mtd._read = part_read;
430 slave->mtd._write = part_write;
432 if (master->_panic_write)
433 slave->mtd._panic_write = part_panic_write;
435 if (master->_point && master->_unpoint) {
436 slave->mtd._point = part_point;
437 slave->mtd._unpoint = part_unpoint;
440 if (master->_get_unmapped_area)
441 slave->mtd._get_unmapped_area = part_get_unmapped_area;
442 if (master->_read_oob)
443 slave->mtd._read_oob = part_read_oob;
444 if (master->_write_oob)
445 slave->mtd._write_oob = part_write_oob;
446 if (master->_read_user_prot_reg)
447 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
448 if (master->_read_fact_prot_reg)
449 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
450 if (master->_write_user_prot_reg)
451 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
452 if (master->_lock_user_prot_reg)
453 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
454 if (master->_get_user_prot_info)
455 slave->mtd._get_user_prot_info = part_get_user_prot_info;
456 if (master->_get_fact_prot_info)
457 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
459 slave->mtd._sync = part_sync;
460 if (!partno && !master->dev.class && master->_suspend &&
462 slave->mtd._suspend = part_suspend;
463 slave->mtd._resume = part_resume;
466 slave->mtd._writev = part_writev;
468 slave->mtd._lock = part_lock;
470 slave->mtd._unlock = part_unlock;
471 if (master->_is_locked)
472 slave->mtd._is_locked = part_is_locked;
473 if (master->_block_isreserved)
474 slave->mtd._block_isreserved = part_block_isreserved;
475 if (master->_block_isbad)
476 slave->mtd._block_isbad = part_block_isbad;
477 if (master->_block_markbad)
478 slave->mtd._block_markbad = part_block_markbad;
480 if (master->_get_device)
481 slave->mtd._get_device = part_get_device;
482 if (master->_put_device)
483 slave->mtd._put_device = part_put_device;
485 slave->mtd._erase = part_erase;
486 slave->master = master;
487 slave->offset = part->offset;
489 if (slave->offset == MTDPART_OFS_APPEND)
490 slave->offset = cur_offset;
491 if (slave->offset == MTDPART_OFS_NXTBLK) {
492 slave->offset = cur_offset;
493 if (mtd_mod_by_eb(cur_offset, master) != 0) {
494 /* Round up to next erasesize */
495 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
496 printk(KERN_NOTICE "Moving partition
%d
: "
497 "0x
%012llx
-> 0x
%012llx
\n", partno,
498 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
501 if (slave->offset == MTDPART_OFS_RETAIN) {
502 slave->offset = cur_offset;
503 if (master->size - slave->offset >= slave->mtd.size) {
504 slave->mtd.size = master->size - slave->offset
507 printk(KERN_ERR "mtd partition
\"%s
\" doesn
't have enough space: %#llx < %#llx, disabled\n",
508 part->name, master->size - slave->offset,
510 /* register to preserve ordering */
514 if (slave->mtd.size == MTDPART_SIZ_FULL)
515 slave->mtd.size = master->size - slave->offset;
517 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
518 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
520 /* let's
do some sanity checks */
521 if (slave
->offset
>= master
->size
) {
522 /* let's register it anyway to preserve ordering */
525 printk(KERN_ERR
"mtd: partition \"%s
\" is out of reach
-- disabled
\n",
529 if (slave->offset + slave->mtd.size > master->size) {
530 slave->mtd.size = master->size - slave->offset;
531 printk(KERN_WARNING"mtd
: partition
\"%s
\" extends beyond the end of device
\"%s
\" -- size truncated to
%#llx\n",
532 part->name, master->name, (unsigned long long)slave->mtd.size);
534 if (master->numeraseregions > 1) {
535 /* Deal with variable erase size stuff */
536 int i, max = master->numeraseregions;
537 u64 end = slave->offset + slave->mtd.size;
538 struct mtd_erase_region_info *regions = master->eraseregions;
540 /* Find the first erase regions which is part of this
542 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
544 /* The loop searched for the region _behind_ the first one */
548 /* Pick biggest erasesize */
549 for (; i < max && regions[i].offset < end; i++) {
550 if (slave->mtd.erasesize < regions[i].erasesize) {
551 slave->mtd.erasesize = regions[i].erasesize;
554 BUG_ON(slave->mtd.erasesize == 0);
556 /* Single erase size */
557 slave->mtd.erasesize = master->erasesize;
560 if ((slave->mtd.flags & MTD_WRITEABLE) &&
561 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
562 /* Doesn't start on a boundary of major erase size */
563 /* FIXME: Let it be writable if it is on a boundary of
564 * _minor_ erase size though */
565 slave->mtd.flags &= ~MTD_WRITEABLE;
566 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
569 if ((slave->mtd.flags & MTD_WRITEABLE) &&
570 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
571 slave->mtd.flags &= ~MTD_WRITEABLE;
572 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
576 mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
577 slave->mtd.ecc_step_size = master->ecc_step_size;
578 slave->mtd.ecc_strength = master->ecc_strength;
579 slave->mtd.bitflip_threshold = master->bitflip_threshold;
581 if (master->_block_isbad) {
584 while (offs < slave->mtd.size) {
585 if (mtd_block_isreserved(master, offs + slave->offset))
586 slave->mtd.ecc_stats.bbtblocks++;
587 else if (mtd_block_isbad(master, offs + slave->offset))
588 slave->mtd.ecc_stats.badblocks++;
589 offs += slave->mtd.erasesize;
597 static ssize_t mtd_partition_offset_show(struct device *dev,
598 struct device_attribute *attr, char *buf)
600 struct mtd_info *mtd = dev_get_drvdata(dev);
601 struct mtd_part *part = mtd_to_part(mtd);
602 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
605 static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
607 static const struct attribute *mtd_partition_attrs[] = {
608 &dev_attr_offset.attr,
612 static int mtd_add_partition_attrs(struct mtd_part *new)
614 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
617 "mtd: failed to create partition attrs, err=%d\n", ret);
621 int mtd_add_partition(struct mtd_info *master, const char *name,
622 long long offset, long long length)
624 struct mtd_partition part;
625 struct mtd_part *new;
628 /* the direct offset is expected */
629 if (offset == MTDPART_OFS_APPEND ||
630 offset == MTDPART_OFS_NXTBLK)
633 if (length == MTDPART_SIZ_FULL)
634 length = master->size - offset;
639 memset(&part, 0, sizeof(part));
642 part.offset = offset;
644 new = allocate_partition(master, &part, -1, offset);
648 mutex_lock(&mtd_partitions_mutex);
649 list_add(&new->list, &mtd_partitions);
650 mutex_unlock(&mtd_partitions_mutex);
652 add_mtd_device(&new->mtd);
654 mtd_add_partition_attrs(new);
658 EXPORT_SYMBOL_GPL(mtd_add_partition);
660 int mtd_del_partition(struct mtd_info *master, int partno)
662 struct mtd_part *slave, *next;
665 mutex_lock(&mtd_partitions_mutex);
666 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
667 if ((slave->master == master) &&
668 (slave->mtd.index == partno)) {
669 sysfs_remove_files(&slave->mtd.dev.kobj,
670 mtd_partition_attrs);
671 ret = del_mtd_device(&slave->mtd);
675 list_del(&slave->list);
676 free_partition(slave);
679 mutex_unlock(&mtd_partitions_mutex);
683 EXPORT_SYMBOL_GPL(mtd_del_partition);
686 * This function, given a master MTD object and a partition table, creates
687 * and registers slave MTD objects which are bound to the master according to
688 * the partition definitions.
690 * For historical reasons, this function's caller only registers the master
691 * if the MTD_PARTITIONED_MASTER config option is set.
694 int add_mtd_partitions(struct mtd_info *master,
695 const struct mtd_partition *parts,
698 struct mtd_part *slave;
699 uint64_t cur_offset = 0;
702 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
704 for (i = 0; i < nbparts; i++) {
705 slave = allocate_partition(master, parts + i, i, cur_offset);
707 del_mtd_partitions(master);
708 return PTR_ERR(slave);
711 mutex_lock(&mtd_partitions_mutex);
712 list_add(&slave->list, &mtd_partitions);
713 mutex_unlock(&mtd_partitions_mutex);
715 add_mtd_device(&slave->mtd);
716 mtd_add_partition_attrs(slave);
718 cur_offset = slave->offset + slave->mtd.size;
724 static DEFINE_SPINLOCK(part_parser_lock);
725 static LIST_HEAD(part_parsers);
727 static struct mtd_part_parser *mtd_part_parser_get(const char *name)
729 struct mtd_part_parser *p, *ret = NULL;
731 spin_lock(&part_parser_lock);
733 list_for_each_entry(p, &part_parsers, list)
734 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
739 spin_unlock(&part_parser_lock);
744 static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
746 module_put(p->owner);
750 * Many partition parsers just expected the core to kfree() all their data in
751 * one chunk. Do that by default.
753 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
759 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
764 p->cleanup = &mtd_part_parser_cleanup_default;
766 spin_lock(&part_parser_lock);
767 list_add(&p->list, &part_parsers);
768 spin_unlock(&part_parser_lock);
772 EXPORT_SYMBOL_GPL(__register_mtd_parser);
774 void deregister_mtd_parser(struct mtd_part_parser *p)
776 spin_lock(&part_parser_lock);
778 spin_unlock(&part_parser_lock);
780 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
783 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
784 * are changing this array!
786 static const char * const default_mtd_part_types[] = {
793 * parse_mtd_partitions - parse MTD partitions
794 * @master: the master partition (describes whole MTD device)
795 * @types: names of partition parsers to try or %NULL
796 * @pparts: info about partitions found is returned here
797 * @data: MTD partition parser-specific data
799 * This function tries to find partition on MTD device @master. It uses MTD
800 * partition parsers, specified in @types. However, if @types is %NULL, then
801 * the default list of parsers is used. The default list contains only the
802 * "cmdlinepart" and "ofpart" parsers ATM.
803 * Note: If there are more then one parser in @types, the kernel only takes the
804 * partitions parsed out by the first parser.
806 * This function may return:
807 * o a negative error code in case of failure
808 * o zero otherwise, and @pparts will describe the partitions, number of
809 * partitions, and the parser which parsed them. Caller must release
810 * resources with mtd_part_parser_cleanup() when finished with the returned
813 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
814 struct mtd_partitions *pparts,
815 struct mtd_part_parser_data *data)
817 struct mtd_part_parser *parser;
821 types = default_mtd_part_types;
823 for ( ; *types; types++) {
824 pr_debug("%s: parsing partitions %s\n", master->name, *types);
825 parser = mtd_part_parser_get(*types);
826 if (!parser && !request_module("%s", *types))
827 parser = mtd_part_parser_get(*types);
828 pr_debug("%s: got parser %s\n", master->name,
829 parser ? parser->name : NULL);
832 ret = (*parser->parse_fn)(master, &pparts->parts, data);
833 pr_debug("%s: parser %s: %i\n",
834 master->name, parser->name, ret);
836 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
837 ret, parser->name, master->name);
838 pparts->nr_parts = ret;
839 pparts->parser = parser;
842 mtd_part_parser_put(parser);
844 * Stash the first error we see; only report it if no parser
853 void mtd_part_parser_cleanup(struct mtd_partitions *parts)
855 const struct mtd_part_parser *parser;
860 parser = parts->parser;
863 parser->cleanup(parts->parts, parts->nr_parts);
865 mtd_part_parser_put(parser);
869 int mtd_is_partition(const struct mtd_info *mtd)
871 struct mtd_part *part;
874 mutex_lock(&mtd_partitions_mutex);
875 list_for_each_entry(part, &mtd_partitions, list)
876 if (&part->mtd == mtd) {
880 mutex_unlock(&mtd_partitions_mutex);
884 EXPORT_SYMBOL_GPL(mtd_is_partition);
886 /* Returns the size of the entire flash chip */
887 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
889 if (!mtd_is_partition(mtd))
892 return mtd_to_part(mtd)->master->size;
894 EXPORT_SYMBOL_GPL(mtd_get_device_size);