xref: /linux/drivers/mtd/mtdpart.c (revision 9bacbced0e32204deb8b9d011279f9beddd8c2ef)
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 #include <linux/of.h>
34 
35 #include "mtdcore.h"
36 
37 /* Our partition linked list */
38 static LIST_HEAD(mtd_partitions);
39 static DEFINE_MUTEX(mtd_partitions_mutex);
40 
41 /**
42  * struct mtd_part - our partition node structure
43  *
44  * @mtd: struct holding partition details
45  * @parent: parent mtd - flash device or another partition
46  * @offset: partition offset relative to the *flash device*
47  */
48 struct mtd_part {
49 	struct mtd_info mtd;
50 	struct mtd_info *parent;
51 	uint64_t offset;
52 	struct list_head list;
53 };
54 
55 /*
56  * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
57  * the pointer to that structure.
58  */
59 static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd)
60 {
61 	return container_of(mtd, struct mtd_part, mtd);
62 }
63 
64 
65 /*
66  * MTD methods which simply translate the effective address and pass through
67  * to the _real_ device.
68  */
69 
70 static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
71 		size_t *retlen, u_char *buf)
72 {
73 	struct mtd_part *part = mtd_to_part(mtd);
74 	struct mtd_ecc_stats stats;
75 	int res;
76 
77 	stats = part->parent->ecc_stats;
78 	res = part->parent->_read(part->parent, from + part->offset, len,
79 				  retlen, buf);
80 	if (unlikely(mtd_is_eccerr(res)))
81 		mtd->ecc_stats.failed +=
82 			part->parent->ecc_stats.failed - stats.failed;
83 	else
84 		mtd->ecc_stats.corrected +=
85 			part->parent->ecc_stats.corrected - stats.corrected;
86 	return res;
87 }
88 
89 static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
90 		size_t *retlen, void **virt, resource_size_t *phys)
91 {
92 	struct mtd_part *part = mtd_to_part(mtd);
93 
94 	return part->parent->_point(part->parent, from + part->offset, len,
95 				    retlen, virt, phys);
96 }
97 
98 static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
99 {
100 	struct mtd_part *part = mtd_to_part(mtd);
101 
102 	return part->parent->_unpoint(part->parent, from + part->offset, len);
103 }
104 
105 static int part_read_oob(struct mtd_info *mtd, loff_t from,
106 		struct mtd_oob_ops *ops)
107 {
108 	struct mtd_part *part = mtd_to_part(mtd);
109 	struct mtd_ecc_stats stats;
110 	int res;
111 
112 	stats = part->parent->ecc_stats;
113 	res = part->parent->_read_oob(part->parent, from + part->offset, ops);
114 	if (unlikely(mtd_is_eccerr(res)))
115 		mtd->ecc_stats.failed +=
116 			part->parent->ecc_stats.failed - stats.failed;
117 	else
118 		mtd->ecc_stats.corrected +=
119 			part->parent->ecc_stats.corrected - stats.corrected;
120 	return res;
121 }
122 
123 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
124 		size_t len, size_t *retlen, u_char *buf)
125 {
126 	struct mtd_part *part = mtd_to_part(mtd);
127 	return part->parent->_read_user_prot_reg(part->parent, from, len,
128 						 retlen, buf);
129 }
130 
131 static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
132 				   size_t *retlen, struct otp_info *buf)
133 {
134 	struct mtd_part *part = mtd_to_part(mtd);
135 	return part->parent->_get_user_prot_info(part->parent, len, retlen,
136 						 buf);
137 }
138 
139 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
140 		size_t len, size_t *retlen, u_char *buf)
141 {
142 	struct mtd_part *part = mtd_to_part(mtd);
143 	return part->parent->_read_fact_prot_reg(part->parent, from, len,
144 						 retlen, buf);
145 }
146 
147 static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
148 				   size_t *retlen, struct otp_info *buf)
149 {
150 	struct mtd_part *part = mtd_to_part(mtd);
151 	return part->parent->_get_fact_prot_info(part->parent, len, retlen,
152 						 buf);
153 }
154 
155 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
156 		size_t *retlen, const u_char *buf)
157 {
158 	struct mtd_part *part = mtd_to_part(mtd);
159 	return part->parent->_write(part->parent, to + part->offset, len,
160 				    retlen, buf);
161 }
162 
163 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
164 		size_t *retlen, const u_char *buf)
165 {
166 	struct mtd_part *part = mtd_to_part(mtd);
167 	return part->parent->_panic_write(part->parent, to + part->offset, len,
168 					  retlen, buf);
169 }
170 
171 static int part_write_oob(struct mtd_info *mtd, loff_t to,
172 		struct mtd_oob_ops *ops)
173 {
174 	struct mtd_part *part = mtd_to_part(mtd);
175 
176 	return part->parent->_write_oob(part->parent, to + part->offset, ops);
177 }
178 
179 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
180 		size_t len, size_t *retlen, u_char *buf)
181 {
182 	struct mtd_part *part = mtd_to_part(mtd);
183 	return part->parent->_write_user_prot_reg(part->parent, from, len,
184 						  retlen, buf);
185 }
186 
187 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
188 		size_t len)
189 {
190 	struct mtd_part *part = mtd_to_part(mtd);
191 	return part->parent->_lock_user_prot_reg(part->parent, from, len);
192 }
193 
194 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
195 		unsigned long count, loff_t to, size_t *retlen)
196 {
197 	struct mtd_part *part = mtd_to_part(mtd);
198 	return part->parent->_writev(part->parent, vecs, count,
199 				     to + part->offset, retlen);
200 }
201 
202 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
203 {
204 	struct mtd_part *part = mtd_to_part(mtd);
205 	int ret;
206 
207 	instr->addr += part->offset;
208 	ret = part->parent->_erase(part->parent, instr);
209 	if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
210 		instr->fail_addr -= part->offset;
211 	instr->addr -= part->offset;
212 
213 	return ret;
214 }
215 
216 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
217 {
218 	struct mtd_part *part = mtd_to_part(mtd);
219 	return part->parent->_lock(part->parent, ofs + part->offset, len);
220 }
221 
222 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
223 {
224 	struct mtd_part *part = mtd_to_part(mtd);
225 	return part->parent->_unlock(part->parent, ofs + part->offset, len);
226 }
227 
228 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
229 {
230 	struct mtd_part *part = mtd_to_part(mtd);
231 	return part->parent->_is_locked(part->parent, ofs + part->offset, len);
232 }
233 
234 static void part_sync(struct mtd_info *mtd)
235 {
236 	struct mtd_part *part = mtd_to_part(mtd);
237 	part->parent->_sync(part->parent);
238 }
239 
240 static int part_suspend(struct mtd_info *mtd)
241 {
242 	struct mtd_part *part = mtd_to_part(mtd);
243 	return part->parent->_suspend(part->parent);
244 }
245 
246 static void part_resume(struct mtd_info *mtd)
247 {
248 	struct mtd_part *part = mtd_to_part(mtd);
249 	part->parent->_resume(part->parent);
250 }
251 
252 static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
253 {
254 	struct mtd_part *part = mtd_to_part(mtd);
255 	ofs += part->offset;
256 	return part->parent->_block_isreserved(part->parent, ofs);
257 }
258 
259 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
260 {
261 	struct mtd_part *part = mtd_to_part(mtd);
262 	ofs += part->offset;
263 	return part->parent->_block_isbad(part->parent, ofs);
264 }
265 
266 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
267 {
268 	struct mtd_part *part = mtd_to_part(mtd);
269 	int res;
270 
271 	ofs += part->offset;
272 	res = part->parent->_block_markbad(part->parent, ofs);
273 	if (!res)
274 		mtd->ecc_stats.badblocks++;
275 	return res;
276 }
277 
278 static int part_get_device(struct mtd_info *mtd)
279 {
280 	struct mtd_part *part = mtd_to_part(mtd);
281 	return part->parent->_get_device(part->parent);
282 }
283 
284 static void part_put_device(struct mtd_info *mtd)
285 {
286 	struct mtd_part *part = mtd_to_part(mtd);
287 	part->parent->_put_device(part->parent);
288 }
289 
290 static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
291 			      struct mtd_oob_region *oobregion)
292 {
293 	struct mtd_part *part = mtd_to_part(mtd);
294 
295 	return mtd_ooblayout_ecc(part->parent, section, oobregion);
296 }
297 
298 static int part_ooblayout_free(struct mtd_info *mtd, int section,
299 			       struct mtd_oob_region *oobregion)
300 {
301 	struct mtd_part *part = mtd_to_part(mtd);
302 
303 	return mtd_ooblayout_free(part->parent, section, oobregion);
304 }
305 
306 static const struct mtd_ooblayout_ops part_ooblayout_ops = {
307 	.ecc = part_ooblayout_ecc,
308 	.free = part_ooblayout_free,
309 };
310 
311 static int part_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
312 {
313 	struct mtd_part *part = mtd_to_part(mtd);
314 
315 	return part->parent->_max_bad_blocks(part->parent,
316 					     ofs + part->offset, len);
317 }
318 
319 static inline void free_partition(struct mtd_part *p)
320 {
321 	kfree(p->mtd.name);
322 	kfree(p);
323 }
324 
325 /**
326  * mtd_parse_part - parse MTD partition looking for subpartitions
327  *
328  * @slave: part that is supposed to be a container and should be parsed
329  * @types: NULL-terminated array with names of partition parsers to try
330  *
331  * Some partitions are kind of containers with extra subpartitions (volumes).
332  * There can be various formats of such containers. This function tries to use
333  * specified parsers to analyze given partition and registers found
334  * subpartitions on success.
335  */
336 static int mtd_parse_part(struct mtd_part *slave, const char *const *types)
337 {
338 	return parse_mtd_partitions(&slave->mtd, types, NULL);
339 }
340 
341 static struct mtd_part *allocate_partition(struct mtd_info *parent,
342 			const struct mtd_partition *part, int partno,
343 			uint64_t cur_offset)
344 {
345 	int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize :
346 							    parent->erasesize;
347 	struct mtd_part *slave;
348 	u32 remainder;
349 	char *name;
350 	u64 tmp;
351 
352 	/* allocate the partition structure */
353 	slave = kzalloc(sizeof(*slave), GFP_KERNEL);
354 	name = kstrdup(part->name, GFP_KERNEL);
355 	if (!name || !slave) {
356 		printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
357 		       parent->name);
358 		kfree(name);
359 		kfree(slave);
360 		return ERR_PTR(-ENOMEM);
361 	}
362 
363 	/* set up the MTD object for this partition */
364 	slave->mtd.type = parent->type;
365 	slave->mtd.flags = parent->flags & ~part->mask_flags;
366 	slave->mtd.size = part->size;
367 	slave->mtd.writesize = parent->writesize;
368 	slave->mtd.writebufsize = parent->writebufsize;
369 	slave->mtd.oobsize = parent->oobsize;
370 	slave->mtd.oobavail = parent->oobavail;
371 	slave->mtd.subpage_sft = parent->subpage_sft;
372 	slave->mtd.pairing = parent->pairing;
373 
374 	slave->mtd.name = name;
375 	slave->mtd.owner = parent->owner;
376 
377 	/* NOTE: Historically, we didn't arrange MTDs as a tree out of
378 	 * concern for showing the same data in multiple partitions.
379 	 * However, it is very useful to have the master node present,
380 	 * so the MTD_PARTITIONED_MASTER option allows that. The master
381 	 * will have device nodes etc only if this is set, so make the
382 	 * parent conditional on that option. Note, this is a way to
383 	 * distinguish between the master and the partition in sysfs.
384 	 */
385 	slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
386 				&parent->dev :
387 				parent->dev.parent;
388 	slave->mtd.dev.of_node = part->of_node;
389 
390 	if (parent->_read)
391 		slave->mtd._read = part_read;
392 	if (parent->_write)
393 		slave->mtd._write = part_write;
394 
395 	if (parent->_panic_write)
396 		slave->mtd._panic_write = part_panic_write;
397 
398 	if (parent->_point && parent->_unpoint) {
399 		slave->mtd._point = part_point;
400 		slave->mtd._unpoint = part_unpoint;
401 	}
402 
403 	if (parent->_read_oob)
404 		slave->mtd._read_oob = part_read_oob;
405 	if (parent->_write_oob)
406 		slave->mtd._write_oob = part_write_oob;
407 	if (parent->_read_user_prot_reg)
408 		slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
409 	if (parent->_read_fact_prot_reg)
410 		slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
411 	if (parent->_write_user_prot_reg)
412 		slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
413 	if (parent->_lock_user_prot_reg)
414 		slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
415 	if (parent->_get_user_prot_info)
416 		slave->mtd._get_user_prot_info = part_get_user_prot_info;
417 	if (parent->_get_fact_prot_info)
418 		slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
419 	if (parent->_sync)
420 		slave->mtd._sync = part_sync;
421 	if (!partno && !parent->dev.class && parent->_suspend &&
422 	    parent->_resume) {
423 		slave->mtd._suspend = part_suspend;
424 		slave->mtd._resume = part_resume;
425 	}
426 	if (parent->_writev)
427 		slave->mtd._writev = part_writev;
428 	if (parent->_lock)
429 		slave->mtd._lock = part_lock;
430 	if (parent->_unlock)
431 		slave->mtd._unlock = part_unlock;
432 	if (parent->_is_locked)
433 		slave->mtd._is_locked = part_is_locked;
434 	if (parent->_block_isreserved)
435 		slave->mtd._block_isreserved = part_block_isreserved;
436 	if (parent->_block_isbad)
437 		slave->mtd._block_isbad = part_block_isbad;
438 	if (parent->_block_markbad)
439 		slave->mtd._block_markbad = part_block_markbad;
440 	if (parent->_max_bad_blocks)
441 		slave->mtd._max_bad_blocks = part_max_bad_blocks;
442 
443 	if (parent->_get_device)
444 		slave->mtd._get_device = part_get_device;
445 	if (parent->_put_device)
446 		slave->mtd._put_device = part_put_device;
447 
448 	slave->mtd._erase = part_erase;
449 	slave->parent = parent;
450 	slave->offset = part->offset;
451 
452 	if (slave->offset == MTDPART_OFS_APPEND)
453 		slave->offset = cur_offset;
454 	if (slave->offset == MTDPART_OFS_NXTBLK) {
455 		tmp = cur_offset;
456 		slave->offset = cur_offset;
457 		remainder = do_div(tmp, wr_alignment);
458 		if (remainder) {
459 			slave->offset += wr_alignment - remainder;
460 			printk(KERN_NOTICE "Moving partition %d: "
461 			       "0x%012llx -> 0x%012llx\n", partno,
462 			       (unsigned long long)cur_offset, (unsigned long long)slave->offset);
463 		}
464 	}
465 	if (slave->offset == MTDPART_OFS_RETAIN) {
466 		slave->offset = cur_offset;
467 		if (parent->size - slave->offset >= slave->mtd.size) {
468 			slave->mtd.size = parent->size - slave->offset
469 							- slave->mtd.size;
470 		} else {
471 			printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
472 				part->name, parent->size - slave->offset,
473 				slave->mtd.size);
474 			/* register to preserve ordering */
475 			goto out_register;
476 		}
477 	}
478 	if (slave->mtd.size == MTDPART_SIZ_FULL)
479 		slave->mtd.size = parent->size - slave->offset;
480 
481 	printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
482 		(unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
483 
484 	/* let's do some sanity checks */
485 	if (slave->offset >= parent->size) {
486 		/* let's register it anyway to preserve ordering */
487 		slave->offset = 0;
488 		slave->mtd.size = 0;
489 		printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
490 			part->name);
491 		goto out_register;
492 	}
493 	if (slave->offset + slave->mtd.size > parent->size) {
494 		slave->mtd.size = parent->size - slave->offset;
495 		printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
496 			part->name, parent->name, (unsigned long long)slave->mtd.size);
497 	}
498 	if (parent->numeraseregions > 1) {
499 		/* Deal with variable erase size stuff */
500 		int i, max = parent->numeraseregions;
501 		u64 end = slave->offset + slave->mtd.size;
502 		struct mtd_erase_region_info *regions = parent->eraseregions;
503 
504 		/* Find the first erase regions which is part of this
505 		 * partition. */
506 		for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
507 			;
508 		/* The loop searched for the region _behind_ the first one */
509 		if (i > 0)
510 			i--;
511 
512 		/* Pick biggest erasesize */
513 		for (; i < max && regions[i].offset < end; i++) {
514 			if (slave->mtd.erasesize < regions[i].erasesize) {
515 				slave->mtd.erasesize = regions[i].erasesize;
516 			}
517 		}
518 		BUG_ON(slave->mtd.erasesize == 0);
519 	} else {
520 		/* Single erase size */
521 		slave->mtd.erasesize = parent->erasesize;
522 	}
523 
524 	/*
525 	 * Slave erasesize might differ from the master one if the master
526 	 * exposes several regions with different erasesize. Adjust
527 	 * wr_alignment accordingly.
528 	 */
529 	if (!(slave->mtd.flags & MTD_NO_ERASE))
530 		wr_alignment = slave->mtd.erasesize;
531 
532 	tmp = slave->offset;
533 	remainder = do_div(tmp, wr_alignment);
534 	if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
535 		/* Doesn't start on a boundary of major erase size */
536 		/* FIXME: Let it be writable if it is on a boundary of
537 		 * _minor_ erase size though */
538 		slave->mtd.flags &= ~MTD_WRITEABLE;
539 		printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
540 			part->name);
541 	}
542 
543 	tmp = slave->mtd.size;
544 	remainder = do_div(tmp, wr_alignment);
545 	if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
546 		slave->mtd.flags &= ~MTD_WRITEABLE;
547 		printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
548 			part->name);
549 	}
550 
551 	mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
552 	slave->mtd.ecc_step_size = parent->ecc_step_size;
553 	slave->mtd.ecc_strength = parent->ecc_strength;
554 	slave->mtd.bitflip_threshold = parent->bitflip_threshold;
555 
556 	if (parent->_block_isbad) {
557 		uint64_t offs = 0;
558 
559 		while (offs < slave->mtd.size) {
560 			if (mtd_block_isreserved(parent, offs + slave->offset))
561 				slave->mtd.ecc_stats.bbtblocks++;
562 			else if (mtd_block_isbad(parent, offs + slave->offset))
563 				slave->mtd.ecc_stats.badblocks++;
564 			offs += slave->mtd.erasesize;
565 		}
566 	}
567 
568 out_register:
569 	return slave;
570 }
571 
572 static ssize_t mtd_partition_offset_show(struct device *dev,
573 		struct device_attribute *attr, char *buf)
574 {
575 	struct mtd_info *mtd = dev_get_drvdata(dev);
576 	struct mtd_part *part = mtd_to_part(mtd);
577 	return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
578 }
579 
580 static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
581 
582 static const struct attribute *mtd_partition_attrs[] = {
583 	&dev_attr_offset.attr,
584 	NULL
585 };
586 
587 static int mtd_add_partition_attrs(struct mtd_part *new)
588 {
589 	int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
590 	if (ret)
591 		printk(KERN_WARNING
592 		       "mtd: failed to create partition attrs, err=%d\n", ret);
593 	return ret;
594 }
595 
596 int mtd_add_partition(struct mtd_info *parent, const char *name,
597 		      long long offset, long long length)
598 {
599 	struct mtd_partition part;
600 	struct mtd_part *new;
601 	int ret = 0;
602 
603 	/* the direct offset is expected */
604 	if (offset == MTDPART_OFS_APPEND ||
605 	    offset == MTDPART_OFS_NXTBLK)
606 		return -EINVAL;
607 
608 	if (length == MTDPART_SIZ_FULL)
609 		length = parent->size - offset;
610 
611 	if (length <= 0)
612 		return -EINVAL;
613 
614 	memset(&part, 0, sizeof(part));
615 	part.name = name;
616 	part.size = length;
617 	part.offset = offset;
618 
619 	new = allocate_partition(parent, &part, -1, offset);
620 	if (IS_ERR(new))
621 		return PTR_ERR(new);
622 
623 	mutex_lock(&mtd_partitions_mutex);
624 	list_add(&new->list, &mtd_partitions);
625 	mutex_unlock(&mtd_partitions_mutex);
626 
627 	add_mtd_device(&new->mtd);
628 
629 	mtd_add_partition_attrs(new);
630 
631 	return ret;
632 }
633 EXPORT_SYMBOL_GPL(mtd_add_partition);
634 
635 /**
636  * __mtd_del_partition - delete MTD partition
637  *
638  * @priv: internal MTD struct for partition to be deleted
639  *
640  * This function must be called with the partitions mutex locked.
641  */
642 static int __mtd_del_partition(struct mtd_part *priv)
643 {
644 	struct mtd_part *child, *next;
645 	int err;
646 
647 	list_for_each_entry_safe(child, next, &mtd_partitions, list) {
648 		if (child->parent == &priv->mtd) {
649 			err = __mtd_del_partition(child);
650 			if (err)
651 				return err;
652 		}
653 	}
654 
655 	sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs);
656 
657 	err = del_mtd_device(&priv->mtd);
658 	if (err)
659 		return err;
660 
661 	list_del(&priv->list);
662 	free_partition(priv);
663 
664 	return 0;
665 }
666 
667 /*
668  * This function unregisters and destroy all slave MTD objects which are
669  * attached to the given MTD object.
670  */
671 int del_mtd_partitions(struct mtd_info *mtd)
672 {
673 	struct mtd_part *slave, *next;
674 	int ret, err = 0;
675 
676 	mutex_lock(&mtd_partitions_mutex);
677 	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
678 		if (slave->parent == mtd) {
679 			ret = __mtd_del_partition(slave);
680 			if (ret < 0)
681 				err = ret;
682 		}
683 	mutex_unlock(&mtd_partitions_mutex);
684 
685 	return err;
686 }
687 
688 int mtd_del_partition(struct mtd_info *mtd, int partno)
689 {
690 	struct mtd_part *slave, *next;
691 	int ret = -EINVAL;
692 
693 	mutex_lock(&mtd_partitions_mutex);
694 	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
695 		if ((slave->parent == mtd) &&
696 		    (slave->mtd.index == partno)) {
697 			ret = __mtd_del_partition(slave);
698 			break;
699 		}
700 	mutex_unlock(&mtd_partitions_mutex);
701 
702 	return ret;
703 }
704 EXPORT_SYMBOL_GPL(mtd_del_partition);
705 
706 /*
707  * This function, given a master MTD object and a partition table, creates
708  * and registers slave MTD objects which are bound to the master according to
709  * the partition definitions.
710  *
711  * For historical reasons, this function's caller only registers the master
712  * if the MTD_PARTITIONED_MASTER config option is set.
713  */
714 
715 int add_mtd_partitions(struct mtd_info *master,
716 		       const struct mtd_partition *parts,
717 		       int nbparts)
718 {
719 	struct mtd_part *slave;
720 	uint64_t cur_offset = 0;
721 	int i;
722 
723 	printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
724 
725 	for (i = 0; i < nbparts; i++) {
726 		slave = allocate_partition(master, parts + i, i, cur_offset);
727 		if (IS_ERR(slave)) {
728 			del_mtd_partitions(master);
729 			return PTR_ERR(slave);
730 		}
731 
732 		mutex_lock(&mtd_partitions_mutex);
733 		list_add(&slave->list, &mtd_partitions);
734 		mutex_unlock(&mtd_partitions_mutex);
735 
736 		add_mtd_device(&slave->mtd);
737 		mtd_add_partition_attrs(slave);
738 		if (parts[i].types)
739 			mtd_parse_part(slave, parts[i].types);
740 
741 		cur_offset = slave->offset + slave->mtd.size;
742 	}
743 
744 	return 0;
745 }
746 
747 static DEFINE_SPINLOCK(part_parser_lock);
748 static LIST_HEAD(part_parsers);
749 
750 static struct mtd_part_parser *mtd_part_parser_get(const char *name)
751 {
752 	struct mtd_part_parser *p, *ret = NULL;
753 
754 	spin_lock(&part_parser_lock);
755 
756 	list_for_each_entry(p, &part_parsers, list)
757 		if (!strcmp(p->name, name) && try_module_get(p->owner)) {
758 			ret = p;
759 			break;
760 		}
761 
762 	spin_unlock(&part_parser_lock);
763 
764 	return ret;
765 }
766 
767 static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
768 {
769 	module_put(p->owner);
770 }
771 
772 /*
773  * Many partition parsers just expected the core to kfree() all their data in
774  * one chunk. Do that by default.
775  */
776 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
777 					    int nr_parts)
778 {
779 	kfree(pparts);
780 }
781 
782 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
783 {
784 	p->owner = owner;
785 
786 	if (!p->cleanup)
787 		p->cleanup = &mtd_part_parser_cleanup_default;
788 
789 	spin_lock(&part_parser_lock);
790 	list_add(&p->list, &part_parsers);
791 	spin_unlock(&part_parser_lock);
792 
793 	return 0;
794 }
795 EXPORT_SYMBOL_GPL(__register_mtd_parser);
796 
797 void deregister_mtd_parser(struct mtd_part_parser *p)
798 {
799 	spin_lock(&part_parser_lock);
800 	list_del(&p->list);
801 	spin_unlock(&part_parser_lock);
802 }
803 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
804 
805 /*
806  * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
807  * are changing this array!
808  */
809 static const char * const default_mtd_part_types[] = {
810 	"cmdlinepart",
811 	"ofpart",
812 	NULL
813 };
814 
815 static int mtd_part_do_parse(struct mtd_part_parser *parser,
816 			     struct mtd_info *master,
817 			     struct mtd_partitions *pparts,
818 			     struct mtd_part_parser_data *data)
819 {
820 	int ret;
821 
822 	ret = (*parser->parse_fn)(master, &pparts->parts, data);
823 	pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
824 	if (ret <= 0)
825 		return ret;
826 
827 	pr_notice("%d %s partitions found on MTD device %s\n", ret,
828 		  parser->name, master->name);
829 
830 	pparts->nr_parts = ret;
831 	pparts->parser = parser;
832 
833 	return ret;
834 }
835 
836 /**
837  * mtd_part_get_compatible_parser - find MTD parser by a compatible string
838  *
839  * @compat: compatible string describing partitions in a device tree
840  *
841  * MTD parsers can specify supported partitions by providing a table of
842  * compatibility strings. This function finds a parser that advertises support
843  * for a passed value of "compatible".
844  */
845 static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
846 {
847 	struct mtd_part_parser *p, *ret = NULL;
848 
849 	spin_lock(&part_parser_lock);
850 
851 	list_for_each_entry(p, &part_parsers, list) {
852 		const struct of_device_id *matches;
853 
854 		matches = p->of_match_table;
855 		if (!matches)
856 			continue;
857 
858 		for (; matches->compatible[0]; matches++) {
859 			if (!strcmp(matches->compatible, compat) &&
860 			    try_module_get(p->owner)) {
861 				ret = p;
862 				break;
863 			}
864 		}
865 
866 		if (ret)
867 			break;
868 	}
869 
870 	spin_unlock(&part_parser_lock);
871 
872 	return ret;
873 }
874 
875 static int mtd_part_of_parse(struct mtd_info *master,
876 			     struct mtd_partitions *pparts)
877 {
878 	struct mtd_part_parser *parser;
879 	struct device_node *np;
880 	struct property *prop;
881 	const char *compat;
882 	const char *fixed = "fixed-partitions";
883 	int ret, err = 0;
884 
885 	np = of_get_child_by_name(mtd_get_of_node(master), "partitions");
886 	of_property_for_each_string(np, "compatible", prop, compat) {
887 		parser = mtd_part_get_compatible_parser(compat);
888 		if (!parser)
889 			continue;
890 		ret = mtd_part_do_parse(parser, master, pparts, NULL);
891 		if (ret > 0) {
892 			of_node_put(np);
893 			return ret;
894 		}
895 		mtd_part_parser_put(parser);
896 		if (ret < 0 && !err)
897 			err = ret;
898 	}
899 	of_node_put(np);
900 
901 	/*
902 	 * For backward compatibility we have to try the "fixed-partitions"
903 	 * parser. It supports old DT format with partitions specified as a
904 	 * direct subnodes of a flash device DT node without any compatibility
905 	 * specified we could match.
906 	 */
907 	parser = mtd_part_parser_get(fixed);
908 	if (!parser && !request_module("%s", fixed))
909 		parser = mtd_part_parser_get(fixed);
910 	if (parser) {
911 		ret = mtd_part_do_parse(parser, master, pparts, NULL);
912 		if (ret > 0)
913 			return ret;
914 		mtd_part_parser_put(parser);
915 		if (ret < 0 && !err)
916 			err = ret;
917 	}
918 
919 	return err;
920 }
921 
922 /**
923  * parse_mtd_partitions - parse and register MTD partitions
924  *
925  * @master: the master partition (describes whole MTD device)
926  * @types: names of partition parsers to try or %NULL
927  * @data: MTD partition parser-specific data
928  *
929  * This function tries to find & register partitions on MTD device @master. It
930  * uses MTD partition parsers, specified in @types. However, if @types is %NULL,
931  * then the default list of parsers is used. The default list contains only the
932  * "cmdlinepart" and "ofpart" parsers ATM.
933  * Note: If there are more then one parser in @types, the kernel only takes the
934  * partitions parsed out by the first parser.
935  *
936  * This function may return:
937  * o a negative error code in case of failure
938  * o number of found partitions otherwise
939  */
940 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
941 			 struct mtd_part_parser_data *data)
942 {
943 	struct mtd_partitions pparts = { };
944 	struct mtd_part_parser *parser;
945 	int ret, err = 0;
946 
947 	if (!types)
948 		types = default_mtd_part_types;
949 
950 	for ( ; *types; types++) {
951 		/*
952 		 * ofpart is a special type that means OF partitioning info
953 		 * should be used. It requires a bit different logic so it is
954 		 * handled in a separated function.
955 		 */
956 		if (!strcmp(*types, "ofpart")) {
957 			ret = mtd_part_of_parse(master, &pparts);
958 		} else {
959 			pr_debug("%s: parsing partitions %s\n", master->name,
960 				 *types);
961 			parser = mtd_part_parser_get(*types);
962 			if (!parser && !request_module("%s", *types))
963 				parser = mtd_part_parser_get(*types);
964 			pr_debug("%s: got parser %s\n", master->name,
965 				parser ? parser->name : NULL);
966 			if (!parser)
967 				continue;
968 			ret = mtd_part_do_parse(parser, master, &pparts, data);
969 			if (ret <= 0)
970 				mtd_part_parser_put(parser);
971 		}
972 		/* Found partitions! */
973 		if (ret > 0) {
974 			err = add_mtd_partitions(master, pparts.parts,
975 						 pparts.nr_parts);
976 			mtd_part_parser_cleanup(&pparts);
977 			return err ? err : pparts.nr_parts;
978 		}
979 		/*
980 		 * Stash the first error we see; only report it if no parser
981 		 * succeeds
982 		 */
983 		if (ret < 0 && !err)
984 			err = ret;
985 	}
986 	return err;
987 }
988 
989 void mtd_part_parser_cleanup(struct mtd_partitions *parts)
990 {
991 	const struct mtd_part_parser *parser;
992 
993 	if (!parts)
994 		return;
995 
996 	parser = parts->parser;
997 	if (parser) {
998 		if (parser->cleanup)
999 			parser->cleanup(parts->parts, parts->nr_parts);
1000 
1001 		mtd_part_parser_put(parser);
1002 	}
1003 }
1004 
1005 int mtd_is_partition(const struct mtd_info *mtd)
1006 {
1007 	struct mtd_part *part;
1008 	int ispart = 0;
1009 
1010 	mutex_lock(&mtd_partitions_mutex);
1011 	list_for_each_entry(part, &mtd_partitions, list)
1012 		if (&part->mtd == mtd) {
1013 			ispart = 1;
1014 			break;
1015 		}
1016 	mutex_unlock(&mtd_partitions_mutex);
1017 
1018 	return ispart;
1019 }
1020 EXPORT_SYMBOL_GPL(mtd_is_partition);
1021 
1022 /* Returns the size of the entire flash chip */
1023 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
1024 {
1025 	if (!mtd_is_partition(mtd))
1026 		return mtd->size;
1027 
1028 	return mtd_get_device_size(mtd_to_part(mtd)->parent);
1029 }
1030 EXPORT_SYMBOL_GPL(mtd_get_device_size);
1031