cmdlinepart.c 11 KB

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