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