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

4.115
Last change on this file since a8afce38 was a8afce38, checked in by Sebastian Huber <sebastian.huber@…>, on 02/17/11 at 16:25:43

2011-02-17 Sebastian Huber <sebastian.huber@…>

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