Skip to content
This repository was archived by the owner on Mar 6, 2022. It is now read-only.

Support selective extraction of partitions #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ Incremental firmware images are not supported (source_copy, source_bsdiff operat
## Usage

```
$ extract_android_ota_payload.py <payload.bin> [target_dir]
<payload.bin> : file extracted from the OTA zip file or the OTA zip file
<target_dir> : output directory for the extracted file
$ extract_android_ota_payload.py <payload.bin> [target_dir] [partition.img]
<payload.bin> : file extracted from the OTA zip file or the OTA zip file
<target_dir> : output directory for the extracted file
<partition_img> : name of partition to be extracted
```

## Example
Expand Down
22 changes: 19 additions & 3 deletions extract_android_ota_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def parse_payload(payload_f, partition, out_f):
else:
raise PayloadError('Unhandled operation type (%d)' % operation.type)

def main(filename, output_dir):
def main(filename, output_dir, partition):
if filename.endswith('.zip'):
print("Extracting 'payload.bin' from OTA file...")
ota_zf = zipfile.ZipFile(filename)
Expand All @@ -117,6 +117,8 @@ def main(filename, output_dir):

for p in payload.manifest.partitions:
name = p.partition_name + '.img'
if (partition is not None and name != partition):
continue
print("Extracting '%s'" % name)
fname = os.path.join(output_dir, name)
out_f = open(fname, 'w')
Expand All @@ -131,15 +133,29 @@ def main(filename, output_dir):
try:
filename = sys.argv[1]
except:
print('Usage: %s payload.bin [output_dir]' % sys.argv[0])
print('Usage: %s payload.bin [output_dir] [partition.img]' % sys.argv[0])
sys.exit()

try:
output_dir = sys.argv[2]
except IndexError:
output_dir = os.getcwd()

try:
partition = sys.argv[3]
except IndexError:
try:
partition = sys.argv[2]
except IndexError:
partition = None

if partition is not None and not partition.endswith('.img'):
partition = None

if output_dir.endswith('.img'):
output_dir = os.getcwd()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly a fan of all this additional handling but I think it covers all cases, I did test manually.

if not os.path.exists(output_dir):
os.makedirs(output_dir)

main(filename, output_dir)
main(filename, output_dir, partition)