Skip to content

Commit a5be747

Browse files
dylanHsieh4963kartben
authored andcommitted
soc: realtek: rts5912 image tool add SHA2 tag
Let rts5912 image tool to add SHA2 tag at the tail end of image. Signed-off-by: Dylan Hsieh <[email protected]>
1 parent f922014 commit a5be747

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

soc/realtek/ec/common/rts5912_imgtool/img_gen.py

+35-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"""
1111

1212
import argparse
13+
import hashlib
1314
import os
1415
import struct
1516

@@ -24,6 +25,11 @@
2425
DUAL_read = "0x3B"
2526
QUAD_read = "0x6B"
2627

28+
TAG_MAGIC = 0x6907
29+
TAG_LEN = 0x0028
30+
TAG_TYPE_SHA = 0x0010
31+
TAG_SHA_LEN = 0x0020
32+
2733

2834
def parse_args():
2935
"""
@@ -88,10 +94,10 @@ def img_gen(load_addr, spi_freq, spi_rdcmd, original_bin, signed_bin):
8894
To obtain the RTS5912 image header and output a new BIN file
8995
"""
9096
img_size = os.path.getsize(original_bin)
91-
payload = bytearray(0)
92-
img_size = os.path.getsize(original_bin)
97+
hdr_payload = bytearray(0)
98+
tag_payload = bytearray(0)
9399

94-
fmt = (
100+
hdr_fmt = (
95101
"<"
96102
+
97103
# type ImageHdr struct {
@@ -108,8 +114,8 @@ def img_gen(load_addr, spi_freq, spi_rdcmd, original_bin, signed_bin):
108114
+ "H" # reserved uint16
109115
) # }
110116

111-
header = struct.pack(
112-
fmt,
117+
hdr = struct.pack(
118+
hdr_fmt,
113119
IMAGE_MAGIC,
114120
load_addr,
115121
IMAGE_HDR_SIZE,
@@ -123,15 +129,37 @@ def img_gen(load_addr, spi_freq, spi_rdcmd, original_bin, signed_bin):
123129
0,
124130
)
125131

126-
payload[: len(header)] = header
132+
hdr_payload[: len(hdr)] = hdr
133+
134+
tag_fmt = (
135+
"<"
136+
+
137+
# type ImageTag struct {
138+
"H" # Magic uint16
139+
+ "H" # TagSz uint16
140+
+ "H" # TagHash uint16
141+
+ "H" # HashSz uint16
142+
) # }
143+
144+
tag = struct.pack(tag_fmt, TAG_MAGIC, TAG_LEN, TAG_TYPE_SHA, TAG_SHA_LEN)
145+
146+
tag_payload[: len(tag)] = tag
147+
148+
sha = hashlib.sha256()
149+
sha.update(hdr_payload)
150+
with open(original_bin, "rb") as original:
151+
sha.update(original.read())
152+
digest = sha.digest()
127153

128154
with open(signed_bin, "wb") as signed:
129-
signed.write(payload)
155+
signed.write(hdr_payload)
130156
signed.flush()
131157
signed.close()
132158

133159
with open(signed_bin, "ab") as signed, open(original_bin, "rb") as original:
134160
signed.write(original.read())
161+
signed.write(tag_payload)
162+
signed.write(digest)
135163
signed.flush()
136164
signed.close()
137165
original.close()

0 commit comments

Comments
 (0)