1 /*
2  * Simple MTD partitioning layer
3  *
4  * (C) 2000 Nicolas Pitre <nico@cam.org>
5  *
6  * This code is GPL
7  *
8  * $Id: mtdpart.c,v 1.32 2002/10/21 13:40:05 jocke Exp $
9  *
10  * 	02-21-2002	Thomas Gleixner <gleixner@autronix.de>
11  *			added support for read_oob, write_oob
12  */
13 
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 
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/partitions.h>
22 
23 
24 /* Our partition linked list */
25 static LIST_HEAD(mtd_partitions);
26 
27 /* Our partition node structure */
28 struct 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 
43 
44 /*
45  * MTD methods which simply translate the effective address and pass through
46  * to the _real_ device.
47  */
48 
part_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)49 static int part_read (struct mtd_info *mtd, loff_t from, size_t len,
50 			size_t *retlen, u_char *buf)
51 {
52 	struct mtd_part *part = PART(mtd);
53 	if (from >= mtd->size)
54 		len = 0;
55 	else if (from + len > mtd->size)
56 		len = mtd->size - from;
57 	return part->master->read (part->master, from + part->offset,
58 					len, retlen, buf);
59 }
60 
part_point(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char ** buf)61 static int part_point (struct mtd_info *mtd, loff_t from, size_t len,
62 			size_t *retlen, u_char **buf)
63 {
64 	struct mtd_part *part = PART(mtd);
65 	if (from >= mtd->size)
66 		len = 0;
67 	else if (from + len > mtd->size)
68 		len = mtd->size - from;
69 	return part->master->point (part->master, from + part->offset,
70 				    len, retlen, buf);
71 }
part_unpoint(struct mtd_info * mtd,u_char * addr,loff_t from,size_t len)72 static void part_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
73 {
74 	struct mtd_part *part = PART(mtd);
75 
76 	part->master->unpoint (part->master, addr, from + part->offset, len);
77 }
78 
79 
part_read_ecc(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf,u_char * eccbuf,int oobsel)80 static int part_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
81 			size_t *retlen, u_char *buf, u_char *eccbuf, int oobsel)
82 {
83 	struct mtd_part *part = PART(mtd);
84 	if (from >= mtd->size)
85 		len = 0;
86 	else if (from + len > mtd->size)
87 		len = mtd->size - from;
88 	return part->master->read_ecc (part->master, from + part->offset,
89 					len, retlen, buf, eccbuf, oobsel);
90 }
91 
part_read_oob(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)92 static int part_read_oob (struct mtd_info *mtd, loff_t from, size_t len,
93 			size_t *retlen, u_char *buf)
94 {
95 	struct mtd_part *part = PART(mtd);
96 	if (from >= mtd->size)
97 		len = 0;
98 	else if (from + len > mtd->size)
99 		len = mtd->size - from;
100 	return part->master->read_oob (part->master, from + part->offset,
101 					len, retlen, buf);
102 }
103 
part_read_user_prot_reg(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)104 static int part_read_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
105 			size_t *retlen, u_char *buf)
106 {
107 	struct mtd_part *part = PART(mtd);
108 	return part->master->read_user_prot_reg (part->master, from,
109 					len, retlen, buf);
110 }
111 
part_read_fact_prot_reg(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)112 static int part_read_fact_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
113 			size_t *retlen, u_char *buf)
114 {
115 	struct mtd_part *part = PART(mtd);
116 	return part->master->read_user_prot_reg (part->master, from,
117 					len, retlen, buf);
118 }
119 
part_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)120 static int part_write (struct mtd_info *mtd, loff_t to, size_t len,
121 			size_t *retlen, const u_char *buf)
122 {
123 	struct mtd_part *part = PART(mtd);
124 	if (!(mtd->flags & MTD_WRITEABLE))
125 		return -EROFS;
126 	if (to >= mtd->size)
127 		len = 0;
128 	else if (to + len > mtd->size)
129 		len = mtd->size - to;
130 	return part->master->write (part->master, to + part->offset,
131 					len, retlen, buf);
132 }
133 
part_write_ecc(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf,u_char * eccbuf,int oobsel)134 static int part_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
135 			size_t *retlen, const u_char *buf,
136 			 u_char *eccbuf, int oobsel)
137 {
138 	struct mtd_part *part = PART(mtd);
139 	if (!(mtd->flags & MTD_WRITEABLE))
140 		return -EROFS;
141 	if (to >= mtd->size)
142 		len = 0;
143 	else if (to + len > mtd->size)
144 		len = mtd->size - to;
145 	return part->master->write_ecc (part->master, to + part->offset,
146 					len, retlen, buf, eccbuf, oobsel);
147 }
148 
part_write_oob(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)149 static int part_write_oob (struct mtd_info *mtd, loff_t to, size_t len,
150 			size_t *retlen, const u_char *buf)
151 {
152 	struct mtd_part *part = PART(mtd);
153 	if (!(mtd->flags & MTD_WRITEABLE))
154 		return -EROFS;
155 	if (to >= mtd->size)
156 		len = 0;
157 	else if (to + len > mtd->size)
158 		len = mtd->size - to;
159 	return part->master->write_oob (part->master, to + part->offset,
160 					len, retlen, buf);
161 }
162 
part_write_user_prot_reg(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)163 static int part_write_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
164 			size_t *retlen, u_char *buf)
165 {
166 	struct mtd_part *part = PART(mtd);
167 	return part->master->write_user_prot_reg (part->master, from,
168 					len, retlen, buf);
169 }
170 
part_writev(struct mtd_info * mtd,const struct iovec * vecs,unsigned long count,loff_t to,size_t * retlen)171 static int part_writev (struct mtd_info *mtd,  const struct iovec *vecs,
172 			 unsigned long count, loff_t to, size_t *retlen)
173 {
174 	struct mtd_part *part = PART(mtd);
175 	if (!(mtd->flags & MTD_WRITEABLE))
176 		return -EROFS;
177 	return part->master->writev (part->master, vecs, count,
178 					to + part->offset, retlen);
179 }
180 
part_readv(struct mtd_info * mtd,struct iovec * vecs,unsigned long count,loff_t from,size_t * retlen)181 static int part_readv (struct mtd_info *mtd,  struct iovec *vecs,
182 			 unsigned long count, loff_t from, size_t *retlen)
183 {
184 	struct mtd_part *part = PART(mtd);
185 	return part->master->readv (part->master, vecs, count,
186 					from + part->offset, retlen);
187 }
188 
part_writev_ecc(struct mtd_info * mtd,const struct iovec * vecs,unsigned long count,loff_t to,size_t * retlen,u_char * eccbuf,int oobsel)189 static int part_writev_ecc (struct mtd_info *mtd,  const struct iovec *vecs,
190 			 unsigned long count, loff_t to, size_t *retlen,
191 			 u_char *eccbuf, int oobsel)
192 {
193 	struct mtd_part *part = PART(mtd);
194 	if (!(mtd->flags & MTD_WRITEABLE))
195 		return -EROFS;
196 	return part->master->writev_ecc (part->master, vecs, count,
197 					to + part->offset, retlen,
198 					eccbuf, oobsel);
199 }
200 
part_readv_ecc(struct mtd_info * mtd,struct iovec * vecs,unsigned long count,loff_t from,size_t * retlen,u_char * eccbuf,int oobsel)201 static int part_readv_ecc (struct mtd_info *mtd,  struct iovec *vecs,
202 			 unsigned long count, loff_t from, size_t *retlen,
203 			 u_char *eccbuf, int oobsel)
204 {
205 	struct mtd_part *part = PART(mtd);
206 	return part->master->readv_ecc (part->master, vecs, count,
207 					from + part->offset, retlen,
208 					eccbuf, oobsel);
209 }
210 
part_erase(struct mtd_info * mtd,struct erase_info * instr)211 static int part_erase (struct mtd_info *mtd, struct erase_info *instr)
212 {
213 	struct mtd_part *part = PART(mtd);
214 	if (!(mtd->flags & MTD_WRITEABLE))
215 		return -EROFS;
216 	if (instr->addr >= mtd->size)
217 		return -EINVAL;
218 	instr->addr += part->offset;
219 	return part->master->erase(part->master, instr);
220 }
221 
part_lock(struct mtd_info * mtd,loff_t ofs,size_t len)222 static int part_lock (struct mtd_info *mtd, loff_t ofs, size_t len)
223 {
224 	struct mtd_part *part = PART(mtd);
225 	if ((len + ofs) > mtd->size)
226 		return -EINVAL;
227 	return part->master->lock(part->master, ofs + part->offset, len);
228 }
229 
part_unlock(struct mtd_info * mtd,loff_t ofs,size_t len)230 static int part_unlock (struct mtd_info *mtd, loff_t ofs, size_t len)
231 {
232 	struct mtd_part *part = PART(mtd);
233 	if ((len + ofs) > mtd->size)
234 		return -EINVAL;
235 	return part->master->unlock(part->master, ofs + part->offset, len);
236 }
237 
part_sync(struct mtd_info * mtd)238 static void part_sync(struct mtd_info *mtd)
239 {
240 	struct mtd_part *part = PART(mtd);
241 	part->master->sync(part->master);
242 }
243 
part_suspend(struct mtd_info * mtd)244 static int part_suspend(struct mtd_info *mtd)
245 {
246 	struct mtd_part *part = PART(mtd);
247 	return part->master->suspend(part->master);
248 }
249 
part_resume(struct mtd_info * mtd)250 static void part_resume(struct mtd_info *mtd)
251 {
252 	struct mtd_part *part = PART(mtd);
253 	part->master->resume(part->master);
254 }
255 
256 /*
257  * This function unregisters and destroy all slave MTD objects which are
258  * attached to the given master MTD object.
259  */
260 
del_mtd_partitions(struct mtd_info * master)261 int del_mtd_partitions(struct mtd_info *master)
262 {
263 	struct list_head *node;
264 	struct mtd_part *slave;
265 
266 	for (node = mtd_partitions.next;
267 	     node != &mtd_partitions;
268 	     node = node->next) {
269 		slave = list_entry(node, struct mtd_part, list);
270 		if (slave->master == master) {
271 			struct list_head *prev = node->prev;
272 			__list_del(prev, node->next);
273 			if(slave->registered)
274 				del_mtd_device(&slave->mtd);
275 			kfree(slave);
276 			node = prev;
277 		}
278 	}
279 
280 	return 0;
281 }
282 
283 /*
284  * This function, given a master MTD object and a partition table, creates
285  * and registers slave MTD objects which are bound to the master according to
286  * the partition definitions.
287  * (Q: should we register the master MTD object as well?)
288  */
289 
add_mtd_partitions(struct mtd_info * master,struct mtd_partition * parts,int nbparts)290 int add_mtd_partitions(struct mtd_info *master,
291 		       struct mtd_partition *parts,
292 		       int nbparts)
293 {
294 	struct mtd_part *slave;
295 	u_int32_t cur_offset = 0;
296 	int i;
297 
298 	printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
299 
300 	for (i = 0; i < nbparts; i++) {
301 
302 		/* allocate the partition structure */
303 		slave = kmalloc (sizeof(*slave), GFP_KERNEL);
304 		if (!slave) {
305 			printk ("memory allocation error while creating partitions for \"%s\"\n",
306 				master->name);
307 			del_mtd_partitions(master);
308 			return -ENOMEM;
309 		}
310 		memset(slave, 0, sizeof(*slave));
311 		list_add(&slave->list, &mtd_partitions);
312 
313 		/* set up the MTD object for this partition */
314 		slave->mtd.type = master->type;
315 		slave->mtd.flags = master->flags & ~parts[i].mask_flags;
316 		slave->mtd.size = parts[i].size;
317 		slave->mtd.oobblock = master->oobblock;
318 		slave->mtd.oobsize = master->oobsize;
319 		slave->mtd.ecctype = master->ecctype;
320 		slave->mtd.eccsize = master->eccsize;
321 
322 		slave->mtd.name = parts[i].name;
323 		slave->mtd.bank_size = master->bank_size;
324 		slave->mtd.module = master->module;
325 
326 		slave->mtd.read = part_read;
327 		slave->mtd.write = part_write;
328 
329 		if(master->point && master->unpoint){
330 			slave->mtd.point = part_point;
331 			slave->mtd.unpoint = part_unpoint;
332 		}
333 
334 		if (master->read_ecc)
335 			slave->mtd.read_ecc = part_read_ecc;
336 		if (master->write_ecc)
337 			slave->mtd.write_ecc = part_write_ecc;
338 		if (master->read_oob)
339 			slave->mtd.read_oob = part_read_oob;
340 		if (master->write_oob)
341 			slave->mtd.write_oob = part_write_oob;
342 		if(master->read_user_prot_reg)
343 			slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
344 		if(master->read_fact_prot_reg)
345 			slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
346 		if(master->write_user_prot_reg)
347 			slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
348 		if (master->sync)
349 			slave->mtd.sync = part_sync;
350 		if (!i && master->suspend && master->resume) {
351 				slave->mtd.suspend = part_suspend;
352 				slave->mtd.resume = part_resume;
353 		}
354 		if (master->writev)
355 			slave->mtd.writev = part_writev;
356 		if (master->readv)
357 			slave->mtd.readv = part_readv;
358 		if (master->writev_ecc)
359 			slave->mtd.writev_ecc = part_writev_ecc;
360 		if (master->readv_ecc)
361 			slave->mtd.readv_ecc = part_readv_ecc;
362 		if (master->lock)
363 			slave->mtd.lock = part_lock;
364 		if (master->unlock)
365 			slave->mtd.unlock = part_unlock;
366 		slave->mtd.erase = part_erase;
367 		slave->master = master;
368 		slave->offset = parts[i].offset;
369 		slave->index = i;
370 
371 		if (slave->offset == MTDPART_OFS_APPEND)
372 			slave->offset = cur_offset;
373 		if (slave->offset == MTDPART_OFS_NXTBLK) {
374 			u_int32_t emask = master->erasesize-1;
375 			slave->offset = (cur_offset + emask) & ~emask;
376 			if (slave->offset != cur_offset) {
377 				printk(KERN_NOTICE "Moving partition %d: "
378 				       "0x%08x -> 0x%08x\n", i,
379 				       cur_offset, slave->offset);
380 			}
381 		}
382 		if (slave->mtd.size == MTDPART_SIZ_FULL)
383 			slave->mtd.size = master->size - slave->offset;
384 		cur_offset = slave->offset + slave->mtd.size;
385 
386 		printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
387 			slave->offset + slave->mtd.size, slave->mtd.name);
388 
389 		/* let's do some sanity checks */
390 		if (slave->offset >= master->size) {
391 				/* let's register it anyway to preserve ordering */
392 			slave->offset = 0;
393 			slave->mtd.size = 0;
394 			printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
395 				parts[i].name);
396 		}
397 		if (slave->offset + slave->mtd.size > master->size) {
398 			slave->mtd.size = master->size - slave->offset;
399 			printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
400 				parts[i].name, master->name, slave->mtd.size);
401 		}
402 		if (master->numeraseregions>1) {
403 			/* Deal with variable erase size stuff */
404 			int i;
405 			struct mtd_erase_region_info *regions = master->eraseregions;
406 
407 			/* Find the first erase regions which is part of this partition. */
408 			for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
409 				;
410 
411 			for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
412 				if (slave->mtd.erasesize < regions[i].erasesize) {
413 					slave->mtd.erasesize = regions[i].erasesize;
414 				}
415 			}
416 		} else {
417 			/* Single erase size */
418 			slave->mtd.erasesize = master->erasesize;
419 		}
420 
421 		if ((slave->mtd.flags & MTD_WRITEABLE) &&
422 		    (slave->offset % slave->mtd.erasesize)) {
423 			/* Doesn't start on a boundary of major erase size */
424 			/* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
425 			slave->mtd.flags &= ~MTD_WRITEABLE;
426 			printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
427 				parts[i].name);
428 		}
429 		if ((slave->mtd.flags & MTD_WRITEABLE) &&
430 		    (slave->mtd.size % slave->mtd.erasesize)) {
431 			slave->mtd.flags &= ~MTD_WRITEABLE;
432 			printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
433 				parts[i].name);
434 		}
435 
436 		if(parts[i].mtdp)
437 		{	/* store the object pointer (caller may or may not register it */
438 			*parts[i].mtdp = &slave->mtd;
439 			slave->registered = 0;
440 		}
441 		else
442 		{
443 			/* register our partition */
444 			add_mtd_device(&slave->mtd);
445 			slave->registered = 1;
446 		}
447 	}
448 
449 	return 0;
450 }
451 
452 EXPORT_SYMBOL(add_mtd_partitions);
453 EXPORT_SYMBOL(del_mtd_partitions);
454 
455 
456 MODULE_LICENSE("GPL");
457 MODULE_AUTHOR("Nicolas Pitre <nico@cam.org>");
458 MODULE_DESCRIPTION("Generic support for partitioning of MTD devices");
459 
460