blob: 07c701169344266e907ce92ee2068c2a8ad6881d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Simple MTD partitioning layer
3 *
4 * (C) 2000 Nicolas Pitre <nico@cam.org>
5 *
6 * This code is GPL
7 *
Thomas Gleixner97894cd2005-11-07 11:15:26 +00008 * $Id: mtdpart.c,v 1.55 2005/11/07 11:14:20 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * 02-21-2002 Thomas Gleixner <gleixner@autronix.de>
11 * added support for read_oob, write_oob
Thomas Gleixner97894cd2005-11-07 11:15:26 +000012 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include <linux/module.h>
15#include <linux/types.h>
16#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kmod.h>
20#include <linux/mtd/mtd.h>
21#include <linux/mtd/partitions.h>
22#include <linux/mtd/compatmac.h>
23
24/* Our partition linked list */
25static LIST_HEAD(mtd_partitions);
26
27/* Our partition node structure */
28struct mtd_part {
29 struct mtd_info mtd;
30 struct mtd_info *master;
31 u_int32_t offset;
32 int index;
33 struct list_head list;
34 int registered;
35};
36
37/*
38 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
39 * the pointer to that structure with this macro.
40 */
41#define PART(x) ((struct mtd_part *)(x))
42
Thomas Gleixner97894cd2005-11-07 11:15:26 +000043
44/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * MTD methods which simply translate the effective address and pass through
46 * to the _real_ device.
47 */
48
Thomas Gleixner97894cd2005-11-07 11:15:26 +000049static int part_read (struct mtd_info *mtd, loff_t from, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 size_t *retlen, u_char *buf)
51{
52 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020053 int res;
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (from >= mtd->size)
56 len = 0;
57 else if (from + len > mtd->size)
58 len = mtd->size - from;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020059 res = part->master->read (part->master, from + part->offset,
Thomas Gleixner9223a452006-05-23 17:21:03 +020060 len, retlen, buf);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020061 if (unlikely(res)) {
62 if (res == -EUCLEAN)
63 mtd->ecc_stats.corrected++;
64 if (res == -EBADMSG)
65 mtd->ecc_stats.failed++;
66 }
67 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Thomas Gleixner97894cd2005-11-07 11:15:26 +000070static int part_point (struct mtd_info *mtd, loff_t from, size_t len,
Jared Hulberta98889f2008-04-29 23:26:49 -070071 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 struct mtd_part *part = PART(mtd);
74 if (from >= mtd->size)
75 len = 0;
76 else if (from + len > mtd->size)
77 len = mtd->size - from;
Thomas Gleixner97894cd2005-11-07 11:15:26 +000078 return part->master->point (part->master, from + part->offset,
Jared Hulberta98889f2008-04-29 23:26:49 -070079 len, retlen, virt, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
Thomas Gleixner9223a452006-05-23 17:21:03 +020081
Jared Hulberta98889f2008-04-29 23:26:49 -070082static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 struct mtd_part *part = PART(mtd);
85
Jared Hulberta98889f2008-04-29 23:26:49 -070086 part->master->unpoint(part->master, from + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020089static int part_read_oob(struct mtd_info *mtd, loff_t from,
90 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020093 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (from >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020096 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +030097 if (ops->datbuf && from + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020098 return -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020099 res = part->master->read_oob(part->master, from + part->offset, ops);
100
101 if (unlikely(res)) {
102 if (res == -EUCLEAN)
103 mtd->ecc_stats.corrected++;
104 if (res == -EBADMSG)
105 mtd->ecc_stats.failed++;
106 }
107 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000110static int part_read_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 size_t *retlen, u_char *buf)
112{
113 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000114 return part->master->read_user_prot_reg (part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 len, retlen, buf);
116}
117
Nicolas Pitref77814d2005-02-08 17:11:19 +0000118static int part_get_user_prot_info (struct mtd_info *mtd,
119 struct otp_info *buf, size_t len)
120{
121 struct mtd_part *part = PART(mtd);
122 return part->master->get_user_prot_info (part->master, buf, len);
123}
124
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000125static int part_read_fact_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 size_t *retlen, u_char *buf)
127{
128 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000129 return part->master->read_fact_prot_reg (part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 len, retlen, buf);
131}
132
Nicolas Pitref77814d2005-02-08 17:11:19 +0000133static int part_get_fact_prot_info (struct mtd_info *mtd,
134 struct otp_info *buf, size_t len)
135{
136 struct mtd_part *part = PART(mtd);
137 return part->master->get_fact_prot_info (part->master, buf, len);
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static int part_write (struct mtd_info *mtd, loff_t to, size_t len,
141 size_t *retlen, const u_char *buf)
142{
143 struct mtd_part *part = PART(mtd);
144 if (!(mtd->flags & MTD_WRITEABLE))
145 return -EROFS;
146 if (to >= mtd->size)
147 len = 0;
148 else if (to + len > mtd->size)
149 len = mtd->size - to;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200150 return part->master->write (part->master, to + part->offset,
151 len, retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Richard Purdie388bbb02008-02-06 10:17:15 +0000154static int part_panic_write (struct mtd_info *mtd, loff_t to, size_t len,
155 size_t *retlen, const u_char *buf)
156{
157 struct mtd_part *part = PART(mtd);
158 if (!(mtd->flags & MTD_WRITEABLE))
159 return -EROFS;
160 if (to >= mtd->size)
161 len = 0;
162 else if (to + len > mtd->size)
163 len = mtd->size - to;
164 return part->master->panic_write (part->master, to + part->offset,
165 len, retlen, buf);
166}
167
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200168static int part_write_oob(struct mtd_info *mtd, loff_t to,
169 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 struct mtd_part *part = PART(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (!(mtd->flags & MTD_WRITEABLE))
174 return -EROFS;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (to >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200177 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300178 if (ops->datbuf && to + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200179 return -EINVAL;
180 return part->master->write_oob(part->master, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000183static int part_write_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 size_t *retlen, u_char *buf)
185{
186 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000187 return part->master->write_user_prot_reg (part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 len, retlen, buf);
189}
190
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000191static int part_lock_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000192{
193 struct mtd_part *part = PART(mtd);
194 return part->master->lock_user_prot_reg (part->master, from, len);
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static int part_writev (struct mtd_info *mtd, const struct kvec *vecs,
198 unsigned long count, loff_t to, size_t *retlen)
199{
200 struct mtd_part *part = PART(mtd);
201 if (!(mtd->flags & MTD_WRITEABLE))
202 return -EROFS;
Thomas Gleixner9d8522d2006-05-23 16:06:03 +0200203 return part->master->writev (part->master, vecs, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static int part_erase (struct mtd_info *mtd, struct erase_info *instr)
208{
209 struct mtd_part *part = PART(mtd);
210 int ret;
211 if (!(mtd->flags & MTD_WRITEABLE))
212 return -EROFS;
213 if (instr->addr >= mtd->size)
214 return -EINVAL;
215 instr->addr += part->offset;
216 ret = part->master->erase(part->master, instr);
Adrian Hunter74641d72007-03-08 12:20:12 +0200217 if (ret) {
218 if (instr->fail_addr != 0xffffffff)
219 instr->fail_addr -= part->offset;
220 instr->addr -= part->offset;
221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return ret;
223}
224
225void mtd_erase_callback(struct erase_info *instr)
226{
227 if (instr->mtd->erase == part_erase) {
228 struct mtd_part *part = PART(instr->mtd);
229
230 if (instr->fail_addr != 0xffffffff)
231 instr->fail_addr -= part->offset;
232 instr->addr -= part->offset;
233 }
234 if (instr->callback)
235 instr->callback(instr);
236}
237EXPORT_SYMBOL_GPL(mtd_erase_callback);
238
239static int part_lock (struct mtd_info *mtd, loff_t ofs, size_t len)
240{
241 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000242 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return -EINVAL;
244 return part->master->lock(part->master, ofs + part->offset, len);
245}
246
247static int part_unlock (struct mtd_info *mtd, loff_t ofs, size_t len)
248{
249 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000250 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return -EINVAL;
252 return part->master->unlock(part->master, ofs + part->offset, len);
253}
254
255static void part_sync(struct mtd_info *mtd)
256{
257 struct mtd_part *part = PART(mtd);
258 part->master->sync(part->master);
259}
260
261static int part_suspend(struct mtd_info *mtd)
262{
263 struct mtd_part *part = PART(mtd);
264 return part->master->suspend(part->master);
265}
266
267static void part_resume(struct mtd_info *mtd)
268{
269 struct mtd_part *part = PART(mtd);
270 part->master->resume(part->master);
271}
272
273static int part_block_isbad (struct mtd_info *mtd, loff_t ofs)
274{
275 struct mtd_part *part = PART(mtd);
276 if (ofs >= mtd->size)
277 return -EINVAL;
278 ofs += part->offset;
279 return part->master->block_isbad(part->master, ofs);
280}
281
282static int part_block_markbad (struct mtd_info *mtd, loff_t ofs)
283{
284 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200285 int res;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!(mtd->flags & MTD_WRITEABLE))
288 return -EROFS;
289 if (ofs >= mtd->size)
290 return -EINVAL;
291 ofs += part->offset;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200292 res = part->master->block_markbad(part->master, ofs);
293 if (!res)
294 mtd->ecc_stats.badblocks++;
295 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000298/*
299 * This function unregisters and destroy all slave MTD objects which are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * attached to the given master MTD object.
301 */
302
303int del_mtd_partitions(struct mtd_info *master)
304{
305 struct list_head *node;
306 struct mtd_part *slave;
307
308 for (node = mtd_partitions.next;
309 node != &mtd_partitions;
310 node = node->next) {
311 slave = list_entry(node, struct mtd_part, list);
312 if (slave->master == master) {
313 struct list_head *prev = node->prev;
314 __list_del(prev, node->next);
315 if(slave->registered)
316 del_mtd_device(&slave->mtd);
317 kfree(slave);
318 node = prev;
319 }
320 }
321
322 return 0;
323}
324
325/*
326 * This function, given a master MTD object and a partition table, creates
327 * and registers slave MTD objects which are bound to the master according to
328 * the partition definitions.
329 * (Q: should we register the master MTD object as well?)
330 */
331
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000332int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 const struct mtd_partition *parts,
334 int nbparts)
335{
336 struct mtd_part *slave;
337 u_int32_t cur_offset = 0;
338 int i;
339
340 printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
341
342 for (i = 0; i < nbparts; i++) {
343
344 /* allocate the partition structure */
Burman Yan95b93a02006-11-15 21:10:29 +0200345 slave = kzalloc (sizeof(*slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (!slave) {
347 printk ("memory allocation error while creating partitions for \"%s\"\n",
348 master->name);
349 del_mtd_partitions(master);
350 return -ENOMEM;
351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 list_add(&slave->list, &mtd_partitions);
353
354 /* set up the MTD object for this partition */
355 slave->mtd.type = master->type;
356 slave->mtd.flags = master->flags & ~parts[i].mask_flags;
357 slave->mtd.size = parts[i].size;
Joern Engel28318772006-05-22 23:18:05 +0200358 slave->mtd.writesize = master->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 slave->mtd.oobsize = master->oobsize;
Vitaly Wool1f922672007-03-06 16:56:34 +0300360 slave->mtd.oobavail = master->oobavail;
Thomas Gleixner29072b92006-09-28 15:38:36 +0200361 slave->mtd.subpage_sft = master->subpage_sft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 slave->mtd.name = parts[i].name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 slave->mtd.owner = master->owner;
365
366 slave->mtd.read = part_read;
367 slave->mtd.write = part_write;
368
Richard Purdie388bbb02008-02-06 10:17:15 +0000369 if (master->panic_write)
370 slave->mtd.panic_write = part_panic_write;
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if(master->point && master->unpoint){
373 slave->mtd.point = part_point;
374 slave->mtd.unpoint = part_unpoint;
375 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (master->read_oob)
378 slave->mtd.read_oob = part_read_oob;
379 if (master->write_oob)
380 slave->mtd.write_oob = part_write_oob;
381 if(master->read_user_prot_reg)
382 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
383 if(master->read_fact_prot_reg)
384 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
385 if(master->write_user_prot_reg)
386 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
Nicolas Pitref77814d2005-02-08 17:11:19 +0000387 if(master->lock_user_prot_reg)
388 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
389 if(master->get_user_prot_info)
390 slave->mtd.get_user_prot_info = part_get_user_prot_info;
391 if(master->get_fact_prot_info)
392 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (master->sync)
394 slave->mtd.sync = part_sync;
395 if (!i && master->suspend && master->resume) {
396 slave->mtd.suspend = part_suspend;
397 slave->mtd.resume = part_resume;
398 }
399 if (master->writev)
400 slave->mtd.writev = part_writev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (master->lock)
402 slave->mtd.lock = part_lock;
403 if (master->unlock)
404 slave->mtd.unlock = part_unlock;
405 if (master->block_isbad)
406 slave->mtd.block_isbad = part_block_isbad;
407 if (master->block_markbad)
408 slave->mtd.block_markbad = part_block_markbad;
409 slave->mtd.erase = part_erase;
410 slave->master = master;
411 slave->offset = parts[i].offset;
412 slave->index = i;
413
414 if (slave->offset == MTDPART_OFS_APPEND)
415 slave->offset = cur_offset;
416 if (slave->offset == MTDPART_OFS_NXTBLK) {
Artem B. Bityutskiy5cea5da2005-09-30 15:49:12 +0100417 slave->offset = cur_offset;
418 if ((cur_offset % master->erasesize) != 0) {
419 /* Round up to next erasesize */
420 slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 printk(KERN_NOTICE "Moving partition %d: "
422 "0x%08x -> 0x%08x\n", i,
423 cur_offset, slave->offset);
424 }
425 }
426 if (slave->mtd.size == MTDPART_SIZ_FULL)
427 slave->mtd.size = master->size - slave->offset;
428 cur_offset = slave->offset + slave->mtd.size;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000429
430 printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 slave->offset + slave->mtd.size, slave->mtd.name);
432
433 /* let's do some sanity checks */
434 if (slave->offset >= master->size) {
435 /* let's register it anyway to preserve ordering */
436 slave->offset = 0;
437 slave->mtd.size = 0;
438 printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
439 parts[i].name);
440 }
441 if (slave->offset + slave->mtd.size > master->size) {
442 slave->mtd.size = master->size - slave->offset;
443 printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
444 parts[i].name, master->name, slave->mtd.size);
445 }
446 if (master->numeraseregions>1) {
447 /* Deal with variable erase size stuff */
448 int i;
449 struct mtd_erase_region_info *regions = master->eraseregions;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* Find the first erase regions which is part of this partition. */
452 for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
453 ;
454
455 for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
456 if (slave->mtd.erasesize < regions[i].erasesize) {
457 slave->mtd.erasesize = regions[i].erasesize;
458 }
459 }
460 } else {
461 /* Single erase size */
462 slave->mtd.erasesize = master->erasesize;
463 }
464
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000465 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 (slave->offset % slave->mtd.erasesize)) {
467 /* Doesn't start on a boundary of major erase size */
468 /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
469 slave->mtd.flags &= ~MTD_WRITEABLE;
470 printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
471 parts[i].name);
472 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000473 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 (slave->mtd.size % slave->mtd.erasesize)) {
475 slave->mtd.flags &= ~MTD_WRITEABLE;
476 printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
477 parts[i].name);
478 }
479
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200480 slave->mtd.ecclayout = master->ecclayout;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200481 if (master->block_isbad) {
482 uint32_t offs = 0;
483
484 while(offs < slave->mtd.size) {
485 if (master->block_isbad(master,
486 offs + slave->offset))
487 slave->mtd.ecc_stats.badblocks++;
488 offs += slave->mtd.erasesize;
489 }
490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if(parts[i].mtdp)
493 { /* store the object pointer (caller may or may not register it */
494 *parts[i].mtdp = &slave->mtd;
495 slave->registered = 0;
496 }
497 else
498 {
499 /* register our partition */
500 add_mtd_device(&slave->mtd);
501 slave->registered = 1;
502 }
503 }
504
505 return 0;
506}
507
508EXPORT_SYMBOL(add_mtd_partitions);
509EXPORT_SYMBOL(del_mtd_partitions);
510
511static DEFINE_SPINLOCK(part_parser_lock);
512static LIST_HEAD(part_parsers);
513
514static struct mtd_part_parser *get_partition_parser(const char *name)
515{
516 struct list_head *this;
517 void *ret = NULL;
518 spin_lock(&part_parser_lock);
519
520 list_for_each(this, &part_parsers) {
521 struct mtd_part_parser *p = list_entry(this, struct mtd_part_parser, list);
522
523 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
524 ret = p;
525 break;
526 }
527 }
528 spin_unlock(&part_parser_lock);
529
530 return ret;
531}
532
533int register_mtd_parser(struct mtd_part_parser *p)
534{
535 spin_lock(&part_parser_lock);
536 list_add(&p->list, &part_parsers);
537 spin_unlock(&part_parser_lock);
538
539 return 0;
540}
541
542int deregister_mtd_parser(struct mtd_part_parser *p)
543{
544 spin_lock(&part_parser_lock);
545 list_del(&p->list);
546 spin_unlock(&part_parser_lock);
547 return 0;
548}
549
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000550int parse_mtd_partitions(struct mtd_info *master, const char **types,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct mtd_partition **pparts, unsigned long origin)
552{
553 struct mtd_part_parser *parser;
554 int ret = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 for ( ; ret <= 0 && *types; types++) {
557 parser = get_partition_parser(*types);
558#ifdef CONFIG_KMOD
559 if (!parser && !request_module("%s", *types))
560 parser = get_partition_parser(*types);
561#endif
562 if (!parser) {
563 printk(KERN_NOTICE "%s partition parsing not available\n",
564 *types);
565 continue;
566 }
567 ret = (*parser->parse_fn)(master, pparts, origin);
568 if (ret > 0) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000569 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 ret, parser->name, master->name);
571 }
572 put_partition_parser(parser);
573 }
574 return ret;
575}
576
577EXPORT_SYMBOL_GPL(parse_mtd_partitions);
578EXPORT_SYMBOL_GPL(register_mtd_parser);
579EXPORT_SYMBOL_GPL(deregister_mtd_parser);