Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Simple MTD partitioning layer |
| 3 | * |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 4 | * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net> |
| 5 | * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de> |
| 6 | * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | * |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 22 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/kmod.h> |
| 30 | #include <linux/mtd/mtd.h> |
| 31 | #include <linux/mtd/partitions.h> |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 32 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | /* Our partition linked list */ |
| 35 | static LIST_HEAD(mtd_partitions); |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 36 | static DEFINE_MUTEX(mtd_partitions_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | /* Our partition node structure */ |
| 39 | struct mtd_part { |
| 40 | struct mtd_info mtd; |
| 41 | struct mtd_info *master; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 42 | uint64_t offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | /* |
| 47 | * Given a pointer to the MTD object in the mtd_part structure, we can retrieve |
| 48 | * the pointer to that structure with this macro. |
| 49 | */ |
| 50 | #define PART(x) ((struct mtd_part *)(x)) |
| 51 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 52 | |
| 53 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | * MTD methods which simply translate the effective address and pass through |
| 55 | * to the _real_ device. |
| 56 | */ |
| 57 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 58 | static int part_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 59 | size_t *retlen, u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
| 61 | struct mtd_part *part = PART(mtd); |
Yauhen Kharuzhy | d8877f1 | 2009-03-27 00:41:09 +0200 | [diff] [blame] | 62 | struct mtd_ecc_stats stats; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 63 | int res; |
| 64 | |
Yauhen Kharuzhy | d8877f1 | 2009-03-27 00:41:09 +0200 | [diff] [blame] | 65 | stats = part->master->ecc_stats; |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | if (from >= mtd->size) |
| 68 | len = 0; |
| 69 | else if (from + len > mtd->size) |
| 70 | len = mtd->size - from; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 71 | res = part->master->read(part->master, from + part->offset, |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 72 | len, retlen, buf); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 73 | if (unlikely(res)) { |
| 74 | if (res == -EUCLEAN) |
Yauhen Kharuzhy | d8877f1 | 2009-03-27 00:41:09 +0200 | [diff] [blame] | 75 | mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 76 | if (res == -EBADMSG) |
Yauhen Kharuzhy | d8877f1 | 2009-03-27 00:41:09 +0200 | [diff] [blame] | 77 | mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 78 | } |
| 79 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 82 | static int part_point(struct mtd_info *mtd, loff_t from, size_t len, |
| 83 | size_t *retlen, void **virt, resource_size_t *phys) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
| 85 | struct mtd_part *part = PART(mtd); |
| 86 | if (from >= mtd->size) |
| 87 | len = 0; |
| 88 | else if (from + len > mtd->size) |
| 89 | len = mtd->size - from; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 90 | return part->master->point (part->master, from + part->offset, |
Jared Hulbert | a98889f | 2008-04-29 23:26:49 -0700 | [diff] [blame] | 91 | len, retlen, virt, phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 93 | |
Jared Hulbert | a98889f | 2008-04-29 23:26:49 -0700 | [diff] [blame] | 94 | 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] | 95 | { |
| 96 | struct mtd_part *part = PART(mtd); |
| 97 | |
Jared Hulbert | a98889f | 2008-04-29 23:26:49 -0700 | [diff] [blame] | 98 | part->master->unpoint(part->master, from + part->offset, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 101 | static unsigned long part_get_unmapped_area(struct mtd_info *mtd, |
| 102 | unsigned long len, |
| 103 | unsigned long offset, |
| 104 | unsigned long flags) |
| 105 | { |
| 106 | struct mtd_part *part = PART(mtd); |
| 107 | |
| 108 | offset += part->offset; |
| 109 | return part->master->get_unmapped_area(part->master, len, offset, |
| 110 | flags); |
| 111 | } |
| 112 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 113 | static int part_read_oob(struct mtd_info *mtd, loff_t from, |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 114 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
| 116 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 117 | int res; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | if (from >= mtd->size) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 120 | return -EINVAL; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 121 | if (ops->datbuf && from + ops->len > mtd->size) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 122 | return -EINVAL; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 123 | |
Artem Bityutskiy | 154bf89 | 2011-01-16 17:50:54 +0200 | [diff] [blame] | 124 | /* |
| 125 | * If OOB is also requested, make sure that we do not read past the end |
| 126 | * of this partition. |
| 127 | */ |
| 128 | if (ops->oobbuf) { |
| 129 | size_t len, pages; |
| 130 | |
| 131 | if (ops->mode == MTD_OOB_AUTO) |
| 132 | len = mtd->oobavail; |
| 133 | else |
| 134 | len = mtd->oobsize; |
| 135 | pages = mtd_div_by_ws(mtd->size, mtd); |
| 136 | pages -= mtd_div_by_ws(from, mtd); |
| 137 | if (ops->ooboffs + ops->ooblen > pages * len) |
| 138 | return -EINVAL; |
| 139 | } |
| 140 | |
| 141 | res = part->master->read_oob(part->master, from + part->offset, ops); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 142 | if (unlikely(res)) { |
| 143 | if (res == -EUCLEAN) |
| 144 | mtd->ecc_stats.corrected++; |
| 145 | if (res == -EBADMSG) |
| 146 | mtd->ecc_stats.failed++; |
| 147 | } |
| 148 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 151 | static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 152 | size_t len, size_t *retlen, u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
| 154 | struct mtd_part *part = PART(mtd); |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 155 | return part->master->read_user_prot_reg(part->master, from, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | len, retlen, buf); |
| 157 | } |
| 158 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 159 | static int part_get_user_prot_info(struct mtd_info *mtd, |
| 160 | struct otp_info *buf, size_t len) |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 161 | { |
| 162 | struct mtd_part *part = PART(mtd); |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 163 | return part->master->get_user_prot_info(part->master, buf, len); |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 166 | static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, |
| 167 | size_t len, size_t *retlen, u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
| 169 | struct mtd_part *part = PART(mtd); |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 170 | return part->master->read_fact_prot_reg(part->master, from, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | len, retlen, buf); |
| 172 | } |
| 173 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 174 | static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf, |
| 175 | size_t len) |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 176 | { |
| 177 | struct mtd_part *part = PART(mtd); |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 178 | return part->master->get_fact_prot_info(part->master, buf, len); |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 181 | static int part_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 182 | size_t *retlen, const u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
| 184 | struct mtd_part *part = PART(mtd); |
| 185 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 186 | return -EROFS; |
| 187 | if (to >= mtd->size) |
| 188 | len = 0; |
| 189 | else if (to + len > mtd->size) |
| 190 | len = mtd->size - to; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 191 | return part->master->write(part->master, to + part->offset, |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 192 | len, retlen, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 195 | static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 196 | size_t *retlen, const u_char *buf) |
Richard Purdie | 388bbb0 | 2008-02-06 10:17:15 +0000 | [diff] [blame] | 197 | { |
| 198 | struct mtd_part *part = PART(mtd); |
| 199 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 200 | return -EROFS; |
| 201 | if (to >= mtd->size) |
| 202 | len = 0; |
| 203 | else if (to + len > mtd->size) |
| 204 | len = mtd->size - to; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 205 | return part->master->panic_write(part->master, to + part->offset, |
Richard Purdie | 388bbb0 | 2008-02-06 10:17:15 +0000 | [diff] [blame] | 206 | len, retlen, buf); |
| 207 | } |
| 208 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 209 | static int part_write_oob(struct mtd_info *mtd, loff_t to, |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 210 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | { |
| 212 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 215 | return -EROFS; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | if (to >= mtd->size) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 218 | return -EINVAL; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 219 | if (ops->datbuf && to + ops->len > mtd->size) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 220 | return -EINVAL; |
| 221 | return part->master->write_oob(part->master, to + part->offset, ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 224 | static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 225 | size_t len, size_t *retlen, u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
| 227 | struct mtd_part *part = PART(mtd); |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 228 | return part->master->write_user_prot_reg(part->master, from, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | len, retlen, buf); |
| 230 | } |
| 231 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 232 | static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 233 | size_t len) |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 234 | { |
| 235 | struct mtd_part *part = PART(mtd); |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 236 | return part->master->lock_user_prot_reg(part->master, from, len); |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 239 | static int part_writev(struct mtd_info *mtd, const struct kvec *vecs, |
| 240 | unsigned long count, loff_t to, size_t *retlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
| 242 | struct mtd_part *part = PART(mtd); |
| 243 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 244 | return -EROFS; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 245 | return part->master->writev(part->master, vecs, count, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | to + part->offset, retlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 249 | static int part_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
| 251 | struct mtd_part *part = PART(mtd); |
| 252 | int ret; |
| 253 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 254 | return -EROFS; |
| 255 | if (instr->addr >= mtd->size) |
| 256 | return -EINVAL; |
| 257 | instr->addr += part->offset; |
| 258 | ret = part->master->erase(part->master, instr); |
Adrian Hunter | 74641d7 | 2007-03-08 12:20:12 +0200 | [diff] [blame] | 259 | if (ret) { |
Adrian Hunter | bb0eb21 | 2008-08-12 12:40:50 +0300 | [diff] [blame] | 260 | if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) |
Adrian Hunter | 74641d7 | 2007-03-08 12:20:12 +0200 | [diff] [blame] | 261 | instr->fail_addr -= part->offset; |
| 262 | instr->addr -= part->offset; |
| 263 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | void mtd_erase_callback(struct erase_info *instr) |
| 268 | { |
| 269 | if (instr->mtd->erase == part_erase) { |
| 270 | struct mtd_part *part = PART(instr->mtd); |
| 271 | |
Adrian Hunter | bb0eb21 | 2008-08-12 12:40:50 +0300 | [diff] [blame] | 272 | if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | instr->fail_addr -= part->offset; |
| 274 | instr->addr -= part->offset; |
| 275 | } |
| 276 | if (instr->callback) |
| 277 | instr->callback(instr); |
| 278 | } |
| 279 | EXPORT_SYMBOL_GPL(mtd_erase_callback); |
| 280 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 281 | static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | { |
| 283 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 284 | if ((len + ofs) > mtd->size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | return -EINVAL; |
| 286 | return part->master->lock(part->master, ofs + part->offset, len); |
| 287 | } |
| 288 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 289 | static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
| 291 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 292 | if ((len + ofs) > mtd->size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | return -EINVAL; |
| 294 | return part->master->unlock(part->master, ofs + part->offset, len); |
| 295 | } |
| 296 | |
Richard Cochran | 9938424 | 2010-06-14 18:10:33 +0200 | [diff] [blame] | 297 | static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 298 | { |
| 299 | struct mtd_part *part = PART(mtd); |
| 300 | if ((len + ofs) > mtd->size) |
| 301 | return -EINVAL; |
| 302 | return part->master->is_locked(part->master, ofs + part->offset, len); |
| 303 | } |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | static void part_sync(struct mtd_info *mtd) |
| 306 | { |
| 307 | struct mtd_part *part = PART(mtd); |
| 308 | part->master->sync(part->master); |
| 309 | } |
| 310 | |
| 311 | static int part_suspend(struct mtd_info *mtd) |
| 312 | { |
| 313 | struct mtd_part *part = PART(mtd); |
| 314 | return part->master->suspend(part->master); |
| 315 | } |
| 316 | |
| 317 | static void part_resume(struct mtd_info *mtd) |
| 318 | { |
| 319 | struct mtd_part *part = PART(mtd); |
| 320 | part->master->resume(part->master); |
| 321 | } |
| 322 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 323 | static int part_block_isbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
| 325 | struct mtd_part *part = PART(mtd); |
| 326 | if (ofs >= mtd->size) |
| 327 | return -EINVAL; |
| 328 | ofs += part->offset; |
| 329 | return part->master->block_isbad(part->master, ofs); |
| 330 | } |
| 331 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 332 | static int part_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { |
| 334 | struct mtd_part *part = PART(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 335 | int res; |
| 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 338 | return -EROFS; |
| 339 | if (ofs >= mtd->size) |
| 340 | return -EINVAL; |
| 341 | ofs += part->offset; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 342 | res = part->master->block_markbad(part->master, ofs); |
| 343 | if (!res) |
| 344 | mtd->ecc_stats.badblocks++; |
| 345 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 348 | static inline void free_partition(struct mtd_part *p) |
| 349 | { |
| 350 | kfree(p->mtd.name); |
| 351 | kfree(p); |
| 352 | } |
| 353 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 354 | /* |
| 355 | * This function unregisters and destroy all slave MTD objects which are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | * attached to the given master MTD object. |
| 357 | */ |
| 358 | |
| 359 | int del_mtd_partitions(struct mtd_info *master) |
| 360 | { |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 361 | struct mtd_part *slave, *next; |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 362 | int ret, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 364 | mutex_lock(&mtd_partitions_mutex); |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 365 | list_for_each_entry_safe(slave, next, &mtd_partitions, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (slave->master == master) { |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 367 | ret = del_mtd_device(&slave->mtd); |
| 368 | if (ret < 0) { |
| 369 | err = ret; |
| 370 | continue; |
| 371 | } |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 372 | list_del(&slave->list); |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 373 | free_partition(slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 375 | mutex_unlock(&mtd_partitions_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 377 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 379 | EXPORT_SYMBOL(del_mtd_partitions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 381 | static struct mtd_part *allocate_partition(struct mtd_info *master, |
| 382 | const struct mtd_partition *part, int partno, |
| 383 | uint64_t cur_offset) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 384 | { |
| 385 | struct mtd_part *slave; |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 386 | char *name; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 387 | |
| 388 | /* allocate the partition structure */ |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 389 | slave = kzalloc(sizeof(*slave), GFP_KERNEL); |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 390 | name = kstrdup(part->name, GFP_KERNEL); |
| 391 | if (!name || !slave) { |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 392 | printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n", |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 393 | master->name); |
| 394 | kfree(name); |
| 395 | kfree(slave); |
| 396 | return ERR_PTR(-ENOMEM); |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 397 | } |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 398 | |
| 399 | /* set up the MTD object for this partition */ |
| 400 | slave->mtd.type = master->type; |
| 401 | slave->mtd.flags = master->flags & ~part->mask_flags; |
| 402 | slave->mtd.size = part->size; |
| 403 | slave->mtd.writesize = master->writesize; |
Anatolij Gustschin | 7fa33ac | 2010-12-16 23:42:18 +0100 | [diff] [blame] | 404 | slave->mtd.writebufsize = master->writebufsize; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 405 | slave->mtd.oobsize = master->oobsize; |
| 406 | slave->mtd.oobavail = master->oobavail; |
| 407 | slave->mtd.subpage_sft = master->subpage_sft; |
| 408 | |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 409 | slave->mtd.name = name; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 410 | slave->mtd.owner = master->owner; |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 411 | slave->mtd.backing_dev_info = master->backing_dev_info; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 412 | |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 413 | /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone |
| 414 | * to have the same data be in two different partitions. |
| 415 | */ |
| 416 | slave->mtd.dev.parent = master->dev.parent; |
| 417 | |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 418 | slave->mtd.read = part_read; |
| 419 | slave->mtd.write = part_write; |
| 420 | |
| 421 | if (master->panic_write) |
| 422 | slave->mtd.panic_write = part_panic_write; |
| 423 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 424 | if (master->point && master->unpoint) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 425 | slave->mtd.point = part_point; |
| 426 | slave->mtd.unpoint = part_unpoint; |
| 427 | } |
| 428 | |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 429 | if (master->get_unmapped_area) |
| 430 | slave->mtd.get_unmapped_area = part_get_unmapped_area; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 431 | if (master->read_oob) |
| 432 | slave->mtd.read_oob = part_read_oob; |
| 433 | if (master->write_oob) |
| 434 | slave->mtd.write_oob = part_write_oob; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 435 | if (master->read_user_prot_reg) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 436 | slave->mtd.read_user_prot_reg = part_read_user_prot_reg; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 437 | if (master->read_fact_prot_reg) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 438 | slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 439 | if (master->write_user_prot_reg) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 440 | slave->mtd.write_user_prot_reg = part_write_user_prot_reg; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 441 | if (master->lock_user_prot_reg) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 442 | slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 443 | if (master->get_user_prot_info) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 444 | slave->mtd.get_user_prot_info = part_get_user_prot_info; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 445 | if (master->get_fact_prot_info) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 446 | slave->mtd.get_fact_prot_info = part_get_fact_prot_info; |
| 447 | if (master->sync) |
| 448 | slave->mtd.sync = part_sync; |
David Woodhouse | 4704a78 | 2009-04-05 07:56:23 -0700 | [diff] [blame] | 449 | if (!partno && !master->dev.class && master->suspend && master->resume) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 450 | slave->mtd.suspend = part_suspend; |
| 451 | slave->mtd.resume = part_resume; |
| 452 | } |
| 453 | if (master->writev) |
| 454 | slave->mtd.writev = part_writev; |
| 455 | if (master->lock) |
| 456 | slave->mtd.lock = part_lock; |
| 457 | if (master->unlock) |
| 458 | slave->mtd.unlock = part_unlock; |
Richard Cochran | 9938424 | 2010-06-14 18:10:33 +0200 | [diff] [blame] | 459 | if (master->is_locked) |
| 460 | slave->mtd.is_locked = part_is_locked; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 461 | if (master->block_isbad) |
| 462 | slave->mtd.block_isbad = part_block_isbad; |
| 463 | if (master->block_markbad) |
| 464 | slave->mtd.block_markbad = part_block_markbad; |
| 465 | slave->mtd.erase = part_erase; |
| 466 | slave->master = master; |
| 467 | slave->offset = part->offset; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 468 | |
| 469 | if (slave->offset == MTDPART_OFS_APPEND) |
| 470 | slave->offset = cur_offset; |
| 471 | if (slave->offset == MTDPART_OFS_NXTBLK) { |
| 472 | slave->offset = cur_offset; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 473 | if (mtd_mod_by_eb(cur_offset, master) != 0) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 474 | /* Round up to next erasesize */ |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 475 | slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 476 | printk(KERN_NOTICE "Moving partition %d: " |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 477 | "0x%012llx -> 0x%012llx\n", partno, |
| 478 | (unsigned long long)cur_offset, (unsigned long long)slave->offset); |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | if (slave->mtd.size == MTDPART_SIZ_FULL) |
| 482 | slave->mtd.size = master->size - slave->offset; |
| 483 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 484 | printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset, |
| 485 | (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name); |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 486 | |
| 487 | /* let's do some sanity checks */ |
| 488 | if (slave->offset >= master->size) { |
Atsushi Nemoto | f636ffb | 2008-07-19 01:01:22 +0900 | [diff] [blame] | 489 | /* let's register it anyway to preserve ordering */ |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 490 | slave->offset = 0; |
| 491 | slave->mtd.size = 0; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 492 | printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n", |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 493 | part->name); |
Atsushi Nemoto | f636ffb | 2008-07-19 01:01:22 +0900 | [diff] [blame] | 494 | goto out_register; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 495 | } |
| 496 | if (slave->offset + slave->mtd.size > master->size) { |
| 497 | slave->mtd.size = master->size - slave->offset; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 498 | printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n", |
| 499 | part->name, master->name, (unsigned long long)slave->mtd.size); |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 500 | } |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 501 | if (master->numeraseregions > 1) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 502 | /* Deal with variable erase size stuff */ |
Atsushi Nemoto | 6910c13 | 2008-07-19 01:00:57 +0900 | [diff] [blame] | 503 | int i, max = master->numeraseregions; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 504 | u64 end = slave->offset + slave->mtd.size; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 505 | struct mtd_erase_region_info *regions = master->eraseregions; |
| 506 | |
Atsushi Nemoto | 6910c13 | 2008-07-19 01:00:57 +0900 | [diff] [blame] | 507 | /* Find the first erase regions which is part of this |
| 508 | * partition. */ |
| 509 | for (i = 0; i < max && regions[i].offset <= slave->offset; i++) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 510 | ; |
Atsushi Nemoto | 6910c13 | 2008-07-19 01:00:57 +0900 | [diff] [blame] | 511 | /* The loop searched for the region _behind_ the first one */ |
Roel Kluin | a57ca04 | 2009-09-18 12:51:50 -0700 | [diff] [blame] | 512 | if (i > 0) |
| 513 | i--; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 514 | |
Atsushi Nemoto | 6910c13 | 2008-07-19 01:00:57 +0900 | [diff] [blame] | 515 | /* Pick biggest erasesize */ |
| 516 | for (; i < max && regions[i].offset < end; i++) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 517 | if (slave->mtd.erasesize < regions[i].erasesize) { |
| 518 | slave->mtd.erasesize = regions[i].erasesize; |
| 519 | } |
| 520 | } |
Atsushi Nemoto | 6910c13 | 2008-07-19 01:00:57 +0900 | [diff] [blame] | 521 | BUG_ON(slave->mtd.erasesize == 0); |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 522 | } else { |
| 523 | /* Single erase size */ |
| 524 | slave->mtd.erasesize = master->erasesize; |
| 525 | } |
| 526 | |
| 527 | if ((slave->mtd.flags & MTD_WRITEABLE) && |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 528 | mtd_mod_by_eb(slave->offset, &slave->mtd)) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 529 | /* Doesn't start on a boundary of major erase size */ |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 530 | /* FIXME: Let it be writable if it is on a boundary of |
| 531 | * _minor_ erase size though */ |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 532 | slave->mtd.flags &= ~MTD_WRITEABLE; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 533 | 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] | 534 | part->name); |
| 535 | } |
| 536 | if ((slave->mtd.flags & MTD_WRITEABLE) && |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 537 | mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 538 | slave->mtd.flags &= ~MTD_WRITEABLE; |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 539 | 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] | 540 | part->name); |
| 541 | } |
| 542 | |
| 543 | slave->mtd.ecclayout = master->ecclayout; |
| 544 | if (master->block_isbad) { |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 545 | uint64_t offs = 0; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 546 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 547 | while (offs < slave->mtd.size) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 548 | if (master->block_isbad(master, |
| 549 | offs + slave->offset)) |
| 550 | slave->mtd.ecc_stats.badblocks++; |
| 551 | offs += slave->mtd.erasesize; |
| 552 | } |
| 553 | } |
| 554 | |
Atsushi Nemoto | f636ffb | 2008-07-19 01:01:22 +0900 | [diff] [blame] | 555 | out_register: |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 556 | return slave; |
| 557 | } |
| 558 | |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 559 | int mtd_add_partition(struct mtd_info *master, char *name, |
| 560 | long long offset, long long length) |
| 561 | { |
| 562 | struct mtd_partition part; |
| 563 | struct mtd_part *p, *new; |
| 564 | uint64_t start, end; |
| 565 | int ret = 0; |
| 566 | |
| 567 | /* the direct offset is expected */ |
| 568 | if (offset == MTDPART_OFS_APPEND || |
| 569 | offset == MTDPART_OFS_NXTBLK) |
| 570 | return -EINVAL; |
| 571 | |
| 572 | if (length == MTDPART_SIZ_FULL) |
| 573 | length = master->size - offset; |
| 574 | |
| 575 | if (length <= 0) |
| 576 | return -EINVAL; |
| 577 | |
| 578 | part.name = name; |
| 579 | part.size = length; |
| 580 | part.offset = offset; |
| 581 | part.mask_flags = 0; |
| 582 | part.ecclayout = NULL; |
| 583 | |
| 584 | new = allocate_partition(master, &part, -1, offset); |
| 585 | if (IS_ERR(new)) |
| 586 | return PTR_ERR(new); |
| 587 | |
| 588 | start = offset; |
| 589 | end = offset + length; |
| 590 | |
| 591 | mutex_lock(&mtd_partitions_mutex); |
| 592 | list_for_each_entry(p, &mtd_partitions, list) |
| 593 | if (p->master == master) { |
| 594 | if ((start >= p->offset) && |
| 595 | (start < (p->offset + p->mtd.size))) |
| 596 | goto err_inv; |
| 597 | |
| 598 | if ((end >= p->offset) && |
| 599 | (end < (p->offset + p->mtd.size))) |
| 600 | goto err_inv; |
| 601 | } |
| 602 | |
| 603 | list_add(&new->list, &mtd_partitions); |
| 604 | mutex_unlock(&mtd_partitions_mutex); |
| 605 | |
| 606 | add_mtd_device(&new->mtd); |
| 607 | |
| 608 | return ret; |
| 609 | err_inv: |
| 610 | mutex_unlock(&mtd_partitions_mutex); |
| 611 | free_partition(new); |
| 612 | return -EINVAL; |
| 613 | } |
| 614 | EXPORT_SYMBOL_GPL(mtd_add_partition); |
| 615 | |
| 616 | int mtd_del_partition(struct mtd_info *master, int partno) |
| 617 | { |
| 618 | struct mtd_part *slave, *next; |
| 619 | int ret = -EINVAL; |
| 620 | |
| 621 | mutex_lock(&mtd_partitions_mutex); |
| 622 | list_for_each_entry_safe(slave, next, &mtd_partitions, list) |
| 623 | if ((slave->master == master) && |
| 624 | (slave->mtd.index == partno)) { |
| 625 | ret = del_mtd_device(&slave->mtd); |
| 626 | if (ret < 0) |
| 627 | break; |
| 628 | |
| 629 | list_del(&slave->list); |
| 630 | free_partition(slave); |
| 631 | break; |
| 632 | } |
| 633 | mutex_unlock(&mtd_partitions_mutex); |
| 634 | |
| 635 | return ret; |
| 636 | } |
| 637 | EXPORT_SYMBOL_GPL(mtd_del_partition); |
| 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | /* |
| 640 | * This function, given a master MTD object and a partition table, creates |
| 641 | * and registers slave MTD objects which are bound to the master according to |
| 642 | * the partition definitions. |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 643 | * |
| 644 | * We don't register the master, or expect the caller to have done so, |
| 645 | * for reasons of data integrity. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | */ |
| 647 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 648 | int add_mtd_partitions(struct mtd_info *master, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | const struct mtd_partition *parts, |
| 650 | int nbparts) |
| 651 | { |
| 652 | struct mtd_part *slave; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 653 | uint64_t cur_offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | int i; |
| 655 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 656 | 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] | 657 | |
| 658 | for (i = 0; i < nbparts; i++) { |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 659 | slave = allocate_partition(master, parts + i, i, cur_offset); |
| 660 | if (IS_ERR(slave)) |
| 661 | return PTR_ERR(slave); |
| 662 | |
| 663 | mutex_lock(&mtd_partitions_mutex); |
| 664 | list_add(&slave->list, &mtd_partitions); |
| 665 | mutex_unlock(&mtd_partitions_mutex); |
| 666 | |
| 667 | add_mtd_device(&slave->mtd); |
| 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | cur_offset = slave->offset + slave->mtd.size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | return 0; |
| 673 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | EXPORT_SYMBOL(add_mtd_partitions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
| 676 | static DEFINE_SPINLOCK(part_parser_lock); |
| 677 | static LIST_HEAD(part_parsers); |
| 678 | |
| 679 | static struct mtd_part_parser *get_partition_parser(const char *name) |
| 680 | { |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 681 | struct mtd_part_parser *p, *ret = NULL; |
| 682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | spin_lock(&part_parser_lock); |
| 684 | |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 685 | list_for_each_entry(p, &part_parsers, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | if (!strcmp(p->name, name) && try_module_get(p->owner)) { |
| 687 | ret = p; |
| 688 | break; |
| 689 | } |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | spin_unlock(&part_parser_lock); |
| 692 | |
| 693 | return ret; |
| 694 | } |
| 695 | |
| 696 | int register_mtd_parser(struct mtd_part_parser *p) |
| 697 | { |
| 698 | spin_lock(&part_parser_lock); |
| 699 | list_add(&p->list, &part_parsers); |
| 700 | spin_unlock(&part_parser_lock); |
| 701 | |
| 702 | return 0; |
| 703 | } |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 704 | EXPORT_SYMBOL_GPL(register_mtd_parser); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
| 706 | int deregister_mtd_parser(struct mtd_part_parser *p) |
| 707 | { |
| 708 | spin_lock(&part_parser_lock); |
| 709 | list_del(&p->list); |
| 710 | spin_unlock(&part_parser_lock); |
| 711 | return 0; |
| 712 | } |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 713 | EXPORT_SYMBOL_GPL(deregister_mtd_parser); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 715 | int parse_mtd_partitions(struct mtd_info *master, const char **types, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | struct mtd_partition **pparts, unsigned long origin) |
| 717 | { |
| 718 | struct mtd_part_parser *parser; |
| 719 | int ret = 0; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | for ( ; ret <= 0 && *types; types++) { |
| 722 | parser = get_partition_parser(*types); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | if (!parser && !request_module("%s", *types)) |
| 724 | parser = get_partition_parser(*types); |
Artem Bityutskiy | 7c802fb | 2011-05-17 11:13:17 +0300 | [diff] [blame^] | 725 | if (!parser) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | ret = (*parser->parse_fn)(master, pparts, origin); |
| 728 | if (ret > 0) { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 729 | printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | ret, parser->name, master->name); |
| 731 | } |
| 732 | put_partition_parser(parser); |
| 733 | } |
| 734 | return ret; |
| 735 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | EXPORT_SYMBOL_GPL(parse_mtd_partitions); |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 737 | |
Roman Tereshonkov | a7e93dc | 2010-11-23 14:17:17 +0200 | [diff] [blame] | 738 | int mtd_is_partition(struct mtd_info *mtd) |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 739 | { |
| 740 | struct mtd_part *part; |
Roman Tereshonkov | a7e93dc | 2010-11-23 14:17:17 +0200 | [diff] [blame] | 741 | int ispart = 0; |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 742 | |
| 743 | mutex_lock(&mtd_partitions_mutex); |
| 744 | list_for_each_entry(part, &mtd_partitions, list) |
| 745 | if (&part->mtd == mtd) { |
Roman Tereshonkov | a7e93dc | 2010-11-23 14:17:17 +0200 | [diff] [blame] | 746 | ispart = 1; |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 747 | break; |
| 748 | } |
| 749 | mutex_unlock(&mtd_partitions_mutex); |
| 750 | |
Roman Tereshonkov | a7e93dc | 2010-11-23 14:17:17 +0200 | [diff] [blame] | 751 | return ispart; |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 752 | } |
Roman Tereshonkov | a7e93dc | 2010-11-23 14:17:17 +0200 | [diff] [blame] | 753 | EXPORT_SYMBOL_GPL(mtd_is_partition); |