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

« back to all changes in this revision

Viewing changes to parttypes.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
// parttypes.cc
 
2
// Class to manage partition type codes -- a slight variant on MBR type
 
3
// codes, GUID type codes, and associated names.
 
4
 
 
5
/* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
 
6
  under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
 
7
 
 
8
#define __STDC_LIMIT_MACROS
 
9
#define __STDC_CONSTANT_MACROS
 
10
 
 
11
#include <string.h>
 
12
#include <stdint.h>
 
13
#include <stdio.h>
 
14
#include "parttypes.h"
 
15
 
 
16
using namespace std;
 
17
 
 
18
int PartTypes::numInstances = 0;
 
19
AType* PartTypes::allTypes = NULL;
 
20
AType* PartTypes::lastType = NULL;
 
21
 
 
22
// Constructor. Its main task is to initialize the data list, but only
 
23
// if this is the first instance, since it's a static linked list.
 
24
// Partition type codes are MBR type codes multiplied by 0x0100, with
 
25
// additional related codes taking on following numbers. For instance,
 
26
// the FreeBSD disklabel code in MBR is 0xa5; here, it's 0xa500, with
 
27
// additional FreeBSD codes being 0xa501, 0xa502, and so on. This gives
 
28
// related codes similar numbers and (given appropriate entry positions
 
29
// in the linked list) keeps them together in the listings generated
 
30
// by typing "L" at the main gdisk menu.
 
31
// See http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
 
32
// for a list of MBR partition type codes.
 
33
PartTypes::PartTypes(void) {
 
34
 
 
35
   numInstances++;
 
36
   if (numInstances == 1) {
 
37
 
 
38
      // Start with the "unused entry," which should normally appear only
 
39
      // on empty partition table entries....
 
40
      AddType(0x0000, UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000000),
 
41
              "Unused entry", 0);
 
42
 
 
43
      // DOS/Windows partition types, which confusingly Linux also uses in GPT
 
44
      AddType(0x0100, UINT64_C(0x4433B9E5EBD0A0A2), UINT64_C(0xC79926B7B668C087),
 
45
              "Linux/Windows data", 0); // FAT-12
 
46
      AddType(0x0400, UINT64_C(0x4433B9E5EBD0A0A2), UINT64_C(0xC79926B7B668C087),
 
47
              "Linux/Windows data", 0); // FAT-16 < 32M
 
48
      AddType(0x0600, UINT64_C(0x4433B9E5EBD0A0A2), UINT64_C(0xC79926B7B668C087),
 
49
              "Linux/Windows data", 0); // FAT-16
 
50
      AddType(0x0700, UINT64_C(0x4433B9E5EBD0A0A2), UINT64_C(0xC79926B7B668C087),
 
51
              "Linux/Windows data", 1); // NTFS (or could be HPFS)
 
52
      AddType(0x0b00, UINT64_C(0x4433B9E5EBD0A0A2), UINT64_C(0xC79926B7B668C087),
 
53
              "Linux/Windows data", 0); // FAT-32
 
54
      AddType(0x0c00, UINT64_C(0x4433B9E5EBD0A0A2), UINT64_C(0xC79926B7B668C087),
 
55
              "Linux/Windows data", 0); // FAT-32 LBA
 
56
      AddType(0x0c01, UINT64_C(0x4DB80B5CE3C9E316), UINT64_C(0xAE1502F02DF97D81),
 
57
              "Microsoft Reserved"); // Microsoft reserved
 
58
      AddType(0x0e00, UINT64_C(0x4433B9E5EBD0A0A2), UINT64_C(0xC79926B7B668C087),
 
59
              "Linux/Windows data", 0); // FAT-16 LBA
 
60
      AddType(0x1100, UINT64_C(0x4433B9E5EBD0A0A2), UINT64_C(0xC79926B7B668C087),
 
61
              "Linux/Windows data", 0); // Hidden FAT-12
 
62
      AddType(0x1400, UINT64_C(0x4433B9E5EBD0A0A2), UINT64_C(0xC79926B7B668C087),
 
63
              "Linux/Windows data", 0); // Hidden FAT-16 < 32M
 
64
      AddType(0x1600, UINT64_C(0x4433B9E5EBD0A0A2), UINT64_C(0xC79926B7B668C087),
 
65
              "Linux/Windows data", 0); // Hidden FAT-16
 
66
      AddType(0x1700, UINT64_C(0x4433B9E5EBD0A0A2), UINT64_C(0xC79926B7B668C087),
 
67
              "Linux/Windows data", 0); // Hidden NTFS (or could be HPFS)
 
68
      AddType(0x1b00, UINT64_C(0x4433B9E5EBD0A0A2), UINT64_C(0xC79926B7B668C087),
 
69
              "Linux/Windows data", 0); // Hidden FAT-32
 
70
      AddType(0x1c00, UINT64_C(0x4433B9E5EBD0A0A2), UINT64_C(0xC79926B7B668C087),
 
71
              "Linux/Windows data", 0); // Hidden FAT-32 LBA
 
72
      AddType(0x1e00, UINT64_C(0x4433B9E5EBD0A0A2), UINT64_C(0xC79926B7B668C087),
 
73
              "Linux/Windows data", 0); // Hidden FAT-16 LBA
 
74
      AddType(0x2700, UINT64_C(0x4D4006D1DE94BBA4), UINT64_C(0xACD67901D5BF6AA1),
 
75
              "Windows RE"); // Windows RE
 
76
      AddType(0x4200, UINT64_C(0x4F621431Af9B60A0), UINT64_C(0xAD694A71113368BC),
 
77
              "Windows LDM data"); // Logical disk manager
 
78
      AddType(0x4201, UINT64_C(0x42E07E8F5808C8AA), UINT64_C(0xB3CF3404E9E1D285),
 
79
              "Windows LDM metadata"); // Logical disk manager
 
80
 
 
81
      // Linux-specific partition types....
 
82
      AddType(0x8200, UINT64_C(0x43C4A4AB0657FD6D), UINT64_C(0x4F4F4BC83309E584),
 
83
              "Linux swap"); // Linux swap (or could be Solaris)
 
84
      AddType(0x8300, UINT64_C(0x4433B9E5EBD0A0A2), UINT64_C(0xC79926B7B668C087),
 
85
              "Linux/Windows data", 0); // Linux native
 
86
      AddType(0x8301, UINT64_C(0x60C000078DA63339), UINT64_C(0x080923C83A0836C4),
 
87
              "Linux Reserved"); // Linux reserved
 
88
      AddType(0x8e00, UINT64_C(0x44C2F507E6D6D379), UINT64_C(0x28F93D2A8F233CA2),
 
89
              "Linux LVM"); // Linux LVM
 
90
 
 
91
      // FreeBSD partition types....
 
92
      // Note: Rather than extract FreeBSD disklabel data, convert FreeBSD
 
93
      // partitions in-place, and let FreeBSD sort out the details....
 
94
      AddType(0xa500, UINT64_C(0x11D66ECF516E7CB4), UINT64_C(0x2B71092D0200F88F),
 
95
              "FreeBSD disklabel"); // FreeBSD disklabel
 
96
      AddType(0xa501, UINT64_C(0x11DC7F4183BD6B9D), UINT64_C(0x0F4FB86015000BBE),
 
97
              "FreeBSD boot"); // FreeBSD boot
 
98
      AddType(0xa502, UINT64_C(0x11D66ECF516E7CB5), UINT64_C(0x2B71092D0200F88F),
 
99
              "FreeBSD swap"); // FreeBSD swap
 
100
      AddType(0xa503, UINT64_C(0x11D66ECF516E7CB6), UINT64_C(0x2B71092D0200F88F),
 
101
              "FreeBSD UFS"); // FreeBSD UFS
 
102
      AddType(0xa504, UINT64_C(0x11D66ECF516E7CBA), UINT64_C(0x2B71092D0200F88F),
 
103
              "FreeBSD ZFS"); // FreeBSD ZFS
 
104
      AddType(0xa505, UINT64_C(0x11D66ECF516E7CB8), UINT64_C(0x2B71092D0200F88F),
 
105
              "FreeBSD Vinum/RAID"); // FreeBSD Vinum
 
106
 
 
107
      // A MacOS partition type, separated from others by NetBSD partition types...
 
108
      AddType(0xa800, UINT64_C(0x11AA000055465300), UINT64_C(0xACEC4365300011AA),
 
109
              "Apple UFS"); // MacOS X
 
110
 
 
111
      // NetBSD partition types. Note that the main entry sets it up as a
 
112
      // FreeBSD disklabel. I'm not 100% certain this is the correct behavior.
 
113
      AddType(0xa900, UINT64_C(0x11D66ECF516E7CB4), UINT64_C(0x2B71092D0200F88F),
 
114
              "FreeBSD disklabel", 0); // NetBSD disklabel
 
115
      AddType(0xa901, UINT64_C(0x11DCB10E49F48D32), UINT64_C(0x489687D119009BB9),
 
116
              "NetBSD swap");
 
117
      AddType(0xa902, UINT64_C(0x11DCB10E49F48D5A), UINT64_C(0x489687D119009BB9),
 
118
              "NetBSD FFS");
 
119
      AddType(0xa903, UINT64_C(0x11DCB10E49F48D82), UINT64_C(0x489687D119009BB9),
 
120
              "NetBSD LFS");
 
121
      AddType(0xa903, UINT64_C(0x11DCB10E49F48DAA), UINT64_C(0x489687D119009BB9),
 
122
              "NetBSD RAID");
 
123
      AddType(0xa904, UINT64_C(0x11DCB10F2DB519C4), UINT64_C(0x489687D119009BB9),
 
124
              "NetBSD concatenated");
 
125
      AddType(0xa905, UINT64_C(0x11DCB10F2DB519EC), UINT64_C(0x489687D119009BB9),
 
126
              "NetBSD encrypted");
 
127
 
 
128
      // MacOS partition types (See also 0xa800, above)....
 
129
      AddType(0xab00, UINT64_C(0x11AA0000426F6F74), UINT64_C(0xACEC4365300011AA),
 
130
              "Apple boot"); // MacOS X
 
131
      AddType(0xaf00, UINT64_C(0x11AA000048465300), UINT64_C(0xACEC4365300011AA),
 
132
              "Apple HFS/HFS+"); // MacOS X
 
133
      AddType(0xaf01, UINT64_C(0x11AA000052414944), UINT64_C(0xACEC4365300011AA),
 
134
              "Apple RAID"); // MacOS X
 
135
      AddType(0xaf02, UINT64_C(0x11AA5F4F52414944), UINT64_C(0xACEC4365300011AA),
 
136
              "Apple RAID offline"); // MacOS X
 
137
      AddType(0xaf03, UINT64_C(0x11AA6C004C616265), UINT64_C(0xACEC4365300011AA),
 
138
              "Apple label"); // MacOS X
 
139
      AddType(0xaf04, UINT64_C(0x11AA76655265636F), UINT64_C(0xACEC4365300011AA),
 
140
              "AppleTV recovery"); // MacOS X
 
141
 
 
142
      // Solaris partition types (one of which is shared with MacOS)
 
143
      AddType(0xbe00, UINT64_C(0x11B21DD26A82CB45), UINT64_C(0x316673200008A699),
 
144
              "Solaris boot"); // Solaris boot
 
145
      AddType(0xbf00, UINT64_C(0x11B21DD26a85CF4D), UINT64_C(0x316673200008A699),
 
146
              "Solaris root"); // Solaris root
 
147
      AddType(0xbf01, UINT64_C(0x11B21DD26A898CC3), UINT64_C(0x316673200008A699),
 
148
              "Solaris /usr & Mac ZFS"); // MacOS X & Solaris
 
149
      AddType(0xbf02, UINT64_C(0x11B21DD26A87C46F), UINT64_C(0x316673200008A699),
 
150
              "Solaris swap");
 
151
      AddType(0xbf03, UINT64_C(0x11B21DD26A8B642B), UINT64_C(0x316673200008A699),
 
152
              "Solaris backup");
 
153
      AddType(0xbf04, UINT64_C(0x11B21DD26A8EF2E9), UINT64_C(0x316673200008A699),
 
154
              "Solaris /var");
 
155
      AddType(0xbf05, UINT64_C(0x11B21DD26A90BA39), UINT64_C(0x316673200008A699),
 
156
              "Solaris /home");
 
157
      AddType(0xbf05, UINT64_C(0x11B21DD26A9283A5), UINT64_C(0x316673200008A699),
 
158
              "Solaris EFI_ALTSCTR");
 
159
      AddType(0xbf06, UINT64_C(0x11B21DD26A945A3B), UINT64_C(0x316673200008A699),
 
160
              "Solaris Reserved 1");
 
161
      AddType(0xbf07, UINT64_C(0x11B21DD26A9630D1), UINT64_C(0x316673200008A699),
 
162
              "Solaris Reserved 2");
 
163
      AddType(0xbf08, UINT64_C(0x11B21DD26A980767), UINT64_C(0x316673200008A699),
 
164
              "Solaris Reserved 3");
 
165
      AddType(0xbf09, UINT64_C(0x11B21DD26A96237F), UINT64_C(0x316673200008A699),
 
166
              "Solaris Reserved 4");
 
167
      AddType(0xbf0a, UINT64_C(0x11B21DD26A8D2AC7), UINT64_C(0x316673200008A699),
 
168
              "Solaris Reserved 5");
 
169
 
 
170
      // I can find no MBR equivalents for these, but they're on the
 
171
      // Wikipedia page for GPT, so here we go....
 
172
      AddType(0xc001, UINT64_C(0x11D33AEB75894C1E), UINT64_C(0x000000A0037BC1B7),
 
173
              "HP-UX data");
 
174
      AddType(0xc002, UINT64_C(0x11D632E3E2A1E728), UINT64_C(0x000000A0037B82A6),
 
175
              "HP-UX service");
 
176
 
 
177
      // EFI system and related partitions
 
178
      AddType(0xEF00, UINT64_C(0x11d2f81fc12a7328), UINT64_C(0x3bc93ec9a0004bba),
 
179
              "EFI System"); // EFI System (parted marks Linux boot
 
180
                             // partitions like this)
 
181
      AddType(0xEF01, UINT64_C(0x11d333e7024dee41), UINT64_C(0x9FF381C70800699d),
 
182
              "MBR partition scheme"); // Used to nest an MBR table on a GPT disk
 
183
      AddType(0xEF02, UINT64_C(0x6E6F644921686148), UINT64_C(0x4946456465654E74),
 
184
              "BIOS boot partition"); //
 
185
 
 
186
      // A straggler Linux partition type....
 
187
      AddType(0xfd00, UINT64_C(0x4D3B05FCA19D880F), UINT64_C(0x1E91840F3F7406A0),
 
188
              "Linux RAID"); // Linux RAID
 
189
   } // if
 
190
} // default constructor
 
191
 
 
192
PartTypes::~PartTypes(void) {
 
193
   AType* tempType;
 
194
 
 
195
   numInstances--;
 
196
   if (numInstances == 0) {
 
197
      while (allTypes != NULL) {
 
198
         tempType = allTypes;
 
199
         allTypes = allTypes->next;
 
200
         delete tempType;
 
201
      } // while
 
202
   } // if
 
203
} // destructor
 
204
 
 
205
// Add a single type to the linked list of types. Returns 1 if operation
 
206
// succeeds, 0 otherwise
 
207
int PartTypes::AddType(uint16_t mbrType, uint64_t guidData1, uint64_t guidData2,
 
208
                       const char* n, int toDisplay) {
 
209
   AType* tempType;
 
210
   int allOK = 1;
 
211
 
 
212
   tempType = new AType;
 
213
   if (tempType != NULL) {
 
214
      tempType->MBRType = mbrType;
 
215
      tempType->GUIDType.data1 = guidData1;
 
216
      tempType->GUIDType.data2 = guidData2;
 
217
      strncpy(tempType->name, n, PNAME_SIZE);
 
218
      tempType->display = toDisplay;
 
219
      tempType->next = NULL;
 
220
      if (allTypes == NULL) { // first entry
 
221
         allTypes = tempType;
 
222
      } else {
 
223
         lastType->next = tempType;
 
224
      } // if/else
 
225
      lastType = tempType;
 
226
   } else {
 
227
      allOK = 0;
 
228
   } // if/else
 
229
   return allOK;
 
230
} // PartTypes::AddType()
 
231
 
 
232
// Displays the available types and my extended MBR codes for same....
 
233
// Note: This function assumes an 80-column display. On wider displays,
 
234
// it stops at under 80 columns; on narrower displays, lines will wrap
 
235
// in an ugly way.
 
236
void PartTypes::ShowTypes(void) {
 
237
   int colCount = 1; // column count
 
238
   AType* thisType = allTypes;
 
239
   char tempStr[20];
 
240
 
 
241
   while (thisType != NULL) {
 
242
      if (thisType->display == 1) { // show it
 
243
         strncpy(tempStr, thisType->name, 19);
 
244
         tempStr[19] = '\0';
 
245
         printf("%04x %-19s  ", thisType->MBRType, tempStr);
 
246
         if ((colCount % 3) == 0)
 
247
            printf("\n");
 
248
         colCount++;
 
249
      } // if
 
250
      thisType = thisType->next;
 
251
   } // while
 
252
   printf("\n");
 
253
} // PartTypes::ShowTypes()
 
254
 
 
255
// Returns 1 if code is a valid extended MBR code, 0 if it's not
 
256
int PartTypes::Valid(uint16_t code) {
 
257
   AType* thisType = allTypes;
 
258
   int found = 0;
 
259
 
 
260
   while ((thisType != NULL) && (!found)) {
 
261
      if (thisType->MBRType == code) {
 
262
         found = 1;
 
263
      } // if
 
264
      thisType = thisType->next;
 
265
   } // while
 
266
   return found;
 
267
} // PartTypes::Valid()
 
268
 
 
269
// Convert a GUID code to a name.
 
270
char* PartTypes::GUIDToName(struct GUIDData typeCode, char typeName[]) {
 
271
   AType* theItem = allTypes;
 
272
   int found = 0;
 
273
 
 
274
   while ((theItem != NULL) && (!found)) {
 
275
      if ((theItem->GUIDType.data1 == typeCode.data1) &&
 
276
          (theItem->GUIDType.data2 == typeCode.data2)) { // found it!
 
277
         strcpy(typeName, theItem->name);
 
278
         found = 1;
 
279
      } else {
 
280
         theItem = theItem->next;
 
281
      } // if/else
 
282
   } // while
 
283
   if (!found) {
 
284
      strcpy(typeName, (char*) "Unknown");
 
285
   } // if (!found)
 
286
   return typeName;
 
287
} // PartTypes::GUIDToName()
 
288
 
 
289
// This function takes a variant of the MBR partition type code and
 
290
// converts it to a GUID type code
 
291
struct GUIDData PartTypes::IDToGUID(uint16_t ID) {
 
292
   AType* theItem = allTypes;
 
293
   int found = 0;
 
294
   struct GUIDData theGUID;
 
295
 
 
296
   // Start by assigning a default GUID for the return value. Done
 
297
   // "raw" to avoid the necessity for a recursive call and the
 
298
   // remote possibility of an infinite recursive loop that this
 
299
   // approach would present....
 
300
   theGUID.data1 = UINT64_C(0x4433B9E5EBD0A0A2);
 
301
   theGUID.data2 = UINT64_C(0xC79926B7B668C087);
 
302
 
 
303
   // Now search the type list for a match to the ID....
 
304
   while ((theItem != NULL) && (!found)) {
 
305
      if (theItem->MBRType == ID)  { // found it!
 
306
         theGUID = theItem->GUIDType;
 
307
         found = 1;
 
308
      } else {
 
309
         theItem = theItem->next;
 
310
      } // if/else
 
311
   } // while
 
312
   if (!found) {
 
313
      printf("Exact type match not found for type code %04X; assigning type code for\n'Linux/Windows data'\n",
 
314
             ID);
 
315
   } // if (!found)
 
316
   return theGUID;
 
317
} // PartTypes::IDToGUID()
 
318
 
 
319
// Convert a GUID to a 16-bit variant of the MBR ID number.
 
320
// Note that this function ignores entries for which the display variable
 
321
// is set to 0. This enables control of which values get returned when
 
322
// there are multiple possibilities, but opens the algorithm up to the
 
323
// potential for problems should the data in the list be bad.
 
324
uint16_t PartTypes::GUIDToID(struct GUIDData typeCode) {
 
325
   AType* theItem = allTypes;
 
326
   int found = 0;
 
327
   uint16_t theID = 0xFFFF;
 
328
 
 
329
   while ((theItem != NULL) && (!found)) {
 
330
      if ((theItem->GUIDType.data1 == typeCode.data1) &&
 
331
          (theItem->GUIDType.data2 == typeCode.data2) &&
 
332
          (theItem->display == 1)) { // found it!
 
333
         theID = theItem->MBRType;
 
334
         found = 1;
 
335
      } else {
 
336
         theItem = theItem->next;
 
337
      } // if/else
 
338
   } // while
 
339
   if (!found) {
 
340
      theID = 0xFFFF;
 
341
   } // if (!found)
 
342
   return theID;
 
343
} // PartTypes::GUIDToID()