| srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 1 | /* bsd.h -- BSD disklabel data structure definitions, types, and functions */ | 
 | 2 |  | 
 | 3 | /* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed | 
 | 4 |   under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ | 
 | 5 |  | 
 | 6 | #include <stdint.h> | 
 | 7 | #include <sys/types.h> | 
 | 8 | #include <sys/ioctl.h> | 
 | 9 | #include "gptpart.h" | 
 | 10 |  | 
 | 11 | #ifndef __BSD_STRUCTS | 
 | 12 | #define __BSD_STRUCTS | 
 | 13 |  | 
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 14 | #define BSD_SIGNATURE UINT32_C(0x82564557)  /* BSD disklabel signature ("magic") */ | 
| srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 15 |  | 
| srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 16 | #define LABEL_OFFSET1 64    /* BSD disklabels can start at any of these three */ | 
 | 17 | #define LABEL_OFFSET2 512   /* values; check all for valid signatures */ | 
 | 18 | #define LABEL_OFFSET3 2048 | 
| srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 19 |  | 
 | 20 | // FreeBSD documents a maximum # of partitions of 8, but I saw 16 on a NetBSD | 
 | 21 | // disk. I'm quadrupling that for further safety. Note that BSDReadData() | 
| srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 22 | // uses a 4096-byte I/O buffer. In combination with LABEL_OFFSET3 and the | 
| srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 23 | // additional 148-byte offset to the actual partition data, that gives a | 
| srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 24 | // theoretical maximum of 118.75 partitions that the program can handle before | 
 | 25 | // memory errors will occur. | 
| srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 26 | #define MAX_BSD_PARTS 64 | 
 | 27 |  | 
 | 28 |  | 
 | 29 | using namespace std; | 
 | 30 |  | 
 | 31 | /**************************************** | 
 | 32 |  *                                      * | 
 | 33 |  * BSDData class and related structures * | 
 | 34 |  *                                      * | 
 | 35 |  ****************************************/ | 
 | 36 |  | 
 | 37 | // Possible states of the MBR | 
 | 38 | enum BSDValidity {unknown, bsd_invalid, bsd}; | 
 | 39 |  | 
 | 40 | // Data for a single BSD partition record | 
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 41 | // Create entries for all fields, although we only use lengthLBA, firstLBA, | 
 | 42 | // and fsType, to simplify loading the data from disk.... | 
 | 43 | struct  BSDRecord {      // the partition table | 
 | 44 |    uint32_t lengthLBA;   // number of sectors in partition | 
 | 45 |    uint32_t firstLBA;    // starting sector | 
 | 46 |    uint32_t fragSize;    // filesystem basic fragment size | 
 | 47 |    uint8_t  fsType;      // filesystem type, see below | 
 | 48 |    uint8_t  frag;        // filesystem fragments per block | 
 | 49 |    uint16_t pcpg;        // filesystem cylinders per group | 
| srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 50 | }; | 
 | 51 |  | 
| srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 52 | // Full data in tweaked BSD format | 
| srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 53 | class BSDData { | 
 | 54 |    protected: | 
 | 55 |       // We only need a few items from the main BSD disklabel data structure.... | 
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 56 |       uint32_t signature;        // the magic number | 
 | 57 |       uint32_t sectorSize;       // # of bytes per sector | 
 | 58 |       uint32_t signature2;       // the magic number (again) | 
 | 59 |       uint16_t numParts;         // number of partitions in table | 
 | 60 |       BSDRecord* partitions;     // partition array | 
| srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 61 |  | 
 | 62 |       // Above are basic BSD disklabel data; now add more stuff.... | 
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 63 |       uint64_t labelFirstLBA;    // first sector of BSD disklabel (partition or disk) | 
 | 64 |       uint64_t labelLastLBA;     // final sector of BSD disklabel | 
 | 65 |       uint64_t labelStart;       // BSD disklabel start point in bytes from labelFirstLBA | 
| srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 66 |       BSDValidity state; | 
| srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 67 |    public: | 
 | 68 |       BSDData(void); | 
 | 69 |       ~BSDData(void); | 
 | 70 |       int ReadBSDData(char* deviceFilename, uint64_t startSector, uint64_t endSector); | 
 | 71 |       void ReadBSDData(int fd, uint64_t startSector, uint64_t endSector); | 
 | 72 |       void ReverseMetaBytes(void); | 
 | 73 |       void DisplayBSDData(void); | 
| srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 74 |       int ShowState(void); // returns 1 if BSD disklabel detected | 
| srs5694 | a6022b0 | 2010-01-19 16:17:20 -0500 | [diff] [blame^] | 75 |       int IsDisklabel(void); | 
| srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 76 |  | 
 | 77 |       // Functions to extract data on specific partitions.... | 
 | 78 |       uint8_t GetType(int i); | 
 | 79 |       uint64_t GetFirstSector(int i); | 
 | 80 |       uint64_t GetLength(int i); | 
 | 81 |       int GetNumParts(void); | 
 | 82 |       GPTPart AsGPT(int i); // Return BSD part. as GPT part. | 
 | 83 | }; // struct MBRData | 
 | 84 |  | 
 | 85 | #endif |