source: rtems/cpukit/include/rtems/bdpart.h

Last change on this file was bcef89f2, checked in by Sebastian Huber <sebastian.huber@…>, on 05/19/23 at 06:18:25

Update company name

The embedded brains GmbH & Co. KG is the legal successor of embedded
brains GmbH.

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