Skip to content

Commit 1fa08da

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 eedf4a6 commit 1fa08da

File tree

3 files changed

+185
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)