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-2014 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#ifndef __STDC_CONSTANT_MACROS
10#define __STDC_CONSTANT_MACROS
11#endif
12
13#include <string.h>
14#include <stdint.h>
15#include <stdio.h>
16#include <iostream>
17#include "parttypes.h"
18
19using namespace std;
20
21int PartType::numInstances = 0;
22AType* PartType::allTypes = NULL;
23AType* PartType::lastType = NULL;
24
25// Constructor. Its main task is to initialize the data list, but only
26// if this is the first instance, since it's a static linked list.
27// Partition type codes are MBR type codes multiplied by 0x0100, with
28// additional related codes taking on following numbers. For instance,
29// the FreeBSD disklabel code in MBR is 0xa5; here, it's 0xa500, with
30// additional FreeBSD codes being 0xa501, 0xa502, and so on. This gives
31// related codes similar numbers and (given appropriate entry positions
32// in the linked list) keeps them together in the listings generated
33// by typing "L" at the main gdisk menu.
34PartType::PartType(void) : GUIDData() {
35   numInstances++;
36   if (numInstances == 1) {
37      AddAllTypes();
38   } // if
39} // default constructor
40
41PartType::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) {
56   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
68// Add all partition type codes to the internal linked-list structure.
69// Used by constructors.
70// See http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
71// for a list of MBR partition type codes.
72void PartType::AddAllTypes(void) {
73   // Start with the "unused entry," which should normally appear only
74   // on empty partition table entries....
75   AddType(0x0000, "00000000-0000-0000-0000-000000000000", "Unused entry", 0);
76
77   // DOS/Windows partition types, most of which are hidden from the "L" listing
78   // (they're available mainly for MBR-to-GPT conversions).
79   AddType(0x0100, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Microsoft basic data", 0); // FAT-12
80   AddType(0x0400, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Microsoft basic data", 0); // FAT-16 < 32M
81   AddType(0x0600, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Microsoft basic data", 0); // FAT-16
82   AddType(0x0700, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Microsoft basic data", 1); // NTFS (or HPFS)
83   AddType(0x0b00, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Microsoft basic data", 0); // FAT-32
84   AddType(0x0c00, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Microsoft basic data", 0); // FAT-32 LBA
85   AddType(0x0c01, "E3C9E316-0B5C-4DB8-817D-F92DF00215AE", "Microsoft reserved");
86   AddType(0x0e00, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Microsoft basic data", 0); // FAT-16 LBA
87   AddType(0x1100, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Microsoft basic data", 0); // Hidden FAT-12
88   AddType(0x1400, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Microsoft basic data", 0); // Hidden FAT-16 < 32M
89   AddType(0x1600, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Microsoft basic data", 0); // Hidden FAT-16
90   AddType(0x1700, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Microsoft basic data", 0); // Hidden NTFS (or HPFS)
91   AddType(0x1b00, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Microsoft basic data", 0); // Hidden FAT-32
92   AddType(0x1c00, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Microsoft basic data", 0); // Hidden FAT-32 LBA
93   AddType(0x1e00, "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", "Microsoft basic data", 0); // Hidden FAT-16 LBA
94   AddType(0x2700, "DE94BBA4-06D1-4D40-A16A-BFD50179D6AC", "Windows RE");
95
96   // Open Network Install Environment (ONIE) specific types.
97   // See http://www.onie.org/ and
98   // https://github.com/onie/onie/blob/master/rootconf/x86_64/sysroot-lib-onie/onie-blkdev-common
99   AddType(0x3000, "7412F7D5-A156-4B13-81DC-867174929325", "ONIE boot");
100   AddType(0x3001, "D4E6E2CD-4469-46F3-B5CB-1BFF57AFC149", "ONIE config");
101
102   // PowerPC reference platform boot partition
103   AddType(0x4100, "9E1A2D38-C612-4316-AA26-8B49521E5A8B", "PowerPC PReP boot");
104
105   // Windows LDM ("dynamic disk") types
106   AddType(0x4200, "AF9B60A0-1431-4F62-BC68-3311714A69AD", "Windows LDM data"); // Logical disk manager
107   AddType(0x4201, "5808C8AA-7E8F-42E0-85D2-E1E90434CFB3", "Windows LDM metadata"); // Logical disk manager
108
109   // An oddball IBM filesystem....
110   AddType(0x7501, "37AFFC90-EF7D-4E96-91C3-2D7AE055B174", "IBM GPFS"); // General Parallel File System (GPFS)
111
112   // ChromeOS-specific partition types...
113   // Values taken from vboot_reference/firmware/lib/cgptlib/include/gpt.h in
114   // ChromeOS source code, retrieved 12/23/2010. They're also at
115   // http://www.chromium.org/chromium-os/chromiumos-design-docs/disk-format.
116   // These have no MBR equivalents, AFAIK, so I'm using 0x7Fxx values, since they're close
117   // to the Linux values.
118   AddType(0x7f00, "FE3A2A5D-4F32-41A7-B725-ACCC3285A309", "ChromeOS kernel");
119   AddType(0x7f01, "3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC", "ChromeOS root");
120   AddType(0x7f02, "2E0A753D-9E48-43B0-8337-B15192CB1B5E", "ChromeOS reserved");
121
122   // Linux-specific partition types....
123   AddType(0x8200, "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F", "Linux swap"); // Linux swap (or Solaris on MBR)
124   AddType(0x8300, "0FC63DAF-8483-4772-8E79-3D69D8477DE4", "Linux filesystem"); // Linux native
125   AddType(0x8301, "8DA63339-0007-60C0-C436-083AC8230908", "Linux reserved");
126   // See http://www.freedesktop.org/software/systemd/man/systemd-gpt-auto-generator.html
127   // and http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/
128   AddType(0x8302, "933AC7E1-2EB4-4F13-B844-0E14E2AEF915", "Linux /home"); // Linux /home (auto-mounted by systemd)
129   AddType(0x8303, "44479540-F297-41B2-9AF7-D131D5F0458A", "Linux x86 root (/)"); // Linux / on x86 (auto-mounted by systemd)
130   AddType(0x8304, "4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709", "Linux x86-64 root (/)"); // Linux / on x86-64 (auto-mounted by systemd)
131   AddType(0x8305, "B921B045-1DF0-41C3-AF44-4C6F280D3FAE", "Linux ARM64 root (/)"); // Linux / on 64-bit ARM (auto-mounted by systemd)
132   AddType(0x8306, "3B8F8425-20E0-4F3B-907F-1A25A76F98E8", "Linux /srv"); // Linux /srv (auto-mounted by systemd)
133
134   // Used by Intel Rapid Start technology
135   AddType(0x8400, "D3BFE2DE-3DAF-11DF-BA40-E3A556D89593", "Intel Rapid Start");
136
137   // Another Linux type code....
138   AddType(0x8e00, "E6D6D379-F507-44C2-A23C-238F2A3DF928", "Linux LVM");
139
140   // FreeBSD partition types....
141   // Note: Rather than extract FreeBSD disklabel data, convert FreeBSD
142   // partitions in-place, and let FreeBSD sort out the details....
143   AddType(0xa500, "516E7CB4-6ECF-11D6-8FF8-00022D09712B", "FreeBSD disklabel");
144   AddType(0xa501, "83BD6B9D-7F41-11DC-BE0B-001560B84F0F", "FreeBSD boot");
145   AddType(0xa502, "516E7CB5-6ECF-11D6-8FF8-00022D09712B", "FreeBSD swap");
146   AddType(0xa503, "516E7CB6-6ECF-11D6-8FF8-00022D09712B", "FreeBSD UFS");
147   AddType(0xa504, "516E7CBA-6ECF-11D6-8FF8-00022D09712B", "FreeBSD ZFS");
148   AddType(0xa505, "516E7CB8-6ECF-11D6-8FF8-00022D09712B", "FreeBSD Vinum/RAID");
149
150   // Midnight BSD partition types....
151   AddType(0xa580, "85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7", "Midnight BSD data");
152   AddType(0xa581, "85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7", "Midnight BSD boot");
153   AddType(0xa582, "85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7", "Midnight BSD swap");
154   AddType(0xa583, "0394Ef8B-237E-11E1-B4B3-E89A8F7FC3A7", "Midnight BSD UFS");
155   AddType(0xa584, "85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7", "Midnight BSD ZFS");
156   AddType(0xa585, "85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7", "Midnight BSD Vinum");
157
158   // A MacOS partition type, separated from others by NetBSD partition types...
159   AddType(0xa800, "55465300-0000-11AA-AA11-00306543ECAC", "Apple UFS"); // Mac OS X
160
161   // NetBSD partition types. Note that the main entry sets it up as a
162   // FreeBSD disklabel. I'm not 100% certain this is the correct behavior.
163   AddType(0xa900, "516E7CB4-6ECF-11D6-8FF8-00022D09712B", "FreeBSD disklabel", 0); // NetBSD disklabel
164   AddType(0xa901, "49F48D32-B10E-11DC-B99B-0019D1879648", "NetBSD swap");
165   AddType(0xa902, "49F48D5A-B10E-11DC-B99B-0019D1879648", "NetBSD FFS");
166   AddType(0xa903, "49F48D82-B10E-11DC-B99B-0019D1879648", "NetBSD LFS");
167   AddType(0xa904, "2DB519C4-B10F-11DC-B99B-0019D1879648", "NetBSD concatenated");
168   AddType(0xa905, "2DB519EC-B10F-11DC-B99B-0019D1879648", "NetBSD encrypted");
169   AddType(0xa906, "49F48DAA-B10E-11DC-B99B-0019D1879648", "NetBSD RAID");
170
171   // Mac OS partition types (See also 0xa800, above)....
172   AddType(0xab00, "426F6F74-0000-11AA-AA11-00306543ECAC", "Apple boot");
173   AddType(0xaf00, "48465300-0000-11AA-AA11-00306543ECAC", "Apple HFS/HFS+");
174   AddType(0xaf01, "52414944-0000-11AA-AA11-00306543ECAC", "Apple RAID");
175   AddType(0xaf02, "52414944-5F4F-11AA-AA11-00306543ECAC", "Apple RAID offline");
176   AddType(0xaf03, "4C616265-6C00-11AA-AA11-00306543ECAC", "Apple label");
177   AddType(0xaf04, "5265636F-7665-11AA-AA11-00306543ECAC", "AppleTV recovery");
178   AddType(0xaf05, "53746F72-6167-11AA-AA11-00306543ECAC", "Apple Core Storage");
179
180   // Solaris partition types (one of which is shared with MacOS)
181   AddType(0xbe00, "6A82CB45-1DD2-11B2-99A6-080020736631", "Solaris boot");
182   AddType(0xbf00, "6A85CF4D-1DD2-11B2-99A6-080020736631", "Solaris root");
183   AddType(0xbf01, "6A898CC3-1DD2-11B2-99A6-080020736631", "Solaris /usr & Mac ZFS"); // Solaris/MacOS
184   AddType(0xbf02, "6A87C46F-1DD2-11B2-99A6-080020736631", "Solaris swap");
185   AddType(0xbf03, "6A8B642B-1DD2-11B2-99A6-080020736631", "Solaris backup");
186   AddType(0xbf04, "6A8EF2E9-1DD2-11B2-99A6-080020736631", "Solaris /var");
187   AddType(0xbf05, "6A90BA39-1DD2-11B2-99A6-080020736631", "Solaris /home");
188   AddType(0xbf06, "6A9283A5-1DD2-11B2-99A6-080020736631", "Solaris alternate sector");
189   AddType(0xbf07, "6A945A3B-1DD2-11B2-99A6-080020736631", "Solaris Reserved 1");
190   AddType(0xbf08, "6A9630D1-1DD2-11B2-99A6-080020736631", "Solaris Reserved 2");
191   AddType(0xbf09, "6A980767-1DD2-11B2-99A6-080020736631", "Solaris Reserved 3");
192   AddType(0xbf0a, "6A96237F-1DD2-11B2-99A6-080020736631", "Solaris Reserved 4");
193   AddType(0xbf0b, "6A8D2AC7-1DD2-11B2-99A6-080020736631", "Solaris Reserved 5");
194
195   // I can find no MBR equivalents for these, but they're on the
196   // Wikipedia page for GPT, so here we go....
197   AddType(0xc001, "75894C1E-3AEB-11D3-B7C1-7B03A0000000", "HP-UX data");
198   AddType(0xc002, "E2A1E728-32E3-11D6-A682-7B03A0000000", "HP-UX service");
199
200   // See http://www.freedesktop.org/wiki/Specifications/BootLoaderSpec
201   AddType(0xea00, "BC13C2FF-59E6-4262-A352-B275FD6F7172", "Freedesktop $BOOT");
202
203   // Type code for Haiku; uses BeOS MBR code as hex code base
204   AddType(0xeb00, "42465331-3BA3-10F1-802A-4861696B7521", "Haiku BFS");
205
206   // Manufacturer-specific ESP-like partitions (in order in which they were added)
207   AddType(0xed00, "F4019732-066E-4E12-8273-346C5641494F", "Sony system partition");
208   AddType(0xed01, "BFBFAFE7-A34F-448A-9A5B-6213EB736C22", "Lenovo system partition");
209
210   // EFI system and related partitions
211   AddType(0xef00, "C12A7328-F81F-11D2-BA4B-00A0C93EC93B", "EFI System"); // Parted identifies these as having the "boot flag" set
212   AddType(0xef01, "024DEE41-33E7-11D3-9D69-0008C781F39F", "MBR partition scheme"); // Used to nest MBR in GPT
213   AddType(0xef02, "21686148-6449-6E6F-744E-656564454649", "BIOS boot partition"); // Used by GRUB
214
215   // Ceph type codes; see https://github.com/ceph/ceph/blob/9bcc42a3e6b08521694b5c0228b2c6ed7b3d312e/src/ceph-disk#L76-L81
216   AddType(0xf800, "4FBD7E29-9D25-41B8-AFD0-062C0CEFF05D", "Ceph OSD"); // Ceph Object Storage Daemon
217   AddType(0xf801, "4FBD7E29-9D25-41B8-AFD0-5EC00CEFF05D", "Ceph dm-crypt OSD"); // Ceph Object Storage Daemon (encrypted)
218   AddType(0xf802, "BFBFAFE7-A34F-448A-9A5B-6213EB736C22", "Ceph journal");
219   AddType(0xf803, "45B0969E-9B03-4F30-B4C6-5EC00CEFF106", "Ceph dm-crypt journal");
220   AddType(0xf804, "89C57F98-2FE5-4DC0-89C1-F3AD0CEFF2BE", "Ceph disk in creation");
221   AddType(0xf805, "89C57F98-2FE5-4DC0-89C1-5EC00CEFF2BE", "Ceph dm-crypt disk in creation");
222
223   // VMWare ESX partition types codes
224   AddType(0xfb00, "AA31E02A-400F-11DB-9590-000C2911D1B8", "VMWare VMFS");
225   AddType(0xfb01, "9198EFFC-31C0-11DB-8F78-000C2911D1B8", "VMWare reserved");
226   AddType(0xfc00, "9D275380-40AD-11DB-BF97-000C2911D1B8", "VMWare kcore crash protection");
227
228   // A straggler Linux partition type....
229   AddType(0xfd00, "A19D880F-05FC-4D3B-A006-743F0F84911E", "Linux RAID");
230
231   // Note: DO NOT use the 0xffff code; that's reserved to indicate an
232   // unknown GUID type code.
233} // PartType::AddAllTypes()
234
235// Add a single type to the linked list of types. Returns 1 if operation
236// succeeds, 0 otherwise.
237int PartType::AddType(uint16_t mbrType, const char * guidData, const char * name,
238                      int toDisplay) {
239   AType* tempType;
240   int allOK = 1;
241
242   tempType = new AType;
243   if (tempType != NULL) {
244      tempType->MBRType = mbrType;
245      tempType->GUIDType = guidData;
246      tempType->name = name;
247      tempType->display = toDisplay;
248      tempType->next = NULL;
249      if (allTypes == NULL) { // first entry
250         allTypes = tempType;
251      } else {
252         lastType->next = tempType;
253      } // if/else
254      lastType = tempType;
255   } else {
256      cerr << "Unable to allocate memory in PartType::AddType()! Partition type list will\n";
257      cerr << "be incomplete!\n";
258      allOK = 0;
259   } // if/else
260   return allOK;
261} // GUID::AddType(const char* variant)
262
263// Assignment operator by string. If the original string is short,
264// interpret it as a gdisk hex code; if it's longer, interpret it as
265// a direct entry of a GUID value. If a short string isn't a hex
266// number, do nothing.
267PartType & PartType::operator=(const string & orig) {
268   uint32_t hexCode;
269
270   if (orig.length() < 32) {
271      if (IsHex(orig)) {
272         sscanf(orig.c_str(), "%x", &hexCode);
273         *this = hexCode;
274      }
275   } else {
276      GUIDData::operator=(orig);
277   } // if/else hexCode or GUID
278   return *this;
279} // PartType::operator=(const char * orig)
280
281// Assignment from C-style string; rely on C++ casting....
282PartType & PartType::operator=(const char * orig) {
283   return operator=((string) orig);
284} // PartType::operator=(const char * orig)
285
286// Assign a GUID based on my custom 2-byte (16-bit) MBR hex ID variant
287PartType & PartType::operator=(uint16_t ID) {
288   AType* theItem = allTypes;
289   int found = 0;
290
291   // Now search the type list for a match to the ID....
292   while ((theItem != NULL) && (!found)) {
293      if (theItem->MBRType == ID)  {
294         GUIDData::operator=(theItem->GUIDType);
295         found = 1;
296      } else {
297         theItem = theItem->next;
298      } // if/else
299   } // while
300   if (!found) {
301      // Assign a default value....
302      operator=(DEFAULT_GPT_TYPE);
303      cout.setf(ios::uppercase);
304      cout.fill('0');
305      cout << "Exact type match not found for type code ";
306      cout.width(4);
307      cout << hex << ID << "; assigning type code for\n'" << TypeName() << "'\n" << dec;
308      cout.fill(' ');
309   } // if (!found)
310   return *this;
311} // PartType::operator=(uint16_t ID)
312
313// Return the English description of the partition type (e.g., "Linux filesystem")
314string PartType::TypeName(void) const {
315   AType* theItem = allTypes;
316   int found = 0;
317   string typeName;
318
319   while ((theItem != NULL) && (!found)) {
320      if (theItem->GUIDType == *this) { // found it!
321         typeName = theItem->name;
322         found = 1;
323      } else {
324         theItem = theItem->next;
325      } // if/else
326   } // while
327   if (!found) {
328      typeName = "Unknown";
329   } // if (!found)
330   return typeName;
331} // PartType::TypeName()
332
333#ifdef USE_UTF16
334// Return the Unicode description of the partition type (e.g., "Linux filesystem")
335UnicodeString PartType::UTypeName(void) const {
336   AType* theItem = allTypes;
337   int found = 0;
338   UnicodeString typeName;
339
340   while ((theItem != NULL) && (!found)) {
341      if (theItem->GUIDType == *this) { // found it!
342         typeName = theItem->name.c_str();
343         found = 1;
344      } else {
345         theItem = theItem->next;
346      } // if/else
347   } // while
348   if (!found) {
349      typeName = "Unknown";
350   } // if (!found)
351   return typeName;
352} // PartType::TypeName()
353#endif
354
355// Return the custom GPT fdisk 2-byte (16-bit) hex code for this GUID partition type
356// Note that this function ignores entries for which the display variable
357// is set to 0. This enables control of which values get returned when
358// there are multiple possibilities, but opens the algorithm up to the
359// potential for problems should the data in the list be bad.
360uint16_t PartType::GetHexType() const {
361   AType* theItem = allTypes;
362   int found = 0;
363   uint16_t theID = 0xFFFF;
364
365   while ((theItem != NULL) && (!found)) {
366      if ((theItem->GUIDType == *this) && (theItem->display == 1)) { // found it!
367         theID = theItem->MBRType;
368         found = 1;
369      } else {
370         theItem = theItem->next;
371      } // if/else
372   } // while
373   if (!found) {
374      theID = 0xFFFF;
375   } // if (!found)
376   return theID;
377} // PartType::GetHex()
378
379// Displays the available types and my extended MBR codes for same....
380// Note: This function assumes an 80-column display. On wider displays,
381// it stops at under 80 columns; on narrower displays, lines will wrap
382// in an ugly way. The maxLines value is the maximum number of lines
383// to display before prompting to continue, or 0 (or a negative value)
384// for no limit.
385void PartType::ShowAllTypes(int maxLines) const {
386   int colCount = 1, lineCount = 1;
387   size_t i;
388   AType* thisType = allTypes;
389   string line;
390
391   cout.unsetf(ios::uppercase);
392   while (thisType != NULL) {
393      if (thisType->display == 1) { // show it
394         cout.fill('0');
395         cout.width(4);
396         cout << hex << thisType->MBRType << " ";
397         cout << thisType->name.substr(0, 20);
398         for (i = 0; i < (20 - (thisType->name.substr(0, 20).length())); i++)
399            cout << " ";
400         if ((colCount % 3) == 0) {
401            if (thisType->next) {
402               cout << "\n";
403               if ((maxLines > 0) && (lineCount++ % maxLines) == 0) {
404                  cout << "Press the <Enter> key to see more codes: ";
405                  getline(cin, line);
406               } // if reached screen line limit
407            } // if there's another entry following this one
408         } else {
409            cout << "  ";
410         }
411         colCount++;
412      } // if
413      thisType = thisType->next;
414   } // while
415   cout.fill(' ');
416   cout << "\n" << dec;
417} // PartType::ShowAllTypes(int maxLines)
418
419// Returns 1 if code is a valid extended MBR code, 0 if it's not
420int PartType::Valid(uint16_t code) const {
421   AType* thisType = allTypes;
422   int found = 0;
423
424   while ((thisType != NULL) && (!found)) {
425      if (thisType->MBRType == code) {
426         found = 1;
427      } // if
428      thisType = thisType->next;
429   } // while
430   return found;
431} // PartType::Valid()
432