1 /* 2 * Simple MTD partitioning layer 3 * 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> 7 * 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. 12 * 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. 17 * 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 21 * 22 */ 23 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> 33 #include <linux/kconfig.h> 34 35 #include "mtdcore.h" 36 37 /* Our partition linked list */ 38 static LIST_HEAD(mtd_partitions); 39 static DEFINE_MUTEX(mtd_partitions_mutex); 40 41 /* Our partition node structure */ 42 struct mtd_part { 43 struct mtd_info mtd; 44 struct mtd_info *master; 45 uint64_t offset; 46 struct list_head list; 47 }; 48 49 /* 50 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve 51 * the pointer to that structure. 52 */ 53 static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd) 54 { 55 return container_of(mtd, struct mtd_part, mtd); 56 } 57 58 59 /* 60 * MTD methods which simply translate the effective address and pass through 61 * to the _real_ device. 62 */ 63 64 static int part_read(struct mtd_info *mtd, loff_t from, size_t len, 65 size_t *retlen, u_char *buf) 66 { 67 struct mtd_part *part = mtd_to_part(mtd); 68 struct mtd_ecc_stats stats; 69 int res; 70 71 stats = part->master->ecc_stats; 72 res = part->master->_read(part->master, from + part->offset, len, 73 retlen, buf); 74 if (unlikely(mtd_is_eccerr(res))) 75 mtd->ecc_stats.failed += 76 part->master->ecc_stats.failed - stats.failed; 77 else 78 mtd->ecc_stats.corrected += 79 part->master->ecc_stats.corrected - stats.corrected; 80 return res; 81 } 82 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) 85 { 86 struct mtd_part *part = mtd_to_part(mtd); 87 88 return part->master->_point(part->master, from + part->offset, len, 89 retlen, virt, phys); 90 } 91 92 static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len) 93 { 94 struct mtd_part *part = mtd_to_part(mtd); 95 96 return part->master->_unpoint(part->master, from + part->offset, len); 97 } 98 99 static unsigned long part_get_unmapped_area(struct mtd_info *mtd, 100 unsigned long len, 101 unsigned long offset, 102 unsigned long flags) 103 { 104 struct mtd_part *part = mtd_to_part(mtd); 105 106 offset += part->offset; 107 return part->master->_get_unmapped_area(part->master, len, offset, 108 flags); 109 } 110 111 static int part_read_oob(struct mtd_info *mtd, loff_t from, 112 struct mtd_oob_ops *ops) 113 { 114 struct mtd_part *part = mtd_to_part(mtd); 115 int res; 116 117 if (from >= mtd->size) 118 return -EINVAL; 119 if (ops->datbuf && from + ops->len > mtd->size) 120 return -EINVAL; 121 122 /* 123 * If OOB is also requested, make sure that we do not read past the end 124 * of this partition. 125 */ 126 if (ops->oobbuf) { 127 size_t len, pages; 128 129 if (ops->mode == MTD_OPS_AUTO_OOB) 130 len = mtd->oobavail; 131 else 132 len = mtd->oobsize; 133 pages = mtd_div_by_ws(mtd->size, mtd); 134 pages -= mtd_div_by_ws(from, mtd); 135 if (ops->ooboffs + ops->ooblen > pages * len) 136 return -EINVAL; 137 } 138 139 res = part->master->_read_oob(part->master, from + part->offset, ops); 140 if (unlikely(res)) { 141 if (mtd_is_bitflip(res)) 142 mtd->ecc_stats.corrected++; 143 if (mtd_is_eccerr(res)) 144 mtd->ecc_stats.failed++; 145 } 146 return res; 147 } 148 149 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from, 150 size_t len, size_t *retlen, u_char *buf) 151 { 152 struct mtd_part *part = mtd_to_part(mtd); 153 return part->master->_read_user_prot_reg(part->master, from, len, 154 retlen, buf); 155 } 156 157 static int part_get_user_prot_info(struct mtd_info *mtd, size_t len, 158 size_t *retlen, struct otp_info *buf) 159 { 160 struct mtd_part *part = mtd_to_part(mtd); 161 return part->master->_get_user_prot_info(part->master, len, retlen, 162 buf); 163 } 164 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) 167 { 168 struct mtd_part *part = mtd_to_part(mtd); 169 return part->master->_read_fact_prot_reg(part->master, from, len, 170 retlen, buf); 171 } 172 173 static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len, 174 size_t *retlen, struct otp_info *buf) 175 { 176 struct mtd_part *part = mtd_to_part(mtd); 177 return part->master->_get_fact_prot_info(part->master, len, retlen, 178 buf); 179 } 180 181 static int part_write(struct mtd_info *mtd, loff_t to, size_t len, 182 size_t *retlen, const u_char *buf) 183 { 184 struct mtd_part *part = mtd_to_part(mtd); 185 return part->master->_write(part->master, to + part->offset, len, 186 retlen, buf); 187 } 188 189 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len, 190 size_t *retlen, const u_char *buf) 191 { 192 struct mtd_part *part = mtd_to_part(mtd); 193 return part->master->_panic_write(part->master, to + part->offset, len, 194 retlen, buf); 195 } 196 197 static int part_write_oob(struct mtd_info *mtd, loff_t to, 198 struct mtd_oob_ops *ops) 199 { 200 struct mtd_part *part = mtd_to_part(mtd); 201 202 if (to >= mtd->size) 203 return -EINVAL; 204 if (ops->datbuf && to + ops->len > mtd->size) 205 return -EINVAL; 206 return part->master->_write_oob(part->master, to + part->offset, ops); 207 } 208 209 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from, 210 size_t len, size_t *retlen, u_char *buf) 211 { 212 struct mtd_part *part = mtd_to_part(mtd); 213 return part->master->_write_user_prot_reg(part->master, from, len, 214 retlen, buf); 215 } 216 217 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, 218 size_t len) 219 { 220 struct mtd_part *part = mtd_to_part(mtd); 221 return part->master->_lock_user_prot_reg(part->master, from, len); 222 } 223 224 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs, 225 unsigned long count, loff_t to, size_t *retlen) 226 { 227 struct mtd_part *part = mtd_to_part(mtd); 228 return part->master->_writev(part->master, vecs, count, 229 to + part->offset, retlen); 230 } 231 232 static int part_erase(struct mtd_info *mtd, struct erase_info *instr) 233 { 234 struct mtd_part *part = mtd_to_part(mtd); 235 int ret; 236 237 instr->addr += part->offset; 238 ret = part->master->_erase(part->master, instr); 239 if (ret) { 240 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) 241 instr->fail_addr -= part->offset; 242 instr->addr -= part->offset; 243 } 244 return ret; 245 } 246 247 void mtd_erase_callback(struct erase_info *instr) 248 { 249 if (instr->mtd->_erase == part_erase) { 250 struct mtd_part *part = mtd_to_part(instr->mtd); 251 252 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) 253 instr->fail_addr -= part->offset; 254 instr->addr -= part->offset; 255 } 256 if (instr->callback) 257 instr->callback(instr); 258 } 259 EXPORT_SYMBOL_GPL(mtd_erase_callback); 260 261 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 262 { 263 struct mtd_part *part = mtd_to_part(mtd); 264 return part->master->_lock(part->master, ofs + part->offset, len); 265 } 266 267 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 268 { 269 struct mtd_part *part = mtd_to_part(mtd); 270 return part->master->_unlock(part->master, ofs + part->offset, len); 271 } 272 273 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) 274 { 275 struct mtd_part *part = mtd_to_part(mtd); 276 return part->master->_is_locked(part->master, ofs + part->offset, len); 277 } 278 279 static void part_sync(struct mtd_info *mtd) 280 { 281 struct mtd_part *part = mtd_to_part(mtd); 282 part->master->_sync(part->master); 283 } 284 285 static int part_suspend(struct mtd_info *mtd) 286 { 287 struct mtd_part *part = mtd_to_part(mtd); 288 return part->master->_suspend(part->master); 289 } 290 291 static void part_resume(struct mtd_info *mtd) 292 { 293 struct mtd_part *part = mtd_to_part(mtd); 294 part->master->_resume(part->master); 295 } 296 297 static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs) 298 { 299 struct mtd_part *part = mtd_to_part(mtd); 300 ofs += part->offset; 301 return part->master->_block_isreserved(part->master, ofs); 302 } 303 304 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs) 305 { 306 struct mtd_part *part = mtd_to_part(mtd); 307 ofs += part->offset; 308 return part->master->_block_isbad(part->master, ofs); 309 } 310 311 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs) 312 { 313 struct mtd_part *part = mtd_to_part(mtd); 314 int res; 315 316 ofs += part->offset; 317 res = part->master->_block_markbad(part->master, ofs); 318 if (!res) 319 mtd->ecc_stats.badblocks++; 320 return res; 321 } 322 323 static inline void free_partition(struct mtd_part *p) 324 { 325 kfree(p->mtd.name); 326 kfree(p); 327 } 328 329 /* 330 * This function unregisters and destroy all slave MTD objects which are 331 * attached to the given master MTD object. 332 */ 333 334 int del_mtd_partitions(struct mtd_info *master) 335 { 336 struct mtd_part *slave, *next; 337 int ret, err = 0; 338 339 mutex_lock(&mtd_partitions_mutex); 340 list_for_each_entry_safe(slave, next, &mtd_partitions, list) 341 if (slave->master == master) { 342 ret = del_mtd_device(&slave->mtd); 343 if (ret < 0) { 344 err = ret; 345 continue; 346 } 347 list_del(&slave->list); 348 free_partition(slave); 349 } 350 mutex_unlock(&mtd_partitions_mutex); 351 352 return err; 353 } 354 355 static struct mtd_part *allocate_partition(struct mtd_info *master, 356 const struct mtd_partition *part, int partno, 357 uint64_t cur_offset) 358 { 359 struct mtd_part *slave; 360 char *name; 361 362 /* allocate the partition structure */ 363 slave = kzalloc(sizeof(*slave), GFP_KERNEL); 364 name = kstrdup(part->name, GFP_KERNEL); 365 if (!name || !slave) { 366 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n", 367 master->name); 368 kfree(name); 369 kfree(slave); 370 return ERR_PTR(-ENOMEM); 371 } 372 373 /* set up the MTD object for this partition */ 374 slave->mtd.type = master->type; 375 slave->mtd.flags = master->flags & ~part->mask_flags; 376 slave->mtd.size = part->size; 377 slave->mtd.writesize = master->writesize; 378 slave->mtd.writebufsize = master->writebufsize; 379 slave->mtd.oobsize = master->oobsize; 380 slave->mtd.oobavail = master->oobavail; 381 slave->mtd.subpage_sft = master->subpage_sft; 382 383 slave->mtd.name = name; 384 slave->mtd.owner = master->owner; 385 386 /* NOTE: Historically, we didn't arrange MTDs as a tree out of 387 * concern for showing the same data in multiple partitions. 388 * However, it is very useful to have the master node present, 389 * so the MTD_PARTITIONED_MASTER option allows that. The master 390 * will have device nodes etc only if this is set, so make the 391 * parent conditional on that option. Note, this is a way to 392 * distinguish between the master and the partition in sysfs. 393 */ 394 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) ? 395 &master->dev : 396 master->dev.parent; 397 398 slave->mtd._read = part_read; 399 slave->mtd._write = part_write; 400 401 if (master->_panic_write) 402 slave->mtd._panic_write = part_panic_write; 403 404 if (master->_point && master->_unpoint) { 405 slave->mtd._point = part_point; 406 slave->mtd._unpoint = part_unpoint; 407 } 408 409 if (master->_get_unmapped_area) 410 slave->mtd._get_unmapped_area = part_get_unmapped_area; 411 if (master->_read_oob) 412 slave->mtd._read_oob = part_read_oob; 413 if (master->_write_oob) 414 slave->mtd._write_oob = part_write_oob; 415 if (master->_read_user_prot_reg) 416 slave->mtd._read_user_prot_reg = part_read_user_prot_reg; 417 if (master->_read_fact_prot_reg) 418 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg; 419 if (master->_write_user_prot_reg) 420 slave->mtd._write_user_prot_reg = part_write_user_prot_reg; 421 if (master->_lock_user_prot_reg) 422 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg; 423 if (master->_get_user_prot_info) 424 slave->mtd._get_user_prot_info = part_get_user_prot_info; 425 if (master->_get_fact_prot_info) 426 slave->mtd._get_fact_prot_info = part_get_fact_prot_info; 427 if (master->_sync) 428 slave->mtd._sync = part_sync; 429 if (!partno && !master->dev.class && master->_suspend && 430 master->_resume) { 431 slave->mtd._suspend = part_suspend; 432 slave->mtd._resume = part_resume; 433 } 434 if (master->_writev) 435 slave->mtd._writev = part_writev; 436 if (master->_lock) 437 slave->mtd._lock = part_lock; 438 if (master->_unlock) 439 slave->mtd._unlock = part_unlock; 440 if (master->_is_locked) 441 slave->mtd._is_locked = part_is_locked; 442 if (master->_block_isreserved) 443 slave->mtd._block_isreserved = part_block_isreserved; 444 if (master->_block_isbad) 445 slave->mtd._block_isbad = part_block_isbad; 446 if (master->_block_markbad) 447 slave->mtd._block_markbad = part_block_markbad; 448 slave->mtd._erase = part_erase; 449 slave->master = master; 450 slave->offset = part->offset; 451 452 if (slave->offset == MTDPART_OFS_APPEND) 453 slave->offset = cur_offset; 454 if (slave->offset == MTDPART_OFS_NXTBLK) { 455 slave->offset = cur_offset; 456 if (mtd_mod_by_eb(cur_offset, master) != 0) { 457 /* Round up to next erasesize */ 458 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize; 459 printk(KERN_NOTICE "Moving partition %d: " 460 "0x%012llx -> 0x%012llx\n", partno, 461 (unsigned long long)cur_offset, (unsigned long long)slave->offset); 462 } 463 } 464 if (slave->offset == MTDPART_OFS_RETAIN) { 465 slave->offset = cur_offset; 466 if (master->size - slave->offset >= slave->mtd.size) { 467 slave->mtd.size = master->size - slave->offset 468 - slave->mtd.size; 469 } else { 470 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n", 471 part->name, master->size - slave->offset, 472 slave->mtd.size); 473 /* register to preserve ordering */ 474 goto out_register; 475 } 476 } 477 if (slave->mtd.size == MTDPART_SIZ_FULL) 478 slave->mtd.size = master->size - slave->offset; 479 480 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset, 481 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name); 482 483 /* let's do some sanity checks */ 484 if (slave->offset >= master->size) { 485 /* let's register it anyway to preserve ordering */ 486 slave->offset = 0; 487 slave->mtd.size = 0; 488 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n", 489 part->name); 490 goto out_register; 491 } 492 if (slave->offset + slave->mtd.size > master->size) { 493 slave->mtd.size = master->size - slave->offset; 494 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n", 495 part->name, master->name, (unsigned long long)slave->mtd.size); 496 } 497 if (master->numeraseregions > 1) { 498 /* Deal with variable erase size stuff */ 499 int i, max = master->numeraseregions; 500 u64 end = slave->offset + slave->mtd.size; 501 struct mtd_erase_region_info *regions = master->eraseregions; 502 503 /* Find the first erase regions which is part of this 504 * partition. */ 505 for (i = 0; i < max && regions[i].offset <= slave->offset; i++) 506 ; 507 /* The loop searched for the region _behind_ the first one */ 508 if (i > 0) 509 i--; 510 511 /* Pick biggest erasesize */ 512 for (; i < max && regions[i].offset < end; i++) { 513 if (slave->mtd.erasesize < regions[i].erasesize) { 514 slave->mtd.erasesize = regions[i].erasesize; 515 } 516 } 517 BUG_ON(slave->mtd.erasesize == 0); 518 } else { 519 /* Single erase size */ 520 slave->mtd.erasesize = master->erasesize; 521 } 522 523 if ((slave->mtd.flags & MTD_WRITEABLE) && 524 mtd_mod_by_eb(slave->offset, &slave->mtd)) { 525 /* Doesn't start on a boundary of major erase size */ 526 /* FIXME: Let it be writable if it is on a boundary of 527 * _minor_ erase size though */ 528 slave->mtd.flags &= ~MTD_WRITEABLE; 529 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n", 530 part->name); 531 } 532 if ((slave->mtd.flags & MTD_WRITEABLE) && 533 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) { 534 slave->mtd.flags &= ~MTD_WRITEABLE; 535 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n", 536 part->name); 537 } 538 539 slave->mtd.ecclayout = master->ecclayout; 540 slave->mtd.ecc_step_size = master->ecc_step_size; 541 slave->mtd.ecc_strength = master->ecc_strength; 542 slave->mtd.bitflip_threshold = master->bitflip_threshold; 543 544 if (master->_block_isbad) { 545 uint64_t offs = 0; 546 547 while (offs < slave->mtd.size) { 548 if (mtd_block_isreserved(master, offs + slave->offset)) 549 slave->mtd.ecc_stats.bbtblocks++; 550 else if (mtd_block_isbad(master, offs + slave->offset)) 551 slave->mtd.ecc_stats.badblocks++; 552 offs += slave->mtd.erasesize; 553 } 554 } 555 556 out_register: 557 return slave; 558 } 559 560 static ssize_t mtd_partition_offset_show(struct device *dev, 561 struct device_attribute *attr, char *buf) 562 { 563 struct mtd_info *mtd = dev_get_drvdata(dev); 564 struct mtd_part *part = mtd_to_part(mtd); 565 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset); 566 } 567 568 static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL); 569 570 static const struct attribute *mtd_partition_attrs[] = { 571 &dev_attr_offset.attr, 572 NULL 573 }; 574 575 static int mtd_add_partition_attrs(struct mtd_part *new) 576 { 577 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs); 578 if (ret) 579 printk(KERN_WARNING 580 "mtd: failed to create partition attrs, err=%d\n", ret); 581 return ret; 582 } 583 584 int mtd_add_partition(struct mtd_info *master, const char *name, 585 long long offset, long long length) 586 { 587 struct mtd_partition part; 588 struct mtd_part *new; 589 int ret = 0; 590 591 /* the direct offset is expected */ 592 if (offset == MTDPART_OFS_APPEND || 593 offset == MTDPART_OFS_NXTBLK) 594 return -EINVAL; 595 596 if (length == MTDPART_SIZ_FULL) 597 length = master->size - offset; 598 599 if (length <= 0) 600 return -EINVAL; 601 602 memset(&part, 0, sizeof(part)); 603 part.name = name; 604 part.size = length; 605 part.offset = offset; 606 607 new = allocate_partition(master, &part, -1, offset); 608 if (IS_ERR(new)) 609 return PTR_ERR(new); 610 611 mutex_lock(&mtd_partitions_mutex); 612 list_add(&new->list, &mtd_partitions); 613 mutex_unlock(&mtd_partitions_mutex); 614 615 add_mtd_device(&new->mtd); 616 617 mtd_add_partition_attrs(new); 618 619 return ret; 620 } 621 EXPORT_SYMBOL_GPL(mtd_add_partition); 622 623 int mtd_del_partition(struct mtd_info *master, int partno) 624 { 625 struct mtd_part *slave, *next; 626 int ret = -EINVAL; 627 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 sysfs_remove_files(&slave->mtd.dev.kobj, 633 mtd_partition_attrs); 634 ret = del_mtd_device(&slave->mtd); 635 if (ret < 0) 636 break; 637 638 list_del(&slave->list); 639 free_partition(slave); 640 break; 641 } 642 mutex_unlock(&mtd_partitions_mutex); 643 644 return ret; 645 } 646 EXPORT_SYMBOL_GPL(mtd_del_partition); 647 648 /* 649 * This function, given a master MTD object and a partition table, creates 650 * and registers slave MTD objects which are bound to the master according to 651 * the partition definitions. 652 * 653 * For historical reasons, this function's caller only registers the master 654 * if the MTD_PARTITIONED_MASTER config option is set. 655 */ 656 657 int add_mtd_partitions(struct mtd_info *master, 658 const struct mtd_partition *parts, 659 int nbparts) 660 { 661 struct mtd_part *slave; 662 uint64_t cur_offset = 0; 663 int i; 664 665 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name); 666 667 for (i = 0; i < nbparts; i++) { 668 slave = allocate_partition(master, parts + i, i, cur_offset); 669 if (IS_ERR(slave)) { 670 del_mtd_partitions(master); 671 return PTR_ERR(slave); 672 } 673 674 mutex_lock(&mtd_partitions_mutex); 675 list_add(&slave->list, &mtd_partitions); 676 mutex_unlock(&mtd_partitions_mutex); 677 678 add_mtd_device(&slave->mtd); 679 mtd_add_partition_attrs(slave); 680 681 cur_offset = slave->offset + slave->mtd.size; 682 } 683 684 return 0; 685 } 686 687 static DEFINE_SPINLOCK(part_parser_lock); 688 static LIST_HEAD(part_parsers); 689 690 static struct mtd_part_parser *mtd_part_parser_get(const char *name) 691 { 692 struct mtd_part_parser *p, *ret = NULL; 693 694 spin_lock(&part_parser_lock); 695 696 list_for_each_entry(p, &part_parsers, list) 697 if (!strcmp(p->name, name) && try_module_get(p->owner)) { 698 ret = p; 699 break; 700 } 701 702 spin_unlock(&part_parser_lock); 703 704 return ret; 705 } 706 707 static inline void mtd_part_parser_put(const struct mtd_part_parser *p) 708 { 709 module_put(p->owner); 710 } 711 712 /* 713 * Many partition parsers just expected the core to kfree() all their data in 714 * one chunk. Do that by default. 715 */ 716 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts, 717 int nr_parts) 718 { 719 kfree(pparts); 720 } 721 722 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner) 723 { 724 p->owner = owner; 725 726 if (!p->cleanup) 727 p->cleanup = &mtd_part_parser_cleanup_default; 728 729 spin_lock(&part_parser_lock); 730 list_add(&p->list, &part_parsers); 731 spin_unlock(&part_parser_lock); 732 733 return 0; 734 } 735 EXPORT_SYMBOL_GPL(__register_mtd_parser); 736 737 void deregister_mtd_parser(struct mtd_part_parser *p) 738 { 739 spin_lock(&part_parser_lock); 740 list_del(&p->list); 741 spin_unlock(&part_parser_lock); 742 } 743 EXPORT_SYMBOL_GPL(deregister_mtd_parser); 744 745 /* 746 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you 747 * are changing this array! 748 */ 749 static const char * const default_mtd_part_types[] = { 750 "cmdlinepart", 751 "ofpart", 752 NULL 753 }; 754 755 /** 756 * parse_mtd_partitions - parse MTD partitions 757 * @master: the master partition (describes whole MTD device) 758 * @types: names of partition parsers to try or %NULL 759 * @pparts: info about partitions found is returned here 760 * @data: MTD partition parser-specific data 761 * 762 * This function tries to find partition on MTD device @master. It uses MTD 763 * partition parsers, specified in @types. However, if @types is %NULL, then 764 * the default list of parsers is used. The default list contains only the 765 * "cmdlinepart" and "ofpart" parsers ATM. 766 * Note: If there are more then one parser in @types, the kernel only takes the 767 * partitions parsed out by the first parser. 768 * 769 * This function may return: 770 * o a negative error code in case of failure 771 * o zero otherwise, and @pparts will describe the partitions, number of 772 * partitions, and the parser which parsed them. Caller must release 773 * resources with mtd_part_parser_cleanup() when finished with the returned 774 * data. 775 */ 776 int parse_mtd_partitions(struct mtd_info *master, const char *const *types, 777 struct mtd_partitions *pparts, 778 struct mtd_part_parser_data *data) 779 { 780 struct mtd_part_parser *parser; 781 int ret, err = 0; 782 783 if (!types) 784 types = default_mtd_part_types; 785 786 for ( ; *types; types++) { 787 pr_debug("%s: parsing partitions %s\n", master->name, *types); 788 parser = mtd_part_parser_get(*types); 789 if (!parser && !request_module("%s", *types)) 790 parser = mtd_part_parser_get(*types); 791 pr_debug("%s: got parser %s\n", master->name, 792 parser ? parser->name : NULL); 793 if (!parser) 794 continue; 795 ret = (*parser->parse_fn)(master, &pparts->parts, data); 796 pr_debug("%s: parser %s: %i\n", 797 master->name, parser->name, ret); 798 if (ret > 0) { 799 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n", 800 ret, parser->name, master->name); 801 pparts->nr_parts = ret; 802 pparts->parser = parser; 803 return 0; 804 } 805 mtd_part_parser_put(parser); 806 /* 807 * Stash the first error we see; only report it if no parser 808 * succeeds 809 */ 810 if (ret < 0 && !err) 811 err = ret; 812 } 813 return err; 814 } 815 816 void mtd_part_parser_cleanup(struct mtd_partitions *parts) 817 { 818 const struct mtd_part_parser *parser; 819 820 if (!parts) 821 return; 822 823 parser = parts->parser; 824 if (parser) { 825 if (parser->cleanup) 826 parser->cleanup(parts->parts, parts->nr_parts); 827 828 mtd_part_parser_put(parser); 829 } 830 } 831 832 int mtd_is_partition(const struct mtd_info *mtd) 833 { 834 struct mtd_part *part; 835 int ispart = 0; 836 837 mutex_lock(&mtd_partitions_mutex); 838 list_for_each_entry(part, &mtd_partitions, list) 839 if (&part->mtd == mtd) { 840 ispart = 1; 841 break; 842 } 843 mutex_unlock(&mtd_partitions_mutex); 844 845 return ispart; 846 } 847 EXPORT_SYMBOL_GPL(mtd_is_partition); 848 849 /* Returns the size of the entire flash chip */ 850 uint64_t mtd_get_device_size(const struct mtd_info *mtd) 851 { 852 if (!mtd_is_partition(mtd)) 853 return mtd->size; 854 855 return mtd_to_part(mtd)->master->size; 856 } 857 EXPORT_SYMBOL_GPL(mtd_get_device_size); 858