Linux Audio

Check our new training course

Loading...
v4.17
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2013 HUAWEI
  4 * Author: Cai Zhiyong <caizhiyong@huawei.com>
  5 *
  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.
 11 *
 12 * The format for the command line is just like mtdparts.
 13 *
 14 * For further information, see "Documentation/block/cmdline-partition.txt"
 15 *
 16 */
 17
 18#include <linux/cmdline-parser.h>
 19
 20#include "check.h"
 21#include "cmdline.h"
 22
 23static char *cmdline;
 24static struct cmdline_parts *bdev_parts;
 25
 26static int add_part(int slot, struct cmdline_subpart *subpart, void *param)
 27{
 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;
 32
 33	if (slot >= state->limit)
 34		return 1;
 35
 36	put_partition(state, slot, subpart->from >> 9,
 37		      subpart->size >> 9);
 38
 39	info = &state->parts[slot].info;
 40
 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';
 45
 46	snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
 47	strlcat(state->pp_buf, tmp, PAGE_SIZE);
 48
 49	state->parts[slot].has_info = true;
 50
 51	return 0;
 52}
 53
 54static int __init cmdline_parts_setup(char *s)
 55{
 56	cmdline = s;
 57	return 1;
 58}
 59__setup("blkdevparts=", cmdline_parts_setup);
 60
 61/*
 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
 67 */
 68int cmdline_partition(struct parsed_partitions *state)
 69{
 70	sector_t disk_size;
 71	char bdev[BDEVNAME_SIZE];
 72	struct cmdline_parts *parts;
 73
 74	if (cmdline) {
 75		if (bdev_parts)
 76			cmdline_parts_free(&bdev_parts);
 77
 78		if (cmdline_parts_parse(&bdev_parts, cmdline)) {
 79			cmdline = NULL;
 80			return -1;
 81		}
 82		cmdline = NULL;
 83	}
 84
 85	if (!bdev_parts)
 86		return 0;
 87
 88	bdevname(state->bdev, bdev);
 89	parts = cmdline_parts_find(bdev_parts, bdev);
 90	if (!parts)
 91		return 0;
 92
 93	disk_size = get_capacity(state->bdev->bd_disk) << 9;
 94
 95	cmdline_parts_set(parts, disk_size, 1, add_part, (void *)state);
 96
 97	strlcat(state->pp_buf, "\n", PAGE_SIZE);
 98
 99	return 1;
100}
v4.6
 
 1/*
 2 * Copyright (C) 2013 HUAWEI
 3 * Author: Cai Zhiyong <caizhiyong@huawei.com>
 4 *
 5 * Read block device partition table from the command line.
 6 * Typically used for fixed block (eMMC) embedded devices.
 7 * It has no MBR, so saves storage space. Bootloader can be easily accessed
 8 * by absolute address of data on the block device.
 9 * Users can easily change the partition.
10 *
11 * The format for the command line is just like mtdparts.
12 *
13 * For further information, see "Documentation/block/cmdline-partition.txt"
14 *
15 */
16
17#include <linux/cmdline-parser.h>
18
19#include "check.h"
20#include "cmdline.h"
21
22static char *cmdline;
23static struct cmdline_parts *bdev_parts;
24
25static int add_part(int slot, struct cmdline_subpart *subpart, void *param)
26{
27	int label_min;
28	struct partition_meta_info *info;
29	char tmp[sizeof(info->volname) + 4];
30	struct parsed_partitions *state = (struct parsed_partitions *)param;
31
32	if (slot >= state->limit)
33		return 1;
34
35	put_partition(state, slot, subpart->from >> 9,
36		      subpart->size >> 9);
37
38	info = &state->parts[slot].info;
39
40	label_min = min_t(int, sizeof(info->volname) - 1,
41			  sizeof(subpart->name));
42	strncpy(info->volname, subpart->name, label_min);
43	info->volname[label_min] = '\0';
44
45	snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
46	strlcat(state->pp_buf, tmp, PAGE_SIZE);
47
48	state->parts[slot].has_info = true;
49
50	return 0;
51}
52
53static int __init cmdline_parts_setup(char *s)
54{
55	cmdline = s;
56	return 1;
57}
58__setup("blkdevparts=", cmdline_parts_setup);
59
60/*
61 * Purpose: allocate cmdline partitions.
62 * Returns:
63 * -1 if unable to read the partition table
64 *  0 if this isn't our partition table
65 *  1 if successful
66 */
67int cmdline_partition(struct parsed_partitions *state)
68{
69	sector_t disk_size;
70	char bdev[BDEVNAME_SIZE];
71	struct cmdline_parts *parts;
72
73	if (cmdline) {
74		if (bdev_parts)
75			cmdline_parts_free(&bdev_parts);
76
77		if (cmdline_parts_parse(&bdev_parts, cmdline)) {
78			cmdline = NULL;
79			return -1;
80		}
81		cmdline = NULL;
82	}
83
84	if (!bdev_parts)
85		return 0;
86
87	bdevname(state->bdev, bdev);
88	parts = cmdline_parts_find(bdev_parts, bdev);
89	if (!parts)
90		return 0;
91
92	disk_size = get_capacity(state->bdev->bd_disk) << 9;
93
94	cmdline_parts_set(parts, disk_size, 1, add_part, (void *)state);
95
96	strlcat(state->pp_buf, "\n", PAGE_SIZE);
97
98	return 1;
99}