From 4b8913920dee8d07dc04abdbbd2d908c187de509 Mon Sep 17 00:00:00 2001 From: Zhang Cheng Date: Tue, 17 Sep 2013 03:02:06 +0800 Subject: [PATCH] add tools/huawei_files/split_updata.py The original split_updata.pl seems to be too old, which will not recognize image names for recent UPDATE.APP . Usage: split_updata.py -l UPDATE.APP # list images in UPDATE.APP split_updata.py -u UPDATE.APP # unpack images to output/ Hope python won't bring too many dependencies for Kitchen. --- scripts/app_file_to_working_folder | 6 +- tools/huawei_files/split_updata.py | 111 +++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 2 deletions(-) create mode 100755 tools/huawei_files/split_updata.py diff --git a/scripts/app_file_to_working_folder b/scripts/app_file_to_working_folder index c512d9a..22fc1dd 100755 --- a/scripts/app_file_to_working_folder +++ b/scripts/app_file_to_working_folder @@ -53,8 +53,10 @@ then fi echo -echo "Extracting system.img and boot.img using ZeBadger's script ..." -temp=`perl ../tools/huawei_files/split_updata.pl 2>/dev/null` +#echo "Extracting system.img and boot.img using ZeBadger's script ..." +#temp=`perl ../tools/huawei_files/split_updata.pl 2>/dev/null` +echo "Extracting system.img and boot.img with huawei_files/split_updata.py ..." +temp=`python ../tools/huawei_files/split_updata.py -u UPDATA.APP 2>/dev/null` if [ ! -e output/system.img ] then diff --git a/tools/huawei_files/split_updata.py b/tools/huawei_files/split_updata.py new file mode 100755 index 0000000..b72b304 --- /dev/null +++ b/tools/huawei_files/split_updata.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Author: Zhang Cheng +# Maintainer: Zhang Cheng + +import os +import sys +import mmap +import errno +import struct + +class UpdateApp(object): + def __init__(self, update_app_file): + self.update_app_file = update_app_file + self.images = list() + + with open(self.update_app_file, "r+b") as fp: + self.update_app = mmap.mmap(fp.fileno(), 0) + + start = 0 + while True: + offset = self.update_app.find('\x55\xaa\x5a\xa5', start) + if offset < 0: break + + # read head + ## 4 magic + ## 4 head size + ## 4 version + ## 8 hardware + ## 4 unknown + ## 4 body size + ## 16 date + ## 16 time + ## 16 image name + ## 16 blank + ## 2 ? + ## 4 + ## 2 * N + ## padding to 4 aligned + hsize = struct.unpack('> 12)) * 2 + else: tsize = (bsize >> 12) * 2 + if tsize + 98 != hsize: raise Exception("Error: tsize + 98 != bsize") + + self.images.append( (name, offset, hsize, bsize) ) + start = offset + hsize + bsize + + def _human_size(self, size): + if size < 1024: + return "%4d B" % size + elif size < 1024 * 1024: + return "%3d KB" % (size / 1024.0) + elif size < 1024 * 1024 * 1024: + return "%3d MB" % (size / 1024.0 / 1024.0) + elif size < 1024 * 1024 * 1024 * 1024: + return "%3d GB" % (size / 1024.0 / 1024.0 / 1024.0) + + def list(self): + i = 0 + for name, offset, hsize, bsize in self.images: + print "%02d: offset = %08x, hsize = %08x, bsize = %08x(%s), name = %s" \ + % (i, offset, hsize, bsize, self._human_size(bsize), name) + i += 1 + + def unpack(self): + i = 0 + for name, offset, hsize, bsize in self.images: + print "%02d: offset = %08x, hsize = %08x, bsize = %08x(%s), name = %s" \ + % (i, offset, hsize, bsize, self._human_size(bsize), name) + + head_filename = "output/%s.head" % name.lower() + with open(head_filename, "w+b") as fp: + fp.write(self.update_app[offset:offset+hsize]) + + body_filename = "output/%s.img" % name.lower() + with open(body_filename, "w+b") as fp: + fp.write(self.update_app[offset+hsize:offset+hsize+bsize]) + + i += 1 + +if __name__ == "__main__": + operation = "list" + filename = "UPDATE.APP" + + for arg in sys.argv[1:]: + if arg == "-l": + operation = "list" + elif arg == "-u": + operation = "unpack" + elif os.path.isfile(arg): + filename = arg + else: + print "ignore unrecognized option:", arg + + try: os.makedirs("output") + except OSError as e: + if e.errno == errno.EEXIST: pass + else: raise + + updateapp = UpdateApp(filename) + if operation == "list": + updateapp.list() + elif operation == "unpack": + updateapp.unpack() + +# vim:ai:et:sts=4:sw=4: