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