Skip to content

Commit

Permalink
Import orginal payload_dumper.py
Browse files Browse the repository at this point in the history
  • Loading branch information
vm03 committed Aug 19, 2019
0 parents commit 6ee284b
Show file tree
Hide file tree
Showing 6 changed files with 789 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore everything
*

# But not these files...
!payload_dumper.py
!requirements.txt
!update_metadata_pb2.py
!output/.gitkeep
!README.md
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# payload dumper
Script work with Nokia 7 plus

### System requirement

- Python3, pip
- google protobuf for python `pip install protobuf`

### Guide

- Make you sure you have Python 3.6 installed.
- Download payload_dumper.py and update_metadata_pb2.py here.
- Extract your OTA zip and place payload.bin in the same folder as these files.
- Open PowerShell, Command Prompt, or Terminal depending on your OS.
- Enter the following command: python -m pip install protobuf
- When that’s finished, enter this command: python payload_dumper.py payload.bin
- This will start to extract the images within the payload.bin file to the current folder you are in.
Empty file added output/.gitkeep
Empty file.
92 changes: 92 additions & 0 deletions payload_dumper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env python
import struct
import hashlib
import bz2
import sys

try:
import lzma
except ImportError:
from backports import lzma

import update_metadata_pb2 as um

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

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

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

def verify_contiguous(exts):
blocks = 0

for ext in exts:
if ext.start_block != blocks:
return False

blocks += ext.num_blocks

return True

def data_for_op(op):
p.seek(data_offset + op.data_offset)
data = p.read(op.data_length)

# assert hashlib.sha256(data).digest() == op.data_sha256_hash, 'operation data hash mismatch'

if op.type == op.REPLACE_XZ:
dec = lzma.LZMADecompressor()
data = dec.decompress(data)
elif op.type == op.REPLACE_BZ:
dec = bz2.BZ2Decompressor()
data = dec.decompress(data)

return data

def dump_part(part):
print(part.partition_name)

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

for op in part.operations:
data = data_for_op(op)
h.update(data)
out_file.write(data)

# assert h.digest() == part.new_partition_info.hash, 'partition hash mismatch'

p = open(sys.argv[1], 'rb')

magic = p.read(4)
assert magic == b'CrAU'

file_format_version = u64(p.read(8))
assert file_format_version == 2

manifest_size = u64(p.read(8))

metadata_signature_size = 0

if file_format_version > 1:
metadata_signature_size = u32(p.read(4))

manifest = p.read(manifest_size)
metadata_signature = p.read(metadata_signature_size)

data_offset = p.tell()

dam = um.DeltaArchiveManifest()
dam.ParseFromString(manifest)

for part in dam.partitions:
# for op in part.operations:
# assert op.type in (op.REPLACE, op.REPLACE_BZ, op.REPLACE_XZ), \
# 'unsupported op'

# extents = flatten([op.dst_extents for op in part.operations])
# assert verify_contiguous(extents), 'operations do not span full image'

dump_part(part)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
protobuf==3.6.0
six==1.11.0
Loading

0 comments on commit 6ee284b

Please sign in to comment.