Created
July 8, 2014 08:04
-
-
Save matteomattei/9e6c8f123e39c620dcd4 to your computer and use it in GitHub Desktop.
Extract u-boot multi-file image in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
def toNumber(buf): | |
""" Convert string in number """ | |
size = 0 | |
for b in buf: size=size*256+ord(b) | |
return size | |
if len(sys.argv)!=2: | |
sys.stdout.write('Usage %s uboot_image_file\n' % sys.argv[0]) | |
sys.exit(1) | |
f = open(sys.argv[1],'rb') | |
f.seek(64) # skip image header | |
parts = [] | |
# get file size of all images | |
while True: | |
buf = f.read(4) | |
size = toNumber(buf) | |
if size==0: break | |
parts.append(size) | |
i = 0 | |
for size in parts: | |
pattern = 'file%d.img' % i | |
p = open(pattern, 'wb') | |
buf = f.read(size) | |
p.write(buf) | |
p.close() | |
if size%4 != 0: | |
# 4 byte padding | |
f.read(4-(size%4)) | |
i+=1 | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeError: ord() expected string of length 1, but int found