Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / block / partitions / cmdline.c
blobe333583ab38c981279401205d2e2151a872287c7
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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.txt"
18 #include <linux/cmdline-parser.h>
20 #include "check.h"
21 #include "cmdline.h"
23 static char *cmdline;
24 static struct cmdline_parts *bdev_parts;
26 static int add_part(int slot, struct cmdline_subpart *subpart, void *param)
28 int label_min;
29 struct partition_meta_info *info;
30 char tmp[sizeof(info->volname) + 4];
31 struct parsed_partitions *state = (struct parsed_partitions *)param;
33 if (slot >= state->limit)
34 return 1;
36 put_partition(state, slot, subpart->from >> 9,
37 subpart->size >> 9);
39 info = &state->parts[slot].info;
41 label_min = min_t(int, sizeof(info->volname) - 1,
42 sizeof(subpart->name));
43 strncpy(info->volname, subpart->name, label_min);
44 info->volname[label_min] = '\0';
46 snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
47 strlcat(state->pp_buf, tmp, PAGE_SIZE);
49 state->parts[slot].has_info = true;
51 return 0;
54 static int __init cmdline_parts_setup(char *s)
56 cmdline = s;
57 return 1;
59 __setup("blkdevparts=", cmdline_parts_setup);
62 * Purpose: allocate cmdline partitions.
63 * Returns:
64 * -1 if unable to read the partition table
65 * 0 if this isn't our partition table
66 * 1 if successful
68 int cmdline_partition(struct parsed_partitions *state)
70 sector_t disk_size;
71 char bdev[BDEVNAME_SIZE];
72 struct cmdline_parts *parts;
74 if (cmdline) {
75 if (bdev_parts)
76 cmdline_parts_free(&bdev_parts);
78 if (cmdline_parts_parse(&bdev_parts, cmdline)) {
79 cmdline = NULL;
80 return -1;
82 cmdline = NULL;
85 if (!bdev_parts)
86 return 0;
88 bdevname(state->bdev, bdev);
89 parts = cmdline_parts_find(bdev_parts, bdev);
90 if (!parts)
91 return 0;
93 disk_size = get_capacity(state->bdev->bd_disk) << 9;
95 cmdline_parts_set(parts, disk_size, 1, add_part, (void *)state);
97 strlcat(state->pp_buf, "\n", PAGE_SIZE);
99 return 1;