華爲p6官方固件包UPDATE.APP解包

華爲官方固件包UPDATE.APP解包,大多依靠一個perl語言腳本split_updata.pl。JoeyJiao的split_updata.pl最新版本,能夠解開華爲p6的包,但文件名識別不了。源碼地址:https://github.com/JoeyJiao/split_updata.pl python

找到一個python版本的,能夠解包並精確給出文件名。原始爲python2版本,自行小修改爲python3版本。 git

代碼源自scue及jnjn,原始代碼看這裏:http://jnjn.blog.163.com/blog/static/275999382013311515491/ github

修改版以下: app

#!/usr/bin/env python
# Version: 0.2.201308040830
# Author: linkscue
# Function: unpack any hauwei honor2 Update.app
# Thanks to McSpoon(zhaoxing-go@163.com)
# 20130411: Modified for alignment to 4 byte boundary by jnjn@163.com
# 20130804: Modified for python3 by atuo @ oschina.net

import os
import sys
import struct
from functools import reduce

print("Usage: unpack $huawei_app_file")
try:
    sys.argv[1]
except IndexError:
        File="UPDATE.APP"
else:
    File=sys.argv[1];

verify_code=0xa55aaa55
verify_bin=struct.pack("I",verify_code)

print("I: unpack",File)
f=open(File,'rb')
cont=f.read()
f.close()

start=0
sub_file=1
search=verify_bin
while True:
    index = cont.find(search, start)
    if index == -1:
        break

    if ((index) % 4  != 0 ):
    	print("skipping a 55aa5aa5 at offset: 0x%08x\n" % (index))
    	start = index + 1
    else:
	    packet_addr=cont[index+4:index+8]
	    data_addr=cont[index+24:index+28]
	    img_name=cont[index+60:index+76]
	    packet_size_tuple=struct.unpack("I",packet_addr)
	    data_size_tuple=struct.unpack("I",data_addr)
	    packet_size=int(reduce(lambda rst, d: rst * 10 + d, packet_size_tuple))
	    data_size=int(reduce(lambda rst, d: rst * 10 + d, data_size_tuple))
	    temp=cont[index+packet_size:index+packet_size+data_size]
	    filename=str(sub_file)+'_'+img_name.strip(b'\x00').decode('ascii')+'.img'
	    print( "%2d I: verify code found at: 0x%08x, size: %10d bytes, --> %-20s" \
	    % (sub_file,index,data_size,filename) )
	    f1=open(filename,'wb')
	    f1.write(temp)
	    f1.close()
	    start = index + 1
	    sub_file += 1
相關文章
相關標籤/搜索