Merge branch 'x86-pti-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / mtd / mtdpart.c
blob76cd21d1171b51bb22dd843519db716591649f93
1 /*
2 * Simple MTD partitioning layer
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>
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.
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.
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
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>
34 #include "mtdcore.h"
36 /* Our partition linked list */
37 static LIST_HEAD(mtd_partitions);
38 static DEFINE_MUTEX(mtd_partitions_mutex);
40 /**
41 * struct mtd_part - our partition node structure
43 * @mtd: struct holding partition details
44 * @parent: parent mtd - flash device or another partition
45 * @offset: partition offset relative to the *flash device*
47 struct mtd_part {
48 struct mtd_info mtd;
49 struct mtd_info *parent;
50 uint64_t offset;
51 struct list_head list;
55 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
56 * the pointer to that structure.
58 static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd)
60 return container_of(mtd, struct mtd_part, mtd);
65 * MTD methods which simply translate the effective address and pass through
66 * to the _real_ device.
69 static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
70 size_t *retlen, u_char *buf)
72 struct mtd_part *part = mtd_to_part(mtd);
73 struct mtd_ecc_stats stats;
74 int res;
76 stats = part->parent->ecc_stats;
77 res = part->parent->_read(part->parent, from + part->offset, len,
78 retlen, buf);
79 if (unlikely(mtd_is_eccerr(res)))
80 mtd->ecc_stats.failed +=
81 part->parent->ecc_stats.failed - stats.failed;
82 else
83 mtd->ecc_stats.corrected +=
84 part->parent->ecc_stats.corrected - stats.corrected;
85 return res;
88 static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
89 size_t *retlen, void **virt, resource_size_t *phys)
91 struct mtd_part *part = mtd_to_part(mtd);
93 return part->parent->_point(part->parent, from + part->offset, len,
94 retlen, virt, phys);
97 static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
99 struct mtd_part *part = mtd_to_part(mtd);
101 return part->parent->_unpoint(part->parent, from + part->offset, len);
104 static int part_read_oob(struct mtd_info *mtd, loff_t from,
105 struct mtd_oob_ops *ops)
107 struct mtd_part *part = mtd_to_part(mtd);
108 struct mtd_ecc_stats stats;
109 int res;
111 stats = part->parent->ecc_stats;
112 res = part->parent->_read_oob(part->parent, from + part->offset, ops);
113 if (unlikely(mtd_is_eccerr(res)))
114 mtd->ecc_stats.failed +=
115 part->parent->ecc_stats.failed - stats.failed;
116 else
117 mtd->ecc_stats.corrected +=
118 part->parent->ecc_stats.corrected - stats.corrected;
119 return res;
122 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
123 size_t len, size_t *retlen, u_char *buf)
125 struct mtd_part *part = mtd_to_part(mtd);
126 return part->parent->_read_user_prot_reg(part->parent, from, len,
127 retlen, buf);
130 static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
131 size_t *retlen, struct otp_info *buf)
133 struct mtd_part *part = mtd_to_part(mtd);
134 return part->parent->_get_user_prot_info(part->parent, len, retlen,
135 buf);
138 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
139 size_t len, size_t *retlen, u_char *buf)
141 struct mtd_part *part = mtd_to_part(mtd);
142 return part->parent->_read_fact_prot_reg(part->parent, from, len,
143 retlen, buf);
146 static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
147 size_t *retlen, struct otp_info *buf)
149 struct mtd_part *part = mtd_to_part(mtd);
150 return part->parent->_get_fact_prot_info(part->parent, len, retlen,
151 buf);
154 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
155 size_t *retlen, const u_char *buf)
157 struct mtd_part *part = mtd_to_part(mtd);
158 return part->parent->_write(part->parent, to + part->offset, len,
159 retlen, buf);
162 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
163 size_t *retlen, const u_char *buf)
165 struct mtd_part *part = mtd_to_part(mtd);
166 return part->parent->_panic_write(part->parent, to + part->offset, len,
167 retlen, buf);
170 static int part_write_oob(struct mtd_info *mtd, loff_t to,
171 struct mtd_oob_ops *ops)
173 struct mtd_part *part = mtd_to_part(mtd);
175 return part->parent->_write_oob(part->parent, to + part->offset, ops);
178 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
179 size_t len, size_t *retlen, u_char *buf)
181 struct mtd_part *part = mtd_to_part(mtd);
182 return part->parent->_write_user_prot_reg(part->parent, from, len,
183 retlen, buf);
186 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
187 size_t len)
189 struct mtd_part *part = mtd_to_part(mtd);
190 return part->parent->_lock_user_prot_reg(part->parent, from, len);
193 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
194 unsigned long count, loff_t to, size_t *retlen)
196 struct mtd_part *part = mtd_to_part(mtd);
197 return part->parent->_writev(part->parent, vecs, count,
198 to + part->offset, retlen);
201 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
203 struct mtd_part *part = mtd_to_part(mtd);
204 int ret;
206 instr->addr += part->offset;
207 ret = part->parent->_erase(part->parent, instr);
208 if (ret) {
209 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
210 instr->fail_addr -= part->offset;
211 instr->addr -= part->offset;
213 return ret;
216 void mtd_erase_callback(struct erase_info *instr)
218 if (instr->mtd->_erase == part_erase) {
219 struct mtd_part *part = mtd_to_part(instr->mtd);
221 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
222 instr->fail_addr -= part->offset;
223 instr->addr -= part->offset;
225 if (instr->callback)
226 instr->callback(instr);
228 EXPORT_SYMBOL_GPL(mtd_erase_callback);
230 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
232 struct mtd_part *part = mtd_to_part(mtd);
233 return part->parent->_lock(part->parent, ofs + part->offset, len);
236 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
238 struct mtd_part *part = mtd_to_part(mtd);
239 return part->parent->_unlock(part->parent, ofs + part->offset, len);
242 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
244 struct mtd_part *part = mtd_to_part(mtd);
245 return part->parent->_is_locked(part->parent, ofs + part->offset, len);
248 static void part_sync(struct mtd_info *mtd)
250 struct mtd_part *part = mtd_to_part(mtd);
251 part->parent->_sync(part->parent);
254 static int part_suspend(struct mtd_info *mtd)
256 struct mtd_part *part = mtd_to_part(mtd);
257 return part->parent->_suspend(part->parent);
260 static void part_resume(struct mtd_info *mtd)
262 struct mtd_part *part = mtd_to_part(mtd);
263 part->parent->_resume(part->parent);
266 static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
268 struct mtd_part *part = mtd_to_part(mtd);
269 ofs += part->offset;
270 return part->parent->_block_isreserved(part->parent, ofs);
273 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
275 struct mtd_part *part = mtd_to_part(mtd);
276 ofs += part->offset;
277 return part->parent->_block_isbad(part->parent, ofs);
280 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
282 struct mtd_part *part = mtd_to_part(mtd);
283 int res;
285 ofs += part->offset;
286 res = part->parent->_block_markbad(part->parent, ofs);
287 if (!res)
288 mtd->ecc_stats.badblocks++;
289 return res;
292 static int part_get_device(struct mtd_info *mtd)
294 struct mtd_part *part = mtd_to_part(mtd);
295 return part->parent->_get_device(part->parent);
298 static void part_put_device(struct mtd_info *mtd)
300 struct mtd_part *part = mtd_to_part(mtd);
301 part->parent->_put_device(part->parent);
304 static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
305 struct mtd_oob_region *oobregion)
307 struct mtd_part *part = mtd_to_part(mtd);
309 return mtd_ooblayout_ecc(part->parent, section, oobregion);
312 static int part_ooblayout_free(struct mtd_info *mtd, int section,
313 struct mtd_oob_region *oobregion)
315 struct mtd_part *part = mtd_to_part(mtd);
317 return mtd_ooblayout_free(part->parent, section, oobregion);
320 static const struct mtd_ooblayout_ops part_ooblayout_ops = {
321 .ecc = part_ooblayout_ecc,
322 .free = part_ooblayout_free,
325 static int part_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
327 struct mtd_part *part = mtd_to_part(mtd);
329 return part->parent->_max_bad_blocks(part->parent,
330 ofs + part->offset, len);
333 static inline void free_partition(struct mtd_part *p)
335 kfree(p->mtd.name);
336 kfree(p);
340 * mtd_parse_part - parse MTD partition looking for subpartitions
342 * @slave: part that is supposed to be a container and should be parsed
343 * @types: NULL-terminated array with names of partition parsers to try
345 * Some partitions are kind of containers with extra subpartitions (volumes).
346 * There can be various formats of such containers. This function tries to use
347 * specified parsers to analyze given partition and registers found
348 * subpartitions on success.
350 static int mtd_parse_part(struct mtd_part *slave, const char *const *types)
352 struct mtd_partitions parsed;
353 int err;
355 err = parse_mtd_partitions(&slave->mtd, types, &parsed, NULL);
356 if (err)
357 return err;
358 else if (!parsed.nr_parts)
359 return -ENOENT;
361 err = add_mtd_partitions(&slave->mtd, parsed.parts, parsed.nr_parts);
363 mtd_part_parser_cleanup(&parsed);
365 return err;
368 static struct mtd_part *allocate_partition(struct mtd_info *parent,
369 const struct mtd_partition *part, int partno,
370 uint64_t cur_offset)
372 int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize :
373 parent->erasesize;
374 struct mtd_part *slave;
375 u32 remainder;
376 char *name;
377 u64 tmp;
379 /* allocate the partition structure */
380 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
381 name = kstrdup(part->name, GFP_KERNEL);
382 if (!name || !slave) {
383 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
384 parent->name);
385 kfree(name);
386 kfree(slave);
387 return ERR_PTR(-ENOMEM);
390 /* set up the MTD object for this partition */
391 slave->mtd.type = parent->type;
392 slave->mtd.flags = parent->flags & ~part->mask_flags;
393 slave->mtd.size = part->size;
394 slave->mtd.writesize = parent->writesize;
395 slave->mtd.writebufsize = parent->writebufsize;
396 slave->mtd.oobsize = parent->oobsize;
397 slave->mtd.oobavail = parent->oobavail;
398 slave->mtd.subpage_sft = parent->subpage_sft;
399 slave->mtd.pairing = parent->pairing;
401 slave->mtd.name = name;
402 slave->mtd.owner = parent->owner;
404 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
405 * concern for showing the same data in multiple partitions.
406 * However, it is very useful to have the master node present,
407 * so the MTD_PARTITIONED_MASTER option allows that. The master
408 * will have device nodes etc only if this is set, so make the
409 * parent conditional on that option. Note, this is a way to
410 * distinguish between the master and the partition in sysfs.
412 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
413 &parent->dev :
414 parent->dev.parent;
415 slave->mtd.dev.of_node = part->of_node;
417 if (parent->_read)
418 slave->mtd._read = part_read;
419 if (parent->_write)
420 slave->mtd._write = part_write;
422 if (parent->_panic_write)
423 slave->mtd._panic_write = part_panic_write;
425 if (parent->_point && parent->_unpoint) {
426 slave->mtd._point = part_point;
427 slave->mtd._unpoint = part_unpoint;
430 if (parent->_read_oob)
431 slave->mtd._read_oob = part_read_oob;
432 if (parent->_write_oob)
433 slave->mtd._write_oob = part_write_oob;
434 if (parent->_read_user_prot_reg)
435 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
436 if (parent->_read_fact_prot_reg)
437 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
438 if (parent->_write_user_prot_reg)
439 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
440 if (parent->_lock_user_prot_reg)
441 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
442 if (parent->_get_user_prot_info)
443 slave->mtd._get_user_prot_info = part_get_user_prot_info;
444 if (parent->_get_fact_prot_info)
445 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
446 if (parent->_sync)
447 slave->mtd._sync = part_sync;
448 if (!partno && !parent->dev.class && parent->_suspend &&
449 parent->_resume) {
450 slave->mtd._suspend = part_suspend;
451 slave->mtd._resume = part_resume;
453 if (parent->_writev)
454 slave->mtd._writev = part_writev;
455 if (parent->_lock)
456 slave->mtd._lock = part_lock;
457 if (parent->_unlock)
458 slave->mtd._unlock = part_unlock;
459 if (parent->_is_locked)
460 slave->mtd._is_locked = part_is_locked;
461 if (parent->_block_isreserved)
462 slave->mtd._block_isreserved = part_block_isreserved;
463 if (parent->_block_isbad)
464 slave->mtd._block_isbad = part_block_isbad;
465 if (parent->_block_markbad)
466 slave->mtd._block_markbad = part_block_markbad;
467 if (parent->_max_bad_blocks)
468 slave->mtd._max_bad_blocks = part_max_bad_blocks;
470 if (parent->_get_device)
471 slave->mtd._get_device = part_get_device;
472 if (parent->_put_device)
473 slave->mtd._put_device = part_put_device;
475 slave->mtd._erase = part_erase;
476 slave->parent = parent;
477 slave->offset = part->offset;
479 if (slave->offset == MTDPART_OFS_APPEND)
480 slave->offset = cur_offset;
481 if (slave->offset == MTDPART_OFS_NXTBLK) {
482 tmp = cur_offset;
483 slave->offset = cur_offset;
484 remainder = do_div(tmp, wr_alignment);
485 if (remainder) {
486 slave->offset += wr_alignment - remainder;
487 printk(KERN_NOTICE "Moving partition %d: "
488 "0x%012llx -> 0x%012llx\n", partno,
489 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
492 if (slave->offset == MTDPART_OFS_RETAIN) {
493 slave->offset = cur_offset;
494 if (parent->size - slave->offset >= slave->mtd.size) {
495 slave->mtd.size = parent->size - slave->offset
496 - slave->mtd.size;
497 } else {
498 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
499 part->name, parent->size - slave->offset,
500 slave->mtd.size);
501 /* register to preserve ordering */
502 goto out_register;
505 if (slave->mtd.size == MTDPART_SIZ_FULL)
506 slave->mtd.size = parent->size - slave->offset;
508 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
509 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
511 /* let's do some sanity checks */
512 if (slave->offset >= parent->size) {
513 /* let's register it anyway to preserve ordering */
514 slave->offset = 0;
515 slave->mtd.size = 0;
516 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
517 part->name);
518 goto out_register;
520 if (slave->offset + slave->mtd.size > parent->size) {
521 slave->mtd.size = parent->size - slave->offset;
522 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
523 part->name, parent->name, (unsigned long long)slave->mtd.size);
525 if (parent->numeraseregions > 1) {
526 /* Deal with variable erase size stuff */
527 int i, max = parent->numeraseregions;
528 u64 end = slave->offset + slave->mtd.size;
529 struct mtd_erase_region_info *regions = parent->eraseregions;
531 /* Find the first erase regions which is part of this
532 * partition. */
533 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
535 /* The loop searched for the region _behind_ the first one */
536 if (i > 0)
537 i--;
539 /* Pick biggest erasesize */
540 for (; i < max && regions[i].offset < end; i++) {
541 if (slave->mtd.erasesize < regions[i].erasesize) {
542 slave->mtd.erasesize = regions[i].erasesize;
545 BUG_ON(slave->mtd.erasesize == 0);
546 } else {
547 /* Single erase size */
548 slave->mtd.erasesize = parent->erasesize;
552 * Slave erasesize might differ from the master one if the master
553 * exposes several regions with different erasesize. Adjust
554 * wr_alignment accordingly.
556 if (!(slave->mtd.flags & MTD_NO_ERASE))
557 wr_alignment = slave->mtd.erasesize;
559 tmp = slave->offset;
560 remainder = do_div(tmp, wr_alignment);
561 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
562 /* Doesn't start on a boundary of major erase size */
563 /* FIXME: Let it be writable if it is on a boundary of
564 * _minor_ erase size though */
565 slave->mtd.flags &= ~MTD_WRITEABLE;
566 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
567 part->name);
570 tmp = slave->mtd.size;
571 remainder = do_div(tmp, wr_alignment);
572 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
573 slave->mtd.flags &= ~MTD_WRITEABLE;
574 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
575 part->name);
578 mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
579 slave->mtd.ecc_step_size = parent->ecc_step_size;
580 slave->mtd.ecc_strength = parent->ecc_strength;
581 slave->mtd.bitflip_threshold = parent->bitflip_threshold;
583 if (parent->_block_isbad) {
584 uint64_t offs = 0;
586 while (offs < slave->mtd.size) {
587 if (mtd_block_isreserved(parent, offs + slave->offset))
588 slave->mtd.ecc_stats.bbtblocks++;
589 else if (mtd_block_isbad(parent, offs + slave->offset))
590 slave->mtd.ecc_stats.badblocks++;
591 offs += slave->mtd.erasesize;
595 out_register:
596 return slave;
599 static ssize_t mtd_partition_offset_show(struct device *dev,
600 struct device_attribute *attr, char *buf)
602 struct mtd_info *mtd = dev_get_drvdata(dev);
603 struct mtd_part *part = mtd_to_part(mtd);
604 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
607 static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
609 static const struct attribute *mtd_partition_attrs[] = {
610 &dev_attr_offset.attr,
611 NULL
614 static int mtd_add_partition_attrs(struct mtd_part *new)
616 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
617 if (ret)
618 printk(KERN_WARNING
619 "mtd: failed to create partition attrs, err=%d\n", ret);
620 return ret;
623 int mtd_add_partition(struct mtd_info *parent, const char *name,
624 long long offset, long long length)
626 struct mtd_partition part;
627 struct mtd_part *new;
628 int ret = 0;
630 /* the direct offset is expected */
631 if (offset == MTDPART_OFS_APPEND ||
632 offset == MTDPART_OFS_NXTBLK)
633 return -EINVAL;
635 if (length == MTDPART_SIZ_FULL)
636 length = parent->size - offset;
638 if (length <= 0)
639 return -EINVAL;
641 memset(&part, 0, sizeof(part));
642 part.name = name;
643 part.size = length;
644 part.offset = offset;
646 new = allocate_partition(parent, &part, -1, offset);
647 if (IS_ERR(new))
648 return PTR_ERR(new);
650 mutex_lock(&mtd_partitions_mutex);
651 list_add(&new->list, &mtd_partitions);
652 mutex_unlock(&mtd_partitions_mutex);
654 add_mtd_device(&new->mtd);
656 mtd_add_partition_attrs(new);
658 return ret;
660 EXPORT_SYMBOL_GPL(mtd_add_partition);
663 * __mtd_del_partition - delete MTD partition
665 * @priv: internal MTD struct for partition to be deleted
667 * This function must be called with the partitions mutex locked.
669 static int __mtd_del_partition(struct mtd_part *priv)
671 struct mtd_part *child, *next;
672 int err;
674 list_for_each_entry_safe(child, next, &mtd_partitions, list) {
675 if (child->parent == &priv->mtd) {
676 err = __mtd_del_partition(child);
677 if (err)
678 return err;
682 sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs);
684 err = del_mtd_device(&priv->mtd);
685 if (err)
686 return err;
688 list_del(&priv->list);
689 free_partition(priv);
691 return 0;
695 * This function unregisters and destroy all slave MTD objects which are
696 * attached to the given MTD object.
698 int del_mtd_partitions(struct mtd_info *mtd)
700 struct mtd_part *slave, *next;
701 int ret, err = 0;
703 mutex_lock(&mtd_partitions_mutex);
704 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
705 if (slave->parent == mtd) {
706 ret = __mtd_del_partition(slave);
707 if (ret < 0)
708 err = ret;
710 mutex_unlock(&mtd_partitions_mutex);
712 return err;
715 int mtd_del_partition(struct mtd_info *mtd, int partno)
717 struct mtd_part *slave, *next;
718 int ret = -EINVAL;
720 mutex_lock(&mtd_partitions_mutex);
721 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
722 if ((slave->parent == mtd) &&
723 (slave->mtd.index == partno)) {
724 ret = __mtd_del_partition(slave);
725 break;
727 mutex_unlock(&mtd_partitions_mutex);
729 return ret;
731 EXPORT_SYMBOL_GPL(mtd_del_partition);
734 * This function, given a master MTD object and a partition table, creates
735 * and registers slave MTD objects which are bound to the master according to
736 * the partition definitions.
738 * For historical reasons, this function's caller only registers the master
739 * if the MTD_PARTITIONED_MASTER config option is set.
742 int add_mtd_partitions(struct mtd_info *master,
743 const struct mtd_partition *parts,
744 int nbparts)
746 struct mtd_part *slave;
747 uint64_t cur_offset = 0;
748 int i;
750 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
752 for (i = 0; i < nbparts; i++) {
753 slave = allocate_partition(master, parts + i, i, cur_offset);
754 if (IS_ERR(slave)) {
755 del_mtd_partitions(master);
756 return PTR_ERR(slave);
759 mutex_lock(&mtd_partitions_mutex);
760 list_add(&slave->list, &mtd_partitions);
761 mutex_unlock(&mtd_partitions_mutex);
763 add_mtd_device(&slave->mtd);
764 mtd_add_partition_attrs(slave);
765 if (parts[i].types)
766 mtd_parse_part(slave, parts[i].types);
768 cur_offset = slave->offset + slave->mtd.size;
771 return 0;
774 static DEFINE_SPINLOCK(part_parser_lock);
775 static LIST_HEAD(part_parsers);
777 static struct mtd_part_parser *mtd_part_parser_get(const char *name)
779 struct mtd_part_parser *p, *ret = NULL;
781 spin_lock(&part_parser_lock);
783 list_for_each_entry(p, &part_parsers, list)
784 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
785 ret = p;
786 break;
789 spin_unlock(&part_parser_lock);
791 return ret;
794 static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
796 module_put(p->owner);
800 * Many partition parsers just expected the core to kfree() all their data in
801 * one chunk. Do that by default.
803 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
804 int nr_parts)
806 kfree(pparts);
809 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
811 p->owner = owner;
813 if (!p->cleanup)
814 p->cleanup = &mtd_part_parser_cleanup_default;
816 spin_lock(&part_parser_lock);
817 list_add(&p->list, &part_parsers);
818 spin_unlock(&part_parser_lock);
820 return 0;
822 EXPORT_SYMBOL_GPL(__register_mtd_parser);
824 void deregister_mtd_parser(struct mtd_part_parser *p)
826 spin_lock(&part_parser_lock);
827 list_del(&p->list);
828 spin_unlock(&part_parser_lock);
830 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
833 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
834 * are changing this array!
836 static const char * const default_mtd_part_types[] = {
837 "cmdlinepart",
838 "ofpart",
839 NULL
842 static int mtd_part_do_parse(struct mtd_part_parser *parser,
843 struct mtd_info *master,
844 struct mtd_partitions *pparts,
845 struct mtd_part_parser_data *data)
847 int ret;
849 ret = (*parser->parse_fn)(master, &pparts->parts, data);
850 pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
851 if (ret <= 0)
852 return ret;
854 pr_notice("%d %s partitions found on MTD device %s\n", ret,
855 parser->name, master->name);
857 pparts->nr_parts = ret;
858 pparts->parser = parser;
860 return ret;
864 * parse_mtd_partitions - parse MTD partitions
865 * @master: the master partition (describes whole MTD device)
866 * @types: names of partition parsers to try or %NULL
867 * @pparts: info about partitions found is returned here
868 * @data: MTD partition parser-specific data
870 * This function tries to find partition on MTD device @master. It uses MTD
871 * partition parsers, specified in @types. However, if @types is %NULL, then
872 * the default list of parsers is used. The default list contains only the
873 * "cmdlinepart" and "ofpart" parsers ATM.
874 * Note: If there are more then one parser in @types, the kernel only takes the
875 * partitions parsed out by the first parser.
877 * This function may return:
878 * o a negative error code in case of failure
879 * o zero otherwise, and @pparts will describe the partitions, number of
880 * partitions, and the parser which parsed them. Caller must release
881 * resources with mtd_part_parser_cleanup() when finished with the returned
882 * data.
884 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
885 struct mtd_partitions *pparts,
886 struct mtd_part_parser_data *data)
888 struct mtd_part_parser *parser;
889 int ret, err = 0;
891 if (!types)
892 types = default_mtd_part_types;
894 for ( ; *types; types++) {
895 pr_debug("%s: parsing partitions %s\n", master->name, *types);
896 parser = mtd_part_parser_get(*types);
897 if (!parser && !request_module("%s", *types))
898 parser = mtd_part_parser_get(*types);
899 pr_debug("%s: got parser %s\n", master->name,
900 parser ? parser->name : NULL);
901 if (!parser)
902 continue;
903 ret = mtd_part_do_parse(parser, master, pparts, data);
904 /* Found partitions! */
905 if (ret > 0)
906 return 0;
907 mtd_part_parser_put(parser);
909 * Stash the first error we see; only report it if no parser
910 * succeeds
912 if (ret < 0 && !err)
913 err = ret;
915 return err;
918 void mtd_part_parser_cleanup(struct mtd_partitions *parts)
920 const struct mtd_part_parser *parser;
922 if (!parts)
923 return;
925 parser = parts->parser;
926 if (parser) {
927 if (parser->cleanup)
928 parser->cleanup(parts->parts, parts->nr_parts);
930 mtd_part_parser_put(parser);
934 int mtd_is_partition(const struct mtd_info *mtd)
936 struct mtd_part *part;
937 int ispart = 0;
939 mutex_lock(&mtd_partitions_mutex);
940 list_for_each_entry(part, &mtd_partitions, list)
941 if (&part->mtd == mtd) {
942 ispart = 1;
943 break;
945 mutex_unlock(&mtd_partitions_mutex);
947 return ispart;
949 EXPORT_SYMBOL_GPL(mtd_is_partition);
951 /* Returns the size of the entire flash chip */
952 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
954 if (!mtd_is_partition(mtd))
955 return mtd->size;
957 return mtd_get_device_size(mtd_to_part(mtd)->parent);
959 EXPORT_SYMBOL_GPL(mtd_get_device_size);