blob: 23fab14f1637cab5fb4ee175ff5eaea6db141915 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Thomas Gleixner97894cd2005-11-07 11:15:26 +00002 * $Id: cmdlinepart.c,v 1.19 2005/11/07 11:14:19 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Read flash partition table from command line
5 *
6 * Copyright 2002 SYSGO Real-Time Solutions GmbH
7 *
8 * The format for the command line is as follows:
Thomas Gleixner97894cd2005-11-07 11:15:26 +00009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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 ')'
Thomas Gleixner97894cd2005-11-07 11:15:26 +000016 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * Examples:
Thomas Gleixner97894cd2005-11-07 11:15:26 +000018 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * 1 NOR Flash, with 1 single writable partition:
20 * edb7312-nor:-
Thomas Gleixner97894cd2005-11-07 11:15:26 +000021 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * 1 NOR Flash with 2 partitions, 1 NAND with one
23 * edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
24 */
25
26#include <linux/kernel.h>
27#include <linux/slab.h>
28
29#include <linux/mtd/mtd.h>
30#include <linux/mtd/partitions.h>
31#include <linux/bootmem.h>
32
33/* error message prefix */
34#define ERRP "mtd: "
35
36/* debug macro */
37#if 0
38#define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0)
39#else
40#define dbg(x)
41#endif
42
43
44/* special size referring to all the remaining space in a partition */
Atsushi Nemotob175d032006-03-31 02:29:46 -080045#define SIZE_REMAINING UINT_MAX
46#define OFFSET_CONTINUOUS UINT_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48struct cmdline_mtd_partition {
49 struct cmdline_mtd_partition *next;
50 char *mtd_id;
51 int num_parts;
52 struct mtd_partition *parts;
53};
54
55/* mtdpart_setup() parses into here */
56static struct cmdline_mtd_partition *partitions;
57
58/* the command line passed to mtdpart_setupd() */
59static char *cmdline;
60static int cmdline_parsed = 0;
61
62/*
63 * Parse one partition definition for an MTD. Since there can be many
Thomas Gleixner97894cd2005-11-07 11:15:26 +000064 * comma separated partition definitions, this function calls itself
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * recursively until no more partition definitions are found. Nice side
66 * effect: the memory to keep the mtd_partition structs and the names
67 * is allocated upon the last definition being found. At that point the
68 * syntax has been verified ok.
69 */
Thomas Gleixner97894cd2005-11-07 11:15:26 +000070static struct mtd_partition * newpart(char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 char **retptr,
72 int *num_parts,
Thomas Gleixner97894cd2005-11-07 11:15:26 +000073 int this_part,
74 unsigned char **extra_mem_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int extra_mem_size)
76{
77 struct mtd_partition *parts;
78 unsigned long size;
Atsushi Nemotob175d032006-03-31 02:29:46 -080079 unsigned long offset = OFFSET_CONTINUOUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 char *name;
81 int name_len;
82 unsigned char *extra_mem;
83 char delim;
84 unsigned int mask_flags;
85
86 /* fetch the partition size */
87 if (*s == '-')
88 { /* assign all remaining space to this partition */
89 size = SIZE_REMAINING;
90 s++;
91 }
92 else
93 {
94 size = memparse(s, &s);
95 if (size < PAGE_SIZE)
96 {
97 printk(KERN_ERR ERRP "partition size too small (%lx)\n", size);
98 return NULL;
99 }
100 }
101
102 /* fetch partition name and flags */
103 mask_flags = 0; /* this is going to be a regular partition */
104 delim = 0;
105 /* check for offset */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000106 if (*s == '@')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 {
108 s++;
109 offset = memparse(s, &s);
110 }
111 /* now look for name */
112 if (*s == '(')
113 {
114 delim = ')';
115 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (delim)
118 {
119 char *p;
120
121 name = ++s;
122 if ((p = strchr(name, delim)) == 0)
123 {
124 printk(KERN_ERR ERRP "no closing %c found in partition name\n", delim);
125 return NULL;
126 }
127 name_len = p - name;
128 s = p + 1;
129 }
130 else
131 {
132 name = NULL;
133 name_len = 13; /* Partition_000 */
134 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /* record name length for memory allocation later */
137 extra_mem_size += name_len + 1;
138
139 /* test for options */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000140 if (strncmp(s, "ro", 2) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 {
142 mask_flags |= MTD_WRITEABLE;
143 s += 2;
144 }
145
146 /* test if more partitions are following */
147 if (*s == ',')
148 {
149 if (size == SIZE_REMAINING)
150 {
151 printk(KERN_ERR ERRP "no partitions allowed after a fill-up partition\n");
152 return NULL;
153 }
154 /* more partitions follow, parse them */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000155 if ((parts = newpart(s + 1, &s, num_parts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 this_part + 1, &extra_mem, extra_mem_size)) == 0)
157 return NULL;
158 }
159 else
160 { /* this is the last partition: allocate space for all */
161 int alloc_size;
162
163 *num_parts = this_part + 1;
164 alloc_size = *num_parts * sizeof(struct mtd_partition) +
165 extra_mem_size;
Burman Yan95b93a02006-11-15 21:10:29 +0200166 parts = kzalloc(alloc_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (!parts)
168 {
169 printk(KERN_ERR ERRP "out of memory\n");
170 return NULL;
171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 extra_mem = (unsigned char *)(parts + *num_parts);
173 }
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;
178 if (name)
179 {
180 strlcpy(extra_mem, name, name_len + 1);
181 }
182 else
183 {
184 sprintf(extra_mem, "Partition_%03d", this_part);
185 }
186 parts[this_part].name = extra_mem;
187 extra_mem += name_len + 1;
188
189 dbg(("partition %d: name <%s>, offset %x, size %x, mask flags %x\n",
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000190 this_part,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 parts[this_part].name,
192 parts[this_part].offset,
193 parts[this_part].size,
194 parts[this_part].mask_flags));
195
196 /* return (updated) pointer to extra_mem memory */
197 if (extra_mem_ptr)
198 *extra_mem_ptr = extra_mem;
199
200 /* return (updated) pointer command line string */
201 *retptr = s;
202
203 /* return partition table */
204 return parts;
205}
206
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000207/*
208 * Parse the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
210static int mtdpart_setup_real(char *s)
211{
212 cmdline_parsed = 1;
213
214 for( ; s != NULL; )
215 {
216 struct cmdline_mtd_partition *this_mtd;
217 struct mtd_partition *parts;
218 int mtd_id_len;
219 int num_parts;
220 char *p, *mtd_id;
221
222 mtd_id = s;
223 /* fetch <mtd-id> */
224 if (!(p = strchr(s, ':')))
225 {
226 printk(KERN_ERR ERRP "no mtd-id\n");
227 return 0;
228 }
229 mtd_id_len = p - mtd_id;
230
231 dbg(("parsing <%s>\n", p+1));
232
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000233 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * parse one mtd. have it reserve memory for the
235 * struct cmdline_mtd_partition and the mtd-id string.
236 */
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 */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000242 mtd_id_len + 1 + sizeof(*this_mtd) +
Joern Engelbe76c5f2005-06-07 16:04:29 +0100243 sizeof(void*)-1 /*alignment*/);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if(!parts)
245 {
246 /*
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
252 */
253 return 0;
254 }
255
Joern Engelbe76c5f2005-06-07 16:04:29 +0100256 /* align this_mtd */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000257 this_mtd = (struct cmdline_mtd_partition *)
Joern Engelbe76c5f2005-06-07 16:04:29 +0100258 ALIGN((unsigned long)this_mtd, sizeof(void*));
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000259 /* enter results */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 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);
264
265 /* link into chain */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000266 this_mtd->next = partitions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 partitions = this_mtd;
268
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000269 dbg(("mtdid=<%s> num_parts=<%d>\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 this_mtd->mtd_id, this_mtd->num_parts));
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 /* EOS - we're done */
274 if (*s == 0)
275 break;
276
277 /* does another spec follow? */
278 if (*s != ';')
279 {
280 printk(KERN_ERR ERRP "bad character after partition (%c)\n", *s);
281 return 0;
282 }
283 s++;
284 }
285 return 1;
286}
287
288/*
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.
294 */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000295static int parse_cmdline_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct mtd_partition **pparts,
297 unsigned long origin)
298{
299 unsigned long offset;
300 int i;
301 struct cmdline_mtd_partition *part;
302 char *mtd_id = master->name;
303
304 if(!cmdline)
305 return -EINVAL;
306
307 /* parse command line */
308 if (!cmdline_parsed)
309 mtdpart_setup_real(cmdline);
310
311 for(part = partitions; part; part = part->next)
312 {
313 if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
314 {
315 for(i = 0, offset = 0; i < part->num_parts; i++)
316 {
Atsushi Nemotob175d032006-03-31 02:29:46 -0800317 if (part->parts[i].offset == OFFSET_CONTINUOUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 part->parts[i].offset = offset;
319 else
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)
324 {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000325 printk(KERN_WARNING ERRP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 "%s: partitioning exceeds flash size, truncating\n",
327 part->mtd_id);
328 part->parts[i].size = master->size - offset;
329 part->num_parts = i;
330 }
331 offset += part->parts[i].size;
332 }
333 *pparts = part->parts;
334 return part->num_parts;
335 }
336 }
337 return -EINVAL;
338}
339
340
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000341/*
342 * This is the handler for our kernel parameter, called from
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * main.c::checksetup(). Note that we can not yet kmalloc() anything,
344 * so we only save the commandline for later processing.
345 *
346 * This function needs to be visible for bootloaders.
347 */
Adrian Bunkddacff12006-11-25 20:15:41 +0100348static int mtdpart_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 cmdline = s;
351 return 1;
352}
353
354__setup("mtdparts=", mtdpart_setup);
355
356static struct mtd_part_parser cmdline_parser = {
357 .owner = THIS_MODULE,
358 .parse_fn = parse_cmdline_partitions,
359 .name = "cmdlinepart",
360};
361
362static int __init cmdline_parser_init(void)
363{
364 return register_mtd_parser(&cmdline_parser);
365}
366
367module_init(cmdline_parser_init);
368
369MODULE_LICENSE("GPL");
370MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>");
371MODULE_DESCRIPTION("Command line configuration of MTD partitions");