blob: f22aeccf01e73b169a40165c1badd4ef34b277ac [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>
19#include <linux/config.h>
20#include <linux/kmod.h>
21#include <linux/mtd/mtd.h>
22#include <linux/mtd/partitions.h>
23#include <linux/mtd/compatmac.h>
24
25/* Our partition linked list */
26static LIST_HEAD(mtd_partitions);
27
28/* Our partition node structure */
29struct mtd_part {
30 struct mtd_info mtd;
31 struct mtd_info *master;
32 u_int32_t offset;
33 int index;
34 struct list_head list;
35 int registered;
36};
37
38/*
39 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
40 * the pointer to that structure with this macro.
41 */
42#define PART(x) ((struct mtd_part *)(x))
43
Thomas Gleixner97894cd2005-11-07 11:15:26 +000044
45/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * MTD methods which simply translate the effective address and pass through
47 * to the _real_ device.
48 */
49
Thomas Gleixner97894cd2005-11-07 11:15:26 +000050static int part_read (struct mtd_info *mtd, loff_t from, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 size_t *retlen, u_char *buf)
52{
53 struct mtd_part *part = PART(mtd);
54 if (from >= mtd->size)
55 len = 0;
56 else if (from + len > mtd->size)
57 len = mtd->size - from;
Thomas Gleixner9223a452006-05-23 17:21:03 +020058 return part->master->read (part->master, from + part->offset,
59 len, retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Thomas Gleixner97894cd2005-11-07 11:15:26 +000062static int part_point (struct mtd_info *mtd, loff_t from, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 size_t *retlen, u_char **buf)
64{
65 struct mtd_part *part = PART(mtd);
66 if (from >= mtd->size)
67 len = 0;
68 else if (from + len > mtd->size)
69 len = mtd->size - from;
Thomas Gleixner97894cd2005-11-07 11:15:26 +000070 return part->master->point (part->master, from + part->offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 len, retlen, buf);
72}
Thomas Gleixner9223a452006-05-23 17:21:03 +020073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static void part_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
75{
76 struct mtd_part *part = PART(mtd);
77
78 part->master->unpoint (part->master, addr, from + part->offset, len);
79}
80
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020081static int part_read_oob(struct mtd_info *mtd, loff_t from,
82 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 struct mtd_part *part = PART(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (from >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020087 return -EINVAL;
88 if (from + ops->len > mtd->size)
89 return -EINVAL;
90 return part->master->read_oob(part->master, from + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Thomas Gleixner97894cd2005-11-07 11:15:26 +000093static int part_read_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 size_t *retlen, u_char *buf)
95{
96 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000097 return part->master->read_user_prot_reg (part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 len, retlen, buf);
99}
100
Nicolas Pitref77814d2005-02-08 17:11:19 +0000101static int part_get_user_prot_info (struct mtd_info *mtd,
102 struct otp_info *buf, size_t len)
103{
104 struct mtd_part *part = PART(mtd);
105 return part->master->get_user_prot_info (part->master, buf, len);
106}
107
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000108static int part_read_fact_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 size_t *retlen, u_char *buf)
110{
111 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000112 return part->master->read_fact_prot_reg (part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 len, retlen, buf);
114}
115
Nicolas Pitref77814d2005-02-08 17:11:19 +0000116static int part_get_fact_prot_info (struct mtd_info *mtd,
117 struct otp_info *buf, size_t len)
118{
119 struct mtd_part *part = PART(mtd);
120 return part->master->get_fact_prot_info (part->master, buf, len);
121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static int part_write (struct mtd_info *mtd, loff_t to, size_t len,
124 size_t *retlen, const u_char *buf)
125{
126 struct mtd_part *part = PART(mtd);
127 if (!(mtd->flags & MTD_WRITEABLE))
128 return -EROFS;
129 if (to >= mtd->size)
130 len = 0;
131 else if (to + len > mtd->size)
132 len = mtd->size - to;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200133 return part->master->write (part->master, to + part->offset,
134 len, retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200137static int part_write_oob(struct mtd_info *mtd, loff_t to,
138 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 struct mtd_part *part = PART(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (!(mtd->flags & MTD_WRITEABLE))
143 return -EROFS;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (to >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200146 return -EINVAL;
147 if (to + ops->len > mtd->size)
148 return -EINVAL;
149 return part->master->write_oob(part->master, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000152static int part_write_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 size_t *retlen, u_char *buf)
154{
155 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000156 return part->master->write_user_prot_reg (part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 len, retlen, buf);
158}
159
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000160static int part_lock_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000161{
162 struct mtd_part *part = PART(mtd);
163 return part->master->lock_user_prot_reg (part->master, from, len);
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static int part_writev (struct mtd_info *mtd, const struct kvec *vecs,
167 unsigned long count, loff_t to, size_t *retlen)
168{
169 struct mtd_part *part = PART(mtd);
170 if (!(mtd->flags & MTD_WRITEABLE))
171 return -EROFS;
Thomas Gleixner9d8522d2006-05-23 16:06:03 +0200172 return part->master->writev (part->master, vecs, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static int part_erase (struct mtd_info *mtd, struct erase_info *instr)
177{
178 struct mtd_part *part = PART(mtd);
179 int ret;
180 if (!(mtd->flags & MTD_WRITEABLE))
181 return -EROFS;
182 if (instr->addr >= mtd->size)
183 return -EINVAL;
184 instr->addr += part->offset;
185 ret = part->master->erase(part->master, instr);
186 return ret;
187}
188
189void mtd_erase_callback(struct erase_info *instr)
190{
191 if (instr->mtd->erase == part_erase) {
192 struct mtd_part *part = PART(instr->mtd);
193
194 if (instr->fail_addr != 0xffffffff)
195 instr->fail_addr -= part->offset;
196 instr->addr -= part->offset;
197 }
198 if (instr->callback)
199 instr->callback(instr);
200}
201EXPORT_SYMBOL_GPL(mtd_erase_callback);
202
203static int part_lock (struct mtd_info *mtd, loff_t ofs, size_t len)
204{
205 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000206 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return -EINVAL;
208 return part->master->lock(part->master, ofs + part->offset, len);
209}
210
211static int part_unlock (struct mtd_info *mtd, loff_t ofs, size_t len)
212{
213 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000214 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return -EINVAL;
216 return part->master->unlock(part->master, ofs + part->offset, len);
217}
218
219static void part_sync(struct mtd_info *mtd)
220{
221 struct mtd_part *part = PART(mtd);
222 part->master->sync(part->master);
223}
224
225static int part_suspend(struct mtd_info *mtd)
226{
227 struct mtd_part *part = PART(mtd);
228 return part->master->suspend(part->master);
229}
230
231static void part_resume(struct mtd_info *mtd)
232{
233 struct mtd_part *part = PART(mtd);
234 part->master->resume(part->master);
235}
236
237static int part_block_isbad (struct mtd_info *mtd, loff_t ofs)
238{
239 struct mtd_part *part = PART(mtd);
240 if (ofs >= mtd->size)
241 return -EINVAL;
242 ofs += part->offset;
243 return part->master->block_isbad(part->master, ofs);
244}
245
246static int part_block_markbad (struct mtd_info *mtd, loff_t ofs)
247{
248 struct mtd_part *part = PART(mtd);
249 if (!(mtd->flags & MTD_WRITEABLE))
250 return -EROFS;
251 if (ofs >= mtd->size)
252 return -EINVAL;
253 ofs += part->offset;
254 return part->master->block_markbad(part->master, ofs);
255}
256
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000257/*
258 * This function unregisters and destroy all slave MTD objects which are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * attached to the given master MTD object.
260 */
261
262int del_mtd_partitions(struct mtd_info *master)
263{
264 struct list_head *node;
265 struct mtd_part *slave;
266
267 for (node = mtd_partitions.next;
268 node != &mtd_partitions;
269 node = node->next) {
270 slave = list_entry(node, struct mtd_part, list);
271 if (slave->master == master) {
272 struct list_head *prev = node->prev;
273 __list_del(prev, node->next);
274 if(slave->registered)
275 del_mtd_device(&slave->mtd);
276 kfree(slave);
277 node = prev;
278 }
279 }
280
281 return 0;
282}
283
284/*
285 * This function, given a master MTD object and a partition table, creates
286 * and registers slave MTD objects which are bound to the master according to
287 * the partition definitions.
288 * (Q: should we register the master MTD object as well?)
289 */
290
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000291int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 const struct mtd_partition *parts,
293 int nbparts)
294{
295 struct mtd_part *slave;
296 u_int32_t cur_offset = 0;
297 int i;
298
299 printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
300
301 for (i = 0; i < nbparts; i++) {
302
303 /* allocate the partition structure */
304 slave = kmalloc (sizeof(*slave), GFP_KERNEL);
305 if (!slave) {
306 printk ("memory allocation error while creating partitions for \"%s\"\n",
307 master->name);
308 del_mtd_partitions(master);
309 return -ENOMEM;
310 }
311 memset(slave, 0, sizeof(*slave));
312 list_add(&slave->list, &mtd_partitions);
313
314 /* set up the MTD object for this partition */
315 slave->mtd.type = master->type;
316 slave->mtd.flags = master->flags & ~parts[i].mask_flags;
317 slave->mtd.size = parts[i].size;
Joern Engel28318772006-05-22 23:18:05 +0200318 slave->mtd.writesize = master->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 slave->mtd.oobsize = master->oobsize;
320 slave->mtd.ecctype = master->ecctype;
321 slave->mtd.eccsize = master->eccsize;
322
323 slave->mtd.name = parts[i].name;
324 slave->mtd.bank_size = master->bank_size;
325 slave->mtd.owner = master->owner;
326
327 slave->mtd.read = part_read;
328 slave->mtd.write = part_write;
329
330 if(master->point && master->unpoint){
331 slave->mtd.point = part_point;
332 slave->mtd.unpoint = part_unpoint;
333 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (master->read_oob)
336 slave->mtd.read_oob = part_read_oob;
337 if (master->write_oob)
338 slave->mtd.write_oob = part_write_oob;
339 if(master->read_user_prot_reg)
340 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
341 if(master->read_fact_prot_reg)
342 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
343 if(master->write_user_prot_reg)
344 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
Nicolas Pitref77814d2005-02-08 17:11:19 +0000345 if(master->lock_user_prot_reg)
346 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
347 if(master->get_user_prot_info)
348 slave->mtd.get_user_prot_info = part_get_user_prot_info;
349 if(master->get_fact_prot_info)
350 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (master->sync)
352 slave->mtd.sync = part_sync;
353 if (!i && master->suspend && master->resume) {
354 slave->mtd.suspend = part_suspend;
355 slave->mtd.resume = part_resume;
356 }
357 if (master->writev)
358 slave->mtd.writev = part_writev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (master->lock)
360 slave->mtd.lock = part_lock;
361 if (master->unlock)
362 slave->mtd.unlock = part_unlock;
363 if (master->block_isbad)
364 slave->mtd.block_isbad = part_block_isbad;
365 if (master->block_markbad)
366 slave->mtd.block_markbad = part_block_markbad;
367 slave->mtd.erase = part_erase;
368 slave->master = master;
369 slave->offset = parts[i].offset;
370 slave->index = i;
371
372 if (slave->offset == MTDPART_OFS_APPEND)
373 slave->offset = cur_offset;
374 if (slave->offset == MTDPART_OFS_NXTBLK) {
Artem B. Bityutskiy5cea5da2005-09-30 15:49:12 +0100375 slave->offset = cur_offset;
376 if ((cur_offset % master->erasesize) != 0) {
377 /* Round up to next erasesize */
378 slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 printk(KERN_NOTICE "Moving partition %d: "
380 "0x%08x -> 0x%08x\n", i,
381 cur_offset, slave->offset);
382 }
383 }
384 if (slave->mtd.size == MTDPART_SIZ_FULL)
385 slave->mtd.size = master->size - slave->offset;
386 cur_offset = slave->offset + slave->mtd.size;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000387
388 printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 slave->offset + slave->mtd.size, slave->mtd.name);
390
391 /* let's do some sanity checks */
392 if (slave->offset >= master->size) {
393 /* let's register it anyway to preserve ordering */
394 slave->offset = 0;
395 slave->mtd.size = 0;
396 printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
397 parts[i].name);
398 }
399 if (slave->offset + slave->mtd.size > master->size) {
400 slave->mtd.size = master->size - slave->offset;
401 printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
402 parts[i].name, master->name, slave->mtd.size);
403 }
404 if (master->numeraseregions>1) {
405 /* Deal with variable erase size stuff */
406 int i;
407 struct mtd_erase_region_info *regions = master->eraseregions;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* Find the first erase regions which is part of this partition. */
410 for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
411 ;
412
413 for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
414 if (slave->mtd.erasesize < regions[i].erasesize) {
415 slave->mtd.erasesize = regions[i].erasesize;
416 }
417 }
418 } else {
419 /* Single erase size */
420 slave->mtd.erasesize = master->erasesize;
421 }
422
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000423 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 (slave->offset % slave->mtd.erasesize)) {
425 /* Doesn't start on a boundary of major erase size */
426 /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
427 slave->mtd.flags &= ~MTD_WRITEABLE;
428 printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
429 parts[i].name);
430 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000431 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 (slave->mtd.size % slave->mtd.erasesize)) {
433 slave->mtd.flags &= ~MTD_WRITEABLE;
434 printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
435 parts[i].name);
436 }
437
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200438 slave->mtd.ecclayout = master->ecclayout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 if(parts[i].mtdp)
441 { /* store the object pointer (caller may or may not register it */
442 *parts[i].mtdp = &slave->mtd;
443 slave->registered = 0;
444 }
445 else
446 {
447 /* register our partition */
448 add_mtd_device(&slave->mtd);
449 slave->registered = 1;
450 }
451 }
452
453 return 0;
454}
455
456EXPORT_SYMBOL(add_mtd_partitions);
457EXPORT_SYMBOL(del_mtd_partitions);
458
459static DEFINE_SPINLOCK(part_parser_lock);
460static LIST_HEAD(part_parsers);
461
462static struct mtd_part_parser *get_partition_parser(const char *name)
463{
464 struct list_head *this;
465 void *ret = NULL;
466 spin_lock(&part_parser_lock);
467
468 list_for_each(this, &part_parsers) {
469 struct mtd_part_parser *p = list_entry(this, struct mtd_part_parser, list);
470
471 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
472 ret = p;
473 break;
474 }
475 }
476 spin_unlock(&part_parser_lock);
477
478 return ret;
479}
480
481int register_mtd_parser(struct mtd_part_parser *p)
482{
483 spin_lock(&part_parser_lock);
484 list_add(&p->list, &part_parsers);
485 spin_unlock(&part_parser_lock);
486
487 return 0;
488}
489
490int deregister_mtd_parser(struct mtd_part_parser *p)
491{
492 spin_lock(&part_parser_lock);
493 list_del(&p->list);
494 spin_unlock(&part_parser_lock);
495 return 0;
496}
497
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000498int parse_mtd_partitions(struct mtd_info *master, const char **types,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct mtd_partition **pparts, unsigned long origin)
500{
501 struct mtd_part_parser *parser;
502 int ret = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 for ( ; ret <= 0 && *types; types++) {
505 parser = get_partition_parser(*types);
506#ifdef CONFIG_KMOD
507 if (!parser && !request_module("%s", *types))
508 parser = get_partition_parser(*types);
509#endif
510 if (!parser) {
511 printk(KERN_NOTICE "%s partition parsing not available\n",
512 *types);
513 continue;
514 }
515 ret = (*parser->parse_fn)(master, pparts, origin);
516 if (ret > 0) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000517 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 ret, parser->name, master->name);
519 }
520 put_partition_parser(parser);
521 }
522 return ret;
523}
524
525EXPORT_SYMBOL_GPL(parse_mtd_partitions);
526EXPORT_SYMBOL_GPL(register_mtd_parser);
527EXPORT_SYMBOL_GPL(deregister_mtd_parser);
528
529MODULE_LICENSE("GPL");
530MODULE_AUTHOR("Nicolas Pitre <nico@cam.org>");
531MODULE_DESCRIPTION("Generic support for partitioning of MTD devices");
532