cmdlinepart.c 9.0 KB

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