cmdlinepart.c 9.0 KB

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