xref: /openbmc/linux/drivers/mtd/mtdpart.c (revision 4cff79e9)
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 	struct mtd_partitions parsed;
339 	int err;
340 
341 	err = parse_mtd_partitions(&slave->mtd, types, &parsed, NULL);
342 	if (err)
343 		return err;
344 	else if (!parsed.nr_parts)
345 		return -ENOENT;
346 
347 	err = add_mtd_partitions(&slave->mtd, parsed.parts, parsed.nr_parts);
348 
349 	mtd_part_parser_cleanup(&parsed);
350 
351 	return err;
352 }
353 
354 static struct mtd_part *allocate_partition(struct mtd_info *parent,
355 			const struct mtd_partition *part, int partno,
356 			uint64_t cur_offset)
357 {
358 	int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize :
359 							    parent->erasesize;
360 	struct mtd_part *slave;
361 	u32 remainder;
362 	char *name;
363 	u64 tmp;
364 
365 	/* allocate the partition structure */
366 	slave = kzalloc(sizeof(*slave), GFP_KERNEL);
367 	name = kstrdup(part->name, GFP_KERNEL);
368 	if (!name || !slave) {
369 		printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
370 		       parent->name);
371 		kfree(name);
372 		kfree(slave);
373 		return ERR_PTR(-ENOMEM);
374 	}
375 
376 	/* set up the MTD object for this partition */
377 	slave->mtd.type = parent->type;
378 	slave->mtd.flags = parent->flags & ~part->mask_flags;
379 	slave->mtd.size = part->size;
380 	slave->mtd.writesize = parent->writesize;
381 	slave->mtd.writebufsize = parent->writebufsize;
382 	slave->mtd.oobsize = parent->oobsize;
383 	slave->mtd.oobavail = parent->oobavail;
384 	slave->mtd.subpage_sft = parent->subpage_sft;
385 	slave->mtd.pairing = parent->pairing;
386 
387 	slave->mtd.name = name;
388 	slave->mtd.owner = parent->owner;
389 
390 	/* NOTE: Historically, we didn't arrange MTDs as a tree out of
391 	 * concern for showing the same data in multiple partitions.
392 	 * However, it is very useful to have the master node present,
393 	 * so the MTD_PARTITIONED_MASTER option allows that. The master
394 	 * will have device nodes etc only if this is set, so make the
395 	 * parent conditional on that option. Note, this is a way to
396 	 * distinguish between the master and the partition in sysfs.
397 	 */
398 	slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
399 				&parent->dev :
400 				parent->dev.parent;
401 	slave->mtd.dev.of_node = part->of_node;
402 
403 	if (parent->_read)
404 		slave->mtd._read = part_read;
405 	if (parent->_write)
406 		slave->mtd._write = part_write;
407 
408 	if (parent->_panic_write)
409 		slave->mtd._panic_write = part_panic_write;
410 
411 	if (parent->_point && parent->_unpoint) {
412 		slave->mtd._point = part_point;
413 		slave->mtd._unpoint = part_unpoint;
414 	}
415 
416 	if (parent->_read_oob)
417 		slave->mtd._read_oob = part_read_oob;
418 	if (parent->_write_oob)
419 		slave->mtd._write_oob = part_write_oob;
420 	if (parent->_read_user_prot_reg)
421 		slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
422 	if (parent->_read_fact_prot_reg)
423 		slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
424 	if (parent->_write_user_prot_reg)
425 		slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
426 	if (parent->_lock_user_prot_reg)
427 		slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
428 	if (parent->_get_user_prot_info)
429 		slave->mtd._get_user_prot_info = part_get_user_prot_info;
430 	if (parent->_get_fact_prot_info)
431 		slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
432 	if (parent->_sync)
433 		slave->mtd._sync = part_sync;
434 	if (!partno && !parent->dev.class && parent->_suspend &&
435 	    parent->_resume) {
436 		slave->mtd._suspend = part_suspend;
437 		slave->mtd._resume = part_resume;
438 	}
439 	if (parent->_writev)
440 		slave->mtd._writev = part_writev;
441 	if (parent->_lock)
442 		slave->mtd._lock = part_lock;
443 	if (parent->_unlock)
444 		slave->mtd._unlock = part_unlock;
445 	if (parent->_is_locked)
446 		slave->mtd._is_locked = part_is_locked;
447 	if (parent->_block_isreserved)
448 		slave->mtd._block_isreserved = part_block_isreserved;
449 	if (parent->_block_isbad)
450 		slave->mtd._block_isbad = part_block_isbad;
451 	if (parent->_block_markbad)
452 		slave->mtd._block_markbad = part_block_markbad;
453 	if (parent->_max_bad_blocks)
454 		slave->mtd._max_bad_blocks = part_max_bad_blocks;
455 
456 	if (parent->_get_device)
457 		slave->mtd._get_device = part_get_device;
458 	if (parent->_put_device)
459 		slave->mtd._put_device = part_put_device;
460 
461 	slave->mtd._erase = part_erase;
462 	slave->parent = parent;
463 	slave->offset = part->offset;
464 
465 	if (slave->offset == MTDPART_OFS_APPEND)
466 		slave->offset = cur_offset;
467 	if (slave->offset == MTDPART_OFS_NXTBLK) {
468 		tmp = cur_offset;
469 		slave->offset = cur_offset;
470 		remainder = do_div(tmp, wr_alignment);
471 		if (remainder) {
472 			slave->offset += wr_alignment - remainder;
473 			printk(KERN_NOTICE "Moving partition %d: "
474 			       "0x%012llx -> 0x%012llx\n", partno,
475 			       (unsigned long long)cur_offset, (unsigned long long)slave->offset);
476 		}
477 	}
478 	if (slave->offset == MTDPART_OFS_RETAIN) {
479 		slave->offset = cur_offset;
480 		if (parent->size - slave->offset >= slave->mtd.size) {
481 			slave->mtd.size = parent->size - slave->offset
482 							- slave->mtd.size;
483 		} else {
484 			printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
485 				part->name, parent->size - slave->offset,
486 				slave->mtd.size);
487 			/* register to preserve ordering */
488 			goto out_register;
489 		}
490 	}
491 	if (slave->mtd.size == MTDPART_SIZ_FULL)
492 		slave->mtd.size = parent->size - slave->offset;
493 
494 	printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
495 		(unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
496 
497 	/* let's do some sanity checks */
498 	if (slave->offset >= parent->size) {
499 		/* let's register it anyway to preserve ordering */
500 		slave->offset = 0;
501 		slave->mtd.size = 0;
502 		printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
503 			part->name);
504 		goto out_register;
505 	}
506 	if (slave->offset + slave->mtd.size > parent->size) {
507 		slave->mtd.size = parent->size - slave->offset;
508 		printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
509 			part->name, parent->name, (unsigned long long)slave->mtd.size);
510 	}
511 	if (parent->numeraseregions > 1) {
512 		/* Deal with variable erase size stuff */
513 		int i, max = parent->numeraseregions;
514 		u64 end = slave->offset + slave->mtd.size;
515 		struct mtd_erase_region_info *regions = parent->eraseregions;
516 
517 		/* Find the first erase regions which is part of this
518 		 * partition. */
519 		for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
520 			;
521 		/* The loop searched for the region _behind_ the first one */
522 		if (i > 0)
523 			i--;
524 
525 		/* Pick biggest erasesize */
526 		for (; i < max && regions[i].offset < end; i++) {
527 			if (slave->mtd.erasesize < regions[i].erasesize) {
528 				slave->mtd.erasesize = regions[i].erasesize;
529 			}
530 		}
531 		BUG_ON(slave->mtd.erasesize == 0);
532 	} else {
533 		/* Single erase size */
534 		slave->mtd.erasesize = parent->erasesize;
535 	}
536 
537 	/*
538 	 * Slave erasesize might differ from the master one if the master
539 	 * exposes several regions with different erasesize. Adjust
540 	 * wr_alignment accordingly.
541 	 */
542 	if (!(slave->mtd.flags & MTD_NO_ERASE))
543 		wr_alignment = slave->mtd.erasesize;
544 
545 	tmp = slave->offset;
546 	remainder = do_div(tmp, wr_alignment);
547 	if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
548 		/* Doesn't start on a boundary of major erase size */
549 		/* FIXME: Let it be writable if it is on a boundary of
550 		 * _minor_ erase size though */
551 		slave->mtd.flags &= ~MTD_WRITEABLE;
552 		printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
553 			part->name);
554 	}
555 
556 	tmp = slave->mtd.size;
557 	remainder = do_div(tmp, wr_alignment);
558 	if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
559 		slave->mtd.flags &= ~MTD_WRITEABLE;
560 		printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
561 			part->name);
562 	}
563 
564 	mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
565 	slave->mtd.ecc_step_size = parent->ecc_step_size;
566 	slave->mtd.ecc_strength = parent->ecc_strength;
567 	slave->mtd.bitflip_threshold = parent->bitflip_threshold;
568 
569 	if (parent->_block_isbad) {
570 		uint64_t offs = 0;
571 
572 		while (offs < slave->mtd.size) {
573 			if (mtd_block_isreserved(parent, offs + slave->offset))
574 				slave->mtd.ecc_stats.bbtblocks++;
575 			else if (mtd_block_isbad(parent, offs + slave->offset))
576 				slave->mtd.ecc_stats.badblocks++;
577 			offs += slave->mtd.erasesize;
578 		}
579 	}
580 
581 out_register:
582 	return slave;
583 }
584 
585 static ssize_t mtd_partition_offset_show(struct device *dev,
586 		struct device_attribute *attr, char *buf)
587 {
588 	struct mtd_info *mtd = dev_get_drvdata(dev);
589 	struct mtd_part *part = mtd_to_part(mtd);
590 	return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
591 }
592 
593 static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
594 
595 static const struct attribute *mtd_partition_attrs[] = {
596 	&dev_attr_offset.attr,
597 	NULL
598 };
599 
600 static int mtd_add_partition_attrs(struct mtd_part *new)
601 {
602 	int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
603 	if (ret)
604 		printk(KERN_WARNING
605 		       "mtd: failed to create partition attrs, err=%d\n", ret);
606 	return ret;
607 }
608 
609 int mtd_add_partition(struct mtd_info *parent, const char *name,
610 		      long long offset, long long length)
611 {
612 	struct mtd_partition part;
613 	struct mtd_part *new;
614 	int ret = 0;
615 
616 	/* the direct offset is expected */
617 	if (offset == MTDPART_OFS_APPEND ||
618 	    offset == MTDPART_OFS_NXTBLK)
619 		return -EINVAL;
620 
621 	if (length == MTDPART_SIZ_FULL)
622 		length = parent->size - offset;
623 
624 	if (length <= 0)
625 		return -EINVAL;
626 
627 	memset(&part, 0, sizeof(part));
628 	part.name = name;
629 	part.size = length;
630 	part.offset = offset;
631 
632 	new = allocate_partition(parent, &part, -1, offset);
633 	if (IS_ERR(new))
634 		return PTR_ERR(new);
635 
636 	mutex_lock(&mtd_partitions_mutex);
637 	list_add(&new->list, &mtd_partitions);
638 	mutex_unlock(&mtd_partitions_mutex);
639 
640 	add_mtd_device(&new->mtd);
641 
642 	mtd_add_partition_attrs(new);
643 
644 	return ret;
645 }
646 EXPORT_SYMBOL_GPL(mtd_add_partition);
647 
648 /**
649  * __mtd_del_partition - delete MTD partition
650  *
651  * @priv: internal MTD struct for partition to be deleted
652  *
653  * This function must be called with the partitions mutex locked.
654  */
655 static int __mtd_del_partition(struct mtd_part *priv)
656 {
657 	struct mtd_part *child, *next;
658 	int err;
659 
660 	list_for_each_entry_safe(child, next, &mtd_partitions, list) {
661 		if (child->parent == &priv->mtd) {
662 			err = __mtd_del_partition(child);
663 			if (err)
664 				return err;
665 		}
666 	}
667 
668 	sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs);
669 
670 	err = del_mtd_device(&priv->mtd);
671 	if (err)
672 		return err;
673 
674 	list_del(&priv->list);
675 	free_partition(priv);
676 
677 	return 0;
678 }
679 
680 /*
681  * This function unregisters and destroy all slave MTD objects which are
682  * attached to the given MTD object.
683  */
684 int del_mtd_partitions(struct mtd_info *mtd)
685 {
686 	struct mtd_part *slave, *next;
687 	int ret, err = 0;
688 
689 	mutex_lock(&mtd_partitions_mutex);
690 	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
691 		if (slave->parent == mtd) {
692 			ret = __mtd_del_partition(slave);
693 			if (ret < 0)
694 				err = ret;
695 		}
696 	mutex_unlock(&mtd_partitions_mutex);
697 
698 	return err;
699 }
700 
701 int mtd_del_partition(struct mtd_info *mtd, int partno)
702 {
703 	struct mtd_part *slave, *next;
704 	int ret = -EINVAL;
705 
706 	mutex_lock(&mtd_partitions_mutex);
707 	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
708 		if ((slave->parent == mtd) &&
709 		    (slave->mtd.index == partno)) {
710 			ret = __mtd_del_partition(slave);
711 			break;
712 		}
713 	mutex_unlock(&mtd_partitions_mutex);
714 
715 	return ret;
716 }
717 EXPORT_SYMBOL_GPL(mtd_del_partition);
718 
719 /*
720  * This function, given a master MTD object and a partition table, creates
721  * and registers slave MTD objects which are bound to the master according to
722  * the partition definitions.
723  *
724  * For historical reasons, this function's caller only registers the master
725  * if the MTD_PARTITIONED_MASTER config option is set.
726  */
727 
728 int add_mtd_partitions(struct mtd_info *master,
729 		       const struct mtd_partition *parts,
730 		       int nbparts)
731 {
732 	struct mtd_part *slave;
733 	uint64_t cur_offset = 0;
734 	int i;
735 
736 	printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
737 
738 	for (i = 0; i < nbparts; i++) {
739 		slave = allocate_partition(master, parts + i, i, cur_offset);
740 		if (IS_ERR(slave)) {
741 			del_mtd_partitions(master);
742 			return PTR_ERR(slave);
743 		}
744 
745 		mutex_lock(&mtd_partitions_mutex);
746 		list_add(&slave->list, &mtd_partitions);
747 		mutex_unlock(&mtd_partitions_mutex);
748 
749 		add_mtd_device(&slave->mtd);
750 		mtd_add_partition_attrs(slave);
751 		if (parts[i].types)
752 			mtd_parse_part(slave, parts[i].types);
753 
754 		cur_offset = slave->offset + slave->mtd.size;
755 	}
756 
757 	return 0;
758 }
759 
760 static DEFINE_SPINLOCK(part_parser_lock);
761 static LIST_HEAD(part_parsers);
762 
763 static struct mtd_part_parser *mtd_part_parser_get(const char *name)
764 {
765 	struct mtd_part_parser *p, *ret = NULL;
766 
767 	spin_lock(&part_parser_lock);
768 
769 	list_for_each_entry(p, &part_parsers, list)
770 		if (!strcmp(p->name, name) && try_module_get(p->owner)) {
771 			ret = p;
772 			break;
773 		}
774 
775 	spin_unlock(&part_parser_lock);
776 
777 	return ret;
778 }
779 
780 static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
781 {
782 	module_put(p->owner);
783 }
784 
785 /*
786  * Many partition parsers just expected the core to kfree() all their data in
787  * one chunk. Do that by default.
788  */
789 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
790 					    int nr_parts)
791 {
792 	kfree(pparts);
793 }
794 
795 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
796 {
797 	p->owner = owner;
798 
799 	if (!p->cleanup)
800 		p->cleanup = &mtd_part_parser_cleanup_default;
801 
802 	spin_lock(&part_parser_lock);
803 	list_add(&p->list, &part_parsers);
804 	spin_unlock(&part_parser_lock);
805 
806 	return 0;
807 }
808 EXPORT_SYMBOL_GPL(__register_mtd_parser);
809 
810 void deregister_mtd_parser(struct mtd_part_parser *p)
811 {
812 	spin_lock(&part_parser_lock);
813 	list_del(&p->list);
814 	spin_unlock(&part_parser_lock);
815 }
816 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
817 
818 /*
819  * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
820  * are changing this array!
821  */
822 static const char * const default_mtd_part_types[] = {
823 	"cmdlinepart",
824 	"ofpart",
825 	NULL
826 };
827 
828 static int mtd_part_do_parse(struct mtd_part_parser *parser,
829 			     struct mtd_info *master,
830 			     struct mtd_partitions *pparts,
831 			     struct mtd_part_parser_data *data)
832 {
833 	int ret;
834 
835 	ret = (*parser->parse_fn)(master, &pparts->parts, data);
836 	pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
837 	if (ret <= 0)
838 		return ret;
839 
840 	pr_notice("%d %s partitions found on MTD device %s\n", ret,
841 		  parser->name, master->name);
842 
843 	pparts->nr_parts = ret;
844 	pparts->parser = parser;
845 
846 	return ret;
847 }
848 
849 /**
850  * mtd_part_get_compatible_parser - find MTD parser by a compatible string
851  *
852  * @compat: compatible string describing partitions in a device tree
853  *
854  * MTD parsers can specify supported partitions by providing a table of
855  * compatibility strings. This function finds a parser that advertises support
856  * for a passed value of "compatible".
857  */
858 static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
859 {
860 	struct mtd_part_parser *p, *ret = NULL;
861 
862 	spin_lock(&part_parser_lock);
863 
864 	list_for_each_entry(p, &part_parsers, list) {
865 		const struct of_device_id *matches;
866 
867 		matches = p->of_match_table;
868 		if (!matches)
869 			continue;
870 
871 		for (; matches->compatible[0]; matches++) {
872 			if (!strcmp(matches->compatible, compat) &&
873 			    try_module_get(p->owner)) {
874 				ret = p;
875 				break;
876 			}
877 		}
878 
879 		if (ret)
880 			break;
881 	}
882 
883 	spin_unlock(&part_parser_lock);
884 
885 	return ret;
886 }
887 
888 static int mtd_part_of_parse(struct mtd_info *master,
889 			     struct mtd_partitions *pparts)
890 {
891 	struct mtd_part_parser *parser;
892 	struct device_node *np;
893 	struct property *prop;
894 	const char *compat;
895 	const char *fixed = "fixed-partitions";
896 	int ret, err = 0;
897 
898 	np = of_get_child_by_name(mtd_get_of_node(master), "partitions");
899 	of_property_for_each_string(np, "compatible", prop, compat) {
900 		parser = mtd_part_get_compatible_parser(compat);
901 		if (!parser)
902 			continue;
903 		ret = mtd_part_do_parse(parser, master, pparts, NULL);
904 		if (ret > 0) {
905 			of_node_put(np);
906 			return ret;
907 		}
908 		mtd_part_parser_put(parser);
909 		if (ret < 0 && !err)
910 			err = ret;
911 	}
912 	of_node_put(np);
913 
914 	/*
915 	 * For backward compatibility we have to try the "fixed-partitions"
916 	 * parser. It supports old DT format with partitions specified as a
917 	 * direct subnodes of a flash device DT node without any compatibility
918 	 * specified we could match.
919 	 */
920 	parser = mtd_part_parser_get(fixed);
921 	if (!parser && !request_module("%s", fixed))
922 		parser = mtd_part_parser_get(fixed);
923 	if (parser) {
924 		ret = mtd_part_do_parse(parser, master, pparts, NULL);
925 		if (ret > 0)
926 			return ret;
927 		mtd_part_parser_put(parser);
928 		if (ret < 0 && !err)
929 			err = ret;
930 	}
931 
932 	return err;
933 }
934 
935 /**
936  * parse_mtd_partitions - parse MTD partitions
937  * @master: the master partition (describes whole MTD device)
938  * @types: names of partition parsers to try or %NULL
939  * @pparts: info about partitions found is returned here
940  * @data: MTD partition parser-specific data
941  *
942  * This function tries to find partition on MTD device @master. It uses MTD
943  * partition parsers, specified in @types. However, if @types is %NULL, then
944  * the default list of parsers is used. The default list contains only the
945  * "cmdlinepart" and "ofpart" parsers ATM.
946  * Note: If there are more then one parser in @types, the kernel only takes the
947  * partitions parsed out by the first parser.
948  *
949  * This function may return:
950  * o a negative error code in case of failure
951  * o zero otherwise, and @pparts will describe the partitions, number of
952  *   partitions, and the parser which parsed them. Caller must release
953  *   resources with mtd_part_parser_cleanup() when finished with the returned
954  *   data.
955  */
956 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
957 			 struct mtd_partitions *pparts,
958 			 struct mtd_part_parser_data *data)
959 {
960 	struct mtd_part_parser *parser;
961 	int ret, err = 0;
962 
963 	if (!types)
964 		types = default_mtd_part_types;
965 
966 	for ( ; *types; types++) {
967 		/*
968 		 * ofpart is a special type that means OF partitioning info
969 		 * should be used. It requires a bit different logic so it is
970 		 * handled in a separated function.
971 		 */
972 		if (!strcmp(*types, "ofpart")) {
973 			ret = mtd_part_of_parse(master, pparts);
974 		} else {
975 			pr_debug("%s: parsing partitions %s\n", master->name,
976 				 *types);
977 			parser = mtd_part_parser_get(*types);
978 			if (!parser && !request_module("%s", *types))
979 				parser = mtd_part_parser_get(*types);
980 			pr_debug("%s: got parser %s\n", master->name,
981 				parser ? parser->name : NULL);
982 			if (!parser)
983 				continue;
984 			ret = mtd_part_do_parse(parser, master, pparts, data);
985 			if (ret <= 0)
986 				mtd_part_parser_put(parser);
987 		}
988 		/* Found partitions! */
989 		if (ret > 0)
990 			return 0;
991 		/*
992 		 * Stash the first error we see; only report it if no parser
993 		 * succeeds
994 		 */
995 		if (ret < 0 && !err)
996 			err = ret;
997 	}
998 	return err;
999 }
1000 
1001 void mtd_part_parser_cleanup(struct mtd_partitions *parts)
1002 {
1003 	const struct mtd_part_parser *parser;
1004 
1005 	if (!parts)
1006 		return;
1007 
1008 	parser = parts->parser;
1009 	if (parser) {
1010 		if (parser->cleanup)
1011 			parser->cleanup(parts->parts, parts->nr_parts);
1012 
1013 		mtd_part_parser_put(parser);
1014 	}
1015 }
1016 
1017 int mtd_is_partition(const struct mtd_info *mtd)
1018 {
1019 	struct mtd_part *part;
1020 	int ispart = 0;
1021 
1022 	mutex_lock(&mtd_partitions_mutex);
1023 	list_for_each_entry(part, &mtd_partitions, list)
1024 		if (&part->mtd == mtd) {
1025 			ispart = 1;
1026 			break;
1027 		}
1028 	mutex_unlock(&mtd_partitions_mutex);
1029 
1030 	return ispart;
1031 }
1032 EXPORT_SYMBOL_GPL(mtd_is_partition);
1033 
1034 /* Returns the size of the entire flash chip */
1035 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
1036 {
1037 	if (!mtd_is_partition(mtd))
1038 		return mtd->size;
1039 
1040 	return mtd_get_device_size(mtd_to_part(mtd)->parent);
1041 }
1042 EXPORT_SYMBOL_GPL(mtd_get_device_size);
1043