cmdline.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2013 HUAWEI
  4. * Author: Cai Zhiyong <[email protected]>
  5. *
  6. * Read block device partition table from the command line.
  7. * Typically used for fixed block (eMMC) embedded devices.
  8. * It has no MBR, so saves storage space. Bootloader can be easily accessed
  9. * by absolute address of data on the block device.
  10. * Users can easily change the partition.
  11. *
  12. * The format for the command line is just like mtdparts.
  13. *
  14. * For further information, see "Documentation/block/cmdline-partition.rst"
  15. *
  16. */
  17. #include <linux/blkdev.h>
  18. #include <linux/fs.h>
  19. #include <linux/slab.h>
  20. #include "check.h"
  21. /* partition flags */
  22. #define PF_RDONLY 0x01 /* Device is read only */
  23. #define PF_POWERUP_LOCK 0x02 /* Always locked after reset */
  24. struct cmdline_subpart {
  25. char name[BDEVNAME_SIZE]; /* partition name, such as 'rootfs' */
  26. sector_t from;
  27. sector_t size;
  28. int flags;
  29. struct cmdline_subpart *next_subpart;
  30. };
  31. struct cmdline_parts {
  32. char name[BDEVNAME_SIZE]; /* block device, such as 'mmcblk0' */
  33. unsigned int nr_subparts;
  34. struct cmdline_subpart *subpart;
  35. struct cmdline_parts *next_parts;
  36. };
  37. static int parse_subpart(struct cmdline_subpart **subpart, char *partdef)
  38. {
  39. int ret = 0;
  40. struct cmdline_subpart *new_subpart;
  41. *subpart = NULL;
  42. new_subpart = kzalloc(sizeof(struct cmdline_subpart), GFP_KERNEL);
  43. if (!new_subpart)
  44. return -ENOMEM;
  45. if (*partdef == '-') {
  46. new_subpart->size = (sector_t)(~0ULL);
  47. partdef++;
  48. } else {
  49. new_subpart->size = (sector_t)memparse(partdef, &partdef);
  50. if (new_subpart->size < (sector_t)PAGE_SIZE) {
  51. pr_warn("cmdline partition size is invalid.");
  52. ret = -EINVAL;
  53. goto fail;
  54. }
  55. }
  56. if (*partdef == '@') {
  57. partdef++;
  58. new_subpart->from = (sector_t)memparse(partdef, &partdef);
  59. } else {
  60. new_subpart->from = (sector_t)(~0ULL);
  61. }
  62. if (*partdef == '(') {
  63. int length;
  64. char *next = strchr(++partdef, ')');
  65. if (!next) {
  66. pr_warn("cmdline partition format is invalid.");
  67. ret = -EINVAL;
  68. goto fail;
  69. }
  70. length = min_t(int, next - partdef,
  71. sizeof(new_subpart->name) - 1);
  72. strncpy(new_subpart->name, partdef, length);
  73. new_subpart->name[length] = '\0';
  74. partdef = ++next;
  75. } else
  76. new_subpart->name[0] = '\0';
  77. new_subpart->flags = 0;
  78. if (!strncmp(partdef, "ro", 2)) {
  79. new_subpart->flags |= PF_RDONLY;
  80. partdef += 2;
  81. }
  82. if (!strncmp(partdef, "lk", 2)) {
  83. new_subpart->flags |= PF_POWERUP_LOCK;
  84. partdef += 2;
  85. }
  86. *subpart = new_subpart;
  87. return 0;
  88. fail:
  89. kfree(new_subpart);
  90. return ret;
  91. }
  92. static void free_subpart(struct cmdline_parts *parts)
  93. {
  94. struct cmdline_subpart *subpart;
  95. while (parts->subpart) {
  96. subpart = parts->subpart;
  97. parts->subpart = subpart->next_subpart;
  98. kfree(subpart);
  99. }
  100. }
  101. static int parse_parts(struct cmdline_parts **parts, const char *bdevdef)
  102. {
  103. int ret = -EINVAL;
  104. char *next;
  105. int length;
  106. struct cmdline_subpart **next_subpart;
  107. struct cmdline_parts *newparts;
  108. char buf[BDEVNAME_SIZE + 32 + 4];
  109. *parts = NULL;
  110. newparts = kzalloc(sizeof(struct cmdline_parts), GFP_KERNEL);
  111. if (!newparts)
  112. return -ENOMEM;
  113. next = strchr(bdevdef, ':');
  114. if (!next) {
  115. pr_warn("cmdline partition has no block device.");
  116. goto fail;
  117. }
  118. length = min_t(int, next - bdevdef, sizeof(newparts->name) - 1);
  119. strncpy(newparts->name, bdevdef, length);
  120. newparts->name[length] = '\0';
  121. newparts->nr_subparts = 0;
  122. next_subpart = &newparts->subpart;
  123. while (next && *(++next)) {
  124. bdevdef = next;
  125. next = strchr(bdevdef, ',');
  126. length = (!next) ? (sizeof(buf) - 1) :
  127. min_t(int, next - bdevdef, sizeof(buf) - 1);
  128. strncpy(buf, bdevdef, length);
  129. buf[length] = '\0';
  130. ret = parse_subpart(next_subpart, buf);
  131. if (ret)
  132. goto fail;
  133. newparts->nr_subparts++;
  134. next_subpart = &(*next_subpart)->next_subpart;
  135. }
  136. if (!newparts->subpart) {
  137. pr_warn("cmdline partition has no valid partition.");
  138. ret = -EINVAL;
  139. goto fail;
  140. }
  141. *parts = newparts;
  142. return 0;
  143. fail:
  144. free_subpart(newparts);
  145. kfree(newparts);
  146. return ret;
  147. }
  148. static void cmdline_parts_free(struct cmdline_parts **parts)
  149. {
  150. struct cmdline_parts *next_parts;
  151. while (*parts) {
  152. next_parts = (*parts)->next_parts;
  153. free_subpart(*parts);
  154. kfree(*parts);
  155. *parts = next_parts;
  156. }
  157. }
  158. static int cmdline_parts_parse(struct cmdline_parts **parts,
  159. const char *cmdline)
  160. {
  161. int ret;
  162. char *buf;
  163. char *pbuf;
  164. char *next;
  165. struct cmdline_parts **next_parts;
  166. *parts = NULL;
  167. next = pbuf = buf = kstrdup(cmdline, GFP_KERNEL);
  168. if (!buf)
  169. return -ENOMEM;
  170. next_parts = parts;
  171. while (next && *pbuf) {
  172. next = strchr(pbuf, ';');
  173. if (next)
  174. *next = '\0';
  175. ret = parse_parts(next_parts, pbuf);
  176. if (ret)
  177. goto fail;
  178. if (next)
  179. pbuf = ++next;
  180. next_parts = &(*next_parts)->next_parts;
  181. }
  182. if (!*parts) {
  183. pr_warn("cmdline partition has no valid partition.");
  184. ret = -EINVAL;
  185. goto fail;
  186. }
  187. ret = 0;
  188. done:
  189. kfree(buf);
  190. return ret;
  191. fail:
  192. cmdline_parts_free(parts);
  193. goto done;
  194. }
  195. static struct cmdline_parts *cmdline_parts_find(struct cmdline_parts *parts,
  196. const char *bdev)
  197. {
  198. while (parts && strncmp(bdev, parts->name, sizeof(parts->name)))
  199. parts = parts->next_parts;
  200. return parts;
  201. }
  202. static char *cmdline;
  203. static struct cmdline_parts *bdev_parts;
  204. static int add_part(int slot, struct cmdline_subpart *subpart,
  205. struct parsed_partitions *state)
  206. {
  207. int label_min;
  208. struct partition_meta_info *info;
  209. char tmp[sizeof(info->volname) + 4];
  210. if (slot >= state->limit)
  211. return 1;
  212. put_partition(state, slot, subpart->from >> 9,
  213. subpart->size >> 9);
  214. info = &state->parts[slot].info;
  215. label_min = min_t(int, sizeof(info->volname) - 1,
  216. sizeof(subpart->name));
  217. strncpy(info->volname, subpart->name, label_min);
  218. info->volname[label_min] = '\0';
  219. snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
  220. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  221. state->parts[slot].has_info = true;
  222. return 0;
  223. }
  224. static int cmdline_parts_set(struct cmdline_parts *parts, sector_t disk_size,
  225. struct parsed_partitions *state)
  226. {
  227. sector_t from = 0;
  228. struct cmdline_subpart *subpart;
  229. int slot = 1;
  230. for (subpart = parts->subpart; subpart;
  231. subpart = subpart->next_subpart, slot++) {
  232. if (subpart->from == (sector_t)(~0ULL))
  233. subpart->from = from;
  234. else
  235. from = subpart->from;
  236. if (from >= disk_size)
  237. break;
  238. if (subpart->size > (disk_size - from))
  239. subpart->size = disk_size - from;
  240. from += subpart->size;
  241. if (add_part(slot, subpart, state))
  242. break;
  243. }
  244. return slot;
  245. }
  246. static int __init cmdline_parts_setup(char *s)
  247. {
  248. cmdline = s;
  249. return 1;
  250. }
  251. __setup("blkdevparts=", cmdline_parts_setup);
  252. static bool has_overlaps(sector_t from, sector_t size,
  253. sector_t from2, sector_t size2)
  254. {
  255. sector_t end = from + size;
  256. sector_t end2 = from2 + size2;
  257. if (from >= from2 && from < end2)
  258. return true;
  259. if (end > from2 && end <= end2)
  260. return true;
  261. if (from2 >= from && from2 < end)
  262. return true;
  263. if (end2 > from && end2 <= end)
  264. return true;
  265. return false;
  266. }
  267. static inline void overlaps_warns_header(void)
  268. {
  269. pr_warn("Overlapping partitions are used in command line partitions.");
  270. pr_warn("Don't use filesystems on overlapping partitions:");
  271. }
  272. static void cmdline_parts_verifier(int slot, struct parsed_partitions *state)
  273. {
  274. int i;
  275. bool header = true;
  276. for (; slot < state->limit && state->parts[slot].has_info; slot++) {
  277. for (i = slot+1; i < state->limit && state->parts[i].has_info;
  278. i++) {
  279. if (has_overlaps(state->parts[slot].from,
  280. state->parts[slot].size,
  281. state->parts[i].from,
  282. state->parts[i].size)) {
  283. if (header) {
  284. header = false;
  285. overlaps_warns_header();
  286. }
  287. pr_warn("%s[%llu,%llu] overlaps with "
  288. "%s[%llu,%llu].",
  289. state->parts[slot].info.volname,
  290. (u64)state->parts[slot].from << 9,
  291. (u64)state->parts[slot].size << 9,
  292. state->parts[i].info.volname,
  293. (u64)state->parts[i].from << 9,
  294. (u64)state->parts[i].size << 9);
  295. }
  296. }
  297. }
  298. }
  299. /*
  300. * Purpose: allocate cmdline partitions.
  301. * Returns:
  302. * -1 if unable to read the partition table
  303. * 0 if this isn't our partition table
  304. * 1 if successful
  305. */
  306. int cmdline_partition(struct parsed_partitions *state)
  307. {
  308. sector_t disk_size;
  309. struct cmdline_parts *parts;
  310. if (cmdline) {
  311. if (bdev_parts)
  312. cmdline_parts_free(&bdev_parts);
  313. if (cmdline_parts_parse(&bdev_parts, cmdline)) {
  314. cmdline = NULL;
  315. return -1;
  316. }
  317. cmdline = NULL;
  318. }
  319. if (!bdev_parts)
  320. return 0;
  321. parts = cmdline_parts_find(bdev_parts, state->disk->disk_name);
  322. if (!parts)
  323. return 0;
  324. disk_size = get_capacity(state->disk) << 9;
  325. cmdline_parts_set(parts, disk_size, state);
  326. cmdline_parts_verifier(1, state);
  327. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  328. return 1;
  329. }