summaryrefslogtreecommitdiffstats
path: root/common/partition.c
blob: bb4006eb0a13f4637fccc15e0f9626032ec96f52 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * (C) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

/**
 * @file
 * @brief Partition handling on top of devices
 */

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

/**
 * Add one partition on top of a device, as a device.
 * @param[in] dev The device to add a partition on
 * @param[in] offset Start offset of the partition inside the whole device
 * @param[in] size Size of the new partition
 * @param[in] flags FIXME
 * @param[in] name Partition name (can be obtained with devinfo command)
 * @return The device representing the new partition.
 */
struct device_d *dev_add_partition(struct device_d *dev, unsigned long offset,
		size_t size, int flags, const 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;
	part->flags = flags;

	register_device(&part->device);
	dev_add_child(dev, &part->device);

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

	free(part);
	return 0;
}

/**
 * Erase partition content.
 * @param[in] dev The partition info as a device
 * @param[in] count Length in bytes to erase
 * @param[in] offset Start offset within the partition
 * @return -1 if no erase feature for this device is available, anything else
 * the erase function returns.
 */
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;
}

/**
 * Protecting a partition.
 * @param[in] dev The partition info as a device
 * @param[in] count Length in bytes to protect
 * @param[in] offset Start offset within the partition
 * @param[in] prot FIXME
 * @return -1 if FIXME.
 */
static int part_protect(struct device_d *dev, size_t count, unsigned long offset, int prot)
{
	struct partition *part = dev->type_data;

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

	return -1;
}

/**
 * FIXME.
 * @param[in] dev The partition info as a device
 * @param[in] map FXIME
 * @param[in] flags FIXME
 * @return -1 if FIXME.
 */
static int part_memmap(struct device_d *dev, void **map, int flags)
{
	struct partition *part = dev->type_data;
	int ret;

	if (part->physdev->driver->memmap) {
		ret = part->physdev->driver->memmap(part->physdev, map, flags);
		if (ret)
			return ret;
		*map = (void *)((unsigned long)*map + part->offset);
		return 0;
	}

	return -1;
}

/**
 * FIXME.
 * @param[in] dev The partition info as a device
 * @param[in] buf FXIME
 * @param[in] count FXIME
 * @param[in] offset FXIME
 * @param[in] flags FIXME
 * @return FIXME.
 */
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);
}

/**
 * FIXME.
 * @param[in] dev The partition info as a device
 * @param[in] buf FXIME
 * @param[in] count FXIME
 * @param[in] offset FXIME
 * @param[in] flags FIXME
 * @return FIXME.
 */
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->flags & PARTITION_READONLY)
 		return -EROFS;
	else
		return dev_write(part->physdev, buf, count, offset + part->offset, flags);
}

/**
 * FIXME.
 * @param[in] dev The partition info as a device
 * @return FIXME.
 */
static int part_probe(struct device_d *dev)
{
#ifdef DEBUG
	struct partition *part = dev->type_data;
#endif

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

/**
 * FIXME.
 * @param[in] dev The partition info as a device
 * @return 0
 */
static int part_remove(struct device_d *dev)
{
	return 0;
}

/**
 * Partition driver description
 */
struct driver_d part_driver = {
	.name  	= "partition",
	.probe 	= part_probe,
	.remove = part_remove,
	.read  	= part_read,
	.write 	= part_write,
	.erase 	= part_erase,
	.protect= part_protect,
	.memmap = part_memmap,
};

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

device_initcall(partition_init);

/**
@page partitions Partition Handling

Partitions are runtime informartion only, not permanent. So they must be set
everytime the system starts. The required command can be embedded into the
default environment.

@note Partitions defined in this way are intended to be used with the kernel
command line partition parsing feature. In Uboot2 these types of partitions are
handled in the same way as every other device.

@par The addpart command

What we want:

@code
 device nor0
    |--- partition 0
    |--- partition 1
    |--- partition 2
    |--- partition 3
    `--- partition 4
@endcode

How to get:

@code
$ addpart /dev/nor0 256k(uboot),128k(env),256k(bla),1024k(blubb),2048k(friesel)
$ devinfo
 |---nor0.uboot
 |---nor0.env
 |---nor0.bla
 |---nor0.blubb
 |---nor0.friesel
@endcode

@par Partitions with sub partitions:

Partitions are based on devices. And partitions will result into devices. So
there is a way to create sub partitions on partitions.

What we want:

@code
 device nor0
    |--- partition 0
    |--- partition 1
    |--- partition 2
    |--- partition 3
    `--- partition 4
           |--- partition 0
           `--- partition 1
@endcode

How to get:

@code
$ addpart /dev/nor0 256k(uboot),128k(env),256k(bla),1M(blubb),2048k(friesel)
$ devinfo
 |---nor0.uboot
 |---nor0.env
 |---nor0.bla
 |---nor0.blubb
 |---nor0.friesel

$ addpart /dev/nor0.friesel 1024(fussel),1024k(boerks)
$ devinfo
 |---nor0.uboot
 |---nor0.env
 |---nor0.bla
 |---nor0.blubb
 |---nor0.friesel
        |---nor0.friesel.fussel
        `---nor0.friesel.boerks
@endcode

@par Forwarding partitions to the kernel:

@code
$ device="nor0"
$ partitions="256k(uboot),128k(env),256k(bla),1024k(blubb),2048k(friesel)"
$ addpart /dev/$device:$partitions
@endcode

@par Removing partitions:

As partitions are a logically information only, they can be removed from a
device at runtime. You can't remove a single partition within others on the
same device. Only all partitions on the given device can be removed with this
command.

As sub partitions occure as devices you also can remove sub partitions from
their parent in this way. Partitions cannot be removed as long as they are
mounted or have subpartitions.

*/