1e29c22f5SKyungmin Park /*
2e29c22f5SKyungmin Park * Simple MTD partitioning layer
3e29c22f5SKyungmin Park *
4ff94bc40SHeiko Schocher * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
5ff94bc40SHeiko Schocher * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
6ff94bc40SHeiko Schocher * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
7e29c22f5SKyungmin Park *
8ff94bc40SHeiko Schocher * SPDX-License-Identifier: GPL-2.0+
9e29c22f5SKyungmin Park *
10e29c22f5SKyungmin Park */
11e29c22f5SKyungmin Park
12ff94bc40SHeiko Schocher #ifndef __UBOOT__
13ff94bc40SHeiko Schocher #include <linux/module.h>
14ff94bc40SHeiko Schocher #include <linux/types.h>
15ff94bc40SHeiko Schocher #include <linux/kernel.h>
16ff94bc40SHeiko Schocher #include <linux/slab.h>
17ff94bc40SHeiko Schocher #include <linux/list.h>
18ff94bc40SHeiko Schocher #include <linux/kmod.h>
19ff94bc40SHeiko Schocher #endif
20ff94bc40SHeiko Schocher
21e29c22f5SKyungmin Park #include <common.h>
22e29c22f5SKyungmin Park #include <malloc.h>
231221ce45SMasahiro Yamada #include <linux/errno.h>
24ff94bc40SHeiko Schocher #include <linux/compat.h>
25ff94bc40SHeiko Schocher #include <ubi_uboot.h>
26e29c22f5SKyungmin Park
27e29c22f5SKyungmin Park #include <linux/mtd/mtd.h>
28e29c22f5SKyungmin Park #include <linux/mtd/partitions.h>
29ff94bc40SHeiko Schocher #include <linux/err.h>
303de98b82SMiquel Raynal #include <linux/sizes.h>
31ff94bc40SHeiko Schocher
32ff94bc40SHeiko Schocher #include "mtdcore.h"
33e29c22f5SKyungmin Park
34ff94bc40SHeiko Schocher #ifndef __UBOOT__
35ff94bc40SHeiko Schocher static DEFINE_MUTEX(mtd_partitions_mutex);
36ff94bc40SHeiko Schocher #else
37ff94bc40SHeiko Schocher DEFINE_MUTEX(mtd_partitions_mutex);
38ff94bc40SHeiko Schocher #endif
39e29c22f5SKyungmin Park
40ff94bc40SHeiko Schocher #ifdef __UBOOT__
41ff94bc40SHeiko Schocher /* from mm/util.c */
42ff94bc40SHeiko Schocher
43ff94bc40SHeiko Schocher /**
44ff94bc40SHeiko Schocher * kstrdup - allocate space for and copy an existing string
45ff94bc40SHeiko Schocher * @s: the string to duplicate
46ff94bc40SHeiko Schocher * @gfp: the GFP mask used in the kmalloc() call when allocating memory
47ff94bc40SHeiko Schocher */
kstrdup(const char * s,gfp_t gfp)48ff94bc40SHeiko Schocher char *kstrdup(const char *s, gfp_t gfp)
49ff94bc40SHeiko Schocher {
50ff94bc40SHeiko Schocher size_t len;
51ff94bc40SHeiko Schocher char *buf;
52ff94bc40SHeiko Schocher
53ff94bc40SHeiko Schocher if (!s)
54ff94bc40SHeiko Schocher return NULL;
55ff94bc40SHeiko Schocher
56ff94bc40SHeiko Schocher len = strlen(s) + 1;
57ff94bc40SHeiko Schocher buf = kmalloc(len, gfp);
58ff94bc40SHeiko Schocher if (buf)
59ff94bc40SHeiko Schocher memcpy(buf, s, len);
60ff94bc40SHeiko Schocher return buf;
61ff94bc40SHeiko Schocher }
62ff94bc40SHeiko Schocher #endif
63ff94bc40SHeiko Schocher
643de98b82SMiquel Raynal #define MTD_SIZE_REMAINING (~0LLU)
653de98b82SMiquel Raynal #define MTD_OFFSET_NOT_SPECIFIED (~0LLU)
663de98b82SMiquel Raynal
mtd_partitions_used(struct mtd_info * master)67*ef964b01SBoris Brezillon bool mtd_partitions_used(struct mtd_info *master)
68*ef964b01SBoris Brezillon {
69*ef964b01SBoris Brezillon struct mtd_info *slave;
70*ef964b01SBoris Brezillon
71*ef964b01SBoris Brezillon list_for_each_entry(slave, &master->partitions, node) {
72*ef964b01SBoris Brezillon if (slave->usecount)
73*ef964b01SBoris Brezillon return true;
74*ef964b01SBoris Brezillon }
75*ef964b01SBoris Brezillon
76*ef964b01SBoris Brezillon return false;
77*ef964b01SBoris Brezillon }
78*ef964b01SBoris Brezillon
793de98b82SMiquel Raynal /**
803de98b82SMiquel Raynal * mtd_parse_partition - Parse @mtdparts partition definition, fill @partition
813de98b82SMiquel Raynal * with it and update the @mtdparts string pointer.
823de98b82SMiquel Raynal *
833de98b82SMiquel Raynal * The partition name is allocated and must be freed by the caller.
843de98b82SMiquel Raynal *
853de98b82SMiquel Raynal * This function is widely inspired from part_parse (mtdparts.c).
863de98b82SMiquel Raynal *
873de98b82SMiquel Raynal * @mtdparts: String describing the partition with mtdparts command syntax
883de98b82SMiquel Raynal * @partition: MTD partition structure to fill
893de98b82SMiquel Raynal *
903de98b82SMiquel Raynal * @return 0 on success, an error otherwise.
913de98b82SMiquel Raynal */
mtd_parse_partition(const char ** _mtdparts,struct mtd_partition * partition)923de98b82SMiquel Raynal static int mtd_parse_partition(const char **_mtdparts,
933de98b82SMiquel Raynal struct mtd_partition *partition)
943de98b82SMiquel Raynal {
953de98b82SMiquel Raynal const char *mtdparts = *_mtdparts;
963de98b82SMiquel Raynal const char *name = NULL;
973de98b82SMiquel Raynal int name_len;
983de98b82SMiquel Raynal char *buf;
993de98b82SMiquel Raynal
1003de98b82SMiquel Raynal /* Ensure the partition structure is empty */
1013de98b82SMiquel Raynal memset(partition, 0, sizeof(struct mtd_partition));
1023de98b82SMiquel Raynal
1033de98b82SMiquel Raynal /* Fetch the partition size */
1043de98b82SMiquel Raynal if (*mtdparts == '-') {
1053de98b82SMiquel Raynal /* Assign all remaining space to this partition */
1063de98b82SMiquel Raynal partition->size = MTD_SIZE_REMAINING;
1073de98b82SMiquel Raynal mtdparts++;
1083de98b82SMiquel Raynal } else {
1093de98b82SMiquel Raynal partition->size = ustrtoull(mtdparts, (char **)&mtdparts, 0);
1103de98b82SMiquel Raynal if (partition->size < SZ_4K) {
1113de98b82SMiquel Raynal printf("Minimum partition size 4kiB, %lldB requested\n",
1123de98b82SMiquel Raynal partition->size);
1133de98b82SMiquel Raynal return -EINVAL;
1143de98b82SMiquel Raynal }
1153de98b82SMiquel Raynal }
1163de98b82SMiquel Raynal
1173de98b82SMiquel Raynal /* Check for the offset */
1183de98b82SMiquel Raynal partition->offset = MTD_OFFSET_NOT_SPECIFIED;
1193de98b82SMiquel Raynal if (*mtdparts == '@') {
1203de98b82SMiquel Raynal mtdparts++;
1213de98b82SMiquel Raynal partition->offset = ustrtoull(mtdparts, (char **)&mtdparts, 0);
1223de98b82SMiquel Raynal }
1233de98b82SMiquel Raynal
1243de98b82SMiquel Raynal /* Now look for the name */
1253de98b82SMiquel Raynal if (*mtdparts == '(') {
1263de98b82SMiquel Raynal name = ++mtdparts;
1273de98b82SMiquel Raynal mtdparts = strchr(name, ')');
1283de98b82SMiquel Raynal if (!mtdparts) {
1293de98b82SMiquel Raynal printf("No closing ')' found in partition name\n");
1303de98b82SMiquel Raynal return -EINVAL;
1313de98b82SMiquel Raynal }
1323de98b82SMiquel Raynal name_len = mtdparts - name + 1;
1333de98b82SMiquel Raynal if ((name_len - 1) == 0) {
1343de98b82SMiquel Raynal printf("Empty partition name\n");
1353de98b82SMiquel Raynal return -EINVAL;
1363de98b82SMiquel Raynal }
1373de98b82SMiquel Raynal mtdparts++;
1383de98b82SMiquel Raynal } else {
1393de98b82SMiquel Raynal /* Name will be of the form size@offset */
1403de98b82SMiquel Raynal name_len = 22;
1413de98b82SMiquel Raynal }
1423de98b82SMiquel Raynal
1433de98b82SMiquel Raynal /* Check if the partition is read-only */
1443de98b82SMiquel Raynal if (strncmp(mtdparts, "ro", 2) == 0) {
1453de98b82SMiquel Raynal partition->mask_flags |= MTD_WRITEABLE;
1463de98b82SMiquel Raynal mtdparts += 2;
1473de98b82SMiquel Raynal }
1483de98b82SMiquel Raynal
1493de98b82SMiquel Raynal /* Check for a potential next partition definition */
1503de98b82SMiquel Raynal if (*mtdparts == ',') {
1513de98b82SMiquel Raynal if (partition->size == MTD_SIZE_REMAINING) {
1523de98b82SMiquel Raynal printf("No partitions allowed after a fill-up\n");
1533de98b82SMiquel Raynal return -EINVAL;
1543de98b82SMiquel Raynal }
1553de98b82SMiquel Raynal ++mtdparts;
1563de98b82SMiquel Raynal } else if ((*mtdparts == ';') || (*mtdparts == '\0')) {
1573de98b82SMiquel Raynal /* NOP */
1583de98b82SMiquel Raynal } else {
1593de98b82SMiquel Raynal printf("Unexpected character '%c' in mtdparts\n", *mtdparts);
1603de98b82SMiquel Raynal return -EINVAL;
1613de98b82SMiquel Raynal }
1623de98b82SMiquel Raynal
1633de98b82SMiquel Raynal /*
1643de98b82SMiquel Raynal * Allocate a buffer for the name and either copy the provided name or
1653de98b82SMiquel Raynal * auto-generate it with the form 'size@offset'.
1663de98b82SMiquel Raynal */
1673de98b82SMiquel Raynal buf = malloc(name_len);
1683de98b82SMiquel Raynal if (!buf)
1693de98b82SMiquel Raynal return -ENOMEM;
1703de98b82SMiquel Raynal
1713de98b82SMiquel Raynal if (name)
1723de98b82SMiquel Raynal strncpy(buf, name, name_len - 1);
1733de98b82SMiquel Raynal else
1743de98b82SMiquel Raynal snprintf(buf, name_len, "0x%08llx@0x%08llx",
1753de98b82SMiquel Raynal partition->size, partition->offset);
1763de98b82SMiquel Raynal
1773de98b82SMiquel Raynal buf[name_len - 1] = '\0';
1783de98b82SMiquel Raynal partition->name = buf;
1793de98b82SMiquel Raynal
1803de98b82SMiquel Raynal *_mtdparts = mtdparts;
1813de98b82SMiquel Raynal
1823de98b82SMiquel Raynal return 0;
1833de98b82SMiquel Raynal }
1843de98b82SMiquel Raynal
1853de98b82SMiquel Raynal /**
1863de98b82SMiquel Raynal * mtd_parse_partitions - Create a partition array from an mtdparts definition
1873de98b82SMiquel Raynal *
1883de98b82SMiquel Raynal * Stateless function that takes a @parent MTD device, a string @_mtdparts
1893de98b82SMiquel Raynal * describing the partitions (with the "mtdparts" command syntax) and creates
1903de98b82SMiquel Raynal * the corresponding MTD partition structure array @_parts. Both the name and
1913de98b82SMiquel Raynal * the structure partition itself must be freed freed, the caller may use
1923de98b82SMiquel Raynal * @mtd_free_parsed_partitions() for this purpose.
1933de98b82SMiquel Raynal *
1943de98b82SMiquel Raynal * @parent: MTD device which contains the partitions
1953de98b82SMiquel Raynal * @_mtdparts: Pointer to a string describing the partitions with "mtdparts"
1963de98b82SMiquel Raynal * command syntax.
1973de98b82SMiquel Raynal * @_parts: Allocated array containing the partitions, must be freed by the
1983de98b82SMiquel Raynal * caller.
1993de98b82SMiquel Raynal * @_nparts: Size of @_parts array.
2003de98b82SMiquel Raynal *
2013de98b82SMiquel Raynal * @return 0 on success, an error otherwise.
2023de98b82SMiquel Raynal */
mtd_parse_partitions(struct mtd_info * parent,const char ** _mtdparts,struct mtd_partition ** _parts,int * _nparts)2033de98b82SMiquel Raynal int mtd_parse_partitions(struct mtd_info *parent, const char **_mtdparts,
2043de98b82SMiquel Raynal struct mtd_partition **_parts, int *_nparts)
2053de98b82SMiquel Raynal {
2063de98b82SMiquel Raynal struct mtd_partition partition = {}, *parts;
2073de98b82SMiquel Raynal const char *mtdparts = *_mtdparts;
2083de98b82SMiquel Raynal int cur_off = 0, cur_sz = 0;
2093de98b82SMiquel Raynal int nparts = 0;
2103de98b82SMiquel Raynal int ret, idx;
2113de98b82SMiquel Raynal u64 sz;
2123de98b82SMiquel Raynal
2133de98b82SMiquel Raynal /* First, iterate over the partitions until we know their number */
2143de98b82SMiquel Raynal while (mtdparts[0] != '\0' && mtdparts[0] != ';') {
2153de98b82SMiquel Raynal ret = mtd_parse_partition(&mtdparts, &partition);
2163de98b82SMiquel Raynal if (ret)
2173de98b82SMiquel Raynal return ret;
2183de98b82SMiquel Raynal
2193de98b82SMiquel Raynal free((char *)partition.name);
2203de98b82SMiquel Raynal nparts++;
2213de98b82SMiquel Raynal }
2223de98b82SMiquel Raynal
2233de98b82SMiquel Raynal /* Allocate an array of partitions to give back to the caller */
2243de98b82SMiquel Raynal parts = malloc(sizeof(*parts) * nparts);
2253de98b82SMiquel Raynal if (!parts) {
2263de98b82SMiquel Raynal printf("Not enough space to save partitions meta-data\n");
2273de98b82SMiquel Raynal return -ENOMEM;
2283de98b82SMiquel Raynal }
2293de98b82SMiquel Raynal
2303de98b82SMiquel Raynal /* Iterate again over each partition to save the data in our array */
2313de98b82SMiquel Raynal for (idx = 0; idx < nparts; idx++) {
2323de98b82SMiquel Raynal ret = mtd_parse_partition(_mtdparts, &parts[idx]);
2333de98b82SMiquel Raynal if (ret)
2343de98b82SMiquel Raynal return ret;
2353de98b82SMiquel Raynal
2363de98b82SMiquel Raynal if (parts[idx].size == MTD_SIZE_REMAINING)
2373de98b82SMiquel Raynal parts[idx].size = parent->size - cur_sz;
2383de98b82SMiquel Raynal cur_sz += parts[idx].size;
2393de98b82SMiquel Raynal
2403de98b82SMiquel Raynal sz = parts[idx].size;
2413de98b82SMiquel Raynal if (sz < parent->writesize || do_div(sz, parent->writesize)) {
2423de98b82SMiquel Raynal printf("Partition size must be a multiple of %d\n",
2433de98b82SMiquel Raynal parent->writesize);
2443de98b82SMiquel Raynal return -EINVAL;
2453de98b82SMiquel Raynal }
2463de98b82SMiquel Raynal
2473de98b82SMiquel Raynal if (parts[idx].offset == MTD_OFFSET_NOT_SPECIFIED)
2483de98b82SMiquel Raynal parts[idx].offset = cur_off;
2493de98b82SMiquel Raynal cur_off += parts[idx].size;
2503de98b82SMiquel Raynal
2513de98b82SMiquel Raynal parts[idx].ecclayout = parent->ecclayout;
2523de98b82SMiquel Raynal }
2533de98b82SMiquel Raynal
2543de98b82SMiquel Raynal /* Offset by one mtdparts to point to the next device if any */
2553de98b82SMiquel Raynal if (*_mtdparts[0] == ';')
2563de98b82SMiquel Raynal (*_mtdparts)++;
2573de98b82SMiquel Raynal
2583de98b82SMiquel Raynal *_parts = parts;
2593de98b82SMiquel Raynal *_nparts = nparts;
2603de98b82SMiquel Raynal
2613de98b82SMiquel Raynal return 0;
2623de98b82SMiquel Raynal }
2633de98b82SMiquel Raynal
2643de98b82SMiquel Raynal /**
2653de98b82SMiquel Raynal * mtd_free_parsed_partitions - Free dynamically allocated partitions
2663de98b82SMiquel Raynal *
2673de98b82SMiquel Raynal * Each successful call to @mtd_parse_partitions must be followed by a call to
2683de98b82SMiquel Raynal * @mtd_free_parsed_partitions to free any allocated array during the parsing
2693de98b82SMiquel Raynal * process.
2703de98b82SMiquel Raynal *
2713de98b82SMiquel Raynal * @parts: Array containing the partitions that will be freed.
2723de98b82SMiquel Raynal * @nparts: Size of @parts array.
2733de98b82SMiquel Raynal */
mtd_free_parsed_partitions(struct mtd_partition * parts,unsigned int nparts)2743de98b82SMiquel Raynal void mtd_free_parsed_partitions(struct mtd_partition *parts,
2753de98b82SMiquel Raynal unsigned int nparts)
2763de98b82SMiquel Raynal {
2773de98b82SMiquel Raynal int i;
2783de98b82SMiquel Raynal
2793de98b82SMiquel Raynal for (i = 0; i < nparts; i++)
2803de98b82SMiquel Raynal free((char *)parts[i].name);
2813de98b82SMiquel Raynal
2823de98b82SMiquel Raynal free(parts);
2833de98b82SMiquel Raynal }
2843de98b82SMiquel Raynal
285e29c22f5SKyungmin Park /*
286e29c22f5SKyungmin Park * MTD methods which simply translate the effective address and pass through
287e29c22f5SKyungmin Park * to the _real_ device.
288e29c22f5SKyungmin Park */
289e29c22f5SKyungmin Park
part_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)290e29c22f5SKyungmin Park static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
291e29c22f5SKyungmin Park size_t *retlen, u_char *buf)
292e29c22f5SKyungmin Park {
2938d2effeaSStefan Roese struct mtd_ecc_stats stats;
294e29c22f5SKyungmin Park int res;
295e29c22f5SKyungmin Park
296c7314be4SMiquel Raynal stats = mtd->parent->ecc_stats;
297c7314be4SMiquel Raynal res = mtd->parent->_read(mtd->parent, from + mtd->offset, len,
298ff94bc40SHeiko Schocher retlen, buf);
29940462e54SPaul Burton if (unlikely(mtd_is_eccerr(res)))
30040462e54SPaul Burton mtd->ecc_stats.failed +=
301c7314be4SMiquel Raynal mtd->parent->ecc_stats.failed - stats.failed;
30240462e54SPaul Burton else
30340462e54SPaul Burton mtd->ecc_stats.corrected +=
304c7314be4SMiquel Raynal mtd->parent->ecc_stats.corrected - stats.corrected;
305e29c22f5SKyungmin Park return res;
306e29c22f5SKyungmin Park }
307e29c22f5SKyungmin Park
308ff94bc40SHeiko Schocher #ifndef __UBOOT__
part_point(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,void ** virt,resource_size_t * phys)309ff94bc40SHeiko Schocher static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
310ff94bc40SHeiko Schocher size_t *retlen, void **virt, resource_size_t *phys)
311ff94bc40SHeiko Schocher {
312c7314be4SMiquel Raynal return mtd->parent->_point(mtd->parent, from + mtd->offset, len,
313ff94bc40SHeiko Schocher retlen, virt, phys);
314ff94bc40SHeiko Schocher }
315ff94bc40SHeiko Schocher
part_unpoint(struct mtd_info * mtd,loff_t from,size_t len)316ff94bc40SHeiko Schocher static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
317ff94bc40SHeiko Schocher {
318c7314be4SMiquel Raynal return mtd->parent->_unpoint(mtd->parent, from + mtd->offset, len);
319ff94bc40SHeiko Schocher }
320ff94bc40SHeiko Schocher #endif
321ff94bc40SHeiko Schocher
part_get_unmapped_area(struct mtd_info * mtd,unsigned long len,unsigned long offset,unsigned long flags)322ff94bc40SHeiko Schocher static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
323ff94bc40SHeiko Schocher unsigned long len,
324ff94bc40SHeiko Schocher unsigned long offset,
325ff94bc40SHeiko Schocher unsigned long flags)
326ff94bc40SHeiko Schocher {
327c7314be4SMiquel Raynal offset += mtd->offset;
328c7314be4SMiquel Raynal return mtd->parent->_get_unmapped_area(mtd->parent, len, offset, flags);
329ff94bc40SHeiko Schocher }
330ff94bc40SHeiko Schocher
part_read_oob(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)331e29c22f5SKyungmin Park static int part_read_oob(struct mtd_info *mtd, loff_t from,
332e29c22f5SKyungmin Park struct mtd_oob_ops *ops)
333e29c22f5SKyungmin Park {
334e29c22f5SKyungmin Park int res;
335e29c22f5SKyungmin Park
336e29c22f5SKyungmin Park if (from >= mtd->size)
337e29c22f5SKyungmin Park return -EINVAL;
338e29c22f5SKyungmin Park if (ops->datbuf && from + ops->len > mtd->size)
339e29c22f5SKyungmin Park return -EINVAL;
340e29c22f5SKyungmin Park
341ff94bc40SHeiko Schocher /*
342ff94bc40SHeiko Schocher * If OOB is also requested, make sure that we do not read past the end
343ff94bc40SHeiko Schocher * of this partition.
344ff94bc40SHeiko Schocher */
345ff94bc40SHeiko Schocher if (ops->oobbuf) {
346ff94bc40SHeiko Schocher size_t len, pages;
347ff94bc40SHeiko Schocher
348ff94bc40SHeiko Schocher if (ops->mode == MTD_OPS_AUTO_OOB)
349ff94bc40SHeiko Schocher len = mtd->oobavail;
350ff94bc40SHeiko Schocher else
351ff94bc40SHeiko Schocher len = mtd->oobsize;
352ff94bc40SHeiko Schocher pages = mtd_div_by_ws(mtd->size, mtd);
353ff94bc40SHeiko Schocher pages -= mtd_div_by_ws(from, mtd);
354ff94bc40SHeiko Schocher if (ops->ooboffs + ops->ooblen > pages * len)
355ff94bc40SHeiko Schocher return -EINVAL;
356ff94bc40SHeiko Schocher }
357ff94bc40SHeiko Schocher
358c7314be4SMiquel Raynal res = mtd->parent->_read_oob(mtd->parent, from + mtd->offset, ops);
359e29c22f5SKyungmin Park if (unlikely(res)) {
360dfe64e2cSSergey Lapin if (mtd_is_bitflip(res))
361e29c22f5SKyungmin Park mtd->ecc_stats.corrected++;
362dfe64e2cSSergey Lapin if (mtd_is_eccerr(res))
363e29c22f5SKyungmin Park mtd->ecc_stats.failed++;
364e29c22f5SKyungmin Park }
365e29c22f5SKyungmin Park return res;
366e29c22f5SKyungmin Park }
367e29c22f5SKyungmin Park
part_read_user_prot_reg(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)3688d2effeaSStefan Roese static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
3698d2effeaSStefan Roese size_t len, size_t *retlen, u_char *buf)
370e29c22f5SKyungmin Park {
371c7314be4SMiquel Raynal return mtd->parent->_read_user_prot_reg(mtd->parent, from, len,
372ff94bc40SHeiko Schocher retlen, buf);
373e29c22f5SKyungmin Park }
374e29c22f5SKyungmin Park
part_get_user_prot_info(struct mtd_info * mtd,size_t len,size_t * retlen,struct otp_info * buf)3754e67c571SHeiko Schocher static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
3764e67c571SHeiko Schocher size_t *retlen, struct otp_info *buf)
377e29c22f5SKyungmin Park {
378c7314be4SMiquel Raynal return mtd->parent->_get_user_prot_info(mtd->parent, len, retlen,
3794e67c571SHeiko Schocher buf);
380e29c22f5SKyungmin Park }
381e29c22f5SKyungmin Park
part_read_fact_prot_reg(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)3828d2effeaSStefan Roese static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
3838d2effeaSStefan Roese size_t len, size_t *retlen, u_char *buf)
384e29c22f5SKyungmin Park {
385c7314be4SMiquel Raynal return mtd->parent->_read_fact_prot_reg(mtd->parent, from, len,
386ff94bc40SHeiko Schocher retlen, buf);
387e29c22f5SKyungmin Park }
388e29c22f5SKyungmin Park
part_get_fact_prot_info(struct mtd_info * mtd,size_t len,size_t * retlen,struct otp_info * buf)3894e67c571SHeiko Schocher static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
3904e67c571SHeiko Schocher size_t *retlen, struct otp_info *buf)
391e29c22f5SKyungmin Park {
392c7314be4SMiquel Raynal return mtd->parent->_get_fact_prot_info(mtd->parent, len, retlen,
3934e67c571SHeiko Schocher buf);
394e29c22f5SKyungmin Park }
395e29c22f5SKyungmin Park
part_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)396e29c22f5SKyungmin Park static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
397e29c22f5SKyungmin Park size_t *retlen, const u_char *buf)
398e29c22f5SKyungmin Park {
399c7314be4SMiquel Raynal return mtd->parent->_write(mtd->parent, to + mtd->offset, len,
400ff94bc40SHeiko Schocher retlen, buf);
401ff94bc40SHeiko Schocher }
402ff94bc40SHeiko Schocher
part_panic_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)403ff94bc40SHeiko Schocher static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
404ff94bc40SHeiko Schocher size_t *retlen, const u_char *buf)
405ff94bc40SHeiko Schocher {
406c7314be4SMiquel Raynal return mtd->parent->_panic_write(mtd->parent, to + mtd->offset, len,
407ff94bc40SHeiko Schocher retlen, buf);
408e29c22f5SKyungmin Park }
409e29c22f5SKyungmin Park
part_write_oob(struct mtd_info * mtd,loff_t to,struct mtd_oob_ops * ops)410e29c22f5SKyungmin Park static int part_write_oob(struct mtd_info *mtd, loff_t to,
411e29c22f5SKyungmin Park struct mtd_oob_ops *ops)
412e29c22f5SKyungmin Park {
413e29c22f5SKyungmin Park if (to >= mtd->size)
414e29c22f5SKyungmin Park return -EINVAL;
415e29c22f5SKyungmin Park if (ops->datbuf && to + ops->len > mtd->size)
416e29c22f5SKyungmin Park return -EINVAL;
417c7314be4SMiquel Raynal return mtd->parent->_write_oob(mtd->parent, to + mtd->offset, ops);
418e29c22f5SKyungmin Park }
419e29c22f5SKyungmin Park
part_write_user_prot_reg(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)4208d2effeaSStefan Roese static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
4218d2effeaSStefan Roese size_t len, size_t *retlen, u_char *buf)
422e29c22f5SKyungmin Park {
423c7314be4SMiquel Raynal return mtd->parent->_write_user_prot_reg(mtd->parent, from, len,
424ff94bc40SHeiko Schocher retlen, buf);
425e29c22f5SKyungmin Park }
426e29c22f5SKyungmin Park
part_lock_user_prot_reg(struct mtd_info * mtd,loff_t from,size_t len)4278d2effeaSStefan Roese static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
4288d2effeaSStefan Roese size_t len)
429e29c22f5SKyungmin Park {
430c7314be4SMiquel Raynal return mtd->parent->_lock_user_prot_reg(mtd->parent, from, len);
431e29c22f5SKyungmin Park }
432e29c22f5SKyungmin Park
433ff94bc40SHeiko Schocher #ifndef __UBOOT__
part_writev(struct mtd_info * mtd,const struct kvec * vecs,unsigned long count,loff_t to,size_t * retlen)434ff94bc40SHeiko Schocher static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
435ff94bc40SHeiko Schocher unsigned long count, loff_t to, size_t *retlen)
436ff94bc40SHeiko Schocher {
437c7314be4SMiquel Raynal return mtd->parent->_writev(mtd->parent, vecs, count,
438c7314be4SMiquel Raynal to + mtd->offset, retlen);
439ff94bc40SHeiko Schocher }
440ff94bc40SHeiko Schocher #endif
441ff94bc40SHeiko Schocher
part_erase(struct mtd_info * mtd,struct erase_info * instr)442e29c22f5SKyungmin Park static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
443e29c22f5SKyungmin Park {
444e29c22f5SKyungmin Park int ret;
445dfe64e2cSSergey Lapin
446c7314be4SMiquel Raynal instr->addr += mtd->offset;
447c7314be4SMiquel Raynal ret = mtd->parent->_erase(mtd->parent, instr);
448e29c22f5SKyungmin Park if (ret) {
4498d2effeaSStefan Roese if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
450c7314be4SMiquel Raynal instr->fail_addr -= mtd->offset;
451c7314be4SMiquel Raynal instr->addr -= mtd->offset;
452e29c22f5SKyungmin Park }
453e29c22f5SKyungmin Park return ret;
454e29c22f5SKyungmin Park }
455e29c22f5SKyungmin Park
mtd_erase_callback(struct erase_info * instr)456e29c22f5SKyungmin Park void mtd_erase_callback(struct erase_info *instr)
457e29c22f5SKyungmin Park {
458dfe64e2cSSergey Lapin if (instr->mtd->_erase == part_erase) {
4598d2effeaSStefan Roese if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
460c7314be4SMiquel Raynal instr->fail_addr -= instr->mtd->offset;
461c7314be4SMiquel Raynal instr->addr -= instr->mtd->offset;
462e29c22f5SKyungmin Park }
463e29c22f5SKyungmin Park if (instr->callback)
464e29c22f5SKyungmin Park instr->callback(instr);
465e29c22f5SKyungmin Park }
466ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_erase_callback);
467e29c22f5SKyungmin Park
part_lock(struct mtd_info * mtd,loff_t ofs,uint64_t len)4688d2effeaSStefan Roese static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
469e29c22f5SKyungmin Park {
470c7314be4SMiquel Raynal return mtd->parent->_lock(mtd->parent, ofs + mtd->offset, len);
471e29c22f5SKyungmin Park }
472e29c22f5SKyungmin Park
part_unlock(struct mtd_info * mtd,loff_t ofs,uint64_t len)4738d2effeaSStefan Roese static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
474e29c22f5SKyungmin Park {
475c7314be4SMiquel Raynal return mtd->parent->_unlock(mtd->parent, ofs + mtd->offset, len);
476ff94bc40SHeiko Schocher }
477ff94bc40SHeiko Schocher
part_is_locked(struct mtd_info * mtd,loff_t ofs,uint64_t len)478ff94bc40SHeiko Schocher static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
479ff94bc40SHeiko Schocher {
480c7314be4SMiquel Raynal return mtd->parent->_is_locked(mtd->parent, ofs + mtd->offset, len);
481e29c22f5SKyungmin Park }
482e29c22f5SKyungmin Park
part_sync(struct mtd_info * mtd)483e29c22f5SKyungmin Park static void part_sync(struct mtd_info *mtd)
484e29c22f5SKyungmin Park {
485c7314be4SMiquel Raynal mtd->parent->_sync(mtd->parent);
486e29c22f5SKyungmin Park }
487e29c22f5SKyungmin Park
488ff94bc40SHeiko Schocher #ifndef __UBOOT__
part_suspend(struct mtd_info * mtd)489ff94bc40SHeiko Schocher static int part_suspend(struct mtd_info *mtd)
490ff94bc40SHeiko Schocher {
491c7314be4SMiquel Raynal return mtd->parent->_suspend(mtd->parent);
492ff94bc40SHeiko Schocher }
493ff94bc40SHeiko Schocher
part_resume(struct mtd_info * mtd)494ff94bc40SHeiko Schocher static void part_resume(struct mtd_info *mtd)
495ff94bc40SHeiko Schocher {
496c7314be4SMiquel Raynal mtd->parent->_resume(mtd->parent);
497ff94bc40SHeiko Schocher }
498ff94bc40SHeiko Schocher #endif
499ff94bc40SHeiko Schocher
part_block_isreserved(struct mtd_info * mtd,loff_t ofs)50086a720aaSEzequiel Garcia static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
50186a720aaSEzequiel Garcia {
502c7314be4SMiquel Raynal ofs += mtd->offset;
503c7314be4SMiquel Raynal return mtd->parent->_block_isreserved(mtd->parent, ofs);
50486a720aaSEzequiel Garcia }
50586a720aaSEzequiel Garcia
part_block_isbad(struct mtd_info * mtd,loff_t ofs)506e29c22f5SKyungmin Park static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
507e29c22f5SKyungmin Park {
508c7314be4SMiquel Raynal ofs += mtd->offset;
509c7314be4SMiquel Raynal return mtd->parent->_block_isbad(mtd->parent, ofs);
510e29c22f5SKyungmin Park }
511e29c22f5SKyungmin Park
part_block_markbad(struct mtd_info * mtd,loff_t ofs)512e29c22f5SKyungmin Park static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
513e29c22f5SKyungmin Park {
514e29c22f5SKyungmin Park int res;
515e29c22f5SKyungmin Park
516c7314be4SMiquel Raynal ofs += mtd->offset;
517c7314be4SMiquel Raynal res = mtd->parent->_block_markbad(mtd->parent, ofs);
518e29c22f5SKyungmin Park if (!res)
519e29c22f5SKyungmin Park mtd->ecc_stats.badblocks++;
520e29c22f5SKyungmin Park return res;
521e29c22f5SKyungmin Park }
522e29c22f5SKyungmin Park
free_partition(struct mtd_info * p)523c7314be4SMiquel Raynal static inline void free_partition(struct mtd_info *p)
524ff94bc40SHeiko Schocher {
525c7314be4SMiquel Raynal kfree(p->name);
526ff94bc40SHeiko Schocher kfree(p);
527ff94bc40SHeiko Schocher }
528ff94bc40SHeiko Schocher
529e29c22f5SKyungmin Park /*
530e29c22f5SKyungmin Park * This function unregisters and destroy all slave MTD objects which are
531c7314be4SMiquel Raynal * attached to the given master MTD object, recursively.
532e29c22f5SKyungmin Park */
do_del_mtd_partitions(struct mtd_info * master)533c7314be4SMiquel Raynal static int do_del_mtd_partitions(struct mtd_info *master)
534e29c22f5SKyungmin Park {
535c7314be4SMiquel Raynal struct mtd_info *slave, *next;
536ff94bc40SHeiko Schocher int ret, err = 0;
537e29c22f5SKyungmin Park
538c7314be4SMiquel Raynal list_for_each_entry_safe(slave, next, &master->partitions, node) {
539c7314be4SMiquel Raynal if (mtd_has_partitions(slave))
540c7314be4SMiquel Raynal del_mtd_partitions(slave);
541857ca405SMiquel Raynal
542c7314be4SMiquel Raynal debug("Deleting %s MTD partition\n", slave->name);
543c7314be4SMiquel Raynal ret = del_mtd_device(slave);
544ff94bc40SHeiko Schocher if (ret < 0) {
545c7314be4SMiquel Raynal printf("Error when deleting partition \"%s\" (%d)\n",
546c7314be4SMiquel Raynal slave->name, ret);
547ff94bc40SHeiko Schocher err = ret;
548ff94bc40SHeiko Schocher continue;
549ff94bc40SHeiko Schocher }
550c7314be4SMiquel Raynal
551c7314be4SMiquel Raynal list_del(&slave->node);
552ff94bc40SHeiko Schocher free_partition(slave);
553ff94bc40SHeiko Schocher }
554ff94bc40SHeiko Schocher
555ff94bc40SHeiko Schocher return err;
556e29c22f5SKyungmin Park }
557e29c22f5SKyungmin Park
del_mtd_partitions(struct mtd_info * master)558c7314be4SMiquel Raynal int del_mtd_partitions(struct mtd_info *master)
559e29c22f5SKyungmin Park {
560c7314be4SMiquel Raynal int ret;
561c7314be4SMiquel Raynal
562c7314be4SMiquel Raynal debug("Deleting MTD partitions on \"%s\":\n", master->name);
563c7314be4SMiquel Raynal
564c7314be4SMiquel Raynal mutex_lock(&mtd_partitions_mutex);
565c7314be4SMiquel Raynal ret = do_del_mtd_partitions(master);
566c7314be4SMiquel Raynal mutex_unlock(&mtd_partitions_mutex);
567c7314be4SMiquel Raynal
568c7314be4SMiquel Raynal return ret;
569c7314be4SMiquel Raynal }
570c7314be4SMiquel Raynal
allocate_partition(struct mtd_info * master,const struct mtd_partition * part,int partno,uint64_t cur_offset)571c7314be4SMiquel Raynal static struct mtd_info *allocate_partition(struct mtd_info *master,
572c7314be4SMiquel Raynal const struct mtd_partition *part,
573c7314be4SMiquel Raynal int partno, uint64_t cur_offset)
574c7314be4SMiquel Raynal {
575c7314be4SMiquel Raynal struct mtd_info *slave;
576ff94bc40SHeiko Schocher char *name;
577e29c22f5SKyungmin Park
578e29c22f5SKyungmin Park /* allocate the partition structure */
579e29c22f5SKyungmin Park slave = kzalloc(sizeof(*slave), GFP_KERNEL);
580ff94bc40SHeiko Schocher name = kstrdup(part->name, GFP_KERNEL);
581ff94bc40SHeiko Schocher if (!name || !slave) {
5828d2effeaSStefan Roese printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
583e29c22f5SKyungmin Park master->name);
584ff94bc40SHeiko Schocher kfree(name);
585ff94bc40SHeiko Schocher kfree(slave);
586ff94bc40SHeiko Schocher return ERR_PTR(-ENOMEM);
587e29c22f5SKyungmin Park }
588e29c22f5SKyungmin Park
589e29c22f5SKyungmin Park /* set up the MTD object for this partition */
590c7314be4SMiquel Raynal slave->type = master->type;
591c7314be4SMiquel Raynal slave->flags = master->flags & ~part->mask_flags;
592c7314be4SMiquel Raynal slave->size = part->size;
593c7314be4SMiquel Raynal slave->writesize = master->writesize;
594c7314be4SMiquel Raynal slave->writebufsize = master->writebufsize;
595c7314be4SMiquel Raynal slave->oobsize = master->oobsize;
596c7314be4SMiquel Raynal slave->oobavail = master->oobavail;
597c7314be4SMiquel Raynal slave->subpage_sft = master->subpage_sft;
598e29c22f5SKyungmin Park
599c7314be4SMiquel Raynal slave->name = name;
600c7314be4SMiquel Raynal slave->owner = master->owner;
601ff94bc40SHeiko Schocher #ifndef __UBOOT__
602c7314be4SMiquel Raynal slave->backing_dev_info = master->backing_dev_info;
603ff94bc40SHeiko Schocher
604ff94bc40SHeiko Schocher /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
605ff94bc40SHeiko Schocher * to have the same data be in two different partitions.
606ff94bc40SHeiko Schocher */
607c7314be4SMiquel Raynal slave->dev.parent = master->dev.parent;
608ff94bc40SHeiko Schocher #endif
609e29c22f5SKyungmin Park
610042673efSBoris Brezillon if (master->_read)
611c7314be4SMiquel Raynal slave->_read = part_read;
612042673efSBoris Brezillon if (master->_write)
613c7314be4SMiquel Raynal slave->_write = part_write;
614e29c22f5SKyungmin Park
615ff94bc40SHeiko Schocher if (master->_panic_write)
616c7314be4SMiquel Raynal slave->_panic_write = part_panic_write;
617ff94bc40SHeiko Schocher
618ff94bc40SHeiko Schocher #ifndef __UBOOT__
619ff94bc40SHeiko Schocher if (master->_point && master->_unpoint) {
620c7314be4SMiquel Raynal slave->_point = part_point;
621c7314be4SMiquel Raynal slave->_unpoint = part_unpoint;
622ff94bc40SHeiko Schocher }
623ff94bc40SHeiko Schocher #endif
624ff94bc40SHeiko Schocher
625ff94bc40SHeiko Schocher if (master->_get_unmapped_area)
626c7314be4SMiquel Raynal slave->_get_unmapped_area = part_get_unmapped_area;
627dfe64e2cSSergey Lapin if (master->_read_oob)
628c7314be4SMiquel Raynal slave->_read_oob = part_read_oob;
629dfe64e2cSSergey Lapin if (master->_write_oob)
630c7314be4SMiquel Raynal slave->_write_oob = part_write_oob;
631dfe64e2cSSergey Lapin if (master->_read_user_prot_reg)
632c7314be4SMiquel Raynal slave->_read_user_prot_reg = part_read_user_prot_reg;
633dfe64e2cSSergey Lapin if (master->_read_fact_prot_reg)
634c7314be4SMiquel Raynal slave->_read_fact_prot_reg = part_read_fact_prot_reg;
635dfe64e2cSSergey Lapin if (master->_write_user_prot_reg)
636c7314be4SMiquel Raynal slave->_write_user_prot_reg = part_write_user_prot_reg;
637dfe64e2cSSergey Lapin if (master->_lock_user_prot_reg)
638c7314be4SMiquel Raynal slave->_lock_user_prot_reg = part_lock_user_prot_reg;
639dfe64e2cSSergey Lapin if (master->_get_user_prot_info)
640c7314be4SMiquel Raynal slave->_get_user_prot_info = part_get_user_prot_info;
641dfe64e2cSSergey Lapin if (master->_get_fact_prot_info)
642c7314be4SMiquel Raynal slave->_get_fact_prot_info = part_get_fact_prot_info;
643dfe64e2cSSergey Lapin if (master->_sync)
644c7314be4SMiquel Raynal slave->_sync = part_sync;
645ff94bc40SHeiko Schocher #ifndef __UBOOT__
646ff94bc40SHeiko Schocher if (!partno && !master->dev.class && master->_suspend &&
647ff94bc40SHeiko Schocher master->_resume) {
648c7314be4SMiquel Raynal slave->_suspend = part_suspend;
649c7314be4SMiquel Raynal slave->_resume = part_resume;
650ff94bc40SHeiko Schocher }
651ff94bc40SHeiko Schocher if (master->_writev)
652c7314be4SMiquel Raynal slave->_writev = part_writev;
653ff94bc40SHeiko Schocher #endif
654dfe64e2cSSergey Lapin if (master->_lock)
655c7314be4SMiquel Raynal slave->_lock = part_lock;
656dfe64e2cSSergey Lapin if (master->_unlock)
657c7314be4SMiquel Raynal slave->_unlock = part_unlock;
658ff94bc40SHeiko Schocher if (master->_is_locked)
659c7314be4SMiquel Raynal slave->_is_locked = part_is_locked;
66086a720aaSEzequiel Garcia if (master->_block_isreserved)
661c7314be4SMiquel Raynal slave->_block_isreserved = part_block_isreserved;
662dfe64e2cSSergey Lapin if (master->_block_isbad)
663c7314be4SMiquel Raynal slave->_block_isbad = part_block_isbad;
664dfe64e2cSSergey Lapin if (master->_block_markbad)
665c7314be4SMiquel Raynal slave->_block_markbad = part_block_markbad;
666c7314be4SMiquel Raynal slave->_erase = part_erase;
667c7314be4SMiquel Raynal slave->parent = master;
6688d2effeaSStefan Roese slave->offset = part->offset;
669c7314be4SMiquel Raynal INIT_LIST_HEAD(&slave->partitions);
670c7314be4SMiquel Raynal INIT_LIST_HEAD(&slave->node);
671e29c22f5SKyungmin Park
672e29c22f5SKyungmin Park if (slave->offset == MTDPART_OFS_APPEND)
673e29c22f5SKyungmin Park slave->offset = cur_offset;
674e29c22f5SKyungmin Park if (slave->offset == MTDPART_OFS_NXTBLK) {
675e29c22f5SKyungmin Park slave->offset = cur_offset;
6768d2effeaSStefan Roese if (mtd_mod_by_eb(cur_offset, master) != 0) {
677e29c22f5SKyungmin Park /* Round up to next erasesize */
6788d2effeaSStefan Roese slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
679ff94bc40SHeiko Schocher debug("Moving partition %d: "
680ff94bc40SHeiko Schocher "0x%012llx -> 0x%012llx\n", partno,
681ff94bc40SHeiko Schocher (unsigned long long)cur_offset, (unsigned long long)slave->offset);
682ff94bc40SHeiko Schocher }
683ff94bc40SHeiko Schocher }
684ff94bc40SHeiko Schocher if (slave->offset == MTDPART_OFS_RETAIN) {
685ff94bc40SHeiko Schocher slave->offset = cur_offset;
686c7314be4SMiquel Raynal if (master->size - slave->offset >= slave->size) {
687c7314be4SMiquel Raynal slave->size = master->size - slave->offset
688c7314be4SMiquel Raynal - slave->size;
689ff94bc40SHeiko Schocher } else {
690ff94bc40SHeiko Schocher debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
691ff94bc40SHeiko Schocher part->name, master->size - slave->offset,
692c7314be4SMiquel Raynal slave->size);
693ff94bc40SHeiko Schocher /* register to preserve ordering */
694ff94bc40SHeiko Schocher goto out_register;
695e29c22f5SKyungmin Park }
696e29c22f5SKyungmin Park }
697c7314be4SMiquel Raynal if (slave->size == MTDPART_SIZ_FULL)
698c7314be4SMiquel Raynal slave->size = master->size - slave->offset;
699e29c22f5SKyungmin Park
700ff94bc40SHeiko Schocher debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
701c7314be4SMiquel Raynal (unsigned long long)(slave->offset + slave->size), slave->name);
702e29c22f5SKyungmin Park
703e29c22f5SKyungmin Park /* let's do some sanity checks */
704e29c22f5SKyungmin Park if (slave->offset >= master->size) {
705e29c22f5SKyungmin Park /* let's register it anyway to preserve ordering */
706e29c22f5SKyungmin Park slave->offset = 0;
707c7314be4SMiquel Raynal slave->size = 0;
7088d2effeaSStefan Roese printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
7098d2effeaSStefan Roese part->name);
7108d2effeaSStefan Roese goto out_register;
711e29c22f5SKyungmin Park }
712c7314be4SMiquel Raynal if (slave->offset + slave->size > master->size) {
713c7314be4SMiquel Raynal slave->size = master->size - slave->offset;
7148d2effeaSStefan Roese printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
715c7314be4SMiquel Raynal part->name, master->name, slave->size);
716e29c22f5SKyungmin Park }
717e29c22f5SKyungmin Park if (master->numeraseregions > 1) {
718e29c22f5SKyungmin Park /* Deal with variable erase size stuff */
7198d2effeaSStefan Roese int i, max = master->numeraseregions;
720c7314be4SMiquel Raynal u64 end = slave->offset + slave->size;
721e29c22f5SKyungmin Park struct mtd_erase_region_info *regions = master->eraseregions;
722e29c22f5SKyungmin Park
7238d2effeaSStefan Roese /* Find the first erase regions which is part of this
7248d2effeaSStefan Roese * partition. */
7258d2effeaSStefan Roese for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
726e29c22f5SKyungmin Park ;
7278d2effeaSStefan Roese /* The loop searched for the region _behind_ the first one */
728ff94bc40SHeiko Schocher if (i > 0)
7298d2effeaSStefan Roese i--;
730e29c22f5SKyungmin Park
7318d2effeaSStefan Roese /* Pick biggest erasesize */
7328d2effeaSStefan Roese for (; i < max && regions[i].offset < end; i++) {
733c7314be4SMiquel Raynal if (slave->erasesize < regions[i].erasesize)
734c7314be4SMiquel Raynal slave->erasesize = regions[i].erasesize;
735e29c22f5SKyungmin Park }
736c7314be4SMiquel Raynal WARN_ON(slave->erasesize == 0);
737e29c22f5SKyungmin Park } else {
738e29c22f5SKyungmin Park /* Single erase size */
739c7314be4SMiquel Raynal slave->erasesize = master->erasesize;
740e29c22f5SKyungmin Park }
741e29c22f5SKyungmin Park
742c7314be4SMiquel Raynal if ((slave->flags & MTD_WRITEABLE) &&
743c7314be4SMiquel Raynal mtd_mod_by_eb(slave->offset, slave)) {
744e29c22f5SKyungmin Park /* Doesn't start on a boundary of major erase size */
7458d2effeaSStefan Roese /* FIXME: Let it be writable if it is on a boundary of
7468d2effeaSStefan Roese * _minor_ erase size though */
747c7314be4SMiquel Raynal slave->flags &= ~MTD_WRITEABLE;
7488d2effeaSStefan Roese printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
7498d2effeaSStefan Roese part->name);
750e29c22f5SKyungmin Park }
751c7314be4SMiquel Raynal if ((slave->flags & MTD_WRITEABLE) &&
752c7314be4SMiquel Raynal mtd_mod_by_eb(slave->size, slave)) {
753c7314be4SMiquel Raynal slave->flags &= ~MTD_WRITEABLE;
7548d2effeaSStefan Roese printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
7558d2effeaSStefan Roese part->name);
756e29c22f5SKyungmin Park }
757e29c22f5SKyungmin Park
758c7314be4SMiquel Raynal slave->ecclayout = master->ecclayout;
759c7314be4SMiquel Raynal slave->ecc_step_size = master->ecc_step_size;
760c7314be4SMiquel Raynal slave->ecc_strength = master->ecc_strength;
761c7314be4SMiquel Raynal slave->bitflip_threshold = master->bitflip_threshold;
762ff94bc40SHeiko Schocher
763dfe64e2cSSergey Lapin if (master->_block_isbad) {
7648d2effeaSStefan Roese uint64_t offs = 0;
765e29c22f5SKyungmin Park
766c7314be4SMiquel Raynal while (offs < slave->size) {
767dfe64e2cSSergey Lapin if (mtd_block_isbad(master, offs + slave->offset))
768c7314be4SMiquel Raynal slave->ecc_stats.badblocks++;
769c7314be4SMiquel Raynal offs += slave->erasesize;
770e29c22f5SKyungmin Park }
771e29c22f5SKyungmin Park }
772e29c22f5SKyungmin Park
7738d2effeaSStefan Roese out_register:
7748d2effeaSStefan Roese return slave;
7758d2effeaSStefan Roese }
7768d2effeaSStefan Roese
777ddf7bcfaSHeiko Schocher #ifndef __UBOOT__
mtd_add_partition(struct mtd_info * master,const char * name,long long offset,long long length)778ff94bc40SHeiko Schocher int mtd_add_partition(struct mtd_info *master, const char *name,
779ff94bc40SHeiko Schocher long long offset, long long length)
780ff94bc40SHeiko Schocher {
781ff94bc40SHeiko Schocher struct mtd_partition part;
782c7314be4SMiquel Raynal struct mtd_info *p, *new;
783ff94bc40SHeiko Schocher uint64_t start, end;
784ff94bc40SHeiko Schocher int ret = 0;
785ff94bc40SHeiko Schocher
786ff94bc40SHeiko Schocher /* the direct offset is expected */
787ff94bc40SHeiko Schocher if (offset == MTDPART_OFS_APPEND ||
788ff94bc40SHeiko Schocher offset == MTDPART_OFS_NXTBLK)
789ff94bc40SHeiko Schocher return -EINVAL;
790ff94bc40SHeiko Schocher
791ff94bc40SHeiko Schocher if (length == MTDPART_SIZ_FULL)
792ff94bc40SHeiko Schocher length = master->size - offset;
793ff94bc40SHeiko Schocher
794ff94bc40SHeiko Schocher if (length <= 0)
795ff94bc40SHeiko Schocher return -EINVAL;
796ff94bc40SHeiko Schocher
797ff94bc40SHeiko Schocher part.name = name;
798ff94bc40SHeiko Schocher part.size = length;
799ff94bc40SHeiko Schocher part.offset = offset;
800ff94bc40SHeiko Schocher part.mask_flags = 0;
801ff94bc40SHeiko Schocher part.ecclayout = NULL;
802ff94bc40SHeiko Schocher
803ff94bc40SHeiko Schocher new = allocate_partition(master, &part, -1, offset);
804ff94bc40SHeiko Schocher if (IS_ERR(new))
805ff94bc40SHeiko Schocher return PTR_ERR(new);
806ff94bc40SHeiko Schocher
807ff94bc40SHeiko Schocher start = offset;
808ff94bc40SHeiko Schocher end = offset + length;
809ff94bc40SHeiko Schocher
810ff94bc40SHeiko Schocher mutex_lock(&mtd_partitions_mutex);
811c7314be4SMiquel Raynal list_for_each_entry(p, &master->partitions, node) {
812c7314be4SMiquel Raynal if (start >= p->offset &&
813c7314be4SMiquel Raynal (start < (p->offset + p->size)))
814ff94bc40SHeiko Schocher goto err_inv;
815ff94bc40SHeiko Schocher
816c7314be4SMiquel Raynal if (end >= p->offset &&
817c7314be4SMiquel Raynal (end < (p->offset + p->size)))
818ff94bc40SHeiko Schocher goto err_inv;
819ff94bc40SHeiko Schocher }
820ff94bc40SHeiko Schocher
821c7314be4SMiquel Raynal list_add_tail(&new->node, &master->partitions);
822ff94bc40SHeiko Schocher mutex_unlock(&mtd_partitions_mutex);
823ff94bc40SHeiko Schocher
824c7314be4SMiquel Raynal add_mtd_device(new);
825ff94bc40SHeiko Schocher
826ff94bc40SHeiko Schocher return ret;
827ff94bc40SHeiko Schocher err_inv:
828ff94bc40SHeiko Schocher mutex_unlock(&mtd_partitions_mutex);
829ff94bc40SHeiko Schocher free_partition(new);
830ff94bc40SHeiko Schocher return -EINVAL;
831ff94bc40SHeiko Schocher }
832ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_add_partition);
833ff94bc40SHeiko Schocher
mtd_del_partition(struct mtd_info * master,int partno)834ff94bc40SHeiko Schocher int mtd_del_partition(struct mtd_info *master, int partno)
835ff94bc40SHeiko Schocher {
836c7314be4SMiquel Raynal struct mtd_info *slave, *next;
837ff94bc40SHeiko Schocher int ret = -EINVAL;
838ff94bc40SHeiko Schocher
839ff94bc40SHeiko Schocher mutex_lock(&mtd_partitions_mutex);
840c7314be4SMiquel Raynal list_for_each_entry_safe(slave, next, &master->partitions, node)
841c7314be4SMiquel Raynal if (slave->index == partno) {
842c7314be4SMiquel Raynal ret = del_mtd_device(slave);
843ff94bc40SHeiko Schocher if (ret < 0)
844ff94bc40SHeiko Schocher break;
845ff94bc40SHeiko Schocher
846c7314be4SMiquel Raynal list_del(&slave->node);
847ff94bc40SHeiko Schocher free_partition(slave);
848ff94bc40SHeiko Schocher break;
849ff94bc40SHeiko Schocher }
850ff94bc40SHeiko Schocher mutex_unlock(&mtd_partitions_mutex);
851ff94bc40SHeiko Schocher
852ff94bc40SHeiko Schocher return ret;
853ff94bc40SHeiko Schocher }
854ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_del_partition);
855ddf7bcfaSHeiko Schocher #endif
856ff94bc40SHeiko Schocher
8578d2effeaSStefan Roese /*
8588d2effeaSStefan Roese * This function, given a master MTD object and a partition table, creates
8598d2effeaSStefan Roese * and registers slave MTD objects which are bound to the master according to
8608d2effeaSStefan Roese * the partition definitions.
8618d2effeaSStefan Roese *
8628d2effeaSStefan Roese * We don't register the master, or expect the caller to have done so,
8638d2effeaSStefan Roese * for reasons of data integrity.
8648d2effeaSStefan Roese */
8658d2effeaSStefan Roese
add_mtd_partitions(struct mtd_info * master,const struct mtd_partition * parts,int nbparts)8668d2effeaSStefan Roese int add_mtd_partitions(struct mtd_info *master,
8678d2effeaSStefan Roese const struct mtd_partition *parts,
8688d2effeaSStefan Roese int nbparts)
8698d2effeaSStefan Roese {
870c7314be4SMiquel Raynal struct mtd_info *slave;
8718d2effeaSStefan Roese uint64_t cur_offset = 0;
8728d2effeaSStefan Roese int i;
8738d2effeaSStefan Roese
874147162daSJoe Hershberger debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
8758d2effeaSStefan Roese
8768d2effeaSStefan Roese for (i = 0; i < nbparts; i++) {
877ff94bc40SHeiko Schocher slave = allocate_partition(master, parts + i, i, cur_offset);
878ff94bc40SHeiko Schocher if (IS_ERR(slave))
879ff94bc40SHeiko Schocher return PTR_ERR(slave);
880ff94bc40SHeiko Schocher
881ff94bc40SHeiko Schocher mutex_lock(&mtd_partitions_mutex);
882c7314be4SMiquel Raynal list_add_tail(&slave->node, &master->partitions);
883ff94bc40SHeiko Schocher mutex_unlock(&mtd_partitions_mutex);
884ff94bc40SHeiko Schocher
885c7314be4SMiquel Raynal add_mtd_device(slave);
886ff94bc40SHeiko Schocher
887c7314be4SMiquel Raynal cur_offset = slave->offset + slave->size;
888e29c22f5SKyungmin Park }
889e29c22f5SKyungmin Park
890e29c22f5SKyungmin Park return 0;
891e29c22f5SKyungmin Park }
892ff94bc40SHeiko Schocher
893ff94bc40SHeiko Schocher #ifndef __UBOOT__
894ff94bc40SHeiko Schocher static DEFINE_SPINLOCK(part_parser_lock);
895ff94bc40SHeiko Schocher static LIST_HEAD(part_parsers);
896ff94bc40SHeiko Schocher
get_partition_parser(const char * name)897ff94bc40SHeiko Schocher static struct mtd_part_parser *get_partition_parser(const char *name)
898ff94bc40SHeiko Schocher {
899ff94bc40SHeiko Schocher struct mtd_part_parser *p, *ret = NULL;
900ff94bc40SHeiko Schocher
901ff94bc40SHeiko Schocher spin_lock(&part_parser_lock);
902ff94bc40SHeiko Schocher
903ff94bc40SHeiko Schocher list_for_each_entry(p, &part_parsers, list)
904ff94bc40SHeiko Schocher if (!strcmp(p->name, name) && try_module_get(p->owner)) {
905ff94bc40SHeiko Schocher ret = p;
906ff94bc40SHeiko Schocher break;
907ff94bc40SHeiko Schocher }
908ff94bc40SHeiko Schocher
909ff94bc40SHeiko Schocher spin_unlock(&part_parser_lock);
910ff94bc40SHeiko Schocher
911ff94bc40SHeiko Schocher return ret;
912ff94bc40SHeiko Schocher }
913ff94bc40SHeiko Schocher
914ff94bc40SHeiko Schocher #define put_partition_parser(p) do { module_put((p)->owner); } while (0)
915ff94bc40SHeiko Schocher
register_mtd_parser(struct mtd_part_parser * p)916ff94bc40SHeiko Schocher void register_mtd_parser(struct mtd_part_parser *p)
917ff94bc40SHeiko Schocher {
918ff94bc40SHeiko Schocher spin_lock(&part_parser_lock);
919ff94bc40SHeiko Schocher list_add(&p->list, &part_parsers);
920ff94bc40SHeiko Schocher spin_unlock(&part_parser_lock);
921ff94bc40SHeiko Schocher }
922ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(register_mtd_parser);
923ff94bc40SHeiko Schocher
deregister_mtd_parser(struct mtd_part_parser * p)924ff94bc40SHeiko Schocher void deregister_mtd_parser(struct mtd_part_parser *p)
925ff94bc40SHeiko Schocher {
926ff94bc40SHeiko Schocher spin_lock(&part_parser_lock);
927ff94bc40SHeiko Schocher list_del(&p->list);
928ff94bc40SHeiko Schocher spin_unlock(&part_parser_lock);
929ff94bc40SHeiko Schocher }
930ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(deregister_mtd_parser);
931ff94bc40SHeiko Schocher
932ff94bc40SHeiko Schocher /*
933ff94bc40SHeiko Schocher * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
934ff94bc40SHeiko Schocher * are changing this array!
935ff94bc40SHeiko Schocher */
936ff94bc40SHeiko Schocher static const char * const default_mtd_part_types[] = {
937ff94bc40SHeiko Schocher "cmdlinepart",
938ff94bc40SHeiko Schocher "ofpart",
939ff94bc40SHeiko Schocher NULL
940ff94bc40SHeiko Schocher };
941ff94bc40SHeiko Schocher
942ff94bc40SHeiko Schocher /**
943ff94bc40SHeiko Schocher * parse_mtd_partitions - parse MTD partitions
944ff94bc40SHeiko Schocher * @master: the master partition (describes whole MTD device)
945ff94bc40SHeiko Schocher * @types: names of partition parsers to try or %NULL
946ff94bc40SHeiko Schocher * @pparts: array of partitions found is returned here
947ff94bc40SHeiko Schocher * @data: MTD partition parser-specific data
948ff94bc40SHeiko Schocher *
949ff94bc40SHeiko Schocher * This function tries to find partition on MTD device @master. It uses MTD
950ff94bc40SHeiko Schocher * partition parsers, specified in @types. However, if @types is %NULL, then
951ff94bc40SHeiko Schocher * the default list of parsers is used. The default list contains only the
952ff94bc40SHeiko Schocher * "cmdlinepart" and "ofpart" parsers ATM.
953ff94bc40SHeiko Schocher * Note: If there are more then one parser in @types, the kernel only takes the
954ff94bc40SHeiko Schocher * partitions parsed out by the first parser.
955ff94bc40SHeiko Schocher *
956ff94bc40SHeiko Schocher * This function may return:
957ff94bc40SHeiko Schocher * o a negative error code in case of failure
958ff94bc40SHeiko Schocher * o zero if no partitions were found
959ff94bc40SHeiko Schocher * o a positive number of found partitions, in which case on exit @pparts will
960ff94bc40SHeiko Schocher * point to an array containing this number of &struct mtd_info objects.
961ff94bc40SHeiko Schocher */
parse_mtd_partitions(struct mtd_info * master,const char * const * types,struct mtd_partition ** pparts,struct mtd_part_parser_data * data)962ff94bc40SHeiko Schocher int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
963ff94bc40SHeiko Schocher struct mtd_partition **pparts,
964ff94bc40SHeiko Schocher struct mtd_part_parser_data *data)
965ff94bc40SHeiko Schocher {
966ff94bc40SHeiko Schocher struct mtd_part_parser *parser;
967ff94bc40SHeiko Schocher int ret = 0;
968ff94bc40SHeiko Schocher
969ff94bc40SHeiko Schocher if (!types)
970ff94bc40SHeiko Schocher types = default_mtd_part_types;
971ff94bc40SHeiko Schocher
972ff94bc40SHeiko Schocher for ( ; ret <= 0 && *types; types++) {
973ff94bc40SHeiko Schocher parser = get_partition_parser(*types);
974ff94bc40SHeiko Schocher if (!parser && !request_module("%s", *types))
975ff94bc40SHeiko Schocher parser = get_partition_parser(*types);
976ff94bc40SHeiko Schocher if (!parser)
977ff94bc40SHeiko Schocher continue;
978ff94bc40SHeiko Schocher ret = (*parser->parse_fn)(master, pparts, data);
979ff94bc40SHeiko Schocher put_partition_parser(parser);
980ff94bc40SHeiko Schocher if (ret > 0) {
981ff94bc40SHeiko Schocher printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
982ff94bc40SHeiko Schocher ret, parser->name, master->name);
983ff94bc40SHeiko Schocher break;
984ff94bc40SHeiko Schocher }
985ff94bc40SHeiko Schocher }
986ff94bc40SHeiko Schocher return ret;
987ff94bc40SHeiko Schocher }
988ff94bc40SHeiko Schocher #endif
989ff94bc40SHeiko Schocher
990ff94bc40SHeiko Schocher /* Returns the size of the entire flash chip */
mtd_get_device_size(const struct mtd_info * mtd)991ff94bc40SHeiko Schocher uint64_t mtd_get_device_size(const struct mtd_info *mtd)
992ff94bc40SHeiko Schocher {
993c7314be4SMiquel Raynal if (mtd_is_partition(mtd))
994c7314be4SMiquel Raynal return mtd->parent->size;
995ff94bc40SHeiko Schocher
996c7314be4SMiquel Raynal return mtd->size;
997ff94bc40SHeiko Schocher }
998ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_get_device_size);
999