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 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 47 | static int part_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 48 | size_t *retlen, u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [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 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 68 | static int part_point(struct mtd_info *mtd, loff_t from, size_t len, |
| 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, |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 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 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 108 | static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 109 | size_t len, size_t *retlen, u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | struct mtd_part *part = PART(mtd); |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [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 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 116 | static int part_get_user_prot_info(struct mtd_info *mtd, |
| 117 | struct otp_info *buf, size_t len) |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 118 | { |
| 119 | struct mtd_part *part = PART(mtd); |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 120 | return part->master->get_user_prot_info(part->master, buf, len); |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 123 | static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, |
| 124 | size_t len, size_t *retlen, u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | struct mtd_part *part = PART(mtd); |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [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 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 131 | static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf, |
| 132 | size_t len) |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 133 | { |
| 134 | struct mtd_part *part = PART(mtd); |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 135 | return part->master->get_fact_prot_info(part->master, buf, len); |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 148 | return part->master->write(part->master, to + part->offset, |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 149 | len, retlen, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [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) |
Richard Purdie | 388bbb0 | 2008-02-06 10:17:15 +0000 | [diff] [blame] | 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; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 162 | return part->master->panic_write(part->master, to + part->offset, |
Richard Purdie | 388bbb0 | 2008-02-06 10:17:15 +0000 | [diff] [blame] | 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, |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 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 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 181 | static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 182 | size_t len, size_t *retlen, u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
| 184 | struct mtd_part *part = PART(mtd); |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [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 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 189 | static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 190 | size_t len) |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 191 | { |
| 192 | struct mtd_part *part = PART(mtd); |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 193 | return part->master->lock_user_prot_reg(part->master, from, len); |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 196 | static int part_writev(struct mtd_info *mtd, const struct kvec *vecs, |
| 197 | unsigned long count, loff_t to, size_t *retlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
| 199 | struct mtd_part *part = PART(mtd); |
| 200 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 201 | return -EROFS; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 202 | return part->master->writev(part->master, vecs, count, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | to + part->offset, retlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 206 | static int part_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
| 208 | struct mtd_part *part = PART(mtd); |
| 209 | int ret; |
| 210 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 211 | return -EROFS; |
| 212 | if (instr->addr >= mtd->size) |
| 213 | return -EINVAL; |
| 214 | instr->addr += part->offset; |
| 215 | ret = part->master->erase(part->master, instr); |
Adrian Hunter | 74641d7 | 2007-03-08 12:20:12 +0200 | [diff] [blame] | 216 | if (ret) { |
| 217 | if (instr->fail_addr != 0xffffffff) |
| 218 | instr->fail_addr -= part->offset; |
| 219 | instr->addr -= part->offset; |
| 220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | void mtd_erase_callback(struct erase_info *instr) |
| 225 | { |
| 226 | if (instr->mtd->erase == part_erase) { |
| 227 | struct mtd_part *part = PART(instr->mtd); |
| 228 | |
| 229 | if (instr->fail_addr != 0xffffffff) |
| 230 | instr->fail_addr -= part->offset; |
| 231 | instr->addr -= part->offset; |
| 232 | } |
| 233 | if (instr->callback) |
| 234 | instr->callback(instr); |
| 235 | } |
| 236 | EXPORT_SYMBOL_GPL(mtd_erase_callback); |
| 237 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 238 | static int part_lock(struct mtd_info *mtd, loff_t ofs, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
| 240 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 241 | if ((len + ofs) > mtd->size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | return -EINVAL; |
| 243 | return part->master->lock(part->master, ofs + part->offset, len); |
| 244 | } |
| 245 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 246 | static int part_unlock(struct mtd_info *mtd, loff_t ofs, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
| 248 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 249 | if ((len + ofs) > mtd->size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | return -EINVAL; |
| 251 | return part->master->unlock(part->master, ofs + part->offset, len); |
| 252 | } |
| 253 | |
| 254 | static void part_sync(struct mtd_info *mtd) |
| 255 | { |
| 256 | struct mtd_part *part = PART(mtd); |
| 257 | part->master->sync(part->master); |
| 258 | } |
| 259 | |
| 260 | static int part_suspend(struct mtd_info *mtd) |
| 261 | { |
| 262 | struct mtd_part *part = PART(mtd); |
| 263 | return part->master->suspend(part->master); |
| 264 | } |
| 265 | |
| 266 | static void part_resume(struct mtd_info *mtd) |
| 267 | { |
| 268 | struct mtd_part *part = PART(mtd); |
| 269 | part->master->resume(part->master); |
| 270 | } |
| 271 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 272 | static int part_block_isbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
| 274 | struct mtd_part *part = PART(mtd); |
| 275 | if (ofs >= mtd->size) |
| 276 | return -EINVAL; |
| 277 | ofs += part->offset; |
| 278 | return part->master->block_isbad(part->master, ofs); |
| 279 | } |
| 280 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 281 | static int part_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | { |
| 283 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 284 | int res; |
| 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 287 | return -EROFS; |
| 288 | if (ofs >= mtd->size) |
| 289 | return -EINVAL; |
| 290 | ofs += part->offset; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 291 | res = part->master->block_markbad(part->master, ofs); |
| 292 | if (!res) |
| 293 | mtd->ecc_stats.badblocks++; |
| 294 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 297 | /* |
| 298 | * This function unregisters and destroy all slave MTD objects which are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | * attached to the given master MTD object. |
| 300 | */ |
| 301 | |
| 302 | int del_mtd_partitions(struct mtd_info *master) |
| 303 | { |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 304 | struct mtd_part *slave, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 306 | list_for_each_entry_safe(slave, next, &mtd_partitions, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | if (slave->master == master) { |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 308 | list_del(&slave->list); |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 309 | if (slave->registered) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | del_mtd_device(&slave->mtd); |
| 311 | kfree(slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | return 0; |
| 315 | } |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 316 | EXPORT_SYMBOL(del_mtd_partitions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 318 | static struct mtd_part *add_one_partition(struct mtd_info *master, |
| 319 | const struct mtd_partition *part, int partno, |
| 320 | u_int32_t cur_offset) |
| 321 | { |
| 322 | struct mtd_part *slave; |
| 323 | |
| 324 | /* allocate the partition structure */ |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 325 | slave = kzalloc(sizeof(*slave), GFP_KERNEL); |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 326 | if (!slave) { |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 327 | printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n", |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 328 | master->name); |
| 329 | del_mtd_partitions(master); |
| 330 | return NULL; |
| 331 | } |
| 332 | list_add(&slave->list, &mtd_partitions); |
| 333 | |
| 334 | /* set up the MTD object for this partition */ |
| 335 | slave->mtd.type = master->type; |
| 336 | slave->mtd.flags = master->flags & ~part->mask_flags; |
| 337 | slave->mtd.size = part->size; |
| 338 | slave->mtd.writesize = master->writesize; |
| 339 | slave->mtd.oobsize = master->oobsize; |
| 340 | slave->mtd.oobavail = master->oobavail; |
| 341 | slave->mtd.subpage_sft = master->subpage_sft; |
| 342 | |
| 343 | slave->mtd.name = part->name; |
| 344 | slave->mtd.owner = master->owner; |
| 345 | |
| 346 | slave->mtd.read = part_read; |
| 347 | slave->mtd.write = part_write; |
| 348 | |
| 349 | if (master->panic_write) |
| 350 | slave->mtd.panic_write = part_panic_write; |
| 351 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 352 | if (master->point && master->unpoint) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 353 | slave->mtd.point = part_point; |
| 354 | slave->mtd.unpoint = part_unpoint; |
| 355 | } |
| 356 | |
| 357 | if (master->read_oob) |
| 358 | slave->mtd.read_oob = part_read_oob; |
| 359 | if (master->write_oob) |
| 360 | slave->mtd.write_oob = part_write_oob; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 361 | if (master->read_user_prot_reg) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 362 | slave->mtd.read_user_prot_reg = part_read_user_prot_reg; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 363 | if (master->read_fact_prot_reg) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 364 | slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 365 | if (master->write_user_prot_reg) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 366 | slave->mtd.write_user_prot_reg = part_write_user_prot_reg; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 367 | if (master->lock_user_prot_reg) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 368 | slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 369 | if (master->get_user_prot_info) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 370 | slave->mtd.get_user_prot_info = part_get_user_prot_info; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 371 | if (master->get_fact_prot_info) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 372 | slave->mtd.get_fact_prot_info = part_get_fact_prot_info; |
| 373 | if (master->sync) |
| 374 | slave->mtd.sync = part_sync; |
| 375 | if (!partno && master->suspend && master->resume) { |
| 376 | slave->mtd.suspend = part_suspend; |
| 377 | slave->mtd.resume = part_resume; |
| 378 | } |
| 379 | if (master->writev) |
| 380 | slave->mtd.writev = part_writev; |
| 381 | if (master->lock) |
| 382 | slave->mtd.lock = part_lock; |
| 383 | if (master->unlock) |
| 384 | slave->mtd.unlock = part_unlock; |
| 385 | if (master->block_isbad) |
| 386 | slave->mtd.block_isbad = part_block_isbad; |
| 387 | if (master->block_markbad) |
| 388 | slave->mtd.block_markbad = part_block_markbad; |
| 389 | slave->mtd.erase = part_erase; |
| 390 | slave->master = master; |
| 391 | slave->offset = part->offset; |
| 392 | slave->index = partno; |
| 393 | |
| 394 | if (slave->offset == MTDPART_OFS_APPEND) |
| 395 | slave->offset = cur_offset; |
| 396 | if (slave->offset == MTDPART_OFS_NXTBLK) { |
| 397 | slave->offset = cur_offset; |
| 398 | if ((cur_offset % master->erasesize) != 0) { |
| 399 | /* Round up to next erasesize */ |
| 400 | slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize; |
| 401 | printk(KERN_NOTICE "Moving partition %d: " |
| 402 | "0x%08x -> 0x%08x\n", partno, |
| 403 | cur_offset, slave->offset); |
| 404 | } |
| 405 | } |
| 406 | if (slave->mtd.size == MTDPART_SIZ_FULL) |
| 407 | slave->mtd.size = master->size - slave->offset; |
| 408 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 409 | printk(KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset, |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 410 | slave->offset + slave->mtd.size, slave->mtd.name); |
| 411 | |
| 412 | /* let's do some sanity checks */ |
| 413 | if (slave->offset >= master->size) { |
Atsushi Nemoto | f636ffb | 2008-07-19 01:01:22 +0900 | [diff] [blame] | 414 | /* let's register it anyway to preserve ordering */ |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 415 | slave->offset = 0; |
| 416 | slave->mtd.size = 0; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 417 | printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n", |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 418 | part->name); |
Atsushi Nemoto | f636ffb | 2008-07-19 01:01:22 +0900 | [diff] [blame] | 419 | goto out_register; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 420 | } |
| 421 | if (slave->offset + slave->mtd.size > master->size) { |
| 422 | slave->mtd.size = master->size - slave->offset; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 423 | printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n", |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 424 | part->name, master->name, slave->mtd.size); |
| 425 | } |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 426 | if (master->numeraseregions > 1) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 427 | /* Deal with variable erase size stuff */ |
Atsushi Nemoto | 6910c13 | 2008-07-19 01:00:57 +0900 | [diff] [blame] | 428 | int i, max = master->numeraseregions; |
| 429 | u32 end = slave->offset + slave->mtd.size; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 430 | struct mtd_erase_region_info *regions = master->eraseregions; |
| 431 | |
Atsushi Nemoto | 6910c13 | 2008-07-19 01:00:57 +0900 | [diff] [blame] | 432 | /* Find the first erase regions which is part of this |
| 433 | * partition. */ |
| 434 | for (i = 0; i < max && regions[i].offset <= slave->offset; i++) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 435 | ; |
Atsushi Nemoto | 6910c13 | 2008-07-19 01:00:57 +0900 | [diff] [blame] | 436 | /* The loop searched for the region _behind_ the first one */ |
| 437 | i--; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 438 | |
Atsushi Nemoto | 6910c13 | 2008-07-19 01:00:57 +0900 | [diff] [blame] | 439 | /* Pick biggest erasesize */ |
| 440 | for (; i < max && regions[i].offset < end; i++) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 441 | if (slave->mtd.erasesize < regions[i].erasesize) { |
| 442 | slave->mtd.erasesize = regions[i].erasesize; |
| 443 | } |
| 444 | } |
Atsushi Nemoto | 6910c13 | 2008-07-19 01:00:57 +0900 | [diff] [blame] | 445 | BUG_ON(slave->mtd.erasesize == 0); |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 446 | } else { |
| 447 | /* Single erase size */ |
| 448 | slave->mtd.erasesize = master->erasesize; |
| 449 | } |
| 450 | |
| 451 | if ((slave->mtd.flags & MTD_WRITEABLE) && |
| 452 | (slave->offset % slave->mtd.erasesize)) { |
| 453 | /* Doesn't start on a boundary of major erase size */ |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 454 | /* FIXME: Let it be writable if it is on a boundary of |
| 455 | * _minor_ erase size though */ |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 456 | slave->mtd.flags &= ~MTD_WRITEABLE; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 457 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n", |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 458 | part->name); |
| 459 | } |
| 460 | if ((slave->mtd.flags & MTD_WRITEABLE) && |
| 461 | (slave->mtd.size % slave->mtd.erasesize)) { |
| 462 | slave->mtd.flags &= ~MTD_WRITEABLE; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 463 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n", |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 464 | part->name); |
| 465 | } |
| 466 | |
| 467 | slave->mtd.ecclayout = master->ecclayout; |
| 468 | if (master->block_isbad) { |
| 469 | uint32_t offs = 0; |
| 470 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 471 | while (offs < slave->mtd.size) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 472 | if (master->block_isbad(master, |
| 473 | offs + slave->offset)) |
| 474 | slave->mtd.ecc_stats.badblocks++; |
| 475 | offs += slave->mtd.erasesize; |
| 476 | } |
| 477 | } |
| 478 | |
Atsushi Nemoto | f636ffb | 2008-07-19 01:01:22 +0900 | [diff] [blame] | 479 | out_register: |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 480 | if (part->mtdp) { |
| 481 | /* store the object pointer (caller may or may not register it*/ |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 482 | *part->mtdp = &slave->mtd; |
| 483 | slave->registered = 0; |
| 484 | } else { |
| 485 | /* register our partition */ |
| 486 | add_mtd_device(&slave->mtd); |
| 487 | slave->registered = 1; |
| 488 | } |
| 489 | return slave; |
| 490 | } |
| 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | /* |
| 493 | * This function, given a master MTD object and a partition table, creates |
| 494 | * and registers slave MTD objects which are bound to the master according to |
| 495 | * the partition definitions. |
| 496 | * (Q: should we register the master MTD object as well?) |
| 497 | */ |
| 498 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 499 | int add_mtd_partitions(struct mtd_info *master, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | const struct mtd_partition *parts, |
| 501 | int nbparts) |
| 502 | { |
| 503 | struct mtd_part *slave; |
| 504 | u_int32_t cur_offset = 0; |
| 505 | int i; |
| 506 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 507 | printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
| 509 | for (i = 0; i < nbparts; i++) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 510 | slave = add_one_partition(master, parts + i, i, cur_offset); |
| 511 | if (!slave) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | cur_offset = slave->offset + slave->mtd.size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | return 0; |
| 517 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | EXPORT_SYMBOL(add_mtd_partitions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
| 520 | static DEFINE_SPINLOCK(part_parser_lock); |
| 521 | static LIST_HEAD(part_parsers); |
| 522 | |
| 523 | static struct mtd_part_parser *get_partition_parser(const char *name) |
| 524 | { |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 525 | struct mtd_part_parser *p, *ret = NULL; |
| 526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | spin_lock(&part_parser_lock); |
| 528 | |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 529 | list_for_each_entry(p, &part_parsers, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | if (!strcmp(p->name, name) && try_module_get(p->owner)) { |
| 531 | ret = p; |
| 532 | break; |
| 533 | } |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | spin_unlock(&part_parser_lock); |
| 536 | |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | int register_mtd_parser(struct mtd_part_parser *p) |
| 541 | { |
| 542 | spin_lock(&part_parser_lock); |
| 543 | list_add(&p->list, &part_parsers); |
| 544 | spin_unlock(&part_parser_lock); |
| 545 | |
| 546 | return 0; |
| 547 | } |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 548 | EXPORT_SYMBOL_GPL(register_mtd_parser); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
| 550 | int deregister_mtd_parser(struct mtd_part_parser *p) |
| 551 | { |
| 552 | spin_lock(&part_parser_lock); |
| 553 | list_del(&p->list); |
| 554 | spin_unlock(&part_parser_lock); |
| 555 | return 0; |
| 556 | } |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 557 | EXPORT_SYMBOL_GPL(deregister_mtd_parser); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 559 | int parse_mtd_partitions(struct mtd_info *master, const char **types, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | struct mtd_partition **pparts, unsigned long origin) |
| 561 | { |
| 562 | struct mtd_part_parser *parser; |
| 563 | int ret = 0; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | for ( ; ret <= 0 && *types; types++) { |
| 566 | parser = get_partition_parser(*types); |
| 567 | #ifdef CONFIG_KMOD |
| 568 | if (!parser && !request_module("%s", *types)) |
| 569 | parser = get_partition_parser(*types); |
| 570 | #endif |
| 571 | if (!parser) { |
| 572 | printk(KERN_NOTICE "%s partition parsing not available\n", |
| 573 | *types); |
| 574 | continue; |
| 575 | } |
| 576 | ret = (*parser->parse_fn)(master, pparts, origin); |
| 577 | if (ret > 0) { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 578 | printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | ret, parser->name, master->name); |
| 580 | } |
| 581 | put_partition_parser(parser); |
| 582 | } |
| 583 | return ret; |
| 584 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | EXPORT_SYMBOL_GPL(parse_mtd_partitions); |