2022-04-13 23:50:25 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
import io
|
2023-05-03 08:48:49 +03:00
|
|
|
import math
|
2022-04-19 11:03:28 +03:00
|
|
|
import os
|
2022-04-13 23:50:25 +03:00
|
|
|
import shutil
|
2022-04-19 11:03:28 +03:00
|
|
|
import tarfile
|
2023-05-03 08:48:49 +03:00
|
|
|
import zlib
|
|
|
|
from os.path import exists, join
|
2022-04-13 23:50:25 +03:00
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
import heatshrink2
|
2023-05-03 08:48:49 +03:00
|
|
|
from flipper.app import App
|
|
|
|
from flipper.assets.coprobin import CoproBinary, get_stack_type
|
2024-06-30 13:38:48 +03:00
|
|
|
from flipper.assets.heatshrink_stream import HeatshrinkDataStreamHeader
|
2023-05-03 08:48:49 +03:00
|
|
|
from flipper.assets.obdata import ObReferenceValues, OptionBytesData
|
2024-06-30 13:38:48 +03:00
|
|
|
from flipper.assets.tarball import compress_tree_tarball, tar_sanitizer_filter
|
2023-05-03 08:48:49 +03:00
|
|
|
from flipper.utils.fff import FlipperFormatFile
|
2022-06-21 17:11:34 +03:00
|
|
|
from slideshow import Main as SlideshowMain
|
|
|
|
|
2022-04-13 23:50:25 +03:00
|
|
|
|
|
|
|
class Main(App):
|
2022-05-11 12:45:01 +03:00
|
|
|
UPDATE_MANIFEST_VERSION = 2
|
2022-04-19 22:02:37 +03:00
|
|
|
UPDATE_MANIFEST_NAME = "update.fuf"
|
|
|
|
|
2022-04-19 11:03:28 +03:00
|
|
|
# No compression, plain tar
|
2022-04-19 22:02:37 +03:00
|
|
|
RESOURCE_TAR_MODE = "w:"
|
2024-06-30 13:38:48 +03:00
|
|
|
RESOURCE_FILE_NAME = "resources.ths" # .Tar.HeatShrink
|
2022-10-13 18:05:07 +03:00
|
|
|
RESOURCE_ENTRY_NAME_MAX_LENGTH = 100
|
2022-04-19 11:03:28 +03:00
|
|
|
|
2022-06-09 12:07:42 +03:00
|
|
|
WHITELISTED_STACK_TYPES = set(
|
|
|
|
map(
|
|
|
|
get_stack_type,
|
|
|
|
["BLE_FULL", "BLE_LIGHT", "BLE_BASIC"],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
FLASH_BASE = 0x8000000
|
|
|
|
MIN_LFS_PAGES = 6
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
HEATSHRINK_WINDOW_SIZE = 13
|
|
|
|
HEATSHRINK_LOOKAHEAD_SIZE = 6
|
|
|
|
|
2022-06-21 17:11:34 +03:00
|
|
|
# Post-update slideshow
|
|
|
|
SPLASH_BIN_NAME = "splash.bin"
|
|
|
|
|
2022-04-13 23:50:25 +03:00
|
|
|
def init(self):
|
|
|
|
self.subparsers = self.parser.add_subparsers(help="sub-command help")
|
|
|
|
|
|
|
|
# generate
|
|
|
|
self.parser_generate = self.subparsers.add_parser(
|
|
|
|
"generate", help="Generate update description file"
|
|
|
|
)
|
|
|
|
|
|
|
|
self.parser_generate.add_argument("-d", dest="directory", required=True)
|
|
|
|
self.parser_generate.add_argument("-v", dest="version", required=True)
|
|
|
|
self.parser_generate.add_argument("-t", dest="target", required=True)
|
2022-04-27 18:53:48 +03:00
|
|
|
self.parser_generate.add_argument(
|
|
|
|
"--dfu", dest="dfu", default="", required=False
|
|
|
|
)
|
2022-04-19 22:02:37 +03:00
|
|
|
self.parser_generate.add_argument("-r", dest="resources", required=False)
|
|
|
|
self.parser_generate.add_argument("--stage", dest="stage", required=True)
|
2022-04-19 11:03:28 +03:00
|
|
|
self.parser_generate.add_argument(
|
2022-04-19 22:02:37 +03:00
|
|
|
"--radio", dest="radiobin", default="", required=False
|
2022-04-19 11:03:28 +03:00
|
|
|
)
|
2022-04-13 23:50:25 +03:00
|
|
|
self.parser_generate.add_argument(
|
2022-04-27 18:53:48 +03:00
|
|
|
"--radioaddr",
|
|
|
|
dest="radioaddr",
|
|
|
|
type=lambda x: int(x, 16),
|
|
|
|
default=0,
|
|
|
|
required=False,
|
2022-04-13 23:50:25 +03:00
|
|
|
)
|
2022-04-27 18:53:48 +03:00
|
|
|
|
2022-04-19 11:03:28 +03:00
|
|
|
self.parser_generate.add_argument(
|
2022-04-27 18:53:48 +03:00
|
|
|
"--radiotype", dest="radiotype", required=False
|
2022-04-19 11:03:28 +03:00
|
|
|
)
|
2022-04-13 23:50:25 +03:00
|
|
|
|
2022-04-27 18:53:48 +03:00
|
|
|
self.parser_generate.add_argument("--obdata", dest="obdata", required=False)
|
2022-06-21 17:11:34 +03:00
|
|
|
self.parser_generate.add_argument("--splash", dest="splash", required=False)
|
2022-06-09 12:07:42 +03:00
|
|
|
self.parser_generate.add_argument(
|
|
|
|
"--I-understand-what-I-am-doing", dest="disclaimer", required=False
|
|
|
|
)
|
2023-09-19 17:22:21 +03:00
|
|
|
self.parser_generate.add_argument(
|
|
|
|
"--stackversion", dest="stack_version", required=False, default=""
|
|
|
|
)
|
2022-04-27 18:53:48 +03:00
|
|
|
|
2022-04-13 23:50:25 +03:00
|
|
|
self.parser_generate.set_defaults(func=self.generate)
|
|
|
|
|
|
|
|
def generate(self):
|
2022-10-13 18:05:07 +03:00
|
|
|
stage_basename = "updater.bin" # used to be basename(self.args.stage)
|
2022-10-14 21:07:23 +03:00
|
|
|
dfu_basename = (
|
|
|
|
"firmware.dfu" if self.args.dfu else ""
|
|
|
|
) # used to be basename(self.args.dfu)
|
|
|
|
radiobin_basename = (
|
|
|
|
"radio.bin" if self.args.radiobin else ""
|
|
|
|
) # used to be basename(self.args.radiobin)
|
2022-04-19 22:02:37 +03:00
|
|
|
resources_basename = ""
|
2022-04-13 23:50:25 +03:00
|
|
|
|
2022-04-27 18:53:48 +03:00
|
|
|
radio_version = 0
|
|
|
|
radio_meta = None
|
|
|
|
radio_addr = self.args.radioaddr
|
|
|
|
if self.args.radiobin:
|
|
|
|
if not self.args.radiotype:
|
|
|
|
raise ValueError("Missing --radiotype")
|
|
|
|
radio_meta = CoproBinary(self.args.radiobin)
|
2023-09-19 17:22:21 +03:00
|
|
|
if self.args.stack_version:
|
|
|
|
actual_stack_version_str = f"{radio_meta.img_sig.version_major}.{radio_meta.img_sig.version_minor}.{radio_meta.img_sig.version_sub}"
|
|
|
|
if actual_stack_version_str != self.args.stack_version:
|
|
|
|
self.logger.error(
|
|
|
|
f"Stack version mismatch: expected {self.args.stack_version}, actual {actual_stack_version_str}"
|
|
|
|
)
|
|
|
|
return 1
|
2022-04-27 18:53:48 +03:00
|
|
|
radio_version = self.copro_version_as_int(radio_meta, self.args.radiotype)
|
2022-06-09 12:07:42 +03:00
|
|
|
if (
|
|
|
|
get_stack_type(self.args.radiotype) not in self.WHITELISTED_STACK_TYPES
|
|
|
|
and self.args.disclaimer != "yes"
|
|
|
|
):
|
|
|
|
self.logger.error(
|
|
|
|
f"You are trying to bundle a non-standard stack type '{self.args.radiotype}'."
|
|
|
|
)
|
|
|
|
self.disclaimer()
|
|
|
|
return 1
|
|
|
|
|
2022-04-27 18:53:48 +03:00
|
|
|
if radio_addr == 0:
|
|
|
|
radio_addr = radio_meta.get_flash_load_addr()
|
|
|
|
self.logger.info(
|
2022-06-09 12:07:42 +03:00
|
|
|
f"Using guessed radio address 0x{radio_addr:08X}, verify with Release_Notes"
|
2022-04-27 18:53:48 +03:00
|
|
|
" or specify --radioaddr"
|
|
|
|
)
|
|
|
|
|
2022-04-13 23:50:25 +03:00
|
|
|
if not exists(self.args.directory):
|
2022-04-19 11:03:28 +03:00
|
|
|
os.makedirs(self.args.directory)
|
2022-04-13 23:50:25 +03:00
|
|
|
|
|
|
|
shutil.copyfile(self.args.stage, join(self.args.directory, stage_basename))
|
2022-06-09 12:07:42 +03:00
|
|
|
dfu_size = 0
|
2022-04-27 18:53:48 +03:00
|
|
|
if self.args.dfu:
|
2022-06-09 12:07:42 +03:00
|
|
|
dfu_size = os.stat(self.args.dfu).st_size
|
2022-04-27 18:53:48 +03:00
|
|
|
shutil.copyfile(self.args.dfu, join(self.args.directory, dfu_basename))
|
2022-04-19 11:03:28 +03:00
|
|
|
if radiobin_basename:
|
|
|
|
shutil.copyfile(
|
|
|
|
self.args.radiobin, join(self.args.directory, radiobin_basename)
|
|
|
|
)
|
2022-04-19 22:02:37 +03:00
|
|
|
if self.args.resources:
|
|
|
|
resources_basename = self.RESOURCE_FILE_NAME
|
2022-10-13 18:05:07 +03:00
|
|
|
if not self.package_resources(
|
2022-04-19 22:02:37 +03:00
|
|
|
self.args.resources, join(self.args.directory, resources_basename)
|
2022-10-13 18:05:07 +03:00
|
|
|
):
|
|
|
|
return 3
|
2022-04-13 23:50:25 +03:00
|
|
|
|
2022-06-09 12:07:42 +03:00
|
|
|
if not self.layout_check(dfu_size, radio_addr):
|
|
|
|
self.logger.warn("Memory layout looks suspicious")
|
|
|
|
if not self.args.disclaimer == "yes":
|
|
|
|
self.disclaimer()
|
|
|
|
return 2
|
|
|
|
|
2022-06-21 17:11:34 +03:00
|
|
|
if self.args.splash:
|
|
|
|
splash_args = [
|
|
|
|
"-i",
|
|
|
|
self.args.splash,
|
|
|
|
"-o",
|
|
|
|
join(self.args.directory, self.SPLASH_BIN_NAME),
|
|
|
|
]
|
|
|
|
if splash_code := SlideshowMain(no_exit=True)(splash_args):
|
|
|
|
self.logger.error(
|
|
|
|
f"Failed to convert splash screen data: {splash_code}"
|
|
|
|
)
|
|
|
|
return splash_code
|
|
|
|
|
2022-04-13 23:50:25 +03:00
|
|
|
file = FlipperFormatFile()
|
2022-05-11 12:45:01 +03:00
|
|
|
file.setHeader(
|
|
|
|
"Flipper firmware upgrade configuration", self.UPDATE_MANIFEST_VERSION
|
|
|
|
)
|
2022-04-13 23:50:25 +03:00
|
|
|
file.writeKey("Info", self.args.version)
|
|
|
|
file.writeKey("Target", self.args.target[1:]) # dirty 'f' strip
|
|
|
|
file.writeKey("Loader", stage_basename)
|
|
|
|
file.writeComment("little-endian hex!")
|
|
|
|
file.writeKey("Loader CRC", self.int2ffhex(self.crc(self.args.stage)))
|
|
|
|
file.writeKey("Firmware", dfu_basename)
|
2022-04-19 11:03:28 +03:00
|
|
|
file.writeKey("Radio", radiobin_basename or "")
|
2022-04-27 18:53:48 +03:00
|
|
|
file.writeKey("Radio address", self.int2ffhex(radio_addr))
|
2022-05-11 12:45:01 +03:00
|
|
|
file.writeKey("Radio version", self.int2ffhex(radio_version, 12))
|
2022-04-19 11:03:28 +03:00
|
|
|
if radiobin_basename:
|
|
|
|
file.writeKey("Radio CRC", self.int2ffhex(self.crc(self.args.radiobin)))
|
|
|
|
else:
|
|
|
|
file.writeKey("Radio CRC", self.int2ffhex(0))
|
2022-04-19 22:02:37 +03:00
|
|
|
file.writeKey("Resources", resources_basename)
|
2022-06-09 12:07:42 +03:00
|
|
|
obvalues = ObReferenceValues((), (), ())
|
2022-04-27 18:53:48 +03:00
|
|
|
if self.args.obdata:
|
|
|
|
obd = OptionBytesData(self.args.obdata)
|
|
|
|
obvalues = obd.gen_values().export()
|
2022-06-09 12:07:42 +03:00
|
|
|
file.writeComment(
|
|
|
|
"NEVER EVER MESS WITH THESE VALUES, YOU WILL BRICK YOUR DEVICE"
|
|
|
|
)
|
|
|
|
file.writeKey("OB reference", self.bytes2ffhex(obvalues.reference))
|
|
|
|
file.writeKey("OB mask", self.bytes2ffhex(obvalues.compare_mask))
|
|
|
|
file.writeKey("OB write mask", self.bytes2ffhex(obvalues.write_mask))
|
2022-06-21 17:11:34 +03:00
|
|
|
file.writeKey("Splashscreen", self.SPLASH_BIN_NAME if self.args.splash else "")
|
2022-04-19 22:02:37 +03:00
|
|
|
file.save(join(self.args.directory, self.UPDATE_MANIFEST_NAME))
|
2022-04-13 23:50:25 +03:00
|
|
|
|
|
|
|
return 0
|
|
|
|
|
2022-06-09 12:07:42 +03:00
|
|
|
def layout_check(self, fw_size, radio_addr):
|
|
|
|
if fw_size == 0 or radio_addr == 0:
|
|
|
|
self.logger.info("Cannot validate layout for partial package")
|
|
|
|
return True
|
|
|
|
|
|
|
|
lfs_span = radio_addr - self.FLASH_BASE - fw_size
|
|
|
|
self.logger.debug(f"Expected LFS size: {lfs_span}")
|
|
|
|
lfs_span_pages = lfs_span / (4 * 1024)
|
|
|
|
if lfs_span_pages < self.MIN_LFS_PAGES:
|
|
|
|
self.logger.warn(
|
|
|
|
f"Expected LFS size is too small (~{int(lfs_span_pages)} pages)"
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def disclaimer(self):
|
|
|
|
self.logger.error(
|
2023-02-25 16:41:49 +03:00
|
|
|
"You might brick your device into a state in which you'd need an SWD programmer to fix it."
|
2022-06-09 12:07:42 +03:00
|
|
|
)
|
|
|
|
self.logger.error(
|
|
|
|
"Please confirm that you REALLY want to do that with --I-understand-what-I-am-doing=yes"
|
|
|
|
)
|
|
|
|
|
2022-10-13 18:05:07 +03:00
|
|
|
def _tar_filter(self, tarinfo: tarfile.TarInfo):
|
|
|
|
if len(tarinfo.name) > self.RESOURCE_ENTRY_NAME_MAX_LENGTH:
|
|
|
|
self.logger.error(
|
|
|
|
f"Cannot package resource: name '{tarinfo.name}' too long"
|
|
|
|
)
|
|
|
|
raise ValueError("Resource name too long")
|
2024-06-30 13:38:48 +03:00
|
|
|
return tar_sanitizer_filter(tarinfo)
|
2022-10-13 18:05:07 +03:00
|
|
|
|
2022-04-19 22:02:37 +03:00
|
|
|
def package_resources(self, srcdir: str, dst_name: str):
|
2022-10-13 18:05:07 +03:00
|
|
|
try:
|
2024-06-30 13:38:48 +03:00
|
|
|
src_size, compressed_size = compress_tree_tarball(
|
|
|
|
srcdir, dst_name, filter=self._tar_filter
|
|
|
|
)
|
|
|
|
|
|
|
|
self.logger.info(
|
|
|
|
f"Resources compression ratio: {compressed_size * 100 / src_size:.2f}%"
|
|
|
|
)
|
2022-10-13 18:05:07 +03:00
|
|
|
return True
|
2024-06-30 13:38:48 +03:00
|
|
|
except Exception as e:
|
2022-10-13 18:05:07 +03:00
|
|
|
self.logger.error(f"Cannot package resources: {e}")
|
|
|
|
return False
|
2022-04-19 11:03:28 +03:00
|
|
|
|
2022-04-27 18:53:48 +03:00
|
|
|
@staticmethod
|
|
|
|
def copro_version_as_int(coprometa, stacktype):
|
|
|
|
major = coprometa.img_sig.version_major
|
|
|
|
minor = coprometa.img_sig.version_minor
|
|
|
|
sub = coprometa.img_sig.version_sub
|
|
|
|
branch = coprometa.img_sig.version_branch
|
|
|
|
release = coprometa.img_sig.version_build
|
|
|
|
stype = get_stack_type(stacktype)
|
|
|
|
return (
|
|
|
|
major
|
|
|
|
| (minor << 8)
|
|
|
|
| (sub << 16)
|
|
|
|
| (branch << 24)
|
|
|
|
| (release << 32)
|
|
|
|
| (stype << 40)
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def bytes2ffhex(value: bytes):
|
|
|
|
return " ".join(f"{b:02X}" for b in value)
|
|
|
|
|
2022-04-13 23:50:25 +03:00
|
|
|
@staticmethod
|
2022-05-11 12:45:01 +03:00
|
|
|
def int2ffhex(value: int, n_hex_syms=8):
|
2022-04-27 18:53:48 +03:00
|
|
|
if value:
|
2022-09-26 14:03:21 +03:00
|
|
|
n_hex_syms = max(math.ceil(math.ceil(math.log2(value)) / 8) * 2, n_hex_syms)
|
2022-05-11 12:45:01 +03:00
|
|
|
fmtstr = f"%0{n_hex_syms}X"
|
2022-04-27 18:53:48 +03:00
|
|
|
hexstr = fmtstr % value
|
2022-04-13 23:50:25 +03:00
|
|
|
return " ".join(list(Main.batch(hexstr, 2))[::-1])
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def crc(fileName):
|
|
|
|
prev = 0
|
|
|
|
with open(fileName, "rb") as file:
|
|
|
|
for eachLine in file:
|
|
|
|
prev = zlib.crc32(eachLine, prev)
|
|
|
|
return prev & 0xFFFFFFFF
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def batch(iterable, n=1):
|
2023-05-03 08:48:49 +03:00
|
|
|
iterable_len = len(iterable)
|
|
|
|
for ndx in range(0, iterable_len, n):
|
|
|
|
yield iterable[ndx : min(ndx + n, iterable_len)]
|
2022-04-13 23:50:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
Main()()
|