2 * Simple MTD partitioning layer
4 * (C) 2000 Nicolas Pitre <nico@fluxnic.net>
8 * 02-21-2002 Thomas Gleixner <gleixner@autronix.de>
9 * added support for read_oob, write_oob
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/list.h>
17 #include <linux/kmod.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/partitions.h>
20 #include <linux/mtd/compatmac.h>
22 /* Our partition linked list */
23 static LIST_HEAD(mtd_partitions
);
25 /* Our partition node structure */
28 struct mtd_info
*master
;
29 struct memory_accessor macc
;
31 struct list_head list
;
35 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
36 * the pointer to that structure with this macro.
38 #define PART(x) ((struct mtd_part *)(x))
42 * MTD methods which simply translate the effective address and pass through
43 * to the _real_ device.
46 static int part_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
47 size_t *retlen
, u_char
*buf
)
49 struct mtd_part
*part
= PART(mtd
);
50 struct mtd_ecc_stats stats
;
53 stats
= part
->master
->ecc_stats
;
55 if (from
>= mtd
->size
)
57 else if (from
+ len
> mtd
->size
)
58 len
= mtd
->size
- from
;
59 res
= part
->master
->read(part
->master
, from
+ part
->offset
,
63 mtd
->ecc_stats
.corrected
+= part
->master
->ecc_stats
.corrected
- stats
.corrected
;
65 mtd
->ecc_stats
.failed
+= part
->master
->ecc_stats
.failed
- stats
.failed
;
70 static int part_point(struct mtd_info
*mtd
, loff_t from
, size_t len
,
71 size_t *retlen
, void **virt
, resource_size_t
*phys
)
73 struct mtd_part
*part
= PART(mtd
);
74 if (from
>= mtd
->size
)
76 else if (from
+ len
> mtd
->size
)
77 len
= mtd
->size
- from
;
78 return part
->master
->point (part
->master
, from
+ part
->offset
,
79 len
, retlen
, virt
, phys
);
82 static void part_unpoint(struct mtd_info
*mtd
, loff_t from
, size_t len
)
84 struct mtd_part
*part
= PART(mtd
);
86 part
->master
->unpoint(part
->master
, from
+ part
->offset
, len
);
89 static unsigned long part_get_unmapped_area(struct mtd_info
*mtd
,
94 struct mtd_part
*part
= PART(mtd
);
96 offset
+= part
->offset
;
97 return part
->master
->get_unmapped_area(part
->master
, len
, offset
,
101 static int part_read_oob(struct mtd_info
*mtd
, loff_t from
,
102 struct mtd_oob_ops
*ops
)
104 struct mtd_part
*part
= PART(mtd
);
107 if (from
>= mtd
->size
)
109 if (ops
->datbuf
&& from
+ ops
->len
> mtd
->size
)
111 res
= part
->master
->read_oob(part
->master
, from
+ part
->offset
, ops
);
115 mtd
->ecc_stats
.corrected
++;
117 mtd
->ecc_stats
.failed
++;
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
= PART(mtd
);
126 return part
->master
->read_user_prot_reg(part
->master
, from
,
130 static int part_get_user_prot_info(struct mtd_info
*mtd
,
131 struct otp_info
*buf
, size_t len
)
133 struct mtd_part
*part
= PART(mtd
);
134 return part
->master
->get_user_prot_info(part
->master
, buf
, len
);
137 static int part_read_fact_prot_reg(struct mtd_info
*mtd
, loff_t from
,
138 size_t len
, size_t *retlen
, u_char
*buf
)
140 struct mtd_part
*part
= PART(mtd
);
141 return part
->master
->read_fact_prot_reg(part
->master
, from
,
145 static int part_get_fact_prot_info(struct mtd_info
*mtd
, struct otp_info
*buf
,
148 struct mtd_part
*part
= PART(mtd
);
149 return part
->master
->get_fact_prot_info(part
->master
, buf
, len
);
152 static int part_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
153 size_t *retlen
, const u_char
*buf
)
155 struct mtd_part
*part
= PART(mtd
);
156 if (!(mtd
->flags
& MTD_WRITEABLE
))
160 else if (to
+ len
> mtd
->size
)
161 len
= mtd
->size
- to
;
162 return part
->master
->write(part
->master
, to
+ part
->offset
,
166 static int part_panic_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
167 size_t *retlen
, const u_char
*buf
)
169 struct mtd_part
*part
= PART(mtd
);
170 if (!(mtd
->flags
& MTD_WRITEABLE
))
174 else if (to
+ len
> mtd
->size
)
175 len
= mtd
->size
- to
;
176 return part
->master
->panic_write(part
->master
, to
+ part
->offset
,
180 static int part_write_oob(struct mtd_info
*mtd
, loff_t to
,
181 struct mtd_oob_ops
*ops
)
183 struct mtd_part
*part
= PART(mtd
);
185 if (!(mtd
->flags
& MTD_WRITEABLE
))
190 if (ops
->datbuf
&& to
+ ops
->len
> mtd
->size
)
192 return part
->master
->write_oob(part
->master
, to
+ part
->offset
, ops
);
195 static int part_write_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
196 size_t len
, size_t *retlen
, u_char
*buf
)
198 struct mtd_part
*part
= PART(mtd
);
199 return part
->master
->write_user_prot_reg(part
->master
, from
,
203 static int part_lock_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
206 struct mtd_part
*part
= PART(mtd
);
207 return part
->master
->lock_user_prot_reg(part
->master
, from
, len
);
210 static int part_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
211 unsigned long count
, loff_t to
, size_t *retlen
)
213 struct mtd_part
*part
= PART(mtd
);
214 if (!(mtd
->flags
& MTD_WRITEABLE
))
216 return part
->master
->writev(part
->master
, vecs
, count
,
217 to
+ part
->offset
, retlen
);
220 static int part_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
222 struct mtd_part
*part
= PART(mtd
);
224 if (!(mtd
->flags
& MTD_WRITEABLE
))
226 if (instr
->addr
>= mtd
->size
)
228 instr
->addr
+= part
->offset
;
229 ret
= part
->master
->erase(part
->master
, instr
);
231 if (instr
->fail_addr
!= MTD_FAIL_ADDR_UNKNOWN
)
232 instr
->fail_addr
-= part
->offset
;
233 instr
->addr
-= part
->offset
;
238 void mtd_erase_callback(struct erase_info
*instr
)
240 if (instr
->mtd
->erase
== part_erase
) {
241 struct mtd_part
*part
= PART(instr
->mtd
);
243 if (instr
->fail_addr
!= MTD_FAIL_ADDR_UNKNOWN
)
244 instr
->fail_addr
-= part
->offset
;
245 instr
->addr
-= part
->offset
;
248 instr
->callback(instr
);
250 EXPORT_SYMBOL_GPL(mtd_erase_callback
);
252 static int part_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
254 struct mtd_part
*part
= PART(mtd
);
255 if ((len
+ ofs
) > mtd
->size
)
257 return part
->master
->lock(part
->master
, ofs
+ part
->offset
, len
);
260 static int part_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
262 struct mtd_part
*part
= PART(mtd
);
263 if ((len
+ ofs
) > mtd
->size
)
265 return part
->master
->unlock(part
->master
, ofs
+ part
->offset
, len
);
268 static void part_sync(struct mtd_info
*mtd
)
270 struct mtd_part
*part
= PART(mtd
);
271 part
->master
->sync(part
->master
);
274 static int part_suspend(struct mtd_info
*mtd
)
276 struct mtd_part
*part
= PART(mtd
);
277 return part
->master
->suspend(part
->master
);
280 static void part_resume(struct mtd_info
*mtd
)
282 struct mtd_part
*part
= PART(mtd
);
283 part
->master
->resume(part
->master
);
286 static int part_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
288 struct mtd_part
*part
= PART(mtd
);
289 if (ofs
>= mtd
->size
)
292 return part
->master
->block_isbad(part
->master
, ofs
);
295 static int part_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
297 struct mtd_part
*part
= PART(mtd
);
300 if (!(mtd
->flags
& MTD_WRITEABLE
))
302 if (ofs
>= mtd
->size
)
305 res
= part
->master
->block_markbad(part
->master
, ofs
);
307 mtd
->ecc_stats
.badblocks
++;
312 * This function unregisters and destroy all slave MTD objects which are
313 * attached to the given master MTD object.
316 int del_mtd_partitions(struct mtd_info
*master
)
318 struct mtd_part
*slave
, *next
;
320 list_for_each_entry_safe(slave
, next
, &mtd_partitions
, list
)
321 if (slave
->master
== master
) {
322 list_del(&slave
->list
);
323 del_mtd_device(&slave
->mtd
);
329 EXPORT_SYMBOL(del_mtd_partitions
);
332 * This lets other kernel code access the flash data. For example, it
333 * might hold a board's Ethernet address, or board-specific calibration
334 * data generated on the manufacturing floor.
336 static ssize_t
mtd_macc_read(struct memory_accessor
*macc
, char *buf
,
337 off_t offset
, size_t count
)
339 struct mtd_part
*part
= container_of(macc
, struct mtd_part
, macc
);
343 if (part_read((struct mtd_info
*)part
, offset
, count
,
350 static ssize_t
mtd_macc_write(struct memory_accessor
*macc
, const char *buf
,
351 off_t offset
, size_t count
)
353 struct mtd_part
*part
= container_of(macc
, struct mtd_part
, macc
);
357 if (part_write((struct mtd_info
*)part
, offset
, count
,
364 static struct mtd_part
*add_one_partition(struct mtd_info
*master
,
365 const struct mtd_partition
*part
, int partno
,
368 struct mtd_part
*slave
;
370 /* allocate the partition structure */
371 slave
= kzalloc(sizeof(*slave
), GFP_KERNEL
);
373 printk(KERN_ERR
"memory allocation error while creating partitions for \"%s
\"\n",
375 del_mtd_partitions(master);
378 list_add(&slave->list, &mtd_partitions);
380 /* set up the MTD object for this partition */
381 slave->mtd.type = master->type;
382 slave->mtd.flags = master->flags & ~part->mask_flags;
383 slave->mtd.size = part->size;
384 slave->mtd.writesize = master->writesize;
385 slave->mtd.oobsize = master->oobsize;
386 slave->mtd.oobavail = master->oobavail;
387 slave->mtd.subpage_sft = master->subpage_sft;
389 slave->mtd.name = part->name;
390 slave->mtd.owner = master->owner;
391 slave->mtd.backing_dev_info = master->backing_dev_info;
393 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
394 * to have the same data be in two different partitions.
396 slave->mtd.dev.parent = master->dev.parent;
398 slave->mtd.read = part_read;
399 slave->mtd.write = part_write;
401 slave->macc.read = mtd_macc_read;
402 slave->macc.write = mtd_macc_write;
404 if (master->panic_write)
405 slave->mtd.panic_write = part_panic_write;
407 if (master->point && master->unpoint) {
408 slave->mtd.point = part_point;
409 slave->mtd.unpoint = part_unpoint;
412 if (master->get_unmapped_area)
413 slave->mtd.get_unmapped_area = part_get_unmapped_area;
414 if (master->read_oob)
415 slave->mtd.read_oob = part_read_oob;
416 if (master->write_oob)
417 slave->mtd.write_oob = part_write_oob;
418 if (master->read_user_prot_reg)
419 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
420 if (master->read_fact_prot_reg)
421 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
422 if (master->write_user_prot_reg)
423 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
424 if (master->lock_user_prot_reg)
425 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
426 if (master->get_user_prot_info)
427 slave->mtd.get_user_prot_info = part_get_user_prot_info;
428 if (master->get_fact_prot_info)
429 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
431 slave->mtd.sync = part_sync;
432 if (!partno && !master->dev.class && master->suspend && master->resume) {
433 slave->mtd.suspend = part_suspend;
434 slave->mtd.resume = part_resume;
437 slave->mtd.writev = part_writev;
439 slave->mtd.lock = part_lock;
441 slave->mtd.unlock = part_unlock;
442 if (master->block_isbad)
443 slave->mtd.block_isbad = part_block_isbad;
444 if (master->block_markbad)
445 slave->mtd.block_markbad = part_block_markbad;
446 slave->mtd.erase = part_erase;
447 slave->master = master;
448 slave->offset = part->offset;
450 if (slave->offset == MTDPART_OFS_APPEND)
451 slave->offset = cur_offset;
452 if (slave->offset == MTDPART_OFS_NXTBLK) {
453 slave->offset = cur_offset;
454 if (mtd_mod_by_eb(cur_offset, master) != 0) {
455 /* Round up to next erasesize */
456 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
457 printk(KERN_NOTICE "Moving partition
%d
: "
458 "0x
%012llx
-> 0x
%012llx
\n", partno,
459 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
462 if (slave->mtd.size == MTDPART_SIZ_FULL)
463 slave->mtd.size = master->size - slave->offset;
465 printk(KERN_NOTICE "0x
%012llx
-0x
%012llx
: \"%s
\"\n", (unsigned long long)slave->offset,
466 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
469 part->setup(&slave->macc, (void *)part->context);
471 /* let's do some sanity checks */
472 if (slave->offset >= master->size) {
473 /* let's register it anyway to preserve ordering */
476 printk(KERN_ERR"mtd
: partition
\"%s
\" is out of reach
-- disabled
\n",
480 if (slave->offset + slave->mtd.size > master->size) {
481 slave->mtd.size = master->size - slave->offset;
482 printk(KERN_WARNING"mtd
: partition
\"%s
\" extends beyond the end of device
\"%s
\" -- size truncated to
%#llx\n",
483 part->name, master->name, (unsigned long long)slave->mtd.size);
485 if (master->numeraseregions > 1) {
486 /* Deal with variable erase size stuff */
487 int i, max = master->numeraseregions;
488 u64 end = slave->offset + slave->mtd.size;
489 struct mtd_erase_region_info *regions = master->eraseregions;
491 /* Find the first erase regions which is part of this
493 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
495 /* The loop searched for the region _behind_ the first one */
499 /* Pick biggest erasesize */
500 for (; i < max && regions[i].offset < end; i++) {
501 if (slave->mtd.erasesize < regions[i].erasesize) {
502 slave->mtd.erasesize = regions[i].erasesize;
505 BUG_ON(slave->mtd.erasesize == 0);
507 /* Single erase size */
508 slave->mtd.erasesize = master->erasesize;
511 if ((slave->mtd.flags & MTD_WRITEABLE) &&
512 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
513 /* Doesn't start on a boundary of major erase size */
514 /* FIXME: Let it be writable if it is on a boundary of
515 * _minor_ erase size though */
516 slave->mtd.flags &= ~MTD_WRITEABLE;
517 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
520 if ((slave->mtd.flags & MTD_WRITEABLE) &&
521 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
522 slave->mtd.flags &= ~MTD_WRITEABLE;
523 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
527 slave->mtd.ecclayout = master->ecclayout;
528 if (master->block_isbad) {
531 while (offs < slave->mtd.size) {
532 if (master->block_isbad(master,
533 offs + slave->offset))
534 slave->mtd.ecc_stats.badblocks++;
535 offs += slave->mtd.erasesize;
540 /* register our partition */
541 add_mtd_device(&slave->mtd);
547 * This function, given a master MTD object and a partition table, creates
548 * and registers slave MTD objects which are bound to the master according to
549 * the partition definitions.
551 * We don't register the master, or expect the caller to have done so,
552 * for reasons of data integrity.
555 int add_mtd_partitions(struct mtd_info *master,
556 const struct mtd_partition *parts,
559 struct mtd_part *slave;
560 uint64_t cur_offset = 0;
563 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
565 for (i = 0; i < nbparts; i++) {
566 slave = add_one_partition(master, parts + i, i, cur_offset);
569 cur_offset = slave->offset + slave->mtd.size;
574 EXPORT_SYMBOL(add_mtd_partitions);
576 static DEFINE_SPINLOCK(part_parser_lock);
577 static LIST_HEAD(part_parsers);
579 static struct mtd_part_parser *get_partition_parser(const char *name)
581 struct mtd_part_parser *p, *ret = NULL;
583 spin_lock(&part_parser_lock);
585 list_for_each_entry(p, &part_parsers, list)
586 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
591 spin_unlock(&part_parser_lock);
596 int register_mtd_parser(struct mtd_part_parser *p)
598 spin_lock(&part_parser_lock);
599 list_add(&p->list, &part_parsers);
600 spin_unlock(&part_parser_lock);
604 EXPORT_SYMBOL_GPL(register_mtd_parser);
606 int deregister_mtd_parser(struct mtd_part_parser *p)
608 spin_lock(&part_parser_lock);
610 spin_unlock(&part_parser_lock);
613 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
615 int parse_mtd_partitions(struct mtd_info *master, const char **types,
616 struct mtd_partition **pparts, unsigned long origin)
618 struct mtd_part_parser *parser;
621 for ( ; ret <= 0 && *types; types++) {
622 parser = get_partition_parser(*types);
623 if (!parser && !request_module("%s", *types))
624 parser = get_partition_parser(*types);
626 printk(KERN_NOTICE "%s partition parsing not available\n",
630 ret = (*parser->parse_fn)(master, pparts, origin);
632 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
633 ret, parser->name, master->name);
635 put_partition_parser(parser);
639 EXPORT_SYMBOL_GPL(parse_mtd_partitions);