~ubuntu-branches/ubuntu/utopic/gdisk/utopic-proposed

« back to all changes in this revision

Viewing changes to gptpart.cc

  • Committer: Bazaar Package Importer
  • Author(s): Guillaume Delacour
  • Date: 2009-12-01 21:04:25 UTC
  • Revision ID: james.westby@ubuntu.com-20091201210425-lznlvi764r2dwkf8
Tags: upstream-0.5.1
ImportĀ upstreamĀ versionĀ 0.5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
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
//
 
12
// 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.
 
14
 
 
15
#define __STDC_LIMIT_MACROS
 
16
#define __STDC_CONSTANT_MACROS
 
17
 
 
18
#include <stdio.h>
 
19
#include <string.h>
 
20
#include "gptpart.h"
 
21
#include "attributes.h"
 
22
 
 
23
using namespace std;
 
24
 
 
25
PartTypes GPTPart::typeHelper;
 
26
 
 
27
GPTPart::GPTPart(void) {
 
28
   int i;
 
29
 
 
30
   for (i = 0; i < NAME_SIZE; i++)
 
31
      name[i] = '\0';
 
32
} // Default constructor
 
33
 
 
34
GPTPart::~GPTPart(void) {
 
35
} // destructor
 
36
 
 
37
// Return partition's name field
 
38
unsigned char* GPTPart::GetName(unsigned char* ref) {
 
39
   if (ref == NULL)
 
40
      ref = (unsigned char*) malloc(NAME_SIZE * sizeof (unsigned char));
 
41
   strcpy((char*) ref, (char*) name);
 
42
   return ref;
 
43
} // GPTPart::GetName()
 
44
 
 
45
// Return the gdisk-specific two-byte hex code for the partition
 
46
uint16_t GPTPart::GetHexType(void) {
 
47
   return typeHelper.GUIDToID(partitionType);
 
48
} // GPTPart::GetHexType()
 
49
 
 
50
// Return a plain-text description of the partition type (e.g., "Linux/Windows
 
51
// data" or "Linux swap").
 
52
char* GPTPart::GetNameType(char* theName) {
 
53
   return typeHelper.GUIDToName(partitionType, theName);
 
54
} // GPTPart::GetNameType()
 
55
 
 
56
// Compute and return the partition's length (or 0 if the end is incorrectly
 
57
// set before the beginning).
 
58
uint64_t GPTPart::GetLengthLBA(void) {
 
59
   uint64_t length = 0;
 
60
   if (firstLBA <= lastLBA)
 
61
      length = lastLBA - firstLBA + UINT64_C(1);
 
62
   return length;
 
63
} // GPTPart::GetLengthLBA()
 
64
 
 
65
GPTPart & GPTPart::operator=(const GPTPart & orig) {
 
66
   int i;
 
67
 
 
68
   partitionType = orig.partitionType;
 
69
   uniqueGUID = orig.uniqueGUID;
 
70
   firstLBA = orig.firstLBA;
 
71
   lastLBA = orig.lastLBA;
 
72
   attributes = orig.attributes;
 
73
   for (i = 0; i < NAME_SIZE; i++)
 
74
      name[i] = orig.name[i];
 
75
   return *this;
 
76
} // assignment operator
 
77
 
 
78
// Sets the unique GUID to a value of 0 or a random value,
 
79
// depending on the parameter: 0 = 0, anything else = random
 
80
void GPTPart::SetUniqueGUID(int zeroOrRandom) {
 
81
   if (zeroOrRandom == 0) {
 
82
      uniqueGUID.data1 = 0;
 
83
      uniqueGUID.data2 = 0;
 
84
   } else {
 
85
      // rand() is only 32 bits on 32-bit systems, so multiply together to
 
86
      // fill a 64-bit value.
 
87
      uniqueGUID.data1 = (uint64_t) rand() * (uint64_t) rand();
 
88
      uniqueGUID.data2 = (uint64_t) rand() * (uint64_t) rand();
 
89
   }
 
90
} // GPTPart::SetUniqueGUID()
 
91
 
 
92
// Blank (delete) a single partition
 
93
void GPTPart::BlankPartition(void) {
 
94
   int j;
 
95
   GUIDData zeroGUID;
 
96
 
 
97
   zeroGUID.data1 = 0;
 
98
   zeroGUID.data2 = 0;
 
99
   uniqueGUID = zeroGUID;
 
100
   partitionType = zeroGUID;
 
101
   firstLBA = 0;
 
102
   lastLBA = 0;
 
103
   attributes = 0;
 
104
   for (j = 0; j < NAME_SIZE; j++)
 
105
      name[j] = '\0';
 
106
} // GPTPart::BlankPartition
 
