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