2021-06-23 17:58:44 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2023-05-03 08:48:49 +03:00
|
|
|
import datetime
|
2021-06-23 17:58:44 +03:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import struct
|
2023-05-03 08:48:49 +03:00
|
|
|
|
|
|
|
from flipper.app import App
|
|
|
|
from flipper.utils.programmer_openocd import OpenOCDProgrammer, OpenOCDProgrammerResult
|
2021-06-23 17:58:44 +03:00
|
|
|
|
2021-07-22 05:49:23 +03:00
|
|
|
OTP_MAGIC = 0xBABE
|
2021-10-15 14:05:14 +03:00
|
|
|
OTP_VERSION = 0x02
|
2021-07-22 05:49:23 +03:00
|
|
|
OTP_RESERVED = 0x00
|
|
|
|
|
|
|
|
OTP_COLORS = {
|
|
|
|
"unknown": 0x00,
|
|
|
|
"black": 0x01,
|
|
|
|
"white": 0x02,
|
2023-08-01 10:56:11 +03:00
|
|
|
"transparent": 0x03,
|
2021-07-22 05:49:23 +03:00
|
|
|
}
|
2021-10-15 14:05:14 +03:00
|
|
|
|
2021-07-22 05:49:23 +03:00
|
|
|
OTP_REGIONS = {
|
|
|
|
"unknown": 0x00,
|
2021-07-22 12:44:52 +03:00
|
|
|
"eu_ru": 0x01,
|
|
|
|
"us_ca_au": 0x02,
|
|
|
|
"jp": 0x03,
|
2022-08-11 19:10:50 +03:00
|
|
|
"world": 0x04,
|
2021-07-22 05:49:23 +03:00
|
|
|
}
|
|
|
|
|
2021-10-15 14:05:14 +03:00
|
|
|
OTP_DISPLAYS = {
|
|
|
|
"unknown": 0x00,
|
|
|
|
"erc": 0x01,
|
|
|
|
"mgg": 0x02,
|
|
|
|
}
|
2021-07-22 05:49:23 +03:00
|
|
|
|
2023-03-20 15:07:17 +03:00
|
|
|
|
|
|
|
class OTPException(Exception):
|
|
|
|
def __init__(self, message: str, result: OpenOCDProgrammerResult):
|
|
|
|
self.message = message
|
|
|
|
self.result = result
|
|
|
|
|
|
|
|
def get_exit_code(self) -> int:
|
|
|
|
return int(self.result.value)
|
2021-06-23 17:58:44 +03:00
|
|
|
|
2021-10-21 15:24:34 +03:00
|
|
|
|
|
|
|
class Main(App):
|
|
|
|
def init(self):
|
|
|
|
# SubParsers
|
2021-06-23 17:58:44 +03:00
|
|
|
self.subparsers = self.parser.add_subparsers(help="sub-command help")
|
2021-10-15 14:05:14 +03:00
|
|
|
# Generate All
|
|
|
|
self.parser_generate_all = self.subparsers.add_parser(
|
2021-07-22 05:49:23 +03:00
|
|
|
"generate", help="Generate OTP binary"
|
2021-06-23 17:58:44 +03:00
|
|
|
)
|
2021-11-23 19:13:34 +03:00
|
|
|
self._addFirstArgs(self.parser_generate_all)
|
|
|
|
self._addSecondArgs(self.parser_generate_all)
|
2021-10-15 14:05:14 +03:00
|
|
|
self.parser_generate_all.add_argument("file", help="Output file")
|
|
|
|
self.parser_generate_all.set_defaults(func=self.generate_all)
|
|
|
|
# Flash First
|
|
|
|
self.parser_flash_first = self.subparsers.add_parser(
|
|
|
|
"flash_first", help="Flash first block of OTP to device"
|
2021-07-22 05:49:23 +03:00
|
|
|
)
|
2022-12-27 15:59:36 +03:00
|
|
|
self._addArgsOpenOCD(self.parser_flash_first)
|
2021-11-23 19:13:34 +03:00
|
|
|
self._addFirstArgs(self.parser_flash_first)
|
2021-10-15 14:05:14 +03:00
|
|
|
self.parser_flash_first.set_defaults(func=self.flash_first)
|
|
|
|
# Flash Second
|
|
|
|
self.parser_flash_second = self.subparsers.add_parser(
|
|
|
|
"flash_second", help="Flash second block of OTP to device"
|
|
|
|
)
|
2022-12-27 15:59:36 +03:00
|
|
|
self._addArgsOpenOCD(self.parser_flash_second)
|
2021-11-23 19:13:34 +03:00
|
|
|
self._addSecondArgs(self.parser_flash_second)
|
2021-10-15 14:05:14 +03:00
|
|
|
self.parser_flash_second.set_defaults(func=self.flash_second)
|
|
|
|
# Flash All
|
|
|
|
self.parser_flash_all = self.subparsers.add_parser(
|
|
|
|
"flash_all", help="Flash OTP to device"
|
2021-07-22 05:49:23 +03:00
|
|
|
)
|
2022-12-27 15:59:36 +03:00
|
|
|
self._addArgsOpenOCD(self.parser_flash_all)
|
2021-11-23 19:13:34 +03:00
|
|
|
self._addFirstArgs(self.parser_flash_all)
|
|
|
|
self._addSecondArgs(self.parser_flash_all)
|
2021-10-15 14:05:14 +03:00
|
|
|
self.parser_flash_all.set_defaults(func=self.flash_all)
|
2021-06-23 17:58:44 +03:00
|
|
|
# logging
|
|
|
|
self.logger = logging.getLogger()
|
2021-07-22 05:49:23 +03:00
|
|
|
self.timestamp = datetime.datetime.now().timestamp()
|
2021-06-23 17:58:44 +03:00
|
|
|
|
2022-12-27 15:59:36 +03:00
|
|
|
def _addArgsOpenOCD(self, parser):
|
2021-10-15 14:05:14 +03:00
|
|
|
parser.add_argument(
|
2022-12-27 15:59:36 +03:00
|
|
|
"--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-10-15 14:05:14 +03:00
|
|
|
)
|
2021-11-23 19:13:34 +03:00
|
|
|
|
|
|
|
def _addFirstArgs(self, parser):
|
2021-10-15 14:05:14 +03:00
|
|
|
parser.add_argument("--version", type=int, help="Version", required=True)
|
|
|
|
parser.add_argument("--firmware", type=int, help="Firmware", required=True)
|
|
|
|
parser.add_argument("--body", type=int, help="Body", required=True)
|
|
|
|
parser.add_argument("--connect", type=int, help="Connect", required=True)
|
|
|
|
parser.add_argument("--display", type=str, help="Display", required=True)
|
|
|
|
|
2021-11-23 19:13:34 +03:00
|
|
|
def _addSecondArgs(self, parser):
|
2021-10-15 14:05:14 +03:00
|
|
|
parser.add_argument("--color", type=str, help="Color", required=True)
|
|
|
|
parser.add_argument("--region", type=str, help="Region", required=True)
|
2021-07-22 05:49:23 +03:00
|
|
|
parser.add_argument("--name", type=str, help="Name", required=True)
|
|
|
|
|
2021-11-23 19:13:34 +03:00
|
|
|
def _processFirstArgs(self):
|
2021-10-15 14:05:14 +03:00
|
|
|
if self.args.display not in OTP_DISPLAYS:
|
|
|
|
self.parser.error(f"Invalid display. Use one of {OTP_DISPLAYS.keys()}")
|
|
|
|
self.args.display = OTP_DISPLAYS[self.args.display]
|
|
|
|
|
2021-11-23 19:13:34 +03:00
|
|
|
def _processSecondArgs(self):
|
2021-07-22 05:49:23 +03:00
|
|
|
if self.args.color not in OTP_COLORS:
|
|
|
|
self.parser.error(f"Invalid color. Use one of {OTP_COLORS.keys()}")
|
|
|
|
self.args.color = OTP_COLORS[self.args.color]
|
2021-06-23 17:58:44 +03:00
|
|
|
|
2021-07-22 05:49:23 +03:00
|
|
|
if self.args.region not in OTP_REGIONS:
|
|
|
|
self.parser.error(f"Invalid region. Use one of {OTP_REGIONS.keys()}")
|
|
|
|
self.args.region = OTP_REGIONS[self.args.region]
|
2021-06-23 17:58:44 +03:00
|
|
|
|
2021-07-22 05:49:23 +03:00
|
|
|
if len(self.args.name) > 8:
|
|
|
|
self.parser.error("Name is too long. Max 8 symbols.")
|
2021-07-29 18:27:06 +03:00
|
|
|
if re.match(r"^[a-zA-Z0-9.]+$", self.args.name) is None:
|
2021-07-22 05:49:23 +03:00
|
|
|
self.parser.error(
|
|
|
|
"Name contains incorrect symbols. Only a-zA-Z0-9 allowed."
|
|
|
|
)
|
2021-06-23 17:58:44 +03:00
|
|
|
|
2021-11-23 19:13:34 +03:00
|
|
|
def _packFirst(self):
|
2021-07-22 05:49:23 +03:00
|
|
|
return struct.pack(
|
2021-10-15 14:05:14 +03:00
|
|
|
"<" "HBBL" "BBBBBBH",
|
2021-07-22 05:49:23 +03:00
|
|
|
OTP_MAGIC,
|
|
|
|
OTP_VERSION,
|
|
|
|
OTP_RESERVED,
|
|
|
|
int(self.timestamp),
|
2021-06-23 17:58:44 +03:00
|
|
|
self.args.version,
|
|
|
|
self.args.firmware,
|
|
|
|
self.args.body,
|
|
|
|
self.args.connect,
|
2021-10-15 14:05:14 +03:00
|
|
|
self.args.display,
|
|
|
|
OTP_RESERVED,
|
|
|
|
OTP_RESERVED,
|
|
|
|
)
|
|
|
|
|
2021-11-23 19:13:34 +03:00
|
|
|
def _packSecond(self):
|
2021-10-15 14:05:14 +03:00
|
|
|
return struct.pack(
|
|
|
|
"<" "BBHL" "8s",
|
2021-07-22 05:49:23 +03:00
|
|
|
self.args.color,
|
|
|
|
self.args.region,
|
2021-10-15 14:05:14 +03:00
|
|
|
OTP_RESERVED,
|
|
|
|
OTP_RESERVED,
|
2021-07-22 05:49:23 +03:00
|
|
|
self.args.name.encode("ascii"),
|
2021-06-23 17:58:44 +03:00
|
|
|
)
|
2021-07-22 05:49:23 +03:00
|
|
|
|
2021-10-15 14:05:14 +03:00
|
|
|
def generate_all(self):
|
2023-05-03 08:48:49 +03:00
|
|
|
self.logger.info("Generating OTP")
|
2021-11-23 19:13:34 +03:00
|
|
|
self._processFirstArgs()
|
|
|
|
self._processSecondArgs()
|
2022-06-26 15:00:03 +03:00
|
|
|
with open(f"{self.args.file}_first.bin", "wb") as file:
|
|
|
|
file.write(self._packFirst())
|
|
|
|
with open(f"{self.args.file}_second.bin", "wb") as file:
|
|
|
|
file.write(self._packSecond())
|
2021-10-21 15:24:34 +03:00
|
|
|
self.logger.info(
|
|
|
|
f"Generated files: {self.args.file}_first.bin and {self.args.file}_second.bin"
|
|
|
|
)
|
|
|
|
|
|
|
|
return 0
|
2021-10-15 14:05:14 +03:00
|
|
|
|
|
|
|
def flash_first(self):
|
2023-05-03 08:48:49 +03:00
|
|
|
self.logger.info("Flashing first block of OTP")
|
2021-06-23 17:58:44 +03:00
|
|
|
|
2021-11-23 19:13:34 +03:00
|
|
|
self._processFirstArgs()
|
2021-10-15 14:05:14 +03:00
|
|
|
|
|
|
|
filename = f"otp_unknown_first_{self.timestamp}.bin"
|
|
|
|
|
2021-10-21 15:24:34 +03:00
|
|
|
try:
|
2023-05-03 08:48:49 +03:00
|
|
|
self.logger.info("Packing binary data")
|
2022-06-26 15:00:03 +03:00
|
|
|
with open(filename, "wb") as file:
|
|
|
|
file.write(self._packFirst())
|
|
|
|
|
2023-05-03 08:48:49 +03:00
|
|
|
self.logger.info("Flashing OTP")
|
2022-12-27 15:59:36 +03:00
|
|
|
|
|
|
|
openocd = OpenOCDProgrammer(
|
|
|
|
self.args.interface,
|
|
|
|
self.args.port_base,
|
|
|
|
self.args.serial,
|
|
|
|
)
|
|
|
|
|
2023-03-20 15:07:17 +03:00
|
|
|
programmer_result = openocd.otp_write(0x1FFF7000, filename)
|
|
|
|
if programmer_result != OpenOCDProgrammerResult.Success:
|
|
|
|
raise OTPException("Failed to flash OTP", programmer_result)
|
2022-12-27 15:59:36 +03:00
|
|
|
|
2023-05-03 08:48:49 +03:00
|
|
|
self.logger.info("Flashed Successfully")
|
2023-03-20 15:07:17 +03:00
|
|
|
except OTPException as e:
|
2021-10-21 15:24:34 +03:00
|
|
|
self.logger.exception(e)
|
2023-03-20 15:07:17 +03:00
|
|
|
return e.get_exit_code()
|
2022-12-27 15:59:36 +03:00
|
|
|
finally:
|
|
|
|
os.remove(filename)
|
2021-10-15 14:05:14 +03:00
|
|
|
|
2021-10-25 18:19:48 +03:00
|
|
|
return 0
|
2021-10-15 14:05:14 +03:00
|
|
|
|
|
|
|
def flash_second(self):
|
2023-05-03 08:48:49 +03:00
|
|
|
self.logger.info("Flashing second block of OTP")
|
2021-10-15 14:05:14 +03:00
|
|
|
|
2021-11-23 19:13:34 +03:00
|
|
|
self._processSecondArgs()
|
2021-10-15 14:05:14 +03:00
|
|
|
|
|
|
|
filename = f"otp_{self.args.name}_second_{self.timestamp}.bin"
|
|
|
|
|
2021-10-21 15:24:34 +03:00
|
|
|
try:
|
2023-05-03 08:48:49 +03:00
|
|
|
self.logger.info("Packing binary data")
|
2022-06-26 15:00:03 +03:00
|
|
|
with open(filename, "wb") as file:
|
|
|
|
file.write(self._packSecond())
|
|
|
|
|
2023-05-03 08:48:49 +03:00
|
|
|
self.logger.info("Flashing OTP")
|
2022-12-27 15:59:36 +03:00
|
|
|
|
|
|
|
openocd = OpenOCDProgrammer(
|
|
|
|
self.args.interface,
|
|
|
|
self.args.port_base,
|
|
|
|
self.args.serial,
|
|
|
|
)
|
|
|
|
|
2023-03-20 15:07:17 +03:00
|
|
|
programmer_result = openocd.otp_write(0x1FFF7010, filename)
|
|
|
|
if programmer_result != OpenOCDProgrammerResult.Success:
|
|
|
|
raise OTPException("Failed to flash OTP", programmer_result)
|
2022-12-27 15:59:36 +03:00
|
|
|
|
2023-05-03 08:48:49 +03:00
|
|
|
self.logger.info("Flashed Successfully")
|
2023-03-20 15:07:17 +03:00
|
|
|
except OTPException as e:
|
2021-10-21 15:24:34 +03:00
|
|
|
self.logger.exception(e)
|
2023-03-20 15:07:17 +03:00
|
|
|
return e.get_exit_code()
|
2022-12-27 15:59:36 +03:00
|
|
|
finally:
|
|
|
|
os.remove(filename)
|
2021-10-15 14:05:14 +03:00
|
|
|
|
2021-10-21 15:24:34 +03:00
|
|
|
return 0
|
2021-10-15 14:05:14 +03:00
|
|
|
|
|
|
|
def flash_all(self):
|
2023-05-03 08:48:49 +03:00
|
|
|
self.logger.info("Flashing OTP")
|
2021-07-22 05:49:23 +03:00
|
|
|
|
2021-11-23 19:13:34 +03:00
|
|
|
self._processFirstArgs()
|
|
|
|
self._processSecondArgs()
|
2021-10-15 14:05:14 +03:00
|
|
|
|
|
|
|
filename = f"otp_{self.args.name}_whole_{self.timestamp}.bin"
|
|
|
|
|
2021-07-22 05:49:23 +03:00
|
|
|
try:
|
2023-05-03 08:48:49 +03:00
|
|
|
self.logger.info("Packing binary data")
|
2022-06-26 15:00:03 +03:00
|
|
|
with open(filename, "wb") as file:
|
|
|
|
file.write(self._packFirst())
|
|
|
|
file.write(self._packSecond())
|
|
|
|
|
2023-05-03 08:48:49 +03:00
|
|
|
self.logger.info("Flashing OTP")
|
2022-12-27 15:59:36 +03:00
|
|
|
|
|
|
|
openocd = OpenOCDProgrammer(
|
|
|
|
self.args.interface,
|
|
|
|
self.args.port_base,
|
|
|
|
self.args.serial,
|
|
|
|
)
|
|
|
|
|
2023-03-20 15:07:17 +03:00
|
|
|
programmer_result = openocd.otp_write(0x1FFF7000, filename)
|
|
|
|
if programmer_result != OpenOCDProgrammerResult.Success:
|
|
|
|
raise OTPException("Failed to flash OTP", programmer_result)
|
2022-12-27 15:59:36 +03:00
|
|
|
|
2023-05-03 08:48:49 +03:00
|
|
|
self.logger.info("Flashed Successfully")
|
2023-03-20 15:07:17 +03:00
|
|
|
except OTPException as e:
|
2021-07-22 05:49:23 +03:00
|
|
|
self.logger.exception(e)
|
2023-03-20 15:07:17 +03:00
|
|
|
return e.get_exit_code()
|
2022-12-27 15:59:36 +03:00
|
|
|
finally:
|
|
|
|
os.remove(filename)
|
2021-10-21 15:24:34 +03:00
|
|
|
|
|
|
|
return 0
|
2021-07-22 05:49:23 +03:00
|
|
|
|
2021-06-23 17:58:44 +03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
Main()()
|