otp.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/usr/bin/env python3
  2. import logging
  3. import argparse
  4. import subprocess
  5. import os
  6. import sys
  7. import re
  8. import struct
  9. import datetime
  10. OTP_MAGIC = 0xBABE
  11. OTP_VERSION = 0x02
  12. OTP_RESERVED = 0x00
  13. OTP_COLORS = {
  14. "unknown": 0x00,
  15. "black": 0x01,
  16. "white": 0x02,
  17. }
  18. OTP_REGIONS = {
  19. "unknown": 0x00,
  20. "eu_ru": 0x01,
  21. "us_ca_au": 0x02,
  22. "jp": 0x03,
  23. }
  24. OTP_DISPLAYS = {
  25. "unknown": 0x00,
  26. "erc": 0x01,
  27. "mgg": 0x02,
  28. }
  29. class Main:
  30. def __init__(self):
  31. # command args
  32. self.parser = argparse.ArgumentParser()
  33. self.parser.add_argument("-d", "--debug", action="store_true", help="Debug")
  34. self.subparsers = self.parser.add_subparsers(help="sub-command help")
  35. # Generate All
  36. self.parser_generate_all = self.subparsers.add_parser(
  37. "generate", help="Generate OTP binary"
  38. )
  39. self._add_first_args(self.parser_generate_all)
  40. self._add_second_args(self.parser_generate_all)
  41. self.parser_generate_all.add_argument("file", help="Output file")
  42. self.parser_generate_all.set_defaults(func=self.generate_all)
  43. # Flash First
  44. self.parser_flash_first = self.subparsers.add_parser(
  45. "flash_first", help="Flash first block of OTP to device"
  46. )
  47. self._add_swd_args(self.parser_flash_first)
  48. self._add_first_args(self.parser_flash_first)
  49. self.parser_flash_first.set_defaults(func=self.flash_first)
  50. # Flash Second
  51. self.parser_flash_second = self.subparsers.add_parser(
  52. "flash_second", help="Flash second block of OTP to device"
  53. )
  54. self._add_swd_args(self.parser_flash_second)
  55. self._add_second_args(self.parser_flash_second)
  56. self.parser_flash_second.set_defaults(func=self.flash_second)
  57. # Flash All
  58. self.parser_flash_all = self.subparsers.add_parser(
  59. "flash_all", help="Flash OTP to device"
  60. )
  61. self._add_swd_args(self.parser_flash_all)
  62. self._add_first_args(self.parser_flash_all)
  63. self._add_second_args(self.parser_flash_all)
  64. self.parser_flash_all.set_defaults(func=self.flash_all)
  65. # logging
  66. self.logger = logging.getLogger()
  67. self.timestamp = datetime.datetime.now().timestamp()
  68. def __call__(self):
  69. self.args = self.parser.parse_args()
  70. if "func" not in self.args:
  71. self.parser.error("Choose something to do")
  72. # configure log output
  73. self.log_level = logging.DEBUG if self.args.debug else logging.INFO
  74. self.logger.setLevel(self.log_level)
  75. self.handler = logging.StreamHandler(sys.stdout)
  76. self.handler.setLevel(self.log_level)
  77. self.formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s")
  78. self.handler.setFormatter(self.formatter)
  79. self.logger.addHandler(self.handler)
  80. # execute requested function
  81. self.args.func()
  82. def _add_swd_args(self, parser):
  83. parser.add_argument(
  84. "--port", type=str, help="Port to connect: swd or usb1", default="swd"
  85. )
  86. def _add_first_args(self, parser):
  87. parser.add_argument("--version", type=int, help="Version", required=True)
  88. parser.add_argument("--firmware", type=int, help="Firmware", required=True)
  89. parser.add_argument("--body", type=int, help="Body", required=True)
  90. parser.add_argument("--connect", type=int, help="Connect", required=True)
  91. parser.add_argument("--display", type=str, help="Display", required=True)
  92. def _add_second_args(self, parser):
  93. parser.add_argument("--color", type=str, help="Color", required=True)
  94. parser.add_argument("--region", type=str, help="Region", required=True)
  95. parser.add_argument("--name", type=str, help="Name", required=True)
  96. def _process_first_args(self):
  97. if self.args.display not in OTP_DISPLAYS:
  98. self.parser.error(f"Invalid display. Use one of {OTP_DISPLAYS.keys()}")
  99. self.args.display = OTP_DISPLAYS[self.args.display]
  100. def _process_second_args(self):
  101. if self.args.color not in OTP_COLORS:
  102. self.parser.error(f"Invalid color. Use one of {OTP_COLORS.keys()}")
  103. self.args.color = OTP_COLORS[self.args.color]
  104. if self.args.region not in OTP_REGIONS:
  105. self.parser.error(f"Invalid region. Use one of {OTP_REGIONS.keys()}")
  106. self.args.region = OTP_REGIONS[self.args.region]
  107. if len(self.args.name) > 8:
  108. self.parser.error("Name is too long. Max 8 symbols.")
  109. if re.match(r"^[a-zA-Z0-9.]+$", self.args.name) is None:
  110. self.parser.error(
  111. "Name contains incorrect symbols. Only a-zA-Z0-9 allowed."
  112. )
  113. def _pack_first(self):
  114. return struct.pack(
  115. "<" "HBBL" "BBBBBBH",
  116. OTP_MAGIC,
  117. OTP_VERSION,
  118. OTP_RESERVED,
  119. int(self.timestamp),
  120. self.args.version,
  121. self.args.firmware,
  122. self.args.body,
  123. self.args.connect,
  124. self.args.display,
  125. OTP_RESERVED,
  126. OTP_RESERVED,
  127. )
  128. def _pack_second(self):
  129. return struct.pack(
  130. "<" "BBHL" "8s",
  131. self.args.color,
  132. self.args.region,
  133. OTP_RESERVED,
  134. OTP_RESERVED,
  135. self.args.name.encode("ascii"),
  136. )
  137. def generate_all(self):
  138. self.logger.debug(f"Generating OTP")
  139. self._process_first_args()
  140. self._process_second_args()
  141. open(f"{self.args.file}_first.bin", "wb").write(self._pack_first())
  142. open(f"{self.args.file}_second.bin", "wb").write(self._pack_second())
  143. def flash_first(self):
  144. self.logger.debug(f"Flashing first block of OTP")
  145. self._process_first_args()
  146. filename = f"otp_unknown_first_{self.timestamp}.bin"
  147. file = open(filename, "wb")
  148. file.write(self._pack_first())
  149. file.close()
  150. self._flash_bin("0x1FFF7000", filename)
  151. os.remove(filename)
  152. def flash_second(self):
  153. self.logger.debug(f"Flashing second block of OTP")
  154. self._process_second_args()
  155. filename = f"otp_{self.args.name}_second_{self.timestamp}.bin"
  156. file = open(filename, "wb")
  157. file.write(self._pack_second())
  158. file.close()
  159. self._flash_bin("0x1FFF7010", filename)
  160. os.remove(filename)
  161. def flash_all(self):
  162. self.logger.debug(f"Flashing OTP")
  163. self._process_first_args()
  164. self._process_second_args()
  165. filename = f"otp_{self.args.name}_whole_{self.timestamp}.bin"
  166. file = open(filename, "wb")
  167. file.write(self._pack_first())
  168. file.write(self._pack_second())
  169. file.close()
  170. self._flash_bin("0x1FFF7000", filename)
  171. os.remove(filename)
  172. def _flash_bin(self, address, filename):
  173. self.logger.debug(f"Programming {filename} at {address}")
  174. try:
  175. output = subprocess.check_output(
  176. [
  177. "STM32_Programmer_CLI",
  178. "-q",
  179. "-c",
  180. f"port={self.args.port}",
  181. "-d",
  182. filename,
  183. f"{address}",
  184. ]
  185. )
  186. assert output
  187. self.logger.info(f"Success")
  188. except subprocess.CalledProcessError as e:
  189. self.logger.error(e.output.decode())
  190. self.logger.error(f"Failed to call STM32_Programmer_CLI")
  191. return
  192. except Exception as e:
  193. self.logger.error(f"Failed to call STM32_Programmer_CLI")
  194. self.logger.exception(e)
  195. return
  196. # reboot
  197. subprocess.check_output(
  198. [
  199. "STM32_Programmer_CLI",
  200. "-q",
  201. "-c",
  202. f"port={self.args.port}",
  203. ]
  204. )
  205. if __name__ == "__main__":
  206. Main()()