1*57696d54SAkhilesh Sanikop /* bsd.h -- BSD disklabel data structure definitions, types, and functions */ 2*57696d54SAkhilesh Sanikop 3*57696d54SAkhilesh Sanikop /* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed 4*57696d54SAkhilesh Sanikop under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ 5*57696d54SAkhilesh Sanikop 6*57696d54SAkhilesh Sanikop #ifndef __BSD_STRUCTS 7*57696d54SAkhilesh Sanikop #define __BSD_STRUCTS 8*57696d54SAkhilesh Sanikop 9*57696d54SAkhilesh Sanikop #include <stdint.h> 10*57696d54SAkhilesh Sanikop #include <sys/types.h> 11*57696d54SAkhilesh Sanikop #include "gptpart.h" 12*57696d54SAkhilesh Sanikop #include "diskio.h" 13*57696d54SAkhilesh Sanikop 14*57696d54SAkhilesh Sanikop #define BSD_SIGNATURE UINT32_C(0x82564557) /* BSD disklabel signature ("magic") */ 15*57696d54SAkhilesh Sanikop 16*57696d54SAkhilesh Sanikop // BSD disklabels can start at offsets of 64 or the sector size -- at least, 17*57696d54SAkhilesh Sanikop // I *THINK* that's what's going on. I've seen them at 64 or 512 on disks 18*57696d54SAkhilesh Sanikop // with 512-byte blocks and at 2048 on disks with 2048-byte blocks. The 19*57696d54SAkhilesh Sanikop // LABEL_OFFSET2 value will be replaced by the block size in the 20*57696d54SAkhilesh Sanikop // ReadBSDData() function.... 21*57696d54SAkhilesh Sanikop #define LABEL_OFFSET1 64 22*57696d54SAkhilesh Sanikop #define LABEL_OFFSET2 512 23*57696d54SAkhilesh Sanikop #define NUM_OFFSETS 2 24*57696d54SAkhilesh Sanikop 25*57696d54SAkhilesh Sanikop // FreeBSD documents a maximum # of partitions of 8, but I saw 16 on a NetBSD 26*57696d54SAkhilesh Sanikop // disk. I'm quadrupling that for further safety. Note that BSDReadData() 27*57696d54SAkhilesh Sanikop // uses a 4096-byte I/O buffer. In combination with LABEL_OFFSET3 and the 28*57696d54SAkhilesh Sanikop // additional 148-byte offset to the actual partition data, that gives a 29*57696d54SAkhilesh Sanikop // theoretical maximum of 118.75 partitions that the program can handle before 30*57696d54SAkhilesh Sanikop // memory errors will occur. 31*57696d54SAkhilesh Sanikop #define MAX_BSD_PARTS 64 32*57696d54SAkhilesh Sanikop 33*57696d54SAkhilesh Sanikop /**************************************** 34*57696d54SAkhilesh Sanikop * * 35*57696d54SAkhilesh Sanikop * BSDData class and related structures * 36*57696d54SAkhilesh Sanikop * * 37*57696d54SAkhilesh Sanikop ****************************************/ 38*57696d54SAkhilesh Sanikop 39*57696d54SAkhilesh Sanikop // Possible states of the MBR 40*57696d54SAkhilesh Sanikop enum BSDValidity {unknown, bsd_invalid, bsd}; 41*57696d54SAkhilesh Sanikop 42*57696d54SAkhilesh Sanikop // Data for a single BSD partition record 43*57696d54SAkhilesh Sanikop // Create entries for all fields, although we only use lengthLBA, firstLBA, 44*57696d54SAkhilesh Sanikop // and fsType, to simplify loading the data from disk.... 45*57696d54SAkhilesh Sanikop struct BSDRecord { // the partition table 46*57696d54SAkhilesh Sanikop uint32_t lengthLBA; // number of sectors in partition 47*57696d54SAkhilesh Sanikop uint32_t firstLBA; // starting sector 48*57696d54SAkhilesh Sanikop uint32_t fragSize; // filesystem basic fragment size 49*57696d54SAkhilesh Sanikop uint8_t fsType; // filesystem type, see below 50*57696d54SAkhilesh Sanikop uint8_t frag; // filesystem fragments per block 51*57696d54SAkhilesh Sanikop uint16_t pcpg; // filesystem cylinders per group 52*57696d54SAkhilesh Sanikop }; 53*57696d54SAkhilesh Sanikop 54*57696d54SAkhilesh Sanikop // Full data in tweaked BSD format 55*57696d54SAkhilesh Sanikop // For some reason this has to be packed or MS Visual C++'s debugger complains 56*57696d54SAkhilesh Sanikop // about memory errors whenever a BSDData variable is destroyed. 57*57696d54SAkhilesh Sanikop #pragma pack (8) 58*57696d54SAkhilesh Sanikop class BSDData { 59*57696d54SAkhilesh Sanikop protected: 60*57696d54SAkhilesh Sanikop // We only need a few items from the main BSD disklabel data structure.... 61*57696d54SAkhilesh Sanikop uint32_t signature; // the magic number 62*57696d54SAkhilesh Sanikop uint32_t sectorSize; // # of bytes per sector 63*57696d54SAkhilesh Sanikop uint32_t signature2; // the magic number (again) 64*57696d54SAkhilesh Sanikop uint16_t numParts; // number of partitions in table 65*57696d54SAkhilesh Sanikop struct BSDRecord* partitions; // partition array 66*57696d54SAkhilesh Sanikop 67*57696d54SAkhilesh Sanikop // Above are basic BSD disklabel data; now add more stuff.... 68*57696d54SAkhilesh Sanikop uint64_t labelFirstLBA; // first sector of BSD disklabel (partition or disk) 69*57696d54SAkhilesh Sanikop uint64_t labelLastLBA; // final sector of BSD disklabel 70*57696d54SAkhilesh Sanikop uint64_t labelStart; // BSD disklabel start point in bytes from labelFirstLBA 71*57696d54SAkhilesh Sanikop BSDValidity state; 72*57696d54SAkhilesh Sanikop public: 73*57696d54SAkhilesh Sanikop BSDData(void); 74*57696d54SAkhilesh Sanikop ~BSDData(void); 75*57696d54SAkhilesh Sanikop int ReadBSDData(const std::string & deviceFilename, uint64_t startSector, uint64_t endSector); 76*57696d54SAkhilesh Sanikop int ReadBSDData(DiskIO *myDisk, uint64_t startSector, uint64_t endSector); 77*57696d54SAkhilesh Sanikop void ReverseMetaBytes(void); 78*57696d54SAkhilesh Sanikop void DisplayBSDData(void); 79*57696d54SAkhilesh Sanikop int ShowState(void); // returns 1 if BSD disklabel detected 80*57696d54SAkhilesh Sanikop int IsDisklabel(void); 81*57696d54SAkhilesh Sanikop 82*57696d54SAkhilesh Sanikop // Functions to extract data on specific partitions.... 83*57696d54SAkhilesh Sanikop uint8_t GetType(int i); 84*57696d54SAkhilesh Sanikop uint64_t GetFirstSector(int i); 85*57696d54SAkhilesh Sanikop uint64_t GetLength(int i); 86*57696d54SAkhilesh Sanikop int GetNumParts(void); 87*57696d54SAkhilesh Sanikop GPTPart AsGPT(int i); // Return BSD part. as GPT part. 88*57696d54SAkhilesh Sanikop }; // struct MBRData 89*57696d54SAkhilesh Sanikop #pragma pack () 90*57696d54SAkhilesh Sanikop 91*57696d54SAkhilesh Sanikop #endif 92