1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 HUAWEI
4 * Author: Cai Zhiyong <caizhiyong@huawei.com>
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.
12 * The format for the command line is just like mtdparts.
14 * For further information, see "Documentation/block/cmdline-partition.rst"
17 #include <linux/blkdev.h>
19 #include <linux/slab.h>
24 #define PF_RDONLY 0x01 /* Device is read only */
25 #define PF_POWERUP_LOCK 0x02 /* Always locked after reset */
27 struct cmdline_subpart {
28 char name[BDEVNAME_SIZE]; /* partition name, such as 'rootfs' */
32 struct cmdline_subpart *next_subpart;
35 struct cmdline_parts {
36 char name[BDEVNAME_SIZE]; /* block device, such as 'mmcblk0' */
37 unsigned int nr_subparts;
38 struct cmdline_subpart *subpart;
39 struct cmdline_parts *next_parts;
42 static int parse_subpart(struct cmdline_subpart **subpart, char *partdef)
45 struct cmdline_subpart *new_subpart;
49 new_subpart = kzalloc(sizeof(struct cmdline_subpart), GFP_KERNEL);
53 if (*partdef == '-') {
54 new_subpart->size = (sector_t)(~0ULL);
57 new_subpart->size = (sector_t)memparse(partdef, &partdef);
58 if (new_subpart->size < (sector_t)PAGE_SIZE) {
59 pr_warn("cmdline partition size is invalid.");
65 if (*partdef == '@') {
67 new_subpart->from = (sector_t)memparse(partdef, &partdef);
69 new_subpart->from = (sector_t)(~0ULL);
72 if (*partdef == '(') {
74 char *next = strchr(++partdef, ')');
77 pr_warn("cmdline partition format is invalid.");
82 length = min_t(int, next - partdef,
83 sizeof(new_subpart->name) - 1);
84 strscpy(new_subpart->name, partdef, length);
88 new_subpart->name[0] = '\0';
90 new_subpart->flags = 0;
92 if (!strncmp(partdef, "ro", 2)) {
93 new_subpart->flags |= PF_RDONLY;
97 if (!strncmp(partdef, "lk", 2)) {
98 new_subpart->flags |= PF_POWERUP_LOCK;
102 *subpart = new_subpart;
109 static void free_subpart(struct cmdline_parts *parts)
111 struct cmdline_subpart *subpart;
113 while (parts->subpart) {
114 subpart = parts->subpart;
115 parts->subpart = subpart->next_subpart;
120 static int parse_parts(struct cmdline_parts **parts, const char *bdevdef)
125 struct cmdline_subpart **next_subpart;
126 struct cmdline_parts *newparts;
127 char buf[BDEVNAME_SIZE + 32 + 4];
131 newparts = kzalloc(sizeof(struct cmdline_parts), GFP_KERNEL);
135 next = strchr(bdevdef, ':');
137 pr_warn("cmdline partition has no block device.");
141 length = min_t(int, next - bdevdef, sizeof(newparts->name) - 1);
142 strscpy(newparts->name, bdevdef, length);
143 newparts->nr_subparts = 0;
145 next_subpart = &newparts->subpart;
147 while (next && *(++next)) {
149 next = strchr(bdevdef, ',');
151 length = (!next) ? (sizeof(buf) - 1) :
152 min_t(int, next - bdevdef, sizeof(buf) - 1);
154 strscpy(buf, bdevdef, length);
156 ret = parse_subpart(next_subpart, buf);
160 newparts->nr_subparts++;
161 next_subpart = &(*next_subpart)->next_subpart;
164 if (!newparts->subpart) {
165 pr_warn("cmdline partition has no valid partition.");
174 free_subpart(newparts);
179 static void cmdline_parts_free(struct cmdline_parts **parts)
181 struct cmdline_parts *next_parts;
184 next_parts = (*parts)->next_parts;
185 free_subpart(*parts);
191 static int cmdline_parts_parse(struct cmdline_parts **parts,
198 struct cmdline_parts **next_parts;
202 next = pbuf = buf = kstrdup(cmdline, GFP_KERNEL);
208 while (next && *pbuf) {
209 next = strchr(pbuf, ';');
213 ret = parse_parts(next_parts, pbuf);
220 next_parts = &(*next_parts)->next_parts;
224 pr_warn("cmdline partition has no valid partition.");
235 cmdline_parts_free(parts);
239 static struct cmdline_parts *cmdline_parts_find(struct cmdline_parts *parts,
242 while (parts && strncmp(bdev, parts->name, sizeof(parts->name)))
243 parts = parts->next_parts;
247 static char *cmdline;
248 static struct cmdline_parts *bdev_parts;
250 static int add_part(int slot, struct cmdline_subpart *subpart,
251 struct parsed_partitions *state)
254 struct partition_meta_info *info;
255 char tmp[sizeof(info->volname) + 4];
257 if (slot >= state->limit)
260 put_partition(state, slot, subpart->from >> 9,
263 info = &state->parts[slot].info;
265 label_min = min_t(int, sizeof(info->volname) - 1,
266 sizeof(subpart->name));
267 strscpy(info->volname, subpart->name, label_min);
269 snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
270 strlcat(state->pp_buf, tmp, PAGE_SIZE);
272 state->parts[slot].has_info = true;
277 static int cmdline_parts_set(struct cmdline_parts *parts, sector_t disk_size,
278 struct parsed_partitions *state)
281 struct cmdline_subpart *subpart;
284 for (subpart = parts->subpart; subpart;
285 subpart = subpart->next_subpart, slot++) {
286 if (subpart->from == (sector_t)(~0ULL))
287 subpart->from = from;
289 from = subpart->from;
291 if (from >= disk_size)
294 if (subpart->size > (disk_size - from))
295 subpart->size = disk_size - from;
297 from += subpart->size;
299 if (add_part(slot, subpart, state))
306 static int __init cmdline_parts_setup(char *s)
311 __setup("blkdevparts=", cmdline_parts_setup);
313 static bool has_overlaps(sector_t from, sector_t size,
314 sector_t from2, sector_t size2)
316 sector_t end = from + size;
317 sector_t end2 = from2 + size2;
319 if (from >= from2 && from < end2)
322 if (end > from2 && end <= end2)
325 if (from2 >= from && from2 < end)
328 if (end2 > from && end2 <= end)
334 static inline void overlaps_warns_header(void)
336 pr_warn("Overlapping partitions are used in command line partitions.");
337 pr_warn("Don't use filesystems on overlapping partitions:");
340 static void cmdline_parts_verifier(int slot, struct parsed_partitions *state)
345 for (; slot < state->limit && state->parts[slot].has_info; slot++) {
346 for (i = slot+1; i < state->limit && state->parts[i].has_info;
348 if (has_overlaps(state->parts[slot].from,
349 state->parts[slot].size,
350 state->parts[i].from,
351 state->parts[i].size)) {
354 overlaps_warns_header();
356 pr_warn("%s[%llu,%llu] overlaps with "
358 state->parts[slot].info.volname,
359 (u64)state->parts[slot].from << 9,
360 (u64)state->parts[slot].size << 9,
361 state->parts[i].info.volname,
362 (u64)state->parts[i].from << 9,
363 (u64)state->parts[i].size << 9);
370 * Purpose: allocate cmdline partitions.
372 * -1 if unable to read the partition table
373 * 0 if this isn't our partition table
376 int cmdline_partition(struct parsed_partitions *state)
379 struct cmdline_parts *parts;
383 cmdline_parts_free(&bdev_parts);
385 if (cmdline_parts_parse(&bdev_parts, cmdline)) {
395 parts = cmdline_parts_find(bdev_parts, state->disk->disk_name);
399 disk_size = get_capacity(state->disk) << 9;
401 cmdline_parts_set(parts, disk_size, state);
402 cmdline_parts_verifier(1, state);
404 strlcat(state->pp_buf, "\n", PAGE_SIZE);