otp.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. from flipper.app import App
  30. from flipper.cube import CubeProgrammer
  31. class Main(App):
  32. def init(self):
  33. # SubParsers
  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 _add_swd_args(self, parser):
  69. parser.add_argument(
  70. "--port", type=str, help="Port to connect: swd or usb1", default="swd"
  71. )
  72. def _add_first_args(self, parser):
  73. parser.add_argument("--version", type=int, help="Version", required=True)
  74. parser.add_argument("--firmware", type=int, help="Firmware", required=True)
  75. parser.add_argument("--body", type=int, help="Body", required=True)
  76. parser.add_argument("--connect", type=int, help="Connect", required=True)
  77. parser.add_argument("--display", type=str, help="Display", required=True)
  78. def _add_second_args(self, parser):
  79. parser.add_argument("--color", type=str, help="Color", required=True)
  80. parser.add_argument("--region", type=str, help="Region", required=True)
  81. parser.add_argument("--name", type=str, help="Name", required=True)
  82. def _process_first_args(self):
  83. if self.args.display not in OTP_DISPLAYS:
  84. self.parser.error(f"Invalid display. Use one of {OTP_DISPLAYS.keys()}")
  85. self.args.display = OTP_DISPLAYS[self.args.display]
  86. def _process_second_args(self):
  87. if self.args.color not in OTP_COLORS:
  88. self.parser.error(f"Invalid color. Use one of {OTP_COLORS.keys()}")
  89. self.args.color = OTP_COLORS[self.args.color]
  90. if self.args.region not in OTP_REGIONS:
  91. self.parser.error(f"Invalid region. Use one of {OTP_REGIONS.keys()}")
  92. self.args.region = OTP_REGIONS[self.args.region]
  93. if len(self.args.name) > 8:
  94. self.parser.error("Name is too long. Max 8 symbols.")
  95. if re.match(r"^[a-zA-Z0-9.]+$", self.args.name) is None:
  96. self.parser.error(
  97. "Name contains incorrect symbols. Only a-zA-Z0-9 allowed."
  98. )
  99. def _pack_first(self):
  100. return struct.pack(
  101. "<" "HBBL" "BBBBBBH",
  102. OTP_MAGIC,
  103. OTP_VERSION,
  104. OTP_RESERVED,
  105. int(self.timestamp),
  106. self.args.version,
  107. self.args.firmware,
  108. self.args.body,
  109. self.args.connect,
  110. self.args.display,
  111. OTP_RESERVED,
  112. OTP_RESERVED,
  113. )
  114. def _pack_second(self):
  115. return struct.pack(
  116. "<" "BBHL" "8s",
  117. self.args.color,
  118. self.args.region,
  119. OTP_RESERVED,
  120. OTP_RESERVED,
  121. self.args.name.encode("ascii"),
  122. )
  123. def generate_all(self):
  124. self.logger.info(f"Generating OTP")
  125. self._process_first_args()
  126. self._process_second_args()
  127. open(f"{self.args.file}_first.bin", "wb").write(self._pack_first())
  128. open(f"{self.args.file}_second.bin", "wb").write(self._pack_second())
  129. self.logger.info(
  130. f"Generated files: {self.args.file}_first.bin and {self.args.file}_second.bin"
  131. )
  132. return 0
  133. def flash_first(self):
  134. self.logger.info(f"Flashing first block of OTP")
  135. self._process_first_args()
  136. filename = f"otp_unknown_first_{self.timestamp}.bin"
  137. try:
  138. self.logger.info(f"Packing binary data")
  139. file = open(filename, "wb")
  140. file.write(self._pack_first())
  141. file.close()
  142. self.logger.info(f"Flashing OTP")
  143. cp = CubeProgrammer(self.args.port)
  144. cp.flashBin("0x1FFF7000", filename)
  145. cp.resetTarget()
  146. self.logger.info(f"Flashed Successfully")
  147. os.remove(filename)
  148. except Exception as e:
  149. self.logger.exception(e)
  150. return 1
  151. return 0
  152. def flash_second(self):
  153. self.logger.info(f"Flashing second block of OTP")
  154. self._process_second_args()
  155. filename = f"otp_{self.args.name}_second_{self.timestamp}.bin"
  156. try:
  157. self.logger.info(f"Packing binary data")
  158. file = open(filename, "wb")
  159. file.write(self._pack_second())
  160. file.close()
  161. self.logger.info(f"Flashing OTP")
  162. cp = CubeProgrammer(self.args.port)
  163. cp.flashBin("0x1FFF7010", filename)
  164. cp.resetTarget()
  165. self.logger.info(f"Flashed Successfully")
  166. os.remove(filename)
  167. except Exception as e:
  168. self.logger.exception(e)
  169. return 1
  170. return 0
  171. def flash_all(self):
  172. self.logger.info(f"Flashing OTP")
  173. self._process_first_args()
  174. self._process_second_args()
  175. filename = f"otp_{self.args.name}_whole_{self.timestamp}.bin"
  176. try:
  177. self.logger.info(f"Packing binary data")
  178. file = open(filename, "wb")
  179. file.write(self._pack_first())
  180. file.write(self._pack_second())
  181. file.close()
  182. self.logger.info(f"Flashing OTP")
  183. cp = CubeProgrammer(self.args.port)
  184. cp.flashBin("0x1FFF7000", filename)
  185. cp.resetTarget()
  186. self.logger.info(f"Flashed Successfully")
  187. os.remove(filename)
  188. except Exception as e:
  189. self.logger.exception(e)
  190. return 1
  191. return 0
  192. if __name__ == "__main__":
  193. Main()()