1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51#include <linux/kernel.h>
52#include <linux/slab.h>
53#include <linux/mtd/mtd.h>
54#include <linux/mtd/partitions.h>
55#include <linux/module.h>
56#include <linux/err.h>
57
58
59#define ERRP "mtd: "
60
61
62#if 0
63#define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0)
64#else
65#define dbg(x)
66#endif
67
68
69
70#define SIZE_REMAINING ULLONG_MAX
71#define OFFSET_CONTINUOUS ULLONG_MAX
72
73struct cmdline_mtd_partition {
74 struct cmdline_mtd_partition *next;
75 char *mtd_id;
76 int num_parts;
77 struct mtd_partition *parts;
78};
79
80
81static struct cmdline_mtd_partition *partitions;
82
83
84static char *mtdparts;
85static char *cmdline;
86static int cmdline_parsed;
87
88
89
90
91
92
93
94
95
96static struct mtd_partition * newpart(char *s,
97 char **retptr,
98 int *num_parts,
99 int this_part,
100 unsigned char **extra_mem_ptr,
101 int extra_mem_size)
102{
103 struct mtd_partition *parts;
104 unsigned long long size, offset = OFFSET_CONTINUOUS;
105 char *name;
106 int name_len;
107 unsigned char *extra_mem;
108 char delim;
109 unsigned int mask_flags;
110
111
112 if (*s == '-') {
113
114 size = SIZE_REMAINING;
115 s++;
116 } else {
117 size = memparse(s, &s);
118 if (size < PAGE_SIZE) {
119 printk(KERN_ERR ERRP "partition size too small (%llx)\n",
120 size);
121 return ERR_PTR(-EINVAL);
122 }
123 }
124
125
126 mask_flags = 0;
127 delim = 0;
128
129
130 if (*s == '@') {
131 s++;
132 offset = memparse(s, &s);
133 }
134
135
136 if (*s == '(')
137 delim = ')';
138
139 if (delim) {
140 char *p;
141
142 name = ++s;
143 p = strchr(name, delim);
144 if (!p) {
145 printk(KERN_ERR ERRP "no closing %c found in partition name\n", delim);
146 return ERR_PTR(-EINVAL);
147 }
148 name_len = p - name;
149 s = p + 1;
150 } else {
151 name = NULL;
152 name_len = 13;
153 }
154
155
156 extra_mem_size += name_len + 1;
157
158
159 if (strncmp(s, "ro", 2) == 0) {
160 mask_flags |= MTD_WRITEABLE;
161 s += 2;
162 }
163
164
165 if (strncmp(s, "lk", 2) == 0) {
166 mask_flags |= MTD_POWERUP_LOCK;
167 s += 2;
168 }
169
170
171 if (*s == ',') {
172 if (size == SIZE_REMAINING) {
173 printk(KERN_ERR ERRP "no partitions allowed after a fill-up partition\n");
174 return ERR_PTR(-EINVAL);
175 }
176
177 parts = newpart(s + 1, &s, num_parts, this_part + 1,
178 &extra_mem, extra_mem_size);
179 if (IS_ERR(parts))
180 return parts;
181 } else {
182
183 int alloc_size;
184
185 *num_parts = this_part + 1;
186 alloc_size = *num_parts * sizeof(struct mtd_partition) +
187 extra_mem_size;
188
189 parts = kzalloc(alloc_size, GFP_KERNEL);
190 if (!parts)
191 return ERR_PTR(-ENOMEM);
192 extra_mem = (unsigned char *)(parts + *num_parts);
193 }
194
195
196 parts[this_part].size = size;
197 parts[this_part].offset = offset;
198 parts[this_part].mask_flags = mask_flags;
199 if (name)
200 strlcpy(extra_mem, name, name_len + 1);
201 else
202 sprintf(extra_mem, "Partition_%03d", this_part);
203 parts[this_part].name = extra_mem;
204 extra_mem += name_len + 1;
205
206 dbg(("partition %d: name <%s>, offset %llx, size %llx, mask flags %x\n",
207 this_part, parts[this_part].name, parts[this_part].offset,
208 parts[this_part].size, parts[this_part].mask_flags));
209
210
211 if (extra_mem_ptr)
212 *extra_mem_ptr = extra_mem;
213
214
215 *retptr = s;
216
217
218 return parts;
219}
220
221
222
223
224static int mtdpart_setup_real(char *s)
225{
226 cmdline_parsed = 1;
227
228 for( ; s != NULL; )
229 {
230 struct cmdline_mtd_partition *this_mtd;
231 struct mtd_partition *parts;
232 int mtd_id_len, num_parts;
233 char *p, *mtd_id;
234
235 mtd_id = s;
236
237
238 p = strchr(s, ':');
239 if (!p) {
240 printk(KERN_ERR ERRP "no mtd-id\n");
241 return -EINVAL;
242 }
243 mtd_id_len = p - mtd_id;
244
245 dbg(("parsing <%s>\n", p+1));
246
247
248
249
250
251 parts = newpart(p + 1,
252 &s,
253 &num_parts,
254 0,
255 (unsigned char**)&this_mtd,
256 mtd_id_len + 1 + sizeof(*this_mtd) +
257 sizeof(void*)-1 );
258 if (IS_ERR(parts)) {
259
260
261
262
263
264
265
266 return PTR_ERR(parts);
267 }
268
269
270 this_mtd = (struct cmdline_mtd_partition *)
271 ALIGN((unsigned long)this_mtd, sizeof(void *));
272
273 this_mtd->parts = parts;
274 this_mtd->num_parts = num_parts;
275 this_mtd->mtd_id = (char*)(this_mtd + 1);
276 strlcpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1);
277
278
279 this_mtd->next = partitions;
280 partitions = this_mtd;
281
282 dbg(("mtdid=<%s> num_parts=<%d>\n",
283 this_mtd->mtd_id, this_mtd->num_parts));
284
285
286
287 if (*s == 0)
288 break;
289
290
291 if (*s != ';') {
292 printk(KERN_ERR ERRP "bad character after partition (%c)\n", *s);
293 return -EINVAL;
294 }
295 s++;
296 }
297
298 return 0;
299}
300
301
302
303
304
305
306
307
308static int parse_cmdline_partitions(struct mtd_info *master,
309 struct mtd_partition **pparts,
310 struct mtd_part_parser_data *data)
311{
312 unsigned long long offset;
313 int i, err;
314 struct cmdline_mtd_partition *part;
315 const char *mtd_id = master->name;
316
317
318 if (!cmdline_parsed) {
319 err = mtdpart_setup_real(cmdline);
320 if (err)
321 return err;
322 }
323
324
325
326
327
328 for (part = partitions; part; part = part->next) {
329 if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
330 break;
331 }
332
333 if (!part)
334 return 0;
335
336 for (i = 0, offset = 0; i < part->num_parts; i++) {
337 if (part->parts[i].offset == OFFSET_CONTINUOUS)
338 part->parts[i].offset = offset;
339 else
340 offset = part->parts[i].offset;
341
342 if (part->parts[i].size == SIZE_REMAINING)
343 part->parts[i].size = master->size - offset;
344
345 if (offset + part->parts[i].size > master->size) {
346 printk(KERN_WARNING ERRP
347 "%s: partitioning exceeds flash size, truncating\n",
348 part->mtd_id);
349 part->parts[i].size = master->size - offset;
350 }
351 offset += part->parts[i].size;
352
353 if (part->parts[i].size == 0) {
354 printk(KERN_WARNING ERRP
355 "%s: skipping zero sized partition\n",
356 part->mtd_id);
357 part->num_parts--;
358 memmove(&part->parts[i], &part->parts[i + 1],
359 sizeof(*part->parts) * (part->num_parts - i));
360 i--;
361 }
362 }
363
364 *pparts = kmemdup(part->parts, sizeof(*part->parts) * part->num_parts,
365 GFP_KERNEL);
366 if (!*pparts)
367 return -ENOMEM;
368
369 return part->num_parts;
370}
371
372
373
374
375
376
377
378
379
380static int __init mtdpart_setup(char *s)
381{
382 cmdline = s;
383 return 1;
384}
385
386__setup("mtdparts=", mtdpart_setup);
387
388static struct mtd_part_parser cmdline_parser = {
389 .owner = THIS_MODULE,
390 .parse_fn = parse_cmdline_partitions,
391 .name = "cmdlinepart",
392};
393
394static int __init cmdline_parser_init(void)
395{
396 if (mtdparts)
397 mtdpart_setup(mtdparts);
398 register_mtd_parser(&cmdline_parser);
399 return 0;
400}
401
402static void __exit cmdline_parser_exit(void)
403{
404 deregister_mtd_parser(&cmdline_parser);
405}
406
407module_init(cmdline_parser_init);
408module_exit(cmdline_parser_exit);
409
410MODULE_PARM_DESC(mtdparts, "Partitioning specification");
411module_param(mtdparts, charp, 0);
412
413MODULE_LICENSE("GPL");
414MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>");
415MODULE_DESCRIPTION("Command line configuration of MTD partitions");
416