Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Simple MTD partitioning layer |
| 4 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5 | * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net> |
| 6 | * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de> |
| 7 | * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org> |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 8 | * |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 11 | #ifndef __UBOOT__ |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/kmod.h> |
| 18 | #endif |
| 19 | |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 20 | #include <common.h> |
| 21 | #include <malloc.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 22 | #include <linux/errno.h> |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 23 | #include <linux/compat.h> |
| 24 | #include <ubi_uboot.h> |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 25 | |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 26 | #include <linux/mtd/mtd.h> |
| 27 | #include <linux/mtd/partitions.h> |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 28 | #include <linux/err.h> |
Miquel Raynal | 6f381d5 | 2018-09-29 12:58:25 +0200 | [diff] [blame^] | 29 | #include <linux/sizes.h> |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 30 | |
| 31 | #include "mtdcore.h" |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 32 | |
| 33 | /* Our partition linked list */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 34 | static LIST_HEAD(mtd_partitions); |
| 35 | #ifndef __UBOOT__ |
| 36 | static DEFINE_MUTEX(mtd_partitions_mutex); |
| 37 | #else |
| 38 | DEFINE_MUTEX(mtd_partitions_mutex); |
| 39 | #endif |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 40 | |
| 41 | /* Our partition node structure */ |
| 42 | struct mtd_part { |
| 43 | struct mtd_info mtd; |
| 44 | struct mtd_info *master; |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 45 | uint64_t offset; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 46 | struct list_head list; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [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 | |
| 55 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 56 | #ifdef __UBOOT__ |
| 57 | /* from mm/util.c */ |
| 58 | |
| 59 | /** |
| 60 | * kstrdup - allocate space for and copy an existing string |
| 61 | * @s: the string to duplicate |
| 62 | * @gfp: the GFP mask used in the kmalloc() call when allocating memory |
| 63 | */ |
| 64 | char *kstrdup(const char *s, gfp_t gfp) |
| 65 | { |
| 66 | size_t len; |
| 67 | char *buf; |
| 68 | |
| 69 | if (!s) |
| 70 | return NULL; |
| 71 | |
| 72 | len = strlen(s) + 1; |
| 73 | buf = kmalloc(len, gfp); |
| 74 | if (buf) |
| 75 | memcpy(buf, s, len); |
| 76 | return buf; |
| 77 | } |
| 78 | #endif |
| 79 | |
Miquel Raynal | 6f381d5 | 2018-09-29 12:58:25 +0200 | [diff] [blame^] | 80 | #define MTD_SIZE_REMAINING (~0LLU) |
| 81 | #define MTD_OFFSET_NOT_SPECIFIED (~0LLU) |
| 82 | |
| 83 | /** |
| 84 | * mtd_parse_partition - Parse @mtdparts partition definition, fill @partition |
| 85 | * with it and update the @mtdparts string pointer. |
| 86 | * |
| 87 | * The partition name is allocated and must be freed by the caller. |
| 88 | * |
| 89 | * This function is widely inspired from part_parse (mtdparts.c). |
| 90 | * |
| 91 | * @mtdparts: String describing the partition with mtdparts command syntax |
| 92 | * @partition: MTD partition structure to fill |
| 93 | * |
| 94 | * @return 0 on success, an error otherwise. |
| 95 | */ |
| 96 | static int mtd_parse_partition(const char **_mtdparts, |
| 97 | struct mtd_partition *partition) |
| 98 | { |
| 99 | const char *mtdparts = *_mtdparts; |
| 100 | const char *name = NULL; |
| 101 | int name_len; |
| 102 | char *buf; |
| 103 | |
| 104 | /* Ensure the partition structure is empty */ |
| 105 | memset(partition, 0, sizeof(struct mtd_partition)); |
| 106 | |
| 107 | /* Fetch the partition size */ |
| 108 | if (*mtdparts == '-') { |
| 109 | /* Assign all remaining space to this partition */ |
| 110 | partition->size = MTD_SIZE_REMAINING; |
| 111 | mtdparts++; |
| 112 | } else { |
| 113 | partition->size = ustrtoull(mtdparts, (char **)&mtdparts, 0); |
| 114 | if (partition->size < SZ_4K) { |
| 115 | printf("Minimum partition size 4kiB, %lldB requested\n", |
| 116 | partition->size); |
| 117 | return -EINVAL; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* Check for the offset */ |
| 122 | partition->offset = MTD_OFFSET_NOT_SPECIFIED; |
| 123 | if (*mtdparts == '@') { |
| 124 | mtdparts++; |
| 125 | partition->offset = ustrtoull(mtdparts, (char **)&mtdparts, 0); |
| 126 | } |
| 127 | |
| 128 | /* Now look for the name */ |
| 129 | if (*mtdparts == '(') { |
| 130 | name = ++mtdparts; |
| 131 | mtdparts = strchr(name, ')'); |
| 132 | if (!mtdparts) { |
| 133 | printf("No closing ')' found in partition name\n"); |
| 134 | return -EINVAL; |
| 135 | } |
| 136 | name_len = mtdparts - name + 1; |
| 137 | if ((name_len - 1) == 0) { |
| 138 | printf("Empty partition name\n"); |
| 139 | return -EINVAL; |
| 140 | } |
| 141 | mtdparts++; |
| 142 | } else { |
| 143 | /* Name will be of the form size@offset */ |
| 144 | name_len = 22; |
| 145 | } |
| 146 | |
| 147 | /* Check if the partition is read-only */ |
| 148 | if (strncmp(mtdparts, "ro", 2) == 0) { |
| 149 | partition->mask_flags |= MTD_WRITEABLE; |
| 150 | mtdparts += 2; |
| 151 | } |
| 152 | |
| 153 | /* Check for a potential next partition definition */ |
| 154 | if (*mtdparts == ',') { |
| 155 | if (partition->size == MTD_SIZE_REMAINING) { |
| 156 | printf("No partitions allowed after a fill-up\n"); |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | ++mtdparts; |
| 160 | } else if ((*mtdparts == ';') || (*mtdparts == '\0')) { |
| 161 | /* NOP */ |
| 162 | } else { |
| 163 | printf("Unexpected character '%c' in mtdparts\n", *mtdparts); |
| 164 | return -EINVAL; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Allocate a buffer for the name and either copy the provided name or |
| 169 | * auto-generate it with the form 'size@offset'. |
| 170 | */ |
| 171 | buf = malloc(name_len); |
| 172 | if (!buf) |
| 173 | return -ENOMEM; |
| 174 | |
| 175 | if (name) |
| 176 | strncpy(buf, name, name_len - 1); |
| 177 | else |
| 178 | snprintf(buf, name_len, "0x%08llx@0x%08llx", |
| 179 | partition->size, partition->offset); |
| 180 | |
| 181 | buf[name_len - 1] = '\0'; |
| 182 | partition->name = buf; |
| 183 | |
| 184 | *_mtdparts = mtdparts; |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * mtd_parse_partitions - Create a partition array from an mtdparts definition |
| 191 | * |
| 192 | * Stateless function that takes a @parent MTD device, a string @_mtdparts |
| 193 | * describing the partitions (with the "mtdparts" command syntax) and creates |
| 194 | * the corresponding MTD partition structure array @_parts. Both the name and |
| 195 | * the structure partition itself must be freed freed, the caller may use |
| 196 | * @mtd_free_parsed_partitions() for this purpose. |
| 197 | * |
| 198 | * @parent: MTD device which contains the partitions |
| 199 | * @_mtdparts: Pointer to a string describing the partitions with "mtdparts" |
| 200 | * command syntax. |
| 201 | * @_parts: Allocated array containing the partitions, must be freed by the |
| 202 | * caller. |
| 203 | * @_nparts: Size of @_parts array. |
| 204 | * |
| 205 | * @return 0 on success, an error otherwise. |
| 206 | */ |
| 207 | int mtd_parse_partitions(struct mtd_info *parent, const char **_mtdparts, |
| 208 | struct mtd_partition **_parts, int *_nparts) |
| 209 | { |
| 210 | struct mtd_partition partition = {}, *parts; |
| 211 | const char *mtdparts = *_mtdparts; |
| 212 | int cur_off = 0, cur_sz = 0; |
| 213 | int nparts = 0; |
| 214 | int ret, idx; |
| 215 | u64 sz; |
| 216 | |
| 217 | /* First, iterate over the partitions until we know their number */ |
| 218 | while (mtdparts[0] != '\0' && mtdparts[0] != ';') { |
| 219 | ret = mtd_parse_partition(&mtdparts, &partition); |
| 220 | if (ret) |
| 221 | return ret; |
| 222 | |
| 223 | free((char *)partition.name); |
| 224 | nparts++; |
| 225 | } |
| 226 | |
| 227 | /* Allocate an array of partitions to give back to the caller */ |
| 228 | parts = malloc(sizeof(*parts) * nparts); |
| 229 | if (!parts) { |
| 230 | printf("Not enough space to save partitions meta-data\n"); |
| 231 | return -ENOMEM; |
| 232 | } |
| 233 | |
| 234 | /* Iterate again over each partition to save the data in our array */ |
| 235 | for (idx = 0; idx < nparts; idx++) { |
| 236 | ret = mtd_parse_partition(_mtdparts, &parts[idx]); |
| 237 | if (ret) |
| 238 | return ret; |
| 239 | |
| 240 | if (parts[idx].size == MTD_SIZE_REMAINING) |
| 241 | parts[idx].size = parent->size - cur_sz; |
| 242 | cur_sz += parts[idx].size; |
| 243 | |
| 244 | sz = parts[idx].size; |
| 245 | if (sz < parent->writesize || do_div(sz, parent->writesize)) { |
| 246 | printf("Partition size must be a multiple of %d\n", |
| 247 | parent->writesize); |
| 248 | return -EINVAL; |
| 249 | } |
| 250 | |
| 251 | if (parts[idx].offset == MTD_OFFSET_NOT_SPECIFIED) |
| 252 | parts[idx].offset = cur_off; |
| 253 | cur_off += parts[idx].size; |
| 254 | |
| 255 | parts[idx].ecclayout = parent->ecclayout; |
| 256 | } |
| 257 | |
| 258 | /* Offset by one mtdparts to point to the next device if any */ |
| 259 | if (*_mtdparts[0] == ';') |
| 260 | (*_mtdparts)++; |
| 261 | |
| 262 | *_parts = parts; |
| 263 | *_nparts = nparts; |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * mtd_free_parsed_partitions - Free dynamically allocated partitions |
| 270 | * |
| 271 | * Each successful call to @mtd_parse_partitions must be followed by a call to |
| 272 | * @mtd_free_parsed_partitions to free any allocated array during the parsing |
| 273 | * process. |
| 274 | * |
| 275 | * @parts: Array containing the partitions that will be freed. |
| 276 | * @nparts: Size of @parts array. |
| 277 | */ |
| 278 | void mtd_free_parsed_partitions(struct mtd_partition *parts, |
| 279 | unsigned int nparts) |
| 280 | { |
| 281 | int i; |
| 282 | |
| 283 | for (i = 0; i < nparts; i++) |
| 284 | free((char *)parts[i].name); |
| 285 | |
| 286 | free(parts); |
| 287 | } |
| 288 | |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 289 | /* |
| 290 | * MTD methods which simply translate the effective address and pass through |
| 291 | * to the _real_ device. |
| 292 | */ |
| 293 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 294 | static int part_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 295 | size_t *retlen, u_char *buf) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 296 | { |
| 297 | struct mtd_part *part = PART(mtd); |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 298 | struct mtd_ecc_stats stats; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 299 | int res; |
| 300 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 301 | stats = part->master->ecc_stats; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 302 | res = part->master->_read(part->master, from + part->offset, len, |
| 303 | retlen, buf); |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 304 | if (unlikely(mtd_is_eccerr(res))) |
| 305 | mtd->ecc_stats.failed += |
| 306 | part->master->ecc_stats.failed - stats.failed; |
| 307 | else |
| 308 | mtd->ecc_stats.corrected += |
| 309 | part->master->ecc_stats.corrected - stats.corrected; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 310 | return res; |
| 311 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 312 | |
| 313 | #ifndef __UBOOT__ |
| 314 | static int part_point(struct mtd_info *mtd, loff_t from, size_t len, |
| 315 | size_t *retlen, void **virt, resource_size_t *phys) |
| 316 | { |
| 317 | struct mtd_part *part = PART(mtd); |
| 318 | |
| 319 | return part->master->_point(part->master, from + part->offset, len, |
| 320 | retlen, virt, phys); |
| 321 | } |
| 322 | |
| 323 | static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len) |
| 324 | { |
| 325 | struct mtd_part *part = PART(mtd); |
| 326 | |
| 327 | return part->master->_unpoint(part->master, from + part->offset, len); |
| 328 | } |
| 329 | #endif |
| 330 | |
| 331 | static unsigned long part_get_unmapped_area(struct mtd_info *mtd, |
| 332 | unsigned long len, |
| 333 | unsigned long offset, |
| 334 | unsigned long flags) |
| 335 | { |
| 336 | struct mtd_part *part = PART(mtd); |
| 337 | |
| 338 | offset += part->offset; |
| 339 | return part->master->_get_unmapped_area(part->master, len, offset, |
| 340 | flags); |
| 341 | } |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 342 | |
| 343 | static int part_read_oob(struct mtd_info *mtd, loff_t from, |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 344 | struct mtd_oob_ops *ops) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 345 | { |
| 346 | struct mtd_part *part = PART(mtd); |
| 347 | int res; |
| 348 | |
| 349 | if (from >= mtd->size) |
| 350 | return -EINVAL; |
| 351 | if (ops->datbuf && from + ops->len > mtd->size) |
| 352 | return -EINVAL; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 353 | |
| 354 | /* |
| 355 | * If OOB is also requested, make sure that we do not read past the end |
| 356 | * of this partition. |
| 357 | */ |
| 358 | if (ops->oobbuf) { |
| 359 | size_t len, pages; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 360 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 361 | if (ops->mode == MTD_OPS_AUTO_OOB) |
| 362 | len = mtd->oobavail; |
| 363 | else |
| 364 | len = mtd->oobsize; |
| 365 | pages = mtd_div_by_ws(mtd->size, mtd); |
| 366 | pages -= mtd_div_by_ws(from, mtd); |
| 367 | if (ops->ooboffs + ops->ooblen > pages * len) |
| 368 | return -EINVAL; |
| 369 | } |
| 370 | |
| 371 | res = part->master->_read_oob(part->master, from + part->offset, ops); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 372 | if (unlikely(res)) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 373 | if (mtd_is_bitflip(res)) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 374 | mtd->ecc_stats.corrected++; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 375 | if (mtd_is_eccerr(res)) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 376 | mtd->ecc_stats.failed++; |
| 377 | } |
| 378 | return res; |
| 379 | } |
| 380 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 381 | static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 382 | size_t len, size_t *retlen, u_char *buf) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 383 | { |
| 384 | struct mtd_part *part = PART(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 385 | return part->master->_read_user_prot_reg(part->master, from, len, |
| 386 | retlen, buf); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 387 | } |
| 388 | |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 389 | static int part_get_user_prot_info(struct mtd_info *mtd, size_t len, |
| 390 | size_t *retlen, struct otp_info *buf) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 391 | { |
| 392 | struct mtd_part *part = PART(mtd); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 393 | return part->master->_get_user_prot_info(part->master, len, retlen, |
| 394 | buf); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 395 | } |
| 396 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 397 | static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, |
| 398 | size_t len, size_t *retlen, u_char *buf) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 399 | { |
| 400 | struct mtd_part *part = PART(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 401 | return part->master->_read_fact_prot_reg(part->master, from, len, |
| 402 | retlen, buf); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 403 | } |
| 404 | |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 405 | static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len, |
| 406 | size_t *retlen, struct otp_info *buf) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 407 | { |
| 408 | struct mtd_part *part = PART(mtd); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 409 | return part->master->_get_fact_prot_info(part->master, len, retlen, |
| 410 | buf); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 413 | static int part_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 414 | size_t *retlen, const u_char *buf) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 415 | { |
| 416 | struct mtd_part *part = PART(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 417 | return part->master->_write(part->master, to + part->offset, len, |
| 418 | retlen, buf); |
| 419 | } |
| 420 | |
| 421 | static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 422 | size_t *retlen, const u_char *buf) |
| 423 | { |
| 424 | struct mtd_part *part = PART(mtd); |
| 425 | return part->master->_panic_write(part->master, to + part->offset, len, |
| 426 | retlen, buf); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 427 | } |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 428 | |
| 429 | static int part_write_oob(struct mtd_info *mtd, loff_t to, |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 430 | struct mtd_oob_ops *ops) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 431 | { |
| 432 | struct mtd_part *part = PART(mtd); |
| 433 | |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 434 | if (to >= mtd->size) |
| 435 | return -EINVAL; |
| 436 | if (ops->datbuf && to + ops->len > mtd->size) |
| 437 | return -EINVAL; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 438 | return part->master->_write_oob(part->master, to + part->offset, ops); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 439 | } |
| 440 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 441 | static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 442 | size_t len, size_t *retlen, u_char *buf) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 443 | { |
| 444 | struct mtd_part *part = PART(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 445 | return part->master->_write_user_prot_reg(part->master, from, len, |
| 446 | retlen, buf); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 447 | } |
| 448 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 449 | static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 450 | size_t len) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 451 | { |
| 452 | struct mtd_part *part = PART(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 453 | return part->master->_lock_user_prot_reg(part->master, from, len); |
| 454 | } |
| 455 | |
| 456 | #ifndef __UBOOT__ |
| 457 | static int part_writev(struct mtd_info *mtd, const struct kvec *vecs, |
| 458 | unsigned long count, loff_t to, size_t *retlen) |
| 459 | { |
| 460 | struct mtd_part *part = PART(mtd); |
| 461 | return part->master->_writev(part->master, vecs, count, |
| 462 | to + part->offset, retlen); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 463 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 464 | #endif |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 465 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 466 | static int part_erase(struct mtd_info *mtd, struct erase_info *instr) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 467 | { |
| 468 | struct mtd_part *part = PART(mtd); |
| 469 | int ret; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 470 | |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 471 | instr->addr += part->offset; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 472 | ret = part->master->_erase(part->master, instr); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 473 | if (ret) { |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 474 | if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 475 | instr->fail_addr -= part->offset; |
| 476 | instr->addr -= part->offset; |
| 477 | } |
| 478 | return ret; |
| 479 | } |
| 480 | |
| 481 | void mtd_erase_callback(struct erase_info *instr) |
| 482 | { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 483 | if (instr->mtd->_erase == part_erase) { |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 484 | struct mtd_part *part = PART(instr->mtd); |
| 485 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 486 | if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 487 | instr->fail_addr -= part->offset; |
| 488 | instr->addr -= part->offset; |
| 489 | } |
| 490 | if (instr->callback) |
| 491 | instr->callback(instr); |
| 492 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 493 | EXPORT_SYMBOL_GPL(mtd_erase_callback); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 494 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 495 | static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 496 | { |
| 497 | struct mtd_part *part = PART(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 498 | return part->master->_lock(part->master, ofs + part->offset, len); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 499 | } |
| 500 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 501 | static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 502 | { |
| 503 | struct mtd_part *part = PART(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 504 | return part->master->_unlock(part->master, ofs + part->offset, len); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 505 | } |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 506 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 507 | static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 508 | { |
| 509 | struct mtd_part *part = PART(mtd); |
| 510 | return part->master->_is_locked(part->master, ofs + part->offset, len); |
| 511 | } |
| 512 | |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 513 | static void part_sync(struct mtd_info *mtd) |
| 514 | { |
| 515 | struct mtd_part *part = PART(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 516 | part->master->_sync(part->master); |
| 517 | } |
| 518 | |
| 519 | #ifndef __UBOOT__ |
| 520 | static int part_suspend(struct mtd_info *mtd) |
| 521 | { |
| 522 | struct mtd_part *part = PART(mtd); |
| 523 | return part->master->_suspend(part->master); |
| 524 | } |
| 525 | |
| 526 | static void part_resume(struct mtd_info *mtd) |
| 527 | { |
| 528 | struct mtd_part *part = PART(mtd); |
| 529 | part->master->_resume(part->master); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 530 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 531 | #endif |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 532 | |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 533 | static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 534 | { |
| 535 | struct mtd_part *part = PART(mtd); |
| 536 | ofs += part->offset; |
| 537 | return part->master->_block_isreserved(part->master, ofs); |
| 538 | } |
| 539 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 540 | static int part_block_isbad(struct mtd_info *mtd, loff_t ofs) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 541 | { |
| 542 | struct mtd_part *part = PART(mtd); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 543 | ofs += part->offset; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 544 | return part->master->_block_isbad(part->master, ofs); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 545 | } |
| 546 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 547 | static int part_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 548 | { |
| 549 | struct mtd_part *part = PART(mtd); |
| 550 | int res; |
| 551 | |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 552 | ofs += part->offset; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 553 | res = part->master->_block_markbad(part->master, ofs); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 554 | if (!res) |
| 555 | mtd->ecc_stats.badblocks++; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 556 | return res; |
| 557 | } |
| 558 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 559 | static inline void free_partition(struct mtd_part *p) |
| 560 | { |
| 561 | kfree(p->mtd.name); |
| 562 | kfree(p); |
| 563 | } |
| 564 | |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 565 | /* |
| 566 | * This function unregisters and destroy all slave MTD objects which are |
| 567 | * attached to the given master MTD object. |
| 568 | */ |
| 569 | |
| 570 | int del_mtd_partitions(struct mtd_info *master) |
| 571 | { |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 572 | struct mtd_part *slave, *next; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 573 | int ret, err = 0; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 574 | |
Miquel Raynal | f78a12b | 2018-08-16 17:30:19 +0200 | [diff] [blame] | 575 | debug("Deleting MTD partitions on \"%s\":\n", master->name); |
| 576 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 577 | mutex_lock(&mtd_partitions_mutex); |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 578 | list_for_each_entry_safe(slave, next, &mtd_partitions, list) |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 579 | if (slave->master == master) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 580 | ret = del_mtd_device(&slave->mtd); |
| 581 | if (ret < 0) { |
| 582 | err = ret; |
| 583 | continue; |
| 584 | } |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 585 | list_del(&slave->list); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 586 | free_partition(slave); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 587 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 588 | mutex_unlock(&mtd_partitions_mutex); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 589 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 590 | return err; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 593 | static struct mtd_part *allocate_partition(struct mtd_info *master, |
| 594 | const struct mtd_partition *part, int partno, |
| 595 | uint64_t cur_offset) |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 596 | { |
| 597 | struct mtd_part *slave; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 598 | char *name; |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 599 | |
| 600 | /* allocate the partition structure */ |
| 601 | slave = kzalloc(sizeof(*slave), GFP_KERNEL); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 602 | name = kstrdup(part->name, GFP_KERNEL); |
| 603 | if (!name || !slave) { |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 604 | printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 605 | master->name); |
| 606 | kfree(name); |
| 607 | kfree(slave); |
| 608 | return ERR_PTR(-ENOMEM); |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 609 | } |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 610 | |
| 611 | /* set up the MTD object for this partition */ |
| 612 | slave->mtd.type = master->type; |
| 613 | slave->mtd.flags = master->flags & ~part->mask_flags; |
| 614 | slave->mtd.size = part->size; |
| 615 | slave->mtd.writesize = master->writesize; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 616 | slave->mtd.writebufsize = master->writebufsize; |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 617 | slave->mtd.oobsize = master->oobsize; |
| 618 | slave->mtd.oobavail = master->oobavail; |
| 619 | slave->mtd.subpage_sft = master->subpage_sft; |
| 620 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 621 | slave->mtd.name = name; |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 622 | slave->mtd.owner = master->owner; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 623 | #ifndef __UBOOT__ |
| 624 | slave->mtd.backing_dev_info = master->backing_dev_info; |
| 625 | |
| 626 | /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone |
| 627 | * to have the same data be in two different partitions. |
| 628 | */ |
| 629 | slave->mtd.dev.parent = master->dev.parent; |
| 630 | #endif |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 631 | |
Boris Brezillon | 6c20df7 | 2018-08-16 17:29:59 +0200 | [diff] [blame] | 632 | if (master->_read) |
| 633 | slave->mtd._read = part_read; |
| 634 | if (master->_write) |
| 635 | slave->mtd._write = part_write; |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 636 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 637 | if (master->_panic_write) |
| 638 | slave->mtd._panic_write = part_panic_write; |
| 639 | |
| 640 | #ifndef __UBOOT__ |
| 641 | if (master->_point && master->_unpoint) { |
| 642 | slave->mtd._point = part_point; |
| 643 | slave->mtd._unpoint = part_unpoint; |
| 644 | } |
| 645 | #endif |
| 646 | |
| 647 | if (master->_get_unmapped_area) |
| 648 | slave->mtd._get_unmapped_area = part_get_unmapped_area; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 649 | if (master->_read_oob) |
| 650 | slave->mtd._read_oob = part_read_oob; |
| 651 | if (master->_write_oob) |
| 652 | slave->mtd._write_oob = part_write_oob; |
| 653 | if (master->_read_user_prot_reg) |
| 654 | slave->mtd._read_user_prot_reg = part_read_user_prot_reg; |
| 655 | if (master->_read_fact_prot_reg) |
| 656 | slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg; |
| 657 | if (master->_write_user_prot_reg) |
| 658 | slave->mtd._write_user_prot_reg = part_write_user_prot_reg; |
| 659 | if (master->_lock_user_prot_reg) |
| 660 | slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg; |
| 661 | if (master->_get_user_prot_info) |
| 662 | slave->mtd._get_user_prot_info = part_get_user_prot_info; |
| 663 | if (master->_get_fact_prot_info) |
| 664 | slave->mtd._get_fact_prot_info = part_get_fact_prot_info; |
| 665 | if (master->_sync) |
| 666 | slave->mtd._sync = part_sync; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 667 | #ifndef __UBOOT__ |
| 668 | if (!partno && !master->dev.class && master->_suspend && |
| 669 | master->_resume) { |
| 670 | slave->mtd._suspend = part_suspend; |
| 671 | slave->mtd._resume = part_resume; |
| 672 | } |
| 673 | if (master->_writev) |
| 674 | slave->mtd._writev = part_writev; |
| 675 | #endif |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 676 | if (master->_lock) |
| 677 | slave->mtd._lock = part_lock; |
| 678 | if (master->_unlock) |
| 679 | slave->mtd._unlock = part_unlock; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 680 | if (master->_is_locked) |
| 681 | slave->mtd._is_locked = part_is_locked; |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 682 | if (master->_block_isreserved) |
| 683 | slave->mtd._block_isreserved = part_block_isreserved; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 684 | if (master->_block_isbad) |
| 685 | slave->mtd._block_isbad = part_block_isbad; |
| 686 | if (master->_block_markbad) |
| 687 | slave->mtd._block_markbad = part_block_markbad; |
| 688 | slave->mtd._erase = part_erase; |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 689 | slave->master = master; |
| 690 | slave->offset = part->offset; |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 691 | |
| 692 | if (slave->offset == MTDPART_OFS_APPEND) |
| 693 | slave->offset = cur_offset; |
| 694 | if (slave->offset == MTDPART_OFS_NXTBLK) { |
| 695 | slave->offset = cur_offset; |
| 696 | if (mtd_mod_by_eb(cur_offset, master) != 0) { |
| 697 | /* Round up to next erasesize */ |
| 698 | slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 699 | debug("Moving partition %d: " |
| 700 | "0x%012llx -> 0x%012llx\n", partno, |
| 701 | (unsigned long long)cur_offset, (unsigned long long)slave->offset); |
| 702 | } |
| 703 | } |
| 704 | if (slave->offset == MTDPART_OFS_RETAIN) { |
| 705 | slave->offset = cur_offset; |
| 706 | if (master->size - slave->offset >= slave->mtd.size) { |
| 707 | slave->mtd.size = master->size - slave->offset |
| 708 | - slave->mtd.size; |
| 709 | } else { |
| 710 | debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n", |
| 711 | part->name, master->size - slave->offset, |
| 712 | slave->mtd.size); |
| 713 | /* register to preserve ordering */ |
| 714 | goto out_register; |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | if (slave->mtd.size == MTDPART_SIZ_FULL) |
| 718 | slave->mtd.size = master->size - slave->offset; |
| 719 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 720 | debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset, |
| 721 | (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name); |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 722 | |
| 723 | /* let's do some sanity checks */ |
| 724 | if (slave->offset >= master->size) { |
| 725 | /* let's register it anyway to preserve ordering */ |
| 726 | slave->offset = 0; |
| 727 | slave->mtd.size = 0; |
| 728 | printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n", |
| 729 | part->name); |
| 730 | goto out_register; |
| 731 | } |
| 732 | if (slave->offset + slave->mtd.size > master->size) { |
| 733 | slave->mtd.size = master->size - slave->offset; |
| 734 | printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n", |
| 735 | part->name, master->name, (unsigned long long)slave->mtd.size); |
| 736 | } |
| 737 | if (master->numeraseregions > 1) { |
| 738 | /* Deal with variable erase size stuff */ |
| 739 | int i, max = master->numeraseregions; |
| 740 | u64 end = slave->offset + slave->mtd.size; |
| 741 | struct mtd_erase_region_info *regions = master->eraseregions; |
| 742 | |
| 743 | /* Find the first erase regions which is part of this |
| 744 | * partition. */ |
| 745 | for (i = 0; i < max && regions[i].offset <= slave->offset; i++) |
| 746 | ; |
| 747 | /* The loop searched for the region _behind_ the first one */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 748 | if (i > 0) |
| 749 | i--; |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 750 | |
| 751 | /* Pick biggest erasesize */ |
| 752 | for (; i < max && regions[i].offset < end; i++) { |
| 753 | if (slave->mtd.erasesize < regions[i].erasesize) { |
| 754 | slave->mtd.erasesize = regions[i].erasesize; |
| 755 | } |
| 756 | } |
| 757 | BUG_ON(slave->mtd.erasesize == 0); |
| 758 | } else { |
| 759 | /* Single erase size */ |
| 760 | slave->mtd.erasesize = master->erasesize; |
| 761 | } |
| 762 | |
| 763 | if ((slave->mtd.flags & MTD_WRITEABLE) && |
| 764 | mtd_mod_by_eb(slave->offset, &slave->mtd)) { |
| 765 | /* Doesn't start on a boundary of major erase size */ |
| 766 | /* FIXME: Let it be writable if it is on a boundary of |
| 767 | * _minor_ erase size though */ |
| 768 | slave->mtd.flags &= ~MTD_WRITEABLE; |
| 769 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n", |
| 770 | part->name); |
| 771 | } |
| 772 | if ((slave->mtd.flags & MTD_WRITEABLE) && |
| 773 | mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) { |
| 774 | slave->mtd.flags &= ~MTD_WRITEABLE; |
| 775 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n", |
| 776 | part->name); |
| 777 | } |
| 778 | |
| 779 | slave->mtd.ecclayout = master->ecclayout; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 780 | slave->mtd.ecc_step_size = master->ecc_step_size; |
| 781 | slave->mtd.ecc_strength = master->ecc_strength; |
| 782 | slave->mtd.bitflip_threshold = master->bitflip_threshold; |
| 783 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 784 | if (master->_block_isbad) { |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 785 | uint64_t offs = 0; |
| 786 | |
| 787 | while (offs < slave->mtd.size) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 788 | if (mtd_block_isbad(master, offs + slave->offset)) |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 789 | slave->mtd.ecc_stats.badblocks++; |
| 790 | offs += slave->mtd.erasesize; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | out_register: |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 795 | return slave; |
| 796 | } |
| 797 | |
Heiko Schocher | b24c427 | 2014-07-15 16:08:42 +0200 | [diff] [blame] | 798 | #ifndef __UBOOT__ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 799 | int mtd_add_partition(struct mtd_info *master, const char *name, |
| 800 | long long offset, long long length) |
| 801 | { |
| 802 | struct mtd_partition part; |
| 803 | struct mtd_part *p, *new; |
| 804 | uint64_t start, end; |
| 805 | int ret = 0; |
| 806 | |
| 807 | /* the direct offset is expected */ |
| 808 | if (offset == MTDPART_OFS_APPEND || |
| 809 | offset == MTDPART_OFS_NXTBLK) |
| 810 | return -EINVAL; |
| 811 | |
| 812 | if (length == MTDPART_SIZ_FULL) |
| 813 | length = master->size - offset; |
| 814 | |
| 815 | if (length <= 0) |
| 816 | return -EINVAL; |
| 817 | |
| 818 | part.name = name; |
| 819 | part.size = length; |
| 820 | part.offset = offset; |
| 821 | part.mask_flags = 0; |
| 822 | part.ecclayout = NULL; |
| 823 | |
| 824 | new = allocate_partition(master, &part, -1, offset); |
| 825 | if (IS_ERR(new)) |
| 826 | return PTR_ERR(new); |
| 827 | |
| 828 | start = offset; |
| 829 | end = offset + length; |
| 830 | |
| 831 | mutex_lock(&mtd_partitions_mutex); |
| 832 | list_for_each_entry(p, &mtd_partitions, list) |
| 833 | if (p->master == master) { |
| 834 | if ((start >= p->offset) && |
| 835 | (start < (p->offset + p->mtd.size))) |
| 836 | goto err_inv; |
| 837 | |
| 838 | if ((end >= p->offset) && |
| 839 | (end < (p->offset + p->mtd.size))) |
| 840 | goto err_inv; |
| 841 | } |
| 842 | |
| 843 | list_add(&new->list, &mtd_partitions); |
| 844 | mutex_unlock(&mtd_partitions_mutex); |
| 845 | |
| 846 | add_mtd_device(&new->mtd); |
| 847 | |
| 848 | return ret; |
| 849 | err_inv: |
| 850 | mutex_unlock(&mtd_partitions_mutex); |
| 851 | free_partition(new); |
| 852 | return -EINVAL; |
| 853 | } |
| 854 | EXPORT_SYMBOL_GPL(mtd_add_partition); |
| 855 | |
| 856 | int mtd_del_partition(struct mtd_info *master, int partno) |
| 857 | { |
| 858 | struct mtd_part *slave, *next; |
| 859 | int ret = -EINVAL; |
| 860 | |
| 861 | mutex_lock(&mtd_partitions_mutex); |
| 862 | list_for_each_entry_safe(slave, next, &mtd_partitions, list) |
| 863 | if ((slave->master == master) && |
| 864 | (slave->mtd.index == partno)) { |
| 865 | ret = del_mtd_device(&slave->mtd); |
| 866 | if (ret < 0) |
| 867 | break; |
| 868 | |
| 869 | list_del(&slave->list); |
| 870 | free_partition(slave); |
| 871 | break; |
| 872 | } |
| 873 | mutex_unlock(&mtd_partitions_mutex); |
| 874 | |
| 875 | return ret; |
| 876 | } |
| 877 | EXPORT_SYMBOL_GPL(mtd_del_partition); |
Heiko Schocher | b24c427 | 2014-07-15 16:08:42 +0200 | [diff] [blame] | 878 | #endif |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 879 | |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 880 | /* |
| 881 | * This function, given a master MTD object and a partition table, creates |
| 882 | * and registers slave MTD objects which are bound to the master according to |
| 883 | * the partition definitions. |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 884 | * |
| 885 | * We don't register the master, or expect the caller to have done so, |
| 886 | * for reasons of data integrity. |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 887 | */ |
| 888 | |
| 889 | int add_mtd_partitions(struct mtd_info *master, |
| 890 | const struct mtd_partition *parts, |
| 891 | int nbparts) |
| 892 | { |
| 893 | struct mtd_part *slave; |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 894 | uint64_t cur_offset = 0; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 895 | int i; |
| 896 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 897 | #ifdef __UBOOT__ |
Stefan Roese | 6137920 | 2008-11-27 14:05:15 +0100 | [diff] [blame] | 898 | /* |
| 899 | * Need to init the list here, since LIST_INIT() does not |
| 900 | * work on platforms where relocation has problems (like MIPS |
| 901 | * & PPC). |
| 902 | */ |
| 903 | if (mtd_partitions.next == NULL) |
| 904 | INIT_LIST_HEAD(&mtd_partitions); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 905 | #endif |
Stefan Roese | 6137920 | 2008-11-27 14:05:15 +0100 | [diff] [blame] | 906 | |
Joe Hershberger | 47550fc | 2013-04-08 10:32:49 +0000 | [diff] [blame] | 907 | debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 908 | |
| 909 | for (i = 0; i < nbparts; i++) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 910 | slave = allocate_partition(master, parts + i, i, cur_offset); |
| 911 | if (IS_ERR(slave)) |
| 912 | return PTR_ERR(slave); |
| 913 | |
| 914 | mutex_lock(&mtd_partitions_mutex); |
| 915 | list_add(&slave->list, &mtd_partitions); |
| 916 | mutex_unlock(&mtd_partitions_mutex); |
| 917 | |
| 918 | add_mtd_device(&slave->mtd); |
| 919 | |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 920 | cur_offset = slave->offset + slave->mtd.size; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | return 0; |
| 924 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 925 | |
| 926 | #ifndef __UBOOT__ |
| 927 | static DEFINE_SPINLOCK(part_parser_lock); |
| 928 | static LIST_HEAD(part_parsers); |
| 929 | |
| 930 | static struct mtd_part_parser *get_partition_parser(const char *name) |
| 931 | { |
| 932 | struct mtd_part_parser *p, *ret = NULL; |
| 933 | |
| 934 | spin_lock(&part_parser_lock); |
| 935 | |
| 936 | list_for_each_entry(p, &part_parsers, list) |
| 937 | if (!strcmp(p->name, name) && try_module_get(p->owner)) { |
| 938 | ret = p; |
| 939 | break; |
| 940 | } |
| 941 | |
| 942 | spin_unlock(&part_parser_lock); |
| 943 | |
| 944 | return ret; |
| 945 | } |
| 946 | |
| 947 | #define put_partition_parser(p) do { module_put((p)->owner); } while (0) |
| 948 | |
| 949 | void register_mtd_parser(struct mtd_part_parser *p) |
| 950 | { |
| 951 | spin_lock(&part_parser_lock); |
| 952 | list_add(&p->list, &part_parsers); |
| 953 | spin_unlock(&part_parser_lock); |
| 954 | } |
| 955 | EXPORT_SYMBOL_GPL(register_mtd_parser); |
| 956 | |
| 957 | void deregister_mtd_parser(struct mtd_part_parser *p) |
| 958 | { |
| 959 | spin_lock(&part_parser_lock); |
| 960 | list_del(&p->list); |
| 961 | spin_unlock(&part_parser_lock); |
| 962 | } |
| 963 | EXPORT_SYMBOL_GPL(deregister_mtd_parser); |
| 964 | |
| 965 | /* |
| 966 | * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you |
| 967 | * are changing this array! |
| 968 | */ |
| 969 | static const char * const default_mtd_part_types[] = { |
| 970 | "cmdlinepart", |
| 971 | "ofpart", |
| 972 | NULL |
| 973 | }; |
| 974 | |
| 975 | /** |
| 976 | * parse_mtd_partitions - parse MTD partitions |
| 977 | * @master: the master partition (describes whole MTD device) |
| 978 | * @types: names of partition parsers to try or %NULL |
| 979 | * @pparts: array of partitions found is returned here |
| 980 | * @data: MTD partition parser-specific data |
| 981 | * |
| 982 | * This function tries to find partition on MTD device @master. It uses MTD |
| 983 | * partition parsers, specified in @types. However, if @types is %NULL, then |
| 984 | * the default list of parsers is used. The default list contains only the |
| 985 | * "cmdlinepart" and "ofpart" parsers ATM. |
| 986 | * Note: If there are more then one parser in @types, the kernel only takes the |
| 987 | * partitions parsed out by the first parser. |
| 988 | * |
| 989 | * This function may return: |
| 990 | * o a negative error code in case of failure |
| 991 | * o zero if no partitions were found |
| 992 | * o a positive number of found partitions, in which case on exit @pparts will |
| 993 | * point to an array containing this number of &struct mtd_info objects. |
| 994 | */ |
| 995 | int parse_mtd_partitions(struct mtd_info *master, const char *const *types, |
| 996 | struct mtd_partition **pparts, |
| 997 | struct mtd_part_parser_data *data) |
| 998 | { |
| 999 | struct mtd_part_parser *parser; |
| 1000 | int ret = 0; |
| 1001 | |
| 1002 | if (!types) |
| 1003 | types = default_mtd_part_types; |
| 1004 | |
| 1005 | for ( ; ret <= 0 && *types; types++) { |
| 1006 | parser = get_partition_parser(*types); |
| 1007 | if (!parser && !request_module("%s", *types)) |
| 1008 | parser = get_partition_parser(*types); |
| 1009 | if (!parser) |
| 1010 | continue; |
| 1011 | ret = (*parser->parse_fn)(master, pparts, data); |
| 1012 | put_partition_parser(parser); |
| 1013 | if (ret > 0) { |
| 1014 | printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n", |
| 1015 | ret, parser->name, master->name); |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
| 1019 | return ret; |
| 1020 | } |
| 1021 | #endif |
| 1022 | |
| 1023 | int mtd_is_partition(const struct mtd_info *mtd) |
| 1024 | { |
| 1025 | struct mtd_part *part; |
| 1026 | int ispart = 0; |
| 1027 | |
| 1028 | mutex_lock(&mtd_partitions_mutex); |
| 1029 | list_for_each_entry(part, &mtd_partitions, list) |
| 1030 | if (&part->mtd == mtd) { |
| 1031 | ispart = 1; |
| 1032 | break; |
| 1033 | } |
| 1034 | mutex_unlock(&mtd_partitions_mutex); |
| 1035 | |
| 1036 | return ispart; |
| 1037 | } |
| 1038 | EXPORT_SYMBOL_GPL(mtd_is_partition); |
| 1039 | |
| 1040 | /* Returns the size of the entire flash chip */ |
| 1041 | uint64_t mtd_get_device_size(const struct mtd_info *mtd) |
| 1042 | { |
| 1043 | if (!mtd_is_partition(mtd)) |
| 1044 | return mtd->size; |
| 1045 | |
| 1046 | return PART(mtd)->master->size; |
| 1047 | } |
| 1048 | EXPORT_SYMBOL_GPL(mtd_get_device_size); |