blob: 1bda724476c2f3ddd5a6471d0078057620413d6e [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
srs5694221e0872009-08-29 15:00:31 -04005/* 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
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.
32// See http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
33// for a list of MBR partition type codes.
srs56946699b012010-02-04 00:55:30 -050034PartType::PartType(void) : GUIDData() {
srs5694e7b4ff92009-08-18 13:16:10 -040035 numInstances++;
36 if (numInstances == 1) {
srs56946699b012010-02-04 00:55:30 -050037 AddAllTypes();
srs5694e7b4ff92009-08-18 13:16:10 -040038 } // if
39} // default constructor
40
srs56946699b012010-02-04 00:55:30 -050041PartType::PartType(const PartType & orig) : GUIDData(orig) {
42 numInstances++;
43 if (numInstances == 1) { // should never happen; just being paranoid
44 AddAllTypes();
45 } // if
46} // PartType copy constructor
47
48PartType::PartType(const GUIDData & orig) : GUIDData(orig) {
49 numInstances++;
50 if (numInstances == 1) {
51 AddAllTypes();
52 } // if
53} // PartType copy constructor
54
55PartType::~PartType(void) {
srs5694e7b4ff92009-08-18 13:16:10 -040056 AType* tempType;
57
58 numInstances--;
59 if (numInstances == 0) {
60 while (allTypes != NULL) {
61 tempType = allTypes;
62 allTypes = allTypes->next;
63 delete tempType;
64 } // while
65 } // if
66} // destructor
67
srs56946699b012010-02-04 00:55:30 -050068// Add all partition type codes to the internal linked-list structure.
69// Used by constructors.
70void 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
98 // Linux-specific partition types....
99 AddType(0x8200, "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F", "Linux swap"); // Linux swap (or Solaris)
100 AddType(0x8300, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Linux/Windows data", 0); // Linux native
101 AddType(0x8301, "8DA63339-0007-60C0-C436-083AC8230908", "Linux reserved");
102 AddType(0x8e00, "E6D6D379-F507-44C2-A23C-238F2A3DF928", "Linux LVM");
103
104 // FreeBSD partition types....
105 // Note: Rather than extract FreeBSD disklabel data, convert FreeBSD
106 // partitions in-place, and let FreeBSD sort out the details....
srs5694cb76c672010-02-11 22:22:22 -0500107 AddType(0xa500, "516E7CB4-6ECF-11D6-8FF8-00022D09712B", "FreeBSD disklabel");
srs56946699b012010-02-04 00:55:30 -0500108 AddType(0xa501, "83BD6B9D-7F41-11DC-BE0B-001560B84F0F", "FreeBSD boot");
109 AddType(0xa502, "516E7CB5-6ECF-11D6-8FF8-00022D09712B", "FreeBSD swap");
110 AddType(0xa503, "516E7CB6-6ECF-11D6-8FF8-00022D09712B", "FreeBSD UFS");
111 AddType(0xa504, "516E7CBA-6ECF-11D6-8FF8-00022D09712B", "FreeBSD ZFS");
112 AddType(0xa505, "516E7CB8-6ECF-11D6-8FF8-00022D09712B", "FreeBSD Vinum/RAID");
113
114 // A MacOS partition type, separated from others by NetBSD partition types...
115 AddType(0xa800, "55465300-0000-11AA-AA11-00306543ECAC", "Apple UFS"); // Mac OS X
116
117 // NetBSD partition types. Note that the main entry sets it up as a
118 // FreeBSD disklabel. I'm not 100% certain this is the correct behavior.
119 AddType(0xa900, "516E7CB4-6ECF-11D6-8FF8-00022D09712B", "FreeBSD disklabel", 0); // NetBSD disklabel
120 AddType(0xa901, "49F48D32-B10E-11DC-B99B-0019D1879648", "NetBSD swap");
121 AddType(0xa902, "49F48D5A-B10E-11DC-B99B-0019D1879648", "NetBSD FFS");
122 AddType(0xa903, "49F48D82-B10E-11DC-B99B-0019D1879648", "NetBSD LFS");
123 AddType(0xa904, "2DB519C4-B10F-11DC-B99B-0019D1879648", "NetBSD concatenated");
124 AddType(0xa905, "2DB519EC-B10F-11DC-B99B-0019D1879648", "NetBSD encrypted");
125 AddType(0xa906, "49F48DAA-B10E-11DC-B99B-0019D1879648", "NetBSD RAID");
126
127 // Mac OS partition types (See also 0xa800, above)....
128 AddType(0xab00, "426F6F74-0000-11AA-AA11-00306543ECAC", "Apple boot");
129 AddType(0xaf00, "48465300-0000-11AA-AA11-00306543ECAC", "Apple HFS/HFS+");
130 AddType(0xaf01, "52414944-0000-11AA-AA11-00306543ECAC", "Apple RAID");
131 AddType(0xaf02, "52414944-5F4F-11AA-AA11-00306543ECAC", "Apple RAID offline");
132 AddType(0xaf03, "4C616265-6C00-11AA-AA11-00306543ECAC", "Apple label");
133 AddType(0xaf04, "5265636F-7665-11AA-AA11-00306543ECAC", "AppleTV recovery");
134
135 // Solaris partition types (one of which is shared with MacOS)
136 AddType(0xbe00, "6A82CB45-1DD2-11B2-99A6-080020736631", "Solaris boot");
137 AddType(0xbf00, "6A85CF4D-1DD2-11B2-99A6-080020736631", "Solaris root");
138 AddType(0xbf01, "6A898CC3-1DD2-11B2-99A6-080020736631", "Solaris /usr & Mac ZFS"); // Solaris/MacOS
139 AddType(0xbf02, "6A87C46F-1DD2-11B2-99A6-080020736631", "Solaris swap");
140 AddType(0xbf03, "6A8B642B-1DD2-11B2-99A6-080020736631", "Solaris backup");
141 AddType(0xbf04, "6A8EF2E9-1DD2-11B2-99A6-080020736631", "Solaris /var");
142 AddType(0xbf05, "6A90BA39-1DD2-11B2-99A6-080020736631", "Solaris /home");
143 AddType(0xbf06, "6A9283A5-1DD2-11B2-99A6-080020736631", "Solaris alternate sector");
144 AddType(0xbf07, "6A945A3B-1DD2-11B2-99A6-080020736631", "Solaris Reserved 1");
145 AddType(0xbf08, "6A9630D1-1DD2-11B2-99A6-080020736631", "Solaris Reserved 2");
146 AddType(0xbf09, "6A980767-1DD2-11B2-99A6-080020736631", "Solaris Reserved 3");
147 AddType(0xbf0a, "6A96237F-1DD2-11B2-99A6-080020736631", "Solaris Reserved 4");
148 AddType(0xbf0b, "6A8D2AC7-1DD2-11B2-99A6-080020736631", "Solaris Reserved 5");
149
150 // I can find no MBR equivalents for these, but they're on the
151 // Wikipedia page for GPT, so here we go....
152 AddType(0xc001, "75894C1E-3AEB-11D3-B7C1-7B03A0000000", "HP-UX data");
153 AddType(0xc002, "E2A1E728-32E3-11D6-A682-7B03A0000000", "HP-UX service");
154
155 // EFI system and related partitions
156 AddType(0xef00, "C12A7328-F81F-11D2-BA4B-00A0C93EC93B", "EFI System"); // Parted marks Linux boot partitions as this
157 AddType(0xef01, "024DEE41-33E7-11D3-9D69-0008C781F39F", "MBR partition scheme"); // Used to nest MBR in GPT
158 AddType(0xef02, "21686148-6449-6E6F-744E-656564454649", "BIOS boot partition"); // Boot loader
159
160 // A straggler Linux partition type....
161 AddType(0xfd00, "A19D880F-05FC-4D3B-A006-743F0F84911E", "Linux RAID");
162
163 // Note: DO NOT use the 0xffff code; that's reserved to indicate an
164 // unknown type code.
165} // PartType::AddAllTypes()
166
srs5694e7b4ff92009-08-18 13:16:10 -0400167// Add a single type to the linked list of types. Returns 1 if operation
srs5694e321d442010-01-29 17:44:04 -0500168// succeeds, 0 otherwise.
srs56946699b012010-02-04 00:55:30 -0500169int PartType::AddType(uint16_t mbrType, const char * guidData, const char * name,
170 int toDisplay) {
srs5694e7b4ff92009-08-18 13:16:10 -0400171 AType* tempType;
172 int allOK = 1;
173
174 tempType = new AType;
175 if (tempType != NULL) {
176 tempType->MBRType = mbrType;
srs56946699b012010-02-04 00:55:30 -0500177 tempType->GUIDType = guidData;
178 tempType->name = name;
srs5694e7b4ff92009-08-18 13:16:10 -0400179 tempType->display = toDisplay;
180 tempType->next = NULL;
181 if (allTypes == NULL) { // first entry
182 allTypes = tempType;
183 } else {
184 lastType->next = tempType;
185 } // if/else
186 lastType = tempType;
187 } else {
188 allOK = 0;
189 } // if/else
190 return allOK;
srs56946699b012010-02-04 00:55:30 -0500191} // GUID::AddType(const char* variant)
192
193// Assign a GUID based on my custom 2-byte (16-bit) MBR hex ID variant
srs5694cb76c672010-02-11 22:22:22 -0500194PartType & PartType::operator=(uint16_t ID) {
srs56946699b012010-02-04 00:55:30 -0500195 AType* theItem = allTypes;
196 int found = 0;
197
198 // Assign a default value....
199 GUIDData::operator=("EBD0A0A2-B9E5-4433-87C0-68B6B72699C7"); // 0700, Linux/Windows data
200
201 // Now search the type list for a match to the ID....
202 while ((theItem != NULL) && (!found)) {
203 if (theItem->MBRType == ID) { // found it!
204 GUIDData::operator=(theItem->GUIDType);
205 found = 1;
206 } else {
207 theItem = theItem->next;
208 } // if/else
209 } // while
210 if (!found) {
211 cout.setf(ios::uppercase);
212 cout.fill('0');
213 cout << "Exact type match not found for type code ";
214 cout.width(4);
215 cout << hex << ID << "; assigning type code for\n'Linux/Windows data'\n" << dec;
216 cout.fill(' ');
217 } // if (!found)
218 return *this;
219} // PartType::operator=(uint16_t ID)
220
221// Return the English description of the partition type (e.g., "Linux/Windows data")
222string PartType::TypeName(void) {
223 AType* theItem = allTypes;
224 int found = 0;
225 string typeName;
226
227 while ((theItem != NULL) && (!found)) {
228 if (theItem->GUIDType == *this) { // found it!
229 typeName = theItem->name;
230 found = 1;
231 } else {
232 theItem = theItem->next;
233 } // if/else
234 } // while
235 if (!found) {
236 typeName = "Unknown";
237 } // if (!found)
238 return typeName;
239} // PartType::TypeName()
240
241// Return the custom GPT fdisk 2-byte (16-bit) hex code for this GUID partition type
242// Note that this function ignores entries for which the display variable
243// is set to 0. This enables control of which values get returned when
244// there are multiple possibilities, but opens the algorithm up to the
245// potential for problems should the data in the list be bad.
246uint16_t PartType::GetHexType() {
247 AType* theItem = allTypes;
248 int found = 0;
249 uint16_t theID = 0xFFFF;
250
251 while ((theItem != NULL) && (!found)) {
252 if ((theItem->GUIDType == *this) && (theItem->display == 1)) { // found it!
253 theID = theItem->MBRType;
254 found = 1;
255 } else {
256 theItem = theItem->next;
257 } // if/else
258 } // while
259 if (!found) {
260 theID = 0xFFFF;
261 } // if (!found)
262 return theID;
263} // PartType::GetHex()
srs5694e7b4ff92009-08-18 13:16:10 -0400264
265// Displays the available types and my extended MBR codes for same....
266// Note: This function assumes an 80-column display. On wider displays,
267// it stops at under 80 columns; on narrower displays, lines will wrap
268// in an ugly way.
srs56946699b012010-02-04 00:55:30 -0500269void PartType::ShowAllTypes(void) {
srs5694e7b4ff92009-08-18 13:16:10 -0400270 int colCount = 1; // column count
srs5694e321d442010-01-29 17:44:04 -0500271 size_t i;
srs5694e7b4ff92009-08-18 13:16:10 -0400272 AType* thisType = allTypes;
srs5694e7b4ff92009-08-18 13:16:10 -0400273
srs5694fed16d02010-01-27 23:03:40 -0500274 cout.unsetf(ios::uppercase);
srs5694e7b4ff92009-08-18 13:16:10 -0400275 while (thisType != NULL) {
276 if (thisType->display == 1) { // show it
srs5694fed16d02010-01-27 23:03:40 -0500277 cout.fill('0');
278 cout.width(4);
279 cout << hex << thisType->MBRType << " ";
srs5694e321d442010-01-29 17:44:04 -0500280 cout << thisType->name.substr(0, 20);
281 for (i = 0; i < (20 - (thisType->name.substr(0, 20).length())); i++) cout << " ";
srs5694e7b4ff92009-08-18 13:16:10 -0400282 if ((colCount % 3) == 0)
srs5694fed16d02010-01-27 23:03:40 -0500283 cout << "\n";
srs5694e321d442010-01-29 17:44:04 -0500284 else
285 cout << " ";
srs5694e7b4ff92009-08-18 13:16:10 -0400286 colCount++;
287 } // if
288 thisType = thisType->next;
289 } // while
srs56940a697312010-01-28 21:10:52 -0500290 cout.fill(' ');
291 cout << "\n" << dec;
srs56946699b012010-02-04 00:55:30 -0500292} // PartType::ShowTypes()
srs5694e7b4ff92009-08-18 13:16:10 -0400293
294// Returns 1 if code is a valid extended MBR code, 0 if it's not
srs56946699b012010-02-04 00:55:30 -0500295int PartType::Valid(uint16_t code) {
srs5694e7b4ff92009-08-18 13:16:10 -0400296 AType* thisType = allTypes;
297 int found = 0;
298
299 while ((thisType != NULL) && (!found)) {
300 if (thisType->MBRType == code) {
301 found = 1;
302 } // if
303 thisType = thisType->next;
304 } // while
305 return found;
srs56946699b012010-02-04 00:55:30 -0500306} // PartType::Valid()