blob: b8be6e4c5510afcbfb08d7384f7f39dea6f54066 [file] [log] [blame]
srs5694f2efa7d2011-03-01 22:03:26 -05001/* basicmbr.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>
srs5694f2efa7d2011-03-01 22:03:26 -05008#include "diskio.h"
srs5694bf8950c2011-03-12 01:23:12 -05009#include "mbrpart.h"
srs5694f2efa7d2011-03-01 22:03:26 -050010
11#ifndef __BASICMBRSTRUCTS
12#define __BASICMBRSTRUCTS
13
14#define MBR_SIGNATURE UINT16_C(0xAA55)
srs5694f2efa7d2011-03-01 22:03:26 -050015
16// Maximum number of MBR partitions
17#define MAX_MBR_PARTS 128
18
19using namespace std;
20
21class PartNotes;
22
23/****************************************
24 * *
25 * MBRData class and related structures *
26 * *
27 ****************************************/
28
srs5694f2efa7d2011-03-01 22:03:26 -050029// A 512-byte data structure into which the MBR can be loaded in one
30// go. Also used when loading logical partitions.
31#pragma pack(1)
32struct TempMBR {
33 uint8_t code[440];
34 uint32_t diskSignature;
35 uint16_t nulls;
36 struct MBRRecord partitions[4];
37 uint16_t MBRSignature;
38}; // struct TempMBR
39
40// Possible states of the MBR
41enum MBRValidity {invalid, gpt, hybrid, mbr};
42
43// Full data in tweaked MBR format
44class BasicMBRData {
45protected:
46 uint8_t code[440];
47 uint32_t diskSignature;
48 uint16_t nulls;
49 // MAX_MBR_PARTS defaults to 128. This array holds both the primary and
50 // the logical partitions, to simplify data retrieval for GPT conversions.
srs5694bf8950c2011-03-12 01:23:12 -050051 MBRPart partitions[MAX_MBR_PARTS];
srs5694f2efa7d2011-03-01 22:03:26 -050052 uint16_t MBRSignature;
53
54 // Above are basic MBR data; now add more stuff....
55 uint32_t blockSize; // block size (usually 512)
56 uint64_t diskSize; // size in blocks
srs5694bf8950c2011-03-12 01:23:12 -050057 uint32_t numHeads; // number of heads, in CHS scheme
58 uint32_t numSecspTrack; // number of sectors per track, in CHS scheme
srs5694f2efa7d2011-03-01 22:03:26 -050059 DiskIO* myDisk;
60 int canDeleteMyDisk;
61 string device;
62 MBRValidity state;
srs5694bf8950c2011-03-12 01:23:12 -050063 MBRPart* GetPartition(int i); // Return primary or logical partition
srs5694f2efa7d2011-03-01 22:03:26 -050064public:
65 BasicMBRData(void);
66 BasicMBRData(string deviceFilename);
67 ~BasicMBRData(void);
68 BasicMBRData & operator=(const BasicMBRData & orig);
69
70 // File I/O functions...
71 int ReadMBRData(const string & deviceFilename);
72 int ReadMBRData(DiskIO * theDisk, int checkBlockSize = 1);
73 // ReadLogicalPart() returns last partition # read to logicals[] array,
74 // or -1 if there was a problem....
srs5694bf8950c2011-03-12 01:23:12 -050075 int ReadLogicalPart(uint64_t extendedStart, uint64_t diskOffset,
srs5694f2efa7d2011-03-01 22:03:26 -050076 int partNum);
77 int WriteMBRData(void);
78 int WriteMBRData(DiskIO *theDisk);
79 int WriteMBRData(const string & deviceFilename);
80 int WriteMBRData(struct TempMBR & mbr, DiskIO *theDisk, uint64_t sector);
srs5694bf8950c2011-03-12 01:23:12 -050081 void DiskSync(void) {myDisk->DiskSync();}
82 void SetDisk(DiskIO *theDisk);
srs5694f2efa7d2011-03-01 22:03:26 -050083
84 // Display data for user...
srs5694bf8950c2011-03-12 01:23:12 -050085 void DisplayMBRData(void);
srs5694f2efa7d2011-03-01 22:03:26 -050086 void ShowState(void);
87
srs5694bf8950c2011-03-12 01:23:12 -050088 // GPT checks and fixes...
89 int CheckForGPT(void);
90 int BlankGPTData(void);
91
srs5694f2efa7d2011-03-01 22:03:26 -050092 // Functions that set or get disk metadata (size, CHS geometry, etc.)
93 void SetDiskSize(uint64_t ds) {diskSize = ds;}
94 void SetBlockSize(uint32_t bs) {blockSize = bs;}
95 MBRValidity GetValidity(void) {return state;}
96 void SetHybrid(void) {state = hybrid;} // Set hybrid flag
srs5694bf8950c2011-03-12 01:23:12 -050097 void ReadCHSGeom(void);
98 int GetPartRange(uint32_t* low, uint32_t* high);
srs5694f2efa7d2011-03-01 22:03:26 -050099 int LBAtoCHS(uint64_t lba, uint8_t * chs); // Convert LBA to CHS
srs5694bf8950c2011-03-12 01:23:12 -0500100 int FindOverlaps(void);
101 int NumPrimaries(void);
102 int NumLogicals(void);
103 int CountParts(void);
104 void UpdateCanBeLogical(void);
105 uint64_t FirstLogicalLBA(void);
106 uint64_t LastLogicalLBA(void);
107 int AreLogicalsContiguous(void);
108 int DoTheyFit(void);
109 int SpaceBeforeAllLogicals(void);
110 int IsLegal(void);
111 int FindNextInUse(int start);
srs5694f2efa7d2011-03-01 22:03:26 -0500112
113 // Functions to create, delete, or change partitions
114 // Pass EmptyMBR 1 to clear the boot loader code, 0 to leave it intact
115 void EmptyMBR(int clearBootloader = 1);
116 void EmptyBootloader(void);
srs5694bf8950c2011-03-12 01:23:12 -0500117 void AddPart(int num, const MBRPart& newPart);
118 void MakePart(int num, uint64_t startLBA, uint64_t lengthLBA, int type = 0x07,
srs5694f2efa7d2011-03-01 22:03:26 -0500119 int bootable = 0);
120 int SetPartType(int num, int type);
121 int SetPartBootable(int num, int bootable = 1);
122 int MakeBiggestPart(int i, int type); // Make partition filling most space
123 void DeletePartition(int i);
srs5694bf8950c2011-03-12 01:23:12 -0500124 int SetInclusionwChecks(int num, int inclStatus);
srs5694f2efa7d2011-03-01 22:03:26 -0500125 void RecomputeCHS(int partNum);
srs5694bf8950c2011-03-12 01:23:12 -0500126 int SwapPartitions(uint32_t partNum1, uint32_t partNum2);
127 void SortMBR(int start = 0);
128 void QuickSortMBR(int start, int finish);
129 int DeleteOversizedParts();
130 int DeleteExtendedParts();
131 void OmitOverlaps(void);
132// void OmitAll(void);
133 void MaximizeLogicals();
134 void MaximizePrimaries();
135 void TrimPrimaries();
136 void MakeLogicalsContiguous(void);
137 void MakeItLegal(void);
138 int RemoveLogicalsFromFirstFour(void);
139 int MovePrimariesToFirstFour(void);
140 int CreateExtended(void);
srs5694f2efa7d2011-03-01 22:03:26 -0500141
142 // Functions to find information on free space....
srs5694bf8950c2011-03-12 01:23:12 -0500143 uint64_t FindFirstAvailable(uint64_t start = 1);
144 uint64_t FindLastInFree(uint64_t start);
145 uint64_t FindFirstInFree(uint64_t start);
146// int IsFree(uint64_t sector, int topPartNum = MAX_MBR_PARTS);
147 int SectorUsedAs(uint64_t sector, int topPartNum = MAX_MBR_PARTS);
srs5694f2efa7d2011-03-01 22:03:26 -0500148
149 // Functions to extract data on specific partitions....
150 uint8_t GetStatus(int i);
151 uint8_t GetType(int i);
srs5694bf8950c2011-03-12 01:23:12 -0500152 uint64_t GetFirstSector(int i);
153 uint64_t GetLength(int i);
154
155 // User interaction functions....
156 int DoMenu(const string& prompt = "\nMBR command (? for help): ");
157 void ShowCommands(void);
158
srs5694f2efa7d2011-03-01 22:03:26 -0500159}; // struct BasicMBRData
160
161#endif