1 #include <linux/kernel.h>
2 #include <linux/slab.h>
3 #include <linux/bootmem.h>
8 /* error message prefix */
13 #define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0)
18 #define SECTOR_1G 0x200000 // 0x200000 * 512 = 1G
19 #define FROM_OFFSET 0x2000 // 4MB
21 /* special size referring to all the remaining space in a partition */
22 #define SIZE_REMAINING UINT_MAX
23 #define OFFSET_CONTINUOUS UINT_MAX
30 struct cmdline_mtd_partition {
31 struct cmdline_mtd_partition *next;
34 struct mtd_partition *parts;
37 /* mtdpart_setup() parses into here */
38 static struct cmdline_mtd_partition *partitions;
40 /* the command line passed to mtdpart_setupd() */
42 static int cmdline_parsed = 0;
45 * Parse one partition definition for an MTD. Since there can be many
46 * comma separated partition definitions, this function calls itself
47 * recursively until no more partition definitions are found. Nice side
48 * effect: the memory to keep the mtd_partition structs and the names
49 * is allocated upon the last definition being found. At that point the
50 * syntax has been verified ok.
52 static struct mtd_partition * newpart(char *s,
56 unsigned char **extra_mem_ptr,
59 struct mtd_partition *parts;
61 sector_t from = OFFSET_CONTINUOUS;
64 unsigned char *extra_mem;
67 /* fetch the partition size */
69 { /* assign all remaining space to this partition */
70 size = SIZE_REMAINING;
75 size = memparse(s, &s);
78 printk(KERN_ERR ERRP "partition size too small (%llx)\n", size);
83 /* fetch partition name */
89 from = memparse(s, &s);
91 /* now look for name */
102 p = strchr(name, delim);
105 printk(KERN_ERR ERRP "no closing %c found in partition name\n", delim);
114 name_len = 13; /* Partition_000 */
117 /* record name length for memory allocation later */
118 extra_mem_size += name_len + 1;
120 /* test if more partitions are following */
123 if (size == SIZE_REMAINING)
125 printk(KERN_ERR ERRP "no partitions allowed after a fill-up partition\n");
128 /* more partitions follow, parse them */
129 parts = newpart(s + 1, &s, num_parts, this_part + 1,
130 &extra_mem, extra_mem_size);
135 { /* this is the last partition: allocate space for all */
138 *num_parts = this_part + 1;
139 alloc_size = *num_parts * sizeof(struct mtd_partition) +
141 parts = kzalloc(alloc_size, GFP_KERNEL);
144 printk(KERN_ERR ERRP "out of memory\n");
147 extra_mem = (unsigned char *)(parts + *num_parts);
149 /* enter this partition (from will be calculated later if it is zero at this point) */
150 parts[this_part].size = size;
151 parts[this_part].from = from;
154 strlcpy(extra_mem, name, name_len + 1);
158 sprintf(extra_mem, "Partition_%03d", this_part);
160 parts[this_part].name = extra_mem;
161 extra_mem += name_len + 1;
163 dbg(("partition %d: name <%s>, from %llx, size %llx\n",
165 parts[this_part].name,
166 parts[this_part].from,
167 parts[this_part].size));
169 /* return (updated) pointer to extra_mem memory */
171 *extra_mem_ptr = extra_mem;
173 /* return (updated) pointer command line string */
176 /* return partition table */
181 * Parse the command line.
183 static int mtdpart_setup_real(char *s)
189 struct cmdline_mtd_partition *this_mtd;
190 struct mtd_partition *parts;
197 if (!(p = strchr(s, ':')))
199 dbg(( "no mtd-id\n"));
202 mtd_id_len = p - mtd_id;
204 dbg(("parsing <%s>\n", p+1));
207 * parse one mtd. have it reserve memory for the
208 * struct cmdline_mtd_partition and the mtd-id string.
210 parts = newpart(p + 1, /* cmdline */
211 &s, /* out: updated cmdline ptr */
212 &num_parts, /* out: number of parts */
213 0, /* first partition */
214 (unsigned char**)&this_mtd, /* out: extra mem */
215 mtd_id_len + 1 + sizeof(*this_mtd) +
216 sizeof(void*)-1 /*alignment*/);
220 * An error occurred. We're either:
221 * a) out of memory, or
222 * b) in the middle of the partition spec
223 * Either way, this mtd is hosed and we're
224 * unlikely to succeed in parsing any more
230 this_mtd = (struct cmdline_mtd_partition *)
231 ALIGN((unsigned long)this_mtd, sizeof(void*));
233 this_mtd->parts = parts;
234 this_mtd->num_parts = num_parts;
235 this_mtd->mtd_id = (char*)(this_mtd + 1);
236 strlcpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1);
238 /* link into chain */
239 this_mtd->next = partitions;
240 partitions = this_mtd;
242 dbg(("mtdid=<%s> num_parts=<%d>\n",
243 this_mtd->mtd_id, this_mtd->num_parts));
245 /* EOS - we're done */
249 /* does another spec follow? */
252 printk(KERN_ERR ERRP "bad character after partition (%c)\n", *s);
262 * Main function to be called from the MTD mapping driver/device to
263 * obtain the partitioning information. At this point the command line
264 * arguments will actually be parsed and turned to struct mtd_partition
265 * information. It returns partitions for the requested mtd device, or
266 * the first one in the chain if a NULL mtd_id is passed in.
268 static int parse_cmdline_partitions(sector_t n,
269 struct mtd_partition **pparts,
270 unsigned long origin)
274 struct cmdline_mtd_partition *part;
275 const char *mtd_id = "rk29xxnand";
277 /* parse command line */
279 mtdpart_setup_real(cmdline);
281 for(part = partitions; part; part = part->next)
283 if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
285 for(i = 0, from = 0; i < part->num_parts; i++)
287 if (part->parts[i].from == OFFSET_CONTINUOUS)
288 part->parts[i].from = from;
290 from = part->parts[i].from;
291 if (part->parts[i].size == SIZE_REMAINING)
292 part->parts[i].size = n - from - FROM_OFFSET;
293 if (from + part->parts[i].size > n)
295 printk(KERN_WARNING ERRP
296 "%s: partitioning exceeds flash size, truncating\n",
298 part->parts[i].size = n - from;
301 from += part->parts[i].size;
303 *pparts = kmemdup(part->parts,
304 sizeof(*part->parts) * part->num_parts,
308 return part->num_parts;
314 int mtdpart_partition(struct parsed_partitions *state)
316 int num_parts = 0, i;
317 sector_t n = get_capacity(state->bdev->bd_disk);
318 struct mtd_partition *parts = NULL;
323 cmdline = strstr(saved_command_line, "mtdparts=") + 9;
325 num_parts = parse_cmdline_partitions(n, &parts, 0);
329 for(i = 0; i < num_parts; i++){
330 put_partition(state, i, parts[i].from + FROM_OFFSET, parts[i].size);
331 strcpy(state->parts[i].info.volname, parts[i].name);
332 printk(KERN_INFO "%10s: 0x%09llx -- 0x%09llx (%llu MB)\n",
335 (parts[i].from + parts[i].size) * 512,
336 parts[i].size / 2048);