Skip to content

Commit

Permalink
ignore hash from filename
Browse files Browse the repository at this point in the history
  • Loading branch information
duhow committed Dec 21, 2024
1 parent eaee6e1 commit 6ed8c09
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
echo "::add-mask::`basename ${UPDATE}`"
wget -q -O mico_${MODEL}.bin "${UPDATE}"
./tools/mico_firmware.py -d mico_${MODEL} -e mico_${MODEL}.bin
./tools/mico_firmware.py -d mico_${MODEL} -e mico_${MODEL}.bin -i
- name: Pull build packages
if: false
Expand Down
20 changes: 14 additions & 6 deletions tools/mico_firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, path):
self.image_header = ImageHeader()
self.fd = open(self.path, 'rb')

def verify(self):
def verify(self, ignore_hash=False):
logging.info('[Jobs] Verifying firmware image...')
assert self.fd.readinto(self.image_header) == ctypes.sizeof(self.image_header)

Expand Down Expand Up @@ -68,7 +68,14 @@ def verify(self):
hash = m.hexdigest()
logging.info(f'computed md5 hash: {hash}')
filename_hash = os.path.basename(self.path).split('_')[-2]
assert hash[(len(filename_hash) * -1):] == filename_hash
try:
assert hash[(len(filename_hash) * -1):] == filename_hash
except AssertionError:
if ignore_hash:
logging.warning(f'Warning: hash "{filename_hash}" does not match expected!')
pass
else:
raise

self.fd.seek(0)
return True
Expand All @@ -94,10 +101,10 @@ def extract(self, dest: str = None):
logging.info(f'extracting segment: {segment_header.segment_name.decode("ascii")}')


def run(path, extract=False, dest=None):
def run(path, extract=False, dest=None, ignore_hash=False):
logging.info(f'[MSG] Input file: {path}')
firmware = Firmware(path)
if firmware.verify():
if firmware.verify(ignore_hash):
logging.info('[Jobs] Verification success: it\'s a genuine firmware from Xiaomi.')

if extract:
Expand All @@ -110,6 +117,7 @@ def run(path, extract=False, dest=None):

parser = argparse.ArgumentParser()
parser.add_argument('-d', '--dest', help='Destination directory to store extracted files, default=current working directory', action='store')
parser.add_argument('-i', '--ignore-hash', help='Do not fail if md5 hash does not match', action='store_true')
parser.add_argument('-e', '--extract', help='Input your firmware', action='store')
parser.add_argument('-s', '--show', help='Show firmware info', action='store')
args = parser.parse_args()
Expand All @@ -119,9 +127,9 @@ def run(path, extract=False, dest=None):
dest_path = os.getcwd()
if args.dest:
dest_path = args.dest
run(firmware_path, extract=True, dest=dest_path)
run(firmware_path, extract=True, dest=dest_path, ignore_hash=args.ignore_hash)
elif args.show:
firmware_path = os.path.abspath(args.show)
run(firmware_path)
run(firmware_path, ignore_hash=args.ignore_hash)
else:
parser.print_help()

0 comments on commit 6ed8c09

Please sign in to comment.