cmdlinepart.c 11 KB

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