Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 2 | * $Id: cmdlinepart.c,v 1.19 2005/11/07 11:14:19 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * mtdparts=<mtddef>[;<mtddef] |
| 11 | * <mtddef> := <mtd-id>:<partdef>[,<partdef>] |
Justin Treon | e619a75 | 2008-01-30 10:25:49 -0800 | [diff] [blame] | 12 | * <partdef> := <size>[@offset][<name>][ro][lk] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * <mtd-id> := unique name used in mapping driver/device (mtd->name) |
| 14 | * <size> := standard linux memsize OR "-" to denote all remaining space |
| 15 | * <name> := '(' NAME ')' |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 16 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * Examples: |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 18 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * 1 NOR Flash, with 1 single writable partition: |
| 20 | * edb7312-nor:- |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 21 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Atsushi Nemoto | b175d03 | 2006-03-31 02:29:46 -0800 | [diff] [blame] | 45 | #define SIZE_REMAINING UINT_MAX |
| 46 | #define OFFSET_CONTINUOUS UINT_MAX |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | struct cmdline_mtd_partition { |
| 49 | struct cmdline_mtd_partition *next; |
| 50 | char *mtd_id; |
| 51 | int num_parts; |
| 52 | struct mtd_partition *parts; |
| 53 | }; |
| 54 | |
| 55 | /* mtdpart_setup() parses into here */ |
| 56 | static struct cmdline_mtd_partition *partitions; |
| 57 | |
| 58 | /* the command line passed to mtdpart_setupd() */ |
| 59 | static char *cmdline; |
| 60 | static int cmdline_parsed = 0; |
| 61 | |
| 62 | /* |
| 63 | * Parse one partition definition for an MTD. Since there can be many |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 64 | * comma separated partition definitions, this function calls itself |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | * recursively until no more partition definitions are found. Nice side |
| 66 | * effect: the memory to keep the mtd_partition structs and the names |
| 67 | * is allocated upon the last definition being found. At that point the |
| 68 | * syntax has been verified ok. |
| 69 | */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 70 | static struct mtd_partition * newpart(char *s, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | char **retptr, |
| 72 | int *num_parts, |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 73 | int this_part, |
| 74 | unsigned char **extra_mem_ptr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | int extra_mem_size) |
| 76 | { |
| 77 | struct mtd_partition *parts; |
| 78 | unsigned long size; |
Atsushi Nemoto | b175d03 | 2006-03-31 02:29:46 -0800 | [diff] [blame] | 79 | unsigned long offset = OFFSET_CONTINUOUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | char *name; |
| 81 | int name_len; |
| 82 | unsigned char *extra_mem; |
| 83 | char delim; |
| 84 | unsigned int mask_flags; |
| 85 | |
| 86 | /* fetch the partition size */ |
| 87 | if (*s == '-') |
| 88 | { /* assign all remaining space to this partition */ |
| 89 | size = SIZE_REMAINING; |
| 90 | s++; |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | size = memparse(s, &s); |
| 95 | if (size < PAGE_SIZE) |
| 96 | { |
| 97 | printk(KERN_ERR ERRP "partition size too small (%lx)\n", size); |
| 98 | return NULL; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /* fetch partition name and flags */ |
| 103 | mask_flags = 0; /* this is going to be a regular partition */ |
| 104 | delim = 0; |
| 105 | /* check for offset */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 106 | if (*s == '@') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
| 108 | s++; |
| 109 | offset = memparse(s, &s); |
| 110 | } |
| 111 | /* now look for name */ |
| 112 | if (*s == '(') |
| 113 | { |
| 114 | delim = ')'; |
| 115 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | if (delim) |
| 118 | { |
| 119 | char *p; |
| 120 | |
| 121 | name = ++s; |
Adrian Bunk | ed262c4 | 2008-04-14 17:20:04 +0300 | [diff] [blame] | 122 | p = strchr(name, delim); |
| 123 | if (!p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | printk(KERN_ERR ERRP "no closing %c found in partition name\n", delim); |
| 126 | return NULL; |
| 127 | } |
| 128 | name_len = p - name; |
| 129 | s = p + 1; |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | name = NULL; |
| 134 | name_len = 13; /* Partition_000 */ |
| 135 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | /* record name length for memory allocation later */ |
| 138 | extra_mem_size += name_len + 1; |
| 139 | |
| 140 | /* test for options */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 141 | if (strncmp(s, "ro", 2) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
| 143 | mask_flags |= MTD_WRITEABLE; |
| 144 | s += 2; |
| 145 | } |
| 146 | |
Justin Treon | e619a75 | 2008-01-30 10:25:49 -0800 | [diff] [blame] | 147 | /* if lk is found do NOT unlock the MTD partition*/ |
| 148 | if (strncmp(s, "lk", 2) == 0) |
| 149 | { |
| 150 | mask_flags |= MTD_POWERUP_LOCK; |
| 151 | s += 2; |
| 152 | } |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 NULL; |
| 161 | } |
| 162 | /* more partitions follow, parse them */ |
Adrian Bunk | ed262c4 | 2008-04-14 17:20:04 +0300 | [diff] [blame] | 163 | parts = newpart(s + 1, &s, num_parts, this_part + 1, |
| 164 | &extra_mem, extra_mem_size); |
| 165 | if (!parts) |
| 166 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | else |
| 169 | { /* this is the last partition: allocate space for all */ |
| 170 | int alloc_size; |
| 171 | |
| 172 | *num_parts = this_part + 1; |
| 173 | alloc_size = *num_parts * sizeof(struct mtd_partition) + |
| 174 | extra_mem_size; |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 175 | parts = kzalloc(alloc_size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | if (!parts) |
| 177 | { |
| 178 | printk(KERN_ERR ERRP "out of memory\n"); |
| 179 | return NULL; |
| 180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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", |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 199 | this_part, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 216 | /* |
| 217 | * Parse the command line. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 242 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 251 | mtd_id_len + 1 + sizeof(*this_mtd) + |
Joern Engel | be76c5f | 2005-06-07 16:04:29 +0100 | [diff] [blame] | 252 | sizeof(void*)-1 /*alignment*/); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | if(!parts) |
| 254 | { |
| 255 | /* |
| 256 | * An error occurred. We're either: |
| 257 | * a) out of memory, or |
| 258 | * b) in the middle of the partition spec |
| 259 | * Either way, this mtd is hosed and we're |
| 260 | * unlikely to succeed in parsing any more |
| 261 | */ |
| 262 | return 0; |
| 263 | } |
| 264 | |
Joern Engel | be76c5f | 2005-06-07 16:04:29 +0100 | [diff] [blame] | 265 | /* align this_mtd */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 266 | this_mtd = (struct cmdline_mtd_partition *) |
Joern Engel | be76c5f | 2005-06-07 16:04:29 +0100 | [diff] [blame] | 267 | ALIGN((unsigned long)this_mtd, sizeof(void*)); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 268 | /* enter results */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | this_mtd->parts = parts; |
| 270 | this_mtd->num_parts = num_parts; |
| 271 | this_mtd->mtd_id = (char*)(this_mtd + 1); |
| 272 | strlcpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1); |
| 273 | |
| 274 | /* link into chain */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 275 | this_mtd->next = partitions; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | partitions = this_mtd; |
| 277 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 278 | dbg(("mtdid=<%s> num_parts=<%d>\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | this_mtd->mtd_id, this_mtd->num_parts)); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
| 282 | /* EOS - we're done */ |
| 283 | if (*s == 0) |
| 284 | break; |
| 285 | |
| 286 | /* does another spec follow? */ |
| 287 | if (*s != ';') |
| 288 | { |
| 289 | printk(KERN_ERR ERRP "bad character after partition (%c)\n", *s); |
| 290 | return 0; |
| 291 | } |
| 292 | s++; |
| 293 | } |
| 294 | return 1; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Main function to be called from the MTD mapping driver/device to |
| 299 | * obtain the partitioning information. At this point the command line |
| 300 | * arguments will actually be parsed and turned to struct mtd_partition |
| 301 | * information. It returns partitions for the requested mtd device, or |
| 302 | * the first one in the chain if a NULL mtd_id is passed in. |
| 303 | */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 304 | static int parse_cmdline_partitions(struct mtd_info *master, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | struct mtd_partition **pparts, |
| 306 | unsigned long origin) |
| 307 | { |
| 308 | unsigned long offset; |
| 309 | int i; |
| 310 | struct cmdline_mtd_partition *part; |
| 311 | char *mtd_id = master->name; |
| 312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | /* parse command line */ |
| 314 | if (!cmdline_parsed) |
| 315 | mtdpart_setup_real(cmdline); |
| 316 | |
| 317 | for(part = partitions; part; part = part->next) |
| 318 | { |
| 319 | if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id))) |
| 320 | { |
| 321 | for(i = 0, offset = 0; i < part->num_parts; i++) |
| 322 | { |
Atsushi Nemoto | b175d03 | 2006-03-31 02:29:46 -0800 | [diff] [blame] | 323 | if (part->parts[i].offset == OFFSET_CONTINUOUS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | part->parts[i].offset = offset; |
| 325 | else |
| 326 | offset = part->parts[i].offset; |
| 327 | if (part->parts[i].size == SIZE_REMAINING) |
| 328 | part->parts[i].size = master->size - offset; |
| 329 | if (offset + part->parts[i].size > master->size) |
| 330 | { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 331 | printk(KERN_WARNING ERRP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | "%s: partitioning exceeds flash size, truncating\n", |
| 333 | part->mtd_id); |
| 334 | part->parts[i].size = master->size - offset; |
| 335 | part->num_parts = i; |
| 336 | } |
| 337 | offset += part->parts[i].size; |
| 338 | } |
| 339 | *pparts = part->parts; |
| 340 | return part->num_parts; |
| 341 | } |
| 342 | } |
Peter Korsgaard | b0d06af | 2008-02-14 17:00:10 +0100 | [diff] [blame^] | 343 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 347 | /* |
| 348 | * This is the handler for our kernel parameter, called from |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | * main.c::checksetup(). Note that we can not yet kmalloc() anything, |
| 350 | * so we only save the commandline for later processing. |
| 351 | * |
| 352 | * This function needs to be visible for bootloaders. |
| 353 | */ |
Adrian Bunk | ddacff1 | 2006-11-25 20:15:41 +0100 | [diff] [blame] | 354 | static int mtdpart_setup(char *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
| 356 | cmdline = s; |
| 357 | return 1; |
| 358 | } |
| 359 | |
| 360 | __setup("mtdparts=", mtdpart_setup); |
| 361 | |
| 362 | static struct mtd_part_parser cmdline_parser = { |
| 363 | .owner = THIS_MODULE, |
| 364 | .parse_fn = parse_cmdline_partitions, |
| 365 | .name = "cmdlinepart", |
| 366 | }; |
| 367 | |
| 368 | static int __init cmdline_parser_init(void) |
| 369 | { |
| 370 | return register_mtd_parser(&cmdline_parser); |
| 371 | } |
| 372 | |
| 373 | module_init(cmdline_parser_init); |
| 374 | |
| 375 | MODULE_LICENSE("GPL"); |
| 376 | MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>"); |
| 377 | MODULE_DESCRIPTION("Command line configuration of MTD partitions"); |