cmdlinepart.c 11 KB

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