summaryrefslogtreecommitdiff
path: root/chip/mec1322/util/pack_ec.py
blob: 9062621c9afbf0d39427a635cb91467db5e41028 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env python3

# Copyright 2013 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# A script to pack EC binary into SPI flash image for MEC1322
# Based on MEC1322_ROM_Doc_Rev0.5.pdf.

import argparse
import hashlib
import os
import struct
import subprocess
import tempfile

LOAD_ADDR = 0x100000
HEADER_SIZE = 0x140
SPI_CLOCK_LIST = [48, 24, 12, 8]
SPI_READ_CMD_LIST = [0x3, 0xB, 0x3B]

CRC_TABLE = [
    0x00,
    0x07,
    0x0E,
    0x09,
    0x1C,
    0x1B,
    0x12,
    0x15,
    0x38,
    0x3F,
    0x36,
    0x31,
    0x24,
    0x23,
    0x2A,
    0x2D,
]


def Crc8(crc, data):
    """Update CRC8 value."""
    for v in data:
        crc = ((crc << 4) & 0xFF) ^ (CRC_TABLE[(crc >> 4) ^ (v >> 4)])
        crc = ((crc << 4) & 0xFF) ^ (CRC_TABLE[(crc >> 4) ^ (v & 0xF)])
    return crc ^ 0x55


def GetEntryPoint(payload_file):
    """Read entry point from payload EC image."""
    with open(payload_file, "rb") as f:
        f.seek(4)
        s = f.read(4)
    return struct.unpack("<I", s)[0]


def GetPayloadFromOffset(payload_file, offset):
    """Read payload and pad it to 64-byte aligned."""
    with open(payload_file, "rb") as f:
        f.seek(offset)
        payload = bytearray(f.read())
    rem_len = len(payload) % 64
    if rem_len:
        payload += b"\0" * (64 - rem_len)
    return payload


def GetPayload(payload_file):
    """Read payload and pad it to 64-byte aligned."""
    return GetPayloadFromOffset(payload_file, 0)


def GetPublicKey(pem_file):
    """Extract public exponent and modulus from PEM file."""
    result = subprocess.run(
        ["openssl", "rsa", "-in", pem_file, "-text", "-noout"],
        stdout=subprocess.PIPE,
        encoding="utf-8",
    )
    modulus_raw = []
    in_modulus = False
    for line in result.stdout.splitlines():
        if line.startswith("modulus"):
            in_modulus = True
        elif not line.startswith(" "):
            in_modulus = False
        elif in_modulus:
            modulus_raw.extend(line.strip().strip(":").split(":"))
        if line.startswith("publicExponent"):
            exp = int(line.split(" ")[1], 10)
    modulus_raw.reverse()
    modulus = bytearray((int(x, 16) for x in modulus_raw[:256]))
    return struct.pack("<Q", exp), modulus


def GetSpiClockParameter(args):
    assert args.spi_clock in SPI_CLOCK_LIST, (
        "Unsupported SPI clock speed %d MHz" % args.spi_clock
    )
    return SPI_CLOCK_LIST.index(args.spi_clock)


def GetSpiReadCmdParameter(args):
    assert args.spi_read_cmd in SPI_READ_CMD_LIST, (
        "Unsupported SPI read command 0x%x" % args.spi_read_cmd
    )
    return SPI_READ_CMD_LIST.index(args.spi_read_cmd)


def PadZeroTo(data, size):
    data.extend(b"\0" * (size - len(data)))


def BuildHeader(args, payload_len, rorofile):
    # Identifier and header version
    header = bytearray(b"CSMS\0")

    PadZeroTo(header, 0x6)
    header.append(GetSpiClockParameter(args))
    header.append(GetSpiReadCmdParameter(args))

    header.extend(struct.pack("<I", LOAD_ADDR))
    header.extend(struct.pack("<I", GetEntryPoint(rorofile)))
    header.append((payload_len >> 6) & 0xFF)
    header.append((payload_len >> 14) & 0xFF)
    PadZeroTo(header, 0x14)
    header.extend(struct.pack("<I", args.payload_offset))

    exp, modulus = GetPublicKey(args.payload_key)
    PadZeroTo(header, 0x20)
    header.extend(exp)
    PadZeroTo(header, 0x30)
    header.extend(modulus)
    PadZeroTo(header, HEADER_SIZE)

    return header


def SignByteArray(data, pem_file):
    hash_file = tempfile.mkstemp(prefix="pack_ec.")[1]
    sign_file = tempfile.mkstemp(prefix="pack_ec.")[1]
    try:
        with open(hash_file, "wb") as f:
            hasher = hashlib.sha256()
            hasher.update(data)
            f.write(hasher.digest())
        subprocess.run(
            [
                "openssl",
                "rsautl",
                "-sign",
                "-inkey",
                pem_file,
                "-keyform",
                "PEM",
                "-in",
                hash_file,
                "-out",
                sign_file,
            ],
            check=True,
        )
        with open(sign_file, "rb") as f:
            signed = f.read()
            return bytearray(reversed(signed))
    finally:
        os.remove(hash_file)
        os.remove(sign_file)


