2022-04-13 20:50:25 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from flipper.app import App
|
|
|
|
from flipper.utils.fff import FlipperFormatFile
|
2022-04-27 15:53:48 +00:00
|
|
|
from flipper.assets.coprobin import CoproBinary, get_stack_type
|
|
|
|
from flipper.assets.obdata import OptionBytesData
|
2022-04-13 20:50:25 +00:00
|
|
|
from os.path import basename, join, exists
|
2022-04-19 08:03:28 +00:00
|
|
|
import os
|
2022-04-13 20:50:25 +00:00
|
|
|
import shutil
|
|
|
|
import zlib
|
2022-04-19 08:03:28 +00:00
|
|
|
import tarfile
|
2022-04-27 15:53:48 +00:00
|
|
|
import math
|
2022-04-13 20:50:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Main(App):
|
2022-05-11 09:45:01 +00:00
|
|
|
UPDATE_MANIFEST_VERSION = 2
|
2022-04-19 19:02:37 +00:00
|
|
|
UPDATE_MANIFEST_NAME = "update.fuf"
|
|
|
|
|
2022-04-19 08:03:28 +00:00
|
|
|
# No compression, plain tar
|
2022-04-19 19:02:37 +00:00
|
|
|
RESOURCE_TAR_MODE = "w:"
|
|
|
|
RESOURCE_TAR_FORMAT = tarfile.USTAR_FORMAT
|
|
|
|
RESOURCE_FILE_NAME = "resources.tar"
|
2022-04-19 08:03:28 +00:00
|
|
|
|
2022-04-13 20:50:25 +00: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 15:53:48 +00:00
|
|
|
self.parser_generate.add_argument(
|
|
|
|
"--dfu", dest="dfu", default="", required=False
|
|
|
|
)
|
2022-04-19 19:02:37 +00:00
|
|
|
self.parser_generate.add_argument("-r", dest="resources", required=False)
|
|
|
|
self.parser_generate.add_argument("--stage", dest="stage", required=True)
|
2022-04-19 08:03:28 +00:00
|
|
|
self.parser_generate.add_argument(
|
2022-04-19 19:02:37 +00:00
|
|
|
"--radio", dest="radiobin", default="", required=False
|
2022-04-19 08:03:28 +00:00
|
|
|
)
|
2022-04-13 20:50:25 +00:00
|
|
|
self.parser_generate.add_argument(
|
2022-04-27 15:53:48 +00:00
|
|
|
"--radioaddr",
|
|
|
|
dest="radioaddr",
|
|
|
|
type=lambda x: int(x, 16),
|
|
|
|
default=0,
|
|
|
|
required=False,
|
2022-04-13 20:50:25 +00:00
|
|
|
)
|
2022-04-27 15:53:48 +00:00
|
|
|
|
2022-04-19 08:03:28 +00:00
|
|
|
self.parser_generate.add_argument(
|
2022-04-27 15:53:48 +00:00
|
|
|
"--radiotype", dest="radiotype", required=False
|
2022-04-19 08:03:28 +00:00
|
|
|
)
|
2022-04-13 20:50:25 +00:00
|
|
|
|
2022-04-27 15:53:48 +00:00
|
|
|
self.parser_generate.add_argument("--obdata", dest="obdata", required=False)
|
|
|
|
|
2022-04-13 20:50:25 +00:00
|
|
|
self.parser_generate.set_defaults(func=self.generate)
|
|
|
|
|
|
|
|
def generate(self):
|
|
|
|
stage_basename = basename(self.args.stage)
|
|
|
|
dfu_basename = basename(self.args.dfu)
|
2022-04-19 08:03:28 +00:00
|
|
|
radiobin_basename = basename(self.args.radiobin)
|
2022-04-19 19:02:37 +00:00
|
|
|
resources_basename = ""
|
2022-04-13 20:50:25 +00:00
|
|
|
|
2022-04-27 15:53:48 +00: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)
|
|
|
|
radio_version = self.copro_version_as_int(radio_meta, self.args.radiotype)
|
|
|
|
if radio_addr == 0:
|
|
|
|
radio_addr = radio_meta.get_flash_load_addr()
|
|
|
|
self.logger.info(
|
|
|
|
f"Using guessed radio address 0x{radio_addr:X}, verify with Release_Notes"
|
|
|
|
" or specify --radioaddr"
|
|
|
|
)
|
|
|
|
|
2022-04-13 20:50:25 +00:00
|
|
|
if not exists(self.args.directory):
|
2022-04-19 08:03:28 +00:00
|
|
|
os.makedirs(self.args.directory)
|
2022-04-13 20:50:25 +00:00
|
|
|
|
|
|
|
shutil.copyfile(self.args.stage, join(self.args.directory, stage_basename))
|
2022-04-27 15:53:48 +00:00
|
|
|
if self.args.dfu:
|
|
|
|
shutil.copyfile(self.args.dfu, join(self.args.directory, dfu_basename))
|
2022-04-19 08:03:28 +00:00
|
|
|
if radiobin_basename:
|
|
|
|
shutil.copyfile(
|
|
|
|
self.args.radiobin, join(self.args.directory, radiobin_basename)
|
|
|
|
)
|
2022-04-19 19:02:37 +00:00
|
|
|
if self.args.resources:
|
|
|
|
resources_basename = self.RESOURCE_FILE_NAME
|
|
|
|
self.package_resources(
|
|
|
|
self.args.resources, join(self.args.directory, resources_basename)
|
2022-04-19 08:03:28 +00:00
|
|
|
)
|
2022-04-13 20:50:25 +00:00
|
|
|
|
|
|
|
file = FlipperFormatFile()
|
2022-05-11 09:45:01 +00:00
|
|
|
file.setHeader(
|
|
|
|
"Flipper firmware upgrade configuration", self.UPDATE_MANIFEST_VERSION
|
|
|
|
)
|
2022-04-13 20:50:25 +00: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 08:03:28 +00:00
|
|
|
file.writeKey("Radio", radiobin_basename or "")
|
2022-04-27 15:53:48 +00:00
|
|
|
file.writeKey("Radio address", self.int2ffhex(radio_addr))
|
2022-05-11 09:45:01 +00:00
|
|
|
file.writeKey("Radio version", self.int2ffhex(radio_version, 12))
|
2022-04-19 08:03:28 +00: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 19:02:37 +00:00
|
|
|
file.writeKey("Resources", resources_basename)
|
2022-04-27 15:53:48 +00:00
|
|
|
file.writeComment(
|
|
|
|
"NEVER EVER MESS WITH THESE VALUES, YOU WILL BRICK YOUR DEVICE"
|
|
|
|
)
|
|
|
|
if self.args.obdata:
|
|
|
|
obd = OptionBytesData(self.args.obdata)
|
|
|
|
obvalues = obd.gen_values().export()
|
|
|
|
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-04-19 19:02:37 +00:00
|
|
|
file.save(join(self.args.directory, self.UPDATE_MANIFEST_NAME))
|
2022-04-13 20:50:25 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
|
2022-04-19 19:02:37 +00:00
|
|
|
def package_resources(self, srcdir: str, dst_name: str):
|
2022-04-19 08:03:28 +00:00
|
|
|
with tarfile.open(
|
2022-04-19 19:02:37 +00:00
|
|
|
dst_name, self.RESOURCE_TAR_MODE, format=self.RESOURCE_TAR_FORMAT
|
2022-04-19 08:03:28 +00:00
|
|
|
) as tarball:
|
|
|
|
tarball.add(srcdir, arcname="")
|
|
|
|
|
2022-04-27 15:53:48 +00: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 20:50:25 +00:00
|
|
|
@staticmethod
|
2022-05-11 09:45:01 +00:00
|
|
|
def int2ffhex(value: int, n_hex_syms=8):
|
2022-04-27 15:53:48 +00:00
|
|
|
if value:
|
2022-05-11 09:45:01 +00:00
|
|
|
n_hex_syms = math.ceil(math.ceil(math.log2(value)) / 8) * 2
|
|
|
|
fmtstr = f"%0{n_hex_syms}X"
|
2022-04-27 15:53:48 +00:00
|
|
|
hexstr = fmtstr % value
|
2022-04-13 20:50:25 +00: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):
|
|
|
|
l = len(iterable)
|
|
|
|
for ndx in range(0, l, n):
|
|
|
|
yield iterable[ndx : min(ndx + n, l)]
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
Main()()
|