xref: /rk3399_rockchip-uboot/drivers/mtd/mtdpart.c (revision fe85de89e43efd43a175b1ece864ce20f4f1244d)
1 /*
2  * Simple MTD partitioning layer
3  *
4  * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
5  * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
6  * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
7  *
8  * SPDX-License-Identifier:	GPL-2.0+
9  *
10  */
11 
12 #ifndef __UBOOT__
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/kmod.h>
19 #endif
20 
21 #include <common.h>
22 #include <malloc.h>
23 #include <linux/errno.h>
24 #include <linux/compat.h>
25 #include <ubi_uboot.h>
26 
27 #include <linux/mtd/mtd.h>
28 #include <linux/mtd/partitions.h>
29 #include <linux/err.h>
30 #include <linux/sizes.h>
31 
32 #include "mtdcore.h"
33 
34 #ifndef __UBOOT__
35 static DEFINE_MUTEX(mtd_partitions_mutex);
36 #else
37 DEFINE_MUTEX(mtd_partitions_mutex);
38 #endif
39 
40 #ifdef __UBOOT__
41 /* from mm/util.c */
42 
43 /**
44  * kstrdup - allocate space for and copy an existing string
45  * @s: the string to duplicate
46  * @gfp: the GFP mask used in the kmalloc() call when allocating memory
47  */
48 char *kstrdup(const char *s, gfp_t gfp)
49 {
50 	size_t len;
51 	char *buf;
52 
53 	if (!s)
54 		return NULL;
55 
56 	len = strlen(s) + 1;
57 	buf = kmalloc(len, gfp);
58 	if (buf)
59 		memcpy(buf, s, len);
60 	return buf;
61 }
62 #endif
63 
64 #define MTD_SIZE_REMAINING		(~0LLU)
65 #define MTD_OFFSET_NOT_SPECIFIED	(~0LLU)
66 
67 /**
68  * mtd_parse_partition - Parse @mtdparts partition definition, fill @partition
69  *                       with it and update the @mtdparts string pointer.
70  *
71  * The partition name is allocated and must be freed by the caller.
72  *
73  * This function is widely inspired from part_parse (mtdparts.c).
74  *
75  * @mtdparts: String describing the partition with mtdparts command syntax
76  * @partition: MTD partition structure to fill
77  *
78  * @return 0 on success, an error otherwise.
79  */
80 static int mtd_parse_partition(const char **_mtdparts,
81 			       struct mtd_partition *partition)
82 {
83 	const char *mtdparts = *_mtdparts;
84 	const char *name = NULL;
85 	int name_len;
86 	char *buf;
87 
88 	/* Ensure the partition structure is empty */
89 	memset(partition, 0, sizeof(struct mtd_partition));
90 
91 	/* Fetch the partition size */
92 	if (*mtdparts == '-') {
93 		/* Assign all remaining space to this partition */
94 		partition->size = MTD_SIZE_REMAINING;
95 		mtdparts++;
96 	} else {
97 		partition->size = ustrtoull(mtdparts, (char **)&mtdparts, 0);
98 		if (partition->size < SZ_4K) {
99 			printf("Minimum partition size 4kiB, %lldB requested\n",
100 			       partition->size);
101 			return -EINVAL;
102 		}
103 	}
104 
105 	/* Check for the offset */
106 	partition->offset = MTD_OFFSET_NOT_SPECIFIED;
107 	if (*mtdparts == '@') {
108 		mtdparts++;
109 		partition->offset = ustrtoull(mtdparts, (char **)&mtdparts, 0);
110 	}
111 
112 	/* Now look for the name */
113 	if (*mtdparts == '(') {
114 		name = ++mtdparts;
115 		mtdparts = strchr(name, ')');
116 		if (!mtdparts) {
117 			printf("No closing ')' found in partition name\n");
118 			return -EINVAL;
119 		}
120 		name_len = mtdparts - name + 1;
121 		if ((name_len - 1) == 0) {
122 			printf("Empty partition name\n");
123 			return -EINVAL;
124 		}
125 		mtdparts++;
126 	} else {
127 		/* Name will be of the form size@offset */
128 		name_len = 22;
129 	}
130 
131 	/* Check if the partition is read-only */
132 	if (strncmp(mtdparts, "ro", 2) == 0) {
133 		partition->mask_flags |= MTD_WRITEABLE;
134 		mtdparts += 2;
135 	}
136 
137 	/* Check for a potential next partition definition */
138 	if (*mtdparts == ',') {
139 		if (partition->size == MTD_SIZE_REMAINING) {
140 			printf("No partitions allowed after a fill-up\n");
141 			return -EINVAL;
142 		}
143 		++mtdparts;
144 	} else if ((*mtdparts == ';') || (*mtdparts == '\0')) {
145 		/* NOP */
146 	} else {
147 		printf("Unexpected character '%c' in mtdparts\n", *mtdparts);
148 		return -EINVAL;
149 	}
150 
151 	/*
152 	 * Allocate a buffer for the name and either copy the provided name or
153 	 * auto-generate it with the form 'size@offset'.
154 	 */
155 	buf = malloc(name_len);
156 	if (!buf)
157 		return -ENOMEM;
158 
159 	if (name)
160 		strncpy(buf, name, name_len - 1);
161 	else
162 		snprintf(buf, name_len, "0x%08llx@0x%08llx",
163 			 partition->size, partition->offset);
164 
165 	buf[name_len - 1] = '\0';
166 	partition->name = buf;
167 
168 	*_mtdparts = mtdparts;
169 
170 	return 0;
171 }
172 
173 /**
174  * mtd_parse_partitions - Create a partition array from an mtdparts definition
175  *
176  * Stateless function that takes a @parent MTD device, a string @_mtdparts
177  * describing the partitions (with the "mtdparts" command syntax) and creates
178  * the corresponding MTD partition structure array @_parts. Both the name and
179  * the structure partition itself must be freed freed, the caller may use
180  * @mtd_free_parsed_partitions() for this purpose.
181  *
182  * @parent: MTD device which contains the partitions
183  * @_mtdparts: Pointer to a string describing the partitions with "mtdparts"
184  *             command syntax.
185  * @_parts: Allocated array containing the partitions, must be freed by the
186  *          caller.
187  * @_nparts: Size of @_parts array.
188  *
189  * @return 0 on success, an error otherwise.
190  */
191 int mtd_parse_partitions(struct mtd_info *parent, const char **_mtdparts,
192 			 struct mtd_partition **_parts, int *_nparts)
193 {
194 	struct mtd_partition partition = {}, *parts;
195 	const char *mtdparts = *_mtdparts;
196 	int cur_off = 0, cur_sz = 0;
197 	int nparts = 0;
198 	int ret, idx;
199 	u64 sz;
200 
201 	/* First, iterate over the partitions until we know their number */
202 	while (mtdparts[0] != '\0' && mtdparts[0] != ';') {
203 		ret = mtd_parse_partition(&mtdparts, &partition);
204 		if (ret)
205 			return ret;
206 
207 		free((char *)partition.name);
208 		nparts++;
209 	}
210 
211 	/* Allocate an array of partitions to give back to the caller */
212 	parts = malloc(sizeof(*parts) * nparts);
213 	if (!parts) {
214 		printf("Not enough space to save partitions meta-data\n");
215 		return -ENOMEM;
216 	}
217 
218 	/* Iterate again over each partition to save the data in our array */
219 	for (idx = 0; idx < nparts; idx++) {
220 		ret = mtd_parse_partition(_mtdparts, &parts[idx]);
221 		if (ret)
222 			return ret;
223 
224 		if (parts[idx].size == MTD_SIZE_REMAINING)
225 			parts[idx].size = parent->size - cur_sz;
226 		cur_sz += parts[idx].size;
227 
228 		sz = parts[idx].size;
229 		if (sz < parent->writesize || do_div(sz, parent->writesize)) {
230 			printf("Partition size must be a multiple of %d\n",
231 			       parent->writesize);
232 			return -EINVAL;
233 		}
234 
235 		if (parts[idx].offset == MTD_OFFSET_NOT_SPECIFIED)
236 			parts[idx].offset = cur_off;
237 		cur_off += parts[idx].size;
238 
239 		parts[idx].ecclayout = parent->ecclayout;
240 	}
241 
242 	/* Offset by one mtdparts to point to the next device if any */
243 	if (*_mtdparts[0] == ';')
244 		(*_mtdparts)++;
245 
246 	*_parts = parts;
247 	*_nparts = nparts;
248 
249 	return 0;
250 }
251 
252 /**
253  * mtd_free_parsed_partitions - Free dynamically allocated partitions
254  *
255  * Each successful call to @mtd_parse_partitions must be followed by a call to
256  * @mtd_free_parsed_partitions to free any allocated array during the parsing
257  * process.
258  *
259  * @parts: Array containing the partitions that will be freed.
260  * @nparts: Size of @parts array.
261  */
262 void mtd_free_parsed_partitions(struct mtd_partition *parts,
263 				unsigned int nparts)
264 {
265 	int i;
266 
267 	for (i = 0; i < nparts; i++)
268 		free((char *)parts[i].name);
269 
270 	free(parts);
271 }
272 
273 /*
274  * MTD methods which simply translate the effective address and pass through
275  * to the _real_ device.
276  */
277 
278 static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
279 		size_t *retlen, u_char *buf)
280 {
281 	struct mtd_ecc_stats stats;
282 	int res;
283 
284 	stats = mtd->parent->ecc_stats;
285 	res = mtd->parent->_read(mtd->parent, from + mtd->offset, len,
286 				 retlen, buf);
287 	if (unlikely(mtd_is_eccerr(res)))
288 		mtd->ecc_stats.failed +=
289 			mtd->parent->ecc_stats.failed - stats.failed;
290 	else
291 		mtd->ecc_stats.corrected +=
292 			mtd->parent->ecc_stats.corrected - stats.corrected;
293 	return res;
294 }
295 
296 #ifndef __UBOOT__
297 static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
298 		size_t *retlen, void **virt, resource_size_t *phys)
299 {
300 	return mtd->parent->_point(mtd->parent, from + mtd->offset, len,
301 				   retlen, virt, phys);
302 }
303 
304 static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
305 {
306 	return mtd->parent->_unpoint(mtd->parent, from + mtd->offset, len);
307 }
308 #endif
309 
310 static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
311 					    unsigned long len,
312 					    unsigned long offset,
313 					    unsigned long flags)
314 {
315 	offset += mtd->offset;
316 	return mtd->parent->_get_unmapped_area(mtd->parent, len, offset, flags);
317 }
318 
319 static int part_read_oob(struct mtd_info *mtd, loff_t from,
320 		struct mtd_oob_ops *ops)
321 {
322 	int res;
323 
324 	if (from >= mtd->size)
325 		return -EINVAL;
326 	if (ops->datbuf && from + ops->len > mtd->size)
327 		return -EINVAL;
328 
329 	/*
330 	 * If OOB is also requested, make sure that we do not read past the end
331 	 * of this partition.
332 	 */
333 	if (ops->oobbuf) {
334 		size_t len, pages;
335 
336 		if (ops->mode == MTD_OPS_AUTO_OOB)
337 			len = mtd->oobavail;
338 		else
339 			len = mtd->oobsize;
340 		pages = mtd_div_by_ws(mtd->size, mtd);
341 		pages -= mtd_div_by_ws(from, mtd);
342 		if (ops->ooboffs + ops->ooblen > pages * len)
343 			return -EINVAL;
344 	}
345 
346 	res = mtd->parent->_read_oob(mtd->parent, from + mtd->offset, ops);
347 	if (unlikely(res)) {
348 		if (mtd_is_bitflip(res))
349 			mtd->ecc_stats.corrected++;
350 		if (mtd_is_eccerr(res))
351 			mtd->ecc_stats.failed++;
352 	}
353 	return res;
354 }
355 
356 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
357 		size_t len, size_t *retlen, u_char *buf)
358 {
359 	return mtd->parent->_read_user_prot_reg(mtd->parent, from, len,
360 						retlen, buf);
361 }
362 
363 static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
364 				   size_t *retlen, struct otp_info *buf)
365 {
366 	return mtd->parent->_get_user_prot_info(mtd->parent, len, retlen,
367 						buf);
368 }
369 
370 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
371 		size_t len, size_t *retlen, u_char *buf)
372 {
373 	return mtd->parent->_read_fact_prot_reg(mtd->parent, from, len,
374 						retlen, buf);
375 }
376 
377 static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
378 				   size_t *retlen, struct otp_info *buf)
379 {
380 	return mtd->parent->_get_fact_prot_info(mtd->parent, len, retlen,
381 						buf);
382 }
383 
384 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
385 		size_t *retlen, const u_char *buf)
386 {
387 	return mtd->parent->_write(mtd->parent, to + mtd->offset, len,
388 				   retlen, buf);
389 }
390 
391 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
392 		size_t *retlen, const u_char *buf)
393 {
394 	return mtd->parent->_panic_write(mtd->parent, to + mtd->offset, len,
395 					 retlen, buf);
396 }
397 
398 static int part_write_oob(struct mtd_info *mtd, loff_t to,
399 		struct mtd_oob_ops *ops)
400 {
401 	if (to >= mtd->size)
402 		return -EINVAL;
403 	if (ops->datbuf && to + ops->len > mtd->size)
404 		return -EINVAL;
405 	return mtd->parent->_write_oob(mtd->parent, to + mtd->offset, ops);
406 }
407 
408 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
409 		size_t len, size_t *retlen, u_char *buf)
410 {
411 	return mtd->parent->_write_user_prot_reg(mtd->parent, from, len,
412 						 retlen, buf);
413 }
414 
415 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
416 		size_t len)
417 {
418 	return mtd->parent->_lock_user_prot_reg(mtd->parent, from, len);
419 }
420 
421 #ifndef __UBOOT__
422 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
423 		unsigned long count, loff_t to, size_t *retlen)
424 {
425 	return mtd->parent->_writev(mtd->parent, vecs, count,
426 				    to + mtd->offset, retlen);
427 }
428 #endif
429 
430 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
431 {
432 	int ret;
433 
434 	instr->addr += mtd->offset;
435 	ret = mtd->parent->_erase(mtd->parent, instr);
436 	if (ret) {
437 		if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
438 			instr->fail_addr -= mtd->offset;
439 		instr->addr -= mtd->offset;
440 	}
441 	return ret;
442 }
443 
444 void mtd_erase_callback(struct erase_info *instr)
445 {
446 	if (instr->mtd->_erase == part_erase) {
447 		if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
448 			instr->fail_addr -= instr->mtd->offset;
449 		instr->addr -= instr->mtd->offset;
450 	}
451 	if (instr->callback)
452 		instr->callback(instr);
453 }
454 EXPORT_SYMBOL_GPL(mtd_erase_callback);
455 
456 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
457 {
458 	return mtd->parent->_lock(mtd->parent, ofs + mtd->offset, len);
459 }
460 
461 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
462 {
463 	return mtd->parent->_unlock(mtd->parent, ofs + mtd->offset, len);
464 }
465 
466 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
467 {
468 	return mtd->parent->_is_locked(mtd->parent, ofs + mtd->offset, len);
469 }
470 
471 static void part_sync(struct mtd_info *mtd)
472 {
473 	mtd->parent->_sync(mtd->parent);
474 }
475 
476 #ifndef __UBOOT__
477 static int part_suspend(struct mtd_info *mtd)
478 {
479 	return mtd->parent->_suspend(mtd->parent);
480 }
481 
482 static void part_resume(struct mtd_info *mtd)
483 {
484 	mtd->parent->_resume(mtd->parent);
485 }
486 #endif
487 
488 static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
489 {
490 	ofs += mtd->offset;
491 	return mtd->parent->_block_isreserved(mtd->parent, ofs);
492 }
493 
494 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
495 {
496 	ofs += mtd->offset;
497 	return mtd->parent->_block_isbad(mtd->parent, ofs);
498 }
499 
500 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
501 {
502 	int res;
503 
504 	ofs += mtd->offset;
505 	res = mtd->parent->_block_markbad(mtd->parent, ofs);
506 	if (!res)
507 		mtd->ecc_stats.badblocks++;
508 	return res;
509 }
510 
511 static inline void free_partition(struct mtd_info *p)
512 {
513 	kfree(p->name);
514 	kfree(p);
515 }
516 
517 /*
518  * This function unregisters and destroy all slave MTD objects which are
519  * attached to the given master MTD object, recursively.
520  */
521 static int do_del_mtd_partitions(struct mtd_info *master)
522 {
523 	struct mtd_info *slave, *next;
524 	int ret, err = 0;
525 
526 	list_for_each_entry_safe(slave, next, &master->partitions, node) {
527 		if (mtd_has_partitions(slave))
528 			del_mtd_partitions(slave);
529 
530 		debug("Deleting %s MTD partition\n", slave->name);
531 		ret = del_mtd_device(slave);
532 		if (ret < 0) {
533 			printf("Error when deleting partition \"%s\" (%d)\n",
534 			       slave->name, ret);
535 			err = ret;
536 			continue;
537 		}
538 
539 		list_del(&slave->node);
540 		free_partition(slave);
541 	}
542 
543 	return err;
544 }
545 
546 int del_mtd_partitions(struct mtd_info *master)
547 {
548 	int ret;
549 
550 	debug("Deleting MTD partitions on \"%s\":\n", master->name);
551 
552 	mutex_lock(&mtd_partitions_mutex);
553 	ret = do_del_mtd_partitions(master);
554 	mutex_unlock(&mtd_partitions_mutex);
555 
556 	return ret;
557 }
558 
559 static struct mtd_info *allocate_partition(struct mtd_info *master,
560 					   const struct mtd_partition *part,
561 					   int partno, uint64_t cur_offset)
562 {
563 	struct mtd_info *slave;
564 	char *name;
565 
566 	/* allocate the partition structure */
567 	slave = kzalloc(sizeof(*slave), GFP_KERNEL);
568 	name = kstrdup(part->name, GFP_KERNEL);
569 	if (!name || !slave) {
570 		printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
571 		       master->name);
572 		kfree(name);
573 		kfree(slave);
574 		return ERR_PTR(-ENOMEM);
575 	}
576 
577 	/* set up the MTD object for this partition */
578 	slave->type = master->type;
579 	slave->flags = master->flags & ~part->mask_flags;
580 	slave->size = part->size;
581 	slave->writesize = master->writesize;
582 	slave->writebufsize = master->writebufsize;
583 	slave->oobsize = master->oobsize;
584 	slave->oobavail = master->oobavail;
585 	slave->subpage_sft = master->subpage_sft;
586 
587 	slave->name = name;
588 	slave->owner = master->owner;
589 #ifndef __UBOOT__
590 	slave->backing_dev_info = master->backing_dev_info;
591 
592 	/* NOTE:  we don't arrange MTDs as a tree; it'd be error-prone
593 	 * to have the same data be in two different partitions.
594 	 */
595 	slave->dev.parent = master->dev.parent;
596 #endif
597 
598 	if (master->_read)
599 		slave->_read = part_read;
600 	if (master->_write)
601 		slave->_write = part_write;
602 
603 	if (master->_panic_write)
604 		slave->_panic_write = part_panic_write;
605 
606 #ifndef __UBOOT__
607 	if (master->_point && master->_unpoint) {
608 		slave->_point = part_point;
609 		slave->_unpoint = part_unpoint;
610 	}
611 #endif
612 
613 	if (master->_get_unmapped_area)
614 		slave->_get_unmapped_area = part_get_unmapped_area;
615 	if (master->_read_oob)
616 		slave->_read_oob = part_read_oob;
617 	if (master->_write_oob)
618 		slave->_write_oob = part_write_oob;
619 	if (master->_read_user_prot_reg)
620 		slave->_read_user_prot_reg = part_read_user_prot_reg;
621 	if (master->_read_fact_prot_reg)
622 		slave->_read_fact_prot_reg = part_read_fact_prot_reg;
623 	if (master->_write_user_prot_reg)
624 		slave->_write_user_prot_reg = part_write_user_prot_reg;
625 	if (master->_lock_user_prot_reg)
626 		slave->_lock_user_prot_reg = part_lock_user_prot_reg;
627 	if (master->_get_user_prot_info)
628 		slave->_get_user_prot_info = part_get_user_prot_info;
629 	if (master->_get_fact_prot_info)
630 		slave->_get_fact_prot_info = part_get_fact_prot_info;
631 	if (master->_sync)
632 		slave->_sync = part_sync;
633 #ifndef __UBOOT__
634 	if (!partno && !master->dev.class && master->_suspend &&
635 	    master->_resume) {
636 		slave->_suspend = part_suspend;
637 		slave->_resume = part_resume;
638 	}
639 	if (master->_writev)
640 		slave->_writev = part_writev;
641 #endif
642 	if (master->_lock)
643 		slave->_lock = part_lock;
644 	if (master->_unlock)
645 		slave->_unlock = part_unlock;
646 	if (master->_is_locked)
647 		slave->_is_locked = part_is_locked;
648 	if (master->_block_isreserved)
649 		slave->_block_isreserved = part_block_isreserved;
650 	if (master->_block_isbad)
651 		slave->_block_isbad = part_block_isbad;
652 	if (master->_block_markbad)
653 		slave->_block_markbad = part_block_markbad;
654 	slave->_erase = part_erase;
655 	slave->parent = master;
656 	slave->offset = part->offset;
657 	INIT_LIST_HEAD(&slave->partitions);
658 	INIT_LIST_HEAD(&slave->node);
659 
660 	if (slave->offset == MTDPART_OFS_APPEND)
661 		slave->offset = cur_offset;
662 	if (slave->offset == MTDPART_OFS_NXTBLK) {
663 		slave->offset = cur_offset;
664 		if (mtd_mod_by_eb(cur_offset, master) != 0) {
665 			/* Round up to next erasesize */
666 			slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
667 			debug("Moving partition %d: "
668 			       "0x%012llx -> 0x%012llx\n", partno,
669 			       (unsigned long long)cur_offset, (unsigned long long)slave->offset);
670 		}
671 	}
672 	if (slave->offset == MTDPART_OFS_RETAIN) {
673 		slave->offset = cur_offset;
674 		if (master->size - slave->offset >= slave->size) {
675 			slave->size = master->size - slave->offset
676 							- slave->size;
677 		} else {
678 			debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
679 				part->name, master->size - slave->offset,
680 				slave->size);
681 			/* register to preserve ordering */
682 			goto out_register;
683 		}
684 	}
685 	if (slave->size == MTDPART_SIZ_FULL)
686 		slave->size = master->size - slave->offset;
687 
688 	debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
689 		(unsigned long long)(slave->offset + slave->size), slave->name);
690 
691 	/* let's do some sanity checks */
692 	if (slave->offset >= master->size) {
693 		/* let's register it anyway to preserve ordering */
694 		slave->offset = 0;
695 		slave->size = 0;
696 		printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
697 			part->name);
698 		goto out_register;
699 	}
700 	if (slave->offset + slave->size > master->size) {
701 		slave->size = master->size - slave->offset;
702 		printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
703 		       part->name, master->name, slave->size);
704 	}
705 	if (master->numeraseregions > 1) {
706 		/* Deal with variable erase size stuff */
707 		int i, max = master->numeraseregions;
708 		u64 end = slave->offset + slave->size;
709 		struct mtd_erase_region_info *regions = master->eraseregions;
710 
711 		/* Find the first erase regions which is part of this
712 		 * partition. */
713 		for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
714 			;
715 		/* The loop searched for the region _behind_ the first one */
716 		if (i > 0)
717 			i--;
718 
719 		/* Pick biggest erasesize */
720 		for (; i < max && regions[i].offset < end; i++) {
721 			if (slave->erasesize < regions[i].erasesize)
722 				slave->erasesize = regions[i].erasesize;
723 		}
724 		WARN_ON(slave->erasesize == 0);
725 	} else {
726 		/* Single erase size */
727 		slave->erasesize = master->erasesize;
728 	}
729 
730 	if ((slave->flags & MTD_WRITEABLE) &&
731 	    mtd_mod_by_eb(slave->offset, slave)) {
732 		/* Doesn't start on a boundary of major erase size */
733 		/* FIXME: Let it be writable if it is on a boundary of
734 		 * _minor_ erase size though */
735 		slave->flags &= ~MTD_WRITEABLE;
736 		printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
737 			part->name);
738 	}
739 	if ((slave->flags & MTD_WRITEABLE) &&
740 	    mtd_mod_by_eb(slave->size, slave)) {
741 		slave->flags &= ~MTD_WRITEABLE;
742 		printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
743 			part->name);
744 	}
745 
746 	slave->ecclayout = master->ecclayout;
747 	slave->ecc_step_size = master->ecc_step_size;
748 	slave->ecc_strength = master->ecc_strength;
749 	slave->bitflip_threshold = master->bitflip_threshold;
750 
751 	if (master->_block_isbad) {
752 		uint64_t offs = 0;
753 
754 		while (offs < slave->size) {
755 			if (mtd_block_isbad(master, offs + slave->offset))
756 				slave->ecc_stats.badblocks++;
757 			offs += slave->erasesize;
758 		}
759 	}
760 
761 out_register:
762 	return slave;
763 }
764 
765 #ifndef __UBOOT__
766 int mtd_add_partition(struct mtd_info *master, const char *name,
767 		      long long offset, long long length)
768 {
769 	struct mtd_partition part;
770 	struct mtd_info *p, *new;
771 	uint64_t start, end;
772 	int ret = 0;
773 
774 	/* the direct offset is expected */
775 	if (offset == MTDPART_OFS_APPEND ||
776 	    offset == MTDPART_OFS_NXTBLK)
777 		return -EINVAL;
778 
779 	if (length == MTDPART_SIZ_FULL)
780 		length = master->size - offset;
781 
782 	if (length <= 0)
783 		return -EINVAL;
784 
785 	part.name = name;
786 	part.size = length;
787 	part.offset = offset;
788 	part.mask_flags = 0;
789 	part.ecclayout = NULL;
790 
791 	new = allocate_partition(master, &part, -1, offset);
792 	if (IS_ERR(new))
793 		return PTR_ERR(new);
794 
795 	start = offset;
796 	end = offset + length;
797 
798 	mutex_lock(&mtd_partitions_mutex);
799 	list_for_each_entry(p, &master->partitions, node) {
800 		if (start >= p->offset &&
801 		    (start < (p->offset + p->size)))
802 			goto err_inv;
803 
804 		if (end >= p->offset &&
805 		    (end < (p->offset + p->size)))
806 			goto err_inv;
807 	}
808 
809 	list_add_tail(&new->node, &master->partitions);
810 	mutex_unlock(&mtd_partitions_mutex);
811 
812 	add_mtd_device(new);
813 
814 	return ret;
815 err_inv:
816 	mutex_unlock(&mtd_partitions_mutex);
817 	free_partition(new);
818 	return -EINVAL;
819 }
820 EXPORT_SYMBOL_GPL(mtd_add_partition);
821 
822 int mtd_del_partition(struct mtd_info *master, int partno)
823 {
824 	struct mtd_info *slave, *next;
825 	int ret = -EINVAL;
826 
827 	mutex_lock(&mtd_partitions_mutex);
828 	list_for_each_entry_safe(slave, next, &master->partitions, node)
829 		if (slave->index == partno) {
830 			ret = del_mtd_device(slave);
831 			if (ret < 0)
832 				break;
833 
834 			list_del(&slave->node);
835 			free_partition(slave);
836 			break;
837 		}
838 	mutex_unlock(&mtd_partitions_mutex);
839 
840 	return ret;
841 }
842 EXPORT_SYMBOL_GPL(mtd_del_partition);
843 #endif
844 
845 /*
846  * This function, given a master MTD object and a partition table, creates
847  * and registers slave MTD objects which are bound to the master according to
848  * the partition definitions.
849  *
850  * We don't register the master, or expect the caller to have done so,
851  * for reasons of data integrity.
852  */
853 
854 int add_mtd_partitions(struct mtd_info *master,
855 		       const struct mtd_partition *parts,
856 		       int nbparts)
857 {
858 	struct mtd_info *slave;
859 	uint64_t cur_offset = 0;
860 	int i;
861 
862 	debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
863 
864 	for (i = 0; i < nbparts; i++) {
865 		slave = allocate_partition(master, parts + i, i, cur_offset);
866 		if (IS_ERR(slave))
867 			return PTR_ERR(slave);
868 
869 		mutex_lock(&mtd_partitions_mutex);
870 		list_add_tail(&slave->node, &master->partitions);
871 		mutex_unlock(&mtd_partitions_mutex);
872 
873 		add_mtd_device(slave);
874 
875 		cur_offset = slave->offset + slave->size;
876 	}
877 
878 	return 0;
879 }
880 
881 #ifndef __UBOOT__
882 static DEFINE_SPINLOCK(part_parser_lock);
883 static LIST_HEAD(part_parsers);
884 
885 static struct mtd_part_parser *get_partition_parser(const char *name)
886 {
887 	struct mtd_part_parser *p, *ret = NULL;
888 
889 	spin_lock(&part_parser_lock);
890 
891 	list_for_each_entry(p, &part_parsers, list)
892 		if (!strcmp(p->name, name) && try_module_get(p->owner)) {
893 			ret = p;
894 			break;
895 		}
896 
897 	spin_unlock(&part_parser_lock);
898 
899 	return ret;
900 }
901 
902 #define put_partition_parser(p) do { module_put((p)->owner); } while (0)
903 
904 void register_mtd_parser(struct mtd_part_parser *p)
905 {
906 	spin_lock(&part_parser_lock);
907 	list_add(&p->list, &part_parsers);
908 	spin_unlock(&part_parser_lock);
909 }
910 EXPORT_SYMBOL_GPL(register_mtd_parser);
911 
912 void deregister_mtd_parser(struct mtd_part_parser *p)
913 {
914 	spin_lock(&part_parser_lock);
915 	list_del(&p->list);
916 	spin_unlock(&part_parser_lock);
917 }
918 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
919 
920 /*
921  * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
922  * are changing this array!
923  */
924 static const char * const default_mtd_part_types[] = {
925 	"cmdlinepart",
926 	"ofpart",
927 	NULL
928 };
929 
930 /**
931  * parse_mtd_partitions - parse MTD partitions
932  * @master: the master partition (describes whole MTD device)
933  * @types: names of partition parsers to try or %NULL
934  * @pparts: array of partitions found is returned here
935  * @data: MTD partition parser-specific data
936  *
937  * This function tries to find partition on MTD device @master. It uses MTD
938  * partition parsers, specified in @types. However, if @types is %NULL, then
939  * the default list of parsers is used. The default list contains only the
940  * "cmdlinepart" and "ofpart" parsers ATM.
941  * Note: If there are more then one parser in @types, the kernel only takes the
942  * partitions parsed out by the first parser.
943  *
944  * This function may return:
945  * o a negative error code in case of failure
946  * o zero if no partitions were found
947  * o a positive number of found partitions, in which case on exit @pparts will
948  *   point to an array containing this number of &struct mtd_info objects.
949  */
950 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
951 			 struct mtd_partition **pparts,
952 			 struct mtd_part_parser_data *data)
953 {
954 	struct mtd_part_parser *parser;
955 	int ret = 0;
956 
957 	if (!types)
958 		types = default_mtd_part_types;
959 
960 	for ( ; ret <= 0 && *types; types++) {
961 		parser = get_partition_parser(*types);
962 		if (!parser && !request_module("%s", *types))
963 			parser = get_partition_parser(*types);
964 		if (!parser)
965 			continue;
966 		ret = (*parser->parse_fn)(master, pparts, data);
967 		put_partition_parser(parser);
968 		if (ret > 0) {
969 			printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
970 			       ret, parser->name, master->name);
971 			break;
972 		}
973 	}
974 	return ret;
975 }
976 #endif
977 
978 /* Returns the size of the entire flash chip */
979 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
980 {
981 	if (mtd_is_partition(mtd))
982 		return mtd->parent->size;
983 
984 	return mtd->size;
985 }
986 EXPORT_SYMBOL_GPL(mtd_get_device_size);
987