Blame view
drivers/mtd/cmdlinepart.c
10.7 KB
1da177e4c Linux-2.6.12-rc2 |
1 |
/* |
1da177e4c Linux-2.6.12-rc2 |
2 3 |
* Read flash partition table from command line * |
a1452a377 mtd: Update copyr... |
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
* Copyright © 2002 SYSGO Real-Time Solutions GmbH * Copyright © 2002-2010 David Woodhouse <dwmw2@infradead.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
1da177e4c Linux-2.6.12-rc2 |
20 21 |
* * The format for the command line is as follows: |
97894cda5 [MTD] core: Clean... |
22 |
* |
1da177e4c Linux-2.6.12-rc2 |
23 24 |
* mtdparts=<mtddef>[;<mtddef] * <mtddef> := <mtd-id>:<partdef>[,<partdef>] |
ea8b8e27f mtd: cmdlinepart:... |
25 |
* <partdef> := <size>[@<offset>][<name>][ro][lk] |
1da177e4c Linux-2.6.12-rc2 |
26 27 |
* <mtd-id> := unique name used in mapping driver/device (mtd->name) * <size> := standard linux memsize OR "-" to denote all remaining space |
ea8b8e27f mtd: cmdlinepart:... |
28 |
* size is automatically truncated at end of device |
29f63f65c mtd: cmdlinepart:... |
29 |
* if specified or truncated size is 0 the part is skipped |
ea8b8e27f mtd: cmdlinepart:... |
30 31 32 |
* <offset> := standard linux memsize * if omitted the part will immediately follow the previous part * or 0 if the first part |
1da177e4c Linux-2.6.12-rc2 |
33 |
* <name> := '(' NAME ')' |
bd6ce5ef9 mtd: cmdlinepart:... |
34 |
* NAME will appear in /proc/mtd |
97894cda5 [MTD] core: Clean... |
35 |
* |
ea8b8e27f mtd: cmdlinepart:... |
36 37 38 39 40 41 |
* <size> and <offset> can be specified such that the parts are out of order * in physical memory and may even overlap. * * The parts are assigned MTD numbers in the order they are specified in the * command line regardless of their order in physical memory. * |
1da177e4c Linux-2.6.12-rc2 |
42 |
* Examples: |
97894cda5 [MTD] core: Clean... |
43 |
* |
1da177e4c Linux-2.6.12-rc2 |
44 45 |
* 1 NOR Flash, with 1 single writable partition: * edb7312-nor:- |
97894cda5 [MTD] core: Clean... |
46 |
* |
1da177e4c Linux-2.6.12-rc2 |
47 48 49 |
* 1 NOR Flash with 2 partitions, 1 NAND with one * edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home) */ |
ecb43e0a5 mtd: cmdlinepart:... |
50 |
#define pr_fmt(fmt) "mtd: " fmt |
1da177e4c Linux-2.6.12-rc2 |
51 52 |
#include <linux/kernel.h> #include <linux/slab.h> |
1da177e4c Linux-2.6.12-rc2 |
53 54 |
#include <linux/mtd/mtd.h> #include <linux/mtd/partitions.h> |
a0e5cc581 mtd: Add module.h... |
55 |
#include <linux/module.h> |
9e0606fc4 mtd: cmdlinepart:... |
56 |
#include <linux/err.h> |
1da177e4c Linux-2.6.12-rc2 |
57 |
|
1da177e4c Linux-2.6.12-rc2 |
58 59 60 61 62 63 64 65 66 |
/* debug macro */ #if 0 #define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0) #else #define dbg(x) #endif /* special size referring to all the remaining space in a partition */ |
6f9f59ee2 mtd: cmdlinepart:... |
67 68 |
#define SIZE_REMAINING ULLONG_MAX #define OFFSET_CONTINUOUS ULLONG_MAX |
1da177e4c Linux-2.6.12-rc2 |
69 70 71 72 73 74 75 76 77 78 |
struct cmdline_mtd_partition { struct cmdline_mtd_partition *next; char *mtd_id; int num_parts; struct mtd_partition *parts; }; /* mtdpart_setup() parses into here */ static struct cmdline_mtd_partition *partitions; |
8abc0d4a1 mtd: cmdlinepart:... |
79 |
/* the command line passed to mtdpart_setup() */ |
f5f172dc0 mtd: cmdlinepart:... |
80 |
static char *mtdparts; |
1da177e4c Linux-2.6.12-rc2 |
81 |
static char *cmdline; |
aa3c5dc52 mtd: cmdlinepart:... |
82 |
static int cmdline_parsed; |
1da177e4c Linux-2.6.12-rc2 |
83 84 85 |
/* * Parse one partition definition for an MTD. Since there can be many |
97894cda5 [MTD] core: Clean... |
86 |
* comma separated partition definitions, this function calls itself |
1da177e4c Linux-2.6.12-rc2 |
87 88 89 90 91 |
* recursively until no more partition definitions are found. Nice side * effect: the memory to keep the mtd_partition structs and the names * is allocated upon the last definition being found. At that point the * syntax has been verified ok. */ |
97894cda5 [MTD] core: Clean... |
92 |
static struct mtd_partition * newpart(char *s, |
fac0077cc mtd: cmdlinepart:... |
93 94 95 96 97 |
char **retptr, int *num_parts, int this_part, unsigned char **extra_mem_ptr, int extra_mem_size) |
1da177e4c Linux-2.6.12-rc2 |
98 99 |
{ struct mtd_partition *parts; |
6f9f59ee2 mtd: cmdlinepart:... |
100 |
unsigned long long size, offset = OFFSET_CONTINUOUS; |
1da177e4c Linux-2.6.12-rc2 |
101 102 103 104 105 106 107 |
char *name; int name_len; unsigned char *extra_mem; char delim; unsigned int mask_flags; /* fetch the partition size */ |
fac0077cc mtd: cmdlinepart:... |
108 109 |
if (*s == '-') { /* assign all remaining space to this partition */ |
1da177e4c Linux-2.6.12-rc2 |
110 111 |
size = SIZE_REMAINING; s++; |
fac0077cc mtd: cmdlinepart:... |
112 |
} else { |
1da177e4c Linux-2.6.12-rc2 |
113 |
size = memparse(s, &s); |
d855d23b5 mtd: cmdlinepart:... |
114 |
if (!size) { |
ecb43e0a5 mtd: cmdlinepart:... |
115 116 |
pr_err("partition has size 0 "); |
9e0606fc4 mtd: cmdlinepart:... |
117 |
return ERR_PTR(-EINVAL); |
1da177e4c Linux-2.6.12-rc2 |
118 119 120 121 122 123 |
} } /* fetch partition name and flags */ mask_flags = 0; /* this is going to be a regular partition */ delim = 0; |
fac0077cc mtd: cmdlinepart:... |
124 125 126 127 128 129 130 131 |
/* check for offset */ if (*s == '@') { s++; offset = memparse(s, &s); } /* now look for name */ |
1da177e4c Linux-2.6.12-rc2 |
132 |
if (*s == '(') |
1da177e4c Linux-2.6.12-rc2 |
133 |
delim = ')'; |
97894cda5 [MTD] core: Clean... |
134 |
|
fac0077cc mtd: cmdlinepart:... |
135 |
if (delim) { |
1da177e4c Linux-2.6.12-rc2 |
136 |
char *p; |
fac0077cc mtd: cmdlinepart:... |
137 |
name = ++s; |
ed262c4f5 [MTD] cmdlinepart... |
138 |
p = strchr(name, delim); |
fac0077cc mtd: cmdlinepart:... |
139 |
if (!p) { |
ecb43e0a5 mtd: cmdlinepart:... |
140 141 |
pr_err("no closing %c found in partition name ", delim); |
9e0606fc4 mtd: cmdlinepart:... |
142 |
return ERR_PTR(-EINVAL); |
1da177e4c Linux-2.6.12-rc2 |
143 144 145 |
} name_len = p - name; s = p + 1; |
fac0077cc mtd: cmdlinepart:... |
146 147 |
} else { name = NULL; |
1da177e4c Linux-2.6.12-rc2 |
148 149 |
name_len = 13; /* Partition_000 */ } |
97894cda5 [MTD] core: Clean... |
150 |
|
1da177e4c Linux-2.6.12-rc2 |
151 152 |
/* record name length for memory allocation later */ extra_mem_size += name_len + 1; |
fac0077cc mtd: cmdlinepart:... |
153 154 |
/* test for options */ if (strncmp(s, "ro", 2) == 0) { |
1da177e4c Linux-2.6.12-rc2 |
155 156 |
mask_flags |= MTD_WRITEABLE; s += 2; |
fac0077cc mtd: cmdlinepart:... |
157 |
} |
1da177e4c Linux-2.6.12-rc2 |
158 |
|
fac0077cc mtd: cmdlinepart:... |
159 160 |
/* if lk is found do NOT unlock the MTD partition*/ if (strncmp(s, "lk", 2) == 0) { |
e619a75ff [MTD] Unlocking a... |
161 162 |
mask_flags |= MTD_POWERUP_LOCK; s += 2; |
fac0077cc mtd: cmdlinepart:... |
163 |
} |
e619a75ff [MTD] Unlocking a... |
164 |
|
1da177e4c Linux-2.6.12-rc2 |
165 |
/* test if more partitions are following */ |
fac0077cc mtd: cmdlinepart:... |
166 167 |
if (*s == ',') { if (size == SIZE_REMAINING) { |
ecb43e0a5 mtd: cmdlinepart:... |
168 169 |
pr_err("no partitions allowed after a fill-up partition "); |
9e0606fc4 mtd: cmdlinepart:... |
170 |
return ERR_PTR(-EINVAL); |
1da177e4c Linux-2.6.12-rc2 |
171 172 |
} /* more partitions follow, parse them */ |
ed262c4f5 [MTD] cmdlinepart... |
173 174 |
parts = newpart(s + 1, &s, num_parts, this_part + 1, &extra_mem, extra_mem_size); |
9e0606fc4 mtd: cmdlinepart:... |
175 176 |
if (IS_ERR(parts)) return parts; |
fac0077cc mtd: cmdlinepart:... |
177 178 |
} else { /* this is the last partition: allocate space for all */ |
1da177e4c Linux-2.6.12-rc2 |
179 180 181 182 183 |
int alloc_size; *num_parts = this_part + 1; alloc_size = *num_parts * sizeof(struct mtd_partition) + extra_mem_size; |
fac0077cc mtd: cmdlinepart:... |
184 |
|
95b93a0cd [MTD] replace kma... |
185 |
parts = kzalloc(alloc_size, GFP_KERNEL); |
1da177e4c Linux-2.6.12-rc2 |
186 |
if (!parts) |
9e0606fc4 mtd: cmdlinepart:... |
187 |
return ERR_PTR(-ENOMEM); |
1da177e4c Linux-2.6.12-rc2 |
188 189 |
extra_mem = (unsigned char *)(parts + *num_parts); } |
fac0077cc mtd: cmdlinepart:... |
190 |
|
1da177e4c Linux-2.6.12-rc2 |
191 192 193 194 195 |
/* enter this partition (offset will be calculated later if it is zero at this point) */ parts[this_part].size = size; parts[this_part].offset = offset; parts[this_part].mask_flags = mask_flags; if (name) |
1da177e4c Linux-2.6.12-rc2 |
196 |
strlcpy(extra_mem, name, name_len + 1); |
1da177e4c Linux-2.6.12-rc2 |
197 |
else |
1da177e4c Linux-2.6.12-rc2 |
198 |
sprintf(extra_mem, "Partition_%03d", this_part); |
1da177e4c Linux-2.6.12-rc2 |
199 200 |
parts[this_part].name = extra_mem; extra_mem += name_len + 1; |
342ba1039 mtd: cmdlineparts... |
201 202 |
dbg(("partition %d: name <%s>, offset %llx, size %llx, mask flags %x ", |
fac0077cc mtd: cmdlinepart:... |
203 204 |
this_part, parts[this_part].name, parts[this_part].offset, parts[this_part].size, parts[this_part].mask_flags)); |
1da177e4c Linux-2.6.12-rc2 |
205 206 207 |
/* return (updated) pointer to extra_mem memory */ if (extra_mem_ptr) |
fac0077cc mtd: cmdlinepart:... |
208 |
*extra_mem_ptr = extra_mem; |
1da177e4c Linux-2.6.12-rc2 |
209 210 211 212 213 214 215 |
/* return (updated) pointer command line string */ *retptr = s; /* return partition table */ return parts; } |
97894cda5 [MTD] core: Clean... |
216 217 |
/* * Parse the command line. |
1da177e4c Linux-2.6.12-rc2 |
218 219 220 221 222 223 224 225 226 |
*/ static int mtdpart_setup_real(char *s) { cmdline_parsed = 1; for( ; s != NULL; ) { struct cmdline_mtd_partition *this_mtd; struct mtd_partition *parts; |
fac0077cc mtd: cmdlinepart:... |
227 |
int mtd_id_len, num_parts; |
1da177e4c Linux-2.6.12-rc2 |
228 |
char *p, *mtd_id; |
fac0077cc mtd: cmdlinepart:... |
229 |
mtd_id = s; |
1da177e4c Linux-2.6.12-rc2 |
230 |
/* fetch <mtd-id> */ |
fac0077cc mtd: cmdlinepart:... |
231 232 |
p = strchr(s, ':'); if (!p) { |
ecb43e0a5 mtd: cmdlinepart:... |
233 234 |
pr_err("no mtd-id "); |
9e0606fc4 mtd: cmdlinepart:... |
235 |
return -EINVAL; |
1da177e4c Linux-2.6.12-rc2 |
236 237 238 239 240 |
} mtd_id_len = p - mtd_id; dbg(("parsing <%s> ", p+1)); |
97894cda5 [MTD] core: Clean... |
241 |
/* |
1da177e4c Linux-2.6.12-rc2 |
242 243 244 245 246 247 248 249 |
* parse one mtd. have it reserve memory for the * struct cmdline_mtd_partition and the mtd-id string. */ parts = newpart(p + 1, /* cmdline */ &s, /* out: updated cmdline ptr */ &num_parts, /* out: number of parts */ 0, /* first partition */ (unsigned char**)&this_mtd, /* out: extra mem */ |
97894cda5 [MTD] core: Clean... |
250 |
mtd_id_len + 1 + sizeof(*this_mtd) + |
be76c5fb4 [MTD] Fix command... |
251 |
sizeof(void*)-1 /*alignment*/); |
fac0077cc mtd: cmdlinepart:... |
252 |
if (IS_ERR(parts)) { |
1da177e4c Linux-2.6.12-rc2 |
253 254 255 256 257 258 259 |
/* * An error occurred. We're either: * a) out of memory, or * b) in the middle of the partition spec * Either way, this mtd is hosed and we're * unlikely to succeed in parsing any more */ |
9e0606fc4 mtd: cmdlinepart:... |
260 |
return PTR_ERR(parts); |
1da177e4c Linux-2.6.12-rc2 |
261 |
} |
be76c5fb4 [MTD] Fix command... |
262 |
/* align this_mtd */ |
97894cda5 [MTD] core: Clean... |
263 |
this_mtd = (struct cmdline_mtd_partition *) |
fac0077cc mtd: cmdlinepart:... |
264 |
ALIGN((unsigned long)this_mtd, sizeof(void *)); |
97894cda5 [MTD] core: Clean... |
265 |
/* enter results */ |
1da177e4c Linux-2.6.12-rc2 |
266 267 268 269 270 271 |
this_mtd->parts = parts; this_mtd->num_parts = num_parts; this_mtd->mtd_id = (char*)(this_mtd + 1); strlcpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1); /* link into chain */ |
97894cda5 [MTD] core: Clean... |
272 |
this_mtd->next = partitions; |
1da177e4c Linux-2.6.12-rc2 |
273 |
partitions = this_mtd; |
97894cda5 [MTD] core: Clean... |
274 275 |
dbg(("mtdid=<%s> num_parts=<%d> ", |
1da177e4c Linux-2.6.12-rc2 |
276 |
this_mtd->mtd_id, this_mtd->num_parts)); |
97894cda5 [MTD] core: Clean... |
277 |
|
1da177e4c Linux-2.6.12-rc2 |
278 279 280 281 282 283 |
/* EOS - we're done */ if (*s == 0) break; /* does another spec follow? */ |
fac0077cc mtd: cmdlinepart:... |
284 |
if (*s != ';') { |
ecb43e0a5 mtd: cmdlinepart:... |
285 286 |
pr_err("bad character after partition (%c) ", *s); |
9e0606fc4 mtd: cmdlinepart:... |
287 |
return -EINVAL; |
1da177e4c Linux-2.6.12-rc2 |
288 289 290 |
} s++; } |
fac0077cc mtd: cmdlinepart:... |
291 |
|
9e0606fc4 mtd: cmdlinepart:... |
292 |
return 0; |
1da177e4c Linux-2.6.12-rc2 |
293 294 295 296 297 298 299 300 301 |
} /* * Main function to be called from the MTD mapping driver/device to * obtain the partitioning information. At this point the command line * arguments will actually be parsed and turned to struct mtd_partition * information. It returns partitions for the requested mtd device, or * the first one in the chain if a NULL mtd_id is passed in. */ |
97894cda5 [MTD] core: Clean... |
302 |
static int parse_cmdline_partitions(struct mtd_info *master, |
b9adf469f mtd: partitions: ... |
303 |
const struct mtd_partition **pparts, |
c79753301 mtd: abstract las... |
304 |
struct mtd_part_parser_data *data) |
1da177e4c Linux-2.6.12-rc2 |
305 |
{ |
6f9f59ee2 mtd: cmdlinepart:... |
306 |
unsigned long long offset; |
9e0606fc4 mtd: cmdlinepart:... |
307 |
int i, err; |
1da177e4c Linux-2.6.12-rc2 |
308 |
struct cmdline_mtd_partition *part; |
36560d255 [MTD] Fix const a... |
309 |
const char *mtd_id = master->name; |
1da177e4c Linux-2.6.12-rc2 |
310 |
|
1da177e4c Linux-2.6.12-rc2 |
311 |
/* parse command line */ |
9e0606fc4 mtd: cmdlinepart:... |
312 313 314 315 316 |
if (!cmdline_parsed) { err = mtdpart_setup_real(cmdline); if (err) return err; } |
1da177e4c Linux-2.6.12-rc2 |
317 |
|
438db5a92 mtd: cmdlinepart:... |
318 319 320 321 |
/* * Search for the partition definition matching master->name. * If master->name is not set, stop at first partition definition. */ |
fac0077cc mtd: cmdlinepart:... |
322 |
for (part = partitions; part; part = part->next) { |
438db5a92 mtd: cmdlinepart:... |
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id))) break; } if (!part) return 0; for (i = 0, offset = 0; i < part->num_parts; i++) { if (part->parts[i].offset == OFFSET_CONTINUOUS) part->parts[i].offset = offset; else offset = part->parts[i].offset; if (part->parts[i].size == SIZE_REMAINING) part->parts[i].size = master->size - offset; |
ebf4f0707 mtd: cmdlinepart:... |
338 |
if (offset + part->parts[i].size > master->size) { |
ecb43e0a5 mtd: cmdlinepart:... |
339 340 341 |
pr_warn("%s: partitioning exceeds flash size, truncating ", part->mtd_id); |
ebf4f0707 mtd: cmdlinepart:... |
342 343 344 |
part->parts[i].size = master->size - offset; } offset += part->parts[i].size; |
438db5a92 mtd: cmdlinepart:... |
345 |
if (part->parts[i].size == 0) { |
ecb43e0a5 mtd: cmdlinepart:... |
346 347 348 |
pr_warn("%s: skipping zero sized partition ", part->mtd_id); |
438db5a92 mtd: cmdlinepart:... |
349 350 351 |
part->num_parts--; memmove(&part->parts[i], &part->parts[i + 1], sizeof(*part->parts) * (part->num_parts - i)); |
e25e0a4de mtd: cmdlinepart:... |
352 |
i--; |
1da177e4c Linux-2.6.12-rc2 |
353 354 |
} } |
fac0077cc mtd: cmdlinepart:... |
355 |
|
438db5a92 mtd: cmdlinepart:... |
356 357 358 359 360 361 |
*pparts = kmemdup(part->parts, sizeof(*part->parts) * part->num_parts, GFP_KERNEL); if (!*pparts) return -ENOMEM; return part->num_parts; |
1da177e4c Linux-2.6.12-rc2 |
362 |
} |
97894cda5 [MTD] core: Clean... |
363 364 |
/* * This is the handler for our kernel parameter, called from |
1da177e4c Linux-2.6.12-rc2 |
365 366 367 368 369 |
* main.c::checksetup(). Note that we can not yet kmalloc() anything, * so we only save the commandline for later processing. * * This function needs to be visible for bootloaders. */ |
f5f172dc0 mtd: cmdlinepart:... |
370 |
static int __init mtdpart_setup(char *s) |
1da177e4c Linux-2.6.12-rc2 |
371 372 373 374 375 376 377 378 |
{ cmdline = s; return 1; } __setup("mtdparts=", mtdpart_setup); static struct mtd_part_parser cmdline_parser = { |
1da177e4c Linux-2.6.12-rc2 |
379 380 381 382 383 384 |
.parse_fn = parse_cmdline_partitions, .name = "cmdlinepart", }; static int __init cmdline_parser_init(void) { |
f5f172dc0 mtd: cmdlinepart:... |
385 386 |
if (mtdparts) mtdpart_setup(mtdparts); |
6e14a61d4 mtd: make registe... |
387 388 |
register_mtd_parser(&cmdline_parser); return 0; |
1da177e4c Linux-2.6.12-rc2 |
389 |
} |
422f3890a mtd: Allow remova... |
390 391 392 393 |
static void __exit cmdline_parser_exit(void) { deregister_mtd_parser(&cmdline_parser); } |
1da177e4c Linux-2.6.12-rc2 |
394 |
module_init(cmdline_parser_init); |
422f3890a mtd: Allow remova... |
395 |
module_exit(cmdline_parser_exit); |
1da177e4c Linux-2.6.12-rc2 |
396 |
|
f5f172dc0 mtd: cmdlinepart:... |
397 398 |
MODULE_PARM_DESC(mtdparts, "Partitioning specification"); module_param(mtdparts, charp, 0); |
1da177e4c Linux-2.6.12-rc2 |
399 400 401 |
MODULE_LICENSE("GPL"); MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>"); MODULE_DESCRIPTION("Command line configuration of MTD partitions"); |