cmdline.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/cmdline-parser.h>
  18. #include "check.h"
  19. static char *cmdline;
  20. static struct cmdline_parts *bdev_parts;
  21. static int add_part(int slot, struct cmdline_subpart *subpart, void *param)
  22. {
  23. int label_min;
  24. struct partition_meta_info *info;
  25. char tmp[sizeof(info->volname) + 4];
  26. struct parsed_partitions *state = (struct parsed_partitions *)param;
  27. if (slot >= state->limit)
  28. return 1;
  29. put_partition(state, slot, subpart->from >> 9,
  30. subpart->size >> 9);
  31. info = &state->parts[slot].info;
  32. label_min = min_t(int, sizeof(info->volname) - 1,
  33. sizeof(subpart->name));
  34. strncpy(info->volname, subpart->name, label_min);
  35. info->volname[label_min] = '\0';
  36. snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
  37. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  38. state->parts[slot].has_info = true;
  39. return 0;
  40. }
  41. static int __init cmdline_parts_setup(char *s)
  42. {
  43. cmdline = s;
  44. return 1;
  45. }
  46. __setup("blkdevparts=", cmdline_parts_setup);
  47. static bool has_overlaps(sector_t from, sector_t size,
  48. sector_t from2, sector_t size2)
  49. {
  50. sector_t end = from + size;
  51. sector_t end2 = from2 + size2;
  52. if (from >= from2 && from < end2)
  53. return true;
  54. if (end > from2 && end <= end2)
  55. return true;
  56. if (from2 >= from && from2 < end)
  57. return true;
  58. if (end2 > from && end2 <= end)
  59. return true;
  60. return false;
  61. }
  62. static inline void overlaps_warns_header(void)
  63. {
  64. pr_warn("Overlapping partitions are used in command line partitions.");
  65. pr_warn("Don't use filesystems on overlapping partitions:");
  66. }
  67. static void cmdline_parts_verifier(int slot, struct parsed_partitions *state)
  68. {
  69. int i;
  70. bool header = true;
  71. for (; slot < state->limit && state->parts[slot].has_info; slot++) {
  72. for (i = slot+1; i < state->limit && state->parts[i].has_info;
  73. i++) {
  74. if (has_overlaps(state->parts[slot].from,
  75. state->parts[slot].size,
  76. state->parts[i].from,
  77. state->parts[i].size)) {
  78. if (header) {
  79. header = false;
  80. overlaps_warns_header();
  81. }
  82. pr_warn("%s[%llu,%llu] overlaps with "
  83. "%s[%llu,%llu].",
  84. state->parts[slot].info.volname,
  85. (u64)state->parts[slot].from << 9,
  86. (u64)state->parts[slot].size << 9,
  87. state->parts[i].info.volname,
  88. (u64)state->parts[i].from << 9,
  89. (u64)state->parts[i].size << 9);
  90. }
  91. }
  92. }
  93. }
  94. /*
  95. * Purpose: allocate cmdline partitions.
  96. * Returns:
  97. * -1 if unable to read the partition table
  98. * 0 if this isn't our partition table
  99. * 1 if successful
  100. */
  101. int cmdline_partition(struct parsed_partitions *state)
  102. {
  103. sector_t disk_size;
  104. char bdev[BDEVNAME_SIZE];
  105. struct cmdline_parts *parts;
  106. if (cmdline) {
  107. if (bdev_parts)
  108. cmdline_parts_free(&bdev_parts);
  109. if (cmdline_parts_parse(&bdev_parts, cmdline)) {
  110. cmdline = NULL;
  111. return -1;
  112. }
  113. cmdline = NULL;
  114. }
  115. if (!bdev_parts)
  116. return 0;
  117. bdevname(state->bdev, bdev);
  118. parts = cmdline_parts_find(bdev_parts, bdev);
  119. if (!parts)
  120. return 0;
  121. disk_size = get_capacity(state->bdev->bd_disk) << 9;
  122. cmdline_parts_set(parts, disk_size, 1, add_part, (void *)state);
  123. cmdline_parts_verifier(1, state);
  124. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  125. return 1;
  126. }