1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * Block device partition management. |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * Copyright (c) 2009 |
---|
9 | * embedded brains GmbH |
---|
10 | * Obere Lagerstr. 30 |
---|
11 | * D-82178 Puchheim |
---|
12 | * Germany |
---|
13 | * <rtems@embedded-brains.de> |
---|
14 | * |
---|
15 | * The license and distribution terms for this file may be |
---|
16 | * found in the file LICENSE in this distribution or at |
---|
17 | * http://www.rtems.com/license/LICENSE. |
---|
18 | */ |
---|
19 | |
---|
20 | #ifndef RTEMS_BDPART_H |
---|
21 | #define RTEMS_BDPART_H |
---|
22 | |
---|
23 | #include <uuid/uuid.h> |
---|
24 | |
---|
25 | #include <rtems.h> |
---|
26 | #include <rtems/blkdev.h> |
---|
27 | |
---|
28 | #ifdef __cplusplus |
---|
29 | extern "C" { |
---|
30 | #endif /* __cplusplus */ |
---|
31 | |
---|
32 | /** |
---|
33 | * @defgroup rtems_bdpart Block Device Partition Management |
---|
34 | * |
---|
35 | * @ingroup rtems_libblock |
---|
36 | * |
---|
37 | * This module provides functions to manage partitions of a disk device. |
---|
38 | * |
---|
39 | * A @ref rtems_disk "disk" is a set of blocks which are identified by a |
---|
40 | * consecutive set of non-negative integers starting at zero. There are also |
---|
41 | * logical disks which contain a subset of consecutive disk blocks. The |
---|
42 | * logical disks are used to represent the partitions of a disk. The disk |
---|
43 | * devices are accessed via the @ref rtems_disk "block device buffer module". |
---|
44 | * |
---|
45 | * The partition format on the physical disk will be converted to an internal |
---|
46 | * representation. It is possible to convert the internal representation into |
---|
47 | * a specific output format and write it to the physical disk. One of the |
---|
48 | * constrains for the internal representation was to support the GPT format |
---|
49 | * easily. |
---|
50 | * |
---|
51 | * Currently two physical partition formats are supported. These are the MBR |
---|
52 | * and the GPT format. Please note that the GPT support is not implemented. |
---|
53 | * With MBR format we mean the partition format of the wide spread IBM |
---|
54 | * PC-compatible systems. The GPT format is defined in the Extensible Firmware |
---|
55 | * Interface (EFI). |
---|
56 | * |
---|
57 | * The most common task will be to read the partition information of a disk and |
---|
58 | * register logical disks for each partition. This can be done with the |
---|
59 | * rtems_bdpart_register_from_disk() function. Afterwards you can |
---|
60 | * @ref rtems_fsmount "mount" the file systems within the partitions. |
---|
61 | * |
---|
62 | * You can read the partition information from a disk with rtems_bdpart_read() |
---|
63 | * and write it to the disk with rtems_bdpart_write(). |
---|
64 | * |
---|
65 | * To create a partition table from scratch for a disk use |
---|
66 | * rtems_bdpart_create(). |
---|
67 | * |
---|
68 | * You can access some disk functions with the shell command @c fdisk. |
---|
69 | * |
---|
70 | * References used to create this module: |
---|
71 | * - <a href="http://en.wikipedia.org/wiki/UUID">Universally Unique Identifier</a> |
---|
72 | * - <a href="http://en.wikipedia.org/wiki/Globally_Unique_Identifier">Globally Unique Identifier</a> |
---|
73 | * - <a href="http://en.wikipedia.org/wiki/Disk_partitioning">Disk Paritioning</a> |
---|
74 | * - <a href="http://en.wikipedia.org/wiki/GUID_Partition_Table">GUID Partition Table</a> |
---|
75 | * - <a href="http://en.wikipedia.org/wiki/Master_boot_record">Master Boot Record</a> |
---|
76 | * - <a href="http://en.wikipedia.org/wiki/Extended_boot_record">Extended Boot Record</a> |
---|
77 | * - <a href="http://en.wikipedia.org/wiki/Cylinder-head-sector">Cylinder Head Sector</a> |
---|
78 | * - <a href="http://www.win.tue.nl/~aeb/partitions/partition_types-1.html">Partition Types</a> |
---|
79 | * |
---|
80 | * @{ |
---|
81 | */ |
---|
82 | |
---|
83 | /** |
---|
84 | * @name MBR Partition Types and Flags |
---|
85 | * |
---|
86 | * @{ |
---|
87 | */ |
---|
88 | |
---|
89 | #define RTEMS_BDPART_MBR_EMPTY 0x0U |
---|
90 | |
---|
91 | #define RTEMS_BDPART_MBR_FAT_12 0x1U |
---|
92 | |
---|
93 | #define RTEMS_BDPART_MBR_FAT_16 0x4U |
---|
94 | |
---|
95 | #define RTEMS_BDPART_MBR_FAT_16_LBA 0xeU |
---|
96 | |
---|
97 | #define RTEMS_BDPART_MBR_FAT_32 0xbU |
---|
98 | |
---|
99 | #define RTEMS_BDPART_MBR_FAT_32_LBA 0xcU |
---|
100 | |
---|
101 | #define RTEMS_BDPART_MBR_EXTENDED 0x5U |
---|
102 | |
---|
103 | #define RTEMS_BDPART_MBR_DATA 0xdaU |
---|
104 | |
---|
105 | #define RTEMS_BDPART_MBR_GPT 0xeeU |
---|
106 | |
---|
107 | #define RTEMS_BDPART_MBR_FLAG_ACTIVE 0x80U |
---|
108 | |
---|
109 | /** @} */ |
---|
110 | |
---|
111 | /** |
---|
112 | * Recommended maximum partition table size. |
---|
113 | */ |
---|
114 | #define RTEMS_BDPART_PARTITION_NUMBER_HINT 16 |
---|
115 | |
---|
116 | /** |
---|
117 | * Partition description. |
---|
118 | */ |
---|
119 | typedef struct rtems_bdpart_partition { |
---|
120 | /** |
---|
121 | * Block index for partition begin. |
---|
122 | */ |
---|
123 | rtems_blkdev_bnum begin; |
---|
124 | |
---|
125 | /** |
---|
126 | * Block index for partition end (this block is not a part of the partition). |
---|
127 | */ |
---|
128 | rtems_blkdev_bnum end; |
---|
129 | |
---|
130 | /** |
---|
131 | * Partition type. |
---|
132 | */ |
---|
133 | uuid_t type; |
---|
134 | |
---|
135 | /** |
---|
136 | * Partition ID. |
---|
137 | */ |
---|
138 | uuid_t id; |
---|
139 | |
---|
140 | /** |
---|
141 | * Partition flags. |
---|
142 | */ |
---|
143 | uint64_t flags; |
---|
144 | } rtems_bdpart_partition; |
---|
145 | |
---|
146 | /** |
---|
147 | * Disk format for the partition tables. |
---|
148 | */ |
---|
149 | typedef enum { |
---|
150 | /** |
---|
151 | * Type value for MBR format. |
---|
152 | */ |
---|
153 | RTEMS_BDPART_FORMAT_MBR, |
---|
154 | |
---|
155 | /** |
---|
156 | * Type value for GPT format. |
---|
157 | */ |
---|
158 | RTEMS_BDPART_FORMAT_GPT |
---|
159 | } rtems_bdpart_format_type; |
---|
160 | |
---|
161 | /** |
---|
162 | * Disk format description. |
---|
163 | */ |
---|
164 | typedef struct { |
---|
165 | /** |
---|
166 | * Format type. |
---|
167 | */ |
---|
168 | rtems_bdpart_format_type type; |
---|
169 | union { |
---|
170 | /** |
---|
171 | * MBR format fields. |
---|
172 | */ |
---|
173 | struct { |
---|
174 | /** |
---|
175 | * Disk ID in MBR at offset 440. |
---|
176 | */ |
---|
177 | uint32_t disk_id; |
---|
178 | |
---|
179 | /** |
---|
180 | * This option is used for partition table creation and validation checks |
---|
181 | * before a write to the disk. It ensures that the first primary |
---|
182 | * partition and the logical partitions start at head one and sector one |
---|
183 | * under the virtual one head and 63 sectors geometry. Each begin and |
---|
184 | * end of a partition will be aligned to the virtual cylinder boundary. |
---|
185 | */ |
---|
186 | bool dos_compatibility; |
---|
187 | } mbr; |
---|
188 | |
---|
189 | /** |
---|
190 | * GPT format fields. |
---|
191 | */ |
---|
192 | struct { |
---|
193 | /** |
---|
194 | * Disk ID in GPT header. |
---|
195 | */ |
---|
196 | uuid_t disk_id; |
---|
197 | } gpt; |
---|
198 | }; |
---|
199 | } rtems_bdpart_format; |
---|
200 | |
---|
201 | /** |
---|
202 | * Reads the partition information from the physical disk device with name |
---|
203 | * @a disk_name. |
---|
204 | * |
---|
205 | * The partition information will be stored in the partition table |
---|
206 | * @a partitions with a maximum of @a count partitions. The number of actual |
---|
207 | * partitions will be stored in @a count. If there are more partitions than |
---|
208 | * space for storage an error status will be returned. The partition table |
---|
209 | * format recognized on the disk will be stored in @a format. |
---|
210 | */ |
---|
211 | rtems_status_code rtems_bdpart_read( |
---|
212 | const char *disk_name, |
---|
213 | rtems_bdpart_format *format, |
---|
214 | rtems_bdpart_partition *partitions, |
---|
215 | size_t *count |
---|
216 | ); |
---|
217 | |
---|
218 | /** |
---|
219 | * Sorts the partition table @a partitions with @a count partitions to have |
---|
220 | * ascending begin blocks |
---|
221 | */ |
---|
222 | void rtems_bdpart_sort( rtems_bdpart_partition *partitions, size_t count); |
---|
223 | |
---|
224 | /** |
---|
225 | * Writes the partition table to the physical disk device with name |
---|
226 | * @a disk_name. |
---|
227 | * |
---|
228 | * The partition table @a partitions with @a count partitions will be written |
---|
229 | * to the disk. The output format for the partition table on the disk is |
---|
230 | * specified by @a format. There are some consistency checks applied to the |
---|
231 | * partition table. The partition table must be sorted such that the begin |
---|
232 | * blocks are in ascending order. This can be done with the |
---|
233 | * rtems_bdpart_sort() function. The partitions must not overlap. The |
---|
234 | * partitions must have a positive size. The partitions must be within the |
---|
235 | * disk. Depending on the output format there are additional constrains. |
---|
236 | */ |
---|
237 | rtems_status_code rtems_bdpart_write( |
---|
238 | const char *disk_name, |
---|
239 | const rtems_bdpart_format *format, |
---|
240 | const rtems_bdpart_partition *partitions, |
---|
241 | size_t count |
---|
242 | ); |
---|
243 | |
---|
244 | /** |
---|
245 | * Creates a partition table in @a partitions with @a count partitions for the |
---|
246 | * physical disk device with name @a disk_name. |
---|
247 | * |
---|
248 | * The array of positive integer weights in @a distribution must be of size @a |
---|
249 | * size. The weights in the distribution array are summed up. Each weight is |
---|
250 | * then divided by the sum to obtain the disk fraction which forms the |
---|
251 | * corresponding partition. The partition boundaries are generated with |
---|
252 | * respect to the output format in @a format. |
---|
253 | */ |
---|
254 | rtems_status_code rtems_bdpart_create( |
---|
255 | const char *disk_name, |
---|
256 | const rtems_bdpart_format *format, |
---|
257 | rtems_bdpart_partition *partitions, |
---|
258 | const unsigned *distribution, |
---|
259 | size_t count |
---|
260 | ); |
---|
261 | |
---|
262 | /** |
---|
263 | * Registers the partitions as logical disks for the physical disk device with |
---|
264 | * name @a disk_name. |
---|
265 | * |
---|
266 | * For each partition of the partition table @a partitions with @a count |
---|
267 | * partitions a logical disk is registered. The partition number equals the |
---|
268 | * partition table index plus one. The name of the logical disk device is the |
---|
269 | * concatenation of the physical disk device name and the partition number. |
---|
270 | */ |
---|
271 | rtems_status_code rtems_bdpart_register( |
---|
272 | const char *disk_name, |
---|
273 | const rtems_bdpart_partition *partitions, |
---|
274 | size_t count |
---|
275 | ); |
---|
276 | |
---|
277 | /** |
---|
278 | * Reads the partition table from the disk device with name @a disk_name and |
---|
279 | * registers the partitions as logical disks. |
---|
280 | * |
---|
281 | * @see rtems_bdpart_register() and rtems_fsmount(). |
---|
282 | */ |
---|
283 | rtems_status_code rtems_bdpart_register_from_disk( const char *disk_name); |
---|
284 | |
---|
285 | /** |
---|
286 | * Deletes the logical disks associated with the partitions of the disk device |
---|
287 | * with name @a disk_name. |
---|
288 | * |
---|
289 | * The partition table @a partitions with @a count partitions will be used to |
---|
290 | * determine which disks need to be deleted. It may be obtained from |
---|
291 | * rtems_bdpart_read(). |
---|
292 | */ |
---|
293 | rtems_status_code rtems_bdpart_unregister( |
---|
294 | const char *disk_name, |
---|
295 | const rtems_bdpart_partition *partitions, |
---|
296 | size_t count |
---|
297 | ); |
---|
298 | |
---|
299 | /** |
---|
300 | * Mounts all supported file systems inside the logical disks derived from the |
---|
301 | * partitions of the physical disk device with name @a disk_name. |
---|
302 | * |
---|
303 | * For each partition in the partition table @a partitions with @a count |
---|
304 | * partitions it will be checked if it contains a supported file system. In |
---|
305 | * this case a mount point derived from the disk name will be created in the |
---|
306 | * mount base path @a mount_base. The file system will be mounted there. The |
---|
307 | * partition number equals the partition table index plus one. The mount point |
---|
308 | * name for each partition will be the concatenation of the mount base path, |
---|
309 | * the disk device file name and the parition number. |
---|
310 | * |
---|
311 | * @see rtems_bdpart_read(). |
---|
312 | */ |
---|
313 | rtems_status_code rtems_bdpart_mount( |
---|
314 | const char *disk_name, |
---|
315 | const rtems_bdpart_partition *partitions, |
---|
316 | size_t count, |
---|
317 | const char *mount_base |
---|
318 | ); |
---|
319 | |
---|
320 | /** |
---|
321 | * Unmounts all file systems mounted with rtems_bdpart_mount(). |
---|
322 | */ |
---|
323 | rtems_status_code rtems_bdpart_unmount( |
---|
324 | const char *disk_name, |
---|
325 | const rtems_bdpart_partition *partitions, |
---|
326 | size_t count, |
---|
327 | const char *mount_base |
---|
328 | ); |
---|
329 | |
---|
330 | /** |
---|
331 | * Prints the partition table @a partitions with @a count partitions to |
---|
332 | * standard output. |
---|
333 | */ |
---|
334 | void rtems_bdpart_dump( const rtems_bdpart_partition *partitions, size_t count); |
---|
335 | |
---|
336 | /** |
---|
337 | * Returns the partition type for the MBR partition type value @a mbr_type in |
---|
338 | * @a type. |
---|
339 | */ |
---|
340 | void rtems_bdpart_to_partition_type( uint8_t mbr_type, uuid_t type); |
---|
341 | |
---|
342 | /** |
---|
343 | * Converts the partition type in @a type to the MBR partition type. |
---|
344 | * |
---|
345 | * The result will be stored in @a mbr_type. Returns @c true in case of a |
---|
346 | * successful convertion and otherwise @c false. Both arguments must not be |
---|
347 | * @c NULL. |
---|
348 | */ |
---|
349 | bool rtems_bdpart_to_mbr_partition_type( |
---|
350 | const uuid_t type, |
---|
351 | uint8_t *mbr_type |
---|
352 | ); |
---|
353 | |
---|
354 | /** @} */ |
---|
355 | |
---|
356 | #ifdef __cplusplus |
---|
357 | } |
---|
358 | #endif /* __cplusplus */ |
---|
359 | |
---|
360 | #endif /* RTEMS_BDPART_H */ |
---|