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