cmdline-parser.c 4.9 KB

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