alx: add a hardware stats structure
[linux/fpc-iii.git] / block / cmdline-parser.c
blobcc2637f8674ed61df149fb3bf51da4cc3a04f7cf
1 /*
2 * Parse command line, get partition information
4 * Written by Cai Zhiyong <caizhiyong@huawei.com>
6 */
7 #include <linux/buffer_head.h>
8 #include <linux/module.h>
9 #include <linux/cmdline-parser.h>
11 static int parse_subpart(struct cmdline_subpart **subpart, char *partdef)
13 int ret = 0;
14 struct cmdline_subpart *new_subpart;
16 *subpart = NULL;
18 new_subpart = kzalloc(sizeof(struct cmdline_subpart), GFP_KERNEL);
19 if (!new_subpart)
20 return -ENOMEM;
22 if (*partdef == '-') {
23 new_subpart->size = (sector_t)(~0ULL);
24 partdef++;
25 } else {
26 new_subpart->size = (sector_t)memparse(partdef, &partdef);
27 if (new_subpart->size < (sector_t)PAGE_SIZE) {
28 pr_warn("cmdline partition size is invalid.");
29 ret = -EINVAL;
30 goto fail;
34 if (*partdef == '@') {
35 partdef++;
36 new_subpart->from = (sector_t)memparse(partdef, &partdef);
37 } else {
38 new_subpart->from = (sector_t)(~0ULL);
41 if (*partdef == '(') {
42 int length;
43 char *next = strchr(++partdef, ')');
45 if (!next) {
46 pr_warn("cmdline partition format is invalid.");
47 ret = -EINVAL;
48 goto fail;
51 length = min_t(int, next - partdef,
52 sizeof(new_subpart->name) - 1);
53 strncpy(new_subpart->name, partdef, length);
54 new_subpart->name[length] = '\0';
56 partdef = ++next;
57 } else
58 new_subpart->name[0] = '\0';
60 new_subpart->flags = 0;
62 if (!strncmp(partdef, "ro", 2)) {
63 new_subpart->flags |= PF_RDONLY;
64 partdef += 2;
67 if (!strncmp(partdef, "lk", 2)) {
68 new_subpart->flags |= PF_POWERUP_LOCK;
69 partdef += 2;
72 *subpart = new_subpart;
73 return 0;
74 fail:
75 kfree(new_subpart);
76 return ret;
79 static void free_subpart(struct cmdline_parts *parts)
81 struct cmdline_subpart *subpart;
83 while (parts->subpart) {
84 subpart = parts->subpart;
85 parts->subpart = subpart->next_subpart;
86 kfree(subpart);
90 static int parse_parts(struct cmdline_parts **parts, const char *bdevdef)
92 int ret = -EINVAL;
93 char *next;
94 int length;
95 struct cmdline_subpart **next_subpart;
96 struct cmdline_parts *newparts;
97 char buf[BDEVNAME_SIZE + 32 + 4];
99 *parts = NULL;
101 newparts = kzalloc(sizeof(struct cmdline_parts), GFP_KERNEL);
102 if (!newparts)
103 return -ENOMEM;
105 next = strchr(bdevdef, ':');
106 if (!next) {
107 pr_warn("cmdline partition has no block device.");
108 goto fail;
111 length = min_t(int, next - bdevdef, sizeof(newparts->name) - 1);
112 strncpy(newparts->name, bdevdef, length);
113 newparts->name[length] = '\0';
114 newparts->nr_subparts = 0;
116 next_subpart = &newparts->subpart;
118 while (next && *(++next)) {
119 bdevdef = next;
120 next = strchr(bdevdef, ',');
122 length = (!next) ? (sizeof(buf) - 1) :
123 min_t(int, next - bdevdef, sizeof(buf) - 1);
125 strncpy(buf, bdevdef, length);
126 buf[length] = '\0';
128 ret = parse_subpart(next_subpart, buf);
129 if (ret)
130 goto fail;
132 newparts->nr_subparts++;
133 next_subpart = &(*next_subpart)->next_subpart;
136 if (!newparts->subpart) {
137 pr_warn("cmdline partition has no valid partition.");
138 ret = -EINVAL;
139 goto fail;
142 *parts = newparts;
144 return 0;
145 fail:
146 free_subpart(newparts);
147 kfree(newparts);
148 return ret;
151 void cmdline_parts_free(struct cmdline_parts **parts)
153 struct cmdline_parts *next_parts;
155 while (*parts) {
156 next_parts = (*parts)->next_parts;
157 free_subpart(*parts);
158 kfree(*parts);
159 *parts = next_parts;
163 int cmdline_parts_parse(struct cmdline_parts **parts, const char *cmdline)
165 int ret;
166 char *buf;
167 char *pbuf;
168 char *next;
169 struct cmdline_parts **next_parts;
171 *parts = NULL;
173 next = pbuf = buf = kstrdup(cmdline, GFP_KERNEL);
174 if (!buf)
175 return -ENOMEM;
177 next_parts = parts;
179 while (next && *pbuf) {
180 next = strchr(pbuf, ';');
181 if (next)
182 *next = '\0';
184 ret = parse_parts(next_parts, pbuf);
185 if (ret)
186 goto fail;
188 if (next)
189 pbuf = ++next;
191 next_parts = &(*next_parts)->next_parts;
194 if (!*parts) {
195 pr_warn("cmdline partition has no valid partition.");
196 ret = -EINVAL;
197 goto fail;
200 ret = 0;
201 done:
202 kfree(buf);
203 return ret;
205 fail:
206 cmdline_parts_free(parts);
207 goto done;
210 struct cmdline_parts *cmdline_parts_find(struct cmdline_parts *parts,
211 const char *bdev)
213 while (parts && strncmp(bdev, parts->name, sizeof(parts->name)))
214 parts = parts->next_parts;
215 return parts;
219 * add_part()
220 * 0 success.
221 * 1 can not add so many partitions.
223 void cmdline_parts_set(struct cmdline_parts *parts, sector_t disk_size,
224 int slot,
225 int (*add_part)(int, struct cmdline_subpart *, void *),
226 void *param)
229 sector_t from = 0;
230 struct cmdline_subpart *subpart;
232 for (subpart = parts->subpart; subpart;
233 subpart = subpart->next_subpart, slot++) {
234 if (subpart->from == (sector_t)(~0ULL))
235 subpart->from = from;
236 else
237 from = subpart->from;
239 if (from >= disk_size)
240 break;
242 if (subpart->size > (disk_size - from))
243 subpart->size = disk_size - from;
245 from += subpart->size;
247 if (add_part(slot, subpart, param))
248 break;