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