blob: 00aafc2d9ee94ce0c6d176c00708d2a083c8d3fe [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
Aurimas Liutikasfcad0602016-05-10 19:16:10 -070010#ifndef __STDC_CONSTANT_MACROS
srs5694e7b4ff92009-08-18 13:16:10 -040011#define __STDC_CONSTANT_MACROS
Aurimas Liutikasfcad0602016-05-10 19:16:10 -070012#endif
srs5694e7b4ff92009-08-18 13:16:10 -040013
14#include <stdio.h>
srs5694e7b4ff92009-08-18 13:16:10 -040015#include <stdlib.h>
16#include <stdint.h>
17#include <fcntl.h>
18#include <string.h>
19#include <time.h>
20#include <sys/stat.h>
21#include <errno.h>
srs5694fed16d02010-01-27 23:03:40 -050022#include <iostream>
srs5694e7b4ff92009-08-18 13:16:10 -040023#include "mbr.h"
srs5694e7b4ff92009-08-18 13:16:10 -040024
25using namespace std;
26
27/****************************************
28 * *
29 * MBRData class and related structures *
30 * *
31 ****************************************/
32
srs5694bf8950c2011-03-12 01:23:12 -050033/* // Assignment operator -- copy entire set of MBR data.
srs569464cbd172011-03-01 22:03:54 -050034MBRData & MBRData::operator=(const MBRData & orig) {
35 BasicMBRData::operator=(orig);
36 return *this;
37} // MBRData::operator=() */
srs5694327129e2010-09-22 01:07:31 -040038
srs5694bf8950c2011-03-12 01:23:12 -050039// Assignment operator -- copy entire set of MBR data.
40MBRData & MBRData::operator=(const BasicMBRData & orig) {
41 BasicMBRData::operator=(orig);
42 return *this;
43} // MBRData::operator=()
44
srs5694978041c2009-09-21 20:51:47 -040045/*****************************************************
46 * *
47 * Functions to create, delete, or change partitions *
48 * *
49 *****************************************************/
50
srs5694221e0872009-08-29 15:00:31 -040051// Create a protective MBR. Clears the boot loader area if clearBoot > 0.
52void MBRData::MakeProtectiveMBR(int clearBoot) {
srs5694978041c2009-09-21 20:51:47 -040053
54 EmptyMBR(clearBoot);
srs5694e7b4ff92009-08-18 13:16:10 -040055
56 // Initialize variables
57 nulls = 0;
58 MBRSignature = MBR_SIGNATURE;
srs56948a4ddfc2010-03-21 19:05:49 -040059 diskSignature = UINT32_C(0);
srs5694e7b4ff92009-08-18 13:16:10 -040060
srs5694bf8950c2011-03-12 01:23:12 -050061 partitions[0].SetStatus(0); // Flag the protective part. as unbootable
srs5694e7b4ff92009-08-18 13:16:10 -040062
srs5694bf8950c2011-03-12 01:23:12 -050063 partitions[0].SetType(UINT8_C(0xEE));
srs5694e7b4ff92009-08-18 13:16:10 -040064 if (diskSize < UINT32_MAX) { // If the disk is under 2TiB
srs5694bf8950c2011-03-12 01:23:12 -050065 partitions[0].SetLocation(UINT32_C(1), (uint32_t) diskSize - UINT32_C(1));
srs5694e7b4ff92009-08-18 13:16:10 -040066 } else { // disk is too big to represent, so fake it...
srs5694bf8950c2011-03-12 01:23:12 -050067 partitions[0].SetLocation(UINT32_C(1), UINT32_MAX);
srs5694e7b4ff92009-08-18 13:16:10 -040068 } // if/else
srs5694bf8950c2011-03-12 01:23:12 -050069 partitions[0].SetInclusion(PRIMARY);
srs5694e7b4ff92009-08-18 13:16:10 -040070
srs5694e7b4ff92009-08-18 13:16:10 -040071 state = gpt;
72} // MBRData::MakeProtectiveMBR()
73
srs5694e4ac11e2009-08-31 10:13:04 -040074// Optimizes the size of the 0xEE (EFI GPT) partition
75void MBRData::OptimizeEESize(void) {
76 int i, typeFlag = 0;
srs5694bf8950c2011-03-12 01:23:12 -050077 uint64_t after;
srs5694e4ac11e2009-08-31 10:13:04 -040078
79 for (i = 0; i < 4; i++) {
80 // Check for non-empty and non-0xEE partitions
srs5694bf8950c2011-03-12 01:23:12 -050081 if ((partitions[i].GetType() != 0xEE) && (partitions[i].GetType() != 0x00))
srs5694e4ac11e2009-08-31 10:13:04 -040082 typeFlag++;
srs5694bf8950c2011-03-12 01:23:12 -050083 if (partitions[i].GetType() == 0xEE) {
srs5694e4ac11e2009-08-31 10:13:04 -040084 // Blank space before this partition; fill it....
srs5694bf8950c2011-03-12 01:23:12 -050085 if (SectorUsedAs(partitions[i].GetStartLBA() - 1, 4) == NONE) {
86 partitions[i].SetStartLBA(FindFirstInFree(partitions[i].GetStartLBA() - 1));
srs5694e4ac11e2009-08-31 10:13:04 -040087 } // if
88 // Blank space after this partition; fill it....
srs5694bf8950c2011-03-12 01:23:12 -050089 after = partitions[i].GetStartLBA() + partitions[i].GetLengthLBA();
90 if (SectorUsedAs(after, 4) == NONE) {
91 partitions[i].SetLengthLBA(FindLastInFree(after) - partitions[i].GetStartLBA() + 1);
srs5694e4ac11e2009-08-31 10:13:04 -040092 } // if free space after
srs569464cbd172011-03-01 22:03:54 -050093 if (after > diskSize) {
94 if (diskSize < UINT32_MAX) { // If the disk is under 2TiB
srs5694bf8950c2011-03-12 01:23:12 -050095 partitions[i].SetLengthLBA((uint32_t) diskSize - partitions[i].GetStartLBA());
srs569464cbd172011-03-01 22:03:54 -050096 } else { // disk is too big to represent, so fake it...
srs5694bf8950c2011-03-12 01:23:12 -050097 partitions[i].SetLengthLBA(UINT32_MAX - partitions[i].GetStartLBA());
srs569464cbd172011-03-01 22:03:54 -050098 } // if/else
99 } // if protective partition is too big
100 RecomputeCHS(i);
srs5694e4ac11e2009-08-31 10:13:04 -0400101 } // if partition is 0xEE
srs5694e4ac11e2009-08-31 10:13:04 -0400102 } // for partition loop
srs5694978041c2009-09-21 20:51:47 -0400103 if (typeFlag == 0) { // No non-hybrid partitions found
srs569464cbd172011-03-01 22:03:54 -0500104 MakeProtectiveMBR(); // ensure it's a fully compliant protective MBR.
srs5694978041c2009-09-21 20:51:47 -0400105 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400106} // MBRData::OptimizeEESize()
107
srs569464cbd172011-03-01 22:03:54 -0500108// Delete a partition if one exists at the specified location.
109// Returns 1 if a partition was deleted, 0 otherwise....
110// Used to help keep GPT & hybrid MBR partitions in sync....
111int MBRData::DeleteByLocation(uint64_t start64, uint64_t length64) {
112 uint32_t start32, length32;
113 int i, deleted = 0;
srs56949ba54212010-05-18 23:24:02 -0400114
srs569464cbd172011-03-01 22:03:54 -0500115 if ((start64 < UINT32_MAX) && (length64 < UINT32_MAX)) {
116 start32 = (uint32_t) start64;
117 length32 = (uint32_t) length64;
118 for (i = 0; i < MAX_MBR_PARTS; i++) {
srs5694bf8950c2011-03-12 01:23:12 -0500119 if ((partitions[i].GetType() != 0xEE) && (partitions[i].GetStartLBA() == start32)
120 && (partitions[i].GetLengthLBA() == length32)) {
srs569464cbd172011-03-01 22:03:54 -0500121 DeletePartition(i);
122 if (state == hybrid)
123 OptimizeEESize();
124 deleted = 1;
125 } // if (match found)
126 } // for i (partition scan)
127 } // if (hybrid & GPT partition < 2TiB)
128 return deleted;
129} // MBRData::DeleteByLocation()
srs5694c0ca8f82009-08-20 21:35:25 -0400130
srs5694978041c2009-09-21 20:51:47 -0400131/******************************************************
132 * *
133 * Functions that extract data on specific partitions *
134 * *
135 ******************************************************/
136
srs5694221e0872009-08-29 15:00:31 -0400137// Return the MBR data as a GPT partition....
138GPTPart MBRData::AsGPT(int i) {
srs5694bf8950c2011-03-12 01:23:12 -0500139 MBRPart* origPart;
srs5694221e0872009-08-29 15:00:31 -0400140 GPTPart newPart;
141 uint8_t origType;
142 uint64_t firstSector, lastSector;
srs5694221e0872009-08-29 15:00:31 -0400143
144 newPart.BlankPartition();
145 origPart = GetPartition(i);
146 if (origPart != NULL) {
srs5694bf8950c2011-03-12 01:23:12 -0500147 origType = origPart->GetType();
srs5694221e0872009-08-29 15:00:31 -0400148
149 // don't convert extended, hybrid protective, or null (non-existent)
150 // partitions (Note similar protection is in GPTData::XFormPartitions(),
151 // but I want it here too in case I call this function in another
152 // context in the future....)
srs5694e35eb1b2009-09-14 00:29:34 -0400153 if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) &&
srs5694221e0872009-08-29 15:00:31 -0400154 (origType != 0x00) && (origType != 0xEE)) {
srs5694bf8950c2011-03-12 01:23:12 -0500155 firstSector = (uint64_t) origPart->GetStartLBA();
srs5694221e0872009-08-29 15:00:31 -0400156 newPart.SetFirstLBA(firstSector);
srs5694bf8950c2011-03-12 01:23:12 -0500157 lastSector = (uint64_t) origPart->GetLastLBA();
srs5694221e0872009-08-29 15:00:31 -0400158 newPart.SetLastLBA(lastSector);
159 newPart.SetType(((uint16_t) origType) * 0x0100);
srs56946699b012010-02-04 00:55:30 -0500160 newPart.RandomizeUniqueGUID();
srs5694221e0872009-08-29 15:00:31 -0400161 newPart.SetAttributes(0);
srs56946699b012010-02-04 00:55:30 -0500162 newPart.SetName(newPart.GetTypeName());
srs5694978041c2009-09-21 20:51:47 -0400163 } // if not extended, protective, or non-existent
164 } // if (origPart != NULL)
srs5694221e0872009-08-29 15:00:31 -0400165 return newPart;
166} // MBRData::AsGPT()
srs5694978041c2009-09-21 20:51:47 -0400167