Commit a0ee24a03b1c06813c814b9f70946c8984752f01

Authored by Philip Rakity
Committed by David Woodhouse
1 parent 87e92c062b

[MTD] cmdlineparts documentation change - explain where mtd-id comes from

Signed-off-by: Philip Rakity <prakity@yahoo.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>

Showing 1 changed file with 1 additions and 0 deletions Inline Diff

drivers/mtd/cmdlinepart.c
1 /* 1 /*
2 * Read flash partition table from command line 2 * Read flash partition table from command line
3 * 3 *
4 * Copyright 2002 SYSGO Real-Time Solutions GmbH 4 * Copyright 2002 SYSGO Real-Time Solutions GmbH
5 * 5 *
6 * The format for the command line is as follows: 6 * The format for the command line is as follows:
7 * 7 *
8 * mtdparts=<mtddef>[;<mtddef] 8 * mtdparts=<mtddef>[;<mtddef]
9 * <mtddef> := <mtd-id>:<partdef>[,<partdef>] 9 * <mtddef> := <mtd-id>:<partdef>[,<partdef>]
10 * where <mtd-id> is the name from the "cat /proc/mtd" command
10 * <partdef> := <size>[@offset][<name>][ro][lk] 11 * <partdef> := <size>[@offset][<name>][ro][lk]
11 * <mtd-id> := unique name used in mapping driver/device (mtd->name) 12 * <mtd-id> := unique name used in mapping driver/device (mtd->name)
12 * <size> := standard linux memsize OR "-" to denote all remaining space 13 * <size> := standard linux memsize OR "-" to denote all remaining space
13 * <name> := '(' NAME ')' 14 * <name> := '(' NAME ')'
14 * 15 *
15 * Examples: 16 * Examples:
16 * 17 *
17 * 1 NOR Flash, with 1 single writable partition: 18 * 1 NOR Flash, with 1 single writable partition:
18 * edb7312-nor:- 19 * edb7312-nor:-
19 * 20 *
20 * 1 NOR Flash with 2 partitions, 1 NAND with one 21 * 1 NOR Flash with 2 partitions, 1 NAND with one
21 * edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home) 22 * edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
22 */ 23 */
23 24
24 #include <linux/kernel.h> 25 #include <linux/kernel.h>
25 #include <linux/slab.h> 26 #include <linux/slab.h>
26 27
27 #include <linux/mtd/mtd.h> 28 #include <linux/mtd/mtd.h>
28 #include <linux/mtd/partitions.h> 29 #include <linux/mtd/partitions.h>
29 #include <linux/bootmem.h> 30 #include <linux/bootmem.h>
30 31
31 /* error message prefix */ 32 /* error message prefix */
32 #define ERRP "mtd: " 33 #define ERRP "mtd: "
33 34
34 /* debug macro */ 35 /* debug macro */
35 #if 0 36 #if 0
36 #define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0) 37 #define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0)
37 #else 38 #else
38 #define dbg(x) 39 #define dbg(x)
39 #endif 40 #endif
40 41
41 42
42 /* special size referring to all the remaining space in a partition */ 43 /* special size referring to all the remaining space in a partition */
43 #define SIZE_REMAINING UINT_MAX 44 #define SIZE_REMAINING UINT_MAX
44 #define OFFSET_CONTINUOUS UINT_MAX 45 #define OFFSET_CONTINUOUS UINT_MAX
45 46
46 struct cmdline_mtd_partition { 47 struct cmdline_mtd_partition {
47 struct cmdline_mtd_partition *next; 48 struct cmdline_mtd_partition *next;
48 char *mtd_id; 49 char *mtd_id;
49 int num_parts; 50 int num_parts;
50 struct mtd_partition *parts; 51 struct mtd_partition *parts;
51 }; 52 };
52 53
53 /* mtdpart_setup() parses into here */ 54 /* mtdpart_setup() parses into here */
54 static struct cmdline_mtd_partition *partitions; 55 static struct cmdline_mtd_partition *partitions;
55 56
56 /* the command line passed to mtdpart_setupd() */ 57 /* the command line passed to mtdpart_setupd() */
57 static char *cmdline; 58 static char *cmdline;
58 static int cmdline_parsed = 0; 59 static int cmdline_parsed = 0;
59 60
60 /* 61 /*
61 * Parse one partition definition for an MTD. Since there can be many 62 * Parse one partition definition for an MTD. Since there can be many
62 * comma separated partition definitions, this function calls itself 63 * comma separated partition definitions, this function calls itself
63 * recursively until no more partition definitions are found. Nice side 64 * recursively until no more partition definitions are found. Nice side
64 * effect: the memory to keep the mtd_partition structs and the names 65 * effect: the memory to keep the mtd_partition structs and the names
65 * is allocated upon the last definition being found. At that point the 66 * is allocated upon the last definition being found. At that point the
66 * syntax has been verified ok. 67 * syntax has been verified ok.
67 */ 68 */
68 static struct mtd_partition * newpart(char *s, 69 static struct mtd_partition * newpart(char *s,
69 char **retptr, 70 char **retptr,
70 int *num_parts, 71 int *num_parts,
71 int this_part, 72 int this_part,
72 unsigned char **extra_mem_ptr, 73 unsigned char **extra_mem_ptr,
73 int extra_mem_size) 74 int extra_mem_size)
74 { 75 {
75 struct mtd_partition *parts; 76 struct mtd_partition *parts;
76 unsigned long size; 77 unsigned long size;
77 unsigned long offset = OFFSET_CONTINUOUS; 78 unsigned long offset = OFFSET_CONTINUOUS;
78 char *name; 79 char *name;
79 int name_len; 80 int name_len;
80 unsigned char *extra_mem; 81 unsigned char *extra_mem;
81 char delim; 82 char delim;
82 unsigned int mask_flags; 83 unsigned int mask_flags;
83 84
84 /* fetch the partition size */ 85 /* fetch the partition size */
85 if (*s == '-') 86 if (*s == '-')
86 { /* assign all remaining space to this partition */ 87 { /* assign all remaining space to this partition */
87 size = SIZE_REMAINING; 88 size = SIZE_REMAINING;
88 s++; 89 s++;
89 } 90 }
90 else 91 else
91 { 92 {
92 size = memparse(s, &s); 93 size = memparse(s, &s);
93 if (size < PAGE_SIZE) 94 if (size < PAGE_SIZE)
94 { 95 {
95 printk(KERN_ERR ERRP "partition size too small (%lx)\n", size); 96 printk(KERN_ERR ERRP "partition size too small (%lx)\n", size);
96 return NULL; 97 return NULL;
97 } 98 }
98 } 99 }
99 100
100 /* fetch partition name and flags */ 101 /* fetch partition name and flags */
101 mask_flags = 0; /* this is going to be a regular partition */ 102 mask_flags = 0; /* this is going to be a regular partition */
102 delim = 0; 103 delim = 0;
103 /* check for offset */ 104 /* check for offset */
104 if (*s == '@') 105 if (*s == '@')
105 { 106 {
106 s++; 107 s++;
107 offset = memparse(s, &s); 108 offset = memparse(s, &s);
108 } 109 }
109 /* now look for name */ 110 /* now look for name */
110 if (*s == '(') 111 if (*s == '(')
111 { 112 {
112 delim = ')'; 113 delim = ')';
113 } 114 }
114 115
115 if (delim) 116 if (delim)
116 { 117 {
117 char *p; 118 char *p;
118 119
119 name = ++s; 120 name = ++s;
120 p = strchr(name, delim); 121 p = strchr(name, delim);
121 if (!p) 122 if (!p)
122 { 123 {
123 printk(KERN_ERR ERRP "no closing %c found in partition name\n", delim); 124 printk(KERN_ERR ERRP "no closing %c found in partition name\n", delim);
124 return NULL; 125 return NULL;
125 } 126 }
126 name_len = p - name; 127 name_len = p - name;
127 s = p + 1; 128 s = p + 1;
128 } 129 }
129 else 130 else
130 { 131 {
131 name = NULL; 132 name = NULL;
132 name_len = 13; /* Partition_000 */ 133 name_len = 13; /* Partition_000 */
133 } 134 }
134 135
135 /* record name length for memory allocation later */ 136 /* record name length for memory allocation later */
136 extra_mem_size += name_len + 1; 137 extra_mem_size += name_len + 1;
137 138
138 /* test for options */ 139 /* test for options */
139 if (strncmp(s, "ro", 2) == 0) 140 if (strncmp(s, "ro", 2) == 0)
140 { 141 {
141 mask_flags |= MTD_WRITEABLE; 142 mask_flags |= MTD_WRITEABLE;
142 s += 2; 143 s += 2;
143 } 144 }
144 145
145 /* if lk is found do NOT unlock the MTD partition*/ 146 /* if lk is found do NOT unlock the MTD partition*/
146 if (strncmp(s, "lk", 2) == 0) 147 if (strncmp(s, "lk", 2) == 0)
147 { 148 {
148 mask_flags |= MTD_POWERUP_LOCK; 149 mask_flags |= MTD_POWERUP_LOCK;
149 s += 2; 150 s += 2;
150 } 151 }
151 152
152 /* test if more partitions are following */ 153 /* test if more partitions are following */
153 if (*s == ',') 154 if (*s == ',')
154 { 155 {
155 if (size == SIZE_REMAINING) 156 if (size == SIZE_REMAINING)
156 { 157 {
157 printk(KERN_ERR ERRP "no partitions allowed after a fill-up partition\n"); 158 printk(KERN_ERR ERRP "no partitions allowed after a fill-up partition\n");
158 return NULL; 159 return NULL;
159 } 160 }
160 /* more partitions follow, parse them */ 161 /* more partitions follow, parse them */
161 parts = newpart(s + 1, &s, num_parts, this_part + 1, 162 parts = newpart(s + 1, &s, num_parts, this_part + 1,
162 &extra_mem, extra_mem_size); 163 &extra_mem, extra_mem_size);
163 if (!parts) 164 if (!parts)
164 return NULL; 165 return NULL;
165 } 166 }
166 else 167 else
167 { /* this is the last partition: allocate space for all */ 168 { /* this is the last partition: allocate space for all */
168 int alloc_size; 169 int alloc_size;
169 170
170 *num_parts = this_part + 1; 171 *num_parts = this_part + 1;
171 alloc_size = *num_parts * sizeof(struct mtd_partition) + 172 alloc_size = *num_parts * sizeof(struct mtd_partition) +
172 extra_mem_size; 173 extra_mem_size;
173 parts = kzalloc(alloc_size, GFP_KERNEL); 174 parts = kzalloc(alloc_size, GFP_KERNEL);
174 if (!parts) 175 if (!parts)
175 { 176 {
176 printk(KERN_ERR ERRP "out of memory\n"); 177 printk(KERN_ERR ERRP "out of memory\n");
177 return NULL; 178 return NULL;
178 } 179 }
179 extra_mem = (unsigned char *)(parts + *num_parts); 180 extra_mem = (unsigned char *)(parts + *num_parts);
180 } 181 }
181 /* enter this partition (offset will be calculated later if it is zero at this point) */ 182 /* enter this partition (offset will be calculated later if it is zero at this point) */
182 parts[this_part].size = size; 183 parts[this_part].size = size;
183 parts[this_part].offset = offset; 184 parts[this_part].offset = offset;
184 parts[this_part].mask_flags = mask_flags; 185 parts[this_part].mask_flags = mask_flags;
185 if (name) 186 if (name)
186 { 187 {
187 strlcpy(extra_mem, name, name_len + 1); 188 strlcpy(extra_mem, name, name_len + 1);
188 } 189 }
189 else 190 else
190 { 191 {
191 sprintf(extra_mem, "Partition_%03d", this_part); 192 sprintf(extra_mem, "Partition_%03d", this_part);
192 } 193 }
193 parts[this_part].name = extra_mem; 194 parts[this_part].name = extra_mem;
194 extra_mem += name_len + 1; 195 extra_mem += name_len + 1;
195 196
196 dbg(("partition %d: name <%s>, offset %x, size %x, mask flags %x\n", 197 dbg(("partition %d: name <%s>, offset %x, size %x, mask flags %x\n",
197 this_part, 198 this_part,
198 parts[this_part].name, 199 parts[this_part].name,
199 parts[this_part].offset, 200 parts[this_part].offset,
200 parts[this_part].size, 201 parts[this_part].size,
201 parts[this_part].mask_flags)); 202 parts[this_part].mask_flags));
202 203
203 /* return (updated) pointer to extra_mem memory */ 204 /* return (updated) pointer to extra_mem memory */
204 if (extra_mem_ptr) 205 if (extra_mem_ptr)
205 *extra_mem_ptr = extra_mem; 206 *extra_mem_ptr = extra_mem;
206 207
207 /* return (updated) pointer command line string */ 208 /* return (updated) pointer command line string */
208 *retptr = s; 209 *retptr = s;
209 210
210 /* return partition table */ 211 /* return partition table */
211 return parts; 212 return parts;
212 } 213 }
213 214
214 /* 215 /*
215 * Parse the command line. 216 * Parse the command line.
216 */ 217 */
217 static int mtdpart_setup_real(char *s) 218 static int mtdpart_setup_real(char *s)
218 { 219 {
219 cmdline_parsed = 1; 220 cmdline_parsed = 1;
220 221
221 for( ; s != NULL; ) 222 for( ; s != NULL; )
222 { 223 {
223 struct cmdline_mtd_partition *this_mtd; 224 struct cmdline_mtd_partition *this_mtd;
224 struct mtd_partition *parts; 225 struct mtd_partition *parts;
225 int mtd_id_len; 226 int mtd_id_len;
226 int num_parts; 227 int num_parts;
227 char *p, *mtd_id; 228 char *p, *mtd_id;
228 229
229 mtd_id = s; 230 mtd_id = s;
230 /* fetch <mtd-id> */ 231 /* fetch <mtd-id> */
231 if (!(p = strchr(s, ':'))) 232 if (!(p = strchr(s, ':')))
232 { 233 {
233 printk(KERN_ERR ERRP "no mtd-id\n"); 234 printk(KERN_ERR ERRP "no mtd-id\n");
234 return 0; 235 return 0;
235 } 236 }
236 mtd_id_len = p - mtd_id; 237 mtd_id_len = p - mtd_id;
237 238
238 dbg(("parsing <%s>\n", p+1)); 239 dbg(("parsing <%s>\n", p+1));
239 240
240 /* 241 /*
241 * parse one mtd. have it reserve memory for the 242 * parse one mtd. have it reserve memory for the
242 * struct cmdline_mtd_partition and the mtd-id string. 243 * struct cmdline_mtd_partition and the mtd-id string.
243 */ 244 */
244 parts = newpart(p + 1, /* cmdline */ 245 parts = newpart(p + 1, /* cmdline */
245 &s, /* out: updated cmdline ptr */ 246 &s, /* out: updated cmdline ptr */
246 &num_parts, /* out: number of parts */ 247 &num_parts, /* out: number of parts */
247 0, /* first partition */ 248 0, /* first partition */
248 (unsigned char**)&this_mtd, /* out: extra mem */ 249 (unsigned char**)&this_mtd, /* out: extra mem */
249 mtd_id_len + 1 + sizeof(*this_mtd) + 250 mtd_id_len + 1 + sizeof(*this_mtd) +
250 sizeof(void*)-1 /*alignment*/); 251 sizeof(void*)-1 /*alignment*/);
251 if(!parts) 252 if(!parts)
252 { 253 {
253 /* 254 /*
254 * An error occurred. We're either: 255 * An error occurred. We're either:
255 * a) out of memory, or 256 * a) out of memory, or
256 * b) in the middle of the partition spec 257 * b) in the middle of the partition spec
257 * Either way, this mtd is hosed and we're 258 * Either way, this mtd is hosed and we're
258 * unlikely to succeed in parsing any more 259 * unlikely to succeed in parsing any more
259 */ 260 */
260 return 0; 261 return 0;
261 } 262 }
262 263
263 /* align this_mtd */ 264 /* align this_mtd */
264 this_mtd = (struct cmdline_mtd_partition *) 265 this_mtd = (struct cmdline_mtd_partition *)
265 ALIGN((unsigned long)this_mtd, sizeof(void*)); 266 ALIGN((unsigned long)this_mtd, sizeof(void*));
266 /* enter results */ 267 /* enter results */
267 this_mtd->parts = parts; 268 this_mtd->parts = parts;
268 this_mtd->num_parts = num_parts; 269 this_mtd->num_parts = num_parts;
269 this_mtd->mtd_id = (char*)(this_mtd + 1); 270 this_mtd->mtd_id = (char*)(this_mtd + 1);
270 strlcpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1); 271 strlcpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1);
271 272
272 /* link into chain */ 273 /* link into chain */
273 this_mtd->next = partitions; 274 this_mtd->next = partitions;
274 partitions = this_mtd; 275 partitions = this_mtd;
275 276
276 dbg(("mtdid=<%s> num_parts=<%d>\n", 277 dbg(("mtdid=<%s> num_parts=<%d>\n",
277 this_mtd->mtd_id, this_mtd->num_parts)); 278 this_mtd->mtd_id, this_mtd->num_parts));
278 279
279 280
280 /* EOS - we're done */ 281 /* EOS - we're done */
281 if (*s == 0) 282 if (*s == 0)
282 break; 283 break;
283 284
284 /* does another spec follow? */ 285 /* does another spec follow? */
285 if (*s != ';') 286 if (*s != ';')
286 { 287 {
287 printk(KERN_ERR ERRP "bad character after partition (%c)\n", *s); 288 printk(KERN_ERR ERRP "bad character after partition (%c)\n", *s);
288 return 0; 289 return 0;
289 } 290 }
290 s++; 291 s++;
291 } 292 }
292 return 1; 293 return 1;
293 } 294 }
294 295
295 /* 296 /*
296 * Main function to be called from the MTD mapping driver/device to 297 * Main function to be called from the MTD mapping driver/device to
297 * obtain the partitioning information. At this point the command line 298 * obtain the partitioning information. At this point the command line
298 * arguments will actually be parsed and turned to struct mtd_partition 299 * arguments will actually be parsed and turned to struct mtd_partition
299 * information. It returns partitions for the requested mtd device, or 300 * information. It returns partitions for the requested mtd device, or
300 * the first one in the chain if a NULL mtd_id is passed in. 301 * the first one in the chain if a NULL mtd_id is passed in.
301 */ 302 */
302 static int parse_cmdline_partitions(struct mtd_info *master, 303 static int parse_cmdline_partitions(struct mtd_info *master,
303 struct mtd_partition **pparts, 304 struct mtd_partition **pparts,
304 unsigned long origin) 305 unsigned long origin)
305 { 306 {
306 unsigned long offset; 307 unsigned long offset;
307 int i; 308 int i;
308 struct cmdline_mtd_partition *part; 309 struct cmdline_mtd_partition *part;
309 const char *mtd_id = master->name; 310 const char *mtd_id = master->name;
310 311
311 /* parse command line */ 312 /* parse command line */
312 if (!cmdline_parsed) 313 if (!cmdline_parsed)
313 mtdpart_setup_real(cmdline); 314 mtdpart_setup_real(cmdline);
314 315
315 for(part = partitions; part; part = part->next) 316 for(part = partitions; part; part = part->next)
316 { 317 {
317 if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id))) 318 if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
318 { 319 {
319 for(i = 0, offset = 0; i < part->num_parts; i++) 320 for(i = 0, offset = 0; i < part->num_parts; i++)
320 { 321 {
321 if (part->parts[i].offset == OFFSET_CONTINUOUS) 322 if (part->parts[i].offset == OFFSET_CONTINUOUS)
322 part->parts[i].offset = offset; 323 part->parts[i].offset = offset;
323 else 324 else
324 offset = part->parts[i].offset; 325 offset = part->parts[i].offset;
325 if (part->parts[i].size == SIZE_REMAINING) 326 if (part->parts[i].size == SIZE_REMAINING)
326 part->parts[i].size = master->size - offset; 327 part->parts[i].size = master->size - offset;
327 if (offset + part->parts[i].size > master->size) 328 if (offset + part->parts[i].size > master->size)
328 { 329 {
329 printk(KERN_WARNING ERRP 330 printk(KERN_WARNING ERRP
330 "%s: partitioning exceeds flash size, truncating\n", 331 "%s: partitioning exceeds flash size, truncating\n",
331 part->mtd_id); 332 part->mtd_id);
332 part->parts[i].size = master->size - offset; 333 part->parts[i].size = master->size - offset;
333 part->num_parts = i; 334 part->num_parts = i;
334 } 335 }
335 offset += part->parts[i].size; 336 offset += part->parts[i].size;
336 } 337 }
337 *pparts = part->parts; 338 *pparts = part->parts;
338 return part->num_parts; 339 return part->num_parts;
339 } 340 }
340 } 341 }
341 return 0; 342 return 0;
342 } 343 }
343 344
344 345
345 /* 346 /*
346 * This is the handler for our kernel parameter, called from 347 * This is the handler for our kernel parameter, called from
347 * main.c::checksetup(). Note that we can not yet kmalloc() anything, 348 * main.c::checksetup(). Note that we can not yet kmalloc() anything,
348 * so we only save the commandline for later processing. 349 * so we only save the commandline for later processing.
349 * 350 *
350 * This function needs to be visible for bootloaders. 351 * This function needs to be visible for bootloaders.
351 */ 352 */
352 static int mtdpart_setup(char *s) 353 static int mtdpart_setup(char *s)
353 { 354 {
354 cmdline = s; 355 cmdline = s;
355 return 1; 356 return 1;
356 } 357 }
357 358
358 __setup("mtdparts=", mtdpart_setup); 359 __setup("mtdparts=", mtdpart_setup);
359 360
360 static struct mtd_part_parser cmdline_parser = { 361 static struct mtd_part_parser cmdline_parser = {
361 .owner = THIS_MODULE, 362 .owner = THIS_MODULE,
362 .parse_fn = parse_cmdline_partitions, 363 .parse_fn = parse_cmdline_partitions,
363 .name = "cmdlinepart", 364 .name = "cmdlinepart",
364 }; 365 };
365 366
366 static int __init cmdline_parser_init(void) 367 static int __init cmdline_parser_init(void)
367 { 368 {
368 return register_mtd_parser(&cmdline_parser); 369 return register_mtd_parser(&cmdline_parser);
369 } 370 }
370 371
371 module_init(cmdline_parser_init); 372 module_init(cmdline_parser_init);
372 373
373 MODULE_LICENSE("GPL"); 374 MODULE_LICENSE("GPL");
374 MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>"); 375 MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>");
375 MODULE_DESCRIPTION("Command line configuration of MTD partitions"); 376 MODULE_DESCRIPTION("Command line configuration of MTD partitions");
376 377