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