source: rtems/cpukit/libblock/include/rtems/bdpart.h @ f6c7bcfe

4.115
Last change on this file since f6c7bcfe was f6c7bcfe, checked in by Mathew Kallada <matkallada@…>, on 12/21/12 at 17:42:39

libblock: Doxygen Enhancement Task #1

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