107
 
 
108
// Returns 1 if the two partitions overlap, 0 if they don't
 
109
int GPTPart::DoTheyOverlap(GPTPart* other) {
 
110
   int theyDo = 0;
 
111
 
 
112
   // Don't bother checking unless these are defined (both start and end points
 
113
   // are 0 for undefined partitions, so just check the start points)
 
114
   if ((firstLBA != 0) && (other->firstLBA != 0)) {
 
115
      if ((firstLBA < other->lastLBA) && (lastLBA >= other->firstLBA))
 
116
         theyDo = 1;
 
117
      if ((other->firstLBA < lastLBA) && (other->lastLBA >= firstLBA))
 
118
         theyDo = 1;
 
119
   } // if
 
120
   return (theyDo);
 
121
} // GPTPart::DoTheyOverlap()
 
122
 
 
123
// Reverse the bytes of integral data types; used on big-endian systems.
 
124
void GPTPart::ReversePartBytes(void) {
 
125
   ReverseBytes(&partitionType.data1, 8);
 
126
   ReverseBytes(&partitionType.data2, 8);
 
127
   ReverseBytes(&uniqueGUID.data1, 8);
 
128
   ReverseBytes(&uniqueGUID.data2, 8);
 
129
   ReverseBytes(&firstLBA, 8);
 
130
   ReverseBytes(&lastLBA, 8);
 
131
   ReverseBytes(&attributes, 8);
 
132
} // GPTPart::ReverseBytes()
 
133
 
 
134
// Display summary information; does nothing if the partition is empty.
 
135
void GPTPart::ShowSummary(int partNum, uint32_t blockSize) {
 
136
   char sizeInSI[255];
 
137
   int j = 0;
 
138
 
 
139
   if (firstLBA != 0) {
 
140
      BytesToSI(blockSize * (lastLBA - firstLBA + 1), sizeInSI);
 
141
      printf("%4d  %14lu  %14lu", partNum + 1, (unsigned long) firstLBA,
 
142
             (unsigned long) lastLBA);
 
143
      printf("   %-10s  %04X  ", sizeInSI,
 
144
             typeHelper.GUIDToID(partitionType));
 
145
      while ((name[j] != '\0') && (j < 44)) {
 
146
         printf("%c", name[j]);
 
147
         j += 2;
 
148
      } // while
 
149
      printf("\n");
 
150
   } // if
 
151
} // GPTPart::ShowSummary()
 
152
 
 
153
// Show detailed partition information. Does nothing if the partition is
 
154
// empty (as determined by firstLBA being 0).
 
155
void GPTPart::ShowDetails(uint32_t blockSize) {
 
156
   char temp[255];
 
157
   int i;
 
158
   uint64_t size;
 
159
 
 
160
   if (firstLBA != 0) {
 
161
      printf("Partition GUID code: %s ", GUIDToStr(partitionType, temp));
 
162
      printf("(%s)\n", typeHelper.GUIDToName(partitionType, temp));
 
163
      printf("Partition unique GUID: %s\n", GUIDToStr(uniqueGUID, temp));
 
164
 
 
165
      printf("First sector: %llu (at %s)\n", (unsigned long long) firstLBA,
 
166
             BytesToSI(firstLBA * blockSize, temp));
 
167
      printf("Last sector: %llu (at %s)\n", (unsigned long long) lastLBA,
 
168
             BytesToSI(lastLBA * blockSize, temp));
 
169
      size = (lastLBA - firstLBA + 1);
 
170
      printf("Partition size: %llu sectors (%s)\n", (unsigned long long)
 
171
             size, BytesToSI(size * ((uint64_t) blockSize), temp));
 
172
      printf("Attribute flags: %016llx\n", (unsigned long long) attributes);
 
173
      printf("Partition name: ");
 
174
      i = 0;
 
175
      while ((name[i] != '\0') && (i < NAME_SIZE)) {
 
176
         printf("%c", name[i]);
 
177
         i += 2;
 
178
      } // while
 
179
      printf("\n");
 
180
   }  // if
 
181
} // GPTPart::ShowDetails()
 
