cmdline.c 3.6 KB

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