summaryrefslogtreecommitdiffstats
path: root/lib/cmdlinepart.c
blob: 5a164628749ffbe3ca0691f7dc7142ac3b7998c5 (plain) (blame)
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
/*
 * command line partition parsing code
 *
 * Copyright (c) 2015 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
#include <common.h>
#include <driver.h>
#include <fs.h>
#include <linux/err.h>
#include <cmdlinepart.h>

#define SIZE_REMAINING ((loff_t)-1)

int cmdlinepart_do_parse_one(const char *devname, const char *partstr,
				 const char **endp, loff_t *offset,
				 loff_t devsize, loff_t *retsize,
				 unsigned int partition_flags)
{
	loff_t size;
	char *end;
	char buf[PATH_MAX] = {};
	unsigned long flags = 0;
	int ret = 0;
	struct cdev *cdev;

	memset(buf, 0, PATH_MAX);

	if (*partstr == '-') {
		size = SIZE_REMAINING;
		end = (char *)partstr + 1;
	} else {
		size = strtoull_suffix(partstr, &end, 0);
	}

	if (*end == '@')
		*offset = strtoull_suffix(end+1, &end, 0);

	if (size == SIZE_REMAINING)
		size = devsize - *offset;

	partstr = end;

	if (*partstr == '(') {
		partstr++;
		end = strchr((char *) partstr, ')');
		if (!end) {
			printf("could not find matching ')'\n");
			return -EINVAL;
		}

		if ((partition_flags & CMDLINEPART_ADD_DEVNAME) &&
				strncmp(devname, partstr, strlen(devname)))
			sprintf(buf, "%s.", devname);
		memcpy(buf + strlen(buf), partstr, end - partstr);

		end++;
	}

	if (size + *offset > devsize) {
		printf("%s: partition end is beyond device\n", buf);
		return -EINVAL;
	}

	partstr = end;

	if (*partstr == 'r' && *(partstr + 1) == 'o') {
		flags |= DEVFS_PARTITION_READONLY;
		end = (char *)(partstr + 2);
	}

	if (endp)
		*endp = end;

	*retsize = size;

	cdev = devfs_add_partition(devname, *offset, size, flags, buf);
	if (IS_ERR(cdev)) {
		ret = PTR_ERR(cdev);
		printf("cannot create %s: %s\n", buf, strerror(-ret));
	}

	return ret;
}

int cmdlinepart_do_parse(const char *devname, const char *parts, loff_t devsize,
		unsigned partition_flags)
{
	loff_t offset = 0;
	int ret;

	if (!parts || *parts == '\0')
		return 0;

	while (1) {
		loff_t size = 0;

		ret = cmdlinepart_do_parse_one(devname, parts, &parts, &offset,
				devsize, &size, partition_flags);
		if (ret)
			return ret;

		offset += size;
		if (!*parts)
			break;

		if (*parts != ',') {
			printf("parse error\n");
			return -EINVAL;
		}
		parts++;
	}

	return 0;
}