mirror of
https://github.com/flipperdevices/flipperzero-firmware.git
synced 2025-12-12 20:59:50 +04:00
[FL-1970, FL-1965, FL-1872, FL-1689] Python framework, Scripts and fixes (#779)
* Scripts: add flipper lib, migrate ob to flipper lib, update ob.data * Makefile: speedup build with phony target for .d files * FuriHal,U8G2: full MGG display support and ERC contrast tuning. * Desktop: fix dolphin rename artifact. * Scripts: port otp.py to flipper scripting lib. * Scripts: add wipe and core1 flashing to flash.py, remove obsolete shell scripts * Scripts: replace core1 flashing script with global makefile. * Scripts: final touches and migration to python. Root Makefile for everything.
This commit is contained in:
116
scripts/ob.py
116
scripts/ob.py
@@ -6,12 +6,12 @@ import subprocess
|
||||
import sys
|
||||
import os
|
||||
|
||||
from flipper.app import App
|
||||
from flipper.cube import CubeProgrammer
|
||||
|
||||
class Main:
|
||||
def __init__(self):
|
||||
# command args
|
||||
self.parser = argparse.ArgumentParser()
|
||||
self.parser.add_argument("-d", "--debug", action="store_true", help="Debug")
|
||||
|
||||
class Main(App):
|
||||
def init(self):
|
||||
self.subparsers = self.parser.add_subparsers(help="sub-command help")
|
||||
self.parser_check = self.subparsers.add_parser(
|
||||
"check", help="Check Option Bytes"
|
||||
@@ -20,39 +20,16 @@ class Main:
|
||||
"--port", type=str, help="Port to connect: swd or usb1", default="swd"
|
||||
)
|
||||
self.parser_check.set_defaults(func=self.check)
|
||||
# Set command
|
||||
self.parser_set = self.subparsers.add_parser("set", help="Set Option Bytes")
|
||||
self.parser_set.add_argument(
|
||||
"--port", type=str, help="Port to connect: swd or usb1", default="swd"
|
||||
)
|
||||
self.parser_set.set_defaults(func=self.set)
|
||||
# logging
|
||||
self.logger = logging.getLogger()
|
||||
# OB
|
||||
self.ob = {}
|
||||
|
||||
def __call__(self):
|
||||
self.args = self.parser.parse_args()
|
||||
if "func" not in self.args:
|
||||
self.parser.error("Choose something to do")
|
||||
# configure log output
|
||||
self.log_level = logging.DEBUG if self.args.debug else logging.INFO
|
||||
self.logger.setLevel(self.log_level)
|
||||
self.handler = logging.StreamHandler(sys.stdout)
|
||||
self.handler.setLevel(self.log_level)
|
||||
self.formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s")
|
||||
self.handler.setFormatter(self.formatter)
|
||||
self.logger.addHandler(self.handler)
|
||||
# execute requested function
|
||||
self.loadOB()
|
||||
|
||||
return_code = self.args.func()
|
||||
if isinstance(return_code, int):
|
||||
return return_code
|
||||
else:
|
||||
self.logger.error(f"Forgotten return code")
|
||||
return 255
|
||||
|
||||
def loadOB(self):
|
||||
def before(self):
|
||||
self.logger.info(f"Loading Option Bytes data")
|
||||
file_path = os.path.join(os.path.dirname(sys.argv[0]), "ob.data")
|
||||
file = open(file_path, "r")
|
||||
@@ -62,47 +39,8 @@ class Main:
|
||||
|
||||
def check(self):
|
||||
self.logger.info(f"Checking Option Bytes")
|
||||
try:
|
||||
output = subprocess.check_output(
|
||||
[
|
||||
"STM32_Programmer_CLI",
|
||||
"-q",
|
||||
"-c",
|
||||
f"port={self.args.port}",
|
||||
"-ob displ",
|
||||
]
|
||||
)
|
||||
assert output
|
||||
except subprocess.CalledProcessError as e:
|
||||
self.logger.error(e.output.decode())
|
||||
self.logger.error(f"Failed to call STM32_Programmer_CLI")
|
||||
return 127
|
||||
except Exception as e:
|
||||
self.logger.error(f"Failed to call STM32_Programmer_CLI")
|
||||
self.logger.exception(e)
|
||||
return 126
|
||||
ob_correct = True
|
||||
for line in output.decode().split("\n"):
|
||||
line = line.strip()
|
||||
if not ":" in line:
|
||||
self.logger.debug(f"Skipping line: {line}")
|
||||
continue
|
||||
key, data = line.split(":", 1)
|
||||
key = key.strip()
|
||||
data = data.strip()
|
||||
if not key in self.ob.keys():
|
||||
self.logger.debug(f"Skipping key: {key}")
|
||||
continue
|
||||
self.logger.debug(f"Processing key: {key} {data}")
|
||||
value, comment = data.split(" ", 1)
|
||||
value = value.strip()
|
||||
comment = comment.strip()
|
||||
if self.ob[key][0] != value:
|
||||
self.logger.error(
|
||||
f"Invalid OB: {key} {value}, expected: {self.ob[key][0]}"
|
||||
)
|
||||
ob_correct = False
|
||||
if ob_correct:
|
||||
cp = CubeProgrammer(self.args.port)
|
||||
if cp.checkOptionBytes(self.ob):
|
||||
self.logger.info(f"OB Check OK")
|
||||
return 0
|
||||
else:
|
||||
@@ -111,34 +49,14 @@ class Main:
|
||||
|
||||
def set(self):
|
||||
self.logger.info(f"Setting Option Bytes")
|
||||
options = []
|
||||
for key, (value, attr) in self.ob.items():
|
||||
if "w" in attr:
|
||||
options.append(f"{key}={value}")
|
||||
try:
|
||||
output = subprocess.check_output(
|
||||
[
|
||||
"STM32_Programmer_CLI",
|
||||
"-q",
|
||||
"-c",
|
||||
f"port={self.args.port}",
|
||||
"-ob",
|
||||
*options,
|
||||
]
|
||||
)
|
||||
assert output
|
||||
self.logger.info(f"Success")
|
||||
except subprocess.CalledProcessError as e:
|
||||
self.logger.error(e.output.decode())
|
||||
self.logger.error(f"Failed to call STM32_Programmer_CLI")
|
||||
return 125
|
||||
except Exception as e:
|
||||
self.logger.error(f"Failed to call STM32_Programmer_CLI")
|
||||
self.logger.exception(e)
|
||||
return 124
|
||||
return 0
|
||||
cp = CubeProgrammer(self.args.port)
|
||||
if cp.setOptionBytes(self.ob):
|
||||
self.logger.info(f"OB Set OK")
|
||||
return 0
|
||||
else:
|
||||
self.logger.error(f"OB Set FAIL")
|
||||
return 255
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
return_code = Main()()
|
||||
exit(return_code)
|
||||
Main()()
|
||||
|
||||
Reference in New Issue
Block a user