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 <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/list.h>
17 #include <linux/kmod.h>
22 #include <linux/errno.h>
23 #include <linux/compat.h>
24 #include <ubi_uboot.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/partitions.h>
28 #include <linux/err.h>
29 #include <linux/sizes.h>
34 static DEFINE_MUTEX(mtd_partitions_mutex);
36 DEFINE_MUTEX(mtd_partitions_mutex);
43 * kstrdup - allocate space for and copy an existing string
44 * @s: the string to duplicate
45 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
47 char *kstrdup(const char *s, gfp_t gfp)
56 buf = kmalloc(len, gfp);
63 #define MTD_SIZE_REMAINING (~0LLU)
64 #define MTD_OFFSET_NOT_SPECIFIED (~0LLU)
66 bool mtd_partitions_used(struct mtd_info *master)
68 struct mtd_info *slave;
70 list_for_each_entry(slave, &master->partitions, node) {
79 * mtd_parse_partition - Parse @mtdparts partition definition, fill @partition
80 * with it and update the @mtdparts string pointer.
82 * The partition name is allocated and must be freed by the caller.
84 * This function is widely inspired from part_parse (mtdparts.c).
86 * @mtdparts: String describing the partition with mtdparts command syntax
87 * @partition: MTD partition structure to fill
89 * @return 0 on success, an error otherwise.
91 static int mtd_parse_partition(const char **_mtdparts,
92 struct mtd_partition *partition)
94 const char *mtdparts = *_mtdparts;
95 const char *name = NULL;
99 /* Ensure the partition structure is empty */
100 memset(partition, 0, sizeof(struct mtd_partition));
102 /* Fetch the partition size */
103 if (*mtdparts == '-') {
104 /* Assign all remaining space to this partition */
105 partition->size = MTD_SIZE_REMAINING;
108 partition->size = ustrtoull(mtdparts, (char **)&mtdparts, 0);
109 if (partition->size < SZ_4K) {
110 printf("Minimum partition size 4kiB, %lldB requested\n",
116 /* Check for the offset */
117 partition->offset = MTD_OFFSET_NOT_SPECIFIED;
118 if (*mtdparts == '@') {
120 partition->offset = ustrtoull(mtdparts, (char **)&mtdparts, 0);
123 /* Now look for the name */
124 if (*mtdparts == '(') {
126 mtdparts = strchr(name, ')');
128 printf("No closing ')' found in partition name\n");
131 name_len = mtdparts - name + 1;
132 if ((name_len - 1) == 0) {
133 printf("Empty partition name\n");
138 /* Name will be of the form size@offset */
142 /* Check if the partition is read-only */
143 if (strncmp(mtdparts, "ro", 2) == 0) {
144 partition->mask_flags |= MTD_WRITEABLE;
148 /* Check for a potential next partition definition */
149 if (*mtdparts == ',') {
150 if (partition->size == MTD_SIZE_REMAINING) {
151 printf("No partitions allowed after a fill-up\n");
155 } else if ((*mtdparts == ';') || (*mtdparts == '\0')) {
158 printf("Unexpected character '%c' in mtdparts\n", *mtdparts);
163 * Allocate a buffer for the name and either copy the provided name or
164 * auto-generate it with the form 'size@offset'.
166 buf = malloc(name_len);
171 strncpy(buf, name, name_len - 1);
173 snprintf(buf, name_len, "0x%08llx@0x%08llx",
174 partition->size, partition->offset);
176 buf[name_len - 1] = '\0';
177 partition->name = buf;
179 *_mtdparts = mtdparts;
185 * mtd_parse_partitions - Create a partition array from an mtdparts definition
187 * Stateless function that takes a @parent MTD device, a string @_mtdparts
188 * describing the partitions (with the "mtdparts" command syntax) and creates
189 * the corresponding MTD partition structure array @_parts. Both the name and
190 * the structure partition itself must be freed freed, the caller may use
191 * @mtd_free_parsed_partitions() for this purpose.
193 * @parent: MTD device which contains the partitions
194 * @_mtdparts: Pointer to a string describing the partitions with "mtdparts"
196 * @_parts: Allocated array containing the partitions, must be freed by the
198 * @_nparts: Size of @_parts array.
200 * @return 0 on success, an error otherwise.
202 int mtd_parse_partitions(struct mtd_info *parent, const char **_mtdparts,
203 struct mtd_partition **_parts, int *_nparts)
205 struct mtd_partition partition = {}, *parts;
206 const char *mtdparts = *_mtdparts;
207 int cur_off = 0, cur_sz = 0;
212 /* First, iterate over the partitions until we know their number */
213 while (mtdparts[0] != '\0' && mtdparts[0] != ';') {
214 ret = mtd_parse_partition(&mtdparts, &partition);
218 free((char *)partition.name);
222 /* Allocate an array of partitions to give back to the caller */
223 parts = malloc(sizeof(*parts) * nparts);
225 printf("Not enough space to save partitions meta-data\n");
229 /* Iterate again over each partition to save the data in our array */
230 for (idx = 0; idx < nparts; idx++) {
231 ret = mtd_parse_partition(_mtdparts, &parts[idx]);
235 if (parts[idx].size == MTD_SIZE_REMAINING)
236 parts[idx].size = parent->size - cur_sz;
237 cur_sz += parts[idx].size;
239 sz = parts[idx].size;
240 if (sz < parent->writesize || do_div(sz, parent->writesize)) {
241 printf("Partition size must be a multiple of %d\n",
246 if (parts[idx].offset == MTD_OFFSET_NOT_SPECIFIED)
247 parts[idx].offset = cur_off;
248 cur_off += parts[idx].size;
250 parts[idx].ecclayout = parent->ecclayout;
253 /* Offset by one mtdparts to point to the next device if any */
254 if (*_mtdparts[0] == ';')
264 * mtd_free_parsed_partitions - Free dynamically allocated partitions
266 * Each successful call to @mtd_parse_partitions must be followed by a call to
267 * @mtd_free_parsed_partitions to free any allocated array during the parsing
270 * @parts: Array containing the partitions that will be freed.
271 * @nparts: Size of @parts array.
273 void mtd_free_parsed_partitions(struct mtd_partition *parts,
278 for (i = 0; i < nparts; i++)
279 free((char *)parts[i].name);
285 * MTD methods which simply translate the effective address and pass through
286 * to the _real_ device.
289 static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
290 size_t *retlen, u_char *buf)
292 struct mtd_ecc_stats stats;
295 stats = mtd->parent->ecc_stats;
296 res = mtd->parent->_read(mtd->parent, from + mtd->offset, len,
298 if (unlikely(mtd_is_eccerr(res)))
299 mtd->ecc_stats.failed +=
300 mtd->parent->ecc_stats.failed - stats.failed;
302 mtd->ecc_stats.corrected +=
303 mtd->parent->ecc_stats.corrected - stats.corrected;
308 static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
309 size_t *retlen, void **virt, resource_size_t *phys)
311 return mtd->parent->_point(mtd->parent, from + mtd->offset, len,
315 static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
317 return mtd->parent->_unpoint(mtd->parent, from + mtd->offset, len);
321 static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
323 unsigned long offset,
326 offset += mtd->offset;
327 return mtd->parent->_get_unmapped_area(mtd->parent, len, offset, flags);
330 static int part_read_oob(struct mtd_info *mtd, loff_t from,
331 struct mtd_oob_ops *ops)
335 if (from >= mtd->size)
337 if (ops->datbuf && from + ops->len > mtd->size)
341 * If OOB is also requested, make sure that we do not read past the end
347 if (ops->mode == MTD_OPS_AUTO_OOB)
351 pages = mtd_div_by_ws(mtd->size, mtd);
352 pages -= mtd_div_by_ws(from, mtd);
353 if (ops->ooboffs + ops->ooblen > pages * len)
357 res = mtd->parent->_read_oob(mtd->parent, from + mtd->offset, ops);
359 if (mtd_is_bitflip(res))
360 mtd->ecc_stats.corrected++;
361 if (mtd_is_eccerr(res))
362 mtd->ecc_stats.failed++;
367 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
368 size_t len, size_t *retlen, u_char *buf)
370 return mtd->parent->_read_user_prot_reg(mtd->parent, from, len,
374 static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
375 size_t *retlen, struct otp_info *buf)
377 return mtd->parent->_get_user_prot_info(mtd->parent, len, retlen,
381 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
382 size_t len, size_t *retlen, u_char *buf)
384 return mtd->parent->_read_fact_prot_reg(mtd->parent, from, len,
388 static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
389 size_t *retlen, struct otp_info *buf)
391 return mtd->parent->_get_fact_prot_info(mtd->parent, len, retlen,
395 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
396 size_t *retlen, const u_char *buf)
398 return mtd->parent->_write(mtd->parent, to + mtd->offset, len,
402 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
403 size_t *retlen, const u_char *buf)
405 return mtd->parent->_panic_write(mtd->parent, to + mtd->offset, len,
409 static int part_write_oob(struct mtd_info *mtd, loff_t to,
410 struct mtd_oob_ops *ops)
414 if (ops->datbuf && to + ops->len > mtd->size)
416 return mtd->parent->_write_oob(mtd->parent, to + mtd->offset, ops);
419 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
420 size_t len, size_t *retlen, u_char *buf)
422 return mtd->parent->_write_user_prot_reg(mtd->parent, from, len,
426 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
429 return mtd->parent->_lock_user_prot_reg(mtd->parent, from, len);
433 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
434 unsigned long count, loff_t to, size_t *retlen)
436 return mtd->parent->_writev(mtd->parent, vecs, count,
437 to + mtd->offset, retlen);
441 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
445 instr->addr += mtd->offset;
446 ret = mtd->parent->_erase(mtd->parent, instr);
448 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
449 instr->fail_addr -= mtd->offset;
450 instr->addr -= mtd->offset;
455 void mtd_erase_callback(struct erase_info *instr)
457 if (instr->mtd->_erase == part_erase) {
458 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
459 instr->fail_addr -= instr->mtd->offset;
460 instr->addr -= instr->mtd->offset;
463 instr->callback(instr);
465 EXPORT_SYMBOL_GPL(mtd_erase_callback);
467 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
469 return mtd->parent->_lock(mtd->parent, ofs + mtd->offset, len);
472 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
474 return mtd->parent->_unlock(mtd->parent, ofs + mtd->offset, len);
477 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
479 return mtd->parent->_is_locked(mtd->parent, ofs + mtd->offset, len);
482 static void part_sync(struct mtd_info *mtd)
484 mtd->parent->_sync(mtd->parent);
488 static int part_suspend(struct mtd_info *mtd)
490 return mtd->parent->_suspend(mtd->parent);
493 static void part_resume(struct mtd_info *mtd)
495 mtd->parent->_resume(mtd->parent);
499 static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
502 return mtd->parent->_block_isreserved(mtd->parent, ofs);
505 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
508 return mtd->parent->_block_isbad(mtd->parent, ofs);
511 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
516 res = mtd->parent->_block_markbad(mtd->parent, ofs);
518 mtd->ecc_stats.badblocks++;
522 static inline void free_partition(struct mtd_info *p)
529 * This function unregisters and destroy all slave MTD objects which are
530 * attached to the given master MTD object, recursively.
532 static int do_del_mtd_partitions(struct mtd_info *master)
534 struct mtd_info *slave, *next;
537 list_for_each_entry_safe(slave, next, &master->partitions, node) {
538 if (mtd_has_partitions(slave))
539 del_mtd_partitions(slave);
541 debug("Deleting %s MTD partition\n", slave->name);
542 ret = del_mtd_device(slave);
544 printf("Error when deleting partition \"%s\" (%d)\n",
550 list_del(&slave->node);
551 free_partition(slave);
557 int del_mtd_partitions(struct mtd_info *master)
561 debug("Deleting MTD partitions on \"%s\":\n", master->name);
563 mutex_lock(&mtd_partitions_mutex);
564 ret = do_del_mtd_partitions(master);
565 mutex_unlock(&mtd_partitions_mutex);
570 static struct mtd_info *allocate_partition(struct mtd_info *master,
571 const struct mtd_partition *part,
572 int partno, uint64_t cur_offset)
574 struct mtd_info *slave;
577 /* allocate the partition structure */
578 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
579 name = kstrdup(part->name, GFP_KERNEL);
580 if (!name || !slave) {
581 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
585 return ERR_PTR(-ENOMEM);
588 /* set up the MTD object for this partition */
589 slave->type = master->type;
590 slave->flags = master->flags & ~part->mask_flags;
591 slave->size = part->size;
592 slave->writesize = master->writesize;
593 slave->writebufsize = master->writebufsize;
594 slave->oobsize = master->oobsize;
595 slave->oobavail = master->oobavail;
596 slave->subpage_sft = master->subpage_sft;
599 slave->owner = master->owner;
601 slave->backing_dev_info = master->backing_dev_info;
603 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
604 * to have the same data be in two different partitions.
606 slave->dev.parent = master->dev.parent;
610 slave->_read = part_read;
612 slave->_write = part_write;
614 if (master->_panic_write)
615 slave->_panic_write = part_panic_write;
618 if (master->_point && master->_unpoint) {
619 slave->_point = part_point;
620 slave->_unpoint = part_unpoint;
624 if (master->_get_unmapped_area)
625 slave->_get_unmapped_area = part_get_unmapped_area;
626 if (master->_read_oob)
627 slave->_read_oob = part_read_oob;
628 if (master->_write_oob)
629 slave->_write_oob = part_write_oob;
630 if (master->_read_user_prot_reg)
631 slave->_read_user_prot_reg = part_read_user_prot_reg;
632 if (master->_read_fact_prot_reg)
633 slave->_read_fact_prot_reg = part_read_fact_prot_reg;
634 if (master->_write_user_prot_reg)
635 slave->_write_user_prot_reg = part_write_user_prot_reg;
636 if (master->_lock_user_prot_reg)
637 slave->_lock_user_prot_reg = part_lock_user_prot_reg;
638 if (master->_get_user_prot_info)
639 slave->_get_user_prot_info = part_get_user_prot_info;
640 if (master->_get_fact_prot_info)
641 slave->_get_fact_prot_info = part_get_fact_prot_info;
643 slave->_sync = part_sync;
645 if (!partno && !master->dev.class && master->_suspend &&
647 slave->_suspend = part_suspend;
648 slave->_resume = part_resume;
651 slave->_writev = part_writev;
654 slave->_lock = part_lock;
656 slave->_unlock = part_unlock;
657 if (master->_is_locked)
658 slave->_is_locked = part_is_locked;
659 if (master->_block_isreserved)
660 slave->_block_isreserved = part_block_isreserved;
661 if (master->_block_isbad)
662 slave->_block_isbad = part_block_isbad;
663 if (master->_block_markbad)
664 slave->_block_markbad = part_block_markbad;
665 slave->_erase = part_erase;
666 slave->parent = master;
667 slave->offset = part->offset;
668 INIT_LIST_HEAD(&slave->partitions);
669 INIT_LIST_HEAD(&slave->node);
671 if (slave->offset == MTDPART_OFS_APPEND)
672 slave->offset = cur_offset;
673 if (slave->offset == MTDPART_OFS_NXTBLK) {
674 slave->offset = cur_offset;
675 if (mtd_mod_by_eb(cur_offset, master) != 0) {
676 /* Round up to next erasesize */
677 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
678 debug("Moving partition %d: "
679 "0x%012llx -> 0x%012llx\n", partno,
680 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
683 if (slave->offset == MTDPART_OFS_RETAIN) {
684 slave->offset = cur_offset;
685 if (master->size - slave->offset >= slave->size) {
686 slave->size = master->size - slave->offset
689 debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
690 part->name, master->size - slave->offset,
692 /* register to preserve ordering */
696 if (slave->size == MTDPART_SIZ_FULL)
697 slave->size = master->size - slave->offset;
699 debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
700 (unsigned long long)(slave->offset + slave->size), slave->name);
702 /* let's do some sanity checks */
703 if (slave->offset >= master->size) {
704 /* let's register it anyway to preserve ordering */
707 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
711 if (slave->offset + slave->size > master->size) {
712 slave->size = master->size - slave->offset;
713 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
714 part->name, master->name, slave->size);
716 if (master->numeraseregions > 1) {
717 /* Deal with variable erase size stuff */
718 int i, max = master->numeraseregions;
719 u64 end = slave->offset + slave->size;
720 struct mtd_erase_region_info *regions = master->eraseregions;
722 /* Find the first erase regions which is part of this
724 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
726 /* The loop searched for the region _behind_ the first one */
730 /* Pick biggest erasesize */
731 for (; i < max && regions[i].offset < end; i++) {
732 if (slave->erasesize < regions[i].erasesize)
733 slave->erasesize = regions[i].erasesize;
735 WARN_ON(slave->erasesize == 0);
737 /* Single erase size */
738 slave->erasesize = master->erasesize;
741 if ((slave->flags & MTD_WRITEABLE) &&
742 mtd_mod_by_eb(slave->offset, slave)) {
743 /* Doesn't start on a boundary of major erase size */
744 /* FIXME: Let it be writable if it is on a boundary of
745 * _minor_ erase size though */
746 slave->flags &= ~MTD_WRITEABLE;
747 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
750 if ((slave->flags & MTD_WRITEABLE) &&
751 mtd_mod_by_eb(slave->size, slave)) {
752 slave->flags &= ~MTD_WRITEABLE;
753 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
757 slave->ecclayout = master->ecclayout;
758 slave->ecc_step_size = master->ecc_step_size;
759 slave->ecc_strength = master->ecc_strength;
760 slave->bitflip_threshold = master->bitflip_threshold;
762 if (master->_block_isbad) {
765 while (offs < slave->size) {
766 if (mtd_block_isbad(master, offs + slave->offset))
767 slave->ecc_stats.badblocks++;
768 offs += slave->erasesize;
777 int mtd_add_partition(struct mtd_info *master, const char *name,
778 long long offset, long long length)
780 struct mtd_partition part;
781 struct mtd_info *p, *new;
785 /* the direct offset is expected */
786 if (offset == MTDPART_OFS_APPEND ||
787 offset == MTDPART_OFS_NXTBLK)
790 if (length == MTDPART_SIZ_FULL)
791 length = master->size - offset;
798 part.offset = offset;
800 part.ecclayout = NULL;
802 new = allocate_partition(master, &part, -1, offset);
807 end = offset + length;
809 mutex_lock(&mtd_partitions_mutex);
810 list_for_each_entry(p, &master->partitions, node) {
811 if (start >= p->offset &&
812 (start < (p->offset + p->size)))
815 if (end >= p->offset &&
816 (end < (p->offset + p->size)))
820 list_add_tail(&new->node, &master->partitions);
821 mutex_unlock(&mtd_partitions_mutex);
827 mutex_unlock(&mtd_partitions_mutex);
831 EXPORT_SYMBOL_GPL(mtd_add_partition);
833 int mtd_del_partition(struct mtd_info *master, int partno)
835 struct mtd_info *slave, *next;
838 mutex_lock(&mtd_partitions_mutex);
839 list_for_each_entry_safe(slave, next, &master->partitions, node)
840 if (slave->index == partno) {
841 ret = del_mtd_device(slave);
845 list_del(&slave->node);
846 free_partition(slave);
849 mutex_unlock(&mtd_partitions_mutex);
853 EXPORT_SYMBOL_GPL(mtd_del_partition);
857 * This function, given a master MTD object and a partition table, creates
858 * and registers slave MTD objects which are bound to the master according to
859 * the partition definitions.
861 * We don't register the master, or expect the caller to have done so,
862 * for reasons of data integrity.
865 int add_mtd_partitions(struct mtd_info *master,
866 const struct mtd_partition *parts,
869 struct mtd_info *slave;
870 uint64_t cur_offset = 0;
873 debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
875 for (i = 0; i < nbparts; i++) {
876 slave = allocate_partition(master, parts + i, i, cur_offset);
878 return PTR_ERR(slave);
880 mutex_lock(&mtd_partitions_mutex);
881 list_add_tail(&slave->node, &master->partitions);
882 mutex_unlock(&mtd_partitions_mutex);
884 add_mtd_device(slave);
886 cur_offset = slave->offset + slave->size;
893 static DEFINE_SPINLOCK(part_parser_lock);
894 static LIST_HEAD(part_parsers);
896 static struct mtd_part_parser *get_partition_parser(const char *name)
898 struct mtd_part_parser *p, *ret = NULL;
900 spin_lock(&part_parser_lock);
902 list_for_each_entry(p, &part_parsers, list)
903 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
908 spin_unlock(&part_parser_lock);
913 #define put_partition_parser(p) do { module_put((p)->owner); } while (0)
915 void register_mtd_parser(struct mtd_part_parser *p)
917 spin_lock(&part_parser_lock);
918 list_add(&p->list, &part_parsers);
919 spin_unlock(&part_parser_lock);
921 EXPORT_SYMBOL_GPL(register_mtd_parser);
923 void deregister_mtd_parser(struct mtd_part_parser *p)
925 spin_lock(&part_parser_lock);
927 spin_unlock(&part_parser_lock);
929 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
932 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
933 * are changing this array!
935 static const char * const default_mtd_part_types[] = {
942 * parse_mtd_partitions - parse MTD partitions
943 * @master: the master partition (describes whole MTD device)
944 * @types: names of partition parsers to try or %NULL
945 * @pparts: array of partitions found is returned here
946 * @data: MTD partition parser-specific data
948 * This function tries to find partition on MTD device @master. It uses MTD
949 * partition parsers, specified in @types. However, if @types is %NULL, then
950 * the default list of parsers is used. The default list contains only the
951 * "cmdlinepart" and "ofpart" parsers ATM.
952 * Note: If there are more then one parser in @types, the kernel only takes the
953 * partitions parsed out by the first parser.
955 * This function may return:
956 * o a negative error code in case of failure
957 * o zero if no partitions were found
958 * o a positive number of found partitions, in which case on exit @pparts will
959 * point to an array containing this number of &struct mtd_info objects.
961 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
962 struct mtd_partition **pparts,
963 struct mtd_part_parser_data *data)
965 struct mtd_part_parser *parser;
969 types = default_mtd_part_types;
971 for ( ; ret <= 0 && *types; types++) {
972 parser = get_partition_parser(*types);
973 if (!parser && !request_module("%s", *types))
974 parser = get_partition_parser(*types);
977 ret = (*parser->parse_fn)(master, pparts, data);
978 put_partition_parser(parser);
980 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
981 ret, parser->name, master->name);
989 /* Returns the size of the entire flash chip */
990 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
992 if (mtd_is_partition(mtd))
993 return mtd->parent->size;
997 EXPORT_SYMBOL_GPL(mtd_get_device_size);