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