From d600f36bc453c7547822e675ad62b7af6325f149 Mon Sep 17 00:00:00 2001 From: Floris Bos Date: Sun, 20 Nov 2022 14:46:58 +0100 Subject: [PATCH] Advanced settings FAT modification: add GPT partition support --- src/devicewrapper.cpp | 17 +++++++++++++++++ src/devicewrapperstructs.h | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/devicewrapper.cpp b/src/devicewrapper.cpp index 988b6b9..9a39454 100644 --- a/src/devicewrapper.cpp +++ b/src/devicewrapper.cpp @@ -161,6 +161,23 @@ DeviceWrapperFatPartition *DeviceWrapper::fatPartition(int nr) if (nr > 4 || nr < 1) throw std::runtime_error("Only basic partitions 1-4 supported"); + /* GPT table handling */ + struct gpt_header gpt; + struct gpt_partition gptpart; + pread((char *) &gpt, sizeof(gpt), 512); + + if (!strcmp("EFI PART", gpt.Signature) && gpt.MyLBA == 1) + { + qDebug() << "Using GPT partition table"; + if (nr > gpt.NumberOfPartitionEntries) + throw std::runtime_error("Partition does not exist"); + + pread((char *) &gptpart, sizeof(gptpart), gpt.PartitionEntryLBA*512 + gpt.SizeOfPartitionEntry*(nr-1)); + + return new DeviceWrapperFatPartition(this, gptpart.StartingLBA*512, (gptpart.EndingLBA-gptpart.StartingLBA+1)*512, this); + } + + /* MBR table handling */ struct mbr_table mbr; pread((char *) &mbr, sizeof(mbr), 0); diff --git a/src/devicewrapperstructs.h b/src/devicewrapperstructs.h index 4a8cb76..6f57b73 100644 --- a/src/devicewrapperstructs.h +++ b/src/devicewrapperstructs.h @@ -17,8 +17,8 @@ struct mbr_partition_entry { char begin_hsc[3]; unsigned char id; char end_hsc[3]; - unsigned int starting_sector; - unsigned int nr_of_sectors; + uint32_t starting_sector; + uint32_t nr_of_sectors; }; struct mbr_table { @@ -29,6 +29,36 @@ struct mbr_table { unsigned char signature[2]; }; +/* GUID on-disk structures + * https://www.intel.com/content/dam/www/public/us/en/zip/efi-110.zip (p. 369) + */ + +struct gpt_header { + char Signature[8]; + uint32_t Revision; + uint32_t HeaderSize; + uint32_t HeaderCRC32; + uint32_t Reserved; + uint64_t MyLBA; + uint64_t AlternateLBA; + uint64_t FirstUsableLBA; + uint64_t LastUsableLBA; + unsigned char DiskGUID[16]; + uint64_t PartitionEntryLBA; + uint32_t NumberOfPartitionEntries; + uint32_t SizeOfPartitionEntry; + uint32_t PartitionEntryArrayCRC32; + char Reserved2[420]; /* Assuming 512 byte sectors */ +}; + +struct gpt_partition { + unsigned char PartitionTypeGuid[16]; + unsigned char UniquePartitionGuid[16]; + uint64_t StartingLBA; + uint64_t EndingLBA; + uint64_t Attributes; + char PartitionName[72]; +}; /* File Allocation Table * https://academy.cba.mit.edu/classes/networking_communications/SD/FAT.pdf