Skip to content

Instantly share code, notes, and snippets.

@u1735067
Last active October 22, 2022 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save u1735067/6d17908e6b3aeaf33396e55a6b8f42c3 to your computer and use it in GitHub Desktop.
Save u1735067/6d17908e6b3aeaf33396e55a6b8f42c3 to your computer and use it in GitHub Desktop.
Simple patcher for Flipper Zero FAPs to change the target SDK API in the manifest ; it doesn't mean the patched app will work correctly (!), it only allow it to be loaded
import re, pathlib
target_version_major = 2
target_version_minor = 0
"""md
* https://github.com/flipperdevices/flipperzero-firmware/blob/0.69.1/lib/flipper_application/flipper_application.c#L45
* https://github.com/flipperdevices/flipperzero-firmware/blob/0.69.1/lib/flipper_application/flipper_application.c#L116
* https://github.com/flipperdevices/flipperzero-firmware/blob/0.69.1/lib/flipper_application/application_manifest.c#L12
* https://github.com/flipperdevices/flipperzero-firmware/blob/0.69.1/lib/flipper_application/application_manifest.h#L15
* https://github.com/flipperdevices/flipperzero-firmware/blob/0.69.1/lib/flipper_application/elf/elf_file_i.h#L42
* https://github.com/flipperdevices/flipperzero-firmware/blob/0.69.1/scripts/fbt_tools/fbt_sdk.py#L250
* https://github.com/flipperdevices/flipperzero-firmware/blob/0.69.1/firmware/targets/f7/api_symbols.csv
"""
pattern = f'(HDGR\1...)(:?[^{chr(target_version_minor)}]...|..[^{chr(target_version_major)}].)'
repl = f'\\1{chr(target_version_minor)}\0{chr(target_version_major)}\0'
for fap_file in pathlib.Path('.').glob('**/*.fap'):
#for fap_file in pathlib.Path('.').glob('*.fap'):
print(f'-- {fap_file}')
with fap_file.open('rb') as fh:
fap_data = fh.read()
fap_data, replaced = re.subn(pattern.encode(), repl.encode(), fap_data)
if replaced < 1:
print('Not patched: version not found or already correct')
elif replaced > 1:
print('! Not patched: version found multiple times')
else:
with fap_file.open('wb') as fh:
fh.write(fap_data)
print('Patched')
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment