blob: 08c61be6421492851c2418e70d5f85f29766a7b2 [file] [log] [blame]
srs5694e7b4ff92009-08-18 13:16:10 -04001/* mbr.cc -- Functions for loading, saving, and manipulating legacy MBR partition
2 data. */
3
srs5694978041c2009-09-21 20:51:47 -04004/* Initial coding by Rod Smith, January to February, 2009 */
srs5694e7b4ff92009-08-18 13:16:10 -04005
Roderick W. Smithe3ee7332013-09-24 12:56:11 -04006/* This program is copyright (c) 2009-2013 by Roderick W. Smith. It is distributed
srs5694221e0872009-08-29 15:00:31 -04007 under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
8
srs5694e7b4ff92009-08-18 13:16:10 -04009#define __STDC_LIMIT_MACROS
10#define __STDC_CONSTANT_MACROS
11
12#include <stdio.h>
srs5694e7b4ff92009-08-18 13:16:10 -040013#include <stdlib.h>
14#include <stdint.h>
15#include <fcntl.h>
16#include <string.h>
17#include <time.h>
18#include <sys/stat.h>
19#include <errno.h>
srs5694fed16d02010-01-27 23:03:40 -050020#include <iostream>
srs5694e7b4ff92009-08-18 13:16:10 -040021#include "mbr.h"
srs5694e7b4ff92009-08-18 13:16:10 -040022
23using namespace std;
24
25/****************************************
26 * *
27 * MBRData class and related structures *
28 * *
29 ****************************************/
30
Aurimas Liutikas74b74902016-05-10 18:53:54 -070031MBRData::~MBRData(void) {
32} // MBRData destructor
33
srs5694bf8950c2011-03-12 01:23:12 -050034/* // Assignment operator -- copy entire set of MBR data.
srs569464cbd172011-03-01 22:03:54 -050035MBRData & MBRData::operator=(const MBRData & orig) {
36 BasicMBRData::operator=(orig);
37 return *this;
38} // MBRData::operator=() */
srs5694327129e2010-09-22 01:07:31 -040039
srs5694bf8950c2011-03-12 01:23:12 -050040// Assignment operator -- copy entire set of MBR data.
41MBRData & MBRData::operator=(const BasicMBRData & orig) {
42 BasicMBRData::operator=(orig);
43 return *this;
44} // MBRData::operator=()
45
srs5694978041c2009-09-21 20:51:47 -040046/*****************************************************
47 * *
48 * Functions to create, delete, or change partitions *
49 * *
50 *****************************************************/
51
srs5694221e0872009-08-29 15:00:31 -040052// Create a protective MBR. Clears the boot loader area if clearBoot > 0.
53void MBRData::MakeProtectiveMBR(int clearBoot) {
srs5694978041c2009-09-21 20:51:47 -040054
55 EmptyMBR(clearBoot);
srs5694e7b4ff92009-08-18 13:16:10 -040056
57 // Initialize variables
58 nulls = 0;
59 MBRSignature = MBR_SIGNATURE;
srs56948a4ddfc2010-03-21 19:05:49 -040060 diskSignature = UINT32_C(0);
srs5694e7b4ff92009-08-18 13:16:10 -040061
srs5694bf8950c2011-03-12 01:23:12 -050062 partitions[0].SetStatus(0); // Flag the protective part. as unbootable
srs5694e7b4ff92009-08-18 13:16:10 -040063
srs5694bf8950c2011-03-12 01:23:12 -050064 partitions[0].SetType(UINT8_C(0xEE));
srs5694e7b4ff92009-08-18 13:16:10 -040065 if (diskSize < UINT32_MAX) { // If the disk is under 2TiB
srs5694bf8950c2011-03-12 01:23:12 -050066 partitions[0].SetLocation(UINT32_C(1), (uint32_t) diskSize - UINT32_C(1));
srs5694e7b4ff92009-08-18 13:16:10 -040067 } else { // disk is too big to represent, so fake it...
srs5694bf8950c2011-03-12 01:23:12 -050068 partitions[0].SetLocation(UINT32_C(1), UINT32_MAX);
srs5694e7b4ff92009-08-18 13:16:10 -040069 } // if/else
srs5694bf8950c2011-03-12 01:23:12 -050070 partitions[0].SetInclusion(PRIMARY);
srs5694e7b4ff92009-08-18 13:16:10 -040071
srs5694e7b4ff92009-08-18 13:16:10 -040072 state = gpt;
73} // MBRData::MakeProtectiveMBR()
74
srs5694e4ac11e2009-08-31 10:13:04 -040075// Optimizes the size of the 0xEE (EFI GPT) partition
76void MBRData::OptimizeEESize(void) {
77 int i, typeFlag = 0;
srs5694bf8950c2011-03-12 01:23:12 -050078 uint64_t after;
srs5694e4ac11e2009-08-31 10:13:04 -040079
80 for (i = 0; i < 4; i++) {
81 // Check for non-empty and non-0xEE partitions
srs5694bf8950c2011-03-12 01:23:12 -050082 if ((partitions[i].GetType() != 0xEE) && (partitions[i].GetType() != 0x00))
srs5694e4ac11e2009-08-31 10:13:04 -040083 typeFlag++;
srs5694bf8950c2011-03-12 01:23:12 -050084 if (partitions[i].GetType() == 0xEE) {
srs5694e4ac11e2009-08-31 10:13:04 -040085 // Blank space before this partition; fill it....
srs5694bf8950c2011-03-12 01:23:12 -050086 if (SectorUsedAs(partitions[i].GetStartLBA() - 1, 4) == NONE) {
87 partitions[i].SetStartLBA(FindFirstInFree(partitions[i].GetStartLBA() - 1));
srs5694e4ac11e2009-08-31 10:13:04 -040088 } // if
89 // Blank space after this partition; fill it....
srs5694bf8950c2011-03-12 01:23:12 -050090 after = partitions[i].GetStartLBA() + partitions[i].GetLengthLBA();
91 if (SectorUsedAs(after, 4) == NONE) {
92 partitions[i].SetLengthLBA(FindLastInFree(after) - partitions[i].GetStartLBA() + 1);
srs5694e4ac11e2009-08-31 10:13:04 -040093 } // if free space after
srs569464cbd172011-03-01 22:03:54 -050094 if (after > diskSize) {
95 if (diskSize < UINT32_MAX) { // If the disk is under 2TiB
srs5694bf8950c2011-03-12 01:23:12 -050096 partitions[i].SetLengthLBA((uint32_t) diskSize - partitions[i].GetStartLBA());
srs569464cbd172011-03-01 22:03:54 -050097 } else { // disk is too big to represent, so fake it...
srs5694bf8950c2011-03-12 01:23:12 -050098 partitions[i].SetLengthLBA(UINT32_MAX - partitions[i].GetStartLBA());
srs569464cbd172011-03-01 22:03:54 -050099 } // if/else
100 } // if protective partition is too big
101 RecomputeCHS(i);
srs5694e4ac11e2009-08-31 10:13:04 -0400102 } // if partition is 0xEE
srs5694e4ac11e2009-08-31 10:13:04 -0400103 } // for partition loop
srs5694978041c2009-09-21 20:51:47 -0400104 if (typeFlag == 0) { // No non-hybrid partitions found
srs569464cbd172011-03-01 22:03:54 -0500105 MakeProtectiveMBR(); // ensure it's a fully compliant protective MBR.
srs5694978041c2009-09-21 20:51:47 -0400106 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400107} // MBRData::OptimizeEESize()
108
srs569464cbd172011-03-01 22:03:54 -0500109// Delete a partition if one exists at the specified location.
110// Returns 1 if a partition was deleted, 0 otherwise....
111// Used to help keep GPT & hybrid MBR partitions in sync....
112int MBRData::DeleteByLocation(uint64_t start64, uint64_t length64) {
113 uint32_t start32, length32;
114 int i, deleted = 0;
srs56949ba54212010-05-18 23:24:02 -0400115
srs569464cbd172011-03-01 22:03:54 -0500116 if ((start64 < UINT32_MAX) && (length64 < UINT32_MAX)) {
117 start32 = (uint32_t) start64;
118 length32 = (uint32_t) length64;
119 for (i = 0; i < MAX_MBR_PARTS; i++) {
srs5694bf8950c2011-03-12 01:23:12 -0500120 if ((partitions[i].GetType() != 0xEE) && (partitions[i].GetStartLBA() == start32)
121 && (partitions[i].GetLengthLBA() == length32)) {
srs569464cbd172011-03-01 22:03:54 -0500122 DeletePartition(i);
123 if (state == hybrid)
124 OptimizeEESize();
125 deleted = 1;
126 } // if (match found)
127 } // for i (partition scan)
128 } // if (hybrid & GPT partition < 2TiB)
129 return deleted;
130} // MBRData::DeleteByLocation()
srs5694c0ca8f82009-08-20 21:35:25 -0400131
srs5694978041c2009-09-21 20:51:47 -0400132/******************************************************
133 * *
134 * Functions that extract data on specific partitions *
135 * *
136 ******************************************************/
137
srs5694221e0872009-08-29 15:00:31 -0400138// Return the MBR data as a GPT partition....
139GPTPart MBRData::AsGPT(int i) {
srs5694bf8950c2011-03-12 01:23:12 -0500140 MBRPart* origPart;
srs5694221e0872009-08-29 15:00:31 -0400141 GPTPart newPart;
142 uint8_t origType;
143 uint64_t firstSector, lastSector;
srs5694221e0872009-08-29 15:00:31 -0400144
145 newPart.BlankPartition();
146 origPart = GetPartition(i);
147 if (origPart != NULL) {
srs5694bf8950c2011-03-12 01:23:12 -0500148 origType = origPart->GetType();
srs5694221e0872009-08-29 15:00:31 -0400149
150 // don't convert extended, hybrid protective, or null (non-existent)
151 // partitions (Note similar protection is in GPTData::XFormPartitions(),
152 // but I want it here too in case I call this function in another
153 // context in the future....)
srs5694e35eb1b2009-09-14 00:29:34 -0400154 if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) &&
srs5694221e0872009-08-29 15:00:31 -0400155 (origType != 0x00) && (origType != 0xEE)) {
srs5694bf8950c2011-03-12 01:23:12 -0500156 firstSector = (uint64_t) origPart->GetStartLBA();
srs5694221e0872009-08-29 15:00:31 -0400157 newPart.SetFirstLBA(firstSector);
srs5694bf8950c2011-03-12 01:23:12 -0500158 lastSector = (uint64_t) origPart->GetLastLBA();
srs5694221e0872009-08-29 15:00:31 -0400159 newPart.SetLastLBA(lastSector);
160 newPart.SetType(((uint16_t) origType) * 0x0100);
srs56946699b012010-02-04 00:55:30 -0500161 newPart.RandomizeUniqueGUID();
srs5694221e0872009-08-29 15:00:31 -0400162 newPart.SetAttributes(0);
srs56946699b012010-02-04 00:55:30 -0500163 newPart.SetName(newPart.GetTypeName());
srs5694978041c2009-09-21 20:51:47 -0400164 } // if not extended, protective, or non-existent
165 } // if (origPart != NULL)
srs5694221e0872009-08-29 15:00:31 -0400166 return newPart;
167} // MBRData::AsGPT()
srs5694978041c2009-09-21 20:51:47 -0400168