Skip to content

Commit bfa2863

Browse files
RtkFPcaiyi_zhong
authored andcommitted
soc: realtek: Add image generate tool for RTS5817
Add tool to genarate images of RTS5817 after post build stage Signed-off-by: Darcy Lu <darcy_lu@realsil.com.cn>
1 parent 087cd2e commit bfa2863

File tree

3 files changed

+189
-0
lines changed

3 files changed

+189
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright (c) 2024 Realtek Semiconductor, Inc.
22
# SPDX-License-Identifier: Apache-2.0
33

4+
add_subdirectory(common)
45
add_subdirectory(${SOC_SERIES})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (c) 2024 Realtek Semiconductor, Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
zephyr_include_directories(.)
5+
6+
set(RTS_BIN_NAME ${CONFIG_KERNEL_BIN_NAME}.rts5817.bin)
7+
string(TOUPPER "${SOC_NAME}" soc_name_upper)
8+
9+
if(APPVERSION)
10+
set(VERSION ${APPVERSION})
11+
else()
12+
set(VERSION 0)
13+
endif()
14+
15+
set_property(GLOBAL APPEND PROPERTY extra_post_build_commands
16+
COMMAND ${PYTHON_EXECUTABLE} ${SOC_${soc_name_upper}_DIR}/common/tool/gen_download_bin.py
17+
-i ${KERNEL_BIN_NAME}
18+
-o ${RTS_BIN_NAME}
19+
-v ${VERSION}
20+
)
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (c) 2024 Realtek Semiconductor, Inc.
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
import argparse
8+
import hashlib
9+
import pathlib
10+
import subprocess
11+
import sys
12+
13+
PATCH_FLAG = 0x3
14+
PATCH_FLAG_OFFSET = 0x0
15+
PATCH_FLAG_LEN = 0x4
16+
PATCH_IMG_SIZE_OFFSET = 0x4
17+
PATCH_IMG_SIZE_LEN = 0x2
18+
19+
PATCH_CONFIG_LEN = 0x50
20+
PATCH_RESERVED_LEN = 0xF70
21+
PATCH_SHA_LEN = 0x20
22+
PATCH_PADDING_LEN = 0x20
23+
24+
IMG_HEADER_FLAG = 0x2
25+
IMG_HEADER_FLAG_OFFSET = 0x0
26+
IMG_HEADER_FLAG_LEN = 0x4
27+
IMG_HEADER_VER_OFFSET = 0x4
28+
IMG_HEADER_VER_LEN = 0x4
29+
IMG_HEADER_RESERVED_LEN = 0xF8
30+
31+
IMG_SIZE_UNIT = 0x1000
32+
IMG_SHA_LEN = 0x20
33+
IMG_REAR_PADDING_LEN = 0x60
34+
35+
36+
def create_parser(arg_list):
37+
"""create argument parser according to pre-defined arguments
38+
39+
:param arg_list: when empty, parses command line arguments,
40+
else parses the given string
41+
"""
42+
parser = argparse.ArgumentParser(conflict_handler='resolve', allow_abbrev=False)
43+
parser.add_argument("--input", "-i", nargs='?', dest="input")
44+
parser.add_argument("--output", "-o", nargs='?', dest="output")
45+
parser.add_argument("--version", "-v", type=lambda x: int(x, 16), dest="version")
46+
47+
args = parser.parse_known_args(arg_list.split())
48+
49+
if arg_list == "":
50+
args = parser.parse_known_args()
51+
52+
return args
53+
54+
55+
def file_check(input, output=None):
56+
"""check input and output files
57+
If the input file is not exists or empty, raise error
58+
59+
:param input: the input file object
60+
:param output: the output file object
61+
"""
62+
if not input.exists():
63+
raise RuntimeError(f'Input file ({input}) is not exists')
64+
elif input.stat().st_size == 0:
65+
raise RuntimeError(f'Input file ({input}) is empty')
66+
67+
if output is None:
68+
output = ['out_', input]
69+
70+
if output.exists():
71+
if output.samefile(input):
72+
raise RuntimeError(f'Input file {input} should be different from Output file {output}')
73+
output.unlink()
74+
75+
output.touch()
76+
return output
77+
78+
79+
def gen_rompatch(img_len):
80+
"""generate rom patch for image
81+
82+
:param img_len: the length of image
83+
"""
84+
patch_config = bytearray([0x0] * PATCH_CONFIG_LEN)
85+
patch_config[PATCH_FLAG_OFFSET] = PATCH_FLAG
86+
patch_config[PATCH_IMG_SIZE_OFFSET] = img_len
87+
patch_reserved = bytearray([0xFF] * PATCH_RESERVED_LEN)
88+
patch = patch_config + patch_reserved
89+
patch_sha = hashlib.sha256(patch).digest()
90+
patch += patch_sha
91+
patch_padding = bytearray([0xFF] * PATCH_PADDING_LEN)
92+
patch += patch_padding
93+
return patch
94+
95+
96+
def gen_img_header(img_version=0):
97+
"""generate rom patch for image
98+
99+
:param img_version: the version of image
100+
"""
101+
img_header = IMG_HEADER_FLAG.to_bytes(IMG_HEADER_FLAG_LEN, "little")
102+
img_header += img_version.to_bytes(IMG_HEADER_VER_LEN, "little")
103+
img_header += bytearray([0xFF] * IMG_HEADER_RESERVED_LEN)
104+
return img_header
105+
106+
107+
def img_padding(img):
108+
size = len(img) // IMG_SIZE_UNIT
109+
rear = len(img) % IMG_SIZE_UNIT
110+
111+
if rear <= IMG_SIZE_UNIT - IMG_SHA_LEN - IMG_REAR_PADDING_LEN:
112+
size += 1
113+
padding_len = IMG_SIZE_UNIT - rear - IMG_SHA_LEN - IMG_REAR_PADDING_LEN
114+
else:
115+
size += 2
116+
padding_len = 2 * IMG_SIZE_UNIT - rear - IMG_SHA_LEN - IMG_REAR_PADDING_LEN
117+
118+
img_with_padding = img + bytes([0xFF] * padding_len)
119+
return size, img_with_padding
120+
121+
122+
def main():
123+
"""main of the application"""
124+
125+
if len(sys.argv) < 3:
126+
sys.exit()
127+
128+
input_file = None
129+
output_file = None
130+
fw_version = 0x0
131+
132+
# parser input arguments
133+
arguments = create_parser("")
134+
for arg in vars(arguments[0]):
135+
if (arg == "input") & (arguments[0].input is not None):
136+
input_file = arguments[0].input
137+
elif (arg == "output") & (arguments[0].output is not None):
138+
output_file = arguments[0].output
139+
elif (arg == "version") & (arguments[0].version is not None):
140+
fw_version = arguments[0].version
141+
142+
# check file
143+
output_file = file_check(pathlib.Path(input_file), pathlib.Path(output_file))
144+
145+
# generate image header
146+
header = gen_img_header(fw_version)
147+
148+
with open(input_file, 'rb') as origin_img_file:
149+
# start handling
150+
origin_img = origin_img_file.read()
151+
img_with_header = header + origin_img
152+
img_size, img_with_padding = img_padding(img_with_header)
153+
img_sha = hashlib.sha256(img_with_padding).digest()
154+
img_with_sha = img_with_padding + img_sha
155+
rompatch = gen_rompatch(img_size)
156+
final_img = rompatch + img_with_sha + bytes([0xFF] * IMG_REAR_PADDING_LEN)
157+
origin_img_file.close()
158+
159+
with open(output_file, 'wb') as new_fw:
160+
new_fw.write(final_img)
161+
new_fw.close()
162+
163+
# add dfu suffix
164+
subprocess.check_call(['dfu-suffix', '-a', output_file])
165+
166+
167+
if __name__ == '__main__':
168+
main()

0 commit comments

Comments
 (0)