2021-06-23 14:58:44 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2022-12-27 12:59:36 +00:00
|
|
|
from os import path
|
2021-06-23 14:58:44 +00:00
|
|
|
|
2021-10-21 12:24:34 +00:00
|
|
|
from flipper.app import App
|
2022-12-27 12:59:36 +00:00
|
|
|
from flipper.utils.programmer_openocd import OpenOCDProgrammer
|
2021-06-23 14:58:44 +00:00
|
|
|
|
2021-10-21 12:24:34 +00:00
|
|
|
|
|
|
|
class Main(App):
|
|
|
|
def init(self):
|
2022-12-27 12:59:36 +00:00
|
|
|
# Subparsers
|
2021-06-23 14:58:44 +00:00
|
|
|
self.subparsers = self.parser.add_subparsers(help="sub-command help")
|
2022-12-27 12:59:36 +00:00
|
|
|
|
|
|
|
# Check command
|
2021-06-23 14:58:44 +00:00
|
|
|
self.parser_check = self.subparsers.add_parser(
|
|
|
|
"check", help="Check Option Bytes"
|
|
|
|
)
|
2022-12-27 12:59:36 +00:00
|
|
|
self._add_args(self.parser_check)
|
2021-06-23 14:58:44 +00:00
|
|
|
self.parser_check.set_defaults(func=self.check)
|
2022-12-27 12:59:36 +00:00
|
|
|
|
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")
|
2022-12-27 12:59:36 +00:00
|
|
|
self._add_args(self.parser_set)
|
2021-06-23 14:58:44 +00:00
|
|
|
self.parser_set.set_defaults(func=self.set)
|
2023-09-11 06:23:00 +00:00
|
|
|
|
|
|
|
# Recover command
|
2023-08-10 09:45:17 +00:00
|
|
|
self.parser_recover = self.subparsers.add_parser(
|
|
|
|
"recover", help="Recover Option Bytes"
|
|
|
|
)
|
|
|
|
self._add_args(self.parser_recover)
|
|
|
|
self.parser_recover.set_defaults(func=self.recover)
|
2021-06-23 14:58:44 +00:00
|
|
|
|
2022-12-27 12:59:36 +00:00
|
|
|
def _add_args(self, parser):
|
|
|
|
parser.add_argument(
|
|
|
|
"--port-base", type=int, help="OpenOCD port base", default=3333
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--interface",
|
|
|
|
type=str,
|
|
|
|
help="OpenOCD interface",
|
|
|
|
default="interface/cmsis-dap.cfg",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--serial", type=str, help="OpenOCD interface serial number"
|
|
|
|
)
|
2021-11-23 16:13:34 +00:00
|
|
|
parser.add_argument(
|
2022-12-27 12:59:36 +00:00
|
|
|
"--ob-path",
|
|
|
|
type=str,
|
|
|
|
help="Option bytes file",
|
|
|
|
default=path.join(path.dirname(__file__), "ob.data"),
|
2021-11-23 16:13:34 +00:00
|
|
|
)
|
2021-06-23 14:58:44 +00:00
|
|
|
|
|
|
|
def check(self):
|
2023-05-03 05:48:49 +00:00
|
|
|
self.logger.info("Checking Option Bytes")
|
2022-12-27 12:59:36 +00:00
|
|
|
|
|
|
|
# OpenOCD
|
|
|
|
openocd = OpenOCDProgrammer(
|
|
|
|
self.args.interface,
|
|
|
|
self.args.port_base,
|
|
|
|
self.args.serial,
|
|
|
|
)
|
|
|
|
|
|
|
|
return_code = 1
|
|
|
|
if openocd.option_bytes_validate(self.args.ob_path):
|
|
|
|
return_code = 0
|
|
|
|
|
|
|
|
return return_code
|
2021-06-23 14:58:44 +00:00
|
|
|
|
|
|
|
def set(self):
|
2023-05-03 05:48:49 +00:00
|
|
|
self.logger.info("Setting Option Bytes")
|
2022-12-27 12:59:36 +00:00
|
|
|
|
|
|
|
# OpenOCD
|
|
|
|
openocd = OpenOCDProgrammer(
|
|
|
|
self.args.interface,
|
|
|
|
self.args.port_base,
|
|
|
|
self.args.serial,
|
|
|
|
)
|
|
|
|
|
|
|
|
return_code = 1
|
|
|
|
if openocd.option_bytes_set(self.args.ob_path):
|
|
|
|
return_code = 0
|
|
|
|
|
|
|
|
return return_code
|
2021-06-23 14:58:44 +00:00
|
|
|
|
2023-08-10 09:45:17 +00:00
|
|
|
def recover(self):
|
|
|
|
self.logger.info("Setting Option Bytes")
|
|
|
|
|
|
|
|
# OpenOCD
|
|
|
|
openocd = OpenOCDProgrammer(
|
|
|
|
self.args.interface,
|
|
|
|
self.args.port_base,
|
|
|
|
self.args.serial,
|
|
|
|
)
|
|
|
|
|
|
|
|
openocd.option_bytes_recover()
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
2021-06-23 14:58:44 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-10-21 12:24:34 +00:00
|
|
|
Main()()
|