blob: a01fe90a5f2ea64099e6fd4e7443361271cb878b [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
Roderick W. Smith54f8fb12015-03-17 19:46:05 -040033MBRData::~MBRData(void) {
34} // MBRData destructor
srs5694327129e2010-09-22 01:07:31 -040035
srs5694bf8950c2011-03-12 01:23:12 -050036// Assignment operator -- copy entire set of MBR data.
37MBRData & MBRData::operator=(const BasicMBRData & orig) {
38 BasicMBRData::operator=(orig);
39 return *this;
40} // MBRData::operator=()
41
srs5694978041c2009-09-21 20:51:47 -040042/*****************************************************
43 * *
44 * Functions to create, delete, or change partitions *
45 * *
46 *****************************************************/
47
srs5694221e0872009-08-29 15:00:31 -040048// Create a protective MBR. Clears the boot loader area if clearBoot > 0.
49void MBRData::MakeProtectiveMBR(int clearBoot) {
srs5694978041c2009-09-21 20:51:47 -040050
51 EmptyMBR(clearBoot);
srs5694e7b4ff92009-08-18 13:16:10 -040052
53 // Initialize variables
54 nulls = 0;
55 MBRSignature = MBR_SIGNATURE;
srs56948a4ddfc2010-03-21 19:05:49 -040056 diskSignature = UINT32_C(0);
srs5694e7b4ff92009-08-18 13:16:10 -040057
srs5694bf8950c2011-03-12 01:23:12 -050058 partitions[0].SetStatus(0); // Flag the protective part. as unbootable
srs5694e7b4ff92009-08-18 13:16:10 -040059
srs5694bf8950c2011-03-12 01:23:12 -050060 partitions[0].SetType(UINT8_C(0xEE));
srs5694e7b4ff92009-08-18 13:16:10 -040061 if (diskSize < UINT32_MAX) { // If the disk is under 2TiB
srs5694bf8950c2011-03-12 01:23:12 -050062 partitions[0].SetLocation(UINT32_C(1), (uint32_t) diskSize - UINT32_C(1));
srs5694e7b4ff92009-08-18 13:16:10 -040063 } else { // disk is too big to represent, so fake it...
srs5694bf8950c2011-03-12 01:23:12 -050064 partitions[0].SetLocation(UINT32_C(1), UINT32_MAX);
srs5694e7b4ff92009-08-18 13:16:10 -040065 } // if/else
srs5694bf8950c2011-03-12 01:23:12 -050066 partitions[0].SetInclusion(PRIMARY);
srs5694e7b4ff92009-08-18 13:16:10 -040067
srs5694e7b4ff92009-08-18 13:16:10 -040068 state = gpt;
69} // MBRData::MakeProtectiveMBR()
70
srs5694e4ac11e2009-08-31 10:13:04 -040071// Optimizes the size of the 0xEE (EFI GPT) partition
72void MBRData::OptimizeEESize(void) {
73 int i, typeFlag = 0;
srs5694bf8950c2011-03-12 01:23:12 -050074 uint64_t after;
srs5694e4ac11e2009-08-31 10:13:04 -040075
76 for (i = 0; i < 4; i++) {
77 // Check for non-empty and non-0xEE partitions
srs5694bf8950c2011-03-12 01:23:12 -050078 if ((partitions[i].GetType() != 0xEE) && (partitions[i].GetType() != 0x00))
srs5694e4ac11e2009-08-31 10:13:04 -040079 typeFlag++;
srs5694bf8950c2011-03-12 01:23:12 -050080 if (partitions[i].GetType() == 0xEE) {
srs5694e4ac11e2009-08-31 10:13:04 -040081 // Blank space before this partition; fill it....
srs5694bf8950c2011-03-12 01:23:12 -050082 if (SectorUsedAs(partitions[i].GetStartLBA() - 1, 4) == NONE) {
83 partitions[i].SetStartLBA(FindFirstInFree(partitions[i].GetStartLBA() - 1));
srs5694e4ac11e2009-08-31 10:13:04 -040084 } // if
85 // Blank space after this partition; fill it....
srs5694bf8950c2011-03-12 01:23:12 -050086 after = partitions[i].GetStartLBA() + partitions[i].GetLengthLBA();
87 if (SectorUsedAs(after, 4) == NONE) {
88 partitions[i].SetLengthLBA(FindLastInFree(after) - partitions[i].GetStartLBA() + 1);
srs5694e4ac11e2009-08-31 10:13:04 -040089 } // if free space after
srs569464cbd172011-03-01 22:03:54 -050090 if (after > diskSize) {
91 if (diskSize < UINT32_MAX) { // If the disk is under 2TiB
srs5694bf8950c2011-03-12 01:23:12 -050092 partitions[i].SetLengthLBA((uint32_t) diskSize - partitions[i].GetStartLBA());
srs569464cbd172011-03-01 22:03:54 -050093 } else { // disk is too big to represent, so fake it...
srs5694bf8950c2011-03-12 01:23:12 -050094 partitions[i].SetLengthLBA(UINT32_MAX - partitions[i].GetStartLBA());
srs569464cbd172011-03-01 22:03:54 -050095 } // if/else
96 } // if protective partition is too big
97 RecomputeCHS(i);
srs5694e4ac11e2009-08-31 10:13:04 -040098 } // if partition is 0xEE
srs5694e4ac11e2009-08-31 10:13:04 -040099 } // for partition loop
srs5694978041c2009-09-21 20:51:47 -0400100 if (typeFlag == 0) { // No non-hybrid partitions found
srs569464cbd172011-03-01 22:03:54 -0500101 MakeProtectiveMBR(); // ensure it's a fully compliant protective MBR.
srs5694978041c2009-09-21 20:51:47 -0400102 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400103} // MBRData::OptimizeEESize()
104
srs569464cbd172011-03-01 22:03:54 -0500105// Delete a partition if one exists at the specified location.
106// Returns 1 if a partition was deleted, 0 otherwise....
107// Used to help keep GPT & hybrid MBR partitions in sync....
108int MBRData::DeleteByLocation(uint64_t start64, uint64_t length64) {
109 uint32_t start32, length32;
110 int i, deleted = 0;
srs56949ba54212010-05-18 23:24:02 -0400111
srs569464cbd172011-03-01 22:03:54 -0500112 if ((start64 < UINT32_MAX) && (length64 < UINT32_MAX)) {
113 start32 = (uint32_t) start64;
114 length32 = (uint32_t) length64;
115 for (i = 0; i < MAX_MBR_PARTS; i++) {
srs5694bf8950c2011-03-12 01:23:12 -0500116 if ((partitions[i].GetType() != 0xEE) && (partitions[i].GetStartLBA() == start32)
117 && (partitions[i].GetLengthLBA() == length32)) {
srs569464cbd172011-03-01 22:03:54 -0500118 DeletePartition(i);
119 if (state == hybrid)
120 OptimizeEESize();
121 deleted = 1;
122 } // if (match found)
123 } // for i (partition scan)
124 } // if (hybrid & GPT partition < 2TiB)
125 return deleted;
126} // MBRData::DeleteByLocation()
srs5694c0ca8f82009-08-20 21:35:25 -0400127
srs5694978041c2009-09-21 20:51:47 -0400128/******************************************************
129 * *
130 * Functions that extract data on specific partitions *
131 * *
132 ******************************************************/
133
srs5694221e0872009-08-29 15:00:31 -0400134// Return the MBR data as a GPT partition....
135GPTPart MBRData::AsGPT(int i) {
srs5694bf8950c2011-03-12 01:23:12 -0500136 MBRPart* origPart;
srs5694221e0872009-08-29 15:00:31 -0400137 GPTPart newPart;
138 uint8_t origType;
139 uint64_t firstSector, lastSector;
srs5694221e0872009-08-29 15:00:31 -0400140
141 newPart.BlankPartition();
142 origPart = GetPartition(i);
143 if (origPart != NULL) {
srs5694bf8950c2011-03-12 01:23:12 -0500144 origType = origPart->GetType();
srs5694221e0872009-08-29 15:00:31 -0400145
146 // don't convert extended, hybrid protective, or null (non-existent)
147 // partitions (Note similar protection is in GPTData::XFormPartitions(),
148 // but I want it here too in case I call this function in another
149 // context in the future....)
srs5694e35eb1b2009-09-14 00:29:34 -0400150 if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) &&
srs5694221e0872009-08-29 15:00:31 -0400151 (origType != 0x00) && (origType != 0xEE)) {
srs5694bf8950c2011-03-12 01:23:12 -0500152 firstSector = (uint64_t) origPart->GetStartLBA();
srs5694221e0872009-08-29 15:00:31 -0400153 newPart.SetFirstLBA(firstSector);
srs5694bf8950c2011-03-12 01:23:12 -0500154 lastSector = (uint64_t) origPart->GetLastLBA();
srs5694221e0872009-08-29 15:00:31 -0400155 newPart.SetLastLBA(lastSector);
156 newPart.SetType(((uint16_t) origType) * 0x0100);
srs56946699b012010-02-04 00:55:30 -0500157 newPart.RandomizeUniqueGUID();
srs5694221e0872009-08-29 15:00:31 -0400158 newPart.SetAttributes(0);
srs56946699b012010-02-04 00:55:30 -0500159 newPart.SetName(newPart.GetTypeName());
srs5694978041c2009-09-21 20:51:47 -0400160 } // if not extended, protective, or non-existent
161 } // if (origPart != NULL)
srs5694221e0872009-08-29 15:00:31 -0400162 return newPart;
163} // MBRData::AsGPT()
srs5694978041c2009-09-21 20:51:47 -0400164