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 strncpy(new_subpart->name, partdef, length);
85 new_subpart->name[length] = '\0';
89 new_subpart->name[0] = '\0';
91 new_subpart->flags = 0;
93 if (!strncmp(partdef, "ro", 2)) {
94 new_subpart->flags |= PF_RDONLY;
98 if (!strncmp(partdef, "lk", 2)) {
99 new_subpart->flags |= PF_POWERUP_LOCK;
103 *subpart = new_subpart;
110 static void free_subpart(struct cmdline_parts *parts)
112 struct cmdline_subpart *subpart;
114 while (parts->subpart) {
115 subpart = parts->subpart;
116 parts->subpart = subpart->next_subpart;
121 static int parse_parts(struct cmdline_parts **parts, const char *bdevdef)
126 struct cmdline_subpart **next_subpart;
127 struct cmdline_parts *newparts;
128 char buf[BDEVNAME_SIZE + 32 + 4];
132 newparts = kzalloc(sizeof(struct cmdline_parts), GFP_KERNEL);
136 next = strchr(bdevdef, ':');
138 pr_warn("cmdline partition has no block device.");
142 length = min_t(int, next - bdevdef, sizeof(newparts->name) - 1);
143 strncpy(newparts->name, bdevdef, length);
144 newparts->name[length] = '\0';
145 newparts->nr_subparts = 0;
147 next_subpart = &newparts->subpart;
149 while (next && *(++next)) {
151 next = strchr(bdevdef, ',');
153 length = (!next) ? (sizeof(buf) - 1) :
154 min_t(int, next - bdevdef, sizeof(buf) - 1);
156 strncpy(buf, bdevdef, length);
159 ret = parse_subpart(next_subpart, buf);
163 newparts->nr_subparts++;
164 next_subpart = &(*next_subpart)->next_subpart;
167 if (!newparts->subpart) {
168 pr_warn("cmdline partition has no valid partition.");
177 free_subpart(newparts);
182 static void cmdline_parts_free(struct cmdline_parts **parts)
184 struct cmdline_parts *next_parts;
187 next_parts = (*parts)->next_parts;
188 free_subpart(*parts);
194 static int cmdline_parts_parse(struct cmdline_parts **parts,
201 struct cmdline_parts **next_parts;
205 next = pbuf = buf = kstrdup(cmdline, GFP_KERNEL);
211 while (next && *pbuf) {
212 next = strchr(pbuf, ';');
216 ret = parse_parts(next_parts, pbuf);
223 next_parts = &(*next_parts)->next_parts;
227 pr_warn("cmdline partition has no valid partition.");
238 cmdline_parts_free(parts);
242 static struct cmdline_parts *cmdline_parts_find(struct cmdline_parts *parts,
245 while (parts && strncmp(bdev, parts->name, sizeof(parts->name)))
246 parts = parts->next_parts;
250 static char *cmdline;
251 static struct cmdline_parts *bdev_parts;
253 static int add_part(int slot, struct cmdline_subpart *subpart,
254 struct parsed_partitions *state)
257 struct partition_meta_info *info;
258 char tmp[sizeof(info->volname) + 4];
260 if (slot >= state->limit)
263 put_partition(state, slot, subpart->from >> 9,
266 info = &state->parts[slot].info;
268 label_min = min_t(int, sizeof(info->volname) - 1,
269 sizeof(subpart->name));
270 strncpy(info->volname, subpart->name, label_min);
271 info->volname[label_min] = '\0';
273 snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
274 strlcat(state->pp_buf, tmp, PAGE_SIZE);
276 state->parts[slot].has_info = true;
281 static int cmdline_parts_set(struct cmdline_parts *parts, sector_t disk_size,
282 struct parsed_partitions *state)
285 struct cmdline_subpart *subpart;
288 for (subpart = parts->subpart; subpart;
289 subpart = subpart->next_subpart, slot++) {
290 if (subpart->from == (sector_t)(~0ULL))
291 subpart->from = from;
293 from = subpart->from;
295 if (from >= disk_size)
298 if (subpart->size > (disk_size - from))
299 subpart->size = disk_size - from;
301 from += subpart->size;
303 if (add_part(slot, subpart, state))
310 static int __init cmdline_parts_setup(char *s)
315 __setup("blkdevparts=", cmdline_parts_setup);
317 static bool has_overlaps(sector_t from, sector_t size,
318 sector_t from2, sector_t size2)
320 sector_t end = from + size;
321 sector_t end2 = from2 + size2;
323 if (from >= from2 && from < end2)
326 if (end > from2 && end <= end2)
329 if (from2 >= from && from2 < end)
332 if (end2 > from && end2 <= end)
338 static inline void overlaps_warns_header(void)
340 pr_warn("Overlapping partitions are used in command line partitions.");
341 pr_warn("Don't use filesystems on overlapping partitions:");
344 static void cmdline_parts_verifier(int slot, struct parsed_partitions *state)
349 for (; slot < state->limit && state->parts[slot].has_info; slot++) {
350 for (i = slot+1; i < state->limit && state->parts[i].has_info;
352 if (has_overlaps(state->parts[slot].from,
353 state->parts[slot].size,
354 state->parts[i].from,
355 state->parts[i].size)) {
358 overlaps_warns_header();
360 pr_warn("%s[%llu,%llu] overlaps with "
362 state->parts[slot].info.volname,
363 (u64)state->parts[slot].from << 9,
364 (u64)state->parts[slot].size << 9,
365 state->parts[i].info.volname,
366 (u64)state->parts[i].from << 9,
367 (u64)state->parts[i].size << 9);
374 * Purpose: allocate cmdline partitions.
376 * -1 if unable to read the partition table
377 * 0 if this isn't our partition table
380 int cmdline_partition(struct parsed_partitions *state)
383 struct cmdline_parts *parts;
387 cmdline_parts_free(&bdev_parts);
389 if (cmdline_parts_parse(&bdev_parts, cmdline)) {
399 parts = cmdline_parts_find(bdev_parts, state->disk->disk_name);
403 disk_size = get_capacity(state->disk) << 9;
405 cmdline_parts_set(parts, disk_size, state);
406 cmdline_parts_verifier(1, state);
408 strlcat(state->pp_buf, "\n", PAGE_SIZE);