1# -*- coding: utf-8 -*-
2
3import os
4import sys
5import shutil
6import subprocess
7import time
8import platform
9
10# if debug_info=True, Debugging Print Information will be turned on
11debug_info=False
12# if make_fal=True, Partition tables are put into firmware
13make_fal=False
14# Setting firmware output directory
15out_path='./Bin'
16# Setting the bin file path
17bin_file='./rtthread.bin'
18# Setting winnermicro libraries path
19wmlib_path='./packages/wm_libraries-'
20# Setting the 1M flash layout file
21layout_1M_file='.'
22# Setting the 2M flash layout file
23layout_2M_file='.'
24# Setting the makeimg by adding rtt flash original fls
25makeimg_new_fls='.'
26
27def execute_command(cmdstring, cwd=None, shell=True):
28    """Execute the system command at the specified address."""
29
30    if shell:
31        cmdstring_list = cmdstring
32
33    sub = subprocess.Popen(cmdstring_list, cwd=cwd, stdin=subprocess.PIPE,
34                           stdout=subprocess.PIPE, shell=shell, bufsize=8192)
35
36    stdout_str = ""
37    while sub.poll() is None:
38        stdout_str += str(sub.stdout.read())
39        time.sleep(0.1)
40
41    return stdout_str
42
43def copy_file(name, path):
44    res = True
45    if os.path.exists(path):
46        shutil.copy(path, out_path)
47    else:
48        print('makeimg err! No ' + name + ' file found: ' + path)
49        res = False
50    return res
51
52def is_exists(name, path):
53    res = True
54    if not os.path.exists(path):
55        print('makeimg err! No ' + name + ' file found: ' + path)
56        res = False
57    return res
58
59def get_exec_path(path):
60    (file_path, file_name) = os.path.split(path)
61    (name, extend) = os.path.splitext(file_name)
62
63    exec_path = ''
64    if (platform.system() == "Windows"):
65        exec_path = os.path.abspath(file_path + '/' + name + '.exe')
66    elif (platform.system() == "Linux"):
67        exec_path = os.path.abspath(file_path + '/' + name)
68
69    if debug_info:
70        print('file_path: ' + file_path)
71        print('file_name: ' + file_name)
72        print('name: ' + name)
73        print('extend: ' + extend)
74
75    return exec_path
76
77def do_makeimg(tool_path, param):
78    str = "\"" + tool_path +  "\"" + ' ' + param
79    if debug_info:
80        print('exec cmd: ' + str);
81
82    execute_command(str)
83
84def get_wmlib_path_full(path):
85    (_wmlib_path,_wmlib_name) = os.path.split(path)
86    files = os.listdir(_wmlib_path)
87    for f in files:
88        if _wmlib_name in f:
89            return _wmlib_path + '/' + f
90    return path
91
92if __name__=='__main__':
93    # find winnermicro libraries full path
94    wmlib_path_full = get_wmlib_path_full(wmlib_path)
95    # Setting the version.txt file path
96    version_file=wmlib_path_full + '/Tools/version.txt'
97    # Setting the secboot.img file path
98    secboot_file=wmlib_path_full + '/Tools/secboot.img'
99    # Setting the wm_gzip.exe file path
100    wm_gzip_file=wmlib_path_full + '/Tools/wm_gzip.exe'
101    # Setting the makeimg.exe file path
102    makeimg_file=wmlib_path_full + '/Tools/makeimg.exe'
103    # Setting the makeimg_all.exe file path
104    makeimg_all_file=wmlib_path_full + '/Tools/makeimg_all.exe'
105
106    if (platform.system() == "Linux"):
107        wm_gzip_file=wmlib_path_full + '/Tools/wm_gzip.py'
108        makeimg_file=wmlib_path_full + '/Tools/makeimg'
109        makeimg_all_file=wmlib_path_full + '/Tools/makeimg_all'
110
111    # Get absolute path
112    out_path = os.path.abspath(out_path).replace('\\', '/');
113    bin_file = os.path.abspath(bin_file).replace('\\', '/');
114    version_file = os.path.abspath(version_file).replace('\\', '/');
115    secboot_file = os.path.abspath(secboot_file).replace('\\', '/');
116    wm_gzip_file = os.path.abspath(wm_gzip_file).replace('\\', '/');
117    makeimg_file = os.path.abspath(makeimg_file).replace('\\', '/');
118    makeimg_all_file = os.path.abspath(makeimg_all_file).replace('\\', '/');
119
120    # Create the output directory
121    if not os.path.exists(out_path): os.mkdir(out_path)
122
123    # Copy file
124    if not copy_file('bin', bin_file): exit(0)
125    if not copy_file('version', version_file): exit(0)
126    if not copy_file('secboot', secboot_file): exit(0)
127
128    # Check the existence of packaging tools
129    if not is_exists('wm_gzip', wm_gzip_file): exit(0)
130    if not is_exists('makeimg', makeimg_file): exit(0)
131    if not is_exists('makeimg_all', makeimg_all_file): exit(0)
132
133    # Get File Names and File Extensions
134    (bin_file_path,bin_file_name) = os.path.split(bin_file)
135    (bin_name,bin_extend) = os.path.splitext(bin_file_name)
136    (version_file_path,version_file_name) = os.path.split(version_file)
137    (secboot_file_path,secboot_file_name) = os.path.split(secboot_file)
138
139    # print debug Information
140    if debug_info: print('bin_file_name:' + bin_file_name + 'bin_name:' + bin_name + 'bin_extend:' + bin_extend + 'version_file_name:' + version_file_name + 'secboot_file_name:' + secboot_file_name)
141
142    print('makeimg 1M Flash...')
143    file_pos_1M='_1M'
144    gzip_param = "\"" + out_path + '/' + bin_file_name + "\""
145    make_img_param = "\"" + out_path + '/' + bin_file_name + "\"" + ' ' + "\"" + out_path + '/' + bin_name + file_pos_1M + '.img' + "\"" + ' 0' + ' 0' + ' ' + "\"" + out_path + '/' + version_file_name + "\"" + ' 90000' + ' 10100'
146    make_GZ_param = "\"" + out_path + '/' + bin_file_name + '.gz' + "\"" + ' ' + "\"" + out_path + '/' + bin_name + '_GZ' + file_pos_1M + '.img' +"\"" + ' 0' + ' 1' + ' ' + "\"" + out_path + '/' + version_file_name + "\"" + ' 90000' + ' 10100' + ' ' + "\"" + out_path + '/' + bin_file_name + "\""
147    make_SEC_param = "\"" + out_path + '/' + bin_file_name + "\"" + ' ' + "\"" + out_path + '/' + bin_name + '_SEC' + file_pos_1M + '.img' + "\"" + ' 0' + ' 0' + ' ' + "\"" + out_path + '/' + version_file_name + "\"" + ' 90000' + ' 10100'
148    make_FLS_param = "\"" + out_path + '/' + secboot_file_name + "\"" + ' ' + "\"" + out_path + '/' + bin_name + file_pos_1M + '.img' + "\"" + ' ' + "\"" + out_path + '/' + bin_name + file_pos_1M + '.FLS' + "\""
149
150    if debug_info:
151        print('gzip_param' + gzip_param)
152        print('make_img_param' + make_img_param)
153        print('make_GZ_param' + make_GZ_param)
154        print('make_SEC_param' + make_SEC_param)
155        print('make_FLS_param' + make_FLS_param)
156
157    if (platform.system() == "Linux"):
158        do_makeimg("python",wm_gzip_file + " " + gzip_param)
159    else:
160        do_makeimg(wm_gzip_file, gzip_param)
161    do_makeimg(makeimg_file, make_img_param)
162    do_makeimg(makeimg_file, make_GZ_param)
163    do_makeimg(makeimg_file, make_SEC_param)
164    do_makeimg(makeimg_all_file, make_FLS_param)
165
166    rm_file = out_path + '/' + bin_name + file_pos_1M + '.img'
167    if os.path.exists(rm_file):
168        os.remove(rm_file)
169    rm_file = out_path + '/' + bin_file_name + '.gz'
170    if os.path.exists(rm_file):
171        os.remove(rm_file)
172
173    print('makeimg 2M Flash...')
174    file_pos_2M='_2M'
175    gzip_param = "\"" + out_path + '/' + bin_file_name + "\""
176    make_img_param = "\"" + out_path + '/' + bin_file_name + "\"" + ' ' + "\"" + out_path + '/' + bin_name + file_pos_2M + '.img' + "\"" + ' 3' + ' 0' + ' ' + "\"" + out_path + '/' + version_file_name + "\"" + ' 100000' + ' 10100'
177    make_GZ_param = "\"" + out_path + '/' + bin_file_name + '.gz' + "\"" + ' ' + "\"" + out_path + '/' + bin_name + '_GZ' + file_pos_2M + '.img' +"\"" + ' 3' + ' 1' + ' ' + "\"" + out_path + '/' + version_file_name + "\"" + ' 100000' + ' 10100' + ' ' + "\"" + out_path + '/' + bin_file_name + "\""
178    make_SEC_param = "\"" + out_path + '/' + bin_file_name + "\"" + ' ' + "\"" + out_path + '/' + bin_name + '_SEC' + file_pos_2M + '.img' + "\"" + ' 3' + ' 0' + ' ' + "\"" + out_path + '/' + version_file_name + "\"" + ' 100000' + ' 10100'
179    make_FLS_param = "\"" + out_path + '/' + secboot_file_name + "\"" + ' ' + "\"" + out_path + '/' + bin_name + file_pos_2M + '.img' + "\"" + ' ' + "\"" + out_path + '/' + bin_name + file_pos_2M + '.FLS' + "\""
180
181    if debug_info:
182        print('gzip_param' + gzip_param)
183        print('make_img_param' + make_img_param)
184        print('make_GZ_param' + make_GZ_param)
185        print('make_SEC_param' + make_SEC_param)
186        print('make_FLS_param' + make_FLS_param)
187
188    if (platform.system() == "Linux"):
189        do_makeimg("python",wm_gzip_file + " " + gzip_param)
190    else:
191        do_makeimg(wm_gzip_file, gzip_param)
192    do_makeimg(makeimg_file, make_img_param)
193    do_makeimg(makeimg_file, make_GZ_param)
194    do_makeimg(makeimg_file, make_SEC_param)
195    do_makeimg(makeimg_all_file, make_FLS_param)
196
197    rm_file = out_path + '/' + bin_name + file_pos_2M + '.img'
198    if os.path.exists(rm_file):
199        os.remove(rm_file)
200    rm_file = out_path + '/' + bin_file_name + '.gz'
201    if os.path.exists(rm_file):
202        os.remove(rm_file)
203
204    if make_fal:
205        # Get absolute path
206        layout_1M_file = os.path.abspath(layout_1M_file).replace('\\', '/');
207        layout_2M_file = os.path.abspath(layout_2M_file).replace('\\', '/');
208        makeimg_new_fls = os.path.abspath(makeimg_new_fls).replace('\\', '/');
209
210        # Create command parameters to new fls
211        makeimg_new_cmd="\"" + out_path + '/' + bin_name + file_pos_1M + '.FLS' + "\"" + ' ' + "\"" + layout_1M_file + "\"" + ' ' + "\"" + out_path + '/'+ bin_name + '_layout' + file_pos_1M+'.FLS' +"\""
212        do_makeimg(makeimg_new_fls, makeimg_new_cmd)
213
214        makeimg_new_cmd="\"" + out_path + '/' + bin_name + file_pos_2M + '.FLS' + "\"" + ' ' + "\"" + layout_2M_file + "\"" + ' ' + "\"" + out_path + '/'+ bin_name + '_layout' + file_pos_2M+'.FLS' +"\""
215        do_makeimg(makeimg_new_fls, makeimg_new_cmd)
216
217        # Delete temporary files
218        rm_file = out_path + '/' + bin_name + file_pos_1M + '.FLS'
219        if os.path.exists(rm_file):
220            os.remove(rm_file)
221        rm_file = out_path + '/' + bin_name + file_pos_2M + '.FLS'
222        if os.path.exists(rm_file):
223            os.remove(rm_file)
224
225    print('end')
226