Linux Audio

Check our new training course

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