]>
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 | ||
1da177e4 LT |
23 | /* Our partition linked list */ |
24 | static LIST_HEAD(mtd_partitions); | |
5daa7b21 | 25 | static DEFINE_MUTEX(mtd_partitions_mutex); |
1da177e4 | 26 | |
0a9d72b6 RM |
27 | /** |
28 | * struct mtd_part - our partition node structure | |
29 | * | |
30 | * @mtd: struct holding partition details | |
31 | * @parent: parent mtd - flash device or another partition | |
32 | * @offset: partition offset relative to the *flash device* | |
33 | */ | |
1da177e4 LT |
34 | struct mtd_part { |
35 | struct mtd_info mtd; | |
0a9d72b6 | 36 | struct mtd_info *parent; |
69423d99 | 37 | uint64_t offset; |
1da177e4 | 38 | struct list_head list; |
1da177e4 LT |
39 | }; |
40 | ||
41 | /* | |
42 | * Given a pointer to the MTD object in the mtd_part structure, we can retrieve | |
2524534d | 43 | * the pointer to that structure. |
1da177e4 | 44 | */ |
2524534d BN |
45 | static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd) |
46 | { | |
47 | return container_of(mtd, struct mtd_part, mtd); | |
48 | } | |
1da177e4 | 49 | |
6750f61a RM |
50 | static u64 part_absolute_offset(struct mtd_info *mtd) |
51 | { | |
52 | struct mtd_part *part = mtd_to_part(mtd); | |
53 | ||
54 | if (!mtd_is_partition(mtd)) | |
55 | return 0; | |
56 | ||
57 | return part_absolute_offset(part->parent) + part->offset; | |
58 | } | |
97894cda TG |
59 | |
60 | /* | |
1da177e4 LT |
61 | * MTD methods which simply translate the effective address and pass through |
62 | * to the _real_ device. | |
63 | */ | |
64 | ||
b33a2887 AN |
65 | static int part_read(struct mtd_info *mtd, loff_t from, size_t len, |
66 | size_t *retlen, u_char *buf) | |
1da177e4 | 67 | { |
2524534d | 68 | struct mtd_part *part = mtd_to_part(mtd); |
d8877f19 | 69 | struct mtd_ecc_stats stats; |
f1a28c02 TG |
70 | int res; |
71 | ||
0a9d72b6 RM |
72 | stats = part->parent->ecc_stats; |
73 | res = part->parent->_read(part->parent, from + part->offset, len, | |
994c8409 | 74 | retlen, buf); |
edbc4540 MD |
75 | if (unlikely(mtd_is_eccerr(res))) |
76 | mtd->ecc_stats.failed += | |
0a9d72b6 | 77 | part->parent->ecc_stats.failed - stats.failed; |
edbc4540 MD |
78 | else |
79 | mtd->ecc_stats.corrected += | |
0a9d72b6 | 80 | part->parent->ecc_stats.corrected - stats.corrected; |
f1a28c02 | 81 | return res; |
1da177e4 LT |
82 | } |
83 | ||
b33a2887 AN |
84 | static int part_point(struct mtd_info *mtd, loff_t from, size_t len, |
85 | size_t *retlen, void **virt, resource_size_t *phys) | |
1da177e4 | 86 | { |
2524534d | 87 | struct mtd_part *part = mtd_to_part(mtd); |
5def4898 | 88 | |
0a9d72b6 | 89 | return part->parent->_point(part->parent, from + part->offset, len, |
994c8409 | 90 | retlen, virt, phys); |
1da177e4 | 91 | } |
9223a456 | 92 | |
5e4e6e3f | 93 | static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len) |
1da177e4 | 94 | { |
2524534d | 95 | struct mtd_part *part = mtd_to_part(mtd); |
1da177e4 | 96 | |
0a9d72b6 | 97 | return part->parent->_unpoint(part->parent, from + part->offset, len); |
1da177e4 LT |
98 | } |
99 | ||
8593fbc6 | 100 | static int part_read_oob(struct mtd_info *mtd, loff_t from, |
b33a2887 | 101 | struct mtd_oob_ops *ops) |
1da177e4 | 102 | { |
2524534d | 103 | struct mtd_part *part = mtd_to_part(mtd); |
d020fc8e | 104 | struct mtd_ecc_stats stats; |
f1a28c02 | 105 | int res; |
8593fbc6 | 106 | |
d020fc8e | 107 | stats = part->parent->ecc_stats; |
0a9d72b6 | 108 | res = part->parent->_read_oob(part->parent, from + part->offset, ops); |
d020fc8e BB |
109 | if (unlikely(mtd_is_eccerr(res))) |
110 | mtd->ecc_stats.failed += | |
111 | part->parent->ecc_stats.failed - stats.failed; | |
112 | else | |
113 | mtd->ecc_stats.corrected += | |
114 | part->parent->ecc_stats.corrected - stats.corrected; | |
f1a28c02 | 115 | return res; |
1da177e4 LT |
116 | } |
117 | ||
b33a2887 AN |
118 | static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from, |
119 | size_t len, size_t *retlen, u_char *buf) | |
1da177e4 | 120 | { |
2524534d | 121 | struct mtd_part *part = mtd_to_part(mtd); |
0a9d72b6 | 122 | return part->parent->_read_user_prot_reg(part->parent, from, len, |
994c8409 | 123 | retlen, buf); |
1da177e4 LT |
124 | } |
125 | ||
4b78fc42 CR |
126 | static int part_get_user_prot_info(struct mtd_info *mtd, size_t len, |
127 | size_t *retlen, struct otp_info *buf) | |
f77814dd | 128 | { |
2524534d | 129 | struct mtd_part *part = mtd_to_part(mtd); |
0a9d72b6 | 130 | return part->parent->_get_user_prot_info(part->parent, len, retlen, |
4b78fc42 | 131 | buf); |
f77814dd NP |
132 | } |
133 | ||
b33a2887 AN |
134 | static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, |
135 | size_t len, size_t *retlen, u_char *buf) | |
1da177e4 | 136 | { |
2524534d | 137 | struct mtd_part *part = mtd_to_part(mtd); |
0a9d72b6 | 138 | return part->parent->_read_fact_prot_reg(part->parent, from, len, |
994c8409 | 139 | retlen, buf); |
1da177e4 LT |
140 | } |
141 | ||
4b78fc42 CR |
142 | static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len, |
143 | size_t *retlen, struct otp_info *buf) | |
f77814dd | 144 | { |
2524534d | 145 | struct mtd_part *part = mtd_to_part(mtd); |
0a9d72b6 | 146 | return part->parent->_get_fact_prot_info(part->parent, len, retlen, |
4b78fc42 | 147 | buf); |
f77814dd NP |
148 | } |
149 | ||
b33a2887 AN |
150 | static int part_write(struct mtd_info *mtd, loff_t to, size_t len, |
151 | size_t *retlen, const u_char *buf) | |
1da177e4 | 152 | { |
2524534d | 153 | struct mtd_part *part = mtd_to_part(mtd); |
0a9d72b6 | 154 | return part->parent->_write(part->parent, to + part->offset, len, |
994c8409 | 155 | retlen, buf); |
1da177e4 LT |
156 | } |
157 | ||
b33a2887 AN |
158 | static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len, |
159 | size_t *retlen, const u_char *buf) | |
388bbb09 | 160 | { |
2524534d | 161 | struct mtd_part *part = mtd_to_part(mtd); |
0a9d72b6 | 162 | return part->parent->_panic_write(part->parent, to + part->offset, len, |
994c8409 | 163 | retlen, buf); |
388bbb09 RP |
164 | } |
165 | ||
8593fbc6 | 166 | static int part_write_oob(struct mtd_info *mtd, loff_t to, |
b33a2887 | 167 | struct mtd_oob_ops *ops) |
1da177e4 | 168 | { |
2524534d | 169 | struct mtd_part *part = mtd_to_part(mtd); |
8593fbc6 | 170 | |
0a9d72b6 | 171 | return part->parent->_write_oob(part->parent, to + part->offset, ops); |
1da177e4 LT |
172 | } |
173 | ||
b33a2887 AN |
174 | static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from, |
175 | size_t len, size_t *retlen, u_char *buf) | |
1da177e4 | 176 | { |
2524534d | 177 | struct mtd_part *part = mtd_to_part(mtd); |
0a9d72b6 | 178 | return part->parent->_write_user_prot_reg(part->parent, from, len, |
994c8409 | 179 | retlen, buf); |
1da177e4 LT |
180 | } |
181 | ||
b33a2887 AN |
182 | static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, |
183 | size_t len) | |
f77814dd | 184 | { |
2524534d | 185 | struct mtd_part *part = mtd_to_part(mtd); |
0a9d72b6 | 186 | return part->parent->_lock_user_prot_reg(part->parent, from, len); |
f77814dd NP |
187 | } |
188 | ||
b33a2887 AN |
189 | static int part_writev(struct mtd_info *mtd, const struct kvec *vecs, |
190 | unsigned long count, loff_t to, size_t *retlen) | |
1da177e4 | 191 | { |
2524534d | 192 | struct mtd_part *part = mtd_to_part(mtd); |
0a9d72b6 | 193 | return part->parent->_writev(part->parent, vecs, count, |
994c8409 | 194 | to + part->offset, retlen); |
1da177e4 LT |
195 | } |
196 | ||
b33a2887 | 197 | static int part_erase(struct mtd_info *mtd, struct erase_info *instr) |
1da177e4 | 198 | { |
2524534d | 199 | struct mtd_part *part = mtd_to_part(mtd); |
1da177e4 | 200 | int ret; |
664addc2 | 201 | |
1da177e4 | 202 | instr->addr += part->offset; |
0a9d72b6 | 203 | ret = part->parent->_erase(part->parent, instr); |
8f347c42 BB |
204 | if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) |
205 | instr->fail_addr -= part->offset; | |
206 | instr->addr -= part->offset; | |
207 | ||
1da177e4 LT |
208 | return ret; |
209 | } | |
210 | ||
69423d99 | 211 | static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
1da177e4 | 212 | { |
2524534d | 213 | struct mtd_part *part = mtd_to_part(mtd); |
0a9d72b6 | 214 | return part->parent->_lock(part->parent, ofs + part->offset, len); |
1da177e4 LT |
215 | } |
216 | ||
69423d99 | 217 | static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
1da177e4 | 218 | { |
2524534d | 219 | struct mtd_part *part = mtd_to_part(mtd); |
0a9d72b6 | 220 | return part->parent->_unlock(part->parent, ofs + part->offset, len); |
1da177e4 LT |
221 | } |
222 | ||
9938424f RC |
223 | static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
224 | { | |
2524534d | 225 | struct mtd_part *part = mtd_to_part(mtd); |
0a9d72b6 | 226 | return part->parent->_is_locked(part->parent, ofs + part->offset, len); |
9938424f RC |
227 | } |
228 | ||
1da177e4 LT |
229 | static void part_sync(struct mtd_info *mtd) |
230 | { | |
2524534d | 231 | struct mtd_part *part = mtd_to_part(mtd); |
0a9d72b6 | 232 | part->parent->_sync(part->parent); |
1da177e4 LT |
233 | } |
234 | ||
235 | static int part_suspend(struct mtd_info *mtd) | |
236 | { | |
2524534d | 237 | struct mtd_part *part = mtd_to_part(mtd); |
0a9d72b6 | 238 | return part->parent->_suspend(part->parent); |
1da177e4 LT |
239 | } |
240 | ||
241 | static void part_resume(struct mtd_info *mtd) | |
242 | { | |
2524534d | 243 | struct mtd_part *part = mtd_to_part(mtd); |
0a9d72b6 | 244 | part->parent->_resume(part->parent); |
1da177e4 LT |
245 | } |
246 | ||
8471bb73 EG |
247 | static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
248 | { | |
2524534d | 249 | struct mtd_part *part = mtd_to_part(mtd); |
8471bb73 | 250 | ofs += part->offset; |
0a9d72b6 | 251 | return part->parent->_block_isreserved(part->parent, ofs); |
8471bb73 EG |
252 | } |
253 | ||
b33a2887 | 254 | static int part_block_isbad(struct mtd_info *mtd, loff_t ofs) |
1da177e4 | 255 | { |
2524534d | 256 | struct mtd_part *part = mtd_to_part(mtd); |
1da177e4 | 257 | ofs += part->offset; |
0a9d72b6 | 258 | return part->parent->_block_isbad(part->parent, ofs); |
1da177e4 LT |
259 | } |
260 | ||
b33a2887 | 261 | static int part_block_markbad(struct mtd_info *mtd, loff_t ofs) |
1da177e4 | 262 | { |
2524534d | 263 | struct mtd_part *part = mtd_to_part(mtd); |
f1a28c02 TG |
264 | int res; |
265 | ||
1da177e4 | 266 | ofs += part->offset; |
0a9d72b6 | 267 | res = part->parent->_block_markbad(part->parent, ofs); |
f1a28c02 TG |
268 | if (!res) |
269 | mtd->ecc_stats.badblocks++; | |
270 | return res; | |
1da177e4 LT |
271 | } |
272 | ||
5e149073 RW |
273 | static int part_get_device(struct mtd_info *mtd) |
274 | { | |
275 | struct mtd_part *part = mtd_to_part(mtd); | |
0a9d72b6 | 276 | return part->parent->_get_device(part->parent); |
5e149073 RW |
277 | } |
278 | ||
279 | static void part_put_device(struct mtd_info *mtd) | |
280 | { | |
281 | struct mtd_part *part = mtd_to_part(mtd); | |
0a9d72b6 | 282 | part->parent->_put_device(part->parent); |
5e149073 RW |
283 | } |
284 | ||
adbbc3bc BB |
285 | static int part_ooblayout_ecc(struct mtd_info *mtd, int section, |
286 | struct mtd_oob_region *oobregion) | |
287 | { | |
288 | struct mtd_part *part = mtd_to_part(mtd); | |
289 | ||
0a9d72b6 | 290 | return mtd_ooblayout_ecc(part->parent, section, oobregion); |
adbbc3bc BB |
291 | } |
292 | ||
293 | static int part_ooblayout_free(struct mtd_info *mtd, int section, | |
294 | struct mtd_oob_region *oobregion) | |
295 | { | |
296 | struct mtd_part *part = mtd_to_part(mtd); | |
297 | ||
0a9d72b6 | 298 | return mtd_ooblayout_free(part->parent, section, oobregion); |
adbbc3bc BB |
299 | } |
300 | ||
301 | static const struct mtd_ooblayout_ops part_ooblayout_ops = { | |
302 | .ecc = part_ooblayout_ecc, | |
303 | .free = part_ooblayout_free, | |
304 | }; | |
305 | ||
6080ef6e JW |
306 | static int part_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len) |
307 | { | |
308 | struct mtd_part *part = mtd_to_part(mtd); | |
309 | ||
0a9d72b6 | 310 | return part->parent->_max_bad_blocks(part->parent, |
6080ef6e JW |
311 | ofs + part->offset, len); |
312 | } | |
313 | ||
5daa7b21 RT |
314 | static inline void free_partition(struct mtd_part *p) |
315 | { | |
316 | kfree(p->mtd.name); | |
317 | kfree(p); | |
318 | } | |
319 | ||
0a9d72b6 | 320 | static struct mtd_part *allocate_partition(struct mtd_info *parent, |
5daa7b21 RT |
321 | const struct mtd_partition *part, int partno, |
322 | uint64_t cur_offset) | |
7788ba71 | 323 | { |
c169e3d3 | 324 | int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize : |
0a9d72b6 | 325 | parent->erasesize; |
7788ba71 | 326 | struct mtd_part *slave; |
1eeef2d7 | 327 | u32 remainder; |
5daa7b21 | 328 | char *name; |
1eeef2d7 | 329 | u64 tmp; |
7788ba71 AN |
330 | |
331 | /* allocate the partition structure */ | |
b33a2887 | 332 | slave = kzalloc(sizeof(*slave), GFP_KERNEL); |
5daa7b21 RT |
333 | name = kstrdup(part->name, GFP_KERNEL); |
334 | if (!name || !slave) { | |
b33a2887 | 335 | printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n", |
0a9d72b6 | 336 | parent->name); |
5daa7b21 RT |
337 | kfree(name); |
338 | kfree(slave); | |
339 | return ERR_PTR(-ENOMEM); | |
7788ba71 | 340 | } |
7788ba71 AN |
341 | |
342 | /* set up the MTD object for this partition */ | |
0a9d72b6 | 343 | slave->mtd.type = parent->type; |
1186af45 RM |
344 | slave->mtd.flags = parent->orig_flags & ~part->mask_flags; |
345 | slave->mtd.orig_flags = slave->mtd.flags; | |
7788ba71 | 346 | slave->mtd.size = part->size; |
0a9d72b6 RM |
347 | slave->mtd.writesize = parent->writesize; |
348 | slave->mtd.writebufsize = parent->writebufsize; | |
349 | slave->mtd.oobsize = parent->oobsize; | |
350 | slave->mtd.oobavail = parent->oobavail; | |
351 | slave->mtd.subpage_sft = parent->subpage_sft; | |
352 | slave->mtd.pairing = parent->pairing; | |
7788ba71 | 353 | |
5daa7b21 | 354 | slave->mtd.name = name; |
0a9d72b6 | 355 | slave->mtd.owner = parent->owner; |
7788ba71 | 356 | |
727dc612 DE |
357 | /* NOTE: Historically, we didn't arrange MTDs as a tree out of |
358 | * concern for showing the same data in multiple partitions. | |
359 | * However, it is very useful to have the master node present, | |
360 | * so the MTD_PARTITIONED_MASTER option allows that. The master | |
361 | * will have device nodes etc only if this is set, so make the | |
362 | * parent conditional on that option. Note, this is a way to | |
363 | * distinguish between the master and the partition in sysfs. | |
1f24b5a8 | 364 | */ |
97519dc5 | 365 | slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ? |
0a9d72b6 RM |
366 | &parent->dev : |
367 | parent->dev.parent; | |
42e9401b | 368 | slave->mtd.dev.of_node = part->of_node; |
1f24b5a8 | 369 | |
24ff1292 BB |
370 | if (parent->_read) |
371 | slave->mtd._read = part_read; | |
372 | if (parent->_write) | |
373 | slave->mtd._write = part_write; | |
7788ba71 | 374 | |
0a9d72b6 | 375 | if (parent->_panic_write) |
3c3c10bb | 376 | slave->mtd._panic_write = part_panic_write; |
7788ba71 | 377 | |
0a9d72b6 | 378 | if (parent->_point && parent->_unpoint) { |
3c3c10bb AB |
379 | slave->mtd._point = part_point; |
380 | slave->mtd._unpoint = part_unpoint; | |
7788ba71 AN |
381 | } |
382 | ||
0a9d72b6 | 383 | if (parent->_read_oob) |
3c3c10bb | 384 | slave->mtd._read_oob = part_read_oob; |
0a9d72b6 | 385 | if (parent->_write_oob) |
3c3c10bb | 386 | slave->mtd._write_oob = part_write_oob; |
0a9d72b6 | 387 | if (parent->_read_user_prot_reg) |
3c3c10bb | 388 | slave->mtd._read_user_prot_reg = part_read_user_prot_reg; |
0a9d72b6 | 389 | if (parent->_read_fact_prot_reg) |
3c3c10bb | 390 | slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg; |
0a9d72b6 | 391 | if (parent->_write_user_prot_reg) |
3c3c10bb | 392 | slave->mtd._write_user_prot_reg = part_write_user_prot_reg; |
0a9d72b6 | 393 | if (parent->_lock_user_prot_reg) |
3c3c10bb | 394 | slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg; |
0a9d72b6 | 395 | if (parent->_get_user_prot_info) |
3c3c10bb | 396 | slave->mtd._get_user_prot_info = part_get_user_prot_info; |
0a9d72b6 | 397 | if (parent->_get_fact_prot_info) |
3c3c10bb | 398 | slave->mtd._get_fact_prot_info = part_get_fact_prot_info; |
0a9d72b6 | 399 | if (parent->_sync) |
3c3c10bb | 400 | slave->mtd._sync = part_sync; |
0a9d72b6 RM |
401 | if (!partno && !parent->dev.class && parent->_suspend && |
402 | parent->_resume) { | |
c169e3d3 BN |
403 | slave->mtd._suspend = part_suspend; |
404 | slave->mtd._resume = part_resume; | |
7788ba71 | 405 | } |
0a9d72b6 | 406 | if (parent->_writev) |
3c3c10bb | 407 | slave->mtd._writev = part_writev; |
0a9d72b6 | 408 | if (parent->_lock) |
3c3c10bb | 409 | slave->mtd._lock = part_lock; |
0a9d72b6 | 410 | if (parent->_unlock) |
3c3c10bb | 411 | slave->mtd._unlock = part_unlock; |
0a9d72b6 | 412 | if (parent->_is_locked) |
3c3c10bb | 413 | slave->mtd._is_locked = part_is_locked; |
0a9d72b6 | 414 | if (parent->_block_isreserved) |
8471bb73 | 415 | slave->mtd._block_isreserved = part_block_isreserved; |
0a9d72b6 | 416 | if (parent->_block_isbad) |
3c3c10bb | 417 | slave->mtd._block_isbad = part_block_isbad; |
0a9d72b6 | 418 | if (parent->_block_markbad) |
3c3c10bb | 419 | slave->mtd._block_markbad = part_block_markbad; |
0a9d72b6 | 420 | if (parent->_max_bad_blocks) |
6080ef6e | 421 | slave->mtd._max_bad_blocks = part_max_bad_blocks; |
5e149073 | 422 | |
0a9d72b6 | 423 | if (parent->_get_device) |
5e149073 | 424 | slave->mtd._get_device = part_get_device; |
0a9d72b6 | 425 | if (parent->_put_device) |
5e149073 RW |
426 | slave->mtd._put_device = part_put_device; |
427 | ||
3c3c10bb | 428 | slave->mtd._erase = part_erase; |
0a9d72b6 | 429 | slave->parent = parent; |
7788ba71 | 430 | slave->offset = part->offset; |
7788ba71 AN |
431 | |
432 | if (slave->offset == MTDPART_OFS_APPEND) | |
433 | slave->offset = cur_offset; | |
434 | if (slave->offset == MTDPART_OFS_NXTBLK) { | |
1eeef2d7 | 435 | tmp = cur_offset; |
7788ba71 | 436 | slave->offset = cur_offset; |
1eeef2d7 CP |
437 | remainder = do_div(tmp, wr_alignment); |
438 | if (remainder) { | |
439 | slave->offset += wr_alignment - remainder; | |
7788ba71 | 440 | printk(KERN_NOTICE "Moving partition %d: " |
69423d99 AH |
441 | "0x%012llx -> 0x%012llx\n", partno, |
442 | (unsigned long long)cur_offset, (unsigned long long)slave->offset); | |
7788ba71 AN |
443 | } |
444 | } | |
1a31368b DB |
445 | if (slave->offset == MTDPART_OFS_RETAIN) { |
446 | slave->offset = cur_offset; | |
0a9d72b6 RM |
447 | if (parent->size - slave->offset >= slave->mtd.size) { |
448 | slave->mtd.size = parent->size - slave->offset | |
1a31368b DB |
449 | - slave->mtd.size; |
450 | } else { | |
451 | printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n", | |
0a9d72b6 | 452 | part->name, parent->size - slave->offset, |
1a31368b DB |
453 | slave->mtd.size); |
454 | /* register to preserve ordering */ | |
455 | goto out_register; | |
456 | } | |
457 | } | |
7788ba71 | 458 | if (slave->mtd.size == MTDPART_SIZ_FULL) |
0a9d72b6 | 459 | slave->mtd.size = parent->size - slave->offset; |
7788ba71 | 460 | |
69423d99 AH |
461 | printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset, |
462 | (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name); | |
7788ba71 AN |
463 | |
464 | /* let's do some sanity checks */ | |
0a9d72b6 | 465 | if (slave->offset >= parent->size) { |
f636ffb4 | 466 | /* let's register it anyway to preserve ordering */ |
7788ba71 AN |
467 | slave->offset = 0; |
468 | slave->mtd.size = 0; | |
ad463515 BB |
469 | |
470 | /* Initialize ->erasesize to make add_mtd_device() happy. */ | |
471 | slave->mtd.erasesize = parent->erasesize; | |
472 | ||
b33a2887 | 473 | printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n", |
7788ba71 | 474 | part->name); |
f636ffb4 | 475 | goto out_register; |
7788ba71 | 476 | } |
0a9d72b6 RM |
477 | if (slave->offset + slave->mtd.size > parent->size) { |
478 | slave->mtd.size = parent->size - slave->offset; | |
69423d99 | 479 | printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n", |
0a9d72b6 | 480 | part->name, parent->name, (unsigned long long)slave->mtd.size); |
7788ba71 | 481 | } |
0a9d72b6 | 482 | if (parent->numeraseregions > 1) { |
7788ba71 | 483 | /* Deal with variable erase size stuff */ |
0a9d72b6 | 484 | int i, max = parent->numeraseregions; |
69423d99 | 485 | u64 end = slave->offset + slave->mtd.size; |
0a9d72b6 | 486 | struct mtd_erase_region_info *regions = parent->eraseregions; |
7788ba71 | 487 | |
6910c136 AN |
488 | /* Find the first erase regions which is part of this |
489 | * partition. */ | |
490 | for (i = 0; i < max && regions[i].offset <= slave->offset; i++) | |
7788ba71 | 491 | ; |
6910c136 | 492 | /* The loop searched for the region _behind_ the first one */ |
a57ca046 RK |
493 | if (i > 0) |
494 | i--; | |
7788ba71 | 495 | |
6910c136 AN |
496 | /* Pick biggest erasesize */ |
497 | for (; i < max && regions[i].offset < end; i++) { | |
7788ba71 AN |
498 | if (slave->mtd.erasesize < regions[i].erasesize) { |
499 | slave->mtd.erasesize = regions[i].erasesize; | |
500 | } | |
501 | } | |
6910c136 | 502 | BUG_ON(slave->mtd.erasesize == 0); |
7788ba71 AN |
503 | } else { |
504 | /* Single erase size */ | |
0a9d72b6 | 505 | slave->mtd.erasesize = parent->erasesize; |
7788ba71 AN |
506 | } |
507 | ||
7e439681 BB |
508 | /* |
509 | * Slave erasesize might differ from the master one if the master | |
510 | * exposes several regions with different erasesize. Adjust | |
511 | * wr_alignment accordingly. | |
512 | */ | |
513 | if (!(slave->mtd.flags & MTD_NO_ERASE)) | |
514 | wr_alignment = slave->mtd.erasesize; | |
515 | ||
6750f61a | 516 | tmp = part_absolute_offset(parent) + slave->offset; |
1eeef2d7 CP |
517 | remainder = do_div(tmp, wr_alignment); |
518 | if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) { | |
7788ba71 | 519 | /* Doesn't start on a boundary of major erase size */ |
b33a2887 AN |
520 | /* FIXME: Let it be writable if it is on a boundary of |
521 | * _minor_ erase size though */ | |
7788ba71 | 522 | slave->mtd.flags &= ~MTD_WRITEABLE; |
1eeef2d7 | 523 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n", |
7788ba71 AN |
524 | part->name); |
525 | } | |
1eeef2d7 | 526 | |
6750f61a | 527 | tmp = part_absolute_offset(parent) + slave->mtd.size; |
1eeef2d7 CP |
528 | remainder = do_div(tmp, wr_alignment); |
529 | if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) { | |
7788ba71 | 530 | slave->mtd.flags &= ~MTD_WRITEABLE; |
1eeef2d7 | 531 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n", |
7788ba71 AN |
532 | part->name); |
533 | } | |
534 | ||
adbbc3bc | 535 | mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops); |
0a9d72b6 RM |
536 | slave->mtd.ecc_step_size = parent->ecc_step_size; |
537 | slave->mtd.ecc_strength = parent->ecc_strength; | |
538 | slave->mtd.bitflip_threshold = parent->bitflip_threshold; | |
d062d4ed | 539 | |
0a9d72b6 | 540 | if (parent->_block_isbad) { |
69423d99 | 541 | uint64_t offs = 0; |
7788ba71 | 542 | |
b33a2887 | 543 | while (offs < slave->mtd.size) { |
0a9d72b6 | 544 | if (mtd_block_isreserved(parent, offs + slave->offset)) |
fdf43a42 | 545 | slave->mtd.ecc_stats.bbtblocks++; |
0a9d72b6 | 546 | else if (mtd_block_isbad(parent, offs + slave->offset)) |
7788ba71 AN |
547 | slave->mtd.ecc_stats.badblocks++; |
548 | offs += slave->mtd.erasesize; | |
549 | } | |
550 | } | |
551 | ||
f636ffb4 | 552 | out_register: |
7788ba71 AN |
553 | return slave; |
554 | } | |
555 | ||
a62c24d7 DE |
556 | static ssize_t mtd_partition_offset_show(struct device *dev, |
557 | struct device_attribute *attr, char *buf) | |
558 | { | |
559 | struct mtd_info *mtd = dev_get_drvdata(dev); | |
2524534d | 560 | struct mtd_part *part = mtd_to_part(mtd); |
3008ba87 | 561 | return snprintf(buf, PAGE_SIZE, "%llu\n", part->offset); |
a62c24d7 DE |
562 | } |
563 | ||
564 | static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL); | |
565 | ||
566 | static const struct attribute *mtd_partition_attrs[] = { | |
567 | &dev_attr_offset.attr, | |
568 | NULL | |
569 | }; | |
570 | ||
571 | static int mtd_add_partition_attrs(struct mtd_part *new) | |
572 | { | |
573 | int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs); | |
574 | if (ret) | |
575 | printk(KERN_WARNING | |
576 | "mtd: failed to create partition attrs, err=%d\n", ret); | |
577 | return ret; | |
578 | } | |
579 | ||
0a9d72b6 | 580 | int mtd_add_partition(struct mtd_info *parent, const char *name, |
5daa7b21 RT |
581 | long long offset, long long length) |
582 | { | |
583 | struct mtd_partition part; | |
3a434f66 | 584 | struct mtd_part *new; |
5daa7b21 RT |
585 | int ret = 0; |
586 | ||
587 | /* the direct offset is expected */ | |
588 | if (offset == MTDPART_OFS_APPEND || | |
589 | offset == MTDPART_OFS_NXTBLK) | |
590 | return -EINVAL; | |
591 | ||
592 | if (length == MTDPART_SIZ_FULL) | |
0a9d72b6 | 593 | length = parent->size - offset; |
5daa7b21 RT |
594 | |
595 | if (length <= 0) | |
596 | return -EINVAL; | |
597 | ||
93867233 | 598 | memset(&part, 0, sizeof(part)); |
5daa7b21 RT |
599 | part.name = name; |
600 | part.size = length; | |
601 | part.offset = offset; | |
5daa7b21 | 602 | |
0a9d72b6 | 603 | new = allocate_partition(parent, &part, -1, offset); |
5daa7b21 RT |
604 | if (IS_ERR(new)) |
605 | return PTR_ERR(new); | |
606 | ||
5daa7b21 | 607 | mutex_lock(&mtd_partitions_mutex); |
5daa7b21 RT |
608 | list_add(&new->list, &mtd_partitions); |
609 | mutex_unlock(&mtd_partitions_mutex); | |
610 | ||
2b6f0090 BB |
611 | ret = add_mtd_device(&new->mtd); |
612 | if (ret) | |
613 | goto err_remove_part; | |
5daa7b21 | 614 | |
a62c24d7 DE |
615 | mtd_add_partition_attrs(new); |
616 | ||
2b6f0090 BB |
617 | return 0; |
618 | ||
619 | err_remove_part: | |
620 | mutex_lock(&mtd_partitions_mutex); | |
621 | list_del(&new->list); | |
622 | mutex_unlock(&mtd_partitions_mutex); | |
623 | ||
624 | free_partition(new); | |
2b6f0090 | 625 | |
5daa7b21 | 626 | return ret; |
5daa7b21 RT |
627 | } |
628 | EXPORT_SYMBOL_GPL(mtd_add_partition); | |
629 | ||
08263a9a RM |
630 | /** |
631 | * __mtd_del_partition - delete MTD partition | |
632 | * | |
633 | * @priv: internal MTD struct for partition to be deleted | |
634 | * | |
635 | * This function must be called with the partitions mutex locked. | |
636 | */ | |
637 | static int __mtd_del_partition(struct mtd_part *priv) | |
638 | { | |
97519dc5 | 639 | struct mtd_part *child, *next; |
08263a9a RM |
640 | int err; |
641 | ||
97519dc5 RM |
642 | list_for_each_entry_safe(child, next, &mtd_partitions, list) { |
643 | if (child->parent == &priv->mtd) { | |
644 | err = __mtd_del_partition(child); | |
645 | if (err) | |
646 | return err; | |
647 | } | |
648 | } | |
649 | ||
c5ceaba7 RM |
650 | sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs); |
651 | ||
08263a9a RM |
652 | err = del_mtd_device(&priv->mtd); |
653 | if (err) | |
654 | return err; | |
655 | ||
656 | list_del(&priv->list); | |
657 | free_partition(priv); | |
658 | ||
659 | return 0; | |
660 | } | |
661 | ||
662 | /* | |
663 | * This function unregisters and destroy all slave MTD objects which are | |
97519dc5 | 664 | * attached to the given MTD object. |
08263a9a | 665 | */ |
97519dc5 | 666 | int del_mtd_partitions(struct mtd_info *mtd) |
08263a9a RM |
667 | { |
668 | struct mtd_part *slave, *next; | |
669 | int ret, err = 0; | |
670 | ||
671 | mutex_lock(&mtd_partitions_mutex); | |
672 | list_for_each_entry_safe(slave, next, &mtd_partitions, list) | |
97519dc5 | 673 | if (slave->parent == mtd) { |
08263a9a RM |
674 | ret = __mtd_del_partition(slave); |
675 | if (ret < 0) | |
676 | err = ret; | |
677 | } | |
678 | mutex_unlock(&mtd_partitions_mutex); | |
679 | ||
680 | return err; | |
681 | } | |
682 | ||
97519dc5 | 683 | int mtd_del_partition(struct mtd_info *mtd, int partno) |
5daa7b21 RT |
684 | { |
685 | struct mtd_part *slave, *next; | |
686 | int ret = -EINVAL; | |
687 | ||
688 | mutex_lock(&mtd_partitions_mutex); | |
689 | list_for_each_entry_safe(slave, next, &mtd_partitions, list) | |
97519dc5 | 690 | if ((slave->parent == mtd) && |
5daa7b21 | 691 | (slave->mtd.index == partno)) { |
08263a9a | 692 | ret = __mtd_del_partition(slave); |
5daa7b21 RT |
693 | break; |
694 | } | |
695 | mutex_unlock(&mtd_partitions_mutex); | |
696 | ||
697 | return ret; | |
698 | } | |
699 | EXPORT_SYMBOL_GPL(mtd_del_partition); | |
700 | ||
1da177e4 LT |
701 | /* |
702 | * This function, given a master MTD object and a partition table, creates | |
703 | * and registers slave MTD objects which are bound to the master according to | |
704 | * the partition definitions. | |
1f24b5a8 | 705 | * |
727dc612 DE |
706 | * For historical reasons, this function's caller only registers the master |
707 | * if the MTD_PARTITIONED_MASTER config option is set. | |
1da177e4 LT |
708 | */ |
709 | ||
97894cda | 710 | int add_mtd_partitions(struct mtd_info *master, |
1da177e4 LT |
711 | const struct mtd_partition *parts, |
712 | int nbparts) | |
713 | { | |
714 | struct mtd_part *slave; | |
69423d99 | 715 | uint64_t cur_offset = 0; |
2b6f0090 | 716 | int i, ret; |
1da177e4 | 717 | |
b33a2887 | 718 | printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name); |
1da177e4 LT |
719 | |
720 | for (i = 0; i < nbparts; i++) { | |
5daa7b21 | 721 | slave = allocate_partition(master, parts + i, i, cur_offset); |
e5bae867 | 722 | if (IS_ERR(slave)) { |
2b6f0090 BB |
723 | ret = PTR_ERR(slave); |
724 | goto err_del_partitions; | |
e5bae867 | 725 | } |
5daa7b21 RT |
726 | |
727 | mutex_lock(&mtd_partitions_mutex); | |
728 | list_add(&slave->list, &mtd_partitions); | |
729 | mutex_unlock(&mtd_partitions_mutex); | |
730 | ||
2b6f0090 BB |
731 | ret = add_mtd_device(&slave->mtd); |
732 | if (ret) { | |
733 | mutex_lock(&mtd_partitions_mutex); | |
734 | list_del(&slave->list); | |
735 | mutex_unlock(&mtd_partitions_mutex); | |
736 | ||
737 | free_partition(slave); | |
738 | goto err_del_partitions; | |
739 | } | |
740 | ||
a62c24d7 | 741 | mtd_add_partition_attrs(slave); |
76a83225 RM |
742 | /* Look for subpartitions */ |
743 | parse_mtd_partitions(&slave->mtd, parts[i].types, NULL); | |
5daa7b21 | 744 | |
1da177e4 | 745 | cur_offset = slave->offset + slave->mtd.size; |
1da177e4 LT |
746 | } |
747 | ||
748 | return 0; | |
2b6f0090 BB |
749 | |
750 | err_del_partitions: | |
751 | del_mtd_partitions(master); | |
752 | ||
753 | return ret; | |
1da177e4 | 754 | } |
1da177e4 LT |
755 | |
756 | static DEFINE_SPINLOCK(part_parser_lock); | |
757 | static LIST_HEAD(part_parsers); | |
758 | ||
5531ae48 | 759 | static struct mtd_part_parser *mtd_part_parser_get(const char *name) |
1da177e4 | 760 | { |
71a928c0 | 761 | struct mtd_part_parser *p, *ret = NULL; |
1da177e4 | 762 | |
71a928c0 | 763 | spin_lock(&part_parser_lock); |
1da177e4 | 764 | |
71a928c0 | 765 | list_for_each_entry(p, &part_parsers, list) |
1da177e4 LT |
766 | if (!strcmp(p->name, name) && try_module_get(p->owner)) { |
767 | ret = p; | |
768 | break; | |
769 | } | |
71a928c0 | 770 | |
1da177e4 LT |
771 | spin_unlock(&part_parser_lock); |
772 | ||
773 | return ret; | |
774 | } | |
775 | ||
5531ae48 BN |
776 | static inline void mtd_part_parser_put(const struct mtd_part_parser *p) |
777 | { | |
778 | module_put(p->owner); | |
779 | } | |
953b3bd1 | 780 | |
adc83bf8 BN |
781 | /* |
782 | * Many partition parsers just expected the core to kfree() all their data in | |
783 | * one chunk. Do that by default. | |
784 | */ | |
785 | static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts, | |
786 | int nr_parts) | |
787 | { | |
788 | kfree(pparts); | |
789 | } | |
790 | ||
b9eab011 | 791 | int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner) |
1da177e4 | 792 | { |
b9eab011 BN |
793 | p->owner = owner; |
794 | ||
adc83bf8 BN |
795 | if (!p->cleanup) |
796 | p->cleanup = &mtd_part_parser_cleanup_default; | |
797 | ||
1da177e4 LT |
798 | spin_lock(&part_parser_lock); |
799 | list_add(&p->list, &part_parsers); | |
800 | spin_unlock(&part_parser_lock); | |
b9eab011 BN |
801 | |
802 | return 0; | |
1da177e4 | 803 | } |
b9eab011 | 804 | EXPORT_SYMBOL_GPL(__register_mtd_parser); |
1da177e4 | 805 | |
cf3b2b1e | 806 | void deregister_mtd_parser(struct mtd_part_parser *p) |
1da177e4 LT |
807 | { |
808 | spin_lock(&part_parser_lock); | |
809 | list_del(&p->list); | |
810 | spin_unlock(&part_parser_lock); | |
1da177e4 | 811 | } |
b33a2887 | 812 | EXPORT_SYMBOL_GPL(deregister_mtd_parser); |
1da177e4 | 813 | |
ad274cec AB |
814 | /* |
815 | * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you | |
816 | * are changing this array! | |
817 | */ | |
ccef4dcc | 818 | static const char * const default_mtd_part_types[] = { |
d26c87d6 DB |
819 | "cmdlinepart", |
820 | "ofpart", | |
821 | NULL | |
822 | }; | |
5c4eefbd | 823 | |
76a83225 RM |
824 | /* Check DT only when looking for subpartitions. */ |
825 | static const char * const default_subpartition_types[] = { | |
826 | "ofpart", | |
827 | NULL | |
828 | }; | |
829 | ||
01f9c724 BN |
830 | static int mtd_part_do_parse(struct mtd_part_parser *parser, |
831 | struct mtd_info *master, | |
832 | struct mtd_partitions *pparts, | |
833 | struct mtd_part_parser_data *data) | |
834 | { | |
835 | int ret; | |
836 | ||
837 | ret = (*parser->parse_fn)(master, &pparts->parts, data); | |
838 | pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret); | |
839 | if (ret <= 0) | |
840 | return ret; | |
841 | ||
842 | pr_notice("%d %s partitions found on MTD device %s\n", ret, | |
843 | parser->name, master->name); | |
844 | ||
845 | pparts->nr_parts = ret; | |
846 | pparts->parser = parser; | |
847 | ||
848 | return ret; | |
849 | } | |
850 | ||
5b644aa0 RM |
851 | /** |
852 | * mtd_part_get_compatible_parser - find MTD parser by a compatible string | |
853 | * | |
854 | * @compat: compatible string describing partitions in a device tree | |
855 | * | |
856 | * MTD parsers can specify supported partitions by providing a table of | |
857 | * compatibility strings. This function finds a parser that advertises support | |
858 | * for a passed value of "compatible". | |
859 | */ | |
860 | static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat) | |
861 | { | |
862 | struct mtd_part_parser *p, *ret = NULL; | |
863 | ||
864 | spin_lock(&part_parser_lock); | |
865 | ||
866 | list_for_each_entry(p, &part_parsers, list) { | |
867 | const struct of_device_id *matches; | |
868 | ||
869 | matches = p->of_match_table; | |
870 | if (!matches) | |
871 | continue; | |
872 | ||
873 | for (; matches->compatible[0]; matches++) { | |
874 | if (!strcmp(matches->compatible, compat) && | |
875 | try_module_get(p->owner)) { | |
876 | ret = p; | |
877 | break; | |
878 | } | |
879 | } | |
880 | ||
881 | if (ret) | |
882 | break; | |
883 | } | |
884 | ||
885 | spin_unlock(&part_parser_lock); | |
886 | ||
887 | return ret; | |
888 | } | |
889 | ||
890 | static int mtd_part_of_parse(struct mtd_info *master, | |
891 | struct mtd_partitions *pparts) | |
892 | { | |
893 | struct mtd_part_parser *parser; | |
894 | struct device_node *np; | |
895 | struct property *prop; | |
896 | const char *compat; | |
c0faf434 | 897 | const char *fixed = "fixed-partitions"; |
5b644aa0 RM |
898 | int ret, err = 0; |
899 | ||
76a83225 | 900 | np = mtd_get_of_node(master); |
85516a98 MR |
901 | if (mtd_is_partition(master)) |
902 | of_node_get(np); | |
903 | else | |
76a83225 | 904 | np = of_get_child_by_name(np, "partitions"); |
85516a98 | 905 | |
5b644aa0 RM |
906 | of_property_for_each_string(np, "compatible", prop, compat) { |
907 | parser = mtd_part_get_compatible_parser(compat); | |
908 | if (!parser) | |
909 | continue; | |
910 | ret = mtd_part_do_parse(parser, master, pparts, NULL); | |
911 | if (ret > 0) { | |
912 | of_node_put(np); | |
913 | return ret; | |
914 | } | |
915 | mtd_part_parser_put(parser); | |
916 | if (ret < 0 && !err) | |
917 | err = ret; | |
918 | } | |
919 | of_node_put(np); | |
920 | ||
921 | /* | |
c0faf434 | 922 | * For backward compatibility we have to try the "fixed-partitions" |
5b644aa0 RM |
923 | * parser. It supports old DT format with partitions specified as a |
924 | * direct subnodes of a flash device DT node without any compatibility | |
925 | * specified we could match. | |
926 | */ | |
927 | parser = mtd_part_parser_get(fixed); | |
928 | if (!parser && !request_module("%s", fixed)) | |
929 | parser = mtd_part_parser_get(fixed); | |
930 | if (parser) { | |
931 | ret = mtd_part_do_parse(parser, master, pparts, NULL); | |
932 | if (ret > 0) | |
933 | return ret; | |
934 | mtd_part_parser_put(parser); | |
935 | if (ret < 0 && !err) | |
936 | err = ret; | |
937 | } | |
938 | ||
939 | return err; | |
940 | } | |
941 | ||
ad274cec | 942 | /** |
5ac67ce3 RM |
943 | * parse_mtd_partitions - parse and register MTD partitions |
944 | * | |
ad274cec AB |
945 | * @master: the master partition (describes whole MTD device) |
946 | * @types: names of partition parsers to try or %NULL | |
c7975330 | 947 | * @data: MTD partition parser-specific data |
ad274cec | 948 | * |
5ac67ce3 RM |
949 | * This function tries to find & register partitions on MTD device @master. It |
950 | * uses MTD partition parsers, specified in @types. However, if @types is %NULL, | |
951 | * then the default list of parsers is used. The default list contains only the | |
d26c87d6 | 952 | * "cmdlinepart" and "ofpart" parsers ATM. |
c51803dd HS |
953 | * Note: If there are more then one parser in @types, the kernel only takes the |
954 | * partitions parsed out by the first parser. | |
ad274cec AB |
955 | * |
956 | * This function may return: | |
957 | * o a negative error code in case of failure | |
5ac67ce3 | 958 | * o number of found partitions otherwise |
ad274cec | 959 | */ |
26a47346 | 960 | int parse_mtd_partitions(struct mtd_info *master, const char *const *types, |
c7975330 | 961 | struct mtd_part_parser_data *data) |
1da177e4 | 962 | { |
5ac67ce3 | 963 | struct mtd_partitions pparts = { }; |
1da177e4 | 964 | struct mtd_part_parser *parser; |
5a2415b0 | 965 | int ret, err = 0; |
97894cda | 966 | |
5c4eefbd | 967 | if (!types) |
76a83225 RM |
968 | types = mtd_is_partition(master) ? default_subpartition_types : |
969 | default_mtd_part_types; | |
5c4eefbd | 970 | |
5a2415b0 | 971 | for ( ; *types; types++) { |
5b644aa0 RM |
972 | /* |
973 | * ofpart is a special type that means OF partitioning info | |
974 | * should be used. It requires a bit different logic so it is | |
975 | * handled in a separated function. | |
976 | */ | |
977 | if (!strcmp(*types, "ofpart")) { | |
5ac67ce3 | 978 | ret = mtd_part_of_parse(master, &pparts); |
5b644aa0 RM |
979 | } else { |
980 | pr_debug("%s: parsing partitions %s\n", master->name, | |
981 | *types); | |
5531ae48 | 982 | parser = mtd_part_parser_get(*types); |
5b644aa0 RM |
983 | if (!parser && !request_module("%s", *types)) |
984 | parser = mtd_part_parser_get(*types); | |
985 | pr_debug("%s: got parser %s\n", master->name, | |
986 | parser ? parser->name : NULL); | |
987 | if (!parser) | |
988 | continue; | |
5ac67ce3 | 989 | ret = mtd_part_do_parse(parser, master, &pparts, data); |
5b644aa0 RM |
990 | if (ret <= 0) |
991 | mtd_part_parser_put(parser); | |
992 | } | |
01f9c724 | 993 | /* Found partitions! */ |
5ac67ce3 RM |
994 | if (ret > 0) { |
995 | err = add_mtd_partitions(master, pparts.parts, | |
996 | pparts.nr_parts); | |
997 | mtd_part_parser_cleanup(&pparts); | |
998 | return err ? err : pparts.nr_parts; | |
999 | } | |
5a2415b0 BN |
1000 | /* |
1001 | * Stash the first error we see; only report it if no parser | |
1002 | * succeeds | |
1003 | */ | |
1004 | if (ret < 0 && !err) | |
1005 | err = ret; | |
1da177e4 | 1006 | } |
5a2415b0 | 1007 | return err; |
1da177e4 | 1008 | } |
5daa7b21 | 1009 | |
adc83bf8 BN |
1010 | void mtd_part_parser_cleanup(struct mtd_partitions *parts) |
1011 | { | |
1012 | const struct mtd_part_parser *parser; | |
1013 | ||
1014 | if (!parts) | |
1015 | return; | |
1016 | ||
1017 | parser = parts->parser; | |
1018 | if (parser) { | |
1019 | if (parser->cleanup) | |
1020 | parser->cleanup(parts->parts, parts->nr_parts); | |
1021 | ||
1022 | mtd_part_parser_put(parser); | |
1023 | } | |
1024 | } | |
1025 | ||
5dee4674 | 1026 | int mtd_is_partition(const struct mtd_info *mtd) |
5daa7b21 RT |
1027 | { |
1028 | struct mtd_part *part; | |
a7e93dcd | 1029 | int ispart = 0; |
5daa7b21 RT |
1030 | |
1031 | mutex_lock(&mtd_partitions_mutex); | |
1032 | list_for_each_entry(part, &mtd_partitions, list) | |
1033 | if (&part->mtd == mtd) { | |
a7e93dcd | 1034 | ispart = 1; |
5daa7b21 RT |
1035 | break; |
1036 | } | |
1037 | mutex_unlock(&mtd_partitions_mutex); | |
1038 | ||
a7e93dcd | 1039 | return ispart; |
5daa7b21 | 1040 | } |
a7e93dcd | 1041 | EXPORT_SYMBOL_GPL(mtd_is_partition); |
62082e56 RG |
1042 | |
1043 | /* Returns the size of the entire flash chip */ | |
1044 | uint64_t mtd_get_device_size(const struct mtd_info *mtd) | |
1045 | { | |
1046 | if (!mtd_is_partition(mtd)) | |
1047 | return mtd->size; | |
1048 | ||
97519dc5 | 1049 | return mtd_get_device_size(mtd_to_part(mtd)->parent); |
62082e56 RG |
1050 | } |
1051 | EXPORT_SYMBOL_GPL(mtd_get_device_size); |