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