2 * $Id: cmdlinepart.c,v 1.18 2005/06/07 15:04:26 joern Exp $
4 * Read flash partition table from command line
6 * Copyright 2002 SYSGO Real-Time Solutions GmbH
8 * The format for the command line is as follows:
10 * mtdparts=<mtddef>[;<mtddef]
11 * <mtddef> := <mtd-id>:<partdef>[,<partdef>]
12 * <partdef> := <size>[@offset][<name>][ro]
13 * <mtd-id> := unique name used in mapping driver/device (mtd->name)
14 * <size> := standard linux memsize OR "-" to denote all remaining space
15 * <name> := '(' NAME ')'
19 * 1 NOR Flash, with 1 single writable partition:
22 * 1 NOR Flash with 2 partitions, 1 NAND with one
23 * edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/partitions.h>
31 #include <linux/bootmem.h>
33 /* error message prefix */
38 #define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0)
44 /* special size referring to all the remaining space in a partition */
45 #define SIZE_REMAINING 0xffffffff
47 struct cmdline_mtd_partition
{
48 struct cmdline_mtd_partition
*next
;
51 struct mtd_partition
*parts
;
54 /* mtdpart_setup() parses into here */
55 static struct cmdline_mtd_partition
*partitions
;
57 /* the command line passed to mtdpart_setupd() */
59 static int cmdline_parsed
= 0;
62 * Parse one partition definition for an MTD. Since there can be many
63 * comma separated partition definitions, this function calls itself
64 * recursively until no more partition definitions are found. Nice side
65 * effect: the memory to keep the mtd_partition structs and the names
66 * is allocated upon the last definition being found. At that point the
67 * syntax has been verified ok.
69 static struct mtd_partition
* newpart(char *s
,
73 unsigned char **extra_mem_ptr
,
76 struct mtd_partition
*parts
;
78 unsigned long offset
= 0;
81 unsigned char *extra_mem
;
83 unsigned int mask_flags
;
85 /* fetch the partition size */
87 { /* assign all remaining space to this partition */
88 size
= SIZE_REMAINING
;
93 size
= memparse(s
, &s
);
96 printk(KERN_ERR ERRP
"partition size too small (%lx)\n", size
);
101 /* fetch partition name and flags */
102 mask_flags
= 0; /* this is going to be a regular partition */
104 /* check for offset */
108 offset
= memparse(s
, &s
);
110 /* now look for name */
121 if ((p
= strchr(name
, delim
)) == 0)
123 printk(KERN_ERR ERRP
"no closing %c found in partition name\n", delim
);
132 name_len
= 13; /* Partition_000 */
135 /* record name length for memory allocation later */
136 extra_mem_size
+= name_len
+ 1;
138 /* test for options */
139 if (strncmp(s
, "ro", 2) == 0)
141 mask_flags
|= MTD_WRITEABLE
;
145 /* test if more partitions are following */
148 if (size
== SIZE_REMAINING
)
150 printk(KERN_ERR ERRP
"no partitions allowed after a fill-up partition\n");
153 /* more partitions follow, parse them */
154 if ((parts
= newpart(s
+ 1, &s
, num_parts
,
155 this_part
+ 1, &extra_mem
, extra_mem_size
)) == 0)
159 { /* this is the last partition: allocate space for all */
162 *num_parts
= this_part
+ 1;
163 alloc_size
= *num_parts
* sizeof(struct mtd_partition
) +
165 parts
= kmalloc(alloc_size
, GFP_KERNEL
);
168 printk(KERN_ERR ERRP
"out of memory\n");
171 memset(parts
, 0, alloc_size
);
172 extra_mem
= (unsigned char *)(parts
+ *num_parts
);
174 /* enter this partition (offset will be calculated later if it is zero at this point) */
175 parts
[this_part
].size
= size
;
176 parts
[this_part
].offset
= offset
;
177 parts
[this_part
].mask_flags
= mask_flags
;
180 strlcpy(extra_mem
, name
, name_len
+ 1);
184 sprintf(extra_mem
, "Partition_%03d", this_part
);
186 parts
[this_part
].name
= extra_mem
;
187 extra_mem
+= name_len
+ 1;
189 dbg(("partition %d: name <%s>, offset %x, size %x, mask flags %x\n",
191 parts
[this_part
].name
,
192 parts
[this_part
].offset
,
193 parts
[this_part
].size
,
194 parts
[this_part
].mask_flags
));
196 /* return (updated) pointer to extra_mem memory */
198 *extra_mem_ptr
= extra_mem
;
200 /* return (updated) pointer command line string */
203 /* return partition table */
208 * Parse the command line.
210 static int mtdpart_setup_real(char *s
)
216 struct cmdline_mtd_partition
*this_mtd
;
217 struct mtd_partition
*parts
;
224 if (!(p
= strchr(s
, ':')))
226 printk(KERN_ERR ERRP
"no mtd-id\n");
229 mtd_id_len
= p
- mtd_id
;
231 dbg(("parsing <%s>\n", p
+1));
234 * parse one mtd. have it reserve memory for the
235 * struct cmdline_mtd_partition and the mtd-id string.
237 parts
= newpart(p
+ 1, /* cmdline */
238 &s
, /* out: updated cmdline ptr */
239 &num_parts
, /* out: number of parts */
240 0, /* first partition */
241 (unsigned char**)&this_mtd
, /* out: extra mem */
242 mtd_id_len
+ 1 + sizeof(*this_mtd
) +
243 sizeof(void*)-1 /*alignment*/);
247 * An error occurred. We're either:
248 * a) out of memory, or
249 * b) in the middle of the partition spec
250 * Either way, this mtd is hosed and we're
251 * unlikely to succeed in parsing any more
257 this_mtd
= (struct cmdline_mtd_partition
*)
258 ALIGN((unsigned long)this_mtd
, sizeof(void*));
260 this_mtd
->parts
= parts
;
261 this_mtd
->num_parts
= num_parts
;
262 this_mtd
->mtd_id
= (char*)(this_mtd
+ 1);
263 strlcpy(this_mtd
->mtd_id
, mtd_id
, mtd_id_len
+ 1);
265 /* link into chain */
266 this_mtd
->next
= partitions
;
267 partitions
= this_mtd
;
269 dbg(("mtdid=<%s> num_parts=<%d>\n",
270 this_mtd
->mtd_id
, this_mtd
->num_parts
));
273 /* EOS - we're done */
277 /* does another spec follow? */
280 printk(KERN_ERR ERRP
"bad character after partition (%c)\n", *s
);
289 * Main function to be called from the MTD mapping driver/device to
290 * obtain the partitioning information. At this point the command line
291 * arguments will actually be parsed and turned to struct mtd_partition
292 * information. It returns partitions for the requested mtd device, or
293 * the first one in the chain if a NULL mtd_id is passed in.
295 static int parse_cmdline_partitions(struct mtd_info
*master
,
296 struct mtd_partition
**pparts
,
297 unsigned long origin
)
299 unsigned long offset
;
301 struct cmdline_mtd_partition
*part
;
302 char *mtd_id
= master
->name
;
307 /* parse command line */
309 mtdpart_setup_real(cmdline
);
311 for(part
= partitions
; part
; part
= part
->next
)
313 if ((!mtd_id
) || (!strcmp(part
->mtd_id
, mtd_id
)))
315 for(i
= 0, offset
= 0; i
< part
->num_parts
; i
++)
317 if (!part
->parts
[i
].offset
)
318 part
->parts
[i
].offset
= offset
;
320 offset
= part
->parts
[i
].offset
;
321 if (part
->parts
[i
].size
== SIZE_REMAINING
)
322 part
->parts
[i
].size
= master
->size
- offset
;
323 if (offset
+ part
->parts
[i
].size
> master
->size
)
325 printk(KERN_WARNING ERRP
326 "%s: partitioning exceeds flash size, truncating\n",
328 part
->parts
[i
].size
= master
->size
- offset
;
331 offset
+= part
->parts
[i
].size
;
333 *pparts
= part
->parts
;
334 return part
->num_parts
;
342 * This is the handler for our kernel parameter, called from
343 * main.c::checksetup(). Note that we can not yet kmalloc() anything,
344 * so we only save the commandline for later processing.
346 * This function needs to be visible for bootloaders.
348 int mtdpart_setup(char *s
)
354 __setup("mtdparts=", mtdpart_setup
);
356 static struct mtd_part_parser cmdline_parser
= {
357 .owner
= THIS_MODULE
,
358 .parse_fn
= parse_cmdline_partitions
,
359 .name
= "cmdlinepart",
362 static int __init
cmdline_parser_init(void)
364 return register_mtd_parser(&cmdline_parser
);
367 module_init(cmdline_parser_init
);
369 MODULE_LICENSE("GPL");
370 MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>");
371 MODULE_DESCRIPTION("Command line configuration of MTD partitions");