summaryrefslogtreecommitdiffstats
path: root/common/partition.c
blob: 0da359d078e7239420c8409210ea039e05040a7f (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

#include <common.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 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);