makeimg.py 9.6 KB

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