blob: 54aa6185cfe0319fc4d26ae5438539b8f1423093 [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
134// Display summary information; does nothing if the partition is empty.
135void GPTPart::ShowSummary(int partNum, uint32_t blockSize) {
136 string sizeInSI;
srs569464cbd172011-03-01 22:03:54 -0500137 size_t i;
srs56940a697312010-01-28 21:10:52 -0500138
139 if (firstLBA != 0) {
srs56940873e9d2010-10-07 13:00:45 -0400140 sizeInSI = BytesToSI(lastLBA - firstLBA + 1, blockSize);
srs569408bb0da2010-02-19 17:19:55 -0500141 cout.fill(' ');
srs56940a697312010-01-28 21:10:52 -0500142 cout.width(4);
143 cout << partNum + 1 << " ";
144 cout.width(14);
145 cout << firstLBA << " ";
146 cout.width(14);
147 cout << lastLBA << " ";
srs56940873e9d2010-10-07 13:00:45 -0400148 cout << BytesToSI(lastLBA - firstLBA + 1, blockSize) << " ";
srs569464cbd172011-03-01 22:03:54 -0500149 for (i = 0; i < 10 - sizeInSI.length(); i++)
srs5694cb76c672010-02-11 22:22:22 -0500150 cout << " ";
srs56940a697312010-01-28 21:10:52 -0500151 cout.fill('0');
152 cout.width(4);
153 cout.setf(ios::uppercase);
srs56946699b012010-02-04 00:55:30 -0500154 cout << hex << partitionType.GetHexType() << " " << dec;
srs56940a697312010-01-28 21:10:52 -0500155 cout.fill(' ');
srs56946699b012010-02-04 00:55:30 -0500156 cout << GetDescription().substr(0, 23) << "\n";
srs56940a697312010-01-28 21:10:52 -0500157 cout.fill(' ');
158 } // if
159} // GPTPart::ShowSummary()
160
161// Show detailed partition information. Does nothing if the partition is
162// empty (as determined by firstLBA being 0).
163void GPTPart::ShowDetails(uint32_t blockSize) {
164 uint64_t size;
165
166 if (firstLBA != 0) {
srs56945a081752010-09-24 20:39:41 -0400167 cout << "Partition GUID code: " << partitionType;
srs56946699b012010-02-04 00:55:30 -0500168 cout << " (" << partitionType.TypeName() << ")\n";
srs56945a081752010-09-24 20:39:41 -0400169 cout << "Partition unique GUID: " << uniqueGUID << "\n";
srs56940a697312010-01-28 21:10:52 -0500170
171 cout << "First sector: " << firstLBA << " (at "
srs56940873e9d2010-10-07 13:00:45 -0400172 << BytesToSI(firstLBA, blockSize) << ")\n";
srs56940a697312010-01-28 21:10:52 -0500173 cout << "Last sector: " << lastLBA << " (at "
srs56940873e9d2010-10-07 13:00:45 -0400174 << BytesToSI(lastLBA, blockSize) << ")\n";
srs56940a697312010-01-28 21:10:52 -0500175 size = (lastLBA - firstLBA + 1);
176 cout << "Partition size: " << size << " sectors ("
srs56940873e9d2010-10-07 13:00:45 -0400177 << BytesToSI(size, blockSize) << ")\n";
srs56940a697312010-01-28 21:10:52 -0500178 cout << "Attribute flags: ";
179 cout.fill('0');
180 cout.width(16);
181 cout << hex;
182 cout << attributes << "\n";
183 cout << dec;
srs56946699b012010-02-04 00:55:30 -0500184 cout << "Partition name: " << GetDescription() << "\n";
srs56940a697312010-01-28 21:10:52 -0500185 cout.fill(' ');
186 } // if
187} // GPTPart::ShowDetails()
188
189// Blank (delete) a single partition
190void GPTPart::BlankPartition(void) {
srs56946699b012010-02-04 00:55:30 -0500191 uniqueGUID.Zero();
192 partitionType.Zero();
srs56940a697312010-01-28 21:10:52 -0500193 firstLBA = 0;
194 lastLBA = 0;
195 attributes = 0;
srs569464cbd172011-03-01 22:03:54 -0500196 memset(name, 0, NAME_SIZE);
srs56940a697312010-01-28 21:10:52 -0500197} // GPTPart::BlankPartition
198
199// Returns 1 if the two partitions overlap, 0 if they don't
200int GPTPart::DoTheyOverlap(const GPTPart & other) {
srs56940a697312010-01-28 21:10:52 -0500201 // Don't bother checking unless these are defined (both start and end points
202 // are 0 for undefined partitions, so just check the start points)
srs569464cbd172011-03-01 22:03:54 -0500203 return firstLBA && other.firstLBA &&
204 (firstLBA <= other.lastLBA) != (lastLBA < other.firstLBA);
srs56940a697312010-01-28 21:10:52 -0500205} // GPTPart::DoTheyOverlap()
206
207// Reverse the bytes of integral data types; used on big-endian systems.
208void GPTPart::ReversePartBytes(void) {
srs56940a697312010-01-28 21:10:52 -0500209 ReverseBytes(&firstLBA, 8);
210 ReverseBytes(&lastLBA, 8);
211 ReverseBytes(&attributes, 8);
212} // GPTPart::ReverseBytes()
213
214/****************************************
215 * Functions requiring user interaction *
216 ****************************************/
217
srs56946699b012010-02-04 00:55:30 -0500218// Change the type code on the partition. Also changes the name if the original
219// name is the generic one for the partition type.
srs56940a697312010-01-28 21:10:52 -0500220void GPTPart::ChangeType(void) {
221 char line[255];
srs569464cbd172011-03-01 22:03:54 -0500222 int changeName;
srs569482f3f0b2010-09-22 10:50:24 -0400223 PartType tempType = (GUIDData) "00000000-0000-0000-0000-000000000000";
srs56940a697312010-01-28 21:10:52 -0500224
srs569464cbd172011-03-01 22:03:54 -0500225 changeName = (GetDescription() == GetTypeName());
srs569455d92612010-03-07 22:16:07 -0500226
srs56946699b012010-02-04 00:55:30 -0500227 cout << "Current type is '" << GetTypeName() << "'\n";
srs569482f3f0b2010-09-22 10:50:24 -0400228 do {
229 cout << "Hex code or GUID (L to show codes, Enter = 0700): ";
srs5694bf8950c2011-03-12 01:23:12 -0500230 ReadCString(line, 255);
srs569482f3f0b2010-09-22 10:50:24 -0400231 if ((line[0] == 'L') || (line[0] == 'l')) {
srs56946699b012010-02-04 00:55:30 -0500232 partitionType.ShowAllTypes();
srs569482f3f0b2010-09-22 10:50:24 -0400233 } else {
234 if (strlen(line) == 1)
235 tempType = 0x0700;
236 else
237 tempType = line;
238 } // if/else
239 } while (tempType == (GUIDData) "00000000-0000-0000-0000-000000000000");
240 partitionType = tempType;
srs56946699b012010-02-04 00:55:30 -0500241 cout << "Changed type of partition to '" << partitionType.TypeName() << "'\n";
242 if (changeName) {
243 SetDefaultDescription();
244 } // if
srs56940a697312010-01-28 21:10:52 -0500245} // GPTPart::ChangeType()