-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpartition.c
182 lines (148 loc) · 4.97 KB
/
partition.c
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
/*
* File : partition.h
* COPYRIGHT (C) 2016, Shanghai Real-Thread Technology Co., Ltd
*
* Change Logs:
* Date Author Notes
* 2016-09-20 Bernard the first version
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <rtthread.h>
#include <rtdevice.h>
#include "partition.h"
// #define PARTITION_DEBUG
#ifdef PARTITION_DEBUG
#define DEBUG_TRACE rt_kprintf("[part] "); rt_kprintf
#else
#define DEBUG_TRACE(...)
#endif
struct partition_device
{
struct rt_device parent; /* parent block device */
int sector_size;
int block_size;
struct rt_device *block_device;
const struct rt_partition *partition;
};
/* RT-Thread device interface */
static rt_err_t partition_init(rt_device_t dev)
{
return RT_EOK;
}
static rt_err_t partition_open(rt_device_t dev, rt_uint16_t oflag)
{
return RT_EOK;
}
static rt_err_t partition_close(rt_device_t dev)
{
return RT_EOK;
}
#if RTTHREAD_VERSION >= 30000
static rt_err_t partition_control(rt_device_t dev, int cmd, void *args)
#else
static rt_err_t partition_control(rt_device_t dev, rt_uint8_t cmd, void *args)
#endif
{
struct partition_device *part;
part = (struct partition_device*) dev;
RT_ASSERT(part != RT_NULL);
if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
{
struct rt_device_blk_geometry *geometry;
geometry = (struct rt_device_blk_geometry *)args;
if (geometry == RT_NULL) return -RT_ERROR;
geometry->bytes_per_sector = part->sector_size;
geometry->sector_count = part->partition->size / part->sector_size;
geometry->block_size = part->block_size;
}
return RT_EOK;
}
static rt_size_t partition_read(rt_device_t dev,
rt_off_t pos,
void* buffer,
rt_size_t size)
{
struct partition_device *part;
part = (struct partition_device*) dev;
RT_ASSERT(part != RT_NULL);
DEBUG_TRACE("read block: %d, size=%d\n", pos + part->partition->offset/part->sector_size, size);
return rt_device_read(part->block_device, pos + part->partition->offset/part->sector_size, buffer, size);
}
static rt_size_t partition_write(rt_device_t dev,
rt_off_t pos,
const void* buffer,
rt_size_t size)
{
struct partition_device *part;
part = (struct partition_device*) dev;
RT_ASSERT(part != RT_NULL);
if (part->partition->flags & PARTITION_WRITEABLE)
{
DEBUG_TRACE("write block: %d, size=%d\n", pos + part->partition->offset/part->sector_size, size);
return rt_device_write(part->block_device, pos + part->partition->offset/part->sector_size,
buffer, size);
}
DEBUG_TRACE("read-only partition!\n");
return 0;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops partition_dev_ops =
{
.init = partition_init,
.open = partition_open,
.close = partition_close,
.read = partition_read,
.write = partition_write,
.control = partition_control
};
#endif
int rt_partition_init(const char* flash_device, const struct rt_partition* parts, rt_size_t num)
{
struct rt_device *device;
struct partition_device *part_dev;
const struct rt_partition* part;
struct rt_device_blk_geometry geometry;
device = (struct rt_device *)rt_device_find(flash_device);
if(device == RT_NULL)
{
DEBUG_TRACE("block device %s not found!\r\n", flash_device);
return -RT_ENOSYS;
}
/* open the block device */
rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
/* get block device geometry */
rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
/* create each partition device */
while(num --)
{
part = parts ++;
part_dev = (struct partition_device*)rt_malloc (sizeof(struct partition_device));
if (part_dev)
{
part_dev->block_device = device;
part_dev->partition = part;
part_dev->sector_size = geometry.bytes_per_sector;
part_dev->block_size = geometry.block_size;
/* register device */
part_dev->parent.type = RT_Device_Class_Block;
#ifdef RT_USING_DEVICE_OPS
part_dev->parent.ops = &partition_dev_ops;
#else
part_dev->parent.init = partition_init;
part_dev->parent.open = partition_open;
part_dev->parent.close = partition_close;
part_dev->parent.read = partition_read;
part_dev->parent.write = partition_write;
part_dev->parent.control = partition_control;
#endif
/* no private */
part_dev->parent.user_data = RT_NULL;
DEBUG_TRACE("add partition: %s\n", part->name);
rt_device_register(RT_DEVICE(part_dev), part->name,
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
}
}
return RT_EOK;
}