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