2021-06-23 14:58:44 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import argparse
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
2021-10-21 12:24:34 +00:00
|
|
|
from flipper.app import App
|
|
|
|
from flipper.cube import CubeProgrammer
|
2021-06-23 14:58:44 +00:00
|
|
|
|
2021-10-21 12:24:34 +00:00
|
|
|
|
|
|
|
class Main(App):
|
|
|
|
def init(self):
|
2021-06-23 14:58:44 +00:00
|
|
|
self.subparsers = self.parser.add_subparsers(help="sub-command help")
|
|
|
|
self.parser_check = self.subparsers.add_parser(
|
|
|
|
"check", help="Check Option Bytes"
|
|
|
|
)
|
2021-11-23 16:13:34 +00:00
|
|
|
self._addArgsSWD(self.parser_check)
|
2021-06-23 14:58:44 +00:00
|
|
|
self.parser_check.set_defaults(func=self.check)
|
2021-10-21 12:24:34 +00:00
|
|
|
# Set command
|
2021-06-23 14:58:44 +00:00
|
|
|
self.parser_set = self.subparsers.add_parser("set", help="Set Option Bytes")
|
2021-11-23 16:13:34 +00:00
|
|
|
self._addArgsSWD(self.parser_set)
|
2021-06-23 14:58:44 +00:00
|
|
|
self.parser_set.set_defaults(func=self.set)
|
|
|
|
# OB
|
|
|
|
self.ob = {}
|
|
|
|
|
2021-11-23 16:13:34 +00:00
|
|
|
def _addArgsSWD(self, parser):
|
|
|
|
parser.add_argument(
|
|
|
|
"--port", type=str, help="Port to connect: swd or usb1", default="swd"
|
|
|
|
)
|
|
|
|
parser.add_argument("--serial", type=str, help="ST-Link Serial Number")
|
|
|
|
|
|
|
|
def _getCubeParams(self):
|
|
|
|
return {
|
|
|
|
"port": self.args.port,
|
|
|
|
"serial": self.args.serial,
|
|
|
|
}
|
|
|
|
|
2021-10-21 12:24:34 +00:00
|
|
|
def before(self):
|
2021-06-23 14:58:44 +00:00
|
|
|
self.logger.info(f"Loading Option Bytes data")
|
|
|
|
file_path = os.path.join(os.path.dirname(sys.argv[0]), "ob.data")
|
2022-06-26 12:00:03 +00:00
|
|
|
with open(file_path, "r") as file:
|
|
|
|
for line in file.readlines():
|
|
|
|
k, v, o = line.split(":")
|
|
|
|
self.ob[k.strip()] = v.strip(), o.strip()
|
2021-06-23 14:58:44 +00:00
|
|
|
|
|
|
|
def check(self):
|
|
|
|
self.logger.info(f"Checking Option Bytes")
|
2021-11-23 16:13:34 +00:00
|
|
|
cp = CubeProgrammer(self._getCubeParams())
|
2021-10-21 12:24:34 +00:00
|
|
|
if cp.checkOptionBytes(self.ob):
|
2021-06-23 14:58:44 +00:00
|
|
|
self.logger.info(f"OB Check OK")
|
2021-09-22 20:57:34 +00:00
|
|
|
return 0
|
2021-06-23 14:58:44 +00:00
|
|
|
else:
|
|
|
|
self.logger.error(f"OB Check FAIL")
|
2021-09-22 20:57:34 +00:00
|
|
|
return 255
|
2021-06-23 14:58:44 +00:00
|
|
|
|
|
|
|
def set(self):
|
|
|
|
self.logger.info(f"Setting Option Bytes")
|
2021-11-23 16:13:34 +00:00
|
|
|
cp = CubeProgrammer(self._getCubeParams())
|
2021-10-21 12:24:34 +00:00
|
|
|
if cp.setOptionBytes(self.ob):
|
|
|
|
self.logger.info(f"OB Set OK")
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
self.logger.error(f"OB Set FAIL")
|
|
|
|
return 255
|
2021-06-23 14:58:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-10-21 12:24:34 +00:00
|
|
|
Main()()
|