cmdline.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2013 HUAWEI
  4. * Author: Cai Zhiyong <caizhiyong@huawei.com>
  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. partdef++;
  64. char *next = strsep(&partdef, ")");
  65. if (!next) {
  66. pr_warn("cmdline partition format is invalid.");
  67. ret = -EINVAL;
  68. goto fail;
  69. }
  70. strscpy(new_subpart->name, next, sizeof(new_subpart->name));
  71. } else
  72. new_subpart->name[0] = '\0';
  73. new_subpart->flags = 0;
  74. if (!strncmp(partdef, "ro", 2)) {
  75. new_subpart->flags |= PF_RDONLY;
  76. partdef += 2;
  77. }
  78. if (!strncmp(partdef, "lk", 2)) {
  79. new_subpart->flags |= PF_POWERUP_LOCK;
  80. partdef += 2;
  81. }
  82. *subpart = new_subpart;
  83. return 0;
  84. fail:
  85. kfree(new_subpart);
  86. return ret;
  87. }
  88. static void free_subpart(struct cmdline_parts *parts)
  89. {
  90. struct cmdline_subpart *subpart;
  91. while (parts->subpart) {
  92. subpart = parts->subpart;
  93. parts->subpart = subpart->next_subpart;
  94. kfree(subpart);
  95. }
  96. }
  97. static int parse_parts(struct cmdline_parts **parts, char *bdevdef)
  98. {
  99. int ret = -EINVAL;
  100. char *next;
  101. struct cmdline_subpart **next_subpart;
  102. struct cmdline_parts *newparts;
  103. *parts = NULL;
  104. newparts = kzalloc(sizeof(struct cmdline_parts), GFP_KERNEL);
  105. if (!newparts)
  106. return -ENOMEM;
  107. next = strsep(&bdevdef, ":");
  108. if (!next) {
  109. pr_warn("cmdline partition has no block device.");
  110. goto fail;
  111. }
  112. strscpy(newparts->name, next, sizeof(newparts->name));
  113. newparts->nr_subparts = 0;
  114. next_subpart = &newparts->subpart;
  115. while ((next = strsep(&bdevdef, ","))) {
  116. ret = parse_subpart(next_subpart, next);
  117. if (ret)
  118. goto fail;
  119. newparts->nr_subparts++;
  120. next_subpart = &(*next_subpart)->next_subpart;
  121. }
  122. if (!newparts->subpart) {
  123. pr_warn("cmdline partition has no valid partition.");
  124. ret = -EINVAL;
  125. goto fail;
  126. }
  127. *parts = newparts;
  128. return 0;
  129. fail:
  130. free_subpart(newparts);
  131. kfree(newparts);
  132. return ret;
  133. }
  134. static void cmdline_parts_free(struct cmdline_parts **parts)
  135. {
  136. struct cmdline_parts *next_parts;
  137. while (*parts) {
  138. next_parts = (*parts)->next_parts;
  139. free_subpart(*parts);
  140. kfree(*parts);
  141. *parts = next_parts;
  142. }
  143. }
  144. static int cmdline_parts_parse(struct cmdline_parts **parts,
  145. const char *cmdline)
  146. {
  147. int ret;
  148. char *buf;
  149. char *pbuf;
  150. char *next;
  151. struct cmdline_parts **next_parts;
  152. *parts = NULL;
  153. pbuf = buf = kstrdup(cmdline, GFP_KERNEL);
  154. if (!buf)
  155. return -ENOMEM;
  156. next_parts = parts;
  157. while ((next = strsep(&pbuf, ";"))) {
  158. ret = parse_parts(next_parts, next);
  159. if (ret)
  160. goto fail;
  161. next_parts = &(*next_parts)->next_parts;
  162. }
  163. if (!*parts) {
  164. pr_warn("cmdline partition has no valid partition.");
  165. ret = -EINVAL;
  166. goto fail;
  167. }
  168. ret = 0;
  169. done:
  170. kfree(buf);
  171. return ret;
  172. fail:
  173. cmdline_parts_free(parts);
  174. goto done;
  175. }
  176. static struct cmdline_parts *cmdline_parts_find(struct cmdline_parts *parts,
  177. const char *bdev)
  178. {
  179. while (parts && strncmp(bdev, parts->name, sizeof(parts->name)))
  180. parts = parts->next_parts;
  181. return parts;
  182. }
  183. static char *cmdline;
  184. static struct cmdline_parts *bdev_parts;
  185. static int add_part(int slot, struct cmdline_subpart *subpart,
  186. struct parsed_partitions *state)
  187. {
  188. struct partition_meta_info *info;
  189. char tmp[sizeof(info->volname) + 4];
  190. if (slot >= state->limit)
  191. return 1;
  192. put_partition(state, slot, subpart->from >> 9,
  193. subpart->size >> 9);
  194. info = &state->parts[slot].info;
  195. strscpy(info->volname, subpart->name, sizeof(info->volname));
  196. snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
  197. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  198. state->parts[slot].has_info = true;
  199. return 0;
  200. }
  201. static int cmdline_parts_set(struct cmdline_parts *parts, sector_t disk_size,
  202. struct parsed_partitions *state)
  203. {
  204. sector_t from = 0;
  205. struct cmdline_subpart *subpart;
  206. int slot = 1;
  207. for (subpart = parts->subpart; subpart;
  208. subpart = subpart->next_subpart, slot++) {
  209. if (subpart->from == (sector_t)(~0ULL))
  210. subpart->from = from;
  211. else
  212. from = subpart->from;
  213. if (from >= disk_size)
  214. break;
  215. if (subpart->size > (disk_size - from))
  216. subpart->size = disk_size - from;
  217. from += subpart->size;
  218. if (add_part(slot, subpart, state))
  219. break;
  220. }
  221. return slot;
  222. }
  223. static int __init cmdline_parts_setup(char *s)
  224. {
  225. cmdline = s;
  226. return 1;
  227. }
  228. __setup("blkdevparts=", cmdline_parts_setup);
  229. static bool has_overlaps(sector_t from, sector_t size,
  230. sector_t from2, sector_t size2)
  231. {
  232. sector_t end = from + size;
  233. sector_t end2 = from2 + size2;
  234. if (from >= from2 && from < end2)
  235. return true;
  236. if (end > from2 && end <= end2)
  237. return true;
  238. if (from2 >= from && from2 < end)
  239. return true;
  240. if (end2 > from && end2 <= end)
  241. return true;
  242. return false;
  243. }
  244. static inline void overlaps_warns_header(void)
  245. {
  246. pr_warn("Overlapping partitions are used in command line partitions.");
  247. pr_warn("Don't use filesystems on overlapping partitions:");
  248. }
  249. static void cmdline_parts_verifier(int slot, struct parsed_partitions *state)
  250. {
  251. int i;
  252. bool header = true;
  253. for (; slot < state->limit && state->parts[slot].has_info; slot++) {
  254. for (i = slot+1; i < state->limit && state->parts[i].has_info;
  255. i++) {
  256. if (has_overlaps(state->parts[slot].from,
  257. state->parts[slot].size,
  258. state->parts[i].from,
  259. state->parts[i].size)) {
  260. if (header) {
  261. header = false;
  262. overlaps_warns_header();
  263. }
  264. pr_warn("%s[%llu,%llu] overlaps with "
  265. "%s[%llu,%llu].",
  266. state->parts[slot].info.volname,
  267. (u64)state->parts[slot].from << 9,
  268. (u64)state->parts[slot].size << 9,
  269. state->parts[i].info.volname,
  270. (u64)state->parts[i].from << 9,
  271. (u64)state->parts[i].size << 9);
  272. }
  273. }
  274. }
  275. }
  276. /*
  277. * Purpose: allocate cmdline partitions.
  278. * Returns:
  279. * -1 if unable to read the partition table
  280. * 0 if this isn't our partition table
  281. * 1 if successful
  282. */
  283. int cmdline_partition(struct parsed_partitions *state)
  284. {
  285. sector_t disk_size;
  286. struct cmdline_parts *parts;
  287. if (cmdline) {
  288. if (bdev_parts)
  289. cmdline_parts_free(&bdev_parts);
  290. if (cmdline_parts_parse(&bdev_parts, cmdline)) {
  291. cmdline = NULL;
  292. return -1;
  293. }
  294. cmdline = NULL;
  295. }
  296. if (!bdev_parts)
  297. return 0;
  298. parts = cmdline_parts_find(bdev_parts, state->disk->disk_name);
  299. if (!parts)
  300. return 0;
  301. disk_size = get_capacity(state->disk) << 9;
  302. cmdline_parts_set(parts, disk_size, state);
  303. cmdline_parts_verifier(1, state);
  304. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  305. return 1;
  306. }