]>
Commit | Line | Data |
---|---|---|
fd534e9b | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * Simple MTD partitioning layer | |
4 | * | |
a1452a37 DW |
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> | |
97894cda | 8 | */ |
1da177e4 LT |
9 | |
10 | #include <linux/module.h> | |
11 | #include <linux/types.h> | |
12 | #include <linux/kernel.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/list.h> | |
1da177e4 LT |
15 | #include <linux/kmod.h> |
16 | #include <linux/mtd/mtd.h> | |
17 | #include <linux/mtd/partitions.h> | |
5daa7b21 | 18 | #include <linux/err.h> |
5b644aa0 | 19 | #include <linux/of.h> |
1da177e4 | 20 | |
eea72d5f JI |
21 | #include "mtdcore.h" |
22 | ||
97894cda | 23 | /* |
1da177e4 LT |
24 | * MTD methods which simply translate the effective address and pass through |
25 | * to the _real_ device. | |
26 | */ | |
27 | ||
46b5889c | 28 | static inline void free_partition(struct mtd_info *mtd) |
5daa7b21 | 29 | { |
46b5889c MR |
30 | kfree(mtd->name); |
31 | kfree(mtd); | |
5daa7b21 RT |
32 | } |
33 | ||
46b5889c MR |
34 | static struct mtd_info *allocate_partition(struct mtd_info *parent, |
35 | const struct mtd_partition *part, | |
36 | int partno, uint64_t cur_offset) | |
7788ba71 | 37 | { |
9e3307a1 BB |
38 | struct mtd_info *master = mtd_get_master(parent); |
39 | int wr_alignment = (parent->flags & MTD_NO_ERASE) ? | |
40 | master->writesize : master->erasesize; | |
41 | u64 parent_size = mtd_is_partition(parent) ? | |
42 | parent->part.size : parent->size; | |
43 | struct mtd_info *child; | |
1eeef2d7 | 44 | u32 remainder; |
5daa7b21 | 45 | char *name; |
1eeef2d7 | 46 | u64 tmp; |
7788ba71 AN |
47 | |
48 | /* allocate the partition structure */ | |
46b5889c | 49 | child = kzalloc(sizeof(*child), GFP_KERNEL); |
5daa7b21 | 50 | name = kstrdup(part->name, GFP_KERNEL); |
46b5889c | 51 | if (!name || !child) { |
b33a2887 | 52 | printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n", |
0a9d72b6 | 53 | parent->name); |
5daa7b21 | 54 | kfree(name); |
46b5889c | 55 | kfree(child); |
5daa7b21 | 56 | return ERR_PTR(-ENOMEM); |
7788ba71 | 57 | } |
7788ba71 AN |
58 | |
59 | /* set up the MTD object for this partition */ | |
46b5889c MR |
60 | child->type = parent->type; |
61 | child->part.flags = parent->flags & ~part->mask_flags; | |
9e3307a1 | 62 | child->part.flags |= part->add_flags; |
46b5889c | 63 | child->flags = child->part.flags; |
9e3307a1 | 64 | child->part.size = part->size; |
46b5889c MR |
65 | child->writesize = parent->writesize; |
66 | child->writebufsize = parent->writebufsize; | |
67 | child->oobsize = parent->oobsize; | |
68 | child->oobavail = parent->oobavail; | |
69 | child->subpage_sft = parent->subpage_sft; | |
70 | ||
71 | child->name = name; | |
72 | child->owner = parent->owner; | |
7788ba71 | 73 | |
727dc612 DE |
74 | /* NOTE: Historically, we didn't arrange MTDs as a tree out of |
75 | * concern for showing the same data in multiple partitions. | |
76 | * However, it is very useful to have the master node present, | |
77 | * so the MTD_PARTITIONED_MASTER option allows that. The master | |
78 | * will have device nodes etc only if this is set, so make the | |
79 | * parent conditional on that option. Note, this is a way to | |
46b5889c | 80 | * distinguish between the parent and its partitions in sysfs. |
1f24b5a8 | 81 | */ |
46b5889c MR |
82 | child->dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ? |
83 | &parent->dev : parent->dev.parent; | |
84 | child->dev.of_node = part->of_node; | |
85 | child->parent = parent; | |
86 | child->part.offset = part->offset; | |
87 | INIT_LIST_HEAD(&child->partitions); | |
88 | ||
89 | if (child->part.offset == MTDPART_OFS_APPEND) | |
90 | child->part.offset = cur_offset; | |
91 | if (child->part.offset == MTDPART_OFS_NXTBLK) { | |
1eeef2d7 | 92 | tmp = cur_offset; |
46b5889c | 93 | child->part.offset = cur_offset; |
1eeef2d7 CP |
94 | remainder = do_div(tmp, wr_alignment); |
95 | if (remainder) { | |
46b5889c | 96 | child->part.offset += wr_alignment - remainder; |
7788ba71 | 97 | printk(KERN_NOTICE "Moving partition %d: " |
69423d99 | 98 | "0x%012llx -> 0x%012llx\n", partno, |
46b5889c MR |
99 | (unsigned long long)cur_offset, |
100 | child->part.offset); | |
7788ba71 AN |
101 | } |
102 | } | |
46b5889c MR |
103 | if (child->part.offset == MTDPART_OFS_RETAIN) { |
104 | child->part.offset = cur_offset; | |
9e3307a1 BB |
105 | if (parent_size - child->part.offset >= child->part.size) { |
106 | child->part.size = parent_size - child->part.offset - | |
107 | child->part.size; | |
1a31368b DB |
108 | } else { |
109 | printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n", | |
9e3307a1 BB |
110 | part->name, parent_size - child->part.offset, |
111 | child->part.size); | |
1a31368b DB |
112 | /* register to preserve ordering */ |
113 | goto out_register; | |
114 | } | |
115 | } | |
9e3307a1 BB |
116 | if (child->part.size == MTDPART_SIZ_FULL) |
117 | child->part.size = parent_size - child->part.offset; | |
7788ba71 | 118 | |
46b5889c | 119 | printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", |
9e3307a1 | 120 | child->part.offset, child->part.offset + child->part.size, |
46b5889c | 121 | child->name); |
7788ba71 AN |
122 | |
123 | /* let's do some sanity checks */ | |
9e3307a1 | 124 | if (child->part.offset >= parent_size) { |
f636ffb4 | 125 | /* let's register it anyway to preserve ordering */ |
46b5889c | 126 | child->part.offset = 0; |
9e3307a1 | 127 | child->part.size = 0; |
ad463515 BB |
128 | |
129 | /* Initialize ->erasesize to make add_mtd_device() happy. */ | |
46b5889c | 130 | child->erasesize = parent->erasesize; |
b33a2887 | 131 | printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n", |
7788ba71 | 132 | part->name); |
f636ffb4 | 133 | goto out_register; |
7788ba71 | 134 | } |
9e3307a1 BB |
135 | if (child->part.offset + child->part.size > parent->size) { |
136 | child->part.size = parent_size - child->part.offset; | |
69423d99 | 137 | printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n", |
9e3307a1 | 138 | part->name, parent->name, child->part.size); |
7788ba71 | 139 | } |
9e3307a1 | 140 | |
0a9d72b6 | 141 | if (parent->numeraseregions > 1) { |
7788ba71 | 142 | /* Deal with variable erase size stuff */ |
0a9d72b6 | 143 | int i, max = parent->numeraseregions; |
9e3307a1 | 144 | u64 end = child->part.offset + child->part.size; |
0a9d72b6 | 145 | struct mtd_erase_region_info *regions = parent->eraseregions; |
7788ba71 | 146 | |
6910c136 AN |
147 | /* Find the first erase regions which is part of this |
148 | * partition. */ | |
46b5889c MR |
149 | for (i = 0; i < max && regions[i].offset <= child->part.offset; |
150 | i++) | |
7788ba71 | 151 | ; |
6910c136 | 152 | /* The loop searched for the region _behind_ the first one */ |
a57ca046 RK |
153 | if (i > 0) |
154 | i--; | |
7788ba71 | 155 | |
6910c136 AN |
156 | /* Pick biggest erasesize */ |
157 | for (; i < max && regions[i].offset < end; i++) { | |
46b5889c MR |
158 | if (child->erasesize < regions[i].erasesize) |
159 | child->erasesize = regions[i].erasesize; | |
7788ba71 | 160 | } |
46b5889c | 161 | BUG_ON(child->erasesize == 0); |
7788ba71 AN |
162 | } else { |
163 | /* Single erase size */ | |
9e3307a1 | 164 | child->erasesize = master->erasesize; |
7788ba71 AN |
165 | } |
166 | ||
7e439681 | 167 | /* |
46b5889c | 168 | * Child erasesize might differ from the parent one if the parent |
7e439681 BB |
169 | * exposes several regions with different erasesize. Adjust |
170 | * wr_alignment accordingly. | |
171 | */ | |
46b5889c MR |
172 | if (!(child->flags & MTD_NO_ERASE)) |
173 | wr_alignment = child->erasesize; | |
7e439681 | 174 | |
46b5889c | 175 | tmp = mtd_get_master_ofs(child, 0); |
1eeef2d7 | 176 | remainder = do_div(tmp, wr_alignment); |
46b5889c | 177 | if ((child->flags & MTD_WRITEABLE) && remainder) { |
7788ba71 | 178 | /* Doesn't start on a boundary of major erase size */ |
b33a2887 AN |
179 | /* FIXME: Let it be writable if it is on a boundary of |
180 | * _minor_ erase size though */ | |
46b5889c | 181 | child->flags &= ~MTD_WRITEABLE; |
1eeef2d7 | 182 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n", |
7788ba71 AN |
183 | part->name); |
184 | } | |
1eeef2d7 | 185 | |
9e3307a1 | 186 | tmp = mtd_get_master_ofs(child, 0) + child->part.size; |
1eeef2d7 | 187 | remainder = do_div(tmp, wr_alignment); |
46b5889c MR |
188 | if ((child->flags & MTD_WRITEABLE) && remainder) { |
189 | child->flags &= ~MTD_WRITEABLE; | |
1eeef2d7 | 190 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n", |
7788ba71 AN |
191 | part->name); |
192 | } | |
193 | ||
9e3307a1 | 194 | child->size = child->part.size; |
46b5889c MR |
195 | child->ecc_step_size = parent->ecc_step_size; |
196 | child->ecc_strength = parent->ecc_strength; | |
197 | child->bitflip_threshold = parent->bitflip_threshold; | |
d062d4ed | 198 | |
46b5889c | 199 | if (master->_block_isbad) { |
69423d99 | 200 | uint64_t offs = 0; |
7788ba71 | 201 | |
9e3307a1 | 202 | while (offs < child->part.size) { |
46b5889c MR |
203 | if (mtd_block_isreserved(child, offs)) |
204 | child->ecc_stats.bbtblocks++; | |
205 | else if (mtd_block_isbad(child, offs)) | |
206 | child->ecc_stats.badblocks++; | |
207 | offs += child->erasesize; | |
7788ba71 AN |
208 | } |
209 | } | |
210 | ||
f636ffb4 | 211 | out_register: |
46b5889c | 212 | return child; |
7788ba71 AN |
213 | } |
214 | ||
a62c24d7 DE |
215 | static ssize_t mtd_partition_offset_show(struct device *dev, |
216 | struct device_attribute *attr, char *buf) | |
217 | { | |
218 | struct mtd_info *mtd = dev_get_drvdata(dev); | |
46b5889c MR |
219 | |
220 | return snprintf(buf, PAGE_SIZE, "%lld\n", mtd->part.offset); | |
a62c24d7 DE |
221 | } |
222 | ||
223 | static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL); | |
224 | ||
225 | static const struct attribute *mtd_partition_attrs[] = { | |
226 | &dev_attr_offset.attr, | |
227 | NULL | |
228 | }; | |
229 | ||
46b5889c | 230 | static int mtd_add_partition_attrs(struct mtd_info *new) |
a62c24d7 | 231 | { |
46b5889c | 232 | int ret = sysfs_create_files(&new->dev.kobj, mtd_partition_attrs); |
a62c24d7 DE |
233 | if (ret) |
234 | printk(KERN_WARNING | |
235 | "mtd: failed to create partition attrs, err=%d\n", ret); | |
236 | return ret; | |
237 | } | |
238 | ||
0a9d72b6 | 239 | int mtd_add_partition(struct mtd_info *parent, const char *name, |
5daa7b21 RT |
240 | long long offset, long long length) |
241 | { | |
46b5889c | 242 | struct mtd_info *master = mtd_get_master(parent); |
9e3307a1 BB |
243 | u64 parent_size = mtd_is_partition(parent) ? |
244 | parent->part.size : parent->size; | |
5daa7b21 | 245 | struct mtd_partition part; |
46b5889c | 246 | struct mtd_info *child; |
5daa7b21 RT |
247 | int ret = 0; |
248 | ||
249 | /* the direct offset is expected */ | |
250 | if (offset == MTDPART_OFS_APPEND || | |
251 | offset == MTDPART_OFS_NXTBLK) | |
252 | return -EINVAL; | |
253 | ||
254 | if (length == MTDPART_SIZ_FULL) | |
9e3307a1 | 255 | length = parent_size - offset; |
5daa7b21 RT |
256 | |
257 | if (length <= 0) | |
258 | return -EINVAL; | |
259 | ||
93867233 | 260 | memset(&part, 0, sizeof(part)); |
5daa7b21 RT |
261 | part.name = name; |
262 | part.size = length; | |
263 | part.offset = offset; | |
5daa7b21 | 264 | |
46b5889c MR |
265 | child = allocate_partition(parent, &part, -1, offset); |
266 | if (IS_ERR(child)) | |
267 | return PTR_ERR(child); | |
5daa7b21 | 268 | |
46b5889c MR |
269 | mutex_lock(&master->master.partitions_lock); |
270 | list_add_tail(&child->part.node, &parent->partitions); | |
271 | mutex_unlock(&master->master.partitions_lock); | |
5daa7b21 | 272 | |
46b5889c | 273 | ret = add_mtd_device(child); |
2b6f0090 BB |
274 | if (ret) |
275 | goto err_remove_part; | |
5daa7b21 | 276 | |
46b5889c | 277 | mtd_add_partition_attrs(child); |
a62c24d7 | 278 | |
2b6f0090 BB |
279 | return 0; |
280 | ||
281 | err_remove_part: | |
46b5889c MR |
282 | mutex_lock(&master->master.partitions_lock); |
283 | list_del(&child->part.node); | |
284 | mutex_unlock(&master->master.partitions_lock); | |
2b6f0090 | 285 | |
46b5889c | 286 | free_partition(child); |
2b6f0090 | 287 | |
5daa7b21 | 288 | return ret; |
5daa7b21 RT |
289 | } |
290 | EXPORT_SYMBOL_GPL(mtd_add_partition); | |
291 | ||
08263a9a RM |
292 | /** |
293 | * __mtd_del_partition - delete MTD partition | |
294 | * | |
46b5889c | 295 | * @priv: MTD structure to be deleted |
08263a9a RM |
296 | * |
297 | * This function must be called with the partitions mutex locked. | |
298 | */ | |
46b5889c | 299 | static int __mtd_del_partition(struct mtd_info *mtd) |
08263a9a | 300 | { |
46b5889c | 301 | struct mtd_info *child, *next; |
08263a9a RM |
302 | int err; |
303 | ||
46b5889c MR |
304 | list_for_each_entry_safe(child, next, &mtd->partitions, part.node) { |
305 | err = __mtd_del_partition(child); | |
306 | if (err) | |
307 | return err; | |
97519dc5 RM |
308 | } |
309 | ||
46b5889c | 310 | sysfs_remove_files(&mtd->dev.kobj, mtd_partition_attrs); |
c5ceaba7 | 311 | |
46b5889c | 312 | err = del_mtd_device(mtd); |
08263a9a RM |
313 | if (err) |
314 | return err; | |
315 | ||
46b5889c MR |
316 | list_del(&child->part.node); |
317 | free_partition(mtd); | |
08263a9a RM |
318 | |
319 | return 0; | |
320 | } | |
321 | ||
322 | /* | |
323 | * This function unregisters and destroy all slave MTD objects which are | |
46b5889c | 324 | * attached to the given MTD object, recursively. |
08263a9a | 325 | */ |
46b5889c | 326 | static int __del_mtd_partitions(struct mtd_info *mtd) |
08263a9a | 327 | { |
46b5889c MR |
328 | struct mtd_info *child, *next; |
329 | LIST_HEAD(tmp_list); | |
08263a9a RM |
330 | int ret, err = 0; |
331 | ||
46b5889c MR |
332 | list_for_each_entry_safe(child, next, &mtd->partitions, part.node) { |
333 | if (mtd_has_partitions(child)) | |
334 | del_mtd_partitions(child); | |
335 | ||
336 | pr_info("Deleting %s MTD partition\n", child->name); | |
337 | ret = del_mtd_device(child); | |
338 | if (ret < 0) { | |
339 | pr_err("Error when deleting partition \"%s\" (%d)\n", | |
340 | child->name, ret); | |
341 | err = ret; | |
342 | continue; | |
08263a9a | 343 | } |
46b5889c MR |
344 | |
345 | list_del(&child->part.node); | |
346 | free_partition(child); | |
347 | } | |
08263a9a RM |
348 | |
349 | return err; | |
350 | } | |
351 | ||
46b5889c MR |
352 | int del_mtd_partitions(struct mtd_info *mtd) |
353 | { | |
354 | struct mtd_info *master = mtd_get_master(mtd); | |
355 | int ret; | |
356 | ||
357 | pr_info("Deleting MTD partitions on \"%s\":\n", mtd->name); | |
358 | ||
359 | mutex_lock(&master->master.partitions_lock); | |
360 | ret = __del_mtd_partitions(mtd); | |
361 | mutex_unlock(&master->master.partitions_lock); | |
362 | ||
363 | return ret; | |
364 | } | |
365 | ||
97519dc5 | 366 | int mtd_del_partition(struct mtd_info *mtd, int partno) |
5daa7b21 | 367 | { |
46b5889c | 368 | struct mtd_info *child, *master = mtd_get_master(mtd); |
5daa7b21 RT |
369 | int ret = -EINVAL; |
370 | ||
46b5889c MR |
371 | mutex_lock(&master->master.partitions_lock); |
372 | list_for_each_entry(child, &mtd->partitions, part.node) { | |
373 | if (child->index == partno) { | |
374 | ret = __mtd_del_partition(child); | |
5daa7b21 RT |
375 | break; |
376 | } | |
46b5889c MR |
377 | } |
378 | mutex_unlock(&master->master.partitions_lock); | |
5daa7b21 RT |
379 | |
380 | return ret; | |
381 | } | |
382 | EXPORT_SYMBOL_GPL(mtd_del_partition); | |
383 | ||
1da177e4 | 384 | /* |
46b5889c MR |
385 | * This function, given a parent MTD object and a partition table, creates |
386 | * and registers the child MTD objects which are bound to the parent according | |
387 | * to the partition definitions. | |
1f24b5a8 | 388 | * |
46b5889c | 389 | * For historical reasons, this function's caller only registers the parent |
727dc612 | 390 | * if the MTD_PARTITIONED_MASTER config option is set. |
1da177e4 LT |
391 | */ |
392 | ||
46b5889c | 393 | int add_mtd_partitions(struct mtd_info *parent, |
1da177e4 LT |
394 | const struct mtd_partition *parts, |
395 | int nbparts) | |
396 | { | |
46b5889c | 397 | struct mtd_info *child, *master = mtd_get_master(parent); |
69423d99 | 398 | uint64_t cur_offset = 0; |
2b6f0090 | 399 | int i, ret; |
1da177e4 | 400 | |
46b5889c MR |
401 | printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", |
402 | nbparts, parent->name); | |
1da177e4 LT |
403 | |
404 | for (i = 0; i < nbparts; i++) { | |
46b5889c MR |
405 | child = allocate_partition(parent, parts + i, i, cur_offset); |
406 | if (IS_ERR(child)) { | |
407 | ret = PTR_ERR(child); | |
2b6f0090 | 408 | goto err_del_partitions; |
e5bae867 | 409 | } |
5daa7b21 | 410 | |
46b5889c MR |
411 | mutex_lock(&master->master.partitions_lock); |
412 | list_add_tail(&child->part.node, &parent->partitions); | |
413 | mutex_unlock(&master->master.partitions_lock); | |
5daa7b21 | 414 | |
46b5889c | 415 | ret = add_mtd_device(child); |
2b6f0090 | 416 | if (ret) { |
46b5889c MR |
417 | mutex_lock(&master->master.partitions_lock); |
418 | list_del(&child->part.node); | |
419 | mutex_unlock(&master->master.partitions_lock); | |
2b6f0090 | 420 | |
46b5889c | 421 | free_partition(child); |
2b6f0090 BB |
422 | goto err_del_partitions; |
423 | } | |
424 | ||
46b5889c MR |
425 | mtd_add_partition_attrs(child); |
426 | ||
76a83225 | 427 | /* Look for subpartitions */ |
46b5889c | 428 | parse_mtd_partitions(child, parts[i].types, NULL); |
5daa7b21 | 429 | |
9e3307a1 | 430 | cur_offset = child->part.offset + child->part.size; |
1da177e4 LT |
431 | } |
432 | ||
433 | return 0; | |
2b6f0090 BB |
434 | |
435 | err_del_partitions: | |
436 | del_mtd_partitions(master); | |
437 | ||
438 | return ret; | |
1da177e4 | 439 | } |
1da177e4 LT |
440 | |
441 | static DEFINE_SPINLOCK(part_parser_lock); | |
442 | static LIST_HEAD(part_parsers); | |
443 | ||
5531ae48 | 444 | static struct mtd_part_parser *mtd_part_parser_get(const char *name) |
1da177e4 | 445 | { |
71a928c0 | 446 | struct mtd_part_parser *p, *ret = NULL; |
1da177e4 | 447 | |
71a928c0 | 448 | spin_lock(&part_parser_lock); |
1da177e4 | 449 | |
71a928c0 | 450 | list_for_each_entry(p, &part_parsers, list) |
1da177e4 LT |
451 | if (!strcmp(p->name, name) && try_module_get(p->owner)) { |
452 | ret = p; | |
453 | break; | |
454 | } | |
71a928c0 | 455 | |
1da177e4 LT |
456 | spin_unlock(&part_parser_lock); |
457 | ||
458 | return ret; | |
459 | } | |
460 | ||
5531ae48 BN |
461 | static inline void mtd_part_parser_put(const struct mtd_part_parser *p) |
462 | { | |
463 | module_put(p->owner); | |
464 | } | |
953b3bd1 | 465 | |
adc83bf8 BN |
466 | /* |
467 | * Many partition parsers just expected the core to kfree() all their data in | |
468 | * one chunk. Do that by default. | |
469 | */ | |
470 | static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts, | |
471 | int nr_parts) | |
472 | { | |
473 | kfree(pparts); | |
474 | } | |
475 | ||
b9eab011 | 476 | int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner) |
1da177e4 | 477 | { |
b9eab011 BN |
478 | p->owner = owner; |
479 | ||
adc83bf8 BN |
480 | if (!p->cleanup) |
481 | p->cleanup = &mtd_part_parser_cleanup_default; | |
482 | ||
1da177e4 LT |
483 | spin_lock(&part_parser_lock); |
484 | list_add(&p->list, &part_parsers); | |
485 | spin_unlock(&part_parser_lock); | |
b9eab011 BN |
486 | |
487 | return 0; | |
1da177e4 | 488 | } |
b9eab011 | 489 | EXPORT_SYMBOL_GPL(__register_mtd_parser); |
1da177e4 | 490 | |
cf3b2b1e | 491 | void deregister_mtd_parser(struct mtd_part_parser *p) |
1da177e4 LT |
492 | { |
493 | spin_lock(&part_parser_lock); | |
494 | list_del(&p->list); | |
495 | spin_unlock(&part_parser_lock); | |
1da177e4 | 496 | } |
b33a2887 | 497 | EXPORT_SYMBOL_GPL(deregister_mtd_parser); |
1da177e4 | 498 | |
ad274cec AB |
499 | /* |
500 | * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you | |
501 | * are changing this array! | |
502 | */ | |
ccef4dcc | 503 | static const char * const default_mtd_part_types[] = { |
d26c87d6 DB |
504 | "cmdlinepart", |
505 | "ofpart", | |
506 | NULL | |
507 | }; | |
5c4eefbd | 508 | |
76a83225 RM |
509 | /* Check DT only when looking for subpartitions. */ |
510 | static const char * const default_subpartition_types[] = { | |
511 | "ofpart", | |
512 | NULL | |
513 | }; | |
514 | ||
01f9c724 BN |
515 | static int mtd_part_do_parse(struct mtd_part_parser *parser, |
516 | struct mtd_info *master, | |
517 | struct mtd_partitions *pparts, | |
518 | struct mtd_part_parser_data *data) | |
519 | { | |
520 | int ret; | |
521 | ||
522 | ret = (*parser->parse_fn)(master, &pparts->parts, data); | |
523 | pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret); | |
524 | if (ret <= 0) | |
525 | return ret; | |
526 | ||
527 | pr_notice("%d %s partitions found on MTD device %s\n", ret, | |
528 | parser->name, master->name); | |
529 | ||
530 | pparts->nr_parts = ret; | |
531 | pparts->parser = parser; | |
532 | ||
533 | return ret; | |
534 | } | |
535 | ||
5b644aa0 RM |
536 | /** |
537 | * mtd_part_get_compatible_parser - find MTD parser by a compatible string | |
538 | * | |
539 | * @compat: compatible string describing partitions in a device tree | |
540 | * | |
541 | * MTD parsers can specify supported partitions by providing a table of | |
542 | * compatibility strings. This function finds a parser that advertises support | |
543 | * for a passed value of "compatible". | |
544 | */ | |
545 | static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat) | |
546 | { | |
547 | struct mtd_part_parser *p, *ret = NULL; | |
548 | ||
549 | spin_lock(&part_parser_lock); | |
550 | ||
551 | list_for_each_entry(p, &part_parsers, list) { | |
552 | const struct of_device_id *matches; | |
553 | ||
554 | matches = p->of_match_table; | |
555 | if (!matches) | |
556 | continue; | |
557 | ||
558 | for (; matches->compatible[0]; matches++) { | |
559 | if (!strcmp(matches->compatible, compat) && | |
560 | try_module_get(p->owner)) { | |
561 | ret = p; | |
562 | break; | |
563 | } | |
564 | } | |
565 | ||
566 | if (ret) | |
567 | break; | |
568 | } | |
569 | ||
570 | spin_unlock(&part_parser_lock); | |
571 | ||
572 | return ret; | |
573 | } | |
574 | ||
575 | static int mtd_part_of_parse(struct mtd_info *master, | |
576 | struct mtd_partitions *pparts) | |
577 | { | |
578 | struct mtd_part_parser *parser; | |
579 | struct device_node *np; | |
580 | struct property *prop; | |
581 | const char *compat; | |
c0faf434 | 582 | const char *fixed = "fixed-partitions"; |
5b644aa0 RM |
583 | int ret, err = 0; |
584 | ||
76a83225 | 585 | np = mtd_get_of_node(master); |
85516a98 MR |
586 | if (mtd_is_partition(master)) |
587 | of_node_get(np); | |
588 | else | |
76a83225 | 589 | np = of_get_child_by_name(np, "partitions"); |
85516a98 | 590 | |
5b644aa0 RM |
591 | of_property_for_each_string(np, "compatible", prop, compat) { |
592 | parser = mtd_part_get_compatible_parser(compat); | |
593 | if (!parser) | |
594 | continue; | |
595 | ret = mtd_part_do_parse(parser, master, pparts, NULL); | |
596 | if (ret > 0) { | |
597 | of_node_put(np); | |
598 | return ret; | |
599 | } | |
600 | mtd_part_parser_put(parser); | |
601 | if (ret < 0 && !err) | |
602 | err = ret; | |
603 | } | |
604 | of_node_put(np); | |
605 | ||
606 | /* | |
c0faf434 | 607 | * For backward compatibility we have to try the "fixed-partitions" |
5b644aa0 RM |
608 | * parser. It supports old DT format with partitions specified as a |
609 | * direct subnodes of a flash device DT node without any compatibility | |
610 | * specified we could match. | |
611 | */ | |
612 | parser = mtd_part_parser_get(fixed); | |
613 | if (!parser && !request_module("%s", fixed)) | |
614 | parser = mtd_part_parser_get(fixed); | |
615 | if (parser) { | |
616 | ret = mtd_part_do_parse(parser, master, pparts, NULL); | |
617 | if (ret > 0) | |
618 | return ret; | |
619 | mtd_part_parser_put(parser); | |
620 | if (ret < 0 && !err) | |
621 | err = ret; | |
622 | } | |
623 | ||
624 | return err; | |
625 | } | |
626 | ||
ad274cec | 627 | /** |
5ac67ce3 RM |
628 | * parse_mtd_partitions - parse and register MTD partitions |
629 | * | |
ad274cec AB |
630 | * @master: the master partition (describes whole MTD device) |
631 | * @types: names of partition parsers to try or %NULL | |
c7975330 | 632 | * @data: MTD partition parser-specific data |
ad274cec | 633 | * |
5ac67ce3 RM |
634 | * This function tries to find & register partitions on MTD device @master. It |
635 | * uses MTD partition parsers, specified in @types. However, if @types is %NULL, | |
636 | * then the default list of parsers is used. The default list contains only the | |
d26c87d6 | 637 | * "cmdlinepart" and "ofpart" parsers ATM. |
c51803dd HS |
638 | * Note: If there are more then one parser in @types, the kernel only takes the |
639 | * partitions parsed out by the first parser. | |
ad274cec AB |
640 | * |
641 | * This function may return: | |
642 | * o a negative error code in case of failure | |
5ac67ce3 | 643 | * o number of found partitions otherwise |
ad274cec | 644 | */ |
26a47346 | 645 | int parse_mtd_partitions(struct mtd_info *master, const char *const *types, |
c7975330 | 646 | struct mtd_part_parser_data *data) |
1da177e4 | 647 | { |
5ac67ce3 | 648 | struct mtd_partitions pparts = { }; |
1da177e4 | 649 | struct mtd_part_parser *parser; |
5a2415b0 | 650 | int ret, err = 0; |
97894cda | 651 | |
5c4eefbd | 652 | if (!types) |
76a83225 RM |
653 | types = mtd_is_partition(master) ? default_subpartition_types : |
654 | default_mtd_part_types; | |
5c4eefbd | 655 | |
5a2415b0 | 656 | for ( ; *types; types++) { |
5b644aa0 RM |
657 | /* |
658 | * ofpart is a special type that means OF partitioning info | |
659 | * should be used. It requires a bit different logic so it is | |
660 | * handled in a separated function. | |
661 | */ | |
662 | if (!strcmp(*types, "ofpart")) { | |
5ac67ce3 | 663 | ret = mtd_part_of_parse(master, &pparts); |
5b644aa0 RM |
664 | } else { |
665 | pr_debug("%s: parsing partitions %s\n", master->name, | |
666 | *types); | |
5531ae48 | 667 | parser = mtd_part_parser_get(*types); |
5b644aa0 RM |
668 | if (!parser && !request_module("%s", *types)) |
669 | parser = mtd_part_parser_get(*types); | |
670 | pr_debug("%s: got parser %s\n", master->name, | |
671 | parser ? parser->name : NULL); | |
672 | if (!parser) | |
673 | continue; | |
5ac67ce3 | 674 | ret = mtd_part_do_parse(parser, master, &pparts, data); |
5b644aa0 RM |
675 | if (ret <= 0) |
676 | mtd_part_parser_put(parser); | |
677 | } | |
01f9c724 | 678 | /* Found partitions! */ |
5ac67ce3 RM |
679 | if (ret > 0) { |
680 | err = add_mtd_partitions(master, pparts.parts, | |
681 | pparts.nr_parts); | |
682 | mtd_part_parser_cleanup(&pparts); | |
683 | return err ? err : pparts.nr_parts; | |
684 | } | |
5a2415b0 BN |
685 | /* |
686 | * Stash the first error we see; only report it if no parser | |
687 | * succeeds | |
688 | */ | |
689 | if (ret < 0 && !err) | |
690 | err = ret; | |
1da177e4 | 691 | } |
5a2415b0 | 692 | return err; |
1da177e4 | 693 | } |
5daa7b21 | 694 | |
adc83bf8 BN |
695 | void mtd_part_parser_cleanup(struct mtd_partitions *parts) |
696 | { | |
697 | const struct mtd_part_parser *parser; | |
698 | ||
699 | if (!parts) | |
700 | return; | |
701 | ||
702 | parser = parts->parser; | |
703 | if (parser) { | |
704 | if (parser->cleanup) | |
705 | parser->cleanup(parts->parts, parts->nr_parts); | |
706 | ||
707 | mtd_part_parser_put(parser); | |
708 | } | |
709 | } | |
710 | ||
62082e56 RG |
711 | /* Returns the size of the entire flash chip */ |
712 | uint64_t mtd_get_device_size(const struct mtd_info *mtd) | |
713 | { | |
46b5889c | 714 | struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd); |
62082e56 | 715 | |
46b5889c | 716 | return master->size; |
62082e56 RG |
717 | } |
718 | EXPORT_SYMBOL_GPL(mtd_get_device_size); |