home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / misc / src / install / libfdisk / libfdisk.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-17  |  11.9 KB  |  251 lines

  1. #ifndef LIB_FDISK_H
  2. #define LIB_FDISK_H
  3.  
  4. /* Function prototypes and data type definitions for the hard drive */
  5. /* partitioning library.                                            */
  6. /*                                                                  */
  7. /* Michael Fulbright <msf@redhat.com>  June 1997                    */
  8.  
  9.  
  10. /* some includes we'll need for our data types */
  11. #include <linux/hdreg.h>
  12. #include <limits.h>
  13.  
  14. /* stuff to handle errors */
  15. extern int fdisk_nerr;
  16. extern char *fdisk_errlist[];
  17.  
  18. /* internal defines */
  19.  
  20. #include "constraints.h"
  21.  
  22. /* some useful defines */
  23. #define SECTORSIZE 512
  24.  
  25. /* offsets within the sector containing partition table of different values */
  26. #define PARTTBLOFF 446    /* start of partition table entries */
  27. #define PARTMAGOFF 510    /* offset of magic bytes */
  28. #define PARTMAGIC  0x55aa /* magic */
  29.  
  30. /* partition types */
  31. #define UNALLOCATED_PARTITION                   0x00
  32. #define DOS_PRIMARY_lt32MEG_PARTITION           0x04
  33. #define DOS_EXTENDED_PARTITION                  0x05
  34. #define DOS_PRIMARY_gt32MEG_PARTITION           0x06
  35. #define WIN_VFAT32_PARTITION                    0x0b
  36. #define LINUX_LEGACY_PARTITION                  0x81
  37. #define LINUX_SWAP_PARTITION                    0x82
  38. #define LINUX_NATIVE_PARTITION                  0x83
  39. #define LINUX_EXTENDED_PARTITION                0x85
  40.  
  41. /* use > 255 values for non-physical partitions (remote) */
  42. #define NFS_REMOTE_PARTITION                    0x1000
  43.  
  44. /* some libfdisk errors */
  45. #define FDISK_ERR_SYSTEM      -1       /* system error, use errno       */
  46. #define FDISK_SUCCESS          0       /* Good job no problems          */
  47. #define FDISK_ERR_USERABORT    1       /* user aborted                  */
  48.  
  49. #define FDISK_ERR_BADNUM      10       /* bad numeric value passed      */
  50. #define FDISK_ERR_BADPTR      11       /* bad ptr value passed          */
  51. #define FDISK_ERR_BADMAGIC    12       /* partition table has bad magic */
  52.  
  53. #define FDISK_ERR_TWOEXT      15       /* two extended partitions!      */
  54. #define FDISK_ERR_TWOLOG      16       /* two LP per EPT!               */
  55. #define FDISK_ERR_CORRUPT     17       /* something in PT is corrupt    */
  56.  
  57. #define FDISK_ERR_NOFREE      20       /* unable to allocate a resource */
  58. #define FDISK_ERR_NOFREEPRIM  21       /* unable to allocate a primary  */
  59. #define FDISK_ERR_NOFREEEXT   22       /* unable to allocate a extended */
  60. #define FDISK_ERR_INUSE       23       /* resource currently in use     */
  61. #define FDISK_ERR_NOPEP       24       /* No Primary Extended Partition */
  62. #define FDISK_ERR_CNSTRFAIL   30       /* constraint(s) failed          */
  63.  
  64. /* Limits for each constraint type (some like 'type' doesnt make sense) */
  65. #define FDISK_START_MIN            0
  66. #define FDISK_START_MAX     UINT_MAX
  67. #define FDISK_SIZE_MIN             1
  68. #define FDISK_SIZE_MAX      UINT_MAX
  69. #define FDISK_ENDCYL_MIN           0
  70. #define FDISK_ENDCYL_MAX    UINT_MAX
  71. #define FDISK_OFFSET_MIN           0
  72. #define FDISK_OFFSET_MAX    UNIT_MAX
  73. #define FDISK_DRIVE_MIN            0
  74. #define FDISK_DRIVE_MAX           64
  75.  
  76. /* types of partitions, used by allocation routines internally */
  77. #define PRIMARY           1
  78. #define LOGICAL           2
  79. #define PRIMARY_EXTENDED  4
  80.    
  81. /* some low-level data types */
  82. /* these are used only to manipulate sectors read off the disk */
  83. /* ASAP it is converted from this form into a higher level form*/
  84. /* which is easier to manipulate                               */
  85. struct raw_PartitionEntry {
  86.     unsigned char active;
  87.     unsigned char start_head;
  88.     unsigned char start_sec;
  89.     unsigned char start_cyl;
  90.     unsigned char type;
  91.     unsigned char end_head;
  92.     unsigned char end_sec;
  93.     unsigned char end_cyl;
  94.     unsigned int  start;
  95.     unsigned int  size;
  96. };
  97.  
  98. struct raw_PartitionTable {
  99.     struct raw_PartitionEntry entry[4];
  100. };
  101.  
  102. /* make these easier to type! */
  103. typedef struct raw_PartitionTable  RawPartitionTable;
  104. typedef struct raw_PartitionEntry  RawPartition;
  105.  
  106. /* THESE ARE THE NEW HIGH LEVEL STRUCTURES */
  107. /* IN PROCESS OF RECODING                  */
  108.  
  109. /*                                                                  */
  110. /* A partition defines a portion of the disk                        */
  111. /*                                                                  */
  112. /* Partitions have various attributes:                              */
  113. /*     size & start -> define region of space used                  */
  114. /*     offset       -> defined start of data inside partition       */
  115. /*                     used for logical partititons in extended     */
  116. /*     type         -> defines which OS(s) are using partition      */
  117. /*     active       -> can we boot from the partition. used by DOS  */
  118. /*     num          -> Partition #, start at 1. Logical start at 5  */
  119. /*     drive        -> Which drive can/is the partition on          */
  120. /*     status       -> Various flags used when allocating partitions*/
  121. /*     immutable    -> If non-zero, do nothing to disturb any of the*/
  122. /*                     above values. Used to preserve existing part.*/
  123. /*                                                                  */
  124. /* Partitions DO NOT have to actually define a space on disk used by*/
  125. /* an OS! Can be used (internally by this code) to represent a region*/
  126. /* which is free for allocation, but cannot be moved. Usually this  */
  127. /* code tries to rearrange partitions to keep free space in one big */
  128. /* contiguous region. Extended/logical partitions, because of how   */
  129. /* they work, can't always be moved around. So it is possible to    */
  130. /* have a extended/logical partition pair which exists but is not   */
  131. /* allocated to a partition type other than 0. This space is prime  */
  132. /* for allocating to the next requested partition.                  */
  133. /*                                                                  */
  134.  
  135. struct fdisk_partition {
  136.     Constraint     num;        /* for /dev/hda1, num = 1 */
  137.     Constraint     start;      /* start sector, from start of drive */
  138.     Constraint     size;       /* size in sectors */
  139.     Constraint     endcyl;     /* ending cylinder, exclusive w start*/
  140.     Constraint     offset;
  141.     Constraint     type;
  142.     Constraint     active;
  143.     DriveSet       drive;
  144.  
  145.     unsigned int   status;     /* used to store info for allocating */
  146.     unsigned int   immutable;  /* if non-zero, do not touch anything*/
  147. };
  148.  
  149. typedef struct fdisk_partition                Partition;
  150.  
  151.  
  152. /* status values */
  153. /* UNAVAILABLE means work has to be done to use it (like make an extended) */
  154. /* UNUSABLE means there is no way to map this space                        */
  155. #define AVAILABLE         0
  156. #define UNAVAILABLE       1
  157. #define ALLOCATED         2
  158. #define UNUSABLE          4
  159.  
  160.  
  161. /*                                                                          */
  162. /* A hard drive describes a collection of partitions                        */
  163. /* Each hard drive has several physical characteristics which can affect    */
  164. /* the attributes that a partition can contain:                             */
  165. /*                                                                          */
  166. /*    geometry -> sectors/heads/cylinders of drive                          */
  167. /*                partitions have to start and end on cylinder boundaries   */
  168. /*                                                                          */
  169. /*    name     -> something like "/dev/hda1"                                */
  170. /*                                                                          */
  171. /*    fd       -> the file descriptor being used to access this drive       */
  172. /*                                                                          */
  173. /*    pep      -> slot number of the Primary Extended Partion, 0 if none    */
  174. /*                                                                          */
  175. /*    table    -> array of partition descriptors, indexed by the number     */
  176. /*                of the partition. Primary partitions range in number from */
  177. /*                1 to 4, while logical partitions start at number 5.       */
  178. /*                You can have any or all of the primaries defined, but the */
  179. /*                logicals always start with 5 and increment upwards. If you*/
  180. /*                remove a logical then all the other logicals get renumbred*/
  181. /*                Note that sometimes you can remove a logical but the      */
  182. /*                associated extended partition DOES NOT get removed. This  */
  183. /*                can happen when a logical partition down the chain of     */
  184. /*                extended partitions is immutable.                         */
  185. /*                                                                          */
  186. /*    eptable  -> array of extended partition descriptors, indexed by the   */
  187. /*                number of the logical partition which it encapsulates.    */
  188. /*                                                                          */
  189. /* The partition table describes existing partitions. Entries 1 thru 4 are  */
  190. /* for the primary partitions (PPs). At most 1 of these can be an           */
  191. /* extended partition (EP). The primary extended partition (PEP) describes  */
  192. /* the chunk of the disk in which ALL other logical partitions (LPs) can be.*/
  193. /* Each LP has an associated EP. At the start of each EP is an extended     */
  194. /* partition table (EPT). It is possible that an EPT could have up to 4 LPs */
  195. /* defined within a given EP, but we (and all other partitioning software I */
  196. /* can find) limit you to 1 LP per EP.  To make more LP within the space    */
  197. /* described by the PEP, you make an entry in the EPT describing another EP.*/
  198. /* These EPT make a chain, starting with the PEP and working down. The first*/
  199. /* link on this chain gets a partition number of 5, and the number goes up  */
  200. /* by one for each link down the chain.                                     */
  201. /*                                                                          */
  202. /* Deleting a PP involved just setting its size and type to 0. However,     */
  203. /* because of the way the EPT chain works, deleting anything but the tail   */
  204. /* link in the EPT chain can have numerous consequences.                    */
  205. /* Say you have LPs 5 thru 7. You delete LP 5. The EPT for LP 6 now has to  */
  206. /* move to the location of the EPT for LP 5, since LP 6 has now become LP 5.*/
  207. /* The old EPT for LP 6 is not used anymore, but LP 6 (now LP 5) will occupy*/
  208. /* at the exact same space on disk. The write routine which takes the       */
  209. /* HardDrive structure and writes it to the disk must be aware of these     */
  210. /* facts so that EPT and LP stay in the same places as read in.             */
  211. /* This routine will write LP 5 first, then 6, etc etc, linking each with   */
  212. /* the following. At some point it would be nice if the automatic allocation*/
  213. /* routines could straighten the EPT chain out.                             */
  214. /*                                                                          */
  215. /* We mark a partition table entry as available by:                         */
  216. /*     setting its size and type to 0                                       */
  217. /*     settting its status to AVAILABLE <- use this for testing status      */
  218. /* We mark a partition used by:                                             */
  219. /*     setting its size and type to != 0                                    */
  220. /*     setting its status to ALLOCATED  <- use this for testing status      */
  221.  
  222.  
  223. #define MAX_HD_NAMELEN 128
  224. #define MAX_PARTITIONS   16
  225. struct hd_device {
  226.     char                            name[MAX_HD_NAMELEN];
  227.     unsigned int                    num; /* 'BIOS' number of drive */
  228.     struct hd_geometry              geom;
  229.     unsigned int                    totalsectors;
  230.     int                             fd;
  231.     unsigned int                    pep;
  232.     Partition                       table[MAX_PARTITIONS+1];
  233.     Partition                       eptable[MAX_PARTITIONS+1];
  234. };
  235.  
  236. typedef struct hd_device           HardDrive;
  237. typedef struct hd_geometry         HDGeometry;
  238.  
  239.  
  240.  
  241.  
  242. /* functions everyone should see */
  243. #include "rawio.h"
  244. #include "primary.h"
  245. #include "extended.h"
  246. #include "partition.h"
  247. #include "partspec.h"
  248. #include "alloc.h"
  249.  
  250. #endif
  251.