blob: 062206a6ab026d01cbd821a486d752e6b5eaf70a [file] [log] [blame]
srs5694a0eb11a2009-08-29 15:00:08 -04001//
2// C++ Implementation: gptpart
3//
4// Description: Class to implement a SINGLE GPT partition
5//
6//
7// Author: Rod Smith <rodsmith@rodsbooks.com>, (C) 2009
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
srs5694978041c2009-09-21 20:51:47 -040012// This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
13// under the terms of the GNU GPL version 2, as detailed in the COPYING file.
srs5694a0eb11a2009-08-29 15:00:08 -040014
15#define __STDC_LIMIT_MACROS
16#define __STDC_CONSTANT_MACROS
17
srs5694a0eb11a2009-08-29 15:00:08 -040018#include <string.h>
srs5694fed16d02010-01-27 23:03:40 -050019#include <stdio.h>
20#include <iostream>
srs5694a0eb11a2009-08-29 15:00:08 -040021#include "gptpart.h"
22#include "attributes.h"
23
24using namespace std;
25
srs5694a0eb11a2009-08-29 15:00:08 -040026GPTPart::GPTPart(void) {
srs569455d92612010-03-07 22:16:07 -050027 partitionType.Zero();
28 uniqueGUID.Zero();
29 firstLBA = 0;
30 lastLBA = 0;
31 attributes = 0;
srs569464cbd172011-03-01 22:03:54 -050032 memset(name, 0, NAME_SIZE);
srs5694a0eb11a2009-08-29 15:00:08 -040033} // Default constructor
34
35GPTPart::~GPTPart(void) {
36} // destructor
37
srs5694a0eb11a2009-08-29 15:00:08 -040038// Return the gdisk-specific two-byte hex code for the partition
srs5694bf8950c2011-03-12 01:23:12 -050039uint16_t GPTPart::GetHexType(void) const {
srs56946699b012010-02-04 00:55:30 -050040 return partitionType.GetHexType();
srs5694a0eb11a2009-08-29 15:00:08 -040041} // GPTPart::GetHexType()
42
43// Return a plain-text description of the partition type (e.g., "Linux/Windows
44// data" or "Linux swap").
srs56946699b012010-02-04 00:55:30 -050045string GPTPart::GetTypeName(void) {
46 return partitionType.TypeName();
srs5694a0eb11a2009-08-29 15:00:08 -040047} // GPTPart::GetNameType()
48
49// Compute and return the partition's length (or 0 if the end is incorrectly
50// set before the beginning).
srs5694bf8950c2011-03-12 01:23:12 -050051uint64_t GPTPart::GetLengthLBA(void) const {
srs5694a0eb11a2009-08-29 15:00:08 -040052 uint64_t length = 0;
srs569455d92612010-03-07 22:16:07 -050053
srs5694a0eb11a2009-08-29 15:00:08 -040054 if (firstLBA <= lastLBA)
55 length = lastLBA - firstLBA + UINT64_C(1);
56 return length;
57} // GPTPart::GetLengthLBA()
58
srs56940a697312010-01-28 21:10:52 -050059// Return partition's name field, converted to a C++ ASCII string
srs56946699b012010-02-04 00:55:30 -050060string GPTPart::GetDescription(void) {
srs56940a697312010-01-28 21:10:52 -050061 string theName;
srs5694a0eb11a2009-08-29 15:00:08 -040062 int i;
63
srs56940a697312010-01-28 21:10:52 -050064 theName = "";
65 for (i = 0; i < NAME_SIZE; i += 2) {
66 if (name[i] != '\0')
67 theName += name[i];
68 } // for
69 return theName;
srs56946699b012010-02-04 00:55:30 -050070} // GPTPart::GetDescription()
srs56940a697312010-01-28 21:10:52 -050071
srs569408bb0da2010-02-19 17:19:55 -050072// Return 1 if the partition is in use
73int GPTPart::IsUsed(void) {
74 return (firstLBA != UINT64_C(0));
75} // GPTPart::IsUsed()
76
srs56940a697312010-01-28 21:10:52 -050077// Set the type code to the specified one. Also changes the partition
78// name *IF* the current name is the generic one for the current partition
79// type.
srs56946699b012010-02-04 00:55:30 -050080void GPTPart::SetType(PartType t) {
81 if (GetDescription() == partitionType.TypeName()) {
82 SetName(t.TypeName());
srs56940a697312010-01-28 21:10:52 -050083 } // if
84 partitionType = t;
85} // GPTPart::SetType()
srs5694a0eb11a2009-08-29 15:00:08 -040086
srs5694a0eb11a2009-08-29 15:00:08 -040087// Set the name for a partition to theName, or prompt for a name if
srs5694fed16d02010-01-27 23:03:40 -050088// theName is empty. Note that theName is a standard C++-style ASCII
srs5694a0eb11a2009-08-29 15:00:08 -040089// string, although the GUID partition definition requires a UTF-16LE
90// string. This function creates a simple-minded copy for this.
srs56940a697312010-01-28 21:10:52 -050091void GPTPart::SetName(const string & theName) {
srs569455d92612010-03-07 22:16:07 -050092 char newName[NAME_SIZE];
srs569464cbd172011-03-01 22:03:54 -050093 size_t i;
srs5694a0eb11a2009-08-29 15:00:08 -040094
srs569464cbd172011-03-01 22:03:54 -050095 // Blank out new name string, so that it will terminate in a null
96 // when data are copied to it....
97 memset(newName, 0, NAME_SIZE);
srs5694a0eb11a2009-08-29 15:00:08 -040098
srs5694fed16d02010-01-27 23:03:40 -050099 if (theName == "") { // No name specified, so get one from the user
100 cout << "Enter name: ";
srs5694bf8950c2011-03-12 01:23:12 -0500101 ReadCString(newName, NAME_SIZE / 2 + 1);
srs5694a0eb11a2009-08-29 15:00:08 -0400102
103 // Input is likely to include a newline, so remove it....
srs569464cbd172011-03-01 22:03:54 -0500104 i = strlen(newName);
105 if (i && newName[i - 1] == '\n')
106 newName[i - 1] = '\0';
srs5694a0eb11a2009-08-29 15:00:08 -0400107 } else {
srs5694fed16d02010-01-27 23:03:40 -0500108 strcpy(newName, theName.substr(0, NAME_SIZE / 2).c_str());
srs5694a0eb11a2009-08-29 15:00:08 -0400109 } // if
110
111 // Copy the C-style ASCII string from newName into a form that the GPT
112 // table will accept....
srs569464cbd172011-03-01 22:03:54 -0500113 memset(name, 0, NAME_SIZE);
114 for (i = 0; i < NAME_SIZE / 2; i++)
115 name[i * 2] = newName[i];
srs5694a0eb11a2009-08-29 15:00:08 -0400116} // GPTPart::SetName()
117
srs56946699b012010-02-04 00:55:30 -0500118// Set the name for the partition based on the current GUID partition type
119// code's associated name
120void GPTPart::SetDefaultDescription(void) {
121 SetName(partitionType.TypeName());
122} // GPTPart::SetDefaultDescription()
123
srs56940a697312010-01-28 21:10:52 -0500124GPTPart & GPTPart::operator=(const GPTPart & orig) {
srs56940a697312010-01-28 21:10:52 -0500125 partitionType = orig.partitionType;
126 uniqueGUID = orig.uniqueGUID;
127 firstLBA = orig.firstLBA;
128 lastLBA = orig.lastLBA;
129 attributes = orig.attributes;
srs569464cbd172011-03-01 22:03:54 -0500130 memcpy(name, orig.name, NAME_SIZE);
srs56940a697312010-01-28 21:10:52 -0500131 return *this;
132} // assignment operator
133
srs56949a46b042011-03-15 00:34:10 -0400134// Compare the values, and return a bool result.
135// Because this is intended for sorting and a firstLBA value of 0 denotes
136// a partition that's not in use and so that should be sorted upwards,
137// we return the opposite of the usual arithmetic result when either
138// firstLBA value is 0.
139bool GPTPart::operator<(const GPTPart &other) const {
140
141 if (firstLBA && other.firstLBA)
142 return (firstLBA < other.firstLBA);
143 else
144 return (other.firstLBA < firstLBA);
145} // GPTPart::operator<()
146
srs56940a697312010-01-28 21:10:52 -0500147// Display summary information; does nothing if the partition is empty.
148void GPTPart::ShowSummary(int partNum, uint32_t blockSize) {
149 string sizeInSI;
srs569464cbd172011-03-01 22:03:54 -0500150 size_t i;
srs56940a697312010-01-28 21:10:52 -0500151
152 if (firstLBA != 0) {
srs56940873e9d2010-10-07 13:00:45 -0400153 sizeInSI = BytesToSI(lastLBA - firstLBA + 1, blockSize);
srs569408bb0da2010-02-19 17:19:55 -0500154 cout.fill(' ');
srs56940a697312010-01-28 21:10:52 -0500155 cout.width(4);
156 cout << partNum + 1 << " ";
157 cout.width(14);
158 cout << firstLBA << " ";
159 cout.width(14);
160 cout << lastLBA << " ";
srs56940873e9d2010-10-07 13:00:45 -0400161 cout << BytesToSI(lastLBA - firstLBA + 1, blockSize) << " ";
srs569464cbd172011-03-01 22:03:54 -0500162 for (i = 0; i < 10 - sizeInSI.length(); i++)
srs5694cb76c672010-02-11 22:22:22 -0500163 cout << " ";
srs56940a697312010-01-28 21:10:52 -0500164 cout.fill('0');
165 cout.width(4);
166 cout.setf(ios::uppercase);
srs56946699b012010-02-04 00:55:30 -0500167 cout << hex << partitionType.GetHexType() << " " << dec;
srs56940a697312010-01-28 21:10:52 -0500168 cout.fill(' ');
srs56946699b012010-02-04 00:55:30 -0500169 cout << GetDescription().substr(0, 23) << "\n";
srs56940a697312010-01-28 21:10:52 -0500170 cout.fill(' ');
171 } // if
172} // GPTPart::ShowSummary()
173
174// Show detailed partition information. Does nothing if the partition is
175// empty (as determined by firstLBA being 0).
176void GPTPart::ShowDetails(uint32_t blockSize) {
177 uint64_t size;
178
179 if (firstLBA != 0) {
srs56945a081752010-09-24 20:39:41 -0400180 cout << "Partition GUID code: " << partitionType;
srs56946699b012010-02-04 00:55:30 -0500181 cout << " (" << partitionType.TypeName() << ")\n";
srs56945a081752010-09-24 20:39:41 -0400182 cout << "Partition unique GUID: " << uniqueGUID << "\n";
srs56940a697312010-01-28 21:10:52 -0500183
184 cout << "First sector: " << firstLBA << " (at "
srs56940873e9d2010-10-07 13:00:45 -0400185 << BytesToSI(firstLBA, blockSize) << ")\n";
srs56940a697312010-01-28 21:10:52 -0500186 cout << "Last sector: " << lastLBA << " (at "
srs56940873e9d2010-10-07 13:00:45 -0400187 << BytesToSI(lastLBA, blockSize) << ")\n";
srs56940a697312010-01-28 21:10:52 -0500188 size = (lastLBA - firstLBA + 1);
189 cout << "Partition size: " << size << " sectors ("
srs56940873e9d2010-10-07 13:00:45 -0400190 << BytesToSI(size, blockSize) << ")\n";
srs56940a697312010-01-28 21:10:52 -0500191 cout << "Attribute flags: ";
192 cout.fill('0');
193 cout.width(16);
194 cout << hex;
195 cout << attributes << "\n";
196 cout << dec;
srs56946699b012010-02-04 00:55:30 -0500197 cout << "Partition name: " << GetDescription() << "\n";
srs56940a697312010-01-28 21:10:52 -0500198 cout.fill(' ');
199 } // if
200} // GPTPart::ShowDetails()
201
202// Blank (delete) a single partition
203void GPTPart::BlankPartition(void) {
srs56946699b012010-02-04 00:55:30 -0500204 uniqueGUID.Zero();
205 partitionType.Zero();
srs56940a697312010-01-28 21:10:52 -0500206 firstLBA = 0;
207 lastLBA = 0;
208 attributes = 0;
srs569464cbd172011-03-01 22:03:54 -0500209 memset(name, 0, NAME_SIZE);
srs56940a697312010-01-28 21:10:52 -0500210} // GPTPart::BlankPartition
211
212// Returns 1 if the two partitions overlap, 0 if they don't
213int GPTPart::DoTheyOverlap(const GPTPart & other) {
srs56940a697312010-01-28 21:10:52 -0500214 // Don't bother checking unless these are defined (both start and end points
215 // are 0 for undefined partitions, so just check the start points)
srs569464cbd172011-03-01 22:03:54 -0500216 return firstLBA && other.firstLBA &&
217 (firstLBA <= other.lastLBA) != (lastLBA < other.firstLBA);
srs56940a697312010-01-28 21:10:52 -0500218} // GPTPart::DoTheyOverlap()
219
220// Reverse the bytes of integral data types; used on big-endian systems.
221void GPTPart::ReversePartBytes(void) {
srs56940a697312010-01-28 21:10:52 -0500222 ReverseBytes(&firstLBA, 8);
223 ReverseBytes(&lastLBA, 8);
224 ReverseBytes(&attributes, 8);
225} // GPTPart::ReverseBytes()
226
227/****************************************
228 * Functions requiring user interaction *
229 ****************************************/
230
srs56946699b012010-02-04 00:55:30 -0500231// Change the type code on the partition. Also changes the name if the original
232// name is the generic one for the partition type.
srs56940a697312010-01-28 21:10:52 -0500233void GPTPart::ChangeType(void) {
234 char line[255];
srs569464cbd172011-03-01 22:03:54 -0500235 int changeName;
srs569482f3f0b2010-09-22 10:50:24 -0400236 PartType tempType = (GUIDData) "00000000-0000-0000-0000-000000000000";
srs56940a697312010-01-28 21:10:52 -0500237
srs569464cbd172011-03-01 22:03:54 -0500238 changeName = (GetDescription() == GetTypeName());
srs569455d92612010-03-07 22:16:07 -0500239
srs56946699b012010-02-04 00:55:30 -0500240 cout << "Current type is '" << GetTypeName() << "'\n";
srs569482f3f0b2010-09-22 10:50:24 -0400241 do {
242 cout << "Hex code or GUID (L to show codes, Enter = 0700): ";
srs56949a46b042011-03-15 00:34:10 -0400243 ReadCString(line, sizeof(line));
srs569482f3f0b2010-09-22 10:50:24 -0400244 if ((line[0] == 'L') || (line[0] == 'l')) {
srs56946699b012010-02-04 00:55:30 -0500245 partitionType.ShowAllTypes();
srs569482f3f0b2010-09-22 10:50:24 -0400246 } else {
247 if (strlen(line) == 1)
248 tempType = 0x0700;
249 else
250 tempType = line;
251 } // if/else
252 } while (tempType == (GUIDData) "00000000-0000-0000-0000-000000000000");
253 partitionType = tempType;
srs56946699b012010-02-04 00:55:30 -0500254 cout << "Changed type of partition to '" << partitionType.TypeName() << "'\n";
255 if (changeName) {
256 SetDefaultDescription();
257 } // if
srs56940a697312010-01-28 21:10:52 -0500258} // GPTPart::ChangeType()