reserved,
my_lba,
alternate_lba,
first_usable_lba,
last_usable_lba,
disk_guid,
partition_entry_lba,
number_of_partition_entries,
size_of_partition_entry,
partition_entry_array_crc32):
self.signature = signature
self.revision = revision
self.header_size = header_size
self.header_crc32 = header_crc32
self.reserved = reserved
self.my_lba = my_lba
self.alternate_lba = alternate_lba
self.first_usable_lba = first_usable_lba
self.last_usable_lba = last_usable_lba
self.disk_guid = disk_guid
self.partition_entry_lba = partition_entry_lba
self.number_of_partition_entries = number_of_partition_entries
self.size_of_partition_entry = size_of_partition_entry
self.partition_of_entry_array_crc32 = partition_entry_array_crc32
def is\_valid(self):
return self.signature == 'EFI PART'.encode('ascii')
def calculate\_header\_crc32(self):
header_crc32_input = pack('<8s 4s I I 4s Q Q Q Q 16s Q I I I',
self.signature,
self.revision,
self.header_size,
0,
self.reserved,
self.my_lba,
self.alternate_lba,
self.first_usable_lba,
self.last_usable_lba,
self.disk_guid,
self.partition_entry_lba,
self.number_of_partition_entries,
self.size_of_partition_entry,
self.partition_entry_array_crc32)
return binascii.crc32(header_crc32_input) & 0xffffffff
def encode_gpt_header(gpt_header):
data = pack(
‘< 8s 4s I I 4s Q Q Q Q 16s Q I I I’,
gpt_header.signature,
gpt_header.revision,
gpt_header.header_size,
gpt_header.header_crc32,
gpt_header.reserved,
gpt_header.my_lba,
gpt_header.alternate_lba,
gpt_header.first_usable_lba,
gpt_header.last_usable_lba,
gpt_header.disk_guid,
gpt_header.partition_entry_lba,
gpt_header.number_of_partition_entries,
gpt_header.size_of_partition_entry,
gpt_header.partition_entry_array_crc32)
return data
def decode_gpt_header(gpt_header):
(signature,
revision,
header_size,
header_crc32,
reserved,
my_lba,
alternate_lba,
first_usable_lba,
last_usable_lba,
disk_guid,
partition_entry_lba,
number_of_partition_entrires,
size_of_partition_entry,
partition_entry_array_crc32) = unpack(
‘< 8s 4s I I 4s Q Q Q Q 16s Q I I I’,
data[0:92]
)
gpt_header = GPTheader(signature,
revision,
header_size,
header_crc32,
reserved,
my_lba,
alternate_lba,
first_usable_lba,
last_usable_lba,
disk_guid,
partition_entry_lba,
number_of_partition_entries,
size_of_partition_entry,
partition_entry_array_crc32)
return gpt_header
def create_empty_gpt_entry():
partition_type_guid = b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
unique_partition_guid = b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
starting_lba = 0
ending_lba = 0
attributes = 0
partition_name = “\x00\x00”.encode(‘utf-16’)[2:]
print(partition_name)
entry = GPTPartitionEntry(partition_type_guid, unique_partition_guid,
starting_lba,
ending_lba,
attributes,
partition_name)
return entry
def create_gpt_entry(start_lba, end_lba, name):
partition_type_guid = uuid.uuid4().bytes
unique_partition_guid = uuid.uuid4().bytes
starting_lba = start_lba
ending_lba = end_lba
attributes = 0
partition_name = name.decode(“utf-8”).encode(‘utf-16’)[2:]
entry = GPTPartitionEntry(partition_type_guid,
unique_partition_guid,
starting_lba,
ending_lba,
attributes,
partition_name)
return entry
def complete_gpt_entries(entrylist):
an = create_empty_gpt_entry()
for i in range(len(entrylist), 0x80, 1):
entrylist.append(an)
def create_gpt_header(current_lba):
signature = b"EFI PART"
revision = b"\x00\x00\x01\x00"
header_size = 0x5c
header_crc32 = 0
reserved = b"\x00\x00\x00\x00&