Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Simple MTD partitioning layer
0004  *
0005  * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
0006  * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
0007  * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/types.h>
0012 #include <linux/kernel.h>
0013 #include <linux/slab.h>
0014 #include <linux/list.h>
0015 #include <linux/kmod.h>
0016 #include <linux/mtd/mtd.h>
0017 #include <linux/mtd/partitions.h>
0018 #include <linux/err.h>
0019 #include <linux/of.h>
0020 #include <linux/of_platform.h>
0021 
0022 #include "mtdcore.h"
0023 
0024 /*
0025  * MTD methods which simply translate the effective address and pass through
0026  * to the _real_ device.
0027  */
0028 
0029 static inline void free_partition(struct mtd_info *mtd)
0030 {
0031     kfree(mtd->name);
0032     kfree(mtd);
0033 }
0034 
0035 static struct mtd_info *allocate_partition(struct mtd_info *parent,
0036                        const struct mtd_partition *part,
0037                        int partno, uint64_t cur_offset)
0038 {
0039     struct mtd_info *master = mtd_get_master(parent);
0040     int wr_alignment = (parent->flags & MTD_NO_ERASE) ?
0041                master->writesize : master->erasesize;
0042     u64 parent_size = mtd_is_partition(parent) ?
0043               parent->part.size : parent->size;
0044     struct mtd_info *child;
0045     u32 remainder;
0046     char *name;
0047     u64 tmp;
0048 
0049     /* allocate the partition structure */
0050     child = kzalloc(sizeof(*child), GFP_KERNEL);
0051     name = kstrdup(part->name, GFP_KERNEL);
0052     if (!name || !child) {
0053         printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
0054                parent->name);
0055         kfree(name);
0056         kfree(child);
0057         return ERR_PTR(-ENOMEM);
0058     }
0059 
0060     /* set up the MTD object for this partition */
0061     child->type = parent->type;
0062     child->part.flags = parent->flags & ~part->mask_flags;
0063     child->part.flags |= part->add_flags;
0064     child->flags = child->part.flags;
0065     child->part.size = part->size;
0066     child->writesize = parent->writesize;
0067     child->writebufsize = parent->writebufsize;
0068     child->oobsize = parent->oobsize;
0069     child->oobavail = parent->oobavail;
0070     child->subpage_sft = parent->subpage_sft;
0071 
0072     child->name = name;
0073     child->owner = parent->owner;
0074 
0075     /* NOTE: Historically, we didn't arrange MTDs as a tree out of
0076      * concern for showing the same data in multiple partitions.
0077      * However, it is very useful to have the master node present,
0078      * so the MTD_PARTITIONED_MASTER option allows that. The master
0079      * will have device nodes etc only if this is set, so make the
0080      * parent conditional on that option. Note, this is a way to
0081      * distinguish between the parent and its partitions in sysfs.
0082      */
0083     child->dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
0084                 &parent->dev : parent->dev.parent;
0085     child->dev.of_node = part->of_node;
0086     child->parent = parent;
0087     child->part.offset = part->offset;
0088     INIT_LIST_HEAD(&child->partitions);
0089 
0090     if (child->part.offset == MTDPART_OFS_APPEND)
0091         child->part.offset = cur_offset;
0092     if (child->part.offset == MTDPART_OFS_NXTBLK) {
0093         tmp = cur_offset;
0094         child->part.offset = cur_offset;
0095         remainder = do_div(tmp, wr_alignment);
0096         if (remainder) {
0097             child->part.offset += wr_alignment - remainder;
0098             printk(KERN_NOTICE "Moving partition %d: "
0099                    "0x%012llx -> 0x%012llx\n", partno,
0100                    (unsigned long long)cur_offset,
0101                    child->part.offset);
0102         }
0103     }
0104     if (child->part.offset == MTDPART_OFS_RETAIN) {
0105         child->part.offset = cur_offset;
0106         if (parent_size - child->part.offset >= child->part.size) {
0107             child->part.size = parent_size - child->part.offset -
0108                        child->part.size;
0109         } else {
0110             printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
0111                 part->name, parent_size - child->part.offset,
0112                 child->part.size);
0113             /* register to preserve ordering */
0114             goto out_register;
0115         }
0116     }
0117     if (child->part.size == MTDPART_SIZ_FULL)
0118         child->part.size = parent_size - child->part.offset;
0119 
0120     printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n",
0121            child->part.offset, child->part.offset + child->part.size,
0122            child->name);
0123 
0124     /* let's do some sanity checks */
0125     if (child->part.offset >= parent_size) {
0126         /* let's register it anyway to preserve ordering */
0127         child->part.offset = 0;
0128         child->part.size = 0;
0129 
0130         /* Initialize ->erasesize to make add_mtd_device() happy. */
0131         child->erasesize = parent->erasesize;
0132         printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
0133             part->name);
0134         goto out_register;
0135     }
0136     if (child->part.offset + child->part.size > parent->size) {
0137         child->part.size = parent_size - child->part.offset;
0138         printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
0139             part->name, parent->name, child->part.size);
0140     }
0141 
0142     if (parent->numeraseregions > 1) {
0143         /* Deal with variable erase size stuff */
0144         int i, max = parent->numeraseregions;
0145         u64 end = child->part.offset + child->part.size;
0146         struct mtd_erase_region_info *regions = parent->eraseregions;
0147 
0148         /* Find the first erase regions which is part of this
0149          * partition. */
0150         for (i = 0; i < max && regions[i].offset <= child->part.offset;
0151              i++)
0152             ;
0153         /* The loop searched for the region _behind_ the first one */
0154         if (i > 0)
0155             i--;
0156 
0157         /* Pick biggest erasesize */
0158         for (; i < max && regions[i].offset < end; i++) {
0159             if (child->erasesize < regions[i].erasesize)
0160                 child->erasesize = regions[i].erasesize;
0161         }
0162         BUG_ON(child->erasesize == 0);
0163     } else {
0164         /* Single erase size */
0165         child->erasesize = master->erasesize;
0166     }
0167 
0168     /*
0169      * Child erasesize might differ from the parent one if the parent
0170      * exposes several regions with different erasesize. Adjust
0171      * wr_alignment accordingly.
0172      */
0173     if (!(child->flags & MTD_NO_ERASE))
0174         wr_alignment = child->erasesize;
0175 
0176     tmp = mtd_get_master_ofs(child, 0);
0177     remainder = do_div(tmp, wr_alignment);
0178     if ((child->flags & MTD_WRITEABLE) && remainder) {
0179         /* Doesn't start on a boundary of major erase size */
0180         /* FIXME: Let it be writable if it is on a boundary of
0181          * _minor_ erase size though */
0182         child->flags &= ~MTD_WRITEABLE;
0183         printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
0184             part->name);
0185     }
0186 
0187     tmp = mtd_get_master_ofs(child, 0) + child->part.size;
0188     remainder = do_div(tmp, wr_alignment);
0189     if ((child->flags & MTD_WRITEABLE) && remainder) {
0190         child->flags &= ~MTD_WRITEABLE;
0191         printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
0192             part->name);
0193     }
0194 
0195     child->size = child->part.size;
0196     child->ecc_step_size = parent->ecc_step_size;
0197     child->ecc_strength = parent->ecc_strength;
0198     child->bitflip_threshold = parent->bitflip_threshold;
0199 
0200     if (master->_block_isbad) {
0201         uint64_t offs = 0;
0202 
0203         while (offs < child->part.size) {
0204             if (mtd_block_isreserved(child, offs))
0205                 child->ecc_stats.bbtblocks++;
0206             else if (mtd_block_isbad(child, offs))
0207                 child->ecc_stats.badblocks++;
0208             offs += child->erasesize;
0209         }
0210     }
0211 
0212 out_register:
0213     return child;
0214 }
0215 
0216 static ssize_t offset_show(struct device *dev,
0217                struct device_attribute *attr, char *buf)
0218 {
0219     struct mtd_info *mtd = dev_get_drvdata(dev);
0220 
0221     return sysfs_emit(buf, "%lld\n", mtd->part.offset);
0222 }
0223 static DEVICE_ATTR_RO(offset);  /* mtd partition offset */
0224 
0225 static const struct attribute *mtd_partition_attrs[] = {
0226     &dev_attr_offset.attr,
0227     NULL
0228 };
0229 
0230 static int mtd_add_partition_attrs(struct mtd_info *new)
0231 {
0232     int ret = sysfs_create_files(&new->dev.kobj, mtd_partition_attrs);
0233     if (ret)
0234         printk(KERN_WARNING
0235                "mtd: failed to create partition attrs, err=%d\n", ret);
0236     return ret;
0237 }
0238 
0239 int mtd_add_partition(struct mtd_info *parent, const char *name,
0240               long long offset, long long length)
0241 {
0242     struct mtd_info *master = mtd_get_master(parent);
0243     u64 parent_size = mtd_is_partition(parent) ?
0244               parent->part.size : parent->size;
0245     struct mtd_partition part;
0246     struct mtd_info *child;
0247     int ret = 0;
0248 
0249     /* the direct offset is expected */
0250     if (offset == MTDPART_OFS_APPEND ||
0251         offset == MTDPART_OFS_NXTBLK)
0252         return -EINVAL;
0253 
0254     if (length == MTDPART_SIZ_FULL)
0255         length = parent_size - offset;
0256 
0257     if (length <= 0)
0258         return -EINVAL;
0259 
0260     memset(&part, 0, sizeof(part));
0261     part.name = name;
0262     part.size = length;
0263     part.offset = offset;
0264 
0265     child = allocate_partition(parent, &part, -1, offset);
0266     if (IS_ERR(child))
0267         return PTR_ERR(child);
0268 
0269     mutex_lock(&master->master.partitions_lock);
0270     list_add_tail(&child->part.node, &parent->partitions);
0271     mutex_unlock(&master->master.partitions_lock);
0272 
0273     ret = add_mtd_device(child);
0274     if (ret)
0275         goto err_remove_part;
0276 
0277     mtd_add_partition_attrs(child);
0278 
0279     return 0;
0280 
0281 err_remove_part:
0282     mutex_lock(&master->master.partitions_lock);
0283     list_del(&child->part.node);
0284     mutex_unlock(&master->master.partitions_lock);
0285 
0286     free_partition(child);
0287 
0288     return ret;
0289 }
0290 EXPORT_SYMBOL_GPL(mtd_add_partition);
0291 
0292 /**
0293  * __mtd_del_partition - delete MTD partition
0294  *
0295  * @mtd: MTD structure to be deleted
0296  *
0297  * This function must be called with the partitions mutex locked.
0298  */
0299 static int __mtd_del_partition(struct mtd_info *mtd)
0300 {
0301     struct mtd_info *child, *next;
0302     int err;
0303 
0304     list_for_each_entry_safe(child, next, &mtd->partitions, part.node) {
0305         err = __mtd_del_partition(child);
0306         if (err)
0307             return err;
0308     }
0309 
0310     sysfs_remove_files(&mtd->dev.kobj, mtd_partition_attrs);
0311 
0312     err = del_mtd_device(mtd);
0313     if (err)
0314         return err;
0315 
0316     list_del(&mtd->part.node);
0317     free_partition(mtd);
0318 
0319     return 0;
0320 }
0321 
0322 /*
0323  * This function unregisters and destroy all slave MTD objects which are
0324  * attached to the given MTD object, recursively.
0325  */
0326 static int __del_mtd_partitions(struct mtd_info *mtd)
0327 {
0328     struct mtd_info *child, *next;
0329     LIST_HEAD(tmp_list);
0330     int ret, err = 0;
0331 
0332     list_for_each_entry_safe(child, next, &mtd->partitions, part.node) {
0333         if (mtd_has_partitions(child))
0334             __del_mtd_partitions(child);
0335 
0336         pr_info("Deleting %s MTD partition\n", child->name);
0337         ret = del_mtd_device(child);
0338         if (ret < 0) {
0339             pr_err("Error when deleting partition \"%s\" (%d)\n",
0340                    child->name, ret);
0341             err = ret;
0342             continue;
0343         }
0344 
0345         list_del(&child->part.node);
0346         free_partition(child);
0347     }
0348 
0349     return err;
0350 }
0351 
0352 int del_mtd_partitions(struct mtd_info *mtd)
0353 {
0354     struct mtd_info *master = mtd_get_master(mtd);
0355     int ret;
0356 
0357     pr_info("Deleting MTD partitions on \"%s\":\n", mtd->name);
0358 
0359     mutex_lock(&master->master.partitions_lock);
0360     ret = __del_mtd_partitions(mtd);
0361     mutex_unlock(&master->master.partitions_lock);
0362 
0363     return ret;
0364 }
0365 
0366 int mtd_del_partition(struct mtd_info *mtd, int partno)
0367 {
0368     struct mtd_info *child, *master = mtd_get_master(mtd);
0369     int ret = -EINVAL;
0370 
0371     mutex_lock(&master->master.partitions_lock);
0372     list_for_each_entry(child, &mtd->partitions, part.node) {
0373         if (child->index == partno) {
0374             ret = __mtd_del_partition(child);
0375             break;
0376         }
0377     }
0378     mutex_unlock(&master->master.partitions_lock);
0379 
0380     return ret;
0381 }
0382 EXPORT_SYMBOL_GPL(mtd_del_partition);
0383 
0384 /*
0385  * This function, given a parent MTD object and a partition table, creates
0386  * and registers the child MTD objects which are bound to the parent according
0387  * to the partition definitions.
0388  *
0389  * For historical reasons, this function's caller only registers the parent
0390  * if the MTD_PARTITIONED_MASTER config option is set.
0391  */
0392 
0393 int add_mtd_partitions(struct mtd_info *parent,
0394                const struct mtd_partition *parts,
0395                int nbparts)
0396 {
0397     struct mtd_info *child, *master = mtd_get_master(parent);
0398     uint64_t cur_offset = 0;
0399     int i, ret;
0400 
0401     printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n",
0402            nbparts, parent->name);
0403 
0404     for (i = 0; i < nbparts; i++) {
0405         child = allocate_partition(parent, parts + i, i, cur_offset);
0406         if (IS_ERR(child)) {
0407             ret = PTR_ERR(child);
0408             goto err_del_partitions;
0409         }
0410 
0411         mutex_lock(&master->master.partitions_lock);
0412         list_add_tail(&child->part.node, &parent->partitions);
0413         mutex_unlock(&master->master.partitions_lock);
0414 
0415         ret = add_mtd_device(child);
0416         if (ret) {
0417             mutex_lock(&master->master.partitions_lock);
0418             list_del(&child->part.node);
0419             mutex_unlock(&master->master.partitions_lock);
0420 
0421             free_partition(child);
0422             goto err_del_partitions;
0423         }
0424 
0425         mtd_add_partition_attrs(child);
0426 
0427         /* Look for subpartitions */
0428         parse_mtd_partitions(child, parts[i].types, NULL);
0429 
0430         cur_offset = child->part.offset + child->part.size;
0431     }
0432 
0433     return 0;
0434 
0435 err_del_partitions:
0436     del_mtd_partitions(master);
0437 
0438     return ret;
0439 }
0440 
0441 static DEFINE_SPINLOCK(part_parser_lock);
0442 static LIST_HEAD(part_parsers);
0443 
0444 static struct mtd_part_parser *mtd_part_parser_get(const char *name)
0445 {
0446     struct mtd_part_parser *p, *ret = NULL;
0447 
0448     spin_lock(&part_parser_lock);
0449 
0450     list_for_each_entry(p, &part_parsers, list)
0451         if (!strcmp(p->name, name) && try_module_get(p->owner)) {
0452             ret = p;
0453             break;
0454         }
0455 
0456     spin_unlock(&part_parser_lock);
0457 
0458     return ret;
0459 }
0460 
0461 static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
0462 {
0463     module_put(p->owner);
0464 }
0465 
0466 /*
0467  * Many partition parsers just expected the core to kfree() all their data in
0468  * one chunk. Do that by default.
0469  */
0470 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
0471                         int nr_parts)
0472 {
0473     kfree(pparts);
0474 }
0475 
0476 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
0477 {
0478     p->owner = owner;
0479 
0480     if (!p->cleanup)
0481         p->cleanup = &mtd_part_parser_cleanup_default;
0482 
0483     spin_lock(&part_parser_lock);
0484     list_add(&p->list, &part_parsers);
0485     spin_unlock(&part_parser_lock);
0486 
0487     return 0;
0488 }
0489 EXPORT_SYMBOL_GPL(__register_mtd_parser);
0490 
0491 void deregister_mtd_parser(struct mtd_part_parser *p)
0492 {
0493     spin_lock(&part_parser_lock);
0494     list_del(&p->list);
0495     spin_unlock(&part_parser_lock);
0496 }
0497 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
0498 
0499 /*
0500  * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
0501  * are changing this array!
0502  */
0503 static const char * const default_mtd_part_types[] = {
0504     "cmdlinepart",
0505     "ofpart",
0506     NULL
0507 };
0508 
0509 /* Check DT only when looking for subpartitions. */
0510 static const char * const default_subpartition_types[] = {
0511     "ofpart",
0512     NULL
0513 };
0514 
0515 static int mtd_part_do_parse(struct mtd_part_parser *parser,
0516                  struct mtd_info *master,
0517                  struct mtd_partitions *pparts,
0518                  struct mtd_part_parser_data *data)
0519 {
0520     int ret;
0521 
0522     ret = (*parser->parse_fn)(master, &pparts->parts, data);
0523     pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
0524     if (ret <= 0)
0525         return ret;
0526 
0527     pr_notice("%d %s partitions found on MTD device %s\n", ret,
0528           parser->name, master->name);
0529 
0530     pparts->nr_parts = ret;
0531     pparts->parser = parser;
0532 
0533     return ret;
0534 }
0535 
0536 /**
0537  * mtd_part_get_compatible_parser - find MTD parser by a compatible string
0538  *
0539  * @compat: compatible string describing partitions in a device tree
0540  *
0541  * MTD parsers can specify supported partitions by providing a table of
0542  * compatibility strings. This function finds a parser that advertises support
0543  * for a passed value of "compatible".
0544  */
0545 static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
0546 {
0547     struct mtd_part_parser *p, *ret = NULL;
0548 
0549     spin_lock(&part_parser_lock);
0550 
0551     list_for_each_entry(p, &part_parsers, list) {
0552         const struct of_device_id *matches;
0553 
0554         matches = p->of_match_table;
0555         if (!matches)
0556             continue;
0557 
0558         for (; matches->compatible[0]; matches++) {
0559             if (!strcmp(matches->compatible, compat) &&
0560                 try_module_get(p->owner)) {
0561                 ret = p;
0562                 break;
0563             }
0564         }
0565 
0566         if (ret)
0567             break;
0568     }
0569 
0570     spin_unlock(&part_parser_lock);
0571 
0572     return ret;
0573 }
0574 
0575 static int mtd_part_of_parse(struct mtd_info *master,
0576                  struct mtd_partitions *pparts)
0577 {
0578     struct mtd_part_parser *parser;
0579     struct device_node *np;
0580     struct property *prop;
0581     struct device *dev;
0582     const char *compat;
0583     const char *fixed = "fixed-partitions";
0584     int ret, err = 0;
0585 
0586     dev = &master->dev;
0587     /* Use parent device (controller) if the top level MTD is not registered */
0588     if (!IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) && !mtd_is_partition(master))
0589         dev = master->dev.parent;
0590 
0591     np = mtd_get_of_node(master);
0592     if (mtd_is_partition(master))
0593         of_node_get(np);
0594     else
0595         np = of_get_child_by_name(np, "partitions");
0596 
0597     of_property_for_each_string(np, "compatible", prop, compat) {
0598         parser = mtd_part_get_compatible_parser(compat);
0599         if (!parser)
0600             continue;
0601         ret = mtd_part_do_parse(parser, master, pparts, NULL);
0602         if (ret > 0) {
0603             of_platform_populate(np, NULL, NULL, dev);
0604             of_node_put(np);
0605             return ret;
0606         }
0607         mtd_part_parser_put(parser);
0608         if (ret < 0 && !err)
0609             err = ret;
0610     }
0611     of_platform_populate(np, NULL, NULL, dev);
0612     of_node_put(np);
0613 
0614     /*
0615      * For backward compatibility we have to try the "fixed-partitions"
0616      * parser. It supports old DT format with partitions specified as a
0617      * direct subnodes of a flash device DT node without any compatibility
0618      * specified we could match.
0619      */
0620     parser = mtd_part_parser_get(fixed);
0621     if (!parser && !request_module("%s", fixed))
0622         parser = mtd_part_parser_get(fixed);
0623     if (parser) {
0624         ret = mtd_part_do_parse(parser, master, pparts, NULL);
0625         if (ret > 0)
0626             return ret;
0627         mtd_part_parser_put(parser);
0628         if (ret < 0 && !err)
0629             err = ret;
0630     }
0631 
0632     return err;
0633 }
0634 
0635 /**
0636  * parse_mtd_partitions - parse and register MTD partitions
0637  *
0638  * @master: the master partition (describes whole MTD device)
0639  * @types: names of partition parsers to try or %NULL
0640  * @data: MTD partition parser-specific data
0641  *
0642  * This function tries to find & register partitions on MTD device @master. It
0643  * uses MTD partition parsers, specified in @types. However, if @types is %NULL,
0644  * then the default list of parsers is used. The default list contains only the
0645  * "cmdlinepart" and "ofpart" parsers ATM.
0646  * Note: If there are more then one parser in @types, the kernel only takes the
0647  * partitions parsed out by the first parser.
0648  *
0649  * This function may return:
0650  * o a negative error code in case of failure
0651  * o number of found partitions otherwise
0652  */
0653 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
0654              struct mtd_part_parser_data *data)
0655 {
0656     struct mtd_partitions pparts = { };
0657     struct mtd_part_parser *parser;
0658     int ret, err = 0;
0659 
0660     if (!types)
0661         types = mtd_is_partition(master) ? default_subpartition_types :
0662             default_mtd_part_types;
0663 
0664     for ( ; *types; types++) {
0665         /*
0666          * ofpart is a special type that means OF partitioning info
0667          * should be used. It requires a bit different logic so it is
0668          * handled in a separated function.
0669          */
0670         if (!strcmp(*types, "ofpart")) {
0671             ret = mtd_part_of_parse(master, &pparts);
0672         } else {
0673             pr_debug("%s: parsing partitions %s\n", master->name,
0674                  *types);
0675             parser = mtd_part_parser_get(*types);
0676             if (!parser && !request_module("%s", *types))
0677                 parser = mtd_part_parser_get(*types);
0678             pr_debug("%s: got parser %s\n", master->name,
0679                 parser ? parser->name : NULL);
0680             if (!parser)
0681                 continue;
0682             ret = mtd_part_do_parse(parser, master, &pparts, data);
0683             if (ret <= 0)
0684                 mtd_part_parser_put(parser);
0685         }
0686         /* Found partitions! */
0687         if (ret > 0) {
0688             err = add_mtd_partitions(master, pparts.parts,
0689                          pparts.nr_parts);
0690             mtd_part_parser_cleanup(&pparts);
0691             return err ? err : pparts.nr_parts;
0692         }
0693         /*
0694          * Stash the first error we see; only report it if no parser
0695          * succeeds
0696          */
0697         if (ret < 0 && !err)
0698             err = ret;
0699     }
0700     return err;
0701 }
0702 
0703 void mtd_part_parser_cleanup(struct mtd_partitions *parts)
0704 {
0705     const struct mtd_part_parser *parser;
0706 
0707     if (!parts)
0708         return;
0709 
0710     parser = parts->parser;
0711     if (parser) {
0712         if (parser->cleanup)
0713             parser->cleanup(parts->parts, parts->nr_parts);
0714 
0715         mtd_part_parser_put(parser);
0716     }
0717 }
0718 
0719 /* Returns the size of the entire flash chip */
0720 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
0721 {
0722     struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd);
0723 
0724     return master->size;
0725 }
0726 EXPORT_SYMBOL_GPL(mtd_get_device_size);