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>
36 /* Our partition linked list */
37 static LIST_HEAD(mtd_partitions
);
38 static DEFINE_MUTEX(mtd_partitions_mutex
);
40 /* Our partition node structure */
43 struct mtd_info
*master
;
45 struct list_head list
;
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.
52 #define PART(x) ((struct mtd_part *)(x))
56 * MTD methods which simply translate the effective address and pass through
57 * to the _real_ device.
60 static int part_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
61 size_t *retlen
, u_char
*buf
)
63 struct mtd_part
*part
= PART(mtd
);
64 struct mtd_ecc_stats stats
;
67 stats
= part
->master
->ecc_stats
;
69 if (from
>= mtd
->size
)
71 else if (from
+ len
> mtd
->size
)
72 len
= mtd
->size
- from
;
73 res
= mtd_read(part
->master
, from
+ part
->offset
, len
, retlen
, buf
);
75 if (mtd_is_bitflip(res
))
76 mtd
->ecc_stats
.corrected
+= part
->master
->ecc_stats
.corrected
- stats
.corrected
;
77 if (mtd_is_eccerr(res
))
78 mtd
->ecc_stats
.failed
+= part
->master
->ecc_stats
.failed
- stats
.failed
;
83 static int part_point(struct mtd_info
*mtd
, loff_t from
, size_t len
,
84 size_t *retlen
, void **virt
, resource_size_t
*phys
)
86 struct mtd_part
*part
= PART(mtd
);
87 if (from
>= mtd
->size
)
89 else if (from
+ len
> mtd
->size
)
90 len
= mtd
->size
- from
;
91 return mtd_point(part
->master
, from
+ part
->offset
, len
, retlen
,
95 static void part_unpoint(struct mtd_info
*mtd
, loff_t from
, size_t len
)
97 struct mtd_part
*part
= PART(mtd
);
99 mtd_unpoint(part
->master
, from
+ part
->offset
, len
);
102 static unsigned long part_get_unmapped_area(struct mtd_info
*mtd
,
104 unsigned long offset
,
107 struct mtd_part
*part
= PART(mtd
);
109 offset
+= part
->offset
;
110 return mtd_get_unmapped_area(part
->master
, len
, offset
, flags
);
113 static int part_read_oob(struct mtd_info
*mtd
, loff_t from
,
114 struct mtd_oob_ops
*ops
)
116 struct mtd_part
*part
= PART(mtd
);
119 if (from
>= mtd
->size
)
121 if (ops
->datbuf
&& from
+ ops
->len
> mtd
->size
)
125 * If OOB is also requested, make sure that we do not read past the end
131 if (ops
->mode
== MTD_OPS_AUTO_OOB
)
135 pages
= mtd_div_by_ws(mtd
->size
, mtd
);
136 pages
-= mtd_div_by_ws(from
, mtd
);
137 if (ops
->ooboffs
+ ops
->ooblen
> pages
* len
)
141 res
= mtd_read_oob(part
->master
, from
+ part
->offset
, ops
);
143 if (mtd_is_bitflip(res
))
144 mtd
->ecc_stats
.corrected
++;
145 if (mtd_is_eccerr(res
))
146 mtd
->ecc_stats
.failed
++;
151 static int part_read_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
152 size_t len
, size_t *retlen
, u_char
*buf
)
154 struct mtd_part
*part
= PART(mtd
);
155 return mtd_read_user_prot_reg(part
->master
, from
, len
, retlen
, buf
);
158 static int part_get_user_prot_info(struct mtd_info
*mtd
,
159 struct otp_info
*buf
, size_t len
)
161 struct mtd_part
*part
= PART(mtd
);
162 return mtd_get_user_prot_info(part
->master
, buf
, len
);
165 static int part_read_fact_prot_reg(struct mtd_info
*mtd
, loff_t from
,
166 size_t len
, size_t *retlen
, u_char
*buf
)
168 struct mtd_part
*part
= PART(mtd
);
169 return mtd_read_fact_prot_reg(part
->master
, from
, len
, retlen
, buf
);
172 static int part_get_fact_prot_info(struct mtd_info
*mtd
, struct otp_info
*buf
,
175 struct mtd_part
*part
= PART(mtd
);
176 return mtd_get_fact_prot_info(part
->master
, buf
, len
);
179 static int part_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
180 size_t *retlen
, const u_char
*buf
)
182 struct mtd_part
*part
= PART(mtd
);
183 if (!(mtd
->flags
& MTD_WRITEABLE
))
187 else if (to
+ len
> mtd
->size
)
188 len
= mtd
->size
- to
;
189 return mtd_write(part
->master
, to
+ part
->offset
, len
, retlen
, buf
);
192 static int part_panic_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
193 size_t *retlen
, const u_char
*buf
)
195 struct mtd_part
*part
= PART(mtd
);
196 if (!(mtd
->flags
& MTD_WRITEABLE
))
200 else if (to
+ len
> mtd
->size
)
201 len
= mtd
->size
- to
;
202 return mtd_panic_write(part
->master
, to
+ part
->offset
, len
, retlen
,
206 static int part_write_oob(struct mtd_info
*mtd
, loff_t to
,
207 struct mtd_oob_ops
*ops
)
209 struct mtd_part
*part
= PART(mtd
);
211 if (!(mtd
->flags
& MTD_WRITEABLE
))
216 if (ops
->datbuf
&& to
+ ops
->len
> mtd
->size
)
218 return mtd_write_oob(part
->master
, to
+ part
->offset
, ops
);
221 static int part_write_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
222 size_t len
, size_t *retlen
, u_char
*buf
)
224 struct mtd_part
*part
= PART(mtd
);
225 return mtd_write_user_prot_reg(part
->master
, from
, len
, retlen
, buf
);
228 static int part_lock_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
231 struct mtd_part
*part
= PART(mtd
);
232 return mtd_lock_user_prot_reg(part
->master
, from
, len
);
235 static int part_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
236 unsigned long count
, loff_t to
, size_t *retlen
)
238 struct mtd_part
*part
= PART(mtd
);
239 if (!(mtd
->flags
& MTD_WRITEABLE
))
241 return mtd_writev(part
->master
, vecs
, count
, to
+ part
->offset
,
245 static int part_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
247 struct mtd_part
*part
= PART(mtd
);
249 if (!(mtd
->flags
& MTD_WRITEABLE
))
251 if (instr
->addr
>= mtd
->size
)
253 instr
->addr
+= part
->offset
;
254 ret
= mtd_erase(part
->master
, instr
);
256 if (instr
->fail_addr
!= MTD_FAIL_ADDR_UNKNOWN
)
257 instr
->fail_addr
-= part
->offset
;
258 instr
->addr
-= part
->offset
;
263 void mtd_erase_callback(struct erase_info
*instr
)
265 if (instr
->mtd
->erase
== part_erase
) {
266 struct mtd_part
*part
= PART(instr
->mtd
);
268 if (instr
->fail_addr
!= MTD_FAIL_ADDR_UNKNOWN
)
269 instr
->fail_addr
-= part
->offset
;
270 instr
->addr
-= part
->offset
;
273 instr
->callback(instr
);
275 EXPORT_SYMBOL_GPL(mtd_erase_callback
);
277 static int part_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
279 struct mtd_part
*part
= PART(mtd
);
280 if ((len
+ ofs
) > mtd
->size
)
282 return mtd_lock(part
->master
, ofs
+ part
->offset
, len
);
285 static int part_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
287 struct mtd_part
*part
= PART(mtd
);
288 if ((len
+ ofs
) > mtd
->size
)
290 return mtd_unlock(part
->master
, ofs
+ part
->offset
, len
);
293 static int part_is_locked(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
295 struct mtd_part
*part
= PART(mtd
);
296 if ((len
+ ofs
) > mtd
->size
)
298 return mtd_is_locked(part
->master
, ofs
+ part
->offset
, len
);
301 static void part_sync(struct mtd_info
*mtd
)
303 struct mtd_part
*part
= PART(mtd
);
304 mtd_sync(part
->master
);
307 static int part_suspend(struct mtd_info
*mtd
)
309 struct mtd_part
*part
= PART(mtd
);
310 return mtd_suspend(part
->master
);
313 static void part_resume(struct mtd_info
*mtd
)
315 struct mtd_part
*part
= PART(mtd
);
316 mtd_resume(part
->master
);
319 static int part_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
321 struct mtd_part
*part
= PART(mtd
);
322 if (ofs
>= mtd
->size
)
325 return mtd_block_isbad(part
->master
, ofs
);
328 static int part_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
330 struct mtd_part
*part
= PART(mtd
);
333 if (!(mtd
->flags
& MTD_WRITEABLE
))
335 if (ofs
>= mtd
->size
)
338 res
= mtd_block_markbad(part
->master
, ofs
);
340 mtd
->ecc_stats
.badblocks
++;
344 static inline void free_partition(struct mtd_part
*p
)
351 * This function unregisters and destroy all slave MTD objects which are
352 * attached to the given master MTD object.
355 int del_mtd_partitions(struct mtd_info
*master
)
357 struct mtd_part
*slave
, *next
;
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
);
368 list_del(&slave
->list
);
369 free_partition(slave
);
371 mutex_unlock(&mtd_partitions_mutex
);
376 static struct mtd_part
*allocate_partition(struct mtd_info
*master
,
377 const struct mtd_partition
*part
, int partno
,
380 struct mtd_part
*slave
;
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",
391 return ERR_PTR(-ENOMEM);
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;
404 slave->mtd.name = name;
405 slave->mtd.owner = master->owner;
406 slave->mtd.backing_dev_info = master->backing_dev_info;
408 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
409 * to have the same data be in two different partitions.
411 slave->mtd.dev.parent = master->dev.parent;
413 slave->mtd.read = part_read;
414 slave->mtd.write = part_write;
416 if (master->panic_write)
417 slave->mtd.panic_write = part_panic_write;
419 if (master->point && master->unpoint) {
420 slave->mtd.point = part_point;
421 slave->mtd.unpoint = part_unpoint;
424 if (master->get_unmapped_area)
425 slave->mtd.get_unmapped_area = part_get_unmapped_area;
426 if (master->read_oob)
427 slave->mtd.read_oob = part_read_oob;
428 if (master->write_oob)
429 slave->mtd.write_oob = part_write_oob;
430 if (master->read_user_prot_reg)
431 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
432 if (master->read_fact_prot_reg)
433 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
434 if (master->write_user_prot_reg)
435 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
436 if (master->lock_user_prot_reg)
437 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
438 if (master->get_user_prot_info)
439 slave->mtd.get_user_prot_info = part_get_user_prot_info;
440 if (master->get_fact_prot_info)
441 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
443 slave->mtd.sync = part_sync;
444 if (!partno && !master->dev.class && master->suspend && master->resume) {
445 slave->mtd.suspend = part_suspend;
446 slave->mtd.resume = part_resume;
449 slave->mtd.writev = part_writev;
451 slave->mtd.lock = part_lock;
453 slave->mtd.unlock = part_unlock;
454 if (master->is_locked)
455 slave->mtd.is_locked = part_is_locked;
456 if (master->block_isbad)
457 slave->mtd.block_isbad = part_block_isbad;
458 if (master->block_markbad)
459 slave->mtd.block_markbad = part_block_markbad;
460 slave->mtd.erase = part_erase;
461 slave->master = master;
462 slave->offset = part->offset;
464 if (slave->offset == MTDPART_OFS_APPEND)
465 slave->offset = cur_offset;
466 if (slave->offset == MTDPART_OFS_NXTBLK) {
467 slave->offset = cur_offset;
468 if (mtd_mod_by_eb(cur_offset, master) != 0) {
469 /* Round up to next erasesize */
470 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
471 printk(KERN_NOTICE "Moving partition
%d
: "
472 "0x
%012llx
-> 0x
%012llx
\n", partno,
473 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
476 if (slave->offset == MTDPART_OFS_RETAIN) {
477 slave->offset = cur_offset;
478 if (master->size - slave->offset >= slave->mtd.size) {
479 slave->mtd.size = master->size - slave->offset
482 printk(KERN_ERR "mtd partition
\"%s
\" doesn
't have enough space: %#llx < %#llx, disabled\n",
483 part->name, master->size - slave->offset,
485 /* register to preserve ordering */
489 if (slave->mtd.size == MTDPART_SIZ_FULL)
490 slave->mtd.size = master->size - slave->offset;
492 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
493 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
495 /* let's
do some sanity checks */
496 if (slave
->offset
>= master
->size
) {
497 /* let's register it anyway to preserve ordering */
500 printk(KERN_ERR
"mtd: partition \"%s
\" is out of reach
-- disabled
\n",
504 if (slave->offset + slave->mtd.size > master->size) {
505 slave->mtd.size = master->size - slave->offset;
506 printk(KERN_WARNING"mtd
: partition
\"%s
\" extends beyond the end of device
\"%s
\" -- size truncated to
%#llx\n",
507 part->name, master->name, (unsigned long long)slave->mtd.size);
509 if (master->numeraseregions > 1) {
510 /* Deal with variable erase size stuff */
511 int i, max = master->numeraseregions;
512 u64 end = slave->offset + slave->mtd.size;
513 struct mtd_erase_region_info *regions = master->eraseregions;
515 /* Find the first erase regions which is part of this
517 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
519 /* The loop searched for the region _behind_ the first one */
523 /* Pick biggest erasesize */
524 for (; i < max && regions[i].offset < end; i++) {
525 if (slave->mtd.erasesize < regions[i].erasesize) {
526 slave->mtd.erasesize = regions[i].erasesize;
529 BUG_ON(slave->mtd.erasesize == 0);
531 /* Single erase size */
532 slave->mtd.erasesize = master->erasesize;
535 if ((slave->mtd.flags & MTD_WRITEABLE) &&
536 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
537 /* Doesn't start on a boundary of major erase size */
538 /* FIXME: Let it be writable if it is on a boundary of
539 * _minor_ erase size though */
540 slave->mtd.flags &= ~MTD_WRITEABLE;
541 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
544 if ((slave->mtd.flags & MTD_WRITEABLE) &&
545 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
546 slave->mtd.flags &= ~MTD_WRITEABLE;
547 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
551 slave->mtd.ecclayout = master->ecclayout;
552 if (master->block_isbad) {
555 while (offs < slave->mtd.size) {
556 if (mtd_block_isbad(master, offs + slave->offset))
557 slave->mtd.ecc_stats.badblocks++;
558 offs += slave->mtd.erasesize;
566 int mtd_add_partition(struct mtd_info *master, char *name,
567 long long offset, long long length)
569 struct mtd_partition part;
570 struct mtd_part *p, *new;
574 /* the direct offset is expected */
575 if (offset == MTDPART_OFS_APPEND ||
576 offset == MTDPART_OFS_NXTBLK)
579 if (length == MTDPART_SIZ_FULL)
580 length = master->size - offset;
587 part.offset = offset;
589 part.ecclayout = NULL;
591 new = allocate_partition(master, &part, -1, offset);
596 end = offset + length;
598 mutex_lock(&mtd_partitions_mutex);
599 list_for_each_entry(p, &mtd_partitions, list)
600 if (p->master == master) {
601 if ((start >= p->offset) &&
602 (start < (p->offset + p->mtd.size)))
605 if ((end >= p->offset) &&
606 (end < (p->offset + p->mtd.size)))
610 list_add(&new->list, &mtd_partitions);
611 mutex_unlock(&mtd_partitions_mutex);
613 add_mtd_device(&new->mtd);
617 mutex_unlock(&mtd_partitions_mutex);
621 EXPORT_SYMBOL_GPL(mtd_add_partition);
623 int mtd_del_partition(struct mtd_info *master, int partno)
625 struct mtd_part *slave, *next;
628 mutex_lock(&mtd_partitions_mutex);
629 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
630 if ((slave->master == master) &&
631 (slave->mtd.index == partno)) {
632 ret = del_mtd_device(&slave->mtd);
636 list_del(&slave->list);
637 free_partition(slave);
640 mutex_unlock(&mtd_partitions_mutex);
644 EXPORT_SYMBOL_GPL(mtd_del_partition);
647 * This function, given a master MTD object and a partition table, creates
648 * and registers slave MTD objects which are bound to the master according to
649 * the partition definitions.
651 * We don't register the master, or expect the caller to have done so,
652 * for reasons of data integrity.
655 int add_mtd_partitions(struct mtd_info *master,
656 const struct mtd_partition *parts,
659 struct mtd_part *slave;
660 uint64_t cur_offset = 0;
663 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
665 for (i = 0; i < nbparts; i++) {
666 slave = allocate_partition(master, parts + i, i, cur_offset);
668 return PTR_ERR(slave);
670 mutex_lock(&mtd_partitions_mutex);
671 list_add(&slave->list, &mtd_partitions);
672 mutex_unlock(&mtd_partitions_mutex);
674 add_mtd_device(&slave->mtd);
676 cur_offset = slave->offset + slave->mtd.size;
682 static DEFINE_SPINLOCK(part_parser_lock);
683 static LIST_HEAD(part_parsers);
685 static struct mtd_part_parser *get_partition_parser(const char *name)
687 struct mtd_part_parser *p, *ret = NULL;
689 spin_lock(&part_parser_lock);
691 list_for_each_entry(p, &part_parsers, list)
692 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
697 spin_unlock(&part_parser_lock);
702 #define put_partition_parser(p) do { module_put((p)->owner); } while (0)
704 int register_mtd_parser(struct mtd_part_parser *p)
706 spin_lock(&part_parser_lock);
707 list_add(&p->list, &part_parsers);
708 spin_unlock(&part_parser_lock);
712 EXPORT_SYMBOL_GPL(register_mtd_parser);
714 int deregister_mtd_parser(struct mtd_part_parser *p)
716 spin_lock(&part_parser_lock);
718 spin_unlock(&part_parser_lock);
721 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
724 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
725 * are changing this array!
727 static const char *default_mtd_part_types[] = {
734 * parse_mtd_partitions - parse MTD partitions
735 * @master: the master partition (describes whole MTD device)
736 * @types: names of partition parsers to try or %NULL
737 * @pparts: array of partitions found is returned here
738 * @data: MTD partition parser-specific data
740 * This function tries to find partition on MTD device @master. It uses MTD
741 * partition parsers, specified in @types. However, if @types is %NULL, then
742 * the default list of parsers is used. The default list contains only the
743 * "cmdlinepart" and "ofpart" parsers ATM.
745 * This function may return:
746 * o a negative error code in case of failure
747 * o zero if no partitions were found
748 * o a positive number of found partitions, in which case on exit @pparts will
749 * point to an array containing this number of &struct mtd_info objects.
751 int parse_mtd_partitions(struct mtd_info *master, const char **types,
752 struct mtd_partition **pparts,
753 struct mtd_part_parser_data *data)
755 struct mtd_part_parser *parser;
759 types = default_mtd_part_types;
761 for ( ; ret <= 0 && *types; types++) {
762 parser = get_partition_parser(*types);
763 if (!parser && !request_module("%s", *types))
764 parser = get_partition_parser(*types);
767 ret = (*parser->parse_fn)(master, pparts, data);
769 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
770 ret, parser->name, master->name);
772 put_partition_parser(parser);
777 int mtd_is_partition(struct mtd_info *mtd)
779 struct mtd_part *part;
782 mutex_lock(&mtd_partitions_mutex);
783 list_for_each_entry(part, &mtd_partitions, list)
784 if (&part->mtd == mtd) {
788 mutex_unlock(&mtd_partitions_mutex);
792 EXPORT_SYMBOL_GPL(mtd_is_partition);