summaryrefslogtreecommitdiffstats
path: root/common/partition.c
blob: 919e2e2e08845b0f09e57189471c74cfde7e4980 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

#include <common.h>
#include <command.h>
#include <init.h>
#include <driver.h>
#include <malloc.h>
#include <errno.h>
#include <partition.h>
#include <xfuncs.h>

struct device_d *dev_add_partition(struct device_d *dev, unsigned long offset, size_t size, char *name)
{
        struct partition *part;

	if (offset + size > dev->size)
		return NULL;

	part = xzalloc(sizeof(struct partition));

	strcpy(part->device.name, "partition");
	part->device.map_base = dev->map_base + offset;
	part->device.size = size;
	part->device.type_data = part;
	get_free_deviceid(part->device.id, name);

	part->offset = offset;
	part->physdev = dev;

	register_device(&part->device);

	if (part->device.driver)
		return &part->device;

	free(part);
	return 0;
}

static void dev_del_partitions(struct device_d *physdev)
{
        struct device_d *dev;
        char buf[MAX_DRIVER_NAME];
        int i = 0;

        /* This is lame. Devices should to able to have children */
        while (1) {
                sprintf(buf, "%s.%d", physdev->id, i);
                dev = device_from_spec_str(buf, NULL);
                if (dev) {
			struct partition *part = dev->type_data;
                        unregister_device(dev);
			free(part);
		}
                else
                        break;
                i++;
        }
}

int mtd_part_do_parse_one (struct partition *part, const char *str, char **endp)
{
        ulong size;
        char *end;
        char buf[MAX_DRIVER_NAME];

	memset(buf, 0, MAX_DRIVER_NAME);

        if (*str == '-') {
                size = part->physdev->size - part->offset;
		end = (char *)str + 1;
        } else {
		size = strtoul_suffix(str, &end, 0);
	}

	if (size + part->offset > part->physdev->size) {
		printf("partition end is beyond device\n");
		return -EINVAL;
	}

        str = end;

        if (*str == '(') {
		str++;
                end = strchr(str, ')');
                if (!end) {
                        printf("could not find matching ')'\n");
                        return -EINVAL;
                }
                if (end - str >= MAX_DRIVER_NAME) {
                        printf("device name too long\n");
                        return -EINVAL;
                }

                memcpy(part->name, str, end - str);
                end++;
        }

        str = end;

        if (*str == 'r' && *(str + 1) == 'o') {
                part->readonly = 1;
                end = (char *)(str + 2);
        }

        if (endp)
                *endp = end;

        strcpy(part->device.name, "partition");
        part->device.size = size;

        return 0;
}

int do_addpart ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
        struct partition *part;
        struct device_d *dev;
        char *endp;
        int num = 0;
        unsigned long offset;

        if (argc != 2) {
		printf ("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}

        dev = device_from_spec_str(argv[1], &endp);
        if (!dev) {
                printf("no such device: %s\n",argv[1]);
                return 1;
        }

	dev_del_partitions(dev);

        offset = 0;

        while (1) {
                part = xzalloc(sizeof(struct partition));

                part->offset = offset;
                part->physdev = dev;
                part->num = num;
                part->device.map_base = dev->map_base + offset;

                if(mtd_part_do_parse_one(part, endp, &endp)) {
			dev_del_partitions(dev);
                        free(part);
                        return 1;
                }

                offset += part->device.size;

                part->device.type_data = part;

                sprintf(part->device.id, "%s.%d", dev->id, num);
                register_device(&part->device);
                num++;

                if(!*endp)
                        break;
                if(*endp != ',') {
                        printf("parse error\n");
                        return 1;
                }
                endp++;
        }

        return 0;
}

static __maybe_unused char cmd_addpart_help[] =
"Usage: addpart <partition description>\n"
"addpart adds a partition description to a device. The partition description\n"
"has the form\n"
"dev:size1(name1)[ro],size2(name2)[ro],...\n"
"<dev> is the device name under /dev. Size can be given in decimal or if\n"
"prefixed with 0x in hex. Sizes can have an optional suffix K,M,G. The size\n"
"of the last partition can be specified as '-' for the remaining space of the\n"
"device.\n"
"This format is the same as used in the Linux kernel for cmdline mtd partitions.\n"
"Note That this command has to be reworked and will probably change it's API.";

U_BOOT_CMD_START(addpart)
	.maxargs	= 2,
	.cmd		= do_addpart,
	.usage		= "add a partition table to a device",
	U_BOOT_CMD_HELP(cmd_addpart_help)
U_BOOT_CMD_END

int do_delpart ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
        struct device_d *dev;

        if (argc != 2) {
		printf ("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}

        dev = device_from_spec_str(argv[1], NULL);
        if (!dev) {
                printf("no such device: %s\n",argv[1]);
                return 1;
        }

	dev_del_partitions(dev);

        return 0;
}

static __maybe_unused char cmd_delpart_help[] =
"Usage: delpart <dev>\n"
"Delete partitions previously added to a device with addpart.\n"
"Note: You have to specify the device as 'devid', _not_ as '/dev/devid'. This\n"
"will likely change soon.\n";

U_BOOT_CMD_START(delpart)
	.maxargs	= 2,
	.cmd		= do_delpart,
	.usage		= "delete a partition table from a device",
	U_BOOT_CMD_HELP(cmd_delpart_help)
U_BOOT_CMD_END

static int part_erase(struct device_d *dev, size_t count, unsigned long offset)
{
        struct partition *part = dev->type_data;

        if (part->physdev->driver->erase)
                return part->physdev->driver->erase(part->physdev, count, offset + part->offset);

        return -1;
}

static ssize_t part_read(struct device_d *dev, void *buf, size_t count, unsigned long offset, ulong flags)
{
        struct partition *part = dev->type_data;

        return dev_read(part->physdev, buf, count, offset + part->offset, flags);
}

static ssize_t part_write(struct device_d *dev, const void *buf, size_t count, unsigned long offset, ulong flags)
{
        struct partition *part = dev->type_data;

	if (part->readonly)
		return -EROFS;
	else
		return dev_write(part->physdev, buf, count, offset + part->offset, flags);
}

static int part_probe(struct device_d *dev)
{
        struct partition *part = dev->type_data;

        printf("registering partition %s on device %s (size=0x%08x, name=%s)\n",
                        dev->id, part->physdev->id, dev->size, part->name);
        return 0;
}

static int part_remove(struct device_d *dev)
{
	return 0;
}

struct driver_d part_driver = {
        .name  	= "partition",
        .probe 	= part_probe,
	.remove = part_remove,
        .read  	= part_read,
        .write 	= part_write,
        .erase 	= part_erase,
};

static int partition_init(void)
{
        return register_driver(&part_driver);
}

device_initcall(partition_init);