PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/mtd/cmdlinepart.c

https://github.com/kipill-nn/Kernel-for-Mega
C | 376 lines | 248 code | 44 blank | 84 comment | 39 complexity | 37ee7154471b8f5bfaa2754178b42ae6 MD5 | raw file
  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. * where <mtd-id> is the name from the "cat /proc/mtd" command
  11. * <partdef> := <size>[@offset][<name>][ro][lk]
  12. * <mtd-id> := unique name used in mapping driver/device (mtd->name)
  13. * <size> := standard linux memsize OR "-" to denote all remaining space
  14. * <name> := '(' NAME ')'
  15. *
  16. * Examples:
  17. *
  18. * 1 NOR Flash, with 1 single writable partition:
  19. * edb7312-nor:-
  20. *
  21. * 1 NOR Flash with 2 partitions, 1 NAND with one
  22. * edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/mtd/partitions.h>
  28. #include <linux/bootmem.h>
  29. /* error message prefix */
  30. #define ERRP "mtd: "
  31. /* debug macro */
  32. #if 0
  33. #define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0)
  34. #else
  35. #define dbg(x)
  36. #endif
  37. /* special size referring to all the remaining space in a partition */
  38. #define SIZE_REMAINING UINT_MAX
  39. #define OFFSET_CONTINUOUS UINT_MAX
  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 = OFFSET_CONTINUOUS;
  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. p = strchr(name, delim);
  108. if (!p)
  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. /* if lk is found do NOT unlock the MTD partition*/
  130. if (strncmp(s, "lk", 2) == 0)
  131. {
  132. mask_flags |= MTD_POWERUP_LOCK;
  133. s += 2;
  134. }
  135. /* test if more partitions are following */
  136. if (*s == ',')
  137. {
  138. if (size == SIZE_REMAINING)
  139. {
  140. printk(KERN_ERR ERRP "no partitions allowed after a fill-up partition\n");
  141. return NULL;
  142. }
  143. /* more partitions follow, parse them */
  144. parts = newpart(s + 1, &s, num_parts, this_part + 1,
  145. &extra_mem, extra_mem_size);
  146. if (!parts)
  147. return NULL;
  148. }
  149. else
  150. { /* this is the last partition: allocate space for all */
  151. int alloc_size;
  152. *num_parts = this_part + 1;
  153. alloc_size = *num_parts * sizeof(struct mtd_partition) +
  154. extra_mem_size;
  155. parts = kzalloc(alloc_size, GFP_KERNEL);
  156. if (!parts)
  157. {
  158. printk(KERN_ERR ERRP "out of memory\n");
  159. return NULL;
  160. }
  161. extra_mem = (unsigned char *)(parts + *num_parts);
  162. }
  163. /* enter this partition (offset will be calculated later if it is zero at this point) */
  164. parts[this_part].size = size;
  165. parts[this_part].offset = offset;
  166. parts[this_part].mask_flags = mask_flags;
  167. if (name)
  168. {
  169. strlcpy(extra_mem, name, name_len + 1);
  170. }
  171. else
  172. {
  173. sprintf(extra_mem, "Partition_%03d", this_part);
  174. }
  175. parts[this_part].name = extra_mem;
  176. extra_mem += name_len + 1;
  177. dbg(("partition %d: name <%s>, offset %x, size %x, mask flags %x\n",
  178. this_part,
  179. parts[this_part].name,
  180. parts[this_part].offset,
  181. parts[this_part].size,
  182. parts[this_part].mask_flags));
  183. /* return (updated) pointer to extra_mem memory */
  184. if (extra_mem_ptr)
  185. *extra_mem_ptr = extra_mem;
  186. /* return (updated) pointer command line string */
  187. *retptr = s;
  188. /* return partition table */
  189. return parts;
  190. }
  191. /*
  192. * Parse the command line.
  193. */
  194. static int mtdpart_setup_real(char *s)
  195. {
  196. cmdline_parsed = 1;
  197. for( ; s != NULL; )
  198. {
  199. struct cmdline_mtd_partition *this_mtd;
  200. struct mtd_partition *parts;
  201. int mtd_id_len;
  202. int num_parts;
  203. char *p, *mtd_id;
  204. mtd_id = s;
  205. /* fetch <mtd-id> */
  206. if (!(p = strchr(s, ':')))
  207. {
  208. printk(KERN_ERR ERRP "no mtd-id\n");
  209. return 0;
  210. }
  211. mtd_id_len = p - mtd_id;
  212. dbg(("parsing <%s>\n", p+1));
  213. /*
  214. * parse one mtd. have it reserve memory for the
  215. * struct cmdline_mtd_partition and the mtd-id string.
  216. */
  217. parts = newpart(p + 1, /* cmdline */
  218. &s, /* out: updated cmdline ptr */
  219. &num_parts, /* out: number of parts */
  220. 0, /* first partition */
  221. (unsigned char**)&this_mtd, /* out: extra mem */
  222. mtd_id_len + 1 + sizeof(*this_mtd) +
  223. sizeof(void*)-1 /*alignment*/);
  224. if(!parts)
  225. {
  226. /*
  227. * An error occurred. We're either:
  228. * a) out of memory, or
  229. * b) in the middle of the partition spec
  230. * Either way, this mtd is hosed and we're
  231. * unlikely to succeed in parsing any more
  232. */
  233. return 0;
  234. }
  235. /* align this_mtd */
  236. this_mtd = (struct cmdline_mtd_partition *)
  237. ALIGN((unsigned long)this_mtd, sizeof(void*));
  238. /* enter results */
  239. this_mtd->parts = parts;
  240. this_mtd->num_parts = num_parts;
  241. this_mtd->mtd_id = (char*)(this_mtd + 1);
  242. strlcpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1);
  243. /* link into chain */
  244. this_mtd->next = partitions;
  245. partitions = this_mtd;
  246. dbg(("mtdid=<%s> num_parts=<%d>\n",
  247. this_mtd->mtd_id, this_mtd->num_parts));
  248. /* EOS - we're done */
  249. if (*s == 0)
  250. break;
  251. /* does another spec follow? */
  252. if (*s != ';')
  253. {
  254. printk(KERN_ERR ERRP "bad character after partition (%c)\n", *s);
  255. return 0;
  256. }
  257. s++;
  258. }
  259. return 1;
  260. }
  261. /*
  262. * Main function to be called from the MTD mapping driver/device to
  263. * obtain the partitioning information. At this point the command line
  264. * arguments will actually be parsed and turned to struct mtd_partition
  265. * information. It returns partitions for the requested mtd device, or
  266. * the first one in the chain if a NULL mtd_id is passed in.
  267. */
  268. static int parse_cmdline_partitions(struct mtd_info *master,
  269. struct mtd_partition **pparts,
  270. unsigned long origin)
  271. {
  272. unsigned long offset;
  273. int i;
  274. struct cmdline_mtd_partition *part;
  275. const char *mtd_id = master->name;
  276. /* parse command line */
  277. if (!cmdline_parsed)
  278. mtdpart_setup_real(cmdline);
  279. for(part = partitions; part; part = part->next)
  280. {
  281. if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
  282. {
  283. for(i = 0, offset = 0; i < part->num_parts; i++)
  284. {
  285. if (part->parts[i].offset == OFFSET_CONTINUOUS)
  286. part->parts[i].offset = offset;
  287. else
  288. offset = part->parts[i].offset;
  289. if (part->parts[i].size == SIZE_REMAINING)
  290. part->parts[i].size = master->size - offset;
  291. if (offset + part->parts[i].size > master->size)
  292. {
  293. printk(KERN_WARNING ERRP
  294. "%s: partitioning exceeds flash size, truncating\n",
  295. part->mtd_id);
  296. part->parts[i].size = master->size - offset;
  297. part->num_parts = i;
  298. }
  299. offset += part->parts[i].size;
  300. }
  301. *pparts = part->parts;
  302. return part->num_parts;
  303. }
  304. }
  305. return 0;
  306. }
  307. /*
  308. * This is the handler for our kernel parameter, called from
  309. * main.c::checksetup(). Note that we can not yet kmalloc() anything,
  310. * so we only save the commandline for later processing.
  311. *
  312. * This function needs to be visible for bootloaders.
  313. */
  314. static int mtdpart_setup(char *s)
  315. {
  316. cmdline = s;
  317. return 1;
  318. }
  319. __setup("mtdparts=", mtdpart_setup);
  320. static struct mtd_part_parser cmdline_parser = {
  321. .owner = THIS_MODULE,
  322. .parse_fn = parse_cmdline_partitions,
  323. .name = "cmdlinepart",
  324. };
  325. static int __init cmdline_parser_init(void)
  326. {
  327. return register_mtd_parser(&cmdline_parser);
  328. }
  329. module_init(cmdline_parser_init);
  330. MODULE_LICENSE("GPL");
  331. MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>");
  332. MODULE_DESCRIPTION("Command line configuration of MTD partitions");