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>
52 #define PART(x) ((struct mtd_part *)(x))
60 static int part_read(
struct mtd_info *mtd, loff_t
from,
size_t len,
67 stats = part->
master->ecc_stats;
79 static int part_point(
struct mtd_info *mtd, loff_t
from,
size_t len,
88 static int part_unpoint(
struct mtd_info *mtd, loff_t from,
size_t len)
95 static unsigned long part_get_unmapped_area(
struct mtd_info *mtd,
103 return part->
master->_get_unmapped_area(part->
master, len, offset,
107 static int part_read_oob(
struct mtd_info *mtd, loff_t from,
113 if (from >= mtd->
size)
129 pages = mtd_div_by_ws(mtd->
size, mtd);
130 pages -= mtd_div_by_ws(from, mtd);
137 if (mtd_is_bitflip(res))
139 if (mtd_is_eccerr(res))
145 static int part_read_user_prot_reg(
struct mtd_info *mtd, loff_t from,
149 return part->
master->_read_user_prot_reg(part->
master, from, len,
153 static int part_get_user_prot_info(
struct mtd_info *mtd,
157 return part->
master->_get_user_prot_info(part->
master, buf, len);
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)
164 return part->
master->_read_fact_prot_reg(part->
master, from, len,
168 static int part_get_fact_prot_info(
struct mtd_info *mtd,
struct otp_info *buf,
172 return part->
master->_get_fact_prot_info(part->
master, buf, len);
175 static int part_write(
struct mtd_info *mtd, loff_t to,
size_t len,
176 size_t *retlen,
const u_char *buf)
183 static int part_panic_write(
struct mtd_info *mtd, loff_t to,
size_t len,
184 size_t *retlen,
const u_char *buf)
191 static int part_write_oob(
struct mtd_info *mtd, loff_t to,
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)
207 return part->
master->_write_user_prot_reg(part->
master, from, len,
211 static int part_lock_user_prot_reg(
struct mtd_info *mtd, loff_t from,
215 return part->
master->_lock_user_prot_reg(part->
master, from, len);
218 static int part_writev(
struct mtd_info *mtd,
const struct kvec *vecs,
219 unsigned long count, loff_t to,
size_t *retlen)
223 to + part->
offset, retlen);
243 if (instr->
mtd->_erase == part_erase) {
273 static void part_sync(
struct mtd_info *mtd)
279 static int part_suspend(
struct mtd_info *mtd)
285 static void part_resume(
struct mtd_info *mtd)
291 static int part_block_isbad(
struct mtd_info *mtd, loff_t ofs)
298 static int part_block_markbad(
struct mtd_info *mtd, loff_t ofs)
310 static inline void free_partition(
struct mtd_part *
p)
328 if (slave->
master == master) {
335 free_partition(slave);
352 if (!name || !slave) {
353 printk(
KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
361 slave->
mtd.type = master->
type;
377 slave->
mtd.dev.parent = master->
dev.parent;
379 slave->
mtd._read = part_read;
380 slave->
mtd._write = part_write;
383 slave->
mtd._panic_write = part_panic_write;
386 slave->
mtd._point = part_point;
387 slave->
mtd._unpoint = part_unpoint;
391 slave->
mtd._get_unmapped_area = part_get_unmapped_area;
393 slave->
mtd._read_oob = part_read_oob;
395 slave->
mtd._write_oob = part_write_oob;
397 slave->
mtd._read_user_prot_reg = part_read_user_prot_reg;
399 slave->
mtd._read_fact_prot_reg = part_read_fact_prot_reg;
401 slave->
mtd._write_user_prot_reg = part_write_user_prot_reg;
403 slave->
mtd._lock_user_prot_reg = part_lock_user_prot_reg;
405 slave->
mtd._get_user_prot_info = part_get_user_prot_info;
407 slave->
mtd._get_fact_prot_info = part_get_fact_prot_info;
409 slave->
mtd._sync = part_sync;
410 if (!partno && !master->
dev.class && master->
_suspend &&
412 slave->
mtd._suspend = part_suspend;
413 slave->
mtd._resume = part_resume;
416 slave->
mtd._writev = part_writev;
418 slave->
mtd._lock = part_lock;
420 slave->
mtd._unlock = part_unlock;
422 slave->
mtd._is_locked = part_is_locked;
424 slave->
mtd._block_isbad = part_block_isbad;
426 slave->
mtd._block_markbad = part_block_markbad;
427 slave->
mtd._erase = part_erase;
432 slave->
offset = cur_offset;
434 slave->
offset = cur_offset;
435 if (mtd_mod_by_eb(cur_offset, master) != 0) {
437 slave->
offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->
erasesize;
439 "0x%012llx -> 0x%012llx\n", partno,
440 (
unsigned long long)cur_offset, (
unsigned long long)slave->
offset);
444 slave->
offset = cur_offset;
449 printk(
KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
460 (
unsigned long long)(slave->
offset + slave->
mtd.size), slave->
mtd.name);
467 printk(
KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
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);
484 for (i = 0; i < max && regions[
i].
offset <= slave->
offset; i++)
491 for (; i < max && regions[
i].
offset <
end; i++) {
503 mtd_mod_by_eb(slave->
offset, &slave->
mtd)) {
508 printk(
KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
512 mtd_mod_by_eb(slave->
mtd.size, &slave->
mtd)) {
514 printk(
KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
525 while (offs < slave->mtd.
size) {
527 slave->
mtd.ecc_stats.badblocks++;
528 offs += slave->
mtd.erasesize;
537 long long offset,
long long length)
561 new = allocate_partition(master, &part, -1, offset);
570 if (p->
master == master) {
571 if ((start >= p->
offset) &&
580 list_add(&new->list, &mtd_partitions);
600 if ((slave->
master == master) &&
601 (slave->
mtd.index == partno)) {
607 free_partition(slave);
635 for (i = 0; i < nbparts; i++) {
636 slave = allocate_partition(master, parts + i, i, cur_offset);
638 return PTR_ERR(slave);
641 list_add(&slave->
list, &mtd_partitions);
646 cur_offset = slave->
offset + slave->
mtd.size;
659 spin_lock(&part_parser_lock);
667 spin_unlock(&part_parser_lock);
672 #define put_partition_parser(p) do { module_put((p)->owner); } while (0)
676 spin_lock(&part_parser_lock);
677 list_add(&p->
list, &part_parsers);
678 spin_unlock(&part_parser_lock);
686 spin_lock(&part_parser_lock);
688 spin_unlock(&part_parser_lock);
697 static const char *default_mtd_part_types[] = {
731 types = default_mtd_part_types;
733 for ( ; ret <= 0 && *types; types++) {
734 parser = get_partition_parser(*types);
735 if (!parser && !request_module(
"%s", *types))
736 parser = get_partition_parser(*types);
739 ret = (*parser->
parse_fn)(master, pparts, data);
757 if (&part->
mtd == mtd) {
773 return PART(mtd)->master->size;