1 // SPDX-License-Identifier: GPL-2.0+
3 * Simple MTD partitioning layer
5 * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
6 * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
7 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
12 #include <dm/devres.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/kmod.h>
23 #include <linux/errno.h>
24 #include <linux/compat.h>
25 #include <ubi_uboot.h>
27 #include <linux/mtd/mtd.h>
28 #include <linux/mtd/partitions.h>
29 #include <linux/err.h>
30 #include <linux/sizes.h>
35 static DEFINE_MUTEX(mtd_partitions_mutex);
37 DEFINE_MUTEX(mtd_partitions_mutex);
44 * kstrdup - allocate space for and copy an existing string
45 * @s: the string to duplicate
46 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
48 char *kstrdup(const char *s, gfp_t gfp)
57 buf = kmalloc(len, gfp);
64 #define MTD_SIZE_REMAINING (~0LLU)
65 #define MTD_OFFSET_NOT_SPECIFIED (~0LLU)
67 bool mtd_partitions_used(struct mtd_info *master)
69 struct mtd_info *slave;
71 list_for_each_entry(slave, &master->partitions, node) {
80 * mtd_parse_partition - Parse @mtdparts partition definition, fill @partition
81 * with it and update the @mtdparts string pointer.
83 * The partition name is allocated and must be freed by the caller.
85 * This function is widely inspired from part_parse (mtdparts.c).
87 * @mtdparts: String describing the partition with mtdparts command syntax
88 * @partition: MTD partition structure to fill
90 * @return 0 on success, an error otherwise.
92 static int mtd_parse_partition(const char **_mtdparts,
93 struct mtd_partition *partition)
95 const char *mtdparts = *_mtdparts;
96 const char *name = NULL;
100 /* Ensure the partition structure is empty */
101 memset(partition, 0, sizeof(struct mtd_partition));
103 /* Fetch the partition size */
104 if (*mtdparts == '-') {
105 /* Assign all remaining space to this partition */
106 partition->size = MTD_SIZE_REMAINING;
109 partition->size = ustrtoull(mtdparts, (char **)&mtdparts, 0);
110 if (partition->size < SZ_4K) {
111 printf("Minimum partition size 4kiB, %lldB requested\n",
117 /* Check for the offset */
118 partition->offset = MTD_OFFSET_NOT_SPECIFIED;
119 if (*mtdparts == '@') {
121 partition->offset = ustrtoull(mtdparts, (char **)&mtdparts, 0);
124 /* Now look for the name */
125 if (*mtdparts == '(') {
127 mtdparts = strchr(name, ')');
129 printf("No closing ')' found in partition name\n");
132 name_len = mtdparts - name + 1;
133 if ((name_len - 1) == 0) {
134 printf("Empty partition name\n");
139 /* Name will be of the form size@offset */
143 /* Check if the partition is read-only */
144 if (strncmp(mtdparts, "ro", 2) == 0) {
145 partition->mask_flags |= MTD_WRITEABLE;
149 /* Check for a potential next partition definition */
150 if (*mtdparts == ',') {
151 if (partition->size == MTD_SIZE_REMAINING) {
152 printf("No partitions allowed after a fill-up\n");
156 } else if ((*mtdparts == ';') || (*mtdparts == '\0')) {
159 printf("Unexpected character '%c' in mtdparts\n", *mtdparts);
164 * Allocate a buffer for the name and either copy the provided name or
165 * auto-generate it with the form 'size@offset'.
167 buf = malloc(name_len);
172 strncpy(buf, name, name_len - 1);
174 snprintf(buf, name_len, "0x%08llx@0x%08llx",
175 partition->size, partition->offset);
177 buf[name_len - 1] = '\0';
178 partition->name = buf;
180 *_mtdparts = mtdparts;
186 * mtd_parse_partitions - Create a partition array from an mtdparts definition
188 * Stateless function that takes a @parent MTD device, a string @_mtdparts
189 * describing the partitions (with the "mtdparts" command syntax) and creates
190 * the corresponding MTD partition structure array @_parts. Both the name and
191 * the structure partition itself must be freed freed, the caller may use
192 * @mtd_free_parsed_partitions() for this purpose.
194 * @parent: MTD device which contains the partitions
195 * @_mtdparts: Pointer to a string describing the partitions with "mtdparts"
197 * @_parts: Allocated array containing the partitions, must be freed by the
199 * @_nparts: Size of @_parts array.
201 * @return 0 on success, an error otherwise.
203 int mtd_parse_partitions(struct mtd_info *parent, const char **_mtdparts,
204 struct mtd_partition **_parts, int *_nparts)
206 struct mtd_partition partition = {}, *parts;
207 const char *mtdparts = *_mtdparts;
208 int cur_off = 0, cur_sz = 0;
213 /* First, iterate over the partitions until we know their number */
214 while (mtdparts[0] != '\0' && mtdparts[0] != ';') {
215 ret = mtd_parse_partition(&mtdparts, &partition);
219 free((char *)partition.name);
223 /* Allocate an array of partitions to give back to the caller */
224 parts = malloc(sizeof(*parts) * nparts);
226 printf("Not enough space to save partitions meta-data\n");
230 /* Iterate again over each partition to save the data in our array */
231 for (idx = 0; idx < nparts; idx++) {
232 ret = mtd_parse_partition(_mtdparts, &parts[idx]);
236 if (parts[idx].size == MTD_SIZE_REMAINING)
237 parts[idx].size = parent->size - cur_sz;
238 cur_sz += parts[idx].size;
240 sz = parts[idx].size;
241 if (sz < parent->writesize || do_div(sz, parent->writesize)) {
242 printf("Partition size must be a multiple of %d\n",
247 if (parts[idx].offset == MTD_OFFSET_NOT_SPECIFIED)
248 parts[idx].offset = cur_off;
249 cur_off += parts[idx].size;
251 parts[idx].ecclayout = parent->ecclayout;
254 /* Offset by one mtdparts to point to the next device if any */
255 if (*_mtdparts[0] == ';')
265 * mtd_free_parsed_partitions - Free dynamically allocated partitions
267 * Each successful call to @mtd_parse_partitions must be followed by a call to
268 * @mtd_free_parsed_partitions to free any allocated array during the parsing
271 * @parts: Array containing the partitions that will be freed.
272 * @nparts: Size of @parts array.
274 void mtd_free_parsed_partitions(struct mtd_partition *parts,
279 for (i = 0; i < nparts; i++)
280 free((char *)parts[i].name);
286 * MTD methods which simply translate the effective address and pass through
287 * to the _real_ device.
290 static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
291 size_t *retlen, u_char *buf)
293 struct mtd_ecc_stats stats;
296 stats = mtd->parent->ecc_stats;
297 res = mtd->parent->_read(mtd->parent, from + mtd->offset, len,
299 if (unlikely(mtd_is_eccerr(res)))
300 mtd->ecc_stats.failed +=
301 mtd->parent->ecc_stats.failed - stats.failed;
303 mtd->ecc_stats.corrected +=
304 mtd->parent->ecc_stats.corrected - stats.corrected;
309 static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
310 size_t *retlen, void **virt, resource_size_t *phys)
312 return mtd->parent->_point(mtd->parent, from + mtd->offset, len,
316 static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
318 return mtd->parent->_unpoint(mtd->parent, from + mtd->offset, len);
322 static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
324 unsigned long offset,
327 offset += mtd->offset;
328 return mtd->parent->_get_unmapped_area(mtd->parent, len, offset, flags);
331 static int part_read_oob(struct mtd_info *mtd, loff_t from,
332 struct mtd_oob_ops *ops)
336 if (from >= mtd->size)
338 if (ops->datbuf && from + ops->len > mtd->size)
342 * If OOB is also requested, make sure that we do not read past the end
348 if (ops->mode == MTD_OPS_AUTO_OOB)
352 pages = mtd_div_by_ws(mtd->size, mtd);
353 pages -= mtd_div_by_ws(from, mtd);
354 if (ops->ooboffs + ops->ooblen > pages * len)
358 res = mtd->parent->_read_oob(mtd->parent, from + mtd->offset, ops);
360 if (mtd_is_bitflip(res))
361 mtd->ecc_stats.corrected++;
362 if (mtd_is_eccerr(res))
363 mtd->ecc_stats.failed++;
368 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
369 size_t len, size_t *retlen, u_char *buf)
371 return mtd->parent->_read_user_prot_reg(mtd->parent, from, len,
375 static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
376 size_t *retlen, struct otp_info *buf)
378 return mtd->parent->_get_user_prot_info(mtd->parent, len, retlen,
382 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
383 size_t len, size_t *retlen, u_char *buf)
385 return mtd->parent->_read_fact_prot_reg(mtd->parent, from, len,
389 static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
390 size_t *retlen, struct otp_info *buf)
392 return mtd->parent->_get_fact_prot_info(mtd->parent, len, retlen,
396 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
397 size_t *retlen, const u_char *buf)
399 return mtd->parent->_write(mtd->parent, to + mtd->offset, len,
403 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
404 size_t *retlen, const u_char *buf)
406 return mtd->parent->_panic_write(mtd->parent, to + mtd->offset, len,
410 static int part_write_oob(struct mtd_info *mtd, loff_t to,
411 struct mtd_oob_ops *ops)
415 if (ops->datbuf && to + ops->len > mtd->size)
417 return mtd->parent->_write_oob(mtd->parent, to + mtd->offset, ops);
420 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
421 size_t len, size_t *retlen, u_char *buf)
423 return mtd->parent->_write_user_prot_reg(mtd->parent, from, len,
427 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
430 return mtd->parent->_lock_user_prot_reg(mtd->parent, from, len);
434 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
435 unsigned long count, loff_t to, size_t *retlen)
437 return mtd->parent->_writev(mtd->parent, vecs, count,
438 to + mtd->offset, retlen);
442 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
446 instr->addr += mtd->offset;
447 ret = mtd->parent->_erase(mtd->parent, instr);
449 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
450 instr->fail_addr -= mtd->offset;
451 instr->addr -= mtd->offset;
456 void mtd_erase_callback(struct erase_info *instr)
458 if (instr->mtd->_erase == part_erase) {
459 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
460 instr->fail_addr -= instr->mtd->offset;
461 instr->addr -= instr->mtd->offset;
464 instr->callback(instr);
466 EXPORT_SYMBOL_GPL(mtd_erase_callback);
468 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
470 return mtd->parent->_lock(mtd->parent, ofs + mtd->offset, len);
473 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
475 return mtd->parent->_unlock(mtd->parent, ofs + mtd->offset, len);
478 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
480 return mtd->parent->_is_locked(mtd->parent, ofs + mtd->offset, len);
483 static void part_sync(struct mtd_info *mtd)
485 mtd->parent->_sync(mtd->parent);
489 static int part_suspend(struct mtd_info *mtd)
491 return mtd->parent->_suspend(mtd->parent);
494 static void part_resume(struct mtd_info *mtd)
496 mtd->parent->_resume(mtd->parent);
500 static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
503 return mtd->parent->_block_isreserved(mtd->parent, ofs);
506 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
509 return mtd->parent->_block_isbad(mtd->parent, ofs);
512 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
517 res = mtd->parent->_block_markbad(mtd->parent, ofs);
519 mtd->ecc_stats.badblocks++;
523 static inline void free_partition(struct mtd_info *p)
530 * This function unregisters and destroy all slave MTD objects which are
531 * attached to the given master MTD object, recursively.
533 static int do_del_mtd_partitions(struct mtd_info *master)
535 struct mtd_info *slave, *next;
538 list_for_each_entry_safe(slave, next, &master->partitions, node) {
539 if (mtd_has_partitions(slave))
540 del_mtd_partitions(slave);
542 debug("Deleting %s MTD partition\n", slave->name);
543 ret = del_mtd_device(slave);
545 printf("Error when deleting partition \"%s\" (%d)\n",
551 list_del(&slave->node);
552 free_partition(slave);
558 int del_mtd_partitions(struct mtd_info *master)
562 debug("Deleting MTD partitions on \"%s\":\n", master->name);
564 mutex_lock(&mtd_partitions_mutex);
565 ret = do_del_mtd_partitions(master);
566 mutex_unlock(&mtd_partitions_mutex);
571 static struct mtd_info *allocate_partition(struct mtd_info *master,
572 const struct mtd_partition *part,
573 int partno, uint64_t cur_offset)
575 struct mtd_info *slave;
578 /* allocate the partition structure */
579 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
580 name = kstrdup(part->name, GFP_KERNEL);
581 if (!name || !slave) {
582 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
586 return ERR_PTR(-ENOMEM);
589 /* set up the MTD object for this partition */
590 slave->type = master->type;
591 slave->flags = master->flags & ~part->mask_flags;
592 slave->size = part->size;
593 slave->writesize = master->writesize;
594 slave->writebufsize = master->writebufsize;
595 slave->oobsize = master->oobsize;
596 slave->oobavail = master->oobavail;
597 slave->subpage_sft = master->subpage_sft;
600 slave->owner = master->owner;
602 slave->backing_dev_info = master->backing_dev_info;
604 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
605 * to have the same data be in two different partitions.
607 slave->dev.parent = master->dev.parent;
611 slave->_read = part_read;
613 slave->_write = part_write;
615 if (master->_panic_write)
616 slave->_panic_write = part_panic_write;
619 if (master->_point && master->_unpoint) {
620 slave->_point = part_point;
621 slave->_unpoint = part_unpoint;
625 if (master->_get_unmapped_area)
626 slave->_get_unmapped_area = part_get_unmapped_area;
627 if (master->_read_oob)
628 slave->_read_oob = part_read_oob;
629 if (master->_write_oob)
630 slave->_write_oob = part_write_oob;
631 if (master->_read_user_prot_reg)
632 slave->_read_user_prot_reg = part_read_user_prot_reg;
633 if (master->_read_fact_prot_reg)
634 slave->_read_fact_prot_reg = part_read_fact_prot_reg;
635 if (master->_write_user_prot_reg)
636 slave->_write_user_prot_reg = part_write_user_prot_reg;
637 if (master->_lock_user_prot_reg)
638 slave->_lock_user_prot_reg = part_lock_user_prot_reg;
639 if (master->_get_user_prot_info)
640 slave->_get_user_prot_info = part_get_user_prot_info;
641 if (master->_get_fact_prot_info)
642 slave->_get_fact_prot_info = part_get_fact_prot_info;
644 slave->_sync = part_sync;
646 if (!partno && !master->dev.class && master->_suspend &&
648 slave->_suspend = part_suspend;
649 slave->_resume = part_resume;
652 slave->_writev = part_writev;
655 slave->_lock = part_lock;
657 slave->_unlock = part_unlock;
658 if (master->_is_locked)
659 slave->_is_locked = part_is_locked;
660 if (master->_block_isreserved)
661 slave->_block_isreserved = part_block_isreserved;
662 if (master->_block_isbad)
663 slave->_block_isbad = part_block_isbad;
664 if (master->_block_markbad)
665 slave->_block_markbad = part_block_markbad;
666 slave->_erase = part_erase;
667 slave->parent = master;
668 slave->offset = part->offset;
669 INIT_LIST_HEAD(&slave->partitions);
670 INIT_LIST_HEAD(&slave->node);
672 if (slave->offset == MTDPART_OFS_APPEND)
673 slave->offset = cur_offset;
674 if (slave->offset == MTDPART_OFS_NXTBLK) {
675 slave->offset = cur_offset;
676 if (mtd_mod_by_eb(cur_offset, master) != 0) {
677 /* Round up to next erasesize */
678 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
679 debug("Moving partition %d: "
680 "0x%012llx -> 0x%012llx\n", partno,
681 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
684 if (slave->offset == MTDPART_OFS_RETAIN) {
685 slave->offset = cur_offset;
686 if (master->size - slave->offset >= slave->size) {
687 slave->size = master->size - slave->offset
690 debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
691 part->name, master->size - slave->offset,
693 /* register to preserve ordering */
697 if (slave->size == MTDPART_SIZ_FULL)
698 slave->size = master->size - slave->offset;
700 debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
701 (unsigned long long)(slave->offset + slave->size), slave->name);
703 /* let's do some sanity checks */
704 if (slave->offset >= master->size) {
705 /* let's register it anyway to preserve ordering */
708 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
712 if (slave->offset + slave->size > master->size) {
713 slave->size = master->size - slave->offset;
714 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
715 part->name, master->name, slave->size);
717 if (master->numeraseregions > 1) {
718 /* Deal with variable erase size stuff */
719 int i, max = master->numeraseregions;
720 u64 end = slave->offset + slave->size;
721 struct mtd_erase_region_info *regions = master->eraseregions;
723 /* Find the first erase regions which is part of this
725 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
727 /* The loop searched for the region _behind_ the first one */
731 /* Pick biggest erasesize */
732 for (; i < max && regions[i].offset < end; i++) {
733 if (slave->erasesize < regions[i].erasesize)
734 slave->erasesize = regions[i].erasesize;
736 WARN_ON(slave->erasesize == 0);
738 /* Single erase size */
739 slave->erasesize = master->erasesize;
742 if ((slave->flags & MTD_WRITEABLE) &&
743 mtd_mod_by_eb(slave->offset, slave)) {
744 /* Doesn't start on a boundary of major erase size */
745 /* FIXME: Let it be writable if it is on a boundary of
746 * _minor_ erase size though */
747 slave->flags &= ~MTD_WRITEABLE;
748 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
751 if ((slave->flags & MTD_WRITEABLE) &&
752 mtd_mod_by_eb(slave->size, slave)) {
753 slave->flags &= ~MTD_WRITEABLE;
754 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
758 slave->ecclayout = master->ecclayout;
759 slave->ecc_step_size = master->ecc_step_size;
760 slave->ecc_strength = master->ecc_strength;
761 slave->bitflip_threshold = master->bitflip_threshold;
763 if (master->_block_isbad) {
766 while (offs < slave->size) {
767 if (mtd_block_isbad(master, offs + slave->offset))
768 slave->ecc_stats.badblocks++;
769 offs += slave->erasesize;
778 int mtd_add_partition(struct mtd_info *master, const char *name,
779 long long offset, long long length)
781 struct mtd_partition part;
782 struct mtd_info *p, *new;
786 /* the direct offset is expected */
787 if (offset == MTDPART_OFS_APPEND ||
788 offset == MTDPART_OFS_NXTBLK)
791 if (length == MTDPART_SIZ_FULL)
792 length = master->size - offset;
799 part.offset = offset;
801 part.ecclayout = NULL;
803 new = allocate_partition(master, &part, -1, offset);
808 end = offset + length;
810 mutex_lock(&mtd_partitions_mutex);
811 list_for_each_entry(p, &master->partitions, node) {
812 if (start >= p->offset &&
813 (start < (p->offset + p->size)))
816 if (end >= p->offset &&
817 (end < (p->offset + p->size)))
821 list_add_tail(&new->node, &master->partitions);
822 mutex_unlock(&mtd_partitions_mutex);
828 mutex_unlock(&mtd_partitions_mutex);
832 EXPORT_SYMBOL_GPL(mtd_add_partition);
834 int mtd_del_partition(struct mtd_info *master, int partno)
836 struct mtd_info *slave, *next;
839 mutex_lock(&mtd_partitions_mutex);
840 list_for_each_entry_safe(slave, next, &master->partitions, node)
841 if (slave->index == partno) {
842 ret = del_mtd_device(slave);
846 list_del(&slave->node);
847 free_partition(slave);
850 mutex_unlock(&mtd_partitions_mutex);
854 EXPORT_SYMBOL_GPL(mtd_del_partition);
858 * This function, given a master MTD object and a partition table, creates
859 * and registers slave MTD objects which are bound to the master according to
860 * the partition definitions.
862 * We don't register the master, or expect the caller to have done so,
863 * for reasons of data integrity.
866 int add_mtd_partitions(struct mtd_info *master,
867 const struct mtd_partition *parts,
870 struct mtd_info *slave;
871 uint64_t cur_offset = 0;
874 debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
876 for (i = 0; i < nbparts; i++) {
877 slave = allocate_partition(master, parts + i, i, cur_offset);
879 return PTR_ERR(slave);
881 mutex_lock(&mtd_partitions_mutex);
882 list_add_tail(&slave->node, &master->partitions);
883 mutex_unlock(&mtd_partitions_mutex);
885 add_mtd_device(slave);
887 cur_offset = slave->offset + slave->size;
894 static DEFINE_SPINLOCK(part_parser_lock);
895 static LIST_HEAD(part_parsers);
897 static struct mtd_part_parser *get_partition_parser(const char *name)
899 struct mtd_part_parser *p, *ret = NULL;
901 spin_lock(&part_parser_lock);
903 list_for_each_entry(p, &part_parsers, list)
904 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
909 spin_unlock(&part_parser_lock);
914 #define put_partition_parser(p) do { module_put((p)->owner); } while (0)
916 void register_mtd_parser(struct mtd_part_parser *p)
918 spin_lock(&part_parser_lock);
919 list_add(&p->list, &part_parsers);
920 spin_unlock(&part_parser_lock);
922 EXPORT_SYMBOL_GPL(register_mtd_parser);
924 void deregister_mtd_parser(struct mtd_part_parser *p)
926 spin_lock(&part_parser_lock);
928 spin_unlock(&part_parser_lock);
930 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
933 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
934 * are changing this array!
936 static const char * const default_mtd_part_types[] = {
943 * parse_mtd_partitions - parse MTD partitions
944 * @master: the master partition (describes whole MTD device)
945 * @types: names of partition parsers to try or %NULL
946 * @pparts: array of partitions found is returned here
947 * @data: MTD partition parser-specific data
949 * This function tries to find partition on MTD device @master. It uses MTD
950 * partition parsers, specified in @types. However, if @types is %NULL, then
951 * the default list of parsers is used. The default list contains only the
952 * "cmdlinepart" and "ofpart" parsers ATM.
953 * Note: If there are more then one parser in @types, the kernel only takes the
954 * partitions parsed out by the first parser.
956 * This function may return:
957 * o a negative error code in case of failure
958 * o zero if no partitions were found
959 * o a positive number of found partitions, in which case on exit @pparts will
960 * point to an array containing this number of &struct mtd_info objects.
962 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
963 struct mtd_partition **pparts,
964 struct mtd_part_parser_data *data)
966 struct mtd_part_parser *parser;
970 types = default_mtd_part_types;
972 for ( ; ret <= 0 && *types; types++) {
973 parser = get_partition_parser(*types);
974 if (!parser && !request_module("%s", *types))
975 parser = get_partition_parser(*types);
978 ret = (*parser->parse_fn)(master, pparts, data);
979 put_partition_parser(parser);
981 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
982 ret, parser->name, master->name);
990 /* Returns the size of the entire flash chip */
991 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
993 if (mtd_is_partition(mtd))
994 return mtd->parent->size;
998 EXPORT_SYMBOL_GPL(mtd_get_device_size);