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