Update ia64 patch to 2.5.69-030521, throwing away the parts included
[linux-flexiantxendom0-3.2.10.git] / drivers / mtd / cmdline.c
1 /*
2  * $Id: cmdline.c,v 1.5 2002/11/06 22:40:04 rmk Exp $
3  *
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:
9  * 
10  * mtdparts=<mtddef>[;<mtddef]
11  * <mtddef>  := <mtd-id>:<partdef>[,<partdef>]
12  * <partdef> := <size>[@offset][<name>][ro]
13  * <mtd-id>  := unique id used in mapping driver/device
14  * <size>    := standard linux memsize OR "-" to denote all remaining space
15  * <name>    := '(' NAME ')'
16  * 
17  * Examples:
18  * 
19  * 1 NOR Flash, with 1 single writable partition:
20  * edb7312-nor:-
21  * 
22  * 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 */
45 #define SIZE_REMAINING 0xffffffff
46
47 struct cmdline_mtd_partition {
48         struct cmdline_mtd_partition *next;
49         char *mtd_id;
50         int num_parts;
51         struct mtd_partition *parts;
52 };
53
54 /* mtdpart_setup() parses into here */
55 static struct cmdline_mtd_partition *partitions;
56
57 /* the command line passed to mtdpart_setupd() */
58 static char *cmdline;
59 static int cmdline_parsed = 0;
60
61 /*
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.
68  */
69 static struct mtd_partition * newpart(char *s, 
70                                       char **retptr,
71                                       int *num_parts,
72                                       int this_part, 
73                                       unsigned char **extra_mem_ptr, 
74                                       int extra_mem_size)
75 {
76         struct mtd_partition *parts;
77         unsigned long size;
78         unsigned long offset = 0;
79         char *name;
80         int name_len;
81         unsigned char *extra_mem;
82         char delim;
83         unsigned int mask_flags;
84
85         /* fetch the partition size */
86         if (*s == '-')
87         {       /* assign all remaining space to this partition */
88                 size = SIZE_REMAINING;
89                 s++;
90         }
91         else
92         {
93                 size = memparse(s, &s);
94                 if (!size)
95                 {
96                         printk(KERN_ERR ERRP "couldn't parse number from input string\n");
97                         return 0;
98                 }
99                 if (size < PAGE_SIZE)
100                 {
101                         printk(KERN_ERR ERRP "partition size too small (%lx)\n", size);
102                         return 0;
103                 }
104         }
105
106         /* fetch partition name and flags */
107         mask_flags = 0; /* this is going to be a regular partition */
108         delim = 0;
109         /* check for offset */
110         if (*s == '@') 
111         {
112                 s++;
113                 offset = memparse(s, &s);
114                 if (!offset)
115                 {
116                         printk(KERN_ERR ERRP "couldn't parse number from input string\n");
117                         return 0;
118                 }
119         }
120         /* now look for name */
121         if (*s == '(')
122         {
123                 delim = ')';
124         }
125         if (delim)
126         {
127                 char *p;
128
129                 name = ++s;
130                 if ((p = strchr(name, delim)) == 0)
131                 {
132                         printk(KERN_ERR ERRP "no closing %c found in partition name\n", delim);
133                         return 0;
134                 }
135                 name_len = p - name;
136                 s = p + 1;
137         }
138         else
139         {
140                 name = NULL;
141                 name_len = 13; /* Partition_000 */
142         }
143    
144         /* record name length for memory allocation later */
145         extra_mem_size += name_len + 1;
146
147         /* test for options */
148         if (strncmp(s, "ro", 2) == 0) 
149         {
150                 mask_flags |= MTD_WRITEABLE;
151                 s += 2;
152         }
153
154         /* test if more partitions are following */
155         if (*s == ',')
156         {
157                 if (size == SIZE_REMAINING)
158                 {
159                         printk(KERN_ERR ERRP "no partitions allowed after a fill-up partition\n");
160                         return 0;
161                 }
162                 /* more partitions follow, parse them */
163                 if ((parts = newpart(s + 1, &s, num_parts, 
164                                      this_part + 1, &extra_mem, extra_mem_size)) == 0)
165                   return 0;
166         }
167         else
168         {       /* this is the last partition: allocate space for all */
169                 int alloc_size;
170
171                 *num_parts = this_part + 1;
172                 alloc_size = *num_parts * sizeof(struct mtd_partition) +
173                              extra_mem_size;
174                 parts = kmalloc(alloc_size, GFP_KERNEL);
175                 if (!parts)
176                 {
177                         printk(KERN_ERR ERRP "out of memory\n");
178                         return 0;
179                 }
180                 memset(parts, 0, alloc_size);
181                 extra_mem = (unsigned char *)(parts + *num_parts);
182         }
183         /* enter this partition (offset will be calculated later if it is zero at this point) */
184         parts[this_part].size = size;
185         parts[this_part].offset = offset;
186         parts[this_part].mask_flags = mask_flags;
187         if (name)
188         {
189                 strlcpy(extra_mem, name, name_len + 1);
190         }
191         else
192         {
193                 sprintf(extra_mem, "Partition_%03d", this_part);
194         }
195         parts[this_part].name = extra_mem;
196         extra_mem += name_len + 1;
197
198         dbg(("partition %d: name <%s>, offset %x, size %x, mask flags %x\n",
199              this_part, 
200              parts[this_part].name,
201              parts[this_part].offset,
202              parts[this_part].size,
203              parts[this_part].mask_flags));
204
205         /* return (updated) pointer to extra_mem memory */
206         if (extra_mem_ptr)
207           *extra_mem_ptr = extra_mem;
208
209         /* return (updated) pointer command line string */
210         *retptr = s;
211
212         /* return partition table */
213         return parts;
214 }
215
216 /* 
217  * Parse the command line. 
218  */
219 static int mtdpart_setup_real(char *s)
220 {
221         cmdline_parsed = 1;
222
223         for( ; s != NULL; )
224         {
225                 struct cmdline_mtd_partition *this_mtd;
226                 struct mtd_partition *parts;
227                 int mtd_id_len;
228                 int num_parts;
229                 char *p, *mtd_id;
230
231                 mtd_id = s;
232                 /* fetch <mtd-id> */
233                 if (!(p = strchr(s, ':')))
234                 {
235                         printk(KERN_ERR ERRP "no mtd-id\n");
236                         return 0;
237                 }
238                 mtd_id_len = p - mtd_id;
239
240                 dbg(("parsing <%s>\n", p+1));
241
242                 /* 
243                  * parse one mtd. have it reserve memory for the
244                  * struct cmdline_mtd_partition and the mtd-id string.
245                  */
246                 parts = newpart(p + 1,          /* cmdline */
247                                 &s,             /* out: updated cmdline ptr */
248                                 &num_parts,     /* out: number of parts */
249                                 0,              /* first partition */
250                                 (unsigned char**)&this_mtd, /* out: extra mem */
251                                 mtd_id_len + 1 + sizeof(*this_mtd));
252                 if(!parts)
253                 {
254                         /*
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
260                          */
261                          return 0;
262                  }
263
264                 /* enter results */         
265                 this_mtd->parts = parts;
266                 this_mtd->num_parts = num_parts;
267                 this_mtd->mtd_id = (char*)(this_mtd + 1);
268                 strlcpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1);
269
270                 /* link into chain */
271                 this_mtd->next = partitions;            
272                 partitions = this_mtd;
273
274                 dbg(("mtdid=<%s> num_parts=<%d>\n", 
275                      this_mtd->mtd_id, this_mtd->num_parts));
276                 
277
278                 /* EOS - we're done */
279                 if (*s == 0)
280                         break;
281
282                 /* does another spec follow? */
283                 if (*s != ';')
284                 {
285                         printk(KERN_ERR ERRP "bad character after partition (%c)\n", *s);
286                         return 0;
287                 }
288                 s++;
289         }
290         return 1;
291 }
292
293 /*
294  * Main function to be called from the MTD mapping driver/device to
295  * obtain the partitioning information. At this point the command line
296  * arguments will actually be parsed and turned to struct mtd_partition
297  * information.
298  */
299 int parse_cmdline_partitions(struct mtd_info *master, 
300                              struct mtd_partition **pparts,
301                              const char *mtd_id)
302 {
303         unsigned long offset;
304         int i;
305         struct cmdline_mtd_partition *part;
306
307         if (!cmdline)
308                 return -EINVAL;
309
310         /* parse command line */
311         if (!cmdline_parsed)
312                 mtdpart_setup_real(cmdline);
313
314         for(part = partitions; part; part = part->next)
315         {
316                 if (!strcmp(part->mtd_id, mtd_id))
317                 {
318                         for(i = 0, offset = 0; i < part->num_parts; i++)
319                         {
320                                 if (!part->parts[i].offset)
321                                   part->parts[i].offset = offset;
322                                 else
323                                   offset = part->parts[i].offset;
324                                 if (part->parts[i].size == SIZE_REMAINING)
325                                   part->parts[i].size = master->size - offset;
326                                 if (offset + part->parts[i].size > master->size)
327                                 {
328                                         printk(KERN_WARNING ERRP 
329                                                "%s: partitioning exceeds flash size, truncating\n",
330                                                mtd_id);
331                                         part->parts[i].size = master->size - offset;
332                                         part->num_parts = i;
333                                 }
334                                 offset += part->parts[i].size;
335                         }
336                         *pparts = part->parts;
337                         return part->num_parts;
338                 }
339         }
340         return -EINVAL;
341 }
342
343
344 /* 
345  * This is the handler for our kernel parameter, called from 
346  * main.c::checksetup(). Note that we can not yet kmalloc() anything,
347  * so we only save the commandline for later processing.
348  */
349 static int __init mtdpart_setup(char *s)
350 {
351         cmdline = s;
352         return 1;
353 }
354
355 __setup("mtdparts=", mtdpart_setup);
356
357 EXPORT_SYMBOL(parse_cmdline_partitions);
358
359 MODULE_LICENSE("GPL");
360 MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>");
361 MODULE_DESCRIPTION("Command line configuration of MTD partitions");