182
 
 
183
/****************************************
 
184
 * Functions requiring user interaction *
 
185
 ****************************************/
 
186
 
 
187
// Change the type code on the partition.
 
188
void GPTPart::ChangeType(void) {
 
189
   char typeName[255], line[255];
 
190
   int typeNum = 0xFFFF;
 
191
//   uint16_t typeNum = 0xFFFF;
 
192
   GUIDData newType;
 
193
 
 
194
   printf("Current type is '%s'\n", GetNameType(line));
 
195
//   printf("Current type is '%s'\n", typeHelper.GUIDToName(partitionType, typeName));
 
196
   while ((!typeHelper.Valid(typeNum)) && (typeNum != 0)) {
 
197
      printf("Hex code (L to show codes, 0 to enter raw code): ");
 
198
      fgets(line, 255, stdin);
 
199
      sscanf(line, "%X", &typeNum);
 
200
      if ((line[0] == 'L') || (line[0] == 'l'))
 
201
         typeHelper.ShowTypes();
 
202
   } // while
 
203
   if (typeNum != 0) // user entered a code, so convert it
 
204
      newType = typeHelper.IDToGUID((uint16_t) typeNum);
 
205
   else // user wants to enter the GUID directly, so do that
 
206
      newType = GetGUID();
 
207
   partitionType = newType;
 
208
   printf("Changed system type of partition to '%s'\n",
 
209
          typeHelper.GUIDToName(partitionType, typeName));
 
210
} // GPTPart::ChangeType()
 
211
 
 
212
// Set the name for a partition to theName, or prompt for a name if
 
213
// theName is a NULL pointer. Note that theName is a standard C-style
 
214
// string, although the GUID partition definition requires a UTF-16LE
 
215
// string. This function creates a simple-minded copy for this.
 
216
void GPTPart::SetName(unsigned char* theName) {
 
217
   char newName[NAME_SIZE]; // New name
 
218
   int i;
 
219
 
 
220
   // Blank out new name string, just to be on the safe side....
 
221
   for (i = 0; i < NAME_SIZE; i++)
 
222
      newName[i] = '\0';
 
223
 
 
224
   if (theName == NULL) { // No name specified, so get one from the user
 
225
      printf("Enter name: ");
 
226
      fgets(newName, NAME_SIZE / 2, stdin);
 
227
 
 
228
      // Input is likely to include a newline, so remove it....
 
229
      i = strlen(newName);
 
230
      if (newName[i - 1] == '\n')
 
231
         newName[i - 1] = '\0';
 
232
   } else {
 
233
      strcpy(newName, (char*) theName);
 
234
   } // if
 
235
 
 
236
   // Copy the C-style ASCII string from newName into a form that the GPT
 
237
   // table will accept....
 
238
   for (i = 0; i < NAME_SIZE; i++) {
 
239
      if ((i % 2) == 0) {
 
240
         name[i] = newName[(i / 2)];
 
241
      } else {
 
242
         name[i] = '\0';
 
243
      } // if/else
 
244
   } // for
 
245
} // GPTPart::SetName()
 
246
 
 
247
/***********************************
 
248
 * Non-class but related functions *
 
249
 ***********************************/
 
250
 
 
251
// Recursive quick sort algorithm for GPT partitions. Note that if there
 
252
// are any empties in the specified range, they'll be sorted to the
 
253
// start, resulting in a sorted set of partitions that begins with
 
254
// partition 2, 3, or higher.
 
255
void QuickSortGPT(GPTPart* partitions, int start, int finish) {
 
256
   uint64_t starterValue; // starting location of median partition
 
257
   int left, right;
 
258
   GPTPart temp;
 
259
 
 
260
   left = start;
 
261
   right = finish;
 
262
   starterValue = partitions[(start + finish) / 2].GetFirstLBA();
 
263
   do {
 
264
      while (partitions[left].GetFirstLBA() < starterValue)
 
265
         left++;
 
266
      while (partitions[right].GetFirstLBA() > starterValue)
 
267
         right--;
 
268
      if (left <= right) {
 
269
         temp = partitions[left];
 
270
         partitions[left] = partitions[right];
 
271
         partitions[right] = temp;
 
272
         left++;
 
273
         right--;
 
274
      } // if
 
275
   } while (left <= right);
 
276
   if (start < right) QuickSortGPT(partitions, start, right);
 
277
   if (finish > left) QuickSortGPT(partitions, left, finish);
 
278
} // QuickSortGPT()
 
279