cmdline-parser.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Parse command line, get partition information
  3. *
  4. * Written by Cai Zhiyong <caizhiyong@huawei.com>
  5. *
  6. */
  7. #include <linux/buffer_head.h>
  8. #include <linux/module.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. 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. struct cmdline_parts *cmdline_parts_find(struct cmdline_parts *parts,
  168. const char *bdev)
  169. {
  170. while (parts && strncmp(bdev, parts->name, sizeof(parts->name)))
  171. parts = parts->next_parts;
  172. return parts;
  173. }
  174. /*
  175. * add_part()
  176. * 0 success.
  177. * 1 can not add so many partitions.
  178. */
  179. void cmdline_parts_set(struct cmdline_parts *parts, sector_t disk_size,
  180. int slot,
  181. int (*add_part)(int, struct cmdline_subpart *, void *),
  182. void *param)
  183. {
  184. sector_t from = 0;
  185. struct cmdline_subpart *subpart;
  186. for (subpart = parts->subpart; subpart;
  187. subpart = subpart->next_subpart, slot++) {
  188. if (subpart->from == (sector_t)(~0ULL))
  189. subpart->from = from;
  190. else
  191. from = subpart->from;
  192. if (from >= disk_size)
  193. break;
  194. if (subpart->size > (disk_size - from))
  195. subpart->size = disk_size - from;
  196. from += subpart->size;
  197. if (add_part(slot, subpart, param))
  198. break;
  199. }
  200. }