blob: 99de5002ece92cd771babcd6a4eef2daffb1ed49 [file] [log] [blame]
srs5694e7b4ff92009-08-18 13:16:10 -04001// parttypes.cc
2// Class to manage partition type codes -- a slight variant on MBR type
3// codes, GUID type codes, and associated names.
4
srs569464cbd172011-03-01 22:03:54 -05005/* This program is copyright (c) 2009-2011 by Roderick W. Smith. It is distributed
srs5694221e0872009-08-29 15:00:31 -04006 under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
7
srs5694e7b4ff92009-08-18 13:16:10 -04008#define __STDC_LIMIT_MACROS
9#define __STDC_CONSTANT_MACROS
10
11#include <string.h>
12#include <stdint.h>
13#include <stdio.h>
srs5694fed16d02010-01-27 23:03:40 -050014#include <iostream>
srs5694e7b4ff92009-08-18 13:16:10 -040015#include "parttypes.h"
16
17using namespace std;
18
srs56946699b012010-02-04 00:55:30 -050019int PartType::numInstances = 0;
20AType* PartType::allTypes = NULL;
21AType* PartType::lastType = NULL;
srs5694e7b4ff92009-08-18 13:16:10 -040022
23// Constructor. Its main task is to initialize the data list, but only
24// if this is the first instance, since it's a static linked list.
25// Partition type codes are MBR type codes multiplied by 0x0100, with
26// additional related codes taking on following numbers. For instance,
27// the FreeBSD disklabel code in MBR is 0xa5; here, it's 0xa500, with
28// additional FreeBSD codes being 0xa501, 0xa502, and so on. This gives
29// related codes similar numbers and (given appropriate entry positions
30// in the linked list) keeps them together in the listings generated
31// by typing "L" at the main gdisk menu.
srs56946699b012010-02-04 00:55:30 -050032PartType::PartType(void) : GUIDData() {
srs5694e7b4ff92009-08-18 13:16:10 -040033 numInstances++;
34 if (numInstances == 1) {
srs56946699b012010-02-04 00:55:30 -050035 AddAllTypes();
srs5694e7b4ff92009-08-18 13:16:10 -040036 } // if
37} // default constructor
38
srs56946699b012010-02-04 00:55:30 -050039PartType::PartType(const PartType & orig) : GUIDData(orig) {
40 numInstances++;
41 if (numInstances == 1) { // should never happen; just being paranoid
42 AddAllTypes();
43 } // if
44} // PartType copy constructor
45
46PartType::PartType(const GUIDData & orig) : GUIDData(orig) {
47 numInstances++;
48 if (numInstances == 1) {
49 AddAllTypes();
50 } // if
51} // PartType copy constructor
52
53PartType::~PartType(void) {
srs5694e7b4ff92009-08-18 13:16:10 -040054 AType* tempType;
55
56 numInstances--;
57 if (numInstances == 0) {
58 while (allTypes != NULL) {
59 tempType = allTypes;
60 allTypes = allTypes->next;
61 delete tempType;
62 } // while
63 } // if
64} // destructor
65
srs56946699b012010-02-04 00:55:30 -050066// Add all partition type codes to the internal linked-list structure.
67// Used by constructors.
srs569455d92612010-03-07 22:16:07 -050068// See http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
69// for a list of MBR partition type codes.
srs56946699b012010-02-04 00:55:30 -050070void PartType::AddAllTypes(void) {
71 // Start with the "unused entry," which should normally appear only
72 // on empty partition table entries....
73 AddType(0x0000, "00000000-0000-0000-0000-000000000000", "Unused entry", 0);
74
75 // DOS/Windows partition types, which confusingly Linux also uses in GPT
76 AddType(0x0100, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 0); // FAT-12
77 AddType(0x0400, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 0); // FAT-16 < 32M
78 AddType(0x0600, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 0); // FAT-16
79 AddType(0x0700, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 1); // NTFS (or HPFS)
80 AddType(0x0b00, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 0); // FAT-32
81 AddType(0x0c00, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 0); // FAT-32 LBA
82 AddType(0x0c01, "E3C9E316-0B5C-4DB8-817D-F92DF00215AE", "Microsoft reserved");
83 AddType(0x0e00, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 0); // FAT-16 LBA
84 AddType(0x1100, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 0); // Hidden FAT-12
85 AddType(0x1400, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 0); // Hidden FAT-16 < 32M
86 AddType(0x1600, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 0); // Hidden FAT-16
87 AddType(0x1700, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 0); // Hidden NTFS (or HPFS)
88 AddType(0x1b00, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 0); // Hidden FAT-32
89 AddType(0x1c00, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 0); // Hidden FAT-32 LBA
90 AddType(0x1e00, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 0); // Hidden FAT-16 LBA
91 AddType(0x2700, "DE94BBA4-06D1-4D40-A16A-BFD50179D6AC", "Windows RE");
92 AddType(0x4200, "AF9B60A0-1431-4F62-BC68-3311714A69AD", "Windows LDM data"); // Logical disk manager
93 AddType(0x4201, "5808C8AA-7E8F-42E0-85D2-E1E90434CFB3", "Windows LDM metadata"); // Logical disk manager
94
95 // An oddball IBM filesystem....
96 AddType(0x7501, "37AFFC90-EF7D-4E96-91C3-2D7AE055B174", "IBM GPFS"); // General Parallel File System (GPFS)
97
srs5694df9d3632011-01-08 18:33:24 -050098 // ChromeOS-specific partition types...
99 // Values taken from vboot_reference/firmware/lib/cgptlib/include/gpt.h in
100 // ChromeOS source code, retrieved 12/23/2010. They're also at
101 // http://www.chromium.org/chromium-os/chromiumos-design-docs/disk-format.
102 // These have no MBR equivalents, AFAIK, so I'm using 0x7Fxx values, since they're close
103 // to the Linux values.
104 AddType(0x7f00, "FE3A2A5D-4F32-41A7-B725-ACCC3285A309", "ChromeOS kernel");
105 AddType(0x7f01, "3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC", "ChromeOS root");
106 AddType(0x7f02, "2E0A753D-9E48-43B0-8337-B15192CB1B5E", "ChromeOS reserved");
107
srs56946699b012010-02-04 00:55:30 -0500108 // Linux-specific partition types....
109 AddType(0x8200, "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F", "Linux swap"); // Linux swap (or Solaris)
110 AddType(0x8300, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 0); // Linux native
111 AddType(0x8301, "8DA63339-0007-60C0-C436-083AC8230908", "Linux reserved");
112 AddType(0x8e00, "E6D6D379-F507-44C2-A23C-238F2A3DF928", "Linux LVM");
113
114 // FreeBSD partition types....
115 // Note: Rather than extract FreeBSD disklabel data, convert FreeBSD
116 // partitions in-place, and let FreeBSD sort out the details....
srs5694cb76c672010-02-11 22:22:22 -0500117 AddType(0xa500, "516E7CB4-6ECF-11D6-8FF8-00022D09712B", "FreeBSD disklabel");
srs56946699b012010-02-04 00:55:30 -0500118 AddType(0xa501, "83BD6B9D-7F41-11DC-BE0B-001560B84F0F", "FreeBSD boot");
119 AddType(0xa502, "516E7CB5-6ECF-11D6-8FF8-00022D09712B", "FreeBSD swap");
120 AddType(0xa503, "516E7CB6-6ECF-11D6-8FF8-00022D09712B", "FreeBSD UFS");
121 AddType(0xa504, "516E7CBA-6ECF-11D6-8FF8-00022D09712B", "FreeBSD ZFS");
122 AddType(0xa505, "516E7CB8-6ECF-11D6-8FF8-00022D09712B", "FreeBSD Vinum/RAID");
123
124 // A MacOS partition type, separated from others by NetBSD partition types...
125 AddType(0xa800, "55465300-0000-11AA-AA11-00306543ECAC", "Apple UFS"); // Mac OS X
126
127 // NetBSD partition types. Note that the main entry sets it up as a
128 // FreeBSD disklabel. I'm not 100% certain this is the correct behavior.
129 AddType(0xa900, "516E7CB4-6ECF-11D6-8FF8-00022D09712B", "FreeBSD disklabel", 0); // NetBSD disklabel
130 AddType(0xa901, "49F48D32-B10E-11DC-B99B-0019D1879648", "NetBSD swap");
131 AddType(0xa902, "49F48D5A-B10E-11DC-B99B-0019D1879648", "NetBSD FFS");
132 AddType(0xa903, "49F48D82-B10E-11DC-B99B-0019D1879648", "NetBSD LFS");
133 AddType(0xa904, "2DB519C4-B10F-11DC-B99B-0019D1879648", "NetBSD concatenated");
134 AddType(0xa905, "2DB519EC-B10F-11DC-B99B-0019D1879648", "NetBSD encrypted");
135 AddType(0xa906, "49F48DAA-B10E-11DC-B99B-0019D1879648", "NetBSD RAID");
136
137 // Mac OS partition types (See also 0xa800, above)....
138 AddType(0xab00, "426F6F74-0000-11AA-AA11-00306543ECAC", "Apple boot");
139 AddType(0xaf00, "48465300-0000-11AA-AA11-00306543ECAC", "Apple HFS/HFS+");
140 AddType(0xaf01, "52414944-0000-11AA-AA11-00306543ECAC", "Apple RAID");
141 AddType(0xaf02, "52414944-5F4F-11AA-AA11-00306543ECAC", "Apple RAID offline");
142 AddType(0xaf03, "4C616265-6C00-11AA-AA11-00306543ECAC", "Apple label");
143 AddType(0xaf04, "5265636F-7665-11AA-AA11-00306543ECAC", "AppleTV recovery");
144
145 // Solaris partition types (one of which is shared with MacOS)
146 AddType(0xbe00, "6A82CB45-1DD2-11B2-99A6-080020736631", "Solaris boot");
147 AddType(0xbf00, "6A85CF4D-1DD2-11B2-99A6-080020736631", "Solaris root");
148 AddType(0xbf01, "6A898CC3-1DD2-11B2-99A6-080020736631", "Solaris /usr & Mac ZFS"); // Solaris/MacOS
149 AddType(0xbf02, "6A87C46F-1DD2-11B2-99A6-080020736631", "Solaris swap");
150 AddType(0xbf03, "6A8B642B-1DD2-11B2-99A6-080020736631", "Solaris backup");
151 AddType(0xbf04, "6A8EF2E9-1DD2-11B2-99A6-080020736631", "Solaris /var");
152 AddType(0xbf05, "6A90BA39-1DD2-11B2-99A6-080020736631", "Solaris /home");
153 AddType(0xbf06, "6A9283A5-1DD2-11B2-99A6-080020736631", "Solaris alternate sector");
154 AddType(0xbf07, "6A945A3B-1DD2-11B2-99A6-080020736631", "Solaris Reserved 1");
155 AddType(0xbf08, "6A9630D1-1DD2-11B2-99A6-080020736631", "Solaris Reserved 2");
156 AddType(0xbf09, "6A980767-1DD2-11B2-99A6-080020736631", "Solaris Reserved 3");
157 AddType(0xbf0a, "6A96237F-1DD2-11B2-99A6-080020736631", "Solaris Reserved 4");
158 AddType(0xbf0b, "6A8D2AC7-1DD2-11B2-99A6-080020736631", "Solaris Reserved 5");
159
160 // I can find no MBR equivalents for these, but they're on the
161 // Wikipedia page for GPT, so here we go....
162 AddType(0xc001, "75894C1E-3AEB-11D3-B7C1-7B03A0000000", "HP-UX data");
163 AddType(0xc002, "E2A1E728-32E3-11D6-A682-7B03A0000000", "HP-UX service");
164
165 // EFI system and related partitions
srs569464cbd172011-03-01 22:03:54 -0500166 AddType(0xef00, "C12A7328-F81F-11D2-BA4B-00A0C93EC93B", "EFI System"); // Parted identifies these as having the "boot flag" set
srs56946699b012010-02-04 00:55:30 -0500167 AddType(0xef01, "024DEE41-33E7-11D3-9D69-0008C781F39F", "MBR partition scheme"); // Used to nest MBR in GPT
168 AddType(0xef02, "21686148-6449-6E6F-744E-656564454649", "BIOS boot partition"); // Boot loader
169
170 // A straggler Linux partition type....
171 AddType(0xfd00, "A19D880F-05FC-4D3B-A006-743F0F84911E", "Linux RAID");
172
173 // Note: DO NOT use the 0xffff code; that's reserved to indicate an
srs569455d92612010-03-07 22:16:07 -0500174 // unknown GUID type code.
srs56946699b012010-02-04 00:55:30 -0500175} // PartType::AddAllTypes()
176
srs5694e7b4ff92009-08-18 13:16:10 -0400177// Add a single type to the linked list of types. Returns 1 if operation
srs5694e321d442010-01-29 17:44:04 -0500178// succeeds, 0 otherwise.
srs56946699b012010-02-04 00:55:30 -0500179int PartType::AddType(uint16_t mbrType, const char * guidData, const char * name,
180 int toDisplay) {
srs5694e7b4ff92009-08-18 13:16:10 -0400181 AType* tempType;
182 int allOK = 1;
183
184 tempType = new AType;
185 if (tempType != NULL) {
186 tempType->MBRType = mbrType;
srs56946699b012010-02-04 00:55:30 -0500187 tempType->GUIDType = guidData;
188 tempType->name = name;
srs5694e7b4ff92009-08-18 13:16:10 -0400189 tempType->display = toDisplay;
190 tempType->next = NULL;
191 if (allTypes == NULL) { // first entry
192 allTypes = tempType;
193 } else {
194 lastType->next = tempType;
195 } // if/else
196 lastType = tempType;
197 } else {
198 allOK = 0;
199 } // if/else
200 return allOK;
srs56946699b012010-02-04 00:55:30 -0500201} // GUID::AddType(const char* variant)
202
srs569482f3f0b2010-09-22 10:50:24 -0400203// Assignment operator by string. If the original string is short,
204// interpret it as a gdisk hex code; if it's longer, interpret it as
srs56940873e9d2010-10-07 13:00:45 -0400205// a direct entry of a GUID value. If a short string isn't a hex
206// number, do nothing.
srs569482f3f0b2010-09-22 10:50:24 -0400207PartType & PartType::operator=(const string & orig) {
208 uint32_t hexCode;
209
210 if (orig.length() < 32) {
srs56940873e9d2010-10-07 13:00:45 -0400211 if (IsHex(orig)) {
212 sscanf(orig.c_str(), "%x", &hexCode);
213 *this = hexCode;
214 } // if
srs569482f3f0b2010-09-22 10:50:24 -0400215 } else {
216 GUIDData::operator=(orig);
217 } // if/else hexCode or GUID
218 return *this;
219} // PartType::operator=(const char * orig)
220
221// Assignment from C-style string; rely on C++ casting....
222PartType & PartType::operator=(const char * orig) {
223 return operator=((string) orig);
224} // PartType::operator=(const char * orig)
225
srs56946699b012010-02-04 00:55:30 -0500226// Assign a GUID based on my custom 2-byte (16-bit) MBR hex ID variant
srs5694cb76c672010-02-11 22:22:22 -0500227PartType & PartType::operator=(uint16_t ID) {
srs56946699b012010-02-04 00:55:30 -0500228 AType* theItem = allTypes;
229 int found = 0;
230
srs56946699b012010-02-04 00:55:30 -0500231 // Now search the type list for a match to the ID....
232 while ((theItem != NULL) && (!found)) {
srs569455d92612010-03-07 22:16:07 -0500233 if (theItem->MBRType == ID) {
srs56946699b012010-02-04 00:55:30 -0500234 GUIDData::operator=(theItem->GUIDType);
235 found = 1;
236 } else {
237 theItem = theItem->next;
238 } // if/else
239 } // while
240 if (!found) {
srs569455d92612010-03-07 22:16:07 -0500241 // Assign a default value....
242 GUIDData::operator=("EBD0A0A2-B9E5-4433-87C0-68B6B72699C7"); // 0700, Linux/Windows data
srs56946699b012010-02-04 00:55:30 -0500243 cout.setf(ios::uppercase);
244 cout.fill('0');
245 cout << "Exact type match not found for type code ";
246 cout.width(4);
247 cout << hex << ID << "; assigning type code for\n'Linux/Windows data'\n" << dec;
248 cout.fill(' ');
249 } // if (!found)
250 return *this;
251} // PartType::operator=(uint16_t ID)
252
253// Return the English description of the partition type (e.g., "Linux/Windows data")
srs56945a081752010-09-24 20:39:41 -0400254string PartType::TypeName(void) const {
srs56946699b012010-02-04 00:55:30 -0500255 AType* theItem = allTypes;
256 int found = 0;
257 string typeName;
258
259 while ((theItem != NULL) && (!found)) {
260 if (theItem->GUIDType == *this) { // found it!
261 typeName = theItem->name;
262 found = 1;
263 } else {
264 theItem = theItem->next;
265 } // if/else
266 } // while
267 if (!found) {
268 typeName = "Unknown";
269 } // if (!found)
270 return typeName;
271} // PartType::TypeName()
272
srs56945a608532011-03-17 13:53:01 -0400273// Return the Unicode description of the partition type (e.g., "Linux/Windows data")
274UnicodeString PartType::UTypeName(void) const {
275 AType* theItem = allTypes;
276 int found = 0;
277 UnicodeString typeName;
278
279 while ((theItem != NULL) && (!found)) {
280 if (theItem->GUIDType == *this) { // found it!
281 typeName = theItem->name.c_str();
282 found = 1;
283 } else {
284 theItem = theItem->next;
285 } // if/else
286 } // while
287 if (!found) {
288 typeName = "Unknown";
289 } // if (!found)
290 return typeName;
291} // PartType::TypeName()
292
srs56946699b012010-02-04 00:55:30 -0500293// Return the custom GPT fdisk 2-byte (16-bit) hex code for this GUID partition type
294// Note that this function ignores entries for which the display variable
295// is set to 0. This enables control of which values get returned when
296// there are multiple possibilities, but opens the algorithm up to the
297// potential for problems should the data in the list be bad.
srs56945a081752010-09-24 20:39:41 -0400298uint16_t PartType::GetHexType() const {
srs56946699b012010-02-04 00:55:30 -0500299 AType* theItem = allTypes;
300 int found = 0;
301 uint16_t theID = 0xFFFF;
302
303 while ((theItem != NULL) && (!found)) {
304 if ((theItem->GUIDType == *this) && (theItem->display == 1)) { // found it!
305 theID = theItem->MBRType;
306 found = 1;
307 } else {
308 theItem = theItem->next;
309 } // if/else
310 } // while
311 if (!found) {
312 theID = 0xFFFF;
313 } // if (!found)
314 return theID;
315} // PartType::GetHex()
srs5694e7b4ff92009-08-18 13:16:10 -0400316
317// Displays the available types and my extended MBR codes for same....
318// Note: This function assumes an 80-column display. On wider displays,
319// it stops at under 80 columns; on narrower displays, lines will wrap
320// in an ugly way.
srs56945a081752010-09-24 20:39:41 -0400321void PartType::ShowAllTypes(void) const {
srs5694e7b4ff92009-08-18 13:16:10 -0400322 int colCount = 1; // column count
srs5694e321d442010-01-29 17:44:04 -0500323 size_t i;
srs5694e7b4ff92009-08-18 13:16:10 -0400324 AType* thisType = allTypes;
srs5694e7b4ff92009-08-18 13:16:10 -0400325
srs5694fed16d02010-01-27 23:03:40 -0500326 cout.unsetf(ios::uppercase);
srs5694e7b4ff92009-08-18 13:16:10 -0400327 while (thisType != NULL) {
328 if (thisType->display == 1) { // show it
srs5694fed16d02010-01-27 23:03:40 -0500329 cout.fill('0');
330 cout.width(4);
331 cout << hex << thisType->MBRType << " ";
srs5694e321d442010-01-29 17:44:04 -0500332 cout << thisType->name.substr(0, 20);
srs569455d92612010-03-07 22:16:07 -0500333 for (i = 0; i < (20 - (thisType->name.substr(0, 20).length())); i++)
334 cout << " ";
srs5694e7b4ff92009-08-18 13:16:10 -0400335 if ((colCount % 3) == 0)
srs5694fed16d02010-01-27 23:03:40 -0500336 cout << "\n";
srs5694e321d442010-01-29 17:44:04 -0500337 else
338 cout << " ";
srs5694e7b4ff92009-08-18 13:16:10 -0400339 colCount++;
340 } // if
341 thisType = thisType->next;
342 } // while
srs56940a697312010-01-28 21:10:52 -0500343 cout.fill(' ');
344 cout << "\n" << dec;
srs56946699b012010-02-04 00:55:30 -0500345} // PartType::ShowTypes()
srs5694e7b4ff92009-08-18 13:16:10 -0400346
347// Returns 1 if code is a valid extended MBR code, 0 if it's not
srs56945a081752010-09-24 20:39:41 -0400348int PartType::Valid(uint16_t code) const {
srs5694e7b4ff92009-08-18 13:16:10 -0400349 AType* thisType = allTypes;
350 int found = 0;
351
352 while ((thisType != NULL) && (!found)) {
353 if (thisType->MBRType == code) {
354 found = 1;
355 } // if
356 thisType = thisType->next;
357 } // while
358 return found;
srs56946699b012010-02-04 00:55:30 -0500359} // PartType::Valid()
srs56945a608532011-03-17 13:53:01 -0400360
361/********************************
362 * *
363 * Non-class support functions. *
364 * *
365 ********************************/
366
367// Note: ReadUString() is here rather than in support.cc so that the ICU
368// libraries need not be linked to fixparts.
369
370// Reads a Unicode string from stdin, returning it as a ICU-style string.
371// Note that the returned string will NOT include the carriage return
372// entered by the user.
373UnicodeString ReadUString(void) {
374 UnicodeString inString = "", oneWord;
375
376 do {
377 cin >> oneWord;
378 if (inString.length() > 0)
379 inString += " ";
380 inString += oneWord;
381 } while (cin.peek() != '\n');
382 cin.get(); // discard CR
383 return inString;
384} // ReadUString()
385