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