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);
53 if (from >= mtd->size)
55 else if (from + len > mtd->size)
56 len = mtd->size - from;
57 res = part->master->read(part->master, from + part->offset,
61 mtd->ecc_stats.corrected++;
63 mtd->ecc_stats.failed++;
68 static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
69 size_t *retlen, void **virt, resource_size_t *phys)
71 struct mtd_part *part = PART(mtd);
72 if (from >= mtd->size)
74 else if (from + len > mtd->size)
75 len = mtd->size - from;
76 return part->master->point (part->master, from + part->offset,
77 len, retlen, virt, phys);
80 static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
82 struct mtd_part *part = PART(mtd);
84 part->master->unpoint(part->master, from + part->offset, len);
87 static int part_read_oob(struct mtd_info *mtd, loff_t from,
88 struct mtd_oob_ops *ops)
90 struct mtd_part *part = PART(mtd);
93 if (from >= mtd->size)
95 if (ops->datbuf && from + ops->len > mtd->size)
97 res = part->master->read_oob(part->master, from + part->offset, ops);
101 mtd->ecc_stats.corrected++;
103 mtd->ecc_stats.failed++;
108 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
109 size_t len, size_t *retlen, u_char *buf)
111 struct mtd_part *part = PART(mtd);
112 return part->master->read_user_prot_reg(part->master, from,
116 static int part_get_user_prot_info(struct mtd_info *mtd,
117 struct otp_info *buf, size_t len)
119 struct mtd_part *part = PART(mtd);
120 return part->master->get_user_prot_info(part->master, buf, len);
123 static int part_read_fact_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_fact_prot_reg(part->master, from,
131 static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
134 struct mtd_part *part = PART(mtd);
135 return part->master->get_fact_prot_info(part->master, buf, len);
138 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
139 size_t *retlen, const u_char *buf)
141 struct mtd_part *part = PART(mtd);
142 if (!(mtd->flags & MTD_WRITEABLE))
146 else if (to + len > mtd->size)
147 len = mtd->size - to;
148 return part->master->write(part->master, to + part->offset,
152 static int part_panic_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->panic_write(part->master, to + part->offset,
166 static int part_write_oob(struct mtd_info *mtd, loff_t to,
167 struct mtd_oob_ops *ops)
169 struct mtd_part *part = PART(mtd);
171 if (!(mtd->flags & MTD_WRITEABLE))
176 if (ops->datbuf && to + ops->len > mtd->size)
178 return part->master->write_oob(part->master, to + part->offset, ops);
181 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
182 size_t len, size_t *retlen, u_char *buf)
184 struct mtd_part *part = PART(mtd);
185 return part->master->write_user_prot_reg(part->master, from,
189 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
192 struct mtd_part *part = PART(mtd);
193 return part->master->lock_user_prot_reg(part->master, from, len);
196 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
197 unsigned long count, loff_t to, size_t *retlen)
199 struct mtd_part *part = PART(mtd);
200 if (!(mtd->flags & MTD_WRITEABLE))
202 return part->master->writev(part->master, vecs, count,
203 to + part->offset, retlen);
206 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
208 struct mtd_part *part = PART(mtd);
210 if (!(mtd->flags & MTD_WRITEABLE))
212 if (instr->addr >= mtd->size)
214 instr->addr += part->offset;
215 ret = part->master->erase(part->master, instr);
217 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
218 instr->fail_addr -= part->offset;
219 instr->addr -= part->offset;
224 void mtd_erase_callback(struct erase_info *instr)
226 if (instr->mtd->erase == part_erase) {
227 struct mtd_part *part = PART(instr->mtd);
229 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
230 instr->fail_addr -= part->offset;
231 instr->addr -= part->offset;
234 instr->callback(instr);
236 EXPORT_SYMBOL_GPL(mtd_erase_callback);
238 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
240 struct mtd_part *part = PART(mtd);
241 if ((len + ofs) > mtd->size)
243 return part->master->lock(part->master, ofs + part->offset, len);
246 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
248 struct mtd_part *part = PART(mtd);
249 if ((len + ofs) > mtd->size)
251 return part->master->unlock(part->master, ofs + part->offset, len);
254 static void part_sync(struct mtd_info *mtd)
256 struct mtd_part *part = PART(mtd);
257 part->master->sync(part->master);
260 static int part_suspend(struct mtd_info *mtd)
262 struct mtd_part *part = PART(mtd);
263 return part->master->suspend(part->master);
266 static void part_resume(struct mtd_info *mtd)
268 struct mtd_part *part = PART(mtd);
269 part->master->resume(part->master);
272 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
274 struct mtd_part *part = PART(mtd);
275 if (ofs >= mtd->size)
278 return part->master->block_isbad(part->master, ofs);
281 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
283 struct mtd_part *part = PART(mtd);
286 if (!(mtd->flags & MTD_WRITEABLE))
288 if (ofs >= mtd->size)
291 res = part->master->block_markbad(part->master, ofs);
293 mtd->ecc_stats.badblocks++;
298 * This function unregisters and destroy all slave MTD objects which are
299 * attached to the given master MTD object.
302 int del_mtd_partitions(struct mtd_info *master)
304 struct mtd_part *slave, *next;
306 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
307 if (slave->master == master) {
308 list_del(&slave->list);
309 if (slave->registered)
310 del_mtd_device(&slave->mtd);
316 EXPORT_SYMBOL(del_mtd_partitions);
318 static struct mtd_part *add_one_partition(struct mtd_info *master,
319 const struct mtd_partition *part, int partno,
322 struct mtd_part *slave;
324 /* allocate the partition structure */
325 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
327 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
329 del_mtd_partitions(master);
332 list_add(&slave->list, &mtd_partitions);
334 /* set up the MTD object for this partition */
335 slave->mtd.type = master->type;
336 slave->mtd.flags = master->flags & ~part->mask_flags;
337 slave->mtd.size = part->size;
338 slave->mtd.writesize = master->writesize;
339 slave->mtd.oobsize = master->oobsize;
340 slave->mtd.oobavail = master->oobavail;
341 slave->mtd.subpage_sft = master->subpage_sft;
343 slave->mtd.name = part->name;
344 slave->mtd.owner = master->owner;
346 slave->mtd.read = part_read;
347 slave->mtd.write = part_write;
349 if (master->panic_write)
350 slave->mtd.panic_write = part_panic_write;
352 if (master->point && master->unpoint) {
353 slave->mtd.point = part_point;
354 slave->mtd.unpoint = part_unpoint;
357 if (master->read_oob)
358 slave->mtd.read_oob = part_read_oob;
359 if (master->write_oob)
360 slave->mtd.write_oob = part_write_oob;
361 if (master->read_user_prot_reg)
362 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
363 if (master->read_fact_prot_reg)
364 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
365 if (master->write_user_prot_reg)
366 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
367 if (master->lock_user_prot_reg)
368 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
369 if (master->get_user_prot_info)
370 slave->mtd.get_user_prot_info = part_get_user_prot_info;
371 if (master->get_fact_prot_info)
372 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
374 slave->mtd.sync = part_sync;
375 if (!partno && master->suspend && master->resume) {
376 slave->mtd.suspend = part_suspend;
377 slave->mtd.resume = part_resume;
380 slave->mtd.writev = part_writev;
382 slave->mtd.lock = part_lock;
384 slave->mtd.unlock = part_unlock;
385 if (master->block_isbad)
386 slave->mtd.block_isbad = part_block_isbad;
387 if (master->block_markbad)
388 slave->mtd.block_markbad = part_block_markbad;
389 slave->mtd.erase = part_erase;
390 slave->master = master;
391 slave->offset = part->offset;
392 slave->index = partno;
394 if (slave->offset == MTDPART_OFS_APPEND)
395 slave->offset = cur_offset;
396 if (slave->offset == MTDPART_OFS_NXTBLK) {
397 slave->offset = cur_offset;
398 if (mtd_mod_by_eb(cur_offset, master) != 0) {
399 /* Round up to next erasesize */
400 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
401 printk(KERN_NOTICE "Moving partition %d: "
402 "0x%012llx -> 0x%012llx\n", partno,
403 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
406 if (slave->mtd.size == MTDPART_SIZ_FULL)
407 slave->mtd.size = master->size - slave->offset;
409 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
410 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
412 /* let's do some sanity checks */
413 if (slave->offset >= master->size) {
414 /* let's register it anyway to preserve ordering */
417 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
421 if (slave->offset + slave->mtd.size > master->size) {
422 slave->mtd.size = master->size - slave->offset;
423 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
424 part->name, master->name, (unsigned long long)slave->mtd.size);
426 if (master->numeraseregions > 1) {
427 /* Deal with variable erase size stuff */
428 int i, max = master->numeraseregions;
429 u64 end = slave->offset + slave->mtd.size;
430 struct mtd_erase_region_info *regions = master->eraseregions;
432 /* Find the first erase regions which is part of this
434 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
436 /* The loop searched for the region _behind_ the first one */
439 /* Pick biggest erasesize */
440 for (; i < max && regions[i].offset < end; i++) {
441 if (slave->mtd.erasesize < regions[i].erasesize) {
442 slave->mtd.erasesize = regions[i].erasesize;
445 BUG_ON(slave->mtd.erasesize == 0);
447 /* Single erase size */
448 slave->mtd.erasesize = master->erasesize;
451 if ((slave->mtd.flags & MTD_WRITEABLE) &&
452 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
453 /* Doesn't start on a boundary of major erase size */
454 /* FIXME: Let it be writable if it is on a boundary of
455 * _minor_ erase size though */
456 slave->mtd.flags &= ~MTD_WRITEABLE;
457 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
460 if ((slave->mtd.flags & MTD_WRITEABLE) &&
461 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
462 slave->mtd.flags &= ~MTD_WRITEABLE;
463 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
467 slave->mtd.ecclayout = master->ecclayout;
468 if (master->block_isbad) {
471 while (offs < slave->mtd.size) {
472 if (master->block_isbad(master,
473 offs + slave->offset))
474 slave->mtd.ecc_stats.badblocks++;
475 offs += slave->mtd.erasesize;
481 /* store the object pointer (caller may or may not register it*/
482 *part->mtdp = &slave->mtd;
483 slave->registered = 0;
485 /* register our partition */
486 add_mtd_device(&slave->mtd);
487 slave->registered = 1;
493 * This function, given a master MTD object and a partition table, creates
494 * and registers slave MTD objects which are bound to the master according to
495 * the partition definitions.
496 * (Q: should we register the master MTD object as well?)
499 int add_mtd_partitions(struct mtd_info *master,
500 const struct mtd_partition *parts,
503 struct mtd_part *slave;
504 uint64_t cur_offset = 0;
507 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
509 for (i = 0; i < nbparts; i++) {
510 slave = add_one_partition(master, parts + i, i, cur_offset);
513 cur_offset = slave->offset + slave->mtd.size;
518 EXPORT_SYMBOL(add_mtd_partitions);
520 static DEFINE_SPINLOCK(part_parser_lock);
521 static LIST_HEAD(part_parsers);
523 static struct mtd_part_parser *get_partition_parser(const char *name)
525 struct mtd_part_parser *p, *ret = NULL;
527 spin_lock(&part_parser_lock);
529 list_for_each_entry(p, &part_parsers, list)
530 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
535 spin_unlock(&part_parser_lock);
540 int register_mtd_parser(struct mtd_part_parser *p)
542 spin_lock(&part_parser_lock);
543 list_add(&p->list, &part_parsers);
544 spin_unlock(&part_parser_lock);
548 EXPORT_SYMBOL_GPL(register_mtd_parser);
550 int deregister_mtd_parser(struct mtd_part_parser *p)
552 spin_lock(&part_parser_lock);
554 spin_unlock(&part_parser_lock);
557 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
559 int parse_mtd_partitions(struct mtd_info *master, const char **types,
560 struct mtd_partition **pparts, unsigned long origin)
562 struct mtd_part_parser *parser;
565 for ( ; ret <= 0 && *types; types++) {
566 parser = get_partition_parser(*types);
567 if (!parser && !request_module("%s", *types))
568 parser = get_partition_parser(*types);
570 printk(KERN_NOTICE "%s partition parsing not available\n",
574 ret = (*parser->parse_fn)(master, pparts, origin);
576 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
577 ret, parser->name, master->name);
579 put_partition_parser(parser);
583 EXPORT_SYMBOL_GPL(parse_mtd_partitions);