cmdlinepart.c 10.0 KB

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