Skip to content

Commit

Permalink
reformat with black
Browse files Browse the repository at this point in the history
  • Loading branch information
nnsee committed Dec 20, 2021
1 parent d1cfbaa commit 4627bbf
Show file tree
Hide file tree
Showing 2 changed files with 1,091 additions and 633 deletions.
86 changes: 48 additions & 38 deletions payload_dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import bsdiff4
import io
import os

try:
import lzma
except ImportError:
Expand All @@ -16,11 +17,14 @@

flatten = lambda l: [item for sublist in l for item in sublist]


def u32(x):
return struct.unpack('>I', x)[0]
return struct.unpack(">I", x)[0]


def u64(x):
return struct.unpack('>Q', x)[0]
return struct.unpack(">Q", x)[0]


def verify_contiguous(exts):
blocks = 0
Expand All @@ -33,7 +37,8 @@ def verify_contiguous(exts):

return True

def data_for_op(op,out_file,old_file):

def data_for_op(op, out_file, old_file):
args.payloadfile.seek(data_offset + op.data_offset)
data = args.payloadfile.read(op.data_length)

Expand All @@ -42,96 +47,102 @@ def data_for_op(op,out_file,old_file):
if op.type == op.REPLACE_XZ:
dec = lzma.LZMADecompressor()
data = dec.decompress(data)
out_file.seek(op.dst_extents[0].start_block*block_size)
out_file.seek(op.dst_extents[0].start_block * block_size)
out_file.write(data)
elif op.type == op.REPLACE_BZ:
dec = bz2.BZ2Decompressor()
data = dec.decompress(data)
out_file.seek(op.dst_extents[0].start_block*block_size)
out_file.seek(op.dst_extents[0].start_block * block_size)
out_file.write(data)
elif op.type == op.REPLACE:
out_file.seek(op.dst_extents[0].start_block*block_size)
out_file.seek(op.dst_extents[0].start_block * block_size)
out_file.write(data)
elif op.type == op.SOURCE_COPY:
if not args.diff:
print ("SOURCE_COPY supported only for differential OTA")
print("SOURCE_COPY supported only for differential OTA")
sys.exit(-2)
out_file.seek(op.dst_extents[0].start_block*block_size)
out_file.seek(op.dst_extents[0].start_block * block_size)
for ext in op.src_extents:
old_file.seek(ext.start_block*block_size)
data = old_file.read(ext.num_blocks*block_size)
old_file.seek(ext.start_block * block_size)
data = old_file.read(ext.num_blocks * block_size)
out_file.write(data)
elif op.type == op.SOURCE_BSDIFF:
if not args.diff:
print ("SOURCE_BSDIFF supported only for differential OTA")
print("SOURCE_BSDIFF supported only for differential OTA")
sys.exit(-3)
out_file.seek(op.dst_extents[0].start_block*block_size)
out_file.seek(op.dst_extents[0].start_block * block_size)
tmp_buff = io.BytesIO()
for ext in op.src_extents:
old_file.seek(ext.start_block*block_size)
old_data = old_file.read(ext.num_blocks*block_size)
old_file.seek(ext.start_block * block_size)
old_data = old_file.read(ext.num_blocks * block_size)
tmp_buff.write(old_data)
tmp_buff.seek(0)
old_data = tmp_buff.read()
tmp_buff.seek(0)
tmp_buff.write(bsdiff4.patch(old_data, data))
n = 0;
n = 0
tmp_buff.seek(0)
for ext in op.dst_extents:
tmp_buff.seek(n*block_size)
tmp_buff.seek(n * block_size)
n += ext.num_blocks
data = tmp_buff.read(ext.num_blocks*block_size)
out_file.seek(ext.start_block*block_size)
data = tmp_buff.read(ext.num_blocks * block_size)
out_file.seek(ext.start_block * block_size)
out_file.write(data)
elif op.type == op.ZERO:
for ext in op.dst_extents:
out_file.seek(ext.start_block*block_size)
out_file.write(b'\x00' * ext.num_blocks*block_size)
out_file.seek(ext.start_block * block_size)
out_file.write(b"\x00" * ext.num_blocks * block_size)
else:
print ("Unsupported type = %d" % op.type)
print("Unsupported type = %d" % op.type)
sys.exit(-1)

return data


def dump_part(part):
sys.stdout.write("Processing %s partition" % part.partition_name)
sys.stdout.flush()

out_file = open('%s/%s.img' % (args.out, part.partition_name), 'wb')
out_file = open("%s/%s.img" % (args.out, part.partition_name), "wb")
h = hashlib.sha256()

if args.diff:
old_file = open('%s/%s.img' % (args.old, part.partition_name), 'rb')
old_file = open("%s/%s.img" % (args.old, part.partition_name), "rb")
else:
old_file = None

for op in part.operations:
data = data_for_op(op,out_file,old_file)
data = data_for_op(op, out_file, old_file)
sys.stdout.write(".")
sys.stdout.flush()

print("Done")


parser = argparse.ArgumentParser(description='OTA payload dumper')
parser.add_argument('payloadfile', type=argparse.FileType('rb'),
help='payload file name')
parser.add_argument('--out', default='output',
help='output directory (defaul: output)')
parser.add_argument('--diff',action='store_true',
help='extract differential OTA, you need put original images to old dir')
parser.add_argument('--old', default='old',
help='directory with original images for differential OTA (defaul: old)')
parser.add_argument('--images', default="",
help='images to extract (default: empty)')
parser = argparse.ArgumentParser(description="OTA payload dumper")
parser.add_argument(
"payloadfile", type=argparse.FileType("rb"), help="payload file name"
)
parser.add_argument("--out", default="output", help="output directory (defaul: output)")
parser.add_argument(
"--diff",
action="store_true",
help="extract differential OTA, you need put original images to old dir",
)
parser.add_argument(
"--old",
default="old",
help="directory with original images for differential OTA (defaul: old)",
)
parser.add_argument("--images", default="", help="images to extract (default: empty)")
args = parser.parse_args()

#Check for --out directory exists
# Check for --out directory exists
if not os.path.exists(args.out):
os.makedirs(args.out)

magic = args.payloadfile.read(4)
assert magic == b'CrAU'
assert magic == b"CrAU"

file_format_version = u64(args.payloadfile.read(8))
assert file_format_version == 2
Expand Down Expand Up @@ -163,4 +174,3 @@ def dump_part(part):
dump_part(partition[0])
else:
sys.stderr.write("Partition %s not found in payload!\n" % image)

Loading

0 comments on commit 4627bbf

Please sign in to comment.