Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Simple MTD partitioning layer |
| 3 | * |
| 4 | * (C) 2000 Nicolas Pitre <nico@cam.org> |
| 5 | * |
| 6 | * This code is GPL |
| 7 | * |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 8 | * $Id: mtdpart.c,v 1.55 2005/11/07 11:14:20 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 10 | * 02-21-2002 Thomas Gleixner <gleixner@autronix.de> |
| 11 | * added support for read_oob, write_oob |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 12 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/kmod.h> |
| 20 | #include <linux/mtd/mtd.h> |
| 21 | #include <linux/mtd/partitions.h> |
| 22 | #include <linux/mtd/compatmac.h> |
| 23 | |
| 24 | /* Our partition linked list */ |
| 25 | static LIST_HEAD(mtd_partitions); |
| 26 | |
| 27 | /* Our partition node structure */ |
| 28 | struct mtd_part { |
| 29 | struct mtd_info mtd; |
| 30 | struct mtd_info *master; |
| 31 | u_int32_t offset; |
| 32 | int index; |
| 33 | struct list_head list; |
| 34 | int registered; |
| 35 | }; |
| 36 | |
| 37 | /* |
| 38 | * Given a pointer to the MTD object in the mtd_part structure, we can retrieve |
| 39 | * the pointer to that structure with this macro. |
| 40 | */ |
| 41 | #define PART(x) ((struct mtd_part *)(x)) |
| 42 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 43 | |
| 44 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | * MTD methods which simply translate the effective address and pass through |
| 46 | * to the _real_ device. |
| 47 | */ |
| 48 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 49 | static int part_read (struct mtd_info *mtd, loff_t from, size_t len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | size_t *retlen, u_char *buf) |
| 51 | { |
| 52 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 53 | int res; |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | if (from >= mtd->size) |
| 56 | len = 0; |
| 57 | else if (from + len > mtd->size) |
| 58 | len = mtd->size - from; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 59 | res = part->master->read (part->master, from + part->offset, |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 60 | len, retlen, buf); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 61 | if (unlikely(res)) { |
| 62 | if (res == -EUCLEAN) |
| 63 | mtd->ecc_stats.corrected++; |
| 64 | if (res == -EBADMSG) |
| 65 | mtd->ecc_stats.failed++; |
| 66 | } |
| 67 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 70 | static int part_point (struct mtd_info *mtd, loff_t from, size_t len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | size_t *retlen, u_char **buf) |
| 72 | { |
| 73 | struct mtd_part *part = PART(mtd); |
| 74 | if (from >= mtd->size) |
| 75 | len = 0; |
| 76 | else if (from + len > mtd->size) |
| 77 | len = mtd->size - from; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 78 | return part->master->point (part->master, from + part->offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | len, retlen, buf); |
| 80 | } |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | static void part_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from, size_t len) |
| 83 | { |
| 84 | struct mtd_part *part = PART(mtd); |
| 85 | |
| 86 | part->master->unpoint (part->master, addr, from + part->offset, len); |
| 87 | } |
| 88 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 89 | static int part_read_oob(struct mtd_info *mtd, loff_t from, |
| 90 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
| 92 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 93 | int res; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | if (from >= mtd->size) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 96 | return -EINVAL; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 97 | if (ops->datbuf && from + ops->len > mtd->size) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 98 | return -EINVAL; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 99 | res = part->master->read_oob(part->master, from + part->offset, ops); |
| 100 | |
| 101 | if (unlikely(res)) { |
| 102 | if (res == -EUCLEAN) |
| 103 | mtd->ecc_stats.corrected++; |
| 104 | if (res == -EBADMSG) |
| 105 | mtd->ecc_stats.failed++; |
| 106 | } |
| 107 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 110 | static int part_read_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | size_t *retlen, u_char *buf) |
| 112 | { |
| 113 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 114 | return part->master->read_user_prot_reg (part->master, from, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | len, retlen, buf); |
| 116 | } |
| 117 | |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 118 | static int part_get_user_prot_info (struct mtd_info *mtd, |
| 119 | struct otp_info *buf, size_t len) |
| 120 | { |
| 121 | struct mtd_part *part = PART(mtd); |
| 122 | return part->master->get_user_prot_info (part->master, buf, len); |
| 123 | } |
| 124 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 125 | static int part_read_fact_prot_reg (struct mtd_info *mtd, loff_t from, size_t len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | size_t *retlen, u_char *buf) |
| 127 | { |
| 128 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 129 | return part->master->read_fact_prot_reg (part->master, from, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | len, retlen, buf); |
| 131 | } |
| 132 | |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 133 | static int part_get_fact_prot_info (struct mtd_info *mtd, |
| 134 | struct otp_info *buf, size_t len) |
| 135 | { |
| 136 | struct mtd_part *part = PART(mtd); |
| 137 | return part->master->get_fact_prot_info (part->master, buf, len); |
| 138 | } |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | static int part_write (struct mtd_info *mtd, loff_t to, size_t len, |
| 141 | size_t *retlen, const u_char *buf) |
| 142 | { |
| 143 | struct mtd_part *part = PART(mtd); |
| 144 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 145 | return -EROFS; |
| 146 | if (to >= mtd->size) |
| 147 | len = 0; |
| 148 | else if (to + len > mtd->size) |
| 149 | len = mtd->size - to; |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 150 | return part->master->write (part->master, to + part->offset, |
| 151 | len, retlen, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 154 | static int part_write_oob(struct mtd_info *mtd, loff_t to, |
| 155 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
| 157 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 160 | return -EROFS; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | if (to >= mtd->size) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 163 | return -EINVAL; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 164 | if (ops->datbuf && to + ops->len > mtd->size) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 165 | return -EINVAL; |
| 166 | return part->master->write_oob(part->master, to + part->offset, ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 169 | static int part_write_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | size_t *retlen, u_char *buf) |
| 171 | { |
| 172 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 173 | return part->master->write_user_prot_reg (part->master, from, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | len, retlen, buf); |
| 175 | } |
| 176 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 177 | static int part_lock_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len) |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 178 | { |
| 179 | struct mtd_part *part = PART(mtd); |
| 180 | return part->master->lock_user_prot_reg (part->master, from, len); |
| 181 | } |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | static int part_writev (struct mtd_info *mtd, const struct kvec *vecs, |
| 184 | unsigned long count, loff_t to, size_t *retlen) |
| 185 | { |
| 186 | struct mtd_part *part = PART(mtd); |
| 187 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 188 | return -EROFS; |
Thomas Gleixner | 9d8522d | 2006-05-23 16:06:03 +0200 | [diff] [blame] | 189 | return part->master->writev (part->master, vecs, count, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | to + part->offset, retlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | static int part_erase (struct mtd_info *mtd, struct erase_info *instr) |
| 194 | { |
| 195 | struct mtd_part *part = PART(mtd); |
| 196 | int ret; |
| 197 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 198 | return -EROFS; |
| 199 | if (instr->addr >= mtd->size) |
| 200 | return -EINVAL; |
| 201 | instr->addr += part->offset; |
| 202 | ret = part->master->erase(part->master, instr); |
Adrian Hunter | 74641d7 | 2007-03-08 12:20:12 +0200 | [diff] [blame] | 203 | if (ret) { |
| 204 | if (instr->fail_addr != 0xffffffff) |
| 205 | instr->fail_addr -= part->offset; |
| 206 | instr->addr -= part->offset; |
| 207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | void mtd_erase_callback(struct erase_info *instr) |
| 212 | { |
| 213 | if (instr->mtd->erase == part_erase) { |
| 214 | struct mtd_part *part = PART(instr->mtd); |
| 215 | |
| 216 | if (instr->fail_addr != 0xffffffff) |
| 217 | instr->fail_addr -= part->offset; |
| 218 | instr->addr -= part->offset; |
| 219 | } |
| 220 | if (instr->callback) |
| 221 | instr->callback(instr); |
| 222 | } |
| 223 | EXPORT_SYMBOL_GPL(mtd_erase_callback); |
| 224 | |
| 225 | static int part_lock (struct mtd_info *mtd, loff_t ofs, size_t len) |
| 226 | { |
| 227 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 228 | if ((len + ofs) > mtd->size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return -EINVAL; |
| 230 | return part->master->lock(part->master, ofs + part->offset, len); |
| 231 | } |
| 232 | |
| 233 | static int part_unlock (struct mtd_info *mtd, loff_t ofs, size_t len) |
| 234 | { |
| 235 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 236 | if ((len + ofs) > mtd->size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | return -EINVAL; |
| 238 | return part->master->unlock(part->master, ofs + part->offset, len); |
| 239 | } |
| 240 | |
| 241 | static void part_sync(struct mtd_info *mtd) |
| 242 | { |
| 243 | struct mtd_part *part = PART(mtd); |
| 244 | part->master->sync(part->master); |
| 245 | } |
| 246 | |
| 247 | static int part_suspend(struct mtd_info *mtd) |
| 248 | { |
| 249 | struct mtd_part *part = PART(mtd); |
| 250 | return part->master->suspend(part->master); |
| 251 | } |
| 252 | |
| 253 | static void part_resume(struct mtd_info *mtd) |
| 254 | { |
| 255 | struct mtd_part *part = PART(mtd); |
| 256 | part->master->resume(part->master); |
| 257 | } |
| 258 | |
| 259 | static int part_block_isbad (struct mtd_info *mtd, loff_t ofs) |
| 260 | { |
| 261 | struct mtd_part *part = PART(mtd); |
| 262 | if (ofs >= mtd->size) |
| 263 | return -EINVAL; |
| 264 | ofs += part->offset; |
| 265 | return part->master->block_isbad(part->master, ofs); |
| 266 | } |
| 267 | |
| 268 | static int part_block_markbad (struct mtd_info *mtd, loff_t ofs) |
| 269 | { |
| 270 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 271 | int res; |
| 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 274 | return -EROFS; |
| 275 | if (ofs >= mtd->size) |
| 276 | return -EINVAL; |
| 277 | ofs += part->offset; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 278 | res = part->master->block_markbad(part->master, ofs); |
| 279 | if (!res) |
| 280 | mtd->ecc_stats.badblocks++; |
| 281 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 284 | /* |
| 285 | * This function unregisters and destroy all slave MTD objects which are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | * attached to the given master MTD object. |
| 287 | */ |
| 288 | |
| 289 | int del_mtd_partitions(struct mtd_info *master) |
| 290 | { |
| 291 | struct list_head *node; |
| 292 | struct mtd_part *slave; |
| 293 | |
| 294 | for (node = mtd_partitions.next; |
| 295 | node != &mtd_partitions; |
| 296 | node = node->next) { |
| 297 | slave = list_entry(node, struct mtd_part, list); |
| 298 | if (slave->master == master) { |
| 299 | struct list_head *prev = node->prev; |
| 300 | __list_del(prev, node->next); |
| 301 | if(slave->registered) |
| 302 | del_mtd_device(&slave->mtd); |
| 303 | kfree(slave); |
| 304 | node = prev; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * This function, given a master MTD object and a partition table, creates |
| 313 | * and registers slave MTD objects which are bound to the master according to |
| 314 | * the partition definitions. |
| 315 | * (Q: should we register the master MTD object as well?) |
| 316 | */ |
| 317 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 318 | int add_mtd_partitions(struct mtd_info *master, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | const struct mtd_partition *parts, |
| 320 | int nbparts) |
| 321 | { |
| 322 | struct mtd_part *slave; |
| 323 | u_int32_t cur_offset = 0; |
| 324 | int i; |
| 325 | |
| 326 | printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name); |
| 327 | |
| 328 | for (i = 0; i < nbparts; i++) { |
| 329 | |
| 330 | /* allocate the partition structure */ |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 331 | slave = kzalloc (sizeof(*slave), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | if (!slave) { |
| 333 | printk ("memory allocation error while creating partitions for \"%s\"\n", |
| 334 | master->name); |
| 335 | del_mtd_partitions(master); |
| 336 | return -ENOMEM; |
| 337 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | list_add(&slave->list, &mtd_partitions); |
| 339 | |
| 340 | /* set up the MTD object for this partition */ |
| 341 | slave->mtd.type = master->type; |
| 342 | slave->mtd.flags = master->flags & ~parts[i].mask_flags; |
| 343 | slave->mtd.size = parts[i].size; |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 344 | slave->mtd.writesize = master->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | slave->mtd.oobsize = master->oobsize; |
Vitaly Wool | 1f92267 | 2007-03-06 16:56:34 +0300 | [diff] [blame] | 346 | slave->mtd.oobavail = master->oobavail; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 347 | slave->mtd.subpage_sft = master->subpage_sft; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
| 349 | slave->mtd.name = parts[i].name; |
| 350 | slave->mtd.bank_size = master->bank_size; |
| 351 | slave->mtd.owner = master->owner; |
| 352 | |
| 353 | slave->mtd.read = part_read; |
| 354 | slave->mtd.write = part_write; |
| 355 | |
| 356 | if(master->point && master->unpoint){ |
| 357 | slave->mtd.point = part_point; |
| 358 | slave->mtd.unpoint = part_unpoint; |
| 359 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | if (master->read_oob) |
| 362 | slave->mtd.read_oob = part_read_oob; |
| 363 | if (master->write_oob) |
| 364 | slave->mtd.write_oob = part_write_oob; |
| 365 | if(master->read_user_prot_reg) |
| 366 | slave->mtd.read_user_prot_reg = part_read_user_prot_reg; |
| 367 | if(master->read_fact_prot_reg) |
| 368 | slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg; |
| 369 | if(master->write_user_prot_reg) |
| 370 | slave->mtd.write_user_prot_reg = part_write_user_prot_reg; |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 371 | if(master->lock_user_prot_reg) |
| 372 | slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg; |
| 373 | if(master->get_user_prot_info) |
| 374 | slave->mtd.get_user_prot_info = part_get_user_prot_info; |
| 375 | if(master->get_fact_prot_info) |
| 376 | slave->mtd.get_fact_prot_info = part_get_fact_prot_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | if (master->sync) |
| 378 | slave->mtd.sync = part_sync; |
| 379 | if (!i && master->suspend && master->resume) { |
| 380 | slave->mtd.suspend = part_suspend; |
| 381 | slave->mtd.resume = part_resume; |
| 382 | } |
| 383 | if (master->writev) |
| 384 | slave->mtd.writev = part_writev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | if (master->lock) |
| 386 | slave->mtd.lock = part_lock; |
| 387 | if (master->unlock) |
| 388 | slave->mtd.unlock = part_unlock; |
| 389 | if (master->block_isbad) |
| 390 | slave->mtd.block_isbad = part_block_isbad; |
| 391 | if (master->block_markbad) |
| 392 | slave->mtd.block_markbad = part_block_markbad; |
| 393 | slave->mtd.erase = part_erase; |
| 394 | slave->master = master; |
| 395 | slave->offset = parts[i].offset; |
| 396 | slave->index = i; |
| 397 | |
| 398 | if (slave->offset == MTDPART_OFS_APPEND) |
| 399 | slave->offset = cur_offset; |
| 400 | if (slave->offset == MTDPART_OFS_NXTBLK) { |
Artem B. Bityutskiy | 5cea5da | 2005-09-30 15:49:12 +0100 | [diff] [blame] | 401 | slave->offset = cur_offset; |
| 402 | if ((cur_offset % master->erasesize) != 0) { |
| 403 | /* Round up to next erasesize */ |
| 404 | slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | printk(KERN_NOTICE "Moving partition %d: " |
| 406 | "0x%08x -> 0x%08x\n", i, |
| 407 | cur_offset, slave->offset); |
| 408 | } |
| 409 | } |
| 410 | if (slave->mtd.size == MTDPART_SIZ_FULL) |
| 411 | slave->mtd.size = master->size - slave->offset; |
| 412 | cur_offset = slave->offset + slave->mtd.size; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 413 | |
| 414 | printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | slave->offset + slave->mtd.size, slave->mtd.name); |
| 416 | |
| 417 | /* let's do some sanity checks */ |
| 418 | if (slave->offset >= master->size) { |
| 419 | /* let's register it anyway to preserve ordering */ |
| 420 | slave->offset = 0; |
| 421 | slave->mtd.size = 0; |
| 422 | printk ("mtd: partition \"%s\" is out of reach -- disabled\n", |
| 423 | parts[i].name); |
| 424 | } |
| 425 | if (slave->offset + slave->mtd.size > master->size) { |
| 426 | slave->mtd.size = master->size - slave->offset; |
| 427 | printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n", |
| 428 | parts[i].name, master->name, slave->mtd.size); |
| 429 | } |
| 430 | if (master->numeraseregions>1) { |
| 431 | /* Deal with variable erase size stuff */ |
| 432 | int i; |
| 433 | struct mtd_erase_region_info *regions = master->eraseregions; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | /* Find the first erase regions which is part of this partition. */ |
| 436 | for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++) |
| 437 | ; |
| 438 | |
| 439 | for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) { |
| 440 | if (slave->mtd.erasesize < regions[i].erasesize) { |
| 441 | slave->mtd.erasesize = regions[i].erasesize; |
| 442 | } |
| 443 | } |
| 444 | } else { |
| 445 | /* Single erase size */ |
| 446 | slave->mtd.erasesize = master->erasesize; |
| 447 | } |
| 448 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 449 | if ((slave->mtd.flags & MTD_WRITEABLE) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | (slave->offset % slave->mtd.erasesize)) { |
| 451 | /* Doesn't start on a boundary of major erase size */ |
| 452 | /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */ |
| 453 | slave->mtd.flags &= ~MTD_WRITEABLE; |
| 454 | printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n", |
| 455 | parts[i].name); |
| 456 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 457 | if ((slave->mtd.flags & MTD_WRITEABLE) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | (slave->mtd.size % slave->mtd.erasesize)) { |
| 459 | slave->mtd.flags &= ~MTD_WRITEABLE; |
| 460 | printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n", |
| 461 | parts[i].name); |
| 462 | } |
| 463 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 464 | slave->mtd.ecclayout = master->ecclayout; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 465 | if (master->block_isbad) { |
| 466 | uint32_t offs = 0; |
| 467 | |
| 468 | while(offs < slave->mtd.size) { |
| 469 | if (master->block_isbad(master, |
| 470 | offs + slave->offset)) |
| 471 | slave->mtd.ecc_stats.badblocks++; |
| 472 | offs += slave->mtd.erasesize; |
| 473 | } |
| 474 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
| 476 | if(parts[i].mtdp) |
| 477 | { /* store the object pointer (caller may or may not register it */ |
| 478 | *parts[i].mtdp = &slave->mtd; |
| 479 | slave->registered = 0; |
| 480 | } |
| 481 | else |
| 482 | { |
| 483 | /* register our partition */ |
| 484 | add_mtd_device(&slave->mtd); |
| 485 | slave->registered = 1; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | EXPORT_SYMBOL(add_mtd_partitions); |
| 493 | EXPORT_SYMBOL(del_mtd_partitions); |
| 494 | |
| 495 | static DEFINE_SPINLOCK(part_parser_lock); |
| 496 | static LIST_HEAD(part_parsers); |
| 497 | |
| 498 | static struct mtd_part_parser *get_partition_parser(const char *name) |
| 499 | { |
| 500 | struct list_head *this; |
| 501 | void *ret = NULL; |
| 502 | spin_lock(&part_parser_lock); |
| 503 | |
| 504 | list_for_each(this, &part_parsers) { |
| 505 | struct mtd_part_parser *p = list_entry(this, struct mtd_part_parser, list); |
| 506 | |
| 507 | if (!strcmp(p->name, name) && try_module_get(p->owner)) { |
| 508 | ret = p; |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | spin_unlock(&part_parser_lock); |
| 513 | |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | int register_mtd_parser(struct mtd_part_parser *p) |
| 518 | { |
| 519 | spin_lock(&part_parser_lock); |
| 520 | list_add(&p->list, &part_parsers); |
| 521 | spin_unlock(&part_parser_lock); |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | int deregister_mtd_parser(struct mtd_part_parser *p) |
| 527 | { |
| 528 | spin_lock(&part_parser_lock); |
| 529 | list_del(&p->list); |
| 530 | spin_unlock(&part_parser_lock); |
| 531 | return 0; |
| 532 | } |
| 533 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 534 | int parse_mtd_partitions(struct mtd_info *master, const char **types, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | struct mtd_partition **pparts, unsigned long origin) |
| 536 | { |
| 537 | struct mtd_part_parser *parser; |
| 538 | int ret = 0; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | for ( ; ret <= 0 && *types; types++) { |
| 541 | parser = get_partition_parser(*types); |
| 542 | #ifdef CONFIG_KMOD |
| 543 | if (!parser && !request_module("%s", *types)) |
| 544 | parser = get_partition_parser(*types); |
| 545 | #endif |
| 546 | if (!parser) { |
| 547 | printk(KERN_NOTICE "%s partition parsing not available\n", |
| 548 | *types); |
| 549 | continue; |
| 550 | } |
| 551 | ret = (*parser->parse_fn)(master, pparts, origin); |
| 552 | if (ret > 0) { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 553 | printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | ret, parser->name, master->name); |
| 555 | } |
| 556 | put_partition_parser(parser); |
| 557 | } |
| 558 | return ret; |
| 559 | } |
| 560 | |
| 561 | EXPORT_SYMBOL_GPL(parse_mtd_partitions); |
| 562 | EXPORT_SYMBOL_GPL(register_mtd_parser); |
| 563 | EXPORT_SYMBOL_GPL(deregister_mtd_parser); |
| 564 | |
| 565 | MODULE_LICENSE("GPL"); |
| 566 | MODULE_AUTHOR("Nicolas Pitre <nico@cam.org>"); |
| 567 | MODULE_DESCRIPTION("Generic support for partitioning of MTD devices"); |