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