xref: /openbmc/linux/drivers/mtd/mtdpart.c (revision 6774def6)
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 
34 #include "mtdcore.h"
35 
36 /* Our partition linked list */
37 static LIST_HEAD(mtd_partitions);
38 static DEFINE_MUTEX(mtd_partitions_mutex);
39 
40 /* Our partition node structure */
41 struct mtd_part {
42 	struct mtd_info mtd;
43 	struct mtd_info *master;
44 	uint64_t offset;
45 	struct list_head list;
46 };
47 
48 /*
49  * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
50  * the pointer to that structure with this macro.
51  */
52 #define PART(x)  ((struct mtd_part *)(x))
53 
54 
55 /*
56  * MTD methods which simply translate the effective address and pass through
57  * to the _real_ device.
58  */
59 
60 static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
61 		size_t *retlen, u_char *buf)
62 {
63 	struct mtd_part *part = PART(mtd);
64 	struct mtd_ecc_stats stats;
65 	int res;
66 
67 	stats = part->master->ecc_stats;
68 	res = part->master->_read(part->master, from + part->offset, len,
69 				  retlen, buf);
70 	if (unlikely(mtd_is_eccerr(res)))
71 		mtd->ecc_stats.failed +=
72 			part->master->ecc_stats.failed - stats.failed;
73 	else
74 		mtd->ecc_stats.corrected +=
75 			part->master->ecc_stats.corrected - stats.corrected;
76 	return res;
77 }
78 
79 static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
80 		size_t *retlen, void **virt, resource_size_t *phys)
81 {
82 	struct mtd_part *part = PART(mtd);
83 
84 	return part->master->_point(part->master, from + part->offset, len,
85 				    retlen, virt, phys);
86 }
87 
88 static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
89 {
90 	struct mtd_part *part = PART(mtd);
91 
92 	return part->master->_unpoint(part->master, from + part->offset, len);
93 }
94 
95 static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
96 					    unsigned long len,
97 					    unsigned long offset,
98 					    unsigned long flags)
99 {
100 	struct mtd_part *part = PART(mtd);
101 
102 	offset += part->offset;
103 	return part->master->_get_unmapped_area(part->master, len, offset,
104 						flags);
105 }
106 
107 static int part_read_oob(struct mtd_info *mtd, loff_t from,
108 		struct mtd_oob_ops *ops)
109 {
110 	struct mtd_part *part = PART(mtd);
111 	int res;
112 
113 	if (from >= mtd->size)
114 		return -EINVAL;
115 	if (ops->datbuf && from + ops->len > mtd->size)
116 		return -EINVAL;
117 
118 	/*
119 	 * If OOB is also requested, make sure that we do not read past the end
120 	 * of this partition.
121 	 */
122 	if (ops->oobbuf) {
123 		size_t len, pages;
124 
125 		if (ops->mode == MTD_OPS_AUTO_OOB)
126 			len = mtd->oobavail;
127 		else
128 			len = mtd->oobsize;
129 		pages = mtd_div_by_ws(mtd->size, mtd);
130 		pages -= mtd_div_by_ws(from, mtd);
131 		if (ops->ooboffs + ops->ooblen > pages * len)
132 			return -EINVAL;
133 	}
134 
135 	res = part->master->_read_oob(part->master, from + part->offset, ops);
136 	if (unlikely(res)) {
137 		if (mtd_is_bitflip(res))
138 			mtd->ecc_stats.corrected++;
139 		if (mtd_is_eccerr(res))
140 			mtd->ecc_stats.failed++;
141 	}
142 	return res;
143 }
144 
145 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
146 		size_t len, size_t *retlen, u_char *buf)
147 {
148 	struct mtd_part *part = PART(mtd);
149 	return part->master->_read_user_prot_reg(part->master, from, len,
150 						 retlen, buf);
151 }
152 
153 static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
154 				   size_t *retlen, struct otp_info *buf)
155 {
156 	struct mtd_part *part = PART(mtd);
157 	return part->master->_get_user_prot_info(part->master, len, retlen,
158 						 buf);
159 }
160 
161 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
162 		size_t len, size_t *retlen, u_char *buf)
163 {
164 	struct mtd_part *part = PART(mtd);
165 	return part->master->_read_fact_prot_reg(part->master, from, len,
166 						 retlen, buf);
167 }
168 
169 static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
170 				   size_t *retlen, struct otp_info *buf)
171 {
172 	struct mtd_part *part = PART(mtd);
173 	return part->master->_get_fact_prot_info(part->master, len, retlen,
174 						 buf);
175 }
176 
177 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
178 		size_t *retlen, const u_char *buf)
179 {
180 	struct mtd_part *part = PART(mtd);
181 	return part->master->_write(part->master, to + part->offset, len,
182 				    retlen, buf);
183 }
184 
185 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
186 		size_t *retlen, const u_char *buf)
187 {
188 	struct mtd_part *part = PART(mtd);
189 	return part->master->_panic_write(part->master, to + part->offset, len,
190 					  retlen, buf);
191 }
192 
193 static int part_write_oob(struct mtd_info *mtd, loff_t to,
194 		struct mtd_oob_ops *ops)
195 {
196 	struct mtd_part *part = PART(mtd);
197 
198 	if (to >= mtd->size)
199 		return -EINVAL;
200 	if (ops->datbuf && to + ops->len > mtd->size)
201 		return -EINVAL;
202 	return part->master->_write_oob(part->master, to + part->offset, ops);
203 }
204 
205 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
206 		size_t len, size_t *retlen, u_char *buf)
207 {
208 	struct mtd_part *part = PART(mtd);
209 	return part->master->_write_user_prot_reg(part->master, from, len,
210 						  retlen, buf);
211 }
212 
213 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
214 		size_t len)
215 {
216 	struct mtd_part *part = PART(mtd);
217 	return part->master->_lock_user_prot_reg(part->master, from, len);
218 }
219 
220 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
221 		unsigned long count, loff_t to, size_t *retlen)
222 {
223 	struct mtd_part *part = PART(mtd);
224 	return part->master->_writev(part->master, vecs, count,
225 				     to + part->offset, retlen);
226 }
227 
228 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
229 {
230 	struct mtd_part *part = PART(mtd);
231 	int ret;
232 
233 	instr->addr += part->offset;
234 	ret = part->master->_erase(part->master, instr);
235 	if (ret) {
236 		if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
237 			instr->fail_addr -= part->offset;
238 		instr->addr -= part->offset;
239 	}
240 	return ret;
241 }
242 
243 void mtd_erase_callback(struct erase_info *instr)
244 {
245 	if (instr->mtd->_erase == part_erase) {
246 		struct mtd_part *part = PART(instr->mtd);
247 
248 		if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
249 			instr->fail_addr -= part->offset;
250 		instr->addr -= part->offset;
251 	}
252 	if (instr->callback)
253 		instr->callback(instr);
254 }
255 EXPORT_SYMBOL_GPL(mtd_erase_callback);
256 
257 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
258 {
259 	struct mtd_part *part = PART(mtd);
260 	return part->master->_lock(part->master, ofs + part->offset, len);
261 }
262 
263 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
264 {
265 	struct mtd_part *part = PART(mtd);
266 	return part->master->_unlock(part->master, ofs + part->offset, len);
267 }
268 
269 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
270 {
271 	struct mtd_part *part = PART(mtd);
272 	return part->master->_is_locked(part->master, ofs + part->offset, len);
273 }
274 
275 static void part_sync(struct mtd_info *mtd)
276 {
277 	struct mtd_part *part = PART(mtd);
278 	part->master->_sync(part->master);
279 }
280 
281 static int part_suspend(struct mtd_info *mtd)
282 {
283 	struct mtd_part *part = PART(mtd);
284 	return part->master->_suspend(part->master);
285 }
286 
287 static void part_resume(struct mtd_info *mtd)
288 {
289 	struct mtd_part *part = PART(mtd);
290 	part->master->_resume(part->master);
291 }
292 
293 static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
294 {
295 	struct mtd_part *part = PART(mtd);
296 	ofs += part->offset;
297 	return part->master->_block_isreserved(part->master, ofs);
298 }
299 
300 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
301 {
302 	struct mtd_part *part = PART(mtd);
303 	ofs += part->offset;
304 	return part->master->_block_isbad(part->master, ofs);
305 }
306 
307 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
308 {
309 	struct mtd_part *part = PART(mtd);
310 	int res;
311 
312 	ofs += part->offset;
313 	res = part->master->_block_markbad(part->master, ofs);
314 	if (!res)
315 		mtd->ecc_stats.badblocks++;
316 	return res;
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  * This function unregisters and destroy all slave MTD objects which are
327  * attached to the given master MTD object.
328  */
329 
330 int del_mtd_partitions(struct mtd_info *master)
331 {
332 	struct mtd_part *slave, *next;
333 	int ret, err = 0;
334 
335 	mutex_lock(&mtd_partitions_mutex);
336 	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
337 		if (slave->master == master) {
338 			ret = del_mtd_device(&slave->mtd);
339 			if (ret < 0) {
340 				err = ret;
341 				continue;
342 			}
343 			list_del(&slave->list);
344 			free_partition(slave);
345 		}
346 	mutex_unlock(&mtd_partitions_mutex);
347 
348 	return err;
349 }
350 
351 static struct mtd_part *allocate_partition(struct mtd_info *master,
352 			const struct mtd_partition *part, int partno,
353 			uint64_t cur_offset)
354 {
355 	struct mtd_part *slave;
356 	char *name;
357 
358 	/* allocate the partition structure */
359 	slave = kzalloc(sizeof(*slave), GFP_KERNEL);
360 	name = kstrdup(part->name, GFP_KERNEL);
361 	if (!name || !slave) {
362 		printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
363 		       master->name);
364 		kfree(name);
365 		kfree(slave);
366 		return ERR_PTR(-ENOMEM);
367 	}
368 
369 	/* set up the MTD object for this partition */
370 	slave->mtd.type = master->type;
371 	slave->mtd.flags = master->flags & ~part->mask_flags;
372 	slave->mtd.size = part->size;
373 	slave->mtd.writesize = master->writesize;
374 	slave->mtd.writebufsize = master->writebufsize;
375 	slave->mtd.oobsize = master->oobsize;
376 	slave->mtd.oobavail = master->oobavail;
377 	slave->mtd.subpage_sft = master->subpage_sft;
378 
379 	slave->mtd.name = name;
380 	slave->mtd.owner = master->owner;
381 	slave->mtd.backing_dev_info = master->backing_dev_info;
382 
383 	/* NOTE:  we don't arrange MTDs as a tree; it'd be error-prone
384 	 * to have the same data be in two different partitions.
385 	 */
386 	slave->mtd.dev.parent = master->dev.parent;
387 
388 	slave->mtd._read = part_read;
389 	slave->mtd._write = part_write;
390 
391 	if (master->_panic_write)
392 		slave->mtd._panic_write = part_panic_write;
393 
394 	if (master->_point && master->_unpoint) {
395 		slave->mtd._point = part_point;
396 		slave->mtd._unpoint = part_unpoint;
397 	}
398 
399 	if (master->_get_unmapped_area)
400 		slave->mtd._get_unmapped_area = part_get_unmapped_area;
401 	if (master->_read_oob)
402 		slave->mtd._read_oob = part_read_oob;
403 	if (master->_write_oob)
404 		slave->mtd._write_oob = part_write_oob;
405 	if (master->_read_user_prot_reg)
406 		slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
407 	if (master->_read_fact_prot_reg)
408 		slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
409 	if (master->_write_user_prot_reg)
410 		slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
411 	if (master->_lock_user_prot_reg)
412 		slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
413 	if (master->_get_user_prot_info)
414 		slave->mtd._get_user_prot_info = part_get_user_prot_info;
415 	if (master->_get_fact_prot_info)
416 		slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
417 	if (master->_sync)
418 		slave->mtd._sync = part_sync;
419 	if (!partno && !master->dev.class && master->_suspend &&
420 	    master->_resume) {
421 			slave->mtd._suspend = part_suspend;
422 			slave->mtd._resume = part_resume;
423 	}
424 	if (master->_writev)
425 		slave->mtd._writev = part_writev;
426 	if (master->_lock)
427 		slave->mtd._lock = part_lock;
428 	if (master->_unlock)
429 		slave->mtd._unlock = part_unlock;
430 	if (master->_is_locked)
431 		slave->mtd._is_locked = part_is_locked;
432 	if (master->_block_isreserved)
433 		slave->mtd._block_isreserved = part_block_isreserved;
434 	if (master->_block_isbad)
435 		slave->mtd._block_isbad = part_block_isbad;
436 	if (master->_block_markbad)
437 		slave->mtd._block_markbad = part_block_markbad;
438 	slave->mtd._erase = part_erase;
439 	slave->master = master;
440 	slave->offset = part->offset;
441 
442 	if (slave->offset == MTDPART_OFS_APPEND)
443 		slave->offset = cur_offset;
444 	if (slave->offset == MTDPART_OFS_NXTBLK) {
445 		slave->offset = cur_offset;
446 		if (mtd_mod_by_eb(cur_offset, master) != 0) {
447 			/* Round up to next erasesize */
448 			slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
449 			printk(KERN_NOTICE "Moving partition %d: "
450 			       "0x%012llx -> 0x%012llx\n", partno,
451 			       (unsigned long long)cur_offset, (unsigned long long)slave->offset);
452 		}
453 	}
454 	if (slave->offset == MTDPART_OFS_RETAIN) {
455 		slave->offset = cur_offset;
456 		if (master->size - slave->offset >= slave->mtd.size) {
457 			slave->mtd.size = master->size - slave->offset
458 							- slave->mtd.size;
459 		} else {
460 			printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
461 				part->name, master->size - slave->offset,
462 				slave->mtd.size);
463 			/* register to preserve ordering */
464 			goto out_register;
465 		}
466 	}
467 	if (slave->mtd.size == MTDPART_SIZ_FULL)
468 		slave->mtd.size = master->size - slave->offset;
469 
470 	printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
471 		(unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
472 
473 	/* let's do some sanity checks */
474 	if (slave->offset >= master->size) {
475 		/* let's register it anyway to preserve ordering */
476 		slave->offset = 0;
477 		slave->mtd.size = 0;
478 		printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
479 			part->name);
480 		goto out_register;
481 	}
482 	if (slave->offset + slave->mtd.size > master->size) {
483 		slave->mtd.size = master->size - slave->offset;
484 		printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
485 			part->name, master->name, (unsigned long long)slave->mtd.size);
486 	}
487 	if (master->numeraseregions > 1) {
488 		/* Deal with variable erase size stuff */
489 		int i, max = master->numeraseregions;
490 		u64 end = slave->offset + slave->mtd.size;
491 		struct mtd_erase_region_info *regions = master->eraseregions;
492 
493 		/* Find the first erase regions which is part of this
494 		 * partition. */
495 		for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
496 			;
497 		/* The loop searched for the region _behind_ the first one */
498 		if (i > 0)
499 			i--;
500 
501 		/* Pick biggest erasesize */
502 		for (; i < max && regions[i].offset < end; i++) {
503 			if (slave->mtd.erasesize < regions[i].erasesize) {
504 				slave->mtd.erasesize = regions[i].erasesize;
505 			}
506 		}
507 		BUG_ON(slave->mtd.erasesize == 0);
508 	} else {
509 		/* Single erase size */
510 		slave->mtd.erasesize = master->erasesize;
511 	}
512 
513 	if ((slave->mtd.flags & MTD_WRITEABLE) &&
514 	    mtd_mod_by_eb(slave->offset, &slave->mtd)) {
515 		/* Doesn't start on a boundary of major erase size */
516 		/* FIXME: Let it be writable if it is on a boundary of
517 		 * _minor_ erase size though */
518 		slave->mtd.flags &= ~MTD_WRITEABLE;
519 		printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
520 			part->name);
521 	}
522 	if ((slave->mtd.flags & MTD_WRITEABLE) &&
523 	    mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
524 		slave->mtd.flags &= ~MTD_WRITEABLE;
525 		printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
526 			part->name);
527 	}
528 
529 	slave->mtd.ecclayout = master->ecclayout;
530 	slave->mtd.ecc_step_size = master->ecc_step_size;
531 	slave->mtd.ecc_strength = master->ecc_strength;
532 	slave->mtd.bitflip_threshold = master->bitflip_threshold;
533 
534 	if (master->_block_isbad) {
535 		uint64_t offs = 0;
536 
537 		while (offs < slave->mtd.size) {
538 			if (mtd_block_isreserved(master, offs + slave->offset))
539 				slave->mtd.ecc_stats.bbtblocks++;
540 			else if (mtd_block_isbad(master, offs + slave->offset))
541 				slave->mtd.ecc_stats.badblocks++;
542 			offs += slave->mtd.erasesize;
543 		}
544 	}
545 
546 out_register:
547 	return slave;
548 }
549 
550 int mtd_add_partition(struct mtd_info *master, const char *name,
551 		      long long offset, long long length)
552 {
553 	struct mtd_partition part;
554 	struct mtd_part *p, *new;
555 	uint64_t start, end;
556 	int ret = 0;
557 
558 	/* the direct offset is expected */
559 	if (offset == MTDPART_OFS_APPEND ||
560 	    offset == MTDPART_OFS_NXTBLK)
561 		return -EINVAL;
562 
563 	if (length == MTDPART_SIZ_FULL)
564 		length = master->size - offset;
565 
566 	if (length <= 0)
567 		return -EINVAL;
568 
569 	part.name = name;
570 	part.size = length;
571 	part.offset = offset;
572 	part.mask_flags = 0;
573 	part.ecclayout = NULL;
574 
575 	new = allocate_partition(master, &part, -1, offset);
576 	if (IS_ERR(new))
577 		return PTR_ERR(new);
578 
579 	start = offset;
580 	end = offset + length;
581 
582 	mutex_lock(&mtd_partitions_mutex);
583 	list_for_each_entry(p, &mtd_partitions, list)
584 		if (p->master == master) {
585 			if ((start >= p->offset) &&
586 			    (start < (p->offset + p->mtd.size)))
587 				goto err_inv;
588 
589 			if ((end >= p->offset) &&
590 			    (end < (p->offset + p->mtd.size)))
591 				goto err_inv;
592 		}
593 
594 	list_add(&new->list, &mtd_partitions);
595 	mutex_unlock(&mtd_partitions_mutex);
596 
597 	add_mtd_device(&new->mtd);
598 
599 	return ret;
600 err_inv:
601 	mutex_unlock(&mtd_partitions_mutex);
602 	free_partition(new);
603 	return -EINVAL;
604 }
605 EXPORT_SYMBOL_GPL(mtd_add_partition);
606 
607 int mtd_del_partition(struct mtd_info *master, int partno)
608 {
609 	struct mtd_part *slave, *next;
610 	int ret = -EINVAL;
611 
612 	mutex_lock(&mtd_partitions_mutex);
613 	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
614 		if ((slave->master == master) &&
615 		    (slave->mtd.index == partno)) {
616 			ret = del_mtd_device(&slave->mtd);
617 			if (ret < 0)
618 				break;
619 
620 			list_del(&slave->list);
621 			free_partition(slave);
622 			break;
623 		}
624 	mutex_unlock(&mtd_partitions_mutex);
625 
626 	return ret;
627 }
628 EXPORT_SYMBOL_GPL(mtd_del_partition);
629 
630 /*
631  * This function, given a master MTD object and a partition table, creates
632  * and registers slave MTD objects which are bound to the master according to
633  * the partition definitions.
634  *
635  * We don't register the master, or expect the caller to have done so,
636  * for reasons of data integrity.
637  */
638 
639 int add_mtd_partitions(struct mtd_info *master,
640 		       const struct mtd_partition *parts,
641 		       int nbparts)
642 {
643 	struct mtd_part *slave;
644 	uint64_t cur_offset = 0;
645 	int i;
646 
647 	printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
648 
649 	for (i = 0; i < nbparts; i++) {
650 		slave = allocate_partition(master, parts + i, i, cur_offset);
651 		if (IS_ERR(slave))
652 			return PTR_ERR(slave);
653 
654 		mutex_lock(&mtd_partitions_mutex);
655 		list_add(&slave->list, &mtd_partitions);
656 		mutex_unlock(&mtd_partitions_mutex);
657 
658 		add_mtd_device(&slave->mtd);
659 
660 		cur_offset = slave->offset + slave->mtd.size;
661 	}
662 
663 	return 0;
664 }
665 
666 static DEFINE_SPINLOCK(part_parser_lock);
667 static LIST_HEAD(part_parsers);
668 
669 static struct mtd_part_parser *get_partition_parser(const char *name)
670 {
671 	struct mtd_part_parser *p, *ret = NULL;
672 
673 	spin_lock(&part_parser_lock);
674 
675 	list_for_each_entry(p, &part_parsers, list)
676 		if (!strcmp(p->name, name) && try_module_get(p->owner)) {
677 			ret = p;
678 			break;
679 		}
680 
681 	spin_unlock(&part_parser_lock);
682 
683 	return ret;
684 }
685 
686 #define put_partition_parser(p) do { module_put((p)->owner); } while (0)
687 
688 void register_mtd_parser(struct mtd_part_parser *p)
689 {
690 	spin_lock(&part_parser_lock);
691 	list_add(&p->list, &part_parsers);
692 	spin_unlock(&part_parser_lock);
693 }
694 EXPORT_SYMBOL_GPL(register_mtd_parser);
695 
696 void deregister_mtd_parser(struct mtd_part_parser *p)
697 {
698 	spin_lock(&part_parser_lock);
699 	list_del(&p->list);
700 	spin_unlock(&part_parser_lock);
701 }
702 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
703 
704 /*
705  * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
706  * are changing this array!
707  */
708 static const char * const default_mtd_part_types[] = {
709 	"cmdlinepart",
710 	"ofpart",
711 	NULL
712 };
713 
714 /**
715  * parse_mtd_partitions - parse MTD partitions
716  * @master: the master partition (describes whole MTD device)
717  * @types: names of partition parsers to try or %NULL
718  * @pparts: array of partitions found is returned here
719  * @data: MTD partition parser-specific data
720  *
721  * This function tries to find partition on MTD device @master. It uses MTD
722  * partition parsers, specified in @types. However, if @types is %NULL, then
723  * the default list of parsers is used. The default list contains only the
724  * "cmdlinepart" and "ofpart" parsers ATM.
725  * Note: If there are more then one parser in @types, the kernel only takes the
726  * partitions parsed out by the first parser.
727  *
728  * This function may return:
729  * o a negative error code in case of failure
730  * o zero if no partitions were found
731  * o a positive number of found partitions, in which case on exit @pparts will
732  *   point to an array containing this number of &struct mtd_info objects.
733  */
734 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
735 			 struct mtd_partition **pparts,
736 			 struct mtd_part_parser_data *data)
737 {
738 	struct mtd_part_parser *parser;
739 	int ret = 0;
740 
741 	if (!types)
742 		types = default_mtd_part_types;
743 
744 	for ( ; ret <= 0 && *types; types++) {
745 		parser = get_partition_parser(*types);
746 		if (!parser && !request_module("%s", *types))
747 			parser = get_partition_parser(*types);
748 		if (!parser)
749 			continue;
750 		ret = (*parser->parse_fn)(master, pparts, data);
751 		put_partition_parser(parser);
752 		if (ret > 0) {
753 			printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
754 			       ret, parser->name, master->name);
755 			break;
756 		}
757 	}
758 	return ret;
759 }
760 
761 int mtd_is_partition(const struct mtd_info *mtd)
762 {
763 	struct mtd_part *part;
764 	int ispart = 0;
765 
766 	mutex_lock(&mtd_partitions_mutex);
767 	list_for_each_entry(part, &mtd_partitions, list)
768 		if (&part->mtd == mtd) {
769 			ispart = 1;
770 			break;
771 		}
772 	mutex_unlock(&mtd_partitions_mutex);
773 
774 	return ispart;
775 }
776 EXPORT_SYMBOL_GPL(mtd_is_partition);
777 
778 /* Returns the size of the entire flash chip */
779 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
780 {
781 	if (!mtd_is_partition(mtd))
782 		return mtd->size;
783 
784 	return PART(mtd)->master->size;
785 }
786 EXPORT_SYMBOL_GPL(mtd_get_device_size);
787