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>
13 #include <dm/devres.h>
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/list.h>
19 #include <linux/kmod.h>
24 #include <linux/bug.h>
25 #include <linux/errno.h>
26 #include <linux/compat.h>
27 #include <ubi_uboot.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/partitions.h>
31 #include <linux/err.h>
32 #include <linux/sizes.h>
37 static DEFINE_MUTEX(mtd_partitions_mutex);
39 DEFINE_MUTEX(mtd_partitions_mutex);
46 * kstrdup - allocate space for and copy an existing string
47 * @s: the string to duplicate
48 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
50 char *kstrdup(const char *s, gfp_t gfp)
59 buf = kmalloc(len, gfp);
66 #define MTD_SIZE_REMAINING (~0LLU)
67 #define MTD_OFFSET_NOT_SPECIFIED (~0LLU)
69 bool mtd_partitions_used(struct mtd_info *master)
71 struct mtd_info *slave;
73 list_for_each_entry(slave, &master->partitions, node) {
82 * mtd_parse_partition - Parse @mtdparts partition definition, fill @partition
83 * with it and update the @mtdparts string pointer.
85 * The partition name is allocated and must be freed by the caller.
87 * This function is widely inspired from part_parse (mtdparts.c).
89 * @mtdparts: String describing the partition with mtdparts command syntax
90 * @partition: MTD partition structure to fill
92 * @return 0 on success, an error otherwise.
94 static int mtd_parse_partition(const char **_mtdparts,
95 struct mtd_partition *partition)
97 const char *mtdparts = *_mtdparts;
98 const char *name = NULL;
102 /* Ensure the partition structure is empty */
103 memset(partition, 0, sizeof(struct mtd_partition));
105 /* Fetch the partition size */
106 if (*mtdparts == '-') {
107 /* Assign all remaining space to this partition */
108 partition->size = MTD_SIZE_REMAINING;
111 partition->size = ustrtoull(mtdparts, (char **)&mtdparts, 0);
112 if (partition->size < SZ_4K) {
113 printf("Minimum partition size 4kiB, %lldB requested\n",
119 /* Check for the offset */
120 partition->offset = MTD_OFFSET_NOT_SPECIFIED;
121 if (*mtdparts == '@') {
123 partition->offset = ustrtoull(mtdparts, (char **)&mtdparts, 0);
126 /* Now look for the name */
127 if (*mtdparts == '(') {
129 mtdparts = strchr(name, ')');
131 printf("No closing ')' found in partition name\n");
134 name_len = mtdparts - name + 1;
135 if ((name_len - 1) == 0) {
136 printf("Empty partition name\n");
141 /* Name will be of the form size@offset */
145 /* Check if the partition is read-only */
146 if (strncmp(mtdparts, "ro", 2) == 0) {
147 partition->mask_flags |= MTD_WRITEABLE;
151 /* Check for a potential next partition definition */
152 if (*mtdparts == ',') {
153 if (partition->size == MTD_SIZE_REMAINING) {
154 printf("No partitions allowed after a fill-up\n");
158 } else if ((*mtdparts == ';') || (*mtdparts == '\0')) {
161 printf("Unexpected character '%c' in mtdparts\n", *mtdparts);
166 * Allocate a buffer for the name and either copy the provided name or
167 * auto-generate it with the form 'size@offset'.
169 buf = malloc(name_len);
174 strncpy(buf, name, name_len - 1);
176 snprintf(buf, name_len, "0x%08llx@0x%08llx",
177 partition->size, partition->offset);
179 buf[name_len - 1] = '\0';
180 partition->name = buf;
182 *_mtdparts = mtdparts;
188 * mtd_parse_partitions - Create a partition array from an mtdparts definition
190 * Stateless function that takes a @parent MTD device, a string @_mtdparts
191 * describing the partitions (with the "mtdparts" command syntax) and creates
192 * the corresponding MTD partition structure array @_parts. Both the name and
193 * the structure partition itself must be freed freed, the caller may use
194 * @mtd_free_parsed_partitions() for this purpose.
196 * @parent: MTD device which contains the partitions
197 * @_mtdparts: Pointer to a string describing the partitions with "mtdparts"
199 * @_parts: Allocated array containing the partitions, must be freed by the
201 * @_nparts: Size of @_parts array.
203 * @return 0 on success, an error otherwise.
205 int mtd_parse_partitions(struct mtd_info *parent, const char **_mtdparts,
206 struct mtd_partition **_parts, int *_nparts)
208 struct mtd_partition partition = {}, *parts;
209 const char *mtdparts = *_mtdparts;
210 uint64_t cur_off = 0, cur_sz = 0;
215 /* First, iterate over the partitions until we know their number */
216 while (mtdparts[0] != '\0' && mtdparts[0] != ';') {
217 ret = mtd_parse_partition(&mtdparts, &partition);
221 free((char *)partition.name);
225 /* Allocate an array of partitions to give back to the caller */
226 parts = malloc(sizeof(*parts) * nparts);
228 printf("Not enough space to save partitions meta-data\n");
232 /* Iterate again over each partition to save the data in our array */
233 for (idx = 0; idx < nparts; idx++) {
234 ret = mtd_parse_partition(_mtdparts, &parts[idx]);
238 if (parts[idx].size == MTD_SIZE_REMAINING)
239 parts[idx].size = parent->size - cur_sz;
240 cur_sz += parts[idx].size;
242 sz = parts[idx].size;
243 if (sz < parent->writesize || do_div(sz, parent->writesize)) {
244 printf("Partition size must be a multiple of %d\n",
249 if (parts[idx].offset == MTD_OFFSET_NOT_SPECIFIED)
250 parts[idx].offset = cur_off;
251 cur_off += parts[idx].size;
253 parts[idx].ecclayout = parent->ecclayout;
256 /* Offset by one mtdparts to point to the next device if any */
257 if (*_mtdparts[0] == ';')
267 * mtd_free_parsed_partitions - Free dynamically allocated partitions
269 * Each successful call to @mtd_parse_partitions must be followed by a call to
270 * @mtd_free_parsed_partitions to free any allocated array during the parsing
273 * @parts: Array containing the partitions that will be freed.
274 * @nparts: Size of @parts array.
276 void mtd_free_parsed_partitions(struct mtd_partition *parts,
281 for (i = 0; i < nparts; i++)
282 free((char *)parts[i].name);
288 * MTD methods which simply translate the effective address and pass through
289 * to the _real_ device.
292 static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
293 size_t *retlen, u_char *buf)
295 struct mtd_ecc_stats stats;
298 stats = mtd->parent->ecc_stats;
299 res = mtd->parent->_read(mtd->parent, from + mtd->offset, len,
301 if (unlikely(mtd_is_eccerr(res)))
302 mtd->ecc_stats.failed +=
303 mtd->parent->ecc_stats.failed - stats.failed;
305 mtd->ecc_stats.corrected +=
306 mtd->parent->ecc_stats.corrected - stats.corrected;
311 static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
312 size_t *retlen, void **virt, resource_size_t *phys)
314 return mtd->parent->_point(mtd->parent, from + mtd->offset, len,
318 static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
320 return mtd->parent->_unpoint(mtd->parent, from + mtd->offset, len);
324 static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
326 unsigned long offset,
329 offset += mtd->offset;
330 return mtd->parent->_get_unmapped_area(mtd->parent, len, offset, flags);
333 static int part_read_oob(struct mtd_info *mtd, loff_t from,
334 struct mtd_oob_ops *ops)
338 if (from >= mtd->size)
340 if (ops->datbuf && from + ops->len > mtd->size)
344 * If OOB is also requested, make sure that we do not read past the end
350 if (ops->mode == MTD_OPS_AUTO_OOB)
354 pages = mtd_div_by_ws(mtd->size, mtd);
355 pages -= mtd_div_by_ws(from, mtd);
356 if (ops->ooboffs + ops->ooblen > pages * len)
360 res = mtd->parent->_read_oob(mtd->parent, from + mtd->offset, ops);
362 if (mtd_is_bitflip(res))
363 mtd->ecc_stats.corrected++;
364 if (mtd_is_eccerr(res))
365 mtd->ecc_stats.failed++;
370 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
371 size_t len, size_t *retlen, u_char *buf)
373 return mtd->parent->_read_user_prot_reg(mtd->parent, from, len,
377 static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
378 size_t *retlen, struct otp_info *buf)
380 return mtd->parent->_get_user_prot_info(mtd->parent, len, retlen,
384 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
385 size_t len, size_t *retlen, u_char *buf)
387 return mtd->parent->_read_fact_prot_reg(mtd->parent, from, len,
391 static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
392 size_t *retlen, struct otp_info *buf)
394 return mtd->parent->_get_fact_prot_info(mtd->parent, len, retlen,
398 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
399 size_t *retlen, const u_char *buf)
401 return mtd->parent->_write(mtd->parent, to + mtd->offset, len,
405 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
406 size_t *retlen, const u_char *buf)
408 return mtd->parent->_panic_write(mtd->parent, to + mtd->offset, len,
412 static int part_write_oob(struct mtd_info *mtd, loff_t to,
413 struct mtd_oob_ops *ops)
417 if (ops->datbuf && to + ops->len > mtd->size)
419 return mtd->parent->_write_oob(mtd->parent, to + mtd->offset, ops);
422 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
423 size_t len, size_t *retlen, u_char *buf)
425 return mtd->parent->_write_user_prot_reg(mtd->parent, from, len,
429 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
432 return mtd->parent->_lock_user_prot_reg(mtd->parent, from, len);
436 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
437 unsigned long count, loff_t to, size_t *retlen)
439 return mtd->parent->_writev(mtd->parent, vecs, count,
440 to + mtd->offset, retlen);
444 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
448 instr->addr += mtd->offset;
449 ret = mtd->parent->_erase(mtd->parent, instr);
451 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
452 instr->fail_addr -= mtd->offset;
453 instr->addr -= mtd->offset;
458 void mtd_erase_callback(struct erase_info *instr)
460 if (instr->mtd->_erase == part_erase) {
461 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
462 instr->fail_addr -= instr->mtd->offset;
463 instr->addr -= instr->mtd->offset;
466 instr->callback(instr);
468 EXPORT_SYMBOL_GPL(mtd_erase_callback);
470 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
472 return mtd->parent->_lock(mtd->parent, ofs + mtd->offset, len);
475 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
477 return mtd->parent->_unlock(mtd->parent, ofs + mtd->offset, len);
480 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
482 return mtd->parent->_is_locked(mtd->parent, ofs + mtd->offset, len);
485 static void part_sync(struct mtd_info *mtd)
487 mtd->parent->_sync(mtd->parent);
491 static int part_suspend(struct mtd_info *mtd)
493 return mtd->parent->_suspend(mtd->parent);
496 static void part_resume(struct mtd_info *mtd)
498 mtd->parent->_resume(mtd->parent);
502 static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
505 return mtd->parent->_block_isreserved(mtd->parent, ofs);
508 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
511 return mtd->parent->_block_isbad(mtd->parent, ofs);
514 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
519 res = mtd->parent->_block_markbad(mtd->parent, ofs);
521 mtd->ecc_stats.badblocks++;
525 static inline void free_partition(struct mtd_info *p)
532 * This function unregisters and destroy all slave MTD objects which are
533 * attached to the given master MTD object, recursively.
535 static int do_del_mtd_partitions(struct mtd_info *master)
537 struct mtd_info *slave, *next;
540 list_for_each_entry_safe(slave, next, &master->partitions, node) {
541 if (mtd_has_partitions(slave))
542 del_mtd_partitions(slave);
544 debug("Deleting %s MTD partition\n", slave->name);
545 ret = del_mtd_device(slave);
547 printf("Error when deleting partition \"%s\" (%d)\n",
553 list_del(&slave->node);
554 free_partition(slave);
560 int del_mtd_partitions(struct mtd_info *master)
564 debug("Deleting MTD partitions on \"%s\":\n", master->name);
566 mutex_lock(&mtd_partitions_mutex);
567 ret = do_del_mtd_partitions(master);
568 mutex_unlock(&mtd_partitions_mutex);
573 static struct mtd_info *allocate_partition(struct mtd_info *master,
574 const struct mtd_partition *part,
575 int partno, uint64_t cur_offset)
577 struct mtd_info *slave;
580 /* allocate the partition structure */
581 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
582 name = kstrdup(part->name, GFP_KERNEL);
583 if (!name || !slave) {
584 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
588 return ERR_PTR(-ENOMEM);
591 /* set up the MTD object for this partition */
592 slave->type = master->type;
593 slave->flags = master->flags & ~part->mask_flags;
594 slave->size = part->size;
595 slave->writesize = master->writesize;
596 slave->writebufsize = master->writebufsize;
597 slave->oobsize = master->oobsize;
598 slave->oobavail = master->oobavail;
599 slave->subpage_sft = master->subpage_sft;
602 slave->owner = master->owner;
604 slave->backing_dev_info = master->backing_dev_info;
606 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
607 * to have the same data be in two different partitions.
609 slave->dev.parent = master->dev.parent;
613 slave->_read = part_read;
615 slave->_write = part_write;
617 if (master->_panic_write)
618 slave->_panic_write = part_panic_write;
621 if (master->_point && master->_unpoint) {
622 slave->_point = part_point;
623 slave->_unpoint = part_unpoint;
627 if (master->_get_unmapped_area)
628 slave->_get_unmapped_area = part_get_unmapped_area;
629 if (master->_read_oob)
630 slave->_read_oob = part_read_oob;
631 if (master->_write_oob)
632 slave->_write_oob = part_write_oob;
633 if (master->_read_user_prot_reg)
634 slave->_read_user_prot_reg = part_read_user_prot_reg;
635 if (master->_read_fact_prot_reg)
636 slave->_read_fact_prot_reg = part_read_fact_prot_reg;
637 if (master->_write_user_prot_reg)
638 slave->_write_user_prot_reg = part_write_user_prot_reg;
639 if (master->_lock_user_prot_reg)
640 slave->_lock_user_prot_reg = part_lock_user_prot_reg;
641 if (master->_get_user_prot_info)
642 slave->_get_user_prot_info = part_get_user_prot_info;
643 if (master->_get_fact_prot_info)
644 slave->_get_fact_prot_info = part_get_fact_prot_info;
646 slave->_sync = part_sync;
648 if (!partno && !master->dev.class && master->_suspend &&
650 slave->_suspend = part_suspend;
651 slave->_resume = part_resume;
654 slave->_writev = part_writev;
657 slave->_lock = part_lock;
659 slave->_unlock = part_unlock;
660 if (master->_is_locked)
661 slave->_is_locked = part_is_locked;
662 if (master->_block_isreserved)
663 slave->_block_isreserved = part_block_isreserved;
664 if (master->_block_isbad)
665 slave->_block_isbad = part_block_isbad;
666 if (master->_block_markbad)
667 slave->_block_markbad = part_block_markbad;
668 slave->_erase = part_erase;
669 slave->parent = master;
670 slave->offset = part->offset;
671 INIT_LIST_HEAD(&slave->partitions);
672 INIT_LIST_HEAD(&slave->node);
674 if (slave->offset == MTDPART_OFS_APPEND)
675 slave->offset = cur_offset;
676 if (slave->offset == MTDPART_OFS_NXTBLK) {
677 slave->offset = cur_offset;
678 if (mtd_mod_by_eb(cur_offset, master) != 0) {
679 /* Round up to next erasesize */
680 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
681 debug("Moving partition %d: "
682 "0x%012llx -> 0x%012llx\n", partno,
683 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
686 if (slave->offset == MTDPART_OFS_RETAIN) {
687 slave->offset = cur_offset;
688 if (master->size - slave->offset >= slave->size) {
689 slave->size = master->size - slave->offset
692 debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
693 part->name, master->size - slave->offset,
695 /* register to preserve ordering */
699 if (slave->size == MTDPART_SIZ_FULL)
700 slave->size = master->size - slave->offset;
702 debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
703 (unsigned long long)(slave->offset + slave->size), slave->name);
705 /* let's do some sanity checks */
706 if (slave->offset >= master->size) {
707 /* let's register it anyway to preserve ordering */
710 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
714 if (slave->offset + slave->size > master->size) {
715 slave->size = master->size - slave->offset;
716 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
717 part->name, master->name, slave->size);
719 if (master->numeraseregions > 1) {
720 /* Deal with variable erase size stuff */
721 int i, max = master->numeraseregions;
722 u64 end = slave->offset + slave->size;
723 struct mtd_erase_region_info *regions = master->eraseregions;
725 /* Find the first erase regions which is part of this
727 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
729 /* The loop searched for the region _behind_ the first one */
733 /* Pick biggest erasesize */
734 for (; i < max && regions[i].offset < end; i++) {
735 if (slave->erasesize < regions[i].erasesize)
736 slave->erasesize = regions[i].erasesize;
738 WARN_ON(slave->erasesize == 0);
740 /* Single erase size */
741 slave->erasesize = master->erasesize;
744 if ((slave->flags & MTD_WRITEABLE) &&
745 mtd_mod_by_eb(slave->offset, slave)) {
746 /* Doesn't start on a boundary of major erase size */
747 /* FIXME: Let it be writable if it is on a boundary of
748 * _minor_ erase size though */
749 slave->flags &= ~MTD_WRITEABLE;
750 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
753 if ((slave->flags & MTD_WRITEABLE) &&
754 mtd_mod_by_eb(slave->size, slave)) {
755 slave->flags &= ~MTD_WRITEABLE;
756 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
760 slave->ecclayout = master->ecclayout;
761 slave->ecc_step_size = master->ecc_step_size;
762 slave->ecc_strength = master->ecc_strength;
763 slave->bitflip_threshold = master->bitflip_threshold;
765 if (master->_block_isbad) {
768 while (offs < slave->size) {
769 if (mtd_block_isbad(master, offs + slave->offset))
770 slave->ecc_stats.badblocks++;
771 offs += slave->erasesize;
780 int mtd_add_partition(struct mtd_info *master, const char *name,
781 long long offset, long long length)
783 struct mtd_partition part;
784 struct mtd_info *p, *new;
788 /* the direct offset is expected */
789 if (offset == MTDPART_OFS_APPEND ||
790 offset == MTDPART_OFS_NXTBLK)
793 if (length == MTDPART_SIZ_FULL)
794 length = master->size - offset;
801 part.offset = offset;
803 part.ecclayout = NULL;
805 new = allocate_partition(master, &part, -1, offset);
810 end = offset + length;
812 mutex_lock(&mtd_partitions_mutex);
813 list_for_each_entry(p, &master->partitions, node) {
814 if (start >= p->offset &&
815 (start < (p->offset + p->size)))
818 if (end >= p->offset &&
819 (end < (p->offset + p->size)))
823 list_add_tail(&new->node, &master->partitions);
824 mutex_unlock(&mtd_partitions_mutex);
830 mutex_unlock(&mtd_partitions_mutex);
834 EXPORT_SYMBOL_GPL(mtd_add_partition);
836 int mtd_del_partition(struct mtd_info *master, int partno)
838 struct mtd_info *slave, *next;
841 mutex_lock(&mtd_partitions_mutex);
842 list_for_each_entry_safe(slave, next, &master->partitions, node)
843 if (slave->index == partno) {
844 ret = del_mtd_device(slave);
848 list_del(&slave->node);
849 free_partition(slave);
852 mutex_unlock(&mtd_partitions_mutex);
856 EXPORT_SYMBOL_GPL(mtd_del_partition);
860 * This function, given a master MTD object and a partition table, creates
861 * and registers slave MTD objects which are bound to the master according to
862 * the partition definitions.
864 * We don't register the master, or expect the caller to have done so,
865 * for reasons of data integrity.
868 int add_mtd_partitions(struct mtd_info *master,
869 const struct mtd_partition *parts,
872 struct mtd_info *slave;
873 uint64_t cur_offset = 0;
876 debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
878 for (i = 0; i < nbparts; i++) {
879 slave = allocate_partition(master, parts + i, i, cur_offset);
881 return PTR_ERR(slave);
883 mutex_lock(&mtd_partitions_mutex);
884 list_add_tail(&slave->node, &master->partitions);
885 mutex_unlock(&mtd_partitions_mutex);
887 add_mtd_device(slave);
889 cur_offset = slave->offset + slave->size;
895 #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(OF_CONTROL)
896 int add_mtd_partitions_of(struct mtd_info *master)
904 parts = ofnode_find_subnode(mtd_get_ofnode(master), "partitions");
905 if (!ofnode_valid(parts) || !ofnode_is_available(parts) ||
906 !ofnode_device_is_compatible(parts, "fixed-partitions"))
909 ofnode_for_each_subnode(child, parts) {
910 struct mtd_partition part = { 0 };
911 struct mtd_info *slave;
912 fdt_addr_t offset, size;
914 if (!ofnode_is_available(child))
917 offset = ofnode_get_addr_size_index_notrans(child, 0, &size);
918 if (offset == FDT_ADDR_T_NONE || !size) {
919 debug("Missing partition offset/size on \"%s\" partition\n",
924 part.name = ofnode_read_string(child, "label");
926 part.name = ofnode_read_string(child, "name");
929 * .mask_flags is used to remove flags in allocate_partition(),
930 * so when "read-only" is present, we add MTD_WRITABLE to the
931 * mask, and so MTD_WRITABLE will be removed on partition
934 if (ofnode_read_bool(child, "read-only"))
935 part.mask_flags |= MTD_WRITEABLE;
936 if (ofnode_read_bool(child, "lock"))
937 part.mask_flags |= MTD_POWERUP_LOCK;
939 part.offset = offset;
941 part.ecclayout = master->ecclayout;
943 slave = allocate_partition(master, &part, i++, 0);
945 return PTR_ERR(slave);
947 mutex_lock(&mtd_partitions_mutex);
948 list_add_tail(&slave->node, &master->partitions);
949 mutex_unlock(&mtd_partitions_mutex);
951 add_mtd_device(slave);
956 #endif /* CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(OF_CONTROL) */
959 static DEFINE_SPINLOCK(part_parser_lock);
960 static LIST_HEAD(part_parsers);
962 static struct mtd_part_parser *get_partition_parser(const char *name)
964 struct mtd_part_parser *p, *ret = NULL;
966 spin_lock(&part_parser_lock);
968 list_for_each_entry(p, &part_parsers, list)
969 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
974 spin_unlock(&part_parser_lock);
979 #define put_partition_parser(p) do { module_put((p)->owner); } while (0)
981 void register_mtd_parser(struct mtd_part_parser *p)
983 spin_lock(&part_parser_lock);
984 list_add(&p->list, &part_parsers);
985 spin_unlock(&part_parser_lock);
987 EXPORT_SYMBOL_GPL(register_mtd_parser);
989 void deregister_mtd_parser(struct mtd_part_parser *p)
991 spin_lock(&part_parser_lock);
993 spin_unlock(&part_parser_lock);
995 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
998 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
999 * are changing this array!
1001 static const char * const default_mtd_part_types[] = {
1008 * parse_mtd_partitions - parse MTD partitions
1009 * @master: the master partition (describes whole MTD device)
1010 * @types: names of partition parsers to try or %NULL
1011 * @pparts: array of partitions found is returned here
1012 * @data: MTD partition parser-specific data
1014 * This function tries to find partition on MTD device @master. It uses MTD
1015 * partition parsers, specified in @types. However, if @types is %NULL, then
1016 * the default list of parsers is used. The default list contains only the
1017 * "cmdlinepart" and "ofpart" parsers ATM.
1018 * Note: If there are more then one parser in @types, the kernel only takes the
1019 * partitions parsed out by the first parser.
1021 * This function may return:
1022 * o a negative error code in case of failure
1023 * o zero if no partitions were found
1024 * o a positive number of found partitions, in which case on exit @pparts will
1025 * point to an array containing this number of &struct mtd_info objects.
1027 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
1028 struct mtd_partition **pparts,
1029 struct mtd_part_parser_data *data)
1031 struct mtd_part_parser *parser;
1035 types = default_mtd_part_types;
1037 for ( ; ret <= 0 && *types; types++) {
1038 parser = get_partition_parser(*types);
1039 if (!parser && !request_module("%s", *types))
1040 parser = get_partition_parser(*types);
1043 ret = (*parser->parse_fn)(master, pparts, data);
1044 put_partition_parser(parser);
1046 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
1047 ret, parser->name, master->name);
1055 /* Returns the size of the entire flash chip */
1056 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
1058 if (mtd_is_partition(mtd))
1059 return mtd->parent->size;
1063 EXPORT_SYMBOL_GPL(mtd_get_device_size);