Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Simple MTD partitioning layer |
| 3 | * |
| 4 | * (C) 2000 Nicolas Pitre <nico@cam.org> |
| 5 | * |
| 6 | * This code is GPL |
| 7 | * |
| 8 | * 02-21-2002 Thomas Gleixner <gleixner@autronix.de> |
| 9 | * added support for read_oob, write_oob |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <malloc.h> |
| 14 | #include <asm/errno.h> |
| 15 | |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/mtd/mtd.h> |
| 19 | #include <linux/mtd/partitions.h> |
Mike Frysinger | 7b15e2b | 2012-04-09 13:39:55 +0000 | [diff] [blame] | 20 | #include <linux/compat.h> |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 21 | |
| 22 | /* Our partition linked list */ |
Stefan Roese | 9def12c | 2008-11-27 14:05:15 +0100 | [diff] [blame] | 23 | struct list_head mtd_partitions; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 24 | |
| 25 | /* Our partition node structure */ |
| 26 | struct mtd_part { |
| 27 | struct mtd_info mtd; |
| 28 | struct mtd_info *master; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 29 | uint64_t offset; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 30 | int index; |
| 31 | struct list_head list; |
| 32 | int registered; |
| 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * Given a pointer to the MTD object in the mtd_part structure, we can retrieve |
| 37 | * the pointer to that structure with this macro. |
| 38 | */ |
| 39 | #define PART(x) ((struct mtd_part *)(x)) |
| 40 | |
| 41 | |
| 42 | /* |
| 43 | * MTD methods which simply translate the effective address and pass through |
| 44 | * to the _real_ device. |
| 45 | */ |
| 46 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 47 | static int part_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 48 | size_t *retlen, u_char *buf) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 49 | { |
| 50 | struct mtd_part *part = PART(mtd); |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 51 | struct mtd_ecc_stats stats; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 52 | int res; |
| 53 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 54 | stats = part->master->ecc_stats; |
| 55 | |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 56 | if (from >= mtd->size) |
| 57 | len = 0; |
| 58 | else if (from + len > mtd->size) |
| 59 | len = mtd->size - from; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 60 | res = part->master->read(part->master, from + part->offset, |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 61 | len, retlen, buf); |
| 62 | if (unlikely(res)) { |
| 63 | if (res == -EUCLEAN) |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 64 | mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 65 | if (res == -EBADMSG) |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 66 | mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 67 | } |
| 68 | return res; |
| 69 | } |
| 70 | |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 71 | static int part_read_oob(struct mtd_info *mtd, loff_t from, |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 72 | struct mtd_oob_ops *ops) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 73 | { |
| 74 | struct mtd_part *part = PART(mtd); |
| 75 | int res; |
| 76 | |
| 77 | if (from >= mtd->size) |
| 78 | return -EINVAL; |
| 79 | if (ops->datbuf && from + ops->len > mtd->size) |
| 80 | return -EINVAL; |
| 81 | res = part->master->read_oob(part->master, from + part->offset, ops); |
| 82 | |
| 83 | if (unlikely(res)) { |
| 84 | if (res == -EUCLEAN) |
| 85 | mtd->ecc_stats.corrected++; |
| 86 | if (res == -EBADMSG) |
| 87 | mtd->ecc_stats.failed++; |
| 88 | } |
| 89 | return res; |
| 90 | } |
| 91 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 92 | static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 93 | size_t len, size_t *retlen, u_char *buf) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 94 | { |
| 95 | struct mtd_part *part = PART(mtd); |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 96 | return part->master->read_user_prot_reg(part->master, from, |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 97 | len, retlen, buf); |
| 98 | } |
| 99 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 100 | static int part_get_user_prot_info(struct mtd_info *mtd, |
| 101 | struct otp_info *buf, size_t len) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 102 | { |
| 103 | struct mtd_part *part = PART(mtd); |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 104 | return part->master->get_user_prot_info(part->master, buf, len); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 107 | static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, |
| 108 | size_t len, size_t *retlen, u_char *buf) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 109 | { |
| 110 | struct mtd_part *part = PART(mtd); |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 111 | return part->master->read_fact_prot_reg(part->master, from, |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 112 | len, retlen, buf); |
| 113 | } |
| 114 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 115 | static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf, |
| 116 | size_t len) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 117 | { |
| 118 | struct mtd_part *part = PART(mtd); |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 119 | return part->master->get_fact_prot_info(part->master, buf, len); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 122 | static int part_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 123 | size_t *retlen, const u_char *buf) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 124 | { |
| 125 | struct mtd_part *part = PART(mtd); |
| 126 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 127 | return -EROFS; |
| 128 | if (to >= mtd->size) |
| 129 | len = 0; |
| 130 | else if (to + len > mtd->size) |
| 131 | len = mtd->size - to; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 132 | return part->master->write(part->master, to + part->offset, |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 133 | len, retlen, buf); |
| 134 | } |
| 135 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 136 | static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 137 | size_t *retlen, const u_char *buf) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 138 | { |
| 139 | struct mtd_part *part = PART(mtd); |
| 140 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 141 | return -EROFS; |
| 142 | if (to >= mtd->size) |
| 143 | len = 0; |
| 144 | else if (to + len > mtd->size) |
| 145 | len = mtd->size - to; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 146 | return part->master->panic_write(part->master, to + part->offset, |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 147 | len, retlen, buf); |
| 148 | } |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 149 | |
| 150 | static int part_write_oob(struct mtd_info *mtd, loff_t to, |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 151 | struct mtd_oob_ops *ops) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 152 | { |
| 153 | struct mtd_part *part = PART(mtd); |
| 154 | |
| 155 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 156 | return -EROFS; |
| 157 | |
| 158 | if (to >= mtd->size) |
| 159 | return -EINVAL; |
| 160 | if (ops->datbuf && to + ops->len > mtd->size) |
| 161 | return -EINVAL; |
| 162 | return part->master->write_oob(part->master, to + part->offset, ops); |
| 163 | } |
| 164 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 165 | static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 166 | size_t len, size_t *retlen, u_char *buf) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 167 | { |
| 168 | struct mtd_part *part = PART(mtd); |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 169 | return part->master->write_user_prot_reg(part->master, from, |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 170 | len, retlen, buf); |
| 171 | } |
| 172 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 173 | static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 174 | size_t len) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 175 | { |
| 176 | struct mtd_part *part = PART(mtd); |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 177 | return part->master->lock_user_prot_reg(part->master, from, len); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 180 | static int part_erase(struct mtd_info *mtd, struct erase_info *instr) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 181 | { |
| 182 | struct mtd_part *part = PART(mtd); |
| 183 | int ret; |
| 184 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 185 | return -EROFS; |
| 186 | if (instr->addr >= mtd->size) |
| 187 | return -EINVAL; |
| 188 | instr->addr += part->offset; |
| 189 | ret = part->master->erase(part->master, instr); |
| 190 | if (ret) { |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 191 | if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 192 | instr->fail_addr -= part->offset; |
| 193 | instr->addr -= part->offset; |
| 194 | } |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | void mtd_erase_callback(struct erase_info *instr) |
| 199 | { |
| 200 | if (instr->mtd->erase == part_erase) { |
| 201 | struct mtd_part *part = PART(instr->mtd); |
| 202 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 203 | if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 204 | instr->fail_addr -= part->offset; |
| 205 | instr->addr -= part->offset; |
| 206 | } |
| 207 | if (instr->callback) |
| 208 | instr->callback(instr); |
| 209 | } |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 210 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 211 | static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 212 | { |
| 213 | struct mtd_part *part = PART(mtd); |
| 214 | if ((len + ofs) > mtd->size) |
| 215 | return -EINVAL; |
| 216 | return part->master->lock(part->master, ofs + part->offset, len); |
| 217 | } |
| 218 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 219 | static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 220 | { |
| 221 | struct mtd_part *part = PART(mtd); |
| 222 | if ((len + ofs) > mtd->size) |
| 223 | return -EINVAL; |
| 224 | return part->master->unlock(part->master, ofs + part->offset, len); |
| 225 | } |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 226 | |
| 227 | static void part_sync(struct mtd_info *mtd) |
| 228 | { |
| 229 | struct mtd_part *part = PART(mtd); |
| 230 | part->master->sync(part->master); |
| 231 | } |
| 232 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 233 | static int part_block_isbad(struct mtd_info *mtd, loff_t ofs) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 234 | { |
| 235 | struct mtd_part *part = PART(mtd); |
| 236 | if (ofs >= mtd->size) |
| 237 | return -EINVAL; |
| 238 | ofs += part->offset; |
| 239 | return part->master->block_isbad(part->master, ofs); |
| 240 | } |
| 241 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 242 | static int part_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 243 | { |
| 244 | struct mtd_part *part = PART(mtd); |
| 245 | int res; |
| 246 | |
| 247 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 248 | return -EROFS; |
| 249 | if (ofs >= mtd->size) |
| 250 | return -EINVAL; |
| 251 | ofs += part->offset; |
| 252 | res = part->master->block_markbad(part->master, ofs); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 253 | if (!res) |
| 254 | mtd->ecc_stats.badblocks++; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 255 | return res; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * This function unregisters and destroy all slave MTD objects which are |
| 260 | * attached to the given master MTD object. |
| 261 | */ |
| 262 | |
| 263 | int del_mtd_partitions(struct mtd_info *master) |
| 264 | { |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 265 | struct mtd_part *slave, *next; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 266 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 267 | list_for_each_entry_safe(slave, next, &mtd_partitions, list) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 268 | if (slave->master == master) { |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 269 | list_del(&slave->list); |
| 270 | if (slave->registered) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 271 | del_mtd_device(&slave->mtd); |
| 272 | kfree(slave); |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static struct mtd_part *add_one_partition(struct mtd_info *master, |
| 279 | const struct mtd_partition *part, int partno, |
| 280 | uint64_t cur_offset) |
| 281 | { |
| 282 | struct mtd_part *slave; |
| 283 | |
| 284 | /* allocate the partition structure */ |
| 285 | slave = kzalloc(sizeof(*slave), GFP_KERNEL); |
| 286 | if (!slave) { |
| 287 | printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n", |
| 288 | master->name); |
| 289 | del_mtd_partitions(master); |
| 290 | return NULL; |
| 291 | } |
| 292 | list_add(&slave->list, &mtd_partitions); |
| 293 | |
| 294 | /* set up the MTD object for this partition */ |
| 295 | slave->mtd.type = master->type; |
| 296 | slave->mtd.flags = master->flags & ~part->mask_flags; |
| 297 | slave->mtd.size = part->size; |
| 298 | slave->mtd.writesize = master->writesize; |
| 299 | slave->mtd.oobsize = master->oobsize; |
| 300 | slave->mtd.oobavail = master->oobavail; |
| 301 | slave->mtd.subpage_sft = master->subpage_sft; |
| 302 | |
| 303 | slave->mtd.name = part->name; |
| 304 | slave->mtd.owner = master->owner; |
| 305 | |
| 306 | slave->mtd.read = part_read; |
| 307 | slave->mtd.write = part_write; |
| 308 | |
| 309 | if (master->panic_write) |
| 310 | slave->mtd.panic_write = part_panic_write; |
| 311 | |
| 312 | if (master->read_oob) |
| 313 | slave->mtd.read_oob = part_read_oob; |
| 314 | if (master->write_oob) |
| 315 | slave->mtd.write_oob = part_write_oob; |
| 316 | if (master->read_user_prot_reg) |
| 317 | slave->mtd.read_user_prot_reg = part_read_user_prot_reg; |
| 318 | if (master->read_fact_prot_reg) |
| 319 | slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg; |
| 320 | if (master->write_user_prot_reg) |
| 321 | slave->mtd.write_user_prot_reg = part_write_user_prot_reg; |
| 322 | if (master->lock_user_prot_reg) |
| 323 | slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg; |
| 324 | if (master->get_user_prot_info) |
| 325 | slave->mtd.get_user_prot_info = part_get_user_prot_info; |
| 326 | if (master->get_fact_prot_info) |
| 327 | slave->mtd.get_fact_prot_info = part_get_fact_prot_info; |
| 328 | if (master->sync) |
| 329 | slave->mtd.sync = part_sync; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 330 | if (master->lock) |
| 331 | slave->mtd.lock = part_lock; |
| 332 | if (master->unlock) |
| 333 | slave->mtd.unlock = part_unlock; |
| 334 | if (master->block_isbad) |
| 335 | slave->mtd.block_isbad = part_block_isbad; |
| 336 | if (master->block_markbad) |
| 337 | slave->mtd.block_markbad = part_block_markbad; |
| 338 | slave->mtd.erase = part_erase; |
| 339 | slave->master = master; |
| 340 | slave->offset = part->offset; |
| 341 | slave->index = partno; |
| 342 | |
| 343 | if (slave->offset == MTDPART_OFS_APPEND) |
| 344 | slave->offset = cur_offset; |
| 345 | if (slave->offset == MTDPART_OFS_NXTBLK) { |
| 346 | slave->offset = cur_offset; |
| 347 | if (mtd_mod_by_eb(cur_offset, master) != 0) { |
| 348 | /* Round up to next erasesize */ |
| 349 | slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize; |
Joe Hershberger | 147162d | 2013-04-08 10:32:49 +0000 | [diff] [blame] | 350 | debug("Moving partition %d: 0x%012llx -> 0x%012llx\n", |
| 351 | partno, (unsigned long long)cur_offset, |
| 352 | (unsigned long long)slave->offset); |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | if (slave->mtd.size == MTDPART_SIZ_FULL) |
| 356 | slave->mtd.size = master->size - slave->offset; |
| 357 | |
Joe Hershberger | 147162d | 2013-04-08 10:32:49 +0000 | [diff] [blame] | 358 | debug("0x%012llx-0x%012llx : \"%s\"\n", |
| 359 | (unsigned long long)slave->offset, |
| 360 | (unsigned long long)(slave->offset + slave->mtd.size), |
| 361 | slave->mtd.name); |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 362 | |
| 363 | /* let's do some sanity checks */ |
| 364 | if (slave->offset >= master->size) { |
| 365 | /* let's register it anyway to preserve ordering */ |
| 366 | slave->offset = 0; |
| 367 | slave->mtd.size = 0; |
| 368 | printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n", |
| 369 | part->name); |
| 370 | goto out_register; |
| 371 | } |
| 372 | if (slave->offset + slave->mtd.size > master->size) { |
| 373 | slave->mtd.size = master->size - slave->offset; |
| 374 | printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n", |
| 375 | part->name, master->name, (unsigned long long)slave->mtd.size); |
| 376 | } |
| 377 | if (master->numeraseregions > 1) { |
| 378 | /* Deal with variable erase size stuff */ |
| 379 | int i, max = master->numeraseregions; |
| 380 | u64 end = slave->offset + slave->mtd.size; |
| 381 | struct mtd_erase_region_info *regions = master->eraseregions; |
| 382 | |
| 383 | /* Find the first erase regions which is part of this |
| 384 | * partition. */ |
| 385 | for (i = 0; i < max && regions[i].offset <= slave->offset; i++) |
| 386 | ; |
| 387 | /* The loop searched for the region _behind_ the first one */ |
| 388 | i--; |
| 389 | |
| 390 | /* Pick biggest erasesize */ |
| 391 | for (; i < max && regions[i].offset < end; i++) { |
| 392 | if (slave->mtd.erasesize < regions[i].erasesize) { |
| 393 | slave->mtd.erasesize = regions[i].erasesize; |
| 394 | } |
| 395 | } |
| 396 | BUG_ON(slave->mtd.erasesize == 0); |
| 397 | } else { |
| 398 | /* Single erase size */ |
| 399 | slave->mtd.erasesize = master->erasesize; |
| 400 | } |
| 401 | |
| 402 | if ((slave->mtd.flags & MTD_WRITEABLE) && |
| 403 | mtd_mod_by_eb(slave->offset, &slave->mtd)) { |
| 404 | /* Doesn't start on a boundary of major erase size */ |
| 405 | /* FIXME: Let it be writable if it is on a boundary of |
| 406 | * _minor_ erase size though */ |
| 407 | slave->mtd.flags &= ~MTD_WRITEABLE; |
| 408 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n", |
| 409 | part->name); |
| 410 | } |
| 411 | if ((slave->mtd.flags & MTD_WRITEABLE) && |
| 412 | mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) { |
| 413 | slave->mtd.flags &= ~MTD_WRITEABLE; |
| 414 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n", |
| 415 | part->name); |
| 416 | } |
| 417 | |
| 418 | slave->mtd.ecclayout = master->ecclayout; |
| 419 | if (master->block_isbad) { |
| 420 | uint64_t offs = 0; |
| 421 | |
| 422 | while (offs < slave->mtd.size) { |
| 423 | if (master->block_isbad(master, |
| 424 | offs + slave->offset)) |
| 425 | slave->mtd.ecc_stats.badblocks++; |
| 426 | offs += slave->mtd.erasesize; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 430 | out_register: |
| 431 | if (part->mtdp) { |
| 432 | /* store the object pointer (caller may or may not register it*/ |
| 433 | *part->mtdp = &slave->mtd; |
| 434 | slave->registered = 0; |
| 435 | } else { |
| 436 | /* register our partition */ |
| 437 | add_mtd_device(&slave->mtd); |
| 438 | slave->registered = 1; |
| 439 | } |
| 440 | return slave; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* |
| 444 | * This function, given a master MTD object and a partition table, creates |
| 445 | * and registers slave MTD objects which are bound to the master according to |
| 446 | * the partition definitions. |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 447 | * |
| 448 | * We don't register the master, or expect the caller to have done so, |
| 449 | * for reasons of data integrity. |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 450 | */ |
| 451 | |
| 452 | int add_mtd_partitions(struct mtd_info *master, |
| 453 | const struct mtd_partition *parts, |
| 454 | int nbparts) |
| 455 | { |
| 456 | struct mtd_part *slave; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 457 | uint64_t cur_offset = 0; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 458 | int i; |
| 459 | |
Stefan Roese | 9def12c | 2008-11-27 14:05:15 +0100 | [diff] [blame] | 460 | /* |
| 461 | * Need to init the list here, since LIST_INIT() does not |
| 462 | * work on platforms where relocation has problems (like MIPS |
| 463 | * & PPC). |
| 464 | */ |
| 465 | if (mtd_partitions.next == NULL) |
| 466 | INIT_LIST_HEAD(&mtd_partitions); |
| 467 | |
Joe Hershberger | 147162d | 2013-04-08 10:32:49 +0000 | [diff] [blame] | 468 | debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 469 | |
| 470 | for (i = 0; i < nbparts; i++) { |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 471 | slave = add_one_partition(master, parts + i, i, cur_offset); |
| 472 | if (!slave) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 473 | return -ENOMEM; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 474 | cur_offset = slave->offset + slave->mtd.size; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | return 0; |
| 478 | } |