mbr.h revision 978041ca613dcb881763b36cf53639d924e52a56
1/* mbr.h -- MBR 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 __MBRSTRUCTS
12#define __MBRSTRUCTS
13
14#define MBR_SIGNATURE UINT16_C(0xAA55)
15#define MAX_HEADS 255        /* numbered 0 - 254 */
16#define MAX_SECSPERTRACK 63  /* numbered 1 - 63 */
17#define MAX_CYLINDERS 1024   /* numbered 0 - 1023 */
18
19// Maximum number of MBR partitions
20#define MAX_MBR_PARTS 128
21
22using namespace std;
23
24/****************************************
25 *                                      *
26 * MBRData class and related structures *
27 *                                      *
28 ****************************************/
29
30// Data for a single MBR partition record
31// Note that firstSector and lastSector are in CHS addressing, which
32// splits the bits up in a weird way.
33struct MBRRecord {
34   uint8_t status;
35   uint8_t firstSector[3];
36   uint8_t partitionType;
37   uint8_t lastSector[3];
38   uint32_t firstLBA;
39   uint32_t lengthLBA;
40}; // struct MBRRecord
41
42// A 512-byte data structure into which the MBR can be loaded in one
43// go, for the benefit of FreeBSD which seems to flake out when loading
44// from block devices in multiples other than the block size.
45// Also used when loading logical partitions.
46struct TempMBR {
47   uint8_t code[440];
48   uint32_t diskSignature;
49   uint16_t nulls;
50   struct MBRRecord partitions[4];
51   uint16_t MBRSignature;
52}; // struct TempMBR
53
54// Possible states of the MBR
55enum MBRValidity {invalid, gpt, hybrid, mbr};
56
57// Full data in tweaked MBR format
58class MBRData {
59protected:
60   uint8_t code[440];
61   uint32_t diskSignature;
62   uint16_t nulls;
63   // MAX_MBR_PARTS defaults to 128. This array holds both the primary and
64   // the logical partitions, to simplify data retrieval for GPT conversions.
65   struct MBRRecord partitions[MAX_MBR_PARTS];
66   uint16_t MBRSignature;
67
68   // Above are basic MBR data; now add more stuff....
69   uint32_t blockSize; // block size (usually 512)
70   uint64_t diskSize; // size in blocks
71   uint64_t numHeads; // number of heads, in CHS scheme
72   uint64_t numSecspTrack; // number of sectors per track, in CHS scheme
73   char device[256];
74   MBRValidity state;
75   struct MBRRecord* GetPartition(int i); // Return primary or logical partition
76public:
77   MBRData(void);
78   MBRData(char* deviceFilename);
79   ~MBRData(void);
80
81   // File I/O functions...
82   int ReadMBRData(char* deviceFilename);
83   void ReadMBRData(int fd, int checkBlockSize = 1);
84   int ReadLogicalPart(int fd, uint32_t extendedStart, uint32_t diskOffset,
85                       int partNum);
86   int WriteMBRData(void);
87   void WriteMBRData(int fd);
88   // ReadLogicalPart() returns last partition # read to logicals[] array,
89   // or -1 if there was a problem....
90
91   // Display data for user...
92   void DisplayMBRData(void);
93   void ShowState(void);
94
95   // Functions that set or get disk metadata (size, CHS geometry, etc.)
96   void SetDiskSize(uint64_t ds) {diskSize = ds;}
97   MBRValidity GetValidity(void) {return state;}
98   void SetHybrid(void) {state = hybrid;} // Set hybrid flag
99   void SetCHSGeom(uint32_t h, uint32_t s);
100   int LBAtoCHS(uint64_t lba, uint8_t * chs); // Convert LBA to CHS
101
102   // Functions to create, delete, or change partitions
103   // Pass EmptyMBR 1 to clear the boot loader code, 0 to leave it intact
104   void EmptyMBR(int clearBootloader = 1);
105   void MakeProtectiveMBR(int clearBoot = 0);
106   void MakePart(int num, uint32_t startLBA, uint32_t lengthLBA, int type = 0x07,
107                 int bootable = 0);
108   int MakeBiggestPart(int i, int type); // Make partition filling most space
109   void DeletePartition(int i);
110   int DeleteByLocation(uint64_t start64, uint64_t length64);
111   void OptimizeEESize(void);
112
113   // Functions to find information on free space....
114   uint32_t FindFirstAvailable(uint32_t start = 1);
115   uint32_t FindLastInFree(uint32_t start);
116   uint32_t FindFirstInFree(uint32_t start);
117   int IsFree(uint32_t sector);
118
119   // Functions to extract data on specific partitions....
120   uint8_t GetStatus(int i);
121   uint8_t GetType(int i);
122   uint32_t GetFirstSector(int i);
123   uint32_t GetLength(int i);
124   GPTPart AsGPT(int i);
125}; // struct MBRData
126
127#endif
128