cmdlinepart.c 10 KB

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