blob: 5dbc95e0df7a0a4338edb0addd261b529dc095bc [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
srs5694bf8950c2011-03-12 01:23:12 -050031/* // Assignment operator -- copy entire set of MBR data.
srs569464cbd172011-03-01 22:03:54 -050032MBRData & MBRData::operator=(const MBRData & orig) {
33 BasicMBRData::operator=(orig);
34 return *this;
35} // MBRData::operator=() */
srs5694327129e2010-09-22 01:07:31 -040036
srs5694bf8950c2011-03-12 01:23:12 -050037// Assignment operator -- copy entire set of MBR data.
38MBRData & MBRData::operator=(const BasicMBRData & orig) {
39 BasicMBRData::operator=(orig);
40 return *this;
41} // MBRData::operator=()
42
srs5694978041c2009-09-21 20:51:47 -040043/*****************************************************
44 * *
45 * Functions to create, delete, or change partitions *
46 * *
47 *****************************************************/
48
srs5694221e0872009-08-29 15:00:31 -040049// Create a protective MBR. Clears the boot loader area if clearBoot > 0.
50void MBRData::MakeProtectiveMBR(int clearBoot) {
srs5694978041c2009-09-21 20:51:47 -040051
52 EmptyMBR(clearBoot);
srs5694e7b4ff92009-08-18 13:16:10 -040053
54 // Initialize variables
55 nulls = 0;
56 MBRSignature = MBR_SIGNATURE;
srs56948a4ddfc2010-03-21 19:05:49 -040057 diskSignature = UINT32_C(0);
srs5694e7b4ff92009-08-18 13:16:10 -040058
srs5694bf8950c2011-03-12 01:23:12 -050059 partitions[0].SetStatus(0); // Flag the protective part. as unbootable
srs5694e7b4ff92009-08-18 13:16:10 -040060
srs5694bf8950c2011-03-12 01:23:12 -050061 partitions[0].SetType(UINT8_C(0xEE));
srs5694e7b4ff92009-08-18 13:16:10 -040062 if (diskSize < UINT32_MAX) { // If the disk is under 2TiB
srs5694bf8950c2011-03-12 01:23:12 -050063 partitions[0].SetLocation(UINT32_C(1), (uint32_t) diskSize - UINT32_C(1));
srs5694e7b4ff92009-08-18 13:16:10 -040064 } else { // disk is too big to represent, so fake it...
srs5694bf8950c2011-03-12 01:23:12 -050065 partitions[0].SetLocation(UINT32_C(1), UINT32_MAX);
srs5694e7b4ff92009-08-18 13:16:10 -040066 } // if/else
srs5694bf8950c2011-03-12 01:23:12 -050067 partitions[0].SetInclusion(PRIMARY);
srs5694e7b4ff92009-08-18 13:16:10 -040068
srs5694e7b4ff92009-08-18 13:16:10 -040069 state = gpt;
70} // MBRData::MakeProtectiveMBR()
71
srs5694e4ac11e2009-08-31 10:13:04 -040072// Optimizes the size of the 0xEE (EFI GPT) partition
73void MBRData::OptimizeEESize(void) {
74 int i, typeFlag = 0;
srs5694bf8950c2011-03-12 01:23:12 -050075 uint64_t after;
srs5694e4ac11e2009-08-31 10:13:04 -040076
77 for (i = 0; i < 4; i++) {
78 // Check for non-empty and non-0xEE partitions
srs5694bf8950c2011-03-12 01:23:12 -050079 if ((partitions[i].GetType() != 0xEE) && (partitions[i].GetType() != 0x00))
srs5694e4ac11e2009-08-31 10:13:04 -040080 typeFlag++;
srs5694bf8950c2011-03-12 01:23:12 -050081 if (partitions[i].GetType() == 0xEE) {
srs5694e4ac11e2009-08-31 10:13:04 -040082 // Blank space before this partition; fill it....
srs5694bf8950c2011-03-12 01:23:12 -050083 if (SectorUsedAs(partitions[i].GetStartLBA() - 1, 4) == NONE) {
84 partitions[i].SetStartLBA(FindFirstInFree(partitions[i].GetStartLBA() - 1));
srs5694e4ac11e2009-08-31 10:13:04 -040085 } // if
86 // Blank space after this partition; fill it....
srs5694bf8950c2011-03-12 01:23:12 -050087 after = partitions[i].GetStartLBA() + partitions[i].GetLengthLBA();
88 if (SectorUsedAs(after, 4) == NONE) {
89 partitions[i].SetLengthLBA(FindLastInFree(after) - partitions[i].GetStartLBA() + 1);
srs5694e4ac11e2009-08-31 10:13:04 -040090 } // if free space after
srs569464cbd172011-03-01 22:03:54 -050091 if (after > diskSize) {
92 if (diskSize < UINT32_MAX) { // If the disk is under 2TiB
srs5694bf8950c2011-03-12 01:23:12 -050093 partitions[i].SetLengthLBA((uint32_t) diskSize - partitions[i].GetStartLBA());
srs569464cbd172011-03-01 22:03:54 -050094 } else { // disk is too big to represent, so fake it...
srs5694bf8950c2011-03-12 01:23:12 -050095 partitions[i].SetLengthLBA(UINT32_MAX - partitions[i].GetStartLBA());
srs569464cbd172011-03-01 22:03:54 -050096 } // if/else
97 } // if protective partition is too big
98 RecomputeCHS(i);
srs5694e4ac11e2009-08-31 10:13:04 -040099 } // if partition is 0xEE
srs5694e4ac11e2009-08-31 10:13:04 -0400100 } // for partition loop
srs5694978041c2009-09-21 20:51:47 -0400101 if (typeFlag == 0) { // No non-hybrid partitions found
srs569464cbd172011-03-01 22:03:54 -0500102 MakeProtectiveMBR(); // ensure it's a fully compliant protective MBR.
srs5694978041c2009-09-21 20:51:47 -0400103 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400104} // MBRData::OptimizeEESize()
105
srs569464cbd172011-03-01 22:03:54 -0500106// Delete a partition if one exists at the specified location.
107// Returns 1 if a partition was deleted, 0 otherwise....
108// Used to help keep GPT & hybrid MBR partitions in sync....
109int MBRData::DeleteByLocation(uint64_t start64, uint64_t length64) {
110 uint32_t start32, length32;
111 int i, deleted = 0;
srs56949ba54212010-05-18 23:24:02 -0400112
srs569464cbd172011-03-01 22:03:54 -0500113 if ((start64 < UINT32_MAX) && (length64 < UINT32_MAX)) {
114 start32 = (uint32_t) start64;
115 length32 = (uint32_t) length64;
116 for (i = 0; i < MAX_MBR_PARTS; i++) {
srs5694bf8950c2011-03-12 01:23:12 -0500117 if ((partitions[i].GetType() != 0xEE) && (partitions[i].GetStartLBA() == start32)
118 && (partitions[i].GetLengthLBA() == length32)) {
srs569464cbd172011-03-01 22:03:54 -0500119 DeletePartition(i);
120 if (state == hybrid)
121 OptimizeEESize();
122 deleted = 1;
123 } // if (match found)
124 } // for i (partition scan)
125 } // if (hybrid & GPT partition < 2TiB)
126 return deleted;
127} // MBRData::DeleteByLocation()
srs5694c0ca8f82009-08-20 21:35:25 -0400128
srs5694978041c2009-09-21 20:51:47 -0400129/******************************************************
130 * *
131 * Functions that extract data on specific partitions *
132 * *
133 ******************************************************/
134
srs5694221e0872009-08-29 15:00:31 -0400135// Return the MBR data as a GPT partition....
136GPTPart MBRData::AsGPT(int i) {
srs5694bf8950c2011-03-12 01:23:12 -0500137 MBRPart* origPart;
srs5694221e0872009-08-29 15:00:31 -0400138 GPTPart newPart;
139 uint8_t origType;
140 uint64_t firstSector, lastSector;
srs5694221e0872009-08-29 15:00:31 -0400141
142 newPart.BlankPartition();
143 origPart = GetPartition(i);
144 if (origPart != NULL) {
srs5694bf8950c2011-03-12 01:23:12 -0500145 origType = origPart->GetType();
srs5694221e0872009-08-29 15:00:31 -0400146
147 // don't convert extended, hybrid protective, or null (non-existent)
148 // partitions (Note similar protection is in GPTData::XFormPartitions(),
149 // but I want it here too in case I call this function in another
150 // context in the future....)
srs5694e35eb1b2009-09-14 00:29:34 -0400151 if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) &&
srs5694221e0872009-08-29 15:00:31 -0400152 (origType != 0x00) && (origType != 0xEE)) {
srs5694bf8950c2011-03-12 01:23:12 -0500153 firstSector = (uint64_t) origPart->GetStartLBA();
srs5694221e0872009-08-29 15:00:31 -0400154 newPart.SetFirstLBA(firstSector);
srs5694bf8950c2011-03-12 01:23:12 -0500155 lastSector = (uint64_t) origPart->GetLastLBA();
srs5694221e0872009-08-29 15:00:31 -0400156 newPart.SetLastLBA(lastSector);
157 newPart.SetType(((uint16_t) origType) * 0x0100);
srs56946699b012010-02-04 00:55:30 -0500158 newPart.RandomizeUniqueGUID();
srs5694221e0872009-08-29 15:00:31 -0400159 newPart.SetAttributes(0);
srs56946699b012010-02-04 00:55:30 -0500160 newPart.SetName(newPart.GetTypeName());
srs5694978041c2009-09-21 20:51:47 -0400161 } // if not extended, protective, or non-existent
162 } // if (origPart != NULL)
srs5694221e0872009-08-29 15:00:31 -0400163 return newPart;
164} // MBRData::AsGPT()
srs5694978041c2009-09-21 20:51:47 -0400165