Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Simple MTD partitioning layer | |
3 | * | |
a1452a37 DW |
4 | * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net> |
5 | * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de> | |
6 | * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org> | |
1da177e4 | 7 | * |
a1452a37 DW |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 21 | * |
97894cda | 22 | */ |
1da177e4 LT |
23 | |
24 | #include <linux/module.h> | |
25 | #include <linux/types.h> | |
26 | #include <linux/kernel.h> | |
27 | #include <linux/slab.h> | |
28 | #include <linux/list.h> | |
1da177e4 LT |
29 | #include <linux/kmod.h> |
30 | #include <linux/mtd/mtd.h> | |
31 | #include <linux/mtd/partitions.h> | |
5daa7b21 | 32 | #include <linux/err.h> |
1da177e4 | 33 | |
eea72d5f JI |
34 | #include "mtdcore.h" |
35 | ||
1da177e4 LT |
36 | /* Our partition linked list */ |
37 | static LIST_HEAD(mtd_partitions); | |
5daa7b21 | 38 | static DEFINE_MUTEX(mtd_partitions_mutex); |
1da177e4 LT |
39 | |
40 | /* Our partition node structure */ | |
41 | struct mtd_part { | |
42 | struct mtd_info mtd; | |
43 | struct mtd_info *master; | |
69423d99 | 44 | uint64_t offset; |
1da177e4 | 45 | struct list_head list; |
1da177e4 LT |
46 | }; |
47 | ||
48 | /* | |
49 | * Given a pointer to the MTD object in the mtd_part structure, we can retrieve | |
50 | * the pointer to that structure with this macro. | |
51 | */ | |
52 | #define PART(x) ((struct mtd_part *)(x)) | |
53 | ||
97894cda TG |
54 | |
55 | /* | |
1da177e4 LT |
56 | * MTD methods which simply translate the effective address and pass through |
57 | * to the _real_ device. | |
58 | */ | |
59 | ||
b33a2887 AN |
60 | static int part_read(struct mtd_info *mtd, loff_t from, size_t len, |
61 | size_t *retlen, u_char *buf) | |
1da177e4 LT |
62 | { |
63 | struct mtd_part *part = PART(mtd); | |
d8877f19 | 64 | struct mtd_ecc_stats stats; |
f1a28c02 TG |
65 | int res; |
66 | ||
d8877f19 | 67 | stats = part->master->ecc_stats; |
994c8409 MD |
68 | res = part->master->_read(part->master, from + part->offset, len, |
69 | retlen, buf); | |
edbc4540 MD |
70 | if (unlikely(mtd_is_eccerr(res))) |
71 | mtd->ecc_stats.failed += | |
72 | part->master->ecc_stats.failed - stats.failed; | |
73 | else | |
74 | mtd->ecc_stats.corrected += | |
75 | part->master->ecc_stats.corrected - stats.corrected; | |
f1a28c02 | 76 | return res; |
1da177e4 LT |
77 | } |
78 | ||
b33a2887 AN |
79 | static int part_point(struct mtd_info *mtd, loff_t from, size_t len, |
80 | size_t *retlen, void **virt, resource_size_t *phys) | |
1da177e4 LT |
81 | { |
82 | struct mtd_part *part = PART(mtd); | |
5def4898 | 83 | |
994c8409 MD |
84 | return part->master->_point(part->master, from + part->offset, len, |
85 | retlen, virt, phys); | |
1da177e4 | 86 | } |
9223a456 | 87 | |
5e4e6e3f | 88 | static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len) |
1da177e4 LT |
89 | { |
90 | struct mtd_part *part = PART(mtd); | |
91 | ||
994c8409 | 92 | return part->master->_unpoint(part->master, from + part->offset, len); |
1da177e4 LT |
93 | } |
94 | ||
402d3265 DH |
95 | static unsigned long part_get_unmapped_area(struct mtd_info *mtd, |
96 | unsigned long len, | |
97 | unsigned long offset, | |
98 | unsigned long flags) | |
99 | { | |
100 | struct mtd_part *part = PART(mtd); | |
101 | ||
102 | offset += part->offset; | |
994c8409 MD |
103 | return part->master->_get_unmapped_area(part->master, len, offset, |
104 | flags); | |
402d3265 DH |
105 | } |
106 | ||
8593fbc6 | 107 | static int part_read_oob(struct mtd_info *mtd, loff_t from, |
b33a2887 | 108 | struct mtd_oob_ops *ops) |
1da177e4 LT |
109 | { |
110 | struct mtd_part *part = PART(mtd); | |
f1a28c02 | 111 | int res; |
8593fbc6 | 112 | |
1da177e4 | 113 | if (from >= mtd->size) |
8593fbc6 | 114 | return -EINVAL; |
7014568b | 115 | if (ops->datbuf && from + ops->len > mtd->size) |
8593fbc6 | 116 | return -EINVAL; |
f1a28c02 | 117 | |
154bf89f AB |
118 | /* |
119 | * If OOB is also requested, make sure that we do not read past the end | |
120 | * of this partition. | |
121 | */ | |
122 | if (ops->oobbuf) { | |
123 | size_t len, pages; | |
124 | ||
0612b9dd | 125 | if (ops->mode == MTD_OPS_AUTO_OOB) |
154bf89f AB |
126 | len = mtd->oobavail; |
127 | else | |
128 | len = mtd->oobsize; | |
129 | pages = mtd_div_by_ws(mtd->size, mtd); | |
130 | pages -= mtd_div_by_ws(from, mtd); | |
131 | if (ops->ooboffs + ops->ooblen > pages * len) | |
132 | return -EINVAL; | |
133 | } | |
134 | ||
994c8409 | 135 | res = part->master->_read_oob(part->master, from + part->offset, ops); |
f1a28c02 | 136 | if (unlikely(res)) { |
d57f4054 | 137 | if (mtd_is_bitflip(res)) |
f1a28c02 | 138 | mtd->ecc_stats.corrected++; |
d57f4054 | 139 | if (mtd_is_eccerr(res)) |
f1a28c02 TG |
140 | mtd->ecc_stats.failed++; |
141 | } | |
142 | return res; | |
1da177e4 LT |
143 | } |
144 | ||
b33a2887 AN |
145 | static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from, |
146 | size_t len, size_t *retlen, u_char *buf) | |
1da177e4 LT |
147 | { |
148 | struct mtd_part *part = PART(mtd); | |
994c8409 MD |
149 | return part->master->_read_user_prot_reg(part->master, from, len, |
150 | retlen, buf); | |
1da177e4 LT |
151 | } |
152 | ||
b33a2887 AN |
153 | static int part_get_user_prot_info(struct mtd_info *mtd, |
154 | struct otp_info *buf, size_t len) | |
f77814dd NP |
155 | { |
156 | struct mtd_part *part = PART(mtd); | |
994c8409 | 157 | return part->master->_get_user_prot_info(part->master, buf, len); |
f77814dd NP |
158 | } |
159 | ||
b33a2887 AN |
160 | static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, |
161 | size_t len, size_t *retlen, u_char *buf) | |
1da177e4 LT |
162 | { |
163 | struct mtd_part *part = PART(mtd); | |
994c8409 MD |
164 | return part->master->_read_fact_prot_reg(part->master, from, len, |
165 | retlen, buf); | |
1da177e4 LT |
166 | } |
167 | ||
b33a2887 AN |
168 | static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf, |
169 | size_t len) | |
f77814dd NP |
170 | { |
171 | struct mtd_part *part = PART(mtd); | |
994c8409 | 172 | return part->master->_get_fact_prot_info(part->master, buf, len); |
f77814dd NP |
173 | } |
174 | ||
b33a2887 AN |
175 | static int part_write(struct mtd_info *mtd, loff_t to, size_t len, |
176 | size_t *retlen, const u_char *buf) | |
1da177e4 LT |
177 | { |
178 | struct mtd_part *part = PART(mtd); | |
994c8409 MD |
179 | return part->master->_write(part->master, to + part->offset, len, |
180 | retlen, buf); | |
1da177e4 LT |
181 | } |
182 | ||
b33a2887 AN |
183 | static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len, |
184 | size_t *retlen, const u_char *buf) | |
388bbb09 RP |
185 | { |
186 | struct mtd_part *part = PART(mtd); | |
994c8409 MD |
187 | return part->master->_panic_write(part->master, to + part->offset, len, |
188 | retlen, buf); | |
388bbb09 RP |
189 | } |
190 | ||
8593fbc6 | 191 | static int part_write_oob(struct mtd_info *mtd, loff_t to, |
b33a2887 | 192 | struct mtd_oob_ops *ops) |
1da177e4 LT |
193 | { |
194 | struct mtd_part *part = PART(mtd); | |
8593fbc6 | 195 | |
1da177e4 | 196 | if (to >= mtd->size) |
8593fbc6 | 197 | return -EINVAL; |
7014568b | 198 | if (ops->datbuf && to + ops->len > mtd->size) |
8593fbc6 | 199 | return -EINVAL; |
994c8409 | 200 | return part->master->_write_oob(part->master, to + part->offset, ops); |
1da177e4 LT |
201 | } |
202 | ||
b33a2887 AN |
203 | static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from, |
204 | size_t len, size_t *retlen, u_char *buf) | |
1da177e4 LT |
205 | { |
206 | struct mtd_part *part = PART(mtd); | |
994c8409 MD |
207 | return part->master->_write_user_prot_reg(part->master, from, len, |
208 | retlen, buf); | |
1da177e4 LT |
209 | } |
210 | ||
b33a2887 AN |
211 | static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, |
212 | size_t len) | |
f77814dd NP |
213 | { |
214 | struct mtd_part *part = PART(mtd); | |
994c8409 | 215 | return part->master->_lock_user_prot_reg(part->master, from, len); |
f77814dd NP |
216 | } |
217 | ||
b33a2887 AN |
218 | static int part_writev(struct mtd_info *mtd, const struct kvec *vecs, |
219 | unsigned long count, loff_t to, size_t *retlen) | |
1da177e4 LT |
220 | { |
221 | struct mtd_part *part = PART(mtd); | |
994c8409 MD |
222 | return part->master->_writev(part->master, vecs, count, |
223 | to + part->offset, retlen); | |
1da177e4 LT |
224 | } |
225 | ||
b33a2887 | 226 | static int part_erase(struct mtd_info *mtd, struct erase_info *instr) |
1da177e4 LT |
227 | { |
228 | struct mtd_part *part = PART(mtd); | |
229 | int ret; | |
664addc2 | 230 | |
1da177e4 | 231 | instr->addr += part->offset; |
994c8409 | 232 | ret = part->master->_erase(part->master, instr); |
74641d75 | 233 | if (ret) { |
bb0eb217 | 234 | if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) |
74641d75 AH |
235 | instr->fail_addr -= part->offset; |
236 | instr->addr -= part->offset; | |
237 | } | |
1da177e4 LT |
238 | return ret; |
239 | } | |
240 | ||
241 | void mtd_erase_callback(struct erase_info *instr) | |
242 | { | |
3c3c10bb | 243 | if (instr->mtd->_erase == part_erase) { |
1da177e4 LT |
244 | struct mtd_part *part = PART(instr->mtd); |
245 | ||
bb0eb217 | 246 | if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) |
1da177e4 LT |
247 | instr->fail_addr -= part->offset; |
248 | instr->addr -= part->offset; | |
249 | } | |
250 | if (instr->callback) | |
251 | instr->callback(instr); | |
252 | } | |
253 | EXPORT_SYMBOL_GPL(mtd_erase_callback); | |
254 | ||
69423d99 | 255 | static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
1da177e4 LT |
256 | { |
257 | struct mtd_part *part = PART(mtd); | |
994c8409 | 258 | return part->master->_lock(part->master, ofs + part->offset, len); |
1da177e4 LT |
259 | } |
260 | ||
69423d99 | 261 | static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
1da177e4 LT |
262 | { |
263 | struct mtd_part *part = PART(mtd); | |
994c8409 | 264 | return part->master->_unlock(part->master, ofs + part->offset, len); |
1da177e4 LT |
265 | } |
266 | ||
9938424f RC |
267 | static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
268 | { | |
269 | struct mtd_part *part = PART(mtd); | |
994c8409 | 270 | return part->master->_is_locked(part->master, ofs + part->offset, len); |
9938424f RC |
271 | } |
272 | ||
1da177e4 LT |
273 | static void part_sync(struct mtd_info *mtd) |
274 | { | |
275 | struct mtd_part *part = PART(mtd); | |
994c8409 | 276 | part->master->_sync(part->master); |
1da177e4 LT |
277 | } |
278 | ||
279 | static int part_suspend(struct mtd_info *mtd) | |
280 | { | |
281 | struct mtd_part *part = PART(mtd); | |
994c8409 | 282 | return part->master->_suspend(part->master); |
1da177e4 LT |
283 | } |
284 | ||
285 | static void part_resume(struct mtd_info *mtd) | |
286 | { | |
287 | struct mtd_part *part = PART(mtd); | |
994c8409 | 288 | part->master->_resume(part->master); |
1da177e4 LT |
289 | } |
290 | ||
b33a2887 | 291 | static int part_block_isbad(struct mtd_info *mtd, loff_t ofs) |
1da177e4 LT |
292 | { |
293 | struct mtd_part *part = PART(mtd); | |
1da177e4 | 294 | ofs += part->offset; |
994c8409 | 295 | return part->master->_block_isbad(part->master, ofs); |
1da177e4 LT |
296 | } |
297 | ||
b33a2887 | 298 | static int part_block_markbad(struct mtd_info *mtd, loff_t ofs) |
1da177e4 LT |
299 | { |
300 | struct mtd_part *part = PART(mtd); | |
f1a28c02 TG |
301 | int res; |
302 | ||
1da177e4 | 303 | ofs += part->offset; |
994c8409 | 304 | res = part->master->_block_markbad(part->master, ofs); |
f1a28c02 TG |
305 | if (!res) |
306 | mtd->ecc_stats.badblocks++; | |
307 | return res; | |
1da177e4 LT |
308 | } |
309 | ||
5daa7b21 RT |
310 | static inline void free_partition(struct mtd_part *p) |
311 | { | |
312 | kfree(p->mtd.name); | |
313 | kfree(p); | |
314 | } | |
315 | ||
97894cda TG |
316 | /* |
317 | * This function unregisters and destroy all slave MTD objects which are | |
1da177e4 LT |
318 | * attached to the given master MTD object. |
319 | */ | |
320 | ||
321 | int del_mtd_partitions(struct mtd_info *master) | |
322 | { | |
71a928c0 | 323 | struct mtd_part *slave, *next; |
5daa7b21 | 324 | int ret, err = 0; |
1da177e4 | 325 | |
5daa7b21 | 326 | mutex_lock(&mtd_partitions_mutex); |
71a928c0 | 327 | list_for_each_entry_safe(slave, next, &mtd_partitions, list) |
1da177e4 | 328 | if (slave->master == master) { |
5daa7b21 RT |
329 | ret = del_mtd_device(&slave->mtd); |
330 | if (ret < 0) { | |
331 | err = ret; | |
332 | continue; | |
333 | } | |
71a928c0 | 334 | list_del(&slave->list); |
5daa7b21 | 335 | free_partition(slave); |
1da177e4 | 336 | } |
5daa7b21 | 337 | mutex_unlock(&mtd_partitions_mutex); |
1da177e4 | 338 | |
5daa7b21 | 339 | return err; |
1da177e4 LT |
340 | } |
341 | ||
5daa7b21 RT |
342 | static struct mtd_part *allocate_partition(struct mtd_info *master, |
343 | const struct mtd_partition *part, int partno, | |
344 | uint64_t cur_offset) | |
7788ba71 AN |
345 | { |
346 | struct mtd_part *slave; | |
5daa7b21 | 347 | char *name; |
7788ba71 AN |
348 | |
349 | /* allocate the partition structure */ | |
b33a2887 | 350 | slave = kzalloc(sizeof(*slave), GFP_KERNEL); |
5daa7b21 RT |
351 | name = kstrdup(part->name, GFP_KERNEL); |
352 | if (!name || !slave) { | |
b33a2887 | 353 | printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n", |
5daa7b21 RT |
354 | master->name); |
355 | kfree(name); | |
356 | kfree(slave); | |
357 | return ERR_PTR(-ENOMEM); | |
7788ba71 | 358 | } |
7788ba71 AN |
359 | |
360 | /* set up the MTD object for this partition */ | |
361 | slave->mtd.type = master->type; | |
362 | slave->mtd.flags = master->flags & ~part->mask_flags; | |
363 | slave->mtd.size = part->size; | |
364 | slave->mtd.writesize = master->writesize; | |
7fa33ac0 | 365 | slave->mtd.writebufsize = master->writebufsize; |
7788ba71 AN |
366 | slave->mtd.oobsize = master->oobsize; |
367 | slave->mtd.oobavail = master->oobavail; | |
368 | slave->mtd.subpage_sft = master->subpage_sft; | |
369 | ||
5daa7b21 | 370 | slave->mtd.name = name; |
7788ba71 | 371 | slave->mtd.owner = master->owner; |
402d3265 | 372 | slave->mtd.backing_dev_info = master->backing_dev_info; |
7788ba71 | 373 | |
1f24b5a8 DB |
374 | /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone |
375 | * to have the same data be in two different partitions. | |
376 | */ | |
377 | slave->mtd.dev.parent = master->dev.parent; | |
378 | ||
3c3c10bb AB |
379 | slave->mtd._read = part_read; |
380 | slave->mtd._write = part_write; | |
7788ba71 | 381 | |
3c3c10bb AB |
382 | if (master->_panic_write) |
383 | slave->mtd._panic_write = part_panic_write; | |
7788ba71 | 384 | |
3c3c10bb AB |
385 | if (master->_point && master->_unpoint) { |
386 | slave->mtd._point = part_point; | |
387 | slave->mtd._unpoint = part_unpoint; | |
7788ba71 AN |
388 | } |
389 | ||
3c3c10bb AB |
390 | if (master->_get_unmapped_area) |
391 | slave->mtd._get_unmapped_area = part_get_unmapped_area; | |
392 | if (master->_read_oob) | |
393 | slave->mtd._read_oob = part_read_oob; | |
394 | if (master->_write_oob) | |
395 | slave->mtd._write_oob = part_write_oob; | |
396 | if (master->_read_user_prot_reg) | |
397 | slave->mtd._read_user_prot_reg = part_read_user_prot_reg; | |
398 | if (master->_read_fact_prot_reg) | |
399 | slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg; | |
400 | if (master->_write_user_prot_reg) | |
401 | slave->mtd._write_user_prot_reg = part_write_user_prot_reg; | |
402 | if (master->_lock_user_prot_reg) | |
403 | slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg; | |
404 | if (master->_get_user_prot_info) | |
405 | slave->mtd._get_user_prot_info = part_get_user_prot_info; | |
406 | if (master->_get_fact_prot_info) | |
407 | slave->mtd._get_fact_prot_info = part_get_fact_prot_info; | |
408 | if (master->_sync) | |
409 | slave->mtd._sync = part_sync; | |
410 | if (!partno && !master->dev.class && master->_suspend && | |
411 | master->_resume) { | |
412 | slave->mtd._suspend = part_suspend; | |
413 | slave->mtd._resume = part_resume; | |
7788ba71 | 414 | } |
3c3c10bb AB |
415 | if (master->_writev) |
416 | slave->mtd._writev = part_writev; | |
417 | if (master->_lock) | |
418 | slave->mtd._lock = part_lock; | |
419 | if (master->_unlock) | |
420 | slave->mtd._unlock = part_unlock; | |
421 | if (master->_is_locked) | |
422 | slave->mtd._is_locked = part_is_locked; | |
423 | if (master->_block_isbad) | |
424 | slave->mtd._block_isbad = part_block_isbad; | |
425 | if (master->_block_markbad) | |
426 | slave->mtd._block_markbad = part_block_markbad; | |
427 | slave->mtd._erase = part_erase; | |
7788ba71 AN |
428 | slave->master = master; |
429 | slave->offset = part->offset; | |
7788ba71 AN |
430 | |
431 | if (slave->offset == MTDPART_OFS_APPEND) | |
432 | slave->offset = cur_offset; | |
433 | if (slave->offset == MTDPART_OFS_NXTBLK) { | |
434 | slave->offset = cur_offset; | |
69423d99 | 435 | if (mtd_mod_by_eb(cur_offset, master) != 0) { |
7788ba71 | 436 | /* Round up to next erasesize */ |
69423d99 | 437 | slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize; |
7788ba71 | 438 | printk(KERN_NOTICE "Moving partition %d: " |
69423d99 AH |
439 | "0x%012llx -> 0x%012llx\n", partno, |
440 | (unsigned long long)cur_offset, (unsigned long long)slave->offset); | |
7788ba71 AN |
441 | } |
442 | } | |
1a31368b DES |
443 | if (slave->offset == MTDPART_OFS_RETAIN) { |
444 | slave->offset = cur_offset; | |
445 | if (master->size - slave->offset >= slave->mtd.size) { | |
446 | slave->mtd.size = master->size - slave->offset | |
447 | - slave->mtd.size; | |
448 | } else { | |
449 | printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n", | |
450 | part->name, master->size - slave->offset, | |
451 | slave->mtd.size); | |
452 | /* register to preserve ordering */ | |
453 | goto out_register; | |
454 | } | |
455 | } | |
7788ba71 AN |
456 | if (slave->mtd.size == MTDPART_SIZ_FULL) |
457 | slave->mtd.size = master->size - slave->offset; | |
458 | ||
69423d99 AH |
459 | printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset, |
460 | (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name); | |
7788ba71 AN |
461 | |
462 | /* let's do some sanity checks */ | |
463 | if (slave->offset >= master->size) { | |
f636ffb4 | 464 | /* let's register it anyway to preserve ordering */ |
7788ba71 AN |
465 | slave->offset = 0; |
466 | slave->mtd.size = 0; | |
b33a2887 | 467 | printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n", |
7788ba71 | 468 | part->name); |
f636ffb4 | 469 | goto out_register; |
7788ba71 AN |
470 | } |
471 | if (slave->offset + slave->mtd.size > master->size) { | |
472 | slave->mtd.size = master->size - slave->offset; | |
69423d99 AH |
473 | printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n", |
474 | part->name, master->name, (unsigned long long)slave->mtd.size); | |
7788ba71 | 475 | } |
b33a2887 | 476 | if (master->numeraseregions > 1) { |
7788ba71 | 477 | /* Deal with variable erase size stuff */ |
6910c136 | 478 | int i, max = master->numeraseregions; |
69423d99 | 479 | u64 end = slave->offset + slave->mtd.size; |
7788ba71 AN |
480 | struct mtd_erase_region_info *regions = master->eraseregions; |
481 | ||
6910c136 AN |
482 | /* Find the first erase regions which is part of this |
483 | * partition. */ | |
484 | for (i = 0; i < max && regions[i].offset <= slave->offset; i++) | |
7788ba71 | 485 | ; |
6910c136 | 486 | /* The loop searched for the region _behind_ the first one */ |
a57ca046 RK |
487 | if (i > 0) |
488 | i--; | |
7788ba71 | 489 | |
6910c136 AN |
490 | /* Pick biggest erasesize */ |
491 | for (; i < max && regions[i].offset < end; i++) { | |
7788ba71 AN |
492 | if (slave->mtd.erasesize < regions[i].erasesize) { |
493 | slave->mtd.erasesize = regions[i].erasesize; | |
494 | } | |
495 | } | |
6910c136 | 496 | BUG_ON(slave->mtd.erasesize == 0); |
7788ba71 AN |
497 | } else { |
498 | /* Single erase size */ | |
499 | slave->mtd.erasesize = master->erasesize; | |
500 | } | |
501 | ||
502 | if ((slave->mtd.flags & MTD_WRITEABLE) && | |
69423d99 | 503 | mtd_mod_by_eb(slave->offset, &slave->mtd)) { |
7788ba71 | 504 | /* Doesn't start on a boundary of major erase size */ |
b33a2887 AN |
505 | /* FIXME: Let it be writable if it is on a boundary of |
506 | * _minor_ erase size though */ | |
7788ba71 | 507 | slave->mtd.flags &= ~MTD_WRITEABLE; |
b33a2887 | 508 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n", |
7788ba71 AN |
509 | part->name); |
510 | } | |
511 | if ((slave->mtd.flags & MTD_WRITEABLE) && | |
69423d99 | 512 | mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) { |
7788ba71 | 513 | slave->mtd.flags &= ~MTD_WRITEABLE; |
b33a2887 | 514 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n", |
7788ba71 AN |
515 | part->name); |
516 | } | |
517 | ||
518 | slave->mtd.ecclayout = master->ecclayout; | |
6a918bad | 519 | slave->mtd.ecc_strength = master->ecc_strength; |
d062d4ed MD |
520 | slave->mtd.bitflip_threshold = master->bitflip_threshold; |
521 | ||
3c3c10bb | 522 | if (master->_block_isbad) { |
69423d99 | 523 | uint64_t offs = 0; |
7788ba71 | 524 | |
b33a2887 | 525 | while (offs < slave->mtd.size) { |
7086c19d | 526 | if (mtd_block_isbad(master, offs + slave->offset)) |
7788ba71 AN |
527 | slave->mtd.ecc_stats.badblocks++; |
528 | offs += slave->mtd.erasesize; | |
529 | } | |
530 | } | |
531 | ||
f636ffb4 | 532 | out_register: |
7788ba71 AN |
533 | return slave; |
534 | } | |
535 | ||
5daa7b21 RT |
536 | int mtd_add_partition(struct mtd_info *master, char *name, |
537 | long long offset, long long length) | |
538 | { | |
539 | struct mtd_partition part; | |
540 | struct mtd_part *p, *new; | |
541 | uint64_t start, end; | |
542 | int ret = 0; | |
543 | ||
544 | /* the direct offset is expected */ | |
545 | if (offset == MTDPART_OFS_APPEND || | |
546 | offset == MTDPART_OFS_NXTBLK) | |
547 | return -EINVAL; | |
548 | ||
549 | if (length == MTDPART_SIZ_FULL) | |
550 | length = master->size - offset; | |
551 | ||
552 | if (length <= 0) | |
553 | return -EINVAL; | |
554 | ||
555 | part.name = name; | |
556 | part.size = length; | |
557 | part.offset = offset; | |
558 | part.mask_flags = 0; | |
559 | part.ecclayout = NULL; | |
560 | ||
561 | new = allocate_partition(master, &part, -1, offset); | |
562 | if (IS_ERR(new)) | |
563 | return PTR_ERR(new); | |
564 | ||
565 | start = offset; | |
566 | end = offset + length; | |
567 | ||
568 | mutex_lock(&mtd_partitions_mutex); | |
569 | list_for_each_entry(p, &mtd_partitions, list) | |
570 | if (p->master == master) { | |
571 | if ((start >= p->offset) && | |
572 | (start < (p->offset + p->mtd.size))) | |
573 | goto err_inv; | |
574 | ||
575 | if ((end >= p->offset) && | |
576 | (end < (p->offset + p->mtd.size))) | |
577 | goto err_inv; | |
578 | } | |
579 | ||
580 | list_add(&new->list, &mtd_partitions); | |
581 | mutex_unlock(&mtd_partitions_mutex); | |
582 | ||
583 | add_mtd_device(&new->mtd); | |
584 | ||
585 | return ret; | |
586 | err_inv: | |
587 | mutex_unlock(&mtd_partitions_mutex); | |
588 | free_partition(new); | |
589 | return -EINVAL; | |
590 | } | |
591 | EXPORT_SYMBOL_GPL(mtd_add_partition); | |
592 | ||
593 | int mtd_del_partition(struct mtd_info *master, int partno) | |
594 | { | |
595 | struct mtd_part *slave, *next; | |
596 | int ret = -EINVAL; | |
597 | ||
598 | mutex_lock(&mtd_partitions_mutex); | |
599 | list_for_each_entry_safe(slave, next, &mtd_partitions, list) | |
600 | if ((slave->master == master) && | |
601 | (slave->mtd.index == partno)) { | |
602 | ret = del_mtd_device(&slave->mtd); | |
603 | if (ret < 0) | |
604 | break; | |
605 | ||
606 | list_del(&slave->list); | |
607 | free_partition(slave); | |
608 | break; | |
609 | } | |
610 | mutex_unlock(&mtd_partitions_mutex); | |
611 | ||
612 | return ret; | |
613 | } | |
614 | EXPORT_SYMBOL_GPL(mtd_del_partition); | |
615 | ||
1da177e4 LT |
616 | /* |
617 | * This function, given a master MTD object and a partition table, creates | |
618 | * and registers slave MTD objects which are bound to the master according to | |
619 | * the partition definitions. | |
1f24b5a8 DB |
620 | * |
621 | * We don't register the master, or expect the caller to have done so, | |
622 | * for reasons of data integrity. | |
1da177e4 LT |
623 | */ |
624 | ||
97894cda | 625 | int add_mtd_partitions(struct mtd_info *master, |
1da177e4 LT |
626 | const struct mtd_partition *parts, |
627 | int nbparts) | |
628 | { | |
629 | struct mtd_part *slave; | |
69423d99 | 630 | uint64_t cur_offset = 0; |
1da177e4 LT |
631 | int i; |
632 | ||
b33a2887 | 633 | printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name); |
1da177e4 LT |
634 | |
635 | for (i = 0; i < nbparts; i++) { | |
5daa7b21 | 636 | slave = allocate_partition(master, parts + i, i, cur_offset); |
1fe6519d BB |
637 | if (IS_ERR(slave)) { |
638 | del_mtd_partitions(master); | |
5daa7b21 | 639 | return PTR_ERR(slave); |
1fe6519d | 640 | } |
5daa7b21 RT |
641 | |
642 | mutex_lock(&mtd_partitions_mutex); | |
643 | list_add(&slave->list, &mtd_partitions); | |
644 | mutex_unlock(&mtd_partitions_mutex); | |
645 | ||
646 | add_mtd_device(&slave->mtd); | |
647 | ||
1da177e4 | 648 | cur_offset = slave->offset + slave->mtd.size; |
1da177e4 LT |
649 | } |
650 | ||
651 | return 0; | |
652 | } | |
1da177e4 LT |
653 | |
654 | static DEFINE_SPINLOCK(part_parser_lock); | |
655 | static LIST_HEAD(part_parsers); | |
656 | ||
657 | static struct mtd_part_parser *get_partition_parser(const char *name) | |
658 | { | |
71a928c0 | 659 | struct mtd_part_parser *p, *ret = NULL; |
1da177e4 | 660 | |
71a928c0 | 661 | spin_lock(&part_parser_lock); |
1da177e4 | 662 | |
71a928c0 | 663 | list_for_each_entry(p, &part_parsers, list) |
1da177e4 LT |
664 | if (!strcmp(p->name, name) && try_module_get(p->owner)) { |
665 | ret = p; | |
666 | break; | |
667 | } | |
71a928c0 | 668 | |
1da177e4 LT |
669 | spin_unlock(&part_parser_lock); |
670 | ||
671 | return ret; | |
672 | } | |
673 | ||
953b3bd1 DES |
674 | #define put_partition_parser(p) do { module_put((p)->owner); } while (0) |
675 | ||
1da177e4 LT |
676 | int register_mtd_parser(struct mtd_part_parser *p) |
677 | { | |
678 | spin_lock(&part_parser_lock); | |
679 | list_add(&p->list, &part_parsers); | |
680 | spin_unlock(&part_parser_lock); | |
681 | ||
682 | return 0; | |
683 | } | |
b33a2887 | 684 | EXPORT_SYMBOL_GPL(register_mtd_parser); |
1da177e4 LT |
685 | |
686 | int deregister_mtd_parser(struct mtd_part_parser *p) | |
687 | { | |
688 | spin_lock(&part_parser_lock); | |
689 | list_del(&p->list); | |
690 | spin_unlock(&part_parser_lock); | |
691 | return 0; | |
692 | } | |
b33a2887 | 693 | EXPORT_SYMBOL_GPL(deregister_mtd_parser); |
1da177e4 | 694 | |
ad274cec AB |
695 | /* |
696 | * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you | |
697 | * are changing this array! | |
698 | */ | |
ccef4dcc | 699 | static const char * const default_mtd_part_types[] = { |
d26c87d6 DES |
700 | "cmdlinepart", |
701 | "ofpart", | |
702 | NULL | |
703 | }; | |
5c4eefbd | 704 | |
ad274cec AB |
705 | /** |
706 | * parse_mtd_partitions - parse MTD partitions | |
707 | * @master: the master partition (describes whole MTD device) | |
708 | * @types: names of partition parsers to try or %NULL | |
709 | * @pparts: array of partitions found is returned here | |
c7975330 | 710 | * @data: MTD partition parser-specific data |
ad274cec AB |
711 | * |
712 | * This function tries to find partition on MTD device @master. It uses MTD | |
713 | * partition parsers, specified in @types. However, if @types is %NULL, then | |
714 | * the default list of parsers is used. The default list contains only the | |
d26c87d6 | 715 | * "cmdlinepart" and "ofpart" parsers ATM. |
c51803dd HS |
716 | * Note: If there are more then one parser in @types, the kernel only takes the |
717 | * partitions parsed out by the first parser. | |
ad274cec AB |
718 | * |
719 | * This function may return: | |
720 | * o a negative error code in case of failure | |
721 | * o zero if no partitions were found | |
722 | * o a positive number of found partitions, in which case on exit @pparts will | |
723 | * point to an array containing this number of &struct mtd_info objects. | |
724 | */ | |
26a47346 | 725 | int parse_mtd_partitions(struct mtd_info *master, const char *const *types, |
c7975330 DES |
726 | struct mtd_partition **pparts, |
727 | struct mtd_part_parser_data *data) | |
1da177e4 LT |
728 | { |
729 | struct mtd_part_parser *parser; | |
730 | int ret = 0; | |
97894cda | 731 | |
5c4eefbd DES |
732 | if (!types) |
733 | types = default_mtd_part_types; | |
734 | ||
1da177e4 LT |
735 | for ( ; ret <= 0 && *types; types++) { |
736 | parser = get_partition_parser(*types); | |
1da177e4 | 737 | if (!parser && !request_module("%s", *types)) |
58edc904 | 738 | parser = get_partition_parser(*types); |
7c802fbd | 739 | if (!parser) |
1da177e4 | 740 | continue; |
c7975330 | 741 | ret = (*parser->parse_fn)(master, pparts, data); |
c51803dd | 742 | put_partition_parser(parser); |
1da177e4 | 743 | if (ret > 0) { |
97894cda | 744 | printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n", |
1da177e4 | 745 | ret, parser->name, master->name); |
c51803dd | 746 | break; |
1da177e4 | 747 | } |
1da177e4 LT |
748 | } |
749 | return ret; | |
750 | } | |
5daa7b21 | 751 | |
5dee4674 | 752 | int mtd_is_partition(const struct mtd_info *mtd) |
5daa7b21 RT |
753 | { |
754 | struct mtd_part *part; | |
a7e93dcd | 755 | int ispart = 0; |
5daa7b21 RT |
756 | |
757 | mutex_lock(&mtd_partitions_mutex); | |
758 | list_for_each_entry(part, &mtd_partitions, list) | |
759 | if (&part->mtd == mtd) { | |
a7e93dcd | 760 | ispart = 1; |
5daa7b21 RT |
761 | break; |
762 | } | |
763 | mutex_unlock(&mtd_partitions_mutex); | |
764 | ||
a7e93dcd | 765 | return ispart; |
5daa7b21 | 766 | } |
a7e93dcd | 767 | EXPORT_SYMBOL_GPL(mtd_is_partition); |
62082e56 RG |
768 | |
769 | /* Returns the size of the entire flash chip */ | |
770 | uint64_t mtd_get_device_size(const struct mtd_info *mtd) | |
771 | { | |
772 | if (!mtd_is_partition(mtd)) | |
773 | return mtd->size; | |
774 | ||
775 | return PART(mtd)->master->size; | |
776 | } | |
777 | EXPORT_SYMBOL_GPL(mtd_get_device_size); |