blob: 9cf73d360e78b231eed1104dd70adefadb6f302e [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 02-21-2002 Thomas Gleixner <gleixner@autronix.de>
9 * added support for read_oob, write_oob
Thomas Gleixner97894cd2005-11-07 11:15:26 +000010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include <linux/module.h>
13#include <linux/types.h>
14#include <linux/kernel.h>
15#include <linux/slab.h>
16#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kmod.h>
18#include <linux/mtd/mtd.h>
19#include <linux/mtd/partitions.h>
20#include <linux/mtd/compatmac.h>
21
22/* Our partition linked list */
23static LIST_HEAD(mtd_partitions);
24
25/* Our partition node structure */
26struct mtd_part {
27 struct mtd_info mtd;
28 struct mtd_info *master;
29 u_int32_t offset;
30 int index;
31 struct list_head list;
32 int registered;
33};
34
35/*
36 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
37 * the pointer to that structure with this macro.
38 */
39#define PART(x) ((struct mtd_part *)(x))
40
Thomas Gleixner97894cd2005-11-07 11:15:26 +000041
42/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * MTD methods which simply translate the effective address and pass through
44 * to the _real_ device.
45 */
46
Atsushi Nemotob33a2882008-07-19 01:00:33 +090047static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
48 size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020051 int res;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (from >= mtd->size)
54 len = 0;
55 else if (from + len > mtd->size)
56 len = mtd->size - from;
Atsushi Nemotob33a2882008-07-19 01:00:33 +090057 res = part->master->read(part->master, from + part->offset,
Thomas Gleixner9223a452006-05-23 17:21:03 +020058 len, retlen, buf);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020059 if (unlikely(res)) {
60 if (res == -EUCLEAN)
61 mtd->ecc_stats.corrected++;
62 if (res == -EBADMSG)
63 mtd->ecc_stats.failed++;
64 }
65 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
Atsushi Nemotob33a2882008-07-19 01:00:33 +090068static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
69 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 struct mtd_part *part = PART(mtd);
72 if (from >= mtd->size)
73 len = 0;
74 else if (from + len > mtd->size)
75 len = mtd->size - from;
Thomas Gleixner97894cd2005-11-07 11:15:26 +000076 return part->master->point (part->master, from + part->offset,
Jared Hulberta98889f2008-04-29 23:26:49 -070077 len, retlen, virt, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
Thomas Gleixner9223a452006-05-23 17:21:03 +020079
Jared Hulberta98889f2008-04-29 23:26:49 -070080static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 struct mtd_part *part = PART(mtd);
83
Jared Hulberta98889f2008-04-29 23:26:49 -070084 part->master->unpoint(part->master, from + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020087static int part_read_oob(struct mtd_info *mtd, loff_t from,
Atsushi Nemotob33a2882008-07-19 01:00:33 +090088 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020091 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (from >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020094 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +030095 if (ops->datbuf && from + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020096 return -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020097 res = part->master->read_oob(part->master, from + part->offset, ops);
98
99 if (unlikely(res)) {
100 if (res == -EUCLEAN)
101 mtd->ecc_stats.corrected++;
102 if (res == -EBADMSG)
103 mtd->ecc_stats.failed++;
104 }
105 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900108static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
109 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900112 return part->master->read_user_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 len, retlen, buf);
114}
115
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900116static int part_get_user_prot_info(struct mtd_info *mtd,
117 struct otp_info *buf, size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000118{
119 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900120 return part->master->get_user_prot_info(part->master, buf, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000121}
122
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900123static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
124 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900127 return part->master->read_fact_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 len, retlen, buf);
129}
130
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900131static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
132 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000133{
134 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900135 return part->master->get_fact_prot_info(part->master, buf, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000136}
137
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900138static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
139 size_t *retlen, const u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 struct mtd_part *part = PART(mtd);
142 if (!(mtd->flags & MTD_WRITEABLE))
143 return -EROFS;
144 if (to >= mtd->size)
145 len = 0;
146 else if (to + len > mtd->size)
147 len = mtd->size - to;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900148 return part->master->write(part->master, to + part->offset,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200149 len, retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900152static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
153 size_t *retlen, const u_char *buf)
Richard Purdie388bbb02008-02-06 10:17:15 +0000154{
155 struct mtd_part *part = PART(mtd);
156 if (!(mtd->flags & MTD_WRITEABLE))
157 return -EROFS;
158 if (to >= mtd->size)
159 len = 0;
160 else if (to + len > mtd->size)
161 len = mtd->size - to;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900162 return part->master->panic_write(part->master, to + part->offset,
Richard Purdie388bbb02008-02-06 10:17:15 +0000163 len, retlen, buf);
164}
165
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200166static int part_write_oob(struct mtd_info *mtd, loff_t to,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900167 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 struct mtd_part *part = PART(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (!(mtd->flags & MTD_WRITEABLE))
172 return -EROFS;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (to >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200175 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300176 if (ops->datbuf && to + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200177 return -EINVAL;
178 return part->master->write_oob(part->master, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900181static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
182 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900185 return part->master->write_user_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 len, retlen, buf);
187}
188
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900189static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
190 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000191{
192 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900193 return part->master->lock_user_prot_reg(part->master, from, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000194}
195
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900196static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
197 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 struct mtd_part *part = PART(mtd);
200 if (!(mtd->flags & MTD_WRITEABLE))
201 return -EROFS;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900202 return part->master->writev(part->master, vecs, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900206static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 struct mtd_part *part = PART(mtd);
209 int ret;
210 if (!(mtd->flags & MTD_WRITEABLE))
211 return -EROFS;
212 if (instr->addr >= mtd->size)
213 return -EINVAL;
214 instr->addr += part->offset;
215 ret = part->master->erase(part->master, instr);
Adrian Hunter74641d72007-03-08 12:20:12 +0200216 if (ret) {
217 if (instr->fail_addr != 0xffffffff)
218 instr->fail_addr -= part->offset;
219 instr->addr -= part->offset;
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return ret;
222}
223
224void mtd_erase_callback(struct erase_info *instr)
225{
226 if (instr->mtd->erase == part_erase) {
227 struct mtd_part *part = PART(instr->mtd);
228
229 if (instr->fail_addr != 0xffffffff)
230 instr->fail_addr -= part->offset;
231 instr->addr -= part->offset;
232 }
233 if (instr->callback)
234 instr->callback(instr);
235}
236EXPORT_SYMBOL_GPL(mtd_erase_callback);
237
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900238static int part_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000241 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return -EINVAL;
243 return part->master->lock(part->master, ofs + part->offset, len);
244}
245
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900246static int part_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000249 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return -EINVAL;
251 return part->master->unlock(part->master, ofs + part->offset, len);
252}
253
254static void part_sync(struct mtd_info *mtd)
255{
256 struct mtd_part *part = PART(mtd);
257 part->master->sync(part->master);
258}
259
260static int part_suspend(struct mtd_info *mtd)
261{
262 struct mtd_part *part = PART(mtd);
263 return part->master->suspend(part->master);
264}
265
266static void part_resume(struct mtd_info *mtd)
267{
268 struct mtd_part *part = PART(mtd);
269 part->master->resume(part->master);
270}
271
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900272static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct mtd_part *part = PART(mtd);
275 if (ofs >= mtd->size)
276 return -EINVAL;
277 ofs += part->offset;
278 return part->master->block_isbad(part->master, ofs);
279}
280
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900281static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200284 int res;
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (!(mtd->flags & MTD_WRITEABLE))
287 return -EROFS;
288 if (ofs >= mtd->size)
289 return -EINVAL;
290 ofs += part->offset;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200291 res = part->master->block_markbad(part->master, ofs);
292 if (!res)
293 mtd->ecc_stats.badblocks++;
294 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000297/*
298 * This function unregisters and destroy all slave MTD objects which are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * attached to the given master MTD object.
300 */
301
302int del_mtd_partitions(struct mtd_info *master)
303{
Chris Malley71a928c2008-05-19 20:11:50 +0100304 struct mtd_part *slave, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Chris Malley71a928c2008-05-19 20:11:50 +0100306 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (slave->master == master) {
Chris Malley71a928c2008-05-19 20:11:50 +0100308 list_del(&slave->list);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900309 if (slave->registered)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 del_mtd_device(&slave->mtd);
311 kfree(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 return 0;
315}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900316EXPORT_SYMBOL(del_mtd_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900318static struct mtd_part *add_one_partition(struct mtd_info *master,
319 const struct mtd_partition *part, int partno,
320 u_int32_t cur_offset)
321{
322 struct mtd_part *slave;
323
324 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900325 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900326 if (!slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900327 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900328 master->name);
329 del_mtd_partitions(master);
330 return NULL;
331 }
332 list_add(&slave->list, &mtd_partitions);
333
334 /* set up the MTD object for this partition */
335 slave->mtd.type = master->type;
336 slave->mtd.flags = master->flags & ~part->mask_flags;
337 slave->mtd.size = part->size;
338 slave->mtd.writesize = master->writesize;
339 slave->mtd.oobsize = master->oobsize;
340 slave->mtd.oobavail = master->oobavail;
341 slave->mtd.subpage_sft = master->subpage_sft;
342
343 slave->mtd.name = part->name;
344 slave->mtd.owner = master->owner;
345
346 slave->mtd.read = part_read;
347 slave->mtd.write = part_write;
348
349 if (master->panic_write)
350 slave->mtd.panic_write = part_panic_write;
351
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900352 if (master->point && master->unpoint) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900353 slave->mtd.point = part_point;
354 slave->mtd.unpoint = part_unpoint;
355 }
356
357 if (master->read_oob)
358 slave->mtd.read_oob = part_read_oob;
359 if (master->write_oob)
360 slave->mtd.write_oob = part_write_oob;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900361 if (master->read_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900362 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900363 if (master->read_fact_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900364 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900365 if (master->write_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900366 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900367 if (master->lock_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900368 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900369 if (master->get_user_prot_info)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900370 slave->mtd.get_user_prot_info = part_get_user_prot_info;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900371 if (master->get_fact_prot_info)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900372 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
373 if (master->sync)
374 slave->mtd.sync = part_sync;
375 if (!partno && master->suspend && master->resume) {
376 slave->mtd.suspend = part_suspend;
377 slave->mtd.resume = part_resume;
378 }
379 if (master->writev)
380 slave->mtd.writev = part_writev;
381 if (master->lock)
382 slave->mtd.lock = part_lock;
383 if (master->unlock)
384 slave->mtd.unlock = part_unlock;
385 if (master->block_isbad)
386 slave->mtd.block_isbad = part_block_isbad;
387 if (master->block_markbad)
388 slave->mtd.block_markbad = part_block_markbad;
389 slave->mtd.erase = part_erase;
390 slave->master = master;
391 slave->offset = part->offset;
392 slave->index = partno;
393
394 if (slave->offset == MTDPART_OFS_APPEND)
395 slave->offset = cur_offset;
396 if (slave->offset == MTDPART_OFS_NXTBLK) {
397 slave->offset = cur_offset;
398 if ((cur_offset % master->erasesize) != 0) {
399 /* Round up to next erasesize */
400 slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
401 printk(KERN_NOTICE "Moving partition %d: "
402 "0x%08x -> 0x%08x\n", partno,
403 cur_offset, slave->offset);
404 }
405 }
406 if (slave->mtd.size == MTDPART_SIZ_FULL)
407 slave->mtd.size = master->size - slave->offset;
408
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900409 printk(KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900410 slave->offset + slave->mtd.size, slave->mtd.name);
411
412 /* let's do some sanity checks */
413 if (slave->offset >= master->size) {
414 /* let's register it anyway to preserve ordering */
415 slave->offset = 0;
416 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900417 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900418 part->name);
419 }
420 if (slave->offset + slave->mtd.size > master->size) {
421 slave->mtd.size = master->size - slave->offset;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900422 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900423 part->name, master->name, slave->mtd.size);
424 }
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900425 if (master->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900426 /* Deal with variable erase size stuff */
427 int i;
428 struct mtd_erase_region_info *regions = master->eraseregions;
429
430 /* Find the first erase regions which is part of this partition. */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900431 for (i = 0; i < master->numeraseregions && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900432 ;
433
434 for (i--; i < master->numeraseregions && regions[i].offset < slave->offset + slave->mtd.size; i++) {
435 if (slave->mtd.erasesize < regions[i].erasesize) {
436 slave->mtd.erasesize = regions[i].erasesize;
437 }
438 }
439 } else {
440 /* Single erase size */
441 slave->mtd.erasesize = master->erasesize;
442 }
443
444 if ((slave->mtd.flags & MTD_WRITEABLE) &&
445 (slave->offset % slave->mtd.erasesize)) {
446 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900447 /* FIXME: Let it be writable if it is on a boundary of
448 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900449 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900450 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900451 part->name);
452 }
453 if ((slave->mtd.flags & MTD_WRITEABLE) &&
454 (slave->mtd.size % slave->mtd.erasesize)) {
455 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900456 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900457 part->name);
458 }
459
460 slave->mtd.ecclayout = master->ecclayout;
461 if (master->block_isbad) {
462 uint32_t offs = 0;
463
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900464 while (offs < slave->mtd.size) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900465 if (master->block_isbad(master,
466 offs + slave->offset))
467 slave->mtd.ecc_stats.badblocks++;
468 offs += slave->mtd.erasesize;
469 }
470 }
471
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900472 if (part->mtdp) {
473 /* store the object pointer (caller may or may not register it*/
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900474 *part->mtdp = &slave->mtd;
475 slave->registered = 0;
476 } else {
477 /* register our partition */
478 add_mtd_device(&slave->mtd);
479 slave->registered = 1;
480 }
481 return slave;
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/*
485 * This function, given a master MTD object and a partition table, creates
486 * and registers slave MTD objects which are bound to the master according to
487 * the partition definitions.
488 * (Q: should we register the master MTD object as well?)
489 */
490
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000491int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 const struct mtd_partition *parts,
493 int nbparts)
494{
495 struct mtd_part *slave;
496 u_int32_t cur_offset = 0;
497 int i;
498
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900499 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 for (i = 0; i < nbparts; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900502 slave = add_one_partition(master, parts + i, i, cur_offset);
503 if (!slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507
508 return 0;
509}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510EXPORT_SYMBOL(add_mtd_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512static DEFINE_SPINLOCK(part_parser_lock);
513static LIST_HEAD(part_parsers);
514
515static struct mtd_part_parser *get_partition_parser(const char *name)
516{
Chris Malley71a928c2008-05-19 20:11:50 +0100517 struct mtd_part_parser *p, *ret = NULL;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 spin_lock(&part_parser_lock);
520
Chris Malley71a928c2008-05-19 20:11:50 +0100521 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
523 ret = p;
524 break;
525 }
Chris Malley71a928c2008-05-19 20:11:50 +0100526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 spin_unlock(&part_parser_lock);
528
529 return ret;
530}
531
532int register_mtd_parser(struct mtd_part_parser *p)
533{
534 spin_lock(&part_parser_lock);
535 list_add(&p->list, &part_parsers);
536 spin_unlock(&part_parser_lock);
537
538 return 0;
539}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900540EXPORT_SYMBOL_GPL(register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
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}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900549EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000551int parse_mtd_partitions(struct mtd_info *master, const char **types,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 struct mtd_partition **pparts, unsigned long origin)
553{
554 struct mtd_part_parser *parser;
555 int ret = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 for ( ; ret <= 0 && *types; types++) {
558 parser = get_partition_parser(*types);
559#ifdef CONFIG_KMOD
560 if (!parser && !request_module("%s", *types))
561 parser = get_partition_parser(*types);
562#endif
563 if (!parser) {
564 printk(KERN_NOTICE "%s partition parsing not available\n",
565 *types);
566 continue;
567 }
568 ret = (*parser->parse_fn)(master, pparts, origin);
569 if (ret > 0) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000570 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 ret, parser->name, master->name);
572 }
573 put_partition_parser(parser);
574 }
575 return ret;
576}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577EXPORT_SYMBOL_GPL(parse_mtd_partitions);