xref: /linux/drivers/mtd/mtdpart.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Simple MTD partitioning layer
4  *
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>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/list.h>
15 #include <linux/kmod.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/partitions.h>
18 #include <linux/err.h>
19 #include <linux/of.h>
20 #include <linux/of_platform.h>
21 #include <linux/mtd/concat.h>
22 
23 #include "mtdcore.h"
24 
25 /*
26  * MTD methods which simply translate the effective address and pass through
27  * to the _real_ device.
28  */
29 
30 static inline void free_partition(struct mtd_info *mtd)
31 {
32 	kfree(mtd->name);
33 	kfree(mtd);
34 }
35 
36 void release_mtd_partition(struct mtd_info *mtd)
37 {
38 	WARN_ON(!list_empty(&mtd->part.node));
39 	free_partition(mtd);
40 }
41 
42 static struct mtd_info *allocate_partition(struct mtd_info *parent,
43 					   const struct mtd_partition *part,
44 					   int partno, uint64_t cur_offset)
45 {
46 	struct mtd_info *master = mtd_get_master(parent);
47 	int wr_alignment = (parent->flags & MTD_NO_ERASE) ?
48 			   master->writesize : master->erasesize;
49 	u64 parent_size = mtd_is_partition(parent) ?
50 			  parent->part.size : parent->size;
51 	struct mtd_info *child;
52 	u32 remainder;
53 	char *name;
54 	u64 tmp;
55 
56 	/* allocate the partition structure */
57 	child = kzalloc_obj(*child);
58 	name = kstrdup(part->name, GFP_KERNEL);
59 	if (!name || !child) {
60 		printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
61 		       parent->name);
62 		kfree(name);
63 		kfree(child);
64 		return ERR_PTR(-ENOMEM);
65 	}
66 
67 	/* set up the MTD object for this partition */
68 	child->type = parent->type;
69 	child->part.flags = parent->flags & ~part->mask_flags;
70 	child->part.flags |= part->add_flags;
71 	child->flags = child->part.flags;
72 	child->part.size = part->size;
73 	child->writesize = parent->writesize;
74 	child->writebufsize = parent->writebufsize;
75 	child->oobsize = parent->oobsize;
76 	child->oobavail = parent->oobavail;
77 	child->subpage_sft = parent->subpage_sft;
78 
79 	child->name = name;
80 	child->owner = parent->owner;
81 
82 	/* NOTE: Historically, we didn't arrange MTDs as a tree out of
83 	 * concern for showing the same data in multiple partitions.
84 	 * However, it is very useful to have the master node present,
85 	 * so the MTD_PARTITIONED_MASTER option allows that. The master
86 	 * will have device nodes etc only if this is set, so make the
87 	 * parent conditional on that option. Note, this is a way to
88 	 * distinguish between the parent and its partitions in sysfs.
89 	 */
90 	child->dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
91 			    &parent->dev : parent->dev.parent;
92 	child->dev.of_node = part->of_node;
93 	child->parent = parent;
94 	child->part.offset = part->offset;
95 	INIT_LIST_HEAD(&child->partitions);
96 
97 	if (child->part.offset == MTDPART_OFS_APPEND)
98 		child->part.offset = cur_offset;
99 	if (child->part.offset == MTDPART_OFS_NXTBLK) {
100 		tmp = cur_offset;
101 		child->part.offset = cur_offset;
102 		remainder = do_div(tmp, wr_alignment);
103 		if (remainder) {
104 			child->part.offset += wr_alignment - remainder;
105 			printk(KERN_NOTICE "Moving partition %d: "
106 			       "0x%012llx -> 0x%012llx\n", partno,
107 			       (unsigned long long)cur_offset,
108 			       child->part.offset);
109 		}
110 	}
111 	if (child->part.offset == MTDPART_OFS_RETAIN) {
112 		child->part.offset = cur_offset;
113 		if (parent_size - child->part.offset >= child->part.size) {
114 			child->part.size = parent_size - child->part.offset -
115 					   child->part.size;
116 		} else {
117 			printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
118 				part->name, parent_size - child->part.offset,
119 				child->part.size);
120 			/* register to preserve ordering */
121 			child->part.offset = 0;
122 			child->part.size = 0;
123 			child->erasesize = parent->erasesize;
124 			goto out_register;
125 		}
126 	}
127 	if (child->part.size == MTDPART_SIZ_FULL)
128 		child->part.size = parent_size - child->part.offset;
129 
130 	printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n",
131 	       child->part.offset, child->part.offset + child->part.size,
132 	       child->name);
133 
134 	/* let's do some sanity checks */
135 	if (child->part.offset >= parent_size) {
136 		/* let's register it anyway to preserve ordering */
137 		child->part.offset = 0;
138 		child->part.size = 0;
139 
140 		/* Initialize ->erasesize to make add_mtd_device() happy. */
141 		child->erasesize = parent->erasesize;
142 		printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
143 			part->name);
144 		goto out_register;
145 	}
146 	if (child->part.offset + child->part.size > parent->size) {
147 		child->part.size = parent_size - child->part.offset;
148 		printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
149 			part->name, parent->name, child->part.size);
150 	}
151 
152 	if (parent->numeraseregions > 1) {
153 		/* Deal with variable erase size stuff */
154 		int i, max = parent->numeraseregions;
155 		u64 end = child->part.offset + child->part.size;
156 		struct mtd_erase_region_info *regions = parent->eraseregions;
157 
158 		/* Find the first erase regions which is part of this
159 		 * partition. */
160 		for (i = 0; i < max && regions[i].offset <= child->part.offset;
161 		     i++)
162 			;
163 		/* The loop searched for the region _behind_ the first one */
164 		if (i > 0)
165 			i--;
166 
167 		/* Pick biggest erasesize */
168 		for (; i < max && regions[i].offset < end; i++) {
169 			if (child->erasesize < regions[i].erasesize)
170 				child->erasesize = regions[i].erasesize;
171 		}
172 		BUG_ON(child->erasesize == 0);
173 	} else {
174 		/* Single erase size */
175 		child->erasesize = master->erasesize;
176 	}
177 
178 	/*
179 	 * Child erasesize might differ from the parent one if the parent
180 	 * exposes several regions with different erasesize. Adjust
181 	 * wr_alignment accordingly.
182 	 */
183 	if (!(child->flags & MTD_NO_ERASE))
184 		wr_alignment = child->erasesize;
185 
186 	tmp = mtd_get_master_ofs(child, 0);
187 	remainder = do_div(tmp, wr_alignment);
188 	if ((child->flags & MTD_WRITEABLE) && remainder) {
189 		/* Doesn't start on a boundary of major erase size */
190 		/* FIXME: Let it be writable if it is on a boundary of
191 		 * _minor_ erase size though */
192 		child->flags &= ~MTD_WRITEABLE;
193 		printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
194 			part->name);
195 	}
196 
197 	tmp = mtd_get_master_ofs(child, 0) + child->part.size;
198 	remainder = do_div(tmp, wr_alignment);
199 	if ((child->flags & MTD_WRITEABLE) && remainder) {
200 		child->flags &= ~MTD_WRITEABLE;
201 		printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
202 			part->name);
203 	}
204 
205 	child->size = child->part.size;
206 	child->ecc_step_size = parent->ecc_step_size;
207 	child->ecc_strength = parent->ecc_strength;
208 	child->bitflip_threshold = parent->bitflip_threshold;
209 
210 	if (master->_block_isbad) {
211 		uint64_t offs = 0;
212 
213 		while (offs < child->part.size) {
214 			if (mtd_block_isreserved(child, offs))
215 				child->ecc_stats.bbtblocks++;
216 			else if (mtd_block_isbad(child, offs))
217 				child->ecc_stats.badblocks++;
218 			offs += child->erasesize;
219 		}
220 	}
221 
222 out_register:
223 	return child;
224 }
225 
226 static ssize_t offset_show(struct device *dev,
227 			   struct device_attribute *attr, char *buf)
228 {
229 	struct mtd_info *mtd = dev_get_drvdata(dev);
230 
231 	return sysfs_emit(buf, "%lld\n", mtd->part.offset);
232 }
233 static DEVICE_ATTR_RO(offset);	/* mtd partition offset */
234 
235 static const struct attribute *mtd_partition_attrs[] = {
236 	&dev_attr_offset.attr,
237 	NULL
238 };
239 
240 static int mtd_add_partition_attrs(struct mtd_info *new)
241 {
242 	int ret = sysfs_create_files(&new->dev.kobj, mtd_partition_attrs);
243 	if (ret)
244 		printk(KERN_WARNING
245 		       "mtd: failed to create partition attrs, err=%d\n", ret);
246 	return ret;
247 }
248 
249 int mtd_add_partition(struct mtd_info *parent, const char *name,
250 		      long long offset, long long length)
251 {
252 	struct mtd_info *master = mtd_get_master(parent);
253 	u64 parent_size = mtd_is_partition(parent) ?
254 			  parent->part.size : parent->size;
255 	struct mtd_partition part;
256 	struct mtd_info *child;
257 	int ret = 0;
258 
259 	/* the direct offset is expected */
260 	if (offset == MTDPART_OFS_APPEND ||
261 	    offset == MTDPART_OFS_NXTBLK)
262 		return -EINVAL;
263 
264 	if (length == MTDPART_SIZ_FULL)
265 		length = parent_size - offset;
266 
267 	if (length <= 0)
268 		return -EINVAL;
269 
270 	if (offset < 0 || offset >= (long long)parent_size)
271 		return -EINVAL;
272 
273 	if ((u64)offset + (u64)length > parent_size)
274 		return -EINVAL;
275 	memset(&part, 0, sizeof(part));
276 	part.name = name;
277 	part.size = length;
278 	part.offset = offset;
279 
280 	child = allocate_partition(parent, &part, -1, offset);
281 	if (IS_ERR(child))
282 		return PTR_ERR(child);
283 
284 	mutex_lock(&master->master.partitions_lock);
285 	list_add_tail(&child->part.node, &parent->partitions);
286 	mutex_unlock(&master->master.partitions_lock);
287 
288 	ret = add_mtd_device(child);
289 	if (ret)
290 		goto err_remove_part;
291 
292 	mtd_add_partition_attrs(child);
293 
294 	return 0;
295 
296 err_remove_part:
297 	mutex_lock(&master->master.partitions_lock);
298 	list_del(&child->part.node);
299 	mutex_unlock(&master->master.partitions_lock);
300 
301 	free_partition(child);
302 
303 	return ret;
304 }
305 EXPORT_SYMBOL_GPL(mtd_add_partition);
306 
307 /**
308  * __mtd_del_partition - delete MTD partition
309  *
310  * @mtd: MTD structure to be deleted
311  *
312  * This function must be called with the partitions mutex locked.
313  */
314 static int __mtd_del_partition(struct mtd_info *mtd)
315 {
316 	struct mtd_info *child, *next;
317 	int err;
318 
319 	list_for_each_entry_safe(child, next, &mtd->partitions, part.node) {
320 		err = __mtd_del_partition(child);
321 		if (err)
322 			return err;
323 	}
324 
325 	sysfs_remove_files(&mtd->dev.kobj, mtd_partition_attrs);
326 
327 	list_del_init(&mtd->part.node);
328 	err = del_mtd_device(mtd);
329 	if (err)
330 		return err;
331 
332 	return 0;
333 }
334 
335 /*
336  * This function unregisters and destroy all slave MTD objects which are
337  * attached to the given MTD object, recursively.
338  */
339 static int __del_mtd_partitions(struct mtd_info *mtd)
340 {
341 	struct mtd_info *child, *next;
342 	int ret, err = 0;
343 
344 	list_for_each_entry_safe(child, next, &mtd->partitions, part.node) {
345 		if (mtd_has_partitions(child))
346 			__del_mtd_partitions(child);
347 
348 		pr_info("Deleting %s MTD partition\n", child->name);
349 		list_del_init(&child->part.node);
350 		ret = del_mtd_device(child);
351 		if (ret < 0) {
352 			pr_err("Error when deleting partition \"%s\" (%d)\n",
353 			       child->name, ret);
354 			err = ret;
355 			continue;
356 		}
357 	}
358 
359 	return err;
360 }
361 
362 int del_mtd_partitions(struct mtd_info *mtd)
363 {
364 	struct mtd_info *master = mtd_get_master(mtd);
365 	int ret;
366 
367 	pr_info("Deleting MTD partitions on \"%s\":\n", mtd->name);
368 
369 	mutex_lock(&master->master.partitions_lock);
370 	ret = __del_mtd_partitions(mtd);
371 	mutex_unlock(&master->master.partitions_lock);
372 
373 	return ret;
374 }
375 
376 int mtd_del_partition(struct mtd_info *mtd, int partno)
377 {
378 	struct mtd_info *child, *master = mtd_get_master(mtd);
379 	int ret = -EINVAL;
380 
381 	mutex_lock(&master->master.partitions_lock);
382 	list_for_each_entry(child, &mtd->partitions, part.node) {
383 		if (child->index == partno) {
384 			ret = __mtd_del_partition(child);
385 			break;
386 		}
387 	}
388 	mutex_unlock(&master->master.partitions_lock);
389 
390 	return ret;
391 }
392 EXPORT_SYMBOL_GPL(mtd_del_partition);
393 
394 /*
395  * This function, given a parent MTD object and a partition table, creates
396  * and registers the child MTD objects which are bound to the parent according
397  * to the partition definitions.
398  *
399  * For historical reasons, this function's caller only registers the parent
400  * if the MTD_PARTITIONED_MASTER config option is set.
401  */
402 
403 int add_mtd_partitions(struct mtd_info *parent,
404 		       const struct mtd_partition *parts,
405 		       int nbparts)
406 {
407 	struct mtd_info *child, *master = mtd_get_master(parent);
408 	uint64_t cur_offset = 0;
409 	int i, ret;
410 
411 	printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n",
412 	       nbparts, parent->name);
413 
414 	for (i = 0; i < nbparts; i++) {
415 		child = allocate_partition(parent, parts + i, i, cur_offset);
416 		if (IS_ERR(child)) {
417 			ret = PTR_ERR(child);
418 			goto err_del_partitions;
419 		}
420 
421 		if (IS_REACHABLE(CONFIG_MTD_VIRT_CONCAT)) {
422 			if (mtd_virt_concat_add(child))
423 				continue;
424 		}
425 
426 		mutex_lock(&master->master.partitions_lock);
427 		list_add_tail(&child->part.node, &parent->partitions);
428 		mutex_unlock(&master->master.partitions_lock);
429 
430 		ret = add_mtd_device(child);
431 		if (ret) {
432 			mutex_lock(&master->master.partitions_lock);
433 			list_del(&child->part.node);
434 			mutex_unlock(&master->master.partitions_lock);
435 
436 			free_partition(child);
437 			goto err_del_partitions;
438 		}
439 
440 		mtd_add_partition_attrs(child);
441 
442 		/* Look for subpartitions (skip if no maching parser found) */
443 		ret = parse_mtd_partitions(child, parts[i].types, NULL);
444 		if (ret < 0 && ret == -ENOENT) {
445 			pr_debug("Skip parsing subpartitions: %d\n", ret);
446 			continue;
447 		} else if (ret < 0) {
448 			pr_err("Failed to parse subpartitions: %d\n", ret);
449 			goto err_del_partitions;
450 		}
451 
452 		cur_offset = child->part.offset + child->part.size;
453 	}
454 
455 	return 0;
456 
457 err_del_partitions:
458 	del_mtd_partitions(master);
459 
460 	return ret;
461 }
462 
463 static DEFINE_SPINLOCK(part_parser_lock);
464 static LIST_HEAD(part_parsers);
465 
466 static struct mtd_part_parser *mtd_part_parser_get(const char *name)
467 {
468 	struct mtd_part_parser *p, *ret = NULL;
469 
470 	spin_lock(&part_parser_lock);
471 
472 	list_for_each_entry(p, &part_parsers, list)
473 		if (!strcmp(p->name, name) && try_module_get(p->owner)) {
474 			ret = p;
475 			break;
476 		}
477 
478 	spin_unlock(&part_parser_lock);
479 
480 	return ret;
481 }
482 
483 static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
484 {
485 	module_put(p->owner);
486 }
487 
488 /*
489  * Many partition parsers just expected the core to kfree() all their data in
490  * one chunk. Do that by default.
491  */
492 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
493 					    int nr_parts)
494 {
495 	kfree(pparts);
496 }
497 
498 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
499 {
500 	p->owner = owner;
501 
502 	if (!p->cleanup)
503 		p->cleanup = &mtd_part_parser_cleanup_default;
504 
505 	spin_lock(&part_parser_lock);
506 	list_add(&p->list, &part_parsers);
507 	spin_unlock(&part_parser_lock);
508 
509 	return 0;
510 }
511 EXPORT_SYMBOL_GPL(__register_mtd_parser);
512 
513 void deregister_mtd_parser(struct mtd_part_parser *p)
514 {
515 	spin_lock(&part_parser_lock);
516 	list_del(&p->list);
517 	spin_unlock(&part_parser_lock);
518 }
519 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
520 
521 /*
522  * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
523  * are changing this array!
524  */
525 static const char * const default_mtd_part_types[] = {
526 	"cmdlinepart",
527 	"ofpart",
528 	NULL
529 };
530 
531 /* Check DT only when looking for subpartitions. */
532 static const char * const default_subpartition_types[] = {
533 	"ofpart",
534 	NULL
535 };
536 
537 static int mtd_part_do_parse(struct mtd_part_parser *parser,
538 			     struct mtd_info *master,
539 			     struct mtd_partitions *pparts,
540 			     struct mtd_part_parser_data *data)
541 {
542 	int ret;
543 
544 	ret = (*parser->parse_fn)(master, &pparts->parts, data);
545 	pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
546 	if (ret <= 0)
547 		return ret;
548 
549 	pr_notice("%d %s partitions found on MTD device %s\n", ret,
550 		  parser->name, master->name);
551 
552 	pparts->nr_parts = ret;
553 	pparts->parser = parser;
554 
555 	return ret;
556 }
557 
558 /**
559  * mtd_part_get_compatible_parser - find MTD parser by a compatible string
560  *
561  * @compat: compatible string describing partitions in a device tree
562  *
563  * MTD parsers can specify supported partitions by providing a table of
564  * compatibility strings. This function finds a parser that advertises support
565  * for a passed value of "compatible".
566  */
567 static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
568 {
569 	struct mtd_part_parser *p, *ret = NULL;
570 
571 	spin_lock(&part_parser_lock);
572 
573 	list_for_each_entry(p, &part_parsers, list) {
574 		const struct of_device_id *matches;
575 
576 		matches = p->of_match_table;
577 		if (!matches)
578 			continue;
579 
580 		for (; matches->compatible[0]; matches++) {
581 			if (!strcmp(matches->compatible, compat) &&
582 			    try_module_get(p->owner)) {
583 				ret = p;
584 				break;
585 			}
586 		}
587 
588 		if (ret)
589 			break;
590 	}
591 
592 	spin_unlock(&part_parser_lock);
593 
594 	return ret;
595 }
596 
597 static int mtd_part_of_parse(struct mtd_info *master,
598 			     struct mtd_partitions *pparts)
599 {
600 	struct mtd_part_parser *parser;
601 	struct device_node *np;
602 	struct device_node *child;
603 	struct property *prop;
604 	struct device *dev;
605 	const char *compat;
606 	const char *fixed = "fixed-partitions";
607 	int ret, err = 0;
608 
609 	dev = &master->dev;
610 	/* Use parent device (controller) if the top level MTD is not registered */
611 	if (!IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) && !mtd_is_partition(master))
612 		dev = master->dev.parent;
613 
614 	np = mtd_get_of_node(master);
615 	if (mtd_is_partition(master))
616 		of_node_get(np);
617 	else
618 		np = of_get_child_by_name(np, "partitions");
619 
620 	/*
621 	 * Don't create devices that are added to a bus but will never get
622 	 * probed. That'll cause fw_devlink to block probing of consumers of
623 	 * this partition until the partition device is probed.
624 	 */
625 	for_each_child_of_node(np, child)
626 		if (of_device_is_compatible(child, "nvmem-cells"))
627 			of_node_set_flag(child, OF_POPULATED);
628 
629 	of_property_for_each_string(np, "compatible", prop, compat) {
630 		parser = mtd_part_get_compatible_parser(compat);
631 		if (!parser)
632 			continue;
633 		ret = mtd_part_do_parse(parser, master, pparts, NULL);
634 		if (ret > 0) {
635 			of_platform_populate(np, NULL, NULL, dev);
636 			of_node_put(np);
637 			return ret;
638 		}
639 		mtd_part_parser_put(parser);
640 		if (ret < 0 && !err)
641 			err = ret;
642 	}
643 	of_platform_populate(np, NULL, NULL, dev);
644 	of_node_put(np);
645 
646 	/*
647 	 * For backward compatibility we have to try the "fixed-partitions"
648 	 * parser. It supports old DT format with partitions specified as a
649 	 * direct subnodes of a flash device DT node without any compatibility
650 	 * specified we could match.
651 	 */
652 	parser = mtd_part_parser_get(fixed);
653 	if (!parser && !request_module("%s", fixed))
654 		parser = mtd_part_parser_get(fixed);
655 	if (parser) {
656 		ret = mtd_part_do_parse(parser, master, pparts, NULL);
657 		if (ret > 0)
658 			return ret;
659 		mtd_part_parser_put(parser);
660 		if (ret < 0 && !err)
661 			err = ret;
662 	}
663 
664 	return err;
665 }
666 
667 /**
668  * parse_mtd_partitions - parse and register MTD partitions
669  *
670  * @master: the master partition (describes whole MTD device)
671  * @types: names of partition parsers to try or %NULL
672  * @data: MTD partition parser-specific data
673  *
674  * This function tries to find & register partitions on MTD device @master. It
675  * uses MTD partition parsers, specified in @types. However, if @types is %NULL,
676  * then the default list of parsers is used. The default list contains only the
677  * "cmdlinepart" and "ofpart" parsers ATM.
678  * Note: If there are more then one parser in @types, the kernel only takes the
679  * partitions parsed out by the first parser.
680  *
681  * This function may return:
682  * o a negative error code in case of failure
683  * o number of found partitions otherwise
684  */
685 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
686 			 struct mtd_part_parser_data *data)
687 {
688 	struct mtd_partitions pparts = { };
689 	struct mtd_part_parser *parser;
690 	int ret, err = 0;
691 
692 	if (!types)
693 		types = mtd_is_partition(master) ? default_subpartition_types :
694 			default_mtd_part_types;
695 
696 	for ( ; *types; types++) {
697 		/*
698 		 * ofpart is a special type that means OF partitioning info
699 		 * should be used. It requires a bit different logic so it is
700 		 * handled in a separated function.
701 		 */
702 		if (!strcmp(*types, "ofpart")) {
703 			ret = mtd_part_of_parse(master, &pparts);
704 		} else {
705 			pr_debug("%s: parsing partitions %s\n", master->name,
706 				 *types);
707 			parser = mtd_part_parser_get(*types);
708 			if (!parser && !request_module("%s", *types))
709 				parser = mtd_part_parser_get(*types);
710 			if (!parser)
711 				continue;
712 			pr_debug("%s: got parser %s\n", master->name, parser->name);
713 			ret = mtd_part_do_parse(parser, master, &pparts, data);
714 			if (ret <= 0)
715 				mtd_part_parser_put(parser);
716 		}
717 		/* Found partitions! */
718 		if (ret > 0) {
719 			err = add_mtd_partitions(master, pparts.parts,
720 						 pparts.nr_parts);
721 			mtd_part_parser_cleanup(&pparts);
722 			return err ? err : pparts.nr_parts;
723 		}
724 		/*
725 		 * Stash the first error we see; only report it if no parser
726 		 * succeeds
727 		 */
728 		if (ret < 0 && !err)
729 			err = ret;
730 	}
731 	return err;
732 }
733 
734 void mtd_part_parser_cleanup(struct mtd_partitions *parts)
735 {
736 	const struct mtd_part_parser *parser;
737 
738 	if (!parts)
739 		return;
740 
741 	parser = parts->parser;
742 	if (parser) {
743 		if (parser->cleanup)
744 			parser->cleanup(parts->parts, parts->nr_parts);
745 
746 		mtd_part_parser_put(parser);
747 	}
748 }
749 
750 /* Returns the size of the entire flash chip */
751 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
752 {
753 	struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd);
754 
755 	return master->size;
756 }
757 EXPORT_SYMBOL_GPL(mtd_get_device_size);
758