2 * Simple MTD partitioning layer
4 * (C) 2000 Nicolas Pitre <nico@cam.org>
8 * 02-21-2002 Thomas Gleixner <gleixner@autronix.de>
9 * added support for read_oob, write_oob
14 #include <asm/errno.h>
16 #include <linux/types.h>
17 #include <linux/list.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/partitions.h>
20 #include <linux/compat.h>
22 /* Our partition linked list */
23 struct list_head mtd_partitions;
25 /* Our partition node structure */
28 struct mtd_info *master;
31 struct list_head list;
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.
39 #define PART(x) ((struct mtd_part *)(x))
43 * MTD methods which simply translate the effective address and pass through
44 * to the _real_ device.
47 static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
48 size_t *retlen, u_char *buf)
50 struct mtd_part *part = PART(mtd);
51 struct mtd_ecc_stats stats;
54 stats = part->master->ecc_stats;
55 res = mtd_read(part->master, from + part->offset, len, retlen, buf);
57 if (mtd_is_bitflip(res))
58 mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
59 if (mtd_is_eccerr(res))
60 mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
65 static int part_read_oob(struct mtd_info *mtd, loff_t from,
66 struct mtd_oob_ops *ops)
68 struct mtd_part *part = PART(mtd);
71 if (from >= mtd->size)
73 if (ops->datbuf && from + ops->len > mtd->size)
75 res = mtd_read_oob(part->master, from + part->offset, ops);
78 if (mtd_is_bitflip(res))
79 mtd->ecc_stats.corrected++;
80 if (mtd_is_eccerr(res))
81 mtd->ecc_stats.failed++;
86 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
87 size_t len, size_t *retlen, u_char *buf)
89 struct mtd_part *part = PART(mtd);
90 return mtd_read_user_prot_reg(part->master, from, len, retlen, buf);
93 static int part_get_user_prot_info(struct mtd_info *mtd,
94 struct otp_info *buf, size_t len)
96 struct mtd_part *part = PART(mtd);
97 return mtd_get_user_prot_info(part->master, buf, len);
100 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
101 size_t len, size_t *retlen, u_char *buf)
103 struct mtd_part *part = PART(mtd);
104 return mtd_read_fact_prot_reg(part->master, from, len, retlen, buf);
107 static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
110 struct mtd_part *part = PART(mtd);
111 return mtd_get_fact_prot_info(part->master, buf, len);
114 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
115 size_t *retlen, const u_char *buf)
117 struct mtd_part *part = PART(mtd);
118 return mtd_write(part->master, to + part->offset, len, retlen, buf);
121 static int part_write_oob(struct mtd_info *mtd, loff_t to,
122 struct mtd_oob_ops *ops)
124 struct mtd_part *part = PART(mtd);
128 if (ops->datbuf && to + ops->len > mtd->size)
130 return mtd_write_oob(part->master, to + part->offset, ops);
133 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
134 size_t len, size_t *retlen, u_char *buf)
136 struct mtd_part *part = PART(mtd);
137 return mtd_write_user_prot_reg(part->master, from, len, retlen, buf);
140 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
143 struct mtd_part *part = PART(mtd);
144 return mtd_lock_user_prot_reg(part->master, from, len);
147 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
149 struct mtd_part *part = PART(mtd);
152 instr->addr += part->offset;
153 ret = mtd_erase(part->master, instr);
155 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
156 instr->fail_addr -= part->offset;
157 instr->addr -= part->offset;
162 void mtd_erase_callback(struct erase_info *instr)
164 if (instr->mtd->_erase == part_erase) {
165 struct mtd_part *part = PART(instr->mtd);
167 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
168 instr->fail_addr -= part->offset;
169 instr->addr -= part->offset;
172 instr->callback(instr);
175 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
177 struct mtd_part *part = PART(mtd);
178 return mtd_lock(part->master, ofs + part->offset, len);
181 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
183 struct mtd_part *part = PART(mtd);
184 return mtd_unlock(part->master, ofs + part->offset, len);
187 static void part_sync(struct mtd_info *mtd)
189 struct mtd_part *part = PART(mtd);
190 mtd_sync(part->master);
193 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
195 struct mtd_part *part = PART(mtd);
197 return mtd_block_isbad(part->master, ofs);
200 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
202 struct mtd_part *part = PART(mtd);
206 res = mtd_block_markbad(part->master, ofs);
208 mtd->ecc_stats.badblocks++;
213 * This function unregisters and destroy all slave MTD objects which are
214 * attached to the given master MTD object.
217 int del_mtd_partitions(struct mtd_info *master)
219 struct mtd_part *slave, *next;
221 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
222 if (slave->master == master) {
223 list_del(&slave->list);
224 if (slave->registered)
225 del_mtd_device(&slave->mtd);
232 static struct mtd_part *add_one_partition(struct mtd_info *master,
233 const struct mtd_partition *part, int partno,
236 struct mtd_part *slave;
238 /* allocate the partition structure */
239 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
241 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
243 del_mtd_partitions(master);
246 list_add(&slave->list, &mtd_partitions);
248 /* set up the MTD object for this partition */
249 slave->mtd.type = master->type;
250 slave->mtd.flags = master->flags & ~part->mask_flags;
251 slave->mtd.size = part->size;
252 slave->mtd.writesize = master->writesize;
253 slave->mtd.oobsize = master->oobsize;
254 slave->mtd.oobavail = master->oobavail;
255 slave->mtd.subpage_sft = master->subpage_sft;
257 slave->mtd.name = part->name;
258 slave->mtd.owner = master->owner;
260 slave->mtd._read = part_read;
261 slave->mtd._write = part_write;
263 if (master->_read_oob)
264 slave->mtd._read_oob = part_read_oob;
265 if (master->_write_oob)
266 slave->mtd._write_oob = part_write_oob;
267 if (master->_read_user_prot_reg)
268 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
269 if (master->_read_fact_prot_reg)
270 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
271 if (master->_write_user_prot_reg)
272 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
273 if (master->_lock_user_prot_reg)
274 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
275 if (master->_get_user_prot_info)
276 slave->mtd._get_user_prot_info = part_get_user_prot_info;
277 if (master->_get_fact_prot_info)
278 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
280 slave->mtd._sync = part_sync;
282 slave->mtd._lock = part_lock;
284 slave->mtd._unlock = part_unlock;
285 if (master->_block_isbad)
286 slave->mtd._block_isbad = part_block_isbad;
287 if (master->_block_markbad)
288 slave->mtd._block_markbad = part_block_markbad;
289 slave->mtd._erase = part_erase;
290 slave->master = master;
291 slave->offset = part->offset;
292 slave->index = partno;
294 if (slave->offset == MTDPART_OFS_APPEND)
295 slave->offset = cur_offset;
296 if (slave->offset == MTDPART_OFS_NXTBLK) {
297 slave->offset = cur_offset;
298 if (mtd_mod_by_eb(cur_offset, master) != 0) {
299 /* Round up to next erasesize */
300 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
301 debug("Moving partition %d: 0x%012llx -> 0x%012llx\n",
302 partno, (unsigned long long)cur_offset,
303 (unsigned long long)slave->offset);
306 if (slave->mtd.size == MTDPART_SIZ_FULL)
307 slave->mtd.size = master->size - slave->offset;
309 debug("0x%012llx-0x%012llx : \"%s\"\n",
310 (unsigned long long)slave->offset,
311 (unsigned long long)(slave->offset + slave->mtd.size),
314 /* let's do some sanity checks */
315 if (slave->offset >= master->size) {
316 /* let's register it anyway to preserve ordering */
319 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
323 if (slave->offset + slave->mtd.size > master->size) {
324 slave->mtd.size = master->size - slave->offset;
325 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
326 part->name, master->name, (unsigned long long)slave->mtd.size);
328 if (master->numeraseregions > 1) {
329 /* Deal with variable erase size stuff */
330 int i, max = master->numeraseregions;
331 u64 end = slave->offset + slave->mtd.size;
332 struct mtd_erase_region_info *regions = master->eraseregions;
334 /* Find the first erase regions which is part of this
336 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
338 /* The loop searched for the region _behind_ the first one */
341 /* Pick biggest erasesize */
342 for (; i < max && regions[i].offset < end; i++) {
343 if (slave->mtd.erasesize < regions[i].erasesize) {
344 slave->mtd.erasesize = regions[i].erasesize;
347 BUG_ON(slave->mtd.erasesize == 0);
349 /* Single erase size */
350 slave->mtd.erasesize = master->erasesize;
353 if ((slave->mtd.flags & MTD_WRITEABLE) &&
354 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
355 /* Doesn't start on a boundary of major erase size */
356 /* FIXME: Let it be writable if it is on a boundary of
357 * _minor_ erase size though */
358 slave->mtd.flags &= ~MTD_WRITEABLE;
359 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
362 if ((slave->mtd.flags & MTD_WRITEABLE) &&
363 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
364 slave->mtd.flags &= ~MTD_WRITEABLE;
365 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
369 slave->mtd.ecclayout = master->ecclayout;
370 if (master->_block_isbad) {
373 while (offs < slave->mtd.size) {
374 if (mtd_block_isbad(master, offs + slave->offset))
375 slave->mtd.ecc_stats.badblocks++;
376 offs += slave->mtd.erasesize;
382 /* store the object pointer (caller may or may not register it*/
383 *part->mtdp = &slave->mtd;
384 slave->registered = 0;
386 /* register our partition */
387 add_mtd_device(&slave->mtd);
388 slave->registered = 1;
394 * This function, given a master MTD object and a partition table, creates
395 * and registers slave MTD objects which are bound to the master according to
396 * the partition definitions.
398 * We don't register the master, or expect the caller to have done so,
399 * for reasons of data integrity.
402 int add_mtd_partitions(struct mtd_info *master,
403 const struct mtd_partition *parts,
406 struct mtd_part *slave;
407 uint64_t cur_offset = 0;
411 * Need to init the list here, since LIST_INIT() does not
412 * work on platforms where relocation has problems (like MIPS
415 if (mtd_partitions.next == NULL)
416 INIT_LIST_HEAD(&mtd_partitions);
418 debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
420 for (i = 0; i < nbparts; i++) {
421 slave = add_one_partition(master, parts + i, i, cur_offset);
424 cur_offset = slave->offset + slave->mtd.size;