2 * Simple MTD partitioning layer
4 * (C) 2000 Nicolas Pitre <nico@cam.org>
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;
31 struct list_head list;
36 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
37 * the pointer to that structure with this macro.
39 #define PART(x) ((struct mtd_part *)(x))
43 * MTD methods which simply translate the effective address and pass through
44 * to the _real_ device.
47 static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
48 size_t *retlen, u_char *buf)
50 struct mtd_part *part = PART(mtd);
51 struct mtd_ecc_stats stats;
54 stats = part->master->ecc_stats;
56 if (from >= mtd->size)
58 else if (from + len > mtd->size)
59 len = mtd->size - from;
60 res = part->master->read(part->master, from + part->offset,
64 mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
66 mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
71 static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
72 size_t *retlen, void **virt, resource_size_t *phys)
74 struct mtd_part *part = PART(mtd);
75 if (from >= mtd->size)
77 else if (from + len > mtd->size)
78 len = mtd->size - from;
79 return part->master->point (part->master, from + part->offset,
80 len, retlen, virt, phys);
83 static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
85 struct mtd_part *part = PART(mtd);
87 part->master->unpoint(part->master, from + part->offset, len);
90 static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
95 struct mtd_part *part = PART(mtd);
97 offset += part->offset;
98 return part->master->get_unmapped_area(part->master, len, offset,
102 static int part_read_oob(struct mtd_info *mtd, loff_t from,
103 struct mtd_oob_ops *ops)
105 struct mtd_part *part = PART(mtd);
108 if (from >= mtd->size)
110 if (ops->datbuf && from + ops->len > mtd->size)
112 res = part->master->read_oob(part->master, from + part->offset, ops);
116 mtd->ecc_stats.corrected++;
118 mtd->ecc_stats.failed++;
123 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
124 size_t len, size_t *retlen, u_char *buf)
126 struct mtd_part *part = PART(mtd);
127 return part->master->read_user_prot_reg(part->master, from,
131 static int part_get_user_prot_info(struct mtd_info *mtd,
132 struct otp_info *buf, size_t len)
134 struct mtd_part *part = PART(mtd);
135 return part->master->get_user_prot_info(part->master, buf, len);
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 = PART(mtd);
142 return part->master->read_fact_prot_reg(part->master, from,
146 static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
149 struct mtd_part *part = PART(mtd);
150 return part->master->get_fact_prot_info(part->master, buf, len);
153 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
154 size_t *retlen, const u_char *buf)
156 struct mtd_part *part = PART(mtd);
157 if (!(mtd->flags & MTD_WRITEABLE))
161 else if (to + len > mtd->size)
162 len = mtd->size - to;
163 return part->master->write(part->master, to + part->offset,
167 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
168 size_t *retlen, const u_char *buf)
170 struct mtd_part *part = PART(mtd);
171 if (!(mtd->flags & MTD_WRITEABLE))
175 else if (to + len > mtd->size)
176 len = mtd->size - to;
177 return part->master->panic_write(part->master, to + part->offset,
181 static int part_write_oob(struct mtd_info *mtd, loff_t to,
182 struct mtd_oob_ops *ops)
184 struct mtd_part *part = PART(mtd);
186 if (!(mtd->flags & MTD_WRITEABLE))
191 if (ops->datbuf && to + ops->len > mtd->size)
193 return part->master->write_oob(part->master, to + part->offset, ops);
196 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
197 size_t len, size_t *retlen, u_char *buf)
199 struct mtd_part *part = PART(mtd);
200 return part->master->write_user_prot_reg(part->master, from,
204 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
207 struct mtd_part *part = PART(mtd);
208 return part->master->lock_user_prot_reg(part->master, from, len);
211 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
212 unsigned long count, loff_t to, size_t *retlen)
214 struct mtd_part *part = PART(mtd);
215 if (!(mtd->flags & MTD_WRITEABLE))
217 return part->master->writev(part->master, vecs, count,
218 to + part->offset, retlen);
221 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
223 struct mtd_part *part = PART(mtd);
225 if (!(mtd->flags & MTD_WRITEABLE))
227 if (instr->addr >= mtd->size)
229 instr->addr += part->offset;
230 ret = part->master->erase(part->master, instr);
232 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
233 instr->fail_addr -= part->offset;
234 instr->addr -= part->offset;
239 void mtd_erase_callback(struct erase_info *instr)
241 if (instr->mtd->erase == part_erase) {
242 struct mtd_part *part = PART(instr->mtd);
244 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
245 instr->fail_addr -= part->offset;
246 instr->addr -= part->offset;
249 instr->callback(instr);
251 EXPORT_SYMBOL_GPL(mtd_erase_callback);
253 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
255 struct mtd_part *part = PART(mtd);
256 if ((len + ofs) > mtd->size)
258 return part->master->lock(part->master, ofs + part->offset, len);
261 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
263 struct mtd_part *part = PART(mtd);
264 if ((len + ofs) > mtd->size)
266 return part->master->unlock(part->master, ofs + part->offset, len);
269 static void part_sync(struct mtd_info *mtd)
271 struct mtd_part *part = PART(mtd);
272 part->master->sync(part->master);
275 static int part_suspend(struct mtd_info *mtd)
277 struct mtd_part *part = PART(mtd);
278 return part->master->suspend(part->master);
281 static void part_resume(struct mtd_info *mtd)
283 struct mtd_part *part = PART(mtd);
284 part->master->resume(part->master);
287 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
289 struct mtd_part *part = PART(mtd);
290 if (ofs >= mtd->size)
293 return part->master->block_isbad(part->master, ofs);
296 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
298 struct mtd_part *part = PART(mtd);
301 if (!(mtd->flags & MTD_WRITEABLE))
303 if (ofs >= mtd->size)
306 res = part->master->block_markbad(part->master, ofs);
308 mtd->ecc_stats.badblocks++;
313 * This function unregisters and destroy all slave MTD objects which are
314 * attached to the given master MTD object.
317 int del_mtd_partitions(struct mtd_info *master)
319 struct mtd_part *slave, *next;
321 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
322 if (slave->master == master) {
323 list_del(&slave->list);
324 if (slave->registered)
325 del_mtd_device(&slave->mtd);
331 EXPORT_SYMBOL(del_mtd_partitions);
333 static struct mtd_part *add_one_partition(struct mtd_info *master,
334 const struct mtd_partition *part, int partno,
337 struct mtd_part *slave;
339 /* allocate the partition structure */
340 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
342 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
344 del_mtd_partitions(master);
347 list_add(&slave->list, &mtd_partitions);
349 /* set up the MTD object for this partition */
350 slave->mtd.type = master->type;
351 slave->mtd.flags = master->flags & ~part->mask_flags;
352 slave->mtd.size = part->size;
353 slave->mtd.writesize = master->writesize;
354 slave->mtd.oobsize = master->oobsize;
355 slave->mtd.oobavail = master->oobavail;
356 slave->mtd.subpage_sft = master->subpage_sft;
358 slave->mtd.name = part->name;
359 slave->mtd.owner = master->owner;
360 slave->mtd.backing_dev_info = master->backing_dev_info;
362 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
363 * to have the same data be in two different partitions.
365 slave->mtd.dev.parent = master->dev.parent;
367 slave->mtd.read = part_read;
368 slave->mtd.write = part_write;
370 if (master->panic_write)
371 slave->mtd.panic_write = part_panic_write;
373 if (master->point && master->unpoint) {
374 slave->mtd.point = part_point;
375 slave->mtd.unpoint = part_unpoint;
378 if (master->get_unmapped_area)
379 slave->mtd.get_unmapped_area = part_get_unmapped_area;
380 if (master->read_oob)
381 slave->mtd.read_oob = part_read_oob;
382 if (master->write_oob)
383 slave->mtd.write_oob = part_write_oob;
384 if (master->read_user_prot_reg)
385 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
386 if (master->read_fact_prot_reg)
387 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
388 if (master->write_user_prot_reg)
389 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
390 if (master->lock_user_prot_reg)
391 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
392 if (master->get_user_prot_info)
393 slave->mtd.get_user_prot_info = part_get_user_prot_info;
394 if (master->get_fact_prot_info)
395 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
397 slave->mtd.sync = part_sync;
398 if (!partno && master->suspend && master->resume) {
399 slave->mtd.suspend = part_suspend;
400 slave->mtd.resume = part_resume;
403 slave->mtd.writev = part_writev;
405 slave->mtd.lock = part_lock;
407 slave->mtd.unlock = part_unlock;
408 if (master->block_isbad)
409 slave->mtd.block_isbad = part_block_isbad;
410 if (master->block_markbad)
411 slave->mtd.block_markbad = part_block_markbad;
412 slave->mtd.erase = part_erase;
413 slave->master = master;
414 slave->offset = part->offset;
415 slave->index = partno;
417 if (slave->offset == MTDPART_OFS_APPEND)
418 slave->offset = cur_offset;
419 if (slave->offset == MTDPART_OFS_NXTBLK) {
420 slave->offset = cur_offset;
421 if (mtd_mod_by_eb(cur_offset, master) != 0) {
422 /* Round up to next erasesize */
423 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
424 printk(KERN_NOTICE "Moving partition %d: "
425 "0x%012llx -> 0x%012llx\n", partno,
426 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
429 if (slave->mtd.size == MTDPART_SIZ_FULL)
430 slave->mtd.size = master->size - slave->offset;
432 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
433 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
435 /* let's do some sanity checks */
436 if (slave->offset >= master->size) {
437 /* let's register it anyway to preserve ordering */
440 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
444 if (slave->offset + slave->mtd.size > master->size) {
445 slave->mtd.size = master->size - slave->offset;
446 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
447 part->name, master->name, (unsigned long long)slave->mtd.size);
449 if (master->numeraseregions > 1) {
450 /* Deal with variable erase size stuff */
451 int i, max = master->numeraseregions;
452 u64 end = slave->offset + slave->mtd.size;
453 struct mtd_erase_region_info *regions = master->eraseregions;
455 /* Find the first erase regions which is part of this
457 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
459 /* The loop searched for the region _behind_ the first one */
462 /* Pick biggest erasesize */
463 for (; i < max && regions[i].offset < end; i++) {
464 if (slave->mtd.erasesize < regions[i].erasesize) {
465 slave->mtd.erasesize = regions[i].erasesize;
468 BUG_ON(slave->mtd.erasesize == 0);
470 /* Single erase size */
471 slave->mtd.erasesize = master->erasesize;
474 if ((slave->mtd.flags & MTD_WRITEABLE) &&
475 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
476 /* Doesn't start on a boundary of major erase size */
477 /* FIXME: Let it be writable if it is on a boundary of
478 * _minor_ erase size though */
479 slave->mtd.flags &= ~MTD_WRITEABLE;
480 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
483 if ((slave->mtd.flags & MTD_WRITEABLE) &&
484 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
485 slave->mtd.flags &= ~MTD_WRITEABLE;
486 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
490 slave->mtd.ecclayout = master->ecclayout;
491 if (master->block_isbad) {
494 while (offs < slave->mtd.size) {
495 if (master->block_isbad(master,
496 offs + slave->offset))
497 slave->mtd.ecc_stats.badblocks++;
498 offs += slave->mtd.erasesize;
504 /* store the object pointer (caller may or may not register it*/
505 *part->mtdp = &slave->mtd;
506 slave->registered = 0;
508 /* register our partition */
509 add_mtd_device(&slave->mtd);
510 slave->registered = 1;
516 * This function, given a master MTD object and a partition table, creates
517 * and registers slave MTD objects which are bound to the master according to
518 * the partition definitions.
520 * We don't register the master, or expect the caller to have done so,
521 * for reasons of data integrity.
524 int add_mtd_partitions(struct mtd_info *master,
525 const struct mtd_partition *parts,
528 struct mtd_part *slave;
529 uint64_t cur_offset = 0;
532 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
534 for (i = 0; i < nbparts; i++) {
535 slave = add_one_partition(master, parts + i, i, cur_offset);
538 cur_offset = slave->offset + slave->mtd.size;
543 EXPORT_SYMBOL(add_mtd_partitions);
545 static DEFINE_SPINLOCK(part_parser_lock);
546 static LIST_HEAD(part_parsers);
548 static struct mtd_part_parser *get_partition_parser(const char *name)
550 struct mtd_part_parser *p, *ret = NULL;
552 spin_lock(&part_parser_lock);
554 list_for_each_entry(p, &part_parsers, list)
555 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
560 spin_unlock(&part_parser_lock);
565 int register_mtd_parser(struct mtd_part_parser *p)
567 spin_lock(&part_parser_lock);
568 list_add(&p->list, &part_parsers);
569 spin_unlock(&part_parser_lock);
573 EXPORT_SYMBOL_GPL(register_mtd_parser);
575 int deregister_mtd_parser(struct mtd_part_parser *p)
577 spin_lock(&part_parser_lock);
579 spin_unlock(&part_parser_lock);
582 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
584 int parse_mtd_partitions(struct mtd_info *master, const char **types,
585 struct mtd_partition **pparts, unsigned long origin)
587 struct mtd_part_parser *parser;
590 for ( ; ret <= 0 && *types; types++) {
591 parser = get_partition_parser(*types);
592 if (!parser && !request_module("%s", *types))
593 parser = get_partition_parser(*types);
595 printk(KERN_NOTICE "%s partition parsing not available\n",
599 ret = (*parser->parse_fn)(master, pparts, origin);
601 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
602 ret, parser->name, master->name);
604 put_partition_parser(parser);
608 EXPORT_SYMBOL_GPL(parse_mtd_partitions);