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