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