cmdlinepart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Read flash partition table from command line
  4. *
  5. * Copyright © 2002 SYSGO Real-Time Solutions GmbH
  6. * Copyright © 2002-2010 David Woodhouse <[email protected]>
  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][lk][slc]
  13. * <mtd-id> := unique name used in mapping driver/device (mtd->name)
  14. * <size> := standard linux memsize OR "-" to denote all remaining space
  15. * size is automatically truncated at end of device
  16. * if specified or truncated size is 0 the part is skipped
  17. * <offset> := standard linux memsize
  18. * if omitted the part will immediately follow the previous part
  19. * or 0 if the first part
  20. * <name> := '(' NAME ')'
  21. * NAME will appear in /proc/mtd
  22. *
  23. * <size> and <offset> can be specified such that the parts are out of order
  24. * in physical memory and may even overlap.
  25. *
  26. * The parts are assigned MTD numbers in the order they are specified in the
  27. * command line regardless of their order in physical memory.
  28. *
  29. * Examples:
  30. *
  31. * 1 NOR Flash, with 1 single writable partition:
  32. * edb7312-nor:-
  33. *
  34. * 1 NOR Flash with 2 partitions, 1 NAND with one
  35. * edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
  36. */
  37. #define pr_fmt(fmt) "mtd: " fmt
  38. #include <linux/kernel.h>
  39. #include <linux/slab.h>
  40. #include <linux/mtd/mtd.h>
  41. #include <linux/mtd/partitions.h>
  42. #include <linux/module.h>
  43. #include <linux/err.h>
  44. /* debug macro */
  45. #if 0
  46. #define dbg(x) do { printk("DEBUG-CMDLINE-PART: "); printk x; } while(0)
  47. #else
  48. #define dbg(x)
  49. #endif
  50. /* special size referring to all the remaining space in a partition */
  51. #define SIZE_REMAINING ULLONG_MAX
  52. #define OFFSET_CONTINUOUS ULLONG_MAX
  53. struct cmdline_mtd_partition {
  54. struct cmdline_mtd_partition *next;
  55. char *mtd_id;
  56. int num_parts;
  57. struct mtd_partition *parts;
  58. };
  59. /* mtdpart_setup() parses into here */
  60. static struct cmdline_mtd_partition *partitions;
  61. /* the command line passed to mtdpart_setup() */
  62. static char *mtdparts;
  63. static char *cmdline;
  64. static int cmdline_parsed;
  65. /*
  66. * Parse one partition definition for an MTD. Since there can be many
  67. * comma separated partition definitions, this function calls itself
  68. * recursively until no more partition definitions are found. Nice side
  69. * effect: the memory to keep the mtd_partition structs and the names
  70. * is allocated upon the last definition being found. At that point the
  71. * syntax has been verified ok.
  72. */
  73. static struct mtd_partition * newpart(char *s,
  74. char **retptr,
  75. int *num_parts,
  76. int this_part,
  77. unsigned char **extra_mem_ptr,
  78. int extra_mem_size)
  79. {
  80. struct mtd_partition *parts;
  81. unsigned long long size, offset = OFFSET_CONTINUOUS;
  82. char *name;
  83. int name_len;
  84. unsigned char *extra_mem;
  85. char delim;
  86. unsigned int mask_flags, add_flags;
  87. /* fetch the partition size */
  88. if (*s == '-') {
  89. /* assign all remaining space to this partition */
  90. size = SIZE_REMAINING;
  91. s++;
  92. } else {
  93. size = memparse(s, &s);
  94. if (!size) {
  95. pr_err("partition has size 0\n");
  96. return ERR_PTR(-EINVAL);
  97. }
  98. }
  99. /* fetch partition name and flags */
  100. mask_flags = 0; /* this is going to be a regular partition */
  101. add_flags = 0;
  102. delim = 0;
  103. /* check for offset */
  104. if (*s == '@') {
  105. s++;
  106. offset = memparse(s, &s);
  107. }
  108. /* now look for name */
  109. if (*s == '(')
  110. delim = ')';
  111. if (delim) {
  112. char *p;
  113. name = ++s;
  114. p = strchr(name, delim);
  115. if (!p) {
  116. pr_err("no closing %c found in partition name\n", delim);
  117. return ERR_PTR(-EINVAL);
  118. }
  119. name_len = p - name;
  120. s = p + 1;
  121. } else {
  122. name = NULL;
  123. name_len = 13; /* Partition_000 */
  124. }
  125. /* record name length for memory allocation later */
  126. extra_mem_size += name_len + 1;
  127. /* test for options */
  128. if (strncmp(s, "ro", 2) == 0) {
  129. mask_flags |= MTD_WRITEABLE;
  130. s += 2;
  131. }
  132. /* if lk is found do NOT unlock the MTD partition*/
  133. if (strncmp(s, "lk", 2) == 0) {
  134. mask_flags |= MTD_POWERUP_LOCK;
  135. s += 2;
  136. }
  137. /* if slc is found use emulated SLC mode on this partition*/
  138. if (!strncmp(s, "slc", 3)) {
  139. add_flags |= MTD_SLC_ON_MLC_EMULATION;
  140. s += 3;
  141. }
  142. /* test if more partitions are following */
  143. if (*s == ',') {
  144. if (size == SIZE_REMAINING) {
  145. pr_err("no partitions allowed after a fill-up partition\n");
  146. return ERR_PTR(-EINVAL);
  147. }
  148. /* more partitions follow, parse them */
  149. parts = newpart(s + 1, &s, num_parts, this_part + 1,
  150. &extra_mem, extra_mem_size);
  151. if (IS_ERR(parts))
  152. return parts;
  153. } else {
  154. /* this is the last partition: allocate space for all */
  155. int alloc_size;
  156. *num_parts = this_part + 1;
  157. alloc_size = *num_parts * sizeof(struct mtd_partition) +
  158. extra_mem_size;
  159. parts = kzalloc(alloc_size, GFP_KERNEL);
  160. if (!parts)
  161. return ERR_PTR(-ENOMEM);
  162. extra_mem = (unsigned char *)(parts + *num_parts);
  163. }
  164. /*
  165. * enter this partition (offset will be calculated later if it is
  166. * OFFSET_CONTINUOUS at this point)
  167. */
  168. parts[this_part].size = size;
  169. parts[this_part].offset = offset;
  170. parts[this_part].mask_flags = mask_flags;
  171. parts[this_part].add_flags = add_flags;
  172. if (name)
  173. strscpy(extra_mem, name, name_len + 1);
  174. else
  175. sprintf(extra_mem, "Partition_%03d", this_part);
  176. parts[this_part].name = extra_mem;
  177. extra_mem += name_len + 1;
  178. dbg(("partition %d: name <%s>, offset %llx, size %llx, mask flags %x\n",
  179. this_part, parts[this_part].name, parts[this_part].offset,
  180. parts[this_part].size, parts[this_part].mask_flags));
  181. /* return (updated) pointer to extra_mem memory */
  182. if (extra_mem_ptr)
  183. *extra_mem_ptr = extra_mem;
  184. /* return (updated) pointer command line string */
  185. *retptr = s;
  186. /* return partition table */
  187. return parts;
  188. }
  189. /*
  190. * Parse the command line.
  191. */
  192. static int mtdpart_setup_real(char *s)
  193. {
  194. cmdline_parsed = 1;
  195. for( ; s != NULL; )
  196. {
  197. struct cmdline_mtd_partition *this_mtd;
  198. struct mtd_partition *parts;
  199. int mtd_id_len, num_parts;
  200. char *p, *mtd_id, *semicol, *open_parenth;
  201. /*
  202. * Replace the first ';' by a NULL char so strrchr can work
  203. * properly.
  204. */
  205. semicol = strchr(s, ';');
  206. if (semicol)
  207. *semicol = '\0';
  208. /*
  209. * make sure that part-names with ":" will not be handled as
  210. * part of the mtd-id with an ":"
  211. */
  212. open_parenth = strchr(s, '(');
  213. if (open_parenth)
  214. *open_parenth = '\0';
  215. mtd_id = s;
  216. /*
  217. * fetch <mtd-id>. We use strrchr to ignore all ':' that could
  218. * be present in the MTD name, only the last one is interpreted
  219. * as an <mtd-id>/<part-definition> separator.
  220. */
  221. p = strrchr(s, ':');
  222. /* Restore the '(' now. */
  223. if (open_parenth)
  224. *open_parenth = '(';
  225. /* Restore the ';' now. */
  226. if (semicol)
  227. *semicol = ';';
  228. if (!p) {
  229. pr_err("no mtd-id\n");
  230. return -EINVAL;
  231. }
  232. mtd_id_len = p - mtd_id;
  233. dbg(("parsing <%s>\n", p+1));
  234. /*
  235. * parse one mtd. have it reserve memory for the
  236. * struct cmdline_mtd_partition and the mtd-id string.
  237. */
  238. parts = newpart(p + 1, /* cmdline */
  239. &s, /* out: updated cmdline ptr */
  240. &num_parts, /* out: number of parts */
  241. 0, /* first partition */
  242. (unsigned char**)&this_mtd, /* out: extra mem */
  243. mtd_id_len + 1 + sizeof(*this_mtd) +
  244. sizeof(void*)-1 /*alignment*/);
  245. if (IS_ERR(parts)) {
  246. /*
  247. * An error occurred. We're either:
  248. * a) out of memory, or
  249. * b) in the middle of the partition spec
  250. * Either way, this mtd is hosed and we're
  251. * unlikely to succeed in parsing any more
  252. */
  253. return PTR_ERR(parts);
  254. }
  255. /* align this_mtd */
  256. this_mtd = (struct cmdline_mtd_partition *)
  257. ALIGN((unsigned long)this_mtd, sizeof(void *));
  258. /* enter results */
  259. this_mtd->parts = parts;
  260. this_mtd->num_parts = num_parts;
  261. this_mtd->mtd_id = (char*)(this_mtd + 1);
  262. strscpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1);
  263. /* link into chain */
  264. this_mtd->next = partitions;
  265. partitions = this_mtd;
  266. dbg(("mtdid=<%s> num_parts=<%d>\n",
  267. this_mtd->mtd_id, this_mtd->num_parts));
  268. /* EOS - we're done */
  269. if (*s == 0)
  270. break;
  271. /* does another spec follow? */
  272. if (*s != ';') {
  273. pr_err("bad character after partition (%c)\n", *s);
  274. return -EINVAL;
  275. }
  276. s++;
  277. }
  278. return 0;
  279. }
  280. /*
  281. * Main function to be called from the MTD mapping driver/device to
  282. * obtain the partitioning information. At this point the command line
  283. * arguments will actually be parsed and turned to struct mtd_partition
  284. * information. It returns partitions for the requested mtd device, or
  285. * the first one in the chain if a NULL mtd_id is passed in.
  286. */
  287. static int parse_cmdline_partitions(struct mtd_info *master,
  288. const struct mtd_partition **pparts,
  289. struct mtd_part_parser_data *data)
  290. {
  291. unsigned long long offset;
  292. int i, err;
  293. struct cmdline_mtd_partition *part;
  294. const char *mtd_id = master->name;
  295. /* parse command line */
  296. if (!cmdline_parsed) {
  297. err = mtdpart_setup_real(cmdline);
  298. if (err)
  299. return err;
  300. }
  301. /*
  302. * Search for the partition definition matching master->name.
  303. * If master->name is not set, stop at first partition definition.
  304. */
  305. for (part = partitions; part; part = part->next) {
  306. if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
  307. break;
  308. }
  309. if (!part)
  310. return 0;
  311. for (i = 0, offset = 0; i < part->num_parts; i++) {
  312. if (part->parts[i].offset == OFFSET_CONTINUOUS)
  313. part->parts[i].offset = offset;
  314. else
  315. offset = part->parts[i].offset;
  316. if (part->parts[i].size == SIZE_REMAINING)
  317. part->parts[i].size = master->size - offset;
  318. if (offset + part->parts[i].size > master->size) {
  319. pr_warn("%s: partitioning exceeds flash size, truncating\n",
  320. part->mtd_id);
  321. part->parts[i].size = master->size - offset;
  322. }
  323. offset += part->parts[i].size;
  324. if (part->parts[i].size == 0) {
  325. pr_warn("%s: skipping zero sized partition\n",
  326. part->mtd_id);
  327. part->num_parts--;
  328. memmove(&part->parts[i], &part->parts[i + 1],
  329. sizeof(*part->parts) * (part->num_parts - i));
  330. i--;
  331. }
  332. }
  333. *pparts = kmemdup(part->parts, sizeof(*part->parts) * part->num_parts,
  334. GFP_KERNEL);
  335. if (!*pparts)
  336. return -ENOMEM;
  337. return part->num_parts;
  338. }
  339. /*
  340. * This is the handler for our kernel parameter, called from
  341. * main.c::checksetup(). Note that we can not yet kmalloc() anything,
  342. * so we only save the commandline for later processing.
  343. *
  344. * This function needs to be visible for bootloaders.
  345. */
  346. static int __init mtdpart_setup(char *s)
  347. {
  348. cmdline = s;
  349. return 1;
  350. }
  351. __setup("mtdparts=", mtdpart_setup);
  352. static struct mtd_part_parser cmdline_parser = {
  353. .parse_fn = parse_cmdline_partitions,
  354. .name = "cmdlinepart",
  355. };
  356. static int __init cmdline_parser_init(void)
  357. {
  358. if (mtdparts)
  359. mtdpart_setup(mtdparts);
  360. register_mtd_parser(&cmdline_parser);
  361. return 0;
  362. }
  363. static void __exit cmdline_parser_exit(void)
  364. {
  365. deregister_mtd_parser(&cmdline_parser);
  366. }
  367. module_init(cmdline_parser_init);
  368. module_exit(cmdline_parser_exit);
  369. MODULE_PARM_DESC(mtdparts, "Partitioning specification");
  370. module_param(mtdparts, charp, 0);
  371. MODULE_LICENSE("GPL");
  372. MODULE_AUTHOR("Marius Groeger <[email protected]>");
  373. MODULE_DESCRIPTION("Command line configuration of MTD partitions");