Welcome to mirror list, hosted at ThFree Co, Russian Federation.

otp.py « scripts - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7b2378d12053057d3a16630e13791c872cc99472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python3

import logging
import argparse
import subprocess
import os
import sys
import re
import struct
import datetime

OTP_MAGIC = 0xBABE
OTP_VERSION = 0x02
OTP_RESERVED = 0x00

OTP_COLORS = {
    "unknown": 0x00,
    "black": 0x01,
    "white": 0x02,
}

OTP_REGIONS = {
    "unknown": 0x00,
    "eu_ru": 0x01,
    "us_ca_au": 0x02,
    "jp": 0x03,
}

OTP_DISPLAYS = {
    "unknown": 0x00,
    "erc": 0x01,
    "mgg": 0x02,
}

from flipper.app import App
from flipper.cube import CubeProgrammer


class Main(App):
    def init(self):
        # SubParsers
        self.subparsers = self.parser.add_subparsers(help="sub-command help")
        # Generate All
        self.parser_generate_all = self.subparsers.add_parser(
            "generate", help="Generate OTP binary"
        )
        self._addFirstArgs(self.parser_generate_all)
        self._addSecondArgs(self.parser_generate_all)
        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"
        )
        self._addArgsSWD(self.parser_flash_first)
        self._addFirstArgs(self.parser_flash_first)
        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"
        )
        self._addArgsSWD(self.parser_flash_second)
        self._addSecondArgs(self.parser_flash_second)
        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"
        )
        self._addArgsSWD(self.parser_flash_all)
        self._addFirstArgs(self.parser_flash_all)
        self._addSecondArgs(self.parser_flash_all)
        self.parser_flash_all.set_defaults(func=self.flash_all)
        # logging
        self.logger = logging.getLogger()
        self.timestamp = datetime.datetime.now().timestamp()

    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,
        }

    def _addFirstArgs(self, parser):
        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)

    def _addSecondArgs(self, parser):
        parser.add_argument("--color", type=str, help="Color", required=True)
        parser.add_argument("--region", type=str, help="Region", required=True)
        parser.add_argument("--name", type=str, help="Name", required=True)

    def _processFirstArgs(self):
        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]

    def _processSecondArgs(self):
        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]

        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]

        if len(self.args.name) > 8:
            self.parser.error("Name is too long. Max 8 symbols.")
        if re.match(r"^[a-zA-Z0-9.]+$", self.args.name) is None:
            self.parser.error(
                "Name contains incorrect symbols. Only a-zA-Z0-9 allowed."
            )

    def _packFirst(self):
        return struct.pack(
            "<" "HBBL" "BBBBBBH",
            OTP_MAGIC,
            OTP_VERSION,
            OTP_RESERVED,
            int(self.timestamp),
            self.args.version,
            self.args.firmware,
            self.args.body,
            self.args.connect,
            self.args.display,
            OTP_RESERVED,
            OTP_RESERVED,
        )

    def _packSecond(self):
        return struct.pack(
            "<" "BBHL" "8s",
            self.args.color,
            self.args.region,
            OTP_RESERVED,
            OTP_RESERVED,
            self.args.name.encode("ascii"),
        )

    def generate_all(self):
        self.logger.info(f"Generating OTP")
        self._processFirstArgs()
        self._processSecondArgs()
        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())
        self.logger.info(
            f"Generated files: {self.args.file}_first.bin and {self.args.file}_second.bin"
        )

        return 0

    def flash_first(self):
        self.logger.info(f"Flashing first block of OTP")

        self._processFirstArgs()

        filename = f"otp_unknown_first_{self.timestamp}.bin"

        try:
            self.logger.info(f"Packing binary data")
            with open(filename, "wb") as file:
                file.write(self._packFirst())

            self.logger.info(f"Flashing OTP")
            cp = CubeProgrammer(self._getCubeParams())
            cp.flashBin("0x1FFF7000", filename)
            cp.resetTarget()
            self.logger.info(f"Flashed Successfully")
            os.remove(filename)
        except Exception as e:
            self.logger.exception(e)
            return 1

        return 0

    def flash_second(self):
        self.logger.info(f"Flashing second block of OTP")

        self._processSecondArgs()

        filename = f"otp_{self.args.name}_second_{self.timestamp}.bin"

        try:
            self.logger.info(f"Packing binary data")
            with open(filename, "wb") as file:
                file.write(self._packSecond())

            self.logger.info(f"Flashing OTP")
            cp = CubeProgrammer(self._getCubeParams())
            cp.flashBin("0x1FFF7010", filename)
            cp.resetTarget()
            self.logger.info(f"Flashed Successfully")
            os.remove(filename)
        except Exception as e:
            self.logger.exception(e)
            return 1

        return 0

    def flash_all(self):
        self.logger.info(f"Flashing OTP")

        self._processFirstArgs()
        self._processSecondArgs()

        filename = f"otp_{self.args.name}_whole_{self.timestamp}.bin"

        try:
            self.logger.info(f"Packing binary data")
            with open(filename, "wb") as file:
                file.write(self._packFirst())
                file.write(self._packSecond())

            self.logger.info(f"Flashing OTP")
            cp = CubeProgrammer(self._getCubeParams())
            cp.flashBin("0x1FFF7000", filename)
            cp.resetTarget()
            self.logger.info(f"Flashed Successfully")
            os.remove(filename)
        except Exception as e:
            self.logger.exception(e)
            return 1

        return 0


if __name__ == "__main__":
    Main()()