2 * Read flash partition table from command line
4 * Copyright 2002 SYSGO Real-Time Solutions GmbH
6 * The format for the command line is as follows:
8 * mtdparts=<mtddef>[;<mtddef]
9 * <mtddef> := <mtd-id>:<partdef>[,<partdef>]
10 * <partdef> := <size>[@offset][<name>][ro][lk]
11 * <mtd-id> := unique name used in mapping driver/device (mtd->name)
12 * <size> := standard linux memsize OR "-" to denote all remaining space
13 * <name> := '(' NAME ')'
17 * 1 NOR Flash, with 1 single writable partition:
20 * 1 NOR Flash with 2 partitions, 1 NAND with one
21 * edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
27 #include <linux/mtd/mtd.h>
28 #include <linux/mtd/partitions.h>
29 #include <linux/bootmem.h>
31 /* error message prefix */
36 #define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0)
42 /* special size referring to all the remaining space in a partition */
43 #define SIZE_REMAINING UINT_MAX
44 #define OFFSET_CONTINUOUS UINT_MAX
46 struct cmdline_mtd_partition
{
47 struct cmdline_mtd_partition
*next
;
50 struct mtd_partition
*parts
;
53 /* mtdpart_setup() parses into here */
54 static struct cmdline_mtd_partition
*partitions
;
56 /* the command line passed to mtdpart_setupd() */
58 static int cmdline_parsed
= 0;
61 * Parse one partition definition for an MTD. Since there can be many
62 * comma separated partition definitions, this function calls itself
63 * recursively until no more partition definitions are found. Nice side
64 * effect: the memory to keep the mtd_partition structs and the names
65 * is allocated upon the last definition being found. At that point the
66 * syntax has been verified ok.
68 static struct mtd_partition
* newpart(char *s
,
72 unsigned char **extra_mem_ptr
,
75 struct mtd_partition
*parts
;
77 unsigned long offset
= OFFSET_CONTINUOUS
;
80 unsigned char *extra_mem
;
82 unsigned int mask_flags
;
84 /* fetch the partition size */
86 { /* assign all remaining space to this partition */
87 size
= SIZE_REMAINING
;
92 size
= memparse(s
, &s
);
95 printk(KERN_ERR ERRP
"partition size too small (%lx)\n", size
);
100 /* fetch partition name and flags */
101 mask_flags
= 0; /* this is going to be a regular partition */
103 /* check for offset */
107 offset
= memparse(s
, &s
);
109 /* now look for name */
120 p
= strchr(name
, delim
);
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 /* if lk is found do NOT unlock the MTD partition*/
146 if (strncmp(s
, "lk", 2) == 0)
148 mask_flags
|= MTD_POWERUP_LOCK
;
152 /* test if more partitions are following */
155 if (size
== SIZE_REMAINING
)
157 printk(KERN_ERR ERRP
"no partitions allowed after a fill-up partition\n");
160 /* more partitions follow, parse them */
161 parts
= newpart(s
+ 1, &s
, num_parts
, this_part
+ 1,
162 &extra_mem
, extra_mem_size
);
167 { /* this is the last partition: allocate space for all */
170 *num_parts
= this_part
+ 1;
171 alloc_size
= *num_parts
* sizeof(struct mtd_partition
) +
173 parts
= kzalloc(alloc_size
, GFP_KERNEL
);
176 printk(KERN_ERR ERRP
"out of memory\n");
179 extra_mem
= (unsigned char *)(parts
+ *num_parts
);
181 /* enter this partition (offset will be calculated later if it is zero at this point) */
182 parts
[this_part
].size
= size
;
183 parts
[this_part
].offset
= offset
;
184 parts
[this_part
].mask_flags
= mask_flags
;
187 strlcpy(extra_mem
, name
, name_len
+ 1);
191 sprintf(extra_mem
, "Partition_%03d", this_part
);
193 parts
[this_part
].name
= extra_mem
;
194 extra_mem
+= name_len
+ 1;
196 dbg(("partition %d: name <%s>, offset %x, size %x, mask flags %x\n",
198 parts
[this_part
].name
,
199 parts
[this_part
].offset
,
200 parts
[this_part
].size
,
201 parts
[this_part
].mask_flags
));
203 /* return (updated) pointer to extra_mem memory */
205 *extra_mem_ptr
= extra_mem
;
207 /* return (updated) pointer command line string */
210 /* return partition table */
215 * Parse the command line.
217 static int mtdpart_setup_real(char *s
)
223 struct cmdline_mtd_partition
*this_mtd
;
224 struct mtd_partition
*parts
;
231 if (!(p
= strchr(s
, ':')))
233 printk(KERN_ERR ERRP
"no mtd-id\n");
236 mtd_id_len
= p
- mtd_id
;
238 dbg(("parsing <%s>\n", p
+1));
241 * parse one mtd. have it reserve memory for the
242 * struct cmdline_mtd_partition and the mtd-id string.
244 parts
= newpart(p
+ 1, /* cmdline */
245 &s
, /* out: updated cmdline ptr */
246 &num_parts
, /* out: number of parts */
247 0, /* first partition */
248 (unsigned char**)&this_mtd
, /* out: extra mem */
249 mtd_id_len
+ 1 + sizeof(*this_mtd
) +
250 sizeof(void*)-1 /*alignment*/);
254 * An error occurred. We're either:
255 * a) out of memory, or
256 * b) in the middle of the partition spec
257 * Either way, this mtd is hosed and we're
258 * unlikely to succeed in parsing any more
264 this_mtd
= (struct cmdline_mtd_partition
*)
265 ALIGN((unsigned long)this_mtd
, sizeof(void*));
267 this_mtd
->parts
= parts
;
268 this_mtd
->num_parts
= num_parts
;
269 this_mtd
->mtd_id
= (char*)(this_mtd
+ 1);
270 strlcpy(this_mtd
->mtd_id
, mtd_id
, mtd_id_len
+ 1);
272 /* link into chain */
273 this_mtd
->next
= partitions
;
274 partitions
= this_mtd
;
276 dbg(("mtdid=<%s> num_parts=<%d>\n",
277 this_mtd
->mtd_id
, this_mtd
->num_parts
));
280 /* EOS - we're done */
284 /* does another spec follow? */
287 printk(KERN_ERR ERRP
"bad character after partition (%c)\n", *s
);
296 * Main function to be called from the MTD mapping driver/device to
297 * obtain the partitioning information. At this point the command line
298 * arguments will actually be parsed and turned to struct mtd_partition
299 * information. It returns partitions for the requested mtd device, or
300 * the first one in the chain if a NULL mtd_id is passed in.
302 static int parse_cmdline_partitions(struct mtd_info
*master
,
303 struct mtd_partition
**pparts
,
304 unsigned long origin
)
306 unsigned long offset
;
308 struct cmdline_mtd_partition
*part
;
309 const char *mtd_id
= master
->name
;
311 /* parse command line */
313 mtdpart_setup_real(cmdline
);
315 for(part
= partitions
; part
; part
= part
->next
)
317 if ((!mtd_id
) || (!strcmp(part
->mtd_id
, mtd_id
)))
319 for(i
= 0, offset
= 0; i
< part
->num_parts
; i
++)
321 if (part
->parts
[i
].offset
== OFFSET_CONTINUOUS
)
322 part
->parts
[i
].offset
= offset
;
324 offset
= part
->parts
[i
].offset
;
325 if (part
->parts
[i
].size
== SIZE_REMAINING
)
326 part
->parts
[i
].size
= master
->size
- offset
;
327 if (offset
+ part
->parts
[i
].size
> master
->size
)
329 printk(KERN_WARNING ERRP
330 "%s: partitioning exceeds flash size, truncating\n",
332 part
->parts
[i
].size
= master
->size
- offset
;
335 offset
+= part
->parts
[i
].size
;
337 *pparts
= part
->parts
;
338 return part
->num_parts
;
346 * This is the handler for our kernel parameter, called from
347 * main.c::checksetup(). Note that we can not yet kmalloc() anything,
348 * so we only save the commandline for later processing.
350 * This function needs to be visible for bootloaders.
352 static int mtdpart_setup(char *s
)
358 __setup("mtdparts=", mtdpart_setup
);
360 static struct mtd_part_parser cmdline_parser
= {
361 .owner
= THIS_MODULE
,
362 .parse_fn
= parse_cmdline_partitions
,
363 .name
= "cmdlinepart",
366 static int __init
cmdline_parser_init(void)
368 return register_mtd_parser(&cmdline_parser
);
371 module_init(cmdline_parser_init
);
373 MODULE_LICENSE("GPL");
374 MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>");
375 MODULE_DESCRIPTION("Command line configuration of MTD partitions");