Skip to content

Commit d600f36

Browse files
committed
Advanced settings FAT modification: add GPT partition support
1 parent 05f1c4d commit d600f36

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/devicewrapper.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,23 @@ DeviceWrapperFatPartition *DeviceWrapper::fatPartition(int nr)
161161
if (nr > 4 || nr < 1)
162162
throw std::runtime_error("Only basic partitions 1-4 supported");
163163

164+
/* GPT table handling */
165+
struct gpt_header gpt;
166+
struct gpt_partition gptpart;
167+
pread((char *) &gpt, sizeof(gpt), 512);
168+
169+
if (!strcmp("EFI PART", gpt.Signature) && gpt.MyLBA == 1)
170+
{
171+
qDebug() << "Using GPT partition table";
172+
if (nr > gpt.NumberOfPartitionEntries)
173+
throw std::runtime_error("Partition does not exist");
174+
175+
pread((char *) &gptpart, sizeof(gptpart), gpt.PartitionEntryLBA*512 + gpt.SizeOfPartitionEntry*(nr-1));
176+
177+
return new DeviceWrapperFatPartition(this, gptpart.StartingLBA*512, (gptpart.EndingLBA-gptpart.StartingLBA+1)*512, this);
178+
}
179+
180+
/* MBR table handling */
164181
struct mbr_table mbr;
165182
pread((char *) &mbr, sizeof(mbr), 0);
166183

src/devicewrapperstructs.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ struct mbr_partition_entry {
1717
char begin_hsc[3];
1818
unsigned char id;
1919
char end_hsc[3];
20-
unsigned int starting_sector;
21-
unsigned int nr_of_sectors;
20+
uint32_t starting_sector;
21+
uint32_t nr_of_sectors;
2222
};
2323

2424
struct mbr_table {
@@ -29,6 +29,36 @@ struct mbr_table {
2929
unsigned char signature[2];
3030
};
3131

32+
/* GUID on-disk structures
33+
* https://www.intel.com/content/dam/www/public/us/en/zip/efi-110.zip (p. 369)
34+
*/
35+
36+
struct gpt_header {
37+
char Signature[8];
38+
uint32_t Revision;
39+
uint32_t HeaderSize;
40+
uint32_t HeaderCRC32;
41+
uint32_t Reserved;
42+
uint64_t MyLBA;
43+
uint64_t AlternateLBA;
44+
uint64_t FirstUsableLBA;
45+
uint64_t LastUsableLBA;
46+
unsigned char DiskGUID[16];
47+
uint64_t PartitionEntryLBA;
48+
uint32_t NumberOfPartitionEntries;
49+
uint32_t SizeOfPartitionEntry;
50+
uint32_t PartitionEntryArrayCRC32;
51+
char Reserved2[420]; /* Assuming 512 byte sectors */
52+
};
53+
54+
struct gpt_partition {
55+
unsigned char PartitionTypeGuid[16];
56+
unsigned char UniquePartitionGuid[16];
57+
uint64_t StartingLBA;
58+
uint64_t EndingLBA;
59+
uint64_t Attributes;
60+
char PartitionName[72];
61+
};
3262

3363
/* File Allocation Table
3464
* https://academy.cba.mit.edu/classes/networking_communications/SD/FAT.pdf

0 commit comments

Comments
 (0)