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