Fix missing crc fix for gpt patch

This commit is contained in:
Bjoern Kerler 2022-02-19 17:57:48 +01:00
parent 25e06b2446
commit 312cf3bb99
2 changed files with 31 additions and 10 deletions

View file

@ -1261,11 +1261,17 @@ class firehose(metaclass=LogBase):
gp = gpt()
slot = partitionname.lower()[-2:]
if "_a" in slot or "_b" in slot:
pdata, offset = gp.patch(data, partitionname, active=partslots[slot])
if data is not None:
start_sector = offset // self.cfg.SECTOR_SIZE_IN_BYTES
byte_offset = offset % self.cfg.SECTOR_SIZE_IN_BYTES
self.cmd_patch(lun,start_sector,byte_offset,pdata,len(pdata),True)
pdata, poffset = gp.patch(data, partitionname, active=partslots[slot])
data[poffset:poffset + len(pdata)] = pdata
wdata = gp.fix_gpt_crc(data)
if wdata is not None:
start_sector_patch = poffset // self.cfg.SECTOR_SIZE_IN_BYTES
byte_offset_patch = poffset % self.cfg.SECTOR_SIZE_IN_BYTES
headeroffset = gp.header.current_lba * gp.sectorsize
start_sector_hdr = headeroffset // self.cfg.SECTOR_SIZE_IN_BYTES
header = wdata[start_sector_hdr:start_sector_hdr+gp.header.header_size]
self.cmd_patch(lun,start_sector_patch,byte_offset_patch,pdata,len(pdata),True)
self.cmd_patch(lun, headeroffset, 0, header, len(pdata), True)
return True
return False

View file

@ -12,6 +12,7 @@ from enum import Enum
from binascii import hexlify
from struct import calcsize, unpack, pack
from io import BytesIO
from binascii import crc32
class ColorFormatter(logging.Formatter):
LOG_COLORS = {
@ -206,6 +207,7 @@ class gpt(metaclass=LogBase):
self.part_entry_start_lba = sh.qword()
self.num_part_entries = sh.dword()
self.part_entry_size = sh.dword()
self.crc32_part_entries = sh.dword()
class gpt_partition:
def __init__(self, data):
@ -494,14 +496,26 @@ class gpt(metaclass=LogBase):
else:
flags |= AB_PARTITION_ATTR_UNBOOTABLE << (AB_FLAG_OFFSET*8)
partentry.flags = flags
data = partentry.create()
return data, partition.entryoffset
pdata = partentry.create()
return pdata, partition.entryoffset
break
return None, None
except Exception as e:
self.error(str(e))
return None, None
def fix_gpt_crc(self, data):
partentry_size = self.header.num_part_entries * self.header.part_entry_size
partentry_offset = self.header.part_entry_start_lba * self.sectorsize
partdata = data[partentry_offset:partentry_offset + partentry_size]
headeroffset = self.header.current_lba * self.sectorsize
headerdata = bytearray(data[headeroffset:headeroffset + self.header.header_size])
headerdata[0x58:0x58 + 4] = pack("<I", crc32(partdata))
headerdata[0x10:0x10 + 4] = pack("<I", 0)
headerdata[0x10:0x10 + 4] = pack("<I", crc32(headerdata))
data[headeroffset:headeroffset + self.header.header_size] = headerdata
return data
def get_flag(self, filename, imagename):
if "." in imagename:
imagename = imagename[:imagename.find(".")]
@ -559,12 +573,13 @@ if __name__ == "__main__":
with open(args.image, "rb") as rf:
size = min(32 * 4096, filesize)
data = bytearray(rf.read(size))
pdata, offset = gp.patch(data,partitition, active=active)
pdata, poffset = gp.patch(data,partitition, active=active)
data[poffset:poffset + len(pdata)] = pdata
wdata = gp.fix_gpt_crc(data)
if data is not None:
data[offset:offset + len(pdata)] = pdata
wfilename = args.image + ".patched"
with open(wfilename,"wb") as wf:
wf.write(data)
wf.write(wdata)
print(f"Successfully wrote patched gpt to {wfilename}")
else:
print("Error on setting bootable mode")