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/of.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 /** 42 * struct mtd_part - our partition node structure 43 * 44 * @mtd: struct holding partition details 45 * @parent: parent mtd - flash device or another partition 46 * @offset: partition offset relative to the *flash device* 47 */ 48 struct mtd_part { 49 struct mtd_info mtd; 50 struct mtd_info *parent; 51 uint64_t offset; 52 struct list_head list; 53 }; 54 55 /* 56 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve 57 * the pointer to that structure. 58 */ 59 static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd) 60 { 61 return container_of(mtd, struct mtd_part, mtd); 62 } 63 64 65 /* 66 * MTD methods which simply translate the effective address and pass through 67 * to the _real_ device. 68 */ 69 70 static int part_read(struct mtd_info *mtd, loff_t from, size_t len, 71 size_t *retlen, u_char *buf) 72 { 73 struct mtd_part *part = mtd_to_part(mtd); 74 struct mtd_ecc_stats stats; 75 int res; 76 77 stats = part->parent->ecc_stats; 78 res = part->parent->_read(part->parent, from + part->offset, len, 79 retlen, buf); 80 if (unlikely(mtd_is_eccerr(res))) 81 mtd->ecc_stats.failed += 82 part->parent->ecc_stats.failed - stats.failed; 83 else 84 mtd->ecc_stats.corrected += 85 part->parent->ecc_stats.corrected - stats.corrected; 86 return res; 87 } 88 89 static int part_point(struct mtd_info *mtd, loff_t from, size_t len, 90 size_t *retlen, void **virt, resource_size_t *phys) 91 { 92 struct mtd_part *part = mtd_to_part(mtd); 93 94 return part->parent->_point(part->parent, from + part->offset, len, 95 retlen, virt, phys); 96 } 97 98 static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len) 99 { 100 struct mtd_part *part = mtd_to_part(mtd); 101 102 return part->parent->_unpoint(part->parent, from + part->offset, len); 103 } 104 105 static int part_read_oob(struct mtd_info *mtd, loff_t from, 106 struct mtd_oob_ops *ops) 107 { 108 struct mtd_part *part = mtd_to_part(mtd); 109 struct mtd_ecc_stats stats; 110 int res; 111 112 stats = part->parent->ecc_stats; 113 res = part->parent->_read_oob(part->parent, from + part->offset, ops); 114 if (unlikely(mtd_is_eccerr(res))) 115 mtd->ecc_stats.failed += 116 part->parent->ecc_stats.failed - stats.failed; 117 else 118 mtd->ecc_stats.corrected += 119 part->parent->ecc_stats.corrected - stats.corrected; 120 return res; 121 } 122 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) 125 { 126 struct mtd_part *part = mtd_to_part(mtd); 127 return part->parent->_read_user_prot_reg(part->parent, from, len, 128 retlen, buf); 129 } 130 131 static int part_get_user_prot_info(struct mtd_info *mtd, size_t len, 132 size_t *retlen, struct otp_info *buf) 133 { 134 struct mtd_part *part = mtd_to_part(mtd); 135 return part->parent->_get_user_prot_info(part->parent, len, retlen, 136 buf); 137 } 138 139 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, 140 size_t len, size_t *retlen, u_char *buf) 141 { 142 struct mtd_part *part = mtd_to_part(mtd); 143 return part->parent->_read_fact_prot_reg(part->parent, from, len, 144 retlen, buf); 145 } 146 147 static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len, 148 size_t *retlen, struct otp_info *buf) 149 { 150 struct mtd_part *part = mtd_to_part(mtd); 151 return part->parent->_get_fact_prot_info(part->parent, len, retlen, 152 buf); 153 } 154 155 static int part_write(struct mtd_info *mtd, loff_t to, size_t len, 156 size_t *retlen, const u_char *buf) 157 { 158 struct mtd_part *part = mtd_to_part(mtd); 159 return part->parent->_write(part->parent, to + part->offset, len, 160 retlen, buf); 161 } 162 163 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len, 164 size_t *retlen, const u_char *buf) 165 { 166 struct mtd_part *part = mtd_to_part(mtd); 167 return part->parent->_panic_write(part->parent, to + part->offset, len, 168 retlen, buf); 169 } 170 171 static int part_write_oob(struct mtd_info *mtd, loff_t to, 172 struct mtd_oob_ops *ops) 173 { 174 struct mtd_part *part = mtd_to_part(mtd); 175 176 return part->parent->_write_oob(part->parent, to + part->offset, ops); 177 } 178 179 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from, 180 size_t len, size_t *retlen, u_char *buf) 181 { 182 struct mtd_part *part = mtd_to_part(mtd); 183 return part->parent->_write_user_prot_reg(part->parent, from, len, 184 retlen, buf); 185 } 186 187 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, 188 size_t len) 189 { 190 struct mtd_part *part = mtd_to_part(mtd); 191 return part->parent->_lock_user_prot_reg(part->parent, from, len); 192 } 193 194 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs, 195 unsigned long count, loff_t to, size_t *retlen) 196 { 197 struct mtd_part *part = mtd_to_part(mtd); 198 return part->parent->_writev(part->parent, vecs, count, 199 to + part->offset, retlen); 200 } 201 202 static int part_erase(struct mtd_info *mtd, struct erase_info *instr) 203 { 204 struct mtd_part *part = mtd_to_part(mtd); 205 int ret; 206 207 instr->addr += part->offset; 208 ret = part->parent->_erase(part->parent, instr); 209 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) 210 instr->fail_addr -= part->offset; 211 instr->addr -= part->offset; 212 213 return ret; 214 } 215 216 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 217 { 218 struct mtd_part *part = mtd_to_part(mtd); 219 return part->parent->_lock(part->parent, ofs + part->offset, len); 220 } 221 222 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 223 { 224 struct mtd_part *part = mtd_to_part(mtd); 225 return part->parent->_unlock(part->parent, ofs + part->offset, len); 226 } 227 228 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) 229 { 230 struct mtd_part *part = mtd_to_part(mtd); 231 return part->parent->_is_locked(part->parent, ofs + part->offset, len); 232 } 233 234 static void part_sync(struct mtd_info *mtd) 235 { 236 struct mtd_part *part = mtd_to_part(mtd); 237 part->parent->_sync(part->parent); 238 } 239 240 static int part_suspend(struct mtd_info *mtd) 241 { 242 struct mtd_part *part = mtd_to_part(mtd); 243 return part->parent->_suspend(part->parent); 244 } 245 246 static void part_resume(struct mtd_info *mtd) 247 { 248 struct mtd_part *part = mtd_to_part(mtd); 249 part->parent->_resume(part->parent); 250 } 251 252 static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs) 253 { 254 struct mtd_part *part = mtd_to_part(mtd); 255 ofs += part->offset; 256 return part->parent->_block_isreserved(part->parent, ofs); 257 } 258 259 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs) 260 { 261 struct mtd_part *part = mtd_to_part(mtd); 262 ofs += part->offset; 263 return part->parent->_block_isbad(part->parent, ofs); 264 } 265 266 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs) 267 { 268 struct mtd_part *part = mtd_to_part(mtd); 269 int res; 270 271 ofs += part->offset; 272 res = part->parent->_block_markbad(part->parent, ofs); 273 if (!res) 274 mtd->ecc_stats.badblocks++; 275 return res; 276 } 277 278 static int part_get_device(struct mtd_info *mtd) 279 { 280 struct mtd_part *part = mtd_to_part(mtd); 281 return part->parent->_get_device(part->parent); 282 } 283 284 static void part_put_device(struct mtd_info *mtd) 285 { 286 struct mtd_part *part = mtd_to_part(mtd); 287 part->parent->_put_device(part->parent); 288 } 289 290 static int part_ooblayout_ecc(struct mtd_info *mtd, int section, 291 struct mtd_oob_region *oobregion) 292 { 293 struct mtd_part *part = mtd_to_part(mtd); 294 295 return mtd_ooblayout_ecc(part->parent, section, oobregion); 296 } 297 298 static int part_ooblayout_free(struct mtd_info *mtd, int section, 299 struct mtd_oob_region *oobregion) 300 { 301 struct mtd_part *part = mtd_to_part(mtd); 302 303 return mtd_ooblayout_free(part->parent, section, oobregion); 304 } 305 306 static const struct mtd_ooblayout_ops part_ooblayout_ops = { 307 .ecc = part_ooblayout_ecc, 308 .free = part_ooblayout_free, 309 }; 310 311 static int part_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len) 312 { 313 struct mtd_part *part = mtd_to_part(mtd); 314 315 return part->parent->_max_bad_blocks(part->parent, 316 ofs + part->offset, len); 317 } 318 319 static inline void free_partition(struct mtd_part *p) 320 { 321 kfree(p->mtd.name); 322 kfree(p); 323 } 324 325 static struct mtd_part *allocate_partition(struct mtd_info *parent, 326 const struct mtd_partition *part, int partno, 327 uint64_t cur_offset) 328 { 329 int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize : 330 parent->erasesize; 331 struct mtd_part *slave; 332 u32 remainder; 333 char *name; 334 u64 tmp; 335 336 /* allocate the partition structure */ 337 slave = kzalloc(sizeof(*slave), GFP_KERNEL); 338 name = kstrdup(part->name, GFP_KERNEL); 339 if (!name || !slave) { 340 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n", 341 parent->name); 342 kfree(name); 343 kfree(slave); 344 return ERR_PTR(-ENOMEM); 345 } 346 347 /* set up the MTD object for this partition */ 348 slave->mtd.type = parent->type; 349 slave->mtd.flags = parent->flags & ~part->mask_flags; 350 slave->mtd.size = part->size; 351 slave->mtd.writesize = parent->writesize; 352 slave->mtd.writebufsize = parent->writebufsize; 353 slave->mtd.oobsize = parent->oobsize; 354 slave->mtd.oobavail = parent->oobavail; 355 slave->mtd.subpage_sft = parent->subpage_sft; 356 slave->mtd.pairing = parent->pairing; 357 358 slave->mtd.name = name; 359 slave->mtd.owner = parent->owner; 360 361 /* NOTE: Historically, we didn't arrange MTDs as a tree out of 362 * concern for showing the same data in multiple partitions. 363 * However, it is very useful to have the master node present, 364 * so the MTD_PARTITIONED_MASTER option allows that. The master 365 * will have device nodes etc only if this is set, so make the 366 * parent conditional on that option. Note, this is a way to 367 * distinguish between the master and the partition in sysfs. 368 */ 369 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ? 370 &parent->dev : 371 parent->dev.parent; 372 slave->mtd.dev.of_node = part->of_node; 373 374 if (parent->_read) 375 slave->mtd._read = part_read; 376 if (parent->_write) 377 slave->mtd._write = part_write; 378 379 if (parent->_panic_write) 380 slave->mtd._panic_write = part_panic_write; 381 382 if (parent->_point && parent->_unpoint) { 383 slave->mtd._point = part_point; 384 slave->mtd._unpoint = part_unpoint; 385 } 386 387 if (parent->_read_oob) 388 slave->mtd._read_oob = part_read_oob; 389 if (parent->_write_oob) 390 slave->mtd._write_oob = part_write_oob; 391 if (parent->_read_user_prot_reg) 392 slave->mtd._read_user_prot_reg = part_read_user_prot_reg; 393 if (parent->_read_fact_prot_reg) 394 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg; 395 if (parent->_write_user_prot_reg) 396 slave->mtd._write_user_prot_reg = part_write_user_prot_reg; 397 if (parent->_lock_user_prot_reg) 398 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg; 399 if (parent->_get_user_prot_info) 400 slave->mtd._get_user_prot_info = part_get_user_prot_info; 401 if (parent->_get_fact_prot_info) 402 slave->mtd._get_fact_prot_info = part_get_fact_prot_info; 403 if (parent->_sync) 404 slave->mtd._sync = part_sync; 405 if (!partno && !parent->dev.class && parent->_suspend && 406 parent->_resume) { 407 slave->mtd._suspend = part_suspend; 408 slave->mtd._resume = part_resume; 409 } 410 if (parent->_writev) 411 slave->mtd._writev = part_writev; 412 if (parent->_lock) 413 slave->mtd._lock = part_lock; 414 if (parent->_unlock) 415 slave->mtd._unlock = part_unlock; 416 if (parent->_is_locked) 417 slave->mtd._is_locked = part_is_locked; 418 if (parent->_block_isreserved) 419 slave->mtd._block_isreserved = part_block_isreserved; 420 if (parent->_block_isbad) 421 slave->mtd._block_isbad = part_block_isbad; 422 if (parent->_block_markbad) 423 slave->mtd._block_markbad = part_block_markbad; 424 if (parent->_max_bad_blocks) 425 slave->mtd._max_bad_blocks = part_max_bad_blocks; 426 427 if (parent->_get_device) 428 slave->mtd._get_device = part_get_device; 429 if (parent->_put_device) 430 slave->mtd._put_device = part_put_device; 431 432 slave->mtd._erase = part_erase; 433 slave->parent = parent; 434 slave->offset = part->offset; 435 436 if (slave->offset == MTDPART_OFS_APPEND) 437 slave->offset = cur_offset; 438 if (slave->offset == MTDPART_OFS_NXTBLK) { 439 tmp = cur_offset; 440 slave->offset = cur_offset; 441 remainder = do_div(tmp, wr_alignment); 442 if (remainder) { 443 slave->offset += wr_alignment - remainder; 444 printk(KERN_NOTICE "Moving partition %d: " 445 "0x%012llx -> 0x%012llx\n", partno, 446 (unsigned long long)cur_offset, (unsigned long long)slave->offset); 447 } 448 } 449 if (slave->offset == MTDPART_OFS_RETAIN) { 450 slave->offset = cur_offset; 451 if (parent->size - slave->offset >= slave->mtd.size) { 452 slave->mtd.size = parent->size - slave->offset 453 - slave->mtd.size; 454 } else { 455 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n", 456 part->name, parent->size - slave->offset, 457 slave->mtd.size); 458 /* register to preserve ordering */ 459 goto out_register; 460 } 461 } 462 if (slave->mtd.size == MTDPART_SIZ_FULL) 463 slave->mtd.size = parent->size - slave->offset; 464 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); 467 468 /* let's do some sanity checks */ 469 if (slave->offset >= parent->size) { 470 /* let's register it anyway to preserve ordering */ 471 slave->offset = 0; 472 slave->mtd.size = 0; 473 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n", 474 part->name); 475 goto out_register; 476 } 477 if (slave->offset + slave->mtd.size > parent->size) { 478 slave->mtd.size = parent->size - slave->offset; 479 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n", 480 part->name, parent->name, (unsigned long long)slave->mtd.size); 481 } 482 if (parent->numeraseregions > 1) { 483 /* Deal with variable erase size stuff */ 484 int i, max = parent->numeraseregions; 485 u64 end = slave->offset + slave->mtd.size; 486 struct mtd_erase_region_info *regions = parent->eraseregions; 487 488 /* Find the first erase regions which is part of this 489 * partition. */ 490 for (i = 0; i < max && regions[i].offset <= slave->offset; i++) 491 ; 492 /* The loop searched for the region _behind_ the first one */ 493 if (i > 0) 494 i--; 495 496 /* Pick biggest erasesize */ 497 for (; i < max && regions[i].offset < end; i++) { 498 if (slave->mtd.erasesize < regions[i].erasesize) { 499 slave->mtd.erasesize = regions[i].erasesize; 500 } 501 } 502 BUG_ON(slave->mtd.erasesize == 0); 503 } else { 504 /* Single erase size */ 505 slave->mtd.erasesize = parent->erasesize; 506 } 507 508 /* 509 * Slave erasesize might differ from the master one if the master 510 * exposes several regions with different erasesize. Adjust 511 * wr_alignment accordingly. 512 */ 513 if (!(slave->mtd.flags & MTD_NO_ERASE)) 514 wr_alignment = slave->mtd.erasesize; 515 516 tmp = slave->offset; 517 remainder = do_div(tmp, wr_alignment); 518 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) { 519 /* Doesn't start on a boundary of major erase size */ 520 /* FIXME: Let it be writable if it is on a boundary of 521 * _minor_ erase size though */ 522 slave->mtd.flags &= ~MTD_WRITEABLE; 523 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n", 524 part->name); 525 } 526 527 tmp = slave->mtd.size; 528 remainder = do_div(tmp, wr_alignment); 529 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) { 530 slave->mtd.flags &= ~MTD_WRITEABLE; 531 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n", 532 part->name); 533 } 534 535 mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops); 536 slave->mtd.ecc_step_size = parent->ecc_step_size; 537 slave->mtd.ecc_strength = parent->ecc_strength; 538 slave->mtd.bitflip_threshold = parent->bitflip_threshold; 539 540 if (parent->_block_isbad) { 541 uint64_t offs = 0; 542 543 while (offs < slave->mtd.size) { 544 if (mtd_block_isreserved(parent, offs + slave->offset)) 545 slave->mtd.ecc_stats.bbtblocks++; 546 else if (mtd_block_isbad(parent, offs + slave->offset)) 547 slave->mtd.ecc_stats.badblocks++; 548 offs += slave->mtd.erasesize; 549 } 550 } 551 552 out_register: 553 return slave; 554 } 555 556 static ssize_t mtd_partition_offset_show(struct device *dev, 557 struct device_attribute *attr, char *buf) 558 { 559 struct mtd_info *mtd = dev_get_drvdata(dev); 560 struct mtd_part *part = mtd_to_part(mtd); 561 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset); 562 } 563 564 static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL); 565 566 static const struct attribute *mtd_partition_attrs[] = { 567 &dev_attr_offset.attr, 568 NULL 569 }; 570 571 static int mtd_add_partition_attrs(struct mtd_part *new) 572 { 573 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs); 574 if (ret) 575 printk(KERN_WARNING 576 "mtd: failed to create partition attrs, err=%d\n", ret); 577 return ret; 578 } 579 580 int mtd_add_partition(struct mtd_info *parent, const char *name, 581 long long offset, long long length) 582 { 583 struct mtd_partition part; 584 struct mtd_part *new; 585 int ret = 0; 586 587 /* the direct offset is expected */ 588 if (offset == MTDPART_OFS_APPEND || 589 offset == MTDPART_OFS_NXTBLK) 590 return -EINVAL; 591 592 if (length == MTDPART_SIZ_FULL) 593 length = parent->size - offset; 594 595 if (length <= 0) 596 return -EINVAL; 597 598 memset(&part, 0, sizeof(part)); 599 part.name = name; 600 part.size = length; 601 part.offset = offset; 602 603 new = allocate_partition(parent, &part, -1, offset); 604 if (IS_ERR(new)) 605 return PTR_ERR(new); 606 607 mutex_lock(&mtd_partitions_mutex); 608 list_add(&new->list, &mtd_partitions); 609 mutex_unlock(&mtd_partitions_mutex); 610 611 add_mtd_device(&new->mtd); 612 613 mtd_add_partition_attrs(new); 614 615 return ret; 616 } 617 EXPORT_SYMBOL_GPL(mtd_add_partition); 618 619 /** 620 * __mtd_del_partition - delete MTD partition 621 * 622 * @priv: internal MTD struct for partition to be deleted 623 * 624 * This function must be called with the partitions mutex locked. 625 */ 626 static int __mtd_del_partition(struct mtd_part *priv) 627 { 628 struct mtd_part *child, *next; 629 int err; 630 631 list_for_each_entry_safe(child, next, &mtd_partitions, list) { 632 if (child->parent == &priv->mtd) { 633 err = __mtd_del_partition(child); 634 if (err) 635 return err; 636 } 637 } 638 639 sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs); 640 641 err = del_mtd_device(&priv->mtd); 642 if (err) 643 return err; 644 645 list_del(&priv->list); 646 free_partition(priv); 647 648 return 0; 649 } 650 651 /* 652 * This function unregisters and destroy all slave MTD objects which are 653 * attached to the given MTD object. 654 */ 655 int del_mtd_partitions(struct mtd_info *mtd) 656 { 657 struct mtd_part *slave, *next; 658 int ret, err = 0; 659 660 mutex_lock(&mtd_partitions_mutex); 661 list_for_each_entry_safe(slave, next, &mtd_partitions, list) 662 if (slave->parent == mtd) { 663 ret = __mtd_del_partition(slave); 664 if (ret < 0) 665 err = ret; 666 } 667 mutex_unlock(&mtd_partitions_mutex); 668 669 return err; 670 } 671 672 int mtd_del_partition(struct mtd_info *mtd, int partno) 673 { 674 struct mtd_part *slave, *next; 675 int ret = -EINVAL; 676 677 mutex_lock(&mtd_partitions_mutex); 678 list_for_each_entry_safe(slave, next, &mtd_partitions, list) 679 if ((slave->parent == mtd) && 680 (slave->mtd.index == partno)) { 681 ret = __mtd_del_partition(slave); 682 break; 683 } 684 mutex_unlock(&mtd_partitions_mutex); 685 686 return ret; 687 } 688 EXPORT_SYMBOL_GPL(mtd_del_partition); 689 690 /* 691 * This function, given a master MTD object and a partition table, creates 692 * and registers slave MTD objects which are bound to the master according to 693 * the partition definitions. 694 * 695 * For historical reasons, this function's caller only registers the master 696 * if the MTD_PARTITIONED_MASTER config option is set. 697 */ 698 699 int add_mtd_partitions(struct mtd_info *master, 700 const struct mtd_partition *parts, 701 int nbparts) 702 { 703 struct mtd_part *slave; 704 uint64_t cur_offset = 0; 705 int i; 706 707 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name); 708 709 for (i = 0; i < nbparts; i++) { 710 slave = allocate_partition(master, parts + i, i, cur_offset); 711 if (IS_ERR(slave)) { 712 del_mtd_partitions(master); 713 return PTR_ERR(slave); 714 } 715 716 mutex_lock(&mtd_partitions_mutex); 717 list_add(&slave->list, &mtd_partitions); 718 mutex_unlock(&mtd_partitions_mutex); 719 720 add_mtd_device(&slave->mtd); 721 mtd_add_partition_attrs(slave); 722 /* Look for subpartitions */ 723 parse_mtd_partitions(&slave->mtd, parts[i].types, NULL); 724 725 cur_offset = slave->offset + slave->mtd.size; 726 } 727 728 return 0; 729 } 730 731 static DEFINE_SPINLOCK(part_parser_lock); 732 static LIST_HEAD(part_parsers); 733 734 static struct mtd_part_parser *mtd_part_parser_get(const char *name) 735 { 736 struct mtd_part_parser *p, *ret = NULL; 737 738 spin_lock(&part_parser_lock); 739 740 list_for_each_entry(p, &part_parsers, list) 741 if (!strcmp(p->name, name) && try_module_get(p->owner)) { 742 ret = p; 743 break; 744 } 745 746 spin_unlock(&part_parser_lock); 747 748 return ret; 749 } 750 751 static inline void mtd_part_parser_put(const struct mtd_part_parser *p) 752 { 753 module_put(p->owner); 754 } 755 756 /* 757 * Many partition parsers just expected the core to kfree() all their data in 758 * one chunk. Do that by default. 759 */ 760 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts, 761 int nr_parts) 762 { 763 kfree(pparts); 764 } 765 766 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner) 767 { 768 p->owner = owner; 769 770 if (!p->cleanup) 771 p->cleanup = &mtd_part_parser_cleanup_default; 772 773 spin_lock(&part_parser_lock); 774 list_add(&p->list, &part_parsers); 775 spin_unlock(&part_parser_lock); 776 777 return 0; 778 } 779 EXPORT_SYMBOL_GPL(__register_mtd_parser); 780 781 void deregister_mtd_parser(struct mtd_part_parser *p) 782 { 783 spin_lock(&part_parser_lock); 784 list_del(&p->list); 785 spin_unlock(&part_parser_lock); 786 } 787 EXPORT_SYMBOL_GPL(deregister_mtd_parser); 788 789 /* 790 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you 791 * are changing this array! 792 */ 793 static const char * const default_mtd_part_types[] = { 794 "cmdlinepart", 795 "ofpart", 796 NULL 797 }; 798 799 /* Check DT only when looking for subpartitions. */ 800 static const char * const default_subpartition_types[] = { 801 "ofpart", 802 NULL 803 }; 804 805 static int mtd_part_do_parse(struct mtd_part_parser *parser, 806 struct mtd_info *master, 807 struct mtd_partitions *pparts, 808 struct mtd_part_parser_data *data) 809 { 810 int ret; 811 812 ret = (*parser->parse_fn)(master, &pparts->parts, data); 813 pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret); 814 if (ret <= 0) 815 return ret; 816 817 pr_notice("%d %s partitions found on MTD device %s\n", ret, 818 parser->name, master->name); 819 820 pparts->nr_parts = ret; 821 pparts->parser = parser; 822 823 return ret; 824 } 825 826 /** 827 * mtd_part_get_compatible_parser - find MTD parser by a compatible string 828 * 829 * @compat: compatible string describing partitions in a device tree 830 * 831 * MTD parsers can specify supported partitions by providing a table of 832 * compatibility strings. This function finds a parser that advertises support 833 * for a passed value of "compatible". 834 */ 835 static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat) 836 { 837 struct mtd_part_parser *p, *ret = NULL; 838 839 spin_lock(&part_parser_lock); 840 841 list_for_each_entry(p, &part_parsers, list) { 842 const struct of_device_id *matches; 843 844 matches = p->of_match_table; 845 if (!matches) 846 continue; 847 848 for (; matches->compatible[0]; matches++) { 849 if (!strcmp(matches->compatible, compat) && 850 try_module_get(p->owner)) { 851 ret = p; 852 break; 853 } 854 } 855 856 if (ret) 857 break; 858 } 859 860 spin_unlock(&part_parser_lock); 861 862 return ret; 863 } 864 865 static int mtd_part_of_parse(struct mtd_info *master, 866 struct mtd_partitions *pparts) 867 { 868 struct mtd_part_parser *parser; 869 struct device_node *np; 870 struct property *prop; 871 const char *compat; 872 const char *fixed = "fixed-partitions"; 873 int ret, err = 0; 874 875 np = mtd_get_of_node(master); 876 if (!mtd_is_partition(master)) 877 np = of_get_child_by_name(np, "partitions"); 878 of_property_for_each_string(np, "compatible", prop, compat) { 879 parser = mtd_part_get_compatible_parser(compat); 880 if (!parser) 881 continue; 882 ret = mtd_part_do_parse(parser, master, pparts, NULL); 883 if (ret > 0) { 884 of_node_put(np); 885 return ret; 886 } 887 mtd_part_parser_put(parser); 888 if (ret < 0 && !err) 889 err = ret; 890 } 891 of_node_put(np); 892 893 /* 894 * For backward compatibility we have to try the "fixed-partitions" 895 * parser. It supports old DT format with partitions specified as a 896 * direct subnodes of a flash device DT node without any compatibility 897 * specified we could match. 898 */ 899 parser = mtd_part_parser_get(fixed); 900 if (!parser && !request_module("%s", fixed)) 901 parser = mtd_part_parser_get(fixed); 902 if (parser) { 903 ret = mtd_part_do_parse(parser, master, pparts, NULL); 904 if (ret > 0) 905 return ret; 906 mtd_part_parser_put(parser); 907 if (ret < 0 && !err) 908 err = ret; 909 } 910 911 return err; 912 } 913 914 /** 915 * parse_mtd_partitions - parse and register MTD partitions 916 * 917 * @master: the master partition (describes whole MTD device) 918 * @types: names of partition parsers to try or %NULL 919 * @data: MTD partition parser-specific data 920 * 921 * This function tries to find & register partitions on MTD device @master. It 922 * uses MTD partition parsers, specified in @types. However, if @types is %NULL, 923 * then the default list of parsers is used. The default list contains only the 924 * "cmdlinepart" and "ofpart" parsers ATM. 925 * Note: If there are more then one parser in @types, the kernel only takes the 926 * partitions parsed out by the first parser. 927 * 928 * This function may return: 929 * o a negative error code in case of failure 930 * o number of found partitions otherwise 931 */ 932 int parse_mtd_partitions(struct mtd_info *master, const char *const *types, 933 struct mtd_part_parser_data *data) 934 { 935 struct mtd_partitions pparts = { }; 936 struct mtd_part_parser *parser; 937 int ret, err = 0; 938 939 if (!types) 940 types = mtd_is_partition(master) ? default_subpartition_types : 941 default_mtd_part_types; 942 943 for ( ; *types; types++) { 944 /* 945 * ofpart is a special type that means OF partitioning info 946 * should be used. It requires a bit different logic so it is 947 * handled in a separated function. 948 */ 949 if (!strcmp(*types, "ofpart")) { 950 ret = mtd_part_of_parse(master, &pparts); 951 } else { 952 pr_debug("%s: parsing partitions %s\n", master->name, 953 *types); 954 parser = mtd_part_parser_get(*types); 955 if (!parser && !request_module("%s", *types)) 956 parser = mtd_part_parser_get(*types); 957 pr_debug("%s: got parser %s\n", master->name, 958 parser ? parser->name : NULL); 959 if (!parser) 960 continue; 961 ret = mtd_part_do_parse(parser, master, &pparts, data); 962 if (ret <= 0) 963 mtd_part_parser_put(parser); 964 } 965 /* Found partitions! */ 966 if (ret > 0) { 967 err = add_mtd_partitions(master, pparts.parts, 968 pparts.nr_parts); 969 mtd_part_parser_cleanup(&pparts); 970 return err ? err : pparts.nr_parts; 971 } 972 /* 973 * Stash the first error we see; only report it if no parser 974 * succeeds 975 */ 976 if (ret < 0 && !err) 977 err = ret; 978 } 979 return err; 980 } 981 982 void mtd_part_parser_cleanup(struct mtd_partitions *parts) 983 { 984 const struct mtd_part_parser *parser; 985 986 if (!parts) 987 return; 988 989 parser = parts->parser; 990 if (parser) { 991 if (parser->cleanup) 992 parser->cleanup(parts->parts, parts->nr_parts); 993 994 mtd_part_parser_put(parser); 995 } 996 } 997 998 int mtd_is_partition(const struct mtd_info *mtd) 999 { 1000 struct mtd_part *part; 1001 int ispart = 0; 1002 1003 mutex_lock(&mtd_partitions_mutex); 1004 list_for_each_entry(part, &mtd_partitions, list) 1005 if (&part->mtd == mtd) { 1006 ispart = 1; 1007 break; 1008 } 1009 mutex_unlock(&mtd_partitions_mutex); 1010 1011 return ispart; 1012 } 1013 EXPORT_SYMBOL_GPL(mtd_is_partition); 1014 1015 /* Returns the size of the entire flash chip */ 1016 uint64_t mtd_get_device_size(const struct mtd_info *mtd) 1017 { 1018 if (!mtd_is_partition(mtd)) 1019 return mtd->size; 1020 1021 return mtd_get_device_size(mtd_to_part(mtd)->parent); 1022 } 1023 EXPORT_SYMBOL_GPL(mtd_get_device_size); 1024