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.
52 static inline struct mtd_part
*mtd_to_part(const struct mtd_info
*mtd
)
54 return container_of(mtd
, struct mtd_part
, mtd
);
59 * MTD methods which simply translate the effective address and pass through
60 * to the _real_ device.
63 static int part_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
64 size_t *retlen
, u_char
*buf
)
66 struct mtd_part
*part
= mtd_to_part(mtd
);
67 struct mtd_ecc_stats stats
;
70 stats
= part
->master
->ecc_stats
;
71 res
= part
->master
->_read(part
->master
, from
+ part
->offset
, len
,
73 if (unlikely(mtd_is_eccerr(res
)))
74 mtd
->ecc_stats
.failed
+=
75 part
->master
->ecc_stats
.failed
- stats
.failed
;
77 mtd
->ecc_stats
.corrected
+=
78 part
->master
->ecc_stats
.corrected
- stats
.corrected
;
82 static int part_point(struct mtd_info
*mtd
, loff_t from
, size_t len
,
83 size_t *retlen
, void **virt
, resource_size_t
*phys
)
85 struct mtd_part
*part
= mtd_to_part(mtd
);
87 return part
->master
->_point(part
->master
, from
+ part
->offset
, len
,
91 static int part_unpoint(struct mtd_info
*mtd
, loff_t from
, size_t len
)
93 struct mtd_part
*part
= mtd_to_part(mtd
);
95 return part
->master
->_unpoint(part
->master
, from
+ part
->offset
, len
);
98 static unsigned long part_get_unmapped_area(struct mtd_info
*mtd
,
100 unsigned long offset
,
103 struct mtd_part
*part
= mtd_to_part(mtd
);
105 offset
+= part
->offset
;
106 return part
->master
->_get_unmapped_area(part
->master
, len
, offset
,
110 static int part_read_oob(struct mtd_info
*mtd
, loff_t from
,
111 struct mtd_oob_ops
*ops
)
113 struct mtd_part
*part
= mtd_to_part(mtd
);
116 if (from
>= mtd
->size
)
118 if (ops
->datbuf
&& from
+ ops
->len
> mtd
->size
)
122 * If OOB is also requested, make sure that we do not read past the end
128 len
= mtd_oobavail(mtd
, ops
);
129 pages
= mtd_div_by_ws(mtd
->size
, mtd
);
130 pages
-= mtd_div_by_ws(from
, mtd
);
131 if (ops
->ooboffs
+ ops
->ooblen
> pages
* len
)
135 res
= part
->master
->_read_oob(part
->master
, from
+ part
->offset
, ops
);
137 if (mtd_is_bitflip(res
))
138 mtd
->ecc_stats
.corrected
++;
139 if (mtd_is_eccerr(res
))
140 mtd
->ecc_stats
.failed
++;
145 static int part_read_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
146 size_t len
, size_t *retlen
, u_char
*buf
)
148 struct mtd_part
*part
= mtd_to_part(mtd
);
149 return part
->master
->_read_user_prot_reg(part
->master
, from
, len
,
153 static int part_get_user_prot_info(struct mtd_info
*mtd
, size_t len
,
154 size_t *retlen
, struct otp_info
*buf
)
156 struct mtd_part
*part
= mtd_to_part(mtd
);
157 return part
->master
->_get_user_prot_info(part
->master
, len
, retlen
,
161 static int part_read_fact_prot_reg(struct mtd_info
*mtd
, loff_t from
,
162 size_t len
, size_t *retlen
, u_char
*buf
)
164 struct mtd_part
*part
= mtd_to_part(mtd
);
165 return part
->master
->_read_fact_prot_reg(part
->master
, from
, len
,
169 static int part_get_fact_prot_info(struct mtd_info
*mtd
, size_t len
,
170 size_t *retlen
, struct otp_info
*buf
)
172 struct mtd_part
*part
= mtd_to_part(mtd
);
173 return part
->master
->_get_fact_prot_info(part
->master
, len
, retlen
,
177 static int part_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
178 size_t *retlen
, const u_char
*buf
)
180 struct mtd_part
*part
= mtd_to_part(mtd
);
181 return part
->master
->_write(part
->master
, to
+ part
->offset
, len
,
185 static int part_panic_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
186 size_t *retlen
, const u_char
*buf
)
188 struct mtd_part
*part
= mtd_to_part(mtd
);
189 return part
->master
->_panic_write(part
->master
, to
+ part
->offset
, len
,
193 static int part_write_oob(struct mtd_info
*mtd
, loff_t to
,
194 struct mtd_oob_ops
*ops
)
196 struct mtd_part
*part
= mtd_to_part(mtd
);
200 if (ops
->datbuf
&& to
+ ops
->len
> mtd
->size
)
202 return part
->master
->_write_oob(part
->master
, to
+ part
->offset
, ops
);
205 static int part_write_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
206 size_t len
, size_t *retlen
, u_char
*buf
)
208 struct mtd_part
*part
= mtd_to_part(mtd
);
209 return part
->master
->_write_user_prot_reg(part
->master
, from
, len
,
213 static int part_lock_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
216 struct mtd_part
*part
= mtd_to_part(mtd
);
217 return part
->master
->_lock_user_prot_reg(part
->master
, from
, len
);
220 static int part_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
221 unsigned long count
, loff_t to
, size_t *retlen
)
223 struct mtd_part
*part
= mtd_to_part(mtd
);
224 return part
->master
->_writev(part
->master
, vecs
, count
,
225 to
+ part
->offset
, retlen
);
228 static int part_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
230 struct mtd_part
*part
= mtd_to_part(mtd
);
233 instr
->addr
+= part
->offset
;
234 ret
= part
->master
->_erase(part
->master
, instr
);
236 if (instr
->fail_addr
!= MTD_FAIL_ADDR_UNKNOWN
)
237 instr
->fail_addr
-= part
->offset
;
238 instr
->addr
-= part
->offset
;
243 void mtd_erase_callback(struct erase_info
*instr
)
245 if (instr
->mtd
->_erase
== part_erase
) {
246 struct mtd_part
*part
= mtd_to_part(instr
->mtd
);
248 if (instr
->fail_addr
!= MTD_FAIL_ADDR_UNKNOWN
)
249 instr
->fail_addr
-= part
->offset
;
250 instr
->addr
-= part
->offset
;
253 instr
->callback(instr
);
255 EXPORT_SYMBOL_GPL(mtd_erase_callback
);
257 static int part_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
259 struct mtd_part
*part
= mtd_to_part(mtd
);
260 return part
->master
->_lock(part
->master
, ofs
+ part
->offset
, len
);
263 static int part_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
265 struct mtd_part
*part
= mtd_to_part(mtd
);
266 return part
->master
->_unlock(part
->master
, ofs
+ part
->offset
, len
);
269 static int part_is_locked(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
271 struct mtd_part
*part
= mtd_to_part(mtd
);
272 return part
->master
->_is_locked(part
->master
, ofs
+ part
->offset
, len
);
275 static void part_sync(struct mtd_info
*mtd
)
277 struct mtd_part
*part
= mtd_to_part(mtd
);
278 part
->master
->_sync(part
->master
);
281 static int part_suspend(struct mtd_info
*mtd
)
283 struct mtd_part
*part
= mtd_to_part(mtd
);
284 return part
->master
->_suspend(part
->master
);
287 static void part_resume(struct mtd_info
*mtd
)
289 struct mtd_part
*part
= mtd_to_part(mtd
);
290 part
->master
->_resume(part
->master
);
293 static int part_block_isreserved(struct mtd_info
*mtd
, loff_t ofs
)
295 struct mtd_part
*part
= mtd_to_part(mtd
);
297 return part
->master
->_block_isreserved(part
->master
, ofs
);
300 static int part_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
302 struct mtd_part
*part
= mtd_to_part(mtd
);
304 return part
->master
->_block_isbad(part
->master
, ofs
);
307 static int part_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
309 struct mtd_part
*part
= mtd_to_part(mtd
);
313 res
= part
->master
->_block_markbad(part
->master
, ofs
);
315 mtd
->ecc_stats
.badblocks
++;
319 static int part_get_device(struct mtd_info
*mtd
)
321 struct mtd_part
*part
= mtd_to_part(mtd
);
322 return part
->master
->_get_device(part
->master
);
325 static void part_put_device(struct mtd_info
*mtd
)
327 struct mtd_part
*part
= mtd_to_part(mtd
);
328 part
->master
->_put_device(part
->master
);
331 static int part_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
332 struct mtd_oob_region
*oobregion
)
334 struct mtd_part
*part
= mtd_to_part(mtd
);
336 return mtd_ooblayout_ecc(part
->master
, section
, oobregion
);
339 static int part_ooblayout_free(struct mtd_info
*mtd
, int section
,
340 struct mtd_oob_region
*oobregion
)
342 struct mtd_part
*part
= mtd_to_part(mtd
);
344 return mtd_ooblayout_free(part
->master
, section
, oobregion
);
347 static const struct mtd_ooblayout_ops part_ooblayout_ops
= {
348 .ecc
= part_ooblayout_ecc
,
349 .free
= part_ooblayout_free
,
352 static int part_max_bad_blocks(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
354 struct mtd_part
*part
= mtd_to_part(mtd
);
356 return part
->master
->_max_bad_blocks(part
->master
,
357 ofs
+ part
->offset
, len
);
360 static inline void free_partition(struct mtd_part
*p
)
367 * This function unregisters and destroy all slave MTD objects which are
368 * attached to the given master MTD object.
371 int del_mtd_partitions(struct mtd_info
*master
)
373 struct mtd_part
*slave
, *next
;
376 mutex_lock(&mtd_partitions_mutex
);
377 list_for_each_entry_safe(slave
, next
, &mtd_partitions
, list
)
378 if (slave
->master
== master
) {
379 ret
= del_mtd_device(&slave
->mtd
);
384 list_del(&slave
->list
);
385 free_partition(slave
);
387 mutex_unlock(&mtd_partitions_mutex
);
392 static struct mtd_part
*allocate_partition(struct mtd_info
*master
,
393 const struct mtd_partition
*part
, int partno
,
396 struct mtd_part
*slave
;
399 /* allocate the partition structure */
400 slave
= kzalloc(sizeof(*slave
), GFP_KERNEL
);
401 name
= kstrdup(part
->name
, GFP_KERNEL
);
402 if (!name
|| !slave
) {
403 printk(KERN_ERR
"memory allocation error while creating partitions for \"%s
\"\n",
407 return ERR_PTR(-ENOMEM);
410 /* set up the MTD object for this partition */
411 slave->mtd.type = master->type;
412 slave->mtd.flags = master->flags & ~part->mask_flags;
413 slave->mtd.size = part->size;
414 slave->mtd.writesize = master->writesize;
415 slave->mtd.writebufsize = master->writebufsize;
416 slave->mtd.oobsize = master->oobsize;
417 slave->mtd.oobavail = master->oobavail;
418 slave->mtd.subpage_sft = master->subpage_sft;
419 slave->mtd.pairing = master->pairing;
421 slave->mtd.name = name;
422 slave->mtd.owner = master->owner;
424 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
425 * concern for showing the same data in multiple partitions.
426 * However, it is very useful to have the master node present,
427 * so the MTD_PARTITIONED_MASTER option allows that. The master
428 * will have device nodes etc only if this is set, so make the
429 * parent conditional on that option. Note, this is a way to
430 * distinguish between the master and the partition in sysfs.
432 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) ?
435 slave->mtd.dev.of_node = part->of_node;
437 slave->mtd._read = part_read;
438 slave->mtd._write = part_write;
440 if (master->_panic_write)
441 slave->mtd._panic_write = part_panic_write;
443 if (master->_point && master->_unpoint) {
444 slave->mtd._point = part_point;
445 slave->mtd._unpoint = part_unpoint;
448 if (master->_get_unmapped_area)
449 slave->mtd._get_unmapped_area = part_get_unmapped_area;
450 if (master->_read_oob)
451 slave->mtd._read_oob = part_read_oob;
452 if (master->_write_oob)
453 slave->mtd._write_oob = part_write_oob;
454 if (master->_read_user_prot_reg)
455 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
456 if (master->_read_fact_prot_reg)
457 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
458 if (master->_write_user_prot_reg)
459 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
460 if (master->_lock_user_prot_reg)
461 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
462 if (master->_get_user_prot_info)
463 slave->mtd._get_user_prot_info = part_get_user_prot_info;
464 if (master->_get_fact_prot_info)
465 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
467 slave->mtd._sync = part_sync;
468 if (!partno && !master->dev.class && master->_suspend &&
470 slave->mtd._suspend = part_suspend;
471 slave->mtd._resume = part_resume;
474 slave->mtd._writev = part_writev;
476 slave->mtd._lock = part_lock;
478 slave->mtd._unlock = part_unlock;
479 if (master->_is_locked)
480 slave->mtd._is_locked = part_is_locked;
481 if (master->_block_isreserved)
482 slave->mtd._block_isreserved = part_block_isreserved;
483 if (master->_block_isbad)
484 slave->mtd._block_isbad = part_block_isbad;
485 if (master->_block_markbad)
486 slave->mtd._block_markbad = part_block_markbad;
487 if (master->_max_bad_blocks)
488 slave->mtd._max_bad_blocks = part_max_bad_blocks;
490 if (master->_get_device)
491 slave->mtd._get_device = part_get_device;
492 if (master->_put_device)
493 slave->mtd._put_device = part_put_device;
495 slave->mtd._erase = part_erase;
496 slave->master = master;
497 slave->offset = part->offset;
499 if (slave->offset == MTDPART_OFS_APPEND)
500 slave->offset = cur_offset;
501 if (slave->offset == MTDPART_OFS_NXTBLK) {
502 slave->offset = cur_offset;
503 if (mtd_mod_by_eb(cur_offset, master) != 0) {
504 /* Round up to next erasesize */
505 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
506 printk(KERN_NOTICE "Moving partition
%d
: "
507 "0x
%012llx
-> 0x
%012llx
\n", partno,
508 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
511 if (slave->offset == MTDPART_OFS_RETAIN) {
512 slave->offset = cur_offset;
513 if (master->size - slave->offset >= slave->mtd.size) {
514 slave->mtd.size = master->size - slave->offset
517 printk(KERN_ERR "mtd partition
\"%s
\" doesn
't have enough space: %#llx < %#llx, disabled\n",
518 part->name, master->size - slave->offset,
520 /* register to preserve ordering */
524 if (slave->mtd.size == MTDPART_SIZ_FULL)
525 slave->mtd.size = master->size - slave->offset;
527 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
528 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
530 /* let's
do some sanity checks */
531 if (slave
->offset
>= master
->size
) {
532 /* let's register it anyway to preserve ordering */
535 printk(KERN_ERR
"mtd: partition \"%s
\" is out of reach
-- disabled
\n",
539 if (slave->offset + slave->mtd.size > master->size) {
540 slave->mtd.size = master->size - slave->offset;
541 printk(KERN_WARNING"mtd
: partition
\"%s
\" extends beyond the end of device
\"%s
\" -- size truncated to
%#llx\n",
542 part->name, master->name, (unsigned long long)slave->mtd.size);
544 if (master->numeraseregions > 1) {
545 /* Deal with variable erase size stuff */
546 int i, max = master->numeraseregions;
547 u64 end = slave->offset + slave->mtd.size;
548 struct mtd_erase_region_info *regions = master->eraseregions;
550 /* Find the first erase regions which is part of this
552 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
554 /* The loop searched for the region _behind_ the first one */
558 /* Pick biggest erasesize */
559 for (; i < max && regions[i].offset < end; i++) {
560 if (slave->mtd.erasesize < regions[i].erasesize) {
561 slave->mtd.erasesize = regions[i].erasesize;
564 BUG_ON(slave->mtd.erasesize == 0);
566 /* Single erase size */
567 slave->mtd.erasesize = master->erasesize;
570 if ((slave->mtd.flags & MTD_WRITEABLE) &&
571 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
572 /* Doesn't start on a boundary of major erase size */
573 /* FIXME: Let it be writable if it is on a boundary of
574 * _minor_ erase size though */
575 slave->mtd.flags &= ~MTD_WRITEABLE;
576 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
579 if ((slave->mtd.flags & MTD_WRITEABLE) &&
580 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
581 slave->mtd.flags &= ~MTD_WRITEABLE;
582 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
586 mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
587 slave->mtd.ecc_step_size = master->ecc_step_size;
588 slave->mtd.ecc_strength = master->ecc_strength;
589 slave->mtd.bitflip_threshold = master->bitflip_threshold;
591 if (master->_block_isbad) {
594 while (offs < slave->mtd.size) {
595 if (mtd_block_isreserved(master, offs + slave->offset))
596 slave->mtd.ecc_stats.bbtblocks++;
597 else if (mtd_block_isbad(master, offs + slave->offset))
598 slave->mtd.ecc_stats.badblocks++;
599 offs += slave->mtd.erasesize;
607 static ssize_t mtd_partition_offset_show(struct device *dev,
608 struct device_attribute *attr, char *buf)
610 struct mtd_info *mtd = dev_get_drvdata(dev);
611 struct mtd_part *part = mtd_to_part(mtd);
612 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
615 static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
617 static const struct attribute *mtd_partition_attrs[] = {
618 &dev_attr_offset.attr,
622 static int mtd_add_partition_attrs(struct mtd_part *new)
624 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
627 "mtd: failed to create partition attrs, err=%d\n", ret);
631 int mtd_add_partition(struct mtd_info *master, const char *name,
632 long long offset, long long length)
634 struct mtd_partition part;
635 struct mtd_part *new;
638 /* the direct offset is expected */
639 if (offset == MTDPART_OFS_APPEND ||
640 offset == MTDPART_OFS_NXTBLK)
643 if (length == MTDPART_SIZ_FULL)
644 length = master->size - offset;
649 memset(&part, 0, sizeof(part));
652 part.offset = offset;
654 new = allocate_partition(master, &part, -1, offset);
658 mutex_lock(&mtd_partitions_mutex);
659 list_add(&new->list, &mtd_partitions);
660 mutex_unlock(&mtd_partitions_mutex);
662 add_mtd_device(&new->mtd);
664 mtd_add_partition_attrs(new);
668 EXPORT_SYMBOL_GPL(mtd_add_partition);
670 int mtd_del_partition(struct mtd_info *master, int partno)
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->master == master) &&
678 (slave->mtd.index == partno)) {
679 sysfs_remove_files(&slave->mtd.dev.kobj,
680 mtd_partition_attrs);
681 ret = del_mtd_device(&slave->mtd);
685 list_del(&slave->list);
686 free_partition(slave);
689 mutex_unlock(&mtd_partitions_mutex);
693 EXPORT_SYMBOL_GPL(mtd_del_partition);
696 * This function, given a master MTD object and a partition table, creates
697 * and registers slave MTD objects which are bound to the master according to
698 * the partition definitions.
700 * For historical reasons, this function's caller only registers the master
701 * if the MTD_PARTITIONED_MASTER config option is set.
704 int add_mtd_partitions(struct mtd_info *master,
705 const struct mtd_partition *parts,
708 struct mtd_part *slave;
709 uint64_t cur_offset = 0;
712 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
714 for (i = 0; i < nbparts; i++) {
715 slave = allocate_partition(master, parts + i, i, cur_offset);
717 del_mtd_partitions(master);
718 return PTR_ERR(slave);
721 mutex_lock(&mtd_partitions_mutex);
722 list_add(&slave->list, &mtd_partitions);
723 mutex_unlock(&mtd_partitions_mutex);
725 add_mtd_device(&slave->mtd);
726 mtd_add_partition_attrs(slave);
728 cur_offset = slave->offset + slave->mtd.size;
734 static DEFINE_SPINLOCK(part_parser_lock);
735 static LIST_HEAD(part_parsers);
737 static struct mtd_part_parser *mtd_part_parser_get(const char *name)
739 struct mtd_part_parser *p, *ret = NULL;
741 spin_lock(&part_parser_lock);
743 list_for_each_entry(p, &part_parsers, list)
744 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
749 spin_unlock(&part_parser_lock);
754 static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
756 module_put(p->owner);
760 * Many partition parsers just expected the core to kfree() all their data in
761 * one chunk. Do that by default.
763 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
769 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
774 p->cleanup = &mtd_part_parser_cleanup_default;
776 spin_lock(&part_parser_lock);
777 list_add(&p->list, &part_parsers);
778 spin_unlock(&part_parser_lock);
782 EXPORT_SYMBOL_GPL(__register_mtd_parser);
784 void deregister_mtd_parser(struct mtd_part_parser *p)
786 spin_lock(&part_parser_lock);
788 spin_unlock(&part_parser_lock);
790 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
793 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
794 * are changing this array!
796 static const char * const default_mtd_part_types[] = {
803 * parse_mtd_partitions - parse MTD partitions
804 * @master: the master partition (describes whole MTD device)
805 * @types: names of partition parsers to try or %NULL
806 * @pparts: info about partitions found is returned here
807 * @data: MTD partition parser-specific data
809 * This function tries to find partition on MTD device @master. It uses MTD
810 * partition parsers, specified in @types. However, if @types is %NULL, then
811 * the default list of parsers is used. The default list contains only the
812 * "cmdlinepart" and "ofpart" parsers ATM.
813 * Note: If there are more then one parser in @types, the kernel only takes the
814 * partitions parsed out by the first parser.
816 * This function may return:
817 * o a negative error code in case of failure
818 * o zero otherwise, and @pparts will describe the partitions, number of
819 * partitions, and the parser which parsed them. Caller must release
820 * resources with mtd_part_parser_cleanup() when finished with the returned
823 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
824 struct mtd_partitions *pparts,
825 struct mtd_part_parser_data *data)
827 struct mtd_part_parser *parser;
831 types = default_mtd_part_types;
833 for ( ; *types; types++) {
834 pr_debug("%s: parsing partitions %s\n", master->name, *types);
835 parser = mtd_part_parser_get(*types);
836 if (!parser && !request_module("%s", *types))
837 parser = mtd_part_parser_get(*types);
838 pr_debug("%s: got parser %s\n", master->name,
839 parser ? parser->name : NULL);
842 ret = (*parser->parse_fn)(master, &pparts->parts, data);
843 pr_debug("%s: parser %s: %i\n",
844 master->name, parser->name, ret);
846 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
847 ret, parser->name, master->name);
848 pparts->nr_parts = ret;
849 pparts->parser = parser;
852 mtd_part_parser_put(parser);
854 * Stash the first error we see; only report it if no parser
863 void mtd_part_parser_cleanup(struct mtd_partitions *parts)
865 const struct mtd_part_parser *parser;
870 parser = parts->parser;
873 parser->cleanup(parts->parts, parts->nr_parts);
875 mtd_part_parser_put(parser);
879 int mtd_is_partition(const struct mtd_info *mtd)
881 struct mtd_part *part;
884 mutex_lock(&mtd_partitions_mutex);
885 list_for_each_entry(part, &mtd_partitions, list)
886 if (&part->mtd == mtd) {
890 mutex_unlock(&mtd_partitions_mutex);
894 EXPORT_SYMBOL_GPL(mtd_is_partition);
896 /* Returns the size of the entire flash chip */
897 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
899 if (!mtd_is_partition(mtd))
902 return mtd_to_part(mtd)->master->size;
904 EXPORT_SYMBOL_GPL(mtd_get_device_size);