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 | |
| 14 | #define BSD_SIGNATURE UINT32_C(0x82564557) |
| 15 | |
| 16 | #define LABEL_OFFSET1 64 /* BSD disklabels can start at one of these two */ |
| 17 | #define LABEL_OFFSET2 512 /* values; check both for valid signatures */ |
| 18 | |
| 19 | // FreeBSD documents a maximum # of partitions of 8, but I saw 16 on a NetBSD |
| 20 | // disk. I'm quadrupling that for further safety. Note that BSDReadData() |
| 21 | // uses a 2048-byte I/O buffer. In combination with LABEL_OFFSET2 and the |
| 22 | // additional 148-byte offset to the actual partition data, that gives a |
| 23 | // theoretical maximum of 86.75 partitions that the program can handle. |
| 24 | #define MAX_BSD_PARTS 64 |
| 25 | |
| 26 | |
| 27 | using namespace std; |
| 28 | |
| 29 | /**************************************** |
| 30 | * * |
| 31 | * BSDData class and related structures * |
| 32 | * * |
| 33 | ****************************************/ |
| 34 | |
| 35 | // Possible states of the MBR |
| 36 | enum BSDValidity {unknown, bsd_invalid, bsd}; |
| 37 | |
| 38 | // Data for a single BSD partition record |
| 39 | struct BSDRecord { // the partition table |
| 40 | uint32_t lengthLBA; // number of sectors in partition |
| 41 | uint32_t firstLBA; // starting sector |
| 42 | uint32_t fragSize; // filesystem basic fragment size |
| 43 | uint8_t fsType; // filesystem type, see below |
| 44 | uint8_t frag; // filesystem fragments per block |
| 45 | uint16_t pcpg; /* filesystem cylinders per group */ // was u_uint16_t |
| 46 | }; |
| 47 | |
| 48 | // Full data in tweaked MBR format |
| 49 | class BSDData { |
| 50 | protected: |
| 51 | // We only need a few items from the main BSD disklabel data structure.... |
| 52 | uint32_t signature; // the magic number |
| 53 | uint32_t sectorSize; // # of bytes per sector |
| 54 | uint32_t signature2; // the magic number (again) |
| 55 | uint16_t numParts; // number of partitions in table |
| 56 | BSDRecord* partitions; // partition array |
| 57 | |
| 58 | // Above are basic BSD disklabel data; now add more stuff.... |
| 59 | // uint64_t offset; // starting point in blocks |
| 60 | uint64_t labelStart; // BSD disklabel start point in bytes from firstLBA |
| 61 | uint64_t labelFirstLBA; // first sector of BSD disklabel (partition or disk) |
| 62 | uint64_t labelLastLBA; // final sector of BSD disklabel |
| 63 | // char deviceFilename[256]; |
| 64 | BSDValidity state; |
| 65 | // struct BSDRecord* GetPartition(int i); // Return BSD partition |
| 66 | public: |
| 67 | BSDData(void); |
| 68 | ~BSDData(void); |
| 69 | int ReadBSDData(char* deviceFilename, uint64_t startSector, uint64_t endSector); |
| 70 | void ReadBSDData(int fd, uint64_t startSector, uint64_t endSector); |
| 71 | void ReverseMetaBytes(void); |
| 72 | void DisplayBSDData(void); |
| 73 | // int ConvertBSDParts(struct GPTPartition gptParts[]); |
| 74 | int ShowState(void); // returns 1 if BSD disklabel detected |
| 75 | int IsDisklabel(void) {return (state == bsd);} |
| 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 |