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 * where <mtd-id> is the name from the "cat /proc/mtd" command
11 * <partdef> := <size>[@offset][<name>][ro][lk]
12 * <mtd-id> := unique name used in mapping driver/device (mtd->name)
13 * <size> := standard linux memsize OR "-" to denote all remaining space
14 * <name> := '(' NAME ')'
18 * 1 NOR Flash, with 1 single writable partition:
21 * 1 NOR Flash with 2 partitions, 1 NAND with one
22 * edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/partitions.h>
30 #include <linux/bootmem.h>
32 /* error message prefix */
37 #define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0)
43 /* special size referring to all the remaining space in a partition */
44 #define SIZE_REMAINING UINT_MAX
45 #define OFFSET_CONTINUOUS UINT_MAX
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 = OFFSET_CONTINUOUS;
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 p = strchr(name, delim);
124 printk(KERN_ERR ERRP "no closing %c found in partition name\n", delim);
133 name_len = 13; /* Partition_000 */
136 /* record name length for memory allocation later */
137 extra_mem_size += name_len + 1;
139 /* test for options */
140 if (strncmp(s, "ro", 2) == 0)
142 mask_flags |= MTD_WRITEABLE;
146 /* if lk is found do NOT unlock the MTD partition*/
147 if (strncmp(s, "lk", 2) == 0)
149 mask_flags |= MTD_POWERUP_LOCK;
153 /* test if more partitions are following */
156 if (size == SIZE_REMAINING)
158 printk(KERN_ERR ERRP "no partitions allowed after a fill-up partition\n");
161 /* more partitions follow, parse them */
162 parts = newpart(s + 1, &s, num_parts, this_part + 1,
163 &extra_mem, extra_mem_size);
168 { /* this is the last partition: allocate space for all */
171 *num_parts = this_part + 1;
172 alloc_size = *num_parts * sizeof(struct mtd_partition) +
174 parts = kzalloc(alloc_size, GFP_KERNEL);
177 printk(KERN_ERR ERRP "out of memory\n");
180 extra_mem = (unsigned char *)(parts + *num_parts);
182 /* enter this partition (offset will be calculated later if it is zero at this point) */
183 parts[this_part].size = size;
184 parts[this_part].offset = offset;
185 parts[this_part].mask_flags = mask_flags;
188 strlcpy(extra_mem, name, name_len + 1);
192 sprintf(extra_mem, "Partition_%03d", this_part);
194 parts[this_part].name = extra_mem;
195 extra_mem += name_len + 1;
197 dbg(("partition %d: name <%s>, offset %x, size %x, mask flags %x\n",
199 parts[this_part].name,
200 parts[this_part].offset,
201 parts[this_part].size,
202 parts[this_part].mask_flags));
204 /* return (updated) pointer to extra_mem memory */
206 *extra_mem_ptr = extra_mem;
208 /* return (updated) pointer command line string */
211 /* return partition table */
216 * Parse the command line.
218 static int mtdpart_setup_real(char *s)
224 struct cmdline_mtd_partition *this_mtd;
225 struct mtd_partition *parts;
232 if (!(p = strchr(s, ':')))
234 printk(KERN_ERR ERRP "no mtd-id\n");
237 mtd_id_len = p - mtd_id;
239 dbg(("parsing <%s>\n", p+1));
242 * parse one mtd. have it reserve memory for the
243 * struct cmdline_mtd_partition and the mtd-id string.
245 parts = newpart(p + 1, /* cmdline */
246 &s, /* out: updated cmdline ptr */
247 &num_parts, /* out: number of parts */
248 0, /* first partition */
249 (unsigned char**)&this_mtd, /* out: extra mem */
250 mtd_id_len + 1 + sizeof(*this_mtd) +
251 sizeof(void*)-1 /*alignment*/);
255 * An error occurred. We're either:
256 * a) out of memory, or
257 * b) in the middle of the partition spec
258 * Either way, this mtd is hosed and we're
259 * unlikely to succeed in parsing any more
265 this_mtd = (struct cmdline_mtd_partition *)
266 ALIGN((unsigned long)this_mtd, sizeof(void*));
268 this_mtd->parts = parts;
269 this_mtd->num_parts = num_parts;
270 this_mtd->mtd_id = (char*)(this_mtd + 1);
271 strlcpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1);
273 /* link into chain */
274 this_mtd->next = partitions;
275 partitions = this_mtd;
277 dbg(("mtdid=<%s> num_parts=<%d>\n",
278 this_mtd->mtd_id, this_mtd->num_parts));
281 /* EOS - we're done */
285 /* does another spec follow? */
288 printk(KERN_ERR ERRP "bad character after partition (%c)\n", *s);
297 * Main function to be called from the MTD mapping driver/device to
298 * obtain the partitioning information. At this point the command line
299 * arguments will actually be parsed and turned to struct mtd_partition
300 * information. It returns partitions for the requested mtd device, or
301 * the first one in the chain if a NULL mtd_id is passed in.
303 static int parse_cmdline_partitions(struct mtd_info *master,
304 struct mtd_partition **pparts,
305 unsigned long origin)
307 unsigned long offset;
309 struct cmdline_mtd_partition *part;
310 const char *mtd_id = master->name;
312 /* parse command line */
314 mtdpart_setup_real(cmdline);
316 for(part = partitions; part; part = part->next)
318 if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
320 for(i = 0, offset = 0; i < part->num_parts; i++)
322 if (part->parts[i].offset == OFFSET_CONTINUOUS)
323 part->parts[i].offset = offset;
325 offset = part->parts[i].offset;
326 if (part->parts[i].size == SIZE_REMAINING)
327 part->parts[i].size = master->size - offset;
328 if (offset + part->parts[i].size > master->size)
330 printk(KERN_WARNING ERRP
331 "%s: partitioning exceeds flash size, truncating\n",
333 part->parts[i].size = master->size - offset;
336 offset += part->parts[i].size;
338 *pparts = part->parts;
339 return part->num_parts;
347 * This is the handler for our kernel parameter, called from
348 * main.c::checksetup(). Note that we can not yet kmalloc() anything,
349 * so we only save the commandline for later processing.
351 * This function needs to be visible for bootloaders.
353 static int mtdpart_setup(char *s)
359 __setup("mtdparts=", mtdpart_setup);
361 static struct mtd_part_parser cmdline_parser = {
362 .owner = THIS_MODULE,
363 .parse_fn = parse_cmdline_partitions,
364 .name = "cmdlinepart",
367 static int __init cmdline_parser_init(void)
369 return register_mtd_parser(&cmdline_parser);
372 module_init(cmdline_parser_init);
374 MODULE_LICENSE("GPL");
375 MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>");
376 MODULE_DESCRIPTION("Command line configuration of MTD partitions");