cmdline-parser.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Parse command line, get partition information
  4. *
  5. * Written by Cai Zhiyong <caizhiyong@huawei.com>
  6. *
  7. */
  8. #include <linux/export.h>
  9. #include <linux/cmdline-parser.h>
  10. static int parse_subpart(struct cmdline_subpart **subpart, char *partdef)
  11. {
  12. int ret = 0;
  13. struct cmdline_subpart *new_subpart;
  14. *subpart = NULL;
  15. new_subpart = kzalloc(sizeof(struct cmdline_subpart), GFP_KERNEL);
  16. if (!new_subpart)
  17. return -ENOMEM;
  18. if (*partdef == '-') {
  19. new_subpart->size = (sector_t)(~0ULL);
  20. partdef++;
  21. } else {
  22. new_subpart->size = (sector_t)memparse(partdef, &partdef);
  23. if (new_subpart->size < (sector_t)PAGE_SIZE) {
  24. pr_warn("cmdline partition size is invalid.");
  25. ret = -EINVAL;
  26. goto fail;
  27. }
  28. }
  29. if (*partdef == '@') {
  30. partdef++;
  31. new_subpart->from = (sector_t)memparse(partdef, &partdef);
  32. } else {
  33. new_subpart->from = (sector_t)(~0ULL);
  34. }
  35. if (*partdef == '(') {
  36. int length;
  37. char *next = strchr(++partdef, ')');
  38. if (!next) {
  39. pr_warn("cmdline partition format is invalid.");
  40. ret = -EINVAL;
  41. goto fail;
  42. }
  43. length = min_t(int, next - partdef,
  44. sizeof(new_subpart->name) - 1);
  45. strncpy(new_subpart->name, partdef, length);
  46. new_subpart->name[length] = '\0';
  47. partdef = ++next;
  48. } else
  49. new_subpart->name[0] = '\0';
  50. new_subpart->flags = 0;
  51. if (!strncmp(partdef, "ro", 2)) {
  52. new_subpart->flags |= PF_RDONLY;
  53. partdef += 2;
  54. }
  55. if (!strncmp(partdef, "lk", 2)) {
  56. new_subpart->flags |= PF_POWERUP_LOCK;
  57. partdef += 2;
  58. }
  59. *subpart = new_subpart;
  60. return 0;
  61. fail:
  62. kfree(new_subpart);
  63. return ret;
  64. }
  65. static void free_subpart(struct cmdline_parts *parts)
  66. {
  67. struct cmdline_subpart *subpart;
  68. while (parts->subpart) {
  69. subpart = parts->subpart;
  70. parts->subpart = subpart->next_subpart;
  71. kfree(subpart);
  72. }
  73. }
  74. static int parse_parts(struct cmdline_parts **parts, const char *bdevdef)
  75. {
  76. int ret = -EINVAL;
  77. char *next;
  78. int length;
  79. struct cmdline_subpart **next_subpart;
  80. struct cmdline_parts *newparts;
  81. char buf[BDEVNAME_SIZE + 32 + 4];
  82. *parts = NULL;
  83. newparts = kzalloc(sizeof(struct cmdline_parts), GFP_KERNEL);
  84. if (!newparts)
  85. return -ENOMEM;
  86. next = strchr(bdevdef, ':');
  87. if (!next) {
  88. pr_warn("cmdline partition has no block device.");
  89. goto fail;
  90. }
  91. length = min_t(int, next - bdevdef, sizeof(newparts->name) - 1);
  92. strncpy(newparts->name, bdevdef, length);
  93. newparts->name[length] = '\0';
  94. newparts->nr_subparts = 0;
  95. next_subpart = &newparts->subpart;
  96. while (next && *(++next)) {
  97. bdevdef = next;
  98. next = strchr(bdevdef, ',');
  99. length = (!next) ? (sizeof(buf) - 1) :
  100. min_t(int, next - bdevdef, sizeof(buf) - 1);
  101. strncpy(buf, bdevdef, length);
  102. buf[length] = '\0';
  103. ret = parse_subpart(next_subpart, buf);
  104. if (ret)
  105. goto fail;
  106. newparts->nr_subparts++;
  107. next_subpart = &(*next_subpart)->next_subpart;
  108. }
  109. if (!newparts->subpart) {
  110. pr_warn("cmdline partition has no valid partition.");
  111. ret = -EINVAL;
  112. goto fail;
  113. }
  114. *parts = newparts;
  115. return 0;
  116. fail:
  117. free_subpart(newparts);
  118. kfree(newparts);
  119. return ret;
  120. }
  121. void cmdline_parts_free(struct cmdline_parts **parts)
  122. {
  123. struct cmdline_parts *next_parts;
  124. while (*parts) {
  125. next_parts = (*parts)->next_parts;
  126. free_subpart(*parts);
  127. kfree(*parts);
  128. *parts = next_parts;
  129. }
  130. }
  131. EXPORT_SYMBOL(cmdline_parts_free);
  132. int cmdline_parts_parse(struct cmdline_parts **parts, const char *cmdline)
  133. {
  134. int ret;
  135. char *buf;
  136. char *pbuf;
  137. char *next;
  138. struct cmdline_parts **next_parts;
  139. *parts = NULL;
  140. next = pbuf = buf = kstrdup(cmdline, GFP_KERNEL);
  141. if (!buf)
  142. return -ENOMEM;
  143. next_parts = parts;
  144. while (next && *pbuf) {
  145. next = strchr(pbuf, ';');
  146. if (next)
  147. *next = '\0';
  148. ret = parse_parts(next_parts, pbuf);
  149. if (ret)
  150. goto fail;
  151. if (next)
  152. pbuf = ++next;
  153. next_parts = &(*next_parts)->next_parts;
  154. }
  155. if (!*parts) {
  156. pr_warn("cmdline partition has no valid partition.");
  157. ret = -EINVAL;
  158. goto fail;
  159. }
  160. ret = 0;
  161. done:
  162. kfree(buf);
  163. return ret;
  164. fail:
  165. cmdline_parts_free(parts);
  166. goto done;
  167. }
  168. EXPORT_SYMBOL(cmdline_parts_parse);
  169. struct cmdline_parts *cmdline_parts_find(struct cmdline_parts *parts,
  170. const char *bdev)
  171. {
  172. while (parts && strncmp(bdev, parts->name, sizeof(parts->name)))
  173. parts = parts->next_parts;
  174. return parts;
  175. }
  176. EXPORT_SYMBOL(cmdline_parts_find);
  177. /*
  178. * add_part()
  179. * 0 success.
  180. * 1 can not add so many partitions.
  181. */
  182. int cmdline_parts_set(struct cmdline_parts *parts, sector_t disk_size,
  183. int slot,
  184. int (*add_part)(int, struct cmdline_subpart *, void *),
  185. void *param)
  186. {
  187. sector_t from = 0;
  188. struct cmdline_subpart *subpart;
  189. for (subpart = parts->subpart; subpart;
  190. subpart = subpart->next_subpart, slot++) {
  191. if (subpart->from == (sector_t)(~0ULL))
  192. subpart->from = from;
  193. else
  194. from = subpart->from;
  195. if (from >= disk_size)
  196. break;
  197. if (subpart->size > (disk_size - from))
  198. subpart->size = disk_size - from;
  199. from += subpart->size;
  200. if (add_part(slot, subpart, param))
  201. break;
  202. }
  203. return slot;
  204. }
  205. EXPORT_SYMBOL(cmdline_parts_set);