xref: /openbmc/linux/drivers/mtd/mtdpart.c (revision d2574c33)
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 
484 		/* Initialize ->erasesize to make add_mtd_device() happy. */
485 		slave->mtd.erasesize = parent->erasesize;
486 
487 		printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
488 			part->name);
489 		goto out_register;
490 	}
491 	if (slave->offset + slave->mtd.size > parent->size) {
492 		slave->mtd.size = parent->size - slave->offset;
493 		printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
494 			part->name, parent->name, (unsigned long long)slave->mtd.size);
495 	}
496 	if (parent->numeraseregions > 1) {
497 		/* Deal with variable erase size stuff */
498 		int i, max = parent->numeraseregions;
499 		u64 end = slave->offset + slave->mtd.size;
500 		struct mtd_erase_region_info *regions = parent->eraseregions;
501 
502 		/* Find the first erase regions which is part of this
503 		 * partition. */
504 		for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
505 			;
506 		/* The loop searched for the region _behind_ the first one */
507 		if (i > 0)
508 			i--;
509 
510 		/* Pick biggest erasesize */
511 		for (; i < max && regions[i].offset < end; i++) {
512 			if (slave->mtd.erasesize < regions[i].erasesize) {
513 				slave->mtd.erasesize = regions[i].erasesize;
514 			}
515 		}
516 		BUG_ON(slave->mtd.erasesize == 0);
517 	} else {
518 		/* Single erase size */
519 		slave->mtd.erasesize = parent->erasesize;
520 	}
521 
522 	/*
523 	 * Slave erasesize might differ from the master one if the master
524 	 * exposes several regions with different erasesize. Adjust
525 	 * wr_alignment accordingly.
526 	 */
527 	if (!(slave->mtd.flags & MTD_NO_ERASE))
528 		wr_alignment = slave->mtd.erasesize;
529 
530 	tmp = part_absolute_offset(parent) + slave->offset;
531 	remainder = do_div(tmp, wr_alignment);
532 	if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
533 		/* Doesn't start on a boundary of major erase size */
534 		/* FIXME: Let it be writable if it is on a boundary of
535 		 * _minor_ erase size though */
536 		slave->mtd.flags &= ~MTD_WRITEABLE;
537 		printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
538 			part->name);
539 	}
540 
541 	tmp = part_absolute_offset(parent) + slave->mtd.size;
542 	remainder = do_div(tmp, wr_alignment);
543 	if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
544 		slave->mtd.flags &= ~MTD_WRITEABLE;
545 		printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
546 			part->name);
547 	}
548 
549 	mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
550 	slave->mtd.ecc_step_size = parent->ecc_step_size;
551 	slave->mtd.ecc_strength = parent->ecc_strength;
552 	slave->mtd.bitflip_threshold = parent->bitflip_threshold;
553 
554 	if (parent->_block_isbad) {
555 		uint64_t offs = 0;
556 
557 		while (offs < slave->mtd.size) {
558 			if (mtd_block_isreserved(parent, offs + slave->offset))
559 				slave->mtd.ecc_stats.bbtblocks++;
560 			else if (mtd_block_isbad(parent, offs + slave->offset))
561 				slave->mtd.ecc_stats.badblocks++;
562 			offs += slave->mtd.erasesize;
563 		}
564 	}
565 
566 out_register:
567 	return slave;
568 }
569 
570 static ssize_t mtd_partition_offset_show(struct device *dev,
571 		struct device_attribute *attr, char *buf)
572 {
573 	struct mtd_info *mtd = dev_get_drvdata(dev);
574 	struct mtd_part *part = mtd_to_part(mtd);
575 	return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
576 }
577 
578 static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
579 
580 static const struct attribute *mtd_partition_attrs[] = {
581 	&dev_attr_offset.attr,
582 	NULL
583 };
584 
585 static int mtd_add_partition_attrs(struct mtd_part *new)
586 {
587 	int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
588 	if (ret)
589 		printk(KERN_WARNING
590 		       "mtd: failed to create partition attrs, err=%d\n", ret);
591 	return ret;
592 }
593 
594 int mtd_add_partition(struct mtd_info *parent, const char *name,
595 		      long long offset, long long length)
596 {
597 	struct mtd_partition part;
598 	struct mtd_part *new;
599 	int ret = 0;
600 
601 	/* the direct offset is expected */
602 	if (offset == MTDPART_OFS_APPEND ||
603 	    offset == MTDPART_OFS_NXTBLK)
604 		return -EINVAL;
605 
606 	if (length == MTDPART_SIZ_FULL)
607 		length = parent->size - offset;
608 
609 	if (length <= 0)
610 		return -EINVAL;
611 
612 	memset(&part, 0, sizeof(part));
613 	part.name = name;
614 	part.size = length;
615 	part.offset = offset;
616 
617 	new = allocate_partition(parent, &part, -1, offset);
618 	if (IS_ERR(new))
619 		return PTR_ERR(new);
620 
621 	mutex_lock(&mtd_partitions_mutex);
622 	list_add(&new->list, &mtd_partitions);
623 	mutex_unlock(&mtd_partitions_mutex);
624 
625 	ret = add_mtd_device(&new->mtd);
626 	if (ret)
627 		goto err_remove_part;
628 
629 	mtd_add_partition_attrs(new);
630 
631 	return 0;
632 
633 err_remove_part:
634 	mutex_lock(&mtd_partitions_mutex);
635 	list_del(&new->list);
636 	mutex_unlock(&mtd_partitions_mutex);
637 
638 	free_partition(new);
639 
640 	return ret;
641 }
642 EXPORT_SYMBOL_GPL(mtd_add_partition);
643 
644 /**
645  * __mtd_del_partition - delete MTD partition
646  *
647  * @priv: internal MTD struct for partition to be deleted
648  *
649  * This function must be called with the partitions mutex locked.
650  */
651 static int __mtd_del_partition(struct mtd_part *priv)
652 {
653 	struct mtd_part *child, *next;
654 	int err;
655 
656 	list_for_each_entry_safe(child, next, &mtd_partitions, list) {
657 		if (child->parent == &priv->mtd) {
658 			err = __mtd_del_partition(child);
659 			if (err)
660 				return err;
661 		}
662 	}
663 
664 	sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs);
665 
666 	err = del_mtd_device(&priv->mtd);
667 	if (err)
668 		return err;
669 
670 	list_del(&priv->list);
671 	free_partition(priv);
672 
673 	return 0;
674 }
675 
676 /*
677  * This function unregisters and destroy all slave MTD objects which are
678  * attached to the given MTD object.
679  */
680 int del_mtd_partitions(struct mtd_info *mtd)
681 {
682 	struct mtd_part *slave, *next;
683 	int ret, err = 0;
684 
685 	mutex_lock(&mtd_partitions_mutex);
686 	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
687 		if (slave->parent == mtd) {
688 			ret = __mtd_del_partition(slave);
689 			if (ret < 0)
690 				err = ret;
691 		}
692 	mutex_unlock(&mtd_partitions_mutex);
693 
694 	return err;
695 }
696 
697 int mtd_del_partition(struct mtd_info *mtd, int partno)
698 {
699 	struct mtd_part *slave, *next;
700 	int ret = -EINVAL;
701 
702 	mutex_lock(&mtd_partitions_mutex);
703 	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
704 		if ((slave->parent == mtd) &&
705 		    (slave->mtd.index == partno)) {
706 			ret = __mtd_del_partition(slave);
707 			break;
708 		}
709 	mutex_unlock(&mtd_partitions_mutex);
710 
711 	return ret;
712 }
713 EXPORT_SYMBOL_GPL(mtd_del_partition);
714 
715 /*
716  * This function, given a master MTD object and a partition table, creates
717  * and registers slave MTD objects which are bound to the master according to
718  * the partition definitions.
719  *
720  * For historical reasons, this function's caller only registers the master
721  * if the MTD_PARTITIONED_MASTER config option is set.
722  */
723 
724 int add_mtd_partitions(struct mtd_info *master,
725 		       const struct mtd_partition *parts,
726 		       int nbparts)
727 {
728 	struct mtd_part *slave;
729 	uint64_t cur_offset = 0;
730 	int i, ret;
731 
732 	printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
733 
734 	for (i = 0; i < nbparts; i++) {
735 		slave = allocate_partition(master, parts + i, i, cur_offset);
736 		if (IS_ERR(slave)) {
737 			ret = PTR_ERR(slave);
738 			goto err_del_partitions;
739 		}
740 
741 		mutex_lock(&mtd_partitions_mutex);
742 		list_add(&slave->list, &mtd_partitions);
743 		mutex_unlock(&mtd_partitions_mutex);
744 
745 		ret = add_mtd_device(&slave->mtd);
746 		if (ret) {
747 			mutex_lock(&mtd_partitions_mutex);
748 			list_del(&slave->list);
749 			mutex_unlock(&mtd_partitions_mutex);
750 
751 			free_partition(slave);
752 			goto err_del_partitions;
753 		}
754 
755 		mtd_add_partition_attrs(slave);
756 		/* Look for subpartitions */
757 		parse_mtd_partitions(&slave->mtd, parts[i].types, NULL);
758 
759 		cur_offset = slave->offset + slave->mtd.size;
760 	}
761 
762 	return 0;
763 
764 err_del_partitions:
765 	del_mtd_partitions(master);
766 
767 	return ret;
768 }
769 
770 static DEFINE_SPINLOCK(part_parser_lock);
771 static LIST_HEAD(part_parsers);
772 
773 static struct mtd_part_parser *mtd_part_parser_get(const char *name)
774 {
775 	struct mtd_part_parser *p, *ret = NULL;
776 
777 	spin_lock(&part_parser_lock);
778 
779 	list_for_each_entry(p, &part_parsers, list)
780 		if (!strcmp(p->name, name) && try_module_get(p->owner)) {
781 			ret = p;
782 			break;
783 		}
784 
785 	spin_unlock(&part_parser_lock);
786 
787 	return ret;
788 }
789 
790 static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
791 {
792 	module_put(p->owner);
793 }
794 
795 /*
796  * Many partition parsers just expected the core to kfree() all their data in
797  * one chunk. Do that by default.
798  */
799 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
800 					    int nr_parts)
801 {
802 	kfree(pparts);
803 }
804 
805 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
806 {
807 	p->owner = owner;
808 
809 	if (!p->cleanup)
810 		p->cleanup = &mtd_part_parser_cleanup_default;
811 
812 	spin_lock(&part_parser_lock);
813 	list_add(&p->list, &part_parsers);
814 	spin_unlock(&part_parser_lock);
815 
816 	return 0;
817 }
818 EXPORT_SYMBOL_GPL(__register_mtd_parser);
819 
820 void deregister_mtd_parser(struct mtd_part_parser *p)
821 {
822 	spin_lock(&part_parser_lock);
823 	list_del(&p->list);
824 	spin_unlock(&part_parser_lock);
825 }
826 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
827 
828 /*
829  * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
830  * are changing this array!
831  */
832 static const char * const default_mtd_part_types[] = {
833 	"cmdlinepart",
834 	"ofpart",
835 	NULL
836 };
837 
838 /* Check DT only when looking for subpartitions. */
839 static const char * const default_subpartition_types[] = {
840 	"ofpart",
841 	NULL
842 };
843 
844 static int mtd_part_do_parse(struct mtd_part_parser *parser,
845 			     struct mtd_info *master,
846 			     struct mtd_partitions *pparts,
847 			     struct mtd_part_parser_data *data)
848 {
849 	int ret;
850 
851 	ret = (*parser->parse_fn)(master, &pparts->parts, data);
852 	pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
853 	if (ret <= 0)
854 		return ret;
855 
856 	pr_notice("%d %s partitions found on MTD device %s\n", ret,
857 		  parser->name, master->name);
858 
859 	pparts->nr_parts = ret;
860 	pparts->parser = parser;
861 
862 	return ret;
863 }
864 
865 /**
866  * mtd_part_get_compatible_parser - find MTD parser by a compatible string
867  *
868  * @compat: compatible string describing partitions in a device tree
869  *
870  * MTD parsers can specify supported partitions by providing a table of
871  * compatibility strings. This function finds a parser that advertises support
872  * for a passed value of "compatible".
873  */
874 static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
875 {
876 	struct mtd_part_parser *p, *ret = NULL;
877 
878 	spin_lock(&part_parser_lock);
879 
880 	list_for_each_entry(p, &part_parsers, list) {
881 		const struct of_device_id *matches;
882 
883 		matches = p->of_match_table;
884 		if (!matches)
885 			continue;
886 
887 		for (; matches->compatible[0]; matches++) {
888 			if (!strcmp(matches->compatible, compat) &&
889 			    try_module_get(p->owner)) {
890 				ret = p;
891 				break;
892 			}
893 		}
894 
895 		if (ret)
896 			break;
897 	}
898 
899 	spin_unlock(&part_parser_lock);
900 
901 	return ret;
902 }
903 
904 static int mtd_part_of_parse(struct mtd_info *master,
905 			     struct mtd_partitions *pparts)
906 {
907 	struct mtd_part_parser *parser;
908 	struct device_node *np;
909 	struct property *prop;
910 	const char *compat;
911 	const char *fixed = "fixed-partitions";
912 	int ret, err = 0;
913 
914 	np = mtd_get_of_node(master);
915 	if (mtd_is_partition(master))
916 		of_node_get(np);
917 	else
918 		np = of_get_child_by_name(np, "partitions");
919 
920 	of_property_for_each_string(np, "compatible", prop, compat) {
921 		parser = mtd_part_get_compatible_parser(compat);
922 		if (!parser)
923 			continue;
924 		ret = mtd_part_do_parse(parser, master, pparts, NULL);
925 		if (ret > 0) {
926 			of_node_put(np);
927 			return ret;
928 		}
929 		mtd_part_parser_put(parser);
930 		if (ret < 0 && !err)
931 			err = ret;
932 	}
933 	of_node_put(np);
934 
935 	/*
936 	 * For backward compatibility we have to try the "fixed-partitions"
937 	 * parser. It supports old DT format with partitions specified as a
938 	 * direct subnodes of a flash device DT node without any compatibility
939 	 * specified we could match.
940 	 */
941 	parser = mtd_part_parser_get(fixed);
942 	if (!parser && !request_module("%s", fixed))
943 		parser = mtd_part_parser_get(fixed);
944 	if (parser) {
945 		ret = mtd_part_do_parse(parser, master, pparts, NULL);
946 		if (ret > 0)
947 			return ret;
948 		mtd_part_parser_put(parser);
949 		if (ret < 0 && !err)
950 			err = ret;
951 	}
952 
953 	return err;
954 }
955 
956 /**
957  * parse_mtd_partitions - parse and register MTD partitions
958  *
959  * @master: the master partition (describes whole MTD device)
960  * @types: names of partition parsers to try or %NULL
961  * @data: MTD partition parser-specific data
962  *
963  * This function tries to find & register partitions on MTD device @master. It
964  * uses MTD partition parsers, specified in @types. However, if @types is %NULL,
965  * then the default list of parsers is used. The default list contains only the
966  * "cmdlinepart" and "ofpart" parsers ATM.
967  * Note: If there are more then one parser in @types, the kernel only takes the
968  * partitions parsed out by the first parser.
969  *
970  * This function may return:
971  * o a negative error code in case of failure
972  * o number of found partitions otherwise
973  */
974 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
975 			 struct mtd_part_parser_data *data)
976 {
977 	struct mtd_partitions pparts = { };
978 	struct mtd_part_parser *parser;
979 	int ret, err = 0;
980 
981 	if (!types)
982 		types = mtd_is_partition(master) ? default_subpartition_types :
983 			default_mtd_part_types;
984 
985 	for ( ; *types; types++) {
986 		/*
987 		 * ofpart is a special type that means OF partitioning info
988 		 * should be used. It requires a bit different logic so it is
989 		 * handled in a separated function.
990 		 */
991 		if (!strcmp(*types, "ofpart")) {
992 			ret = mtd_part_of_parse(master, &pparts);
993 		} else {
994 			pr_debug("%s: parsing partitions %s\n", master->name,
995 				 *types);
996 			parser = mtd_part_parser_get(*types);
997 			if (!parser && !request_module("%s", *types))
998 				parser = mtd_part_parser_get(*types);
999 			pr_debug("%s: got parser %s\n", master->name,
1000 				parser ? parser->name : NULL);
1001 			if (!parser)
1002 				continue;
1003 			ret = mtd_part_do_parse(parser, master, &pparts, data);
1004 			if (ret <= 0)
1005 				mtd_part_parser_put(parser);
1006 		}
1007 		/* Found partitions! */
1008 		if (ret > 0) {
1009 			err = add_mtd_partitions(master, pparts.parts,
1010 						 pparts.nr_parts);
1011 			mtd_part_parser_cleanup(&pparts);
1012 			return err ? err : pparts.nr_parts;
1013 		}
1014 		/*
1015 		 * Stash the first error we see; only report it if no parser
1016 		 * succeeds
1017 		 */
1018 		if (ret < 0 && !err)
1019 			err = ret;
1020 	}
1021 	return err;
1022 }
1023 
1024 void mtd_part_parser_cleanup(struct mtd_partitions *parts)
1025 {
1026 	const struct mtd_part_parser *parser;
1027 
1028 	if (!parts)
1029 		return;
1030 
1031 	parser = parts->parser;
1032 	if (parser) {
1033 		if (parser->cleanup)
1034 			parser->cleanup(parts->parts, parts->nr_parts);
1035 
1036 		mtd_part_parser_put(parser);
1037 	}
1038 }
1039 
1040 int mtd_is_partition(const struct mtd_info *mtd)
1041 {
1042 	struct mtd_part *part;
1043 	int ispart = 0;
1044 
1045 	mutex_lock(&mtd_partitions_mutex);
1046 	list_for_each_entry(part, &mtd_partitions, list)
1047 		if (&part->mtd == mtd) {
1048 			ispart = 1;
1049 			break;
1050 		}
1051 	mutex_unlock(&mtd_partitions_mutex);
1052 
1053 	return ispart;
1054 }
1055 EXPORT_SYMBOL_GPL(mtd_is_partition);
1056 
1057 /* Returns the size of the entire flash chip */
1058 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
1059 {
1060 	if (!mtd_is_partition(mtd))
1061 		return mtd->size;
1062 
1063 	return mtd_get_device_size(mtd_to_part(mtd)->parent);
1064 }
1065 EXPORT_SYMBOL_GPL(mtd_get_device_size);
1066