def BuildTag(args):
    tag = bytearray(
        [
            (args.header_loc >> 8) & 0xFF,
            (args.header_loc >> 16) & 0xFF,
            (args.header_loc >> 24) & 0xFF,
        ]
    )
    if args.chip_select != 0:
        tag[2] |= 0x80
    tag.append(Crc8(0, tag))
    return tag


def PacklfwRoImage(rorw_file, loader_file, image_size):
    """TODO:Clean up to get rid of Temp file and just use memory
    to save data"""
    """Create a temp file with the
  first image_size bytes from the rorw file and the
  bytes from the loader_file appended
  return the filename"""
    fo = tempfile.NamedTemporaryFile(delete=False)  # Need to keep file around
    with open(loader_file, "rb") as fin1:
        pro = fin1.read()
    fo.write(pro)
    with open(rorw_file, "rb") as fin:
        ro = fin.read(image_size)
    fo.write(ro)
    fo.close()
    return fo.name


def parseargs():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-i",
        "--input",
        help="EC binary to pack, usually ec.bin or ec.RO.flat.",
        metavar="EC_BIN",
        default="ec.bin",
    )
    parser.add_argument(
        "-o",
        "--output",
        help="Output flash binary file",
        metavar="EC_SPI_FLASH",
        default="ec.packed.bin",
    )
    parser.add_argument(
        "--header_key",
        help="PEM key file for signing header",
        default="rsakey_sign_header.pem",
    )
    parser.add_argument(
        "--payload_key",
        help="PEM key file for signing payload",
        default="rsakey_sign_payload.pem",
    )
    parser.add_argument(
        "--loader_file", help="EC loader binary", default="ecloader.bin"
    )
    parser.add_argument(
        "-s",
        "--spi_size",
        type=int,
        help="Size of the SPI flash in MB",
        default=4,
    )
    parser.add_argument(
        "-l",
        "--header_loc",
        type=int,
        help="Location of header in SPI flash",
        default=0x170000,
    )
    parser.add_argument(
        "-p",
        "--payload_offset",
        type=int,
        help="The offset of payload from the header",
        default=0x240,
    )
    parser.add_argument(
        "-r",
        "--rwpayload_loc",
        type=int,
        help="The offset of payload from the header",
        default=0x190000,
    )
    parser.add_argument(
        "-z",
        "--romstart",
        type=int,
        help="The first location to output of the rom",
        default=0,
    )
    parser.add_argument(
        "-c",
        "--chip_select",
        type=int,
        help="Chip select signal to use, either 0 or 1.",
        default=0,
    )
    parser.add_argument(
        "--spi_clock",
        type=int,
        help="SPI clock speed. 8, 12, 24, or 48 MHz.",
        default=24,
    )
    parser.add_argument(
        "--spi_read_cmd",
        type=int,
        help="SPI read command. 0x3, 0xB, or 0x3B.",
        default=0xB,
    )
    parser.add_argument(
        "--image_size",
        type=int,
        help="Size of a single image.",
        default=(96 * 1024),
    )
    return parser.parse_args()


def main():
    args = parseargs()

    spi_size = args.spi_size * 1024
    args.header_loc = spi_size - (128 * 1024)
    args.rwpayload_loc = spi_size - (256 * 1024)
    args.romstart = spi_size - (256 * 1024)

    spi_list = []

    rorofile = PacklfwRoImage(args.input, args.loader_file, args.image_size)
    payload = GetPayload(rorofile)
    payload_len = len(payload)
    payload_signature = SignByteArray(payload, args.payload_key)
    header = BuildHeader(args, payload_len, rorofile)
    header_signature = SignByteArray(header, args.header_key)
    tag = BuildTag(args)
    # truncate the RW to 128k
    payloadrw = GetPayloadFromOffset(args.input, args.image_size)[: 128 * 1024]
    os.remove(rorofile)  # clean up the temp file

    spi_list.append((args.header_loc, header, "header"))
    spi_list.append(
        (args.header_loc + HEADER_SIZE, header_signature, "header_signature")
    )
    spi_list.append((args.header_loc + args.payload_offset, payload, "payload"))
    spi_list.append(
        (
            args.header_loc + args.payload_offset + payload_len,
            payload_signature,
            "payload_signature",
        )
    )
    spi_list.append((spi_size - 256, tag, "tag"))
    spi_list.append((args.rwpayload_loc, payloadrw, "payloadrw"))

    spi_list = sorted(spi_list)

    with open(args.output, "wb") as f:
        addr = args.romstart
        for s in spi_list:
            assert addr <= s[0]
            if addr < s[0]:
                f.write(b"\xff" * (s[0] - addr))
                addr = s[0]
            f.write(s[1])
            addr += len(s[1])
        if addr < spi_size:
            f.write(b"\xff" * (spi_size - addr))


if __name__ == "__main__":
    main()