summaryrefslogtreecommitdiff
path: root/block/partitions/blkdev_parts.c
blob: 030565b7ce7219422df82676e4e8081c1c5d7e51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 *
 * Copyright (C) ST-Ericsson SA 2010
 *
 * Author: Ulf Hansson <ulf.hansson@stericsson.com> for ST-Ericsson
 * License terms: GNU General Public License (GPL) version 2
 *
 * Create partitions for block devices by reading from the kernel
 * command line (kernel boot arguments).
 *
 */

#include "check.h"
#include "blkdev_parts.h"

static char *cmdline;

/*
 * This is the handler for our kernel commandline parameter,
 * called from main.c::checksetup().
 * Note that we can not yet kmalloc() anything, so we only save
 * the commandline for later processing.
 */
static int cmdline_setup(char *s)
{
	cmdline = s;
	return 1;
}
__setup("blkdevparts=", cmdline_setup);

/* Parse for a matching blkdev-id and return pointer to partdef */
static char *parse_blkdev_id(char *blkdev_name)
{
	int blkdev_id_len;
	char *p, *blkdev_id;

	/* Start parsing for a matching blkdev-id */
	p = blkdev_id = cmdline;
	while (blkdev_id != NULL) {

		/* Find the end of the blkdev-id string */
		p = strchr(blkdev_id, ':');
		if (p == NULL)
			return NULL;

		/* Check if we found a matching blkdev-id */
		blkdev_id_len = p - blkdev_id;
		if (strlen(blkdev_name)	== blkdev_id_len) {
			if (strncmp(blkdev_name, blkdev_id, blkdev_id_len) == 0)
				return p;
		}

		/* Move to next blkdev-id string if there is one */
		blkdev_id = strchr(p, ';');
		if (blkdev_id != NULL)
			blkdev_id++;
	}
	return NULL;
}

static int parse_partdef(char **part, struct parsed_partitions *state, int part_nbr)
{
	sector_t size, offset;
	char *p = *part;

	/* Skip the beginning "," or ":" */
	p++;

	/* Fetch and verify size from partdef */
	size = simple_strtoull(p, &p, 10);
	if ((size == 0) || (*p != '@'))
		return 0;

	/* Skip the "@" */
	p++;

	/* Fetch offset from partdef and check if there are more parts */
	offset = simple_strtoull(p, &p, 10);
	if (*p == ',')
		*part = p;
	else
		*part = NULL;

	/* Add partition to state */
	put_partition(state, part_nbr, offset, size);
	printk(KERN_INFO "\nPartition: size=%llu, offset=%llu\n",
		(unsigned long long) size,
		(unsigned long long) offset);
	return 1;
}

static int parse_blkdev_parts(char *blkdev_name, struct parsed_partitions *state)
{
	char *partdef;
	int part_nbr = 0;

	/* Find partdef */
	partdef = parse_blkdev_id(blkdev_name);

	/* Add parts */
	while (partdef != NULL) {
		/* Find next part and add it to state */
		part_nbr++;
		if (!parse_partdef(&partdef, state, part_nbr))
			return 0;
	}
	return part_nbr;
}

int blkdev_partition(struct parsed_partitions *state)
{
	char blkdev_name[BDEVNAME_SIZE];

	/* Check if there are any partitions to handle */
	if (cmdline == NULL)
		return 0;

	/* Get the name of the blockdevice we are operating upon */
	if (bdevname(state->bdev, blkdev_name) == NULL) {
		printk(KERN_WARNING "Could not get a blkdev name\n");
		return 0;
	}

	/* Parse for partitions and add them to the state */
	return parse_blkdev_parts(blkdev_name, state);
}