cmdlinepart.c 8.9 KB

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