Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:13

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  * 
0006  * @brief "MS-DOS-style" Partition Tables Support
0007  */
0008 
0009 /*
0010  * Copyright (C) 2002 OKTET Ltd., St.-Petersburg, Russia
0011  *
0012  * Author: Konstantin Abramenko <Konstantin.Abramenko@oktet.ru>
0013  *         Alexander Kukuta <Alexander.Kukuta@oktet.ru>
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  *
0036  *****************************************************************************/
0037 
0038 #ifndef _RTEMS_IDE_PART_TABLE_H
0039 #define _RTEMS_IDE_PART_TABLE_H
0040 
0041 #include <rtems/chain.h>
0042 #include <stdio.h>
0043 #include <stdlib.h>
0044 #include <string.h>
0045 #include <errno.h>
0046 #include <sys/ioctl.h>
0047 #include <sys/types.h>
0048 #include <sys/endian.h>
0049 #include <sys/stat.h>
0050 #include <unistd.h>
0051 #include <fcntl.h>
0052 #include <rtems.h>
0053 #include <rtems/blkdev.h>
0054 #include <rtems/libio.h>
0055 #include <rtems/libio_.h>
0056 #include <rtems/bdbuf.h>
0057 #include <rtems/seterr.h>
0058 
0059 /* Minor base number for all logical devices */
0060 #define RTEMS_IDE_SECTOR_BITS                             9
0061 #define RTEMS_IDE_SECTOR_SIZE                             512
0062 #define RTEMS_IDE_PARTITION_DESCRIPTOR_SIZE               16
0063 #define RTEMS_IDE_PARTITION_MAX_PARTITION_NUMBER          63
0064 #define RTEMS_IDE_PARTITION_MAX_SUB_PARTITION_NUMBER      4
0065 #define RTEMS_IDE_PARTITION_DEV_NAME_LENGTH_MAX           16
0066 
0067 #define RTEMS_IDE_PARTITION_MSDOS_SIGNATURE_DATA1         0x55
0068 #define RTEMS_IDE_PARTITION_MSDOS_SIGNATURE_DATA2         0xaa
0069 #define RTEMS_IDE_PARTITION_MSDOS_SIGNATURE_OFFSET        0x1fe
0070 #define RTEMS_IDE_PARTITION_TABLE_OFFSET                  0x1be
0071 #define RTEMS_IDE_PARTITION_TABLE_SIZE                    (4 * 16)
0072 #define RTEMS_IDE_PARTITION_BOOTABLE_OFFSET               0
0073 #define RTEMS_IDE_PARTITION_SYS_TYPE_OFFSET               4
0074 #define RTEMS_IDE_PARTITION_START_OFFSET                  8
0075 #define RTEMS_IDE_PARTITION_SIZE_OFFSET                   12
0076 
0077 /*
0078  * Conversion from and to little-endian byte order. (no-op on i386/i486)
0079  */
0080 #define LE_TO_CPU_U16(v) le16toh(v)
0081 #define LE_TO_CPU_U32(v) le32toh(v)
0082 #define CPU_TO_LE_U16(v) htole16(v)
0083 #define CPU_TO_LE_U32(v) htole32(v)
0084 
0085 /*
0086  * sector_data_t --
0087  *      corresponds to the sector on the device
0088  */
0089 typedef struct rtems_sector_data_s
0090 {
0091     uint32_t   sector_num; /* sector number on the device */
0092     uint8_t    data[RTEMS_ZERO_LENGTH_ARRAY]; /* raw sector data */
0093 } rtems_sector_data_t;
0094 
0095 
0096 /*
0097  * Enum partition types
0098  * see list at http://ata-atapi.com/hiwtab.htm
0099  *
0100  * @todo Should these have RTEMS before them.
0101  */
0102 enum {
0103     EMPTY_PARTITION     = 0x00,
0104     DOS_FAT12_PARTITION = 0x01,
0105     DOS_FAT16_PARTITION = 0x04,
0106     EXTENDED_PARTITION  = 0x05,
0107     DOS_P32MB_PARTITION = 0x06,
0108     FAT32_PARTITION     = 0x0B,
0109     FAT32_LBA_PARTITION = 0x0C,
0110     FAT16_LBA_PARTITION = 0x0E,
0111     DM6_PARTITION       = 0x54,
0112     EZD_PARTITION       = 0x55,
0113     DM6_AUX1PARTITION   = 0x51,
0114     DM6_AUX3PARTITION   = 0x53,
0115     LINUX_SWAP          = 0x82,
0116     LINUX_NATIVE        = 0x83,
0117     LINUX_EXTENDED      = 0x85
0118 };
0119 
0120 
0121 /* Forward declaration */
0122 struct rtems_disk_desc_s;
0123 
0124 /*
0125  * part_desc_t --
0126  *      contains all neccessary information about partition
0127  */
0128 typedef struct rtems_part_desc_s {
0129     uint8_t             bootable; /* is the partition active */
0130     uint8_t             sys_type; /* type of partition */
0131     uint8_t             log_id; /* logical number of partition */
0132     uint32_t            start; /* first partition sector, in absolute
0133                                 * numeration */
0134     uint32_t            size; /* size in sectors */
0135     uint32_t            end; /* last partition sector, end = start + size - 1 */
0136     struct rtems_disk_desc_s *disk_desc; /* descriptor of disk, partition
0137                                           * contains in */
0138     struct rtems_part_desc_s *ext_part; /* extended partition containing this
0139                                          * one */
0140 
0141     /* partitions, containing in this one */
0142     struct rtems_part_desc_s *sub_part[RTEMS_IDE_PARTITION_MAX_SUB_PARTITION_NUMBER];
0143 } rtems_part_desc_t;
0144 
0145 
0146 
0147 typedef struct rtems_disk_desc_s {
0148     /* device name in /dev filesystem */
0149     char         dev_name[RTEMS_IDE_PARTITION_DEV_NAME_LENGTH_MAX];
0150 
0151     uint32_t     sector_size; /* size of sector */
0152     uint32_t     sector_bits; /* the base-2 logarithm of sector_size */
0153     uint32_t     lba_size; /* total amount of sectors in lba address mode */
0154     int          last_log_id; /* used for logical disks enumerating */
0155 
0156     /* primary partition descriptors */
0157     rtems_part_desc_t *partitions[RTEMS_IDE_PARTITION_MAX_PARTITION_NUMBER];
0158 } rtems_disk_desc_t;
0159 
0160 #ifdef __cplusplus
0161 extern "C" {
0162 #endif
0163 
0164 /*
0165  * rtems_ide_part_table_free --
0166  *      frees disk descriptor structure
0167  *
0168  * PARAMETERS:
0169  *      disk_desc - disc descriptor structure to free
0170  *
0171  * RETURNS:
0172  *      N/A
0173  */
0174 /**
0175  * @deprecated Use the @ref rtems_bdpart "block device partition module" instead.
0176  */
0177 void rtems_ide_part_table_free(
0178   rtems_disk_desc_t *disk_desc
0179 ) RTEMS_DEPRECATED;
0180 
0181 
0182 /*
0183  * rtems_ide_part_table_get --
0184  *      reads partition table structure from the device
0185  *      and creates disk description structure
0186  *
0187  * PARAMETERS:
0188  *      dev_name  - path to physical device in /dev filesystem
0189  *      disk_desc - returned disc description structure
0190  *
0191  * RETURNS:
0192  *      RTEMS_SUCCESSFUL if success, or -1 and corresponding errno else
0193  */
0194 /**
0195  * @deprecated Use the @ref rtems_bdpart "block device partition module" instead.
0196  */
0197 rtems_status_code rtems_ide_part_table_get(
0198   const char *dev_name,
0199   rtems_disk_desc_t *disk_desc
0200 ) RTEMS_DEPRECATED;
0201 
0202 
0203 /*
0204  * rtems_ide_part_table_initialize --
0205  *      initializes logical devices on the physical IDE drive
0206  *
0207  * PARAMETERS:
0208  *      dev_name - path to physical device in /dev filesystem
0209  *
0210  * RETURNS:
0211  *      RTEMS_SUCCESSFUL if success, or -1 and corresponding errno else
0212  */
0213 /**
0214  * @deprecated Use the @ref rtems_bdpart "block device partition module" instead.
0215  */
0216 rtems_status_code rtems_ide_part_table_initialize(
0217   const char *dev_name
0218 ) RTEMS_DEPRECATED;
0219 
0220 #ifdef __cplusplus
0221 }
0222 #endif
0223 
0224 #endif /* _RTEMS_IDE_PART_TABLE_H */