• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/mtd/
1/*
2 * Read flash partition table from command line
3 *
4 * Copyright �� 2002      SYSGO Real-Time Solutions GmbH
5 * Copyright �� 2002-2010 David Woodhouse <dwmw2@infradead.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 *
21 * The format for the command line is as follows:
22 *
23 * mtdparts=<mtddef>[;<mtddef]
24 * <mtddef>  := <mtd-id>:<partdef>[,<partdef>]
25 *              where <mtd-id> is the name from the "cat /proc/mtd" command
26 * <partdef> := <size>[@offset][<name>][ro][lk]
27 * <mtd-id>  := unique name used in mapping driver/device (mtd->name)
28 * <size>    := standard linux memsize OR "-" to denote all remaining space
29 * <name>    := '(' NAME ')'
30 *
31 * Examples:
32 *
33 * 1 NOR Flash, with 1 single writable partition:
34 * edb7312-nor:-
35 *
36 * 1 NOR Flash with 2 partitions, 1 NAND with one
37 * edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
38 */
39
40#include <linux/kernel.h>
41#include <linux/slab.h>
42
43#include <linux/mtd/mtd.h>
44#include <linux/mtd/partitions.h>
45#include <linux/bootmem.h>
46
47/* error message prefix */
48#define ERRP "mtd: "
49
50/* debug macro */
51#define dbg(x)
52
53
54/* special size referring to all the remaining space in a partition */
55#define SIZE_REMAINING UINT_MAX
56#define OFFSET_CONTINUOUS UINT_MAX
57
58struct cmdline_mtd_partition {
59	struct cmdline_mtd_partition *next;
60	char *mtd_id;
61	int num_parts;
62	struct mtd_partition *parts;
63};
64
65/* mtdpart_setup() parses into here */
66static struct cmdline_mtd_partition *partitions;
67
68/* the command line passed to mtdpart_setupd() */
69static char *cmdline;
70static int cmdline_parsed = 0;
71
72/*
73 * Parse one partition definition for an MTD. Since there can be many
74 * comma separated partition definitions, this function calls itself
75 * recursively until no more partition definitions are found. Nice side
76 * effect: the memory to keep the mtd_partition structs and the names
77 * is allocated upon the last definition being found. At that point the
78 * syntax has been verified ok.
79 */
80static struct mtd_partition * newpart(char *s,
81                                      char **retptr,
82                                      int *num_parts,
83                                      int this_part,
84                                      unsigned char **extra_mem_ptr,
85                                      int extra_mem_size)
86{
87	struct mtd_partition *parts;
88	unsigned long size;
89	unsigned long offset = OFFSET_CONTINUOUS;
90	char *name;
91	int name_len;
92	unsigned char *extra_mem;
93	char delim;
94	unsigned int mask_flags;
95
96	/* fetch the partition size */
97	if (*s == '-')
98	{	/* assign all remaining space to this partition */
99		size = SIZE_REMAINING;
100		s++;
101	}
102	else
103	{
104		size = memparse(s, &s);
105		if (size < PAGE_SIZE)
106		{
107			printk(KERN_ERR ERRP "partition size too small (%lx)\n", size);
108			return NULL;
109		}
110	}
111
112	/* fetch partition name and flags */
113	mask_flags = 0; /* this is going to be a regular partition */
114	delim = 0;
115        /* check for offset */
116        if (*s == '@')
117	{
118                s++;
119                offset = memparse(s, &s);
120        }
121        /* now look for name */
122	if (*s == '(')
123	{
124		delim = ')';
125	}
126
127	if (delim)
128	{
129		char *p;
130
131	    	name = ++s;
132		p = strchr(name, delim);
133		if (!p)
134		{
135			printk(KERN_ERR ERRP "no closing %c found in partition name\n", delim);
136			return NULL;
137		}
138		name_len = p - name;
139		s = p + 1;
140	}
141	else
142	{
143	    	name = NULL;
144		name_len = 13; /* Partition_000 */
145	}
146
147	/* record name length for memory allocation later */
148	extra_mem_size += name_len + 1;
149
150        /* test for options */
151        if (strncmp(s, "ro", 2) == 0)
152	{
153		mask_flags |= MTD_WRITEABLE;
154		s += 2;
155        }
156
157        /* if lk is found do NOT unlock the MTD partition*/
158        if (strncmp(s, "lk", 2) == 0)
159	{
160		mask_flags |= MTD_POWERUP_LOCK;
161		s += 2;
162        }
163
164	/* test if more partitions are following */
165	if (*s == ',')
166	{
167		if (size == SIZE_REMAINING)
168		{
169			printk(KERN_ERR ERRP "no partitions allowed after a fill-up partition\n");
170			return NULL;
171		}
172		/* more partitions follow, parse them */
173		parts = newpart(s + 1, &s, num_parts, this_part + 1,
174				&extra_mem, extra_mem_size);
175		if (!parts)
176			return NULL;
177	}
178	else
179	{	/* this is the last partition: allocate space for all */
180		int alloc_size;
181
182		*num_parts = this_part + 1;
183		alloc_size = *num_parts * sizeof(struct mtd_partition) +
184			     extra_mem_size;
185		parts = kzalloc(alloc_size, GFP_KERNEL);
186		if (!parts)
187		{
188			printk(KERN_ERR ERRP "out of memory\n");
189			return NULL;
190		}
191		extra_mem = (unsigned char *)(parts + *num_parts);
192	}
193	/* enter this partition (offset will be calculated later if it is zero at this point) */
194	parts[this_part].size = size;
195	parts[this_part].offset = offset;
196	parts[this_part].mask_flags = mask_flags;
197	if (name)
198	{
199		strlcpy(extra_mem, name, name_len + 1);
200	}
201	else
202	{
203		sprintf(extra_mem, "Partition_%03d", this_part);
204	}
205	parts[this_part].name = extra_mem;
206	extra_mem += name_len + 1;
207
208	dbg(("partition %d: name <%s>, offset %llx, size %llx, mask flags %x\n",
209	     this_part,
210	     parts[this_part].name,
211	     parts[this_part].offset,
212	     parts[this_part].size,
213	     parts[this_part].mask_flags));
214
215	/* return (updated) pointer to extra_mem memory */
216	if (extra_mem_ptr)
217	  *extra_mem_ptr = extra_mem;
218
219	/* return (updated) pointer command line string */
220	*retptr = s;
221
222	/* return partition table */
223	return parts;
224}
225
226/*
227 * Parse the command line.
228 */
229static int mtdpart_setup_real(char *s)
230{
231	cmdline_parsed = 1;
232
233	for( ; s != NULL; )
234	{
235		struct cmdline_mtd_partition *this_mtd;
236		struct mtd_partition *parts;
237	    	int mtd_id_len;
238		int num_parts;
239		char *p, *mtd_id;
240
241	    	mtd_id = s;
242		/* fetch <mtd-id> */
243		if (!(p = strchr(s, ':')))
244		{
245			printk(KERN_ERR ERRP "no mtd-id\n");
246			return 0;
247		}
248		mtd_id_len = p - mtd_id;
249
250		dbg(("parsing <%s>\n", p+1));
251
252		/*
253		 * parse one mtd. have it reserve memory for the
254		 * struct cmdline_mtd_partition and the mtd-id string.
255		 */
256		parts = newpart(p + 1,		/* cmdline */
257				&s,		/* out: updated cmdline ptr */
258				&num_parts,	/* out: number of parts */
259				0,		/* first partition */
260				(unsigned char**)&this_mtd, /* out: extra mem */
261				mtd_id_len + 1 + sizeof(*this_mtd) +
262				sizeof(void*)-1 /*alignment*/);
263		if(!parts)
264		{
265			/*
266			 * An error occurred. We're either:
267			 * a) out of memory, or
268			 * b) in the middle of the partition spec
269			 * Either way, this mtd is hosed and we're
270			 * unlikely to succeed in parsing any more
271			 */
272			 return 0;
273		 }
274
275		/* align this_mtd */
276		this_mtd = (struct cmdline_mtd_partition *)
277			ALIGN((unsigned long)this_mtd, sizeof(void*));
278		/* enter results */
279		this_mtd->parts = parts;
280		this_mtd->num_parts = num_parts;
281		this_mtd->mtd_id = (char*)(this_mtd + 1);
282		strlcpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1);
283
284		/* link into chain */
285		this_mtd->next = partitions;
286		partitions = this_mtd;
287
288		dbg(("mtdid=<%s> num_parts=<%d>\n",
289		     this_mtd->mtd_id, this_mtd->num_parts));
290
291
292		/* EOS - we're done */
293		if (*s == 0)
294			break;
295
296		/* does another spec follow? */
297		if (*s != ';')
298		{
299			printk(KERN_ERR ERRP "bad character after partition (%c)\n", *s);
300			return 0;
301		}
302		s++;
303	}
304	return 1;
305}
306
307/*
308 * Main function to be called from the MTD mapping driver/device to
309 * obtain the partitioning information. At this point the command line
310 * arguments will actually be parsed and turned to struct mtd_partition
311 * information. It returns partitions for the requested mtd device, or
312 * the first one in the chain if a NULL mtd_id is passed in.
313 */
314static int parse_cmdline_partitions(struct mtd_info *master,
315                             struct mtd_partition **pparts,
316                             unsigned long origin)
317{
318	unsigned long offset;
319	int i;
320	struct cmdline_mtd_partition *part;
321	const char *mtd_id = master->name;
322
323	/* parse command line */
324	if (!cmdline_parsed)
325		mtdpart_setup_real(cmdline);
326
327	for(part = partitions; part; part = part->next)
328	{
329		if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
330		{
331			for(i = 0, offset = 0; i < part->num_parts; i++)
332			{
333				if (part->parts[i].offset == OFFSET_CONTINUOUS)
334				  part->parts[i].offset = offset;
335				else
336				  offset = part->parts[i].offset;
337				if (part->parts[i].size == SIZE_REMAINING)
338				  part->parts[i].size = master->size - offset;
339				if (offset + part->parts[i].size > master->size)
340				{
341					printk(KERN_WARNING ERRP
342					       "%s: partitioning exceeds flash size, truncating\n",
343					       part->mtd_id);
344					part->parts[i].size = master->size - offset;
345					part->num_parts = i;
346				}
347				offset += part->parts[i].size;
348			}
349			*pparts = kmemdup(part->parts,
350					sizeof(*part->parts) * part->num_parts,
351					GFP_KERNEL);
352			if (!*pparts)
353				return -ENOMEM;
354			return part->num_parts;
355		}
356	}
357	return 0;
358}
359
360
361/*
362 * This is the handler for our kernel parameter, called from
363 * main.c::checksetup(). Note that we can not yet kmalloc() anything,
364 * so we only save the commandline for later processing.
365 *
366 * This function needs to be visible for bootloaders.
367 */
368static int mtdpart_setup(char *s)
369{
370	cmdline = s;
371	return 1;
372}
373
374__setup("mtdparts=", mtdpart_setup);
375
376static struct mtd_part_parser cmdline_parser = {
377	.owner = THIS_MODULE,
378	.parse_fn = parse_cmdline_partitions,
379	.name = "cmdlinepart",
380};
381
382static int __init cmdline_parser_init(void)
383{
384	return register_mtd_parser(&cmdline_parser);
385}
386
387module_init(cmdline_parser_init);
388
389MODULE_LICENSE("GPL");
390MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>");
391MODULE_DESCRIPTION("Command line configuration of MTD partitions");
392